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);
174 /* temporary expedient, try to catch use of queue lock
175 * after free of sdev */
176 sdev->request_queue = NULL;
179 scsi_target_reap(scsi_target(sdev));
181 kfree(sdev->inquiry);
188 struct class sdev_class = {
189 .name = "scsi_device",
190 .release = scsi_device_cls_release,
193 /* all probing is done in the individual ->probe routines */
194 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
196 struct scsi_device *sdp = to_scsi_device(dev);
197 if (sdp->no_uld_attach)
199 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
202 struct bus_type scsi_bus_type = {
204 .match = scsi_bus_match,
207 int scsi_sysfs_register(void)
211 error = bus_register(&scsi_bus_type);
213 error = class_register(&sdev_class);
215 bus_unregister(&scsi_bus_type);
221 void scsi_sysfs_unregister(void)
223 class_unregister(&sdev_class);
224 bus_unregister(&scsi_bus_type);
228 * sdev_show_function: macro to create an attr function that can be used to
229 * show a non-bit field.
231 #define sdev_show_function(field, format_string) \
233 sdev_show_##field (struct device *dev, char *buf) \
235 struct scsi_device *sdev; \
236 sdev = to_scsi_device(dev); \
237 return snprintf (buf, 20, format_string, sdev->field); \
241 * sdev_rd_attr: macro to create a function and attribute variable for a
244 #define sdev_rd_attr(field, format_string) \
245 sdev_show_function(field, format_string) \
246 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
250 * sdev_rd_attr: create a function and attribute variable for a
253 #define sdev_rw_attr(field, format_string) \
254 sdev_show_function(field, format_string) \
257 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
259 struct scsi_device *sdev; \
260 sdev = to_scsi_device(dev); \
261 snscanf (buf, 20, format_string, &sdev->field); \
264 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
266 /* Currently we don't export bit fields, but we might in future,
267 * so leave this code in */
270 * sdev_rd_attr: create a function and attribute variable for a
271 * read/write bit field.
273 #define sdev_rw_attr_bit(field) \
274 sdev_show_function(field, "%d\n") \
277 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
280 struct scsi_device *sdev; \
281 ret = scsi_sdev_check_buf_bit(buf); \
283 sdev = to_scsi_device(dev); \
289 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
292 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
293 * else return -EINVAL.
295 static int scsi_sdev_check_buf_bit(const char *buf)
297 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
300 else if (buf[0] == '0')
309 * Create the actual show/store functions and data structures.
311 sdev_rd_attr (device_blocked, "%d\n");
312 sdev_rd_attr (queue_depth, "%d\n");
313 sdev_rd_attr (type, "%d\n");
314 sdev_rd_attr (scsi_level, "%d\n");
315 sdev_rd_attr (vendor, "%.8s\n");
316 sdev_rd_attr (model, "%.16s\n");
317 sdev_rd_attr (rev, "%.4s\n");
320 sdev_show_timeout (struct device *dev, char *buf)
322 struct scsi_device *sdev;
323 sdev = to_scsi_device(dev);
324 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
328 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
330 struct scsi_device *sdev;
332 sdev = to_scsi_device(dev);
333 sscanf (buf, "%d\n", &timeout);
334 sdev->timeout = timeout * HZ;
337 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
340 store_rescan_field (struct device *dev, const char *buf, size_t count)
342 scsi_rescan_device(dev);
345 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
347 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
350 scsi_remove_device(to_scsi_device(dev));
353 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
356 store_state_field(struct device *dev, const char *buf, size_t count)
359 struct scsi_device *sdev = to_scsi_device(dev);
360 enum scsi_device_state state = 0;
362 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
363 const int len = strlen(sdev_states[i].name);
364 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
366 state = sdev_states[i].value;
373 if (scsi_device_set_state(sdev, state))
379 show_state_field(struct device *dev, char *buf)
381 struct scsi_device *sdev = to_scsi_device(dev);
382 const char *name = scsi_device_state_name(sdev->sdev_state);
387 return snprintf(buf, 20, "%s\n", name);
390 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
393 show_queue_type_field(struct device *dev, char *buf)
395 struct scsi_device *sdev = to_scsi_device(dev);
396 const char *name = "none";
398 if (sdev->ordered_tags)
400 else if (sdev->simple_tags)
403 return snprintf(buf, 20, "%s\n", name);
406 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
409 show_iostat_counterbits(struct device *dev, char *buf)
411 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
414 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
416 #define show_sdev_iostat(field) \
418 show_iostat_##field(struct device *dev, char *buf) \
420 struct scsi_device *sdev = to_scsi_device(dev); \
421 unsigned long long count = atomic_read(&sdev->field); \
422 return snprintf(buf, 20, "0x%llx\n", count); \
424 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
426 show_sdev_iostat(iorequest_cnt);
427 show_sdev_iostat(iodone_cnt);
428 show_sdev_iostat(ioerr_cnt);
431 /* Default template for device attributes. May NOT be modified */
432 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
433 &dev_attr_device_blocked,
434 &dev_attr_queue_depth,
435 &dev_attr_queue_type,
437 &dev_attr_scsi_level,
445 &dev_attr_iocounterbits,
446 &dev_attr_iorequest_cnt,
447 &dev_attr_iodone_cnt,
452 static ssize_t sdev_store_queue_depth_rw(struct device *dev, const char *buf,
456 struct scsi_device *sdev = to_scsi_device(dev);
457 struct scsi_host_template *sht = sdev->host->hostt;
459 if (!sht->change_queue_depth)
462 depth = simple_strtoul(buf, NULL, 0);
467 retval = sht->change_queue_depth(sdev, depth);
474 static struct device_attribute sdev_attr_queue_depth_rw =
475 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
476 sdev_store_queue_depth_rw);
478 static ssize_t sdev_store_queue_type_rw(struct device *dev, const char *buf,
481 struct scsi_device *sdev = to_scsi_device(dev);
482 struct scsi_host_template *sht = sdev->host->hostt;
483 int tag_type = 0, retval;
484 int prev_tag_type = scsi_get_tag_type(sdev);
486 if (!sdev->tagged_supported || !sht->change_queue_type)
489 if (strncmp(buf, "ordered", 7) == 0)
490 tag_type = MSG_ORDERED_TAG;
491 else if (strncmp(buf, "simple", 6) == 0)
492 tag_type = MSG_SIMPLE_TAG;
493 else if (strncmp(buf, "none", 4) != 0)
496 if (tag_type == prev_tag_type)
499 retval = sht->change_queue_type(sdev, tag_type);
506 static struct device_attribute sdev_attr_queue_type_rw =
507 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
508 sdev_store_queue_type_rw);
510 static struct device_attribute *attr_changed_internally(
511 struct Scsi_Host *shost,
512 struct device_attribute * attr)
514 if (!strcmp("queue_depth", attr->attr.name)
515 && shost->hostt->change_queue_depth)
516 return &sdev_attr_queue_depth_rw;
517 else if (!strcmp("queue_type", attr->attr.name)
518 && shost->hostt->change_queue_type)
519 return &sdev_attr_queue_type_rw;
524 static struct device_attribute *attr_overridden(
525 struct device_attribute **attrs,
526 struct device_attribute *attr)
532 for (i = 0; attrs[i]; i++)
533 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
538 static int attr_add(struct device *dev, struct device_attribute *attr)
540 struct device_attribute *base_attr;
543 * Spare the caller from having to copy things it's not interested in.
545 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
547 /* extend permissions */
548 attr->attr.mode |= base_attr->attr.mode;
550 /* override null show/store with default */
552 attr->show = base_attr->show;
554 attr->store = base_attr->store;
557 return device_create_file(dev, attr);
561 * scsi_sysfs_add_sdev - add scsi device to sysfs
562 * @sdev: scsi_device to add
565 * 0 on Success / non-zero on Failure
567 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
571 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
574 error = device_add(&sdev->sdev_gendev);
576 put_device(sdev->sdev_gendev.parent);
577 printk(KERN_INFO "error 1\n");
580 error = class_device_add(&sdev->sdev_classdev);
582 printk(KERN_INFO "error 2\n");
586 /* take a reference for the sdev_classdev; this is
587 * released by the sdev_class .release */
588 get_device(&sdev->sdev_gendev);
589 if (sdev->host->hostt->sdev_attrs) {
590 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
591 error = attr_add(&sdev->sdev_gendev,
592 sdev->host->hostt->sdev_attrs[i]);
594 scsi_remove_device(sdev);
600 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
601 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
602 scsi_sysfs_sdev_attrs[i])) {
603 struct device_attribute * attr =
604 attr_changed_internally(sdev->host,
605 scsi_sysfs_sdev_attrs[i]);
606 error = device_create_file(&sdev->sdev_gendev, attr);
608 scsi_remove_device(sdev);
614 transport_add_device(&sdev->sdev_gendev);
619 scsi_device_set_state(sdev, SDEV_CANCEL);
621 device_del(&sdev->sdev_gendev);
622 transport_destroy_device(&sdev->sdev_gendev);
623 put_device(&sdev->sdev_gendev);
629 * scsi_remove_device - unregister a device from the scsi bus
630 * @sdev: scsi_device to unregister
632 void scsi_remove_device(struct scsi_device *sdev)
634 struct Scsi_Host *shost = sdev->host;
636 down(&shost->scan_mutex);
637 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
640 class_device_unregister(&sdev->sdev_classdev);
641 device_del(&sdev->sdev_gendev);
642 scsi_device_set_state(sdev, SDEV_DEL);
643 if (sdev->host->hostt->slave_destroy)
644 sdev->host->hostt->slave_destroy(sdev);
645 transport_unregister_device(&sdev->sdev_gendev);
646 put_device(&sdev->sdev_gendev);
648 up(&shost->scan_mutex);
650 EXPORT_SYMBOL(scsi_remove_device);
652 void __scsi_remove_target(struct scsi_target *starget)
654 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
656 struct scsi_device *sdev, *tmp;
658 spin_lock_irqsave(shost->host_lock, flags);
660 list_for_each_entry_safe(sdev, tmp, &shost->__devices, siblings) {
661 if (sdev->channel != starget->channel ||
662 sdev->id != starget->id)
664 spin_unlock_irqrestore(shost->host_lock, flags);
665 scsi_remove_device(sdev);
666 spin_lock_irqsave(shost->host_lock, flags);
668 spin_unlock_irqrestore(shost->host_lock, flags);
669 scsi_target_reap(starget);
673 * scsi_remove_target - try to remove a target and all its devices
674 * @dev: generic starget or parent of generic stargets to be removed
676 * Note: This is slightly racy. It is possible that if the user
677 * requests the addition of another device then the target won't be
680 void scsi_remove_target(struct device *dev)
682 struct device *rdev, *idev, *next;
684 if (scsi_is_target_device(dev)) {
685 __scsi_remove_target(to_scsi_target(dev));
689 rdev = get_device(dev);
690 list_for_each_entry_safe(idev, next, &dev->children, node) {
691 if (scsi_is_target_device(idev))
692 __scsi_remove_target(to_scsi_target(idev));
696 EXPORT_SYMBOL(scsi_remove_target);
698 int scsi_register_driver(struct device_driver *drv)
700 drv->bus = &scsi_bus_type;
702 return driver_register(drv);
704 EXPORT_SYMBOL(scsi_register_driver);
706 int scsi_register_interface(struct class_interface *intf)
708 intf->class = &sdev_class;
710 return class_interface_register(intf);
712 EXPORT_SYMBOL(scsi_register_interface);
715 static struct class_device_attribute *class_attr_overridden(
716 struct class_device_attribute **attrs,
717 struct class_device_attribute *attr)
723 for (i = 0; attrs[i]; i++)
724 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
729 static int class_attr_add(struct class_device *classdev,
730 struct class_device_attribute *attr)
732 struct class_device_attribute *base_attr;
735 * Spare the caller from having to copy things it's not interested in.
737 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
739 /* extend permissions */
740 attr->attr.mode |= base_attr->attr.mode;
742 /* override null show/store with default */
744 attr->show = base_attr->show;
746 attr->store = base_attr->store;
749 return class_device_create_file(classdev, attr);
753 * scsi_sysfs_add_host - add scsi host to subsystem
754 * @shost: scsi host struct to add to subsystem
755 * @dev: parent struct device pointer
757 int scsi_sysfs_add_host(struct Scsi_Host *shost)
761 if (shost->hostt->shost_attrs) {
762 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
763 error = class_attr_add(&shost->shost_classdev,
764 shost->hostt->shost_attrs[i]);
770 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
771 if (!class_attr_overridden(shost->hostt->shost_attrs,
772 scsi_sysfs_shost_attrs[i])) {
773 error = class_device_create_file(&shost->shost_classdev,
774 scsi_sysfs_shost_attrs[i]);
780 transport_register_device(&shost->shost_gendev);
784 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
787 struct Scsi_Host *shost = sdev->host;
788 struct scsi_target *starget = sdev->sdev_target;
790 device_initialize(&sdev->sdev_gendev);
791 sdev->sdev_gendev.bus = &scsi_bus_type;
792 sdev->sdev_gendev.release = scsi_device_dev_release;
793 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
794 sdev->host->host_no, sdev->channel, sdev->id,
797 class_device_initialize(&sdev->sdev_classdev);
798 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
799 sdev->sdev_classdev.class = &sdev_class;
800 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
801 "%d:%d:%d:%d", sdev->host->host_no,
802 sdev->channel, sdev->id, sdev->lun);
803 sdev->scsi_level = SCSI_2;
804 transport_setup_device(&sdev->sdev_gendev);
805 spin_lock_irqsave(shost->host_lock, flags);
806 list_add_tail(&sdev->same_target_siblings, &starget->devices);
807 list_add_tail(&sdev->siblings, &shost->__devices);
808 spin_unlock_irqrestore(shost->host_lock, flags);
811 int scsi_is_sdev_device(const struct device *dev)
813 return dev->release == scsi_device_dev_release;
815 EXPORT_SYMBOL(scsi_is_sdev_device);
817 /* A blank transport template that is used in drivers that don't
818 * yet implement Transport Attributes */
819 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };