Pull bugzilla-7880 into release branch
[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/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_uevent(struct device *dev, char **envp, int num_envp,
280                            char *buffer, int buffer_size)
281 {
282         struct scsi_device *sdev = to_scsi_device(dev);
283         int i = 0;
284         int length = 0;
285
286         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
287                        "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
288         envp[i] = NULL;
289         return 0;
290 }
291
292 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
293 {
294         struct device_driver *drv = dev->driver;
295         struct scsi_device *sdev = to_scsi_device(dev);
296         int err;
297
298         err = scsi_device_quiesce(sdev);
299         if (err)
300                 return err;
301
302         if (drv && drv->suspend) {
303                 err = drv->suspend(dev, state);
304                 if (err)
305                         return err;
306         }
307
308         return 0;
309 }
310
311 static int scsi_bus_resume(struct device * dev)
312 {
313         struct device_driver *drv = dev->driver;
314         struct scsi_device *sdev = to_scsi_device(dev);
315         int err = 0;
316
317         if (drv && drv->resume)
318                 err = drv->resume(dev);
319
320         scsi_device_resume(sdev);
321
322         return err;
323 }
324
325 struct bus_type scsi_bus_type = {
326         .name           = "scsi",
327         .match          = scsi_bus_match,
328         .uevent         = scsi_bus_uevent,
329         .suspend        = scsi_bus_suspend,
330         .resume         = scsi_bus_resume,
331 };
332
333 int scsi_sysfs_register(void)
334 {
335         int error;
336
337         error = bus_register(&scsi_bus_type);
338         if (!error) {
339                 error = class_register(&sdev_class);
340                 if (error)
341                         bus_unregister(&scsi_bus_type);
342         }
343
344         return error;
345 }
346
347 void scsi_sysfs_unregister(void)
348 {
349         class_unregister(&sdev_class);
350         bus_unregister(&scsi_bus_type);
351 }
352
353 /*
354  * sdev_show_function: macro to create an attr function that can be used to
355  * show a non-bit field.
356  */
357 #define sdev_show_function(field, format_string)                                \
358 static ssize_t                                                          \
359 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf)                                \
360 {                                                                       \
361         struct scsi_device *sdev;                                       \
362         sdev = to_scsi_device(dev);                                     \
363         return snprintf (buf, 20, format_string, sdev->field);          \
364 }                                                                       \
365
366 /*
367  * sdev_rd_attr: macro to create a function and attribute variable for a
368  * read only field.
369  */
370 #define sdev_rd_attr(field, format_string)                              \
371         sdev_show_function(field, format_string)                        \
372 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
373
374
375 /*
376  * sdev_rd_attr: create a function and attribute variable for a
377  * read/write field.
378  */
379 #define sdev_rw_attr(field, format_string)                              \
380         sdev_show_function(field, format_string)                                \
381                                                                         \
382 static ssize_t                                                          \
383 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)   \
384 {                                                                       \
385         struct scsi_device *sdev;                                       \
386         sdev = to_scsi_device(dev);                                     \
387         snscanf (buf, 20, format_string, &sdev->field);                 \
388         return count;                                                   \
389 }                                                                       \
390 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
391
392 /* Currently we don't export bit fields, but we might in future,
393  * so leave this code in */
394 #if 0
395 /*
396  * sdev_rd_attr: create a function and attribute variable for a
397  * read/write bit field.
398  */
399 #define sdev_rw_attr_bit(field)                                         \
400         sdev_show_function(field, "%d\n")                                       \
401                                                                         \
402 static ssize_t                                                          \
403 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)   \
404 {                                                                       \
405         int ret;                                                        \
406         struct scsi_device *sdev;                                       \
407         ret = scsi_sdev_check_buf_bit(buf);                             \
408         if (ret >= 0)   {                                               \
409                 sdev = to_scsi_device(dev);                             \
410                 sdev->field = ret;                                      \
411                 ret = count;                                            \
412         }                                                               \
413         return ret;                                                     \
414 }                                                                       \
415 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
416
417 /*
418  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
419  * else return -EINVAL.
420  */
421 static int scsi_sdev_check_buf_bit(const char *buf)
422 {
423         if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
424                 if (buf[0] == '1')
425                         return 1;
426                 else if (buf[0] == '0')
427                         return 0;
428                 else 
429                         return -EINVAL;
430         } else
431                 return -EINVAL;
432 }
433 #endif
434 /*
435  * Create the actual show/store functions and data structures.
436  */
437 sdev_rd_attr (device_blocked, "%d\n");
438 sdev_rd_attr (queue_depth, "%d\n");
439 sdev_rd_attr (type, "%d\n");
440 sdev_rd_attr (scsi_level, "%d\n");
441 sdev_rd_attr (vendor, "%.8s\n");
442 sdev_rd_attr (model, "%.16s\n");
443 sdev_rd_attr (rev, "%.4s\n");
444
445 static ssize_t
446 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
447 {
448         struct scsi_device *sdev;
449         sdev = to_scsi_device(dev);
450         return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
451 }
452
453 static ssize_t
454 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
455 {
456         struct scsi_device *sdev;
457         int timeout;
458         sdev = to_scsi_device(dev);
459         sscanf (buf, "%d\n", &timeout);
460         sdev->timeout = timeout * HZ;
461         return count;
462 }
463 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
464
465 static ssize_t
466 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
467 {
468         scsi_rescan_device(dev);
469         return count;
470 }
471 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
472
473 static void sdev_store_delete_callback(struct device *dev)
474 {
475         scsi_remove_device(to_scsi_device(dev));
476 }
477
478 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
479                                  size_t count)
480 {
481         int rc;
482
483         /* An attribute cannot be unregistered by one of its own methods,
484          * so we have to use this roundabout approach.
485          */
486         rc = device_schedule_callback(dev, sdev_store_delete_callback);
487         if (rc)
488                 count = rc;
489         return count;
490 };
491 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
492
493 static ssize_t
494 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
495 {
496         int i;
497         struct scsi_device *sdev = to_scsi_device(dev);
498         enum scsi_device_state state = 0;
499
500         for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
501                 const int len = strlen(sdev_states[i].name);
502                 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
503                    buf[len] == '\n') {
504                         state = sdev_states[i].value;
505                         break;
506                 }
507         }
508         if (!state)
509                 return -EINVAL;
510
511         if (scsi_device_set_state(sdev, state))
512                 return -EINVAL;
513         return count;
514 }
515
516 static ssize_t
517 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
518 {
519         struct scsi_device *sdev = to_scsi_device(dev);
520         const char *name = scsi_device_state_name(sdev->sdev_state);
521
522         if (!name)
523                 return -EINVAL;
524
525         return snprintf(buf, 20, "%s\n", name);
526 }
527
528 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
529
530 static ssize_t
531 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
532 {
533         struct scsi_device *sdev = to_scsi_device(dev);
534         const char *name = "none";
535
536         if (sdev->ordered_tags)
537                 name = "ordered";
538         else if (sdev->simple_tags)
539                 name = "simple";
540
541         return snprintf(buf, 20, "%s\n", name);
542 }
543
544 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
545
546 static ssize_t
547 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
548 {
549         return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
550 }
551
552 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
553
554 #define show_sdev_iostat(field)                                         \
555 static ssize_t                                                          \
556 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf)                       \
557 {                                                                       \
558         struct scsi_device *sdev = to_scsi_device(dev);                 \
559         unsigned long long count = atomic_read(&sdev->field);           \
560         return snprintf(buf, 20, "0x%llx\n", count);                    \
561 }                                                                       \
562 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
563
564 show_sdev_iostat(iorequest_cnt);
565 show_sdev_iostat(iodone_cnt);
566 show_sdev_iostat(ioerr_cnt);
567
568 static ssize_t
569 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
570 {
571         struct scsi_device *sdev;
572         sdev = to_scsi_device(dev);
573         return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
574 }
575 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
576
577 /* Default template for device attributes.  May NOT be modified */
578 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
579         &dev_attr_device_blocked,
580         &dev_attr_queue_depth,
581         &dev_attr_queue_type,
582         &dev_attr_type,
583         &dev_attr_scsi_level,
584         &dev_attr_vendor,
585         &dev_attr_model,
586         &dev_attr_rev,
587         &dev_attr_rescan,
588         &dev_attr_delete,
589         &dev_attr_state,
590         &dev_attr_timeout,
591         &dev_attr_iocounterbits,
592         &dev_attr_iorequest_cnt,
593         &dev_attr_iodone_cnt,
594         &dev_attr_ioerr_cnt,
595         &dev_attr_modalias,
596         NULL
597 };
598
599 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
600                                          size_t count)
601 {
602         int depth, retval;
603         struct scsi_device *sdev = to_scsi_device(dev);
604         struct scsi_host_template *sht = sdev->host->hostt;
605
606         if (!sht->change_queue_depth)
607                 return -EINVAL;
608
609         depth = simple_strtoul(buf, NULL, 0);
610
611         if (depth < 1)
612                 return -EINVAL;
613
614         retval = sht->change_queue_depth(sdev, depth);
615         if (retval < 0)
616                 return retval;
617
618         return count;
619 }
620
621 static struct device_attribute sdev_attr_queue_depth_rw =
622         __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
623                sdev_store_queue_depth_rw);
624
625 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
626                                         size_t count)
627 {
628         struct scsi_device *sdev = to_scsi_device(dev);
629         struct scsi_host_template *sht = sdev->host->hostt;
630         int tag_type = 0, retval;
631         int prev_tag_type = scsi_get_tag_type(sdev);
632
633         if (!sdev->tagged_supported || !sht->change_queue_type)
634                 return -EINVAL;
635
636         if (strncmp(buf, "ordered", 7) == 0)
637                 tag_type = MSG_ORDERED_TAG;
638         else if (strncmp(buf, "simple", 6) == 0)
639                 tag_type = MSG_SIMPLE_TAG;
640         else if (strncmp(buf, "none", 4) != 0)
641                 return -EINVAL;
642
643         if (tag_type == prev_tag_type)
644                 return count;
645
646         retval = sht->change_queue_type(sdev, tag_type);
647         if (retval < 0)
648                 return retval;
649
650         return count;
651 }
652
653 static struct device_attribute sdev_attr_queue_type_rw =
654         __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
655                sdev_store_queue_type_rw);
656
657 static struct device_attribute *attr_changed_internally(
658                 struct Scsi_Host *shost,
659                 struct device_attribute * attr)
660 {
661         if (!strcmp("queue_depth", attr->attr.name)
662             && shost->hostt->change_queue_depth)
663                 return &sdev_attr_queue_depth_rw;
664         else if (!strcmp("queue_type", attr->attr.name)
665             && shost->hostt->change_queue_type)
666                 return &sdev_attr_queue_type_rw;
667         return attr;
668 }
669
670
671 static struct device_attribute *attr_overridden(
672                 struct device_attribute **attrs,
673                 struct device_attribute *attr)
674 {
675         int i;
676
677         if (!attrs)
678                 return NULL;
679         for (i = 0; attrs[i]; i++)
680                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
681                         return attrs[i];
682         return NULL;
683 }
684
685 static int attr_add(struct device *dev, struct device_attribute *attr)
686 {
687         struct device_attribute *base_attr;
688
689         /*
690          * Spare the caller from having to copy things it's not interested in.
691          */
692         base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
693         if (base_attr) {
694                 /* extend permissions */
695                 attr->attr.mode |= base_attr->attr.mode;
696
697                 /* override null show/store with default */
698                 if (!attr->show)
699                         attr->show = base_attr->show;
700                 if (!attr->store)
701                         attr->store = base_attr->store;
702         }
703
704         return device_create_file(dev, attr);
705 }
706
707 /**
708  * scsi_sysfs_add_sdev - add scsi device to sysfs
709  * @sdev:       scsi_device to add
710  *
711  * Return value:
712  *      0 on Success / non-zero on Failure
713  **/
714 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
715 {
716         int error, i;
717
718         if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
719                 return error;
720
721         error = device_add(&sdev->sdev_gendev);
722         if (error) {
723                 put_device(sdev->sdev_gendev.parent);
724                 printk(KERN_INFO "error 1\n");
725                 return error;
726         }
727         error = class_device_add(&sdev->sdev_classdev);
728         if (error) {
729                 printk(KERN_INFO "error 2\n");
730                 goto clean_device;
731         }
732
733         /* take a reference for the sdev_classdev; this is
734          * released by the sdev_class .release */
735         get_device(&sdev->sdev_gendev);
736         if (sdev->host->hostt->sdev_attrs) {
737                 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
738                         error = attr_add(&sdev->sdev_gendev,
739                                         sdev->host->hostt->sdev_attrs[i]);
740                         if (error) {
741                                 __scsi_remove_device(sdev);
742                                 goto out;
743                         }
744                 }
745         }
746         
747         for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
748                 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
749                                         scsi_sysfs_sdev_attrs[i])) {
750                         struct device_attribute * attr = 
751                                 attr_changed_internally(sdev->host, 
752                                                         scsi_sysfs_sdev_attrs[i]);
753                         error = device_create_file(&sdev->sdev_gendev, attr);
754                         if (error) {
755                                 __scsi_remove_device(sdev);
756                                 goto out;
757                         }
758                 }
759         }
760
761         transport_add_device(&sdev->sdev_gendev);
762  out:
763         return error;
764
765  clean_device:
766         scsi_device_set_state(sdev, SDEV_CANCEL);
767
768         device_del(&sdev->sdev_gendev);
769         transport_destroy_device(&sdev->sdev_gendev);
770         put_device(&sdev->sdev_gendev);
771
772         return error;
773 }
774
775 void __scsi_remove_device(struct scsi_device *sdev)
776 {
777         struct device *dev = &sdev->sdev_gendev;
778
779         if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
780                 return;
781
782         class_device_unregister(&sdev->sdev_classdev);
783         transport_remove_device(dev);
784         device_del(dev);
785         scsi_device_set_state(sdev, SDEV_DEL);
786         if (sdev->host->hostt->slave_destroy)
787                 sdev->host->hostt->slave_destroy(sdev);
788         transport_destroy_device(dev);
789         put_device(dev);
790 }
791
792 /**
793  * scsi_remove_device - unregister a device from the scsi bus
794  * @sdev:       scsi_device to unregister
795  **/
796 void scsi_remove_device(struct scsi_device *sdev)
797 {
798         struct Scsi_Host *shost = sdev->host;
799
800         mutex_lock(&shost->scan_mutex);
801         __scsi_remove_device(sdev);
802         mutex_unlock(&shost->scan_mutex);
803 }
804 EXPORT_SYMBOL(scsi_remove_device);
805
806 void __scsi_remove_target(struct scsi_target *starget)
807 {
808         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
809         unsigned long flags;
810         struct scsi_device *sdev;
811
812         spin_lock_irqsave(shost->host_lock, flags);
813         starget->reap_ref++;
814  restart:
815         list_for_each_entry(sdev, &shost->__devices, siblings) {
816                 if (sdev->channel != starget->channel ||
817                     sdev->id != starget->id ||
818                     sdev->sdev_state == SDEV_DEL)
819                         continue;
820                 spin_unlock_irqrestore(shost->host_lock, flags);
821                 scsi_remove_device(sdev);
822                 spin_lock_irqsave(shost->host_lock, flags);
823                 goto restart;
824         }
825         spin_unlock_irqrestore(shost->host_lock, flags);
826         scsi_target_reap(starget);
827 }
828
829 static int __remove_child (struct device * dev, void * data)
830 {
831         if (scsi_is_target_device(dev))
832                 __scsi_remove_target(to_scsi_target(dev));
833         return 0;
834 }
835
836 /**
837  * scsi_remove_target - try to remove a target and all its devices
838  * @dev: generic starget or parent of generic stargets to be removed
839  *
840  * Note: This is slightly racy.  It is possible that if the user
841  * requests the addition of another device then the target won't be
842  * removed.
843  */
844 void scsi_remove_target(struct device *dev)
845 {
846         struct device *rdev;
847
848         if (scsi_is_target_device(dev)) {
849                 __scsi_remove_target(to_scsi_target(dev));
850                 return;
851         }
852
853         rdev = get_device(dev);
854         device_for_each_child(dev, NULL, __remove_child);
855         put_device(rdev);
856 }
857 EXPORT_SYMBOL(scsi_remove_target);
858
859 int scsi_register_driver(struct device_driver *drv)
860 {
861         drv->bus = &scsi_bus_type;
862
863         return driver_register(drv);
864 }
865 EXPORT_SYMBOL(scsi_register_driver);
866
867 int scsi_register_interface(struct class_interface *intf)
868 {
869         intf->class = &sdev_class;
870
871         return class_interface_register(intf);
872 }
873 EXPORT_SYMBOL(scsi_register_interface);
874
875
876 static struct class_device_attribute *class_attr_overridden(
877                 struct class_device_attribute **attrs,
878                 struct class_device_attribute *attr)
879 {
880         int i;
881
882         if (!attrs)
883                 return NULL;
884         for (i = 0; attrs[i]; i++)
885                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
886                         return attrs[i];
887         return NULL;
888 }
889
890 static int class_attr_add(struct class_device *classdev,
891                 struct class_device_attribute *attr)
892 {
893         struct class_device_attribute *base_attr;
894
895         /*
896          * Spare the caller from having to copy things it's not interested in.
897          */
898         base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
899         if (base_attr) {
900                 /* extend permissions */
901                 attr->attr.mode |= base_attr->attr.mode;
902
903                 /* override null show/store with default */
904                 if (!attr->show)
905                         attr->show = base_attr->show;
906                 if (!attr->store)
907                         attr->store = base_attr->store;
908         }
909
910         return class_device_create_file(classdev, attr);
911 }
912
913 /**
914  * scsi_sysfs_add_host - add scsi host to subsystem
915  * @shost:     scsi host struct to add to subsystem
916  * @dev:       parent struct device pointer
917  **/
918 int scsi_sysfs_add_host(struct Scsi_Host *shost)
919 {
920         int error, i;
921
922         if (shost->hostt->shost_attrs) {
923                 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
924                         error = class_attr_add(&shost->shost_classdev,
925                                         shost->hostt->shost_attrs[i]);
926                         if (error)
927                                 return error;
928                 }
929         }
930
931         for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
932                 if (!class_attr_overridden(shost->hostt->shost_attrs,
933                                         scsi_sysfs_shost_attrs[i])) {
934                         error = class_device_create_file(&shost->shost_classdev,
935                                         scsi_sysfs_shost_attrs[i]);
936                         if (error)
937                                 return error;
938                 }
939         }
940
941         transport_register_device(&shost->shost_gendev);
942         return 0;
943 }
944
945 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
946 {
947         unsigned long flags;
948         struct Scsi_Host *shost = sdev->host;
949         struct scsi_target  *starget = sdev->sdev_target;
950
951         device_initialize(&sdev->sdev_gendev);
952         sdev->sdev_gendev.bus = &scsi_bus_type;
953         sdev->sdev_gendev.release = scsi_device_dev_release;
954         sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
955                 sdev->host->host_no, sdev->channel, sdev->id,
956                 sdev->lun);
957         
958         class_device_initialize(&sdev->sdev_classdev);
959         sdev->sdev_classdev.dev = &sdev->sdev_gendev;
960         sdev->sdev_classdev.class = &sdev_class;
961         snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
962                  "%d:%d:%d:%d", sdev->host->host_no,
963                  sdev->channel, sdev->id, sdev->lun);
964         sdev->scsi_level = starget->scsi_level;
965         transport_setup_device(&sdev->sdev_gendev);
966         spin_lock_irqsave(shost->host_lock, flags);
967         list_add_tail(&sdev->same_target_siblings, &starget->devices);
968         list_add_tail(&sdev->siblings, &shost->__devices);
969         spin_unlock_irqrestore(shost->host_lock, flags);
970 }
971
972 int scsi_is_sdev_device(const struct device *dev)
973 {
974         return dev->release == scsi_device_dev_release;
975 }
976 EXPORT_SYMBOL(scsi_is_sdev_device);
977
978 /* A blank transport template that is used in drivers that don't
979  * yet implement Transport Attributes */
980 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };