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