4 * SCSI sysfs interface routines.
6 * Created to pull SCSI mid layer sysfs routines into one file.
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/device.h>
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_device.h>
16 #include <scsi/scsi_host.h>
17 #include <scsi/scsi_tcq.h>
18 #include <scsi/scsi_transport.h>
19 #include <scsi/scsi_driver.h>
21 #include "scsi_priv.h"
22 #include "scsi_logging.h"
24 static struct device_type scsi_dev_type;
27 enum scsi_device_state value;
30 { SDEV_CREATED, "created" },
31 { SDEV_RUNNING, "running" },
32 { SDEV_CANCEL, "cancel" },
33 { SDEV_DEL, "deleted" },
34 { SDEV_QUIESCE, "quiesce" },
35 { SDEV_OFFLINE, "offline" },
36 { SDEV_BLOCK, "blocked" },
39 const char *scsi_device_state_name(enum scsi_device_state state)
44 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
45 if (sdev_states[i].value == state) {
46 name = sdev_states[i].name;
54 enum scsi_host_state value;
57 { SHOST_CREATED, "created" },
58 { SHOST_RUNNING, "running" },
59 { SHOST_CANCEL, "cancel" },
60 { SHOST_DEL, "deleted" },
61 { SHOST_RECOVERY, "recovery" },
62 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
63 { SHOST_DEL_RECOVERY, "deleted/recovery", },
65 const char *scsi_host_state_name(enum scsi_host_state state)
70 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
71 if (shost_states[i].value == state) {
72 name = shost_states[i].name;
79 static int check_set(unsigned int *val, char *src)
83 if (strncmp(src, "-", 20) == 0) {
84 *val = SCAN_WILD_CARD;
87 * Doesn't check for int overflow
89 *val = simple_strtoul(src, &last, 0);
96 static int scsi_scan(struct Scsi_Host *shost, const char *str)
98 char s1[15], s2[15], s3[15], junk;
99 unsigned int channel, id, lun;
102 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
105 if (check_set(&channel, s1))
107 if (check_set(&id, s2))
109 if (check_set(&lun, s3))
111 if (shost->transportt->user_scan)
112 res = shost->transportt->user_scan(shost, channel, id, lun);
114 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
119 * shost_show_function: macro to create an attr function that can be used to
120 * show a non-bit field.
122 #define shost_show_function(name, field, format_string) \
124 show_##name (struct device *dev, struct device_attribute *attr, \
127 struct Scsi_Host *shost = class_to_shost(dev); \
128 return snprintf (buf, 20, format_string, shost->field); \
132 * shost_rd_attr: macro to create a function and attribute variable for a
135 #define shost_rd_attr2(name, field, format_string) \
136 shost_show_function(name, field, format_string) \
137 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
139 #define shost_rd_attr(field, format_string) \
140 shost_rd_attr2(field, field, format_string)
143 * Create the actual show/store functions and data structures.
147 store_scan(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t count)
150 struct Scsi_Host *shost = class_to_shost(dev);
153 res = scsi_scan(shost, buf);
158 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
161 store_shost_state(struct device *dev, struct device_attribute *attr,
162 const char *buf, size_t count)
165 struct Scsi_Host *shost = class_to_shost(dev);
166 enum scsi_host_state state = 0;
168 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
169 const int len = strlen(shost_states[i].name);
170 if (strncmp(shost_states[i].name, buf, len) == 0 &&
172 state = shost_states[i].value;
179 if (scsi_host_set_state(shost, state))
185 show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
187 struct Scsi_Host *shost = class_to_shost(dev);
188 const char *name = scsi_host_state_name(shost->shost_state);
193 return snprintf(buf, 20, "%s\n", name);
196 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
197 struct device_attribute dev_attr_hstate =
198 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
201 show_shost_mode(unsigned int mode, char *buf)
205 if (mode & MODE_INITIATOR)
206 len = sprintf(buf, "%s", "Initiator");
208 if (mode & MODE_TARGET)
209 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
211 len += sprintf(buf + len, "\n");
217 show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
220 struct Scsi_Host *shost = class_to_shost(dev);
221 unsigned int supported_mode = shost->hostt->supported_mode;
223 if (supported_mode == MODE_UNKNOWN)
224 /* by default this should be initiator */
225 supported_mode = MODE_INITIATOR;
227 return show_shost_mode(supported_mode, buf);
230 static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
233 show_shost_active_mode(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct Scsi_Host *shost = class_to_shost(dev);
238 if (shost->active_mode == MODE_UNKNOWN)
239 return snprintf(buf, 20, "unknown\n");
241 return show_shost_mode(shost->active_mode, buf);
244 static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
246 shost_rd_attr(unique_id, "%u\n");
247 shost_rd_attr(host_busy, "%hu\n");
248 shost_rd_attr(cmd_per_lun, "%hd\n");
249 shost_rd_attr(can_queue, "%hd\n");
250 shost_rd_attr(sg_tablesize, "%hu\n");
251 shost_rd_attr(unchecked_isa_dma, "%d\n");
252 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
254 static struct device_attribute *scsi_sysfs_shost_attrs[] = {
257 &dev_attr_cmd_per_lun,
259 &dev_attr_sg_tablesize,
260 &dev_attr_unchecked_isa_dma,
264 &dev_attr_supported_mode,
265 &dev_attr_active_mode,
269 static void scsi_device_cls_release(struct device *class_dev)
271 struct scsi_device *sdev;
273 sdev = class_to_sdev(class_dev);
274 put_device(&sdev->sdev_gendev);
277 static void scsi_device_dev_release_usercontext(struct work_struct *work)
279 struct scsi_device *sdev;
280 struct device *parent;
281 struct scsi_target *starget;
282 struct list_head *this, *tmp;
285 sdev = container_of(work, struct scsi_device, ew.work);
287 parent = sdev->sdev_gendev.parent;
288 starget = to_scsi_target(parent);
290 spin_lock_irqsave(sdev->host->host_lock, flags);
292 list_del(&sdev->siblings);
293 list_del(&sdev->same_target_siblings);
294 list_del(&sdev->starved_entry);
295 spin_unlock_irqrestore(sdev->host->host_lock, flags);
297 cancel_work_sync(&sdev->event_work);
299 list_for_each_safe(this, tmp, &sdev->event_list) {
300 struct scsi_event *evt;
302 evt = list_entry(this, struct scsi_event, node);
303 list_del(&evt->node);
307 if (sdev->request_queue) {
308 sdev->request_queue->queuedata = NULL;
309 /* user context needed to free queue */
310 scsi_free_queue(sdev->request_queue);
311 /* temporary expedient, try to catch use of queue lock
312 * after free of sdev */
313 sdev->request_queue = NULL;
316 scsi_target_reap(scsi_target(sdev));
318 kfree(sdev->inquiry);
325 static void scsi_device_dev_release(struct device *dev)
327 struct scsi_device *sdp = to_scsi_device(dev);
328 execute_in_process_context(scsi_device_dev_release_usercontext,
332 static struct class sdev_class = {
333 .name = "scsi_device",
334 .dev_release = scsi_device_cls_release,
337 /* all probing is done in the individual ->probe routines */
338 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
340 struct scsi_device *sdp;
342 if (dev->type != &scsi_dev_type)
345 sdp = to_scsi_device(dev);
346 if (sdp->no_uld_attach)
348 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
351 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
353 struct scsi_device *sdev = to_scsi_device(dev);
355 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
359 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
361 struct device_driver *drv;
362 struct scsi_device *sdev;
365 if (dev->type != &scsi_dev_type)
369 sdev = to_scsi_device(dev);
371 err = scsi_device_quiesce(sdev);
375 if (drv && drv->suspend) {
376 err = drv->suspend(dev, state);
384 static int scsi_bus_resume(struct device * dev)
386 struct device_driver *drv;
387 struct scsi_device *sdev;
390 if (dev->type != &scsi_dev_type)
394 sdev = to_scsi_device(dev);
396 if (drv && drv->resume)
397 err = drv->resume(dev);
399 scsi_device_resume(sdev);
404 static int scsi_bus_remove(struct device *dev)
406 struct device_driver *drv = dev->driver;
407 struct scsi_device *sdev = to_scsi_device(dev);
410 /* reset the prep_fn back to the default since the
411 * driver may have altered it and it's being removed */
412 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
414 if (drv && drv->remove)
415 err = drv->remove(dev);
420 struct bus_type scsi_bus_type = {
422 .match = scsi_bus_match,
423 .uevent = scsi_bus_uevent,
424 .suspend = scsi_bus_suspend,
425 .resume = scsi_bus_resume,
426 .remove = scsi_bus_remove,
429 int scsi_sysfs_register(void)
433 error = bus_register(&scsi_bus_type);
435 error = class_register(&sdev_class);
437 bus_unregister(&scsi_bus_type);
443 void scsi_sysfs_unregister(void)
445 class_unregister(&sdev_class);
446 bus_unregister(&scsi_bus_type);
450 * sdev_show_function: macro to create an attr function that can be used to
451 * show a non-bit field.
453 #define sdev_show_function(field, format_string) \
455 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
458 struct scsi_device *sdev; \
459 sdev = to_scsi_device(dev); \
460 return snprintf (buf, 20, format_string, sdev->field); \
464 * sdev_rd_attr: macro to create a function and attribute variable for a
467 #define sdev_rd_attr(field, format_string) \
468 sdev_show_function(field, format_string) \
469 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
473 * sdev_rd_attr: create a function and attribute variable for a
476 #define sdev_rw_attr(field, format_string) \
477 sdev_show_function(field, format_string) \
480 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
481 const char *buf, size_t count) \
483 struct scsi_device *sdev; \
484 sdev = to_scsi_device(dev); \
485 snscanf (buf, 20, format_string, &sdev->field); \
488 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
490 /* Currently we don't export bit fields, but we might in future,
491 * so leave this code in */
494 * sdev_rd_attr: create a function and attribute variable for a
495 * read/write bit field.
497 #define sdev_rw_attr_bit(field) \
498 sdev_show_function(field, "%d\n") \
501 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
502 const char *buf, size_t count) \
505 struct scsi_device *sdev; \
506 ret = scsi_sdev_check_buf_bit(buf); \
508 sdev = to_scsi_device(dev); \
514 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
517 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
518 * else return -EINVAL.
520 static int scsi_sdev_check_buf_bit(const char *buf)
522 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
525 else if (buf[0] == '0')
534 * Create the actual show/store functions and data structures.
536 sdev_rd_attr (device_blocked, "%d\n");
537 sdev_rd_attr (queue_depth, "%d\n");
538 sdev_rd_attr (type, "%d\n");
539 sdev_rd_attr (scsi_level, "%d\n");
540 sdev_rd_attr (vendor, "%.8s\n");
541 sdev_rd_attr (model, "%.16s\n");
542 sdev_rd_attr (rev, "%.4s\n");
545 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
547 struct scsi_device *sdev;
548 sdev = to_scsi_device(dev);
549 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
553 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
554 const char *buf, size_t count)
556 struct scsi_device *sdev;
558 sdev = to_scsi_device(dev);
559 sscanf (buf, "%d\n", &timeout);
560 sdev->timeout = timeout * HZ;
563 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
566 store_rescan_field (struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
569 scsi_rescan_device(dev);
572 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
574 static void sdev_store_delete_callback(struct device *dev)
576 scsi_remove_device(to_scsi_device(dev));
580 sdev_store_delete(struct device *dev, struct device_attribute *attr,
581 const char *buf, size_t count)
585 /* An attribute cannot be unregistered by one of its own methods,
586 * so we have to use this roundabout approach.
588 rc = device_schedule_callback(dev, sdev_store_delete_callback);
593 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
596 store_state_field(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
600 struct scsi_device *sdev = to_scsi_device(dev);
601 enum scsi_device_state state = 0;
603 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
604 const int len = strlen(sdev_states[i].name);
605 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
607 state = sdev_states[i].value;
614 if (scsi_device_set_state(sdev, state))
620 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
622 struct scsi_device *sdev = to_scsi_device(dev);
623 const char *name = scsi_device_state_name(sdev->sdev_state);
628 return snprintf(buf, 20, "%s\n", name);
631 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
634 show_queue_type_field(struct device *dev, struct device_attribute *attr,
637 struct scsi_device *sdev = to_scsi_device(dev);
638 const char *name = "none";
640 if (sdev->ordered_tags)
642 else if (sdev->simple_tags)
645 return snprintf(buf, 20, "%s\n", name);
648 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
651 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
653 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
656 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
658 #define show_sdev_iostat(field) \
660 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
663 struct scsi_device *sdev = to_scsi_device(dev); \
664 unsigned long long count = atomic_read(&sdev->field); \
665 return snprintf(buf, 20, "0x%llx\n", count); \
667 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
669 show_sdev_iostat(iorequest_cnt);
670 show_sdev_iostat(iodone_cnt);
671 show_sdev_iostat(ioerr_cnt);
674 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
676 struct scsi_device *sdev;
677 sdev = to_scsi_device(dev);
678 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
680 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
682 #define DECLARE_EVT_SHOW(name, Cap_name) \
684 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
687 struct scsi_device *sdev = to_scsi_device(dev); \
688 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
689 return snprintf(buf, 20, "%d\n", val); \
692 #define DECLARE_EVT_STORE(name, Cap_name) \
694 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
695 const char *buf, size_t count) \
697 struct scsi_device *sdev = to_scsi_device(dev); \
698 int val = simple_strtoul(buf, NULL, 0); \
700 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
702 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
708 #define DECLARE_EVT(name, Cap_name) \
709 DECLARE_EVT_SHOW(name, Cap_name) \
710 DECLARE_EVT_STORE(name, Cap_name) \
711 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
712 sdev_store_evt_##name);
713 #define REF_EVT(name) &dev_attr_evt_##name.attr
715 DECLARE_EVT(media_change, MEDIA_CHANGE)
717 /* Default template for device attributes. May NOT be modified */
718 static struct attribute *scsi_sdev_attrs[] = {
719 &dev_attr_device_blocked.attr,
721 &dev_attr_scsi_level.attr,
722 &dev_attr_vendor.attr,
723 &dev_attr_model.attr,
725 &dev_attr_rescan.attr,
726 &dev_attr_delete.attr,
727 &dev_attr_state.attr,
728 &dev_attr_timeout.attr,
729 &dev_attr_iocounterbits.attr,
730 &dev_attr_iorequest_cnt.attr,
731 &dev_attr_iodone_cnt.attr,
732 &dev_attr_ioerr_cnt.attr,
733 &dev_attr_modalias.attr,
734 REF_EVT(media_change),
738 static struct attribute_group scsi_sdev_attr_group = {
739 .attrs = scsi_sdev_attrs,
742 static struct attribute_group *scsi_sdev_attr_groups[] = {
743 &scsi_sdev_attr_group,
748 sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
752 struct scsi_device *sdev = to_scsi_device(dev);
753 struct scsi_host_template *sht = sdev->host->hostt;
755 if (!sht->change_queue_depth)
758 depth = simple_strtoul(buf, NULL, 0);
763 retval = sht->change_queue_depth(sdev, depth);
770 static struct device_attribute sdev_attr_queue_depth_rw =
771 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
772 sdev_store_queue_depth_rw);
775 sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
778 struct scsi_device *sdev = to_scsi_device(dev);
779 struct scsi_host_template *sht = sdev->host->hostt;
780 int tag_type = 0, retval;
781 int prev_tag_type = scsi_get_tag_type(sdev);
783 if (!sdev->tagged_supported || !sht->change_queue_type)
786 if (strncmp(buf, "ordered", 7) == 0)
787 tag_type = MSG_ORDERED_TAG;
788 else if (strncmp(buf, "simple", 6) == 0)
789 tag_type = MSG_SIMPLE_TAG;
790 else if (strncmp(buf, "none", 4) != 0)
793 if (tag_type == prev_tag_type)
796 retval = sht->change_queue_type(sdev, tag_type);
803 static struct device_attribute sdev_attr_queue_type_rw =
804 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
805 sdev_store_queue_type_rw);
808 * scsi_sysfs_add_sdev - add scsi device to sysfs
809 * @sdev: scsi_device to add
812 * 0 on Success / non-zero on Failure
814 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
817 struct request_queue *rq = sdev->request_queue;
819 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
822 error = device_add(&sdev->sdev_gendev);
824 put_device(sdev->sdev_gendev.parent);
825 printk(KERN_INFO "error 1\n");
828 error = device_add(&sdev->sdev_dev);
830 printk(KERN_INFO "error 2\n");
834 /* take a reference for the sdev_dev; this is
835 * released by the sdev_class .release */
836 get_device(&sdev->sdev_gendev);
838 /* create queue files, which may be writable, depending on the host */
839 if (sdev->host->hostt->change_queue_depth)
840 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
842 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
844 __scsi_remove_device(sdev);
847 if (sdev->host->hostt->change_queue_type)
848 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
850 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
852 __scsi_remove_device(sdev);
856 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
859 sdev_printk(KERN_INFO, sdev,
860 "Failed to register bsg queue, errno=%d\n", error);
862 /* we're treating error on bsg register as non-fatal, so pretend
863 * nothing went wrong */
866 /* add additional host specific attributes */
867 if (sdev->host->hostt->sdev_attrs) {
868 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
869 error = device_create_file(&sdev->sdev_gendev,
870 sdev->host->hostt->sdev_attrs[i]);
872 __scsi_remove_device(sdev);
878 transport_add_device(&sdev->sdev_gendev);
883 scsi_device_set_state(sdev, SDEV_CANCEL);
885 device_del(&sdev->sdev_gendev);
886 transport_destroy_device(&sdev->sdev_gendev);
887 put_device(&sdev->sdev_gendev);
892 void __scsi_remove_device(struct scsi_device *sdev)
894 struct device *dev = &sdev->sdev_gendev;
896 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
899 bsg_unregister_queue(sdev->request_queue);
900 device_unregister(&sdev->sdev_dev);
901 transport_remove_device(dev);
903 scsi_device_set_state(sdev, SDEV_DEL);
904 if (sdev->host->hostt->slave_destroy)
905 sdev->host->hostt->slave_destroy(sdev);
906 transport_destroy_device(dev);
911 * scsi_remove_device - unregister a device from the scsi bus
912 * @sdev: scsi_device to unregister
914 void scsi_remove_device(struct scsi_device *sdev)
916 struct Scsi_Host *shost = sdev->host;
918 mutex_lock(&shost->scan_mutex);
919 __scsi_remove_device(sdev);
920 mutex_unlock(&shost->scan_mutex);
922 EXPORT_SYMBOL(scsi_remove_device);
924 static void __scsi_remove_target(struct scsi_target *starget)
926 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
928 struct scsi_device *sdev;
930 spin_lock_irqsave(shost->host_lock, flags);
933 list_for_each_entry(sdev, &shost->__devices, siblings) {
934 if (sdev->channel != starget->channel ||
935 sdev->id != starget->id ||
936 sdev->sdev_state == SDEV_DEL)
938 spin_unlock_irqrestore(shost->host_lock, flags);
939 scsi_remove_device(sdev);
940 spin_lock_irqsave(shost->host_lock, flags);
943 spin_unlock_irqrestore(shost->host_lock, flags);
944 scsi_target_reap(starget);
947 static int __remove_child (struct device * dev, void * data)
949 if (scsi_is_target_device(dev))
950 __scsi_remove_target(to_scsi_target(dev));
955 * scsi_remove_target - try to remove a target and all its devices
956 * @dev: generic starget or parent of generic stargets to be removed
958 * Note: This is slightly racy. It is possible that if the user
959 * requests the addition of another device then the target won't be
962 void scsi_remove_target(struct device *dev)
966 if (scsi_is_target_device(dev)) {
967 __scsi_remove_target(to_scsi_target(dev));
971 rdev = get_device(dev);
972 device_for_each_child(dev, NULL, __remove_child);
975 EXPORT_SYMBOL(scsi_remove_target);
977 int scsi_register_driver(struct device_driver *drv)
979 drv->bus = &scsi_bus_type;
981 return driver_register(drv);
983 EXPORT_SYMBOL(scsi_register_driver);
985 int scsi_register_interface(struct class_interface *intf)
987 intf->class = &sdev_class;
989 return class_interface_register(intf);
991 EXPORT_SYMBOL(scsi_register_interface);
994 static struct device_attribute *class_attr_overridden(
995 struct device_attribute **attrs,
996 struct device_attribute *attr)
1002 for (i = 0; attrs[i]; i++)
1003 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
1008 static int class_attr_add(struct device *classdev,
1009 struct device_attribute *attr)
1011 struct device_attribute *base_attr;
1014 * Spare the caller from having to copy things it's not interested in.
1016 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
1018 /* extend permissions */
1019 attr->attr.mode |= base_attr->attr.mode;
1021 /* override null show/store with default */
1023 attr->show = base_attr->show;
1025 attr->store = base_attr->store;
1028 return device_create_file(classdev, attr);
1032 * scsi_sysfs_add_host - add scsi host to subsystem
1033 * @shost: scsi host struct to add to subsystem
1034 * @dev: parent struct device pointer
1036 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1040 if (shost->hostt->shost_attrs) {
1041 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1042 error = class_attr_add(&shost->shost_dev,
1043 shost->hostt->shost_attrs[i]);
1049 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
1050 if (!class_attr_overridden(shost->hostt->shost_attrs,
1051 scsi_sysfs_shost_attrs[i])) {
1052 error = device_create_file(&shost->shost_dev,
1053 scsi_sysfs_shost_attrs[i]);
1059 transport_register_device(&shost->shost_gendev);
1060 transport_configure_device(&shost->shost_gendev);
1064 static struct device_type scsi_dev_type = {
1065 .name = "scsi_device",
1066 .release = scsi_device_dev_release,
1067 .groups = scsi_sdev_attr_groups,
1070 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1072 unsigned long flags;
1073 struct Scsi_Host *shost = sdev->host;
1074 struct scsi_target *starget = sdev->sdev_target;
1076 device_initialize(&sdev->sdev_gendev);
1077 sdev->sdev_gendev.bus = &scsi_bus_type;
1078 sdev->sdev_gendev.type = &scsi_dev_type;
1079 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1080 sdev->host->host_no, sdev->channel, sdev->id,
1083 device_initialize(&sdev->sdev_dev);
1084 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1085 sdev->sdev_dev.class = &sdev_class;
1086 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
1087 "%d:%d:%d:%d", sdev->host->host_no,
1088 sdev->channel, sdev->id, sdev->lun);
1089 sdev->scsi_level = starget->scsi_level;
1090 transport_setup_device(&sdev->sdev_gendev);
1091 spin_lock_irqsave(shost->host_lock, flags);
1092 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1093 list_add_tail(&sdev->siblings, &shost->__devices);
1094 spin_unlock_irqrestore(shost->host_lock, flags);
1097 int scsi_is_sdev_device(const struct device *dev)
1099 return dev->type == &scsi_dev_type;
1101 EXPORT_SYMBOL(scsi_is_sdev_device);
1103 /* A blank transport template that is used in drivers that don't
1104 * yet implement Transport Attributes */
1105 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };