| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * scsi_sysfs.c | 
|  | 3 | * | 
|  | 4 | * SCSI sysfs interface routines. | 
|  | 5 | * | 
|  | 6 | * Created to pull SCSI mid layer sysfs routines into one file. | 
|  | 7 | */ | 
|  | 8 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> | 
|  | 10 | #include <linux/init.h> | 
|  | 11 | #include <linux/blkdev.h> | 
|  | 12 | #include <linux/device.h> | 
|  | 13 |  | 
|  | 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> | 
| Adrian Bunk | 44818ef | 2007-07-09 11:59:59 -0700 | [diff] [blame] | 19 | #include <scsi/scsi_driver.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | #include "scsi_priv.h" | 
|  | 22 | #include "scsi_logging.h" | 
|  | 23 |  | 
| Hannes Reinecke | b0ed433 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 24 | static struct device_type scsi_dev_type; | 
|  | 25 |  | 
| Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 26 | static const struct { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | enum scsi_device_state	value; | 
|  | 28 | char			*name; | 
|  | 29 | } sdev_states[] = { | 
|  | 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" }, | 
|  | 37 | }; | 
|  | 38 |  | 
|  | 39 | const char *scsi_device_state_name(enum scsi_device_state state) | 
|  | 40 | { | 
|  | 41 | int i; | 
|  | 42 | char *name = NULL; | 
|  | 43 |  | 
| Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 44 | for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (sdev_states[i].value == state) { | 
|  | 46 | name = sdev_states[i].name; | 
|  | 47 | break; | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 | return name; | 
|  | 51 | } | 
|  | 52 |  | 
| Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 53 | static const struct { | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 54 | enum scsi_host_state	value; | 
|  | 55 | char			*name; | 
|  | 56 | } shost_states[] = { | 
|  | 57 | { SHOST_CREATED, "created" }, | 
|  | 58 | { SHOST_RUNNING, "running" }, | 
|  | 59 | { SHOST_CANCEL, "cancel" }, | 
|  | 60 | { SHOST_DEL, "deleted" }, | 
|  | 61 | { SHOST_RECOVERY, "recovery" }, | 
| James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 62 | { SHOST_CANCEL_RECOVERY, "cancel/recovery" }, | 
|  | 63 | { SHOST_DEL_RECOVERY, "deleted/recovery", }, | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 64 | }; | 
|  | 65 | const char *scsi_host_state_name(enum scsi_host_state state) | 
|  | 66 | { | 
|  | 67 | int i; | 
|  | 68 | char *name = NULL; | 
|  | 69 |  | 
| Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 70 | for (i = 0; i < ARRAY_SIZE(shost_states); i++) { | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 71 | if (shost_states[i].value == state) { | 
|  | 72 | name = shost_states[i].name; | 
|  | 73 | break; | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 | return name; | 
|  | 77 | } | 
|  | 78 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static int check_set(unsigned int *val, char *src) | 
|  | 80 | { | 
|  | 81 | char *last; | 
|  | 82 |  | 
|  | 83 | if (strncmp(src, "-", 20) == 0) { | 
|  | 84 | *val = SCAN_WILD_CARD; | 
|  | 85 | } else { | 
|  | 86 | /* | 
|  | 87 | * Doesn't check for int overflow | 
|  | 88 | */ | 
|  | 89 | *val = simple_strtoul(src, &last, 0); | 
|  | 90 | if (*last != '\0') | 
|  | 91 | return 1; | 
|  | 92 | } | 
|  | 93 | return 0; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | static int scsi_scan(struct Scsi_Host *shost, const char *str) | 
|  | 97 | { | 
|  | 98 | char s1[15], s2[15], s3[15], junk; | 
|  | 99 | unsigned int channel, id, lun; | 
|  | 100 | int res; | 
|  | 101 |  | 
|  | 102 | res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk); | 
|  | 103 | if (res != 3) | 
|  | 104 | return -EINVAL; | 
|  | 105 | if (check_set(&channel, s1)) | 
|  | 106 | return -EINVAL; | 
|  | 107 | if (check_set(&id, s2)) | 
|  | 108 | return -EINVAL; | 
|  | 109 | if (check_set(&lun, s3)) | 
|  | 110 | return -EINVAL; | 
| Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 111 | if (shost->transportt->user_scan) | 
|  | 112 | res = shost->transportt->user_scan(shost, channel, id, lun); | 
|  | 113 | else | 
|  | 114 | res = scsi_scan_host_selected(shost, channel, id, lun, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return res; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | /* | 
|  | 119 | * shost_show_function: macro to create an attr function that can be used to | 
|  | 120 | * show a non-bit field. | 
|  | 121 | */ | 
|  | 122 | #define shost_show_function(name, field, format_string)			\ | 
|  | 123 | static ssize_t								\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 124 | show_##name (struct device *dev, struct device_attribute *attr, 	\ | 
|  | 125 | char *buf)							\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | {									\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 127 | struct Scsi_Host *shost = class_to_shost(dev);			\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return snprintf (buf, 20, format_string, shost->field);		\ | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | /* | 
|  | 132 | * shost_rd_attr: macro to create a function and attribute variable for a | 
|  | 133 | * read only field. | 
|  | 134 | */ | 
|  | 135 | #define shost_rd_attr2(name, field, format_string)			\ | 
|  | 136 | shost_show_function(name, field, format_string)			\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 137 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 |  | 
|  | 139 | #define shost_rd_attr(field, format_string) \ | 
|  | 140 | shost_rd_attr2(field, field, format_string) | 
|  | 141 |  | 
|  | 142 | /* | 
|  | 143 | * Create the actual show/store functions and data structures. | 
|  | 144 | */ | 
|  | 145 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 146 | static ssize_t | 
|  | 147 | store_scan(struct device *dev, struct device_attribute *attr, | 
|  | 148 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 150 | struct Scsi_Host *shost = class_to_shost(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | int res; | 
|  | 152 |  | 
|  | 153 | res = scsi_scan(shost, buf); | 
|  | 154 | if (res == 0) | 
|  | 155 | res = count; | 
|  | 156 | return res; | 
|  | 157 | }; | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 158 | static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 |  | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 160 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 161 | store_shost_state(struct device *dev, struct device_attribute *attr, | 
|  | 162 | const char *buf, size_t count) | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 163 | { | 
|  | 164 | int i; | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 165 | struct Scsi_Host *shost = class_to_shost(dev); | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 166 | enum scsi_host_state state = 0; | 
|  | 167 |  | 
| Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 168 | for (i = 0; i < ARRAY_SIZE(shost_states); i++) { | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 169 | const int len = strlen(shost_states[i].name); | 
|  | 170 | if (strncmp(shost_states[i].name, buf, len) == 0 && | 
|  | 171 | buf[len] == '\n') { | 
|  | 172 | state = shost_states[i].value; | 
|  | 173 | break; | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 | if (!state) | 
|  | 177 | return -EINVAL; | 
|  | 178 |  | 
|  | 179 | if (scsi_host_set_state(shost, state)) | 
|  | 180 | return -EINVAL; | 
|  | 181 | return count; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 185 | show_shost_state(struct device *dev, struct device_attribute *attr, char *buf) | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 186 | { | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 187 | struct Scsi_Host *shost = class_to_shost(dev); | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 188 | const char *name = scsi_host_state_name(shost->shost_state); | 
|  | 189 |  | 
|  | 190 | if (!name) | 
|  | 191 | return -EINVAL; | 
|  | 192 |  | 
|  | 193 | return snprintf(buf, 20, "%s\n", name); | 
|  | 194 | } | 
|  | 195 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 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); | 
| Mike Anderson | d330187 | 2005-06-16 11:12:38 -0700 | [diff] [blame] | 199 |  | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 200 | static ssize_t | 
|  | 201 | show_shost_mode(unsigned int mode, char *buf) | 
|  | 202 | { | 
|  | 203 | ssize_t len = 0; | 
|  | 204 |  | 
|  | 205 | if (mode & MODE_INITIATOR) | 
|  | 206 | len = sprintf(buf, "%s", "Initiator"); | 
|  | 207 |  | 
|  | 208 | if (mode & MODE_TARGET) | 
|  | 209 | len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target"); | 
|  | 210 |  | 
|  | 211 | len += sprintf(buf + len, "\n"); | 
|  | 212 |  | 
|  | 213 | return len; | 
|  | 214 | } | 
|  | 215 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 216 | static ssize_t | 
|  | 217 | show_shost_supported_mode(struct device *dev, struct device_attribute *attr, | 
|  | 218 | char *buf) | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 219 | { | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 220 | struct Scsi_Host *shost = class_to_shost(dev); | 
| James Bottomley | 7a39ac3 | 2007-09-25 22:45:53 -0500 | [diff] [blame] | 221 | unsigned int supported_mode = shost->hostt->supported_mode; | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 222 |  | 
| James Bottomley | 7a39ac3 | 2007-09-25 22:45:53 -0500 | [diff] [blame] | 223 | if (supported_mode == MODE_UNKNOWN) | 
|  | 224 | /* by default this should be initiator */ | 
|  | 225 | supported_mode = MODE_INITIATOR; | 
|  | 226 |  | 
|  | 227 | return show_shost_mode(supported_mode, buf); | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 230 | static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL); | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 231 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 232 | static ssize_t | 
|  | 233 | show_shost_active_mode(struct device *dev, | 
|  | 234 | struct device_attribute *attr, char *buf) | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 235 | { | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 236 | struct Scsi_Host *shost = class_to_shost(dev); | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 237 |  | 
|  | 238 | if (shost->active_mode == MODE_UNKNOWN) | 
|  | 239 | return snprintf(buf, 20, "unknown\n"); | 
|  | 240 | else | 
|  | 241 | return show_shost_mode(shost->active_mode, buf); | 
|  | 242 | } | 
|  | 243 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 244 | static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL); | 
| FUJITA Tomonori | 5dc2b89 | 2007-09-01 02:02:20 +0900 | [diff] [blame] | 245 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); | 
| James Bottomley | ed632da | 2006-10-16 10:06:27 -0500 | [diff] [blame] | 249 | shost_rd_attr(can_queue, "%hd\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); | 
|  | 253 |  | 
| Hannes Reinecke | f7120a4 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 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, | 
|  | 262 | &dev_attr_scan.attr, | 
|  | 263 | &dev_attr_hstate.attr, | 
|  | 264 | &dev_attr_supported_mode.attr, | 
|  | 265 | &dev_attr_active_mode.attr, | 
|  | 266 | NULL | 
|  | 267 | }; | 
|  | 268 |  | 
|  | 269 | struct attribute_group scsi_shost_attr_group = { | 
|  | 270 | .attrs =	scsi_sysfs_shost_attrs, | 
|  | 271 | }; | 
|  | 272 |  | 
|  | 273 | struct attribute_group *scsi_sysfs_shost_attr_groups[] = { | 
|  | 274 | &scsi_shost_attr_group, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | NULL | 
|  | 276 | }; | 
|  | 277 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 278 | static void scsi_device_cls_release(struct device *class_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { | 
|  | 280 | struct scsi_device *sdev; | 
|  | 281 |  | 
|  | 282 | sdev = class_to_sdev(class_dev); | 
|  | 283 | put_device(&sdev->sdev_gendev); | 
|  | 284 | } | 
|  | 285 |  | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 286 | static void scsi_device_dev_release_usercontext(struct work_struct *work) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { | 
|  | 288 | struct scsi_device *sdev; | 
|  | 289 | struct device *parent; | 
|  | 290 | struct scsi_target *starget; | 
| Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 291 | struct list_head *this, *tmp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | unsigned long flags; | 
|  | 293 |  | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 294 | sdev = container_of(work, struct scsi_device, ew.work); | 
|  | 295 |  | 
|  | 296 | parent = sdev->sdev_gendev.parent; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | starget = to_scsi_target(parent); | 
|  | 298 |  | 
|  | 299 | spin_lock_irqsave(sdev->host->host_lock, flags); | 
|  | 300 | starget->reap_ref++; | 
|  | 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); | 
|  | 305 |  | 
| Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 306 | cancel_work_sync(&sdev->event_work); | 
|  | 307 |  | 
|  | 308 | list_for_each_safe(this, tmp, &sdev->event_list) { | 
|  | 309 | struct scsi_event *evt; | 
|  | 310 |  | 
|  | 311 | evt = list_entry(this, struct scsi_event, node); | 
|  | 312 | list_del(&evt->node); | 
|  | 313 | kfree(evt); | 
|  | 314 | } | 
|  | 315 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (sdev->request_queue) { | 
|  | 317 | sdev->request_queue->queuedata = NULL; | 
| James Bottomley | 65110b2 | 2006-02-14 10:48:46 -0600 | [diff] [blame] | 318 | /* user context needed to free queue */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | scsi_free_queue(sdev->request_queue); | 
|  | c2a9331 | 2005-04-12 16:38:09 -0500 | [diff] [blame] | 320 | /* temporary expedient, try to catch use of queue lock | 
|  | 321 | * after free of sdev */ | 
|  | 322 | sdev->request_queue = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
|  | 325 | scsi_target_reap(scsi_target(sdev)); | 
|  | 326 |  | 
|  | 327 | kfree(sdev->inquiry); | 
|  | 328 | kfree(sdev); | 
|  | 329 |  | 
|  | 330 | if (parent) | 
|  | 331 | put_device(parent); | 
|  | 332 | } | 
|  | 333 |  | 
| James Bottomley | 65110b2 | 2006-02-14 10:48:46 -0600 | [diff] [blame] | 334 | static void scsi_device_dev_release(struct device *dev) | 
|  | 335 | { | 
| James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 336 | struct scsi_device *sdp = to_scsi_device(dev); | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 337 | execute_in_process_context(scsi_device_dev_release_usercontext, | 
| James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 338 | &sdp->ew); | 
| James Bottomley | 65110b2 | 2006-02-14 10:48:46 -0600 | [diff] [blame] | 339 | } | 
|  | 340 |  | 
| Adrian Bunk | 52c1da3 | 2005-06-23 22:05:33 -0700 | [diff] [blame] | 341 | static struct class sdev_class = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | .name		= "scsi_device", | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 343 | .dev_release	= scsi_device_cls_release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | }; | 
|  | 345 |  | 
|  | 346 | /* all probing is done in the individual ->probe routines */ | 
|  | 347 | static int scsi_bus_match(struct device *dev, struct device_driver *gendrv) | 
|  | 348 | { | 
| Hannes Reinecke | b0ed433 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 349 | struct scsi_device *sdp; | 
|  | 350 |  | 
|  | 351 | if (dev->type != &scsi_dev_type) | 
|  | 352 | return 0; | 
|  | 353 |  | 
|  | 354 | sdp = to_scsi_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (sdp->no_uld_attach) | 
|  | 356 | return 0; | 
|  | 357 | return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0; | 
|  | 358 | } | 
|  | 359 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 360 | static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) | 
| Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 361 | { | 
| James Bottomley | 1f42ea7 | 2008-05-22 12:34:41 +0100 | [diff] [blame] | 362 | struct scsi_device *sdev; | 
|  | 363 |  | 
|  | 364 | if (dev->type != &scsi_dev_type) | 
|  | 365 | return 0; | 
|  | 366 |  | 
|  | 367 | sdev = to_scsi_device(dev); | 
| Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 368 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 369 | add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type); | 
| Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 370 | return 0; | 
|  | 371 | } | 
|  | 372 |  | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 373 | static int scsi_bus_suspend(struct device * dev, pm_message_t state) | 
|  | 374 | { | 
| Hannes Reinecke | b0ed433 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 375 | struct device_driver *drv; | 
|  | 376 | struct scsi_device *sdev; | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 377 | int err; | 
|  | 378 |  | 
| Hannes Reinecke | b0ed433 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 379 | if (dev->type != &scsi_dev_type) | 
|  | 380 | return 0; | 
|  | 381 |  | 
|  | 382 | drv = dev->driver; | 
|  | 383 | sdev = to_scsi_device(dev); | 
|  | 384 |  | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 385 | err = scsi_device_quiesce(sdev); | 
|  | 386 | if (err) | 
|  | 387 | return err; | 
|  | 388 |  | 
| Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 389 | if (drv && drv->suspend) { | 
|  | 390 | err = drv->suspend(dev, state); | 
|  | 391 | if (err) | 
|  | 392 | return err; | 
|  | 393 | } | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 394 |  | 
| Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 395 | return 0; | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
|  | 398 | static int scsi_bus_resume(struct device * dev) | 
|  | 399 | { | 
| Hannes Reinecke | b0ed433 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 400 | struct device_driver *drv; | 
|  | 401 | struct scsi_device *sdev; | 
| Tejun Heo | 1dfcda0 | 2007-03-21 16:05:16 +0900 | [diff] [blame] | 402 | int err = 0; | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 403 |  | 
| Hannes Reinecke | b0ed433 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 404 | if (dev->type != &scsi_dev_type) | 
|  | 405 | return 0; | 
|  | 406 |  | 
|  | 407 | drv = dev->driver; | 
|  | 408 | sdev = to_scsi_device(dev); | 
|  | 409 |  | 
| Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 410 | if (drv && drv->resume) | 
| Tejun Heo | 1dfcda0 | 2007-03-21 16:05:16 +0900 | [diff] [blame] | 411 | err = drv->resume(dev); | 
| Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 412 |  | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 413 | scsi_device_resume(sdev); | 
| Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 414 |  | 
| Tejun Heo | 1dfcda0 | 2007-03-21 16:05:16 +0900 | [diff] [blame] | 415 | return err; | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| James Bottomley | 751bf4d | 2008-01-02 11:14:30 -0600 | [diff] [blame] | 418 | static int scsi_bus_remove(struct device *dev) | 
|  | 419 | { | 
|  | 420 | struct device_driver *drv = dev->driver; | 
|  | 421 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 422 | int err = 0; | 
|  | 423 |  | 
|  | 424 | /* reset the prep_fn back to the default since the | 
|  | 425 | * driver may have altered it and it's being removed */ | 
|  | 426 | blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn); | 
|  | 427 |  | 
|  | 428 | if (drv && drv->remove) | 
|  | 429 | err = drv->remove(dev); | 
|  | 430 |  | 
|  | 431 | return 0; | 
|  | 432 | } | 
|  | 433 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | struct bus_type scsi_bus_type = { | 
|  | 435 | .name		= "scsi", | 
|  | 436 | .match		= scsi_bus_match, | 
| Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 437 | .uevent		= scsi_bus_uevent, | 
| Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 438 | .suspend	= scsi_bus_suspend, | 
|  | 439 | .resume		= scsi_bus_resume, | 
| James Bottomley | 751bf4d | 2008-01-02 11:14:30 -0600 | [diff] [blame] | 440 | .remove		= scsi_bus_remove, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | }; | 
| Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 442 | EXPORT_SYMBOL_GPL(scsi_bus_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 |  | 
|  | 444 | int scsi_sysfs_register(void) | 
|  | 445 | { | 
|  | 446 | int error; | 
|  | 447 |  | 
|  | 448 | error = bus_register(&scsi_bus_type); | 
|  | 449 | if (!error) { | 
|  | 450 | error = class_register(&sdev_class); | 
|  | 451 | if (error) | 
|  | 452 | bus_unregister(&scsi_bus_type); | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | return error; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | void scsi_sysfs_unregister(void) | 
|  | 459 | { | 
|  | 460 | class_unregister(&sdev_class); | 
|  | 461 | bus_unregister(&scsi_bus_type); | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | /* | 
|  | 465 | * sdev_show_function: macro to create an attr function that can be used to | 
|  | 466 | * show a non-bit field. | 
|  | 467 | */ | 
|  | 468 | #define sdev_show_function(field, format_string)				\ | 
|  | 469 | static ssize_t								\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 470 | sdev_show_##field (struct device *dev, struct device_attribute *attr,	\ | 
|  | 471 | char *buf)						\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | {									\ | 
|  | 473 | struct scsi_device *sdev;					\ | 
|  | 474 | sdev = to_scsi_device(dev);					\ | 
|  | 475 | return snprintf (buf, 20, format_string, sdev->field);		\ | 
|  | 476 | }									\ | 
|  | 477 |  | 
|  | 478 | /* | 
|  | 479 | * sdev_rd_attr: macro to create a function and attribute variable for a | 
|  | 480 | * read only field. | 
|  | 481 | */ | 
|  | 482 | #define sdev_rd_attr(field, format_string)				\ | 
|  | 483 | sdev_show_function(field, format_string)			\ | 
|  | 484 | static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL); | 
|  | 485 |  | 
|  | 486 |  | 
|  | 487 | /* | 
|  | 488 | * sdev_rd_attr: create a function and attribute variable for a | 
|  | 489 | * read/write field. | 
|  | 490 | */ | 
|  | 491 | #define sdev_rw_attr(field, format_string)				\ | 
|  | 492 | sdev_show_function(field, format_string)				\ | 
|  | 493 | \ | 
|  | 494 | static ssize_t								\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 495 | sdev_store_##field (struct device *dev, struct device_attribute *attr,	\ | 
|  | 496 | const char *buf, size_t count)			\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | {									\ | 
|  | 498 | struct scsi_device *sdev;					\ | 
|  | 499 | sdev = to_scsi_device(dev);					\ | 
|  | 500 | snscanf (buf, 20, format_string, &sdev->field);			\ | 
|  | 501 | return count;							\ | 
|  | 502 | }									\ | 
|  | 503 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field); | 
|  | 504 |  | 
|  | 505 | /* Currently we don't export bit fields, but we might in future, | 
|  | 506 | * so leave this code in */ | 
|  | 507 | #if 0 | 
|  | 508 | /* | 
|  | 509 | * sdev_rd_attr: create a function and attribute variable for a | 
|  | 510 | * read/write bit field. | 
|  | 511 | */ | 
|  | 512 | #define sdev_rw_attr_bit(field)						\ | 
|  | 513 | sdev_show_function(field, "%d\n")					\ | 
|  | 514 | \ | 
|  | 515 | static ssize_t								\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 516 | sdev_store_##field (struct device *dev, struct device_attribute *attr,	\ | 
|  | 517 | const char *buf, size_t count)			\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | {									\ | 
|  | 519 | int ret;							\ | 
|  | 520 | struct scsi_device *sdev;					\ | 
|  | 521 | ret = scsi_sdev_check_buf_bit(buf);				\ | 
|  | 522 | if (ret >= 0)	{						\ | 
|  | 523 | sdev = to_scsi_device(dev);				\ | 
|  | 524 | sdev->field = ret;					\ | 
|  | 525 | ret = count;						\ | 
|  | 526 | }								\ | 
|  | 527 | return ret;							\ | 
|  | 528 | }									\ | 
|  | 529 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field); | 
|  | 530 |  | 
|  | 531 | /* | 
|  | 532 | * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1", | 
|  | 533 | * else return -EINVAL. | 
|  | 534 | */ | 
|  | 535 | static int scsi_sdev_check_buf_bit(const char *buf) | 
|  | 536 | { | 
|  | 537 | if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) { | 
|  | 538 | if (buf[0] == '1') | 
|  | 539 | return 1; | 
|  | 540 | else if (buf[0] == '0') | 
|  | 541 | return 0; | 
|  | 542 | else | 
|  | 543 | return -EINVAL; | 
|  | 544 | } else | 
|  | 545 | return -EINVAL; | 
|  | 546 | } | 
|  | 547 | #endif | 
|  | 548 | /* | 
|  | 549 | * Create the actual show/store functions and data structures. | 
|  | 550 | */ | 
|  | 551 | sdev_rd_attr (device_blocked, "%d\n"); | 
|  | 552 | sdev_rd_attr (queue_depth, "%d\n"); | 
|  | 553 | sdev_rd_attr (type, "%d\n"); | 
|  | 554 | sdev_rd_attr (scsi_level, "%d\n"); | 
|  | 555 | sdev_rd_attr (vendor, "%.8s\n"); | 
|  | 556 | sdev_rd_attr (model, "%.16s\n"); | 
|  | 557 | sdev_rd_attr (rev, "%.4s\n"); | 
|  | 558 |  | 
|  | 559 | static ssize_t | 
| Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 560 | sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { | 
|  | 562 | struct scsi_device *sdev; | 
|  | 563 | sdev = to_scsi_device(dev); | 
|  | 564 | return snprintf (buf, 20, "%d\n", sdev->timeout / HZ); | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 568 | sdev_store_timeout (struct device *dev, struct device_attribute *attr, | 
|  | 569 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { | 
|  | 571 | struct scsi_device *sdev; | 
|  | 572 | int timeout; | 
|  | 573 | sdev = to_scsi_device(dev); | 
|  | 574 | sscanf (buf, "%d\n", &timeout); | 
|  | 575 | sdev->timeout = timeout * HZ; | 
|  | 576 | return count; | 
|  | 577 | } | 
|  | 578 | static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout); | 
|  | 579 |  | 
|  | 580 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 581 | store_rescan_field (struct device *dev, struct device_attribute *attr, | 
|  | 582 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { | 
|  | 584 | scsi_rescan_device(dev); | 
|  | 585 | return count; | 
|  | 586 | } | 
|  | 587 | static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field); | 
|  | 588 |  | 
| Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 589 | static void sdev_store_delete_callback(struct device *dev) | 
|  | 590 | { | 
|  | 591 | scsi_remove_device(to_scsi_device(dev)); | 
|  | 592 | } | 
|  | 593 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 594 | static ssize_t | 
|  | 595 | sdev_store_delete(struct device *dev, struct device_attribute *attr, | 
|  | 596 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { | 
| Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 598 | int rc; | 
|  | 599 |  | 
|  | 600 | /* An attribute cannot be unregistered by one of its own methods, | 
|  | 601 | * so we have to use this roundabout approach. | 
|  | 602 | */ | 
|  | 603 | rc = device_schedule_callback(dev, sdev_store_delete_callback); | 
|  | 604 | if (rc) | 
|  | 605 | count = rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return count; | 
|  | 607 | }; | 
|  | 608 | static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete); | 
|  | 609 |  | 
|  | 610 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 611 | store_state_field(struct device *dev, struct device_attribute *attr, | 
|  | 612 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | { | 
|  | 614 | int i; | 
|  | 615 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 616 | enum scsi_device_state state = 0; | 
|  | 617 |  | 
| Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 618 | for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | const int len = strlen(sdev_states[i].name); | 
|  | 620 | if (strncmp(sdev_states[i].name, buf, len) == 0 && | 
|  | 621 | buf[len] == '\n') { | 
|  | 622 | state = sdev_states[i].value; | 
|  | 623 | break; | 
|  | 624 | } | 
|  | 625 | } | 
|  | 626 | if (!state) | 
|  | 627 | return -EINVAL; | 
|  | 628 |  | 
|  | 629 | if (scsi_device_set_state(sdev, state)) | 
|  | 630 | return -EINVAL; | 
|  | 631 | return count; | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | static ssize_t | 
| Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 635 | show_state_field(struct device *dev, struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { | 
|  | 637 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 638 | const char *name = scsi_device_state_name(sdev->sdev_state); | 
|  | 639 |  | 
|  | 640 | if (!name) | 
|  | 641 | return -EINVAL; | 
|  | 642 |  | 
|  | 643 | return snprintf(buf, 20, "%s\n", name); | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field); | 
|  | 647 |  | 
|  | 648 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 649 | show_queue_type_field(struct device *dev, struct device_attribute *attr, | 
|  | 650 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { | 
|  | 652 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 653 | const char *name = "none"; | 
|  | 654 |  | 
|  | 655 | if (sdev->ordered_tags) | 
|  | 656 | name = "ordered"; | 
|  | 657 | else if (sdev->simple_tags) | 
|  | 658 | name = "simple"; | 
|  | 659 |  | 
|  | 660 | return snprintf(buf, 20, "%s\n", name); | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL); | 
|  | 664 |  | 
|  | 665 | static ssize_t | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 666 | show_iostat_counterbits(struct device *dev, struct device_attribute *attr, 				char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { | 
|  | 668 | return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8); | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL); | 
|  | 672 |  | 
|  | 673 | #define show_sdev_iostat(field)						\ | 
|  | 674 | static ssize_t								\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 675 | show_iostat_##field(struct device *dev, struct device_attribute *attr,	\ | 
|  | 676 | char *buf)						\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | {									\ | 
|  | 678 | struct scsi_device *sdev = to_scsi_device(dev);			\ | 
|  | 679 | unsigned long long count = atomic_read(&sdev->field);		\ | 
|  | 680 | return snprintf(buf, 20, "0x%llx\n", count);			\ | 
|  | 681 | }									\ | 
|  | 682 | static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL) | 
|  | 683 |  | 
|  | 684 | show_sdev_iostat(iorequest_cnt); | 
|  | 685 | show_sdev_iostat(iodone_cnt); | 
|  | 686 | show_sdev_iostat(ioerr_cnt); | 
|  | 687 |  | 
| Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 688 | static ssize_t | 
|  | 689 | sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | 
|  | 690 | { | 
|  | 691 | struct scsi_device *sdev; | 
|  | 692 | sdev = to_scsi_device(dev); | 
|  | 693 | return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type); | 
|  | 694 | } | 
|  | 695 | static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 |  | 
| Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 697 | #define DECLARE_EVT_SHOW(name, Cap_name)				\ | 
|  | 698 | static ssize_t								\ | 
|  | 699 | sdev_show_evt_##name(struct device *dev, struct device_attribute *attr,	\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 700 | char *buf)						\ | 
| Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 701 | {									\ | 
|  | 702 | struct scsi_device *sdev = to_scsi_device(dev);			\ | 
|  | 703 | int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\ | 
|  | 704 | return snprintf(buf, 20, "%d\n", val);				\ | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | #define DECLARE_EVT_STORE(name, Cap_name)				\ | 
|  | 708 | static ssize_t								\ | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 709 | sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\ | 
| Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 710 | const char *buf, size_t count)			\ | 
|  | 711 | {									\ | 
|  | 712 | struct scsi_device *sdev = to_scsi_device(dev);			\ | 
|  | 713 | int val = simple_strtoul(buf, NULL, 0);				\ | 
|  | 714 | if (val == 0)							\ | 
|  | 715 | clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\ | 
|  | 716 | else if (val == 1)						\ | 
|  | 717 | set_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\ | 
|  | 718 | else								\ | 
|  | 719 | return -EINVAL;						\ | 
|  | 720 | return count;							\ | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | #define DECLARE_EVT(name, Cap_name)					\ | 
|  | 724 | DECLARE_EVT_SHOW(name, Cap_name)				\ | 
|  | 725 | DECLARE_EVT_STORE(name, Cap_name)				\ | 
|  | 726 | static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name,	\ | 
|  | 727 | sdev_store_evt_##name); | 
|  | 728 | #define REF_EVT(name) &dev_attr_evt_##name.attr | 
|  | 729 |  | 
|  | 730 | DECLARE_EVT(media_change, MEDIA_CHANGE) | 
|  | 731 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | /* Default template for device attributes.  May NOT be modified */ | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 733 | static struct attribute *scsi_sdev_attrs[] = { | 
|  | 734 | &dev_attr_device_blocked.attr, | 
|  | 735 | &dev_attr_type.attr, | 
|  | 736 | &dev_attr_scsi_level.attr, | 
|  | 737 | &dev_attr_vendor.attr, | 
|  | 738 | &dev_attr_model.attr, | 
|  | 739 | &dev_attr_rev.attr, | 
|  | 740 | &dev_attr_rescan.attr, | 
|  | 741 | &dev_attr_delete.attr, | 
|  | 742 | &dev_attr_state.attr, | 
|  | 743 | &dev_attr_timeout.attr, | 
|  | 744 | &dev_attr_iocounterbits.attr, | 
|  | 745 | &dev_attr_iorequest_cnt.attr, | 
|  | 746 | &dev_attr_iodone_cnt.attr, | 
|  | 747 | &dev_attr_ioerr_cnt.attr, | 
|  | 748 | &dev_attr_modalias.attr, | 
| Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 749 | REF_EVT(media_change), | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 750 | NULL | 
|  | 751 | }; | 
|  | 752 |  | 
|  | 753 | static struct attribute_group scsi_sdev_attr_group = { | 
|  | 754 | .attrs =	scsi_sdev_attrs, | 
|  | 755 | }; | 
|  | 756 |  | 
|  | 757 | static struct attribute_group *scsi_sdev_attr_groups[] = { | 
|  | 758 | &scsi_sdev_attr_group, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | NULL | 
|  | 760 | }; | 
|  | 761 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 762 | static ssize_t | 
|  | 763 | sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, | 
|  | 764 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { | 
|  | 766 | int depth, retval; | 
|  | 767 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 768 | struct scsi_host_template *sht = sdev->host->hostt; | 
|  | 769 |  | 
|  | 770 | if (!sht->change_queue_depth) | 
|  | 771 | return -EINVAL; | 
|  | 772 |  | 
|  | 773 | depth = simple_strtoul(buf, NULL, 0); | 
|  | 774 |  | 
|  | 775 | if (depth < 1) | 
|  | 776 | return -EINVAL; | 
|  | 777 |  | 
|  | 778 | retval = sht->change_queue_depth(sdev, depth); | 
|  | 779 | if (retval < 0) | 
|  | 780 | return retval; | 
|  | 781 |  | 
|  | 782 | return count; | 
|  | 783 | } | 
|  | 784 |  | 
|  | 785 | static struct device_attribute sdev_attr_queue_depth_rw = | 
|  | 786 | __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth, | 
|  | 787 | sdev_store_queue_depth_rw); | 
|  | 788 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 789 | static ssize_t | 
|  | 790 | sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, | 
|  | 791 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { | 
|  | 793 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 794 | struct scsi_host_template *sht = sdev->host->hostt; | 
|  | 795 | int tag_type = 0, retval; | 
|  | 796 | int prev_tag_type = scsi_get_tag_type(sdev); | 
|  | 797 |  | 
|  | 798 | if (!sdev->tagged_supported || !sht->change_queue_type) | 
|  | 799 | return -EINVAL; | 
|  | 800 |  | 
|  | 801 | if (strncmp(buf, "ordered", 7) == 0) | 
|  | 802 | tag_type = MSG_ORDERED_TAG; | 
|  | 803 | else if (strncmp(buf, "simple", 6) == 0) | 
|  | 804 | tag_type = MSG_SIMPLE_TAG; | 
|  | 805 | else if (strncmp(buf, "none", 4) != 0) | 
|  | 806 | return -EINVAL; | 
|  | 807 |  | 
|  | 808 | if (tag_type == prev_tag_type) | 
|  | 809 | return count; | 
|  | 810 |  | 
|  | 811 | retval = sht->change_queue_type(sdev, tag_type); | 
|  | 812 | if (retval < 0) | 
|  | 813 | return retval; | 
|  | 814 |  | 
|  | 815 | return count; | 
|  | 816 | } | 
|  | 817 |  | 
| James Bottomley | 643eb2d | 2008-03-22 22:42:27 -0500 | [diff] [blame] | 818 | static int scsi_target_add(struct scsi_target *starget) | 
|  | 819 | { | 
|  | 820 | int error; | 
|  | 821 |  | 
|  | 822 | if (starget->state != STARGET_CREATED) | 
|  | 823 | return 0; | 
|  | 824 |  | 
|  | 825 | error = device_add(&starget->dev); | 
|  | 826 | if (error) { | 
|  | 827 | dev_err(&starget->dev, "target device_add failed, error %d\n", error); | 
|  | 828 | get_device(&starget->dev); | 
|  | 829 | scsi_target_reap(starget); | 
|  | 830 | put_device(&starget->dev); | 
|  | 831 | return error; | 
|  | 832 | } | 
|  | 833 | transport_add_device(&starget->dev); | 
|  | 834 | starget->state = STARGET_RUNNING; | 
|  | 835 |  | 
|  | 836 | return 0; | 
|  | 837 | } | 
|  | 838 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | static struct device_attribute sdev_attr_queue_type_rw = | 
|  | 840 | __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field, | 
|  | 841 | sdev_store_queue_type_rw); | 
|  | 842 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /** | 
|  | 844 | * scsi_sysfs_add_sdev - add scsi device to sysfs | 
|  | 845 | * @sdev:	scsi_device to add | 
|  | 846 | * | 
|  | 847 | * Return value: | 
|  | 848 | * 	0 on Success / non-zero on Failure | 
|  | 849 | **/ | 
|  | 850 | int scsi_sysfs_add_sdev(struct scsi_device *sdev) | 
|  | 851 | { | 
|  | 852 | int error, i; | 
| James Bottomley | 80ed71c | 2007-07-19 10:15:10 -0500 | [diff] [blame] | 853 | struct request_queue *rq = sdev->request_queue; | 
| James Bottomley | 643eb2d | 2008-03-22 22:42:27 -0500 | [diff] [blame] | 854 | struct scsi_target *starget = sdev->sdev_target; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 |  | 
|  | 856 | if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0) | 
|  | 857 | return error; | 
|  | 858 |  | 
| James Bottomley | 643eb2d | 2008-03-22 22:42:27 -0500 | [diff] [blame] | 859 | error = scsi_target_add(starget); | 
|  | 860 | if (error) | 
|  | 861 | return error; | 
|  | 862 |  | 
|  | 863 | transport_configure_device(&starget->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | error = device_add(&sdev->sdev_gendev); | 
|  | 865 | if (error) { | 
|  | 866 | put_device(sdev->sdev_gendev.parent); | 
|  | 867 | printk(KERN_INFO "error 1\n"); | 
|  | 868 | return error; | 
|  | 869 | } | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 870 | error = device_add(&sdev->sdev_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | if (error) { | 
|  | 872 | printk(KERN_INFO "error 2\n"); | 
|  | 873 | goto clean_device; | 
|  | 874 | } | 
|  | 875 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 876 | /* take a reference for the sdev_dev; this is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | * released by the sdev_class .release */ | 
|  | 878 | get_device(&sdev->sdev_gendev); | 
| James Bottomley | 80ed71c | 2007-07-19 10:15:10 -0500 | [diff] [blame] | 879 |  | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 880 | /* create queue files, which may be writable, depending on the host */ | 
|  | 881 | if (sdev->host->hostt->change_queue_depth) | 
|  | 882 | error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw); | 
|  | 883 | else | 
|  | 884 | error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth); | 
|  | 885 | if (error) { | 
|  | 886 | __scsi_remove_device(sdev); | 
|  | 887 | goto out; | 
|  | 888 | } | 
|  | 889 | if (sdev->host->hostt->change_queue_type) | 
|  | 890 | error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw); | 
|  | 891 | else | 
|  | 892 | error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type); | 
|  | 893 | if (error) { | 
|  | 894 | __scsi_remove_device(sdev); | 
|  | 895 | goto out; | 
|  | 896 | } | 
|  | 897 |  | 
| FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 898 | error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL); | 
| James Bottomley | 80ed71c | 2007-07-19 10:15:10 -0500 | [diff] [blame] | 899 |  | 
|  | 900 | if (error) | 
|  | 901 | sdev_printk(KERN_INFO, sdev, | 
|  | 902 | "Failed to register bsg queue, errno=%d\n", error); | 
|  | 903 |  | 
|  | 904 | /* we're treating error on bsg register as non-fatal, so pretend | 
|  | 905 | * nothing went wrong */ | 
|  | 906 | error = 0; | 
|  | 907 |  | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 908 | /* add additional host specific attributes */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | if (sdev->host->hostt->sdev_attrs) { | 
|  | 910 | for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) { | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 911 | error = device_create_file(&sdev->sdev_gendev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | sdev->host->hostt->sdev_attrs[i]); | 
|  | 913 | if (error) { | 
| Alan Stern | 903f4fe | 2005-07-26 10:20:53 -0400 | [diff] [blame] | 914 | __scsi_remove_device(sdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | goto out; | 
|  | 916 | } | 
|  | 917 | } | 
|  | 918 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 |  | 
|  | 920 | transport_add_device(&sdev->sdev_gendev); | 
|  | 921 | out: | 
|  | 922 | return error; | 
|  | 923 |  | 
|  | 924 | clean_device: | 
|  | 925 | scsi_device_set_state(sdev, SDEV_CANCEL); | 
|  | 926 |  | 
|  | 927 | device_del(&sdev->sdev_gendev); | 
|  | 928 | transport_destroy_device(&sdev->sdev_gendev); | 
|  | 929 | put_device(&sdev->sdev_gendev); | 
|  | 930 |  | 
|  | 931 | return error; | 
|  | 932 | } | 
|  | 933 |  | 
| Alan Stern | 903f4fe | 2005-07-26 10:20:53 -0400 | [diff] [blame] | 934 | void __scsi_remove_device(struct scsi_device *sdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { | 
| James Bottomley | df133c2 | 2005-11-06 11:47:08 -0600 | [diff] [blame] | 936 | struct device *dev = &sdev->sdev_gendev; | 
|  | 937 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0) | 
| Alan Stern | 903f4fe | 2005-07-26 10:20:53 -0400 | [diff] [blame] | 939 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 |  | 
| James Bottomley | 0feed27 | 2008-03-26 09:09:19 -0700 | [diff] [blame] | 941 | bsg_unregister_queue(sdev->request_queue); | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 942 | device_unregister(&sdev->sdev_dev); | 
| James Bottomley | df133c2 | 2005-11-06 11:47:08 -0600 | [diff] [blame] | 943 | transport_remove_device(dev); | 
|  | 944 | device_del(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | scsi_device_set_state(sdev, SDEV_DEL); | 
|  | 946 | if (sdev->host->hostt->slave_destroy) | 
|  | 947 | sdev->host->hostt->slave_destroy(sdev); | 
| James Bottomley | df133c2 | 2005-11-06 11:47:08 -0600 | [diff] [blame] | 948 | transport_destroy_device(dev); | 
|  | 949 | put_device(dev); | 
| Alan Stern | 903f4fe | 2005-07-26 10:20:53 -0400 | [diff] [blame] | 950 | } | 
|  | 951 |  | 
|  | 952 | /** | 
|  | 953 | * scsi_remove_device - unregister a device from the scsi bus | 
|  | 954 | * @sdev:	scsi_device to unregister | 
|  | 955 | **/ | 
|  | 956 | void scsi_remove_device(struct scsi_device *sdev) | 
|  | 957 | { | 
| Alan Stern | 5419500 | 2005-09-15 21:52:51 -0400 | [diff] [blame] | 958 | struct Scsi_Host *shost = sdev->host; | 
|  | 959 |  | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 960 | mutex_lock(&shost->scan_mutex); | 
| Alan Stern | 903f4fe | 2005-07-26 10:20:53 -0400 | [diff] [blame] | 961 | __scsi_remove_device(sdev); | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 962 | mutex_unlock(&shost->scan_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } | 
|  | 964 | EXPORT_SYMBOL(scsi_remove_device); | 
|  | 965 |  | 
| Adrian Bunk | 44818ef | 2007-07-09 11:59:59 -0700 | [diff] [blame] | 966 | static void __scsi_remove_target(struct scsi_target *starget) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { | 
|  | 968 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); | 
|  | 969 | unsigned long flags; | 
| Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 970 | struct scsi_device *sdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 |  | 
|  | 972 | spin_lock_irqsave(shost->host_lock, flags); | 
|  | 973 | starget->reap_ref++; | 
| Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 974 | restart: | 
|  | 975 | list_for_each_entry(sdev, &shost->__devices, siblings) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | if (sdev->channel != starget->channel || | 
| Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 977 | sdev->id != starget->id || | 
|  | 978 | sdev->sdev_state == SDEV_DEL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | continue; | 
|  | 980 | spin_unlock_irqrestore(shost->host_lock, flags); | 
|  | 981 | scsi_remove_device(sdev); | 
|  | 982 | spin_lock_irqsave(shost->host_lock, flags); | 
| Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 983 | goto restart; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } | 
|  | 985 | spin_unlock_irqrestore(shost->host_lock, flags); | 
|  | 986 | scsi_target_reap(starget); | 
|  | 987 | } | 
|  | 988 |  | 
| mochel@digitalimplant.org | 20b1e67 | 2005-03-24 19:03:59 -0800 | [diff] [blame] | 989 | static int __remove_child (struct device * dev, void * data) | 
|  | 990 | { | 
|  | 991 | if (scsi_is_target_device(dev)) | 
|  | 992 | __scsi_remove_target(to_scsi_target(dev)); | 
|  | 993 | return 0; | 
|  | 994 | } | 
|  | 995 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | /** | 
|  | 997 | * scsi_remove_target - try to remove a target and all its devices | 
|  | 998 | * @dev: generic starget or parent of generic stargets to be removed | 
|  | 999 | * | 
|  | 1000 | * Note: This is slightly racy.  It is possible that if the user | 
|  | 1001 | * requests the addition of another device then the target won't be | 
|  | 1002 | * removed. | 
|  | 1003 | */ | 
|  | 1004 | void scsi_remove_target(struct device *dev) | 
|  | 1005 | { | 
| mochel@digitalimplant.org | 20b1e67 | 2005-03-24 19:03:59 -0800 | [diff] [blame] | 1006 | struct device *rdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 |  | 
|  | 1008 | if (scsi_is_target_device(dev)) { | 
|  | 1009 | __scsi_remove_target(to_scsi_target(dev)); | 
|  | 1010 | return; | 
|  | 1011 | } | 
|  | 1012 |  | 
|  | 1013 | rdev = get_device(dev); | 
| mochel@digitalimplant.org | 20b1e67 | 2005-03-24 19:03:59 -0800 | [diff] [blame] | 1014 | device_for_each_child(dev, NULL, __remove_child); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | put_device(rdev); | 
|  | 1016 | } | 
|  | 1017 | EXPORT_SYMBOL(scsi_remove_target); | 
|  | 1018 |  | 
|  | 1019 | int scsi_register_driver(struct device_driver *drv) | 
|  | 1020 | { | 
|  | 1021 | drv->bus = &scsi_bus_type; | 
|  | 1022 |  | 
|  | 1023 | return driver_register(drv); | 
|  | 1024 | } | 
|  | 1025 | EXPORT_SYMBOL(scsi_register_driver); | 
|  | 1026 |  | 
|  | 1027 | int scsi_register_interface(struct class_interface *intf) | 
|  | 1028 | { | 
|  | 1029 | intf->class = &sdev_class; | 
|  | 1030 |  | 
|  | 1031 | return class_interface_register(intf); | 
|  | 1032 | } | 
|  | 1033 | EXPORT_SYMBOL(scsi_register_interface); | 
|  | 1034 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | /** | 
|  | 1036 | * scsi_sysfs_add_host - add scsi host to subsystem | 
|  | 1037 | * @shost:     scsi host struct to add to subsystem | 
|  | 1038 | * @dev:       parent struct device pointer | 
|  | 1039 | **/ | 
|  | 1040 | int scsi_sysfs_add_host(struct Scsi_Host *shost) | 
|  | 1041 | { | 
|  | 1042 | int error, i; | 
|  | 1043 |  | 
| Hannes Reinecke | f7120a4 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 1044 | /* add host specific attributes */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | if (shost->hostt->shost_attrs) { | 
|  | 1046 | for (i = 0; shost->hostt->shost_attrs[i]; i++) { | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1047 | error = device_create_file(&shost->shost_dev, | 
| Hannes Reinecke | f7120a4 | 2008-03-18 14:32:28 +0100 | [diff] [blame] | 1048 | shost->hostt->shost_attrs[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | if (error) | 
|  | 1050 | return error; | 
|  | 1051 | } | 
|  | 1052 | } | 
|  | 1053 |  | 
|  | 1054 | transport_register_device(&shost->shost_gendev); | 
| James Bottomley | d52b381 | 2008-01-05 09:38:30 -0600 | [diff] [blame] | 1055 | transport_configure_device(&shost->shost_gendev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return 0; | 
|  | 1057 | } | 
|  | 1058 |  | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 1059 | static struct device_type scsi_dev_type = { | 
|  | 1060 | .name =		"scsi_device", | 
|  | 1061 | .release =	scsi_device_dev_release, | 
|  | 1062 | .groups =	scsi_sdev_attr_groups, | 
|  | 1063 | }; | 
|  | 1064 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | void scsi_sysfs_device_initialize(struct scsi_device *sdev) | 
|  | 1066 | { | 
|  | 1067 | unsigned long flags; | 
|  | 1068 | struct Scsi_Host *shost = sdev->host; | 
|  | 1069 | struct scsi_target  *starget = sdev->sdev_target; | 
|  | 1070 |  | 
|  | 1071 | device_initialize(&sdev->sdev_gendev); | 
|  | 1072 | sdev->sdev_gendev.bus = &scsi_bus_type; | 
| Kay Sievers | bfd1294 | 2007-09-11 17:00:14 +0200 | [diff] [blame] | 1073 | sdev->sdev_gendev.type = &scsi_dev_type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d", | 
|  | 1075 | sdev->host->host_no, sdev->channel, sdev->id, | 
|  | 1076 | sdev->lun); | 
|  | 1077 |  | 
| Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1078 | device_initialize(&sdev->sdev_dev); | 
|  | 1079 | sdev->sdev_dev.parent = &sdev->sdev_gendev; | 
|  | 1080 | sdev->sdev_dev.class = &sdev_class; | 
|  | 1081 | snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | "%d:%d:%d:%d", sdev->host->host_no, | 
|  | 1083 | sdev->channel, sdev->id, sdev->lun); | 
| Alan Stern | 7c9d6f1 | 2007-01-08 11:12:32 -0500 | [diff] [blame] | 1084 | sdev->scsi_level = starget->scsi_level; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | transport_setup_device(&sdev->sdev_gendev); | 
|  | 1086 | spin_lock_irqsave(shost->host_lock, flags); | 
|  | 1087 | list_add_tail(&sdev->same_target_siblings, &starget->devices); | 
|  | 1088 | list_add_tail(&sdev->siblings, &shost->__devices); | 
|  | 1089 | spin_unlock_irqrestore(shost->host_lock, flags); | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 | int scsi_is_sdev_device(const struct device *dev) | 
|  | 1093 | { | 
| Kay Sievers | 13ba9bc | 2007-09-26 19:54:49 +0200 | [diff] [blame] | 1094 | return dev->type == &scsi_dev_type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } | 
|  | 1096 | EXPORT_SYMBOL(scsi_is_sdev_device); | 
|  | 1097 |  | 
|  | 1098 | /* A blank transport template that is used in drivers that don't | 
|  | 1099 | * yet implement Transport Attributes */ | 
|  | 1100 | struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, }; |