blob: 869b5bd1ad252243d0133c3da35681a13fb6ecee [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);
31
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
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070047/*
Hannes Reinecke6c3633d2011-08-24 10:51:15 +020048 * device_handler_match_function - Match a device handler to a device
49 * @sdev - SCSI device to be tested
50 *
51 * Tests @sdev against the match function of all registered device_handler.
52 * Returns the found device handler or NULL if not found.
53 */
54static struct scsi_device_handler *
55device_handler_match_function(struct scsi_device *sdev)
56{
57 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
58
59 spin_lock(&list_lock);
60 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
61 if (tmp_dh->match && tmp_dh->match(sdev)) {
62 found_dh = tmp_dh;
63 break;
64 }
65 }
66 spin_unlock(&list_lock);
67 return found_dh;
68}
69
70/*
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070071 * device_handler_match - Attach a device handler to a device
72 * @scsi_dh - The device handler to match against or NULL
73 * @sdev - SCSI device to be tested against @scsi_dh
74 *
75 * Tests @sdev against the device handler @scsi_dh or against
76 * all registered device_handler if @scsi_dh == NULL.
77 * Returns the found device handler or NULL if not found.
78 */
79static struct scsi_device_handler *
80device_handler_match(struct scsi_device_handler *scsi_dh,
81 struct scsi_device *sdev)
82{
Hannes Reinecke6c3633d2011-08-24 10:51:15 +020083 struct scsi_device_handler *found_dh;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070084
Hannes Reinecke6c3633d2011-08-24 10:51:15 +020085 found_dh = device_handler_match_function(sdev);
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070086
Peter Jones940d7fa2011-01-06 15:38:24 -050087 if (scsi_dh && found_dh != scsi_dh)
88 found_dh = NULL;
Hannes Reinecke765cbc62008-07-17 16:52:51 -070089
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -070090 return found_dh;
Hannes Reinecke765cbc62008-07-17 16:52:51 -070091}
92
93/*
94 * scsi_dh_handler_attach - Attach a device handler to a device
95 * @sdev - SCSI device the device handler should attach to
96 * @scsi_dh - The device handler to attach
97 */
98static int scsi_dh_handler_attach(struct scsi_device *sdev,
99 struct scsi_device_handler *scsi_dh)
100{
Christoph Hellwig1d520322014-09-14 11:08:21 -0700101 struct scsi_dh_data *d;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700102
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700103 if (!try_module_get(scsi_dh->module))
104 return -EINVAL;
Christoph Hellwig27c888f2014-09-13 19:41:16 -0700105
Christoph Hellwig1d520322014-09-14 11:08:21 -0700106 d = scsi_dh->attach(sdev);
107 if (IS_ERR(d)) {
108 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
109 scsi_dh->name, PTR_ERR(d));
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700110 module_put(scsi_dh->module);
Christoph Hellwig1d520322014-09-14 11:08:21 -0700111 return PTR_ERR(d);
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700112 }
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700113
Christoph Hellwig1d520322014-09-14 11:08:21 -0700114 d->scsi_dh = scsi_dh;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700115 d->sdev = sdev;
116
117 spin_lock_irq(sdev->request_queue->queue_lock);
118 sdev->scsi_dh_data = d;
119 spin_unlock_irq(sdev->request_queue->queue_lock);
120 return 0;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700121}
122
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200123/*
124 * scsi_dh_handler_detach - Detach a device handler from a device
125 * @sdev - SCSI device the device handler should be detached from
126 */
127static void scsi_dh_handler_detach(struct scsi_device *sdev)
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700128{
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200129 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
Christoph Hellwig27c888f2014-09-13 19:41:16 -0700130 struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
131
Mike Christie28072ad2015-01-28 03:46:53 -0600132 scsi_dh->detach(sdev);
133
Christoph Hellwig1d520322014-09-14 11:08:21 -0700134 spin_lock_irq(sdev->request_queue->queue_lock);
135 sdev->scsi_dh_data = NULL;
136 spin_unlock_irq(sdev->request_queue->queue_lock);
137
Christoph Hellwig1d520322014-09-14 11:08:21 -0700138 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
Christoph Hellwig27c888f2014-09-13 19:41:16 -0700139 module_put(scsi_dh->module);
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700140}
141
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700142/*
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700143 * Functions for sysfs attribute 'dh_state'
144 */
145static ssize_t
146store_dh_state(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t count)
148{
149 struct scsi_device *sdev = to_scsi_device(dev);
150 struct scsi_device_handler *scsi_dh;
151 int err = -EINVAL;
152
Hannes Reinecke6bc8d2a2011-08-24 10:51:17 +0200153 if (sdev->sdev_state == SDEV_CANCEL ||
154 sdev->sdev_state == SDEV_DEL)
155 return -ENODEV;
156
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700157 if (!sdev->scsi_dh_data) {
158 /*
159 * Attach to a device handler
160 */
161 if (!(scsi_dh = get_device_handler(buf)))
162 return err;
163 err = scsi_dh_handler_attach(sdev, scsi_dh);
164 } else {
165 scsi_dh = sdev->scsi_dh_data->scsi_dh;
166 if (!strncmp(buf, "detach", 6)) {
167 /*
168 * Detach from a device handler
169 */
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200170 scsi_dh_handler_detach(sdev);
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700171 err = 0;
172 } else if (!strncmp(buf, "activate", 8)) {
173 /*
174 * Activate a device handler
175 */
176 if (scsi_dh->activate)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700177 err = scsi_dh->activate(sdev, NULL, NULL);
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700178 else
179 err = 0;
180 }
181 }
182
183 return err<0?err:count;
184}
185
186static ssize_t
187show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
188{
189 struct scsi_device *sdev = to_scsi_device(dev);
190
191 if (!sdev->scsi_dh_data)
192 return snprintf(buf, 20, "detached\n");
193
194 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
195}
196
197static struct device_attribute scsi_dh_state_attr =
198 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
199 store_dh_state);
200
201/*
202 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
203 */
204static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
205{
206 struct scsi_device *sdev;
207 int err;
208
209 if (!scsi_is_sdev_device(dev))
210 return 0;
211
212 sdev = to_scsi_device(dev);
213
214 err = device_create_file(&sdev->sdev_gendev,
215 &scsi_dh_state_attr);
216
217 return 0;
218}
219
220/*
221 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
222 */
223static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
224{
225 struct scsi_device *sdev;
226
227 if (!scsi_is_sdev_device(dev))
228 return 0;
229
230 sdev = to_scsi_device(dev);
231
232 device_remove_file(&sdev->sdev_gendev,
233 &scsi_dh_state_attr);
234
235 return 0;
236}
237
238/*
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700239 * scsi_dh_notifier - notifier chain callback
240 */
241static int scsi_dh_notifier(struct notifier_block *nb,
242 unsigned long action, void *data)
243{
244 struct device *dev = data;
245 struct scsi_device *sdev;
246 int err = 0;
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700247 struct scsi_device_handler *devinfo = NULL;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700248
249 if (!scsi_is_sdev_device(dev))
250 return 0;
251
252 sdev = to_scsi_device(dev);
253
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700254 if (action == BUS_NOTIFY_ADD_DEVICE) {
Chandra Seetharaman59172902009-09-11 10:20:35 -0700255 err = device_create_file(dev, &scsi_dh_state_attr);
256 /* don't care about err */
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700257 devinfo = device_handler_match(NULL, sdev);
Chandra Seetharaman59172902009-09-11 10:20:35 -0700258 if (devinfo)
259 err = scsi_dh_handler_attach(sdev, devinfo);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700260 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700261 device_remove_file(dev, &scsi_dh_state_attr);
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200262 if (sdev->scsi_dh_data)
263 scsi_dh_handler_detach(sdev);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700264 }
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700265 return err;
266}
267
268/*
269 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
270 */
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700271static int scsi_dh_notifier_add(struct device *dev, void *data)
272{
273 struct scsi_device_handler *scsi_dh = data;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700274 struct scsi_device *sdev;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700275
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700276 if (!scsi_is_sdev_device(dev))
277 return 0;
278
279 if (!get_device(dev))
280 return 0;
281
282 sdev = to_scsi_device(dev);
283
284 if (device_handler_match(scsi_dh, sdev))
285 scsi_dh_handler_attach(sdev, scsi_dh);
286
287 put_device(dev);
288
289 return 0;
290}
291
292/*
293 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
294 */
295static int scsi_dh_notifier_remove(struct device *dev, void *data)
296{
297 struct scsi_device_handler *scsi_dh = data;
298 struct scsi_device *sdev;
299
300 if (!scsi_is_sdev_device(dev))
301 return 0;
302
303 if (!get_device(dev))
304 return 0;
305
306 sdev = to_scsi_device(dev);
307
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200308 if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh == scsi_dh)
309 scsi_dh_handler_detach(sdev);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700310
311 put_device(dev);
312
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700313 return 0;
314}
315
316/*
317 * scsi_register_device_handler - register a device handler personality
318 * module.
319 * @scsi_dh - device handler to be registered.
320 *
321 * Returns 0 on success, -EBUSY if handler already registered.
322 */
323int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
324{
Peter Jones940d7fa2011-01-06 15:38:24 -0500325
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700326 if (get_device_handler(scsi_dh->name))
327 return -EBUSY;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700328
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700329 if (!scsi_dh->attach || !scsi_dh->detach)
330 return -EINVAL;
331
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700332 spin_lock(&list_lock);
333 list_add(&scsi_dh->list, &scsi_dh_list);
334 spin_unlock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -0500335
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700336 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
337 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700338
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700339 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700340}
341EXPORT_SYMBOL_GPL(scsi_register_device_handler);
342
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700343/*
344 * scsi_unregister_device_handler - register a device handler personality
345 * module.
346 * @scsi_dh - device handler to be unregistered.
347 *
348 * Returns 0 on success, -ENODEV if handler not registered.
349 */
350int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
351{
Hannes Reinecke7c32c7a2008-07-17 16:53:33 -0700352
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700353 if (!get_device_handler(scsi_dh->name))
354 return -ENODEV;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700355
356 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700357 scsi_dh_notifier_remove);
358
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700359 spin_lock(&list_lock);
360 list_del(&scsi_dh->list);
361 spin_unlock(&list_lock);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700362 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700363
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700364 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700365}
366EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
367
368/*
369 * scsi_dh_activate - activate the path associated with the scsi_device
370 * corresponding to the given request queue.
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700371 * Returns immediately without waiting for activation to be completed.
372 * @q - Request queue that is associated with the scsi_device to be
373 * activated.
374 * @fn - Function to be called upon completion of the activation.
375 * Function fn is called with data (below) and the error code.
376 * Function fn may be called from the same calling context. So,
377 * do not hold the lock in the caller which may be needed in fn.
378 * @data - data passed to the function fn upon completion.
379 *
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700380 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700381int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700382{
383 int err = 0;
384 unsigned long flags;
385 struct scsi_device *sdev;
386 struct scsi_device_handler *scsi_dh = NULL;
Mike Snitzer0b839352011-04-08 15:05:36 -0400387 struct device *dev = NULL;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700388
389 spin_lock_irqsave(q->queue_lock, flags);
390 sdev = q->queuedata;
Moger, Babua18a9202011-10-26 14:29:38 -0400391 if (!sdev) {
392 spin_unlock_irqrestore(q->queue_lock, flags);
393 err = SCSI_DH_NOSYS;
394 if (fn)
395 fn(data, err);
396 return err;
397 }
398
399 if (sdev->scsi_dh_data)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700400 scsi_dh = sdev->scsi_dh_data->scsi_dh;
Mike Snitzer0b839352011-04-08 15:05:36 -0400401 dev = get_device(&sdev->sdev_gendev);
402 if (!scsi_dh || !dev ||
Menny Hamburgerdb422312010-12-16 14:57:07 -0500403 sdev->sdev_state == SDEV_CANCEL ||
404 sdev->sdev_state == SDEV_DEL)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700405 err = SCSI_DH_NOSYS;
Menny Hamburgerdb422312010-12-16 14:57:07 -0500406 if (sdev->sdev_state == SDEV_OFFLINE)
407 err = SCSI_DH_DEV_OFFLINED;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700408 spin_unlock_irqrestore(q->queue_lock, flags);
409
Menny Hamburgerdb422312010-12-16 14:57:07 -0500410 if (err) {
411 if (fn)
412 fn(data, err);
Mike Snitzer0b839352011-04-08 15:05:36 -0400413 goto out;
Menny Hamburgerdb422312010-12-16 14:57:07 -0500414 }
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700415
416 if (scsi_dh->activate)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700417 err = scsi_dh->activate(sdev, fn, data);
Mike Snitzer0b839352011-04-08 15:05:36 -0400418out:
419 put_device(dev);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700420 return err;
421}
422EXPORT_SYMBOL_GPL(scsi_dh_activate);
423
424/*
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700425 * scsi_dh_set_params - set the parameters for the device as per the
426 * string specified in params.
427 * @q - Request queue that is associated with the scsi_device for
428 * which the parameters to be set.
429 * @params - parameters in the following format
430 * "no_of_params\0param1\0param2\0param3\0...\0"
431 * for example, string for 2 parameters with value 10 and 21
432 * is specified as "2\010\021\0".
433 */
434int scsi_dh_set_params(struct request_queue *q, const char *params)
435{
436 int err = -SCSI_DH_NOSYS;
437 unsigned long flags;
438 struct scsi_device *sdev;
439 struct scsi_device_handler *scsi_dh = NULL;
440
441 spin_lock_irqsave(q->queue_lock, flags);
442 sdev = q->queuedata;
443 if (sdev && sdev->scsi_dh_data)
444 scsi_dh = sdev->scsi_dh_data->scsi_dh;
445 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
446 err = 0;
447 spin_unlock_irqrestore(q->queue_lock, flags);
448
449 if (err)
450 return err;
451 err = scsi_dh->set_params(sdev, params);
452 put_device(&sdev->sdev_gendev);
453 return err;
454}
455EXPORT_SYMBOL_GPL(scsi_dh_set_params);
456
457/*
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700458 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
459 * the given name. FALSE(0) otherwise.
460 * @name - name of the device handler.
461 */
462int scsi_dh_handler_exist(const char *name)
463{
464 return (get_device_handler(name) != NULL);
465}
466EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
467
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700468/*
Hannes Reinecke2a9ab402011-08-24 10:51:14 +0200469 * scsi_dh_attach - Attach device handler
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400470 * @q - Request queue that is associated with the scsi_device
471 * the handler should be attached to
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700472 * @name - name of the handler to attach
473 */
474int scsi_dh_attach(struct request_queue *q, const char *name)
475{
476 unsigned long flags;
477 struct scsi_device *sdev;
478 struct scsi_device_handler *scsi_dh;
479 int err = 0;
480
481 scsi_dh = get_device_handler(name);
482 if (!scsi_dh)
483 return -EINVAL;
484
485 spin_lock_irqsave(q->queue_lock, flags);
486 sdev = q->queuedata;
487 if (!sdev || !get_device(&sdev->sdev_gendev))
488 err = -ENODEV;
489 spin_unlock_irqrestore(q->queue_lock, flags);
490
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200491 if (err)
492 return err;
493
494 if (sdev->scsi_dh_data) {
495 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
496 err = -EBUSY;
497 goto out_put_device;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700498 }
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200499
500 err = scsi_dh_handler_attach(sdev, scsi_dh);
501
502out_put_device:
503 put_device(&sdev->sdev_gendev);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700504 return err;
505}
506EXPORT_SYMBOL_GPL(scsi_dh_attach);
507
508/*
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400509 * scsi_dh_attached_handler_name - Get attached device handler's name
510 * @q - Request queue that is associated with the scsi_device
511 * that may have a device handler attached
512 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
513 *
514 * Returns name of attached handler, NULL if no handler is attached.
515 * Caller must take care to free the returned string.
516 */
517const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
518{
519 unsigned long flags;
520 struct scsi_device *sdev;
521 const char *handler_name = NULL;
522
523 spin_lock_irqsave(q->queue_lock, flags);
524 sdev = q->queuedata;
525 if (!sdev || !get_device(&sdev->sdev_gendev))
526 sdev = NULL;
527 spin_unlock_irqrestore(q->queue_lock, flags);
528
529 if (!sdev)
530 return NULL;
531
532 if (sdev->scsi_dh_data)
533 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
534
535 put_device(&sdev->sdev_gendev);
536 return handler_name;
537}
538EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
539
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700540static struct notifier_block scsi_dh_nb = {
541 .notifier_call = scsi_dh_notifier
542};
543
544static int __init scsi_dh_init(void)
545{
546 int r;
547
548 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
549
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700550 if (!r)
551 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
552 scsi_dh_sysfs_attr_add);
553
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700554 return r;
555}
556
557static void __exit scsi_dh_exit(void)
558{
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700559 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
560 scsi_dh_sysfs_attr_remove);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700561 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
562}
563
564module_init(scsi_dh_init);
565module_exit(scsi_dh_exit);
566
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700567MODULE_DESCRIPTION("SCSI device handler");
568MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
569MODULE_LICENSE("GPL");