vserver 1.9.5.x5
[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         unsigned long flags;
158         int delete;
159
160         parent = dev->parent;
161         sdev = to_scsi_device(dev);
162
163         spin_lock_irqsave(sdev->host->host_lock, flags);
164         /* If we're the last LUN on the target, destroy the target */
165         delete = list_empty(&sdev->same_target_siblings);
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 (delete) {
172                 struct scsi_target *starget = to_scsi_target(parent);
173                 if (!starget->create) {
174                         transport_remove_device(&starget->dev);
175                         device_del(parent);
176                 }
177                 transport_destroy_device(&starget->dev);
178
179                 put_device(parent);
180         }
181         if (sdev->request_queue)
182                 scsi_free_queue(sdev->request_queue);
183
184         kfree(sdev->inquiry);
185         kfree(sdev);
186
187         if (parent)
188                 put_device(parent);
189 }
190
191 struct class sdev_class = {
192         .name           = "scsi_device",
193         .release        = scsi_device_cls_release,
194 };
195
196 /* all probing is done in the individual ->probe routines */
197 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
198 {
199         struct scsi_device *sdp = to_scsi_device(dev);
200         if (sdp->no_uld_attach)
201                 return 0;
202         return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
203 }
204
205 struct bus_type scsi_bus_type = {
206         .name           = "scsi",
207         .match          = scsi_bus_match,
208 };
209
210 int scsi_sysfs_register(void)
211 {
212         int error;
213
214         error = bus_register(&scsi_bus_type);
215         if (!error) {
216                 error = class_register(&sdev_class);
217                 if (error)
218                         bus_unregister(&scsi_bus_type);
219         }
220
221         return error;
222 }
223
224 void scsi_sysfs_unregister(void)
225 {
226         class_unregister(&sdev_class);
227         bus_unregister(&scsi_bus_type);
228 }
229
230 /*
231  * sdev_show_function: macro to create an attr function that can be used to
232  * show a non-bit field.
233  */
234 #define sdev_show_function(field, format_string)                                \
235 static ssize_t                                                          \
236 sdev_show_##field (struct device *dev, char *buf)                               \
237 {                                                                       \
238         struct scsi_device *sdev;                                       \
239         sdev = to_scsi_device(dev);                                     \
240         return snprintf (buf, 20, format_string, sdev->field);          \
241 }                                                                       \
242
243 /*
244  * sdev_rd_attr: macro to create a function and attribute variable for a
245  * read only field.
246  */
247 #define sdev_rd_attr(field, format_string)                              \
248         sdev_show_function(field, format_string)                        \
249 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
250
251
252 /*
253  * sdev_rd_attr: create a function and attribute variable for a
254  * read/write field.
255  */
256 #define sdev_rw_attr(field, format_string)                              \
257         sdev_show_function(field, format_string)                                \
258                                                                         \
259 static ssize_t                                                          \
260 sdev_store_##field (struct device *dev, const char *buf, size_t count)  \
261 {                                                                       \
262         struct scsi_device *sdev;                                       \
263         sdev = to_scsi_device(dev);                                     \
264         snscanf (buf, 20, format_string, &sdev->field);                 \
265         return count;                                                   \
266 }                                                                       \
267 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
268
269 /* Currently we don't export bit fields, but we might in future,
270  * so leave this code in */
271 #if 0
272 /*
273  * sdev_rd_attr: create a function and attribute variable for a
274  * read/write bit field.
275  */
276 #define sdev_rw_attr_bit(field)                                         \
277         sdev_show_function(field, "%d\n")                                       \
278                                                                         \
279 static ssize_t                                                          \
280 sdev_store_##field (struct device *dev, const char *buf, size_t count)  \
281 {                                                                       \
282         int ret;                                                        \
283         struct scsi_device *sdev;                                       \
284         ret = scsi_sdev_check_buf_bit(buf);                             \
285         if (ret >= 0)   {                                               \
286                 sdev = to_scsi_device(dev);                             \
287                 sdev->field = ret;                                      \
288                 ret = count;                                            \
289         }                                                               \
290         return ret;                                                     \
291 }                                                                       \
292 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
293
294 /*
295  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
296  * else return -EINVAL.
297  */
298 static int scsi_sdev_check_buf_bit(const char *buf)
299 {
300         if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
301                 if (buf[0] == '1')
302                         return 1;
303                 else if (buf[0] == '0')
304                         return 0;
305                 else 
306                         return -EINVAL;
307         } else
308                 return -EINVAL;
309 }
310 #endif
311 /*
312  * Create the actual show/store functions and data structures.
313  */
314 sdev_rd_attr (device_blocked, "%d\n");
315 sdev_rd_attr (queue_depth, "%d\n");
316 sdev_rd_attr (type, "%d\n");
317 sdev_rd_attr (scsi_level, "%d\n");
318 sdev_rd_attr (vendor, "%.8s\n");
319 sdev_rd_attr (model, "%.16s\n");
320 sdev_rd_attr (rev, "%.4s\n");
321
322 static ssize_t
323 sdev_show_timeout (struct device *dev, char *buf)
324 {
325         struct scsi_device *sdev;
326         sdev = to_scsi_device(dev);
327         return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
328 }
329
330 static ssize_t
331 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
332 {
333         struct scsi_device *sdev;
334         int timeout;
335         sdev = to_scsi_device(dev);
336         sscanf (buf, "%d\n", &timeout);
337         sdev->timeout = timeout * HZ;
338         return count;
339 }
340 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
341
342 static ssize_t
343 store_rescan_field (struct device *dev, const char *buf, size_t count) 
344 {
345         scsi_rescan_device(dev);
346         return count;
347 }
348 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
349
350 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
351                                  size_t count)
352 {
353         scsi_remove_device(to_scsi_device(dev));
354         return count;
355 };
356 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
357
358 static ssize_t
359 store_state_field(struct device *dev, const char *buf, size_t count)
360 {
361         int i;
362         struct scsi_device *sdev = to_scsi_device(dev);
363         enum scsi_device_state state = 0;
364
365         for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
366                 const int len = strlen(sdev_states[i].name);
367                 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
368                    buf[len] == '\n') {
369                         state = sdev_states[i].value;
370                         break;
371                 }
372         }
373         if (!state)
374                 return -EINVAL;
375
376         if (scsi_device_set_state(sdev, state))
377                 return -EINVAL;
378         return count;
379 }
380
381 static ssize_t
382 show_state_field(struct device *dev, char *buf)
383 {
384         struct scsi_device *sdev = to_scsi_device(dev);
385         const char *name = scsi_device_state_name(sdev->sdev_state);
386
387         if (!name)
388                 return -EINVAL;
389
390         return snprintf(buf, 20, "%s\n", name);
391 }
392
393 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
394
395 static ssize_t
396 show_queue_type_field(struct device *dev, char *buf)
397 {
398         struct scsi_device *sdev = to_scsi_device(dev);
399         const char *name = "none";
400
401         if (sdev->ordered_tags)
402                 name = "ordered";
403         else if (sdev->simple_tags)
404                 name = "simple";
405
406         return snprintf(buf, 20, "%s\n", name);
407 }
408
409 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
410
411
412 /* Default template for device attributes.  May NOT be modified */
413 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
414         &dev_attr_device_blocked,
415         &dev_attr_queue_depth,
416         &dev_attr_queue_type,
417         &dev_attr_type,
418         &dev_attr_scsi_level,
419         &dev_attr_vendor,
420         &dev_attr_model,
421         &dev_attr_rev,
422         &dev_attr_rescan,
423         &dev_attr_delete,
424         &dev_attr_state,
425         &dev_attr_timeout,
426         NULL
427 };
428
429 static ssize_t sdev_store_queue_depth_rw(struct device *dev, const char *buf,
430                                          size_t count)
431 {
432         int depth, retval;
433         struct scsi_device *sdev = to_scsi_device(dev);
434         struct scsi_host_template *sht = sdev->host->hostt;
435
436         if (!sht->change_queue_depth)
437                 return -EINVAL;
438
439         depth = simple_strtoul(buf, NULL, 0);
440
441         if (depth < 1)
442                 return -EINVAL;
443
444         retval = sht->change_queue_depth(sdev, depth);
445         if (retval < 0)
446                 return retval;
447
448         return count;
449 }
450
451 static struct device_attribute sdev_attr_queue_depth_rw =
452         __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
453                sdev_store_queue_depth_rw);
454
455 static ssize_t sdev_store_queue_type_rw(struct device *dev, const char *buf,
456                                         size_t count)
457 {
458         struct scsi_device *sdev = to_scsi_device(dev);
459         struct scsi_host_template *sht = sdev->host->hostt;
460         int tag_type = 0, retval;
461         int prev_tag_type = scsi_get_tag_type(sdev);
462
463         if (!sdev->tagged_supported || !sht->change_queue_type)
464                 return -EINVAL;
465
466         if (strncmp(buf, "ordered", 7) == 0)
467                 tag_type = MSG_ORDERED_TAG;
468         else if (strncmp(buf, "simple", 6) == 0)
469                 tag_type = MSG_SIMPLE_TAG;
470         else if (strncmp(buf, "none", 4) != 0)
471                 return -EINVAL;
472
473         if (tag_type == prev_tag_type)
474                 return count;
475
476         retval = sht->change_queue_type(sdev, tag_type);
477         if (retval < 0)
478                 return retval;
479
480         return count;
481 }
482
483 static struct device_attribute sdev_attr_queue_type_rw =
484         __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
485                sdev_store_queue_type_rw);
486
487 static struct device_attribute *attr_changed_internally(
488                 struct Scsi_Host *shost,
489                 struct device_attribute * attr)
490 {
491         if (!strcmp("queue_depth", attr->attr.name)
492             && shost->hostt->change_queue_depth)
493                 return &sdev_attr_queue_depth_rw;
494         else if (!strcmp("queue_type", attr->attr.name)
495             && shost->hostt->change_queue_type)
496                 return &sdev_attr_queue_type_rw;
497         return attr;
498 }
499
500
501 static struct device_attribute *attr_overridden(
502                 struct device_attribute **attrs,
503                 struct device_attribute *attr)
504 {
505         int i;
506
507         if (!attrs)
508                 return NULL;
509         for (i = 0; attrs[i]; i++)
510                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
511                         return attrs[i];
512         return NULL;
513 }
514
515 static int attr_add(struct device *dev, struct device_attribute *attr)
516 {
517         struct device_attribute *base_attr;
518
519         /*
520          * Spare the caller from having to copy things it's not interested in.
521          */
522         base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
523         if (base_attr) {
524                 /* extend permissions */
525                 attr->attr.mode |= base_attr->attr.mode;
526
527                 /* override null show/store with default */
528                 if (!attr->show)
529                         attr->show = base_attr->show;
530                 if (!attr->store)
531                         attr->store = base_attr->store;
532         }
533
534         return device_create_file(dev, attr);
535 }
536
537 static void scsi_target_dev_release(struct device *dev)
538 {
539         struct scsi_target *starget = to_scsi_target(dev);
540         struct device *parent = dev->parent;
541         kfree(starget);
542         put_device(parent);
543 }
544
545 /**
546  * scsi_sysfs_add_sdev - add scsi device to sysfs
547  * @sdev:       scsi_device to add
548  *
549  * Return value:
550  *      0 on Success / non-zero on Failure
551  **/
552 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
553 {
554         struct scsi_target *starget = sdev->sdev_target;
555         struct Scsi_Host *shost = sdev->host;
556         int error, i, create;
557         unsigned long flags;
558
559         spin_lock_irqsave(shost->host_lock, flags);
560         create = starget->create;
561         starget->create = 0;
562         spin_unlock_irqrestore(shost->host_lock, flags);
563
564         if (create) {
565                 error = device_add(&starget->dev);
566                 if (error) {
567                         printk(KERN_ERR "Target device_add failed\n");
568                         return error;
569                 }
570                 transport_add_device(&starget->dev);
571         }
572
573         if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
574                 return error;
575
576         error = device_add(&sdev->sdev_gendev);
577         if (error) {
578                 put_device(sdev->sdev_gendev.parent);
579                 printk(KERN_INFO "error 1\n");
580                 return error;
581         }
582         error = class_device_add(&sdev->sdev_classdev);
583         if (error) {
584                 printk(KERN_INFO "error 2\n");
585                 goto clean_device;
586         }
587
588         /* take a reference for the sdev_classdev; this is
589          * released by the sdev_class .release */
590         get_device(&sdev->sdev_gendev);
591         if (sdev->host->hostt->sdev_attrs) {
592                 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
593                         error = attr_add(&sdev->sdev_gendev,
594                                         sdev->host->hostt->sdev_attrs[i]);
595                         if (error) {
596                                 scsi_remove_device(sdev);
597                                 goto out;
598                         }
599                 }
600         }
601         
602         for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
603                 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
604                                         scsi_sysfs_sdev_attrs[i])) {
605                         struct device_attribute * attr = 
606                                 attr_changed_internally(sdev->host, 
607                                                         scsi_sysfs_sdev_attrs[i]);
608                         error = device_create_file(&sdev->sdev_gendev, attr);
609                         if (error) {
610                                 scsi_remove_device(sdev);
611                                 goto out;
612                         }
613                 }
614         }
615
616         transport_add_device(&sdev->sdev_gendev);
617  out:
618         return error;
619
620         class_device_del(&sdev->sdev_classdev);
621  clean_device:
622         scsi_device_set_state(sdev, SDEV_CANCEL);
623
624         device_del(&sdev->sdev_gendev);
625         transport_destroy_device(&sdev->sdev_gendev);
626         put_device(&sdev->sdev_gendev);
627
628         return error;
629 }
630
631 /**
632  * scsi_remove_device - unregister a device from the scsi bus
633  * @sdev:       scsi_device to unregister
634  **/
635 void scsi_remove_device(struct scsi_device *sdev)
636 {
637         struct Scsi_Host *shost = sdev->host;
638
639         down(&shost->scan_mutex);
640         if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
641                 goto out;
642
643         class_device_unregister(&sdev->sdev_classdev);
644         device_del(&sdev->sdev_gendev);
645         scsi_device_set_state(sdev, SDEV_DEL);
646         if (sdev->host->hostt->slave_destroy)
647                 sdev->host->hostt->slave_destroy(sdev);
648         transport_unregister_device(&sdev->sdev_gendev);
649         put_device(&sdev->sdev_gendev);
650
651 out:
652         up(&shost->scan_mutex);
653 }
654 EXPORT_SYMBOL(scsi_remove_device);
655
656 int scsi_register_driver(struct device_driver *drv)
657 {
658         drv->bus = &scsi_bus_type;
659
660         return driver_register(drv);
661 }
662 EXPORT_SYMBOL(scsi_register_driver);
663
664 int scsi_register_interface(struct class_interface *intf)
665 {
666         intf->class = &sdev_class;
667
668         return class_interface_register(intf);
669 }
670 EXPORT_SYMBOL(scsi_register_interface);
671
672
673 static struct class_device_attribute *class_attr_overridden(
674                 struct class_device_attribute **attrs,
675                 struct class_device_attribute *attr)
676 {
677         int i;
678
679         if (!attrs)
680                 return NULL;
681         for (i = 0; attrs[i]; i++)
682                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
683                         return attrs[i];
684         return NULL;
685 }
686
687 static int class_attr_add(struct class_device *classdev,
688                 struct class_device_attribute *attr)
689 {
690         struct class_device_attribute *base_attr;
691
692         /*
693          * Spare the caller from having to copy things it's not interested in.
694          */
695         base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
696         if (base_attr) {
697                 /* extend permissions */
698                 attr->attr.mode |= base_attr->attr.mode;
699
700                 /* override null show/store with default */
701                 if (!attr->show)
702                         attr->show = base_attr->show;
703                 if (!attr->store)
704                         attr->store = base_attr->store;
705         }
706
707         return class_device_create_file(classdev, attr);
708 }
709
710 /**
711  * scsi_sysfs_add_host - add scsi host to subsystem
712  * @shost:     scsi host struct to add to subsystem
713  * @dev:       parent struct device pointer
714  **/
715 int scsi_sysfs_add_host(struct Scsi_Host *shost)
716 {
717         int error, i;
718
719         if (shost->hostt->shost_attrs) {
720                 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
721                         error = class_attr_add(&shost->shost_classdev,
722                                         shost->hostt->shost_attrs[i]);
723                         if (error)
724                                 return error;
725                 }
726         }
727
728         for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
729                 if (!class_attr_overridden(shost->hostt->shost_attrs,
730                                         scsi_sysfs_shost_attrs[i])) {
731                         error = class_device_create_file(&shost->shost_classdev,
732                                         scsi_sysfs_shost_attrs[i]);
733                         if (error)
734                                 return error;
735                 }
736         }
737
738         transport_register_device(&shost->shost_gendev);
739         return 0;
740 }
741
742 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
743 {
744         device_initialize(&sdev->sdev_gendev);
745         sdev->sdev_gendev.bus = &scsi_bus_type;
746         sdev->sdev_gendev.release = scsi_device_dev_release;
747         sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
748                 sdev->host->host_no, sdev->channel, sdev->id,
749                 sdev->lun);
750         
751         class_device_initialize(&sdev->sdev_classdev);
752         sdev->sdev_classdev.dev = &sdev->sdev_gendev;
753         sdev->sdev_classdev.class = &sdev_class;
754         snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
755                  "%d:%d:%d:%d", sdev->host->host_no,
756                  sdev->channel, sdev->id, sdev->lun);
757
758         transport_setup_device(&sdev->sdev_gendev);
759 }
760
761 int scsi_is_sdev_device(const struct device *dev)
762 {
763         return dev->release == scsi_device_dev_release;
764 }
765 EXPORT_SYMBOL(scsi_is_sdev_device);
766
767 int scsi_sysfs_target_initialize(struct scsi_device *sdev)
768 {
769         struct scsi_target *starget = NULL;
770         struct Scsi_Host *shost = sdev->host;
771         struct scsi_device *device;
772         struct device *dev = NULL;
773         unsigned long flags;
774         int create = 0;
775
776         spin_lock_irqsave(shost->host_lock, flags);
777         /*
778          * Search for an existing target for this sdev.
779          */
780         list_for_each_entry(device, &shost->__devices, siblings) {
781                 if (device->id == sdev->id &&
782                     device->channel == sdev->channel) {
783                         list_add_tail(&sdev->same_target_siblings,
784                                       &device->same_target_siblings);
785                         sdev->scsi_level = device->scsi_level;
786                         starget = device->sdev_target;
787                         break;
788                 }
789         }
790                         
791         if (!starget) {
792                 const int size = sizeof(*starget) +
793                         shost->transportt->target_size;
794                 starget = kmalloc(size, GFP_ATOMIC);
795                 if (!starget) {
796                         printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
797                         spin_unlock_irqrestore(shost->host_lock,
798                                                flags);
799                         return -ENOMEM;
800                 }
801                 memset(starget, 0, size);
802                 dev = &starget->dev;
803                 device_initialize(dev);
804                 dev->parent = get_device(&shost->shost_gendev);
805                 dev->release = scsi_target_dev_release;
806                 sprintf(dev->bus_id, "target%d:%d:%d",
807                         shost->host_no, sdev->channel, sdev->id);
808                 starget->id = sdev->id;
809                 starget->channel = sdev->channel;
810                 create = starget->create = 1;
811                 /*
812                  * If there wasn't another lun already configured at
813                  * this target, then default this device to SCSI_2
814                  * until we know better
815                  */
816                 sdev->scsi_level = SCSI_2;
817         }
818         get_device(&starget->dev);
819         sdev->sdev_gendev.parent = &starget->dev;
820         sdev->sdev_target = starget;
821         list_add_tail(&sdev->siblings, &shost->__devices);
822         spin_unlock_irqrestore(shost->host_lock, flags);
823         if (create)
824                 transport_setup_device(&starget->dev);
825         return 0;
826 }
827
828 int scsi_is_target_device(const struct device *dev)
829 {
830         return dev->release == scsi_target_dev_release;
831 }
832 EXPORT_SYMBOL(scsi_is_target_device);
833
834 /* A blank transport template that is used in drivers that don't
835  * yet implement Transport Attributes */
836 struct scsi_transport_template blank_transport_template = { NULL, };