[POWERPC] enable sysrq in pmac32_defconfig
[linux-2.6/libata-dev.git] / drivers / scsi / scsi_sysfs.c
blobe7fe565b96de93ffb5354d4c86d36dfd46410806
1 /*
2 * scsi_sysfs.c
4 * SCSI sysfs interface routines.
6 * Created to pull SCSI mid layer sysfs routines into one file.
7 */
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/device.h>
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_device.h>
16 #include <scsi/scsi_host.h>
17 #include <scsi/scsi_tcq.h>
18 #include <scsi/scsi_transport.h>
20 #include "scsi_priv.h"
21 #include "scsi_logging.h"
23 static const struct {
24 enum scsi_device_state value;
25 char *name;
26 } sdev_states[] = {
27 { SDEV_CREATED, "created" },
28 { SDEV_RUNNING, "running" },
29 { SDEV_CANCEL, "cancel" },
30 { SDEV_DEL, "deleted" },
31 { SDEV_QUIESCE, "quiesce" },
32 { SDEV_OFFLINE, "offline" },
33 { SDEV_BLOCK, "blocked" },
36 const char *scsi_device_state_name(enum scsi_device_state state)
38 int i;
39 char *name = NULL;
41 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
42 if (sdev_states[i].value == state) {
43 name = sdev_states[i].name;
44 break;
47 return name;
50 static const struct {
51 enum scsi_host_state value;
52 char *name;
53 } shost_states[] = {
54 { SHOST_CREATED, "created" },
55 { SHOST_RUNNING, "running" },
56 { SHOST_CANCEL, "cancel" },
57 { SHOST_DEL, "deleted" },
58 { SHOST_RECOVERY, "recovery" },
59 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
60 { SHOST_DEL_RECOVERY, "deleted/recovery", },
62 const char *scsi_host_state_name(enum scsi_host_state state)
64 int i;
65 char *name = NULL;
67 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
68 if (shost_states[i].value == state) {
69 name = shost_states[i].name;
70 break;
73 return name;
76 static int check_set(unsigned int *val, char *src)
78 char *last;
80 if (strncmp(src, "-", 20) == 0) {
81 *val = SCAN_WILD_CARD;
82 } else {
84 * Doesn't check for int overflow
86 *val = simple_strtoul(src, &last, 0);
87 if (*last != '\0')
88 return 1;
90 return 0;
93 static int scsi_scan(struct Scsi_Host *shost, const char *str)
95 char s1[15], s2[15], s3[15], junk;
96 unsigned int channel, id, lun;
97 int res;
99 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
100 if (res != 3)
101 return -EINVAL;
102 if (check_set(&channel, s1))
103 return -EINVAL;
104 if (check_set(&id, s2))
105 return -EINVAL;
106 if (check_set(&lun, s3))
107 return -EINVAL;
108 if (shost->transportt->user_scan)
109 res = shost->transportt->user_scan(shost, channel, id, lun);
110 else
111 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
112 return res;
116 * shost_show_function: macro to create an attr function that can be used to
117 * show a non-bit field.
119 #define shost_show_function(name, field, format_string) \
120 static ssize_t \
121 show_##name (struct class_device *class_dev, char *buf) \
123 struct Scsi_Host *shost = class_to_shost(class_dev); \
124 return snprintf (buf, 20, format_string, shost->field); \
128 * shost_rd_attr: macro to create a function and attribute variable for a
129 * read only field.
131 #define shost_rd_attr2(name, field, format_string) \
132 shost_show_function(name, field, format_string) \
133 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
135 #define shost_rd_attr(field, format_string) \
136 shost_rd_attr2(field, field, format_string)
139 * Create the actual show/store functions and data structures.
142 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
143 size_t count)
145 struct Scsi_Host *shost = class_to_shost(class_dev);
146 int res;
148 res = scsi_scan(shost, buf);
149 if (res == 0)
150 res = count;
151 return res;
153 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
155 static ssize_t
156 store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
158 int i;
159 struct Scsi_Host *shost = class_to_shost(class_dev);
160 enum scsi_host_state state = 0;
162 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
163 const int len = strlen(shost_states[i].name);
164 if (strncmp(shost_states[i].name, buf, len) == 0 &&
165 buf[len] == '\n') {
166 state = shost_states[i].value;
167 break;
170 if (!state)
171 return -EINVAL;
173 if (scsi_host_set_state(shost, state))
174 return -EINVAL;
175 return count;
178 static ssize_t
179 show_shost_state(struct class_device *class_dev, char *buf)
181 struct Scsi_Host *shost = class_to_shost(class_dev);
182 const char *name = scsi_host_state_name(shost->shost_state);
184 if (!name)
185 return -EINVAL;
187 return snprintf(buf, 20, "%s\n", name);
190 static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
192 shost_rd_attr(unique_id, "%u\n");
193 shost_rd_attr(host_busy, "%hu\n");
194 shost_rd_attr(cmd_per_lun, "%hd\n");
195 shost_rd_attr(sg_tablesize, "%hu\n");
196 shost_rd_attr(unchecked_isa_dma, "%d\n");
197 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
199 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
200 &class_device_attr_unique_id,
201 &class_device_attr_host_busy,
202 &class_device_attr_cmd_per_lun,
203 &class_device_attr_sg_tablesize,
204 &class_device_attr_unchecked_isa_dma,
205 &class_device_attr_proc_name,
206 &class_device_attr_scan,
207 &class_device_attr_state,
208 NULL
211 static void scsi_device_cls_release(struct class_device *class_dev)
213 struct scsi_device *sdev;
215 sdev = class_to_sdev(class_dev);
216 put_device(&sdev->sdev_gendev);
219 static void scsi_device_dev_release_usercontext(void *data)
221 struct device *dev = data;
222 struct scsi_device *sdev;
223 struct device *parent;
224 struct scsi_target *starget;
225 unsigned long flags;
227 parent = dev->parent;
228 sdev = to_scsi_device(dev);
229 starget = to_scsi_target(parent);
231 spin_lock_irqsave(sdev->host->host_lock, flags);
232 starget->reap_ref++;
233 list_del(&sdev->siblings);
234 list_del(&sdev->same_target_siblings);
235 list_del(&sdev->starved_entry);
236 spin_unlock_irqrestore(sdev->host->host_lock, flags);
238 if (sdev->request_queue) {
239 sdev->request_queue->queuedata = NULL;
240 /* user context needed to free queue */
241 scsi_free_queue(sdev->request_queue);
242 /* temporary expedient, try to catch use of queue lock
243 * after free of sdev */
244 sdev->request_queue = NULL;
247 scsi_target_reap(scsi_target(sdev));
249 kfree(sdev->inquiry);
250 kfree(sdev);
252 if (parent)
253 put_device(parent);
256 static void scsi_device_dev_release(struct device *dev)
258 struct scsi_device *sdp = to_scsi_device(dev);
259 execute_in_process_context(scsi_device_dev_release_usercontext, dev,
260 &sdp->ew);
263 static struct class sdev_class = {
264 .name = "scsi_device",
265 .release = scsi_device_cls_release,
268 /* all probing is done in the individual ->probe routines */
269 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
271 struct scsi_device *sdp = to_scsi_device(dev);
272 if (sdp->no_uld_attach)
273 return 0;
274 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
277 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
279 struct scsi_device *sdev = to_scsi_device(dev);
280 struct scsi_host_template *sht = sdev->host->hostt;
281 int err;
283 err = scsi_device_quiesce(sdev);
284 if (err)
285 return err;
287 if (sht->suspend)
288 err = sht->suspend(sdev, state);
290 return err;
293 static int scsi_bus_resume(struct device * dev)
295 struct scsi_device *sdev = to_scsi_device(dev);
296 struct scsi_host_template *sht = sdev->host->hostt;
297 int err = 0;
299 if (sht->resume)
300 err = sht->resume(sdev);
302 scsi_device_resume(sdev);
303 return err;
306 struct bus_type scsi_bus_type = {
307 .name = "scsi",
308 .match = scsi_bus_match,
309 .suspend = scsi_bus_suspend,
310 .resume = scsi_bus_resume,
313 int scsi_sysfs_register(void)
315 int error;
317 error = bus_register(&scsi_bus_type);
318 if (!error) {
319 error = class_register(&sdev_class);
320 if (error)
321 bus_unregister(&scsi_bus_type);
324 return error;
327 void scsi_sysfs_unregister(void)
329 class_unregister(&sdev_class);
330 bus_unregister(&scsi_bus_type);
334 * sdev_show_function: macro to create an attr function that can be used to
335 * show a non-bit field.
337 #define sdev_show_function(field, format_string) \
338 static ssize_t \
339 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
341 struct scsi_device *sdev; \
342 sdev = to_scsi_device(dev); \
343 return snprintf (buf, 20, format_string, sdev->field); \
347 * sdev_rd_attr: macro to create a function and attribute variable for a
348 * read only field.
350 #define sdev_rd_attr(field, format_string) \
351 sdev_show_function(field, format_string) \
352 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
356 * sdev_rd_attr: create a function and attribute variable for a
357 * read/write field.
359 #define sdev_rw_attr(field, format_string) \
360 sdev_show_function(field, format_string) \
362 static ssize_t \
363 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
365 struct scsi_device *sdev; \
366 sdev = to_scsi_device(dev); \
367 snscanf (buf, 20, format_string, &sdev->field); \
368 return count; \
370 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
372 /* Currently we don't export bit fields, but we might in future,
373 * so leave this code in */
374 #if 0
376 * sdev_rd_attr: create a function and attribute variable for a
377 * read/write bit field.
379 #define sdev_rw_attr_bit(field) \
380 sdev_show_function(field, "%d\n") \
382 static ssize_t \
383 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
385 int ret; \
386 struct scsi_device *sdev; \
387 ret = scsi_sdev_check_buf_bit(buf); \
388 if (ret >= 0) { \
389 sdev = to_scsi_device(dev); \
390 sdev->field = ret; \
391 ret = count; \
393 return ret; \
395 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
398 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
399 * else return -EINVAL.
401 static int scsi_sdev_check_buf_bit(const char *buf)
403 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
404 if (buf[0] == '1')
405 return 1;
406 else if (buf[0] == '0')
407 return 0;
408 else
409 return -EINVAL;
410 } else
411 return -EINVAL;
413 #endif
415 * Create the actual show/store functions and data structures.
417 sdev_rd_attr (device_blocked, "%d\n");
418 sdev_rd_attr (queue_depth, "%d\n");
419 sdev_rd_attr (type, "%d\n");
420 sdev_rd_attr (scsi_level, "%d\n");
421 sdev_rd_attr (vendor, "%.8s\n");
422 sdev_rd_attr (model, "%.16s\n");
423 sdev_rd_attr (rev, "%.4s\n");
425 static ssize_t
426 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
428 struct scsi_device *sdev;
429 sdev = to_scsi_device(dev);
430 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
433 static ssize_t
434 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
436 struct scsi_device *sdev;
437 int timeout;
438 sdev = to_scsi_device(dev);
439 sscanf (buf, "%d\n", &timeout);
440 sdev->timeout = timeout * HZ;
441 return count;
443 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
445 static ssize_t
446 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
448 scsi_rescan_device(dev);
449 return count;
451 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
453 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
454 size_t count)
456 scsi_remove_device(to_scsi_device(dev));
457 return count;
459 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
461 static ssize_t
462 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
464 int i;
465 struct scsi_device *sdev = to_scsi_device(dev);
466 enum scsi_device_state state = 0;
468 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
469 const int len = strlen(sdev_states[i].name);
470 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
471 buf[len] == '\n') {
472 state = sdev_states[i].value;
473 break;
476 if (!state)
477 return -EINVAL;
479 if (scsi_device_set_state(sdev, state))
480 return -EINVAL;
481 return count;
484 static ssize_t
485 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
487 struct scsi_device *sdev = to_scsi_device(dev);
488 const char *name = scsi_device_state_name(sdev->sdev_state);
490 if (!name)
491 return -EINVAL;
493 return snprintf(buf, 20, "%s\n", name);
496 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
498 static ssize_t
499 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
501 struct scsi_device *sdev = to_scsi_device(dev);
502 const char *name = "none";
504 if (sdev->ordered_tags)
505 name = "ordered";
506 else if (sdev->simple_tags)
507 name = "simple";
509 return snprintf(buf, 20, "%s\n", name);
512 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
514 static ssize_t
515 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
517 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
520 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
522 #define show_sdev_iostat(field) \
523 static ssize_t \
524 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
526 struct scsi_device *sdev = to_scsi_device(dev); \
527 unsigned long long count = atomic_read(&sdev->field); \
528 return snprintf(buf, 20, "0x%llx\n", count); \
530 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
532 show_sdev_iostat(iorequest_cnt);
533 show_sdev_iostat(iodone_cnt);
534 show_sdev_iostat(ioerr_cnt);
537 /* Default template for device attributes. May NOT be modified */
538 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
539 &dev_attr_device_blocked,
540 &dev_attr_queue_depth,
541 &dev_attr_queue_type,
542 &dev_attr_type,
543 &dev_attr_scsi_level,
544 &dev_attr_vendor,
545 &dev_attr_model,
546 &dev_attr_rev,
547 &dev_attr_rescan,
548 &dev_attr_delete,
549 &dev_attr_state,
550 &dev_attr_timeout,
551 &dev_attr_iocounterbits,
552 &dev_attr_iorequest_cnt,
553 &dev_attr_iodone_cnt,
554 &dev_attr_ioerr_cnt,
555 NULL
558 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
559 size_t count)
561 int depth, retval;
562 struct scsi_device *sdev = to_scsi_device(dev);
563 struct scsi_host_template *sht = sdev->host->hostt;
565 if (!sht->change_queue_depth)
566 return -EINVAL;
568 depth = simple_strtoul(buf, NULL, 0);
570 if (depth < 1)
571 return -EINVAL;
573 retval = sht->change_queue_depth(sdev, depth);
574 if (retval < 0)
575 return retval;
577 return count;
580 static struct device_attribute sdev_attr_queue_depth_rw =
581 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
582 sdev_store_queue_depth_rw);
584 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
585 size_t count)
587 struct scsi_device *sdev = to_scsi_device(dev);
588 struct scsi_host_template *sht = sdev->host->hostt;
589 int tag_type = 0, retval;
590 int prev_tag_type = scsi_get_tag_type(sdev);
592 if (!sdev->tagged_supported || !sht->change_queue_type)
593 return -EINVAL;
595 if (strncmp(buf, "ordered", 7) == 0)
596 tag_type = MSG_ORDERED_TAG;
597 else if (strncmp(buf, "simple", 6) == 0)
598 tag_type = MSG_SIMPLE_TAG;
599 else if (strncmp(buf, "none", 4) != 0)
600 return -EINVAL;
602 if (tag_type == prev_tag_type)
603 return count;
605 retval = sht->change_queue_type(sdev, tag_type);
606 if (retval < 0)
607 return retval;
609 return count;
612 static struct device_attribute sdev_attr_queue_type_rw =
613 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
614 sdev_store_queue_type_rw);
616 static struct device_attribute *attr_changed_internally(
617 struct Scsi_Host *shost,
618 struct device_attribute * attr)
620 if (!strcmp("queue_depth", attr->attr.name)
621 && shost->hostt->change_queue_depth)
622 return &sdev_attr_queue_depth_rw;
623 else if (!strcmp("queue_type", attr->attr.name)
624 && shost->hostt->change_queue_type)
625 return &sdev_attr_queue_type_rw;
626 return attr;
630 static struct device_attribute *attr_overridden(
631 struct device_attribute **attrs,
632 struct device_attribute *attr)
634 int i;
636 if (!attrs)
637 return NULL;
638 for (i = 0; attrs[i]; i++)
639 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
640 return attrs[i];
641 return NULL;
644 static int attr_add(struct device *dev, struct device_attribute *attr)
646 struct device_attribute *base_attr;
649 * Spare the caller from having to copy things it's not interested in.
651 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
652 if (base_attr) {
653 /* extend permissions */
654 attr->attr.mode |= base_attr->attr.mode;
656 /* override null show/store with default */
657 if (!attr->show)
658 attr->show = base_attr->show;
659 if (!attr->store)
660 attr->store = base_attr->store;
663 return device_create_file(dev, attr);
667 * scsi_sysfs_add_sdev - add scsi device to sysfs
668 * @sdev: scsi_device to add
670 * Return value:
671 * 0 on Success / non-zero on Failure
673 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
675 int error, i;
677 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
678 return error;
680 error = device_add(&sdev->sdev_gendev);
681 if (error) {
682 put_device(sdev->sdev_gendev.parent);
683 printk(KERN_INFO "error 1\n");
684 return error;
686 error = class_device_add(&sdev->sdev_classdev);
687 if (error) {
688 printk(KERN_INFO "error 2\n");
689 goto clean_device;
692 /* take a reference for the sdev_classdev; this is
693 * released by the sdev_class .release */
694 get_device(&sdev->sdev_gendev);
695 if (sdev->host->hostt->sdev_attrs) {
696 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
697 error = attr_add(&sdev->sdev_gendev,
698 sdev->host->hostt->sdev_attrs[i]);
699 if (error) {
700 __scsi_remove_device(sdev);
701 goto out;
706 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
707 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
708 scsi_sysfs_sdev_attrs[i])) {
709 struct device_attribute * attr =
710 attr_changed_internally(sdev->host,
711 scsi_sysfs_sdev_attrs[i]);
712 error = device_create_file(&sdev->sdev_gendev, attr);
713 if (error) {
714 __scsi_remove_device(sdev);
715 goto out;
720 transport_add_device(&sdev->sdev_gendev);
721 out:
722 return error;
724 clean_device:
725 scsi_device_set_state(sdev, SDEV_CANCEL);
727 device_del(&sdev->sdev_gendev);
728 transport_destroy_device(&sdev->sdev_gendev);
729 put_device(&sdev->sdev_gendev);
731 return error;
734 void __scsi_remove_device(struct scsi_device *sdev)
736 struct device *dev = &sdev->sdev_gendev;
738 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
739 return;
741 class_device_unregister(&sdev->sdev_classdev);
742 transport_remove_device(dev);
743 device_del(dev);
744 scsi_device_set_state(sdev, SDEV_DEL);
745 if (sdev->host->hostt->slave_destroy)
746 sdev->host->hostt->slave_destroy(sdev);
747 transport_destroy_device(dev);
748 put_device(dev);
752 * scsi_remove_device - unregister a device from the scsi bus
753 * @sdev: scsi_device to unregister
755 void scsi_remove_device(struct scsi_device *sdev)
757 struct Scsi_Host *shost = sdev->host;
759 mutex_lock(&shost->scan_mutex);
760 __scsi_remove_device(sdev);
761 mutex_unlock(&shost->scan_mutex);
763 EXPORT_SYMBOL(scsi_remove_device);
765 void __scsi_remove_target(struct scsi_target *starget)
767 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
768 unsigned long flags;
769 struct scsi_device *sdev;
771 spin_lock_irqsave(shost->host_lock, flags);
772 starget->reap_ref++;
773 restart:
774 list_for_each_entry(sdev, &shost->__devices, siblings) {
775 if (sdev->channel != starget->channel ||
776 sdev->id != starget->id ||
777 sdev->sdev_state == SDEV_DEL)
778 continue;
779 spin_unlock_irqrestore(shost->host_lock, flags);
780 scsi_remove_device(sdev);
781 spin_lock_irqsave(shost->host_lock, flags);
782 goto restart;
784 spin_unlock_irqrestore(shost->host_lock, flags);
785 scsi_target_reap(starget);
788 static int __remove_child (struct device * dev, void * data)
790 if (scsi_is_target_device(dev))
791 __scsi_remove_target(to_scsi_target(dev));
792 return 0;
796 * scsi_remove_target - try to remove a target and all its devices
797 * @dev: generic starget or parent of generic stargets to be removed
799 * Note: This is slightly racy. It is possible that if the user
800 * requests the addition of another device then the target won't be
801 * removed.
803 void scsi_remove_target(struct device *dev)
805 struct device *rdev;
807 if (scsi_is_target_device(dev)) {
808 __scsi_remove_target(to_scsi_target(dev));
809 return;
812 rdev = get_device(dev);
813 device_for_each_child(dev, NULL, __remove_child);
814 put_device(rdev);
816 EXPORT_SYMBOL(scsi_remove_target);
818 int scsi_register_driver(struct device_driver *drv)
820 drv->bus = &scsi_bus_type;
822 return driver_register(drv);
824 EXPORT_SYMBOL(scsi_register_driver);
826 int scsi_register_interface(struct class_interface *intf)
828 intf->class = &sdev_class;
830 return class_interface_register(intf);
832 EXPORT_SYMBOL(scsi_register_interface);
835 static struct class_device_attribute *class_attr_overridden(
836 struct class_device_attribute **attrs,
837 struct class_device_attribute *attr)
839 int i;
841 if (!attrs)
842 return NULL;
843 for (i = 0; attrs[i]; i++)
844 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
845 return attrs[i];
846 return NULL;
849 static int class_attr_add(struct class_device *classdev,
850 struct class_device_attribute *attr)
852 struct class_device_attribute *base_attr;
855 * Spare the caller from having to copy things it's not interested in.
857 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
858 if (base_attr) {
859 /* extend permissions */
860 attr->attr.mode |= base_attr->attr.mode;
862 /* override null show/store with default */
863 if (!attr->show)
864 attr->show = base_attr->show;
865 if (!attr->store)
866 attr->store = base_attr->store;
869 return class_device_create_file(classdev, attr);
873 * scsi_sysfs_add_host - add scsi host to subsystem
874 * @shost: scsi host struct to add to subsystem
875 * @dev: parent struct device pointer
877 int scsi_sysfs_add_host(struct Scsi_Host *shost)
879 int error, i;
881 if (shost->hostt->shost_attrs) {
882 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
883 error = class_attr_add(&shost->shost_classdev,
884 shost->hostt->shost_attrs[i]);
885 if (error)
886 return error;
890 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
891 if (!class_attr_overridden(shost->hostt->shost_attrs,
892 scsi_sysfs_shost_attrs[i])) {
893 error = class_device_create_file(&shost->shost_classdev,
894 scsi_sysfs_shost_attrs[i]);
895 if (error)
896 return error;
900 transport_register_device(&shost->shost_gendev);
901 return 0;
904 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
906 unsigned long flags;
907 struct Scsi_Host *shost = sdev->host;
908 struct scsi_target *starget = sdev->sdev_target;
910 device_initialize(&sdev->sdev_gendev);
911 sdev->sdev_gendev.bus = &scsi_bus_type;
912 sdev->sdev_gendev.release = scsi_device_dev_release;
913 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
914 sdev->host->host_no, sdev->channel, sdev->id,
915 sdev->lun);
917 class_device_initialize(&sdev->sdev_classdev);
918 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
919 sdev->sdev_classdev.class = &sdev_class;
920 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
921 "%d:%d:%d:%d", sdev->host->host_no,
922 sdev->channel, sdev->id, sdev->lun);
923 sdev->scsi_level = SCSI_2;
924 transport_setup_device(&sdev->sdev_gendev);
925 spin_lock_irqsave(shost->host_lock, flags);
926 list_add_tail(&sdev->same_target_siblings, &starget->devices);
927 list_add_tail(&sdev->siblings, &shost->__devices);
928 spin_unlock_irqrestore(shost->host_lock, flags);
931 int scsi_is_sdev_device(const struct device *dev)
933 return dev->release == scsi_device_dev_release;
935 EXPORT_SYMBOL(scsi_is_sdev_device);
937 /* A blank transport template that is used in drivers that don't
938 * yet implement Transport Attributes */
939 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };