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;
51 static int check_set(unsigned int *val, char *src)
55 if (strncmp(src, "-", 20) == 0) {
56 *val = SCAN_WILD_CARD;
59 * Doesn't check for int overflow
61 *val = simple_strtoul(src, &last, 0);
68 static int scsi_scan(struct Scsi_Host *shost, const char *str)
70 char s1[15], s2[15], s3[15], junk;
71 unsigned int channel, id, lun;
74 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
77 if (check_set(&channel, s1))
79 if (check_set(&id, s2))
81 if (check_set(&lun, s3))
83 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
88 * shost_show_function: macro to create an attr function that can be used to
89 * show a non-bit field.
91 #define shost_show_function(name, field, format_string) \
93 show_##name (struct class_device *class_dev, char *buf) \
95 struct Scsi_Host *shost = class_to_shost(class_dev); \
96 return snprintf (buf, 20, format_string, shost->field); \
100 * shost_rd_attr: macro to create a function and attribute variable for a
103 #define shost_rd_attr2(name, field, format_string) \
104 shost_show_function(name, field, format_string) \
105 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
107 #define shost_rd_attr(field, format_string) \
108 shost_rd_attr2(field, field, format_string)
111 * Create the actual show/store functions and data structures.
114 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
117 struct Scsi_Host *shost = class_to_shost(class_dev);
120 res = scsi_scan(shost, buf);
125 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
127 shost_rd_attr(unique_id, "%u\n");
128 shost_rd_attr(host_busy, "%hu\n");
129 shost_rd_attr(cmd_per_lun, "%hd\n");
130 shost_rd_attr(sg_tablesize, "%hu\n");
131 shost_rd_attr(unchecked_isa_dma, "%d\n");
132 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
134 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
135 &class_device_attr_unique_id,
136 &class_device_attr_host_busy,
137 &class_device_attr_cmd_per_lun,
138 &class_device_attr_sg_tablesize,
139 &class_device_attr_unchecked_isa_dma,
140 &class_device_attr_proc_name,
141 &class_device_attr_scan,
145 static void scsi_device_cls_release(struct class_device *class_dev)
147 struct scsi_device *sdev;
149 sdev = class_to_sdev(class_dev);
150 put_device(&sdev->sdev_gendev);
153 void scsi_device_dev_release(struct device *dev)
155 struct scsi_device *sdev;
156 struct device *parent;
157 struct scsi_target *starget;
160 parent = dev->parent;
161 sdev = to_scsi_device(dev);
162 starget = to_scsi_target(parent);
164 spin_lock_irqsave(sdev->host->host_lock, flags);
166 list_del(&sdev->siblings);
167 list_del(&sdev->same_target_siblings);
168 list_del(&sdev->starved_entry);
169 spin_unlock_irqrestore(sdev->host->host_lock, flags);
171 if (sdev->request_queue) {
172 sdev->request_queue->queuedata = NULL;
173 scsi_free_queue(sdev->request_queue);
176 scsi_target_reap(scsi_target(sdev));
178 kfree(sdev->inquiry);
185 struct class sdev_class = {
186 .name = "scsi_device",
187 .release = scsi_device_cls_release,
190 /* all probing is done in the individual ->probe routines */
191 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
193 struct scsi_device *sdp = to_scsi_device(dev);
194 if (sdp->no_uld_attach)
196 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
199 struct bus_type scsi_bus_type = {
201 .match = scsi_bus_match,
204 int scsi_sysfs_register(void)
208 error = bus_register(&scsi_bus_type);
210 error = class_register(&sdev_class);
212 bus_unregister(&scsi_bus_type);
218 void scsi_sysfs_unregister(void)
220 class_unregister(&sdev_class);
221 bus_unregister(&scsi_bus_type);
225 * sdev_show_function: macro to create an attr function that can be used to
226 * show a non-bit field.
228 #define sdev_show_function(field, format_string) \
230 sdev_show_##field (struct device *dev, char *buf) \
232 struct scsi_device *sdev; \
233 sdev = to_scsi_device(dev); \
234 return snprintf (buf, 20, format_string, sdev->field); \
238 * sdev_rd_attr: macro to create a function and attribute variable for a
241 #define sdev_rd_attr(field, format_string) \
242 sdev_show_function(field, format_string) \
243 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
247 * sdev_rd_attr: create a function and attribute variable for a
250 #define sdev_rw_attr(field, format_string) \
251 sdev_show_function(field, format_string) \
254 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
256 struct scsi_device *sdev; \
257 sdev = to_scsi_device(dev); \
258 snscanf (buf, 20, format_string, &sdev->field); \
261 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
263 /* Currently we don't export bit fields, but we might in future,
264 * so leave this code in */
267 * sdev_rd_attr: create a function and attribute variable for a
268 * read/write bit field.
270 #define sdev_rw_attr_bit(field) \
271 sdev_show_function(field, "%d\n") \
274 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
277 struct scsi_device *sdev; \
278 ret = scsi_sdev_check_buf_bit(buf); \
280 sdev = to_scsi_device(dev); \
286 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
289 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
290 * else return -EINVAL.
292 static int scsi_sdev_check_buf_bit(const char *buf)
294 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
297 else if (buf[0] == '0')
306 * Create the actual show/store functions and data structures.
308 sdev_rd_attr (device_blocked, "%d\n");
309 sdev_rd_attr (queue_depth, "%d\n");
310 sdev_rd_attr (type, "%d\n");
311 sdev_rd_attr (scsi_level, "%d\n");
312 sdev_rd_attr (vendor, "%.8s\n");
313 sdev_rd_attr (model, "%.16s\n");
314 sdev_rd_attr (rev, "%.4s\n");
317 sdev_show_timeout (struct device *dev, char *buf)
319 struct scsi_device *sdev;
320 sdev = to_scsi_device(dev);
321 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
325 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
327 struct scsi_device *sdev;
329 sdev = to_scsi_device(dev);
330 sscanf (buf, "%d\n", &timeout);
331 sdev->timeout = timeout * HZ;
334 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
337 store_rescan_field (struct device *dev, const char *buf, size_t count)
339 scsi_rescan_device(dev);
342 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
344 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
347 scsi_remove_device(to_scsi_device(dev));
350 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
353 store_state_field(struct device *dev, const char *buf, size_t count)
356 struct scsi_device *sdev = to_scsi_device(dev);
357 enum scsi_device_state state = 0;
359 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
360 const int len = strlen(sdev_states[i].name);
361 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
363 state = sdev_states[i].value;
370 if (scsi_device_set_state(sdev, state))
376 show_state_field(struct device *dev, char *buf)
378 struct scsi_device *sdev = to_scsi_device(dev);
379 const char *name = scsi_device_state_name(sdev->sdev_state);
384 return snprintf(buf, 20, "%s\n", name);
387 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
390 show_queue_type_field(struct device *dev, char *buf)
392 struct scsi_device *sdev = to_scsi_device(dev);
393 const char *name = "none";
395 if (sdev->ordered_tags)
397 else if (sdev->simple_tags)
400 return snprintf(buf, 20, "%s\n", name);
403 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
406 show_iostat_counterbits(struct device *dev, char *buf)
408 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
411 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
413 #define show_sdev_iostat(field) \
415 show_iostat_##field(struct device *dev, char *buf) \
417 struct scsi_device *sdev = to_scsi_device(dev); \
418 unsigned long long count = atomic_read(&sdev->field); \
419 return snprintf(buf, 20, "0x%llx\n", count); \
421 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
423 show_sdev_iostat(iorequest_cnt);
424 show_sdev_iostat(iodone_cnt);
425 show_sdev_iostat(ioerr_cnt);
428 /* Default template for device attributes. May NOT be modified */
429 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
430 &dev_attr_device_blocked,
431 &dev_attr_queue_depth,
432 &dev_attr_queue_type,
434 &dev_attr_scsi_level,
442 &dev_attr_iocounterbits,
443 &dev_attr_iorequest_cnt,
444 &dev_attr_iodone_cnt,
449 static ssize_t sdev_store_queue_depth_rw(struct device *dev, const char *buf,
453 struct scsi_device *sdev = to_scsi_device(dev);
454 struct scsi_host_template *sht = sdev->host->hostt;
456 if (!sht->change_queue_depth)
459 depth = simple_strtoul(buf, NULL, 0);
464 retval = sht->change_queue_depth(sdev, depth);
471 static struct device_attribute sdev_attr_queue_depth_rw =
472 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
473 sdev_store_queue_depth_rw);
475 static ssize_t sdev_store_queue_type_rw(struct device *dev, const char *buf,
478 struct scsi_device *sdev = to_scsi_device(dev);
479 struct scsi_host_template *sht = sdev->host->hostt;
480 int tag_type = 0, retval;
481 int prev_tag_type = scsi_get_tag_type(sdev);
483 if (!sdev->tagged_supported || !sht->change_queue_type)
486 if (strncmp(buf, "ordered", 7) == 0)
487 tag_type = MSG_ORDERED_TAG;
488 else if (strncmp(buf, "simple", 6) == 0)
489 tag_type = MSG_SIMPLE_TAG;
490 else if (strncmp(buf, "none", 4) != 0)
493 if (tag_type == prev_tag_type)
496 retval = sht->change_queue_type(sdev, tag_type);
503 static struct device_attribute sdev_attr_queue_type_rw =
504 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
505 sdev_store_queue_type_rw);
507 static struct device_attribute *attr_changed_internally(
508 struct Scsi_Host *shost,
509 struct device_attribute * attr)
511 if (!strcmp("queue_depth", attr->attr.name)
512 && shost->hostt->change_queue_depth)
513 return &sdev_attr_queue_depth_rw;
514 else if (!strcmp("queue_type", attr->attr.name)
515 && shost->hostt->change_queue_type)
516 return &sdev_attr_queue_type_rw;
521 static struct device_attribute *attr_overridden(
522 struct device_attribute **attrs,
523 struct device_attribute *attr)
529 for (i = 0; attrs[i]; i++)
530 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
535 static int attr_add(struct device *dev, struct device_attribute *attr)
537 struct device_attribute *base_attr;
540 * Spare the caller from having to copy things it's not interested in.
542 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
544 /* extend permissions */
545 attr->attr.mode |= base_attr->attr.mode;
547 /* override null show/store with default */
549 attr->show = base_attr->show;
551 attr->store = base_attr->store;
554 return device_create_file(dev, attr);
558 * scsi_sysfs_add_sdev - add scsi device to sysfs
559 * @sdev: scsi_device to add
562 * 0 on Success / non-zero on Failure
564 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
568 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
571 error = device_add(&sdev->sdev_gendev);
573 put_device(sdev->sdev_gendev.parent);
574 printk(KERN_INFO "error 1\n");
577 error = class_device_add(&sdev->sdev_classdev);
579 printk(KERN_INFO "error 2\n");
583 /* take a reference for the sdev_classdev; this is
584 * released by the sdev_class .release */
585 get_device(&sdev->sdev_gendev);
586 if (sdev->host->hostt->sdev_attrs) {
587 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
588 error = attr_add(&sdev->sdev_gendev,
589 sdev->host->hostt->sdev_attrs[i]);
591 scsi_remove_device(sdev);
597 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
598 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
599 scsi_sysfs_sdev_attrs[i])) {
600 struct device_attribute * attr =
601 attr_changed_internally(sdev->host,
602 scsi_sysfs_sdev_attrs[i]);
603 error = device_create_file(&sdev->sdev_gendev, attr);
605 scsi_remove_device(sdev);
611 transport_add_device(&sdev->sdev_gendev);
616 scsi_device_set_state(sdev, SDEV_CANCEL);
618 device_del(&sdev->sdev_gendev);
619 transport_destroy_device(&sdev->sdev_gendev);
620 put_device(&sdev->sdev_gendev);
626 * scsi_remove_device - unregister a device from the scsi bus
627 * @sdev: scsi_device to unregister
629 void scsi_remove_device(struct scsi_device *sdev)
631 struct Scsi_Host *shost = sdev->host;
633 down(&shost->scan_mutex);
634 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
637 class_device_unregister(&sdev->sdev_classdev);
638 device_del(&sdev->sdev_gendev);
639 scsi_device_set_state(sdev, SDEV_DEL);
640 if (sdev->host->hostt->slave_destroy)
641 sdev->host->hostt->slave_destroy(sdev);
642 transport_unregister_device(&sdev->sdev_gendev);
643 put_device(&sdev->sdev_gendev);
645 up(&shost->scan_mutex);
647 EXPORT_SYMBOL(scsi_remove_device);
649 void __scsi_remove_target(struct scsi_target *starget)
651 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
653 struct scsi_device *sdev, *tmp;
655 spin_lock_irqsave(shost->host_lock, flags);
657 list_for_each_entry_safe(sdev, tmp, &shost->__devices, siblings) {
658 if (sdev->channel != starget->channel ||
659 sdev->id != starget->id)
661 spin_unlock_irqrestore(shost->host_lock, flags);
662 scsi_remove_device(sdev);
663 spin_lock_irqsave(shost->host_lock, flags);
665 spin_unlock_irqrestore(shost->host_lock, flags);
666 scsi_target_reap(starget);
670 * scsi_remove_target - try to remove a target and all its devices
671 * @dev: generic starget or parent of generic stargets to be removed
673 * Note: This is slightly racy. It is possible that if the user
674 * requests the addition of another device then the target won't be
677 void scsi_remove_target(struct device *dev)
679 struct device *rdev, *idev, *next;
681 if (scsi_is_target_device(dev)) {
682 __scsi_remove_target(to_scsi_target(dev));
686 rdev = get_device(dev);
687 list_for_each_entry_safe(idev, next, &dev->children, node) {
688 if (scsi_is_target_device(idev))
689 __scsi_remove_target(to_scsi_target(idev));
693 EXPORT_SYMBOL(scsi_remove_target);
695 int scsi_register_driver(struct device_driver *drv)
697 drv->bus = &scsi_bus_type;
699 return driver_register(drv);
701 EXPORT_SYMBOL(scsi_register_driver);
703 int scsi_register_interface(struct class_interface *intf)
705 intf->class = &sdev_class;
707 return class_interface_register(intf);
709 EXPORT_SYMBOL(scsi_register_interface);
712 static struct class_device_attribute *class_attr_overridden(
713 struct class_device_attribute **attrs,
714 struct class_device_attribute *attr)
720 for (i = 0; attrs[i]; i++)
721 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
726 static int class_attr_add(struct class_device *classdev,
727 struct class_device_attribute *attr)
729 struct class_device_attribute *base_attr;
732 * Spare the caller from having to copy things it's not interested in.
734 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
736 /* extend permissions */
737 attr->attr.mode |= base_attr->attr.mode;
739 /* override null show/store with default */
741 attr->show = base_attr->show;
743 attr->store = base_attr->store;
746 return class_device_create_file(classdev, attr);
750 * scsi_sysfs_add_host - add scsi host to subsystem
751 * @shost: scsi host struct to add to subsystem
752 * @dev: parent struct device pointer
754 int scsi_sysfs_add_host(struct Scsi_Host *shost)
758 if (shost->hostt->shost_attrs) {
759 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
760 error = class_attr_add(&shost->shost_classdev,
761 shost->hostt->shost_attrs[i]);
767 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
768 if (!class_attr_overridden(shost->hostt->shost_attrs,
769 scsi_sysfs_shost_attrs[i])) {
770 error = class_device_create_file(&shost->shost_classdev,
771 scsi_sysfs_shost_attrs[i]);
777 transport_register_device(&shost->shost_gendev);
781 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
784 struct Scsi_Host *shost = sdev->host;
785 struct scsi_target *starget = sdev->sdev_target;
787 device_initialize(&sdev->sdev_gendev);
788 sdev->sdev_gendev.bus = &scsi_bus_type;
789 sdev->sdev_gendev.release = scsi_device_dev_release;
790 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
791 sdev->host->host_no, sdev->channel, sdev->id,
794 class_device_initialize(&sdev->sdev_classdev);
795 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
796 sdev->sdev_classdev.class = &sdev_class;
797 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
798 "%d:%d:%d:%d", sdev->host->host_no,
799 sdev->channel, sdev->id, sdev->lun);
800 sdev->scsi_level = SCSI_2;
801 transport_setup_device(&sdev->sdev_gendev);
802 spin_lock_irqsave(shost->host_lock, flags);
803 list_add_tail(&sdev->same_target_siblings, &starget->devices);
804 list_add_tail(&sdev->siblings, &shost->__devices);
805 spin_unlock_irqrestore(shost->host_lock, flags);
808 int scsi_is_sdev_device(const struct device *dev)
810 return dev->release == scsi_device_dev_release;
812 EXPORT_SYMBOL(scsi_is_sdev_device);
814 /* A blank transport template that is used in drivers that don't
815 * yet implement Transport Attributes */
816 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };