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