]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/scsi_sysfs.c
ncr5380: Call complete_cmd() for disconnected commands on bus reset
[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 9#include <linux/module.h>
5a0e3ad6 10#include <linux/slab.h>
1da177e4
LT
11#include <linux/init.h>
12#include <linux/blkdev.h>
13#include <linux/device.h>
bc4f2401 14#include <linux/pm_runtime.h>
1da177e4
LT
15
16#include <scsi/scsi.h>
17#include <scsi/scsi_device.h>
18#include <scsi/scsi_host.h>
19#include <scsi/scsi_tcq.h>
41f95dd2 20#include <scsi/scsi_dh.h>
1da177e4 21#include <scsi/scsi_transport.h>
44818efb 22#include <scsi/scsi_driver.h>
1da177e4
LT
23
24#include "scsi_priv.h"
25#include "scsi_logging.h"
26
b0ed4336
HR
27static struct device_type scsi_dev_type;
28
0ad78200 29static const struct {
1da177e4
LT
30 enum scsi_device_state value;
31 char *name;
32} sdev_states[] = {
33 { SDEV_CREATED, "created" },
34 { SDEV_RUNNING, "running" },
35 { SDEV_CANCEL, "cancel" },
36 { SDEV_DEL, "deleted" },
37 { SDEV_QUIESCE, "quiesce" },
38 { SDEV_OFFLINE, "offline" },
1b8d2620 39 { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
1da177e4 40 { SDEV_BLOCK, "blocked" },
6f4267e3 41 { SDEV_CREATED_BLOCK, "created-blocked" },
1da177e4
LT
42};
43
44const char *scsi_device_state_name(enum scsi_device_state state)
45{
46 int i;
47 char *name = NULL;
48
6391a113 49 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
50 if (sdev_states[i].value == state) {
51 name = sdev_states[i].name;
52 break;
53 }
54 }
55 return name;
56}
57
0ad78200 58static const struct {
d3301874
MA
59 enum scsi_host_state value;
60 char *name;
61} shost_states[] = {
62 { SHOST_CREATED, "created" },
63 { SHOST_RUNNING, "running" },
64 { SHOST_CANCEL, "cancel" },
65 { SHOST_DEL, "deleted" },
66 { SHOST_RECOVERY, "recovery" },
939647ee
JB
67 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
68 { SHOST_DEL_RECOVERY, "deleted/recovery", },
d3301874
MA
69};
70const char *scsi_host_state_name(enum scsi_host_state state)
71{
72 int i;
73 char *name = NULL;
74
6391a113 75 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
d3301874
MA
76 if (shost_states[i].value == state) {
77 name = shost_states[i].name;
78 break;
79 }
80 }
81 return name;
82}
83
d78540da 84#ifdef CONFIG_SCSI_DH
77c9df96
HR
85static const struct {
86 unsigned char value;
87 char *name;
88} sdev_access_states[] = {
89 { SCSI_ACCESS_STATE_OPTIMAL, "active/optimized" },
90 { SCSI_ACCESS_STATE_ACTIVE, "active/non-optimized" },
91 { SCSI_ACCESS_STATE_STANDBY, "standby" },
92 { SCSI_ACCESS_STATE_UNAVAILABLE, "unavailable" },
93 { SCSI_ACCESS_STATE_LBA, "lba-dependent" },
94 { SCSI_ACCESS_STATE_OFFLINE, "offline" },
95 { SCSI_ACCESS_STATE_TRANSITIONING, "transitioning" },
96};
97
d78540da 98static const char *scsi_access_state_name(unsigned char state)
77c9df96
HR
99{
100 int i;
101 char *name = NULL;
102
103 for (i = 0; i < ARRAY_SIZE(sdev_access_states); i++) {
104 if (sdev_access_states[i].value == state) {
105 name = sdev_access_states[i].name;
106 break;
107 }
108 }
109 return name;
110}
d78540da 111#endif
77c9df96 112
9cb78c16 113static int check_set(unsigned long long *val, char *src)
1da177e4
LT
114{
115 char *last;
116
117 if (strncmp(src, "-", 20) == 0) {
118 *val = SCAN_WILD_CARD;
119 } else {
120 /*
121 * Doesn't check for int overflow
122 */
9cb78c16 123 *val = simple_strtoull(src, &last, 0);
1da177e4
LT
124 if (*last != '\0')
125 return 1;
126 }
127 return 0;
128}
129
130static int scsi_scan(struct Scsi_Host *shost, const char *str)
131{
9cb78c16
HR
132 char s1[15], s2[15], s3[17], junk;
133 unsigned long long channel, id, lun;
1da177e4
LT
134 int res;
135
9cb78c16 136 res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
1da177e4
LT
137 if (res != 3)
138 return -EINVAL;
139 if (check_set(&channel, s1))
140 return -EINVAL;
141 if (check_set(&id, s2))
142 return -EINVAL;
143 if (check_set(&lun, s3))
144 return -EINVAL;
e02f3f59
CH
145 if (shost->transportt->user_scan)
146 res = shost->transportt->user_scan(shost, channel, id, lun);
147 else
148 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
1da177e4
LT
149 return res;
150}
151
152/*
153 * shost_show_function: macro to create an attr function that can be used to
154 * show a non-bit field.
155 */
156#define shost_show_function(name, field, format_string) \
157static ssize_t \
ee959b00
TJ
158show_##name (struct device *dev, struct device_attribute *attr, \
159 char *buf) \
1da177e4 160{ \
ee959b00 161 struct Scsi_Host *shost = class_to_shost(dev); \
1da177e4
LT
162 return snprintf (buf, 20, format_string, shost->field); \
163}
164
165/*
166 * shost_rd_attr: macro to create a function and attribute variable for a
167 * read only field.
168 */
169#define shost_rd_attr2(name, field, format_string) \
170 shost_show_function(name, field, format_string) \
ee959b00 171static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
1da177e4
LT
172
173#define shost_rd_attr(field, format_string) \
174shost_rd_attr2(field, field, format_string)
175
176/*
177 * Create the actual show/store functions and data structures.
178 */
179
ee959b00
TJ
180static ssize_t
181store_scan(struct device *dev, struct device_attribute *attr,
182 const char *buf, size_t count)
1da177e4 183{
ee959b00 184 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
185 int res;
186
187 res = scsi_scan(shost, buf);
188 if (res == 0)
189 res = count;
190 return res;
191};
ee959b00 192static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1da177e4 193
d3301874 194static ssize_t
ee959b00
TJ
195store_shost_state(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
d3301874
MA
197{
198 int i;
ee959b00 199 struct Scsi_Host *shost = class_to_shost(dev);
d3301874
MA
200 enum scsi_host_state state = 0;
201
6391a113 202 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
d3301874
MA
203 const int len = strlen(shost_states[i].name);
204 if (strncmp(shost_states[i].name, buf, len) == 0 &&
205 buf[len] == '\n') {
206 state = shost_states[i].value;
207 break;
208 }
209 }
210 if (!state)
211 return -EINVAL;
212
213 if (scsi_host_set_state(shost, state))
214 return -EINVAL;
215 return count;
216}
217
218static ssize_t
ee959b00 219show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
d3301874 220{
ee959b00 221 struct Scsi_Host *shost = class_to_shost(dev);
d3301874
MA
222 const char *name = scsi_host_state_name(shost->shost_state);
223
224 if (!name)
225 return -EINVAL;
226
227 return snprintf(buf, 20, "%s\n", name);
228}
229
ee959b00 230/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
d78540da 231static struct device_attribute dev_attr_hstate =
ee959b00 232 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
d3301874 233
5dc2b89e
FT
234static ssize_t
235show_shost_mode(unsigned int mode, char *buf)
236{
237 ssize_t len = 0;
238
239 if (mode & MODE_INITIATOR)
240 len = sprintf(buf, "%s", "Initiator");
241
242 if (mode & MODE_TARGET)
243 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
244
245 len += sprintf(buf + len, "\n");
246
247 return len;
248}
249
ee959b00
TJ
250static ssize_t
251show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
252 char *buf)
5dc2b89e 253{
ee959b00 254 struct Scsi_Host *shost = class_to_shost(dev);
7a39ac3f 255 unsigned int supported_mode = shost->hostt->supported_mode;
5dc2b89e 256
7a39ac3f
JB
257 if (supported_mode == MODE_UNKNOWN)
258 /* by default this should be initiator */
259 supported_mode = MODE_INITIATOR;
260
261 return show_shost_mode(supported_mode, buf);
5dc2b89e
FT
262}
263
ee959b00 264static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
5dc2b89e 265
ee959b00
TJ
266static ssize_t
267show_shost_active_mode(struct device *dev,
268 struct device_attribute *attr, char *buf)
5dc2b89e 269{
ee959b00 270 struct Scsi_Host *shost = class_to_shost(dev);
5dc2b89e
FT
271
272 if (shost->active_mode == MODE_UNKNOWN)
273 return snprintf(buf, 20, "unknown\n");
274 else
275 return show_shost_mode(shost->active_mode, buf);
276}
277
ee959b00 278static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
5dc2b89e 279
072f19b4 280static int check_reset_type(const char *str)
29443691 281{
072f19b4 282 if (sysfs_streq(str, "adapter"))
29443691 283 return SCSI_ADAPTER_RESET;
072f19b4 284 else if (sysfs_streq(str, "firmware"))
29443691
VC
285 return SCSI_FIRMWARE_RESET;
286 else
287 return 0;
288}
289
290static ssize_t
291store_host_reset(struct device *dev, struct device_attribute *attr,
292 const char *buf, size_t count)
293{
294 struct Scsi_Host *shost = class_to_shost(dev);
295 struct scsi_host_template *sht = shost->hostt;
296 int ret = -EINVAL;
29443691
VC
297 int type;
298
072f19b4 299 type = check_reset_type(buf);
29443691
VC
300 if (!type)
301 goto exit_store_host_reset;
302
303 if (sht->host_reset)
304 ret = sht->host_reset(shost, type);
305
306exit_store_host_reset:
307 if (ret == 0)
308 ret = count;
309 return ret;
310}
311
312static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
313
b4562022
HR
314static ssize_t
315show_shost_eh_deadline(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct Scsi_Host *shost = class_to_shost(dev);
319
bb3b621a
RM
320 if (shost->eh_deadline == -1)
321 return snprintf(buf, strlen("off") + 2, "off\n");
322 return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
b4562022
HR
323}
324
325static ssize_t
326store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
327 const char *buf, size_t count)
328{
329 struct Scsi_Host *shost = class_to_shost(dev);
330 int ret = -EINVAL;
bb3b621a 331 unsigned long deadline, flags;
b4562022 332
ad469a57
HR
333 if (shost->transportt &&
334 (shost->transportt->eh_strategy_handler ||
335 !shost->hostt->eh_host_reset_handler))
b4562022
HR
336 return ret;
337
bb3b621a
RM
338 if (!strncmp(buf, "off", strlen("off")))
339 deadline = -1;
340 else {
341 ret = kstrtoul(buf, 10, &deadline);
342 if (ret)
343 return ret;
344 if (deadline * HZ > UINT_MAX)
345 return -EINVAL;
346 }
347
348 spin_lock_irqsave(shost->host_lock, flags);
349 if (scsi_host_in_recovery(shost))
350 ret = -EBUSY;
351 else {
352 if (deadline == -1)
353 shost->eh_deadline = -1;
354 else
b4562022 355 shost->eh_deadline = deadline * HZ;
bb3b621a
RM
356
357 ret = count;
b4562022 358 }
bb3b621a
RM
359 spin_unlock_irqrestore(shost->host_lock, flags);
360
b4562022
HR
361 return ret;
362}
363
364static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
365
d285203c 366shost_rd_attr(use_blk_mq, "%d\n");
1da177e4 367shost_rd_attr(unique_id, "%u\n");
1da177e4 368shost_rd_attr(cmd_per_lun, "%hd\n");
ed632da8 369shost_rd_attr(can_queue, "%hd\n");
1da177e4 370shost_rd_attr(sg_tablesize, "%hu\n");
13f05c8d 371shost_rd_attr(sg_prot_tablesize, "%hu\n");
1da177e4 372shost_rd_attr(unchecked_isa_dma, "%d\n");
4469f987
MP
373shost_rd_attr(prot_capabilities, "%u\n");
374shost_rd_attr(prot_guard_type, "%hd\n");
1da177e4
LT
375shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
376
74665016
CH
377static ssize_t
378show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
379{
380 struct Scsi_Host *shost = class_to_shost(dev);
381 return snprintf(buf, 20, "%d\n", atomic_read(&shost->host_busy));
382}
383static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
384
f7120a4f 385static struct attribute *scsi_sysfs_shost_attrs[] = {
d285203c 386 &dev_attr_use_blk_mq.attr,
f7120a4f
HR
387 &dev_attr_unique_id.attr,
388 &dev_attr_host_busy.attr,
389 &dev_attr_cmd_per_lun.attr,
390 &dev_attr_can_queue.attr,
391 &dev_attr_sg_tablesize.attr,
13f05c8d 392 &dev_attr_sg_prot_tablesize.attr,
f7120a4f
HR
393 &dev_attr_unchecked_isa_dma.attr,
394 &dev_attr_proc_name.attr,
395 &dev_attr_scan.attr,
396 &dev_attr_hstate.attr,
397 &dev_attr_supported_mode.attr,
398 &dev_attr_active_mode.attr,
4469f987
MP
399 &dev_attr_prot_capabilities.attr,
400 &dev_attr_prot_guard_type.attr,
29443691 401 &dev_attr_host_reset.attr,
b4562022 402 &dev_attr_eh_deadline.attr,
f7120a4f
HR
403 NULL
404};
405
d78540da 406static struct attribute_group scsi_shost_attr_group = {
f7120a4f
HR
407 .attrs = scsi_sysfs_shost_attrs,
408};
409
a4dbd674 410const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
f7120a4f 411 &scsi_shost_attr_group,
1da177e4
LT
412 NULL
413};
414
ee959b00 415static void scsi_device_cls_release(struct device *class_dev)
1da177e4
LT
416{
417 struct scsi_device *sdev;
418
419 sdev = class_to_sdev(class_dev);
420 put_device(&sdev->sdev_gendev);
421}
422
65f27f38 423static void scsi_device_dev_release_usercontext(struct work_struct *work)
1da177e4
LT
424{
425 struct scsi_device *sdev;
426 struct device *parent;
a341cd0f 427 struct list_head *this, *tmp;
1da177e4
LT
428 unsigned long flags;
429
65f27f38
DH
430 sdev = container_of(work, struct scsi_device, ew.work);
431
23695e41
JN
432 scsi_dh_release_device(sdev);
433
65f27f38 434 parent = sdev->sdev_gendev.parent;
1da177e4
LT
435
436 spin_lock_irqsave(sdev->host->host_lock, flags);
1da177e4
LT
437 list_del(&sdev->siblings);
438 list_del(&sdev->same_target_siblings);
439 list_del(&sdev->starved_entry);
440 spin_unlock_irqrestore(sdev->host->host_lock, flags);
441
a341cd0f
JG
442 cancel_work_sync(&sdev->event_work);
443
444 list_for_each_safe(this, tmp, &sdev->event_list) {
445 struct scsi_event *evt;
446
447 evt = list_entry(this, struct scsi_event, node);
448 list_del(&evt->node);
449 kfree(evt);
450 }
451
e73e079b 452 blk_put_queue(sdev->request_queue);
86cbfb56
JB
453 /* NULL queue means the device can't be used */
454 sdev->request_queue = NULL;
1da177e4 455
b3ae8780
HR
456 kfree(sdev->vpd_pg83);
457 kfree(sdev->vpd_pg80);
1da177e4
LT
458 kfree(sdev->inquiry);
459 kfree(sdev);
460
461 if (parent)
462 put_device(parent);
463}
464
65110b21
JB
465static void scsi_device_dev_release(struct device *dev)
466{
ffedb452 467 struct scsi_device *sdp = to_scsi_device(dev);
65f27f38 468 execute_in_process_context(scsi_device_dev_release_usercontext,
ffedb452 469 &sdp->ew);
65110b21
JB
470}
471
52c1da39 472static struct class sdev_class = {
1da177e4 473 .name = "scsi_device",
ee959b00 474 .dev_release = scsi_device_cls_release,
1da177e4
LT
475};
476
477/* all probing is done in the individual ->probe routines */
478static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
479{
b0ed4336
HR
480 struct scsi_device *sdp;
481
482 if (dev->type != &scsi_dev_type)
483 return 0;
484
485 sdp = to_scsi_device(dev);
1da177e4
LT
486 if (sdp->no_uld_attach)
487 return 0;
488 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
489}
490
7eff2e7a 491static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
d7b8bcb0 492{
1f42ea7b
JB
493 struct scsi_device *sdev;
494
495 if (dev->type != &scsi_dev_type)
496 return 0;
497
498 sdev = to_scsi_device(dev);
d7b8bcb0 499
7eff2e7a 500 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
d7b8bcb0
MT
501 return 0;
502}
503
1da177e4
LT
504struct bus_type scsi_bus_type = {
505 .name = "scsi",
506 .match = scsi_bus_match,
d7b8bcb0 507 .uevent = scsi_bus_uevent,
aa338601 508#ifdef CONFIG_PM
db5bd1e0 509 .pm = &scsi_bus_pm_ops,
e6da54d8 510#endif
1da177e4 511};
a6a8d9f8 512EXPORT_SYMBOL_GPL(scsi_bus_type);
1da177e4
LT
513
514int scsi_sysfs_register(void)
515{
516 int error;
517
518 error = bus_register(&scsi_bus_type);
519 if (!error) {
520 error = class_register(&sdev_class);
521 if (error)
522 bus_unregister(&scsi_bus_type);
523 }
524
525 return error;
526}
527
528void scsi_sysfs_unregister(void)
529{
530 class_unregister(&sdev_class);
531 bus_unregister(&scsi_bus_type);
532}
533
534/*
535 * sdev_show_function: macro to create an attr function that can be used to
536 * show a non-bit field.
537 */
538#define sdev_show_function(field, format_string) \
539static ssize_t \
ee959b00
TJ
540sdev_show_##field (struct device *dev, struct device_attribute *attr, \
541 char *buf) \
1da177e4
LT
542{ \
543 struct scsi_device *sdev; \
544 sdev = to_scsi_device(dev); \
545 return snprintf (buf, 20, format_string, sdev->field); \
546} \
547
548/*
549 * sdev_rd_attr: macro to create a function and attribute variable for a
550 * read only field.
551 */
552#define sdev_rd_attr(field, format_string) \
553 sdev_show_function(field, format_string) \
554static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
555
556
557/*
160e7f67 558 * sdev_rw_attr: create a function and attribute variable for a
1da177e4
LT
559 * read/write field.
560 */
561#define sdev_rw_attr(field, format_string) \
562 sdev_show_function(field, format_string) \
563 \
564static ssize_t \
ee959b00
TJ
565sdev_store_##field (struct device *dev, struct device_attribute *attr, \
566 const char *buf, size_t count) \
1da177e4
LT
567{ \
568 struct scsi_device *sdev; \
569 sdev = to_scsi_device(dev); \
160e7f67 570 sscanf (buf, format_string, &sdev->field); \
1da177e4
LT
571 return count; \
572} \
573static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
574
575/* Currently we don't export bit fields, but we might in future,
576 * so leave this code in */
577#if 0
578/*
579 * sdev_rd_attr: create a function and attribute variable for a
580 * read/write bit field.
581 */
582#define sdev_rw_attr_bit(field) \
583 sdev_show_function(field, "%d\n") \
584 \
585static ssize_t \
ee959b00
TJ
586sdev_store_##field (struct device *dev, struct device_attribute *attr, \
587 const char *buf, size_t count) \
1da177e4
LT
588{ \
589 int ret; \
590 struct scsi_device *sdev; \
591 ret = scsi_sdev_check_buf_bit(buf); \
592 if (ret >= 0) { \
593 sdev = to_scsi_device(dev); \
594 sdev->field = ret; \
595 ret = count; \
596 } \
597 return ret; \
598} \
599static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
600
601/*
602 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
603 * else return -EINVAL.
604 */
605static int scsi_sdev_check_buf_bit(const char *buf)
606{
607 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
608 if (buf[0] == '1')
609 return 1;
610 else if (buf[0] == '0')
611 return 0;
612 else
613 return -EINVAL;
614 } else
615 return -EINVAL;
616}
617#endif
618/*
619 * Create the actual show/store functions and data structures.
620 */
1da177e4
LT
621sdev_rd_attr (type, "%d\n");
622sdev_rd_attr (scsi_level, "%d\n");
623sdev_rd_attr (vendor, "%.8s\n");
624sdev_rd_attr (model, "%.16s\n");
625sdev_rd_attr (rev, "%.4s\n");
626
71e75c97
CH
627static ssize_t
628sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
629 char *buf)
630{
631 struct scsi_device *sdev = to_scsi_device(dev);
632 return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_busy));
633}
634static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
635
cd9070c9
CH
636static ssize_t
637sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
638 char *buf)
639{
640 struct scsi_device *sdev = to_scsi_device(dev);
641 return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
642}
643static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
644
242f9dcb
JA
645/*
646 * TODO: can we make these symlinks to the block layer ones?
647 */
1da177e4 648static ssize_t
10523b3b 649sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
650{
651 struct scsi_device *sdev;
652 sdev = to_scsi_device(dev);
242f9dcb 653 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
1da177e4
LT
654}
655
656static ssize_t
ee959b00
TJ
657sdev_store_timeout (struct device *dev, struct device_attribute *attr,
658 const char *buf, size_t count)
1da177e4
LT
659{
660 struct scsi_device *sdev;
661 int timeout;
662 sdev = to_scsi_device(dev);
663 sscanf (buf, "%d\n", &timeout);
242f9dcb 664 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
1da177e4
LT
665 return count;
666}
667static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
668
0816c925
MP
669static ssize_t
670sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
671{
672 struct scsi_device *sdev;
673 sdev = to_scsi_device(dev);
674 return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
675}
676
677static ssize_t
678sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
679 const char *buf, size_t count)
680{
681 struct scsi_device *sdev;
682 unsigned int eh_timeout;
683 int err;
684
685 if (!capable(CAP_SYS_ADMIN))
686 return -EACCES;
687
688 sdev = to_scsi_device(dev);
689 err = kstrtouint(buf, 10, &eh_timeout);
690 if (err)
691 return err;
692 sdev->eh_timeout = eh_timeout * HZ;
693
694 return count;
695}
696static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
697
1da177e4 698static ssize_t
ee959b00
TJ
699store_rescan_field (struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
1da177e4
LT
701{
702 scsi_rescan_device(dev);
703 return count;
704}
705static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
706
ee959b00
TJ
707static ssize_t
708sdev_store_delete(struct device *dev, struct device_attribute *attr,
709 const char *buf, size_t count)
1da177e4 710{
ac0ece91
TH
711 if (device_remove_file_self(dev, attr))
712 scsi_remove_device(to_scsi_device(dev));
1da177e4
LT
713 return count;
714};
715static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
716
717static ssize_t
ee959b00
TJ
718store_state_field(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
1da177e4
LT
720{
721 int i;
722 struct scsi_device *sdev = to_scsi_device(dev);
723 enum scsi_device_state state = 0;
724
6391a113 725 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
726 const int len = strlen(sdev_states[i].name);
727 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
728 buf[len] == '\n') {
729 state = sdev_states[i].value;
730 break;
731 }
732 }
733 if (!state)
734 return -EINVAL;
735
736 if (scsi_device_set_state(sdev, state))
737 return -EINVAL;
738 return count;
739}
740
741static ssize_t
10523b3b 742show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
743{
744 struct scsi_device *sdev = to_scsi_device(dev);
745 const char *name = scsi_device_state_name(sdev->sdev_state);
746
747 if (!name)
748 return -EINVAL;
749
750 return snprintf(buf, 20, "%s\n", name);
751}
752
753static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
754
755static ssize_t
ee959b00
TJ
756show_queue_type_field(struct device *dev, struct device_attribute *attr,
757 char *buf)
1da177e4
LT
758{
759 struct scsi_device *sdev = to_scsi_device(dev);
760 const char *name = "none";
761
609aa22f 762 if (sdev->simple_tags)
1da177e4
LT
763 name = "simple";
764
765 return snprintf(buf, 20, "%s\n", name);
766}
767
276b20d0
HR
768static ssize_t
769store_queue_type_field(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
771{
772 struct scsi_device *sdev = to_scsi_device(dev);
276b20d0 773
efc3c1df 774 if (!sdev->tagged_supported)
276b20d0 775 return -EINVAL;
efc3c1df
CH
776
777 sdev_printk(KERN_INFO, sdev,
778 "ignoring write to deprecated queue_type attribute");
276b20d0
HR
779 return count;
780}
781
782static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
783 store_queue_type_field);
1da177e4 784
b3ae8780
HR
785#define sdev_vpd_pg_attr(_page) \
786static ssize_t \
787show_vpd_##_page(struct file *filp, struct kobject *kobj, \
788 struct bin_attribute *bin_attr, \
789 char *buf, loff_t off, size_t count) \
790{ \
791 struct device *dev = container_of(kobj, struct device, kobj); \
792 struct scsi_device *sdev = to_scsi_device(dev); \
09e2b0b1 793 int ret; \
b3ae8780
HR
794 if (!sdev->vpd_##_page) \
795 return -EINVAL; \
09e2b0b1
HR
796 rcu_read_lock(); \
797 ret = memory_read_from_buffer(buf, count, &off, \
798 rcu_dereference(sdev->vpd_##_page), \
b3ae8780 799 sdev->vpd_##_page##_len); \
09e2b0b1
HR
800 rcu_read_unlock(); \
801 return ret; \
b3ae8780
HR
802} \
803static struct bin_attribute dev_attr_vpd_##_page = { \
804 .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO }, \
805 .size = 0, \
806 .read = show_vpd_##_page, \
807};
808
809sdev_vpd_pg_attr(pg83);
810sdev_vpd_pg_attr(pg80);
1da177e4 811
92e6246c
JT
812static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
813 struct bin_attribute *bin_attr,
814 char *buf, loff_t off, size_t count)
815{
816 struct device *dev = container_of(kobj, struct device, kobj);
817 struct scsi_device *sdev = to_scsi_device(dev);
818
819 if (!sdev->inquiry)
820 return -EINVAL;
821
822 return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
823 sdev->inquiry_len);
824}
825
826static struct bin_attribute dev_attr_inquiry = {
827 .attr = {
828 .name = "inquiry",
829 .mode = S_IRUGO,
830 },
831 .size = 0,
832 .read = show_inquiry,
833};
834
1da177e4 835static ssize_t
b3ae8780
HR
836show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
837 char *buf)
1da177e4
LT
838{
839 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
840}
841
842static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
843
844#define show_sdev_iostat(field) \
845static ssize_t \
ee959b00
TJ
846show_iostat_##field(struct device *dev, struct device_attribute *attr, \
847 char *buf) \
1da177e4
LT
848{ \
849 struct scsi_device *sdev = to_scsi_device(dev); \
850 unsigned long long count = atomic_read(&sdev->field); \
851 return snprintf(buf, 20, "0x%llx\n", count); \
852} \
853static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
854
855show_sdev_iostat(iorequest_cnt);
856show_sdev_iostat(iodone_cnt);
857show_sdev_iostat(ioerr_cnt);
858
d7b8bcb0
MT
859static ssize_t
860sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
861{
862 struct scsi_device *sdev;
863 sdev = to_scsi_device(dev);
864 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
865}
866static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
1da177e4 867
a341cd0f
JG
868#define DECLARE_EVT_SHOW(name, Cap_name) \
869static ssize_t \
870sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
ee959b00 871 char *buf) \
a341cd0f
JG
872{ \
873 struct scsi_device *sdev = to_scsi_device(dev); \
874 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
875 return snprintf(buf, 20, "%d\n", val); \
876}
877
878#define DECLARE_EVT_STORE(name, Cap_name) \
879static ssize_t \
ee959b00 880sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
a341cd0f
JG
881 const char *buf, size_t count) \
882{ \
883 struct scsi_device *sdev = to_scsi_device(dev); \
884 int val = simple_strtoul(buf, NULL, 0); \
885 if (val == 0) \
886 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
887 else if (val == 1) \
888 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
889 else \
890 return -EINVAL; \
891 return count; \
892}
893
894#define DECLARE_EVT(name, Cap_name) \
895 DECLARE_EVT_SHOW(name, Cap_name) \
896 DECLARE_EVT_STORE(name, Cap_name) \
897 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
898 sdev_store_evt_##name);
899#define REF_EVT(name) &dev_attr_evt_##name.attr
900
901DECLARE_EVT(media_change, MEDIA_CHANGE)
279afdfe
EM
902DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
903DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
904DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
905DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
906DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
a341cd0f 907
ee959b00 908static ssize_t
276b20d0
HR
909sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
910 const char *buf, size_t count)
1da177e4
LT
911{
912 int depth, retval;
913 struct scsi_device *sdev = to_scsi_device(dev);
914 struct scsi_host_template *sht = sdev->host->hostt;
915
916 if (!sht->change_queue_depth)
917 return -EINVAL;
918
919 depth = simple_strtoul(buf, NULL, 0);
920
1278dd68 921 if (depth < 1 || depth > sdev->host->can_queue)
1da177e4
LT
922 return -EINVAL;
923
db5ed4df 924 retval = sht->change_queue_depth(sdev, depth);
1da177e4
LT
925 if (retval < 0)
926 return retval;
927
4a84067d
VD
928 sdev->max_queue_depth = sdev->queue_depth;
929
1da177e4
LT
930 return count;
931}
276b20d0 932sdev_show_function(queue_depth, "%d\n");
1da177e4 933
276b20d0
HR
934static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
935 sdev_store_queue_depth);
1da177e4 936
248d4fe9
HR
937static ssize_t
938sdev_show_wwid(struct device *dev, struct device_attribute *attr,
939 char *buf)
940{
941 struct scsi_device *sdev = to_scsi_device(dev);
942 ssize_t count;
943
944 count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
945 if (count > 0) {
946 buf[count] = '\n';
947 count++;
948 }
949 return count;
950}
951static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
952
41f95dd2
HR
953#ifdef CONFIG_SCSI_DH
954static ssize_t
955sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
956 char *buf)
957{
958 struct scsi_device *sdev = to_scsi_device(dev);
959
960 if (!sdev->handler)
961 return snprintf(buf, 20, "detached\n");
962
963 return snprintf(buf, 20, "%s\n", sdev->handler->name);
964}
965
966static ssize_t
967sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
968 const char *buf, size_t count)
969{
970 struct scsi_device *sdev = to_scsi_device(dev);
971 int err = -EINVAL;
972
973 if (sdev->sdev_state == SDEV_CANCEL ||
974 sdev->sdev_state == SDEV_DEL)
975 return -ENODEV;
976
977 if (!sdev->handler) {
978 /*
979 * Attach to a device handler
980 */
981 err = scsi_dh_attach(sdev->request_queue, buf);
982 } else if (!strncmp(buf, "activate", 8)) {
983 /*
984 * Activate a device handler
985 */
986 if (sdev->handler->activate)
987 err = sdev->handler->activate(sdev, NULL, NULL);
988 else
989 err = 0;
990 } else if (!strncmp(buf, "detach", 6)) {
991 /*
992 * Detach from a device handler
993 */
994 sdev_printk(KERN_WARNING, sdev,
995 "can't detach handler %s.\n",
996 sdev->handler->name);
997 err = -EINVAL;
998 }
999
1000 return err < 0 ? err : count;
1001}
1002
1003static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
1004 sdev_store_dh_state);
77c9df96
HR
1005
1006static ssize_t
1007sdev_show_access_state(struct device *dev,
1008 struct device_attribute *attr,
1009 char *buf)
1010{
1011 struct scsi_device *sdev = to_scsi_device(dev);
1012 unsigned char access_state;
1013 const char *access_state_name;
1014
1015 if (!sdev->handler)
1016 return -EINVAL;
1017
1018 access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
1019 access_state_name = scsi_access_state_name(access_state);
1020
1021 return sprintf(buf, "%s\n",
1022 access_state_name ? access_state_name : "unknown");
1023}
1024static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
1025
1026static ssize_t
1027sdev_show_preferred_path(struct device *dev,
1028 struct device_attribute *attr,
1029 char *buf)
1030{
1031 struct scsi_device *sdev = to_scsi_device(dev);
1032
1033 if (!sdev->handler)
1034 return -EINVAL;
1035
1036 if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
1037 return sprintf(buf, "1\n");
1038 else
1039 return sprintf(buf, "0\n");
1040}
1041static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
41f95dd2
HR
1042#endif
1043
4a84067d
VD
1044static ssize_t
1045sdev_show_queue_ramp_up_period(struct device *dev,
1046 struct device_attribute *attr,
1047 char *buf)
1048{
1049 struct scsi_device *sdev;
1050 sdev = to_scsi_device(dev);
1051 return snprintf(buf, 20, "%u\n",
1052 jiffies_to_msecs(sdev->queue_ramp_up_period));
1053}
1054
1055static ssize_t
1056sdev_store_queue_ramp_up_period(struct device *dev,
1057 struct device_attribute *attr,
1058 const char *buf, size_t count)
1059{
1060 struct scsi_device *sdev = to_scsi_device(dev);
f7c65af5 1061 unsigned int period;
4a84067d 1062
f7c65af5 1063 if (kstrtouint(buf, 10, &period))
4a84067d
VD
1064 return -EINVAL;
1065
1066 sdev->queue_ramp_up_period = msecs_to_jiffies(period);
5cb9b40d 1067 return count;
4a84067d
VD
1068}
1069
276b20d0
HR
1070static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
1071 sdev_show_queue_ramp_up_period,
1072 sdev_store_queue_ramp_up_period);
4a84067d 1073
276b20d0
HR
1074static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
1075 struct attribute *attr, int i)
1da177e4 1076{
276b20d0 1077 struct device *dev = container_of(kobj, struct device, kobj);
1da177e4 1078 struct scsi_device *sdev = to_scsi_device(dev);
1da177e4 1079
1da177e4 1080
276b20d0
HR
1081 if (attr == &dev_attr_queue_depth.attr &&
1082 !sdev->host->hostt->change_queue_depth)
1083 return S_IRUGO;
1da177e4 1084
276b20d0
HR
1085 if (attr == &dev_attr_queue_ramp_up_period.attr &&
1086 !sdev->host->hostt->change_queue_depth)
1087 return 0;
1da177e4 1088
77c9df96
HR
1089#ifdef CONFIG_SCSI_DH
1090 if (attr == &dev_attr_access_state.attr &&
1091 !sdev->handler)
1092 return 0;
1093 if (attr == &dev_attr_preferred_path.attr &&
1094 !sdev->handler)
1095 return 0;
1096#endif
276b20d0 1097 return attr->mode;
1da177e4
LT
1098}
1099
7e47976b
HR
1100static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
1101 struct bin_attribute *attr, int i)
1102{
1103 struct device *dev = container_of(kobj, struct device, kobj);
1104 struct scsi_device *sdev = to_scsi_device(dev);
1105
1106
1107 if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
1108 return 0;
1109
68887582 1110 if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
7e47976b
HR
1111 return 0;
1112
1113 return S_IRUGO;
1114}
1115
276b20d0
HR
1116/* Default template for device attributes. May NOT be modified */
1117static struct attribute *scsi_sdev_attrs[] = {
1118 &dev_attr_device_blocked.attr,
1119 &dev_attr_type.attr,
1120 &dev_attr_scsi_level.attr,
1121 &dev_attr_device_busy.attr,
1122 &dev_attr_vendor.attr,
1123 &dev_attr_model.attr,
1124 &dev_attr_rev.attr,
1125 &dev_attr_rescan.attr,
1126 &dev_attr_delete.attr,
1127 &dev_attr_state.attr,
1128 &dev_attr_timeout.attr,
1129 &dev_attr_eh_timeout.attr,
1130 &dev_attr_iocounterbits.attr,
1131 &dev_attr_iorequest_cnt.attr,
1132 &dev_attr_iodone_cnt.attr,
1133 &dev_attr_ioerr_cnt.attr,
1134 &dev_attr_modalias.attr,
1135 &dev_attr_queue_depth.attr,
1136 &dev_attr_queue_type.attr,
248d4fe9 1137 &dev_attr_wwid.attr,
41f95dd2
HR
1138#ifdef CONFIG_SCSI_DH
1139 &dev_attr_dh_state.attr,
77c9df96
HR
1140 &dev_attr_access_state.attr,
1141 &dev_attr_preferred_path.attr,
41f95dd2 1142#endif
276b20d0
HR
1143 &dev_attr_queue_ramp_up_period.attr,
1144 REF_EVT(media_change),
1145 REF_EVT(inquiry_change_reported),
1146 REF_EVT(capacity_change_reported),
1147 REF_EVT(soft_threshold_reached),
1148 REF_EVT(mode_parameter_change_reported),
1149 REF_EVT(lun_change_reported),
1150 NULL
1151};
1152
b3ae8780
HR
1153static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1154 &dev_attr_vpd_pg83,
1155 &dev_attr_vpd_pg80,
92e6246c 1156 &dev_attr_inquiry,
b3ae8780
HR
1157 NULL
1158};
276b20d0
HR
1159static struct attribute_group scsi_sdev_attr_group = {
1160 .attrs = scsi_sdev_attrs,
b3ae8780 1161 .bin_attrs = scsi_sdev_bin_attrs,
276b20d0 1162 .is_visible = scsi_sdev_attr_is_visible,
7e47976b 1163 .is_bin_visible = scsi_sdev_bin_attr_is_visible,
276b20d0
HR
1164};
1165
1166static const struct attribute_group *scsi_sdev_attr_groups[] = {
1167 &scsi_sdev_attr_group,
1168 NULL
1169};
1170
643eb2d9
JB
1171static int scsi_target_add(struct scsi_target *starget)
1172{
1173 int error;
1174
1175 if (starget->state != STARGET_CREATED)
1176 return 0;
1177
1178 error = device_add(&starget->dev);
1179 if (error) {
1180 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
643eb2d9
JB
1181 return error;
1182 }
1183 transport_add_device(&starget->dev);
1184 starget->state = STARGET_RUNNING;
1185
bc4f2401
AS
1186 pm_runtime_set_active(&starget->dev);
1187 pm_runtime_enable(&starget->dev);
1188 device_enable_async_suspend(&starget->dev);
1189
643eb2d9
JB
1190 return 0;
1191}
1192
1da177e4
LT
1193/**
1194 * scsi_sysfs_add_sdev - add scsi device to sysfs
1195 * @sdev: scsi_device to add
1196 *
1197 * Return value:
1198 * 0 on Success / non-zero on Failure
1199 **/
1200int scsi_sysfs_add_sdev(struct scsi_device *sdev)
1201{
1202 int error, i;
80ed71ce 1203 struct request_queue *rq = sdev->request_queue;
643eb2d9 1204 struct scsi_target *starget = sdev->sdev_target;
1da177e4 1205
ee37e09d
AS
1206 error = scsi_device_set_state(sdev, SDEV_RUNNING);
1207 if (error)
1da177e4
LT
1208 return error;
1209
643eb2d9
JB
1210 error = scsi_target_add(starget);
1211 if (error)
1212 return error;
1213
1214 transport_configure_device(&starget->dev);
bc4f2401 1215
4cb077d9 1216 device_enable_async_suspend(&sdev->sdev_gendev);
bc4f2401
AS
1217 scsi_autopm_get_target(starget);
1218 pm_runtime_set_active(&sdev->sdev_gendev);
1219 pm_runtime_forbid(&sdev->sdev_gendev);
1220 pm_runtime_enable(&sdev->sdev_gendev);
1221 scsi_autopm_put_target(starget);
1222
bc4f2401
AS
1223 scsi_autopm_get_device(sdev);
1224
086b91d0 1225 error = scsi_dh_add_device(sdev);
221255ae
HR
1226 if (error)
1227 /*
1228 * device_handler is optional, so any error can be ignored
1229 */
086b91d0
CH
1230 sdev_printk(KERN_INFO, sdev,
1231 "failed to add device handler: %d\n", error);
086b91d0 1232
4cd2459c
HR
1233 error = device_add(&sdev->sdev_gendev);
1234 if (error) {
1235 sdev_printk(KERN_INFO, sdev,
1236 "failed to add device: %d\n", error);
1237 scsi_dh_remove_device(sdev);
1238 return error;
1239 }
1240
4cb077d9 1241 device_enable_async_suspend(&sdev->sdev_dev);
ee959b00 1242 error = device_add(&sdev->sdev_dev);
1da177e4 1243 if (error) {
73d8c34f
AS
1244 sdev_printk(KERN_INFO, sdev,
1245 "failed to add class device: %d\n", error);
086b91d0 1246 scsi_dh_remove_device(sdev);
860dc736 1247 device_del(&sdev->sdev_gendev);
ee37e09d 1248 return error;
1da177e4 1249 }
860dc736
JB
1250 transport_add_device(&sdev->sdev_gendev);
1251 sdev->is_visible = 1;
1da177e4 1252
97f46ae4 1253 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
80ed71ce
JB
1254
1255 if (error)
860dc736
JB
1256 /* we're treating error on bsg register as non-fatal,
1257 * so pretend nothing went wrong */
80ed71ce
JB
1258 sdev_printk(KERN_INFO, sdev,
1259 "Failed to register bsg queue, errno=%d\n", error);
1260
bfd12944 1261 /* add additional host specific attributes */
1da177e4
LT
1262 if (sdev->host->hostt->sdev_attrs) {
1263 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
bfd12944 1264 error = device_create_file(&sdev->sdev_gendev,
1da177e4 1265 sdev->host->hostt->sdev_attrs[i]);
860dc736 1266 if (error)
ee37e09d 1267 return error;
1da177e4
LT
1268 }
1269 }
1da177e4 1270
6fe8c1db 1271 scsi_autopm_put_device(sdev);
1da177e4
LT
1272 return error;
1273}
1274
903f4fed 1275void __scsi_remove_device(struct scsi_device *sdev)
1da177e4 1276{
df133c21
JB
1277 struct device *dev = &sdev->sdev_gendev;
1278
be821fd8
VK
1279 /*
1280 * This cleanup path is not reentrant and while it is impossible
1281 * to get a new reference with scsi_device_get() someone can still
1282 * hold a previously acquired one.
1283 */
1284 if (sdev->sdev_state == SDEV_DEL)
1285 return;
1286
860dc736
JB
1287 if (sdev->is_visible) {
1288 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
1289 return;
1da177e4 1290
860dc736
JB
1291 bsg_unregister_queue(sdev->request_queue);
1292 device_unregister(&sdev->sdev_dev);
1293 transport_remove_device(dev);
086b91d0 1294 scsi_dh_remove_device(sdev);
e619e6cb
BVA
1295 device_del(dev);
1296 } else
1297 put_device(&sdev->sdev_dev);
b485462a
BVA
1298
1299 /*
1300 * Stop accepting new requests and wait until all queuecommand() and
1301 * scsi_run_queue() invocations have finished before tearing down the
1302 * device.
1303 */
1da177e4 1304 scsi_device_set_state(sdev, SDEV_DEL);
b485462a
BVA
1305 blk_cleanup_queue(sdev->request_queue);
1306 cancel_work_sync(&sdev->requeue_work);
1307
1da177e4
LT
1308 if (sdev->host->hostt->slave_destroy)
1309 sdev->host->hostt->slave_destroy(sdev);
df133c21 1310 transport_destroy_device(dev);
86cbfb56 1311
e63ed0d7
JB
1312 /*
1313 * Paired with the kref_get() in scsi_sysfs_initialize(). We have
1314 * remoed sysfs visibility from the device, so make the target
1315 * invisible if this was the last device underneath it.
1316 */
1317 scsi_target_reap(scsi_target(sdev));
1318
df133c21 1319 put_device(dev);
903f4fed
AS
1320}
1321
1322/**
1323 * scsi_remove_device - unregister a device from the scsi bus
1324 * @sdev: scsi_device to unregister
1325 **/
1326void scsi_remove_device(struct scsi_device *sdev)
1327{
54195002
AS
1328 struct Scsi_Host *shost = sdev->host;
1329
0b950672 1330 mutex_lock(&shost->scan_mutex);
903f4fed 1331 __scsi_remove_device(sdev);
0b950672 1332 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
1333}
1334EXPORT_SYMBOL(scsi_remove_device);
1335
44818efb 1336static void __scsi_remove_target(struct scsi_target *starget)
1da177e4
LT
1337{
1338 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1339 unsigned long flags;
a64358db 1340 struct scsi_device *sdev;
1da177e4
LT
1341
1342 spin_lock_irqsave(shost->host_lock, flags);
a64358db
AS
1343 restart:
1344 list_for_each_entry(sdev, &shost->__devices, siblings) {
1da177e4 1345 if (sdev->channel != starget->channel ||
a64358db 1346 sdev->id != starget->id ||
546ae796 1347 scsi_device_get(sdev))
1da177e4
LT
1348 continue;
1349 spin_unlock_irqrestore(shost->host_lock, flags);
1350 scsi_remove_device(sdev);
546ae796 1351 scsi_device_put(sdev);
1da177e4 1352 spin_lock_irqsave(shost->host_lock, flags);
a64358db 1353 goto restart;
1da177e4
LT
1354 }
1355 spin_unlock_irqrestore(shost->host_lock, flags);
20b1e674
PM
1356}
1357
1da177e4
LT
1358/**
1359 * scsi_remove_target - try to remove a target and all its devices
1360 * @dev: generic starget or parent of generic stargets to be removed
1361 *
1362 * Note: This is slightly racy. It is possible that if the user
1363 * requests the addition of another device then the target won't be
1364 * removed.
1365 */
1366void scsi_remove_target(struct device *dev)
1367{
3b661a92 1368 struct Scsi_Host *shost = dev_to_shost(dev->parent);
90a88d6e 1369 struct scsi_target *starget, *last_target = NULL;
3b661a92
DW
1370 unsigned long flags;
1371
40998193 1372restart:
3b661a92
DW
1373 spin_lock_irqsave(shost->host_lock, flags);
1374 list_for_each_entry(starget, &shost->__targets, siblings) {
90a88d6e
JB
1375 if (starget->state == STARGET_DEL ||
1376 starget == last_target)
3b661a92
DW
1377 continue;
1378 if (starget->dev.parent == dev || &starget->dev == dev) {
e63ed0d7 1379 kref_get(&starget->reap_ref);
90a88d6e 1380 last_target = starget;
bc3f02a7 1381 spin_unlock_irqrestore(shost->host_lock, flags);
bc3f02a7 1382 __scsi_remove_target(starget);
40998193
CH
1383 scsi_target_reap(starget);
1384 goto restart;
3b661a92 1385 }
1da177e4 1386 }
3b661a92 1387 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
1388}
1389EXPORT_SYMBOL(scsi_remove_target);
1390
1391int scsi_register_driver(struct device_driver *drv)
1392{
1393 drv->bus = &scsi_bus_type;
1394
1395 return driver_register(drv);
1396}
1397EXPORT_SYMBOL(scsi_register_driver);
1398
1399int scsi_register_interface(struct class_interface *intf)
1400{
1401 intf->class = &sdev_class;
1402
1403 return class_interface_register(intf);
1404}
1405EXPORT_SYMBOL(scsi_register_interface);
1406
1da177e4
LT
1407/**
1408 * scsi_sysfs_add_host - add scsi host to subsystem
1409 * @shost: scsi host struct to add to subsystem
1da177e4
LT
1410 **/
1411int scsi_sysfs_add_host(struct Scsi_Host *shost)
1412{
1413 int error, i;
1414
f7120a4f 1415 /* add host specific attributes */
1da177e4
LT
1416 if (shost->hostt->shost_attrs) {
1417 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
ee959b00 1418 error = device_create_file(&shost->shost_dev,
f7120a4f 1419 shost->hostt->shost_attrs[i]);
1da177e4
LT
1420 if (error)
1421 return error;
1422 }
1423 }
1424
1425 transport_register_device(&shost->shost_gendev);
d52b3815 1426 transport_configure_device(&shost->shost_gendev);
1da177e4
LT
1427 return 0;
1428}
1429
bfd12944
KS
1430static struct device_type scsi_dev_type = {
1431 .name = "scsi_device",
1432 .release = scsi_device_dev_release,
1433 .groups = scsi_sdev_attr_groups,
1434};
1435
1da177e4
LT
1436void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1437{
1438 unsigned long flags;
1439 struct Scsi_Host *shost = sdev->host;
1440 struct scsi_target *starget = sdev->sdev_target;
1441
1442 device_initialize(&sdev->sdev_gendev);
1443 sdev->sdev_gendev.bus = &scsi_bus_type;
bfd12944 1444 sdev->sdev_gendev.type = &scsi_dev_type;
9cb78c16 1445 dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
71610f55
KS
1446 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1447
ee959b00 1448 device_initialize(&sdev->sdev_dev);
37e6ba00 1449 sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
ee959b00 1450 sdev->sdev_dev.class = &sdev_class;
9cb78c16 1451 dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
71610f55 1452 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
50c4e964
AS
1453 /*
1454 * Get a default scsi_level from the target (derived from sibling
1455 * devices). This is the best we can do for guessing how to set
1456 * sdev->lun_in_cdb for the initial INQUIRY command. For LUN 0 the
1457 * setting doesn't matter, because all the bits are zero anyway.
1458 * But it does matter for higher LUNs.
1459 */
7c9d6f16 1460 sdev->scsi_level = starget->scsi_level;
50c4e964
AS
1461 if (sdev->scsi_level <= SCSI_2 &&
1462 sdev->scsi_level != SCSI_UNKNOWN &&
1463 !shost->no_scsi2_lun_in_cdb)
1464 sdev->lun_in_cdb = 1;
1465
1da177e4
LT
1466 transport_setup_device(&sdev->sdev_gendev);
1467 spin_lock_irqsave(shost->host_lock, flags);
1468 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1469 list_add_tail(&sdev->siblings, &shost->__devices);
1470 spin_unlock_irqrestore(shost->host_lock, flags);
e63ed0d7
JB
1471 /*
1472 * device can now only be removed via __scsi_remove_device() so hold
1473 * the target. Target will be held in CREATED state until something
1474 * beneath it becomes visible (in which case it moves to RUNNING)
1475 */
1476 kref_get(&starget->reap_ref);
1da177e4
LT
1477}
1478
1479int scsi_is_sdev_device(const struct device *dev)
1480{
13ba9bcb 1481 return dev->type == &scsi_dev_type;
1da177e4
LT
1482}
1483EXPORT_SYMBOL(scsi_is_sdev_device);
1484
1485/* A blank transport template that is used in drivers that don't
1486 * yet implement Transport Attributes */
1487struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };