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