1230557Sjimharris/*- 2230557Sjimharris * BSD LICENSE 3230557Sjimharris * 4230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 5230557Sjimharris * All rights reserved. 6230557Sjimharris * 7230557Sjimharris * Redistribution and use in source and binary forms, with or without 8230557Sjimharris * modification, are permitted provided that the following conditions 9230557Sjimharris * are met: 10230557Sjimharris * 11230557Sjimharris * * Redistributions of source code must retain the above copyright 12230557Sjimharris * notice, this list of conditions and the following disclaimer. 13230557Sjimharris * * Redistributions in binary form must reproduce the above copyright 14230557Sjimharris * notice, this list of conditions and the following disclaimer in 15230557Sjimharris * the documentation and/or other materials provided with the 16230557Sjimharris * distribution. 17230557Sjimharris * 18230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29230557Sjimharris */ 30230557Sjimharris 31230557Sjimharris#include <sys/cdefs.h> 32230557Sjimharris__FBSDID("$FreeBSD$"); 33230557Sjimharris 34230557Sjimharris#include <dev/isci/isci.h> 35230557Sjimharris 36230557Sjimharris#include <dev/isci/scil/scif_user_callback.h> 37230557Sjimharris 38230557Sjimharrisstatic void 39230557Sjimharrisisci_timer_timeout(void *arg) 40230557Sjimharris{ 41230557Sjimharris struct ISCI_TIMER *timer = (struct ISCI_TIMER *)arg; 42230557Sjimharris 43230557Sjimharris isci_log_message(3, "TIMER", "timeout %p\n", timer); 44230557Sjimharris 45230557Sjimharris /* callout_stop() will *not* keep the timer from running if it is 46230557Sjimharris * pending. callout_drain() cannot be called from interrupt context, 47230557Sjimharris * because it may cause thread to sleep which is not allowed in 48230557Sjimharris * interrupt context. So instead, check the is_started flag to see if 49230557Sjimharris * the timer routine should actually be run or not. 50230557Sjimharris */ 51230557Sjimharris if (timer->is_started == TRUE) 52230557Sjimharris timer->callback(timer->cookie); 53230557Sjimharris} 54230557Sjimharris 55230557Sjimharris/** 56230557Sjimharris * @brief This callback method asks the user to start the supplied timer. 57230557Sjimharris * 58230557Sjimharris * @warning All timers in the system started by the SCI Framework are one 59230557Sjimharris * shot timers. Therefore, the SCI user should make sure that it 60230557Sjimharris * removes the timer from it's list when a timer actually fires. 61230557Sjimharris * Additionally, SCI Framework user's should be able to handle 62230557Sjimharris * calls from the SCI Framework to stop a timer that may already 63230557Sjimharris * be stopped. 64230557Sjimharris * 65230557Sjimharris * @param[in] controller This parameter specifies the controller with 66230557Sjimharris * which this timer is to associated. 67230557Sjimharris * @param[in] timer This parameter specifies the timer to be started. 68230557Sjimharris * @param[in] milliseconds This parameter specifies the number of 69230557Sjimharris * milliseconds for which to stall. The operating system driver 70230557Sjimharris * is allowed to round this value up where necessary. 71230557Sjimharris * 72230557Sjimharris * @return none 73230557Sjimharris */ 74230557Sjimharrisvoid 75230557Sjimharrisscif_cb_timer_start(SCI_CONTROLLER_HANDLE_T controller, void *timer, 76230557Sjimharris uint32_t milliseconds) 77230557Sjimharris{ 78230557Sjimharris struct ISCI_TIMER *isci_timer = (struct ISCI_TIMER *)timer; 79230557Sjimharris 80230557Sjimharris isci_timer->is_started = TRUE; 81230557Sjimharris isci_log_message(3, "TIMER", "start %p %d\n", timer, milliseconds); 82230557Sjimharris callout_reset(&isci_timer->callout, (milliseconds * hz)/1000, 83230557Sjimharris isci_timer_timeout, timer); 84230557Sjimharris} 85230557Sjimharris 86230557Sjimharris/** 87230557Sjimharris * @brief This callback method asks the user to stop the supplied timer. 88230557Sjimharris * 89230557Sjimharris * @param[in] controller This parameter specifies the controller with 90230557Sjimharris * which this timer is to associated. 91230557Sjimharris * @param[in] timer This parameter specifies the timer to be stopped. 92230557Sjimharris * 93230557Sjimharris * @return none 94230557Sjimharris */ 95230557Sjimharrisvoid 96230557Sjimharrisscif_cb_timer_stop(SCI_CONTROLLER_HANDLE_T controller, void *timer) 97230557Sjimharris{ 98230557Sjimharris struct ISCI_TIMER *isci_timer = (struct ISCI_TIMER *)timer; 99230557Sjimharris 100230557Sjimharris isci_log_message(3, "TIMER", "stop %p\n", timer); 101230557Sjimharris isci_timer->is_started = FALSE; 102230557Sjimharris callout_stop(&isci_timer->callout); 103230557Sjimharris} 104230557Sjimharris 105230557Sjimharris/** 106230557Sjimharris * @brief This callback method asks the user to create a timer and provide 107230557Sjimharris * a handle for this timer for use in further timer interactions. 108230557Sjimharris * 109230557Sjimharris * @warning The "timer_callback" method should be executed in a mutually 110230557Sjimharris * exlusive manner from the controller completion handler 111230557Sjimharris * handler (refer to scic_controller_get_handler_methods()). 112230557Sjimharris * 113230557Sjimharris * @param[in] timer_callback This parameter specifies the callback method 114230557Sjimharris * to be invoked whenever the timer expires. 115230557Sjimharris * @param[in] controller This parameter specifies the controller with 116230557Sjimharris * which this timer is to be associated. 117230557Sjimharris * @param[in] cookie This parameter specifies a piece of information that 118230557Sjimharris * the user must retain. This cookie is to be supplied by the 119230557Sjimharris * user anytime a timeout occurs for the created timer. 120230557Sjimharris * 121230557Sjimharris * @return This method returns a handle to a timer object created by the 122230557Sjimharris * user. The handle will be utilized for all further interactions 123230557Sjimharris * relating to this timer. 124230557Sjimharris */ 125230557Sjimharrisvoid * 126230557Sjimharrisscif_cb_timer_create(SCI_CONTROLLER_HANDLE_T scif_controller, 127230557Sjimharris SCI_TIMER_CALLBACK_T timer_callback, void *cookie) 128230557Sjimharris{ 129230557Sjimharris struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 130230557Sjimharris sci_object_get_association(scif_controller); 131230557Sjimharris struct ISCI_TIMER *timer; 132230557Sjimharris 133230557Sjimharris sci_pool_get(isci_controller->timer_pool, timer); 134230557Sjimharris 135230557Sjimharris callout_init_mtx(&timer->callout, &isci_controller->lock, FALSE); 136230557Sjimharris 137230557Sjimharris timer->callback = timer_callback; 138230557Sjimharris timer->cookie = cookie; 139230557Sjimharris timer->is_started = FALSE; 140230557Sjimharris 141230557Sjimharris isci_log_message(3, "TIMER", "create %p %p %p\n", timer, timer_callback, cookie); 142230557Sjimharris 143230557Sjimharris return (timer); 144230557Sjimharris} 145230557Sjimharris 146230557Sjimharris/** 147230557Sjimharris * @brief This callback method asks the user to destory the supplied timer. 148230557Sjimharris * 149230557Sjimharris * @param[in] controller This parameter specifies the controller with 150230557Sjimharris * which this timer is to associated. 151230557Sjimharris * @param[in] timer This parameter specifies the timer to be destroyed. 152230557Sjimharris * 153230557Sjimharris * @return none 154230557Sjimharris */ 155230557Sjimharrisvoid 156230557Sjimharrisscif_cb_timer_destroy(SCI_CONTROLLER_HANDLE_T scif_controller, 157230557Sjimharris void *timer_handle) 158230557Sjimharris{ 159230557Sjimharris struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 160230557Sjimharris sci_object_get_association(scif_controller); 161230557Sjimharris 162230557Sjimharris scif_cb_timer_stop(scif_controller, timer_handle); 163230557Sjimharris sci_pool_put(isci_controller->timer_pool, (struct ISCI_TIMER *)timer_handle); 164230557Sjimharris 165230557Sjimharris isci_log_message(3, "TIMER", "destroy %p\n", timer_handle); 166230557Sjimharris} 167