Merge with Linus' kernel.
[linux-2.6] / 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/config.h>
10 #include <linux/module.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
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 < sizeof(sdev_states)/sizeof(sdev_states[0]); 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 < sizeof(shost_states)/sizeof(shost_states[0]); 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         res = scsi_scan_host_selected(shost, channel, id, lun, 1);
110         return res;
111 }
112
113 /*
114  * shost_show_function: macro to create an attr function that can be used to
115  * show a non-bit field.
116  */
117 #define shost_show_function(name, field, format_string)                 \
118 static ssize_t                                                          \
119 show_##name (struct class_device *class_dev, char *buf)                 \
120 {                                                                       \
121         struct Scsi_Host *shost = class_to_shost(class_dev);            \
122         return snprintf (buf, 20, format_string, shost->field);         \
123 }
124
125 /*
126  * shost_rd_attr: macro to create a function and attribute variable for a
127  * read only field.
128  */
129 #define shost_rd_attr2(name, field, format_string)                      \
130         shost_show_function(name, field, format_string)                 \
131 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
132
133 #define shost_rd_attr(field, format_string) \
134 shost_rd_attr2(field, field, format_string)
135
136 /*
137  * Create the actual show/store functions and data structures.
138  */
139
140 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
141                           size_t count)
142 {
143         struct Scsi_Host *shost = class_to_shost(class_dev);
144         int res;
145
146         res = scsi_scan(shost, buf);
147         if (res == 0)
148                 res = count;
149         return res;
150 };
151 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
152
153 static ssize_t
154 store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
155 {
156         int i;
157         struct Scsi_Host *shost = class_to_shost(class_dev);
158         enum scsi_host_state state = 0;
159
160         for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
161                 const int len = strlen(shost_states[i].name);
162                 if (strncmp(shost_states[i].name, buf, len) == 0 &&
163                    buf[len] == '\n') {
164                         state = shost_states[i].value;
165                         break;
166                 }
167         }
168         if (!state)
169                 return -EINVAL;
170
171         if (scsi_host_set_state(shost, state))
172                 return -EINVAL;
173         return count;
174 }
175
176 static ssize_t
177 show_shost_state(struct class_device *class_dev, char *buf)
178 {
179         struct Scsi_Host *shost = class_to_shost(class_dev);
180         const char *name = scsi_host_state_name(shost->shost_state);
181
182         if (!name)
183                 return -EINVAL;
184
185         return snprintf(buf, 20, "%s\n", name);
186 }
187
188 static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
189
190 shost_rd_attr(unique_id, "%u\n");
191 shost_rd_attr(host_busy, "%hu\n");
192 shost_rd_attr(cmd_per_lun, "%hd\n");
193 shost_rd_attr(sg_tablesize, "%hu\n");
194 shost_rd_attr(unchecked_isa_dma, "%d\n");
195 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
196
197 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
198         &class_device_attr_unique_id,
199         &class_device_attr_host_busy,
200         &class_device_attr_cmd_per_lun,
201         &class_device_attr_sg_tablesize,
202         &class_device_attr_unchecked_isa_dma,
203         &class_device_attr_proc_name,
204         &class_device_attr_scan,
205         &class_device_attr_state,
206         NULL
207 };
208
209 static void scsi_device_cls_release(struct class_device *class_dev)
210 {
211         struct scsi_device *sdev;
212
213         sdev = class_to_sdev(class_dev);
214         put_device(&sdev->sdev_gendev);
215 }
216
217 static void scsi_device_dev_release(struct device *dev)
218 {
219         struct scsi_device *sdev;
220         struct device *parent;
221         struct scsi_target *starget;
222         unsigned long flags;
223
224         parent = dev->parent;
225         sdev = to_scsi_device(dev);
226         starget = to_scsi_target(parent);
227
228         spin_lock_irqsave(sdev->host->host_lock, flags);
229         starget->reap_ref++;
230         list_del(&sdev->siblings);
231         list_del(&sdev->same_target_siblings);
232         list_del(&sdev->starved_entry);
233         spin_unlock_irqrestore(sdev->host->host_lock, flags);
234
235         if (sdev->request_queue) {
236                 sdev->request_queue->queuedata = NULL;
237                 scsi_free_queue(sdev->request_queue);
238                 /* temporary expedient, try to catch use of queue lock
239                  * after free of sdev */
240                 sdev->request_queue = NULL;
241         }
242
243         scsi_target_reap(scsi_target(sdev));
244
245         kfree(sdev->inquiry);
246         kfree(sdev);
247
248         if (parent)
249                 put_device(parent);
250 }
251
252 static struct class sdev_class = {
253         .name           = "scsi_device",
254         .release        = scsi_device_cls_release,
255 };
256
257 /* all probing is done in the individual ->probe routines */
258 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
259 {
260         struct scsi_device *sdp = to_scsi_device(dev);
261         if (sdp->no_uld_attach)
262                 return 0;
263         return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
264 }
265
266 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
267 {
268         struct scsi_device *sdev = to_scsi_device(dev);
269         struct scsi_host_template *sht = sdev->host->hostt;
270         int err;
271
272         err = scsi_device_quiesce(sdev);
273         if (err)
274                 return err;
275
276         if (sht->suspend)
277                 err = sht->suspend(sdev);
278
279         return err;
280 }
281
282 static int scsi_bus_resume(struct device * dev)
283 {
284         struct scsi_device *sdev = to_scsi_device(dev);
285         struct scsi_host_template *sht = sdev->host->hostt;
286         int err = 0;
287
288         if (sht->resume)
289                 err = sht->resume(sdev);
290
291         scsi_device_resume(sdev);
292         return err;
293 }
294
295 struct bus_type scsi_bus_type = {
296         .name           = "scsi",
297         .match          = scsi_bus_match,
298         .suspend        = scsi_bus_suspend,
299         .resume         = scsi_bus_resume,
300 };
301
302 int scsi_sysfs_register(void)
303 {
304         int error;
305
306         error = bus_register(&scsi_bus_type);
307         if (!error) {
308                 error = class_register(&sdev_class);
309                 if (error)
310                         bus_unregister(&scsi_bus_type);
311         }
312
313         return error;
314 }
315
316 void scsi_sysfs_unregister(void)
317 {
318         class_unregister(&sdev_class);
319         bus_unregister(&scsi_bus_type);
320 }
321
322 /*
323  * sdev_show_function: macro to create an attr function that can be used to
324  * show a non-bit field.
325  */
326 #define sdev_show_function(field, format_string)                                \
327 static ssize_t                                                          \
328 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf)                                \
329 {                                                                       \
330         struct scsi_device *sdev;                                       \
331         sdev = to_scsi_device(dev);                                     \
332         return snprintf (buf, 20, format_string, sdev->field);          \
333 }                                                                       \
334
335 /*
336  * sdev_rd_attr: macro to create a function and attribute variable for a
337  * read only field.
338  */
339 #define sdev_rd_attr(field, format_string)                              \
340         sdev_show_function(field, format_string)                        \
341 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
342
343
344 /*
345  * sdev_rd_attr: create a function and attribute variable for a
346  * read/write field.
347  */
348 #define sdev_rw_attr(field, format_string)                              \
349         sdev_show_function(field, format_string)                                \
350                                                                         \
351 static ssize_t                                                          \
352 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)   \
353 {                                                                       \
354         struct scsi_device *sdev;                                       \
355         sdev = to_scsi_device(dev);                                     \
356         snscanf (buf, 20, format_string, &sdev->field);                 \
357         return count;                                                   \
358 }                                                                       \
359 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
360
361 /* Currently we don't export bit fields, but we might in future,
362  * so leave this code in */
363 #if 0
364 /*
365  * sdev_rd_attr: create a function and attribute variable for a
366  * read/write bit field.
367  */
368 #define sdev_rw_attr_bit(field)                                         \
369         sdev_show_function(field, "%d\n")                                       \
370                                                                         \
371 static ssize_t                                                          \
372 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)   \
373 {                                                                       \
374         int ret;                                                        \
375         struct scsi_device *sdev;                                       \
376         ret = scsi_sdev_check_buf_bit(buf);                             \
377         if (ret >= 0)   {                                               \
378                 sdev = to_scsi_device(dev);                             \
379                 sdev->field = ret;                                      \
380                 ret = count;                                            \
381         }                                                               \
382         return ret;                                                     \
383 }                                                                       \
384 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
385
386 /*
387  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
388  * else return -EINVAL.
389  */
390 static int scsi_sdev_check_buf_bit(const char *buf)
391 {
392         if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
393                 if (buf[0] == '1')
394                         return 1;
395                 else if (buf[0] == '0')
396                         return 0;
397                 else 
398                         return -EINVAL;
399         } else
400                 return -EINVAL;
401 }
402 #endif
403 /*
404  * Create the actual show/store functions and data structures.
405  */
406 sdev_rd_attr (device_blocked, "%d\n");
407 sdev_rd_attr (queue_depth, "%d\n");
408 sdev_rd_attr (type, "%d\n");
409 sdev_rd_attr (scsi_level, "%d\n");
410 sdev_rd_attr (vendor, "%.8s\n");
411 sdev_rd_attr (model, "%.16s\n");
412 sdev_rd_attr (rev, "%.4s\n");
413
414 static ssize_t
415 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
416 {
417         struct scsi_device *sdev;
418         sdev = to_scsi_device(dev);
419         return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
420 }
421
422 static ssize_t
423 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
424 {
425         struct scsi_device *sdev;
426         int timeout;
427         sdev = to_scsi_device(dev);
428         sscanf (buf, "%d\n", &timeout);
429         sdev->timeout = timeout * HZ;
430         return count;
431 }
432 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
433
434 static ssize_t
435 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
436 {
437         scsi_rescan_device(dev);
438         return count;
439 }
440 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
441
442 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
443                                  size_t count)
444 {
445         scsi_remove_device(to_scsi_device(dev));
446         return count;
447 };
448 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
449
450 static ssize_t
451 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
452 {
453         int i;
454         struct scsi_device *sdev = to_scsi_device(dev);
455         enum scsi_device_state state = 0;
456
457         for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
458                 const int len = strlen(sdev_states[i].name);
459                 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
460                    buf[len] == '\n') {
461                         state = sdev_states[i].value;
462                         break;
463                 }
464         }
465         if (!state)
466                 return -EINVAL;
467
468         if (scsi_device_set_state(sdev, state))
469                 return -EINVAL;
470         return count;
471 }
472
473 static ssize_t
474 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
475 {
476         struct scsi_device *sdev = to_scsi_device(dev);
477         const char *name = scsi_device_state_name(sdev->sdev_state);
478
479         if (!name)
480                 return -EINVAL;
481
482         return snprintf(buf, 20, "%s\n", name);
483 }
484
485 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
486
487 static ssize_t
488 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
489 {
490         struct scsi_device *sdev = to_scsi_device(dev);
491         const char *name = "none";
492
493         if (sdev->ordered_tags)
494                 name = "ordered";
495         else if (sdev->simple_tags)
496                 name = "simple";
497
498         return snprintf(buf, 20, "%s\n", name);
499 }
500
501 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
502
503 static ssize_t
504 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
505 {
506         return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
507 }
508
509 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
510
511 #define show_sdev_iostat(field)                                         \
512 static ssize_t                                                          \
513 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf)                       \
514 {                                                                       \
515         struct scsi_device *sdev = to_scsi_device(dev);                 \
516         unsigned long long count = atomic_read(&sdev->field);           \
517         return snprintf(buf, 20, "0x%llx\n", count);                    \
518 }                                                                       \
519 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
520
521 show_sdev_iostat(iorequest_cnt);
522 show_sdev_iostat(iodone_cnt);
523 show_sdev_iostat(ioerr_cnt);
524
525
526 /* Default template for device attributes.  May NOT be modified */
527 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
528         &dev_attr_device_blocked,
529         &dev_attr_queue_depth,
530         &dev_attr_queue_type,
531         &dev_attr_type,
532         &dev_attr_scsi_level,
533         &dev_attr_vendor,
534         &dev_attr_model,
535         &dev_attr_rev,
536         &dev_attr_rescan,
537         &dev_attr_delete,
538         &dev_attr_state,
539         &dev_attr_timeout,
540         &dev_attr_iocounterbits,
541         &dev_attr_iorequest_cnt,
542         &dev_attr_iodone_cnt,
543         &dev_attr_ioerr_cnt,
544         NULL
545 };
546
547 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
548                                          size_t count)
549 {
550         int depth, retval;
551         struct scsi_device *sdev = to_scsi_device(dev);
552         struct scsi_host_template *sht = sdev->host->hostt;
553
554         if (!sht->change_queue_depth)
555                 return -EINVAL;
556
557         depth = simple_strtoul(buf, NULL, 0);
558
559         if (depth < 1)
560                 return -EINVAL;
561
562         retval = sht->change_queue_depth(sdev, depth);
563         if (retval < 0)
564                 return retval;
565
566         return count;
567 }
568
569 static struct device_attribute sdev_attr_queue_depth_rw =
570         __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
571                sdev_store_queue_depth_rw);
572
573 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
574                                         size_t count)
575 {
576         struct scsi_device *sdev = to_scsi_device(dev);
577         struct scsi_host_template *sht = sdev->host->hostt;
578         int tag_type = 0, retval;
579         int prev_tag_type = scsi_get_tag_type(sdev);
580
581         if (!sdev->tagged_supported || !sht->change_queue_type)
582                 return -EINVAL;
583
584         if (strncmp(buf, "ordered", 7) == 0)
585                 tag_type = MSG_ORDERED_TAG;
586         else if (strncmp(buf, "simple", 6) == 0)
587                 tag_type = MSG_SIMPLE_TAG;
588         else if (strncmp(buf, "none", 4) != 0)
589                 return -EINVAL;
590
591         if (tag_type == prev_tag_type)
592                 return count;
593
594         retval = sht->change_queue_type(sdev, tag_type);
595         if (retval < 0)
596                 return retval;
597
598         return count;
599 }
600
601 static struct device_attribute sdev_attr_queue_type_rw =
602         __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
603                sdev_store_queue_type_rw);
604
605 static struct device_attribute *attr_changed_internally(
606                 struct Scsi_Host *shost,
607                 struct device_attribute * attr)
608 {
609         if (!strcmp("queue_depth", attr->attr.name)
610             && shost->hostt->change_queue_depth)
611                 return &sdev_attr_queue_depth_rw;
612         else if (!strcmp("queue_type", attr->attr.name)
613             && shost->hostt->change_queue_type)
614                 return &sdev_attr_queue_type_rw;
615         return attr;
616 }
617
618
619 static struct device_attribute *attr_overridden(
620                 struct device_attribute **attrs,
621                 struct device_attribute *attr)
622 {
623         int i;
624
625         if (!attrs)
626                 return NULL;
627         for (i = 0; attrs[i]; i++)
628                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
629                         return attrs[i];
630         return NULL;
631 }
632
633 static int attr_add(struct device *dev, struct device_attribute *attr)
634 {
635         struct device_attribute *base_attr;
636
637         /*
638          * Spare the caller from having to copy things it's not interested in.
639          */
640         base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
641         if (base_attr) {
642                 /* extend permissions */
643                 attr->attr.mode |= base_attr->attr.mode;
644
645                 /* override null show/store with default */
646                 if (!attr->show)
647                         attr->show = base_attr->show;
648                 if (!attr->store)
649                         attr->store = base_attr->store;
650         }
651
652         return device_create_file(dev, attr);
653 }
654
655 /**
656  * scsi_sysfs_add_sdev - add scsi device to sysfs
657  * @sdev:       scsi_device to add
658  *
659  * Return value:
660  *      0 on Success / non-zero on Failure
661  **/
662 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
663 {
664         int error, i;
665
666         if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
667                 return error;
668
669         error = device_add(&sdev->sdev_gendev);
670         if (error) {
671                 put_device(sdev->sdev_gendev.parent);
672                 printk(KERN_INFO "error 1\n");
673                 return error;
674         }
675         error = class_device_add(&sdev->sdev_classdev);
676         if (error) {
677                 printk(KERN_INFO "error 2\n");
678                 goto clean_device;
679         }
680
681         /* take a reference for the sdev_classdev; this is
682          * released by the sdev_class .release */
683         get_device(&sdev->sdev_gendev);
684         if (sdev->host->hostt->sdev_attrs) {
685                 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
686                         error = attr_add(&sdev->sdev_gendev,
687                                         sdev->host->hostt->sdev_attrs[i]);
688                         if (error) {
689                                 __scsi_remove_device(sdev);
690                                 goto out;
691                         }
692                 }
693         }
694         
695         for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
696                 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
697                                         scsi_sysfs_sdev_attrs[i])) {
698                         struct device_attribute * attr = 
699                                 attr_changed_internally(sdev->host, 
700                                                         scsi_sysfs_sdev_attrs[i]);
701                         error = device_create_file(&sdev->sdev_gendev, attr);
702                         if (error) {
703                                 __scsi_remove_device(sdev);
704                                 goto out;
705                         }
706                 }
707         }
708
709         transport_add_device(&sdev->sdev_gendev);
710  out:
711         return error;
712
713  clean_device:
714         scsi_device_set_state(sdev, SDEV_CANCEL);
715
716         device_del(&sdev->sdev_gendev);
717         transport_destroy_device(&sdev->sdev_gendev);
718         put_device(&sdev->sdev_gendev);
719
720         return error;
721 }
722
723 void __scsi_remove_device(struct scsi_device *sdev)
724 {
725         struct device *dev = &sdev->sdev_gendev;
726
727         if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
728                 return;
729
730         class_device_unregister(&sdev->sdev_classdev);
731         transport_remove_device(dev);
732         device_del(dev);
733         scsi_device_set_state(sdev, SDEV_DEL);
734         if (sdev->host->hostt->slave_destroy)
735                 sdev->host->hostt->slave_destroy(sdev);
736         transport_destroy_device(dev);
737         put_device(dev);
738 }
739
740 /**
741  * scsi_remove_device - unregister a device from the scsi bus
742  * @sdev:       scsi_device to unregister
743  **/
744 void scsi_remove_device(struct scsi_device *sdev)
745 {
746         struct Scsi_Host *shost = sdev->host;
747
748         down(&shost->scan_mutex);
749         __scsi_remove_device(sdev);
750         up(&shost->scan_mutex);
751 }
752 EXPORT_SYMBOL(scsi_remove_device);
753
754 void __scsi_remove_target(struct scsi_target *starget)
755 {
756         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
757         unsigned long flags;
758         struct scsi_device *sdev;
759
760         spin_lock_irqsave(shost->host_lock, flags);
761         starget->reap_ref++;
762  restart:
763         list_for_each_entry(sdev, &shost->__devices, siblings) {
764                 if (sdev->channel != starget->channel ||
765                     sdev->id != starget->id ||
766                     sdev->sdev_state == SDEV_DEL)
767                         continue;
768                 spin_unlock_irqrestore(shost->host_lock, flags);
769                 scsi_remove_device(sdev);
770                 spin_lock_irqsave(shost->host_lock, flags);
771                 goto restart;
772         }
773         spin_unlock_irqrestore(shost->host_lock, flags);
774         scsi_target_reap(starget);
775 }
776
777 static int __remove_child (struct device * dev, void * data)
778 {
779         if (scsi_is_target_device(dev))
780                 __scsi_remove_target(to_scsi_target(dev));
781         return 0;
782 }
783
784 /**
785  * scsi_remove_target - try to remove a target and all its devices
786  * @dev: generic starget or parent of generic stargets to be removed
787  *
788  * Note: This is slightly racy.  It is possible that if the user
789  * requests the addition of another device then the target won't be
790  * removed.
791  */
792 void scsi_remove_target(struct device *dev)
793 {
794         struct device *rdev;
795
796         if (scsi_is_target_device(dev)) {
797                 __scsi_remove_target(to_scsi_target(dev));
798                 return;
799         }
800
801         rdev = get_device(dev);
802         device_for_each_child(dev, NULL, __remove_child);
803         put_device(rdev);
804 }
805 EXPORT_SYMBOL(scsi_remove_target);
806
807 int scsi_register_driver(struct device_driver *drv)
808 {
809         drv->bus = &scsi_bus_type;
810
811         return driver_register(drv);
812 }
813 EXPORT_SYMBOL(scsi_register_driver);
814
815 int scsi_register_interface(struct class_interface *intf)
816 {
817         intf->class = &sdev_class;
818
819         return class_interface_register(intf);
820 }
821 EXPORT_SYMBOL(scsi_register_interface);
822
823
824 static struct class_device_attribute *class_attr_overridden(
825                 struct class_device_attribute **attrs,
826                 struct class_device_attribute *attr)
827 {
828         int i;
829
830         if (!attrs)
831                 return NULL;
832         for (i = 0; attrs[i]; i++)
833                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
834                         return attrs[i];
835         return NULL;
836 }
837
838 static int class_attr_add(struct class_device *classdev,
839                 struct class_device_attribute *attr)
840 {
841         struct class_device_attribute *base_attr;
842
843         /*
844          * Spare the caller from having to copy things it's not interested in.
845          */
846         base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
847         if (base_attr) {
848                 /* extend permissions */
849                 attr->attr.mode |= base_attr->attr.mode;
850
851                 /* override null show/store with default */
852                 if (!attr->show)
853                         attr->show = base_attr->show;
854                 if (!attr->store)
855                         attr->store = base_attr->store;
856         }
857
858         return class_device_create_file(classdev, attr);
859 }
860
861 /**
862  * scsi_sysfs_add_host - add scsi host to subsystem
863  * @shost:     scsi host struct to add to subsystem
864  * @dev:       parent struct device pointer
865  **/
866 int scsi_sysfs_add_host(struct Scsi_Host *shost)
867 {
868         int error, i;
869
870         if (shost->hostt->shost_attrs) {
871                 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
872                         error = class_attr_add(&shost->shost_classdev,
873                                         shost->hostt->shost_attrs[i]);
874                         if (error)
875                                 return error;
876                 }
877         }
878
879         for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
880                 if (!class_attr_overridden(shost->hostt->shost_attrs,
881                                         scsi_sysfs_shost_attrs[i])) {
882                         error = class_device_create_file(&shost->shost_classdev,
883                                         scsi_sysfs_shost_attrs[i]);
884                         if (error)
885                                 return error;
886                 }
887         }
888
889         transport_register_device(&shost->shost_gendev);
890         return 0;
891 }
892
893 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
894 {
895         unsigned long flags;
896         struct Scsi_Host *shost = sdev->host;
897         struct scsi_target  *starget = sdev->sdev_target;
898
899         device_initialize(&sdev->sdev_gendev);
900         sdev->sdev_gendev.bus = &scsi_bus_type;
901         sdev->sdev_gendev.release = scsi_device_dev_release;
902         sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
903                 sdev->host->host_no, sdev->channel, sdev->id,
904                 sdev->lun);
905         
906         class_device_initialize(&sdev->sdev_classdev);
907         sdev->sdev_classdev.dev = &sdev->sdev_gendev;
908         sdev->sdev_classdev.class = &sdev_class;
909         snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
910                  "%d:%d:%d:%d", sdev->host->host_no,
911                  sdev->channel, sdev->id, sdev->lun);
912         sdev->scsi_level = SCSI_2;
913         transport_setup_device(&sdev->sdev_gendev);
914         spin_lock_irqsave(shost->host_lock, flags);
915         list_add_tail(&sdev->same_target_siblings, &starget->devices);
916         list_add_tail(&sdev->siblings, &shost->__devices);
917         spin_unlock_irqrestore(shost->host_lock, flags);
918 }
919
920 int scsi_is_sdev_device(const struct device *dev)
921 {
922         return dev->release == scsi_device_dev_release;
923 }
924 EXPORT_SYMBOL(scsi_is_sdev_device);
925
926 /* A blank transport template that is used in drivers that don't
927  * yet implement Transport Attributes */
928 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };