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