isci: Intel(R) C600 Series Chipset Storage Control Unit Driver
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / isci / timers.c
1 /*
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.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
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.
12  *
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.
17  *
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.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
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
38  *     distribution.
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.
42  *
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.
54  */
55
56 #include "isci.h"
57 #include "timers.h"
58
59
60 /**
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
69  *    isci_timer_list.
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.
72  *
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
75  * indicates success.
76  */
77 int isci_timer_list_construct(
78         struct isci_timer_list *isci_timer_list,
79         int timer_list_size)
80 {
81         struct isci_timer *isci_timer;
82         int i;
83         int err = 0;
84
85
86         for (i = 0; i < timer_list_size; i++) {
87
88                 isci_timer = kzalloc(sizeof(*isci_timer), GFP_KERNEL);
89
90                 if (!isci_timer) {
91
92                         err = -ENOMEM;
93                         break;
94                 }
95                 isci_timer->used = 0;
96                 isci_timer->stopped = 1;
97                 isci_timer->parent = isci_timer_list;
98                 list_add(&isci_timer->node, &isci_timer_list->timers);
99         }
100
101         return 0;
102
103 }
104
105
106 /**
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
113  *    destroyed.
114  *
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.
118  */
119 int isci_timer_list_destroy(
120         struct isci_timer_list *isci_timer_list)
121 {
122         struct isci_timer *timer, *tmp;
123
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);
127                 kfree(timer);
128         }
129         return 0;
130 }
131
132
133
134 static void isci_timer_restart(struct isci_timer *isci_timer)
135 {
136         struct timer_list *timer =
137                 &isci_timer->timer;
138         unsigned long timeout;
139
140         dev_dbg(&isci_timer->isci_host->pdev->dev,
141                 "%s: isci_timer = %p\n", __func__, isci_timer);
142
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);
149 }
150
151 /**
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
157  *    timer is reserved.
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.
163  *
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.
167  */
168
169 static void timer_function(unsigned long data)
170 {
171
172         struct isci_timer *timer     = (struct isci_timer *)data;
173         struct isci_host *isci_host = timer->isci_host;
174         unsigned long flags;
175
176         dev_dbg(&isci_host->pdev->dev,
177                 "%s: isci_timer = %p\n", __func__, timer);
178
179         if (isci_stopped == isci_host_get_state(isci_host)) {
180                 timer->stopped = 1;
181                 return;
182         }
183
184         spin_lock_irqsave(&isci_host->scic_lock, flags);
185
186         if (!timer->stopped) {
187                 timer->stopped = 1;
188                 timer->timer_callback(timer->cookie);
189
190                 if (timer->restart)
191                         isci_timer_restart(timer);
192         }
193
194         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
195 }
196
197
198 struct isci_timer *isci_timer_create(
199         struct isci_timer_list *isci_timer_list,
200         struct isci_host *isci_host,
201         void *cookie,
202         void (*timer_callback)(void *))
203 {
204
205         struct timer_list *timer;
206         struct isci_timer *isci_timer;
207         struct list_head *timer_list =
208                 &isci_timer_list->timers;
209         unsigned long flags;
210
211         spin_lock_irqsave(&isci_host->scic_lock, flags);
212
213         if (list_empty(timer_list)) {
214                 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
215                 return NULL;
216         }
217
218         isci_timer = list_entry(timer_list->next, struct isci_timer, node);
219
220         if (isci_timer->used) {
221                 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
222                 return NULL;
223         }
224         isci_timer->used = 1;
225         isci_timer->stopped = 1;
226         list_move_tail(&isci_timer->node, timer_list);
227
228         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
229
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;
236
237         dev_dbg(&isci_host->pdev->dev,
238                 "%s: isci_timer = %p\n", __func__, isci_timer);
239
240         return isci_timer;
241 }
242
243 /**
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
248  *    timer is reserved.
249  * @isci_timer: This parameter specifies the timer to be freed.
250  *
251  */
252 void isci_timer_free(
253         struct isci_timer_list *isci_timer_list,
254         struct isci_timer *isci_timer)
255 {
256         struct list_head *timer_list = &isci_timer_list->timers;
257
258         dev_dbg(&isci_timer->isci_host->pdev->dev,
259                 "%s: isci_timer = %p\n", __func__, isci_timer);
260
261         if (list_empty(timer_list))
262                 return;
263
264         isci_timer->used = 0;
265         list_move(&isci_timer->node, timer_list);
266
267         if (!isci_timer->stopped) {
268                 del_timer(&isci_timer->timer);
269                 isci_timer->stopped = 1;
270         }
271 }
272
273 /**
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.
279  *
280  */
281 void isci_timer_start(
282         struct isci_timer *isci_timer,
283         unsigned long timeout)
284 {
285         struct timer_list *timer = &isci_timer->timer;
286
287         dev_dbg(&isci_timer->isci_host->pdev->dev,
288                 "%s: isci_timer = %p\n", __func__, isci_timer);
289
290         isci_timer->timeout_value = timeout;
291         init_timer(timer);
292         timeout = (timeout * HZ) / 1000;
293         timeout = timeout ? timeout : 1;
294
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;
300         add_timer(timer);
301 }
302
303 /**
304  * isci_timer_stop() - This method stops the supplied isci_timer.
305  * @isci_timer: This parameter specifies the isci_timer to be stopped.
306  *
307  */
308 void isci_timer_stop(struct isci_timer *isci_timer)
309 {
310         dev_dbg(&isci_timer->isci_host->pdev->dev,
311                 "%s: isci_timer = %p\n", __func__, isci_timer);
312
313         if (isci_timer->stopped)
314                 return;
315
316         isci_timer->stopped = 1;
317
318         del_timer(&isci_timer->timer);
319 }