4 * SCSI sysfs interface routines.
6 * Created to pull SCSI mid layer sysfs routines into one file.
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/device.h>
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_device.h>
17 #include <scsi/scsi_host.h>
18 #include <scsi/scsi_tcq.h>
19 #include <scsi/scsi_transport.h>
21 #include "scsi_priv.h"
22 #include "scsi_logging.h"
25 enum scsi_device_state value;
28 { SDEV_CREATED, "created" },
29 { SDEV_RUNNING, "running" },
30 { SDEV_CANCEL, "cancel" },
31 { SDEV_DEL, "deleted" },
32 { SDEV_QUIESCE, "quiesce" },
33 { SDEV_OFFLINE, "offline" },
34 { SDEV_BLOCK, "blocked" },
37 const char *scsi_device_state_name(enum scsi_device_state state)
42 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
43 if (sdev_states[i].value == state) {
44 name = sdev_states[i].name;
52 enum scsi_host_state value;
55 { SHOST_CREATED, "created" },
56 { SHOST_RUNNING, "running" },
57 { SHOST_CANCEL, "cancel" },
58 { SHOST_DEL, "deleted" },
59 { SHOST_RECOVERY, "recovery" },
61 const char *scsi_host_state_name(enum scsi_host_state state)
66 for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
67 if (shost_states[i].value == state) {
68 name = shost_states[i].name;
75 static int check_set(unsigned int *val, char *src)
79 if (strncmp(src, "-", 20) == 0) {
80 *val = SCAN_WILD_CARD;
83 * Doesn't check for int overflow
85 *val = simple_strtoul(src, &last, 0);
92 static int scsi_scan(struct Scsi_Host *shost, const char *str)
94 char s1[15], s2[15], s3[15], junk;
95 unsigned int channel, id, lun;
98 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
101 if (check_set(&channel, s1))
103 if (check_set(&id, s2))
105 if (check_set(&lun, s3))
107 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
112 * shost_show_function: macro to create an attr function that can be used to
113 * show a non-bit field.
115 #define shost_show_function(name, field, format_string) \
117 show_##name (struct class_device *class_dev, char *buf) \
119 struct Scsi_Host *shost = class_to_shost(class_dev); \
120 return snprintf (buf, 20, format_string, shost->field); \
124 * shost_rd_attr: macro to create a function and attribute variable for a
127 #define shost_rd_attr2(name, field, format_string) \
128 shost_show_function(name, field, format_string) \
129 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
131 #define shost_rd_attr(field, format_string) \
132 shost_rd_attr2(field, field, format_string)
135 * Create the actual show/store functions and data structures.
138 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
141 struct Scsi_Host *shost = class_to_shost(class_dev);
144 res = scsi_scan(shost, buf);
149 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
152 store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
155 struct Scsi_Host *shost = class_to_shost(class_dev);
156 enum scsi_host_state state = 0;
158 for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
159 const int len = strlen(shost_states[i].name);
160 if (strncmp(shost_states[i].name, buf, len) == 0 &&
162 state = shost_states[i].value;
169 if (scsi_host_set_state(shost, state))
175 show_shost_state(struct class_device *class_dev, char *buf)
177 struct Scsi_Host *shost = class_to_shost(class_dev);
178 const char *name = scsi_host_state_name(shost->shost_state);
183 return snprintf(buf, 20, "%s\n", name);
186 static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
188 shost_rd_attr(unique_id, "%u\n");
189 shost_rd_attr(host_busy, "%hu\n");
190 shost_rd_attr(cmd_per_lun, "%hd\n");
191 shost_rd_attr(sg_tablesize, "%hu\n");
192 shost_rd_attr(unchecked_isa_dma, "%d\n");
193 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
195 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
196 &class_device_attr_unique_id,
197 &class_device_attr_host_busy,
198 &class_device_attr_cmd_per_lun,
199 &class_device_attr_sg_tablesize,
200 &class_device_attr_unchecked_isa_dma,
201 &class_device_attr_proc_name,
202 &class_device_attr_scan,
203 &class_device_attr_state,
207 static void scsi_device_cls_release(struct class_device *class_dev)
209 struct scsi_device *sdev;
211 sdev = class_to_sdev(class_dev);
212 put_device(&sdev->sdev_gendev);
215 static void scsi_device_dev_release(struct device *dev)
217 struct scsi_device *sdev;
218 struct device *parent;
219 struct scsi_target *starget;
222 parent = dev->parent;
223 sdev = to_scsi_device(dev);
224 starget = to_scsi_target(parent);
226 spin_lock_irqsave(sdev->host->host_lock, flags);
228 list_del(&sdev->siblings);
229 list_del(&sdev->same_target_siblings);
230 list_del(&sdev->starved_entry);
231 spin_unlock_irqrestore(sdev->host->host_lock, flags);
233 if (sdev->request_queue) {
234 sdev->request_queue->queuedata = NULL;
235 scsi_free_queue(sdev->request_queue);
236 /* temporary expedient, try to catch use of queue lock
237 * after free of sdev */
238 sdev->request_queue = NULL;
241 scsi_target_reap(scsi_target(sdev));
243 kfree(sdev->inquiry);
250 static struct class sdev_class = {
251 .name = "scsi_device",
252 .release = scsi_device_cls_release,
255 /* all probing is done in the individual ->probe routines */
256 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
258 struct scsi_device *sdp = to_scsi_device(dev);
259 if (sdp->no_uld_attach)
261 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
264 struct bus_type scsi_bus_type = {
266 .match = scsi_bus_match,
269 int scsi_sysfs_register(void)
273 error = bus_register(&scsi_bus_type);
275 error = class_register(&sdev_class);
277 bus_unregister(&scsi_bus_type);
283 void scsi_sysfs_unregister(void)
285 class_unregister(&sdev_class);
286 bus_unregister(&scsi_bus_type);
290 * sdev_show_function: macro to create an attr function that can be used to
291 * show a non-bit field.
293 #define sdev_show_function(field, format_string) \
295 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
297 struct scsi_device *sdev; \
298 sdev = to_scsi_device(dev); \
299 return snprintf (buf, 20, format_string, sdev->field); \
303 * sdev_rd_attr: macro to create a function and attribute variable for a
306 #define sdev_rd_attr(field, format_string) \
307 sdev_show_function(field, format_string) \
308 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
312 * sdev_rd_attr: create a function and attribute variable for a
315 #define sdev_rw_attr(field, format_string) \
316 sdev_show_function(field, format_string) \
319 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
321 struct scsi_device *sdev; \
322 sdev = to_scsi_device(dev); \
323 snscanf (buf, 20, format_string, &sdev->field); \
326 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
328 /* Currently we don't export bit fields, but we might in future,
329 * so leave this code in */
332 * sdev_rd_attr: create a function and attribute variable for a
333 * read/write bit field.
335 #define sdev_rw_attr_bit(field) \
336 sdev_show_function(field, "%d\n") \
339 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
342 struct scsi_device *sdev; \
343 ret = scsi_sdev_check_buf_bit(buf); \
345 sdev = to_scsi_device(dev); \
351 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
354 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
355 * else return -EINVAL.
357 static int scsi_sdev_check_buf_bit(const char *buf)
359 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
362 else if (buf[0] == '0')
371 * Create the actual show/store functions and data structures.
373 sdev_rd_attr (device_blocked, "%d\n");
374 sdev_rd_attr (queue_depth, "%d\n");
375 sdev_rd_attr (type, "%d\n");
376 sdev_rd_attr (scsi_level, "%d\n");
377 sdev_rd_attr (vendor, "%.8s\n");
378 sdev_rd_attr (model, "%.16s\n");
379 sdev_rd_attr (rev, "%.4s\n");
382 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
384 struct scsi_device *sdev;
385 sdev = to_scsi_device(dev);
386 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
390 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
392 struct scsi_device *sdev;
394 sdev = to_scsi_device(dev);
395 sscanf (buf, "%d\n", &timeout);
396 sdev->timeout = timeout * HZ;
399 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
402 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
404 scsi_rescan_device(dev);
407 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
409 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
412 scsi_remove_device(to_scsi_device(dev));
415 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
418 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
421 struct scsi_device *sdev = to_scsi_device(dev);
422 enum scsi_device_state state = 0;
424 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
425 const int len = strlen(sdev_states[i].name);
426 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
428 state = sdev_states[i].value;
435 if (scsi_device_set_state(sdev, state))
441 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
443 struct scsi_device *sdev = to_scsi_device(dev);
444 const char *name = scsi_device_state_name(sdev->sdev_state);
449 return snprintf(buf, 20, "%s\n", name);
452 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
455 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
457 struct scsi_device *sdev = to_scsi_device(dev);
458 const char *name = "none";
460 if (sdev->ordered_tags)
462 else if (sdev->simple_tags)
465 return snprintf(buf, 20, "%s\n", name);
468 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
471 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
473 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
476 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
478 #define show_sdev_iostat(field) \
480 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
482 struct scsi_device *sdev = to_scsi_device(dev); \
483 unsigned long long count = atomic_read(&sdev->field); \
484 return snprintf(buf, 20, "0x%llx\n", count); \
486 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
488 show_sdev_iostat(iorequest_cnt);
489 show_sdev_iostat(iodone_cnt);
490 show_sdev_iostat(ioerr_cnt);
493 /* Default template for device attributes. May NOT be modified */
494 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
495 &dev_attr_device_blocked,
496 &dev_attr_queue_depth,
497 &dev_attr_queue_type,
499 &dev_attr_scsi_level,
507 &dev_attr_iocounterbits,
508 &dev_attr_iorequest_cnt,
509 &dev_attr_iodone_cnt,
514 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
518 struct scsi_device *sdev = to_scsi_device(dev);
519 struct scsi_host_template *sht = sdev->host->hostt;
521 if (!sht->change_queue_depth)
524 depth = simple_strtoul(buf, NULL, 0);
529 retval = sht->change_queue_depth(sdev, depth);
536 static struct device_attribute sdev_attr_queue_depth_rw =
537 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
538 sdev_store_queue_depth_rw);
540 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
543 struct scsi_device *sdev = to_scsi_device(dev);
544 struct scsi_host_template *sht = sdev->host->hostt;
545 int tag_type = 0, retval;
546 int prev_tag_type = scsi_get_tag_type(sdev);
548 if (!sdev->tagged_supported || !sht->change_queue_type)
551 if (strncmp(buf, "ordered", 7) == 0)
552 tag_type = MSG_ORDERED_TAG;
553 else if (strncmp(buf, "simple", 6) == 0)
554 tag_type = MSG_SIMPLE_TAG;
555 else if (strncmp(buf, "none", 4) != 0)
558 if (tag_type == prev_tag_type)
561 retval = sht->change_queue_type(sdev, tag_type);
568 static struct device_attribute sdev_attr_queue_type_rw =
569 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
570 sdev_store_queue_type_rw);
572 static struct device_attribute *attr_changed_internally(
573 struct Scsi_Host *shost,
574 struct device_attribute * attr)
576 if (!strcmp("queue_depth", attr->attr.name)
577 && shost->hostt->change_queue_depth)
578 return &sdev_attr_queue_depth_rw;
579 else if (!strcmp("queue_type", attr->attr.name)
580 && shost->hostt->change_queue_type)
581 return &sdev_attr_queue_type_rw;
586 static struct device_attribute *attr_overridden(
587 struct device_attribute **attrs,
588 struct device_attribute *attr)
594 for (i = 0; attrs[i]; i++)
595 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
600 static int attr_add(struct device *dev, struct device_attribute *attr)
602 struct device_attribute *base_attr;
605 * Spare the caller from having to copy things it's not interested in.
607 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
609 /* extend permissions */
610 attr->attr.mode |= base_attr->attr.mode;
612 /* override null show/store with default */
614 attr->show = base_attr->show;
616 attr->store = base_attr->store;
619 return device_create_file(dev, attr);
623 * scsi_sysfs_add_sdev - add scsi device to sysfs
624 * @sdev: scsi_device to add
627 * 0 on Success / non-zero on Failure
629 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
633 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
636 error = device_add(&sdev->sdev_gendev);
638 put_device(sdev->sdev_gendev.parent);
639 printk(KERN_INFO "error 1\n");
642 error = class_device_add(&sdev->sdev_classdev);
644 printk(KERN_INFO "error 2\n");
648 /* take a reference for the sdev_classdev; this is
649 * released by the sdev_class .release */
650 get_device(&sdev->sdev_gendev);
651 if (sdev->host->hostt->sdev_attrs) {
652 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
653 error = attr_add(&sdev->sdev_gendev,
654 sdev->host->hostt->sdev_attrs[i]);
656 __scsi_remove_device(sdev);
662 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
663 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
664 scsi_sysfs_sdev_attrs[i])) {
665 struct device_attribute * attr =
666 attr_changed_internally(sdev->host,
667 scsi_sysfs_sdev_attrs[i]);
668 error = device_create_file(&sdev->sdev_gendev, attr);
670 __scsi_remove_device(sdev);
676 transport_add_device(&sdev->sdev_gendev);
681 scsi_device_set_state(sdev, SDEV_CANCEL);
683 device_del(&sdev->sdev_gendev);
684 transport_destroy_device(&sdev->sdev_gendev);
685 put_device(&sdev->sdev_gendev);
690 void __scsi_remove_device(struct scsi_device *sdev)
692 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
695 class_device_unregister(&sdev->sdev_classdev);
696 device_del(&sdev->sdev_gendev);
697 scsi_device_set_state(sdev, SDEV_DEL);
698 if (sdev->host->hostt->slave_destroy)
699 sdev->host->hostt->slave_destroy(sdev);
700 transport_unregister_device(&sdev->sdev_gendev);
701 put_device(&sdev->sdev_gendev);
705 * scsi_remove_device - unregister a device from the scsi bus
706 * @sdev: scsi_device to unregister
708 void scsi_remove_device(struct scsi_device *sdev)
710 down(&sdev->host->scan_mutex);
711 __scsi_remove_device(sdev);
712 up(&sdev->host->scan_mutex);
714 EXPORT_SYMBOL(scsi_remove_device);
716 void __scsi_remove_target(struct scsi_target *starget)
718 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
720 struct scsi_device *sdev, *tmp;
722 spin_lock_irqsave(shost->host_lock, flags);
724 list_for_each_entry_safe(sdev, tmp, &shost->__devices, siblings) {
725 if (sdev->channel != starget->channel ||
726 sdev->id != starget->id)
728 spin_unlock_irqrestore(shost->host_lock, flags);
729 scsi_remove_device(sdev);
730 spin_lock_irqsave(shost->host_lock, flags);
732 spin_unlock_irqrestore(shost->host_lock, flags);
733 scsi_target_reap(starget);
736 static int __remove_child (struct device * dev, void * data)
738 if (scsi_is_target_device(dev))
739 __scsi_remove_target(to_scsi_target(dev));
744 * scsi_remove_target - try to remove a target and all its devices
745 * @dev: generic starget or parent of generic stargets to be removed
747 * Note: This is slightly racy. It is possible that if the user
748 * requests the addition of another device then the target won't be
751 void scsi_remove_target(struct device *dev)
755 if (scsi_is_target_device(dev)) {
756 __scsi_remove_target(to_scsi_target(dev));
760 rdev = get_device(dev);
761 device_for_each_child(dev, NULL, __remove_child);
764 EXPORT_SYMBOL(scsi_remove_target);
766 int scsi_register_driver(struct device_driver *drv)
768 drv->bus = &scsi_bus_type;
770 return driver_register(drv);
772 EXPORT_SYMBOL(scsi_register_driver);
774 int scsi_register_interface(struct class_interface *intf)
776 intf->class = &sdev_class;
778 return class_interface_register(intf);
780 EXPORT_SYMBOL(scsi_register_interface);
783 static struct class_device_attribute *class_attr_overridden(
784 struct class_device_attribute **attrs,
785 struct class_device_attribute *attr)
791 for (i = 0; attrs[i]; i++)
792 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
797 static int class_attr_add(struct class_device *classdev,
798 struct class_device_attribute *attr)
800 struct class_device_attribute *base_attr;
803 * Spare the caller from having to copy things it's not interested in.
805 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
807 /* extend permissions */
808 attr->attr.mode |= base_attr->attr.mode;
810 /* override null show/store with default */
812 attr->show = base_attr->show;
814 attr->store = base_attr->store;
817 return class_device_create_file(classdev, attr);
821 * scsi_sysfs_add_host - add scsi host to subsystem
822 * @shost: scsi host struct to add to subsystem
823 * @dev: parent struct device pointer
825 int scsi_sysfs_add_host(struct Scsi_Host *shost)
829 if (shost->hostt->shost_attrs) {
830 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
831 error = class_attr_add(&shost->shost_classdev,
832 shost->hostt->shost_attrs[i]);
838 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
839 if (!class_attr_overridden(shost->hostt->shost_attrs,
840 scsi_sysfs_shost_attrs[i])) {
841 error = class_device_create_file(&shost->shost_classdev,
842 scsi_sysfs_shost_attrs[i]);
848 transport_register_device(&shost->shost_gendev);
852 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
855 struct Scsi_Host *shost = sdev->host;
856 struct scsi_target *starget = sdev->sdev_target;
858 device_initialize(&sdev->sdev_gendev);
859 sdev->sdev_gendev.bus = &scsi_bus_type;
860 sdev->sdev_gendev.release = scsi_device_dev_release;
861 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
862 sdev->host->host_no, sdev->channel, sdev->id,
865 class_device_initialize(&sdev->sdev_classdev);
866 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
867 sdev->sdev_classdev.class = &sdev_class;
868 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
869 "%d:%d:%d:%d", sdev->host->host_no,
870 sdev->channel, sdev->id, sdev->lun);
871 sdev->scsi_level = SCSI_2;
872 transport_setup_device(&sdev->sdev_gendev);
873 spin_lock_irqsave(shost->host_lock, flags);
874 list_add_tail(&sdev->same_target_siblings, &starget->devices);
875 list_add_tail(&sdev->siblings, &shost->__devices);
876 spin_unlock_irqrestore(shost->host_lock, flags);
879 int scsi_is_sdev_device(const struct device *dev)
881 return dev->release == scsi_device_dev_release;
883 EXPORT_SYMBOL(scsi_is_sdev_device);
885 /* A blank transport template that is used in drivers that don't
886 * yet implement Transport Attributes */
887 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };