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/slab.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/device.h>
14 #include <linux/pm_runtime.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_tcq.h>
20 #include <scsi/scsi_transport.h>
21 #include <scsi/scsi_driver.h>
23 #include "scsi_priv.h"
24 #include "scsi_logging.h"
26 static struct device_type scsi_dev_type
;
29 enum scsi_device_state value
;
32 { SDEV_CREATED
, "created" },
33 { SDEV_RUNNING
, "running" },
34 { SDEV_CANCEL
, "cancel" },
35 { SDEV_DEL
, "deleted" },
36 { SDEV_QUIESCE
, "quiesce" },
37 { SDEV_OFFLINE
, "offline" },
38 { SDEV_BLOCK
, "blocked" },
39 { SDEV_CREATED_BLOCK
, "created-blocked" },
42 const char *scsi_device_state_name(enum scsi_device_state state
)
47 for (i
= 0; i
< ARRAY_SIZE(sdev_states
); i
++) {
48 if (sdev_states
[i
].value
== state
) {
49 name
= sdev_states
[i
].name
;
57 enum scsi_host_state value
;
60 { SHOST_CREATED
, "created" },
61 { SHOST_RUNNING
, "running" },
62 { SHOST_CANCEL
, "cancel" },
63 { SHOST_DEL
, "deleted" },
64 { SHOST_RECOVERY
, "recovery" },
65 { SHOST_CANCEL_RECOVERY
, "cancel/recovery" },
66 { SHOST_DEL_RECOVERY
, "deleted/recovery", },
68 const char *scsi_host_state_name(enum scsi_host_state state
)
73 for (i
= 0; i
< ARRAY_SIZE(shost_states
); i
++) {
74 if (shost_states
[i
].value
== state
) {
75 name
= shost_states
[i
].name
;
82 static int check_set(unsigned int *val
, char *src
)
86 if (strncmp(src
, "-", 20) == 0) {
87 *val
= SCAN_WILD_CARD
;
90 * Doesn't check for int overflow
92 *val
= simple_strtoul(src
, &last
, 0);
99 static int scsi_scan(struct Scsi_Host
*shost
, const char *str
)
101 char s1
[15], s2
[15], s3
[15], junk
;
102 unsigned int channel
, id
, lun
;
105 res
= sscanf(str
, "%10s %10s %10s %c", s1
, s2
, s3
, &junk
);
108 if (check_set(&channel
, s1
))
110 if (check_set(&id
, s2
))
112 if (check_set(&lun
, s3
))
114 if (shost
->transportt
->user_scan
)
115 res
= shost
->transportt
->user_scan(shost
, channel
, id
, lun
);
117 res
= scsi_scan_host_selected(shost
, channel
, id
, lun
, 1);
122 * shost_show_function: macro to create an attr function that can be used to
123 * show a non-bit field.
125 #define shost_show_function(name, field, format_string) \
127 show_##name (struct device *dev, struct device_attribute *attr, \
130 struct Scsi_Host *shost = class_to_shost(dev); \
131 return snprintf (buf, 20, format_string, shost->field); \
135 * shost_rd_attr: macro to create a function and attribute variable for a
138 #define shost_rd_attr2(name, field, format_string) \
139 shost_show_function(name, field, format_string) \
140 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
142 #define shost_rd_attr(field, format_string) \
143 shost_rd_attr2(field, field, format_string)
146 * Create the actual show/store functions and data structures.
150 store_scan(struct device
*dev
, struct device_attribute
*attr
,
151 const char *buf
, size_t count
)
153 struct Scsi_Host
*shost
= class_to_shost(dev
);
156 res
= scsi_scan(shost
, buf
);
161 static DEVICE_ATTR(scan
, S_IWUSR
, NULL
, store_scan
);
164 store_shost_state(struct device
*dev
, struct device_attribute
*attr
,
165 const char *buf
, size_t count
)
168 struct Scsi_Host
*shost
= class_to_shost(dev
);
169 enum scsi_host_state state
= 0;
171 for (i
= 0; i
< ARRAY_SIZE(shost_states
); i
++) {
172 const int len
= strlen(shost_states
[i
].name
);
173 if (strncmp(shost_states
[i
].name
, buf
, len
) == 0 &&
175 state
= shost_states
[i
].value
;
182 if (scsi_host_set_state(shost
, state
))
188 show_shost_state(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
190 struct Scsi_Host
*shost
= class_to_shost(dev
);
191 const char *name
= scsi_host_state_name(shost
->shost_state
);
196 return snprintf(buf
, 20, "%s\n", name
);
199 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
200 struct device_attribute dev_attr_hstate
=
201 __ATTR(state
, S_IRUGO
| S_IWUSR
, show_shost_state
, store_shost_state
);
204 show_shost_mode(unsigned int mode
, char *buf
)
208 if (mode
& MODE_INITIATOR
)
209 len
= sprintf(buf
, "%s", "Initiator");
211 if (mode
& MODE_TARGET
)
212 len
+= sprintf(buf
+ len
, "%s%s", len
? ", " : "", "Target");
214 len
+= sprintf(buf
+ len
, "\n");
220 show_shost_supported_mode(struct device
*dev
, struct device_attribute
*attr
,
223 struct Scsi_Host
*shost
= class_to_shost(dev
);
224 unsigned int supported_mode
= shost
->hostt
->supported_mode
;
226 if (supported_mode
== MODE_UNKNOWN
)
227 /* by default this should be initiator */
228 supported_mode
= MODE_INITIATOR
;
230 return show_shost_mode(supported_mode
, buf
);
233 static DEVICE_ATTR(supported_mode
, S_IRUGO
| S_IWUSR
, show_shost_supported_mode
, NULL
);
236 show_shost_active_mode(struct device
*dev
,
237 struct device_attribute
*attr
, char *buf
)
239 struct Scsi_Host
*shost
= class_to_shost(dev
);
241 if (shost
->active_mode
== MODE_UNKNOWN
)
242 return snprintf(buf
, 20, "unknown\n");
244 return show_shost_mode(shost
->active_mode
, buf
);
247 static DEVICE_ATTR(active_mode
, S_IRUGO
| S_IWUSR
, show_shost_active_mode
, NULL
);
249 static int check_reset_type(const char *str
)
251 if (sysfs_streq(str
, "adapter"))
252 return SCSI_ADAPTER_RESET
;
253 else if (sysfs_streq(str
, "firmware"))
254 return SCSI_FIRMWARE_RESET
;
260 store_host_reset(struct device
*dev
, struct device_attribute
*attr
,
261 const char *buf
, size_t count
)
263 struct Scsi_Host
*shost
= class_to_shost(dev
);
264 struct scsi_host_template
*sht
= shost
->hostt
;
268 type
= check_reset_type(buf
);
270 goto exit_store_host_reset
;
273 ret
= sht
->host_reset(shost
, type
);
275 exit_store_host_reset
:
281 static DEVICE_ATTR(host_reset
, S_IWUSR
, NULL
, store_host_reset
);
283 shost_rd_attr(unique_id
, "%u\n");
284 shost_rd_attr(host_busy
, "%hu\n");
285 shost_rd_attr(cmd_per_lun
, "%hd\n");
286 shost_rd_attr(can_queue
, "%hd\n");
287 shost_rd_attr(sg_tablesize
, "%hu\n");
288 shost_rd_attr(sg_prot_tablesize
, "%hu\n");
289 shost_rd_attr(unchecked_isa_dma
, "%d\n");
290 shost_rd_attr(prot_capabilities
, "%u\n");
291 shost_rd_attr(prot_guard_type
, "%hd\n");
292 shost_rd_attr2(proc_name
, hostt
->proc_name
, "%s\n");
294 static struct attribute
*scsi_sysfs_shost_attrs
[] = {
295 &dev_attr_unique_id
.attr
,
296 &dev_attr_host_busy
.attr
,
297 &dev_attr_cmd_per_lun
.attr
,
298 &dev_attr_can_queue
.attr
,
299 &dev_attr_sg_tablesize
.attr
,
300 &dev_attr_sg_prot_tablesize
.attr
,
301 &dev_attr_unchecked_isa_dma
.attr
,
302 &dev_attr_proc_name
.attr
,
304 &dev_attr_hstate
.attr
,
305 &dev_attr_supported_mode
.attr
,
306 &dev_attr_active_mode
.attr
,
307 &dev_attr_prot_capabilities
.attr
,
308 &dev_attr_prot_guard_type
.attr
,
309 &dev_attr_host_reset
.attr
,
313 struct attribute_group scsi_shost_attr_group
= {
314 .attrs
= scsi_sysfs_shost_attrs
,
317 const struct attribute_group
*scsi_sysfs_shost_attr_groups
[] = {
318 &scsi_shost_attr_group
,
322 static void scsi_device_cls_release(struct device
*class_dev
)
324 struct scsi_device
*sdev
;
326 sdev
= class_to_sdev(class_dev
);
327 put_device(&sdev
->sdev_gendev
);
330 static void scsi_device_dev_release_usercontext(struct work_struct
*work
)
332 struct scsi_device
*sdev
;
333 struct device
*parent
;
334 struct scsi_target
*starget
;
335 struct list_head
*this, *tmp
;
338 sdev
= container_of(work
, struct scsi_device
, ew
.work
);
340 parent
= sdev
->sdev_gendev
.parent
;
341 starget
= to_scsi_target(parent
);
343 spin_lock_irqsave(sdev
->host
->host_lock
, flags
);
345 list_del(&sdev
->siblings
);
346 list_del(&sdev
->same_target_siblings
);
347 list_del(&sdev
->starved_entry
);
348 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
350 cancel_work_sync(&sdev
->event_work
);
352 list_for_each_safe(this, tmp
, &sdev
->event_list
) {
353 struct scsi_event
*evt
;
355 evt
= list_entry(this, struct scsi_event
, node
);
356 list_del(&evt
->node
);
360 blk_put_queue(sdev
->request_queue
);
361 /* NULL queue means the device can't be used */
362 sdev
->request_queue
= NULL
;
364 scsi_target_reap(scsi_target(sdev
));
366 kfree(sdev
->inquiry
);
373 static void scsi_device_dev_release(struct device
*dev
)
375 struct scsi_device
*sdp
= to_scsi_device(dev
);
376 execute_in_process_context(scsi_device_dev_release_usercontext
,
380 static struct class sdev_class
= {
381 .name
= "scsi_device",
382 .dev_release
= scsi_device_cls_release
,
385 /* all probing is done in the individual ->probe routines */
386 static int scsi_bus_match(struct device
*dev
, struct device_driver
*gendrv
)
388 struct scsi_device
*sdp
;
390 if (dev
->type
!= &scsi_dev_type
)
393 sdp
= to_scsi_device(dev
);
394 if (sdp
->no_uld_attach
)
396 return (sdp
->inq_periph_qual
== SCSI_INQ_PQ_CON
)? 1: 0;
399 static int scsi_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
401 struct scsi_device
*sdev
;
403 if (dev
->type
!= &scsi_dev_type
)
406 sdev
= to_scsi_device(dev
);
408 add_uevent_var(env
, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT
, sdev
->type
);
412 struct bus_type scsi_bus_type
= {
414 .match
= scsi_bus_match
,
415 .uevent
= scsi_bus_uevent
,
417 .pm
= &scsi_bus_pm_ops
,
420 EXPORT_SYMBOL_GPL(scsi_bus_type
);
422 int scsi_sysfs_register(void)
426 error
= bus_register(&scsi_bus_type
);
428 error
= class_register(&sdev_class
);
430 bus_unregister(&scsi_bus_type
);
436 void scsi_sysfs_unregister(void)
438 class_unregister(&sdev_class
);
439 bus_unregister(&scsi_bus_type
);
443 * sdev_show_function: macro to create an attr function that can be used to
444 * show a non-bit field.
446 #define sdev_show_function(field, format_string) \
448 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
451 struct scsi_device *sdev; \
452 sdev = to_scsi_device(dev); \
453 return snprintf (buf, 20, format_string, sdev->field); \
457 * sdev_rd_attr: macro to create a function and attribute variable for a
460 #define sdev_rd_attr(field, format_string) \
461 sdev_show_function(field, format_string) \
462 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
466 * sdev_rw_attr: create a function and attribute variable for a
469 #define sdev_rw_attr(field, format_string) \
470 sdev_show_function(field, format_string) \
473 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
474 const char *buf, size_t count) \
476 struct scsi_device *sdev; \
477 sdev = to_scsi_device(dev); \
478 sscanf (buf, format_string, &sdev->field); \
481 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
483 /* Currently we don't export bit fields, but we might in future,
484 * so leave this code in */
487 * sdev_rd_attr: create a function and attribute variable for a
488 * read/write bit field.
490 #define sdev_rw_attr_bit(field) \
491 sdev_show_function(field, "%d\n") \
494 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
495 const char *buf, size_t count) \
498 struct scsi_device *sdev; \
499 ret = scsi_sdev_check_buf_bit(buf); \
501 sdev = to_scsi_device(dev); \
507 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
510 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
511 * else return -EINVAL.
513 static int scsi_sdev_check_buf_bit(const char *buf
)
515 if ((buf
[1] == '\0') || ((buf
[1] == '\n') && (buf
[2] == '\0'))) {
518 else if (buf
[0] == '0')
527 * Create the actual show/store functions and data structures.
529 sdev_rd_attr (device_blocked
, "%d\n");
530 sdev_rd_attr (queue_depth
, "%d\n");
531 sdev_rd_attr (type
, "%d\n");
532 sdev_rd_attr (scsi_level
, "%d\n");
533 sdev_rd_attr (vendor
, "%.8s\n");
534 sdev_rd_attr (model
, "%.16s\n");
535 sdev_rd_attr (rev
, "%.4s\n");
538 * TODO: can we make these symlinks to the block layer ones?
541 sdev_show_timeout (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
543 struct scsi_device
*sdev
;
544 sdev
= to_scsi_device(dev
);
545 return snprintf(buf
, 20, "%d\n", sdev
->request_queue
->rq_timeout
/ HZ
);
549 sdev_store_timeout (struct device
*dev
, struct device_attribute
*attr
,
550 const char *buf
, size_t count
)
552 struct scsi_device
*sdev
;
554 sdev
= to_scsi_device(dev
);
555 sscanf (buf
, "%d\n", &timeout
);
556 blk_queue_rq_timeout(sdev
->request_queue
, timeout
* HZ
);
559 static DEVICE_ATTR(timeout
, S_IRUGO
| S_IWUSR
, sdev_show_timeout
, sdev_store_timeout
);
562 store_rescan_field (struct device
*dev
, struct device_attribute
*attr
,
563 const char *buf
, size_t count
)
565 scsi_rescan_device(dev
);
568 static DEVICE_ATTR(rescan
, S_IWUSR
, NULL
, store_rescan_field
);
570 static void sdev_store_delete_callback(struct device
*dev
)
572 scsi_remove_device(to_scsi_device(dev
));
576 sdev_store_delete(struct device
*dev
, struct device_attribute
*attr
,
577 const char *buf
, size_t count
)
581 /* An attribute cannot be unregistered by one of its own methods,
582 * so we have to use this roundabout approach.
584 rc
= device_schedule_callback(dev
, sdev_store_delete_callback
);
589 static DEVICE_ATTR(delete, S_IWUSR
, NULL
, sdev_store_delete
);
592 store_state_field(struct device
*dev
, struct device_attribute
*attr
,
593 const char *buf
, size_t count
)
596 struct scsi_device
*sdev
= to_scsi_device(dev
);
597 enum scsi_device_state state
= 0;
599 for (i
= 0; i
< ARRAY_SIZE(sdev_states
); i
++) {
600 const int len
= strlen(sdev_states
[i
].name
);
601 if (strncmp(sdev_states
[i
].name
, buf
, len
) == 0 &&
603 state
= sdev_states
[i
].value
;
610 if (scsi_device_set_state(sdev
, state
))
616 show_state_field(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
618 struct scsi_device
*sdev
= to_scsi_device(dev
);
619 const char *name
= scsi_device_state_name(sdev
->sdev_state
);
624 return snprintf(buf
, 20, "%s\n", name
);
627 static DEVICE_ATTR(state
, S_IRUGO
| S_IWUSR
, show_state_field
, store_state_field
);
630 show_queue_type_field(struct device
*dev
, struct device_attribute
*attr
,
633 struct scsi_device
*sdev
= to_scsi_device(dev
);
634 const char *name
= "none";
636 if (sdev
->ordered_tags
)
638 else if (sdev
->simple_tags
)
641 return snprintf(buf
, 20, "%s\n", name
);
644 static DEVICE_ATTR(queue_type
, S_IRUGO
, show_queue_type_field
, NULL
);
647 show_iostat_counterbits(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
649 return snprintf(buf
, 20, "%d\n", (int)sizeof(atomic_t
) * 8);
652 static DEVICE_ATTR(iocounterbits
, S_IRUGO
, show_iostat_counterbits
, NULL
);
654 #define show_sdev_iostat(field) \
656 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
659 struct scsi_device *sdev = to_scsi_device(dev); \
660 unsigned long long count = atomic_read(&sdev->field); \
661 return snprintf(buf, 20, "0x%llx\n", count); \
663 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
665 show_sdev_iostat(iorequest_cnt
);
666 show_sdev_iostat(iodone_cnt
);
667 show_sdev_iostat(ioerr_cnt
);
670 sdev_show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
672 struct scsi_device
*sdev
;
673 sdev
= to_scsi_device(dev
);
674 return snprintf (buf
, 20, SCSI_DEVICE_MODALIAS_FMT
"\n", sdev
->type
);
676 static DEVICE_ATTR(modalias
, S_IRUGO
, sdev_show_modalias
, NULL
);
678 #define DECLARE_EVT_SHOW(name, Cap_name) \
680 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
683 struct scsi_device *sdev = to_scsi_device(dev); \
684 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
685 return snprintf(buf, 20, "%d\n", val); \
688 #define DECLARE_EVT_STORE(name, Cap_name) \
690 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
691 const char *buf, size_t count) \
693 struct scsi_device *sdev = to_scsi_device(dev); \
694 int val = simple_strtoul(buf, NULL, 0); \
696 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
698 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
704 #define DECLARE_EVT(name, Cap_name) \
705 DECLARE_EVT_SHOW(name, Cap_name) \
706 DECLARE_EVT_STORE(name, Cap_name) \
707 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
708 sdev_store_evt_##name);
709 #define REF_EVT(name) &dev_attr_evt_##name.attr
711 DECLARE_EVT(media_change
, MEDIA_CHANGE
)
713 /* Default template for device attributes. May NOT be modified */
714 static struct attribute
*scsi_sdev_attrs
[] = {
715 &dev_attr_device_blocked
.attr
,
717 &dev_attr_scsi_level
.attr
,
718 &dev_attr_vendor
.attr
,
719 &dev_attr_model
.attr
,
721 &dev_attr_rescan
.attr
,
722 &dev_attr_delete
.attr
,
723 &dev_attr_state
.attr
,
724 &dev_attr_timeout
.attr
,
725 &dev_attr_iocounterbits
.attr
,
726 &dev_attr_iorequest_cnt
.attr
,
727 &dev_attr_iodone_cnt
.attr
,
728 &dev_attr_ioerr_cnt
.attr
,
729 &dev_attr_modalias
.attr
,
730 REF_EVT(media_change
),
734 static struct attribute_group scsi_sdev_attr_group
= {
735 .attrs
= scsi_sdev_attrs
,
738 static const struct attribute_group
*scsi_sdev_attr_groups
[] = {
739 &scsi_sdev_attr_group
,
744 sdev_store_queue_depth_rw(struct device
*dev
, struct device_attribute
*attr
,
745 const char *buf
, size_t count
)
748 struct scsi_device
*sdev
= to_scsi_device(dev
);
749 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
751 if (!sht
->change_queue_depth
)
754 depth
= simple_strtoul(buf
, NULL
, 0);
759 retval
= sht
->change_queue_depth(sdev
, depth
,
760 SCSI_QDEPTH_DEFAULT
);
764 sdev
->max_queue_depth
= sdev
->queue_depth
;
769 static struct device_attribute sdev_attr_queue_depth_rw
=
770 __ATTR(queue_depth
, S_IRUGO
| S_IWUSR
, sdev_show_queue_depth
,
771 sdev_store_queue_depth_rw
);
774 sdev_show_queue_ramp_up_period(struct device
*dev
,
775 struct device_attribute
*attr
,
778 struct scsi_device
*sdev
;
779 sdev
= to_scsi_device(dev
);
780 return snprintf(buf
, 20, "%u\n",
781 jiffies_to_msecs(sdev
->queue_ramp_up_period
));
785 sdev_store_queue_ramp_up_period(struct device
*dev
,
786 struct device_attribute
*attr
,
787 const char *buf
, size_t count
)
789 struct scsi_device
*sdev
= to_scsi_device(dev
);
790 unsigned long period
;
792 if (strict_strtoul(buf
, 10, &period
))
795 sdev
->queue_ramp_up_period
= msecs_to_jiffies(period
);
799 static struct device_attribute sdev_attr_queue_ramp_up_period
=
800 __ATTR(queue_ramp_up_period
, S_IRUGO
| S_IWUSR
,
801 sdev_show_queue_ramp_up_period
,
802 sdev_store_queue_ramp_up_period
);
805 sdev_store_queue_type_rw(struct device
*dev
, struct device_attribute
*attr
,
806 const char *buf
, size_t count
)
808 struct scsi_device
*sdev
= to_scsi_device(dev
);
809 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
810 int tag_type
= 0, retval
;
811 int prev_tag_type
= scsi_get_tag_type(sdev
);
813 if (!sdev
->tagged_supported
|| !sht
->change_queue_type
)
816 if (strncmp(buf
, "ordered", 7) == 0)
817 tag_type
= MSG_ORDERED_TAG
;
818 else if (strncmp(buf
, "simple", 6) == 0)
819 tag_type
= MSG_SIMPLE_TAG
;
820 else if (strncmp(buf
, "none", 4) != 0)
823 if (tag_type
== prev_tag_type
)
826 retval
= sht
->change_queue_type(sdev
, tag_type
);
833 static int scsi_target_add(struct scsi_target
*starget
)
837 if (starget
->state
!= STARGET_CREATED
)
840 error
= device_add(&starget
->dev
);
842 dev_err(&starget
->dev
, "target device_add failed, error %d\n", error
);
845 transport_add_device(&starget
->dev
);
846 starget
->state
= STARGET_RUNNING
;
848 pm_runtime_set_active(&starget
->dev
);
849 pm_runtime_enable(&starget
->dev
);
850 device_enable_async_suspend(&starget
->dev
);
855 static struct device_attribute sdev_attr_queue_type_rw
=
856 __ATTR(queue_type
, S_IRUGO
| S_IWUSR
, show_queue_type_field
,
857 sdev_store_queue_type_rw
);
860 * scsi_sysfs_add_sdev - add scsi device to sysfs
861 * @sdev: scsi_device to add
864 * 0 on Success / non-zero on Failure
866 int scsi_sysfs_add_sdev(struct scsi_device
*sdev
)
869 struct request_queue
*rq
= sdev
->request_queue
;
870 struct scsi_target
*starget
= sdev
->sdev_target
;
872 error
= scsi_device_set_state(sdev
, SDEV_RUNNING
);
876 error
= scsi_target_add(starget
);
880 transport_configure_device(&starget
->dev
);
882 device_enable_async_suspend(&sdev
->sdev_gendev
);
883 scsi_autopm_get_target(starget
);
884 pm_runtime_set_active(&sdev
->sdev_gendev
);
885 pm_runtime_forbid(&sdev
->sdev_gendev
);
886 pm_runtime_enable(&sdev
->sdev_gendev
);
887 scsi_autopm_put_target(starget
);
889 /* The following call will keep sdev active indefinitely, until
890 * its driver does a corresponding scsi_autopm_pm_device(). Only
891 * drivers supporting autosuspend will do this.
893 scsi_autopm_get_device(sdev
);
895 error
= device_add(&sdev
->sdev_gendev
);
897 sdev_printk(KERN_INFO
, sdev
,
898 "failed to add device: %d\n", error
);
901 device_enable_async_suspend(&sdev
->sdev_dev
);
902 error
= device_add(&sdev
->sdev_dev
);
904 sdev_printk(KERN_INFO
, sdev
,
905 "failed to add class device: %d\n", error
);
906 device_del(&sdev
->sdev_gendev
);
909 transport_add_device(&sdev
->sdev_gendev
);
910 sdev
->is_visible
= 1;
912 /* create queue files, which may be writable, depending on the host */
913 if (sdev
->host
->hostt
->change_queue_depth
) {
914 error
= device_create_file(&sdev
->sdev_gendev
,
915 &sdev_attr_queue_depth_rw
);
916 error
= device_create_file(&sdev
->sdev_gendev
,
917 &sdev_attr_queue_ramp_up_period
);
920 error
= device_create_file(&sdev
->sdev_gendev
, &dev_attr_queue_depth
);
924 if (sdev
->host
->hostt
->change_queue_type
)
925 error
= device_create_file(&sdev
->sdev_gendev
, &sdev_attr_queue_type_rw
);
927 error
= device_create_file(&sdev
->sdev_gendev
, &dev_attr_queue_type
);
931 error
= bsg_register_queue(rq
, &sdev
->sdev_gendev
, NULL
, NULL
);
934 /* we're treating error on bsg register as non-fatal,
935 * so pretend nothing went wrong */
936 sdev_printk(KERN_INFO
, sdev
,
937 "Failed to register bsg queue, errno=%d\n", error
);
939 /* add additional host specific attributes */
940 if (sdev
->host
->hostt
->sdev_attrs
) {
941 for (i
= 0; sdev
->host
->hostt
->sdev_attrs
[i
]; i
++) {
942 error
= device_create_file(&sdev
->sdev_gendev
,
943 sdev
->host
->hostt
->sdev_attrs
[i
]);
952 void __scsi_remove_device(struct scsi_device
*sdev
)
954 struct device
*dev
= &sdev
->sdev_gendev
;
956 if (sdev
->is_visible
) {
957 if (scsi_device_set_state(sdev
, SDEV_CANCEL
) != 0)
960 bsg_unregister_queue(sdev
->request_queue
);
961 device_unregister(&sdev
->sdev_dev
);
962 transport_remove_device(dev
);
965 put_device(&sdev
->sdev_dev
);
966 scsi_device_set_state(sdev
, SDEV_DEL
);
967 if (sdev
->host
->hostt
->slave_destroy
)
968 sdev
->host
->hostt
->slave_destroy(sdev
);
969 transport_destroy_device(dev
);
971 /* Freeing the queue signals to block that we're done */
972 blk_cleanup_queue(sdev
->request_queue
);
977 * scsi_remove_device - unregister a device from the scsi bus
978 * @sdev: scsi_device to unregister
980 void scsi_remove_device(struct scsi_device
*sdev
)
982 struct Scsi_Host
*shost
= sdev
->host
;
984 mutex_lock(&shost
->scan_mutex
);
985 __scsi_remove_device(sdev
);
986 mutex_unlock(&shost
->scan_mutex
);
988 EXPORT_SYMBOL(scsi_remove_device
);
990 static void __scsi_remove_target(struct scsi_target
*starget
)
992 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
994 struct scsi_device
*sdev
;
996 spin_lock_irqsave(shost
->host_lock
, flags
);
998 list_for_each_entry(sdev
, &shost
->__devices
, siblings
) {
999 if (sdev
->channel
!= starget
->channel
||
1000 sdev
->id
!= starget
->id
||
1001 scsi_device_get(sdev
))
1003 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1004 scsi_remove_device(sdev
);
1005 scsi_device_put(sdev
);
1006 spin_lock_irqsave(shost
->host_lock
, flags
);
1009 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1013 * scsi_remove_target - try to remove a target and all its devices
1014 * @dev: generic starget or parent of generic stargets to be removed
1016 * Note: This is slightly racy. It is possible that if the user
1017 * requests the addition of another device then the target won't be
1020 void scsi_remove_target(struct device
*dev
)
1022 struct Scsi_Host
*shost
= dev_to_shost(dev
->parent
);
1023 struct scsi_target
*starget
, *last
= NULL
;
1024 unsigned long flags
;
1026 /* remove targets being careful to lookup next entry before
1029 spin_lock_irqsave(shost
->host_lock
, flags
);
1030 list_for_each_entry(starget
, &shost
->__targets
, siblings
) {
1031 if (starget
->state
== STARGET_DEL
)
1033 if (starget
->dev
.parent
== dev
|| &starget
->dev
== dev
) {
1034 /* assuming new targets arrive at the end */
1035 starget
->reap_ref
++;
1036 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1038 scsi_target_reap(last
);
1040 __scsi_remove_target(starget
);
1041 spin_lock_irqsave(shost
->host_lock
, flags
);
1044 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1047 scsi_target_reap(last
);
1049 EXPORT_SYMBOL(scsi_remove_target
);
1051 int scsi_register_driver(struct device_driver
*drv
)
1053 drv
->bus
= &scsi_bus_type
;
1055 return driver_register(drv
);
1057 EXPORT_SYMBOL(scsi_register_driver
);
1059 int scsi_register_interface(struct class_interface
*intf
)
1061 intf
->class = &sdev_class
;
1063 return class_interface_register(intf
);
1065 EXPORT_SYMBOL(scsi_register_interface
);
1068 * scsi_sysfs_add_host - add scsi host to subsystem
1069 * @shost: scsi host struct to add to subsystem
1071 int scsi_sysfs_add_host(struct Scsi_Host
*shost
)
1075 /* add host specific attributes */
1076 if (shost
->hostt
->shost_attrs
) {
1077 for (i
= 0; shost
->hostt
->shost_attrs
[i
]; i
++) {
1078 error
= device_create_file(&shost
->shost_dev
,
1079 shost
->hostt
->shost_attrs
[i
]);
1085 transport_register_device(&shost
->shost_gendev
);
1086 transport_configure_device(&shost
->shost_gendev
);
1090 static struct device_type scsi_dev_type
= {
1091 .name
= "scsi_device",
1092 .release
= scsi_device_dev_release
,
1093 .groups
= scsi_sdev_attr_groups
,
1096 void scsi_sysfs_device_initialize(struct scsi_device
*sdev
)
1098 unsigned long flags
;
1099 struct Scsi_Host
*shost
= sdev
->host
;
1100 struct scsi_target
*starget
= sdev
->sdev_target
;
1102 device_initialize(&sdev
->sdev_gendev
);
1103 sdev
->sdev_gendev
.bus
= &scsi_bus_type
;
1104 sdev
->sdev_gendev
.type
= &scsi_dev_type
;
1105 dev_set_name(&sdev
->sdev_gendev
, "%d:%d:%d:%d",
1106 sdev
->host
->host_no
, sdev
->channel
, sdev
->id
, sdev
->lun
);
1108 device_initialize(&sdev
->sdev_dev
);
1109 sdev
->sdev_dev
.parent
= get_device(&sdev
->sdev_gendev
);
1110 sdev
->sdev_dev
.class = &sdev_class
;
1111 dev_set_name(&sdev
->sdev_dev
, "%d:%d:%d:%d",
1112 sdev
->host
->host_no
, sdev
->channel
, sdev
->id
, sdev
->lun
);
1113 sdev
->scsi_level
= starget
->scsi_level
;
1114 transport_setup_device(&sdev
->sdev_gendev
);
1115 spin_lock_irqsave(shost
->host_lock
, flags
);
1116 list_add_tail(&sdev
->same_target_siblings
, &starget
->devices
);
1117 list_add_tail(&sdev
->siblings
, &shost
->__devices
);
1118 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1121 int scsi_is_sdev_device(const struct device
*dev
)
1123 return dev
->type
== &scsi_dev_type
;
1125 EXPORT_SYMBOL(scsi_is_sdev_device
);
1127 /* A blank transport template that is used in drivers that don't
1128 * yet implement Transport Attributes */
1129 struct scsi_transport_template blank_transport_template
= { { { {NULL
, }, }, }, };