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