4 * SCSI sysfs interface routines.
6 * Created to pull SCSI mid layer sysfs routines into one file.
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"
24 enum scsi_device_state value
;
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
)
41 for (i
= 0; i
< ARRAY_SIZE(sdev_states
); i
++) {
42 if (sdev_states
[i
].value
== state
) {
43 name
= sdev_states
[i
].name
;
51 enum scsi_host_state value
;
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
)
67 for (i
= 0; i
< ARRAY_SIZE(shost_states
); i
++) {
68 if (shost_states
[i
].value
== state
) {
69 name
= shost_states
[i
].name
;
76 static int check_set(unsigned int *val
, char *src
)
80 if (strncmp(src
, "-", 20) == 0) {
81 *val
= SCAN_WILD_CARD
;
84 * Doesn't check for int overflow
86 *val
= simple_strtoul(src
, &last
, 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
;
99 res
= sscanf(str
, "%10s %10s %10s %c", s1
, s2
, s3
, &junk
);
102 if (check_set(&channel
, s1
))
104 if (check_set(&id
, s2
))
106 if (check_set(&lun
, s3
))
108 if (shost
->transportt
->user_scan
)
109 res
= shost
->transportt
->user_scan(shost
, channel
, id
, lun
);
111 res
= scsi_scan_host_selected(shost
, channel
, id
, lun
, 1);
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) \
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
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
,
145 struct Scsi_Host
*shost
= class_to_shost(class_dev
);
148 res
= scsi_scan(shost
, buf
);
153 static CLASS_DEVICE_ATTR(scan
, S_IWUSR
, NULL
, store_scan
);
156 store_shost_state(struct class_device
*class_dev
, const char *buf
, size_t count
)
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 &&
166 state
= shost_states
[i
].value
;
173 if (scsi_host_set_state(shost
, state
))
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
);
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(can_queue
, "%hd\n");
196 shost_rd_attr(sg_tablesize
, "%hu\n");
197 shost_rd_attr(unchecked_isa_dma
, "%d\n");
198 shost_rd_attr2(proc_name
, hostt
->proc_name
, "%s\n");
200 static struct class_device_attribute
*scsi_sysfs_shost_attrs
[] = {
201 &class_device_attr_unique_id
,
202 &class_device_attr_host_busy
,
203 &class_device_attr_cmd_per_lun
,
204 &class_device_attr_can_queue
,
205 &class_device_attr_sg_tablesize
,
206 &class_device_attr_unchecked_isa_dma
,
207 &class_device_attr_proc_name
,
208 &class_device_attr_scan
,
209 &class_device_attr_state
,
213 static void scsi_device_cls_release(struct class_device
*class_dev
)
215 struct scsi_device
*sdev
;
217 sdev
= class_to_sdev(class_dev
);
218 put_device(&sdev
->sdev_gendev
);
221 static void scsi_device_dev_release_usercontext(struct work_struct
*work
)
223 struct scsi_device
*sdev
;
224 struct device
*parent
;
225 struct scsi_target
*starget
;
228 sdev
= container_of(work
, struct scsi_device
, ew
.work
);
230 parent
= sdev
->sdev_gendev
.parent
;
231 starget
= to_scsi_target(parent
);
233 spin_lock_irqsave(sdev
->host
->host_lock
, flags
);
235 list_del(&sdev
->siblings
);
236 list_del(&sdev
->same_target_siblings
);
237 list_del(&sdev
->starved_entry
);
238 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
240 if (sdev
->request_queue
) {
241 sdev
->request_queue
->queuedata
= NULL
;
242 /* user context needed to free queue */
243 scsi_free_queue(sdev
->request_queue
);
244 /* temporary expedient, try to catch use of queue lock
245 * after free of sdev */
246 sdev
->request_queue
= NULL
;
249 scsi_target_reap(scsi_target(sdev
));
251 kfree(sdev
->inquiry
);
258 static void scsi_device_dev_release(struct device
*dev
)
260 struct scsi_device
*sdp
= to_scsi_device(dev
);
261 execute_in_process_context(scsi_device_dev_release_usercontext
,
265 static struct class sdev_class
= {
266 .name
= "scsi_device",
267 .release
= scsi_device_cls_release
,
270 /* all probing is done in the individual ->probe routines */
271 static int scsi_bus_match(struct device
*dev
, struct device_driver
*gendrv
)
273 struct scsi_device
*sdp
= to_scsi_device(dev
);
274 if (sdp
->no_uld_attach
)
276 return (sdp
->inq_periph_qual
== SCSI_INQ_PQ_CON
)? 1: 0;
279 static int scsi_bus_uevent(struct device
*dev
, char **envp
, int num_envp
,
280 char *buffer
, int buffer_size
)
282 struct scsi_device
*sdev
= to_scsi_device(dev
);
286 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &length
,
287 "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT
, sdev
->type
);
292 static int scsi_bus_suspend(struct device
* dev
, pm_message_t state
)
294 struct device_driver
*drv
= dev
->driver
;
295 struct scsi_device
*sdev
= to_scsi_device(dev
);
296 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
299 err
= scsi_device_quiesce(sdev
);
303 /* call HLD suspend first */
304 if (drv
&& drv
->suspend
) {
305 err
= drv
->suspend(dev
, state
);
310 /* then, call host suspend */
312 err
= sht
->suspend(sdev
, state
);
314 if (drv
&& drv
->resume
)
323 static int scsi_bus_resume(struct device
* dev
)
325 struct device_driver
*drv
= dev
->driver
;
326 struct scsi_device
*sdev
= to_scsi_device(dev
);
327 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
328 int err
= 0, err2
= 0;
330 /* call host resume first */
332 err
= sht
->resume(sdev
);
334 /* then, call HLD resume */
335 if (drv
&& drv
->resume
)
336 err2
= drv
->resume(dev
);
338 scsi_device_resume(sdev
);
340 /* favor LLD failure */
341 return err
? err
: err2
;;
344 struct bus_type scsi_bus_type
= {
346 .match
= scsi_bus_match
,
347 .uevent
= scsi_bus_uevent
,
348 .suspend
= scsi_bus_suspend
,
349 .resume
= scsi_bus_resume
,
352 int scsi_sysfs_register(void)
356 error
= bus_register(&scsi_bus_type
);
358 error
= class_register(&sdev_class
);
360 bus_unregister(&scsi_bus_type
);
366 void scsi_sysfs_unregister(void)
368 class_unregister(&sdev_class
);
369 bus_unregister(&scsi_bus_type
);
373 * sdev_show_function: macro to create an attr function that can be used to
374 * show a non-bit field.
376 #define sdev_show_function(field, format_string) \
378 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
380 struct scsi_device *sdev; \
381 sdev = to_scsi_device(dev); \
382 return snprintf (buf, 20, format_string, sdev->field); \
386 * sdev_rd_attr: macro to create a function and attribute variable for a
389 #define sdev_rd_attr(field, format_string) \
390 sdev_show_function(field, format_string) \
391 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
395 * sdev_rd_attr: create a function and attribute variable for a
398 #define sdev_rw_attr(field, format_string) \
399 sdev_show_function(field, format_string) \
402 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
404 struct scsi_device *sdev; \
405 sdev = to_scsi_device(dev); \
406 snscanf (buf, 20, format_string, &sdev->field); \
409 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
411 /* Currently we don't export bit fields, but we might in future,
412 * so leave this code in */
415 * sdev_rd_attr: create a function and attribute variable for a
416 * read/write bit field.
418 #define sdev_rw_attr_bit(field) \
419 sdev_show_function(field, "%d\n") \
422 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
425 struct scsi_device *sdev; \
426 ret = scsi_sdev_check_buf_bit(buf); \
428 sdev = to_scsi_device(dev); \
434 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
437 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
438 * else return -EINVAL.
440 static int scsi_sdev_check_buf_bit(const char *buf
)
442 if ((buf
[1] == '\0') || ((buf
[1] == '\n') && (buf
[2] == '\0'))) {
445 else if (buf
[0] == '0')
454 * Create the actual show/store functions and data structures.
456 sdev_rd_attr (device_blocked
, "%d\n");
457 sdev_rd_attr (queue_depth
, "%d\n");
458 sdev_rd_attr (type
, "%d\n");
459 sdev_rd_attr (scsi_level
, "%d\n");
460 sdev_rd_attr (vendor
, "%.8s\n");
461 sdev_rd_attr (model
, "%.16s\n");
462 sdev_rd_attr (rev
, "%.4s\n");
465 sdev_show_timeout (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
467 struct scsi_device
*sdev
;
468 sdev
= to_scsi_device(dev
);
469 return snprintf (buf
, 20, "%d\n", sdev
->timeout
/ HZ
);
473 sdev_store_timeout (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
475 struct scsi_device
*sdev
;
477 sdev
= to_scsi_device(dev
);
478 sscanf (buf
, "%d\n", &timeout
);
479 sdev
->timeout
= timeout
* HZ
;
482 static DEVICE_ATTR(timeout
, S_IRUGO
| S_IWUSR
, sdev_show_timeout
, sdev_store_timeout
);
485 store_rescan_field (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
487 scsi_rescan_device(dev
);
490 static DEVICE_ATTR(rescan
, S_IWUSR
, NULL
, store_rescan_field
);
492 static void sdev_store_delete_callback(struct device
*dev
)
494 scsi_remove_device(to_scsi_device(dev
));
497 static ssize_t
sdev_store_delete(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
502 /* An attribute cannot be unregistered by one of its own methods,
503 * so we have to use this roundabout approach.
505 rc
= device_schedule_callback(dev
, sdev_store_delete_callback
);
510 static DEVICE_ATTR(delete, S_IWUSR
, NULL
, sdev_store_delete
);
513 store_state_field(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
516 struct scsi_device
*sdev
= to_scsi_device(dev
);
517 enum scsi_device_state state
= 0;
519 for (i
= 0; i
< ARRAY_SIZE(sdev_states
); i
++) {
520 const int len
= strlen(sdev_states
[i
].name
);
521 if (strncmp(sdev_states
[i
].name
, buf
, len
) == 0 &&
523 state
= sdev_states
[i
].value
;
530 if (scsi_device_set_state(sdev
, state
))
536 show_state_field(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
538 struct scsi_device
*sdev
= to_scsi_device(dev
);
539 const char *name
= scsi_device_state_name(sdev
->sdev_state
);
544 return snprintf(buf
, 20, "%s\n", name
);
547 static DEVICE_ATTR(state
, S_IRUGO
| S_IWUSR
, show_state_field
, store_state_field
);
550 show_queue_type_field(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
552 struct scsi_device
*sdev
= to_scsi_device(dev
);
553 const char *name
= "none";
555 if (sdev
->ordered_tags
)
557 else if (sdev
->simple_tags
)
560 return snprintf(buf
, 20, "%s\n", name
);
563 static DEVICE_ATTR(queue_type
, S_IRUGO
, show_queue_type_field
, NULL
);
566 show_iostat_counterbits(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
568 return snprintf(buf
, 20, "%d\n", (int)sizeof(atomic_t
) * 8);
571 static DEVICE_ATTR(iocounterbits
, S_IRUGO
, show_iostat_counterbits
, NULL
);
573 #define show_sdev_iostat(field) \
575 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
577 struct scsi_device *sdev = to_scsi_device(dev); \
578 unsigned long long count = atomic_read(&sdev->field); \
579 return snprintf(buf, 20, "0x%llx\n", count); \
581 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
583 show_sdev_iostat(iorequest_cnt
);
584 show_sdev_iostat(iodone_cnt
);
585 show_sdev_iostat(ioerr_cnt
);
588 sdev_show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
590 struct scsi_device
*sdev
;
591 sdev
= to_scsi_device(dev
);
592 return snprintf (buf
, 20, SCSI_DEVICE_MODALIAS_FMT
"\n", sdev
->type
);
594 static DEVICE_ATTR(modalias
, S_IRUGO
, sdev_show_modalias
, NULL
);
596 /* Default template for device attributes. May NOT be modified */
597 static struct device_attribute
*scsi_sysfs_sdev_attrs
[] = {
598 &dev_attr_device_blocked
,
599 &dev_attr_queue_depth
,
600 &dev_attr_queue_type
,
602 &dev_attr_scsi_level
,
610 &dev_attr_iocounterbits
,
611 &dev_attr_iorequest_cnt
,
612 &dev_attr_iodone_cnt
,
618 static ssize_t
sdev_store_queue_depth_rw(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
622 struct scsi_device
*sdev
= to_scsi_device(dev
);
623 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
625 if (!sht
->change_queue_depth
)
628 depth
= simple_strtoul(buf
, NULL
, 0);
633 retval
= sht
->change_queue_depth(sdev
, depth
);
640 static struct device_attribute sdev_attr_queue_depth_rw
=
641 __ATTR(queue_depth
, S_IRUGO
| S_IWUSR
, sdev_show_queue_depth
,
642 sdev_store_queue_depth_rw
);
644 static ssize_t
sdev_store_queue_type_rw(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
647 struct scsi_device
*sdev
= to_scsi_device(dev
);
648 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
649 int tag_type
= 0, retval
;
650 int prev_tag_type
= scsi_get_tag_type(sdev
);
652 if (!sdev
->tagged_supported
|| !sht
->change_queue_type
)
655 if (strncmp(buf
, "ordered", 7) == 0)
656 tag_type
= MSG_ORDERED_TAG
;
657 else if (strncmp(buf
, "simple", 6) == 0)
658 tag_type
= MSG_SIMPLE_TAG
;
659 else if (strncmp(buf
, "none", 4) != 0)
662 if (tag_type
== prev_tag_type
)
665 retval
= sht
->change_queue_type(sdev
, tag_type
);
672 static struct device_attribute sdev_attr_queue_type_rw
=
673 __ATTR(queue_type
, S_IRUGO
| S_IWUSR
, show_queue_type_field
,
674 sdev_store_queue_type_rw
);
676 static struct device_attribute
*attr_changed_internally(
677 struct Scsi_Host
*shost
,
678 struct device_attribute
* attr
)
680 if (!strcmp("queue_depth", attr
->attr
.name
)
681 && shost
->hostt
->change_queue_depth
)
682 return &sdev_attr_queue_depth_rw
;
683 else if (!strcmp("queue_type", attr
->attr
.name
)
684 && shost
->hostt
->change_queue_type
)
685 return &sdev_attr_queue_type_rw
;
690 static struct device_attribute
*attr_overridden(
691 struct device_attribute
**attrs
,
692 struct device_attribute
*attr
)
698 for (i
= 0; attrs
[i
]; i
++)
699 if (!strcmp(attrs
[i
]->attr
.name
, attr
->attr
.name
))
704 static int attr_add(struct device
*dev
, struct device_attribute
*attr
)
706 struct device_attribute
*base_attr
;
709 * Spare the caller from having to copy things it's not interested in.
711 base_attr
= attr_overridden(scsi_sysfs_sdev_attrs
, attr
);
713 /* extend permissions */
714 attr
->attr
.mode
|= base_attr
->attr
.mode
;
716 /* override null show/store with default */
718 attr
->show
= base_attr
->show
;
720 attr
->store
= base_attr
->store
;
723 return device_create_file(dev
, attr
);
727 * scsi_sysfs_add_sdev - add scsi device to sysfs
728 * @sdev: scsi_device to add
731 * 0 on Success / non-zero on Failure
733 int scsi_sysfs_add_sdev(struct scsi_device
*sdev
)
737 if ((error
= scsi_device_set_state(sdev
, SDEV_RUNNING
)) != 0)
740 error
= device_add(&sdev
->sdev_gendev
);
742 put_device(sdev
->sdev_gendev
.parent
);
743 printk(KERN_INFO
"error 1\n");
746 error
= class_device_add(&sdev
->sdev_classdev
);
748 printk(KERN_INFO
"error 2\n");
752 /* take a reference for the sdev_classdev; this is
753 * released by the sdev_class .release */
754 get_device(&sdev
->sdev_gendev
);
755 if (sdev
->host
->hostt
->sdev_attrs
) {
756 for (i
= 0; sdev
->host
->hostt
->sdev_attrs
[i
]; i
++) {
757 error
= attr_add(&sdev
->sdev_gendev
,
758 sdev
->host
->hostt
->sdev_attrs
[i
]);
760 __scsi_remove_device(sdev
);
766 for (i
= 0; scsi_sysfs_sdev_attrs
[i
]; i
++) {
767 if (!attr_overridden(sdev
->host
->hostt
->sdev_attrs
,
768 scsi_sysfs_sdev_attrs
[i
])) {
769 struct device_attribute
* attr
=
770 attr_changed_internally(sdev
->host
,
771 scsi_sysfs_sdev_attrs
[i
]);
772 error
= device_create_file(&sdev
->sdev_gendev
, attr
);
774 __scsi_remove_device(sdev
);
780 transport_add_device(&sdev
->sdev_gendev
);
785 scsi_device_set_state(sdev
, SDEV_CANCEL
);
787 device_del(&sdev
->sdev_gendev
);
788 transport_destroy_device(&sdev
->sdev_gendev
);
789 put_device(&sdev
->sdev_gendev
);
794 void __scsi_remove_device(struct scsi_device
*sdev
)
796 struct device
*dev
= &sdev
->sdev_gendev
;
798 if (scsi_device_set_state(sdev
, SDEV_CANCEL
) != 0)
801 class_device_unregister(&sdev
->sdev_classdev
);
802 transport_remove_device(dev
);
804 scsi_device_set_state(sdev
, SDEV_DEL
);
805 if (sdev
->host
->hostt
->slave_destroy
)
806 sdev
->host
->hostt
->slave_destroy(sdev
);
807 transport_destroy_device(dev
);
812 * scsi_remove_device - unregister a device from the scsi bus
813 * @sdev: scsi_device to unregister
815 void scsi_remove_device(struct scsi_device
*sdev
)
817 struct Scsi_Host
*shost
= sdev
->host
;
819 mutex_lock(&shost
->scan_mutex
);
820 __scsi_remove_device(sdev
);
821 mutex_unlock(&shost
->scan_mutex
);
823 EXPORT_SYMBOL(scsi_remove_device
);
825 void __scsi_remove_target(struct scsi_target
*starget
)
827 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
829 struct scsi_device
*sdev
;
831 spin_lock_irqsave(shost
->host_lock
, flags
);
834 list_for_each_entry(sdev
, &shost
->__devices
, siblings
) {
835 if (sdev
->channel
!= starget
->channel
||
836 sdev
->id
!= starget
->id
||
837 sdev
->sdev_state
== SDEV_DEL
)
839 spin_unlock_irqrestore(shost
->host_lock
, flags
);
840 scsi_remove_device(sdev
);
841 spin_lock_irqsave(shost
->host_lock
, flags
);
844 spin_unlock_irqrestore(shost
->host_lock
, flags
);
845 scsi_target_reap(starget
);
848 static int __remove_child (struct device
* dev
, void * data
)
850 if (scsi_is_target_device(dev
))
851 __scsi_remove_target(to_scsi_target(dev
));
856 * scsi_remove_target - try to remove a target and all its devices
857 * @dev: generic starget or parent of generic stargets to be removed
859 * Note: This is slightly racy. It is possible that if the user
860 * requests the addition of another device then the target won't be
863 void scsi_remove_target(struct device
*dev
)
867 if (scsi_is_target_device(dev
)) {
868 __scsi_remove_target(to_scsi_target(dev
));
872 rdev
= get_device(dev
);
873 device_for_each_child(dev
, NULL
, __remove_child
);
876 EXPORT_SYMBOL(scsi_remove_target
);
878 int scsi_register_driver(struct device_driver
*drv
)
880 drv
->bus
= &scsi_bus_type
;
882 return driver_register(drv
);
884 EXPORT_SYMBOL(scsi_register_driver
);
886 int scsi_register_interface(struct class_interface
*intf
)
888 intf
->class = &sdev_class
;
890 return class_interface_register(intf
);
892 EXPORT_SYMBOL(scsi_register_interface
);
895 static struct class_device_attribute
*class_attr_overridden(
896 struct class_device_attribute
**attrs
,
897 struct class_device_attribute
*attr
)
903 for (i
= 0; attrs
[i
]; i
++)
904 if (!strcmp(attrs
[i
]->attr
.name
, attr
->attr
.name
))
909 static int class_attr_add(struct class_device
*classdev
,
910 struct class_device_attribute
*attr
)
912 struct class_device_attribute
*base_attr
;
915 * Spare the caller from having to copy things it's not interested in.
917 base_attr
= class_attr_overridden(scsi_sysfs_shost_attrs
, attr
);
919 /* extend permissions */
920 attr
->attr
.mode
|= base_attr
->attr
.mode
;
922 /* override null show/store with default */
924 attr
->show
= base_attr
->show
;
926 attr
->store
= base_attr
->store
;
929 return class_device_create_file(classdev
, attr
);
933 * scsi_sysfs_add_host - add scsi host to subsystem
934 * @shost: scsi host struct to add to subsystem
935 * @dev: parent struct device pointer
937 int scsi_sysfs_add_host(struct Scsi_Host
*shost
)
941 if (shost
->hostt
->shost_attrs
) {
942 for (i
= 0; shost
->hostt
->shost_attrs
[i
]; i
++) {
943 error
= class_attr_add(&shost
->shost_classdev
,
944 shost
->hostt
->shost_attrs
[i
]);
950 for (i
= 0; scsi_sysfs_shost_attrs
[i
]; i
++) {
951 if (!class_attr_overridden(shost
->hostt
->shost_attrs
,
952 scsi_sysfs_shost_attrs
[i
])) {
953 error
= class_device_create_file(&shost
->shost_classdev
,
954 scsi_sysfs_shost_attrs
[i
]);
960 transport_register_device(&shost
->shost_gendev
);
964 void scsi_sysfs_device_initialize(struct scsi_device
*sdev
)
967 struct Scsi_Host
*shost
= sdev
->host
;
968 struct scsi_target
*starget
= sdev
->sdev_target
;
970 device_initialize(&sdev
->sdev_gendev
);
971 sdev
->sdev_gendev
.bus
= &scsi_bus_type
;
972 sdev
->sdev_gendev
.release
= scsi_device_dev_release
;
973 sprintf(sdev
->sdev_gendev
.bus_id
,"%d:%d:%d:%d",
974 sdev
->host
->host_no
, sdev
->channel
, sdev
->id
,
977 class_device_initialize(&sdev
->sdev_classdev
);
978 sdev
->sdev_classdev
.dev
= &sdev
->sdev_gendev
;
979 sdev
->sdev_classdev
.class = &sdev_class
;
980 snprintf(sdev
->sdev_classdev
.class_id
, BUS_ID_SIZE
,
981 "%d:%d:%d:%d", sdev
->host
->host_no
,
982 sdev
->channel
, sdev
->id
, sdev
->lun
);
983 sdev
->scsi_level
= starget
->scsi_level
;
984 transport_setup_device(&sdev
->sdev_gendev
);
985 spin_lock_irqsave(shost
->host_lock
, flags
);
986 list_add_tail(&sdev
->same_target_siblings
, &starget
->devices
);
987 list_add_tail(&sdev
->siblings
, &shost
->__devices
);
988 spin_unlock_irqrestore(shost
->host_lock
, flags
);
991 int scsi_is_sdev_device(const struct device
*dev
)
993 return dev
->release
== scsi_device_dev_release
;
995 EXPORT_SYMBOL(scsi_is_sdev_device
);
997 /* A blank transport template that is used in drivers that don't
998 * yet implement Transport Attributes */
999 struct scsi_transport_template blank_transport_template
= { { { {NULL
, }, }, }, };