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_attr(prot_capabilities, "%u\n");
253 shost_rd_attr(prot_guard_type, "%hd\n");
254 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
256 static struct attribute *scsi_sysfs_shost_attrs[] = {
257 &dev_attr_unique_id.attr,
258 &dev_attr_host_busy.attr,
259 &dev_attr_cmd_per_lun.attr,
260 &dev_attr_can_queue.attr,
261 &dev_attr_sg_tablesize.attr,
262 &dev_attr_unchecked_isa_dma.attr,
263 &dev_attr_proc_name.attr,
265 &dev_attr_hstate.attr,
266 &dev_attr_supported_mode.attr,
267 &dev_attr_active_mode.attr,
268 &dev_attr_prot_capabilities.attr,
269 &dev_attr_prot_guard_type.attr,
273 struct attribute_group scsi_shost_attr_group = {
274 .attrs = scsi_sysfs_shost_attrs,
277 struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
278 &scsi_shost_attr_group,
282 static void scsi_device_cls_release(struct device *class_dev)
284 struct scsi_device *sdev;
286 sdev = class_to_sdev(class_dev);
287 put_device(&sdev->sdev_gendev);
290 static void scsi_device_dev_release_usercontext(struct work_struct *work)
292 struct scsi_device *sdev;
293 struct device *parent;
294 struct scsi_target *starget;
295 struct list_head *this, *tmp;
298 sdev = container_of(work, struct scsi_device, ew.work);
300 parent = sdev->sdev_gendev.parent;
301 starget = to_scsi_target(parent);
303 spin_lock_irqsave(sdev->host->host_lock, flags);
305 list_del(&sdev->siblings);
306 list_del(&sdev->same_target_siblings);
307 list_del(&sdev->starved_entry);
308 spin_unlock_irqrestore(sdev->host->host_lock, flags);
310 cancel_work_sync(&sdev->event_work);
312 list_for_each_safe(this, tmp, &sdev->event_list) {
313 struct scsi_event *evt;
315 evt = list_entry(this, struct scsi_event, node);
316 list_del(&evt->node);
320 if (sdev->request_queue) {
321 sdev->request_queue->queuedata = NULL;
322 /* user context needed to free queue */
323 scsi_free_queue(sdev->request_queue);
324 /* temporary expedient, try to catch use of queue lock
325 * after free of sdev */
326 sdev->request_queue = NULL;
329 scsi_target_reap(scsi_target(sdev));
331 kfree(sdev->inquiry);
338 static void scsi_device_dev_release(struct device *dev)
340 struct scsi_device *sdp = to_scsi_device(dev);
341 execute_in_process_context(scsi_device_dev_release_usercontext,
345 static struct class sdev_class = {
346 .name = "scsi_device",
347 .dev_release = scsi_device_cls_release,
350 /* all probing is done in the individual ->probe routines */
351 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
353 struct scsi_device *sdp;
355 if (dev->type != &scsi_dev_type)
358 sdp = to_scsi_device(dev);
359 if (sdp->no_uld_attach)
361 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
364 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
366 struct scsi_device *sdev;
368 if (dev->type != &scsi_dev_type)
371 sdev = to_scsi_device(dev);
373 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
377 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
379 struct device_driver *drv;
380 struct scsi_device *sdev;
383 if (dev->type != &scsi_dev_type)
387 sdev = to_scsi_device(dev);
389 err = scsi_device_quiesce(sdev);
393 if (drv && drv->suspend) {
394 err = drv->suspend(dev, state);
402 static int scsi_bus_resume(struct device * dev)
404 struct device_driver *drv;
405 struct scsi_device *sdev;
408 if (dev->type != &scsi_dev_type)
412 sdev = to_scsi_device(dev);
414 if (drv && drv->resume)
415 err = drv->resume(dev);
417 scsi_device_resume(sdev);
422 static int scsi_bus_remove(struct device *dev)
424 struct device_driver *drv = dev->driver;
425 struct scsi_device *sdev = to_scsi_device(dev);
428 /* reset the prep_fn back to the default since the
429 * driver may have altered it and it's being removed */
430 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
432 if (drv && drv->remove)
433 err = drv->remove(dev);
438 struct bus_type scsi_bus_type = {
440 .match = scsi_bus_match,
441 .uevent = scsi_bus_uevent,
442 .suspend = scsi_bus_suspend,
443 .resume = scsi_bus_resume,
444 .remove = scsi_bus_remove,
446 EXPORT_SYMBOL_GPL(scsi_bus_type);
448 int scsi_sysfs_register(void)
452 error = bus_register(&scsi_bus_type);
454 error = class_register(&sdev_class);
456 bus_unregister(&scsi_bus_type);
462 void scsi_sysfs_unregister(void)
464 class_unregister(&sdev_class);
465 bus_unregister(&scsi_bus_type);
469 * sdev_show_function: macro to create an attr function that can be used to
470 * show a non-bit field.
472 #define sdev_show_function(field, format_string) \
474 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
477 struct scsi_device *sdev; \
478 sdev = to_scsi_device(dev); \
479 return snprintf (buf, 20, format_string, sdev->field); \
483 * sdev_rd_attr: macro to create a function and attribute variable for a
486 #define sdev_rd_attr(field, format_string) \
487 sdev_show_function(field, format_string) \
488 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
492 * sdev_rd_attr: create a function and attribute variable for a
495 #define sdev_rw_attr(field, format_string) \
496 sdev_show_function(field, format_string) \
499 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
500 const char *buf, size_t count) \
502 struct scsi_device *sdev; \
503 sdev = to_scsi_device(dev); \
504 snscanf (buf, 20, format_string, &sdev->field); \
507 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
509 /* Currently we don't export bit fields, but we might in future,
510 * so leave this code in */
513 * sdev_rd_attr: create a function and attribute variable for a
514 * read/write bit field.
516 #define sdev_rw_attr_bit(field) \
517 sdev_show_function(field, "%d\n") \
520 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
521 const char *buf, size_t count) \
524 struct scsi_device *sdev; \
525 ret = scsi_sdev_check_buf_bit(buf); \
527 sdev = to_scsi_device(dev); \
533 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
536 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
537 * else return -EINVAL.
539 static int scsi_sdev_check_buf_bit(const char *buf)
541 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
544 else if (buf[0] == '0')
553 * Create the actual show/store functions and data structures.
555 sdev_rd_attr (device_blocked, "%d\n");
556 sdev_rd_attr (queue_depth, "%d\n");
557 sdev_rd_attr (type, "%d\n");
558 sdev_rd_attr (scsi_level, "%d\n");
559 sdev_rd_attr (vendor, "%.8s\n");
560 sdev_rd_attr (model, "%.16s\n");
561 sdev_rd_attr (rev, "%.4s\n");
564 * TODO: can we make these symlinks to the block layer ones?
567 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
569 struct scsi_device *sdev;
570 sdev = to_scsi_device(dev);
571 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
575 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
578 struct scsi_device *sdev;
580 sdev = to_scsi_device(dev);
581 sscanf (buf, "%d\n", &timeout);
582 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
585 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
588 store_rescan_field (struct device *dev, struct device_attribute *attr,
589 const char *buf, size_t count)
591 scsi_rescan_device(dev);
594 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
596 static void sdev_store_delete_callback(struct device *dev)
598 scsi_remove_device(to_scsi_device(dev));
602 sdev_store_delete(struct device *dev, struct device_attribute *attr,
603 const char *buf, size_t count)
607 /* An attribute cannot be unregistered by one of its own methods,
608 * so we have to use this roundabout approach.
610 rc = device_schedule_callback(dev, sdev_store_delete_callback);
615 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
618 store_state_field(struct device *dev, struct device_attribute *attr,
619 const char *buf, size_t count)
622 struct scsi_device *sdev = to_scsi_device(dev);
623 enum scsi_device_state state = 0;
625 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
626 const int len = strlen(sdev_states[i].name);
627 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
629 state = sdev_states[i].value;
636 if (scsi_device_set_state(sdev, state))
642 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
644 struct scsi_device *sdev = to_scsi_device(dev);
645 const char *name = scsi_device_state_name(sdev->sdev_state);
650 return snprintf(buf, 20, "%s\n", name);
653 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
656 show_queue_type_field(struct device *dev, struct device_attribute *attr,
659 struct scsi_device *sdev = to_scsi_device(dev);
660 const char *name = "none";
662 if (sdev->ordered_tags)
664 else if (sdev->simple_tags)
667 return snprintf(buf, 20, "%s\n", name);
670 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
673 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
675 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
678 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
680 #define show_sdev_iostat(field) \
682 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
685 struct scsi_device *sdev = to_scsi_device(dev); \
686 unsigned long long count = atomic_read(&sdev->field); \
687 return snprintf(buf, 20, "0x%llx\n", count); \
689 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
691 show_sdev_iostat(iorequest_cnt);
692 show_sdev_iostat(iodone_cnt);
693 show_sdev_iostat(ioerr_cnt);
696 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
698 struct scsi_device *sdev;
699 sdev = to_scsi_device(dev);
700 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
702 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
704 #define DECLARE_EVT_SHOW(name, Cap_name) \
706 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
709 struct scsi_device *sdev = to_scsi_device(dev); \
710 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
711 return snprintf(buf, 20, "%d\n", val); \
714 #define DECLARE_EVT_STORE(name, Cap_name) \
716 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
717 const char *buf, size_t count) \
719 struct scsi_device *sdev = to_scsi_device(dev); \
720 int val = simple_strtoul(buf, NULL, 0); \
722 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
724 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
730 #define DECLARE_EVT(name, Cap_name) \
731 DECLARE_EVT_SHOW(name, Cap_name) \
732 DECLARE_EVT_STORE(name, Cap_name) \
733 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
734 sdev_store_evt_##name);
735 #define REF_EVT(name) &dev_attr_evt_##name.attr
737 DECLARE_EVT(media_change, MEDIA_CHANGE)
739 /* Default template for device attributes. May NOT be modified */
740 static struct attribute *scsi_sdev_attrs[] = {
741 &dev_attr_device_blocked.attr,
743 &dev_attr_scsi_level.attr,
744 &dev_attr_vendor.attr,
745 &dev_attr_model.attr,
747 &dev_attr_rescan.attr,
748 &dev_attr_delete.attr,
749 &dev_attr_state.attr,
750 &dev_attr_timeout.attr,
751 &dev_attr_iocounterbits.attr,
752 &dev_attr_iorequest_cnt.attr,
753 &dev_attr_iodone_cnt.attr,
754 &dev_attr_ioerr_cnt.attr,
755 &dev_attr_modalias.attr,
756 REF_EVT(media_change),
760 static struct attribute_group scsi_sdev_attr_group = {
761 .attrs = scsi_sdev_attrs,
764 static struct attribute_group *scsi_sdev_attr_groups[] = {
765 &scsi_sdev_attr_group,
770 sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
771 const char *buf, size_t count)
774 struct scsi_device *sdev = to_scsi_device(dev);
775 struct scsi_host_template *sht = sdev->host->hostt;
777 if (!sht->change_queue_depth)
780 depth = simple_strtoul(buf, NULL, 0);
785 retval = sht->change_queue_depth(sdev, depth);
792 static struct device_attribute sdev_attr_queue_depth_rw =
793 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
794 sdev_store_queue_depth_rw);
797 sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
798 const char *buf, size_t count)
800 struct scsi_device *sdev = to_scsi_device(dev);
801 struct scsi_host_template *sht = sdev->host->hostt;
802 int tag_type = 0, retval;
803 int prev_tag_type = scsi_get_tag_type(sdev);
805 if (!sdev->tagged_supported || !sht->change_queue_type)
808 if (strncmp(buf, "ordered", 7) == 0)
809 tag_type = MSG_ORDERED_TAG;
810 else if (strncmp(buf, "simple", 6) == 0)
811 tag_type = MSG_SIMPLE_TAG;
812 else if (strncmp(buf, "none", 4) != 0)
815 if (tag_type == prev_tag_type)
818 retval = sht->change_queue_type(sdev, tag_type);
825 static int scsi_target_add(struct scsi_target *starget)
829 if (starget->state != STARGET_CREATED)
832 error = device_add(&starget->dev);
834 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
835 get_device(&starget->dev);
836 scsi_target_reap(starget);
837 put_device(&starget->dev);
840 transport_add_device(&starget->dev);
841 starget->state = STARGET_RUNNING;
846 static struct device_attribute sdev_attr_queue_type_rw =
847 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
848 sdev_store_queue_type_rw);
851 * scsi_sysfs_add_sdev - add scsi device to sysfs
852 * @sdev: scsi_device to add
855 * 0 on Success / non-zero on Failure
857 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
860 struct request_queue *rq = sdev->request_queue;
861 struct scsi_target *starget = sdev->sdev_target;
863 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
866 error = scsi_target_add(starget);
870 transport_configure_device(&starget->dev);
871 error = device_add(&sdev->sdev_gendev);
873 put_device(sdev->sdev_gendev.parent);
874 printk(KERN_INFO "error 1\n");
877 error = device_add(&sdev->sdev_dev);
879 printk(KERN_INFO "error 2\n");
883 /* take a reference for the sdev_dev; this is
884 * released by the sdev_class .release */
885 get_device(&sdev->sdev_gendev);
887 /* create queue files, which may be writable, depending on the host */
888 if (sdev->host->hostt->change_queue_depth)
889 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
891 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
893 __scsi_remove_device(sdev);
896 if (sdev->host->hostt->change_queue_type)
897 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
899 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
901 __scsi_remove_device(sdev);
905 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
908 sdev_printk(KERN_INFO, sdev,
909 "Failed to register bsg queue, errno=%d\n", error);
911 /* we're treating error on bsg register as non-fatal, so pretend
912 * nothing went wrong */
915 /* add additional host specific attributes */
916 if (sdev->host->hostt->sdev_attrs) {
917 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
918 error = device_create_file(&sdev->sdev_gendev,
919 sdev->host->hostt->sdev_attrs[i]);
921 __scsi_remove_device(sdev);
927 transport_add_device(&sdev->sdev_gendev);
932 scsi_device_set_state(sdev, SDEV_CANCEL);
934 device_del(&sdev->sdev_gendev);
935 transport_destroy_device(&sdev->sdev_gendev);
936 put_device(&sdev->sdev_gendev);
941 void __scsi_remove_device(struct scsi_device *sdev)
943 struct device *dev = &sdev->sdev_gendev;
945 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
948 bsg_unregister_queue(sdev->request_queue);
949 device_unregister(&sdev->sdev_dev);
950 transport_remove_device(dev);
952 scsi_device_set_state(sdev, SDEV_DEL);
953 if (sdev->host->hostt->slave_destroy)
954 sdev->host->hostt->slave_destroy(sdev);
955 transport_destroy_device(dev);
960 * scsi_remove_device - unregister a device from the scsi bus
961 * @sdev: scsi_device to unregister
963 void scsi_remove_device(struct scsi_device *sdev)
965 struct Scsi_Host *shost = sdev->host;
967 mutex_lock(&shost->scan_mutex);
968 __scsi_remove_device(sdev);
969 mutex_unlock(&shost->scan_mutex);
971 EXPORT_SYMBOL(scsi_remove_device);
973 static void __scsi_remove_target(struct scsi_target *starget)
975 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
977 struct scsi_device *sdev;
979 spin_lock_irqsave(shost->host_lock, flags);
982 list_for_each_entry(sdev, &shost->__devices, siblings) {
983 if (sdev->channel != starget->channel ||
984 sdev->id != starget->id ||
985 sdev->sdev_state == SDEV_DEL)
987 spin_unlock_irqrestore(shost->host_lock, flags);
988 scsi_remove_device(sdev);
989 spin_lock_irqsave(shost->host_lock, flags);
992 spin_unlock_irqrestore(shost->host_lock, flags);
993 scsi_target_reap(starget);
996 static int __remove_child (struct device * dev, void * data)
998 if (scsi_is_target_device(dev))
999 __scsi_remove_target(to_scsi_target(dev));
1004 * scsi_remove_target - try to remove a target and all its devices
1005 * @dev: generic starget or parent of generic stargets to be removed
1007 * Note: This is slightly racy. It is possible that if the user
1008 * requests the addition of another device then the target won't be
1011 void scsi_remove_target(struct device *dev)
1013 struct device *rdev;
1015 if (scsi_is_target_device(dev)) {
1016 __scsi_remove_target(to_scsi_target(dev));
1020 rdev = get_device(dev);
1021 device_for_each_child(dev, NULL, __remove_child);
1024 EXPORT_SYMBOL(scsi_remove_target);
1026 int scsi_register_driver(struct device_driver *drv)
1028 drv->bus = &scsi_bus_type;
1030 return driver_register(drv);
1032 EXPORT_SYMBOL(scsi_register_driver);
1034 int scsi_register_interface(struct class_interface *intf)
1036 intf->class = &sdev_class;
1038 return class_interface_register(intf);
1040 EXPORT_SYMBOL(scsi_register_interface);
1043 * scsi_sysfs_add_host - add scsi host to subsystem
1044 * @shost: scsi host struct to add to subsystem
1045 * @dev: parent struct device pointer
1047 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1051 /* add host specific attributes */
1052 if (shost->hostt->shost_attrs) {
1053 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1054 error = device_create_file(&shost->shost_dev,
1055 shost->hostt->shost_attrs[i]);
1061 transport_register_device(&shost->shost_gendev);
1062 transport_configure_device(&shost->shost_gendev);
1066 static struct device_type scsi_dev_type = {
1067 .name = "scsi_device",
1068 .release = scsi_device_dev_release,
1069 .groups = scsi_sdev_attr_groups,
1072 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1074 unsigned long flags;
1075 struct Scsi_Host *shost = sdev->host;
1076 struct scsi_target *starget = sdev->sdev_target;
1078 device_initialize(&sdev->sdev_gendev);
1079 sdev->sdev_gendev.bus = &scsi_bus_type;
1080 sdev->sdev_gendev.type = &scsi_dev_type;
1081 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1082 sdev->host->host_no, sdev->channel, sdev->id,
1085 device_initialize(&sdev->sdev_dev);
1086 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1087 sdev->sdev_dev.class = &sdev_class;
1088 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
1089 "%d:%d:%d:%d", sdev->host->host_no,
1090 sdev->channel, sdev->id, sdev->lun);
1091 sdev->scsi_level = starget->scsi_level;
1092 transport_setup_device(&sdev->sdev_gendev);
1093 spin_lock_irqsave(shost->host_lock, flags);
1094 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1095 list_add_tail(&sdev->siblings, &shost->__devices);
1096 spin_unlock_irqrestore(shost->host_lock, flags);
1099 int scsi_is_sdev_device(const struct device *dev)
1101 return dev->type == &scsi_dev_type;
1103 EXPORT_SYMBOL(scsi_is_sdev_device);
1105 /* A blank transport template that is used in drivers that don't
1106 * yet implement Transport Attributes */
1107 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };