4 * SCSI sysfs interface routines.
6 * Created to pull SCSI mid layer sysfs routines into one file.
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/device.h>
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>
20 #include "scsi_priv.h"
21 #include "scsi_logging.h"
24 enum scsi_device_state value;
27 { SDEV_CREATED, "created" },
28 { SDEV_RUNNING, "running" },
29 { SDEV_CANCEL, "cancel" },
30 { SDEV_DEL, "deleted" },
31 { SDEV_QUIESCE, "quiesce" },
32 { SDEV_OFFLINE, "offline" },
33 { SDEV_BLOCK, "blocked" },
36 const char *scsi_device_state_name(enum scsi_device_state state)
41 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
42 if (sdev_states[i].value == state) {
43 name = sdev_states[i].name;
51 enum scsi_host_state value;
54 { SHOST_CREATED, "created" },
55 { SHOST_RUNNING, "running" },
56 { SHOST_CANCEL, "cancel" },
57 { SHOST_DEL, "deleted" },
58 { SHOST_RECOVERY, "recovery" },
59 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
60 { SHOST_DEL_RECOVERY, "deleted/recovery", },
62 const char *scsi_host_state_name(enum scsi_host_state state)
67 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
68 if (shost_states[i].value == state) {
69 name = shost_states[i].name;
76 static int check_set(unsigned int *val, char *src)
80 if (strncmp(src, "-", 20) == 0) {
81 *val = SCAN_WILD_CARD;
84 * Doesn't check for int overflow
86 *val = simple_strtoul(src, &last, 0);
93 static int scsi_scan(struct Scsi_Host *shost, const char *str)
95 char s1[15], s2[15], s3[15], junk;
96 unsigned int channel, id, lun;
99 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
102 if (check_set(&channel, s1))
104 if (check_set(&id, s2))
106 if (check_set(&lun, s3))
108 if (shost->transportt->user_scan)
109 res = shost->transportt->user_scan(shost, channel, id, lun);
111 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
116 * shost_show_function: macro to create an attr function that can be used to
117 * show a non-bit field.
119 #define shost_show_function(name, field, format_string) \
121 show_##name (struct class_device *class_dev, char *buf) \
123 struct Scsi_Host *shost = class_to_shost(class_dev); \
124 return snprintf (buf, 20, format_string, shost->field); \
128 * shost_rd_attr: macro to create a function and attribute variable for a
131 #define shost_rd_attr2(name, field, format_string) \
132 shost_show_function(name, field, format_string) \
133 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
135 #define shost_rd_attr(field, format_string) \
136 shost_rd_attr2(field, field, format_string)
139 * Create the actual show/store functions and data structures.
142 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
145 struct Scsi_Host *shost = class_to_shost(class_dev);
148 res = scsi_scan(shost, buf);
153 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
156 store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
159 struct Scsi_Host *shost = class_to_shost(class_dev);
160 enum scsi_host_state state = 0;
162 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
163 const int len = strlen(shost_states[i].name);
164 if (strncmp(shost_states[i].name, buf, len) == 0 &&
166 state = shost_states[i].value;
173 if (scsi_host_set_state(shost, state))
179 show_shost_state(struct class_device *class_dev, char *buf)
181 struct Scsi_Host *shost = class_to_shost(class_dev);
182 const char *name = scsi_host_state_name(shost->shost_state);
187 return snprintf(buf, 20, "%s\n", name);
190 static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
192 shost_rd_attr(unique_id, "%u\n");
193 shost_rd_attr(host_busy, "%hu\n");
194 shost_rd_attr(cmd_per_lun, "%hd\n");
195 shost_rd_attr(sg_tablesize, "%hu\n");
196 shost_rd_attr(unchecked_isa_dma, "%d\n");
197 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
199 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
200 &class_device_attr_unique_id,
201 &class_device_attr_host_busy,
202 &class_device_attr_cmd_per_lun,
203 &class_device_attr_sg_tablesize,
204 &class_device_attr_unchecked_isa_dma,
205 &class_device_attr_proc_name,
206 &class_device_attr_scan,
207 &class_device_attr_state,
211 static void scsi_device_cls_release(struct class_device *class_dev)
213 struct scsi_device *sdev;
215 sdev = class_to_sdev(class_dev);
216 put_device(&sdev->sdev_gendev);
219 static void scsi_device_dev_release_usercontext(void *data)
221 struct device *dev = data;
222 struct scsi_device *sdev;
223 struct device *parent;
224 struct scsi_target *starget;
227 parent = dev->parent;
228 sdev = to_scsi_device(dev);
229 starget = to_scsi_target(parent);
231 spin_lock_irqsave(sdev->host->host_lock, flags);
233 list_del(&sdev->siblings);
234 list_del(&sdev->same_target_siblings);
235 list_del(&sdev->starved_entry);
236 spin_unlock_irqrestore(sdev->host->host_lock, flags);
238 if (sdev->request_queue) {
239 sdev->request_queue->queuedata = NULL;
240 /* user context needed to free queue */
241 scsi_free_queue(sdev->request_queue);
242 /* temporary expedient, try to catch use of queue lock
243 * after free of sdev */
244 sdev->request_queue = NULL;
247 scsi_target_reap(scsi_target(sdev));
249 kfree(sdev->inquiry);
256 static void scsi_device_dev_release(struct device *dev)
258 struct scsi_device *sdp = to_scsi_device(dev);
259 execute_in_process_context(scsi_device_dev_release_usercontext, dev,
263 static struct class sdev_class = {
264 .name = "scsi_device",
265 .release = scsi_device_cls_release,
268 /* all probing is done in the individual ->probe routines */
269 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
271 struct scsi_device *sdp = to_scsi_device(dev);
272 if (sdp->no_uld_attach)
274 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
277 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
279 struct scsi_device *sdev = to_scsi_device(dev);
280 struct scsi_host_template *sht = sdev->host->hostt;
283 err = scsi_device_quiesce(sdev);
288 err = sht->suspend(sdev, state);
293 static int scsi_bus_resume(struct device * dev)
295 struct scsi_device *sdev = to_scsi_device(dev);
296 struct scsi_host_template *sht = sdev->host->hostt;
300 err = sht->resume(sdev);
302 scsi_device_resume(sdev);
306 struct bus_type scsi_bus_type = {
308 .match = scsi_bus_match,
309 .suspend = scsi_bus_suspend,
310 .resume = scsi_bus_resume,
313 int scsi_sysfs_register(void)
317 error = bus_register(&scsi_bus_type);
319 error = class_register(&sdev_class);
321 bus_unregister(&scsi_bus_type);
327 void scsi_sysfs_unregister(void)
329 class_unregister(&sdev_class);
330 bus_unregister(&scsi_bus_type);
334 * sdev_show_function: macro to create an attr function that can be used to
335 * show a non-bit field.
337 #define sdev_show_function(field, format_string) \
339 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
341 struct scsi_device *sdev; \
342 sdev = to_scsi_device(dev); \
343 return snprintf (buf, 20, format_string, sdev->field); \
347 * sdev_rd_attr: macro to create a function and attribute variable for a
350 #define sdev_rd_attr(field, format_string) \
351 sdev_show_function(field, format_string) \
352 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
356 * sdev_rd_attr: create a function and attribute variable for a
359 #define sdev_rw_attr(field, format_string) \
360 sdev_show_function(field, format_string) \
363 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
365 struct scsi_device *sdev; \
366 sdev = to_scsi_device(dev); \
367 snscanf (buf, 20, format_string, &sdev->field); \
370 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
372 /* Currently we don't export bit fields, but we might in future,
373 * so leave this code in */
376 * sdev_rd_attr: create a function and attribute variable for a
377 * read/write bit field.
379 #define sdev_rw_attr_bit(field) \
380 sdev_show_function(field, "%d\n") \
383 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
386 struct scsi_device *sdev; \
387 ret = scsi_sdev_check_buf_bit(buf); \
389 sdev = to_scsi_device(dev); \
395 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
398 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
399 * else return -EINVAL.
401 static int scsi_sdev_check_buf_bit(const char *buf)
403 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
406 else if (buf[0] == '0')
415 * Create the actual show/store functions and data structures.
417 sdev_rd_attr (device_blocked, "%d\n");
418 sdev_rd_attr (queue_depth, "%d\n");
419 sdev_rd_attr (type, "%d\n");
420 sdev_rd_attr (scsi_level, "%d\n");
421 sdev_rd_attr (vendor, "%.8s\n");
422 sdev_rd_attr (model, "%.16s\n");
423 sdev_rd_attr (rev, "%.4s\n");
426 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
428 struct scsi_device *sdev;
429 sdev = to_scsi_device(dev);
430 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
434 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
436 struct scsi_device *sdev;
438 sdev = to_scsi_device(dev);
439 sscanf (buf, "%d\n", &timeout);
440 sdev->timeout = timeout * HZ;
443 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
446 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
448 scsi_rescan_device(dev);
451 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
453 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
456 scsi_remove_device(to_scsi_device(dev));
459 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
462 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
465 struct scsi_device *sdev = to_scsi_device(dev);
466 enum scsi_device_state state = 0;
468 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
469 const int len = strlen(sdev_states[i].name);
470 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
472 state = sdev_states[i].value;
479 if (scsi_device_set_state(sdev, state))
485 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
487 struct scsi_device *sdev = to_scsi_device(dev);
488 const char *name = scsi_device_state_name(sdev->sdev_state);
493 return snprintf(buf, 20, "%s\n", name);
496 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
499 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
501 struct scsi_device *sdev = to_scsi_device(dev);
502 const char *name = "none";
504 if (sdev->ordered_tags)
506 else if (sdev->simple_tags)
509 return snprintf(buf, 20, "%s\n", name);
512 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
515 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
517 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
520 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
522 #define show_sdev_iostat(field) \
524 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
526 struct scsi_device *sdev = to_scsi_device(dev); \
527 unsigned long long count = atomic_read(&sdev->field); \
528 return snprintf(buf, 20, "0x%llx\n", count); \
530 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
532 show_sdev_iostat(iorequest_cnt);
533 show_sdev_iostat(iodone_cnt);
534 show_sdev_iostat(ioerr_cnt);
537 /* Default template for device attributes. May NOT be modified */
538 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
539 &dev_attr_device_blocked,
540 &dev_attr_queue_depth,
541 &dev_attr_queue_type,
543 &dev_attr_scsi_level,
551 &dev_attr_iocounterbits,
552 &dev_attr_iorequest_cnt,
553 &dev_attr_iodone_cnt,
558 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
562 struct scsi_device *sdev = to_scsi_device(dev);
563 struct scsi_host_template *sht = sdev->host->hostt;
565 if (!sht->change_queue_depth)
568 depth = simple_strtoul(buf, NULL, 0);
573 retval = sht->change_queue_depth(sdev, depth);
580 static struct device_attribute sdev_attr_queue_depth_rw =
581 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
582 sdev_store_queue_depth_rw);
584 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
587 struct scsi_device *sdev = to_scsi_device(dev);
588 struct scsi_host_template *sht = sdev->host->hostt;
589 int tag_type = 0, retval;
590 int prev_tag_type = scsi_get_tag_type(sdev);
592 if (!sdev->tagged_supported || !sht->change_queue_type)
595 if (strncmp(buf, "ordered", 7) == 0)
596 tag_type = MSG_ORDERED_TAG;
597 else if (strncmp(buf, "simple", 6) == 0)
598 tag_type = MSG_SIMPLE_TAG;
599 else if (strncmp(buf, "none", 4) != 0)
602 if (tag_type == prev_tag_type)
605 retval = sht->change_queue_type(sdev, tag_type);
612 static struct device_attribute sdev_attr_queue_type_rw =
613 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
614 sdev_store_queue_type_rw);
616 static struct device_attribute *attr_changed_internally(
617 struct Scsi_Host *shost,
618 struct device_attribute * attr)
620 if (!strcmp("queue_depth", attr->attr.name)
621 && shost->hostt->change_queue_depth)
622 return &sdev_attr_queue_depth_rw;
623 else if (!strcmp("queue_type", attr->attr.name)
624 && shost->hostt->change_queue_type)
625 return &sdev_attr_queue_type_rw;
630 static struct device_attribute *attr_overridden(
631 struct device_attribute **attrs,
632 struct device_attribute *attr)
638 for (i = 0; attrs[i]; i++)
639 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
644 static int attr_add(struct device *dev, struct device_attribute *attr)
646 struct device_attribute *base_attr;
649 * Spare the caller from having to copy things it's not interested in.
651 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
653 /* extend permissions */
654 attr->attr.mode |= base_attr->attr.mode;
656 /* override null show/store with default */
658 attr->show = base_attr->show;
660 attr->store = base_attr->store;
663 return device_create_file(dev, attr);
667 * scsi_sysfs_add_sdev - add scsi device to sysfs
668 * @sdev: scsi_device to add
671 * 0 on Success / non-zero on Failure
673 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
677 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
680 error = device_add(&sdev->sdev_gendev);
682 put_device(sdev->sdev_gendev.parent);
683 printk(KERN_INFO "error 1\n");
686 error = class_device_add(&sdev->sdev_classdev);
688 printk(KERN_INFO "error 2\n");
692 /* take a reference for the sdev_classdev; this is
693 * released by the sdev_class .release */
694 get_device(&sdev->sdev_gendev);
695 if (sdev->host->hostt->sdev_attrs) {
696 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
697 error = attr_add(&sdev->sdev_gendev,
698 sdev->host->hostt->sdev_attrs[i]);
700 __scsi_remove_device(sdev);
706 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
707 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
708 scsi_sysfs_sdev_attrs[i])) {
709 struct device_attribute * attr =
710 attr_changed_internally(sdev->host,
711 scsi_sysfs_sdev_attrs[i]);
712 error = device_create_file(&sdev->sdev_gendev, attr);
714 __scsi_remove_device(sdev);
720 transport_add_device(&sdev->sdev_gendev);
725 scsi_device_set_state(sdev, SDEV_CANCEL);
727 device_del(&sdev->sdev_gendev);
728 transport_destroy_device(&sdev->sdev_gendev);
729 put_device(&sdev->sdev_gendev);
734 void __scsi_remove_device(struct scsi_device *sdev)
736 struct device *dev = &sdev->sdev_gendev;
738 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
741 class_device_unregister(&sdev->sdev_classdev);
742 transport_remove_device(dev);
744 scsi_device_set_state(sdev, SDEV_DEL);
745 if (sdev->host->hostt->slave_destroy)
746 sdev->host->hostt->slave_destroy(sdev);
747 transport_destroy_device(dev);
752 * scsi_remove_device - unregister a device from the scsi bus
753 * @sdev: scsi_device to unregister
755 void scsi_remove_device(struct scsi_device *sdev)
757 struct Scsi_Host *shost = sdev->host;
759 mutex_lock(&shost->scan_mutex);
760 __scsi_remove_device(sdev);
761 mutex_unlock(&shost->scan_mutex);
763 EXPORT_SYMBOL(scsi_remove_device);
765 void __scsi_remove_target(struct scsi_target *starget)
767 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
769 struct scsi_device *sdev;
771 spin_lock_irqsave(shost->host_lock, flags);
774 list_for_each_entry(sdev, &shost->__devices, siblings) {
775 if (sdev->channel != starget->channel ||
776 sdev->id != starget->id ||
777 sdev->sdev_state == SDEV_DEL)
779 spin_unlock_irqrestore(shost->host_lock, flags);
780 scsi_remove_device(sdev);
781 spin_lock_irqsave(shost->host_lock, flags);
784 spin_unlock_irqrestore(shost->host_lock, flags);
785 scsi_target_reap(starget);
788 static int __remove_child (struct device * dev, void * data)
790 if (scsi_is_target_device(dev))
791 __scsi_remove_target(to_scsi_target(dev));
796 * scsi_remove_target - try to remove a target and all its devices
797 * @dev: generic starget or parent of generic stargets to be removed
799 * Note: This is slightly racy. It is possible that if the user
800 * requests the addition of another device then the target won't be
803 void scsi_remove_target(struct device *dev)
807 if (scsi_is_target_device(dev)) {
808 __scsi_remove_target(to_scsi_target(dev));
812 rdev = get_device(dev);
813 device_for_each_child(dev, NULL, __remove_child);
816 EXPORT_SYMBOL(scsi_remove_target);
818 int scsi_register_driver(struct device_driver *drv)
820 drv->bus = &scsi_bus_type;
822 return driver_register(drv);
824 EXPORT_SYMBOL(scsi_register_driver);
826 int scsi_register_interface(struct class_interface *intf)
828 intf->class = &sdev_class;
830 return class_interface_register(intf);
832 EXPORT_SYMBOL(scsi_register_interface);
835 static struct class_device_attribute *class_attr_overridden(
836 struct class_device_attribute **attrs,
837 struct class_device_attribute *attr)
843 for (i = 0; attrs[i]; i++)
844 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
849 static int class_attr_add(struct class_device *classdev,
850 struct class_device_attribute *attr)
852 struct class_device_attribute *base_attr;
855 * Spare the caller from having to copy things it's not interested in.
857 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
859 /* extend permissions */
860 attr->attr.mode |= base_attr->attr.mode;
862 /* override null show/store with default */
864 attr->show = base_attr->show;
866 attr->store = base_attr->store;
869 return class_device_create_file(classdev, attr);
873 * scsi_sysfs_add_host - add scsi host to subsystem
874 * @shost: scsi host struct to add to subsystem
875 * @dev: parent struct device pointer
877 int scsi_sysfs_add_host(struct Scsi_Host *shost)
881 if (shost->hostt->shost_attrs) {
882 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
883 error = class_attr_add(&shost->shost_classdev,
884 shost->hostt->shost_attrs[i]);
890 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
891 if (!class_attr_overridden(shost->hostt->shost_attrs,
892 scsi_sysfs_shost_attrs[i])) {
893 error = class_device_create_file(&shost->shost_classdev,
894 scsi_sysfs_shost_attrs[i]);
900 transport_register_device(&shost->shost_gendev);
904 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
907 struct Scsi_Host *shost = sdev->host;
908 struct scsi_target *starget = sdev->sdev_target;
910 device_initialize(&sdev->sdev_gendev);
911 sdev->sdev_gendev.bus = &scsi_bus_type;
912 sdev->sdev_gendev.release = scsi_device_dev_release;
913 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
914 sdev->host->host_no, sdev->channel, sdev->id,
917 class_device_initialize(&sdev->sdev_classdev);
918 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
919 sdev->sdev_classdev.class = &sdev_class;
920 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
921 "%d:%d:%d:%d", sdev->host->host_no,
922 sdev->channel, sdev->id, sdev->lun);
923 sdev->scsi_level = SCSI_2;
924 transport_setup_device(&sdev->sdev_gendev);
925 spin_lock_irqsave(shost->host_lock, flags);
926 list_add_tail(&sdev->same_target_siblings, &starget->devices);
927 list_add_tail(&sdev->siblings, &shost->__devices);
928 spin_unlock_irqrestore(shost->host_lock, flags);
931 int scsi_is_sdev_device(const struct device *dev)
933 return dev->release == scsi_device_dev_release;
935 EXPORT_SYMBOL(scsi_is_sdev_device);
937 /* A blank transport template that is used in drivers that don't
938 * yet implement Transport Attributes */
939 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };