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