GNU Linux-libre 4.4.294-gnu1
[releases.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_transport.h>
21 #include <scsi/scsi_driver.h>
22
23 #include "scsi_priv.h"
24 #include "scsi_logging.h"
25
26 static struct device_type scsi_dev_type;
27
28 static const struct {
29         enum scsi_device_state  value;
30         char                    *name;
31 } sdev_states[] = {
32         { SDEV_CREATED, "created" },
33         { SDEV_RUNNING, "running" },
34         { SDEV_CANCEL, "cancel" },
35         { SDEV_DEL, "deleted" },
36         { SDEV_QUIESCE, "quiesce" },
37         { SDEV_OFFLINE, "offline" },
38         { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
39         { SDEV_BLOCK,   "blocked" },
40         { SDEV_CREATED_BLOCK, "created-blocked" },
41 };
42
43 const char *scsi_device_state_name(enum scsi_device_state state)
44 {
45         int i;
46         char *name = NULL;
47
48         for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
49                 if (sdev_states[i].value == state) {
50                         name = sdev_states[i].name;
51                         break;
52                 }
53         }
54         return name;
55 }
56
57 static const struct {
58         enum scsi_host_state    value;
59         char                    *name;
60 } shost_states[] = {
61         { SHOST_CREATED, "created" },
62         { SHOST_RUNNING, "running" },
63         { SHOST_CANCEL, "cancel" },
64         { SHOST_DEL, "deleted" },
65         { SHOST_RECOVERY, "recovery" },
66         { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
67         { SHOST_DEL_RECOVERY, "deleted/recovery", },
68 };
69 const char *scsi_host_state_name(enum scsi_host_state state)
70 {
71         int i;
72         char *name = NULL;
73
74         for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
75                 if (shost_states[i].value == state) {
76                         name = shost_states[i].name;
77                         break;
78                 }
79         }
80         return name;
81 }
82
83 static int check_set(unsigned long long *val, char *src)
84 {
85         char *last;
86
87         if (strncmp(src, "-", 20) == 0) {
88                 *val = SCAN_WILD_CARD;
89         } else {
90                 /*
91                  * Doesn't check for int overflow
92                  */
93                 *val = simple_strtoull(src, &last, 0);
94                 if (*last != '\0')
95                         return 1;
96         }
97         return 0;
98 }
99
100 static int scsi_scan(struct Scsi_Host *shost, const char *str)
101 {
102         char s1[15], s2[15], s3[17], junk;
103         unsigned long long channel, id, lun;
104         int res;
105
106         res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
107         if (res != 3)
108                 return -EINVAL;
109         if (check_set(&channel, s1))
110                 return -EINVAL;
111         if (check_set(&id, s2))
112                 return -EINVAL;
113         if (check_set(&lun, s3))
114                 return -EINVAL;
115         if (shost->transportt->user_scan)
116                 res = shost->transportt->user_scan(shost, channel, id, lun);
117         else
118                 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
119         return res;
120 }
121
122 /*
123  * shost_show_function: macro to create an attr function that can be used to
124  * show a non-bit field.
125  */
126 #define shost_show_function(name, field, format_string)                 \
127 static ssize_t                                                          \
128 show_##name (struct device *dev, struct device_attribute *attr,         \
129              char *buf)                                                 \
130 {                                                                       \
131         struct Scsi_Host *shost = class_to_shost(dev);                  \
132         return snprintf (buf, 20, format_string, shost->field);         \
133 }
134
135 /*
136  * shost_rd_attr: macro to create a function and attribute variable for a
137  * read only field.
138  */
139 #define shost_rd_attr2(name, field, format_string)                      \
140         shost_show_function(name, field, format_string)                 \
141 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
142
143 #define shost_rd_attr(field, format_string) \
144 shost_rd_attr2(field, field, format_string)
145
146 /*
147  * Create the actual show/store functions and data structures.
148  */
149
150 static ssize_t
151 store_scan(struct device *dev, struct device_attribute *attr,
152            const char *buf, size_t count)
153 {
154         struct Scsi_Host *shost = class_to_shost(dev);
155         int res;
156
157         res = scsi_scan(shost, buf);
158         if (res == 0)
159                 res = count;
160         return res;
161 };
162 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
163
164 static ssize_t
165 store_shost_state(struct device *dev, struct device_attribute *attr,
166                   const char *buf, size_t count)
167 {
168         int i;
169         struct Scsi_Host *shost = class_to_shost(dev);
170         enum scsi_host_state state = 0;
171
172         for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
173                 const int len = strlen(shost_states[i].name);
174                 if (strncmp(shost_states[i].name, buf, len) == 0 &&
175                    buf[len] == '\n') {
176                         state = shost_states[i].value;
177                         break;
178                 }
179         }
180         if (!state)
181                 return -EINVAL;
182
183         if (scsi_host_set_state(shost, state))
184                 return -EINVAL;
185         return count;
186 }
187
188 static ssize_t
189 show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
190 {
191         struct Scsi_Host *shost = class_to_shost(dev);
192         const char *name = scsi_host_state_name(shost->shost_state);
193
194         if (!name)
195                 return -EINVAL;
196
197         return snprintf(buf, 20, "%s\n", name);
198 }
199
200 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
201 struct device_attribute dev_attr_hstate =
202         __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
203
204 static ssize_t
205 show_shost_mode(unsigned int mode, char *buf)
206 {
207         ssize_t len = 0;
208
209         if (mode & MODE_INITIATOR)
210                 len = sprintf(buf, "%s", "Initiator");
211
212         if (mode & MODE_TARGET)
213                 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
214
215         len += sprintf(buf + len, "\n");
216
217         return len;
218 }
219
220 static ssize_t
221 show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
222                           char *buf)
223 {
224         struct Scsi_Host *shost = class_to_shost(dev);
225         unsigned int supported_mode = shost->hostt->supported_mode;
226
227         if (supported_mode == MODE_UNKNOWN)
228                 /* by default this should be initiator */
229                 supported_mode = MODE_INITIATOR;
230
231         return show_shost_mode(supported_mode, buf);
232 }
233
234 static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
235
236 static ssize_t
237 show_shost_active_mode(struct device *dev,
238                        struct device_attribute *attr, char *buf)
239 {
240         struct Scsi_Host *shost = class_to_shost(dev);
241
242         if (shost->active_mode == MODE_UNKNOWN)
243                 return snprintf(buf, 20, "unknown\n");
244         else
245                 return show_shost_mode(shost->active_mode, buf);
246 }
247
248 static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
249
250 static int check_reset_type(const char *str)
251 {
252         if (sysfs_streq(str, "adapter"))
253                 return SCSI_ADAPTER_RESET;
254         else if (sysfs_streq(str, "firmware"))
255                 return SCSI_FIRMWARE_RESET;
256         else
257                 return 0;
258 }
259
260 static ssize_t
261 store_host_reset(struct device *dev, struct device_attribute *attr,
262                 const char *buf, size_t count)
263 {
264         struct Scsi_Host *shost = class_to_shost(dev);
265         struct scsi_host_template *sht = shost->hostt;
266         int ret = -EINVAL;
267         int type;
268
269         type = check_reset_type(buf);
270         if (!type)
271                 goto exit_store_host_reset;
272
273         if (sht->host_reset)
274                 ret = sht->host_reset(shost, type);
275
276 exit_store_host_reset:
277         if (ret == 0)
278                 ret = count;
279         return ret;
280 }
281
282 static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
283
284 static ssize_t
285 show_shost_eh_deadline(struct device *dev,
286                       struct device_attribute *attr, char *buf)
287 {
288         struct Scsi_Host *shost = class_to_shost(dev);
289
290         if (shost->eh_deadline == -1)
291                 return snprintf(buf, strlen("off") + 2, "off\n");
292         return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
293 }
294
295 static ssize_t
296 store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
297                 const char *buf, size_t count)
298 {
299         struct Scsi_Host *shost = class_to_shost(dev);
300         int ret = -EINVAL;
301         unsigned long deadline, flags;
302
303         if (shost->transportt &&
304             (shost->transportt->eh_strategy_handler ||
305              !shost->hostt->eh_host_reset_handler))
306                 return ret;
307
308         if (!strncmp(buf, "off", strlen("off")))
309                 deadline = -1;
310         else {
311                 ret = kstrtoul(buf, 10, &deadline);
312                 if (ret)
313                         return ret;
314                 if (deadline * HZ > UINT_MAX)
315                         return -EINVAL;
316         }
317
318         spin_lock_irqsave(shost->host_lock, flags);
319         if (scsi_host_in_recovery(shost))
320                 ret = -EBUSY;
321         else {
322                 if (deadline == -1)
323                         shost->eh_deadline = -1;
324                 else
325                         shost->eh_deadline = deadline * HZ;
326
327                 ret = count;
328         }
329         spin_unlock_irqrestore(shost->host_lock, flags);
330
331         return ret;
332 }
333
334 static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
335
336 shost_rd_attr(use_blk_mq, "%d\n");
337 shost_rd_attr(unique_id, "%u\n");
338 shost_rd_attr(cmd_per_lun, "%hd\n");
339 shost_rd_attr(can_queue, "%hd\n");
340 shost_rd_attr(sg_tablesize, "%hu\n");
341 shost_rd_attr(sg_prot_tablesize, "%hu\n");
342 shost_rd_attr(unchecked_isa_dma, "%d\n");
343 shost_rd_attr(prot_capabilities, "%u\n");
344 shost_rd_attr(prot_guard_type, "%hd\n");
345 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
346
347 static ssize_t
348 show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
349 {
350         struct Scsi_Host *shost = class_to_shost(dev);
351         return snprintf(buf, 20, "%d\n", atomic_read(&shost->host_busy));
352 }
353 static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
354
355 static struct attribute *scsi_sysfs_shost_attrs[] = {
356         &dev_attr_use_blk_mq.attr,
357         &dev_attr_unique_id.attr,
358         &dev_attr_host_busy.attr,
359         &dev_attr_cmd_per_lun.attr,
360         &dev_attr_can_queue.attr,
361         &dev_attr_sg_tablesize.attr,
362         &dev_attr_sg_prot_tablesize.attr,
363         &dev_attr_unchecked_isa_dma.attr,
364         &dev_attr_proc_name.attr,
365         &dev_attr_scan.attr,
366         &dev_attr_hstate.attr,
367         &dev_attr_supported_mode.attr,
368         &dev_attr_active_mode.attr,
369         &dev_attr_prot_capabilities.attr,
370         &dev_attr_prot_guard_type.attr,
371         &dev_attr_host_reset.attr,
372         &dev_attr_eh_deadline.attr,
373         NULL
374 };
375
376 struct attribute_group scsi_shost_attr_group = {
377         .attrs =        scsi_sysfs_shost_attrs,
378 };
379
380 const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
381         &scsi_shost_attr_group,
382         NULL
383 };
384
385 static void scsi_device_cls_release(struct device *class_dev)
386 {
387         struct scsi_device *sdev;
388
389         sdev = class_to_sdev(class_dev);
390         put_device(&sdev->sdev_gendev);
391 }
392
393 static void scsi_device_dev_release_usercontext(struct work_struct *work)
394 {
395         struct scsi_device *sdev;
396         struct device *parent;
397         struct list_head *this, *tmp;
398         unsigned long flags;
399         struct module *mod;
400
401         sdev = container_of(work, struct scsi_device, ew.work);
402
403         mod = sdev->host->hostt->module;
404
405         scsi_dh_release_device(sdev);
406
407         parent = sdev->sdev_gendev.parent;
408
409         spin_lock_irqsave(sdev->host->host_lock, flags);
410         list_del(&sdev->siblings);
411         list_del(&sdev->same_target_siblings);
412         list_del(&sdev->starved_entry);
413         spin_unlock_irqrestore(sdev->host->host_lock, flags);
414
415         cancel_work_sync(&sdev->event_work);
416
417         list_for_each_safe(this, tmp, &sdev->event_list) {
418                 struct scsi_event *evt;
419
420                 evt = list_entry(this, struct scsi_event, node);
421                 list_del(&evt->node);
422                 kfree(evt);
423         }
424
425         blk_put_queue(sdev->request_queue);
426         /* NULL queue means the device can't be used */
427         sdev->request_queue = NULL;
428
429         kfree(sdev->vpd_pg83);
430         kfree(sdev->vpd_pg80);
431         kfree(sdev->inquiry);
432         kfree(sdev);
433
434         if (parent)
435                 put_device(parent);
436         module_put(mod);
437 }
438
439 static void scsi_device_dev_release(struct device *dev)
440 {
441         struct scsi_device *sdp = to_scsi_device(dev);
442
443         /* Set module pointer as NULL in case of module unloading */
444         if (!try_module_get(sdp->host->hostt->module))
445                 sdp->host->hostt->module = NULL;
446
447         execute_in_process_context(scsi_device_dev_release_usercontext,
448                                    &sdp->ew);
449 }
450
451 static struct class sdev_class = {
452         .name           = "scsi_device",
453         .dev_release    = scsi_device_cls_release,
454 };
455
456 /* all probing is done in the individual ->probe routines */
457 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
458 {
459         struct scsi_device *sdp;
460
461         if (dev->type != &scsi_dev_type)
462                 return 0;
463
464         sdp = to_scsi_device(dev);
465         if (sdp->no_uld_attach)
466                 return 0;
467         return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
468 }
469
470 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
471 {
472         struct scsi_device *sdev;
473
474         if (dev->type != &scsi_dev_type)
475                 return 0;
476
477         sdev = to_scsi_device(dev);
478
479         add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
480         return 0;
481 }
482
483 struct bus_type scsi_bus_type = {
484         .name           = "scsi",
485         .match          = scsi_bus_match,
486         .uevent         = scsi_bus_uevent,
487 #ifdef CONFIG_PM
488         .pm             = &scsi_bus_pm_ops,
489 #endif
490 };
491 EXPORT_SYMBOL_GPL(scsi_bus_type);
492
493 int scsi_sysfs_register(void)
494 {
495         int error;
496
497         error = bus_register(&scsi_bus_type);
498         if (!error) {
499                 error = class_register(&sdev_class);
500                 if (error)
501                         bus_unregister(&scsi_bus_type);
502         }
503
504         return error;
505 }
506
507 void scsi_sysfs_unregister(void)
508 {
509         class_unregister(&sdev_class);
510         bus_unregister(&scsi_bus_type);
511 }
512
513 /*
514  * sdev_show_function: macro to create an attr function that can be used to
515  * show a non-bit field.
516  */
517 #define sdev_show_function(field, format_string)                                \
518 static ssize_t                                                          \
519 sdev_show_##field (struct device *dev, struct device_attribute *attr,   \
520                    char *buf)                                           \
521 {                                                                       \
522         struct scsi_device *sdev;                                       \
523         sdev = to_scsi_device(dev);                                     \
524         return snprintf (buf, 20, format_string, sdev->field);          \
525 }                                                                       \
526
527 /*
528  * sdev_rd_attr: macro to create a function and attribute variable for a
529  * read only field.
530  */
531 #define sdev_rd_attr(field, format_string)                              \
532         sdev_show_function(field, format_string)                        \
533 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
534
535
536 /*
537  * sdev_rw_attr: create a function and attribute variable for a
538  * read/write field.
539  */
540 #define sdev_rw_attr(field, format_string)                              \
541         sdev_show_function(field, format_string)                                \
542                                                                         \
543 static ssize_t                                                          \
544 sdev_store_##field (struct device *dev, struct device_attribute *attr,  \
545                     const char *buf, size_t count)                      \
546 {                                                                       \
547         struct scsi_device *sdev;                                       \
548         sdev = to_scsi_device(dev);                                     \
549         sscanf (buf, format_string, &sdev->field);                      \
550         return count;                                                   \
551 }                                                                       \
552 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
553
554 /* Currently we don't export bit fields, but we might in future,
555  * so leave this code in */
556 #if 0
557 /*
558  * sdev_rd_attr: create a function and attribute variable for a
559  * read/write bit field.
560  */
561 #define sdev_rw_attr_bit(field)                                         \
562         sdev_show_function(field, "%d\n")                                       \
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         int ret;                                                        \
569         struct scsi_device *sdev;                                       \
570         ret = scsi_sdev_check_buf_bit(buf);                             \
571         if (ret >= 0)   {                                               \
572                 sdev = to_scsi_device(dev);                             \
573                 sdev->field = ret;                                      \
574                 ret = count;                                            \
575         }                                                               \
576         return ret;                                                     \
577 }                                                                       \
578 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
579
580 /*
581  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
582  * else return -EINVAL.
583  */
584 static int scsi_sdev_check_buf_bit(const char *buf)
585 {
586         if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
587                 if (buf[0] == '1')
588                         return 1;
589                 else if (buf[0] == '0')
590                         return 0;
591                 else 
592                         return -EINVAL;
593         } else
594                 return -EINVAL;
595 }
596 #endif
597 /*
598  * Create the actual show/store functions and data structures.
599  */
600 sdev_rd_attr (type, "%d\n");
601 sdev_rd_attr (scsi_level, "%d\n");
602 sdev_rd_attr (vendor, "%.8s\n");
603 sdev_rd_attr (model, "%.16s\n");
604 sdev_rd_attr (rev, "%.4s\n");
605
606 static ssize_t
607 sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
608                 char *buf)
609 {
610         struct scsi_device *sdev = to_scsi_device(dev);
611         return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_busy));
612 }
613 static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
614
615 static ssize_t
616 sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
617                 char *buf)
618 {
619         struct scsi_device *sdev = to_scsi_device(dev);
620         return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
621 }
622 static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
623
624 /*
625  * TODO: can we make these symlinks to the block layer ones?
626  */
627 static ssize_t
628 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
629 {
630         struct scsi_device *sdev;
631         sdev = to_scsi_device(dev);
632         return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
633 }
634
635 static ssize_t
636 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
637                     const char *buf, size_t count)
638 {
639         struct scsi_device *sdev;
640         int timeout;
641         sdev = to_scsi_device(dev);
642         sscanf (buf, "%d\n", &timeout);
643         blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
644         return count;
645 }
646 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
647
648 static ssize_t
649 sdev_show_eh_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, "%u\n", sdev->eh_timeout / HZ);
654 }
655
656 static ssize_t
657 sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
658                     const char *buf, size_t count)
659 {
660         struct scsi_device *sdev;
661         unsigned int eh_timeout;
662         int err;
663
664         if (!capable(CAP_SYS_ADMIN))
665                 return -EACCES;
666
667         sdev = to_scsi_device(dev);
668         err = kstrtouint(buf, 10, &eh_timeout);
669         if (err)
670                 return err;
671         sdev->eh_timeout = eh_timeout * HZ;
672
673         return count;
674 }
675 static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
676
677 static ssize_t
678 store_rescan_field (struct device *dev, struct device_attribute *attr,
679                     const char *buf, size_t count)
680 {
681         scsi_rescan_device(dev);
682         return count;
683 }
684 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
685
686 static ssize_t
687 sdev_store_delete(struct device *dev, struct device_attribute *attr,
688                   const char *buf, size_t count)
689 {
690         struct kernfs_node *kn;
691         struct scsi_device *sdev = to_scsi_device(dev);
692
693         /*
694          * We need to try to get module, avoiding the module been removed
695          * during delete.
696          */
697         if (scsi_device_get(sdev))
698                 return -ENODEV;
699
700         kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
701         WARN_ON_ONCE(!kn);
702         /*
703          * Concurrent writes into the "delete" sysfs attribute may trigger
704          * concurrent calls to device_remove_file() and scsi_remove_device().
705          * device_remove_file() handles concurrent removal calls by
706          * serializing these and by ignoring the second and later removal
707          * attempts.  Concurrent calls of scsi_remove_device() are
708          * serialized. The second and later calls of scsi_remove_device() are
709          * ignored because the first call of that function changes the device
710          * state into SDEV_DEL.
711          */
712         device_remove_file(dev, attr);
713         scsi_remove_device(sdev);
714         if (kn)
715                 sysfs_unbreak_active_protection(kn);
716         scsi_device_put(sdev);
717         return count;
718 };
719 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
720
721 static ssize_t
722 store_state_field(struct device *dev, struct device_attribute *attr,
723                   const char *buf, size_t count)
724 {
725         int i;
726         struct scsi_device *sdev = to_scsi_device(dev);
727         enum scsi_device_state state = 0;
728
729         for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
730                 const int len = strlen(sdev_states[i].name);
731                 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
732                    buf[len] == '\n') {
733                         state = sdev_states[i].value;
734                         break;
735                 }
736         }
737         if (!state)
738                 return -EINVAL;
739
740         if (scsi_device_set_state(sdev, state))
741                 return -EINVAL;
742         return count;
743 }
744
745 static ssize_t
746 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
747 {
748         struct scsi_device *sdev = to_scsi_device(dev);
749         const char *name = scsi_device_state_name(sdev->sdev_state);
750
751         if (!name)
752                 return -EINVAL;
753
754         return snprintf(buf, 20, "%s\n", name);
755 }
756
757 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
758
759 static ssize_t
760 show_queue_type_field(struct device *dev, struct device_attribute *attr,
761                       char *buf)
762 {
763         struct scsi_device *sdev = to_scsi_device(dev);
764         const char *name = "none";
765
766         if (sdev->simple_tags)
767                 name = "simple";
768
769         return snprintf(buf, 20, "%s\n", name);
770 }
771
772 static ssize_t
773 store_queue_type_field(struct device *dev, struct device_attribute *attr,
774                        const char *buf, size_t count)
775 {
776         struct scsi_device *sdev = to_scsi_device(dev);
777
778         if (!sdev->tagged_supported)
779                 return -EINVAL;
780                 
781         sdev_printk(KERN_INFO, sdev,
782                     "ignoring write to deprecated queue_type attribute");
783         return count;
784 }
785
786 static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
787                    store_queue_type_field);
788
789 #define sdev_vpd_pg_attr(_page)                                         \
790 static ssize_t                                                  \
791 show_vpd_##_page(struct file *filp, struct kobject *kobj,       \
792                  struct bin_attribute *bin_attr,                        \
793                  char *buf, loff_t off, size_t count)                   \
794 {                                                                       \
795         struct device *dev = container_of(kobj, struct device, kobj);   \
796         struct scsi_device *sdev = to_scsi_device(dev);                 \
797         if (!sdev->vpd_##_page)                                         \
798                 return -EINVAL;                                         \
799         return memory_read_from_buffer(buf, count, &off,                \
800                                        sdev->vpd_##_page,               \
801                                        sdev->vpd_##_page##_len);        \
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_queue_ramp_up_period(struct device *dev,
939                                struct device_attribute *attr,
940                                char *buf)
941 {
942         struct scsi_device *sdev;
943         sdev = to_scsi_device(dev);
944         return snprintf(buf, 20, "%u\n",
945                         jiffies_to_msecs(sdev->queue_ramp_up_period));
946 }
947
948 static ssize_t
949 sdev_store_queue_ramp_up_period(struct device *dev,
950                                 struct device_attribute *attr,
951                                 const char *buf, size_t count)
952 {
953         struct scsi_device *sdev = to_scsi_device(dev);
954         unsigned int period;
955
956         if (kstrtouint(buf, 10, &period))
957                 return -EINVAL;
958
959         sdev->queue_ramp_up_period = msecs_to_jiffies(period);
960         return count;
961 }
962
963 static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
964                    sdev_show_queue_ramp_up_period,
965                    sdev_store_queue_ramp_up_period);
966
967 static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
968                                          struct attribute *attr, int i)
969 {
970         struct device *dev = container_of(kobj, struct device, kobj);
971         struct scsi_device *sdev = to_scsi_device(dev);
972
973
974         if (attr == &dev_attr_queue_depth.attr &&
975             !sdev->host->hostt->change_queue_depth)
976                 return S_IRUGO;
977
978         if (attr == &dev_attr_queue_ramp_up_period.attr &&
979             !sdev->host->hostt->change_queue_depth)
980                 return 0;
981
982         return attr->mode;
983 }
984
985 /* Default template for device attributes.  May NOT be modified */
986 static struct attribute *scsi_sdev_attrs[] = {
987         &dev_attr_device_blocked.attr,
988         &dev_attr_type.attr,
989         &dev_attr_scsi_level.attr,
990         &dev_attr_device_busy.attr,
991         &dev_attr_vendor.attr,
992         &dev_attr_model.attr,
993         &dev_attr_rev.attr,
994         &dev_attr_rescan.attr,
995         &dev_attr_delete.attr,
996         &dev_attr_state.attr,
997         &dev_attr_timeout.attr,
998         &dev_attr_eh_timeout.attr,
999         &dev_attr_iocounterbits.attr,
1000         &dev_attr_iorequest_cnt.attr,
1001         &dev_attr_iodone_cnt.attr,
1002         &dev_attr_ioerr_cnt.attr,
1003         &dev_attr_modalias.attr,
1004         &dev_attr_queue_depth.attr,
1005         &dev_attr_queue_type.attr,
1006         &dev_attr_queue_ramp_up_period.attr,
1007         REF_EVT(media_change),
1008         REF_EVT(inquiry_change_reported),
1009         REF_EVT(capacity_change_reported),
1010         REF_EVT(soft_threshold_reached),
1011         REF_EVT(mode_parameter_change_reported),
1012         REF_EVT(lun_change_reported),
1013         NULL
1014 };
1015
1016 static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1017         &dev_attr_vpd_pg83,
1018         &dev_attr_vpd_pg80,
1019         &dev_attr_inquiry,
1020         NULL
1021 };
1022 static struct attribute_group scsi_sdev_attr_group = {
1023         .attrs =        scsi_sdev_attrs,
1024         .bin_attrs =    scsi_sdev_bin_attrs,
1025         .is_visible =   scsi_sdev_attr_is_visible,
1026 };
1027
1028 static const struct attribute_group *scsi_sdev_attr_groups[] = {
1029         &scsi_sdev_attr_group,
1030         NULL
1031 };
1032
1033 static int scsi_target_add(struct scsi_target *starget)
1034 {
1035         int error;
1036
1037         if (starget->state != STARGET_CREATED)
1038                 return 0;
1039
1040         error = device_add(&starget->dev);
1041         if (error) {
1042                 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
1043                 return error;
1044         }
1045         transport_add_device(&starget->dev);
1046         starget->state = STARGET_RUNNING;
1047
1048         pm_runtime_set_active(&starget->dev);
1049         pm_runtime_enable(&starget->dev);
1050         device_enable_async_suspend(&starget->dev);
1051
1052         return 0;
1053 }
1054
1055 /**
1056  * scsi_sysfs_add_sdev - add scsi device to sysfs
1057  * @sdev:       scsi_device to add
1058  *
1059  * Return value:
1060  *      0 on Success / non-zero on Failure
1061  **/
1062 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
1063 {
1064         int error, i;
1065         struct request_queue *rq = sdev->request_queue;
1066         struct scsi_target *starget = sdev->sdev_target;
1067
1068         error = scsi_target_add(starget);
1069         if (error)
1070                 return error;
1071
1072         transport_configure_device(&starget->dev);
1073
1074         device_enable_async_suspend(&sdev->sdev_gendev);
1075         scsi_autopm_get_target(starget);
1076         pm_runtime_set_active(&sdev->sdev_gendev);
1077         pm_runtime_forbid(&sdev->sdev_gendev);
1078         pm_runtime_enable(&sdev->sdev_gendev);
1079         scsi_autopm_put_target(starget);
1080
1081         scsi_autopm_get_device(sdev);
1082
1083         error = device_add(&sdev->sdev_gendev);
1084         if (error) {
1085                 sdev_printk(KERN_INFO, sdev,
1086                                 "failed to add device: %d\n", error);
1087                 return error;
1088         }
1089
1090         error = scsi_dh_add_device(sdev);
1091         if (error)
1092                 /*
1093                  * device_handler is optional, so any error can be ignored
1094                  */
1095                 sdev_printk(KERN_INFO, sdev,
1096                                 "failed to add device handler: %d\n", error);
1097
1098         device_enable_async_suspend(&sdev->sdev_dev);
1099         error = device_add(&sdev->sdev_dev);
1100         if (error) {
1101                 sdev_printk(KERN_INFO, sdev,
1102                                 "failed to add class device: %d\n", error);
1103                 scsi_dh_remove_device(sdev);
1104                 device_del(&sdev->sdev_gendev);
1105                 return error;
1106         }
1107         transport_add_device(&sdev->sdev_gendev);
1108         sdev->is_visible = 1;
1109
1110         error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
1111
1112         if (error)
1113                 /* we're treating error on bsg register as non-fatal,
1114                  * so pretend nothing went wrong */
1115                 sdev_printk(KERN_INFO, sdev,
1116                             "Failed to register bsg queue, errno=%d\n", error);
1117
1118         /* add additional host specific attributes */
1119         if (sdev->host->hostt->sdev_attrs) {
1120                 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
1121                         error = device_create_file(&sdev->sdev_gendev,
1122                                         sdev->host->hostt->sdev_attrs[i]);
1123                         if (error)
1124                                 return error;
1125                 }
1126         }
1127
1128         scsi_autopm_put_device(sdev);
1129         return error;
1130 }
1131
1132 void __scsi_remove_device(struct scsi_device *sdev)
1133 {
1134         struct device *dev = &sdev->sdev_gendev;
1135
1136         /*
1137          * This cleanup path is not reentrant and while it is impossible
1138          * to get a new reference with scsi_device_get() someone can still
1139          * hold a previously acquired one.
1140          */
1141         if (sdev->sdev_state == SDEV_DEL)
1142                 return;
1143
1144         if (sdev->is_visible) {
1145                 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
1146                         return;
1147
1148                 bsg_unregister_queue(sdev->request_queue);
1149                 device_unregister(&sdev->sdev_dev);
1150                 transport_remove_device(dev);
1151                 scsi_dh_remove_device(sdev);
1152                 device_del(dev);
1153         } else
1154                 put_device(&sdev->sdev_dev);
1155
1156         /*
1157          * Stop accepting new requests and wait until all queuecommand() and
1158          * scsi_run_queue() invocations have finished before tearing down the
1159          * device.
1160          */
1161         scsi_device_set_state(sdev, SDEV_DEL);
1162         blk_cleanup_queue(sdev->request_queue);
1163         cancel_work_sync(&sdev->requeue_work);
1164
1165         if (sdev->host->hostt->slave_destroy)
1166                 sdev->host->hostt->slave_destroy(sdev);
1167         transport_destroy_device(dev);
1168
1169         /*
1170          * Paired with the kref_get() in scsi_sysfs_initialize().  We have
1171          * remoed sysfs visibility from the device, so make the target
1172          * invisible if this was the last device underneath it.
1173          */
1174         scsi_target_reap(scsi_target(sdev));
1175
1176         put_device(dev);
1177 }
1178
1179 /**
1180  * scsi_remove_device - unregister a device from the scsi bus
1181  * @sdev:       scsi_device to unregister
1182  **/
1183 void scsi_remove_device(struct scsi_device *sdev)
1184 {
1185         struct Scsi_Host *shost = sdev->host;
1186
1187         mutex_lock(&shost->scan_mutex);
1188         __scsi_remove_device(sdev);
1189         mutex_unlock(&shost->scan_mutex);
1190 }
1191 EXPORT_SYMBOL(scsi_remove_device);
1192
1193 static void __scsi_remove_target(struct scsi_target *starget)
1194 {
1195         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1196         unsigned long flags;
1197         struct scsi_device *sdev;
1198
1199         spin_lock_irqsave(shost->host_lock, flags);
1200  restart:
1201         list_for_each_entry(sdev, &shost->__devices, siblings) {
1202                 if (sdev->channel != starget->channel ||
1203                     sdev->id != starget->id ||
1204                     scsi_device_get(sdev))
1205                         continue;
1206                 spin_unlock_irqrestore(shost->host_lock, flags);
1207                 scsi_remove_device(sdev);
1208                 scsi_device_put(sdev);
1209                 spin_lock_irqsave(shost->host_lock, flags);
1210                 goto restart;
1211         }
1212         spin_unlock_irqrestore(shost->host_lock, flags);
1213 }
1214
1215 /**
1216  * scsi_remove_target - try to remove a target and all its devices
1217  * @dev: generic starget or parent of generic stargets to be removed
1218  *
1219  * Note: This is slightly racy.  It is possible that if the user
1220  * requests the addition of another device then the target won't be
1221  * removed.
1222  */
1223 void scsi_remove_target(struct device *dev)
1224 {
1225         struct Scsi_Host *shost = dev_to_shost(dev->parent);
1226         struct scsi_target *starget;
1227         unsigned long flags;
1228
1229 restart:
1230         spin_lock_irqsave(shost->host_lock, flags);
1231         list_for_each_entry(starget, &shost->__targets, siblings) {
1232                 if (starget->state == STARGET_DEL ||
1233                     starget->state == STARGET_REMOVE ||
1234                     starget->state == STARGET_CREATED_REMOVE)
1235                         continue;
1236                 if (starget->dev.parent == dev || &starget->dev == dev) {
1237                         kref_get(&starget->reap_ref);
1238                         if (starget->state == STARGET_CREATED)
1239                                 starget->state = STARGET_CREATED_REMOVE;
1240                         else
1241                                 starget->state = STARGET_REMOVE;
1242                         spin_unlock_irqrestore(shost->host_lock, flags);
1243                         __scsi_remove_target(starget);
1244                         scsi_target_reap(starget);
1245                         goto restart;
1246                 }
1247         }
1248         spin_unlock_irqrestore(shost->host_lock, flags);
1249 }
1250 EXPORT_SYMBOL(scsi_remove_target);
1251
1252 int scsi_register_driver(struct device_driver *drv)
1253 {
1254         drv->bus = &scsi_bus_type;
1255
1256         return driver_register(drv);
1257 }
1258 EXPORT_SYMBOL(scsi_register_driver);
1259
1260 int scsi_register_interface(struct class_interface *intf)
1261 {
1262         intf->class = &sdev_class;
1263
1264         return class_interface_register(intf);
1265 }
1266 EXPORT_SYMBOL(scsi_register_interface);
1267
1268 /**
1269  * scsi_sysfs_add_host - add scsi host to subsystem
1270  * @shost:     scsi host struct to add to subsystem
1271  **/
1272 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1273 {
1274         int error, i;
1275
1276         /* add host specific attributes */
1277         if (shost->hostt->shost_attrs) {
1278                 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1279                         error = device_create_file(&shost->shost_dev,
1280                                         shost->hostt->shost_attrs[i]);
1281                         if (error)
1282                                 return error;
1283                 }
1284         }
1285
1286         transport_register_device(&shost->shost_gendev);
1287         transport_configure_device(&shost->shost_gendev);
1288         return 0;
1289 }
1290
1291 static struct device_type scsi_dev_type = {
1292         .name =         "scsi_device",
1293         .release =      scsi_device_dev_release,
1294         .groups =       scsi_sdev_attr_groups,
1295 };
1296
1297 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1298 {
1299         unsigned long flags;
1300         struct Scsi_Host *shost = sdev->host;
1301         struct scsi_target  *starget = sdev->sdev_target;
1302
1303         device_initialize(&sdev->sdev_gendev);
1304         sdev->sdev_gendev.bus = &scsi_bus_type;
1305         sdev->sdev_gendev.type = &scsi_dev_type;
1306         dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
1307                      sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1308
1309         device_initialize(&sdev->sdev_dev);
1310         sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1311         sdev->sdev_dev.class = &sdev_class;
1312         dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
1313                      sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1314         /*
1315          * Get a default scsi_level from the target (derived from sibling
1316          * devices).  This is the best we can do for guessing how to set
1317          * sdev->lun_in_cdb for the initial INQUIRY command.  For LUN 0 the
1318          * setting doesn't matter, because all the bits are zero anyway.
1319          * But it does matter for higher LUNs.
1320          */
1321         sdev->scsi_level = starget->scsi_level;
1322         if (sdev->scsi_level <= SCSI_2 &&
1323                         sdev->scsi_level != SCSI_UNKNOWN &&
1324                         !shost->no_scsi2_lun_in_cdb)
1325                 sdev->lun_in_cdb = 1;
1326
1327         transport_setup_device(&sdev->sdev_gendev);
1328         spin_lock_irqsave(shost->host_lock, flags);
1329         list_add_tail(&sdev->same_target_siblings, &starget->devices);
1330         list_add_tail(&sdev->siblings, &shost->__devices);
1331         spin_unlock_irqrestore(shost->host_lock, flags);
1332         /*
1333          * device can now only be removed via __scsi_remove_device() so hold
1334          * the target.  Target will be held in CREATED state until something
1335          * beneath it becomes visible (in which case it moves to RUNNING)
1336          */
1337         kref_get(&starget->reap_ref);
1338 }
1339
1340 int scsi_is_sdev_device(const struct device *dev)
1341 {
1342         return dev->type == &scsi_dev_type;
1343 }
1344 EXPORT_SYMBOL(scsi_is_sdev_device);
1345
1346 /* A blank transport template that is used in drivers that don't
1347  * yet implement Transport Attributes */
1348 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };