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