9 #include <linux/module.h>
10 #include <linux/slab.h>
13 #include <linux/device.h>
16 #include <scsi/scsi.h>
48 for (i = 0; i <
ARRAY_SIZE(sdev_states); i++) {
49 if (sdev_states[i].
value == state) {
50 name = sdev_states[
i].name;
74 for (i = 0; i <
ARRAY_SIZE(shost_states); i++) {
75 if (shost_states[i].
value == state) {
76 name = shost_states[
i].name;
83 static int check_set(
unsigned int *
val,
char *
src)
87 if (
strncmp(src,
"-", 20) == 0) {
102 char s1[15],
s2[15],
s3[15], junk;
106 res =
sscanf(str,
"%10s %10s %10s %c", s1, s2, s3, &junk);
109 if (check_set(&channel, s1))
111 if (check_set(&
id, s2))
113 if (check_set(&lun, s3))
116 res = shost->
transportt->user_scan(shost, channel,
id, lun);
126 #define shost_show_function(name, field, format_string) \
128 show_##name (struct device *dev, struct device_attribute *attr, \
131 struct Scsi_Host *shost = class_to_shost(dev); \
132 return snprintf (buf, 20, format_string, shost->field); \
139 #define shost_rd_attr2(name, field, format_string) \
140 shost_show_function(name, field, format_string) \
141 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
143 #define shost_rd_attr(field, format_string) \
144 shost_rd_attr2(field, field, format_string)
157 res = scsi_scan(shost, buf);
172 for (i = 0; i <
ARRAY_SIZE(shost_states); i++) {
174 if (
strncmp(shost_states[i].
name, buf, len) == 0 &&
176 state = shost_states[
i].value;
197 return snprintf(buf, 20,
"%s\n", name);
205 show_shost_mode(
unsigned int mode,
char *buf)
210 len =
sprintf(buf,
"%s",
"Initiator");
213 len +=
sprintf(buf + len,
"%s%s", len ?
", " :
"",
"Target");
215 len +=
sprintf(buf + len,
"\n");
225 unsigned int supported_mode = shost->
hostt->supported_mode;
231 return show_shost_mode(supported_mode, buf);
237 show_shost_active_mode(
struct device *dev,
243 return snprintf(buf, 20,
"unknown\n");
250 static int check_reset_type(
char *str)
252 if (
strncmp(str,
"adapter", 10) == 0)
254 else if (
strncmp(str,
"firmware", 10) == 0)
262 const char *buf,
size_t count)
271 type = check_reset_type(str);
274 goto exit_store_host_reset;
279 exit_store_host_reset:
298 static struct attribute *scsi_sysfs_shost_attrs[] = {
299 &dev_attr_unique_id.attr,
300 &dev_attr_host_busy.attr,
301 &dev_attr_cmd_per_lun.attr,
302 &dev_attr_can_queue.attr,
303 &dev_attr_sg_tablesize.attr,
304 &dev_attr_sg_prot_tablesize.attr,
305 &dev_attr_unchecked_isa_dma.attr,
306 &dev_attr_proc_name.attr,
308 &dev_attr_hstate.
attr,
309 &dev_attr_supported_mode.attr,
310 &dev_attr_active_mode.attr,
311 &dev_attr_prot_capabilities.attr,
312 &dev_attr_prot_guard_type.attr,
313 &dev_attr_host_reset.attr,
318 .attrs = scsi_sysfs_shost_attrs,
326 static void scsi_device_cls_release(
struct device *class_dev)
334 static void scsi_device_dev_release_usercontext(
struct work_struct *
work)
344 parent = sdev->sdev_gendev.parent;
352 spin_unlock_irqrestore(sdev->
host->host_lock, flags);
377 static void scsi_device_dev_release(
struct device *dev)
384 static struct class sdev_class = {
385 .name =
"scsi_device",
386 .dev_release = scsi_device_cls_release,
394 if (dev->
type != &scsi_dev_type)
407 if (dev->
type != &scsi_dev_type)
418 .match = scsi_bus_match,
419 .uevent = scsi_bus_uevent,
450 #define sdev_show_function(field, format_string) \
452 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
455 struct scsi_device *sdev; \
456 sdev = to_scsi_device(dev); \
457 return snprintf (buf, 20, format_string, sdev->field); \
464 #define sdev_rd_attr(field, format_string) \
465 sdev_show_function(field, format_string) \
466 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
473 #define sdev_rw_attr(field, format_string) \
474 sdev_show_function(field, format_string) \
477 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
478 const char *buf, size_t count) \
480 struct scsi_device *sdev; \
481 sdev = to_scsi_device(dev); \
482 sscanf (buf, format_string, &sdev->field); \
485 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
494 #define sdev_rw_attr_bit(field) \
495 sdev_show_function(field, "%d\n") \
498 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
499 const char *buf, size_t count) \
502 struct scsi_device *sdev; \
503 ret = scsi_sdev_check_buf_bit(buf); \
505 sdev = to_scsi_device(dev); \
511 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
517 static int scsi_sdev_check_buf_bit(
const char *buf)
519 if ((buf[1] ==
'\0') || ((buf[1] ==
'\n') && (buf[2] ==
'\0'))) {
522 else if (buf[0] ==
'0')
554 const char *buf,
size_t count)
559 sscanf (buf,
"%d\n", &timeout);
567 const char *buf,
size_t count)
574 static void sdev_store_delete_callback(
struct device *dev)
581 const char *buf,
size_t count)
597 const char *buf,
size_t count)
603 for (i = 0; i <
ARRAY_SIZE(sdev_states); i++) {
604 const int len =
strlen(sdev_states[i].name);
605 if (
strncmp(sdev_states[i].name, buf, len) == 0 &&
607 state = sdev_states[
i].value;
628 return snprintf(buf, 20,
"%s\n", name);
638 const char *name =
"none";
645 return snprintf(buf, 20,
"%s\n", name);
658 #define show_sdev_iostat(field) \
660 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
663 struct scsi_device *sdev = to_scsi_device(dev); \
664 unsigned long long count = atomic_read(&sdev->field); \
665 return snprintf(buf, 20, "0x%llx\n", count); \
667 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
682 #define DECLARE_EVT_SHOW(name, Cap_name) \
684 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
687 struct scsi_device *sdev = to_scsi_device(dev); \
688 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
689 return snprintf(buf, 20, "%d\n", val); \
692 #define DECLARE_EVT_STORE(name, Cap_name) \
694 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
695 const char *buf, size_t count) \
697 struct scsi_device *sdev = to_scsi_device(dev); \
698 int val = simple_strtoul(buf, NULL, 0); \
700 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
702 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
708 #define DECLARE_EVT(name, Cap_name) \
709 DECLARE_EVT_SHOW(name, Cap_name) \
710 DECLARE_EVT_STORE(name, Cap_name) \
711 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
712 sdev_store_evt_##name);
713 #define REF_EVT(name) &dev_attr_evt_##name.attr
719 &dev_attr_device_blocked.attr,
721 &dev_attr_scsi_level.attr,
722 &dev_attr_vendor.attr,
723 &dev_attr_model.attr,
725 &dev_attr_rescan.attr,
726 &dev_attr_delete.attr,
727 &dev_attr_state.attr,
728 &dev_attr_timeout.attr,
729 &dev_attr_iocounterbits.attr,
730 &dev_attr_iorequest_cnt.attr,
731 &dev_attr_iodone_cnt.attr,
732 &dev_attr_ioerr_cnt.attr,
733 &dev_attr_modalias.attr,
739 .attrs = scsi_sdev_attrs,
743 &scsi_sdev_attr_group,
749 const char *buf,
size_t count)
775 sdev_store_queue_depth_rw);
778 sdev_show_queue_ramp_up_period(
struct device *dev,
789 sdev_store_queue_ramp_up_period(
struct device *dev,
791 const char *buf,
size_t count)
805 sdev_show_queue_ramp_up_period,
806 sdev_store_queue_ramp_up_period);
810 const char *buf,
size_t count)
815 int prev_tag_type = scsi_get_tag_type(sdev);
820 if (
strncmp(buf,
"ordered", 7) == 0)
822 else if (
strncmp(buf,
"simple", 6) == 0)
824 else if (
strncmp(buf,
"none", 4) != 0)
827 if (tag_type == prev_tag_type)
837 static int scsi_target_add(
struct scsi_target *starget)
846 dev_err(&starget->
dev,
"target device_add failed, error %d\n", error);
852 pm_runtime_set_active(&starget->
dev);
854 device_enable_async_suspend(&starget->
dev);
861 sdev_store_queue_type_rw);
880 error = scsi_target_add(starget);
886 device_enable_async_suspend(&sdev->sdev_gendev);
887 scsi_autopm_get_target(starget);
888 pm_runtime_set_active(&sdev->sdev_gendev);
891 scsi_autopm_put_target(starget);
897 scsi_autopm_get_device(sdev);
902 "failed to add device: %d\n", error);
905 device_enable_async_suspend(&sdev->
sdev_dev);
909 "failed to add class device: %d\n", error);
917 if (sdev->
host->hostt->change_queue_depth) {
919 &sdev_attr_queue_depth_rw);
921 &sdev_attr_queue_ramp_up_period);
928 if (sdev->
host->hostt->change_queue_type)
941 "Failed to register bsg queue, errno=%d\n", error);
944 if (sdev->
host->hostt->sdev_attrs) {
945 for (i = 0; sdev->
host->hostt->sdev_attrs[
i]; i++) {
947 sdev->
host->hostt->sdev_attrs[i]);
958 struct device *dev = &sdev->sdev_gendev;
980 if (sdev->
host->hostt->slave_destroy)
981 sdev->
host->hostt->slave_destroy(sdev);
1001 static void __scsi_remove_target(
struct scsi_target *starget)
1003 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
1004 unsigned long flags;
1011 sdev->
id != starget->
id ||
1014 spin_unlock_irqrestore(shost->
host_lock, flags);
1020 spin_unlock_irqrestore(shost->
host_lock, flags);
1035 unsigned long flags;
1044 if (starget->
dev.parent == dev || &starget->
dev == dev) {
1047 spin_unlock_irqrestore(shost->
host_lock, flags);
1051 __scsi_remove_target(starget);
1055 spin_unlock_irqrestore(shost->
host_lock, flags);
1072 intf->
class = &sdev_class;
1087 if (shost->
hostt->shost_attrs) {
1088 for (i = 0; shost->
hostt->shost_attrs[
i]; i++) {
1090 shost->
hostt->shost_attrs[i]);
1096 transport_register_device(&shost->shost_gendev);
1102 .name =
"scsi_device",
1103 .release = scsi_device_dev_release,
1104 .groups = scsi_sdev_attr_groups,
1109 unsigned long flags;
1115 sdev->sdev_gendev.
type = &scsi_dev_type;
1121 sdev->
sdev_dev.class = &sdev_class;
1129 spin_unlock_irqrestore(shost->
host_lock, flags);
1134 return dev->
type == &scsi_dev_type;