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