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"
25 enum scsi_device_state value
;
28 { SDEV_CREATED
, "created" },
29 { SDEV_RUNNING
, "running" },
30 { SDEV_CANCEL
, "cancel" },
31 { SDEV_DEL
, "deleted" },
32 { SDEV_QUIESCE
, "quiesce" },
33 { SDEV_OFFLINE
, "offline" },
34 { SDEV_BLOCK
, "blocked" },
37 const char *scsi_device_state_name(enum scsi_device_state state
)
42 for (i
= 0; i
< ARRAY_SIZE(sdev_states
); i
++) {
43 if (sdev_states
[i
].value
== state
) {
44 name
= sdev_states
[i
].name
;
52 enum scsi_host_state value
;
55 { SHOST_CREATED
, "created" },
56 { SHOST_RUNNING
, "running" },
57 { SHOST_CANCEL
, "cancel" },
58 { SHOST_DEL
, "deleted" },
59 { SHOST_RECOVERY
, "recovery" },
60 { SHOST_CANCEL_RECOVERY
, "cancel/recovery" },
61 { SHOST_DEL_RECOVERY
, "deleted/recovery", },
63 const char *scsi_host_state_name(enum scsi_host_state state
)
68 for (i
= 0; i
< ARRAY_SIZE(shost_states
); i
++) {
69 if (shost_states
[i
].value
== state
) {
70 name
= shost_states
[i
].name
;
77 static int check_set(unsigned int *val
, char *src
)
81 if (strncmp(src
, "-", 20) == 0) {
82 *val
= SCAN_WILD_CARD
;
85 * Doesn't check for int overflow
87 *val
= simple_strtoul(src
, &last
, 0);
94 static int scsi_scan(struct Scsi_Host
*shost
, const char *str
)
96 char s1
[15], s2
[15], s3
[15], junk
;
97 unsigned int channel
, id
, lun
;
100 res
= sscanf(str
, "%10s %10s %10s %c", s1
, s2
, s3
, &junk
);
103 if (check_set(&channel
, s1
))
105 if (check_set(&id
, s2
))
107 if (check_set(&lun
, s3
))
109 if (shost
->transportt
->user_scan
)
110 res
= shost
->transportt
->user_scan(shost
, channel
, id
, lun
);
112 res
= scsi_scan_host_selected(shost
, channel
, id
, lun
, 1);
117 * shost_show_function: macro to create an attr function that can be used to
118 * show a non-bit field.
120 #define shost_show_function(name, field, format_string) \
122 show_##name (struct device *dev, struct device_attribute *attr, \
125 struct Scsi_Host *shost = class_to_shost(dev); \
126 return snprintf (buf, 20, format_string, shost->field); \
130 * shost_rd_attr: macro to create a function and attribute variable for a
133 #define shost_rd_attr2(name, field, format_string) \
134 shost_show_function(name, field, format_string) \
135 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
137 #define shost_rd_attr(field, format_string) \
138 shost_rd_attr2(field, field, format_string)
141 * Create the actual show/store functions and data structures.
145 store_scan(struct device
*dev
, struct device_attribute
*attr
,
146 const char *buf
, size_t count
)
148 struct Scsi_Host
*shost
= class_to_shost(dev
);
151 res
= scsi_scan(shost
, buf
);
156 static DEVICE_ATTR(scan
, S_IWUSR
, NULL
, store_scan
);
159 store_shost_state(struct device
*dev
, struct device_attribute
*attr
,
160 const char *buf
, size_t count
)
163 struct Scsi_Host
*shost
= class_to_shost(dev
);
164 enum scsi_host_state state
= 0;
166 for (i
= 0; i
< ARRAY_SIZE(shost_states
); i
++) {
167 const int len
= strlen(shost_states
[i
].name
);
168 if (strncmp(shost_states
[i
].name
, buf
, len
) == 0 &&
170 state
= shost_states
[i
].value
;
177 if (scsi_host_set_state(shost
, state
))
183 show_shost_state(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
185 struct Scsi_Host
*shost
= class_to_shost(dev
);
186 const char *name
= scsi_host_state_name(shost
->shost_state
);
191 return snprintf(buf
, 20, "%s\n", name
);
194 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
195 struct device_attribute dev_attr_hstate
=
196 __ATTR(state
, S_IRUGO
| S_IWUSR
, show_shost_state
, store_shost_state
);
199 show_shost_mode(unsigned int mode
, char *buf
)
203 if (mode
& MODE_INITIATOR
)
204 len
= sprintf(buf
, "%s", "Initiator");
206 if (mode
& MODE_TARGET
)
207 len
+= sprintf(buf
+ len
, "%s%s", len
? ", " : "", "Target");
209 len
+= sprintf(buf
+ len
, "\n");
215 show_shost_supported_mode(struct device
*dev
, struct device_attribute
*attr
,
218 struct Scsi_Host
*shost
= class_to_shost(dev
);
219 unsigned int supported_mode
= shost
->hostt
->supported_mode
;
221 if (supported_mode
== MODE_UNKNOWN
)
222 /* by default this should be initiator */
223 supported_mode
= MODE_INITIATOR
;
225 return show_shost_mode(supported_mode
, buf
);
228 static DEVICE_ATTR(supported_mode
, S_IRUGO
| S_IWUSR
, show_shost_supported_mode
, NULL
);
231 show_shost_active_mode(struct device
*dev
,
232 struct device_attribute
*attr
, char *buf
)
234 struct Scsi_Host
*shost
= class_to_shost(dev
);
236 if (shost
->active_mode
== MODE_UNKNOWN
)
237 return snprintf(buf
, 20, "unknown\n");
239 return show_shost_mode(shost
->active_mode
, buf
);
242 static DEVICE_ATTR(active_mode
, S_IRUGO
| S_IWUSR
, show_shost_active_mode
, NULL
);
244 shost_rd_attr(unique_id
, "%u\n");
245 shost_rd_attr(host_busy
, "%hu\n");
246 shost_rd_attr(cmd_per_lun
, "%hd\n");
247 shost_rd_attr(can_queue
, "%hd\n");
248 shost_rd_attr(sg_tablesize
, "%hu\n");
249 shost_rd_attr(unchecked_isa_dma
, "%d\n");
250 shost_rd_attr2(proc_name
, hostt
->proc_name
, "%s\n");
252 static struct device_attribute
*scsi_sysfs_shost_attrs
[] = {
255 &dev_attr_cmd_per_lun
,
257 &dev_attr_sg_tablesize
,
258 &dev_attr_unchecked_isa_dma
,
262 &dev_attr_supported_mode
,
263 &dev_attr_active_mode
,
267 static void scsi_device_cls_release(struct device
*class_dev
)
269 struct scsi_device
*sdev
;
271 sdev
= class_to_sdev(class_dev
);
272 put_device(&sdev
->sdev_gendev
);
275 static void scsi_device_dev_release_usercontext(struct work_struct
*work
)
277 struct scsi_device
*sdev
;
278 struct device
*parent
;
279 struct scsi_target
*starget
;
280 struct list_head
*this, *tmp
;
283 sdev
= container_of(work
, struct scsi_device
, ew
.work
);
285 parent
= sdev
->sdev_gendev
.parent
;
286 starget
= to_scsi_target(parent
);
288 spin_lock_irqsave(sdev
->host
->host_lock
, flags
);
290 list_del(&sdev
->siblings
);
291 list_del(&sdev
->same_target_siblings
);
292 list_del(&sdev
->starved_entry
);
293 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
295 cancel_work_sync(&sdev
->event_work
);
297 list_for_each_safe(this, tmp
, &sdev
->event_list
) {
298 struct scsi_event
*evt
;
300 evt
= list_entry(this, struct scsi_event
, node
);
301 list_del(&evt
->node
);
305 if (sdev
->request_queue
) {
306 sdev
->request_queue
->queuedata
= NULL
;
307 /* user context needed to free queue */
308 scsi_free_queue(sdev
->request_queue
);
309 /* temporary expedient, try to catch use of queue lock
310 * after free of sdev */
311 sdev
->request_queue
= NULL
;
314 scsi_target_reap(scsi_target(sdev
));
316 kfree(sdev
->inquiry
);
323 static void scsi_device_dev_release(struct device
*dev
)
325 struct scsi_device
*sdp
= to_scsi_device(dev
);
326 execute_in_process_context(scsi_device_dev_release_usercontext
,
330 static struct class sdev_class
= {
331 .name
= "scsi_device",
332 .dev_release
= scsi_device_cls_release
,
335 /* all probing is done in the individual ->probe routines */
336 static int scsi_bus_match(struct device
*dev
, struct device_driver
*gendrv
)
338 struct scsi_device
*sdp
= to_scsi_device(dev
);
339 if (sdp
->no_uld_attach
)
341 return (sdp
->inq_periph_qual
== SCSI_INQ_PQ_CON
)? 1: 0;
344 static int scsi_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
346 struct scsi_device
*sdev
= to_scsi_device(dev
);
348 add_uevent_var(env
, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT
, sdev
->type
);
352 static int scsi_bus_suspend(struct device
* dev
, pm_message_t state
)
354 struct device_driver
*drv
= dev
->driver
;
355 struct scsi_device
*sdev
= to_scsi_device(dev
);
358 err
= scsi_device_quiesce(sdev
);
362 if (drv
&& drv
->suspend
) {
363 err
= drv
->suspend(dev
, state
);
371 static int scsi_bus_resume(struct device
* dev
)
373 struct device_driver
*drv
= dev
->driver
;
374 struct scsi_device
*sdev
= to_scsi_device(dev
);
377 if (drv
&& drv
->resume
)
378 err
= drv
->resume(dev
);
380 scsi_device_resume(sdev
);
385 static int scsi_bus_remove(struct device
*dev
)
387 struct device_driver
*drv
= dev
->driver
;
388 struct scsi_device
*sdev
= to_scsi_device(dev
);
391 /* reset the prep_fn back to the default since the
392 * driver may have altered it and it's being removed */
393 blk_queue_prep_rq(sdev
->request_queue
, scsi_prep_fn
);
395 if (drv
&& drv
->remove
)
396 err
= drv
->remove(dev
);
401 struct bus_type scsi_bus_type
= {
403 .match
= scsi_bus_match
,
404 .uevent
= scsi_bus_uevent
,
405 .suspend
= scsi_bus_suspend
,
406 .resume
= scsi_bus_resume
,
407 .remove
= scsi_bus_remove
,
410 int scsi_sysfs_register(void)
414 error
= bus_register(&scsi_bus_type
);
416 error
= class_register(&sdev_class
);
418 bus_unregister(&scsi_bus_type
);
424 void scsi_sysfs_unregister(void)
426 class_unregister(&sdev_class
);
427 bus_unregister(&scsi_bus_type
);
431 * sdev_show_function: macro to create an attr function that can be used to
432 * show a non-bit field.
434 #define sdev_show_function(field, format_string) \
436 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
439 struct scsi_device *sdev; \
440 sdev = to_scsi_device(dev); \
441 return snprintf (buf, 20, format_string, sdev->field); \
445 * sdev_rd_attr: macro to create a function and attribute variable for a
448 #define sdev_rd_attr(field, format_string) \
449 sdev_show_function(field, format_string) \
450 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
454 * sdev_rd_attr: create a function and attribute variable for a
457 #define sdev_rw_attr(field, format_string) \
458 sdev_show_function(field, format_string) \
461 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
462 const char *buf, size_t count) \
464 struct scsi_device *sdev; \
465 sdev = to_scsi_device(dev); \
466 snscanf (buf, 20, format_string, &sdev->field); \
469 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
471 /* Currently we don't export bit fields, but we might in future,
472 * so leave this code in */
475 * sdev_rd_attr: create a function and attribute variable for a
476 * read/write bit field.
478 #define sdev_rw_attr_bit(field) \
479 sdev_show_function(field, "%d\n") \
482 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
483 const char *buf, size_t count) \
486 struct scsi_device *sdev; \
487 ret = scsi_sdev_check_buf_bit(buf); \
489 sdev = to_scsi_device(dev); \
495 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
498 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
499 * else return -EINVAL.
501 static int scsi_sdev_check_buf_bit(const char *buf
)
503 if ((buf
[1] == '\0') || ((buf
[1] == '\n') && (buf
[2] == '\0'))) {
506 else if (buf
[0] == '0')
515 * Create the actual show/store functions and data structures.
517 sdev_rd_attr (device_blocked
, "%d\n");
518 sdev_rd_attr (queue_depth
, "%d\n");
519 sdev_rd_attr (type
, "%d\n");
520 sdev_rd_attr (scsi_level
, "%d\n");
521 sdev_rd_attr (vendor
, "%.8s\n");
522 sdev_rd_attr (model
, "%.16s\n");
523 sdev_rd_attr (rev
, "%.4s\n");
526 sdev_show_timeout (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
528 struct scsi_device
*sdev
;
529 sdev
= to_scsi_device(dev
);
530 return snprintf (buf
, 20, "%d\n", sdev
->timeout
/ HZ
);
534 sdev_store_timeout (struct device
*dev
, struct device_attribute
*attr
,
535 const char *buf
, size_t count
)
537 struct scsi_device
*sdev
;
539 sdev
= to_scsi_device(dev
);
540 sscanf (buf
, "%d\n", &timeout
);
541 sdev
->timeout
= timeout
* HZ
;
544 static DEVICE_ATTR(timeout
, S_IRUGO
| S_IWUSR
, sdev_show_timeout
, sdev_store_timeout
);
547 store_rescan_field (struct device
*dev
, struct device_attribute
*attr
,
548 const char *buf
, size_t count
)
550 scsi_rescan_device(dev
);
553 static DEVICE_ATTR(rescan
, S_IWUSR
, NULL
, store_rescan_field
);
555 static void sdev_store_delete_callback(struct device
*dev
)
557 scsi_remove_device(to_scsi_device(dev
));
561 sdev_store_delete(struct device
*dev
, struct device_attribute
*attr
,
562 const char *buf
, size_t count
)
566 /* An attribute cannot be unregistered by one of its own methods,
567 * so we have to use this roundabout approach.
569 rc
= device_schedule_callback(dev
, sdev_store_delete_callback
);
574 static DEVICE_ATTR(delete, S_IWUSR
, NULL
, sdev_store_delete
);
577 store_state_field(struct device
*dev
, struct device_attribute
*attr
,
578 const char *buf
, size_t count
)
581 struct scsi_device
*sdev
= to_scsi_device(dev
);
582 enum scsi_device_state state
= 0;
584 for (i
= 0; i
< ARRAY_SIZE(sdev_states
); i
++) {
585 const int len
= strlen(sdev_states
[i
].name
);
586 if (strncmp(sdev_states
[i
].name
, buf
, len
) == 0 &&
588 state
= sdev_states
[i
].value
;
595 if (scsi_device_set_state(sdev
, state
))
601 show_state_field(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
603 struct scsi_device
*sdev
= to_scsi_device(dev
);
604 const char *name
= scsi_device_state_name(sdev
->sdev_state
);
609 return snprintf(buf
, 20, "%s\n", name
);
612 static DEVICE_ATTR(state
, S_IRUGO
| S_IWUSR
, show_state_field
, store_state_field
);
615 show_queue_type_field(struct device
*dev
, struct device_attribute
*attr
,
618 struct scsi_device
*sdev
= to_scsi_device(dev
);
619 const char *name
= "none";
621 if (sdev
->ordered_tags
)
623 else if (sdev
->simple_tags
)
626 return snprintf(buf
, 20, "%s\n", name
);
629 static DEVICE_ATTR(queue_type
, S_IRUGO
, show_queue_type_field
, NULL
);
632 show_iostat_counterbits(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
634 return snprintf(buf
, 20, "%d\n", (int)sizeof(atomic_t
) * 8);
637 static DEVICE_ATTR(iocounterbits
, S_IRUGO
, show_iostat_counterbits
, NULL
);
639 #define show_sdev_iostat(field) \
641 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
644 struct scsi_device *sdev = to_scsi_device(dev); \
645 unsigned long long count = atomic_read(&sdev->field); \
646 return snprintf(buf, 20, "0x%llx\n", count); \
648 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
650 show_sdev_iostat(iorequest_cnt
);
651 show_sdev_iostat(iodone_cnt
);
652 show_sdev_iostat(ioerr_cnt
);
655 sdev_show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
657 struct scsi_device
*sdev
;
658 sdev
= to_scsi_device(dev
);
659 return snprintf (buf
, 20, SCSI_DEVICE_MODALIAS_FMT
"\n", sdev
->type
);
661 static DEVICE_ATTR(modalias
, S_IRUGO
, sdev_show_modalias
, NULL
);
663 #define DECLARE_EVT_SHOW(name, Cap_name) \
665 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
668 struct scsi_device *sdev = to_scsi_device(dev); \
669 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
670 return snprintf(buf, 20, "%d\n", val); \
673 #define DECLARE_EVT_STORE(name, Cap_name) \
675 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
676 const char *buf, size_t count) \
678 struct scsi_device *sdev = to_scsi_device(dev); \
679 int val = simple_strtoul(buf, NULL, 0); \
681 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
683 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
689 #define DECLARE_EVT(name, Cap_name) \
690 DECLARE_EVT_SHOW(name, Cap_name) \
691 DECLARE_EVT_STORE(name, Cap_name) \
692 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
693 sdev_store_evt_##name);
694 #define REF_EVT(name) &dev_attr_evt_##name.attr
696 DECLARE_EVT(media_change
, MEDIA_CHANGE
)
698 /* Default template for device attributes. May NOT be modified */
699 static struct attribute
*scsi_sdev_attrs
[] = {
700 &dev_attr_device_blocked
.attr
,
702 &dev_attr_scsi_level
.attr
,
703 &dev_attr_vendor
.attr
,
704 &dev_attr_model
.attr
,
706 &dev_attr_rescan
.attr
,
707 &dev_attr_delete
.attr
,
708 &dev_attr_state
.attr
,
709 &dev_attr_timeout
.attr
,
710 &dev_attr_iocounterbits
.attr
,
711 &dev_attr_iorequest_cnt
.attr
,
712 &dev_attr_iodone_cnt
.attr
,
713 &dev_attr_ioerr_cnt
.attr
,
714 &dev_attr_modalias
.attr
,
715 REF_EVT(media_change
),
719 static struct attribute_group scsi_sdev_attr_group
= {
720 .attrs
= scsi_sdev_attrs
,
723 static struct attribute_group
*scsi_sdev_attr_groups
[] = {
724 &scsi_sdev_attr_group
,
729 sdev_store_queue_depth_rw(struct device
*dev
, struct device_attribute
*attr
,
730 const char *buf
, size_t count
)
733 struct scsi_device
*sdev
= to_scsi_device(dev
);
734 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
736 if (!sht
->change_queue_depth
)
739 depth
= simple_strtoul(buf
, NULL
, 0);
744 retval
= sht
->change_queue_depth(sdev
, depth
);
751 static struct device_attribute sdev_attr_queue_depth_rw
=
752 __ATTR(queue_depth
, S_IRUGO
| S_IWUSR
, sdev_show_queue_depth
,
753 sdev_store_queue_depth_rw
);
756 sdev_store_queue_type_rw(struct device
*dev
, struct device_attribute
*attr
,
757 const char *buf
, size_t count
)
759 struct scsi_device
*sdev
= to_scsi_device(dev
);
760 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
761 int tag_type
= 0, retval
;
762 int prev_tag_type
= scsi_get_tag_type(sdev
);
764 if (!sdev
->tagged_supported
|| !sht
->change_queue_type
)
767 if (strncmp(buf
, "ordered", 7) == 0)
768 tag_type
= MSG_ORDERED_TAG
;
769 else if (strncmp(buf
, "simple", 6) == 0)
770 tag_type
= MSG_SIMPLE_TAG
;
771 else if (strncmp(buf
, "none", 4) != 0)
774 if (tag_type
== prev_tag_type
)
777 retval
= sht
->change_queue_type(sdev
, tag_type
);
784 static struct device_attribute sdev_attr_queue_type_rw
=
785 __ATTR(queue_type
, S_IRUGO
| S_IWUSR
, show_queue_type_field
,
786 sdev_store_queue_type_rw
);
789 * scsi_sysfs_add_sdev - add scsi device to sysfs
790 * @sdev: scsi_device to add
793 * 0 on Success / non-zero on Failure
795 int scsi_sysfs_add_sdev(struct scsi_device
*sdev
)
798 struct request_queue
*rq
= sdev
->request_queue
;
800 if ((error
= scsi_device_set_state(sdev
, SDEV_RUNNING
)) != 0)
803 error
= device_add(&sdev
->sdev_gendev
);
805 put_device(sdev
->sdev_gendev
.parent
);
806 printk(KERN_INFO
"error 1\n");
809 error
= device_add(&sdev
->sdev_dev
);
811 printk(KERN_INFO
"error 2\n");
815 /* take a reference for the sdev_dev; this is
816 * released by the sdev_class .release */
817 get_device(&sdev
->sdev_gendev
);
819 /* create queue files, which may be writable, depending on the host */
820 if (sdev
->host
->hostt
->change_queue_depth
)
821 error
= device_create_file(&sdev
->sdev_gendev
, &sdev_attr_queue_depth_rw
);
823 error
= device_create_file(&sdev
->sdev_gendev
, &dev_attr_queue_depth
);
825 __scsi_remove_device(sdev
);
828 if (sdev
->host
->hostt
->change_queue_type
)
829 error
= device_create_file(&sdev
->sdev_gendev
, &sdev_attr_queue_type_rw
);
831 error
= device_create_file(&sdev
->sdev_gendev
, &dev_attr_queue_type
);
833 __scsi_remove_device(sdev
);
837 error
= bsg_register_queue(rq
, &sdev
->sdev_gendev
, NULL
);
840 sdev_printk(KERN_INFO
, sdev
,
841 "Failed to register bsg queue, errno=%d\n", error
);
843 /* we're treating error on bsg register as non-fatal, so pretend
844 * nothing went wrong */
847 /* add additional host specific attributes */
848 if (sdev
->host
->hostt
->sdev_attrs
) {
849 for (i
= 0; sdev
->host
->hostt
->sdev_attrs
[i
]; i
++) {
850 error
= device_create_file(&sdev
->sdev_gendev
,
851 sdev
->host
->hostt
->sdev_attrs
[i
]);
853 __scsi_remove_device(sdev
);
859 transport_add_device(&sdev
->sdev_gendev
);
864 scsi_device_set_state(sdev
, SDEV_CANCEL
);
866 device_del(&sdev
->sdev_gendev
);
867 transport_destroy_device(&sdev
->sdev_gendev
);
868 put_device(&sdev
->sdev_gendev
);
873 void __scsi_remove_device(struct scsi_device
*sdev
)
875 struct device
*dev
= &sdev
->sdev_gendev
;
877 if (scsi_device_set_state(sdev
, SDEV_CANCEL
) != 0)
880 bsg_unregister_queue(sdev
->request_queue
);
881 device_unregister(&sdev
->sdev_dev
);
882 transport_remove_device(dev
);
884 scsi_device_set_state(sdev
, SDEV_DEL
);
885 if (sdev
->host
->hostt
->slave_destroy
)
886 sdev
->host
->hostt
->slave_destroy(sdev
);
887 transport_destroy_device(dev
);
892 * scsi_remove_device - unregister a device from the scsi bus
893 * @sdev: scsi_device to unregister
895 void scsi_remove_device(struct scsi_device
*sdev
)
897 struct Scsi_Host
*shost
= sdev
->host
;
899 mutex_lock(&shost
->scan_mutex
);
900 __scsi_remove_device(sdev
);
901 mutex_unlock(&shost
->scan_mutex
);
903 EXPORT_SYMBOL(scsi_remove_device
);
905 static void __scsi_remove_target(struct scsi_target
*starget
)
907 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
909 struct scsi_device
*sdev
;
911 spin_lock_irqsave(shost
->host_lock
, flags
);
914 list_for_each_entry(sdev
, &shost
->__devices
, siblings
) {
915 if (sdev
->channel
!= starget
->channel
||
916 sdev
->id
!= starget
->id
||
917 sdev
->sdev_state
== SDEV_DEL
)
919 spin_unlock_irqrestore(shost
->host_lock
, flags
);
920 scsi_remove_device(sdev
);
921 spin_lock_irqsave(shost
->host_lock
, flags
);
924 spin_unlock_irqrestore(shost
->host_lock
, flags
);
925 scsi_target_reap(starget
);
928 static int __remove_child (struct device
* dev
, void * data
)
930 if (scsi_is_target_device(dev
))
931 __scsi_remove_target(to_scsi_target(dev
));
936 * scsi_remove_target - try to remove a target and all its devices
937 * @dev: generic starget or parent of generic stargets to be removed
939 * Note: This is slightly racy. It is possible that if the user
940 * requests the addition of another device then the target won't be
943 void scsi_remove_target(struct device
*dev
)
947 if (scsi_is_target_device(dev
)) {
948 __scsi_remove_target(to_scsi_target(dev
));
952 rdev
= get_device(dev
);
953 device_for_each_child(dev
, NULL
, __remove_child
);
956 EXPORT_SYMBOL(scsi_remove_target
);
958 int scsi_register_driver(struct device_driver
*drv
)
960 drv
->bus
= &scsi_bus_type
;
962 return driver_register(drv
);
964 EXPORT_SYMBOL(scsi_register_driver
);
966 int scsi_register_interface(struct class_interface
*intf
)
968 intf
->class = &sdev_class
;
970 return class_interface_register(intf
);
972 EXPORT_SYMBOL(scsi_register_interface
);
975 static struct device_attribute
*class_attr_overridden(
976 struct device_attribute
**attrs
,
977 struct device_attribute
*attr
)
983 for (i
= 0; attrs
[i
]; i
++)
984 if (!strcmp(attrs
[i
]->attr
.name
, attr
->attr
.name
))
989 static int class_attr_add(struct device
*classdev
,
990 struct device_attribute
*attr
)
992 struct device_attribute
*base_attr
;
995 * Spare the caller from having to copy things it's not interested in.
997 base_attr
= class_attr_overridden(scsi_sysfs_shost_attrs
, attr
);
999 /* extend permissions */
1000 attr
->attr
.mode
|= base_attr
->attr
.mode
;
1002 /* override null show/store with default */
1004 attr
->show
= base_attr
->show
;
1006 attr
->store
= base_attr
->store
;
1009 return device_create_file(classdev
, attr
);
1013 * scsi_sysfs_add_host - add scsi host to subsystem
1014 * @shost: scsi host struct to add to subsystem
1015 * @dev: parent struct device pointer
1017 int scsi_sysfs_add_host(struct Scsi_Host
*shost
)
1021 if (shost
->hostt
->shost_attrs
) {
1022 for (i
= 0; shost
->hostt
->shost_attrs
[i
]; i
++) {
1023 error
= class_attr_add(&shost
->shost_dev
,
1024 shost
->hostt
->shost_attrs
[i
]);
1030 for (i
= 0; scsi_sysfs_shost_attrs
[i
]; i
++) {
1031 if (!class_attr_overridden(shost
->hostt
->shost_attrs
,
1032 scsi_sysfs_shost_attrs
[i
])) {
1033 error
= device_create_file(&shost
->shost_dev
,
1034 scsi_sysfs_shost_attrs
[i
]);
1040 transport_register_device(&shost
->shost_gendev
);
1041 transport_configure_device(&shost
->shost_gendev
);
1045 static struct device_type scsi_dev_type
= {
1046 .name
= "scsi_device",
1047 .release
= scsi_device_dev_release
,
1048 .groups
= scsi_sdev_attr_groups
,
1051 void scsi_sysfs_device_initialize(struct scsi_device
*sdev
)
1053 unsigned long flags
;
1054 struct Scsi_Host
*shost
= sdev
->host
;
1055 struct scsi_target
*starget
= sdev
->sdev_target
;
1057 device_initialize(&sdev
->sdev_gendev
);
1058 sdev
->sdev_gendev
.bus
= &scsi_bus_type
;
1059 sdev
->sdev_gendev
.type
= &scsi_dev_type
;
1060 sprintf(sdev
->sdev_gendev
.bus_id
,"%d:%d:%d:%d",
1061 sdev
->host
->host_no
, sdev
->channel
, sdev
->id
,
1064 device_initialize(&sdev
->sdev_dev
);
1065 sdev
->sdev_dev
.parent
= &sdev
->sdev_gendev
;
1066 sdev
->sdev_dev
.class = &sdev_class
;
1067 snprintf(sdev
->sdev_dev
.bus_id
, BUS_ID_SIZE
,
1068 "%d:%d:%d:%d", sdev
->host
->host_no
,
1069 sdev
->channel
, sdev
->id
, sdev
->lun
);
1070 sdev
->scsi_level
= starget
->scsi_level
;
1071 transport_setup_device(&sdev
->sdev_gendev
);
1072 spin_lock_irqsave(shost
->host_lock
, flags
);
1073 list_add_tail(&sdev
->same_target_siblings
, &starget
->devices
);
1074 list_add_tail(&sdev
->siblings
, &shost
->__devices
);
1075 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1078 int scsi_is_sdev_device(const struct device
*dev
)
1080 return dev
->type
== &scsi_dev_type
;
1082 EXPORT_SYMBOL(scsi_is_sdev_device
);
1084 /* A blank transport template that is used in drivers that don't
1085 * yet implement Transport Attributes */
1086 struct scsi_transport_template blank_transport_template
= { { { {NULL
, }, }, }, };