]>
Commit | Line | Data |
---|---|---|
a6a8d9f8 CS |
1 | /* |
2 | * SCSI device handler infrastruture. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License as published by the | |
6 | * Free Software Foundation; either version 2 of the License, or (at your | |
7 | * option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * Copyright IBM Corporation, 2007 | |
19 | * Authors: | |
20 | * Chandra Seetharaman <sekharan@us.ibm.com> | |
21 | * Mike Anderson <andmike@linux.vnet.ibm.com> | |
22 | */ | |
23 | ||
24 | #include <scsi/scsi_dh.h> | |
25 | #include "../scsi_priv.h" | |
26 | ||
27 | static DEFINE_SPINLOCK(list_lock); | |
28 | static LIST_HEAD(scsi_dh_list); | |
29 | ||
30 | static struct scsi_device_handler *get_device_handler(const char *name) | |
31 | { | |
32 | struct scsi_device_handler *tmp, *found = NULL; | |
33 | ||
34 | spin_lock(&list_lock); | |
35 | list_for_each_entry(tmp, &scsi_dh_list, list) { | |
765cbc6d | 36 | if (!strncmp(tmp->name, name, strlen(tmp->name))) { |
a6a8d9f8 CS |
37 | found = tmp; |
38 | break; | |
39 | } | |
40 | } | |
41 | spin_unlock(&list_lock); | |
42 | return found; | |
43 | } | |
44 | ||
765cbc6d HR |
45 | static int device_handler_match(struct scsi_device_handler *tmp, |
46 | struct scsi_device *sdev) | |
a6a8d9f8 | 47 | { |
765cbc6d HR |
48 | int i; |
49 | ||
50 | for(i = 0; tmp->devlist[i].vendor; i++) { | |
51 | if (!strncmp(sdev->vendor, tmp->devlist[i].vendor, | |
52 | strlen(tmp->devlist[i].vendor)) && | |
53 | !strncmp(sdev->model, tmp->devlist[i].model, | |
54 | strlen(tmp->devlist[i].model))) { | |
55 | return 1; | |
56 | } | |
57 | } | |
a6a8d9f8 | 58 | |
a6a8d9f8 CS |
59 | return 0; |
60 | } | |
61 | ||
62 | /* | |
765cbc6d HR |
63 | * scsi_dh_handler_attach - Attach a device handler to a device |
64 | * @sdev - SCSI device the device handler should attach to | |
65 | * @scsi_dh - The device handler to attach | |
66 | */ | |
67 | static int scsi_dh_handler_attach(struct scsi_device *sdev, | |
68 | struct scsi_device_handler *scsi_dh) | |
69 | { | |
70 | int err = 0; | |
71 | ||
72 | if (sdev->scsi_dh_data) { | |
73 | if (sdev->scsi_dh_data->scsi_dh != scsi_dh) | |
74 | err = -EBUSY; | |
75 | } else if (scsi_dh->attach) | |
76 | err = scsi_dh->attach(sdev); | |
77 | ||
78 | return err; | |
79 | } | |
80 | ||
81 | /* | |
82 | * scsi_dh_handler_detach - Detach a device handler from a device | |
83 | * @sdev - SCSI device the device handler should be detached from | |
84 | * @scsi_dh - Device handler to be detached | |
a6a8d9f8 | 85 | * |
765cbc6d | 86 | * Detach from a device handler. If a device handler is specified, |
4c05ae52 | 87 | * only detach if the currently attached handler matches @scsi_dh. |
a6a8d9f8 | 88 | */ |
765cbc6d HR |
89 | static void scsi_dh_handler_detach(struct scsi_device *sdev, |
90 | struct scsi_device_handler *scsi_dh) | |
a6a8d9f8 | 91 | { |
765cbc6d HR |
92 | if (!sdev->scsi_dh_data) |
93 | return; | |
a6a8d9f8 | 94 | |
765cbc6d HR |
95 | if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh) |
96 | return; | |
a6a8d9f8 | 97 | |
765cbc6d HR |
98 | if (!scsi_dh) |
99 | scsi_dh = sdev->scsi_dh_data->scsi_dh; | |
100 | ||
101 | if (scsi_dh && scsi_dh->detach) | |
102 | scsi_dh->detach(sdev); | |
103 | } | |
104 | ||
4c05ae52 HR |
105 | /* |
106 | * Functions for sysfs attribute 'dh_state' | |
107 | */ | |
108 | static ssize_t | |
109 | store_dh_state(struct device *dev, struct device_attribute *attr, | |
110 | const char *buf, size_t count) | |
111 | { | |
112 | struct scsi_device *sdev = to_scsi_device(dev); | |
113 | struct scsi_device_handler *scsi_dh; | |
114 | int err = -EINVAL; | |
115 | ||
116 | if (!sdev->scsi_dh_data) { | |
117 | /* | |
118 | * Attach to a device handler | |
119 | */ | |
120 | if (!(scsi_dh = get_device_handler(buf))) | |
121 | return err; | |
122 | err = scsi_dh_handler_attach(sdev, scsi_dh); | |
123 | } else { | |
124 | scsi_dh = sdev->scsi_dh_data->scsi_dh; | |
125 | if (!strncmp(buf, "detach", 6)) { | |
126 | /* | |
127 | * Detach from a device handler | |
128 | */ | |
129 | scsi_dh_handler_detach(sdev, scsi_dh); | |
130 | err = 0; | |
131 | } else if (!strncmp(buf, "activate", 8)) { | |
132 | /* | |
133 | * Activate a device handler | |
134 | */ | |
135 | if (scsi_dh->activate) | |
136 | err = scsi_dh->activate(sdev); | |
137 | else | |
138 | err = 0; | |
139 | } | |
140 | } | |
141 | ||
142 | return err<0?err:count; | |
143 | } | |
144 | ||
145 | static ssize_t | |
146 | show_dh_state(struct device *dev, struct device_attribute *attr, char *buf) | |
147 | { | |
148 | struct scsi_device *sdev = to_scsi_device(dev); | |
149 | ||
150 | if (!sdev->scsi_dh_data) | |
151 | return snprintf(buf, 20, "detached\n"); | |
152 | ||
153 | return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name); | |
154 | } | |
155 | ||
156 | static struct device_attribute scsi_dh_state_attr = | |
157 | __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state, | |
158 | store_dh_state); | |
159 | ||
160 | /* | |
161 | * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh | |
162 | */ | |
163 | static int scsi_dh_sysfs_attr_add(struct device *dev, void *data) | |
164 | { | |
165 | struct scsi_device *sdev; | |
166 | int err; | |
167 | ||
168 | if (!scsi_is_sdev_device(dev)) | |
169 | return 0; | |
170 | ||
171 | sdev = to_scsi_device(dev); | |
172 | ||
173 | err = device_create_file(&sdev->sdev_gendev, | |
174 | &scsi_dh_state_attr); | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
179 | /* | |
180 | * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh | |
181 | */ | |
182 | static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data) | |
183 | { | |
184 | struct scsi_device *sdev; | |
185 | ||
186 | if (!scsi_is_sdev_device(dev)) | |
187 | return 0; | |
188 | ||
189 | sdev = to_scsi_device(dev); | |
190 | ||
191 | device_remove_file(&sdev->sdev_gendev, | |
192 | &scsi_dh_state_attr); | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
765cbc6d HR |
197 | /* |
198 | * scsi_dh_notifier - notifier chain callback | |
199 | */ | |
200 | static int scsi_dh_notifier(struct notifier_block *nb, | |
201 | unsigned long action, void *data) | |
202 | { | |
203 | struct device *dev = data; | |
204 | struct scsi_device *sdev; | |
205 | int err = 0; | |
206 | struct scsi_device_handler *tmp, *devinfo = NULL; | |
207 | ||
208 | if (!scsi_is_sdev_device(dev)) | |
209 | return 0; | |
210 | ||
211 | sdev = to_scsi_device(dev); | |
a6a8d9f8 | 212 | |
a6a8d9f8 | 213 | spin_lock(&list_lock); |
765cbc6d HR |
214 | list_for_each_entry(tmp, &scsi_dh_list, list) { |
215 | if (device_handler_match(tmp, sdev)) { | |
216 | devinfo = tmp; | |
217 | break; | |
218 | } | |
219 | } | |
a6a8d9f8 CS |
220 | spin_unlock(&list_lock); |
221 | ||
765cbc6d HR |
222 | if (!devinfo) |
223 | goto out; | |
224 | ||
225 | if (action == BUS_NOTIFY_ADD_DEVICE) { | |
226 | err = scsi_dh_handler_attach(sdev, devinfo); | |
4c05ae52 HR |
227 | if (!err) |
228 | err = device_create_file(dev, &scsi_dh_state_attr); | |
765cbc6d | 229 | } else if (action == BUS_NOTIFY_DEL_DEVICE) { |
4c05ae52 | 230 | device_remove_file(dev, &scsi_dh_state_attr); |
765cbc6d HR |
231 | scsi_dh_handler_detach(sdev, NULL); |
232 | } | |
233 | out: | |
234 | return err; | |
a6a8d9f8 | 235 | } |
a6a8d9f8 | 236 | |
765cbc6d HR |
237 | /* |
238 | * scsi_dh_notifier_add - Callback for scsi_register_device_handler | |
239 | */ | |
240 | static int scsi_dh_notifier_add(struct device *dev, void *data) | |
241 | { | |
242 | struct scsi_device_handler *scsi_dh = data; | |
243 | struct scsi_device *sdev; | |
244 | ||
245 | if (!scsi_is_sdev_device(dev)) | |
246 | return 0; | |
247 | ||
248 | if (!get_device(dev)) | |
249 | return 0; | |
250 | ||
251 | sdev = to_scsi_device(dev); | |
252 | ||
253 | if (device_handler_match(scsi_dh, sdev)) | |
254 | scsi_dh_handler_attach(sdev, scsi_dh); | |
255 | ||
256 | put_device(dev); | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | /* | |
262 | * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler | |
263 | */ | |
a6a8d9f8 CS |
264 | static int scsi_dh_notifier_remove(struct device *dev, void *data) |
265 | { | |
266 | struct scsi_device_handler *scsi_dh = data; | |
765cbc6d HR |
267 | struct scsi_device *sdev; |
268 | ||
269 | if (!scsi_is_sdev_device(dev)) | |
270 | return 0; | |
271 | ||
272 | if (!get_device(dev)) | |
273 | return 0; | |
274 | ||
275 | sdev = to_scsi_device(dev); | |
276 | ||
277 | scsi_dh_handler_detach(sdev, scsi_dh); | |
278 | ||
279 | put_device(dev); | |
a6a8d9f8 | 280 | |
a6a8d9f8 CS |
281 | return 0; |
282 | } | |
283 | ||
765cbc6d HR |
284 | /* |
285 | * scsi_register_device_handler - register a device handler personality | |
286 | * module. | |
287 | * @scsi_dh - device handler to be registered. | |
288 | * | |
289 | * Returns 0 on success, -EBUSY if handler already registered. | |
290 | */ | |
291 | int scsi_register_device_handler(struct scsi_device_handler *scsi_dh) | |
292 | { | |
293 | if (get_device_handler(scsi_dh->name)) | |
294 | return -EBUSY; | |
295 | ||
296 | spin_lock(&list_lock); | |
297 | list_add(&scsi_dh->list, &scsi_dh_list); | |
298 | spin_unlock(&list_lock); | |
299 | bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add); | |
300 | printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name); | |
301 | ||
302 | return SCSI_DH_OK; | |
303 | } | |
304 | EXPORT_SYMBOL_GPL(scsi_register_device_handler); | |
305 | ||
a6a8d9f8 CS |
306 | /* |
307 | * scsi_unregister_device_handler - register a device handler personality | |
308 | * module. | |
309 | * @scsi_dh - device handler to be unregistered. | |
310 | * | |
311 | * Returns 0 on success, -ENODEV if handler not registered. | |
312 | */ | |
313 | int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh) | |
314 | { | |
765cbc6d HR |
315 | if (!get_device_handler(scsi_dh->name)) |
316 | return -ENODEV; | |
a6a8d9f8 CS |
317 | |
318 | bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, | |
765cbc6d HR |
319 | scsi_dh_notifier_remove); |
320 | ||
a6a8d9f8 CS |
321 | spin_lock(&list_lock); |
322 | list_del(&scsi_dh->list); | |
323 | spin_unlock(&list_lock); | |
765cbc6d | 324 | printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name); |
a6a8d9f8 | 325 | |
765cbc6d | 326 | return SCSI_DH_OK; |
a6a8d9f8 CS |
327 | } |
328 | EXPORT_SYMBOL_GPL(scsi_unregister_device_handler); | |
329 | ||
330 | /* | |
331 | * scsi_dh_activate - activate the path associated with the scsi_device | |
332 | * corresponding to the given request queue. | |
333 | * @q - Request queue that is associated with the scsi_device to be | |
334 | * activated. | |
335 | */ | |
336 | int scsi_dh_activate(struct request_queue *q) | |
337 | { | |
338 | int err = 0; | |
339 | unsigned long flags; | |
340 | struct scsi_device *sdev; | |
341 | struct scsi_device_handler *scsi_dh = NULL; | |
342 | ||
343 | spin_lock_irqsave(q->queue_lock, flags); | |
344 | sdev = q->queuedata; | |
345 | if (sdev && sdev->scsi_dh_data) | |
346 | scsi_dh = sdev->scsi_dh_data->scsi_dh; | |
347 | if (!scsi_dh || !get_device(&sdev->sdev_gendev)) | |
348 | err = SCSI_DH_NOSYS; | |
349 | spin_unlock_irqrestore(q->queue_lock, flags); | |
350 | ||
351 | if (err) | |
352 | return err; | |
353 | ||
354 | if (scsi_dh->activate) | |
355 | err = scsi_dh->activate(sdev); | |
356 | put_device(&sdev->sdev_gendev); | |
357 | return err; | |
358 | } | |
359 | EXPORT_SYMBOL_GPL(scsi_dh_activate); | |
360 | ||
361 | /* | |
362 | * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for | |
363 | * the given name. FALSE(0) otherwise. | |
364 | * @name - name of the device handler. | |
365 | */ | |
366 | int scsi_dh_handler_exist(const char *name) | |
367 | { | |
368 | return (get_device_handler(name) != NULL); | |
369 | } | |
370 | EXPORT_SYMBOL_GPL(scsi_dh_handler_exist); | |
371 | ||
765cbc6d HR |
372 | static struct notifier_block scsi_dh_nb = { |
373 | .notifier_call = scsi_dh_notifier | |
374 | }; | |
375 | ||
376 | static int __init scsi_dh_init(void) | |
377 | { | |
378 | int r; | |
379 | ||
380 | r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb); | |
381 | ||
4c05ae52 HR |
382 | if (!r) |
383 | bus_for_each_dev(&scsi_bus_type, NULL, NULL, | |
384 | scsi_dh_sysfs_attr_add); | |
385 | ||
765cbc6d HR |
386 | return r; |
387 | } | |
388 | ||
389 | static void __exit scsi_dh_exit(void) | |
390 | { | |
4c05ae52 HR |
391 | bus_for_each_dev(&scsi_bus_type, NULL, NULL, |
392 | scsi_dh_sysfs_attr_remove); | |
765cbc6d HR |
393 | bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb); |
394 | } | |
395 | ||
396 | module_init(scsi_dh_init); | |
397 | module_exit(scsi_dh_exit); | |
398 | ||
a6a8d9f8 CS |
399 | MODULE_DESCRIPTION("SCSI device handler"); |
400 | MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>"); | |
401 | MODULE_LICENSE("GPL"); |