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