xref: /freebsd/sys/dev/isci/isci_timer.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1f11c7f63SJim Harris /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro 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 #include <dev/isci/isci.h>
35f11c7f63SJim Harris 
36f11c7f63SJim Harris #include <dev/isci/scil/scif_user_callback.h>
37f11c7f63SJim Harris 
38f11c7f63SJim Harris static void
isci_timer_timeout(void * arg)39f11c7f63SJim Harris isci_timer_timeout(void *arg)
40f11c7f63SJim Harris {
41f11c7f63SJim Harris 	struct ISCI_TIMER *timer = (struct ISCI_TIMER *)arg;
42f11c7f63SJim Harris 
43f11c7f63SJim Harris 	isci_log_message(3, "TIMER", "timeout %p\n", timer);
44f11c7f63SJim Harris 
45f11c7f63SJim Harris 	/* callout_stop() will *not* keep the timer from running if it is
46f11c7f63SJim Harris 	 *  pending.  callout_drain() cannot be called from interrupt context,
47f11c7f63SJim Harris 	 *  because it may cause thread to sleep which is not allowed in
48f11c7f63SJim Harris 	 *  interrupt context.  So instead, check the is_started flag to see if
49f11c7f63SJim Harris 	 *  the timer routine should actually be run or not.
50f11c7f63SJim Harris 	 */
51f11c7f63SJim Harris 	if (timer->is_started == TRUE)
52f11c7f63SJim Harris 		timer->callback(timer->cookie);
53f11c7f63SJim Harris }
54f11c7f63SJim Harris 
55f11c7f63SJim Harris /**
56f11c7f63SJim Harris  * @brief This callback method asks the user to start the supplied timer.
57f11c7f63SJim Harris  *
58f11c7f63SJim Harris  * @warning All timers in the system started by the SCI Framework are one
59f11c7f63SJim Harris  *          shot timers.  Therefore, the SCI user should make sure that it
60f11c7f63SJim Harris  *          removes the timer from it's list when a timer actually fires.
61f11c7f63SJim Harris  *          Additionally, SCI Framework user's should be able to handle
62f11c7f63SJim Harris  *          calls from the SCI Framework to stop a timer that may already
63f11c7f63SJim Harris  *          be stopped.
64f11c7f63SJim Harris  *
65f11c7f63SJim Harris  * @param[in]  controller This parameter specifies the controller with
66f11c7f63SJim Harris  *             which this timer is to associated.
67f11c7f63SJim Harris  * @param[in]  timer This parameter specifies the timer to be started.
68f11c7f63SJim Harris  * @param[in]  milliseconds This parameter specifies the number of
69f11c7f63SJim Harris  *             milliseconds for which to stall.  The operating system driver
70f11c7f63SJim Harris  *             is allowed to round this value up where necessary.
71f11c7f63SJim Harris  *
72f11c7f63SJim Harris  * @return none
73f11c7f63SJim Harris  */
74f11c7f63SJim Harris void
scif_cb_timer_start(SCI_CONTROLLER_HANDLE_T controller,void * timer,uint32_t milliseconds)75f11c7f63SJim Harris scif_cb_timer_start(SCI_CONTROLLER_HANDLE_T controller, void *timer,
76f11c7f63SJim Harris     uint32_t milliseconds)
77f11c7f63SJim Harris {
78f11c7f63SJim Harris 	struct ISCI_TIMER *isci_timer = (struct ISCI_TIMER *)timer;
79f11c7f63SJim Harris 
80f11c7f63SJim Harris 	isci_timer->is_started = TRUE;
81f11c7f63SJim Harris 	isci_log_message(3, "TIMER", "start %p %d\n", timer, milliseconds);
8285c9dd9dSSteven Hartland 	callout_reset_sbt(&isci_timer->callout, SBT_1MS * milliseconds, 0,
8385c9dd9dSSteven Hartland 	    isci_timer_timeout, timer, 0);
84f11c7f63SJim Harris }
85f11c7f63SJim Harris 
86f11c7f63SJim Harris /**
87f11c7f63SJim Harris  * @brief This callback method asks the user to stop the supplied timer.
88f11c7f63SJim Harris  *
89f11c7f63SJim Harris  * @param[in]  controller This parameter specifies the controller with
90f11c7f63SJim Harris  *             which this timer is to associated.
91f11c7f63SJim Harris  * @param[in]  timer This parameter specifies the timer to be stopped.
92f11c7f63SJim Harris  *
93f11c7f63SJim Harris  * @return none
94f11c7f63SJim Harris  */
95f11c7f63SJim Harris void
scif_cb_timer_stop(SCI_CONTROLLER_HANDLE_T controller,void * timer)96f11c7f63SJim Harris scif_cb_timer_stop(SCI_CONTROLLER_HANDLE_T controller, void *timer)
97f11c7f63SJim Harris {
98f11c7f63SJim Harris 	struct ISCI_TIMER *isci_timer = (struct ISCI_TIMER *)timer;
99f11c7f63SJim Harris 
100f11c7f63SJim Harris 	isci_log_message(3, "TIMER", "stop %p\n", timer);
101f11c7f63SJim Harris 	isci_timer->is_started = FALSE;
102f11c7f63SJim Harris 	callout_stop(&isci_timer->callout);
103f11c7f63SJim Harris }
104f11c7f63SJim Harris 
105f11c7f63SJim Harris /**
106f11c7f63SJim Harris  * @brief This callback method asks the user to create a timer and provide
107f11c7f63SJim Harris  *        a handle for this timer for use in further timer interactions.
108f11c7f63SJim Harris  *
109f11c7f63SJim Harris  * @warning The "timer_callback" method should be executed in a mutually
110*2dfcc3a9SGordon Bergling  *          exclusive manner from the controller completion handler
111f11c7f63SJim Harris  *          handler (refer to scic_controller_get_handler_methods()).
112f11c7f63SJim Harris  *
113f11c7f63SJim Harris  * @param[in]  timer_callback This parameter specifies the callback method
114f11c7f63SJim Harris  *             to be invoked whenever the timer expires.
115f11c7f63SJim Harris  * @param[in]  controller This parameter specifies the controller with
116f11c7f63SJim Harris  *             which this timer is to be associated.
117f11c7f63SJim Harris  * @param[in]  cookie This parameter specifies a piece of information that
118f11c7f63SJim Harris  *             the user must retain.  This cookie is to be supplied by the
119f11c7f63SJim Harris  *             user anytime a timeout occurs for the created timer.
120f11c7f63SJim Harris  *
121f11c7f63SJim Harris  * @return This method returns a handle to a timer object created by the
122f11c7f63SJim Harris  *         user.  The handle will be utilized for all further interactions
123f11c7f63SJim Harris  *         relating to this timer.
124f11c7f63SJim Harris  */
125f11c7f63SJim Harris void *
scif_cb_timer_create(SCI_CONTROLLER_HANDLE_T scif_controller,SCI_TIMER_CALLBACK_T timer_callback,void * cookie)126f11c7f63SJim Harris scif_cb_timer_create(SCI_CONTROLLER_HANDLE_T scif_controller,
127f11c7f63SJim Harris     SCI_TIMER_CALLBACK_T timer_callback, void *cookie)
128f11c7f63SJim Harris {
129f11c7f63SJim Harris 	struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)
130f11c7f63SJim Harris 	    sci_object_get_association(scif_controller);
131f11c7f63SJim Harris 	struct ISCI_TIMER *timer;
132f11c7f63SJim Harris 
133f11c7f63SJim Harris 	sci_pool_get(isci_controller->timer_pool, timer);
134f11c7f63SJim Harris 
135f11c7f63SJim Harris 	callout_init_mtx(&timer->callout, &isci_controller->lock, FALSE);
136f11c7f63SJim Harris 
137f11c7f63SJim Harris 	timer->callback = timer_callback;
138f11c7f63SJim Harris 	timer->cookie = cookie;
139f11c7f63SJim Harris 	timer->is_started = FALSE;
140f11c7f63SJim Harris 
141f11c7f63SJim Harris 	isci_log_message(3, "TIMER", "create %p %p %p\n", timer, timer_callback, cookie);
142f11c7f63SJim Harris 
143f11c7f63SJim Harris 	return (timer);
144f11c7f63SJim Harris }
145f11c7f63SJim Harris 
146f11c7f63SJim Harris /**
147453130d9SPedro F. Giffuni  * @brief This callback method asks the user to destroy the supplied timer.
148f11c7f63SJim Harris  *
149f11c7f63SJim Harris  * @param[in]  controller This parameter specifies the controller with
150f11c7f63SJim Harris  *             which this timer is to associated.
151f11c7f63SJim Harris  * @param[in]  timer This parameter specifies the timer to be destroyed.
152f11c7f63SJim Harris  *
153f11c7f63SJim Harris  * @return none
154f11c7f63SJim Harris  */
155f11c7f63SJim Harris void
scif_cb_timer_destroy(SCI_CONTROLLER_HANDLE_T scif_controller,void * timer_handle)156f11c7f63SJim Harris scif_cb_timer_destroy(SCI_CONTROLLER_HANDLE_T scif_controller,
157f11c7f63SJim Harris     void *timer_handle)
158f11c7f63SJim Harris {
159f11c7f63SJim Harris 	struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)
160f11c7f63SJim Harris 	    sci_object_get_association(scif_controller);
161f11c7f63SJim Harris 
162f11c7f63SJim Harris 	scif_cb_timer_stop(scif_controller, timer_handle);
163f11c7f63SJim Harris 	sci_pool_put(isci_controller->timer_pool, (struct ISCI_TIMER *)timer_handle);
164f11c7f63SJim Harris 
165f11c7f63SJim Harris 	isci_log_message(3, "TIMER", "destroy %p\n", timer_handle);
166f11c7f63SJim Harris }
167