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