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_host.h>
16 #include <scsi/scsi_transport.h>
19 #include "scsi_priv.h"
20 #include "scsi_logging.h"
23 enum scsi_device_state value;
26 { SDEV_CREATED, "created" },
27 { SDEV_RUNNING, "running" },
28 { SDEV_CANCEL, "cancel" },
29 { SDEV_DEL, "deleted" },
30 { SDEV_QUIESCE, "quiesce" },
31 { SDEV_OFFLINE, "offline" },
34 const char *scsi_device_state_name(enum scsi_device_state state)
39 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
40 if (sdev_states[i].value == state) {
41 name = sdev_states[i].name;
48 static int check_set(unsigned int *val, char *src)
52 if (strncmp(src, "-", 20) == 0) {
53 *val = SCAN_WILD_CARD;
56 * Doesn't check for int overflow
58 *val = simple_strtoul(src, &last, 0);
65 static int scsi_scan(struct Scsi_Host *shost, const char *str)
67 char s1[15], s2[15], s3[15], junk;
68 unsigned int channel, id, lun;
71 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
74 if (check_set(&channel, s1))
76 if (check_set(&id, s2))
78 if (check_set(&lun, s3))
80 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
85 * shost_show_function: macro to create an attr function that can be used to
86 * show a non-bit field.
88 #define shost_show_function(name, field, format_string) \
90 show_##name (struct class_device *class_dev, char *buf) \
92 struct Scsi_Host *shost = class_to_shost(class_dev); \
93 return snprintf (buf, 20, format_string, shost->field); \
97 * shost_rd_attr: macro to create a function and attribute variable for a
100 #define shost_rd_attr2(name, field, format_string) \
101 shost_show_function(name, field, format_string) \
102 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
104 #define shost_rd_attr(field, format_string) \
105 shost_rd_attr2(field, field, format_string)
108 * Create the actual show/store functions and data structures.
111 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
114 struct Scsi_Host *shost = class_to_shost(class_dev);
117 res = scsi_scan(shost, buf);
122 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
124 shost_rd_attr(unique_id, "%u\n");
125 shost_rd_attr(host_busy, "%hu\n");
126 shost_rd_attr(cmd_per_lun, "%hd\n");
127 shost_rd_attr(sg_tablesize, "%hu\n");
128 shost_rd_attr(unchecked_isa_dma, "%d\n");
129 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
131 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
132 &class_device_attr_unique_id,
133 &class_device_attr_host_busy,
134 &class_device_attr_cmd_per_lun,
135 &class_device_attr_sg_tablesize,
136 &class_device_attr_unchecked_isa_dma,
137 &class_device_attr_proc_name,
138 &class_device_attr_scan,
142 static void scsi_device_cls_release(struct class_device *class_dev)
144 struct scsi_device *sdev;
146 sdev = class_to_sdev(class_dev);
147 put_device(&sdev->sdev_gendev);
150 void scsi_device_dev_release(struct device *dev)
152 struct scsi_device *sdev;
153 struct device *parent;
156 parent = dev->parent;
157 sdev = to_scsi_device(dev);
159 spin_lock_irqsave(sdev->host->host_lock, flags);
160 list_del(&sdev->siblings);
161 list_del(&sdev->same_target_siblings);
162 list_del(&sdev->starved_entry);
163 if (sdev->single_lun && --sdev->sdev_target->starget_refcnt == 0)
164 kfree(sdev->sdev_target);
165 spin_unlock_irqrestore(sdev->host->host_lock, flags);
167 if (sdev->request_queue)
168 scsi_free_queue(sdev->request_queue);
170 kfree(sdev->inquiry);
176 struct class sdev_class = {
177 .name = "scsi_device",
178 .release = scsi_device_cls_release,
181 /* all probing is done in the individual ->probe routines */
182 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
187 struct bus_type scsi_bus_type = {
189 .match = scsi_bus_match,
192 int scsi_sysfs_register(void)
196 error = bus_register(&scsi_bus_type);
198 error = class_register(&sdev_class);
200 bus_unregister(&scsi_bus_type);
206 void scsi_sysfs_unregister(void)
208 class_unregister(&sdev_class);
209 bus_unregister(&scsi_bus_type);
213 * sdev_show_function: macro to create an attr function that can be used to
214 * show a non-bit field.
216 #define sdev_show_function(field, format_string) \
218 sdev_show_##field (struct device *dev, char *buf) \
220 struct scsi_device *sdev; \
221 sdev = to_scsi_device(dev); \
222 return snprintf (buf, 20, format_string, sdev->field); \
226 * sdev_rd_attr: macro to create a function and attribute variable for a
229 #define sdev_rd_attr(field, format_string) \
230 sdev_show_function(field, format_string) \
231 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL)
235 * sdev_rd_attr: create a function and attribute variable for a
238 #define sdev_rw_attr(field, format_string) \
239 sdev_show_function(field, format_string) \
242 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
244 struct scsi_device *sdev; \
245 sdev = to_scsi_device(dev); \
246 snscanf (buf, 20, format_string, &sdev->field); \
249 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field)
251 /* Currently we don't export bit fields, but we might in future,
252 * so leave this code in */
255 * sdev_rd_attr: create a function and attribute variable for a
256 * read/write bit field.
258 #define sdev_rw_attr_bit(field) \
259 sdev_show_function(field, "%d\n") \
262 sdev_store_##field (struct device *dev, const char *buf, size_t count) \
265 struct scsi_device *sdev; \
266 ret = scsi_sdev_check_buf_bit(buf); \
268 sdev = to_scsi_device(dev); \
274 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field)
277 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
278 * else return -EINVAL.
280 static int scsi_sdev_check_buf_bit(const char *buf)
282 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
285 else if (buf[0] == '0')
294 * Create the actual show/store functions and data structures.
296 sdev_rd_attr (device_blocked, "%d\n");
297 sdev_rd_attr (queue_depth, "%d\n");
298 sdev_rd_attr (type, "%d\n");
299 sdev_rd_attr (scsi_level, "%d\n");
300 sdev_rd_attr (vendor, "%.8s\n");
301 sdev_rd_attr (model, "%.16s\n");
302 sdev_rd_attr (rev, "%.4s\n");
305 store_rescan_field (struct device *dev, const char *buf, size_t count)
307 scsi_rescan_device(dev);
310 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field)
312 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
315 scsi_remove_device(to_scsi_device(dev));
318 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
321 store_state_field(struct device *dev, const char *buf, size_t count)
324 struct scsi_device *sdev = to_scsi_device(dev);
325 enum scsi_device_state state = 0;
327 for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
328 const int len = strlen(sdev_states[i].name);
329 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
331 state = sdev_states[i].value;
338 if (scsi_device_set_state(sdev, state))
344 show_state_field(struct device *dev, char *buf)
346 struct scsi_device *sdev = to_scsi_device(dev);
347 const char *name = scsi_device_state_name(sdev->sdev_state);
352 return snprintf(buf, 20, "%s\n", name);
355 DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
358 /* Default template for device attributes. May NOT be modified */
359 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
360 &dev_attr_device_blocked,
361 &dev_attr_queue_depth,
363 &dev_attr_scsi_level,
374 static struct device_attribute *attr_overridden(
375 struct device_attribute **attrs,
376 struct device_attribute *attr)
382 for (i = 0; attrs[i]; i++)
383 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
388 static int attr_add(struct device *dev, struct device_attribute *attr)
390 struct device_attribute *base_attr;
393 * Spare the caller from having to copy things it's not interested in.
395 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
397 /* extend permissions */
398 attr->attr.mode |= base_attr->attr.mode;
400 /* override null show/store with default */
402 attr->show = base_attr->show;
404 attr->store = base_attr->store;
407 return device_create_file(dev, attr);
411 * scsi_sysfs_add_sdev - add scsi device to sysfs
412 * @sdev: scsi_device to add
415 * 0 on Success / non-zero on Failure
417 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
419 struct class_device_attribute **attrs;
422 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
425 error = device_add(&sdev->sdev_gendev);
427 printk(KERN_INFO "error 1\n");
431 error = class_device_add(&sdev->sdev_classdev);
433 printk(KERN_INFO "error 2\n");
436 /* take a reference for the sdev_classdev; this is
437 * released by the sdev_class .release */
438 get_device(&sdev->sdev_gendev);
440 if (sdev->transport_classdev.class) {
441 error = class_device_add(&sdev->transport_classdev);
444 /* take a reference for the transport_classdev; this
445 * is released by the transport_class .release */
446 get_device(&sdev->sdev_gendev);
450 if (sdev->host->hostt->sdev_attrs) {
451 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
452 error = attr_add(&sdev->sdev_gendev,
453 sdev->host->hostt->sdev_attrs[i]);
455 scsi_remove_device(sdev);
461 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
462 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
463 scsi_sysfs_sdev_attrs[i])) {
464 error = device_create_file(&sdev->sdev_gendev,
465 scsi_sysfs_sdev_attrs[i]);
467 scsi_remove_device(sdev);
473 if (sdev->transport_classdev.class) {
474 attrs = sdev->host->transportt->attrs;
475 for (i = 0; attrs[i]; i++) {
476 error = class_device_create_file(&sdev->transport_classdev,
479 scsi_remove_device(sdev);
489 class_device_del(&sdev->sdev_classdev);
491 scsi_device_set_state(sdev, SDEV_CANCEL);
493 device_del(&sdev->sdev_gendev);
494 put_device(&sdev->sdev_gendev);
500 * scsi_remove_device - unregister a device from the scsi bus
501 * @sdev: scsi_device to unregister
503 void scsi_remove_device(struct scsi_device *sdev)
505 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
508 class_device_unregister(&sdev->sdev_classdev);
509 if (sdev->transport_classdev.class)
510 class_device_unregister(&sdev->transport_classdev);
511 device_del(&sdev->sdev_gendev);
512 scsi_device_set_state(sdev, SDEV_DEL);
513 if (sdev->host->hostt->slave_destroy)
514 sdev->host->hostt->slave_destroy(sdev);
515 if (sdev->host->transportt->cleanup)
516 sdev->host->transportt->cleanup(sdev);
517 put_device(&sdev->sdev_gendev);
520 int scsi_register_driver(struct device_driver *drv)
522 drv->bus = &scsi_bus_type;
524 return driver_register(drv);
527 int scsi_register_interface(struct class_interface *intf)
529 intf->class = &sdev_class;
531 return class_interface_register(intf);
535 static struct class_device_attribute *class_attr_overridden(
536 struct class_device_attribute **attrs,
537 struct class_device_attribute *attr)
543 for (i = 0; attrs[i]; i++)
544 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
549 static int class_attr_add(struct class_device *classdev,
550 struct class_device_attribute *attr)
552 struct class_device_attribute *base_attr;
555 * Spare the caller from having to copy things it's not interested in.
557 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
559 /* extend permissions */
560 attr->attr.mode |= base_attr->attr.mode;
562 /* override null show/store with default */
564 attr->show = base_attr->show;
566 attr->store = base_attr->store;
569 return class_device_create_file(classdev, attr);
573 * scsi_sysfs_add_host - add scsi host to subsystem
574 * @shost: scsi host struct to add to subsystem
575 * @dev: parent struct device pointer
577 int scsi_sysfs_add_host(struct Scsi_Host *shost)
581 if (shost->hostt->shost_attrs) {
582 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
583 error = class_attr_add(&shost->shost_classdev,
584 shost->hostt->shost_attrs[i]);
590 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
591 if (!class_attr_overridden(shost->hostt->shost_attrs,
592 scsi_sysfs_shost_attrs[i])) {
593 error = class_device_create_file(&shost->shost_classdev,
594 scsi_sysfs_shost_attrs[i]);
603 /* A blank transport template that is used in drivers that don't
604 * yet implement Transport Attributes */
605 struct scsi_transport_template blank_transport_template = { 0, };