]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/device_handler/scsi_dh.c
scsi: Add export.h for EXPORT_SYMBOL/THIS_MODULE as required
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / device_handler / scsi_dh.c
CommitLineData
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
5a0e3ad6 24#include <linux/slab.h>
a6a8d9f8
CS
25#include <scsi/scsi_dh.h>
26#include "../scsi_priv.h"
27
28static DEFINE_SPINLOCK(list_lock);
29static LIST_HEAD(scsi_dh_list);
940d7faa 30static int scsi_dh_list_idx = 1;
a6a8d9f8
CS
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) {
765cbc6d 38 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
a6a8d9f8
CS
39 found = tmp;
40 break;
41 }
42 }
43 spin_unlock(&list_lock);
44 return found;
45}
46
940d7faa 47static struct scsi_device_handler *get_device_handler_by_idx(int idx)
a6a8d9f8 48{
940d7faa 49 struct scsi_device_handler *tmp, *found = NULL;
7c32c7a2
HR
50
51 spin_lock(&list_lock);
940d7faa
PJ
52 list_for_each_entry(tmp, &scsi_dh_list, list) {
53 if (tmp->idx == idx) {
54 found = tmp;
7c32c7a2 55 break;
765cbc6d
HR
56 }
57 }
7c32c7a2 58 spin_unlock(&list_lock);
7c32c7a2
HR
59 return found;
60}
61
6c3633d0
HR
62/*
63 * 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
7c32c7a2
HR
102/*
103 * 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{
6c3633d0 115 struct scsi_device_handler *found_dh;
7c32c7a2 116
6c3633d0
HR
117 found_dh = device_handler_match_function(sdev);
118 if (!found_dh)
119 found_dh = device_handler_match_devlist(sdev);
7c32c7a2 120
940d7faa
PJ
121 if (scsi_dh && found_dh != scsi_dh)
122 found_dh = NULL;
7c32c7a2
HR
123
124 return found_dh;
a6a8d9f8
CS
125}
126
127/*
765cbc6d
HR
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;
6c10db72
CS
140 else
141 kref_get(&sdev->scsi_dh_data->kref);
142 } else if (scsi_dh->attach) {
765cbc6d 143 err = scsi_dh->attach(sdev);
6c10db72
CS
144 if (!err) {
145 kref_init(&sdev->scsi_dh_data->kref);
146 sdev->scsi_dh_data->sdev = sdev;
147 }
148 }
765cbc6d
HR
149 return err;
150}
151
6c10db72
CS
152static 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
765cbc6d
HR
158/*
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
a6a8d9f8 162 *
765cbc6d 163 * Detach from a device handler. If a device handler is specified,
4c05ae52 164 * only detach if the currently attached handler matches @scsi_dh.
a6a8d9f8 165 */
765cbc6d
HR
166static void scsi_dh_handler_detach(struct scsi_device *sdev,
167 struct scsi_device_handler *scsi_dh)
a6a8d9f8 168{
765cbc6d
HR
169 if (!sdev->scsi_dh_data)
170 return;
a6a8d9f8 171
765cbc6d
HR
172 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
173 return;
a6a8d9f8 174
765cbc6d
HR
175 if (!scsi_dh)
176 scsi_dh = sdev->scsi_dh_data->scsi_dh;
177
178 if (scsi_dh && scsi_dh->detach)
6c10db72 179 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
765cbc6d
HR
180}
181
4c05ae52
HR
182/*
183 * 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
6bc8d2a0
HR
193 if (sdev->sdev_state == SDEV_CANCEL ||
194 sdev->sdev_state == SDEV_DEL)
195 return -ENODEV;
196
4c05ae52
HR
197 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)
3ae31f6a 217 err = scsi_dh->activate(sdev, NULL, NULL);
4c05ae52
HR
218 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
765cbc6d
HR
278/*
279 * 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;
7c32c7a2 287 struct scsi_device_handler *devinfo = NULL;
765cbc6d
HR
288
289 if (!scsi_is_sdev_device(dev))
290 return 0;
291
292 sdev = to_scsi_device(dev);
a6a8d9f8 293
765cbc6d 294 if (action == BUS_NOTIFY_ADD_DEVICE) {
5917290c
CS
295 err = device_create_file(dev, &scsi_dh_state_attr);
296 /* don't care about err */
7c32c7a2 297 devinfo = device_handler_match(NULL, sdev);
5917290c
CS
298 if (devinfo)
299 err = scsi_dh_handler_attach(sdev, devinfo);
765cbc6d 300 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
4c05ae52 301 device_remove_file(dev, &scsi_dh_state_attr);
765cbc6d
HR
302 scsi_dh_handler_detach(sdev, NULL);
303 }
765cbc6d 304 return err;
a6a8d9f8 305}
a6a8d9f8 306
765cbc6d
HR
307/*
308 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
309 */
310static int scsi_dh_notifier_add(struct device *dev, void *data)
311{
312 struct scsi_device_handler *scsi_dh = data;
313 struct scsi_device *sdev;
314
315 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 */
a6a8d9f8
CS
334static int scsi_dh_notifier_remove(struct device *dev, void *data)
335{
336 struct scsi_device_handler *scsi_dh = data;
765cbc6d
HR
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);
a6a8d9f8 350
a6a8d9f8
CS
351 return 0;
352}
353
765cbc6d
HR
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{
940d7faa
PJ
363 int i;
364
765cbc6d
HR
365 if (get_device_handler(scsi_dh->name))
366 return -EBUSY;
367
368 spin_lock(&list_lock);
940d7faa 369 scsi_dh->idx = scsi_dh_list_idx++;
765cbc6d
HR
370 list_add(&scsi_dh->list, &scsi_dh_list);
371 spin_unlock(&list_lock);
940d7faa 372
6c3633d0 373 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
940d7faa
PJ
374 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
765cbc6d
HR
382 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);
384
385 return SCSI_DH_OK;
386}
387EXPORT_SYMBOL_GPL(scsi_register_device_handler);
388
a6a8d9f8
CS
389/*
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{
940d7faa 398 int i;
7c32c7a2 399
765cbc6d
HR
400 if (!get_device_handler(scsi_dh->name))
401 return -ENODEV;
a6a8d9f8
CS
402
403 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
765cbc6d
HR
404 scsi_dh_notifier_remove);
405
6c3633d0 406 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
940d7faa
PJ
407 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
408 scsi_dh->devlist[i].model,
409 SCSI_DEVINFO_DH);
410 }
411
a6a8d9f8
CS
412 spin_lock(&list_lock);
413 list_del(&scsi_dh->list);
414 spin_unlock(&list_lock);
765cbc6d 415 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
a6a8d9f8 416
765cbc6d 417 return SCSI_DH_OK;
a6a8d9f8
CS
418}
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.
3ae31f6a
CS
424 * 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 *
a6a8d9f8 433 */
3ae31f6a 434int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
a6a8d9f8
CS
435{
436 int err = 0;
437 unsigned long flags;
438 struct scsi_device *sdev;
439 struct scsi_device_handler *scsi_dh = NULL;
0b839357 440 struct device *dev = NULL;
a6a8d9f8
CS
441
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;
0b839357
MS
446 dev = get_device(&sdev->sdev_gendev);
447 if (!scsi_dh || !dev ||
db422318
MH
448 sdev->sdev_state == SDEV_CANCEL ||
449 sdev->sdev_state == SDEV_DEL)
a6a8d9f8 450 err = SCSI_DH_NOSYS;
db422318
MH
451 if (sdev->sdev_state == SDEV_OFFLINE)
452 err = SCSI_DH_DEV_OFFLINED;
a6a8d9f8
CS
453 spin_unlock_irqrestore(q->queue_lock, flags);
454
db422318
MH
455 if (err) {
456 if (fn)
457 fn(data, err);
0b839357 458 goto out;
db422318 459 }
a6a8d9f8
CS
460
461 if (scsi_dh->activate)
3ae31f6a 462 err = scsi_dh->activate(sdev, fn, data);
0b839357
MS
463out:
464 put_device(dev);
a6a8d9f8
CS
465 return err;
466}
467EXPORT_SYMBOL_GPL(scsi_dh_activate);
468
18ee70c9
CS
469/*
470 * 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
a6a8d9f8
CS
502/*
503 * 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
ae11b1b3 513/*
2a9ab40f 514 * scsi_dh_attach - Attach device handler
ae11b1b3
HR
515 * @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);
ae11b1b3
HR
537 put_device(&sdev->sdev_gendev);
538 }
539 return err;
540}
541EXPORT_SYMBOL_GPL(scsi_dh_attach);
542
543/*
2a9ab40f 544 * scsi_dh_detach - Detach device handler
ae11b1b3
HR
545 * @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) {
ae11b1b3 567 scsi_dh = sdev->scsi_dh_data->scsi_dh;
6c10db72 568 scsi_dh_handler_detach(sdev, scsi_dh);
ae11b1b3
HR
569 }
570 put_device(&sdev->sdev_gendev);
571}
572EXPORT_SYMBOL_GPL(scsi_dh_detach);
573
765cbc6d
HR
574static 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
940d7faa
PJ
582 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
583 if (r)
584 return r;
585
765cbc6d
HR
586 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
587
4c05ae52
HR
588 if (!r)
589 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
590 scsi_dh_sysfs_attr_add);
591
765cbc6d
HR
592 return r;
593}
594
595static void __exit scsi_dh_exit(void)
596{
4c05ae52
HR
597 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
598 scsi_dh_sysfs_attr_remove);
765cbc6d 599 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
940d7faa 600 scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
765cbc6d
HR
601}
602
603module_init(scsi_dh_init);
604module_exit(scsi_dh_exit);
605
a6a8d9f8
CS
606MODULE_DESCRIPTION("SCSI device handler");
607MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
608MODULE_LICENSE("GPL");