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" },
35 const char *scsi_device_state_name(enum scsi_device_state state)
40 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
41 if (sdev_states[i].value == state) {
42 name = sdev_states[i].name;
49 static int check_set(unsigned int *val, char *src)
53 if (strncmp(src, "-", 20) == 0) {
54 *val = SCAN_WILD_CARD;
57 * Doesn't check for int overflow
59 *val = simple_strtoul(src, &last, 0);
66 static int scsi_scan(struct Scsi_Host *shost, const char *str)
68 char s1[15], s2[15], s3[15], junk;
69 unsigned int channel, id, lun;
72 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
75 if (check_set(&channel, s1))
77 if (check_set(&id, s2))
79 if (check_set(&lun, s3))
81 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
86 * shost_show_function: macro to create an attr function that can be used to
87 * show a non-bit field.
89 #define shost_show_function(name, field, format_string) \
91 show_##name (struct class_device *class_dev, char *buf) \
93 struct Scsi_Host *shost = class_to_shost(class_dev); \
94 return snprintf (buf, 20, format_string, shost->field); \
98 * shost_rd_attr: macro to create a function and attribute variable for a
101 #define shost_rd_attr2(name, field, format_string) \
102 shost_show_function(name, field, format_string) \
103 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
105 #define shost_rd_attr(field, format_string) \
106 shost_rd_attr2(field, field, format_string)
109 * Create the actual show/store functions and data structures.
112 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
115 struct Scsi_Host *shost = class_to_shost(class_dev);
118 res = scsi_scan(shost, buf);
123 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
125 shost_rd_attr(unique_id, "%u\n");
126 shost_rd_attr(host_busy, "%hu\n");
127 shost_rd_attr(cmd_per_lun, "%hd\n");
128 shost_rd_attr(sg_tablesize, "%hu\n");
129 shost_rd_attr(unchecked_isa_dma, "%d\n");
130 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
132 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
133 &class_device_attr_unique_id,
134 &class_device_attr_host_busy,
135 &class_device_attr_cmd_per_lun,
136 &class_device_attr_sg_tablesize,
137 &class_device_attr_unchecked_isa_dma,
138 &class_device_attr_proc_name,
139 &class_device_attr_scan,
143 static void scsi_device_cls_release(struct class_device *class_dev)
145 struct scsi_device *sdev;
147 sdev = class_to_sdev(class_dev);
148 put_device(&sdev->sdev_gendev);
151 void scsi_device_dev_release(struct device *dev)
153 struct scsi_device *sdev;
154 struct device *parent;
157 parent = dev->parent;
158 sdev = to_scsi_device(dev);
160 spin_lock_irqsave(sdev->host->host_lock, flags);
161 list_del(&sdev->siblings);
162 list_del(&sdev->same_target_siblings);
163 list_del(&sdev->starved_entry);
164 if (sdev->single_lun && --sdev->sdev_target->starget_refcnt == 0)
165 kfree(sdev->sdev_target);
166 spin_unlock_irqrestore(sdev->host->host_lock, flags);
168 if (sdev->request_queue)
169 scsi_free_queue(sdev->request_queue);
171 kfree(sdev->inquiry);
177 struct class sdev_class = {
178 .name = "scsi_device",
179 .release = scsi_device_cls_release,
182 /* all probing is done in the individual ->probe routines */
183 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
185 struct scsi_device *sdp = to_scsi_device(dev);
186 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
189 struct bus_type scsi_bus_type = {
191 .match = scsi_bus_match,
194 int scsi_sysfs_register(void)
198 error = bus_register(&scsi_bus_type);
200 error = class_register(&sdev_class);
202 bus_unregister(&scsi_bus_type);
208 void scsi_sysfs_unregister(void)
210 class_unregister(&sdev_class);
211 bus_unregister(&scsi_bus_type);
215 * sdev_show_function: macro to create an attr function that can be used to
216 * show a non-bit field.
218 #define sdev_show_function(field, format_string) \
220 sdev_show_##field (struct device *dev, char *buf) \
222 struct scsi_device *sdev; \
223 sdev = to_scsi_device(dev); \
224 return snprintf (buf, 20, format_string, sdev->field); \
228 * sdev_rd_attr: macro to create a function and attribute variable for a
231 #define sdev_rd_attr(field, format_string) \
232 sdev_show_function(field, format_string) \
233 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
237 * sdev_rd_attr: create a function and attribute variable for a
240 #define sdev_rw_attr(field, format_string) \
241 sdev_show_function(field, format_string) \
244 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
246 struct scsi_device *sdev; \
247 sdev = to_scsi_device(dev); \
248 snscanf (buf, 20, format_string, &sdev->field); \
251 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
253 /* Currently we don't export bit fields, but we might in future,
254 * so leave this code in */
257 * sdev_rd_attr: create a function and attribute variable for a
258 * read/write bit field.
260 #define sdev_rw_attr_bit(field) \
261 sdev_show_function(field, "%d\n") \
264 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
267 struct scsi_device *sdev; \
268 ret = scsi_sdev_check_buf_bit(buf); \
270 sdev = to_scsi_device(dev); \
276 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
279 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
280 * else return -EINVAL.
282 static int scsi_sdev_check_buf_bit(const char *buf)
284 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
287 else if (buf[0] == '0')
296 * Create the actual show/store functions and data structures.
298 sdev_rd_attr (device_blocked, "%d\n");
299 sdev_rd_attr (queue_depth, "%d\n");
300 sdev_rd_attr (type, "%d\n");
301 sdev_rd_attr (scsi_level, "%d\n");
302 sdev_rd_attr (vendor, "%.8s\n");
303 sdev_rd_attr (model, "%.16s\n");
304 sdev_rd_attr (rev, "%.4s\n");
307 sdev_show_timeout (struct device *dev, char *buf)
309 struct scsi_device *sdev;
310 sdev = to_scsi_device(dev);
311 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
315 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
317 struct scsi_device *sdev;
319 sdev = to_scsi_device(dev);
320 sscanf (buf, "%d\n", &timeout);
321 sdev->timeout = timeout * HZ;
324 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
327 store_rescan_field (struct device *dev, const char *buf, size_t count)
329 scsi_rescan_device(dev);
332 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
334 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
337 scsi_remove_device(to_scsi_device(dev));
340 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
343 store_state_field(struct device *dev, const char *buf, size_t count)
346 struct scsi_device *sdev = to_scsi_device(dev);
347 enum scsi_device_state state = 0;
349 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
350 const int len = strlen(sdev_states[i].name);
351 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
353 state = sdev_states[i].value;
360 if (scsi_device_set_state(sdev, state))
366 show_state_field(struct device *dev, char *buf)
368 struct scsi_device *sdev = to_scsi_device(dev);
369 const char *name = scsi_device_state_name(sdev->sdev_state);
374 return snprintf(buf, 20, "%s\n", name);
377 DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
380 /* Default template for device attributes. May NOT be modified */
381 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
382 &dev_attr_device_blocked,
383 &dev_attr_queue_depth,
385 &dev_attr_scsi_level,
397 static struct device_attribute *attr_overridden(
398 struct device_attribute **attrs,
399 struct device_attribute *attr)
405 for (i = 0; attrs[i]; i++)
406 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
411 static int attr_add(struct device *dev, struct device_attribute *attr)
413 struct device_attribute *base_attr;
416 * Spare the caller from having to copy things it's not interested in.
418 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
420 /* extend permissions */
421 attr->attr.mode |= base_attr->attr.mode;
423 /* override null show/store with default */
425 attr->show = base_attr->show;
427 attr->store = base_attr->store;
430 return device_create_file(dev, attr);
434 * scsi_sysfs_add_sdev - add scsi device to sysfs
435 * @sdev: scsi_device to add
438 * 0 on Success / non-zero on Failure
440 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
442 struct class_device_attribute **attrs;
445 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
448 error = device_add(&sdev->sdev_gendev);
450 printk(KERN_INFO "error 1\n");
454 error = class_device_add(&sdev->sdev_classdev);
456 printk(KERN_INFO "error 2\n");
459 /* take a reference for the sdev_classdev; this is
460 * released by the sdev_class .release */
461 get_device(&sdev->sdev_gendev);
463 if (sdev->transport_classdev.class) {
464 error = class_device_add(&sdev->transport_classdev);
467 /* take a reference for the transport_classdev; this
468 * is released by the transport_class .release */
469 get_device(&sdev->sdev_gendev);
473 if (sdev->host->hostt->sdev_attrs) {
474 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
475 error = attr_add(&sdev->sdev_gendev,
476 sdev->host->hostt->sdev_attrs[i]);
478 scsi_remove_device(sdev);
484 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
485 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
486 scsi_sysfs_sdev_attrs[i])) {
487 error = device_create_file(&sdev->sdev_gendev,
488 scsi_sysfs_sdev_attrs[i]);
490 scsi_remove_device(sdev);
496 if (sdev->transport_classdev.class) {
497 attrs = sdev->host->transportt->attrs;
498 for (i = 0; attrs[i]; i++) {
499 error = class_device_create_file(&sdev->transport_classdev,
502 scsi_remove_device(sdev);
512 class_device_del(&sdev->sdev_classdev);
514 scsi_device_set_state(sdev, SDEV_CANCEL);
516 device_del(&sdev->sdev_gendev);
517 put_device(&sdev->sdev_gendev);
523 * scsi_remove_device - unregister a device from the scsi bus
524 * @sdev: scsi_device to unregister
526 void scsi_remove_device(struct scsi_device *sdev)
528 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
531 class_device_unregister(&sdev->sdev_classdev);
532 if (sdev->transport_classdev.class)
533 class_device_unregister(&sdev->transport_classdev);
534 device_del(&sdev->sdev_gendev);
535 scsi_device_set_state(sdev, SDEV_DEL);
536 if (sdev->host->hostt->slave_destroy)
537 sdev->host->hostt->slave_destroy(sdev);
538 if (sdev->host->transportt->cleanup)
539 sdev->host->transportt->cleanup(sdev);
540 put_device(&sdev->sdev_gendev);
543 int scsi_register_driver(struct device_driver *drv)
545 drv->bus = &scsi_bus_type;
547 return driver_register(drv);
550 int scsi_register_interface(struct class_interface *intf)
552 intf->class = &sdev_class;
554 return class_interface_register(intf);
558 static struct class_device_attribute *class_attr_overridden(
559 struct class_device_attribute **attrs,
560 struct class_device_attribute *attr)
566 for (i = 0; attrs[i]; i++)
567 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
572 static int class_attr_add(struct class_device *classdev,
573 struct class_device_attribute *attr)
575 struct class_device_attribute *base_attr;
578 * Spare the caller from having to copy things it's not interested in.
580 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
582 /* extend permissions */
583 attr->attr.mode |= base_attr->attr.mode;
585 /* override null show/store with default */
587 attr->show = base_attr->show;
589 attr->store = base_attr->store;
592 return class_device_create_file(classdev, attr);
596 * scsi_sysfs_add_host - add scsi host to subsystem
597 * @shost: scsi host struct to add to subsystem
598 * @dev: parent struct device pointer
600 int scsi_sysfs_add_host(struct Scsi_Host *shost)
604 if (shost->hostt->shost_attrs) {
605 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
606 error = class_attr_add(&shost->shost_classdev,
607 shost->hostt->shost_attrs[i]);
613 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
614 if (!class_attr_overridden(shost->hostt->shost_attrs,
615 scsi_sysfs_shost_attrs[i])) {
616 error = class_device_create_file(&shost->shost_classdev,
617 scsi_sysfs_shost_attrs[i]);
626 /* A blank transport template that is used in drivers that don't
627 * yet implement Transport Attributes */
628 struct scsi_transport_template blank_transport_template = { NULL, };