blob: 23149b9e297c29ae82dff0330498f09423a0e557 [file] [log] [blame]
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040025#include <linux/module.h>
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070026#include <scsi/scsi_dh.h>
27#include "../scsi_priv.h"
28
29static DEFINE_SPINLOCK(list_lock);
30static LIST_HEAD(scsi_dh_list);
Peter Jones940d7fa2011-01-06 15:38:24 -050031static int scsi_dh_list_idx = 1;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070032
33static struct scsi_device_handler *get_device_handler(const char *name)
34{
35 struct scsi_device_handler *tmp, *found = NULL;
36
37 spin_lock(&list_lock);
38 list_for_each_entry(tmp, &scsi_dh_list, list) {
Hannes Reinecke765cbc62008-07-17 16:52:51 -070039 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070040 found = tmp;
41 break;
42 }
43 }
44 spin_unlock(&list_lock);
45 return found;
46}
47
Peter Jones940d7fa2011-01-06 15:38:24 -050048static struct scsi_device_handler *get_device_handler_by_idx(int idx)
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070049{
Peter Jones940d7fa2011-01-06 15:38:24 -050050 struct scsi_device_handler *tmp, *found = NULL;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070051
52 spin_lock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -050053 list_for_each_entry(tmp, &scsi_dh_list, list) {
54 if (tmp->idx == idx) {
55 found = tmp;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070056 break;
57 }
58 }
59 spin_unlock(&list_lock);
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070060 return found;
61}
62
63/*
Hannes Reinecke6c3633d2011-08-24 10:51:15 +020064 * device_handler_match_function - Match a device handler to a device
65 * @sdev - SCSI device to be tested
66 *
67 * Tests @sdev against the match function of all registered device_handler.
68 * Returns the found device handler or NULL if not found.
69 */
70static struct scsi_device_handler *
71device_handler_match_function(struct scsi_device *sdev)
72{
73 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
74
75 spin_lock(&list_lock);
76 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
77 if (tmp_dh->match && tmp_dh->match(sdev)) {
78 found_dh = tmp_dh;
79 break;
80 }
81 }
82 spin_unlock(&list_lock);
83 return found_dh;
84}
85
86/*
87 * device_handler_match_devlist - Match a device handler to a device
88 * @sdev - SCSI device to be tested
89 *
90 * Tests @sdev against all device_handler registered in the devlist.
91 * Returns the found device handler or NULL if not found.
92 */
93static struct scsi_device_handler *
94device_handler_match_devlist(struct scsi_device *sdev)
95{
96 int idx;
97
98 idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
99 SCSI_DEVINFO_DH);
100 return get_device_handler_by_idx(idx);
101}
102
103/*
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700104 * device_handler_match - Attach a device handler to a device
105 * @scsi_dh - The device handler to match against or NULL
106 * @sdev - SCSI device to be tested against @scsi_dh
107 *
108 * Tests @sdev against the device handler @scsi_dh or against
109 * all registered device_handler if @scsi_dh == NULL.
110 * Returns the found device handler or NULL if not found.
111 */
112static struct scsi_device_handler *
113device_handler_match(struct scsi_device_handler *scsi_dh,
114 struct scsi_device *sdev)
115{
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200116 struct scsi_device_handler *found_dh;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700117
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200118 found_dh = device_handler_match_function(sdev);
119 if (!found_dh)
120 found_dh = device_handler_match_devlist(sdev);
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700121
Peter Jones940d7fa2011-01-06 15:38:24 -0500122 if (scsi_dh && found_dh != scsi_dh)
123 found_dh = NULL;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700124
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700125 return found_dh;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700126}
127
128/*
129 * scsi_dh_handler_attach - Attach a device handler to a device
130 * @sdev - SCSI device the device handler should attach to
131 * @scsi_dh - The device handler to attach
132 */
133static int scsi_dh_handler_attach(struct scsi_device *sdev,
134 struct scsi_device_handler *scsi_dh)
135{
136 int err = 0;
137
138 if (sdev->scsi_dh_data) {
139 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
140 err = -EBUSY;
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700141 else
142 kref_get(&sdev->scsi_dh_data->kref);
143 } else if (scsi_dh->attach) {
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700144 err = scsi_dh->attach(sdev);
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700145 if (!err) {
146 kref_init(&sdev->scsi_dh_data->kref);
147 sdev->scsi_dh_data->sdev = sdev;
148 }
149 }
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700150 return err;
151}
152
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700153static void __detach_handler (struct kref *kref)
154{
155 struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
156 scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
157}
158
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700159/*
160 * scsi_dh_handler_detach - Detach a device handler from a device
161 * @sdev - SCSI device the device handler should be detached from
162 * @scsi_dh - Device handler to be detached
163 *
164 * Detach from a device handler. If a device handler is specified,
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700165 * only detach if the currently attached handler matches @scsi_dh.
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700166 */
167static void scsi_dh_handler_detach(struct scsi_device *sdev,
168 struct scsi_device_handler *scsi_dh)
169{
170 if (!sdev->scsi_dh_data)
171 return;
172
173 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
174 return;
175
176 if (!scsi_dh)
177 scsi_dh = sdev->scsi_dh_data->scsi_dh;
178
179 if (scsi_dh && scsi_dh->detach)
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700180 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700181}
182
183/*
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700184 * Functions for sysfs attribute 'dh_state'
185 */
186static ssize_t
187store_dh_state(struct device *dev, struct device_attribute *attr,
188 const char *buf, size_t count)
189{
190 struct scsi_device *sdev = to_scsi_device(dev);
191 struct scsi_device_handler *scsi_dh;
192 int err = -EINVAL;
193
Hannes Reinecke6bc8d2a2011-08-24 10:51:17 +0200194 if (sdev->sdev_state == SDEV_CANCEL ||
195 sdev->sdev_state == SDEV_DEL)
196 return -ENODEV;
197
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700198 if (!sdev->scsi_dh_data) {
199 /*
200 * Attach to a device handler
201 */
202 if (!(scsi_dh = get_device_handler(buf)))
203 return err;
204 err = scsi_dh_handler_attach(sdev, scsi_dh);
205 } else {
206 scsi_dh = sdev->scsi_dh_data->scsi_dh;
207 if (!strncmp(buf, "detach", 6)) {
208 /*
209 * Detach from a device handler
210 */
211 scsi_dh_handler_detach(sdev, scsi_dh);
212 err = 0;
213 } else if (!strncmp(buf, "activate", 8)) {
214 /*
215 * Activate a device handler
216 */
217 if (scsi_dh->activate)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700218 err = scsi_dh->activate(sdev, NULL, NULL);
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700219 else
220 err = 0;
221 }
222 }
223
224 return err<0?err:count;
225}
226
227static ssize_t
228show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
229{
230 struct scsi_device *sdev = to_scsi_device(dev);
231
232 if (!sdev->scsi_dh_data)
233 return snprintf(buf, 20, "detached\n");
234
235 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
236}
237
238static struct device_attribute scsi_dh_state_attr =
239 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
240 store_dh_state);
241
242/*
243 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
244 */
245static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
246{
247 struct scsi_device *sdev;
248 int err;
249
250 if (!scsi_is_sdev_device(dev))
251 return 0;
252
253 sdev = to_scsi_device(dev);
254
255 err = device_create_file(&sdev->sdev_gendev,
256 &scsi_dh_state_attr);
257
258 return 0;
259}
260
261/*
262 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
263 */
264static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
265{
266 struct scsi_device *sdev;
267
268 if (!scsi_is_sdev_device(dev))
269 return 0;
270
271 sdev = to_scsi_device(dev);
272
273 device_remove_file(&sdev->sdev_gendev,
274 &scsi_dh_state_attr);
275
276 return 0;
277}
278
279/*
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700280 * scsi_dh_notifier - notifier chain callback
281 */
282static int scsi_dh_notifier(struct notifier_block *nb,
283 unsigned long action, void *data)
284{
285 struct device *dev = data;
286 struct scsi_device *sdev;
287 int err = 0;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700288 struct scsi_device_handler *devinfo = NULL;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700289
290 if (!scsi_is_sdev_device(dev))
291 return 0;
292
293 sdev = to_scsi_device(dev);
294
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700295 if (action == BUS_NOTIFY_ADD_DEVICE) {
Chandra Seetharaman59172902009-09-11 10:20:35 -0700296 err = device_create_file(dev, &scsi_dh_state_attr);
297 /* don't care about err */
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700298 devinfo = device_handler_match(NULL, sdev);
Chandra Seetharaman59172902009-09-11 10:20:35 -0700299 if (devinfo)
300 err = scsi_dh_handler_attach(sdev, devinfo);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700301 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700302 device_remove_file(dev, &scsi_dh_state_attr);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700303 scsi_dh_handler_detach(sdev, NULL);
304 }
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700305 return err;
306}
307
308/*
309 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
310 */
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700311static int scsi_dh_notifier_add(struct device *dev, void *data)
312{
313 struct scsi_device_handler *scsi_dh = data;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700314 struct scsi_device *sdev;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700315
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700316 if (!scsi_is_sdev_device(dev))
317 return 0;
318
319 if (!get_device(dev))
320 return 0;
321
322 sdev = to_scsi_device(dev);
323
324 if (device_handler_match(scsi_dh, sdev))
325 scsi_dh_handler_attach(sdev, scsi_dh);
326
327 put_device(dev);
328
329 return 0;
330}
331
332/*
333 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
334 */
335static int scsi_dh_notifier_remove(struct device *dev, void *data)
336{
337 struct scsi_device_handler *scsi_dh = data;
338 struct scsi_device *sdev;
339
340 if (!scsi_is_sdev_device(dev))
341 return 0;
342
343 if (!get_device(dev))
344 return 0;
345
346 sdev = to_scsi_device(dev);
347
348 scsi_dh_handler_detach(sdev, scsi_dh);
349
350 put_device(dev);
351
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700352 return 0;
353}
354
355/*
356 * scsi_register_device_handler - register a device handler personality
357 * module.
358 * @scsi_dh - device handler to be registered.
359 *
360 * Returns 0 on success, -EBUSY if handler already registered.
361 */
362int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
363{
Peter Jones940d7fa2011-01-06 15:38:24 -0500364 int i;
365
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700366 if (get_device_handler(scsi_dh->name))
367 return -EBUSY;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700368
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700369 spin_lock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -0500370 scsi_dh->idx = scsi_dh_list_idx++;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700371 list_add(&scsi_dh->list, &scsi_dh_list);
372 spin_unlock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -0500373
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200374 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
Peter Jones940d7fa2011-01-06 15:38:24 -0500375 scsi_dev_info_list_add_keyed(0,
376 scsi_dh->devlist[i].vendor,
377 scsi_dh->devlist[i].model,
378 NULL,
379 scsi_dh->idx,
380 SCSI_DEVINFO_DH);
381 }
382
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700383 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
384 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700385
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700386 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700387}
388EXPORT_SYMBOL_GPL(scsi_register_device_handler);
389
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700390/*
391 * scsi_unregister_device_handler - register a device handler personality
392 * module.
393 * @scsi_dh - device handler to be unregistered.
394 *
395 * Returns 0 on success, -ENODEV if handler not registered.
396 */
397int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
398{
Peter Jones940d7fa2011-01-06 15:38:24 -0500399 int i;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700400
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700401 if (!get_device_handler(scsi_dh->name))
402 return -ENODEV;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700403
404 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700405 scsi_dh_notifier_remove);
406
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200407 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
Peter Jones940d7fa2011-01-06 15:38:24 -0500408 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
409 scsi_dh->devlist[i].model,
410 SCSI_DEVINFO_DH);
411 }
412
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700413 spin_lock(&list_lock);
414 list_del(&scsi_dh->list);
415 spin_unlock(&list_lock);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700416 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700417
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700418 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700419}
420EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
421
422/*
423 * scsi_dh_activate - activate the path associated with the scsi_device
424 * corresponding to the given request queue.
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700425 * Returns immediately without waiting for activation to be completed.
426 * @q - Request queue that is associated with the scsi_device to be
427 * activated.
428 * @fn - Function to be called upon completion of the activation.
429 * Function fn is called with data (below) and the error code.
430 * Function fn may be called from the same calling context. So,
431 * do not hold the lock in the caller which may be needed in fn.
432 * @data - data passed to the function fn upon completion.
433 *
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700434 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700435int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700436{
437 int err = 0;
438 unsigned long flags;
439 struct scsi_device *sdev;
440 struct scsi_device_handler *scsi_dh = NULL;
Mike Snitzer0b839352011-04-08 15:05:36 -0400441 struct device *dev = NULL;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700442
443 spin_lock_irqsave(q->queue_lock, flags);
444 sdev = q->queuedata;
Moger, Babua18a9202011-10-26 14:29:38 -0400445 if (!sdev) {
446 spin_unlock_irqrestore(q->queue_lock, flags);
447 err = SCSI_DH_NOSYS;
448 if (fn)
449 fn(data, err);
450 return err;
451 }
452
453 if (sdev->scsi_dh_data)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700454 scsi_dh = sdev->scsi_dh_data->scsi_dh;
Mike Snitzer0b839352011-04-08 15:05:36 -0400455 dev = get_device(&sdev->sdev_gendev);
456 if (!scsi_dh || !dev ||
Menny Hamburgerdb422312010-12-16 14:57:07 -0500457 sdev->sdev_state == SDEV_CANCEL ||
458 sdev->sdev_state == SDEV_DEL)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700459 err = SCSI_DH_NOSYS;
Menny Hamburgerdb422312010-12-16 14:57:07 -0500460 if (sdev->sdev_state == SDEV_OFFLINE)
461 err = SCSI_DH_DEV_OFFLINED;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700462 spin_unlock_irqrestore(q->queue_lock, flags);
463
Menny Hamburgerdb422312010-12-16 14:57:07 -0500464 if (err) {
465 if (fn)
466 fn(data, err);
Mike Snitzer0b839352011-04-08 15:05:36 -0400467 goto out;
Menny Hamburgerdb422312010-12-16 14:57:07 -0500468 }
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700469
470 if (scsi_dh->activate)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700471 err = scsi_dh->activate(sdev, fn, data);
Mike Snitzer0b839352011-04-08 15:05:36 -0400472out:
473 put_device(dev);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700474 return err;
475}
476EXPORT_SYMBOL_GPL(scsi_dh_activate);
477
478/*
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700479 * scsi_dh_set_params - set the parameters for the device as per the
480 * string specified in params.
481 * @q - Request queue that is associated with the scsi_device for
482 * which the parameters to be set.
483 * @params - parameters in the following format
484 * "no_of_params\0param1\0param2\0param3\0...\0"
485 * for example, string for 2 parameters with value 10 and 21
486 * is specified as "2\010\021\0".
487 */
488int scsi_dh_set_params(struct request_queue *q, const char *params)
489{
490 int err = -SCSI_DH_NOSYS;
491 unsigned long flags;
492 struct scsi_device *sdev;
493 struct scsi_device_handler *scsi_dh = NULL;
494
495 spin_lock_irqsave(q->queue_lock, flags);
496 sdev = q->queuedata;
497 if (sdev && sdev->scsi_dh_data)
498 scsi_dh = sdev->scsi_dh_data->scsi_dh;
499 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
500 err = 0;
501 spin_unlock_irqrestore(q->queue_lock, flags);
502
503 if (err)
504 return err;
505 err = scsi_dh->set_params(sdev, params);
506 put_device(&sdev->sdev_gendev);
507 return err;
508}
509EXPORT_SYMBOL_GPL(scsi_dh_set_params);
510
511/*
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700512 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
513 * the given name. FALSE(0) otherwise.
514 * @name - name of the device handler.
515 */
516int scsi_dh_handler_exist(const char *name)
517{
518 return (get_device_handler(name) != NULL);
519}
520EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
521
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700522/*
Hannes Reinecke2a9ab402011-08-24 10:51:14 +0200523 * scsi_dh_attach - Attach device handler
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700524 * @sdev - sdev the handler should be attached to
525 * @name - name of the handler to attach
526 */
527int scsi_dh_attach(struct request_queue *q, const char *name)
528{
529 unsigned long flags;
530 struct scsi_device *sdev;
531 struct scsi_device_handler *scsi_dh;
532 int err = 0;
533
534 scsi_dh = get_device_handler(name);
535 if (!scsi_dh)
536 return -EINVAL;
537
538 spin_lock_irqsave(q->queue_lock, flags);
539 sdev = q->queuedata;
540 if (!sdev || !get_device(&sdev->sdev_gendev))
541 err = -ENODEV;
542 spin_unlock_irqrestore(q->queue_lock, flags);
543
544 if (!err) {
545 err = scsi_dh_handler_attach(sdev, scsi_dh);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700546 put_device(&sdev->sdev_gendev);
547 }
548 return err;
549}
550EXPORT_SYMBOL_GPL(scsi_dh_attach);
551
552/*
Hannes Reinecke2a9ab402011-08-24 10:51:14 +0200553 * scsi_dh_detach - Detach device handler
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700554 * @sdev - sdev the handler should be detached from
555 *
556 * This function will detach the device handler only
557 * if the sdev is not part of the internal list, ie
558 * if it has been attached manually.
559 */
560void scsi_dh_detach(struct request_queue *q)
561{
562 unsigned long flags;
563 struct scsi_device *sdev;
564 struct scsi_device_handler *scsi_dh = NULL;
565
566 spin_lock_irqsave(q->queue_lock, flags);
567 sdev = q->queuedata;
568 if (!sdev || !get_device(&sdev->sdev_gendev))
569 sdev = NULL;
570 spin_unlock_irqrestore(q->queue_lock, flags);
571
572 if (!sdev)
573 return;
574
575 if (sdev->scsi_dh_data) {
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700576 scsi_dh = sdev->scsi_dh_data->scsi_dh;
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700577 scsi_dh_handler_detach(sdev, scsi_dh);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700578 }
579 put_device(&sdev->sdev_gendev);
580}
581EXPORT_SYMBOL_GPL(scsi_dh_detach);
582
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700583static struct notifier_block scsi_dh_nb = {
584 .notifier_call = scsi_dh_notifier
585};
586
587static int __init scsi_dh_init(void)
588{
589 int r;
590
Peter Jones940d7fa2011-01-06 15:38:24 -0500591 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
592 if (r)
593 return r;
594
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700595 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
596
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700597 if (!r)
598 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
599 scsi_dh_sysfs_attr_add);
600
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700601 return r;
602}
603
604static void __exit scsi_dh_exit(void)
605{
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700606 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
607 scsi_dh_sysfs_attr_remove);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700608 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
Peter Jones940d7fa2011-01-06 15:38:24 -0500609 scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700610}
611
612module_init(scsi_dh_init);
613module_exit(scsi_dh_exit);
614
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700615MODULE_DESCRIPTION("SCSI device handler");
616MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
617MODULE_LICENSE("GPL");