Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[deliverable/linux.git] / drivers / scsi / scsi_sysfs.c
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
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/device.h>
14 #include <linux/pm_runtime.h>
15
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_tcq.h>
20 #include <scsi/scsi_dh.h>
21 #include <scsi/scsi_transport.h>
22 #include <scsi/scsi_driver.h>
23
24 #include "scsi_priv.h"
25 #include "scsi_logging.h"
26
27 static struct device_type scsi_dev_type;
28
29 static const struct {
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" },
39 { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
40 { SDEV_BLOCK, "blocked" },
41 { SDEV_CREATED_BLOCK, "created-blocked" },
42 };
43
44 const char *scsi_device_state_name(enum scsi_device_state state)
45 {
46 int i;
47 char *name = NULL;
48
49 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
50 if (sdev_states[i].value == state) {
51 name = sdev_states[i].name;
52 break;
53 }
54 }
55 return name;
56 }
57
58 static const struct {
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" },
67 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
68 { SHOST_DEL_RECOVERY, "deleted/recovery", },
69 };
70 const char *scsi_host_state_name(enum scsi_host_state state)
71 {
72 int i;
73 char *name = NULL;
74
75 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
76 if (shost_states[i].value == state) {
77 name = shost_states[i].name;
78 break;
79 }
80 }
81 return name;
82 }
83
84 #ifdef CONFIG_SCSI_DH
85 static 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
98 static const char *scsi_access_state_name(unsigned char state)
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 }
111 #endif
112
113 static int check_set(unsigned long long *val, char *src)
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 */
123 *val = simple_strtoull(src, &last, 0);
124 if (*last != '\0')
125 return 1;
126 }
127 return 0;
128 }
129
130 static int scsi_scan(struct Scsi_Host *shost, const char *str)
131 {
132 char s1[15], s2[15], s3[17], junk;
133 unsigned long long channel, id, lun;
134 int res;
135
136 res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
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;
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);
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) \
157 static ssize_t \
158 show_##name (struct device *dev, struct device_attribute *attr, \
159 char *buf) \
160 { \
161 struct Scsi_Host *shost = class_to_shost(dev); \
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) \
171 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
172
173 #define shost_rd_attr(field, format_string) \
174 shost_rd_attr2(field, field, format_string)
175
176 /*
177 * Create the actual show/store functions and data structures.
178 */
179
180 static ssize_t
181 store_scan(struct device *dev, struct device_attribute *attr,
182 const char *buf, size_t count)
183 {
184 struct Scsi_Host *shost = class_to_shost(dev);
185 int res;
186
187 res = scsi_scan(shost, buf);
188 if (res == 0)
189 res = count;
190 return res;
191 };
192 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
193
194 static ssize_t
195 store_shost_state(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
197 {
198 int i;
199 struct Scsi_Host *shost = class_to_shost(dev);
200 enum scsi_host_state state = 0;
201
202 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
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
218 static ssize_t
219 show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
220 {
221 struct Scsi_Host *shost = class_to_shost(dev);
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
230 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
231 static struct device_attribute dev_attr_hstate =
232 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
233
234 static ssize_t
235 show_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
250 static ssize_t
251 show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
252 char *buf)
253 {
254 struct Scsi_Host *shost = class_to_shost(dev);
255 unsigned int supported_mode = shost->hostt->supported_mode;
256
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);
262 }
263
264 static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
265
266 static ssize_t
267 show_shost_active_mode(struct device *dev,
268 struct device_attribute *attr, char *buf)
269 {
270 struct Scsi_Host *shost = class_to_shost(dev);
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
278 static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
279
280 static int check_reset_type(const char *str)
281 {
282 if (sysfs_streq(str, "adapter"))
283 return SCSI_ADAPTER_RESET;
284 else if (sysfs_streq(str, "firmware"))
285 return SCSI_FIRMWARE_RESET;
286 else
287 return 0;
288 }
289
290 static ssize_t
291 store_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;
297 int type;
298
299 type = check_reset_type(buf);
300 if (!type)
301 goto exit_store_host_reset;
302
303 if (sht->host_reset)
304 ret = sht->host_reset(shost, type);
305
306 exit_store_host_reset:
307 if (ret == 0)
308 ret = count;
309 return ret;
310 }
311
312 static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
313
314 static ssize_t
315 show_shost_eh_deadline(struct device *dev,
316 struct device_attribute *attr, char *buf)
317 {
318 struct Scsi_Host *shost = class_to_shost(dev);
319
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);
323 }
324
325 static ssize_t
326 store_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;
331 unsigned long deadline, flags;
332
333 if (shost->transportt &&
334 (shost->transportt->eh_strategy_handler ||
335 !shost->hostt->eh_host_reset_handler))
336 return ret;
337
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
355 shost->eh_deadline = deadline * HZ;
356
357 ret = count;
358 }
359 spin_unlock_irqrestore(shost->host_lock, flags);
360
361 return ret;
362 }
363
364 static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
365
366 shost_rd_attr(use_blk_mq, "%d\n");
367 shost_rd_attr(unique_id, "%u\n");
368 shost_rd_attr(cmd_per_lun, "%hd\n");
369 shost_rd_attr(can_queue, "%hd\n");
370 shost_rd_attr(sg_tablesize, "%hu\n");
371 shost_rd_attr(sg_prot_tablesize, "%hu\n");
372 shost_rd_attr(unchecked_isa_dma, "%d\n");
373 shost_rd_attr(prot_capabilities, "%u\n");
374 shost_rd_attr(prot_guard_type, "%hd\n");
375 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
376
377 static ssize_t
378 show_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 }
383 static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
384
385 static struct attribute *scsi_sysfs_shost_attrs[] = {
386 &dev_attr_use_blk_mq.attr,
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,
392 &dev_attr_sg_prot_tablesize.attr,
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,
399 &dev_attr_prot_capabilities.attr,
400 &dev_attr_prot_guard_type.attr,
401 &dev_attr_host_reset.attr,
402 &dev_attr_eh_deadline.attr,
403 NULL
404 };
405
406 static struct attribute_group scsi_shost_attr_group = {
407 .attrs = scsi_sysfs_shost_attrs,
408 };
409
410 const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
411 &scsi_shost_attr_group,
412 NULL
413 };
414
415 static void scsi_device_cls_release(struct device *class_dev)
416 {
417 struct scsi_device *sdev;
418
419 sdev = class_to_sdev(class_dev);
420 put_device(&sdev->sdev_gendev);
421 }
422
423 static void scsi_device_dev_release_usercontext(struct work_struct *work)
424 {
425 struct scsi_device *sdev;
426 struct device *parent;
427 struct list_head *this, *tmp;
428 unsigned long flags;
429
430 sdev = container_of(work, struct scsi_device, ew.work);
431
432 scsi_dh_release_device(sdev);
433
434 parent = sdev->sdev_gendev.parent;
435
436 spin_lock_irqsave(sdev->host->host_lock, flags);
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
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
452 blk_put_queue(sdev->request_queue);
453 /* NULL queue means the device can't be used */
454 sdev->request_queue = NULL;
455
456 kfree(sdev->vpd_pg83);
457 kfree(sdev->vpd_pg80);
458 kfree(sdev->inquiry);
459 kfree(sdev);
460
461 if (parent)
462 put_device(parent);
463 }
464
465 static void scsi_device_dev_release(struct device *dev)
466 {
467 struct scsi_device *sdp = to_scsi_device(dev);
468 execute_in_process_context(scsi_device_dev_release_usercontext,
469 &sdp->ew);
470 }
471
472 static struct class sdev_class = {
473 .name = "scsi_device",
474 .dev_release = scsi_device_cls_release,
475 };
476
477 /* all probing is done in the individual ->probe routines */
478 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
479 {
480 struct scsi_device *sdp;
481
482 if (dev->type != &scsi_dev_type)
483 return 0;
484
485 sdp = to_scsi_device(dev);
486 if (sdp->no_uld_attach)
487 return 0;
488 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
489 }
490
491 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
492 {
493 struct scsi_device *sdev;
494
495 if (dev->type != &scsi_dev_type)
496 return 0;
497
498 sdev = to_scsi_device(dev);
499
500 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
501 return 0;
502 }
503
504 struct bus_type scsi_bus_type = {
505 .name = "scsi",
506 .match = scsi_bus_match,
507 .uevent = scsi_bus_uevent,
508 #ifdef CONFIG_PM
509 .pm = &scsi_bus_pm_ops,
510 #endif
511 };
512 EXPORT_SYMBOL_GPL(scsi_bus_type);
513
514 int 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
528 void 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) \
539 static ssize_t \
540 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
541 char *buf) \
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) \
554 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
555
556
557 /*
558 * sdev_rw_attr: create a function and attribute variable for a
559 * read/write field.
560 */
561 #define sdev_rw_attr(field, format_string) \
562 sdev_show_function(field, format_string) \
563 \
564 static ssize_t \
565 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
566 const char *buf, size_t count) \
567 { \
568 struct scsi_device *sdev; \
569 sdev = to_scsi_device(dev); \
570 sscanf (buf, format_string, &sdev->field); \
571 return count; \
572 } \
573 static 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 \
585 static ssize_t \
586 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
587 const char *buf, size_t count) \
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 } \
599 static 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 */
605 static 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 */
621 sdev_rd_attr (type, "%d\n");
622 sdev_rd_attr (scsi_level, "%d\n");
623 sdev_rd_attr (vendor, "%.8s\n");
624 sdev_rd_attr (model, "%.16s\n");
625 sdev_rd_attr (rev, "%.4s\n");
626
627 static ssize_t
628 sdev_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 }
634 static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
635
636 static ssize_t
637 sdev_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 }
643 static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
644
645 /*
646 * TODO: can we make these symlinks to the block layer ones?
647 */
648 static ssize_t
649 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
650 {
651 struct scsi_device *sdev;
652 sdev = to_scsi_device(dev);
653 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
654 }
655
656 static ssize_t
657 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
658 const char *buf, size_t count)
659 {
660 struct scsi_device *sdev;
661 int timeout;
662 sdev = to_scsi_device(dev);
663 sscanf (buf, "%d\n", &timeout);
664 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
665 return count;
666 }
667 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
668
669 static ssize_t
670 sdev_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
677 static ssize_t
678 sdev_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 }
696 static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
697
698 static ssize_t
699 store_rescan_field (struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
701 {
702 scsi_rescan_device(dev);
703 return count;
704 }
705 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
706
707 static ssize_t
708 sdev_store_delete(struct device *dev, struct device_attribute *attr,
709 const char *buf, size_t count)
710 {
711 if (device_remove_file_self(dev, attr))
712 scsi_remove_device(to_scsi_device(dev));
713 return count;
714 };
715 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
716
717 static ssize_t
718 store_state_field(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
720 {
721 int i;
722 struct scsi_device *sdev = to_scsi_device(dev);
723 enum scsi_device_state state = 0;
724
725 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
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
741 static ssize_t
742 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
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
753 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
754
755 static ssize_t
756 show_queue_type_field(struct device *dev, struct device_attribute *attr,
757 char *buf)
758 {
759 struct scsi_device *sdev = to_scsi_device(dev);
760 const char *name = "none";
761
762 if (sdev->simple_tags)
763 name = "simple";
764
765 return snprintf(buf, 20, "%s\n", name);
766 }
767
768 static ssize_t
769 store_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);
773
774 if (!sdev->tagged_supported)
775 return -EINVAL;
776
777 sdev_printk(KERN_INFO, sdev,
778 "ignoring write to deprecated queue_type attribute");
779 return count;
780 }
781
782 static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
783 store_queue_type_field);
784
785 #define sdev_vpd_pg_attr(_page) \
786 static ssize_t \
787 show_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); \
793 int ret; \
794 if (!sdev->vpd_##_page) \
795 return -EINVAL; \
796 rcu_read_lock(); \
797 ret = memory_read_from_buffer(buf, count, &off, \
798 rcu_dereference(sdev->vpd_##_page), \
799 sdev->vpd_##_page##_len); \
800 rcu_read_unlock(); \
801 return ret; \
802 } \
803 static 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
809 sdev_vpd_pg_attr(pg83);
810 sdev_vpd_pg_attr(pg80);
811
812 static 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
826 static 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
835 static ssize_t
836 show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
837 char *buf)
838 {
839 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
840 }
841
842 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
843
844 #define show_sdev_iostat(field) \
845 static ssize_t \
846 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
847 char *buf) \
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 } \
853 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
854
855 show_sdev_iostat(iorequest_cnt);
856 show_sdev_iostat(iodone_cnt);
857 show_sdev_iostat(ioerr_cnt);
858
859 static ssize_t
860 sdev_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 }
866 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
867
868 #define DECLARE_EVT_SHOW(name, Cap_name) \
869 static ssize_t \
870 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
871 char *buf) \
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) \
879 static ssize_t \
880 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
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
901 DECLARE_EVT(media_change, MEDIA_CHANGE)
902 DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
903 DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
904 DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
905 DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
906 DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
907
908 static ssize_t
909 sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
910 const char *buf, size_t count)
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
921 if (depth < 1 || depth > sdev->host->can_queue)
922 return -EINVAL;
923
924 retval = sht->change_queue_depth(sdev, depth);
925 if (retval < 0)
926 return retval;
927
928 sdev->max_queue_depth = sdev->queue_depth;
929
930 return count;
931 }
932 sdev_show_function(queue_depth, "%d\n");
933
934 static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
935 sdev_store_queue_depth);
936
937 static ssize_t
938 sdev_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 }
951 static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
952
953 #ifdef CONFIG_SCSI_DH
954 static ssize_t
955 sdev_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
966 static ssize_t
967 sdev_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
1003 static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
1004 sdev_store_dh_state);
1005
1006 static ssize_t
1007 sdev_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 }
1024 static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
1025
1026 static ssize_t
1027 sdev_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 }
1041 static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
1042 #endif
1043
1044 static ssize_t
1045 sdev_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
1055 static ssize_t
1056 sdev_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);
1061 unsigned int period;
1062
1063 if (kstrtouint(buf, 10, &period))
1064 return -EINVAL;
1065
1066 sdev->queue_ramp_up_period = msecs_to_jiffies(period);
1067 return count;
1068 }
1069
1070 static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
1071 sdev_show_queue_ramp_up_period,
1072 sdev_store_queue_ramp_up_period);
1073
1074 static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
1075 struct attribute *attr, int i)
1076 {
1077 struct device *dev = container_of(kobj, struct device, kobj);
1078 struct scsi_device *sdev = to_scsi_device(dev);
1079
1080
1081 if (attr == &dev_attr_queue_depth.attr &&
1082 !sdev->host->hostt->change_queue_depth)
1083 return S_IRUGO;
1084
1085 if (attr == &dev_attr_queue_ramp_up_period.attr &&
1086 !sdev->host->hostt->change_queue_depth)
1087 return 0;
1088
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
1097 return attr->mode;
1098 }
1099
1100 static 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
1110 if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
1111 return 0;
1112
1113 return S_IRUGO;
1114 }
1115
1116 /* Default template for device attributes. May NOT be modified */
1117 static 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,
1137 &dev_attr_wwid.attr,
1138 #ifdef CONFIG_SCSI_DH
1139 &dev_attr_dh_state.attr,
1140 &dev_attr_access_state.attr,
1141 &dev_attr_preferred_path.attr,
1142 #endif
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
1153 static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1154 &dev_attr_vpd_pg83,
1155 &dev_attr_vpd_pg80,
1156 &dev_attr_inquiry,
1157 NULL
1158 };
1159 static struct attribute_group scsi_sdev_attr_group = {
1160 .attrs = scsi_sdev_attrs,
1161 .bin_attrs = scsi_sdev_bin_attrs,
1162 .is_visible = scsi_sdev_attr_is_visible,
1163 .is_bin_visible = scsi_sdev_bin_attr_is_visible,
1164 };
1165
1166 static const struct attribute_group *scsi_sdev_attr_groups[] = {
1167 &scsi_sdev_attr_group,
1168 NULL
1169 };
1170
1171 static 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);
1181 return error;
1182 }
1183 transport_add_device(&starget->dev);
1184 starget->state = STARGET_RUNNING;
1185
1186 pm_runtime_set_active(&starget->dev);
1187 pm_runtime_enable(&starget->dev);
1188 device_enable_async_suspend(&starget->dev);
1189
1190 return 0;
1191 }
1192
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 **/
1200 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
1201 {
1202 int error, i;
1203 struct request_queue *rq = sdev->request_queue;
1204 struct scsi_target *starget = sdev->sdev_target;
1205
1206 error = scsi_device_set_state(sdev, SDEV_RUNNING);
1207 if (error)
1208 return error;
1209
1210 error = scsi_target_add(starget);
1211 if (error)
1212 return error;
1213
1214 transport_configure_device(&starget->dev);
1215
1216 device_enable_async_suspend(&sdev->sdev_gendev);
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
1223 scsi_autopm_get_device(sdev);
1224
1225 error = scsi_dh_add_device(sdev);
1226 if (error)
1227 /*
1228 * device_handler is optional, so any error can be ignored
1229 */
1230 sdev_printk(KERN_INFO, sdev,
1231 "failed to add device handler: %d\n", error);
1232
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
1241 device_enable_async_suspend(&sdev->sdev_dev);
1242 error = device_add(&sdev->sdev_dev);
1243 if (error) {
1244 sdev_printk(KERN_INFO, sdev,
1245 "failed to add class device: %d\n", error);
1246 scsi_dh_remove_device(sdev);
1247 device_del(&sdev->sdev_gendev);
1248 return error;
1249 }
1250 transport_add_device(&sdev->sdev_gendev);
1251 sdev->is_visible = 1;
1252
1253 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
1254
1255 if (error)
1256 /* we're treating error on bsg register as non-fatal,
1257 * so pretend nothing went wrong */
1258 sdev_printk(KERN_INFO, sdev,
1259 "Failed to register bsg queue, errno=%d\n", error);
1260
1261 /* add additional host specific attributes */
1262 if (sdev->host->hostt->sdev_attrs) {
1263 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
1264 error = device_create_file(&sdev->sdev_gendev,
1265 sdev->host->hostt->sdev_attrs[i]);
1266 if (error)
1267 return error;
1268 }
1269 }
1270
1271 scsi_autopm_put_device(sdev);
1272 return error;
1273 }
1274
1275 void __scsi_remove_device(struct scsi_device *sdev)
1276 {
1277 struct device *dev = &sdev->sdev_gendev;
1278
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
1287 if (sdev->is_visible) {
1288 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
1289 return;
1290
1291 bsg_unregister_queue(sdev->request_queue);
1292 device_unregister(&sdev->sdev_dev);
1293 transport_remove_device(dev);
1294 scsi_dh_remove_device(sdev);
1295 device_del(dev);
1296 } else
1297 put_device(&sdev->sdev_dev);
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 */
1304 scsi_device_set_state(sdev, SDEV_DEL);
1305 blk_cleanup_queue(sdev->request_queue);
1306 cancel_work_sync(&sdev->requeue_work);
1307
1308 if (sdev->host->hostt->slave_destroy)
1309 sdev->host->hostt->slave_destroy(sdev);
1310 transport_destroy_device(dev);
1311
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
1319 put_device(dev);
1320 }
1321
1322 /**
1323 * scsi_remove_device - unregister a device from the scsi bus
1324 * @sdev: scsi_device to unregister
1325 **/
1326 void scsi_remove_device(struct scsi_device *sdev)
1327 {
1328 struct Scsi_Host *shost = sdev->host;
1329
1330 mutex_lock(&shost->scan_mutex);
1331 __scsi_remove_device(sdev);
1332 mutex_unlock(&shost->scan_mutex);
1333 }
1334 EXPORT_SYMBOL(scsi_remove_device);
1335
1336 static void __scsi_remove_target(struct scsi_target *starget)
1337 {
1338 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1339 unsigned long flags;
1340 struct scsi_device *sdev;
1341
1342 spin_lock_irqsave(shost->host_lock, flags);
1343 restart:
1344 list_for_each_entry(sdev, &shost->__devices, siblings) {
1345 if (sdev->channel != starget->channel ||
1346 sdev->id != starget->id ||
1347 scsi_device_get(sdev))
1348 continue;
1349 spin_unlock_irqrestore(shost->host_lock, flags);
1350 scsi_remove_device(sdev);
1351 scsi_device_put(sdev);
1352 spin_lock_irqsave(shost->host_lock, flags);
1353 goto restart;
1354 }
1355 spin_unlock_irqrestore(shost->host_lock, flags);
1356 }
1357
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 */
1366 void scsi_remove_target(struct device *dev)
1367 {
1368 struct Scsi_Host *shost = dev_to_shost(dev->parent);
1369 struct scsi_target *starget, *last_target = NULL;
1370 unsigned long flags;
1371
1372 restart:
1373 spin_lock_irqsave(shost->host_lock, flags);
1374 list_for_each_entry(starget, &shost->__targets, siblings) {
1375 if (starget->state == STARGET_DEL ||
1376 starget == last_target)
1377 continue;
1378 if (starget->dev.parent == dev || &starget->dev == dev) {
1379 kref_get(&starget->reap_ref);
1380 last_target = starget;
1381 spin_unlock_irqrestore(shost->host_lock, flags);
1382 __scsi_remove_target(starget);
1383 scsi_target_reap(starget);
1384 goto restart;
1385 }
1386 }
1387 spin_unlock_irqrestore(shost->host_lock, flags);
1388 }
1389 EXPORT_SYMBOL(scsi_remove_target);
1390
1391 int scsi_register_driver(struct device_driver *drv)
1392 {
1393 drv->bus = &scsi_bus_type;
1394
1395 return driver_register(drv);
1396 }
1397 EXPORT_SYMBOL(scsi_register_driver);
1398
1399 int scsi_register_interface(struct class_interface *intf)
1400 {
1401 intf->class = &sdev_class;
1402
1403 return class_interface_register(intf);
1404 }
1405 EXPORT_SYMBOL(scsi_register_interface);
1406
1407 /**
1408 * scsi_sysfs_add_host - add scsi host to subsystem
1409 * @shost: scsi host struct to add to subsystem
1410 **/
1411 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1412 {
1413 int error, i;
1414
1415 /* add host specific attributes */
1416 if (shost->hostt->shost_attrs) {
1417 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1418 error = device_create_file(&shost->shost_dev,
1419 shost->hostt->shost_attrs[i]);
1420 if (error)
1421 return error;
1422 }
1423 }
1424
1425 transport_register_device(&shost->shost_gendev);
1426 transport_configure_device(&shost->shost_gendev);
1427 return 0;
1428 }
1429
1430 static struct device_type scsi_dev_type = {
1431 .name = "scsi_device",
1432 .release = scsi_device_dev_release,
1433 .groups = scsi_sdev_attr_groups,
1434 };
1435
1436 void 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;
1444 sdev->sdev_gendev.type = &scsi_dev_type;
1445 dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
1446 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1447
1448 device_initialize(&sdev->sdev_dev);
1449 sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1450 sdev->sdev_dev.class = &sdev_class;
1451 dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
1452 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
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 */
1460 sdev->scsi_level = starget->scsi_level;
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
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);
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);
1477 }
1478
1479 int scsi_is_sdev_device(const struct device *dev)
1480 {
1481 return dev->type == &scsi_dev_type;
1482 }
1483 EXPORT_SYMBOL(scsi_is_sdev_device);
1484
1485 /* A blank transport template that is used in drivers that don't
1486 * yet implement Transport Attributes */
1487 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
This page took 0.181106 seconds and 5 git commands to generate.