2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 * isci_timer_list_construct() - This method contrucst the SCI Timer List
62 * object used by the SCI Module class. The construction process involves
63 * creating isci_timer objects and adding them to the SCI Timer List
64 * object's list member. The number of isci_timer objects is determined by
65 * the timer_list_size parameter.
66 * @isci_timer_list: This parameter points to the SCI Timer List object being
67 * constructed. The calling routine is responsible for allocating the memory
68 * for isci_timer_list and initializing the timer list_head member of
70 * @timer_list_size: This parameter specifies the number of isci_timer objects
71 * contained by the SCI Timer List. which this timer is to be associated.
73 * This method returns an error code indicating sucess or failure. The user
74 * should check for possible memory allocation error return otherwise, a zero
77 int isci_timer_list_construct(
78 struct isci_timer_list *isci_timer_list,
81 struct isci_timer *isci_timer;
86 for (i = 0; i < timer_list_size; i++) {
88 isci_timer = kzalloc(sizeof(*isci_timer), GFP_KERNEL);
96 isci_timer->stopped = 1;
97 isci_timer->parent = isci_timer_list;
98 list_add(&isci_timer->node, &isci_timer_list->timers);
107 * isci_timer_list_destroy() - This method destroys the SCI Timer List object
108 * used by the SCI Module class. The destruction process involves freeing
109 * memory allocated for isci_timer objects on the SCI Timer List object's
110 * timers list_head member. If any isci_timer objects are mark as "in use",
111 * they are not freed and the function returns an error code of -EBUSY.
112 * @isci_timer_list: This parameter points to the SCI Timer List object being
115 * This method returns an error code indicating sucess or failure. The user
116 * should check for possible -EBUSY error return, in the event of one or more
117 * isci_timers still "in use", otherwise, a zero indicates success.
119 int isci_timer_list_destroy(
120 struct isci_timer_list *isci_timer_list)
122 struct isci_timer *timer, *tmp;
124 list_for_each_entry_safe(timer, tmp, &isci_timer_list->timers, node) {
125 isci_timer_free(isci_timer_list, timer);
126 list_del(&timer->node);
134 static void isci_timer_restart(struct isci_timer *isci_timer)
136 struct timer_list *timer =
138 unsigned long timeout;
140 dev_dbg(&isci_timer->isci_host->pdev->dev,
141 "%s: isci_timer = %p\n", __func__, isci_timer);
143 isci_timer->restart = 0;
144 isci_timer->stopped = 0;
145 timeout = isci_timer->timeout_value;
146 timeout = (timeout * HZ) / 1000;
147 timeout = timeout ? timeout : 1;
148 mod_timer(timer, jiffies + timeout);
152 * This method pulls an isci_timer object off of the list for the SCI Timer
153 * List object specified, marks the isci_timer as "in use" and initializes
154 * it with user callback function and cookie data. The timer is not start at
155 * this time, just reserved for the user.
156 * @isci_timer_list: This parameter points to the SCI Timer List from which the
158 * @cookie: This parameter specifies a piece of information that the user must
159 * retain. This cookie is to be supplied by the user anytime a timeout
160 * occurs for the created timer.
161 * @timer_callback: This parameter specifies the callback method to be invoked
162 * whenever the timer expires.
164 * This method returns a pointer to an isci_timer object reserved from the SCI
165 * Timer List. The pointer will be utilized for all further interactions
166 * relating to this timer.
169 static void timer_function(unsigned long data)
172 struct isci_timer *timer = (struct isci_timer *)data;
173 struct isci_host *isci_host = timer->isci_host;
176 dev_dbg(&isci_host->pdev->dev,
177 "%s: isci_timer = %p\n", __func__, timer);
179 if (isci_stopped == isci_host_get_state(isci_host)) {
184 spin_lock_irqsave(&isci_host->scic_lock, flags);
186 if (!timer->stopped) {
188 timer->timer_callback(timer->cookie);
191 isci_timer_restart(timer);
194 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
198 struct isci_timer *isci_timer_create(
199 struct isci_timer_list *isci_timer_list,
200 struct isci_host *isci_host,
202 void (*timer_callback)(void *))
205 struct timer_list *timer;
206 struct isci_timer *isci_timer;
207 struct list_head *timer_list =
208 &isci_timer_list->timers;
211 spin_lock_irqsave(&isci_host->scic_lock, flags);
213 if (list_empty(timer_list)) {
214 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
218 isci_timer = list_entry(timer_list->next, struct isci_timer, node);
220 if (isci_timer->used) {
221 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
224 isci_timer->used = 1;
225 isci_timer->stopped = 1;
226 list_move_tail(&isci_timer->node, timer_list);
228 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
230 timer = &isci_timer->timer;
231 timer->data = (unsigned long)isci_timer;
232 timer->function = timer_function;
233 isci_timer->cookie = cookie;
234 isci_timer->timer_callback = timer_callback;
235 isci_timer->isci_host = isci_host;
237 dev_dbg(&isci_host->pdev->dev,
238 "%s: isci_timer = %p\n", __func__, isci_timer);
244 * isci_timer_free() - This method frees the isci_timer, marking it "free to
245 * use", then places its back at the head of the timers list for the SCI
246 * Timer List object specified.
247 * @isci_timer_list: This parameter points to the SCI Timer List from which the
249 * @isci_timer: This parameter specifies the timer to be freed.
252 void isci_timer_free(
253 struct isci_timer_list *isci_timer_list,
254 struct isci_timer *isci_timer)
256 struct list_head *timer_list = &isci_timer_list->timers;
258 dev_dbg(&isci_timer->isci_host->pdev->dev,
259 "%s: isci_timer = %p\n", __func__, isci_timer);
261 if (list_empty(timer_list))
264 isci_timer->used = 0;
265 list_move(&isci_timer->node, timer_list);
267 if (!isci_timer->stopped) {
268 del_timer(&isci_timer->timer);
269 isci_timer->stopped = 1;
274 * isci_timer_start() - This method starts the specified isci_timer, with the
275 * specified timeout value.
276 * @isci_timer: This parameter specifies the timer to be started.
277 * @timeout: This parameter specifies the timeout, in milliseconds, after which
278 * the associated callback function will be called.
281 void isci_timer_start(
282 struct isci_timer *isci_timer,
283 unsigned long timeout)
285 struct timer_list *timer = &isci_timer->timer;
287 dev_dbg(&isci_timer->isci_host->pdev->dev,
288 "%s: isci_timer = %p\n", __func__, isci_timer);
290 isci_timer->timeout_value = timeout;
292 timeout = (timeout * HZ) / 1000;
293 timeout = timeout ? timeout : 1;
295 timer->expires = jiffies + timeout;
296 timer->data = (unsigned long)isci_timer;
297 timer->function = timer_function;
298 isci_timer->stopped = 0;
299 isci_timer->restart = 0;
304 * isci_timer_stop() - This method stops the supplied isci_timer.
305 * @isci_timer: This parameter specifies the isci_timer to be stopped.
308 void isci_timer_stop(struct isci_timer *isci_timer)
310 dev_dbg(&isci_timer->isci_host->pdev->dev,
311 "%s: isci_timer = %p\n", __func__, isci_timer);
313 if (isci_timer->stopped)
316 isci_timer->stopped = 1;
318 del_timer(&isci_timer->timer);