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