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" },
60 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
61 { SHOST_DEL_RECOVERY, "deleted/recovery", },
63 const char *scsi_host_state_name(enum scsi_host_state state)
68 for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
69 if (shost_states[i].value == state) {
70 name = shost_states[i].name;
77 static int check_set(unsigned int *val, char *src)
81 if (strncmp(src, "-", 20) == 0) {
82 *val = SCAN_WILD_CARD;
85 * Doesn't check for int overflow
87 *val = simple_strtoul(src, &last, 0);
94 static int scsi_scan(struct Scsi_Host *shost, const char *str)
96 char s1[15], s2[15], s3[15], junk;
97 unsigned int channel, id, lun;
100 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
103 if (check_set(&channel, s1))
105 if (check_set(&id, s2))
107 if (check_set(&lun, s3))
109 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
114 * shost_show_function: macro to create an attr function that can be used to
115 * show a non-bit field.
117 #define shost_show_function(name, field, format_string) \
119 show_##name (struct class_device *class_dev, char *buf) \
121 struct Scsi_Host *shost = class_to_shost(class_dev); \
122 return snprintf (buf, 20, format_string, shost->field); \
126 * shost_rd_attr: macro to create a function and attribute variable for a
129 #define shost_rd_attr2(name, field, format_string) \
130 shost_show_function(name, field, format_string) \
131 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
133 #define shost_rd_attr(field, format_string) \
134 shost_rd_attr2(field, field, format_string)
137 * Create the actual show/store functions and data structures.
140 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
143 struct Scsi_Host *shost = class_to_shost(class_dev);
146 res = scsi_scan(shost, buf);
151 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
154 store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
157 struct Scsi_Host *shost = class_to_shost(class_dev);
158 enum scsi_host_state state = 0;
160 for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
161 const int len = strlen(shost_states[i].name);
162 if (strncmp(shost_states[i].name, buf, len) == 0 &&
164 state = shost_states[i].value;
171 if (scsi_host_set_state(shost, state))
177 show_shost_state(struct class_device *class_dev, char *buf)
179 struct Scsi_Host *shost = class_to_shost(class_dev);
180 const char *name = scsi_host_state_name(shost->shost_state);
185 return snprintf(buf, 20, "%s\n", name);
188 static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
190 shost_rd_attr(unique_id, "%u\n");
191 shost_rd_attr(host_busy, "%hu\n");
192 shost_rd_attr(cmd_per_lun, "%hd\n");
193 shost_rd_attr(sg_tablesize, "%hu\n");
194 shost_rd_attr(unchecked_isa_dma, "%d\n");
195 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
197 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
198 &class_device_attr_unique_id,
199 &class_device_attr_host_busy,
200 &class_device_attr_cmd_per_lun,
201 &class_device_attr_sg_tablesize,
202 &class_device_attr_unchecked_isa_dma,
203 &class_device_attr_proc_name,
204 &class_device_attr_scan,
205 &class_device_attr_state,
209 static void scsi_device_cls_release(struct class_device *class_dev)
211 struct scsi_device *sdev;
213 sdev = class_to_sdev(class_dev);
214 put_device(&sdev->sdev_gendev);
217 static void scsi_device_dev_release(struct device *dev)
219 struct scsi_device *sdev;
220 struct device *parent;
221 struct scsi_target *starget;
224 parent = dev->parent;
225 sdev = to_scsi_device(dev);
226 starget = to_scsi_target(parent);
228 spin_lock_irqsave(sdev->host->host_lock, flags);
230 list_del(&sdev->siblings);
231 list_del(&sdev->same_target_siblings);
232 list_del(&sdev->starved_entry);
233 spin_unlock_irqrestore(sdev->host->host_lock, flags);
235 if (sdev->request_queue) {
236 sdev->request_queue->queuedata = NULL;
237 scsi_free_queue(sdev->request_queue);
238 /* temporary expedient, try to catch use of queue lock
239 * after free of sdev */
240 sdev->request_queue = NULL;
243 scsi_target_reap(scsi_target(sdev));
245 kfree(sdev->inquiry);
252 static struct class sdev_class = {
253 .name = "scsi_device",
254 .release = scsi_device_cls_release,
257 /* all probing is done in the individual ->probe routines */
258 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
260 struct scsi_device *sdp = to_scsi_device(dev);
261 if (sdp->no_uld_attach)
263 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
266 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
268 struct scsi_device *sdev = to_scsi_device(dev);
269 struct scsi_host_template *sht = sdev->host->hostt;
272 err = scsi_device_quiesce(sdev);
277 err = sht->suspend(sdev);
282 static int scsi_bus_resume(struct device * dev)
284 struct scsi_device *sdev = to_scsi_device(dev);
285 struct scsi_host_template *sht = sdev->host->hostt;
289 err = sht->resume(sdev);
291 scsi_device_resume(sdev);
295 struct bus_type scsi_bus_type = {
297 .match = scsi_bus_match,
298 .suspend = scsi_bus_suspend,
299 .resume = scsi_bus_resume,
302 int scsi_sysfs_register(void)
306 error = bus_register(&scsi_bus_type);
308 error = class_register(&sdev_class);
310 bus_unregister(&scsi_bus_type);
316 void scsi_sysfs_unregister(void)
318 class_unregister(&sdev_class);
319 bus_unregister(&scsi_bus_type);
323 * sdev_show_function: macro to create an attr function that can be used to
324 * show a non-bit field.
326 #define sdev_show_function(field, format_string) \
328 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
330 struct scsi_device *sdev; \
331 sdev = to_scsi_device(dev); \
332 return snprintf (buf, 20, format_string, sdev->field); \
336 * sdev_rd_attr: macro to create a function and attribute variable for a
339 #define sdev_rd_attr(field, format_string) \
340 sdev_show_function(field, format_string) \
341 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
345 * sdev_rd_attr: create a function and attribute variable for a
348 #define sdev_rw_attr(field, format_string) \
349 sdev_show_function(field, format_string) \
352 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
354 struct scsi_device *sdev; \
355 sdev = to_scsi_device(dev); \
356 snscanf (buf, 20, format_string, &sdev->field); \
359 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
361 /* Currently we don't export bit fields, but we might in future,
362 * so leave this code in */
365 * sdev_rd_attr: create a function and attribute variable for a
366 * read/write bit field.
368 #define sdev_rw_attr_bit(field) \
369 sdev_show_function(field, "%d\n") \
372 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
375 struct scsi_device *sdev; \
376 ret = scsi_sdev_check_buf_bit(buf); \
378 sdev = to_scsi_device(dev); \
384 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
387 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
388 * else return -EINVAL.
390 static int scsi_sdev_check_buf_bit(const char *buf)
392 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
395 else if (buf[0] == '0')
404 * Create the actual show/store functions and data structures.
406 sdev_rd_attr (device_blocked, "%d\n");
407 sdev_rd_attr (queue_depth, "%d\n");
408 sdev_rd_attr (type, "%d\n");
409 sdev_rd_attr (scsi_level, "%d\n");
410 sdev_rd_attr (vendor, "%.8s\n");
411 sdev_rd_attr (model, "%.16s\n");
412 sdev_rd_attr (rev, "%.4s\n");
415 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
417 struct scsi_device *sdev;
418 sdev = to_scsi_device(dev);
419 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
423 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
425 struct scsi_device *sdev;
427 sdev = to_scsi_device(dev);
428 sscanf (buf, "%d\n", &timeout);
429 sdev->timeout = timeout * HZ;
432 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
435 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
437 scsi_rescan_device(dev);
440 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
442 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
445 scsi_remove_device(to_scsi_device(dev));
448 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
451 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
454 struct scsi_device *sdev = to_scsi_device(dev);
455 enum scsi_device_state state = 0;
457 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
458 const int len = strlen(sdev_states[i].name);
459 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
461 state = sdev_states[i].value;
468 if (scsi_device_set_state(sdev, state))
474 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
476 struct scsi_device *sdev = to_scsi_device(dev);
477 const char *name = scsi_device_state_name(sdev->sdev_state);
482 return snprintf(buf, 20, "%s\n", name);
485 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
488 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
490 struct scsi_device *sdev = to_scsi_device(dev);
491 const char *name = "none";
493 if (sdev->ordered_tags)
495 else if (sdev->simple_tags)
498 return snprintf(buf, 20, "%s\n", name);
501 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
504 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
506 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
509 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
511 #define show_sdev_iostat(field) \
513 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
515 struct scsi_device *sdev = to_scsi_device(dev); \
516 unsigned long long count = atomic_read(&sdev->field); \
517 return snprintf(buf, 20, "0x%llx\n", count); \
519 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
521 show_sdev_iostat(iorequest_cnt);
522 show_sdev_iostat(iodone_cnt);
523 show_sdev_iostat(ioerr_cnt);
526 /* Default template for device attributes. May NOT be modified */
527 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
528 &dev_attr_device_blocked,
529 &dev_attr_queue_depth,
530 &dev_attr_queue_type,
532 &dev_attr_scsi_level,
540 &dev_attr_iocounterbits,
541 &dev_attr_iorequest_cnt,
542 &dev_attr_iodone_cnt,
547 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
551 struct scsi_device *sdev = to_scsi_device(dev);
552 struct scsi_host_template *sht = sdev->host->hostt;
554 if (!sht->change_queue_depth)
557 depth = simple_strtoul(buf, NULL, 0);
562 retval = sht->change_queue_depth(sdev, depth);
569 static struct device_attribute sdev_attr_queue_depth_rw =
570 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
571 sdev_store_queue_depth_rw);
573 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
576 struct scsi_device *sdev = to_scsi_device(dev);
577 struct scsi_host_template *sht = sdev->host->hostt;
578 int tag_type = 0, retval;
579 int prev_tag_type = scsi_get_tag_type(sdev);
581 if (!sdev->tagged_supported || !sht->change_queue_type)
584 if (strncmp(buf, "ordered", 7) == 0)
585 tag_type = MSG_ORDERED_TAG;
586 else if (strncmp(buf, "simple", 6) == 0)
587 tag_type = MSG_SIMPLE_TAG;
588 else if (strncmp(buf, "none", 4) != 0)
591 if (tag_type == prev_tag_type)
594 retval = sht->change_queue_type(sdev, tag_type);
601 static struct device_attribute sdev_attr_queue_type_rw =
602 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
603 sdev_store_queue_type_rw);
605 static struct device_attribute *attr_changed_internally(
606 struct Scsi_Host *shost,
607 struct device_attribute * attr)
609 if (!strcmp("queue_depth", attr->attr.name)
610 && shost->hostt->change_queue_depth)
611 return &sdev_attr_queue_depth_rw;
612 else if (!strcmp("queue_type", attr->attr.name)
613 && shost->hostt->change_queue_type)
614 return &sdev_attr_queue_type_rw;
619 static struct device_attribute *attr_overridden(
620 struct device_attribute **attrs,
621 struct device_attribute *attr)
627 for (i = 0; attrs[i]; i++)
628 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
633 static int attr_add(struct device *dev, struct device_attribute *attr)
635 struct device_attribute *base_attr;
638 * Spare the caller from having to copy things it's not interested in.
640 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
642 /* extend permissions */
643 attr->attr.mode |= base_attr->attr.mode;
645 /* override null show/store with default */
647 attr->show = base_attr->show;
649 attr->store = base_attr->store;
652 return device_create_file(dev, attr);
656 * scsi_sysfs_add_sdev - add scsi device to sysfs
657 * @sdev: scsi_device to add
660 * 0 on Success / non-zero on Failure
662 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
666 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
669 error = device_add(&sdev->sdev_gendev);
671 put_device(sdev->sdev_gendev.parent);
672 printk(KERN_INFO "error 1\n");
675 error = class_device_add(&sdev->sdev_classdev);
677 printk(KERN_INFO "error 2\n");
681 /* take a reference for the sdev_classdev; this is
682 * released by the sdev_class .release */
683 get_device(&sdev->sdev_gendev);
684 if (sdev->host->hostt->sdev_attrs) {
685 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
686 error = attr_add(&sdev->sdev_gendev,
687 sdev->host->hostt->sdev_attrs[i]);
689 __scsi_remove_device(sdev);
695 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
696 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
697 scsi_sysfs_sdev_attrs[i])) {
698 struct device_attribute * attr =
699 attr_changed_internally(sdev->host,
700 scsi_sysfs_sdev_attrs[i]);
701 error = device_create_file(&sdev->sdev_gendev, attr);
703 __scsi_remove_device(sdev);
709 transport_add_device(&sdev->sdev_gendev);
714 scsi_device_set_state(sdev, SDEV_CANCEL);
716 device_del(&sdev->sdev_gendev);
717 transport_destroy_device(&sdev->sdev_gendev);
718 put_device(&sdev->sdev_gendev);
723 void __scsi_remove_device(struct scsi_device *sdev)
725 struct device *dev = &sdev->sdev_gendev;
727 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
730 class_device_unregister(&sdev->sdev_classdev);
731 transport_remove_device(dev);
733 scsi_device_set_state(sdev, SDEV_DEL);
734 if (sdev->host->hostt->slave_destroy)
735 sdev->host->hostt->slave_destroy(sdev);
736 transport_destroy_device(dev);
741 * scsi_remove_device - unregister a device from the scsi bus
742 * @sdev: scsi_device to unregister
744 void scsi_remove_device(struct scsi_device *sdev)
746 struct Scsi_Host *shost = sdev->host;
748 down(&shost->scan_mutex);
749 __scsi_remove_device(sdev);
750 up(&shost->scan_mutex);
752 EXPORT_SYMBOL(scsi_remove_device);
754 void __scsi_remove_target(struct scsi_target *starget)
756 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
758 struct scsi_device *sdev;
760 spin_lock_irqsave(shost->host_lock, flags);
763 list_for_each_entry(sdev, &shost->__devices, siblings) {
764 if (sdev->channel != starget->channel ||
765 sdev->id != starget->id ||
766 sdev->sdev_state == SDEV_DEL)
768 spin_unlock_irqrestore(shost->host_lock, flags);
769 scsi_remove_device(sdev);
770 spin_lock_irqsave(shost->host_lock, flags);
773 spin_unlock_irqrestore(shost->host_lock, flags);
774 scsi_target_reap(starget);
777 static int __remove_child (struct device * dev, void * data)
779 if (scsi_is_target_device(dev))
780 __scsi_remove_target(to_scsi_target(dev));
785 * scsi_remove_target - try to remove a target and all its devices
786 * @dev: generic starget or parent of generic stargets to be removed
788 * Note: This is slightly racy. It is possible that if the user
789 * requests the addition of another device then the target won't be
792 void scsi_remove_target(struct device *dev)
796 if (scsi_is_target_device(dev)) {
797 __scsi_remove_target(to_scsi_target(dev));
801 rdev = get_device(dev);
802 device_for_each_child(dev, NULL, __remove_child);
805 EXPORT_SYMBOL(scsi_remove_target);
807 int scsi_register_driver(struct device_driver *drv)
809 drv->bus = &scsi_bus_type;
811 return driver_register(drv);
813 EXPORT_SYMBOL(scsi_register_driver);
815 int scsi_register_interface(struct class_interface *intf)
817 intf->class = &sdev_class;
819 return class_interface_register(intf);
821 EXPORT_SYMBOL(scsi_register_interface);
824 static struct class_device_attribute *class_attr_overridden(
825 struct class_device_attribute **attrs,
826 struct class_device_attribute *attr)
832 for (i = 0; attrs[i]; i++)
833 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
838 static int class_attr_add(struct class_device *classdev,
839 struct class_device_attribute *attr)
841 struct class_device_attribute *base_attr;
844 * Spare the caller from having to copy things it's not interested in.
846 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
848 /* extend permissions */
849 attr->attr.mode |= base_attr->attr.mode;
851 /* override null show/store with default */
853 attr->show = base_attr->show;
855 attr->store = base_attr->store;
858 return class_device_create_file(classdev, attr);
862 * scsi_sysfs_add_host - add scsi host to subsystem
863 * @shost: scsi host struct to add to subsystem
864 * @dev: parent struct device pointer
866 int scsi_sysfs_add_host(struct Scsi_Host *shost)
870 if (shost->hostt->shost_attrs) {
871 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
872 error = class_attr_add(&shost->shost_classdev,
873 shost->hostt->shost_attrs[i]);
879 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
880 if (!class_attr_overridden(shost->hostt->shost_attrs,
881 scsi_sysfs_shost_attrs[i])) {
882 error = class_device_create_file(&shost->shost_classdev,
883 scsi_sysfs_shost_attrs[i]);
889 transport_register_device(&shost->shost_gendev);
893 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
896 struct Scsi_Host *shost = sdev->host;
897 struct scsi_target *starget = sdev->sdev_target;
899 device_initialize(&sdev->sdev_gendev);
900 sdev->sdev_gendev.bus = &scsi_bus_type;
901 sdev->sdev_gendev.release = scsi_device_dev_release;
902 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
903 sdev->host->host_no, sdev->channel, sdev->id,
906 class_device_initialize(&sdev->sdev_classdev);
907 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
908 sdev->sdev_classdev.class = &sdev_class;
909 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
910 "%d:%d:%d:%d", sdev->host->host_no,
911 sdev->channel, sdev->id, sdev->lun);
912 sdev->scsi_level = SCSI_2;
913 transport_setup_device(&sdev->sdev_gendev);
914 spin_lock_irqsave(shost->host_lock, flags);
915 list_add_tail(&sdev->same_target_siblings, &starget->devices);
916 list_add_tail(&sdev->siblings, &shost->__devices);
917 spin_unlock_irqrestore(shost->host_lock, flags);
920 int scsi_is_sdev_device(const struct device *dev)
922 return dev->release == scsi_device_dev_release;
924 EXPORT_SYMBOL(scsi_is_sdev_device);
926 /* A blank transport template that is used in drivers that don't
927 * yet implement Transport Attributes */
928 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };