]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/device_handler/scsi_dh.c
[SCSI] Add scsi_dev_info_list_del_keyed()
[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
7c32c7a2
HR
28struct scsi_dh_devinfo_list {
29 struct list_head node;
30 char vendor[9];
31 char model[17];
32 struct scsi_device_handler *handler;
33};
34
a6a8d9f8
CS
35static DEFINE_SPINLOCK(list_lock);
36static LIST_HEAD(scsi_dh_list);
7c32c7a2 37static LIST_HEAD(scsi_dh_dev_list);
a6a8d9f8
CS
38
39static struct scsi_device_handler *get_device_handler(const char *name)
40{
41 struct scsi_device_handler *tmp, *found = NULL;
42
43 spin_lock(&list_lock);
44 list_for_each_entry(tmp, &scsi_dh_list, list) {
765cbc6d 45 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
a6a8d9f8
CS
46 found = tmp;
47 break;
48 }
49 }
50 spin_unlock(&list_lock);
51 return found;
52}
53
7c32c7a2
HR
54
55static struct scsi_device_handler *
56scsi_dh_cache_lookup(struct scsi_device *sdev)
a6a8d9f8 57{
7c32c7a2
HR
58 struct scsi_dh_devinfo_list *tmp;
59 struct scsi_device_handler *found_dh = NULL;
60
61 spin_lock(&list_lock);
62 list_for_each_entry(tmp, &scsi_dh_dev_list, node) {
63 if (!strncmp(sdev->vendor, tmp->vendor, strlen(tmp->vendor)) &&
64 !strncmp(sdev->model, tmp->model, strlen(tmp->model))) {
65 found_dh = tmp->handler;
66 break;
765cbc6d
HR
67 }
68 }
7c32c7a2 69 spin_unlock(&list_lock);
a6a8d9f8 70
7c32c7a2
HR
71 return found_dh;
72}
73
74static int scsi_dh_handler_lookup(struct scsi_device_handler *scsi_dh,
75 struct scsi_device *sdev)
76{
77 int i, found = 0;
78
79 for(i = 0; scsi_dh->devlist[i].vendor; i++) {
80 if (!strncmp(sdev->vendor, scsi_dh->devlist[i].vendor,
81 strlen(scsi_dh->devlist[i].vendor)) &&
82 !strncmp(sdev->model, scsi_dh->devlist[i].model,
83 strlen(scsi_dh->devlist[i].model))) {
84 found = 1;
85 break;
86 }
87 }
88 return found;
89}
90
91/*
92 * device_handler_match - Attach a device handler to a device
93 * @scsi_dh - The device handler to match against or NULL
94 * @sdev - SCSI device to be tested against @scsi_dh
95 *
96 * Tests @sdev against the device handler @scsi_dh or against
97 * all registered device_handler if @scsi_dh == NULL.
98 * Returns the found device handler or NULL if not found.
99 */
100static struct scsi_device_handler *
101device_handler_match(struct scsi_device_handler *scsi_dh,
102 struct scsi_device *sdev)
103{
104 struct scsi_device_handler *found_dh = NULL;
105 struct scsi_dh_devinfo_list *tmp;
106
107 found_dh = scsi_dh_cache_lookup(sdev);
108 if (found_dh)
109 return found_dh;
110
111 if (scsi_dh) {
112 if (scsi_dh_handler_lookup(scsi_dh, sdev))
113 found_dh = scsi_dh;
114 } else {
115 struct scsi_device_handler *tmp_dh;
116
117 spin_lock(&list_lock);
118 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
119 if (scsi_dh_handler_lookup(tmp_dh, sdev))
120 found_dh = tmp_dh;
121 }
122 spin_unlock(&list_lock);
123 }
124
125 if (found_dh) { /* If device is found, add it to the cache */
126 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
127 if (tmp) {
128 strncpy(tmp->vendor, sdev->vendor, 8);
129 strncpy(tmp->model, sdev->model, 16);
130 tmp->vendor[8] = '\0';
131 tmp->model[16] = '\0';
132 tmp->handler = found_dh;
133 spin_lock(&list_lock);
134 list_add(&tmp->node, &scsi_dh_dev_list);
135 spin_unlock(&list_lock);
136 } else {
137 found_dh = NULL;
138 }
139 }
140
141 return found_dh;
a6a8d9f8
CS
142}
143
144/*
765cbc6d
HR
145 * scsi_dh_handler_attach - Attach a device handler to a device
146 * @sdev - SCSI device the device handler should attach to
147 * @scsi_dh - The device handler to attach
148 */
149static int scsi_dh_handler_attach(struct scsi_device *sdev,
150 struct scsi_device_handler *scsi_dh)
151{
152 int err = 0;
153
154 if (sdev->scsi_dh_data) {
155 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
156 err = -EBUSY;
6c10db72
CS
157 else
158 kref_get(&sdev->scsi_dh_data->kref);
159 } else if (scsi_dh->attach) {
765cbc6d 160 err = scsi_dh->attach(sdev);
6c10db72
CS
161 if (!err) {
162 kref_init(&sdev->scsi_dh_data->kref);
163 sdev->scsi_dh_data->sdev = sdev;
164 }
165 }
765cbc6d
HR
166 return err;
167}
168
6c10db72
CS
169static void __detach_handler (struct kref *kref)
170{
171 struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
172 scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
173}
174
765cbc6d
HR
175/*
176 * scsi_dh_handler_detach - Detach a device handler from a device
177 * @sdev - SCSI device the device handler should be detached from
178 * @scsi_dh - Device handler to be detached
a6a8d9f8 179 *
765cbc6d 180 * Detach from a device handler. If a device handler is specified,
4c05ae52 181 * only detach if the currently attached handler matches @scsi_dh.
a6a8d9f8 182 */
765cbc6d
HR
183static void scsi_dh_handler_detach(struct scsi_device *sdev,
184 struct scsi_device_handler *scsi_dh)
a6a8d9f8 185{
765cbc6d
HR
186 if (!sdev->scsi_dh_data)
187 return;
a6a8d9f8 188
765cbc6d
HR
189 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
190 return;
a6a8d9f8 191
765cbc6d
HR
192 if (!scsi_dh)
193 scsi_dh = sdev->scsi_dh_data->scsi_dh;
194
195 if (scsi_dh && scsi_dh->detach)
6c10db72 196 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
765cbc6d
HR
197}
198
4c05ae52
HR
199/*
200 * Functions for sysfs attribute 'dh_state'
201 */
202static ssize_t
203store_dh_state(struct device *dev, struct device_attribute *attr,
204 const char *buf, size_t count)
205{
206 struct scsi_device *sdev = to_scsi_device(dev);
207 struct scsi_device_handler *scsi_dh;
208 int err = -EINVAL;
209
210 if (!sdev->scsi_dh_data) {
211 /*
212 * Attach to a device handler
213 */
214 if (!(scsi_dh = get_device_handler(buf)))
215 return err;
216 err = scsi_dh_handler_attach(sdev, scsi_dh);
217 } else {
218 scsi_dh = sdev->scsi_dh_data->scsi_dh;
219 if (!strncmp(buf, "detach", 6)) {
220 /*
221 * Detach from a device handler
222 */
223 scsi_dh_handler_detach(sdev, scsi_dh);
224 err = 0;
225 } else if (!strncmp(buf, "activate", 8)) {
226 /*
227 * Activate a device handler
228 */
229 if (scsi_dh->activate)
3ae31f6a 230 err = scsi_dh->activate(sdev, NULL, NULL);
4c05ae52
HR
231 else
232 err = 0;
233 }
234 }
235
236 return err<0?err:count;
237}
238
239static ssize_t
240show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
241{
242 struct scsi_device *sdev = to_scsi_device(dev);
243
244 if (!sdev->scsi_dh_data)
245 return snprintf(buf, 20, "detached\n");
246
247 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
248}
249
250static struct device_attribute scsi_dh_state_attr =
251 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
252 store_dh_state);
253
254/*
255 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
256 */
257static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
258{
259 struct scsi_device *sdev;
260 int err;
261
262 if (!scsi_is_sdev_device(dev))
263 return 0;
264
265 sdev = to_scsi_device(dev);
266
267 err = device_create_file(&sdev->sdev_gendev,
268 &scsi_dh_state_attr);
269
270 return 0;
271}
272
273/*
274 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
275 */
276static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
277{
278 struct scsi_device *sdev;
279
280 if (!scsi_is_sdev_device(dev))
281 return 0;
282
283 sdev = to_scsi_device(dev);
284
285 device_remove_file(&sdev->sdev_gendev,
286 &scsi_dh_state_attr);
287
288 return 0;
289}
290
765cbc6d
HR
291/*
292 * scsi_dh_notifier - notifier chain callback
293 */
294static int scsi_dh_notifier(struct notifier_block *nb,
295 unsigned long action, void *data)
296{
297 struct device *dev = data;
298 struct scsi_device *sdev;
299 int err = 0;
7c32c7a2 300 struct scsi_device_handler *devinfo = NULL;
765cbc6d
HR
301
302 if (!scsi_is_sdev_device(dev))
303 return 0;
304
305 sdev = to_scsi_device(dev);
a6a8d9f8 306
765cbc6d 307 if (action == BUS_NOTIFY_ADD_DEVICE) {
5917290c
CS
308 err = device_create_file(dev, &scsi_dh_state_attr);
309 /* don't care about err */
7c32c7a2 310 devinfo = device_handler_match(NULL, sdev);
5917290c
CS
311 if (devinfo)
312 err = scsi_dh_handler_attach(sdev, devinfo);
765cbc6d 313 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
4c05ae52 314 device_remove_file(dev, &scsi_dh_state_attr);
765cbc6d
HR
315 scsi_dh_handler_detach(sdev, NULL);
316 }
765cbc6d 317 return err;
a6a8d9f8 318}
a6a8d9f8 319
765cbc6d
HR
320/*
321 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
322 */
323static int scsi_dh_notifier_add(struct device *dev, void *data)
324{
325 struct scsi_device_handler *scsi_dh = data;
326 struct scsi_device *sdev;
327
328 if (!scsi_is_sdev_device(dev))
329 return 0;
330
331 if (!get_device(dev))
332 return 0;
333
334 sdev = to_scsi_device(dev);
335
336 if (device_handler_match(scsi_dh, sdev))
337 scsi_dh_handler_attach(sdev, scsi_dh);
338
339 put_device(dev);
340
341 return 0;
342}
343
344/*
345 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
346 */
a6a8d9f8
CS
347static int scsi_dh_notifier_remove(struct device *dev, void *data)
348{
349 struct scsi_device_handler *scsi_dh = data;
765cbc6d
HR
350 struct scsi_device *sdev;
351
352 if (!scsi_is_sdev_device(dev))
353 return 0;
354
355 if (!get_device(dev))
356 return 0;
357
358 sdev = to_scsi_device(dev);
359
360 scsi_dh_handler_detach(sdev, scsi_dh);
361
362 put_device(dev);
a6a8d9f8 363
a6a8d9f8
CS
364 return 0;
365}
366
765cbc6d
HR
367/*
368 * scsi_register_device_handler - register a device handler personality
369 * module.
370 * @scsi_dh - device handler to be registered.
371 *
372 * Returns 0 on success, -EBUSY if handler already registered.
373 */
374int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
375{
376 if (get_device_handler(scsi_dh->name))
377 return -EBUSY;
378
379 spin_lock(&list_lock);
380 list_add(&scsi_dh->list, &scsi_dh_list);
381 spin_unlock(&list_lock);
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{
7c32c7a2
HR
398 struct scsi_dh_devinfo_list *tmp, *pos;
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
a6a8d9f8
CS
406 spin_lock(&list_lock);
407 list_del(&scsi_dh->list);
7c32c7a2
HR
408 list_for_each_entry_safe(pos, tmp, &scsi_dh_dev_list, node) {
409 if (pos->handler == scsi_dh) {
410 list_del(&pos->node);
411 kfree(pos);
412 }
413 }
a6a8d9f8 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;
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;
db422318
MH
445 if (!scsi_dh || !get_device(&sdev->sdev_gendev) ||
446 sdev->sdev_state == SDEV_CANCEL ||
447 sdev->sdev_state == SDEV_DEL)
a6a8d9f8 448 err = SCSI_DH_NOSYS;
db422318
MH
449 if (sdev->sdev_state == SDEV_OFFLINE)
450 err = SCSI_DH_DEV_OFFLINED;
a6a8d9f8
CS
451 spin_unlock_irqrestore(q->queue_lock, flags);
452
db422318
MH
453 if (err) {
454 if (fn)
455 fn(data, err);
a6a8d9f8 456 return err;
db422318 457 }
a6a8d9f8
CS
458
459 if (scsi_dh->activate)
3ae31f6a 460 err = scsi_dh->activate(sdev, fn, data);
a6a8d9f8
CS
461 put_device(&sdev->sdev_gendev);
462 return err;
463}
464EXPORT_SYMBOL_GPL(scsi_dh_activate);
465
18ee70c9
CS
466/*
467 * scsi_dh_set_params - set the parameters for the device as per the
468 * string specified in params.
469 * @q - Request queue that is associated with the scsi_device for
470 * which the parameters to be set.
471 * @params - parameters in the following format
472 * "no_of_params\0param1\0param2\0param3\0...\0"
473 * for example, string for 2 parameters with value 10 and 21
474 * is specified as "2\010\021\0".
475 */
476int scsi_dh_set_params(struct request_queue *q, const char *params)
477{
478 int err = -SCSI_DH_NOSYS;
479 unsigned long flags;
480 struct scsi_device *sdev;
481 struct scsi_device_handler *scsi_dh = NULL;
482
483 spin_lock_irqsave(q->queue_lock, flags);
484 sdev = q->queuedata;
485 if (sdev && sdev->scsi_dh_data)
486 scsi_dh = sdev->scsi_dh_data->scsi_dh;
487 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
488 err = 0;
489 spin_unlock_irqrestore(q->queue_lock, flags);
490
491 if (err)
492 return err;
493 err = scsi_dh->set_params(sdev, params);
494 put_device(&sdev->sdev_gendev);
495 return err;
496}
497EXPORT_SYMBOL_GPL(scsi_dh_set_params);
498
a6a8d9f8
CS
499/*
500 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
501 * the given name. FALSE(0) otherwise.
502 * @name - name of the device handler.
503 */
504int scsi_dh_handler_exist(const char *name)
505{
506 return (get_device_handler(name) != NULL);
507}
508EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
509
ae11b1b3
HR
510/*
511 * scsi_dh_handler_attach - Attach device handler
512 * @sdev - sdev the handler should be attached to
513 * @name - name of the handler to attach
514 */
515int scsi_dh_attach(struct request_queue *q, const char *name)
516{
517 unsigned long flags;
518 struct scsi_device *sdev;
519 struct scsi_device_handler *scsi_dh;
520 int err = 0;
521
522 scsi_dh = get_device_handler(name);
523 if (!scsi_dh)
524 return -EINVAL;
525
526 spin_lock_irqsave(q->queue_lock, flags);
527 sdev = q->queuedata;
528 if (!sdev || !get_device(&sdev->sdev_gendev))
529 err = -ENODEV;
530 spin_unlock_irqrestore(q->queue_lock, flags);
531
532 if (!err) {
533 err = scsi_dh_handler_attach(sdev, scsi_dh);
ae11b1b3
HR
534 put_device(&sdev->sdev_gendev);
535 }
536 return err;
537}
538EXPORT_SYMBOL_GPL(scsi_dh_attach);
539
540/*
541 * scsi_dh_handler_detach - Detach device handler
542 * @sdev - sdev the handler should be detached from
543 *
544 * This function will detach the device handler only
545 * if the sdev is not part of the internal list, ie
546 * if it has been attached manually.
547 */
548void scsi_dh_detach(struct request_queue *q)
549{
550 unsigned long flags;
551 struct scsi_device *sdev;
552 struct scsi_device_handler *scsi_dh = NULL;
553
554 spin_lock_irqsave(q->queue_lock, flags);
555 sdev = q->queuedata;
556 if (!sdev || !get_device(&sdev->sdev_gendev))
557 sdev = NULL;
558 spin_unlock_irqrestore(q->queue_lock, flags);
559
560 if (!sdev)
561 return;
562
563 if (sdev->scsi_dh_data) {
ae11b1b3 564 scsi_dh = sdev->scsi_dh_data->scsi_dh;
6c10db72 565 scsi_dh_handler_detach(sdev, scsi_dh);
ae11b1b3
HR
566 }
567 put_device(&sdev->sdev_gendev);
568}
569EXPORT_SYMBOL_GPL(scsi_dh_detach);
570
765cbc6d
HR
571static struct notifier_block scsi_dh_nb = {
572 .notifier_call = scsi_dh_notifier
573};
574
575static int __init scsi_dh_init(void)
576{
577 int r;
578
579 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
580
4c05ae52
HR
581 if (!r)
582 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
583 scsi_dh_sysfs_attr_add);
584
765cbc6d
HR
585 return r;
586}
587
588static void __exit scsi_dh_exit(void)
589{
4c05ae52
HR
590 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
591 scsi_dh_sysfs_attr_remove);
765cbc6d
HR
592 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
593}
594
595module_init(scsi_dh_init);
596module_exit(scsi_dh_exit);
597
a6a8d9f8
CS
598MODULE_DESCRIPTION("SCSI device handler");
599MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
600MODULE_LICENSE("GPL");