]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/scsi_sysfs.c
fbd7f9ed1251feb5dab52bf1c92b48c4045b6886
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / scsi_sysfs.c
1 /*
2 * scsi_sysfs.c
3 *
4 * SCSI sysfs interface routines.
5 *
6 * Created to pull SCSI mid layer sysfs routines into one file.
7 */
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/device.h>
13
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_device.h>
16 #include <scsi/scsi_host.h>
17 #include <scsi/scsi_tcq.h>
18 #include <scsi/scsi_transport.h>
19 #include <scsi/scsi_driver.h>
20
21 #include "scsi_priv.h"
22 #include "scsi_logging.h"
23
24 static struct device_type scsi_dev_type;
25
26 static const struct {
27 enum scsi_device_state value;
28 char *name;
29 } sdev_states[] = {
30 { SDEV_CREATED, "created" },
31 { SDEV_RUNNING, "running" },
32 { SDEV_CANCEL, "cancel" },
33 { SDEV_DEL, "deleted" },
34 { SDEV_QUIESCE, "quiesce" },
35 { SDEV_OFFLINE, "offline" },
36 { SDEV_BLOCK, "blocked" },
37 };
38
39 const char *scsi_device_state_name(enum scsi_device_state state)
40 {
41 int i;
42 char *name = NULL;
43
44 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
45 if (sdev_states[i].value == state) {
46 name = sdev_states[i].name;
47 break;
48 }
49 }
50 return name;
51 }
52
53 static const struct {
54 enum scsi_host_state value;
55 char *name;
56 } shost_states[] = {
57 { SHOST_CREATED, "created" },
58 { SHOST_RUNNING, "running" },
59 { SHOST_CANCEL, "cancel" },
60 { SHOST_DEL, "deleted" },
61 { SHOST_RECOVERY, "recovery" },
62 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
63 { SHOST_DEL_RECOVERY, "deleted/recovery", },
64 };
65 const char *scsi_host_state_name(enum scsi_host_state state)
66 {
67 int i;
68 char *name = NULL;
69
70 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
71 if (shost_states[i].value == state) {
72 name = shost_states[i].name;
73 break;
74 }
75 }
76 return name;
77 }
78
79 static int check_set(unsigned int *val, char *src)
80 {
81 char *last;
82
83 if (strncmp(src, "-", 20) == 0) {
84 *val = SCAN_WILD_CARD;
85 } else {
86 /*
87 * Doesn't check for int overflow
88 */
89 *val = simple_strtoul(src, &last, 0);
90 if (*last != '\0')
91 return 1;
92 }
93 return 0;
94 }
95
96 static int scsi_scan(struct Scsi_Host *shost, const char *str)
97 {
98 char s1[15], s2[15], s3[15], junk;
99 unsigned int channel, id, lun;
100 int res;
101
102 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
103 if (res != 3)
104 return -EINVAL;
105 if (check_set(&channel, s1))
106 return -EINVAL;
107 if (check_set(&id, s2))
108 return -EINVAL;
109 if (check_set(&lun, s3))
110 return -EINVAL;
111 if (shost->transportt->user_scan)
112 res = shost->transportt->user_scan(shost, channel, id, lun);
113 else
114 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
115 return res;
116 }
117
118 /*
119 * shost_show_function: macro to create an attr function that can be used to
120 * show a non-bit field.
121 */
122 #define shost_show_function(name, field, format_string) \
123 static ssize_t \
124 show_##name (struct device *dev, struct device_attribute *attr, \
125 char *buf) \
126 { \
127 struct Scsi_Host *shost = class_to_shost(dev); \
128 return snprintf (buf, 20, format_string, shost->field); \
129 }
130
131 /*
132 * shost_rd_attr: macro to create a function and attribute variable for a
133 * read only field.
134 */
135 #define shost_rd_attr2(name, field, format_string) \
136 shost_show_function(name, field, format_string) \
137 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
138
139 #define shost_rd_attr(field, format_string) \
140 shost_rd_attr2(field, field, format_string)
141
142 /*
143 * Create the actual show/store functions and data structures.
144 */
145
146 static ssize_t
147 store_scan(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t count)
149 {
150 struct Scsi_Host *shost = class_to_shost(dev);
151 int res;
152
153 res = scsi_scan(shost, buf);
154 if (res == 0)
155 res = count;
156 return res;
157 };
158 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
159
160 static ssize_t
161 store_shost_state(struct device *dev, struct device_attribute *attr,
162 const char *buf, size_t count)
163 {
164 int i;
165 struct Scsi_Host *shost = class_to_shost(dev);
166 enum scsi_host_state state = 0;
167
168 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
169 const int len = strlen(shost_states[i].name);
170 if (strncmp(shost_states[i].name, buf, len) == 0 &&
171 buf[len] == '\n') {
172 state = shost_states[i].value;
173 break;
174 }
175 }
176 if (!state)
177 return -EINVAL;
178
179 if (scsi_host_set_state(shost, state))
180 return -EINVAL;
181 return count;
182 }
183
184 static ssize_t
185 show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
186 {
187 struct Scsi_Host *shost = class_to_shost(dev);
188 const char *name = scsi_host_state_name(shost->shost_state);
189
190 if (!name)
191 return -EINVAL;
192
193 return snprintf(buf, 20, "%s\n", name);
194 }
195
196 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
197 struct device_attribute dev_attr_hstate =
198 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
199
200 static ssize_t
201 show_shost_mode(unsigned int mode, char *buf)
202 {
203 ssize_t len = 0;
204
205 if (mode & MODE_INITIATOR)
206 len = sprintf(buf, "%s", "Initiator");
207
208 if (mode & MODE_TARGET)
209 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
210
211 len += sprintf(buf + len, "\n");
212
213 return len;
214 }
215
216 static ssize_t
217 show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
218 char *buf)
219 {
220 struct Scsi_Host *shost = class_to_shost(dev);
221 unsigned int supported_mode = shost->hostt->supported_mode;
222
223 if (supported_mode == MODE_UNKNOWN)
224 /* by default this should be initiator */
225 supported_mode = MODE_INITIATOR;
226
227 return show_shost_mode(supported_mode, buf);
228 }
229
230 static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
231
232 static ssize_t
233 show_shost_active_mode(struct device *dev,
234 struct device_attribute *attr, char *buf)
235 {
236 struct Scsi_Host *shost = class_to_shost(dev);
237
238 if (shost->active_mode == MODE_UNKNOWN)
239 return snprintf(buf, 20, "unknown\n");
240 else
241 return show_shost_mode(shost->active_mode, buf);
242 }
243
244 static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
245
246 shost_rd_attr(unique_id, "%u\n");
247 shost_rd_attr(host_busy, "%hu\n");
248 shost_rd_attr(cmd_per_lun, "%hd\n");
249 shost_rd_attr(can_queue, "%hd\n");
250 shost_rd_attr(sg_tablesize, "%hu\n");
251 shost_rd_attr(unchecked_isa_dma, "%d\n");
252 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
253
254 static struct device_attribute *scsi_sysfs_shost_attrs[] = {
255 &dev_attr_unique_id,
256 &dev_attr_host_busy,
257 &dev_attr_cmd_per_lun,
258 &dev_attr_can_queue,
259 &dev_attr_sg_tablesize,
260 &dev_attr_unchecked_isa_dma,
261 &dev_attr_proc_name,
262 &dev_attr_scan,
263 &dev_attr_hstate,
264 &dev_attr_supported_mode,
265 &dev_attr_active_mode,
266 NULL
267 };
268
269 static void scsi_device_cls_release(struct device *class_dev)
270 {
271 struct scsi_device *sdev;
272
273 sdev = class_to_sdev(class_dev);
274 put_device(&sdev->sdev_gendev);
275 }
276
277 static void scsi_device_dev_release_usercontext(struct work_struct *work)
278 {
279 struct scsi_device *sdev;
280 struct device *parent;
281 struct scsi_target *starget;
282 struct list_head *this, *tmp;
283 unsigned long flags;
284
285 sdev = container_of(work, struct scsi_device, ew.work);
286
287 parent = sdev->sdev_gendev.parent;
288 starget = to_scsi_target(parent);
289
290 spin_lock_irqsave(sdev->host->host_lock, flags);
291 starget->reap_ref++;
292 list_del(&sdev->siblings);
293 list_del(&sdev->same_target_siblings);
294 list_del(&sdev->starved_entry);
295 spin_unlock_irqrestore(sdev->host->host_lock, flags);
296
297 cancel_work_sync(&sdev->event_work);
298
299 list_for_each_safe(this, tmp, &sdev->event_list) {
300 struct scsi_event *evt;
301
302 evt = list_entry(this, struct scsi_event, node);
303 list_del(&evt->node);
304 kfree(evt);
305 }
306
307 if (sdev->request_queue) {
308 sdev->request_queue->queuedata = NULL;
309 /* user context needed to free queue */
310 scsi_free_queue(sdev->request_queue);
311 /* temporary expedient, try to catch use of queue lock
312 * after free of sdev */
313 sdev->request_queue = NULL;
314 }
315
316 scsi_target_reap(scsi_target(sdev));
317
318 kfree(sdev->inquiry);
319 kfree(sdev);
320
321 if (parent)
322 put_device(parent);
323 }
324
325 static void scsi_device_dev_release(struct device *dev)
326 {
327 struct scsi_device *sdp = to_scsi_device(dev);
328 execute_in_process_context(scsi_device_dev_release_usercontext,
329 &sdp->ew);
330 }
331
332 static struct class sdev_class = {
333 .name = "scsi_device",
334 .dev_release = scsi_device_cls_release,
335 };
336
337 /* all probing is done in the individual ->probe routines */
338 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
339 {
340 struct scsi_device *sdp;
341
342 if (dev->type != &scsi_dev_type)
343 return 0;
344
345 sdp = to_scsi_device(dev);
346 if (sdp->no_uld_attach)
347 return 0;
348 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
349 }
350
351 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
352 {
353 struct scsi_device *sdev = to_scsi_device(dev);
354
355 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
356 return 0;
357 }
358
359 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
360 {
361 struct device_driver *drv;
362 struct scsi_device *sdev;
363 int err;
364
365 if (dev->type != &scsi_dev_type)
366 return 0;
367
368 drv = dev->driver;
369 sdev = to_scsi_device(dev);
370
371 err = scsi_device_quiesce(sdev);
372 if (err)
373 return err;
374
375 if (drv && drv->suspend) {
376 err = drv->suspend(dev, state);
377 if (err)
378 return err;
379 }
380
381 return 0;
382 }
383
384 static int scsi_bus_resume(struct device * dev)
385 {
386 struct device_driver *drv;
387 struct scsi_device *sdev;
388 int err = 0;
389
390 if (dev->type != &scsi_dev_type)
391 return 0;
392
393 drv = dev->driver;
394 sdev = to_scsi_device(dev);
395
396 if (drv && drv->resume)
397 err = drv->resume(dev);
398
399 scsi_device_resume(sdev);
400
401 return err;
402 }
403
404 static int scsi_bus_remove(struct device *dev)
405 {
406 struct device_driver *drv = dev->driver;
407 struct scsi_device *sdev = to_scsi_device(dev);
408 int err = 0;
409
410 /* reset the prep_fn back to the default since the
411 * driver may have altered it and it's being removed */
412 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
413
414 if (drv && drv->remove)
415 err = drv->remove(dev);
416
417 return 0;
418 }
419
420 struct bus_type scsi_bus_type = {
421 .name = "scsi",
422 .match = scsi_bus_match,
423 .uevent = scsi_bus_uevent,
424 .suspend = scsi_bus_suspend,
425 .resume = scsi_bus_resume,
426 .remove = scsi_bus_remove,
427 };
428
429 int scsi_sysfs_register(void)
430 {
431 int error;
432
433 error = bus_register(&scsi_bus_type);
434 if (!error) {
435 error = class_register(&sdev_class);
436 if (error)
437 bus_unregister(&scsi_bus_type);
438 }
439
440 return error;
441 }
442
443 void scsi_sysfs_unregister(void)
444 {
445 class_unregister(&sdev_class);
446 bus_unregister(&scsi_bus_type);
447 }
448
449 /*
450 * sdev_show_function: macro to create an attr function that can be used to
451 * show a non-bit field.
452 */
453 #define sdev_show_function(field, format_string) \
454 static ssize_t \
455 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
456 char *buf) \
457 { \
458 struct scsi_device *sdev; \
459 sdev = to_scsi_device(dev); \
460 return snprintf (buf, 20, format_string, sdev->field); \
461 } \
462
463 /*
464 * sdev_rd_attr: macro to create a function and attribute variable for a
465 * read only field.
466 */
467 #define sdev_rd_attr(field, format_string) \
468 sdev_show_function(field, format_string) \
469 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
470
471
472 /*
473 * sdev_rd_attr: create a function and attribute variable for a
474 * read/write field.
475 */
476 #define sdev_rw_attr(field, format_string) \
477 sdev_show_function(field, format_string) \
478 \
479 static ssize_t \
480 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
481 const char *buf, size_t count) \
482 { \
483 struct scsi_device *sdev; \
484 sdev = to_scsi_device(dev); \
485 snscanf (buf, 20, format_string, &sdev->field); \
486 return count; \
487 } \
488 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
489
490 /* Currently we don't export bit fields, but we might in future,
491 * so leave this code in */
492 #if 0
493 /*
494 * sdev_rd_attr: create a function and attribute variable for a
495 * read/write bit field.
496 */
497 #define sdev_rw_attr_bit(field) \
498 sdev_show_function(field, "%d\n") \
499 \
500 static ssize_t \
501 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
502 const char *buf, size_t count) \
503 { \
504 int ret; \
505 struct scsi_device *sdev; \
506 ret = scsi_sdev_check_buf_bit(buf); \
507 if (ret >= 0) { \
508 sdev = to_scsi_device(dev); \
509 sdev->field = ret; \
510 ret = count; \
511 } \
512 return ret; \
513 } \
514 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
515
516 /*
517 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
518 * else return -EINVAL.
519 */
520 static int scsi_sdev_check_buf_bit(const char *buf)
521 {
522 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
523 if (buf[0] == '1')
524 return 1;
525 else if (buf[0] == '0')
526 return 0;
527 else
528 return -EINVAL;
529 } else
530 return -EINVAL;
531 }
532 #endif
533 /*
534 * Create the actual show/store functions and data structures.
535 */
536 sdev_rd_attr (device_blocked, "%d\n");
537 sdev_rd_attr (queue_depth, "%d\n");
538 sdev_rd_attr (type, "%d\n");
539 sdev_rd_attr (scsi_level, "%d\n");
540 sdev_rd_attr (vendor, "%.8s\n");
541 sdev_rd_attr (model, "%.16s\n");
542 sdev_rd_attr (rev, "%.4s\n");
543
544 static ssize_t
545 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
546 {
547 struct scsi_device *sdev;
548 sdev = to_scsi_device(dev);
549 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
550 }
551
552 static ssize_t
553 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
554 const char *buf, size_t count)
555 {
556 struct scsi_device *sdev;
557 int timeout;
558 sdev = to_scsi_device(dev);
559 sscanf (buf, "%d\n", &timeout);
560 sdev->timeout = timeout * HZ;
561 return count;
562 }
563 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
564
565 static ssize_t
566 store_rescan_field (struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
568 {
569 scsi_rescan_device(dev);
570 return count;
571 }
572 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
573
574 static void sdev_store_delete_callback(struct device *dev)
575 {
576 scsi_remove_device(to_scsi_device(dev));
577 }
578
579 static ssize_t
580 sdev_store_delete(struct device *dev, struct device_attribute *attr,
581 const char *buf, size_t count)
582 {
583 int rc;
584
585 /* An attribute cannot be unregistered by one of its own methods,
586 * so we have to use this roundabout approach.
587 */
588 rc = device_schedule_callback(dev, sdev_store_delete_callback);
589 if (rc)
590 count = rc;
591 return count;
592 };
593 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
594
595 static ssize_t
596 store_state_field(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
598 {
599 int i;
600 struct scsi_device *sdev = to_scsi_device(dev);
601 enum scsi_device_state state = 0;
602
603 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
604 const int len = strlen(sdev_states[i].name);
605 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
606 buf[len] == '\n') {
607 state = sdev_states[i].value;
608 break;
609 }
610 }
611 if (!state)
612 return -EINVAL;
613
614 if (scsi_device_set_state(sdev, state))
615 return -EINVAL;
616 return count;
617 }
618
619 static ssize_t
620 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
621 {
622 struct scsi_device *sdev = to_scsi_device(dev);
623 const char *name = scsi_device_state_name(sdev->sdev_state);
624
625 if (!name)
626 return -EINVAL;
627
628 return snprintf(buf, 20, "%s\n", name);
629 }
630
631 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
632
633 static ssize_t
634 show_queue_type_field(struct device *dev, struct device_attribute *attr,
635 char *buf)
636 {
637 struct scsi_device *sdev = to_scsi_device(dev);
638 const char *name = "none";
639
640 if (sdev->ordered_tags)
641 name = "ordered";
642 else if (sdev->simple_tags)
643 name = "simple";
644
645 return snprintf(buf, 20, "%s\n", name);
646 }
647
648 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
649
650 static ssize_t
651 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
652 {
653 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
654 }
655
656 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
657
658 #define show_sdev_iostat(field) \
659 static ssize_t \
660 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
661 char *buf) \
662 { \
663 struct scsi_device *sdev = to_scsi_device(dev); \
664 unsigned long long count = atomic_read(&sdev->field); \
665 return snprintf(buf, 20, "0x%llx\n", count); \
666 } \
667 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
668
669 show_sdev_iostat(iorequest_cnt);
670 show_sdev_iostat(iodone_cnt);
671 show_sdev_iostat(ioerr_cnt);
672
673 static ssize_t
674 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
675 {
676 struct scsi_device *sdev;
677 sdev = to_scsi_device(dev);
678 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
679 }
680 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
681
682 #define DECLARE_EVT_SHOW(name, Cap_name) \
683 static ssize_t \
684 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
685 char *buf) \
686 { \
687 struct scsi_device *sdev = to_scsi_device(dev); \
688 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
689 return snprintf(buf, 20, "%d\n", val); \
690 }
691
692 #define DECLARE_EVT_STORE(name, Cap_name) \
693 static ssize_t \
694 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
695 const char *buf, size_t count) \
696 { \
697 struct scsi_device *sdev = to_scsi_device(dev); \
698 int val = simple_strtoul(buf, NULL, 0); \
699 if (val == 0) \
700 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
701 else if (val == 1) \
702 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
703 else \
704 return -EINVAL; \
705 return count; \
706 }
707
708 #define DECLARE_EVT(name, Cap_name) \
709 DECLARE_EVT_SHOW(name, Cap_name) \
710 DECLARE_EVT_STORE(name, Cap_name) \
711 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
712 sdev_store_evt_##name);
713 #define REF_EVT(name) &dev_attr_evt_##name.attr
714
715 DECLARE_EVT(media_change, MEDIA_CHANGE)
716
717 /* Default template for device attributes. May NOT be modified */
718 static struct attribute *scsi_sdev_attrs[] = {
719 &dev_attr_device_blocked.attr,
720 &dev_attr_type.attr,
721 &dev_attr_scsi_level.attr,
722 &dev_attr_vendor.attr,
723 &dev_attr_model.attr,
724 &dev_attr_rev.attr,
725 &dev_attr_rescan.attr,
726 &dev_attr_delete.attr,
727 &dev_attr_state.attr,
728 &dev_attr_timeout.attr,
729 &dev_attr_iocounterbits.attr,
730 &dev_attr_iorequest_cnt.attr,
731 &dev_attr_iodone_cnt.attr,
732 &dev_attr_ioerr_cnt.attr,
733 &dev_attr_modalias.attr,
734 REF_EVT(media_change),
735 NULL
736 };
737
738 static struct attribute_group scsi_sdev_attr_group = {
739 .attrs = scsi_sdev_attrs,
740 };
741
742 static struct attribute_group *scsi_sdev_attr_groups[] = {
743 &scsi_sdev_attr_group,
744 NULL
745 };
746
747 static ssize_t
748 sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
750 {
751 int depth, retval;
752 struct scsi_device *sdev = to_scsi_device(dev);
753 struct scsi_host_template *sht = sdev->host->hostt;
754
755 if (!sht->change_queue_depth)
756 return -EINVAL;
757
758 depth = simple_strtoul(buf, NULL, 0);
759
760 if (depth < 1)
761 return -EINVAL;
762
763 retval = sht->change_queue_depth(sdev, depth);
764 if (retval < 0)
765 return retval;
766
767 return count;
768 }
769
770 static struct device_attribute sdev_attr_queue_depth_rw =
771 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
772 sdev_store_queue_depth_rw);
773
774 static ssize_t
775 sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
777 {
778 struct scsi_device *sdev = to_scsi_device(dev);
779 struct scsi_host_template *sht = sdev->host->hostt;
780 int tag_type = 0, retval;
781 int prev_tag_type = scsi_get_tag_type(sdev);
782
783 if (!sdev->tagged_supported || !sht->change_queue_type)
784 return -EINVAL;
785
786 if (strncmp(buf, "ordered", 7) == 0)
787 tag_type = MSG_ORDERED_TAG;
788 else if (strncmp(buf, "simple", 6) == 0)
789 tag_type = MSG_SIMPLE_TAG;
790 else if (strncmp(buf, "none", 4) != 0)
791 return -EINVAL;
792
793 if (tag_type == prev_tag_type)
794 return count;
795
796 retval = sht->change_queue_type(sdev, tag_type);
797 if (retval < 0)
798 return retval;
799
800 return count;
801 }
802
803 static struct device_attribute sdev_attr_queue_type_rw =
804 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
805 sdev_store_queue_type_rw);
806
807 /**
808 * scsi_sysfs_add_sdev - add scsi device to sysfs
809 * @sdev: scsi_device to add
810 *
811 * Return value:
812 * 0 on Success / non-zero on Failure
813 **/
814 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
815 {
816 int error, i;
817 struct request_queue *rq = sdev->request_queue;
818
819 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
820 return error;
821
822 error = device_add(&sdev->sdev_gendev);
823 if (error) {
824 put_device(sdev->sdev_gendev.parent);
825 printk(KERN_INFO "error 1\n");
826 return error;
827 }
828 error = device_add(&sdev->sdev_dev);
829 if (error) {
830 printk(KERN_INFO "error 2\n");
831 goto clean_device;
832 }
833
834 /* take a reference for the sdev_dev; this is
835 * released by the sdev_class .release */
836 get_device(&sdev->sdev_gendev);
837
838 /* create queue files, which may be writable, depending on the host */
839 if (sdev->host->hostt->change_queue_depth)
840 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
841 else
842 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
843 if (error) {
844 __scsi_remove_device(sdev);
845 goto out;
846 }
847 if (sdev->host->hostt->change_queue_type)
848 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
849 else
850 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
851 if (error) {
852 __scsi_remove_device(sdev);
853 goto out;
854 }
855
856 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
857
858 if (error)
859 sdev_printk(KERN_INFO, sdev,
860 "Failed to register bsg queue, errno=%d\n", error);
861
862 /* we're treating error on bsg register as non-fatal, so pretend
863 * nothing went wrong */
864 error = 0;
865
866 /* add additional host specific attributes */
867 if (sdev->host->hostt->sdev_attrs) {
868 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
869 error = device_create_file(&sdev->sdev_gendev,
870 sdev->host->hostt->sdev_attrs[i]);
871 if (error) {
872 __scsi_remove_device(sdev);
873 goto out;
874 }
875 }
876 }
877
878 transport_add_device(&sdev->sdev_gendev);
879 out:
880 return error;
881
882 clean_device:
883 scsi_device_set_state(sdev, SDEV_CANCEL);
884
885 device_del(&sdev->sdev_gendev);
886 transport_destroy_device(&sdev->sdev_gendev);
887 put_device(&sdev->sdev_gendev);
888
889 return error;
890 }
891
892 void __scsi_remove_device(struct scsi_device *sdev)
893 {
894 struct device *dev = &sdev->sdev_gendev;
895
896 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
897 return;
898
899 bsg_unregister_queue(sdev->request_queue);
900 device_unregister(&sdev->sdev_dev);
901 transport_remove_device(dev);
902 device_del(dev);
903 scsi_device_set_state(sdev, SDEV_DEL);
904 if (sdev->host->hostt->slave_destroy)
905 sdev->host->hostt->slave_destroy(sdev);
906 transport_destroy_device(dev);
907 put_device(dev);
908 }
909
910 /**
911 * scsi_remove_device - unregister a device from the scsi bus
912 * @sdev: scsi_device to unregister
913 **/
914 void scsi_remove_device(struct scsi_device *sdev)
915 {
916 struct Scsi_Host *shost = sdev->host;
917
918 mutex_lock(&shost->scan_mutex);
919 __scsi_remove_device(sdev);
920 mutex_unlock(&shost->scan_mutex);
921 }
922 EXPORT_SYMBOL(scsi_remove_device);
923
924 static void __scsi_remove_target(struct scsi_target *starget)
925 {
926 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
927 unsigned long flags;
928 struct scsi_device *sdev;
929
930 spin_lock_irqsave(shost->host_lock, flags);
931 starget->reap_ref++;
932 restart:
933 list_for_each_entry(sdev, &shost->__devices, siblings) {
934 if (sdev->channel != starget->channel ||
935 sdev->id != starget->id ||
936 sdev->sdev_state == SDEV_DEL)
937 continue;
938 spin_unlock_irqrestore(shost->host_lock, flags);
939 scsi_remove_device(sdev);
940 spin_lock_irqsave(shost->host_lock, flags);
941 goto restart;
942 }
943 spin_unlock_irqrestore(shost->host_lock, flags);
944 scsi_target_reap(starget);
945 }
946
947 static int __remove_child (struct device * dev, void * data)
948 {
949 if (scsi_is_target_device(dev))
950 __scsi_remove_target(to_scsi_target(dev));
951 return 0;
952 }
953
954 /**
955 * scsi_remove_target - try to remove a target and all its devices
956 * @dev: generic starget or parent of generic stargets to be removed
957 *
958 * Note: This is slightly racy. It is possible that if the user
959 * requests the addition of another device then the target won't be
960 * removed.
961 */
962 void scsi_remove_target(struct device *dev)
963 {
964 struct device *rdev;
965
966 if (scsi_is_target_device(dev)) {
967 __scsi_remove_target(to_scsi_target(dev));
968 return;
969 }
970
971 rdev = get_device(dev);
972 device_for_each_child(dev, NULL, __remove_child);
973 put_device(rdev);
974 }
975 EXPORT_SYMBOL(scsi_remove_target);
976
977 int scsi_register_driver(struct device_driver *drv)
978 {
979 drv->bus = &scsi_bus_type;
980
981 return driver_register(drv);
982 }
983 EXPORT_SYMBOL(scsi_register_driver);
984
985 int scsi_register_interface(struct class_interface *intf)
986 {
987 intf->class = &sdev_class;
988
989 return class_interface_register(intf);
990 }
991 EXPORT_SYMBOL(scsi_register_interface);
992
993
994 static struct device_attribute *class_attr_overridden(
995 struct device_attribute **attrs,
996 struct device_attribute *attr)
997 {
998 int i;
999
1000 if (!attrs)
1001 return NULL;
1002 for (i = 0; attrs[i]; i++)
1003 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
1004 return attrs[i];
1005 return NULL;
1006 }
1007
1008 static int class_attr_add(struct device *classdev,
1009 struct device_attribute *attr)
1010 {
1011 struct device_attribute *base_attr;
1012
1013 /*
1014 * Spare the caller from having to copy things it's not interested in.
1015 */
1016 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
1017 if (base_attr) {
1018 /* extend permissions */
1019 attr->attr.mode |= base_attr->attr.mode;
1020
1021 /* override null show/store with default */
1022 if (!attr->show)
1023 attr->show = base_attr->show;
1024 if (!attr->store)
1025 attr->store = base_attr->store;
1026 }
1027
1028 return device_create_file(classdev, attr);
1029 }
1030
1031 /**
1032 * scsi_sysfs_add_host - add scsi host to subsystem
1033 * @shost: scsi host struct to add to subsystem
1034 * @dev: parent struct device pointer
1035 **/
1036 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1037 {
1038 int error, i;
1039
1040 if (shost->hostt->shost_attrs) {
1041 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1042 error = class_attr_add(&shost->shost_dev,
1043 shost->hostt->shost_attrs[i]);
1044 if (error)
1045 return error;
1046 }
1047 }
1048
1049 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
1050 if (!class_attr_overridden(shost->hostt->shost_attrs,
1051 scsi_sysfs_shost_attrs[i])) {
1052 error = device_create_file(&shost->shost_dev,
1053 scsi_sysfs_shost_attrs[i]);
1054 if (error)
1055 return error;
1056 }
1057 }
1058
1059 transport_register_device(&shost->shost_gendev);
1060 transport_configure_device(&shost->shost_gendev);
1061 return 0;
1062 }
1063
1064 static struct device_type scsi_dev_type = {
1065 .name = "scsi_device",
1066 .release = scsi_device_dev_release,
1067 .groups = scsi_sdev_attr_groups,
1068 };
1069
1070 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1071 {
1072 unsigned long flags;
1073 struct Scsi_Host *shost = sdev->host;
1074 struct scsi_target *starget = sdev->sdev_target;
1075
1076 device_initialize(&sdev->sdev_gendev);
1077 sdev->sdev_gendev.bus = &scsi_bus_type;
1078 sdev->sdev_gendev.type = &scsi_dev_type;
1079 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1080 sdev->host->host_no, sdev->channel, sdev->id,
1081 sdev->lun);
1082
1083 device_initialize(&sdev->sdev_dev);
1084 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1085 sdev->sdev_dev.class = &sdev_class;
1086 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
1087 "%d:%d:%d:%d", sdev->host->host_no,
1088 sdev->channel, sdev->id, sdev->lun);
1089 sdev->scsi_level = starget->scsi_level;
1090 transport_setup_device(&sdev->sdev_gendev);
1091 spin_lock_irqsave(shost->host_lock, flags);
1092 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1093 list_add_tail(&sdev->siblings, &shost->__devices);
1094 spin_unlock_irqrestore(shost->host_lock, flags);
1095 }
1096
1097 int scsi_is_sdev_device(const struct device *dev)
1098 {
1099 return dev->type == &scsi_dev_type;
1100 }
1101 EXPORT_SYMBOL(scsi_is_sdev_device);
1102
1103 /* A blank transport template that is used in drivers that don't
1104 * yet implement Transport Attributes */
1105 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };