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;
160 parent = dev->parent;
161 sdev = to_scsi_device(dev);
163 spin_lock_irqsave(sdev->host->host_lock, flags);
164 /* If we're the last LUN on the target, destroy the target */
165 delete = list_empty(&sdev->same_target_siblings);
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);
172 struct scsi_target *starget = to_scsi_target(parent);
173 if (!starget->create) {
174 transport_remove_device(&starget->dev);
177 transport_destroy_device(&starget->dev);
181 if (sdev->request_queue)
182 scsi_free_queue(sdev->request_queue);
184 kfree(sdev->inquiry);
191 struct class sdev_class = {
192 .name = "scsi_device",
193 .release = scsi_device_cls_release,
196 /* all probing is done in the individual ->probe routines */
197 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
199 struct scsi_device *sdp = to_scsi_device(dev);
200 if (sdp->no_uld_attach)
202 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
205 struct bus_type scsi_bus_type = {
207 .match = scsi_bus_match,
210 int scsi_sysfs_register(void)
214 error = bus_register(&scsi_bus_type);
216 error = class_register(&sdev_class);
218 bus_unregister(&scsi_bus_type);
224 void scsi_sysfs_unregister(void)
226 class_unregister(&sdev_class);
227 bus_unregister(&scsi_bus_type);
231 * sdev_show_function: macro to create an attr function that can be used to
232 * show a non-bit field.
234 #define sdev_show_function(field, format_string) \
236 sdev_show_##field (struct device *dev, char *buf) \
238 struct scsi_device *sdev; \
239 sdev = to_scsi_device(dev); \
240 return snprintf (buf, 20, format_string, sdev->field); \
244 * sdev_rd_attr: macro to create a function and attribute variable for a
247 #define sdev_rd_attr(field, format_string) \
248 sdev_show_function(field, format_string) \
249 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
253 * sdev_rd_attr: create a function and attribute variable for a
256 #define sdev_rw_attr(field, format_string) \
257 sdev_show_function(field, format_string) \
260 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
262 struct scsi_device *sdev; \
263 sdev = to_scsi_device(dev); \
264 snscanf (buf, 20, format_string, &sdev->field); \
267 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
269 /* Currently we don't export bit fields, but we might in future,
270 * so leave this code in */
273 * sdev_rd_attr: create a function and attribute variable for a
274 * read/write bit field.
276 #define sdev_rw_attr_bit(field) \
277 sdev_show_function(field, "%d\n") \
280 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
283 struct scsi_device *sdev; \
284 ret = scsi_sdev_check_buf_bit(buf); \
286 sdev = to_scsi_device(dev); \
292 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
295 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
296 * else return -EINVAL.
298 static int scsi_sdev_check_buf_bit(const char *buf)
300 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
303 else if (buf[0] == '0')
312 * Create the actual show/store functions and data structures.
314 sdev_rd_attr (device_blocked, "%d\n");
315 sdev_rd_attr (queue_depth, "%d\n");
316 sdev_rd_attr (type, "%d\n");
317 sdev_rd_attr (scsi_level, "%d\n");
318 sdev_rd_attr (vendor, "%.8s\n");
319 sdev_rd_attr (model, "%.16s\n");
320 sdev_rd_attr (rev, "%.4s\n");
323 sdev_show_timeout (struct device *dev, char *buf)
325 struct scsi_device *sdev;
326 sdev = to_scsi_device(dev);
327 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
331 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
333 struct scsi_device *sdev;
335 sdev = to_scsi_device(dev);
336 sscanf (buf, "%d\n", &timeout);
337 sdev->timeout = timeout * HZ;
340 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
343 store_rescan_field (struct device *dev, const char *buf, size_t count)
345 scsi_rescan_device(dev);
348 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
350 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
353 scsi_remove_device(to_scsi_device(dev));
356 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
359 store_state_field(struct device *dev, const char *buf, size_t count)
362 struct scsi_device *sdev = to_scsi_device(dev);
363 enum scsi_device_state state = 0;
365 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
366 const int len = strlen(sdev_states[i].name);
367 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
369 state = sdev_states[i].value;
376 if (scsi_device_set_state(sdev, state))
382 show_state_field(struct device *dev, char *buf)
384 struct scsi_device *sdev = to_scsi_device(dev);
385 const char *name = scsi_device_state_name(sdev->sdev_state);
390 return snprintf(buf, 20, "%s\n", name);
393 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
396 show_queue_type_field(struct device *dev, char *buf)
398 struct scsi_device *sdev = to_scsi_device(dev);
399 const char *name = "none";
401 if (sdev->ordered_tags)
403 else if (sdev->simple_tags)
406 return snprintf(buf, 20, "%s\n", name);
409 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
412 /* Default template for device attributes. May NOT be modified */
413 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
414 &dev_attr_device_blocked,
415 &dev_attr_queue_depth,
416 &dev_attr_queue_type,
418 &dev_attr_scsi_level,
429 static ssize_t sdev_store_queue_depth_rw(struct device *dev, const char *buf,
433 struct scsi_device *sdev = to_scsi_device(dev);
434 struct scsi_host_template *sht = sdev->host->hostt;
436 if (!sht->change_queue_depth)
439 depth = simple_strtoul(buf, NULL, 0);
444 retval = sht->change_queue_depth(sdev, depth);
451 static struct device_attribute sdev_attr_queue_depth_rw =
452 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
453 sdev_store_queue_depth_rw);
455 static ssize_t sdev_store_queue_type_rw(struct device *dev, const char *buf,
458 struct scsi_device *sdev = to_scsi_device(dev);
459 struct scsi_host_template *sht = sdev->host->hostt;
460 int tag_type = 0, retval;
461 int prev_tag_type = scsi_get_tag_type(sdev);
463 if (!sdev->tagged_supported || !sht->change_queue_type)
466 if (strncmp(buf, "ordered", 7) == 0)
467 tag_type = MSG_ORDERED_TAG;
468 else if (strncmp(buf, "simple", 6) == 0)
469 tag_type = MSG_SIMPLE_TAG;
470 else if (strncmp(buf, "none", 4) != 0)
473 if (tag_type == prev_tag_type)
476 retval = sht->change_queue_type(sdev, tag_type);
483 static struct device_attribute sdev_attr_queue_type_rw =
484 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
485 sdev_store_queue_type_rw);
487 static struct device_attribute *attr_changed_internally(
488 struct Scsi_Host *shost,
489 struct device_attribute * attr)
491 if (!strcmp("queue_depth", attr->attr.name)
492 && shost->hostt->change_queue_depth)
493 return &sdev_attr_queue_depth_rw;
494 else if (!strcmp("queue_type", attr->attr.name)
495 && shost->hostt->change_queue_type)
496 return &sdev_attr_queue_type_rw;
501 static struct device_attribute *attr_overridden(
502 struct device_attribute **attrs,
503 struct device_attribute *attr)
509 for (i = 0; attrs[i]; i++)
510 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
515 static int attr_add(struct device *dev, struct device_attribute *attr)
517 struct device_attribute *base_attr;
520 * Spare the caller from having to copy things it's not interested in.
522 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
524 /* extend permissions */
525 attr->attr.mode |= base_attr->attr.mode;
527 /* override null show/store with default */
529 attr->show = base_attr->show;
531 attr->store = base_attr->store;
534 return device_create_file(dev, attr);
537 static void scsi_target_dev_release(struct device *dev)
539 struct scsi_target *starget = to_scsi_target(dev);
540 struct device *parent = dev->parent;
546 * scsi_sysfs_add_sdev - add scsi device to sysfs
547 * @sdev: scsi_device to add
550 * 0 on Success / non-zero on Failure
552 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
554 struct scsi_target *starget = sdev->sdev_target;
555 struct Scsi_Host *shost = sdev->host;
556 int error, i, create;
559 spin_lock_irqsave(shost->host_lock, flags);
560 create = starget->create;
562 spin_unlock_irqrestore(shost->host_lock, flags);
565 error = device_add(&starget->dev);
567 printk(KERN_ERR "Target device_add failed\n");
570 transport_add_device(&starget->dev);
573 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
576 error = device_add(&sdev->sdev_gendev);
578 put_device(sdev->sdev_gendev.parent);
579 printk(KERN_INFO "error 1\n");
582 error = class_device_add(&sdev->sdev_classdev);
584 printk(KERN_INFO "error 2\n");
588 /* take a reference for the sdev_classdev; this is
589 * released by the sdev_class .release */
590 get_device(&sdev->sdev_gendev);
591 if (sdev->host->hostt->sdev_attrs) {
592 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
593 error = attr_add(&sdev->sdev_gendev,
594 sdev->host->hostt->sdev_attrs[i]);
596 scsi_remove_device(sdev);
602 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
603 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
604 scsi_sysfs_sdev_attrs[i])) {
605 struct device_attribute * attr =
606 attr_changed_internally(sdev->host,
607 scsi_sysfs_sdev_attrs[i]);
608 error = device_create_file(&sdev->sdev_gendev, attr);
610 scsi_remove_device(sdev);
616 transport_add_device(&sdev->sdev_gendev);
620 class_device_del(&sdev->sdev_classdev);
622 scsi_device_set_state(sdev, SDEV_CANCEL);
624 device_del(&sdev->sdev_gendev);
625 transport_destroy_device(&sdev->sdev_gendev);
626 put_device(&sdev->sdev_gendev);
632 * scsi_remove_device - unregister a device from the scsi bus
633 * @sdev: scsi_device to unregister
635 void scsi_remove_device(struct scsi_device *sdev)
637 struct Scsi_Host *shost = sdev->host;
639 down(&shost->scan_mutex);
640 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
643 class_device_unregister(&sdev->sdev_classdev);
644 device_del(&sdev->sdev_gendev);
645 scsi_device_set_state(sdev, SDEV_DEL);
646 if (sdev->host->hostt->slave_destroy)
647 sdev->host->hostt->slave_destroy(sdev);
648 transport_unregister_device(&sdev->sdev_gendev);
649 put_device(&sdev->sdev_gendev);
652 up(&shost->scan_mutex);
654 EXPORT_SYMBOL(scsi_remove_device);
656 int scsi_register_driver(struct device_driver *drv)
658 drv->bus = &scsi_bus_type;
660 return driver_register(drv);
662 EXPORT_SYMBOL(scsi_register_driver);
664 int scsi_register_interface(struct class_interface *intf)
666 intf->class = &sdev_class;
668 return class_interface_register(intf);
670 EXPORT_SYMBOL(scsi_register_interface);
673 static struct class_device_attribute *class_attr_overridden(
674 struct class_device_attribute **attrs,
675 struct class_device_attribute *attr)
681 for (i = 0; attrs[i]; i++)
682 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
687 static int class_attr_add(struct class_device *classdev,
688 struct class_device_attribute *attr)
690 struct class_device_attribute *base_attr;
693 * Spare the caller from having to copy things it's not interested in.
695 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
697 /* extend permissions */
698 attr->attr.mode |= base_attr->attr.mode;
700 /* override null show/store with default */
702 attr->show = base_attr->show;
704 attr->store = base_attr->store;
707 return class_device_create_file(classdev, attr);
711 * scsi_sysfs_add_host - add scsi host to subsystem
712 * @shost: scsi host struct to add to subsystem
713 * @dev: parent struct device pointer
715 int scsi_sysfs_add_host(struct Scsi_Host *shost)
719 if (shost->hostt->shost_attrs) {
720 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
721 error = class_attr_add(&shost->shost_classdev,
722 shost->hostt->shost_attrs[i]);
728 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
729 if (!class_attr_overridden(shost->hostt->shost_attrs,
730 scsi_sysfs_shost_attrs[i])) {
731 error = class_device_create_file(&shost->shost_classdev,
732 scsi_sysfs_shost_attrs[i]);
738 transport_register_device(&shost->shost_gendev);
742 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
744 device_initialize(&sdev->sdev_gendev);
745 sdev->sdev_gendev.bus = &scsi_bus_type;
746 sdev->sdev_gendev.release = scsi_device_dev_release;
747 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
748 sdev->host->host_no, sdev->channel, sdev->id,
751 class_device_initialize(&sdev->sdev_classdev);
752 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
753 sdev->sdev_classdev.class = &sdev_class;
754 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
755 "%d:%d:%d:%d", sdev->host->host_no,
756 sdev->channel, sdev->id, sdev->lun);
758 transport_setup_device(&sdev->sdev_gendev);
761 int scsi_is_sdev_device(const struct device *dev)
763 return dev->release == scsi_device_dev_release;
765 EXPORT_SYMBOL(scsi_is_sdev_device);
767 int scsi_sysfs_target_initialize(struct scsi_device *sdev)
769 struct scsi_target *starget = NULL;
770 struct Scsi_Host *shost = sdev->host;
771 struct scsi_device *device;
772 struct device *dev = NULL;
776 spin_lock_irqsave(shost->host_lock, flags);
778 * Search for an existing target for this sdev.
780 list_for_each_entry(device, &shost->__devices, siblings) {
781 if (device->id == sdev->id &&
782 device->channel == sdev->channel) {
783 list_add_tail(&sdev->same_target_siblings,
784 &device->same_target_siblings);
785 sdev->scsi_level = device->scsi_level;
786 starget = device->sdev_target;
792 const int size = sizeof(*starget) +
793 shost->transportt->target_size;
794 starget = kmalloc(size, GFP_ATOMIC);
796 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
797 spin_unlock_irqrestore(shost->host_lock,
801 memset(starget, 0, size);
803 device_initialize(dev);
804 dev->parent = get_device(&shost->shost_gendev);
805 dev->release = scsi_target_dev_release;
806 sprintf(dev->bus_id, "target%d:%d:%d",
807 shost->host_no, sdev->channel, sdev->id);
808 starget->id = sdev->id;
809 starget->channel = sdev->channel;
810 create = starget->create = 1;
812 * If there wasn't another lun already configured at
813 * this target, then default this device to SCSI_2
814 * until we know better
816 sdev->scsi_level = SCSI_2;
818 get_device(&starget->dev);
819 sdev->sdev_gendev.parent = &starget->dev;
820 sdev->sdev_target = starget;
821 list_add_tail(&sdev->siblings, &shost->__devices);
822 spin_unlock_irqrestore(shost->host_lock, flags);
824 transport_setup_device(&starget->dev);
828 int scsi_is_target_device(const struct device *dev)
830 return dev->release == scsi_target_dev_release;
832 EXPORT_SYMBOL(scsi_is_target_device);
834 /* A blank transport template that is used in drivers that don't
835 * yet implement Transport Attributes */
836 struct scsi_transport_template blank_transport_template = { NULL, };