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 attribute *scsi_sysfs_shost_attrs[] = {
255 &dev_attr_unique_id.attr,
256 &dev_attr_host_busy.attr,
257 &dev_attr_cmd_per_lun.attr,
258 &dev_attr_can_queue.attr,
259 &dev_attr_sg_tablesize.attr,
260 &dev_attr_unchecked_isa_dma.attr,
261 &dev_attr_proc_name.attr,
263 &dev_attr_hstate.attr,
264 &dev_attr_supported_mode.attr,
265 &dev_attr_active_mode.attr,
269 struct attribute_group scsi_shost_attr_group = {
270 .attrs = scsi_sysfs_shost_attrs,
273 struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
274 &scsi_shost_attr_group,
278 static void scsi_device_cls_release(struct device *class_dev)
280 struct scsi_device *sdev;
282 sdev = class_to_sdev(class_dev);
283 put_device(&sdev->sdev_gendev);
286 static void scsi_device_dev_release_usercontext(struct work_struct *work)
288 struct scsi_device *sdev;
289 struct device *parent;
290 struct scsi_target *starget;
291 struct list_head *this, *tmp;
294 sdev = container_of(work, struct scsi_device, ew.work);
296 parent = sdev->sdev_gendev.parent;
297 starget = to_scsi_target(parent);
299 spin_lock_irqsave(sdev->host->host_lock, flags);
301 list_del(&sdev->siblings);
302 list_del(&sdev->same_target_siblings);
303 list_del(&sdev->starved_entry);
304 spin_unlock_irqrestore(sdev->host->host_lock, flags);
306 cancel_work_sync(&sdev->event_work);
308 list_for_each_safe(this, tmp, &sdev->event_list) {
309 struct scsi_event *evt;
311 evt = list_entry(this, struct scsi_event, node);
312 list_del(&evt->node);
316 if (sdev->request_queue) {
317 sdev->request_queue->queuedata = NULL;
318 /* user context needed to free queue */
319 scsi_free_queue(sdev->request_queue);
320 /* temporary expedient, try to catch use of queue lock
321 * after free of sdev */
322 sdev->request_queue = NULL;
325 scsi_target_reap(scsi_target(sdev));
327 kfree(sdev->inquiry);
334 static void scsi_device_dev_release(struct device *dev)
336 struct scsi_device *sdp = to_scsi_device(dev);
337 execute_in_process_context(scsi_device_dev_release_usercontext,
341 static struct class sdev_class = {
342 .name = "scsi_device",
343 .dev_release = scsi_device_cls_release,
346 /* all probing is done in the individual ->probe routines */
347 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
349 struct scsi_device *sdp;
351 if (dev->type != &scsi_dev_type)
354 sdp = to_scsi_device(dev);
355 if (sdp->no_uld_attach)
357 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
360 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
362 struct scsi_device *sdev = to_scsi_device(dev);
364 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
368 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
370 struct device_driver *drv;
371 struct scsi_device *sdev;
374 if (dev->type != &scsi_dev_type)
378 sdev = to_scsi_device(dev);
380 err = scsi_device_quiesce(sdev);
384 if (drv && drv->suspend) {
385 err = drv->suspend(dev, state);
393 static int scsi_bus_resume(struct device * dev)
395 struct device_driver *drv;
396 struct scsi_device *sdev;
399 if (dev->type != &scsi_dev_type)
403 sdev = to_scsi_device(dev);
405 if (drv && drv->resume)
406 err = drv->resume(dev);
408 scsi_device_resume(sdev);
413 static int scsi_bus_remove(struct device *dev)
415 struct device_driver *drv = dev->driver;
416 struct scsi_device *sdev = to_scsi_device(dev);
419 /* reset the prep_fn back to the default since the
420 * driver may have altered it and it's being removed */
421 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
423 if (drv && drv->remove)
424 err = drv->remove(dev);
429 struct bus_type scsi_bus_type = {
431 .match = scsi_bus_match,
432 .uevent = scsi_bus_uevent,
433 .suspend = scsi_bus_suspend,
434 .resume = scsi_bus_resume,
435 .remove = scsi_bus_remove,
438 int scsi_sysfs_register(void)
442 error = bus_register(&scsi_bus_type);
444 error = class_register(&sdev_class);
446 bus_unregister(&scsi_bus_type);
452 void scsi_sysfs_unregister(void)
454 class_unregister(&sdev_class);
455 bus_unregister(&scsi_bus_type);
459 * sdev_show_function: macro to create an attr function that can be used to
460 * show a non-bit field.
462 #define sdev_show_function(field, format_string) \
464 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
467 struct scsi_device *sdev; \
468 sdev = to_scsi_device(dev); \
469 return snprintf (buf, 20, format_string, sdev->field); \
473 * sdev_rd_attr: macro to create a function and attribute variable for a
476 #define sdev_rd_attr(field, format_string) \
477 sdev_show_function(field, format_string) \
478 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
482 * sdev_rd_attr: create a function and attribute variable for a
485 #define sdev_rw_attr(field, format_string) \
486 sdev_show_function(field, format_string) \
489 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
490 const char *buf, size_t count) \
492 struct scsi_device *sdev; \
493 sdev = to_scsi_device(dev); \
494 snscanf (buf, 20, format_string, &sdev->field); \
497 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
499 /* Currently we don't export bit fields, but we might in future,
500 * so leave this code in */
503 * sdev_rd_attr: create a function and attribute variable for a
504 * read/write bit field.
506 #define sdev_rw_attr_bit(field) \
507 sdev_show_function(field, "%d\n") \
510 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
511 const char *buf, size_t count) \
514 struct scsi_device *sdev; \
515 ret = scsi_sdev_check_buf_bit(buf); \
517 sdev = to_scsi_device(dev); \
523 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
526 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
527 * else return -EINVAL.
529 static int scsi_sdev_check_buf_bit(const char *buf)
531 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
534 else if (buf[0] == '0')
543 * Create the actual show/store functions and data structures.
545 sdev_rd_attr (device_blocked, "%d\n");
546 sdev_rd_attr (queue_depth, "%d\n");
547 sdev_rd_attr (type, "%d\n");
548 sdev_rd_attr (scsi_level, "%d\n");
549 sdev_rd_attr (vendor, "%.8s\n");
550 sdev_rd_attr (model, "%.16s\n");
551 sdev_rd_attr (rev, "%.4s\n");
554 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
556 struct scsi_device *sdev;
557 sdev = to_scsi_device(dev);
558 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
562 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
565 struct scsi_device *sdev;
567 sdev = to_scsi_device(dev);
568 sscanf (buf, "%d\n", &timeout);
569 sdev->timeout = timeout * HZ;
572 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
575 store_rescan_field (struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
578 scsi_rescan_device(dev);
581 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
583 static void sdev_store_delete_callback(struct device *dev)
585 scsi_remove_device(to_scsi_device(dev));
589 sdev_store_delete(struct device *dev, struct device_attribute *attr,
590 const char *buf, size_t count)
594 /* An attribute cannot be unregistered by one of its own methods,
595 * so we have to use this roundabout approach.
597 rc = device_schedule_callback(dev, sdev_store_delete_callback);
602 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
605 store_state_field(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
609 struct scsi_device *sdev = to_scsi_device(dev);
610 enum scsi_device_state state = 0;
612 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
613 const int len = strlen(sdev_states[i].name);
614 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
616 state = sdev_states[i].value;
623 if (scsi_device_set_state(sdev, state))
629 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
631 struct scsi_device *sdev = to_scsi_device(dev);
632 const char *name = scsi_device_state_name(sdev->sdev_state);
637 return snprintf(buf, 20, "%s\n", name);
640 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
643 show_queue_type_field(struct device *dev, struct device_attribute *attr,
646 struct scsi_device *sdev = to_scsi_device(dev);
647 const char *name = "none";
649 if (sdev->ordered_tags)
651 else if (sdev->simple_tags)
654 return snprintf(buf, 20, "%s\n", name);
657 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
660 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
662 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
665 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
667 #define show_sdev_iostat(field) \
669 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
672 struct scsi_device *sdev = to_scsi_device(dev); \
673 unsigned long long count = atomic_read(&sdev->field); \
674 return snprintf(buf, 20, "0x%llx\n", count); \
676 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
678 show_sdev_iostat(iorequest_cnt);
679 show_sdev_iostat(iodone_cnt);
680 show_sdev_iostat(ioerr_cnt);
683 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
685 struct scsi_device *sdev;
686 sdev = to_scsi_device(dev);
687 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
689 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
691 #define DECLARE_EVT_SHOW(name, Cap_name) \
693 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
696 struct scsi_device *sdev = to_scsi_device(dev); \
697 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
698 return snprintf(buf, 20, "%d\n", val); \
701 #define DECLARE_EVT_STORE(name, Cap_name) \
703 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
704 const char *buf, size_t count) \
706 struct scsi_device *sdev = to_scsi_device(dev); \
707 int val = simple_strtoul(buf, NULL, 0); \
709 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
711 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
717 #define DECLARE_EVT(name, Cap_name) \
718 DECLARE_EVT_SHOW(name, Cap_name) \
719 DECLARE_EVT_STORE(name, Cap_name) \
720 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
721 sdev_store_evt_##name);
722 #define REF_EVT(name) &dev_attr_evt_##name.attr
724 DECLARE_EVT(media_change, MEDIA_CHANGE)
726 /* Default template for device attributes. May NOT be modified */
727 static struct attribute *scsi_sdev_attrs[] = {
728 &dev_attr_device_blocked.attr,
730 &dev_attr_scsi_level.attr,
731 &dev_attr_vendor.attr,
732 &dev_attr_model.attr,
734 &dev_attr_rescan.attr,
735 &dev_attr_delete.attr,
736 &dev_attr_state.attr,
737 &dev_attr_timeout.attr,
738 &dev_attr_iocounterbits.attr,
739 &dev_attr_iorequest_cnt.attr,
740 &dev_attr_iodone_cnt.attr,
741 &dev_attr_ioerr_cnt.attr,
742 &dev_attr_modalias.attr,
743 REF_EVT(media_change),
747 static struct attribute_group scsi_sdev_attr_group = {
748 .attrs = scsi_sdev_attrs,
751 static struct attribute_group *scsi_sdev_attr_groups[] = {
752 &scsi_sdev_attr_group,
757 sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
758 const char *buf, size_t count)
761 struct scsi_device *sdev = to_scsi_device(dev);
762 struct scsi_host_template *sht = sdev->host->hostt;
764 if (!sht->change_queue_depth)
767 depth = simple_strtoul(buf, NULL, 0);
772 retval = sht->change_queue_depth(sdev, depth);
779 static struct device_attribute sdev_attr_queue_depth_rw =
780 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
781 sdev_store_queue_depth_rw);
784 sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
785 const char *buf, size_t count)
787 struct scsi_device *sdev = to_scsi_device(dev);
788 struct scsi_host_template *sht = sdev->host->hostt;
789 int tag_type = 0, retval;
790 int prev_tag_type = scsi_get_tag_type(sdev);
792 if (!sdev->tagged_supported || !sht->change_queue_type)
795 if (strncmp(buf, "ordered", 7) == 0)
796 tag_type = MSG_ORDERED_TAG;
797 else if (strncmp(buf, "simple", 6) == 0)
798 tag_type = MSG_SIMPLE_TAG;
799 else if (strncmp(buf, "none", 4) != 0)
802 if (tag_type == prev_tag_type)
805 retval = sht->change_queue_type(sdev, tag_type);
812 static int scsi_target_add(struct scsi_target *starget)
816 if (starget->state != STARGET_CREATED)
819 error = device_add(&starget->dev);
821 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
822 get_device(&starget->dev);
823 scsi_target_reap(starget);
824 put_device(&starget->dev);
827 transport_add_device(&starget->dev);
828 starget->state = STARGET_RUNNING;
833 static struct device_attribute sdev_attr_queue_type_rw =
834 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
835 sdev_store_queue_type_rw);
838 * scsi_sysfs_add_sdev - add scsi device to sysfs
839 * @sdev: scsi_device to add
842 * 0 on Success / non-zero on Failure
844 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
847 struct request_queue *rq = sdev->request_queue;
848 struct scsi_target *starget = sdev->sdev_target;
850 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
853 error = scsi_target_add(starget);
857 transport_configure_device(&starget->dev);
858 error = device_add(&sdev->sdev_gendev);
860 put_device(sdev->sdev_gendev.parent);
861 printk(KERN_INFO "error 1\n");
864 error = device_add(&sdev->sdev_dev);
866 printk(KERN_INFO "error 2\n");
870 /* take a reference for the sdev_dev; this is
871 * released by the sdev_class .release */
872 get_device(&sdev->sdev_gendev);
874 /* create queue files, which may be writable, depending on the host */
875 if (sdev->host->hostt->change_queue_depth)
876 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
878 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
880 __scsi_remove_device(sdev);
883 if (sdev->host->hostt->change_queue_type)
884 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
886 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
888 __scsi_remove_device(sdev);
892 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
895 sdev_printk(KERN_INFO, sdev,
896 "Failed to register bsg queue, errno=%d\n", error);
898 /* we're treating error on bsg register as non-fatal, so pretend
899 * nothing went wrong */
902 /* add additional host specific attributes */
903 if (sdev->host->hostt->sdev_attrs) {
904 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
905 error = device_create_file(&sdev->sdev_gendev,
906 sdev->host->hostt->sdev_attrs[i]);
908 __scsi_remove_device(sdev);
914 transport_add_device(&sdev->sdev_gendev);
919 scsi_device_set_state(sdev, SDEV_CANCEL);
921 device_del(&sdev->sdev_gendev);
922 transport_destroy_device(&sdev->sdev_gendev);
923 put_device(&sdev->sdev_gendev);
928 void __scsi_remove_device(struct scsi_device *sdev)
930 struct device *dev = &sdev->sdev_gendev;
932 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
935 bsg_unregister_queue(sdev->request_queue);
936 device_unregister(&sdev->sdev_dev);
937 transport_remove_device(dev);
939 scsi_device_set_state(sdev, SDEV_DEL);
940 if (sdev->host->hostt->slave_destroy)
941 sdev->host->hostt->slave_destroy(sdev);
942 transport_destroy_device(dev);
947 * scsi_remove_device - unregister a device from the scsi bus
948 * @sdev: scsi_device to unregister
950 void scsi_remove_device(struct scsi_device *sdev)
952 struct Scsi_Host *shost = sdev->host;
954 mutex_lock(&shost->scan_mutex);
955 __scsi_remove_device(sdev);
956 mutex_unlock(&shost->scan_mutex);
958 EXPORT_SYMBOL(scsi_remove_device);
960 static void __scsi_remove_target(struct scsi_target *starget)
962 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
964 struct scsi_device *sdev;
966 spin_lock_irqsave(shost->host_lock, flags);
969 list_for_each_entry(sdev, &shost->__devices, siblings) {
970 if (sdev->channel != starget->channel ||
971 sdev->id != starget->id ||
972 sdev->sdev_state == SDEV_DEL)
974 spin_unlock_irqrestore(shost->host_lock, flags);
975 scsi_remove_device(sdev);
976 spin_lock_irqsave(shost->host_lock, flags);
979 spin_unlock_irqrestore(shost->host_lock, flags);
980 scsi_target_reap(starget);
983 static int __remove_child (struct device * dev, void * data)
985 if (scsi_is_target_device(dev))
986 __scsi_remove_target(to_scsi_target(dev));
991 * scsi_remove_target - try to remove a target and all its devices
992 * @dev: generic starget or parent of generic stargets to be removed
994 * Note: This is slightly racy. It is possible that if the user
995 * requests the addition of another device then the target won't be
998 void scsi_remove_target(struct device *dev)
1000 struct device *rdev;
1002 if (scsi_is_target_device(dev)) {
1003 __scsi_remove_target(to_scsi_target(dev));
1007 rdev = get_device(dev);
1008 device_for_each_child(dev, NULL, __remove_child);
1011 EXPORT_SYMBOL(scsi_remove_target);
1013 int scsi_register_driver(struct device_driver *drv)
1015 drv->bus = &scsi_bus_type;
1017 return driver_register(drv);
1019 EXPORT_SYMBOL(scsi_register_driver);
1021 int scsi_register_interface(struct class_interface *intf)
1023 intf->class = &sdev_class;
1025 return class_interface_register(intf);
1027 EXPORT_SYMBOL(scsi_register_interface);
1030 * scsi_sysfs_add_host - add scsi host to subsystem
1031 * @shost: scsi host struct to add to subsystem
1032 * @dev: parent struct device pointer
1034 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1038 /* add host specific attributes */
1039 if (shost->hostt->shost_attrs) {
1040 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1041 error = device_create_file(&shost->shost_dev,
1042 shost->hostt->shost_attrs[i]);
1048 transport_register_device(&shost->shost_gendev);
1049 transport_configure_device(&shost->shost_gendev);
1053 static struct device_type scsi_dev_type = {
1054 .name = "scsi_device",
1055 .release = scsi_device_dev_release,
1056 .groups = scsi_sdev_attr_groups,
1059 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1061 unsigned long flags;
1062 struct Scsi_Host *shost = sdev->host;
1063 struct scsi_target *starget = sdev->sdev_target;
1065 device_initialize(&sdev->sdev_gendev);
1066 sdev->sdev_gendev.bus = &scsi_bus_type;
1067 sdev->sdev_gendev.type = &scsi_dev_type;
1068 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1069 sdev->host->host_no, sdev->channel, sdev->id,
1072 device_initialize(&sdev->sdev_dev);
1073 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1074 sdev->sdev_dev.class = &sdev_class;
1075 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
1076 "%d:%d:%d:%d", sdev->host->host_no,
1077 sdev->channel, sdev->id, sdev->lun);
1078 sdev->scsi_level = starget->scsi_level;
1079 transport_setup_device(&sdev->sdev_gendev);
1080 spin_lock_irqsave(shost->host_lock, flags);
1081 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1082 list_add_tail(&sdev->siblings, &shost->__devices);
1083 spin_unlock_irqrestore(shost->host_lock, flags);
1086 int scsi_is_sdev_device(const struct device *dev)
1088 return dev->type == &scsi_dev_type;
1090 EXPORT_SYMBOL(scsi_is_sdev_device);
1092 /* A blank transport template that is used in drivers that don't
1093 * yet implement Transport Attributes */
1094 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };