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