]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/scsi_sysfs.c
[SCSI] megaraid_{mm,mbox}: 64-bit DMA capability fix
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / scsi_sysfs.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/blkdev.h>
12#include <linux/device.h>
13
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>
19
20#include "scsi_priv.h"
21#include "scsi_logging.h"
22
0ad78200 23static const struct {
1da177e4
LT
24 enum scsi_device_state value;
25 char *name;
26} sdev_states[] = {
27 { SDEV_CREATED, "created" },
28 { SDEV_RUNNING, "running" },
29 { SDEV_CANCEL, "cancel" },
30 { SDEV_DEL, "deleted" },
31 { SDEV_QUIESCE, "quiesce" },
32 { SDEV_OFFLINE, "offline" },
33 { SDEV_BLOCK, "blocked" },
34};
35
36const char *scsi_device_state_name(enum scsi_device_state state)
37{
38 int i;
39 char *name = NULL;
40
6391a113 41 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
42 if (sdev_states[i].value == state) {
43 name = sdev_states[i].name;
44 break;
45 }
46 }
47 return name;
48}
49
0ad78200 50static const struct {
d3301874
MA
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" },
939647ee
JB
59 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
60 { SHOST_DEL_RECOVERY, "deleted/recovery", },
d3301874
MA
61};
62const char *scsi_host_state_name(enum scsi_host_state state)
63{
64 int i;
65 char *name = NULL;
66
6391a113 67 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
d3301874
MA
68 if (shost_states[i].value == state) {
69 name = shost_states[i].name;
70 break;
71 }
72 }
73 return name;
74}
75
1da177e4
LT
76static int check_set(unsigned int *val, char *src)
77{
78 char *last;
79
80 if (strncmp(src, "-", 20) == 0) {
81 *val = SCAN_WILD_CARD;
82 } else {
83 /*
84 * Doesn't check for int overflow
85 */
86 *val = simple_strtoul(src, &last, 0);
87 if (*last != '\0')
88 return 1;
89 }
90 return 0;
91}
92
93static int scsi_scan(struct Scsi_Host *shost, const char *str)
94{
95 char s1[15], s2[15], s3[15], junk;
96 unsigned int channel, id, lun;
97 int res;
98
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;
e02f3f59
CH
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);
1da177e4
LT
112 return res;
113}
114
115/*
116 * shost_show_function: macro to create an attr function that can be used to
117 * show a non-bit field.
118 */
119#define shost_show_function(name, field, format_string) \
120static ssize_t \
121show_##name (struct class_device *class_dev, char *buf) \
122{ \
123 struct Scsi_Host *shost = class_to_shost(class_dev); \
124 return snprintf (buf, 20, format_string, shost->field); \
125}
126
127/*
128 * shost_rd_attr: macro to create a function and attribute variable for a
129 * read only field.
130 */
131#define shost_rd_attr2(name, field, format_string) \
132 shost_show_function(name, field, format_string) \
133static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
134
135#define shost_rd_attr(field, format_string) \
136shost_rd_attr2(field, field, format_string)
137
138/*
139 * Create the actual show/store functions and data structures.
140 */
141
142static ssize_t store_scan(struct class_device *class_dev, const char *buf,
143 size_t count)
144{
145 struct Scsi_Host *shost = class_to_shost(class_dev);
146 int res;
147
148 res = scsi_scan(shost, buf);
149 if (res == 0)
150 res = count;
151 return res;
152};
153static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
154
d3301874
MA
155static ssize_t
156store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
157{
158 int i;
159 struct Scsi_Host *shost = class_to_shost(class_dev);
160 enum scsi_host_state state = 0;
161
6391a113 162 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
d3301874
MA
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;
168 }
169 }
170 if (!state)
171 return -EINVAL;
172
173 if (scsi_host_set_state(shost, state))
174 return -EINVAL;
175 return count;
176}
177
178static ssize_t
179show_shost_state(struct class_device *class_dev, char *buf)
180{
181 struct Scsi_Host *shost = class_to_shost(class_dev);
182 const char *name = scsi_host_state_name(shost->shost_state);
183
184 if (!name)
185 return -EINVAL;
186
187 return snprintf(buf, 20, "%s\n", name);
188}
189
190static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
191
1da177e4
LT
192shost_rd_attr(unique_id, "%u\n");
193shost_rd_attr(host_busy, "%hu\n");
194shost_rd_attr(cmd_per_lun, "%hd\n");
195shost_rd_attr(sg_tablesize, "%hu\n");
196shost_rd_attr(unchecked_isa_dma, "%d\n");
197shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
198
199static 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,
d3301874 207 &class_device_attr_state,
1da177e4
LT
208 NULL
209};
210
211static void scsi_device_cls_release(struct class_device *class_dev)
212{
213 struct scsi_device *sdev;
214
215 sdev = class_to_sdev(class_dev);
216 put_device(&sdev->sdev_gendev);
217}
218
65110b21 219static void scsi_device_dev_release_usercontext(void *data)
1da177e4 220{
65110b21 221 struct device *dev = data;
1da177e4
LT
222 struct scsi_device *sdev;
223 struct device *parent;
224 struct scsi_target *starget;
225 unsigned long flags;
226
227 parent = dev->parent;
228 sdev = to_scsi_device(dev);
229 starget = to_scsi_target(parent);
230
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);
237
238 if (sdev->request_queue) {
239 sdev->request_queue->queuedata = NULL;
65110b21 240 /* user context needed to free queue */
1da177e4 241 scsi_free_queue(sdev->request_queue);
c2a9331c
JB
242 /* temporary expedient, try to catch use of queue lock
243 * after free of sdev */
244 sdev->request_queue = NULL;
1da177e4
LT
245 }
246
247 scsi_target_reap(scsi_target(sdev));
248
249 kfree(sdev->inquiry);
250 kfree(sdev);
251
252 if (parent)
253 put_device(parent);
254}
255
65110b21
JB
256static void scsi_device_dev_release(struct device *dev)
257{
ffedb452
JB
258 struct scsi_device *sdp = to_scsi_device(dev);
259 execute_in_process_context(scsi_device_dev_release_usercontext, dev,
260 &sdp->ew);
65110b21
JB
261}
262
52c1da39 263static struct class sdev_class = {
1da177e4
LT
264 .name = "scsi_device",
265 .release = scsi_device_cls_release,
266};
267
268/* all probing is done in the individual ->probe routines */
269static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
270{
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;
275}
276
9b847548
JA
277static int scsi_bus_suspend(struct device * dev, pm_message_t state)
278{
279 struct scsi_device *sdev = to_scsi_device(dev);
280 struct scsi_host_template *sht = sdev->host->hostt;
281 int err;
282
283 err = scsi_device_quiesce(sdev);
284 if (err)
285 return err;
286
287 if (sht->suspend)
082776e4 288 err = sht->suspend(sdev, state);
9b847548
JA
289
290 return err;
291}
292
293static int scsi_bus_resume(struct device * dev)
294{
295 struct scsi_device *sdev = to_scsi_device(dev);
296 struct scsi_host_template *sht = sdev->host->hostt;
297 int err = 0;
298
299 if (sht->resume)
300 err = sht->resume(sdev);
301
302 scsi_device_resume(sdev);
303 return err;
304}
305
1da177e4
LT
306struct bus_type scsi_bus_type = {
307 .name = "scsi",
308 .match = scsi_bus_match,
9b847548
JA
309 .suspend = scsi_bus_suspend,
310 .resume = scsi_bus_resume,
1da177e4
LT
311};
312
313int scsi_sysfs_register(void)
314{
315 int error;
316
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);
322 }
323
324 return error;
325}
326
327void scsi_sysfs_unregister(void)
328{
329 class_unregister(&sdev_class);
330 bus_unregister(&scsi_bus_type);
331}
332
333/*
334 * sdev_show_function: macro to create an attr function that can be used to
335 * show a non-bit field.
336 */
337#define sdev_show_function(field, format_string) \
338static ssize_t \
10523b3b 339sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
340{ \
341 struct scsi_device *sdev; \
342 sdev = to_scsi_device(dev); \
343 return snprintf (buf, 20, format_string, sdev->field); \
344} \
345
346/*
347 * sdev_rd_attr: macro to create a function and attribute variable for a
348 * read only field.
349 */
350#define sdev_rd_attr(field, format_string) \
351 sdev_show_function(field, format_string) \
352static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
353
354
355/*
356 * sdev_rd_attr: create a function and attribute variable for a
357 * read/write field.
358 */
359#define sdev_rw_attr(field, format_string) \
360 sdev_show_function(field, format_string) \
361 \
362static ssize_t \
10523b3b 363sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
364{ \
365 struct scsi_device *sdev; \
366 sdev = to_scsi_device(dev); \
367 snscanf (buf, 20, format_string, &sdev->field); \
368 return count; \
369} \
370static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
371
372/* Currently we don't export bit fields, but we might in future,
373 * so leave this code in */
374#if 0
375/*
376 * sdev_rd_attr: create a function and attribute variable for a
377 * read/write bit field.
378 */
379#define sdev_rw_attr_bit(field) \
380 sdev_show_function(field, "%d\n") \
381 \
382static ssize_t \
10523b3b 383sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
384{ \
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; \
392 } \
393 return ret; \
394} \
395static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
396
397/*
398 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
399 * else return -EINVAL.
400 */
401static int scsi_sdev_check_buf_bit(const char *buf)
402{
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;
412}
413#endif
414/*
415 * Create the actual show/store functions and data structures.
416 */
417sdev_rd_attr (device_blocked, "%d\n");
418sdev_rd_attr (queue_depth, "%d\n");
419sdev_rd_attr (type, "%d\n");
420sdev_rd_attr (scsi_level, "%d\n");
421sdev_rd_attr (vendor, "%.8s\n");
422sdev_rd_attr (model, "%.16s\n");
423sdev_rd_attr (rev, "%.4s\n");
424
425static ssize_t
10523b3b 426sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
427{
428 struct scsi_device *sdev;
429 sdev = to_scsi_device(dev);
430 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
431}
432
433static ssize_t
10523b3b 434sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
435{
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;
442}
443static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
444
445static ssize_t
10523b3b 446store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
447{
448 scsi_rescan_device(dev);
449 return count;
450}
451static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
452
10523b3b 453static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
454 size_t count)
455{
456 scsi_remove_device(to_scsi_device(dev));
457 return count;
458};
459static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
460
461static ssize_t
10523b3b 462store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
463{
464 int i;
465 struct scsi_device *sdev = to_scsi_device(dev);
466 enum scsi_device_state state = 0;
467
6391a113 468 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
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;
474 }
475 }
476 if (!state)
477 return -EINVAL;
478
479 if (scsi_device_set_state(sdev, state))
480 return -EINVAL;
481 return count;
482}
483
484static ssize_t
10523b3b 485show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
486{
487 struct scsi_device *sdev = to_scsi_device(dev);
488 const char *name = scsi_device_state_name(sdev->sdev_state);
489
490 if (!name)
491 return -EINVAL;
492
493 return snprintf(buf, 20, "%s\n", name);
494}
495
496static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
497
498static ssize_t
10523b3b 499show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
500{
501 struct scsi_device *sdev = to_scsi_device(dev);
502 const char *name = "none";
503
504 if (sdev->ordered_tags)
505 name = "ordered";
506 else if (sdev->simple_tags)
507 name = "simple";
508
509 return snprintf(buf, 20, "%s\n", name);
510}
511
512static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
513
514static ssize_t
10523b3b 515show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
516{
517 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
518}
519
520static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
521
522#define show_sdev_iostat(field) \
523static ssize_t \
10523b3b 524show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
525{ \
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); \
529} \
530static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
531
532show_sdev_iostat(iorequest_cnt);
533show_sdev_iostat(iodone_cnt);
534show_sdev_iostat(ioerr_cnt);
535
536
537/* Default template for device attributes. May NOT be modified */
538static 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
556};
557
10523b3b 558static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
559 size_t count)
560{
561 int depth, retval;
562 struct scsi_device *sdev = to_scsi_device(dev);
563 struct scsi_host_template *sht = sdev->host->hostt;
564
565 if (!sht->change_queue_depth)
566 return -EINVAL;
567
568 depth = simple_strtoul(buf, NULL, 0);
569
570 if (depth < 1)
571 return -EINVAL;
572
573 retval = sht->change_queue_depth(sdev, depth);
574 if (retval < 0)
575 return retval;
576
577 return count;
578}
579
580static 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);
583
10523b3b 584static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
585 size_t count)
586{
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);
591
592 if (!sdev->tagged_supported || !sht->change_queue_type)
593 return -EINVAL;
594
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;
601
602 if (tag_type == prev_tag_type)
603 return count;
604
605 retval = sht->change_queue_type(sdev, tag_type);
606 if (retval < 0)
607 return retval;
608
609 return count;
610}
611
612static 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);
615
616static struct device_attribute *attr_changed_internally(
617 struct Scsi_Host *shost,
618 struct device_attribute * attr)
619{
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;
627}
628
629
630static struct device_attribute *attr_overridden(
631 struct device_attribute **attrs,
632 struct device_attribute *attr)
633{
634 int i;
635
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;
642}
643
644static int attr_add(struct device *dev, struct device_attribute *attr)
645{
646 struct device_attribute *base_attr;
647
648 /*
649 * Spare the caller from having to copy things it's not interested in.
650 */
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;
655
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;
661 }
662
663 return device_create_file(dev, attr);
664}
665
666/**
667 * scsi_sysfs_add_sdev - add scsi device to sysfs
668 * @sdev: scsi_device to add
669 *
670 * Return value:
671 * 0 on Success / non-zero on Failure
672 **/
673int scsi_sysfs_add_sdev(struct scsi_device *sdev)
674{
675 int error, i;
676
677 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
678 return error;
679
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;
685 }
686 error = class_device_add(&sdev->sdev_classdev);
687 if (error) {
688 printk(KERN_INFO "error 2\n");
689 goto clean_device;
690 }
691
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) {
903f4fed 700 __scsi_remove_device(sdev);
1da177e4
LT
701 goto out;
702 }
703 }
704 }
705
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) {
903f4fed 714 __scsi_remove_device(sdev);
1da177e4
LT
715 goto out;
716 }
717 }
718 }
719
720 transport_add_device(&sdev->sdev_gendev);
721 out:
722 return error;
723
724 clean_device:
725 scsi_device_set_state(sdev, SDEV_CANCEL);
726
727 device_del(&sdev->sdev_gendev);
728 transport_destroy_device(&sdev->sdev_gendev);
729 put_device(&sdev->sdev_gendev);
730
731 return error;
732}
733
903f4fed 734void __scsi_remove_device(struct scsi_device *sdev)
1da177e4 735{
df133c21
JB
736 struct device *dev = &sdev->sdev_gendev;
737
1da177e4 738 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
903f4fed 739 return;
1da177e4
LT
740
741 class_device_unregister(&sdev->sdev_classdev);
df133c21
JB
742 transport_remove_device(dev);
743 device_del(dev);
1da177e4
LT
744 scsi_device_set_state(sdev, SDEV_DEL);
745 if (sdev->host->hostt->slave_destroy)
746 sdev->host->hostt->slave_destroy(sdev);
df133c21
JB
747 transport_destroy_device(dev);
748 put_device(dev);
903f4fed
AS
749}
750
751/**
752 * scsi_remove_device - unregister a device from the scsi bus
753 * @sdev: scsi_device to unregister
754 **/
755void scsi_remove_device(struct scsi_device *sdev)
756{
54195002
AS
757 struct Scsi_Host *shost = sdev->host;
758
0b950672 759 mutex_lock(&shost->scan_mutex);
903f4fed 760 __scsi_remove_device(sdev);
0b950672 761 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
762}
763EXPORT_SYMBOL(scsi_remove_device);
764
765void __scsi_remove_target(struct scsi_target *starget)
766{
767 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
768 unsigned long flags;
a64358db 769 struct scsi_device *sdev;
1da177e4
LT
770
771 spin_lock_irqsave(shost->host_lock, flags);
772 starget->reap_ref++;
a64358db
AS
773 restart:
774 list_for_each_entry(sdev, &shost->__devices, siblings) {
1da177e4 775 if (sdev->channel != starget->channel ||
a64358db
AS
776 sdev->id != starget->id ||
777 sdev->sdev_state == SDEV_DEL)
1da177e4
LT
778 continue;
779 spin_unlock_irqrestore(shost->host_lock, flags);
780 scsi_remove_device(sdev);
781 spin_lock_irqsave(shost->host_lock, flags);
a64358db 782 goto restart;
1da177e4
LT
783 }
784 spin_unlock_irqrestore(shost->host_lock, flags);
785 scsi_target_reap(starget);
786}
787
20b1e674
PM
788static int __remove_child (struct device * dev, void * data)
789{
790 if (scsi_is_target_device(dev))
791 __scsi_remove_target(to_scsi_target(dev));
792 return 0;
793}
794
1da177e4
LT
795/**
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
798 *
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.
802 */
803void scsi_remove_target(struct device *dev)
804{
20b1e674 805 struct device *rdev;
1da177e4
LT
806
807 if (scsi_is_target_device(dev)) {
808 __scsi_remove_target(to_scsi_target(dev));
809 return;
810 }
811
812 rdev = get_device(dev);
20b1e674 813 device_for_each_child(dev, NULL, __remove_child);
1da177e4
LT
814 put_device(rdev);
815}
816EXPORT_SYMBOL(scsi_remove_target);
817
818int scsi_register_driver(struct device_driver *drv)
819{
820 drv->bus = &scsi_bus_type;
821
822 return driver_register(drv);
823}
824EXPORT_SYMBOL(scsi_register_driver);
825
826int scsi_register_interface(struct class_interface *intf)
827{
828 intf->class = &sdev_class;
829
830 return class_interface_register(intf);
831}
832EXPORT_SYMBOL(scsi_register_interface);
833
834
835static struct class_device_attribute *class_attr_overridden(
836 struct class_device_attribute **attrs,
837 struct class_device_attribute *attr)
838{
839 int i;
840
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;
847}
848
849static int class_attr_add(struct class_device *classdev,
850 struct class_device_attribute *attr)
851{
852 struct class_device_attribute *base_attr;
853
854 /*
855 * Spare the caller from having to copy things it's not interested in.
856 */
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;
861
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;
867 }
868
869 return class_device_create_file(classdev, attr);
870}
871
872/**
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
876 **/
877int scsi_sysfs_add_host(struct Scsi_Host *shost)
878{
879 int error, i;
880
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;
887 }
888 }
889
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;
897 }
898 }
899
900 transport_register_device(&shost->shost_gendev);
901 return 0;
902}
903
904void scsi_sysfs_device_initialize(struct scsi_device *sdev)
905{
906 unsigned long flags;
907 struct Scsi_Host *shost = sdev->host;
908 struct scsi_target *starget = sdev->sdev_target;
909
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);
916
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);
929}
930
931int scsi_is_sdev_device(const struct device *dev)
932{
933 return dev->release == scsi_device_dev_release;
934}
935EXPORT_SYMBOL(scsi_is_sdev_device);
936
937/* A blank transport template that is used in drivers that don't
938 * yet implement Transport Attributes */
939struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };