blob: 7c05fd9dccfd1259133adaa1691df648cff56521 [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>
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070025#include <scsi/scsi_dh.h>
26#include "../scsi_priv.h"
27
28static DEFINE_SPINLOCK(list_lock);
29static LIST_HEAD(scsi_dh_list);
Peter Jones940d7fa2011-01-06 15:38:24 -050030static int scsi_dh_list_idx = 1;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070031
32static struct scsi_device_handler *get_device_handler(const char *name)
33{
34 struct scsi_device_handler *tmp, *found = NULL;
35
36 spin_lock(&list_lock);
37 list_for_each_entry(tmp, &scsi_dh_list, list) {
Hannes Reinecke765cbc62008-07-17 16:52:51 -070038 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070039 found = tmp;
40 break;
41 }
42 }
43 spin_unlock(&list_lock);
44 return found;
45}
46
Peter Jones940d7fa2011-01-06 15:38:24 -050047static struct scsi_device_handler *get_device_handler_by_idx(int idx)
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070048{
Peter Jones940d7fa2011-01-06 15:38:24 -050049 struct scsi_device_handler *tmp, *found = NULL;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070050
51 spin_lock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -050052 list_for_each_entry(tmp, &scsi_dh_list, list) {
53 if (tmp->idx == idx) {
54 found = tmp;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070055 break;
56 }
57 }
58 spin_unlock(&list_lock);
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070059 return found;
60}
61
62/*
Hannes Reinecke6c3633d2011-08-24 10:51:15 +020063 * device_handler_match_function - Match a device handler to a device
64 * @sdev - SCSI device to be tested
65 *
66 * Tests @sdev against the match function of all registered device_handler.
67 * Returns the found device handler or NULL if not found.
68 */
69static struct scsi_device_handler *
70device_handler_match_function(struct scsi_device *sdev)
71{
72 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
73
74 spin_lock(&list_lock);
75 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
76 if (tmp_dh->match && tmp_dh->match(sdev)) {
77 found_dh = tmp_dh;
78 break;
79 }
80 }
81 spin_unlock(&list_lock);
82 return found_dh;
83}
84
85/*
86 * device_handler_match_devlist - Match a device handler to a device
87 * @sdev - SCSI device to be tested
88 *
89 * Tests @sdev against all device_handler registered in the devlist.
90 * Returns the found device handler or NULL if not found.
91 */
92static struct scsi_device_handler *
93device_handler_match_devlist(struct scsi_device *sdev)
94{
95 int idx;
96
97 idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
98 SCSI_DEVINFO_DH);
99 return get_device_handler_by_idx(idx);
100}
101
102/*
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700103 * device_handler_match - Attach a device handler to a device
104 * @scsi_dh - The device handler to match against or NULL
105 * @sdev - SCSI device to be tested against @scsi_dh
106 *
107 * Tests @sdev against the device handler @scsi_dh or against
108 * all registered device_handler if @scsi_dh == NULL.
109 * Returns the found device handler or NULL if not found.
110 */
111static struct scsi_device_handler *
112device_handler_match(struct scsi_device_handler *scsi_dh,
113 struct scsi_device *sdev)
114{
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200115 struct scsi_device_handler *found_dh;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700116
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200117 found_dh = device_handler_match_function(sdev);
118 if (!found_dh)
119 found_dh = device_handler_match_devlist(sdev);
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700120
Peter Jones940d7fa2011-01-06 15:38:24 -0500121 if (scsi_dh && found_dh != scsi_dh)
122 found_dh = NULL;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700123
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700124 return found_dh;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700125}
126
127/*
128 * scsi_dh_handler_attach - Attach a device handler to a device
129 * @sdev - SCSI device the device handler should attach to
130 * @scsi_dh - The device handler to attach
131 */
132static int scsi_dh_handler_attach(struct scsi_device *sdev,
133 struct scsi_device_handler *scsi_dh)
134{
135 int err = 0;
136
137 if (sdev->scsi_dh_data) {
138 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
139 err = -EBUSY;
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700140 else
141 kref_get(&sdev->scsi_dh_data->kref);
142 } else if (scsi_dh->attach) {
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700143 err = scsi_dh->attach(sdev);
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700144 if (!err) {
145 kref_init(&sdev->scsi_dh_data->kref);
146 sdev->scsi_dh_data->sdev = sdev;
147 }
148 }
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700149 return err;
150}
151
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700152static void __detach_handler (struct kref *kref)
153{
154 struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
155 scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
156}
157
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700158/*
159 * scsi_dh_handler_detach - Detach a device handler from a device
160 * @sdev - SCSI device the device handler should be detached from
161 * @scsi_dh - Device handler to be detached
162 *
163 * Detach from a device handler. If a device handler is specified,
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700164 * only detach if the currently attached handler matches @scsi_dh.
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700165 */
166static void scsi_dh_handler_detach(struct scsi_device *sdev,
167 struct scsi_device_handler *scsi_dh)
168{
169 if (!sdev->scsi_dh_data)
170 return;
171
172 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
173 return;
174
175 if (!scsi_dh)
176 scsi_dh = sdev->scsi_dh_data->scsi_dh;
177
178 if (scsi_dh && scsi_dh->detach)
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700179 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700180}
181
182/*
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700183 * Functions for sysfs attribute 'dh_state'
184 */
185static ssize_t
186store_dh_state(struct device *dev, struct device_attribute *attr,
187 const char *buf, size_t count)
188{
189 struct scsi_device *sdev = to_scsi_device(dev);
190 struct scsi_device_handler *scsi_dh;
191 int err = -EINVAL;
192
Hannes Reinecke6bc8d2a2011-08-24 10:51:17 +0200193 if (sdev->sdev_state == SDEV_CANCEL ||
194 sdev->sdev_state == SDEV_DEL)
195 return -ENODEV;
196
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700197 if (!sdev->scsi_dh_data) {
198 /*
199 * Attach to a device handler
200 */
201 if (!(scsi_dh = get_device_handler(buf)))
202 return err;
203 err = scsi_dh_handler_attach(sdev, scsi_dh);
204 } else {
205 scsi_dh = sdev->scsi_dh_data->scsi_dh;
206 if (!strncmp(buf, "detach", 6)) {
207 /*
208 * Detach from a device handler
209 */
210 scsi_dh_handler_detach(sdev, scsi_dh);
211 err = 0;
212 } else if (!strncmp(buf, "activate", 8)) {
213 /*
214 * Activate a device handler
215 */
216 if (scsi_dh->activate)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700217 err = scsi_dh->activate(sdev, NULL, NULL);
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700218 else
219 err = 0;
220 }
221 }
222
223 return err<0?err:count;
224}
225
226static ssize_t
227show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
228{
229 struct scsi_device *sdev = to_scsi_device(dev);
230
231 if (!sdev->scsi_dh_data)
232 return snprintf(buf, 20, "detached\n");
233
234 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
235}
236
237static struct device_attribute scsi_dh_state_attr =
238 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
239 store_dh_state);
240
241/*
242 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
243 */
244static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
245{
246 struct scsi_device *sdev;
247 int err;
248
249 if (!scsi_is_sdev_device(dev))
250 return 0;
251
252 sdev = to_scsi_device(dev);
253
254 err = device_create_file(&sdev->sdev_gendev,
255 &scsi_dh_state_attr);
256
257 return 0;
258}
259
260/*
261 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
262 */
263static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
264{
265 struct scsi_device *sdev;
266
267 if (!scsi_is_sdev_device(dev))
268 return 0;
269
270 sdev = to_scsi_device(dev);
271
272 device_remove_file(&sdev->sdev_gendev,
273 &scsi_dh_state_attr);
274
275 return 0;
276}
277
278/*
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700279 * scsi_dh_notifier - notifier chain callback
280 */
281static int scsi_dh_notifier(struct notifier_block *nb,
282 unsigned long action, void *data)
283{
284 struct device *dev = data;
285 struct scsi_device *sdev;
286 int err = 0;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700287 struct scsi_device_handler *devinfo = NULL;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700288
289 if (!scsi_is_sdev_device(dev))
290 return 0;
291
292 sdev = to_scsi_device(dev);
293
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700294 if (action == BUS_NOTIFY_ADD_DEVICE) {
Chandra Seetharaman59172902009-09-11 10:20:35 -0700295 err = device_create_file(dev, &scsi_dh_state_attr);
296 /* don't care about err */
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700297 devinfo = device_handler_match(NULL, sdev);
Chandra Seetharaman59172902009-09-11 10:20:35 -0700298 if (devinfo)
299 err = scsi_dh_handler_attach(sdev, devinfo);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700300 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700301 device_remove_file(dev, &scsi_dh_state_attr);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700302 scsi_dh_handler_detach(sdev, NULL);
303 }
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700304 return err;
305}
306
307/*
308 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
309 */
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700310static int scsi_dh_notifier_add(struct device *dev, void *data)
311{
312 struct scsi_device_handler *scsi_dh = data;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700313 struct scsi_device *sdev;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700314
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700315 if (!scsi_is_sdev_device(dev))
316 return 0;
317
318 if (!get_device(dev))
319 return 0;
320
321 sdev = to_scsi_device(dev);
322
323 if (device_handler_match(scsi_dh, sdev))
324 scsi_dh_handler_attach(sdev, scsi_dh);
325
326 put_device(dev);
327
328 return 0;
329}
330
331/*
332 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
333 */
334static int scsi_dh_notifier_remove(struct device *dev, void *data)
335{
336 struct scsi_device_handler *scsi_dh = data;
337 struct scsi_device *sdev;
338
339 if (!scsi_is_sdev_device(dev))
340 return 0;
341
342 if (!get_device(dev))
343 return 0;
344
345 sdev = to_scsi_device(dev);
346
347 scsi_dh_handler_detach(sdev, scsi_dh);
348
349 put_device(dev);
350
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700351 return 0;
352}
353
354/*
355 * scsi_register_device_handler - register a device handler personality
356 * module.
357 * @scsi_dh - device handler to be registered.
358 *
359 * Returns 0 on success, -EBUSY if handler already registered.
360 */
361int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
362{
Peter Jones940d7fa2011-01-06 15:38:24 -0500363 int i;
364
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700365 if (get_device_handler(scsi_dh->name))
366 return -EBUSY;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700367
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700368 spin_lock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -0500369 scsi_dh->idx = scsi_dh_list_idx++;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700370 list_add(&scsi_dh->list, &scsi_dh_list);
371 spin_unlock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -0500372
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200373 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
Peter Jones940d7fa2011-01-06 15:38:24 -0500374 scsi_dev_info_list_add_keyed(0,
375 scsi_dh->devlist[i].vendor,
376 scsi_dh->devlist[i].model,
377 NULL,
378 scsi_dh->idx,
379 SCSI_DEVINFO_DH);
380 }
381
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700382 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
383 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700384
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700385 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700386}
387EXPORT_SYMBOL_GPL(scsi_register_device_handler);
388
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700389/*
390 * scsi_unregister_device_handler - register a device handler personality
391 * module.
392 * @scsi_dh - device handler to be unregistered.
393 *
394 * Returns 0 on success, -ENODEV if handler not registered.
395 */
396int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
397{
Peter Jones940d7fa2011-01-06 15:38:24 -0500398 int i;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700399
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700400 if (!get_device_handler(scsi_dh->name))
401 return -ENODEV;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700402
403 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700404 scsi_dh_notifier_remove);
405
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200406 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
Peter Jones940d7fa2011-01-06 15:38:24 -0500407 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
408 scsi_dh->devlist[i].model,
409 SCSI_DEVINFO_DH);
410 }
411
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700412 spin_lock(&list_lock);
413 list_del(&scsi_dh->list);
414 spin_unlock(&list_lock);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700415 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700416
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700417 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700418}
419EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
420
421/*
422 * scsi_dh_activate - activate the path associated with the scsi_device
423 * corresponding to the given request queue.
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700424 * Returns immediately without waiting for activation to be completed.
425 * @q - Request queue that is associated with the scsi_device to be
426 * activated.
427 * @fn - Function to be called upon completion of the activation.
428 * Function fn is called with data (below) and the error code.
429 * Function fn may be called from the same calling context. So,
430 * do not hold the lock in the caller which may be needed in fn.
431 * @data - data passed to the function fn upon completion.
432 *
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700433 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700434int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700435{
436 int err = 0;
437 unsigned long flags;
438 struct scsi_device *sdev;
439 struct scsi_device_handler *scsi_dh = NULL;
Mike Snitzer0b839352011-04-08 15:05:36 -0400440 struct device *dev = NULL;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700441
442 spin_lock_irqsave(q->queue_lock, flags);
443 sdev = q->queuedata;
444 if (sdev && sdev->scsi_dh_data)
445 scsi_dh = sdev->scsi_dh_data->scsi_dh;
Mike Snitzer0b839352011-04-08 15:05:36 -0400446 dev = get_device(&sdev->sdev_gendev);
447 if (!scsi_dh || !dev ||
Menny Hamburgerdb422312010-12-16 14:57:07 -0500448 sdev->sdev_state == SDEV_CANCEL ||
449 sdev->sdev_state == SDEV_DEL)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700450 err = SCSI_DH_NOSYS;
Menny Hamburgerdb422312010-12-16 14:57:07 -0500451 if (sdev->sdev_state == SDEV_OFFLINE)
452 err = SCSI_DH_DEV_OFFLINED;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700453 spin_unlock_irqrestore(q->queue_lock, flags);
454
Menny Hamburgerdb422312010-12-16 14:57:07 -0500455 if (err) {
456 if (fn)
457 fn(data, err);
Mike Snitzer0b839352011-04-08 15:05:36 -0400458 goto out;
Menny Hamburgerdb422312010-12-16 14:57:07 -0500459 }
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700460
461 if (scsi_dh->activate)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700462 err = scsi_dh->activate(sdev, fn, data);
Mike Snitzer0b839352011-04-08 15:05:36 -0400463out:
464 put_device(dev);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700465 return err;
466}
467EXPORT_SYMBOL_GPL(scsi_dh_activate);
468
469/*
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700470 * scsi_dh_set_params - set the parameters for the device as per the
471 * string specified in params.
472 * @q - Request queue that is associated with the scsi_device for
473 * which the parameters to be set.
474 * @params - parameters in the following format
475 * "no_of_params\0param1\0param2\0param3\0...\0"
476 * for example, string for 2 parameters with value 10 and 21
477 * is specified as "2\010\021\0".
478 */
479int scsi_dh_set_params(struct request_queue *q, const char *params)
480{
481 int err = -SCSI_DH_NOSYS;
482 unsigned long flags;
483 struct scsi_device *sdev;
484 struct scsi_device_handler *scsi_dh = NULL;
485
486 spin_lock_irqsave(q->queue_lock, flags);
487 sdev = q->queuedata;
488 if (sdev && sdev->scsi_dh_data)
489 scsi_dh = sdev->scsi_dh_data->scsi_dh;
490 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
491 err = 0;
492 spin_unlock_irqrestore(q->queue_lock, flags);
493
494 if (err)
495 return err;
496 err = scsi_dh->set_params(sdev, params);
497 put_device(&sdev->sdev_gendev);
498 return err;
499}
500EXPORT_SYMBOL_GPL(scsi_dh_set_params);
501
502/*
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700503 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
504 * the given name. FALSE(0) otherwise.
505 * @name - name of the device handler.
506 */
507int scsi_dh_handler_exist(const char *name)
508{
509 return (get_device_handler(name) != NULL);
510}
511EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
512
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700513/*
Hannes Reinecke2a9ab402011-08-24 10:51:14 +0200514 * scsi_dh_attach - Attach device handler
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700515 * @sdev - sdev the handler should be attached to
516 * @name - name of the handler to attach
517 */
518int scsi_dh_attach(struct request_queue *q, const char *name)
519{
520 unsigned long flags;
521 struct scsi_device *sdev;
522 struct scsi_device_handler *scsi_dh;
523 int err = 0;
524
525 scsi_dh = get_device_handler(name);
526 if (!scsi_dh)
527 return -EINVAL;
528
529 spin_lock_irqsave(q->queue_lock, flags);
530 sdev = q->queuedata;
531 if (!sdev || !get_device(&sdev->sdev_gendev))
532 err = -ENODEV;
533 spin_unlock_irqrestore(q->queue_lock, flags);
534
535 if (!err) {
536 err = scsi_dh_handler_attach(sdev, scsi_dh);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700537 put_device(&sdev->sdev_gendev);
538 }
539 return err;
540}
541EXPORT_SYMBOL_GPL(scsi_dh_attach);
542
543/*
Hannes Reinecke2a9ab402011-08-24 10:51:14 +0200544 * scsi_dh_detach - Detach device handler
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700545 * @sdev - sdev the handler should be detached from
546 *
547 * This function will detach the device handler only
548 * if the sdev is not part of the internal list, ie
549 * if it has been attached manually.
550 */
551void scsi_dh_detach(struct request_queue *q)
552{
553 unsigned long flags;
554 struct scsi_device *sdev;
555 struct scsi_device_handler *scsi_dh = NULL;
556
557 spin_lock_irqsave(q->queue_lock, flags);
558 sdev = q->queuedata;
559 if (!sdev || !get_device(&sdev->sdev_gendev))
560 sdev = NULL;
561 spin_unlock_irqrestore(q->queue_lock, flags);
562
563 if (!sdev)
564 return;
565
566 if (sdev->scsi_dh_data) {
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700567 scsi_dh = sdev->scsi_dh_data->scsi_dh;
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700568 scsi_dh_handler_detach(sdev, scsi_dh);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700569 }
570 put_device(&sdev->sdev_gendev);
571}
572EXPORT_SYMBOL_GPL(scsi_dh_detach);
573
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700574static struct notifier_block scsi_dh_nb = {
575 .notifier_call = scsi_dh_notifier
576};
577
578static int __init scsi_dh_init(void)
579{
580 int r;
581
Peter Jones940d7fa2011-01-06 15:38:24 -0500582 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
583 if (r)
584 return r;
585
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700586 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
587
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700588 if (!r)
589 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
590 scsi_dh_sysfs_attr_add);
591
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700592 return r;
593}
594
595static void __exit scsi_dh_exit(void)
596{
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700597 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
598 scsi_dh_sysfs_attr_remove);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700599 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
Peter Jones940d7fa2011-01-06 15:38:24 -0500600 scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700601}
602
603module_init(scsi_dh_init);
604module_exit(scsi_dh_exit);
605
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700606MODULE_DESCRIPTION("SCSI device handler");
607MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
608MODULE_LICENSE("GPL");