f33eff00dc0181005f1697ba63bf719dcb6a2cd2
[deliverable/linux.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 * isci_timer_list_construct() - This method contrucst the SCI Timer List
61 * object used by the SCI Module class. The construction process involves
62 * creating isci_timer objects and adding them to the SCI Timer List
63 * object's list member. The number of isci_timer objects is determined by
64 * the timer_list_size parameter.
65 * @ihost: container of the timer list
66 *
67 * This method returns an error code indicating sucess or failure. The user
68 * should check for possible memory allocation error return otherwise, a zero
69 * indicates success.
70 */
71 int isci_timer_list_construct(struct isci_host *ihost)
72 {
73 struct isci_timer *itimer;
74 int i, err = 0;
75
76 INIT_LIST_HEAD(&ihost->timers);
77 for (i = 0; i < SCI_MAX_TIMER_COUNT; i++) {
78 itimer = devm_kzalloc(&ihost->pdev->dev, sizeof(*itimer), GFP_KERNEL);
79
80 if (!itimer) {
81 err = -ENOMEM;
82 break;
83 }
84 init_timer(&itimer->timer);
85 itimer->used = 0;
86 itimer->stopped = 1;
87 list_add(&itimer->node, &ihost->timers);
88 }
89
90 return err;
91 }
92
93 /**
94 * isci_timer_list_destroy() - This method destroys the SCI Timer List object
95 * used by the SCI Module class. The destruction process involves freeing
96 * memory allocated for isci_timer objects on the SCI Timer List object's
97 * timers list_head member. If any isci_timer objects are mark as "in use",
98 * they are not freed and the function returns an error code of -EBUSY.
99 * @ihost: container of the list to be destroyed
100 */
101 void isci_timer_list_destroy(struct isci_host *ihost)
102 {
103 struct isci_timer *timer;
104 LIST_HEAD(list);
105
106 spin_lock_irq(&ihost->scic_lock);
107 list_splice_init(&ihost->timers, &list);
108 spin_unlock_irq(&ihost->scic_lock);
109
110 list_for_each_entry(timer, &list, node)
111 del_timer_sync(&timer->timer);
112 }
113
114 /**
115 * This method pulls an isci_timer object off of the list for the SCI Timer
116 * List object specified, marks the isci_timer as "in use" and initializes
117 * it with user callback function and cookie data. The timer is not start at
118 * this time, just reserved for the user.
119 * @isci_timer_list: This parameter points to the SCI Timer List from which the
120 * timer is reserved.
121 * @cookie: This parameter specifies a piece of information that the user must
122 * retain. This cookie is to be supplied by the user anytime a timeout
123 * occurs for the created timer.
124 * @timer_callback: This parameter specifies the callback method to be invoked
125 * whenever the timer expires.
126 *
127 * This method returns a pointer to an isci_timer object reserved from the SCI
128 * Timer List. The pointer will be utilized for all further interactions
129 * relating to this timer.
130 */
131
132 static void timer_function(unsigned long data)
133 {
134
135 struct isci_timer *timer = (struct isci_timer *)data;
136 struct isci_host *isci_host = timer->isci_host;
137 unsigned long flags;
138
139 dev_dbg(&isci_host->pdev->dev,
140 "%s: isci_timer = %p\n", __func__, timer);
141
142 if (isci_stopped == isci_host_get_state(isci_host)) {
143 timer->stopped = 1;
144 return;
145 }
146
147 spin_lock_irqsave(&isci_host->scic_lock, flags);
148
149 if (!timer->stopped) {
150 timer->stopped = 1;
151 timer->timer_callback(timer->cb_param);
152 }
153
154 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
155 }
156
157
158 struct isci_timer *isci_timer_create(struct isci_host *ihost, void *cb_param,
159 void (*timer_callback)(void *))
160 {
161 struct timer_list *timer;
162 struct isci_timer *isci_timer;
163 struct list_head *list = &ihost->timers;
164
165 WARN_ONCE(!spin_is_locked(&ihost->scic_lock),
166 "%s: unlocked!\n", __func__);
167
168 if (WARN_ONCE(list_empty(list), "%s: timer pool empty\n", __func__))
169 return NULL;
170
171 isci_timer = list_entry(list->next, struct isci_timer, node);
172
173 isci_timer->used = 1;
174 isci_timer->stopped = 1;
175 /* FIXME: what!? we recycle the timer, rather than take it off
176 * the free list?
177 */
178 list_move_tail(&isci_timer->node, list);
179
180 timer = &isci_timer->timer;
181 timer->data = (unsigned long)isci_timer;
182 timer->function = timer_function;
183 isci_timer->cb_param = cb_param;
184 isci_timer->timer_callback = timer_callback;
185 isci_timer->isci_host = ihost;
186
187 dev_dbg(&ihost->pdev->dev,
188 "%s: isci_timer = %p\n", __func__, isci_timer);
189
190 return isci_timer;
191 }
192
193 /* isci_del_timer() - This method frees the isci_timer, marking it "free to
194 * use", then places its back at the head of the timers list for the SCI
195 * Timer List object specified.
196 */
197 void isci_del_timer(struct isci_host *ihost, struct isci_timer *isci_timer)
198 {
199 struct list_head *list = &ihost->timers;
200
201 WARN_ONCE(!spin_is_locked(&ihost->scic_lock),
202 "%s unlocked!\n", __func__);
203
204 dev_dbg(&isci_timer->isci_host->pdev->dev,
205 "%s: isci_timer = %p\n", __func__, isci_timer);
206
207 isci_timer->used = 0;
208 list_move(&isci_timer->node, list);
209 del_timer(&isci_timer->timer);
210 isci_timer->stopped = 1;
211 }
212
213 /**
214 * isci_timer_start() - This method starts the specified isci_timer, with the
215 * specified timeout value.
216 * @isci_timer: This parameter specifies the timer to be started.
217 * @timeout: This parameter specifies the timeout, in milliseconds, after which
218 * the associated callback function will be called.
219 *
220 */
221 void isci_timer_start(struct isci_timer *isci_timer, unsigned long tmo)
222 {
223 struct timer_list *timer = &isci_timer->timer;
224
225 dev_dbg(&isci_timer->isci_host->pdev->dev,
226 "%s: isci_timer = %p\n", __func__, isci_timer);
227
228 isci_timer->stopped = 0;
229 mod_timer(timer, jiffies + msecs_to_jiffies(tmo));
230 }
231
232 /**
233 * isci_timer_stop() - This method stops the supplied isci_timer.
234 * @isci_timer: This parameter specifies the isci_timer to be stopped.
235 *
236 */
237 void isci_timer_stop(struct isci_timer *isci_timer)
238 {
239 dev_dbg(&isci_timer->isci_host->pdev->dev,
240 "%s: isci_timer = %p\n", __func__, isci_timer);
241
242 isci_timer->stopped = 1;
243 del_timer(&isci_timer->timer);
244 }
This page took 0.0346 seconds and 4 git commands to generate.