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