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