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