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