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