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_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 < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
42 if (sdev_states[i].value == state) {
43 name = sdev_states[i].name;
50 static int check_set(unsigned int *val, char *src)
54 if (strncmp(src, "-", 20) == 0) {
55 *val = SCAN_WILD_CARD;
58 * Doesn't check for int overflow
60 *val = simple_strtoul(src, &last, 0);
67 static int scsi_scan(struct Scsi_Host *shost, const char *str)
69 char s1[15], s2[15], s3[15], junk;
70 unsigned int channel, id, lun;
73 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
76 if (check_set(&channel, s1))
78 if (check_set(&id, s2))
80 if (check_set(&lun, s3))
82 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
87 * shost_show_function: macro to create an attr function that can be used to
88 * show a non-bit field.
90 #define shost_show_function(name, field, format_string) \
92 show_##name (struct class_device *class_dev, char *buf) \
94 struct Scsi_Host *shost = class_to_shost(class_dev); \
95 return snprintf (buf, 20, format_string, shost->field); \
99 * shost_rd_attr: macro to create a function and attribute variable for a
102 #define shost_rd_attr2(name, field, format_string) \
103 shost_show_function(name, field, format_string) \
104 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
106 #define shost_rd_attr(field, format_string) \
107 shost_rd_attr2(field, field, format_string)
110 * Create the actual show/store functions and data structures.
113 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
116 struct Scsi_Host *shost = class_to_shost(class_dev);
119 res = scsi_scan(shost, buf);
124 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
126 shost_rd_attr(unique_id, "%u\n");
127 shost_rd_attr(host_busy, "%hu\n");
128 shost_rd_attr(cmd_per_lun, "%hd\n");
129 shost_rd_attr(sg_tablesize, "%hu\n");
130 shost_rd_attr(unchecked_isa_dma, "%d\n");
131 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
133 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
134 &class_device_attr_unique_id,
135 &class_device_attr_host_busy,
136 &class_device_attr_cmd_per_lun,
137 &class_device_attr_sg_tablesize,
138 &class_device_attr_unchecked_isa_dma,
139 &class_device_attr_proc_name,
140 &class_device_attr_scan,
144 static void scsi_device_cls_release(struct class_device *class_dev)
146 struct scsi_device *sdev;
148 sdev = class_to_sdev(class_dev);
149 put_device(&sdev->sdev_gendev);
152 void scsi_device_dev_release(struct device *dev)
154 struct scsi_device *sdev;
155 struct device *parent;
159 parent = dev->parent;
160 sdev = to_scsi_device(dev);
162 spin_lock_irqsave(sdev->host->host_lock, flags);
163 /* If we're the last LUN on the target, destroy the target */
164 delete = list_empty(&sdev->same_target_siblings);
165 list_del(&sdev->siblings);
166 list_del(&sdev->same_target_siblings);
167 list_del(&sdev->starved_entry);
168 spin_unlock_irqrestore(sdev->host->host_lock, flags);
171 struct scsi_target *starget = to_scsi_target(parent);
172 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
173 if (!starget->create) {
174 if (shost->transportt->target_destroy)
175 shost->transportt->target_destroy(starget);
177 if (starget->transport_classdev.class)
178 class_device_unregister(&starget->transport_classdev);
182 if (sdev->request_queue)
183 scsi_free_queue(sdev->request_queue);
185 kfree(sdev->inquiry);
192 struct class sdev_class = {
193 .name = "scsi_device",
194 .release = scsi_device_cls_release,
197 /* all probing is done in the individual ->probe routines */
198 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
200 struct scsi_device *sdp = to_scsi_device(dev);
201 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
204 struct bus_type scsi_bus_type = {
206 .match = scsi_bus_match,
209 int scsi_sysfs_register(void)
213 error = bus_register(&scsi_bus_type);
215 error = class_register(&sdev_class);
217 bus_unregister(&scsi_bus_type);
223 void scsi_sysfs_unregister(void)
225 class_unregister(&sdev_class);
226 bus_unregister(&scsi_bus_type);
230 * sdev_show_function: macro to create an attr function that can be used to
231 * show a non-bit field.
233 #define sdev_show_function(field, format_string) \
235 sdev_show_##field (struct device *dev, char *buf) \
237 struct scsi_device *sdev; \
238 sdev = to_scsi_device(dev); \
239 return snprintf (buf, 20, format_string, sdev->field); \
243 * sdev_rd_attr: macro to create a function and attribute variable for a
246 #define sdev_rd_attr(field, format_string) \
247 sdev_show_function(field, format_string) \
248 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
252 * sdev_rd_attr: create a function and attribute variable for a
255 #define sdev_rw_attr(field, format_string) \
256 sdev_show_function(field, format_string) \
259 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
261 struct scsi_device *sdev; \
262 sdev = to_scsi_device(dev); \
263 snscanf (buf, 20, format_string, &sdev->field); \
266 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
268 /* Currently we don't export bit fields, but we might in future,
269 * so leave this code in */
272 * sdev_rd_attr: create a function and attribute variable for a
273 * read/write bit field.
275 #define sdev_rw_attr_bit(field) \
276 sdev_show_function(field, "%d\n") \
279 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
282 struct scsi_device *sdev; \
283 ret = scsi_sdev_check_buf_bit(buf); \
285 sdev = to_scsi_device(dev); \
291 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
294 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
295 * else return -EINVAL.
297 static int scsi_sdev_check_buf_bit(const char *buf)
299 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
302 else if (buf[0] == '0')
311 * Create the actual show/store functions and data structures.
313 sdev_rd_attr (device_blocked, "%d\n");
314 sdev_rd_attr (queue_depth, "%d\n");
315 sdev_rd_attr (type, "%d\n");
316 sdev_rd_attr (scsi_level, "%d\n");
317 sdev_rd_attr (vendor, "%.8s\n");
318 sdev_rd_attr (model, "%.16s\n");
319 sdev_rd_attr (rev, "%.4s\n");
322 sdev_show_timeout (struct device *dev, char *buf)
324 struct scsi_device *sdev;
325 sdev = to_scsi_device(dev);
326 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
330 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
332 struct scsi_device *sdev;
334 sdev = to_scsi_device(dev);
335 sscanf (buf, "%d\n", &timeout);
336 sdev->timeout = timeout * HZ;
339 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
342 store_rescan_field (struct device *dev, const char *buf, size_t count)
344 scsi_rescan_device(dev);
347 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
349 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
352 scsi_remove_device(to_scsi_device(dev));
355 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
358 store_state_field(struct device *dev, const char *buf, size_t count)
361 struct scsi_device *sdev = to_scsi_device(dev);
362 enum scsi_device_state state = 0;
364 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
365 const int len = strlen(sdev_states[i].name);
366 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
368 state = sdev_states[i].value;
375 if (scsi_device_set_state(sdev, state))
381 show_state_field(struct device *dev, char *buf)
383 struct scsi_device *sdev = to_scsi_device(dev);
384 const char *name = scsi_device_state_name(sdev->sdev_state);
389 return snprintf(buf, 20, "%s\n", name);
392 DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
395 /* Default template for device attributes. May NOT be modified */
396 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
397 &dev_attr_device_blocked,
398 &dev_attr_queue_depth,
400 &dev_attr_scsi_level,
412 static struct device_attribute *attr_overridden(
413 struct device_attribute **attrs,
414 struct device_attribute *attr)
420 for (i = 0; attrs[i]; i++)
421 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
426 static int attr_add(struct device *dev, struct device_attribute *attr)
428 struct device_attribute *base_attr;
431 * Spare the caller from having to copy things it's not interested in.
433 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
435 /* extend permissions */
436 attr->attr.mode |= base_attr->attr.mode;
438 /* override null show/store with default */
440 attr->show = base_attr->show;
442 attr->store = base_attr->store;
445 return device_create_file(dev, attr);
448 static void scsi_target_dev_release(struct device *dev)
450 struct scsi_target *starget = to_scsi_target(dev);
451 struct device *parent = dev->parent;
457 * scsi_sysfs_add_sdev - add scsi device to sysfs
458 * @sdev: scsi_device to add
461 * 0 on Success / non-zero on Failure
463 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
465 struct class_device_attribute **attrs;
466 struct scsi_target *starget = sdev->sdev_target;
467 struct Scsi_Host *shost = sdev->host;
468 int error, i, create;
471 spin_lock_irqsave(shost->host_lock, flags);
472 create = starget->create;
474 spin_unlock_irqrestore(shost->host_lock, flags);
477 error = device_add(&starget->dev);
479 printk(KERN_ERR "Target device_add failed\n");
482 if (starget->transport_classdev.class) {
484 struct class_device_attribute **attrs =
485 sdev->host->transportt->target_attrs;
487 error = class_device_add(&starget->transport_classdev);
489 dev_printk(KERN_ERR, &starget->dev,
490 "Target transport add failed\n");
494 /* take a reference for the transport_classdev; this
495 * is released by the transport_class .release */
496 get_device(&starget->dev);
497 for (i = 0; attrs[i]; i++) {
498 error = class_device_create_file(&starget->transport_classdev,
501 dev_printk(KERN_ERR, &starget->dev,
502 "Target transport attr add failed\n");
509 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
512 error = device_add(&sdev->sdev_gendev);
514 put_device(sdev->sdev_gendev.parent);
515 printk(KERN_INFO "error 1\n");
519 error = class_device_add(&sdev->sdev_classdev);
521 printk(KERN_INFO "error 2\n");
524 /* take a reference for the sdev_classdev; this is
525 * released by the sdev_class .release */
526 get_device(&sdev->sdev_gendev);
527 if (sdev->transport_classdev.class) {
528 error = class_device_add(&sdev->transport_classdev);
531 /* take a reference for the transport_classdev; this
532 * is released by the transport_class .release */
533 get_device(&sdev->sdev_gendev);
537 if (sdev->host->hostt->sdev_attrs) {
538 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
539 error = attr_add(&sdev->sdev_gendev,
540 sdev->host->hostt->sdev_attrs[i]);
542 scsi_remove_device(sdev);
548 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
549 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
550 scsi_sysfs_sdev_attrs[i])) {
551 error = device_create_file(&sdev->sdev_gendev,
552 scsi_sysfs_sdev_attrs[i]);
554 scsi_remove_device(sdev);
560 if (sdev->transport_classdev.class) {
561 attrs = sdev->host->transportt->device_attrs;
562 for (i = 0; attrs[i]; i++) {
563 error = class_device_create_file(&sdev->transport_classdev,
566 scsi_remove_device(sdev);
576 class_device_del(&sdev->sdev_classdev);
578 scsi_device_set_state(sdev, SDEV_CANCEL);
580 device_del(&sdev->sdev_gendev);
581 put_device(&sdev->sdev_gendev);
587 * scsi_remove_device - unregister a device from the scsi bus
588 * @sdev: scsi_device to unregister
590 void scsi_remove_device(struct scsi_device *sdev)
592 struct Scsi_Host *shost = sdev->host;
594 down(&shost->scan_mutex);
595 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
598 class_device_unregister(&sdev->sdev_classdev);
599 if (sdev->transport_classdev.class)
600 class_device_unregister(&sdev->transport_classdev);
601 device_del(&sdev->sdev_gendev);
602 scsi_device_set_state(sdev, SDEV_DEL);
603 if (sdev->host->hostt->slave_destroy)
604 sdev->host->hostt->slave_destroy(sdev);
605 if (sdev->host->transportt->device_destroy)
606 sdev->host->transportt->device_destroy(sdev);
607 put_device(&sdev->sdev_gendev);
610 up(&shost->scan_mutex);
613 int scsi_register_driver(struct device_driver *drv)
615 drv->bus = &scsi_bus_type;
617 return driver_register(drv);
620 int scsi_register_interface(struct class_interface *intf)
622 intf->class = &sdev_class;
624 return class_interface_register(intf);
628 static struct class_device_attribute *class_attr_overridden(
629 struct class_device_attribute **attrs,
630 struct class_device_attribute *attr)
636 for (i = 0; attrs[i]; i++)
637 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
642 static int class_attr_add(struct class_device *classdev,
643 struct class_device_attribute *attr)
645 struct class_device_attribute *base_attr;
648 * Spare the caller from having to copy things it's not interested in.
650 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
652 /* extend permissions */
653 attr->attr.mode |= base_attr->attr.mode;
655 /* override null show/store with default */
657 attr->show = base_attr->show;
659 attr->store = base_attr->store;
662 return class_device_create_file(classdev, attr);
666 * scsi_sysfs_add_host - add scsi host to subsystem
667 * @shost: scsi host struct to add to subsystem
668 * @dev: parent struct device pointer
670 int scsi_sysfs_add_host(struct Scsi_Host *shost)
674 if (shost->hostt->shost_attrs) {
675 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
676 error = class_attr_add(&shost->shost_classdev,
677 shost->hostt->shost_attrs[i]);
683 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
684 if (!class_attr_overridden(shost->hostt->shost_attrs,
685 scsi_sysfs_shost_attrs[i])) {
686 error = class_device_create_file(&shost->shost_classdev,
687 scsi_sysfs_shost_attrs[i]);
693 class_device_initialize(&shost->transport_classdev);
694 shost->transport_classdev.class = shost->transportt->host_class;
695 shost->transport_classdev.dev = &shost->shost_gendev;
696 snprintf(shost->transport_classdev.class_id, BUS_ID_SIZE,
697 "host%d", shost->host_no);
699 if (shost->transport_classdev.class) {
700 struct class_device_attribute **attrs =
701 shost->transportt->host_attrs;
702 error = class_device_add(&shost->transport_classdev);
705 /* take a reference for the transport_classdev; this
706 * is released by the transport_class .release */
707 get_device(&shost->shost_gendev);
708 for (i = 0; attrs[i]; i++) {
709 error = class_device_create_file(&shost->transport_classdev,
719 int scsi_sysfs_device_initialize(struct scsi_device *sdev)
721 device_initialize(&sdev->sdev_gendev);
722 sdev->sdev_gendev.bus = &scsi_bus_type;
723 sdev->sdev_gendev.release = scsi_device_dev_release;
724 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
725 sdev->host->host_no, sdev->channel, sdev->id,
728 class_device_initialize(&sdev->sdev_classdev);
729 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
730 sdev->sdev_classdev.class = &sdev_class;
731 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
732 "%d:%d:%d:%d", sdev->host->host_no,
733 sdev->channel, sdev->id, sdev->lun);
735 class_device_initialize(&sdev->transport_classdev);
736 sdev->transport_classdev.dev = &sdev->sdev_gendev;
737 sdev->transport_classdev.class = sdev->host->transportt->device_class;
738 snprintf(sdev->transport_classdev.class_id, BUS_ID_SIZE,
739 "%d:%d:%d:%d", sdev->host->host_no,
740 sdev->channel, sdev->id, sdev->lun);
744 int scsi_sysfs_target_initialize(struct scsi_device *sdev)
746 struct scsi_target *starget = NULL;
747 struct Scsi_Host *shost = sdev->host;
748 struct scsi_device *device;
749 struct device *dev = NULL;
753 spin_lock_irqsave(shost->host_lock, flags);
755 * Search for an existing target for this sdev.
757 list_for_each_entry(device, &shost->__devices, siblings) {
758 if (device->id == sdev->id &&
759 device->channel == sdev->channel) {
760 list_add_tail(&sdev->same_target_siblings,
761 &device->same_target_siblings);
762 sdev->scsi_level = device->scsi_level;
763 starget = device->sdev_target;
769 const int size = sizeof(*starget) +
770 shost->transportt->target_size;
771 starget = kmalloc(size, GFP_ATOMIC);
773 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
774 spin_unlock_irqrestore(shost->host_lock,
778 memset(starget, 0, size);
780 device_initialize(dev);
781 dev->parent = get_device(&shost->shost_gendev);
782 dev->release = scsi_target_dev_release;
783 sprintf(dev->bus_id, "target%d:%d:%d",
784 shost->host_no, sdev->channel, sdev->id);
785 class_device_initialize(&starget->transport_classdev);
786 starget->transport_classdev.dev = &starget->dev;
787 starget->transport_classdev.class = shost->transportt->target_class;
788 snprintf(starget->transport_classdev.class_id, BUS_ID_SIZE,
790 shost->host_no, sdev->channel, sdev->id);
791 starget->id = sdev->id;
792 starget->channel = sdev->channel;
793 create = starget->create = 1;
795 * If there wasn't another lun already configured at
796 * this target, then default this device to SCSI_2
797 * until we know better
799 sdev->scsi_level = SCSI_2;
801 get_device(&starget->dev);
802 sdev->sdev_gendev.parent = &starget->dev;
803 sdev->sdev_target = starget;
804 list_add_tail(&sdev->siblings, &shost->__devices);
805 spin_unlock_irqrestore(shost->host_lock, flags);
806 if (create && shost->transportt->target_setup)
807 shost->transportt->target_setup(starget);
811 /* A blank transport template that is used in drivers that don't
812 * yet implement Transport Attributes */
813 struct scsi_transport_template blank_transport_template = { NULL, };