[SCSI] Unify SAM_ and SAM_STAT_ macros
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / scsi_sysfs.c
CommitLineData
1da177e4
LT
1/*
2 * scsi_sysfs.c
3 *
4 * SCSI sysfs interface routines.
5 *
6 * Created to pull SCSI mid layer sysfs routines into one file.
7 */
8
1da177e4 9#include <linux/module.h>
5a0e3ad6 10#include <linux/slab.h>
1da177e4
LT
11#include <linux/init.h>
12#include <linux/blkdev.h>
13#include <linux/device.h>
14
15#include <scsi/scsi.h>
16#include <scsi/scsi_device.h>
17#include <scsi/scsi_host.h>
18#include <scsi/scsi_tcq.h>
19#include <scsi/scsi_transport.h>
44818efb 20#include <scsi/scsi_driver.h>
1da177e4
LT
21
22#include "scsi_priv.h"
23#include "scsi_logging.h"
24
b0ed4336
HR
25static struct device_type scsi_dev_type;
26
0ad78200 27static const struct {
1da177e4
LT
28 enum scsi_device_state value;
29 char *name;
30} sdev_states[] = {
31 { SDEV_CREATED, "created" },
32 { SDEV_RUNNING, "running" },
33 { SDEV_CANCEL, "cancel" },
34 { SDEV_DEL, "deleted" },
35 { SDEV_QUIESCE, "quiesce" },
36 { SDEV_OFFLINE, "offline" },
37 { SDEV_BLOCK, "blocked" },
6f4267e3 38 { SDEV_CREATED_BLOCK, "created-blocked" },
1da177e4
LT
39};
40
41const char *scsi_device_state_name(enum scsi_device_state state)
42{
43 int i;
44 char *name = NULL;
45
6391a113 46 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
47 if (sdev_states[i].value == state) {
48 name = sdev_states[i].name;
49 break;
50 }
51 }
52 return name;
53}
54
0ad78200 55static const struct {
d3301874
MA
56 enum scsi_host_state value;
57 char *name;
58} shost_states[] = {
59 { SHOST_CREATED, "created" },
60 { SHOST_RUNNING, "running" },
61 { SHOST_CANCEL, "cancel" },
62 { SHOST_DEL, "deleted" },
63 { SHOST_RECOVERY, "recovery" },
939647ee
JB
64 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
65 { SHOST_DEL_RECOVERY, "deleted/recovery", },
d3301874
MA
66};
67const char *scsi_host_state_name(enum scsi_host_state state)
68{
69 int i;
70 char *name = NULL;
71
6391a113 72 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
d3301874
MA
73 if (shost_states[i].value == state) {
74 name = shost_states[i].name;
75 break;
76 }
77 }
78 return name;
79}
80
1da177e4
LT
81static int check_set(unsigned int *val, char *src)
82{
83 char *last;
84
85 if (strncmp(src, "-", 20) == 0) {
86 *val = SCAN_WILD_CARD;
87 } else {
88 /*
89 * Doesn't check for int overflow
90 */
91 *val = simple_strtoul(src, &last, 0);
92 if (*last != '\0')
93 return 1;
94 }
95 return 0;
96}
97
98static int scsi_scan(struct Scsi_Host *shost, const char *str)
99{
100 char s1[15], s2[15], s3[15], junk;
101 unsigned int channel, id, lun;
102 int res;
103
104 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
105 if (res != 3)
106 return -EINVAL;
107 if (check_set(&channel, s1))
108 return -EINVAL;
109 if (check_set(&id, s2))
110 return -EINVAL;
111 if (check_set(&lun, s3))
112 return -EINVAL;
e02f3f59
CH
113 if (shost->transportt->user_scan)
114 res = shost->transportt->user_scan(shost, channel, id, lun);
115 else
116 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
1da177e4
LT
117 return res;
118}
119
120/*
121 * shost_show_function: macro to create an attr function that can be used to
122 * show a non-bit field.
123 */
124#define shost_show_function(name, field, format_string) \
125static ssize_t \
ee959b00
TJ
126show_##name (struct device *dev, struct device_attribute *attr, \
127 char *buf) \
1da177e4 128{ \
ee959b00 129 struct Scsi_Host *shost = class_to_shost(dev); \
1da177e4
LT
130 return snprintf (buf, 20, format_string, shost->field); \
131}
132
133/*
134 * shost_rd_attr: macro to create a function and attribute variable for a
135 * read only field.
136 */
137#define shost_rd_attr2(name, field, format_string) \
138 shost_show_function(name, field, format_string) \
ee959b00 139static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
1da177e4
LT
140
141#define shost_rd_attr(field, format_string) \
142shost_rd_attr2(field, field, format_string)
143
144/*
145 * Create the actual show/store functions and data structures.
146 */
147
ee959b00
TJ
148static ssize_t
149store_scan(struct device *dev, struct device_attribute *attr,
150 const char *buf, size_t count)
1da177e4 151{
ee959b00 152 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
153 int res;
154
155 res = scsi_scan(shost, buf);
156 if (res == 0)
157 res = count;
158 return res;
159};
ee959b00 160static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1da177e4 161
d3301874 162static ssize_t
ee959b00
TJ
163store_shost_state(struct device *dev, struct device_attribute *attr,
164 const char *buf, size_t count)
d3301874
MA
165{
166 int i;
ee959b00 167 struct Scsi_Host *shost = class_to_shost(dev);
d3301874
MA
168 enum scsi_host_state state = 0;
169
6391a113 170 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
d3301874
MA
171 const int len = strlen(shost_states[i].name);
172 if (strncmp(shost_states[i].name, buf, len) == 0 &&
173 buf[len] == '\n') {
174 state = shost_states[i].value;
175 break;
176 }
177 }
178 if (!state)
179 return -EINVAL;
180
181 if (scsi_host_set_state(shost, state))
182 return -EINVAL;
183 return count;
184}
185
186static ssize_t
ee959b00 187show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
d3301874 188{
ee959b00 189 struct Scsi_Host *shost = class_to_shost(dev);
d3301874
MA
190 const char *name = scsi_host_state_name(shost->shost_state);
191
192 if (!name)
193 return -EINVAL;
194
195 return snprintf(buf, 20, "%s\n", name);
196}
197
ee959b00
TJ
198/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
199struct device_attribute dev_attr_hstate =
200 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
d3301874 201
5dc2b89e
FT
202static ssize_t
203show_shost_mode(unsigned int mode, char *buf)
204{
205 ssize_t len = 0;
206
207 if (mode & MODE_INITIATOR)
208 len = sprintf(buf, "%s", "Initiator");
209
210 if (mode & MODE_TARGET)
211 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
212
213 len += sprintf(buf + len, "\n");
214
215 return len;
216}
217
ee959b00
TJ
218static ssize_t
219show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
220 char *buf)
5dc2b89e 221{
ee959b00 222 struct Scsi_Host *shost = class_to_shost(dev);
7a39ac3f 223 unsigned int supported_mode = shost->hostt->supported_mode;
5dc2b89e 224
7a39ac3f
JB
225 if (supported_mode == MODE_UNKNOWN)
226 /* by default this should be initiator */
227 supported_mode = MODE_INITIATOR;
228
229 return show_shost_mode(supported_mode, buf);
5dc2b89e
FT
230}
231
ee959b00 232static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
5dc2b89e 233
ee959b00
TJ
234static ssize_t
235show_shost_active_mode(struct device *dev,
236 struct device_attribute *attr, char *buf)
5dc2b89e 237{
ee959b00 238 struct Scsi_Host *shost = class_to_shost(dev);
5dc2b89e
FT
239
240 if (shost->active_mode == MODE_UNKNOWN)
241 return snprintf(buf, 20, "unknown\n");
242 else
243 return show_shost_mode(shost->active_mode, buf);
244}
245
ee959b00 246static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
5dc2b89e 247
1da177e4
LT
248shost_rd_attr(unique_id, "%u\n");
249shost_rd_attr(host_busy, "%hu\n");
250shost_rd_attr(cmd_per_lun, "%hd\n");
ed632da8 251shost_rd_attr(can_queue, "%hd\n");
1da177e4
LT
252shost_rd_attr(sg_tablesize, "%hu\n");
253shost_rd_attr(unchecked_isa_dma, "%d\n");
4469f987
MP
254shost_rd_attr(prot_capabilities, "%u\n");
255shost_rd_attr(prot_guard_type, "%hd\n");
1da177e4
LT
256shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
257
f7120a4f
HR
258static struct attribute *scsi_sysfs_shost_attrs[] = {
259 &dev_attr_unique_id.attr,
260 &dev_attr_host_busy.attr,
261 &dev_attr_cmd_per_lun.attr,
262 &dev_attr_can_queue.attr,
263 &dev_attr_sg_tablesize.attr,
264 &dev_attr_unchecked_isa_dma.attr,
265 &dev_attr_proc_name.attr,
266 &dev_attr_scan.attr,
267 &dev_attr_hstate.attr,
268 &dev_attr_supported_mode.attr,
269 &dev_attr_active_mode.attr,
4469f987
MP
270 &dev_attr_prot_capabilities.attr,
271 &dev_attr_prot_guard_type.attr,
f7120a4f
HR
272 NULL
273};
274
275struct attribute_group scsi_shost_attr_group = {
276 .attrs = scsi_sysfs_shost_attrs,
277};
278
a4dbd674 279const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
f7120a4f 280 &scsi_shost_attr_group,
1da177e4
LT
281 NULL
282};
283
ee959b00 284static void scsi_device_cls_release(struct device *class_dev)
1da177e4
LT
285{
286 struct scsi_device *sdev;
287
288 sdev = class_to_sdev(class_dev);
289 put_device(&sdev->sdev_gendev);
290}
291
65f27f38 292static void scsi_device_dev_release_usercontext(struct work_struct *work)
1da177e4
LT
293{
294 struct scsi_device *sdev;
295 struct device *parent;
296 struct scsi_target *starget;
a341cd0f 297 struct list_head *this, *tmp;
1da177e4
LT
298 unsigned long flags;
299
65f27f38
DH
300 sdev = container_of(work, struct scsi_device, ew.work);
301
302 parent = sdev->sdev_gendev.parent;
1da177e4
LT
303 starget = to_scsi_target(parent);
304
305 spin_lock_irqsave(sdev->host->host_lock, flags);
306 starget->reap_ref++;
307 list_del(&sdev->siblings);
308 list_del(&sdev->same_target_siblings);
309 list_del(&sdev->starved_entry);
310 spin_unlock_irqrestore(sdev->host->host_lock, flags);
311
a341cd0f
JG
312 cancel_work_sync(&sdev->event_work);
313
314 list_for_each_safe(this, tmp, &sdev->event_list) {
315 struct scsi_event *evt;
316
317 evt = list_entry(this, struct scsi_event, node);
318 list_del(&evt->node);
319 kfree(evt);
320 }
321
1da177e4
LT
322 if (sdev->request_queue) {
323 sdev->request_queue->queuedata = NULL;
65110b21 324 /* user context needed to free queue */
1da177e4 325 scsi_free_queue(sdev->request_queue);
c2a9331c
JB
326 /* temporary expedient, try to catch use of queue lock
327 * after free of sdev */
328 sdev->request_queue = NULL;
1da177e4
LT
329 }
330
331 scsi_target_reap(scsi_target(sdev));
332
333 kfree(sdev->inquiry);
334 kfree(sdev);
335
336 if (parent)
337 put_device(parent);
338}
339
65110b21
JB
340static void scsi_device_dev_release(struct device *dev)
341{
ffedb452 342 struct scsi_device *sdp = to_scsi_device(dev);
65f27f38 343 execute_in_process_context(scsi_device_dev_release_usercontext,
ffedb452 344 &sdp->ew);
65110b21
JB
345}
346
52c1da39 347static struct class sdev_class = {
1da177e4 348 .name = "scsi_device",
ee959b00 349 .dev_release = scsi_device_cls_release,
1da177e4
LT
350};
351
352/* all probing is done in the individual ->probe routines */
353static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
354{
b0ed4336
HR
355 struct scsi_device *sdp;
356
357 if (dev->type != &scsi_dev_type)
358 return 0;
359
360 sdp = to_scsi_device(dev);
1da177e4
LT
361 if (sdp->no_uld_attach)
362 return 0;
363 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
364}
365
7eff2e7a 366static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
d7b8bcb0 367{
1f42ea7b
JB
368 struct scsi_device *sdev;
369
370 if (dev->type != &scsi_dev_type)
371 return 0;
372
373 sdev = to_scsi_device(dev);
d7b8bcb0 374
7eff2e7a 375 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
d7b8bcb0
MT
376 return 0;
377}
378
9b847548
JA
379static int scsi_bus_suspend(struct device * dev, pm_message_t state)
380{
b0ed4336
HR
381 struct device_driver *drv;
382 struct scsi_device *sdev;
9b847548
JA
383 int err;
384
b0ed4336
HR
385 if (dev->type != &scsi_dev_type)
386 return 0;
387
388 drv = dev->driver;
389 sdev = to_scsi_device(dev);
390
9b847548
JA
391 err = scsi_device_quiesce(sdev);
392 if (err)
393 return err;
394
c3c94c5a
TH
395 if (drv && drv->suspend) {
396 err = drv->suspend(dev, state);
397 if (err)
398 return err;
399 }
400
c3c94c5a 401 return 0;
9b847548
JA
402}
403
404static int scsi_bus_resume(struct device * dev)
405{
b0ed4336
HR
406 struct device_driver *drv;
407 struct scsi_device *sdev;
1dfcda06 408 int err = 0;
9b847548 409
b0ed4336
HR
410 if (dev->type != &scsi_dev_type)
411 return 0;
412
413 drv = dev->driver;
414 sdev = to_scsi_device(dev);
415
c3c94c5a 416 if (drv && drv->resume)
1dfcda06 417 err = drv->resume(dev);
c3c94c5a 418
9b847548 419 scsi_device_resume(sdev);
c3c94c5a 420
1dfcda06 421 return err;
9b847548
JA
422}
423
1da177e4
LT
424struct bus_type scsi_bus_type = {
425 .name = "scsi",
426 .match = scsi_bus_match,
d7b8bcb0 427 .uevent = scsi_bus_uevent,
9b847548
JA
428 .suspend = scsi_bus_suspend,
429 .resume = scsi_bus_resume,
1da177e4 430};
a6a8d9f8 431EXPORT_SYMBOL_GPL(scsi_bus_type);
1da177e4
LT
432
433int scsi_sysfs_register(void)
434{
435 int error;
436
437 error = bus_register(&scsi_bus_type);
438 if (!error) {
439 error = class_register(&sdev_class);
440 if (error)
441 bus_unregister(&scsi_bus_type);
442 }
443
444 return error;
445}
446
447void scsi_sysfs_unregister(void)
448{
449 class_unregister(&sdev_class);
450 bus_unregister(&scsi_bus_type);
451}
452
453/*
454 * sdev_show_function: macro to create an attr function that can be used to
455 * show a non-bit field.
456 */
457#define sdev_show_function(field, format_string) \
458static ssize_t \
ee959b00
TJ
459sdev_show_##field (struct device *dev, struct device_attribute *attr, \
460 char *buf) \
1da177e4
LT
461{ \
462 struct scsi_device *sdev; \
463 sdev = to_scsi_device(dev); \
464 return snprintf (buf, 20, format_string, sdev->field); \
465} \
466
467/*
468 * sdev_rd_attr: macro to create a function and attribute variable for a
469 * read only field.
470 */
471#define sdev_rd_attr(field, format_string) \
472 sdev_show_function(field, format_string) \
473static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
474
475
476/*
160e7f67 477 * sdev_rw_attr: create a function and attribute variable for a
1da177e4
LT
478 * read/write field.
479 */
480#define sdev_rw_attr(field, format_string) \
481 sdev_show_function(field, format_string) \
482 \
483static ssize_t \
ee959b00
TJ
484sdev_store_##field (struct device *dev, struct device_attribute *attr, \
485 const char *buf, size_t count) \
1da177e4
LT
486{ \
487 struct scsi_device *sdev; \
488 sdev = to_scsi_device(dev); \
160e7f67 489 sscanf (buf, format_string, &sdev->field); \
1da177e4
LT
490 return count; \
491} \
492static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
493
494/* Currently we don't export bit fields, but we might in future,
495 * so leave this code in */
496#if 0
497/*
498 * sdev_rd_attr: create a function and attribute variable for a
499 * read/write bit field.
500 */
501#define sdev_rw_attr_bit(field) \
502 sdev_show_function(field, "%d\n") \
503 \
504static ssize_t \
ee959b00
TJ
505sdev_store_##field (struct device *dev, struct device_attribute *attr, \
506 const char *buf, size_t count) \
1da177e4
LT
507{ \
508 int ret; \
509 struct scsi_device *sdev; \
510 ret = scsi_sdev_check_buf_bit(buf); \
511 if (ret >= 0) { \
512 sdev = to_scsi_device(dev); \
513 sdev->field = ret; \
514 ret = count; \
515 } \
516 return ret; \
517} \
518static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
519
520/*
521 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
522 * else return -EINVAL.
523 */
524static int scsi_sdev_check_buf_bit(const char *buf)
525{
526 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
527 if (buf[0] == '1')
528 return 1;
529 else if (buf[0] == '0')
530 return 0;
531 else
532 return -EINVAL;
533 } else
534 return -EINVAL;
535}
536#endif
537/*
538 * Create the actual show/store functions and data structures.
539 */
540sdev_rd_attr (device_blocked, "%d\n");
541sdev_rd_attr (queue_depth, "%d\n");
542sdev_rd_attr (type, "%d\n");
543sdev_rd_attr (scsi_level, "%d\n");
544sdev_rd_attr (vendor, "%.8s\n");
545sdev_rd_attr (model, "%.16s\n");
546sdev_rd_attr (rev, "%.4s\n");
547
242f9dcb
JA
548/*
549 * TODO: can we make these symlinks to the block layer ones?
550 */
1da177e4 551static ssize_t
10523b3b 552sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
553{
554 struct scsi_device *sdev;
555 sdev = to_scsi_device(dev);
242f9dcb 556 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
1da177e4
LT
557}
558
559static ssize_t
ee959b00
TJ
560sdev_store_timeout (struct device *dev, struct device_attribute *attr,
561 const char *buf, size_t count)
1da177e4
LT
562{
563 struct scsi_device *sdev;
564 int timeout;
565 sdev = to_scsi_device(dev);
566 sscanf (buf, "%d\n", &timeout);
242f9dcb 567 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
1da177e4
LT
568 return count;
569}
570static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
571
572static ssize_t
ee959b00
TJ
573store_rescan_field (struct device *dev, struct device_attribute *attr,
574 const char *buf, size_t count)
1da177e4
LT
575{
576 scsi_rescan_device(dev);
577 return count;
578}
579static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
580
d9a9cdfb
AS
581static void sdev_store_delete_callback(struct device *dev)
582{
583 scsi_remove_device(to_scsi_device(dev));
584}
585
ee959b00
TJ
586static ssize_t
587sdev_store_delete(struct device *dev, struct device_attribute *attr,
588 const char *buf, size_t count)
1da177e4 589{
d9a9cdfb
AS
590 int rc;
591
592 /* An attribute cannot be unregistered by one of its own methods,
593 * so we have to use this roundabout approach.
594 */
595 rc = device_schedule_callback(dev, sdev_store_delete_callback);
596 if (rc)
597 count = rc;
1da177e4
LT
598 return count;
599};
600static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
601
602static ssize_t
ee959b00
TJ
603store_state_field(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
1da177e4
LT
605{
606 int i;
607 struct scsi_device *sdev = to_scsi_device(dev);
608 enum scsi_device_state state = 0;
609
6391a113 610 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
611 const int len = strlen(sdev_states[i].name);
612 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
613 buf[len] == '\n') {
614 state = sdev_states[i].value;
615 break;
616 }
617 }
618 if (!state)
619 return -EINVAL;
620
621 if (scsi_device_set_state(sdev, state))
622 return -EINVAL;
623 return count;
624}
625
626static ssize_t
10523b3b 627show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
628{
629 struct scsi_device *sdev = to_scsi_device(dev);
630 const char *name = scsi_device_state_name(sdev->sdev_state);
631
632 if (!name)
633 return -EINVAL;
634
635 return snprintf(buf, 20, "%s\n", name);
636}
637
638static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
639
640static ssize_t
ee959b00
TJ
641show_queue_type_field(struct device *dev, struct device_attribute *attr,
642 char *buf)
1da177e4
LT
643{
644 struct scsi_device *sdev = to_scsi_device(dev);
645 const char *name = "none";
646
647 if (sdev->ordered_tags)
648 name = "ordered";
649 else if (sdev->simple_tags)
650 name = "simple";
651
652 return snprintf(buf, 20, "%s\n", name);
653}
654
655static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
656
657static ssize_t
ee959b00 658show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
659{
660 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
661}
662
663static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
664
665#define show_sdev_iostat(field) \
666static ssize_t \
ee959b00
TJ
667show_iostat_##field(struct device *dev, struct device_attribute *attr, \
668 char *buf) \
1da177e4
LT
669{ \
670 struct scsi_device *sdev = to_scsi_device(dev); \
671 unsigned long long count = atomic_read(&sdev->field); \
672 return snprintf(buf, 20, "0x%llx\n", count); \
673} \
674static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
675
676show_sdev_iostat(iorequest_cnt);
677show_sdev_iostat(iodone_cnt);
678show_sdev_iostat(ioerr_cnt);
679
d7b8bcb0
MT
680static ssize_t
681sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
682{
683 struct scsi_device *sdev;
684 sdev = to_scsi_device(dev);
685 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
686}
687static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
1da177e4 688
a341cd0f
JG
689#define DECLARE_EVT_SHOW(name, Cap_name) \
690static ssize_t \
691sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
ee959b00 692 char *buf) \
a341cd0f
JG
693{ \
694 struct scsi_device *sdev = to_scsi_device(dev); \
695 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
696 return snprintf(buf, 20, "%d\n", val); \
697}
698
699#define DECLARE_EVT_STORE(name, Cap_name) \
700static ssize_t \
ee959b00 701sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
a341cd0f
JG
702 const char *buf, size_t count) \
703{ \
704 struct scsi_device *sdev = to_scsi_device(dev); \
705 int val = simple_strtoul(buf, NULL, 0); \
706 if (val == 0) \
707 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
708 else if (val == 1) \
709 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
710 else \
711 return -EINVAL; \
712 return count; \
713}
714
715#define DECLARE_EVT(name, Cap_name) \
716 DECLARE_EVT_SHOW(name, Cap_name) \
717 DECLARE_EVT_STORE(name, Cap_name) \
718 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
719 sdev_store_evt_##name);
720#define REF_EVT(name) &dev_attr_evt_##name.attr
721
722DECLARE_EVT(media_change, MEDIA_CHANGE)
723
1da177e4 724/* Default template for device attributes. May NOT be modified */
bfd12944
KS
725static struct attribute *scsi_sdev_attrs[] = {
726 &dev_attr_device_blocked.attr,
727 &dev_attr_type.attr,
728 &dev_attr_scsi_level.attr,
729 &dev_attr_vendor.attr,
730 &dev_attr_model.attr,
731 &dev_attr_rev.attr,
732 &dev_attr_rescan.attr,
733 &dev_attr_delete.attr,
734 &dev_attr_state.attr,
735 &dev_attr_timeout.attr,
736 &dev_attr_iocounterbits.attr,
737 &dev_attr_iorequest_cnt.attr,
738 &dev_attr_iodone_cnt.attr,
739 &dev_attr_ioerr_cnt.attr,
740 &dev_attr_modalias.attr,
a341cd0f 741 REF_EVT(media_change),
bfd12944
KS
742 NULL
743};
744
745static struct attribute_group scsi_sdev_attr_group = {
746 .attrs = scsi_sdev_attrs,
747};
748
a4dbd674 749static const struct attribute_group *scsi_sdev_attr_groups[] = {
bfd12944 750 &scsi_sdev_attr_group,
1da177e4
LT
751 NULL
752};
753
ee959b00
TJ
754static ssize_t
755sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
756 const char *buf, size_t count)
1da177e4
LT
757{
758 int depth, retval;
759 struct scsi_device *sdev = to_scsi_device(dev);
760 struct scsi_host_template *sht = sdev->host->hostt;
761
762 if (!sht->change_queue_depth)
763 return -EINVAL;
764
765 depth = simple_strtoul(buf, NULL, 0);
766
767 if (depth < 1)
768 return -EINVAL;
769
e881a172
MC
770 retval = sht->change_queue_depth(sdev, depth,
771 SCSI_QDEPTH_DEFAULT);
1da177e4
LT
772 if (retval < 0)
773 return retval;
774
4a84067d
VD
775 sdev->max_queue_depth = sdev->queue_depth;
776
1da177e4
LT
777 return count;
778}
779
780static struct device_attribute sdev_attr_queue_depth_rw =
781 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
782 sdev_store_queue_depth_rw);
783
4a84067d
VD
784static ssize_t
785sdev_show_queue_ramp_up_period(struct device *dev,
786 struct device_attribute *attr,
787 char *buf)
788{
789 struct scsi_device *sdev;
790 sdev = to_scsi_device(dev);
791 return snprintf(buf, 20, "%u\n",
792 jiffies_to_msecs(sdev->queue_ramp_up_period));
793}
794
795static ssize_t
796sdev_store_queue_ramp_up_period(struct device *dev,
797 struct device_attribute *attr,
798 const char *buf, size_t count)
799{
800 struct scsi_device *sdev = to_scsi_device(dev);
801 unsigned long period;
802
803 if (strict_strtoul(buf, 10, &period))
804 return -EINVAL;
805
806 sdev->queue_ramp_up_period = msecs_to_jiffies(period);
807 return period;
808}
809
810static struct device_attribute sdev_attr_queue_ramp_up_period =
811 __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
812 sdev_show_queue_ramp_up_period,
813 sdev_store_queue_ramp_up_period);
814
ee959b00
TJ
815static ssize_t
816sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
1da177e4
LT
818{
819 struct scsi_device *sdev = to_scsi_device(dev);
820 struct scsi_host_template *sht = sdev->host->hostt;
821 int tag_type = 0, retval;
822 int prev_tag_type = scsi_get_tag_type(sdev);
823
824 if (!sdev->tagged_supported || !sht->change_queue_type)
825 return -EINVAL;
826
827 if (strncmp(buf, "ordered", 7) == 0)
828 tag_type = MSG_ORDERED_TAG;
829 else if (strncmp(buf, "simple", 6) == 0)
830 tag_type = MSG_SIMPLE_TAG;
831 else if (strncmp(buf, "none", 4) != 0)
832 return -EINVAL;
833
834 if (tag_type == prev_tag_type)
835 return count;
836
837 retval = sht->change_queue_type(sdev, tag_type);
838 if (retval < 0)
839 return retval;
840
841 return count;
842}
843
643eb2d9
JB
844static int scsi_target_add(struct scsi_target *starget)
845{
846 int error;
847
848 if (starget->state != STARGET_CREATED)
849 return 0;
850
4cb077d9
RW
851 device_enable_async_suspend(&starget->dev);
852
643eb2d9
JB
853 error = device_add(&starget->dev);
854 if (error) {
855 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
643eb2d9
JB
856 return error;
857 }
858 transport_add_device(&starget->dev);
859 starget->state = STARGET_RUNNING;
860
861 return 0;
862}
863
1da177e4
LT
864static struct device_attribute sdev_attr_queue_type_rw =
865 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
866 sdev_store_queue_type_rw);
867
1da177e4
LT
868/**
869 * scsi_sysfs_add_sdev - add scsi device to sysfs
870 * @sdev: scsi_device to add
871 *
872 * Return value:
873 * 0 on Success / non-zero on Failure
874 **/
875int scsi_sysfs_add_sdev(struct scsi_device *sdev)
876{
877 int error, i;
80ed71ce 878 struct request_queue *rq = sdev->request_queue;
643eb2d9 879 struct scsi_target *starget = sdev->sdev_target;
1da177e4 880
ee37e09d
AS
881 error = scsi_device_set_state(sdev, SDEV_RUNNING);
882 if (error)
1da177e4
LT
883 return error;
884
643eb2d9
JB
885 error = scsi_target_add(starget);
886 if (error)
887 return error;
888
889 transport_configure_device(&starget->dev);
4cb077d9 890 device_enable_async_suspend(&sdev->sdev_gendev);
1da177e4
LT
891 error = device_add(&sdev->sdev_gendev);
892 if (error) {
1da177e4 893 printk(KERN_INFO "error 1\n");
ee37e09d 894 return error;
1da177e4 895 }
4cb077d9 896 device_enable_async_suspend(&sdev->sdev_dev);
ee959b00 897 error = device_add(&sdev->sdev_dev);
1da177e4
LT
898 if (error) {
899 printk(KERN_INFO "error 2\n");
860dc736 900 device_del(&sdev->sdev_gendev);
ee37e09d 901 return error;
1da177e4 902 }
860dc736
JB
903 transport_add_device(&sdev->sdev_gendev);
904 sdev->is_visible = 1;
1da177e4 905
bfd12944 906 /* create queue files, which may be writable, depending on the host */
4a84067d
VD
907 if (sdev->host->hostt->change_queue_depth) {
908 error = device_create_file(&sdev->sdev_gendev,
909 &sdev_attr_queue_depth_rw);
910 error = device_create_file(&sdev->sdev_gendev,
911 &sdev_attr_queue_ramp_up_period);
912 }
bfd12944
KS
913 else
914 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
860dc736 915 if (error)
ee37e09d 916 return error;
860dc736 917
bfd12944
KS
918 if (sdev->host->hostt->change_queue_type)
919 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
920 else
921 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
860dc736 922 if (error)
ee37e09d 923 return error;
bfd12944 924
97f46ae4 925 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
80ed71ce
JB
926
927 if (error)
860dc736
JB
928 /* we're treating error on bsg register as non-fatal,
929 * so pretend nothing went wrong */
80ed71ce
JB
930 sdev_printk(KERN_INFO, sdev,
931 "Failed to register bsg queue, errno=%d\n", error);
932
bfd12944 933 /* add additional host specific attributes */
1da177e4
LT
934 if (sdev->host->hostt->sdev_attrs) {
935 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
bfd12944 936 error = device_create_file(&sdev->sdev_gendev,
1da177e4 937 sdev->host->hostt->sdev_attrs[i]);
860dc736 938 if (error)
ee37e09d 939 return error;
1da177e4
LT
940 }
941 }
1da177e4 942
1da177e4
LT
943 return error;
944}
945
903f4fed 946void __scsi_remove_device(struct scsi_device *sdev)
1da177e4 947{
df133c21
JB
948 struct device *dev = &sdev->sdev_gendev;
949
860dc736
JB
950 if (sdev->is_visible) {
951 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
952 return;
1da177e4 953
860dc736
JB
954 bsg_unregister_queue(sdev->request_queue);
955 device_unregister(&sdev->sdev_dev);
956 transport_remove_device(dev);
957 device_del(dev);
958 } else
959 put_device(&sdev->sdev_dev);
1da177e4
LT
960 scsi_device_set_state(sdev, SDEV_DEL);
961 if (sdev->host->hostt->slave_destroy)
962 sdev->host->hostt->slave_destroy(sdev);
df133c21
JB
963 transport_destroy_device(dev);
964 put_device(dev);
903f4fed
AS
965}
966
967/**
968 * scsi_remove_device - unregister a device from the scsi bus
969 * @sdev: scsi_device to unregister
970 **/
971void scsi_remove_device(struct scsi_device *sdev)
972{
54195002
AS
973 struct Scsi_Host *shost = sdev->host;
974
0b950672 975 mutex_lock(&shost->scan_mutex);
903f4fed 976 __scsi_remove_device(sdev);
0b950672 977 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
978}
979EXPORT_SYMBOL(scsi_remove_device);
980
44818efb 981static void __scsi_remove_target(struct scsi_target *starget)
1da177e4
LT
982{
983 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
984 unsigned long flags;
a64358db 985 struct scsi_device *sdev;
1da177e4
LT
986
987 spin_lock_irqsave(shost->host_lock, flags);
988 starget->reap_ref++;
a64358db
AS
989 restart:
990 list_for_each_entry(sdev, &shost->__devices, siblings) {
1da177e4 991 if (sdev->channel != starget->channel ||
a64358db
AS
992 sdev->id != starget->id ||
993 sdev->sdev_state == SDEV_DEL)
1da177e4
LT
994 continue;
995 spin_unlock_irqrestore(shost->host_lock, flags);
996 scsi_remove_device(sdev);
997 spin_lock_irqsave(shost->host_lock, flags);
a64358db 998 goto restart;
1da177e4
LT
999 }
1000 spin_unlock_irqrestore(shost->host_lock, flags);
1001 scsi_target_reap(starget);
1002}
1003
20b1e674
PM
1004static int __remove_child (struct device * dev, void * data)
1005{
1006 if (scsi_is_target_device(dev))
1007 __scsi_remove_target(to_scsi_target(dev));
1008 return 0;
1009}
1010
1da177e4
LT
1011/**
1012 * scsi_remove_target - try to remove a target and all its devices
1013 * @dev: generic starget or parent of generic stargets to be removed
1014 *
1015 * Note: This is slightly racy. It is possible that if the user
1016 * requests the addition of another device then the target won't be
1017 * removed.
1018 */
1019void scsi_remove_target(struct device *dev)
1020{
20b1e674 1021 struct device *rdev;
1da177e4
LT
1022
1023 if (scsi_is_target_device(dev)) {
1024 __scsi_remove_target(to_scsi_target(dev));
1025 return;
1026 }
1027
1028 rdev = get_device(dev);
20b1e674 1029 device_for_each_child(dev, NULL, __remove_child);
1da177e4
LT
1030 put_device(rdev);
1031}
1032EXPORT_SYMBOL(scsi_remove_target);
1033
1034int scsi_register_driver(struct device_driver *drv)
1035{
1036 drv->bus = &scsi_bus_type;
1037
1038 return driver_register(drv);
1039}
1040EXPORT_SYMBOL(scsi_register_driver);
1041
1042int scsi_register_interface(struct class_interface *intf)
1043{
1044 intf->class = &sdev_class;
1045
1046 return class_interface_register(intf);
1047}
1048EXPORT_SYMBOL(scsi_register_interface);
1049
1da177e4
LT
1050/**
1051 * scsi_sysfs_add_host - add scsi host to subsystem
1052 * @shost: scsi host struct to add to subsystem
1da177e4
LT
1053 **/
1054int scsi_sysfs_add_host(struct Scsi_Host *shost)
1055{
1056 int error, i;
1057
f7120a4f 1058 /* add host specific attributes */
1da177e4
LT
1059 if (shost->hostt->shost_attrs) {
1060 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
ee959b00 1061 error = device_create_file(&shost->shost_dev,
f7120a4f 1062 shost->hostt->shost_attrs[i]);
1da177e4
LT
1063 if (error)
1064 return error;
1065 }
1066 }
1067
1068 transport_register_device(&shost->shost_gendev);
d52b3815 1069 transport_configure_device(&shost->shost_gendev);
1da177e4
LT
1070 return 0;
1071}
1072
bfd12944
KS
1073static struct device_type scsi_dev_type = {
1074 .name = "scsi_device",
1075 .release = scsi_device_dev_release,
1076 .groups = scsi_sdev_attr_groups,
1077};
1078
1da177e4
LT
1079void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1080{
1081 unsigned long flags;
1082 struct Scsi_Host *shost = sdev->host;
1083 struct scsi_target *starget = sdev->sdev_target;
1084
1085 device_initialize(&sdev->sdev_gendev);
1086 sdev->sdev_gendev.bus = &scsi_bus_type;
bfd12944 1087 sdev->sdev_gendev.type = &scsi_dev_type;
71610f55
KS
1088 dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
1089 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1090
ee959b00 1091 device_initialize(&sdev->sdev_dev);
37e6ba00 1092 sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
ee959b00 1093 sdev->sdev_dev.class = &sdev_class;
71610f55
KS
1094 dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
1095 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
7c9d6f16 1096 sdev->scsi_level = starget->scsi_level;
1da177e4
LT
1097 transport_setup_device(&sdev->sdev_gendev);
1098 spin_lock_irqsave(shost->host_lock, flags);
1099 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1100 list_add_tail(&sdev->siblings, &shost->__devices);
1101 spin_unlock_irqrestore(shost->host_lock, flags);
1102}
1103
1104int scsi_is_sdev_device(const struct device *dev)
1105{
13ba9bcb 1106 return dev->type == &scsi_dev_type;
1da177e4
LT
1107}
1108EXPORT_SYMBOL(scsi_is_sdev_device);
1109
1110/* A blank transport template that is used in drivers that don't
1111 * yet implement Transport Attributes */
1112struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };