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