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