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