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