1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * BSD LICENSE 5 * 6 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * * Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <dev/isci/isci.h> 37 38 #include <dev/isci/scil/scif_user_callback.h> 39 40 static void 41 isci_timer_timeout(void *arg) 42 { 43 struct ISCI_TIMER *timer = (struct ISCI_TIMER *)arg; 44 45 isci_log_message(3, "TIMER", "timeout %p\n", timer); 46 47 /* callout_stop() will *not* keep the timer from running if it is 48 * pending. callout_drain() cannot be called from interrupt context, 49 * because it may cause thread to sleep which is not allowed in 50 * interrupt context. So instead, check the is_started flag to see if 51 * the timer routine should actually be run or not. 52 */ 53 if (timer->is_started == TRUE) 54 timer->callback(timer->cookie); 55 } 56 57 /** 58 * @brief This callback method asks the user to start the supplied timer. 59 * 60 * @warning All timers in the system started by the SCI Framework are one 61 * shot timers. Therefore, the SCI user should make sure that it 62 * removes the timer from it's list when a timer actually fires. 63 * Additionally, SCI Framework user's should be able to handle 64 * calls from the SCI Framework to stop a timer that may already 65 * be stopped. 66 * 67 * @param[in] controller This parameter specifies the controller with 68 * which this timer is to associated. 69 * @param[in] timer This parameter specifies the timer to be started. 70 * @param[in] milliseconds This parameter specifies the number of 71 * milliseconds for which to stall. The operating system driver 72 * is allowed to round this value up where necessary. 73 * 74 * @return none 75 */ 76 void 77 scif_cb_timer_start(SCI_CONTROLLER_HANDLE_T controller, void *timer, 78 uint32_t milliseconds) 79 { 80 struct ISCI_TIMER *isci_timer = (struct ISCI_TIMER *)timer; 81 82 isci_timer->is_started = TRUE; 83 isci_log_message(3, "TIMER", "start %p %d\n", timer, milliseconds); 84 callout_reset_sbt(&isci_timer->callout, SBT_1MS * milliseconds, 0, 85 isci_timer_timeout, timer, 0); 86 } 87 88 /** 89 * @brief This callback method asks the user to stop the supplied timer. 90 * 91 * @param[in] controller This parameter specifies the controller with 92 * which this timer is to associated. 93 * @param[in] timer This parameter specifies the timer to be stopped. 94 * 95 * @return none 96 */ 97 void 98 scif_cb_timer_stop(SCI_CONTROLLER_HANDLE_T controller, void *timer) 99 { 100 struct ISCI_TIMER *isci_timer = (struct ISCI_TIMER *)timer; 101 102 isci_log_message(3, "TIMER", "stop %p\n", timer); 103 isci_timer->is_started = FALSE; 104 callout_stop(&isci_timer->callout); 105 } 106 107 /** 108 * @brief This callback method asks the user to create a timer and provide 109 * a handle for this timer for use in further timer interactions. 110 * 111 * @warning The "timer_callback" method should be executed in a mutually 112 * exclusive manner from the controller completion handler 113 * handler (refer to scic_controller_get_handler_methods()). 114 * 115 * @param[in] timer_callback This parameter specifies the callback method 116 * to be invoked whenever the timer expires. 117 * @param[in] controller This parameter specifies the controller with 118 * which this timer is to be associated. 119 * @param[in] cookie This parameter specifies a piece of information that 120 * the user must retain. This cookie is to be supplied by the 121 * user anytime a timeout occurs for the created timer. 122 * 123 * @return This method returns a handle to a timer object created by the 124 * user. The handle will be utilized for all further interactions 125 * relating to this timer. 126 */ 127 void * 128 scif_cb_timer_create(SCI_CONTROLLER_HANDLE_T scif_controller, 129 SCI_TIMER_CALLBACK_T timer_callback, void *cookie) 130 { 131 struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 132 sci_object_get_association(scif_controller); 133 struct ISCI_TIMER *timer; 134 135 sci_pool_get(isci_controller->timer_pool, timer); 136 137 callout_init_mtx(&timer->callout, &isci_controller->lock, FALSE); 138 139 timer->callback = timer_callback; 140 timer->cookie = cookie; 141 timer->is_started = FALSE; 142 143 isci_log_message(3, "TIMER", "create %p %p %p\n", timer, timer_callback, cookie); 144 145 return (timer); 146 } 147 148 /** 149 * @brief This callback method asks the user to destroy the supplied timer. 150 * 151 * @param[in] controller This parameter specifies the controller with 152 * which this timer is to associated. 153 * @param[in] timer This parameter specifies the timer to be destroyed. 154 * 155 * @return none 156 */ 157 void 158 scif_cb_timer_destroy(SCI_CONTROLLER_HANDLE_T scif_controller, 159 void *timer_handle) 160 { 161 struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 162 sci_object_get_association(scif_controller); 163 164 scif_cb_timer_stop(scif_controller, timer_handle); 165 sci_pool_put(isci_controller->timer_pool, (struct ISCI_TIMER *)timer_handle); 166 167 isci_log_message(3, "TIMER", "destroy %p\n", timer_handle); 168 } 169