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