]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/scsi_sysfs.c
[SCSI] include linux/scatterlist.h in scsi_eh.h
[net-next-2.6.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);
212
213 if (shost->hostt->supported_mode == MODE_UNKNOWN)
214 return snprintf(buf, 20, "unknown\n");
215 else
216 return show_shost_mode(shost->hostt->supported_mode, buf);
217}
218
219static CLASS_DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
220
221static ssize_t show_shost_active_mode(struct class_device *class_dev, char *buf)
222{
223 struct Scsi_Host *shost = class_to_shost(class_dev);
224
225 if (shost->active_mode == MODE_UNKNOWN)
226 return snprintf(buf, 20, "unknown\n");
227 else
228 return show_shost_mode(shost->active_mode, buf);
229}
230
231static CLASS_DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
232
1da177e4
LT
233shost_rd_attr(unique_id, "%u\n");
234shost_rd_attr(host_busy, "%hu\n");
235shost_rd_attr(cmd_per_lun, "%hd\n");
ed632da8 236shost_rd_attr(can_queue, "%hd\n");
1da177e4
LT
237shost_rd_attr(sg_tablesize, "%hu\n");
238shost_rd_attr(unchecked_isa_dma, "%d\n");
239shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
240
241static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
242 &class_device_attr_unique_id,
243 &class_device_attr_host_busy,
244 &class_device_attr_cmd_per_lun,
ed632da8 245 &class_device_attr_can_queue,
1da177e4
LT
246 &class_device_attr_sg_tablesize,
247 &class_device_attr_unchecked_isa_dma,
248 &class_device_attr_proc_name,
249 &class_device_attr_scan,
d3301874 250 &class_device_attr_state,
5dc2b89e
FT
251 &class_device_attr_supported_mode,
252 &class_device_attr_active_mode,
1da177e4
LT
253 NULL
254};
255
256static void scsi_device_cls_release(struct class_device *class_dev)
257{
258 struct scsi_device *sdev;
259
260 sdev = class_to_sdev(class_dev);
261 put_device(&sdev->sdev_gendev);
262}
263
65f27f38 264static void scsi_device_dev_release_usercontext(struct work_struct *work)
1da177e4
LT
265{
266 struct scsi_device *sdev;
267 struct device *parent;
268 struct scsi_target *starget;
269 unsigned long flags;
270
65f27f38
DH
271 sdev = container_of(work, struct scsi_device, ew.work);
272
273 parent = sdev->sdev_gendev.parent;
1da177e4
LT
274 starget = to_scsi_target(parent);
275
276 spin_lock_irqsave(sdev->host->host_lock, flags);
277 starget->reap_ref++;
278 list_del(&sdev->siblings);
279 list_del(&sdev->same_target_siblings);
280 list_del(&sdev->starved_entry);
281 spin_unlock_irqrestore(sdev->host->host_lock, flags);
282
283 if (sdev->request_queue) {
284 sdev->request_queue->queuedata = NULL;
65110b21 285 /* user context needed to free queue */
1da177e4 286 scsi_free_queue(sdev->request_queue);
c2a9331c
JB
287 /* temporary expedient, try to catch use of queue lock
288 * after free of sdev */
289 sdev->request_queue = NULL;
1da177e4
LT
290 }
291
292 scsi_target_reap(scsi_target(sdev));
293
294 kfree(sdev->inquiry);
295 kfree(sdev);
296
297 if (parent)
298 put_device(parent);
299}
300
65110b21
JB
301static void scsi_device_dev_release(struct device *dev)
302{
ffedb452 303 struct scsi_device *sdp = to_scsi_device(dev);
65f27f38 304 execute_in_process_context(scsi_device_dev_release_usercontext,
ffedb452 305 &sdp->ew);
65110b21
JB
306}
307
52c1da39 308static struct class sdev_class = {
1da177e4
LT
309 .name = "scsi_device",
310 .release = scsi_device_cls_release,
311};
312
313/* all probing is done in the individual ->probe routines */
314static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
315{
316 struct scsi_device *sdp = to_scsi_device(dev);
317 if (sdp->no_uld_attach)
318 return 0;
319 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
320}
321
7eff2e7a 322static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
d7b8bcb0
MT
323{
324 struct scsi_device *sdev = to_scsi_device(dev);
d7b8bcb0 325
7eff2e7a 326 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
d7b8bcb0
MT
327 return 0;
328}
329
9b847548
JA
330static int scsi_bus_suspend(struct device * dev, pm_message_t state)
331{
c3c94c5a 332 struct device_driver *drv = dev->driver;
9b847548 333 struct scsi_device *sdev = to_scsi_device(dev);
9b847548
JA
334 int err;
335
336 err = scsi_device_quiesce(sdev);
337 if (err)
338 return err;
339
c3c94c5a
TH
340 if (drv && drv->suspend) {
341 err = drv->suspend(dev, state);
342 if (err)
343 return err;
344 }
345
c3c94c5a 346 return 0;
9b847548
JA
347}
348
349static int scsi_bus_resume(struct device * dev)
350{
c3c94c5a 351 struct device_driver *drv = dev->driver;
9b847548 352 struct scsi_device *sdev = to_scsi_device(dev);
1dfcda06 353 int err = 0;
9b847548 354
c3c94c5a 355 if (drv && drv->resume)
1dfcda06 356 err = drv->resume(dev);
c3c94c5a 357
9b847548 358 scsi_device_resume(sdev);
c3c94c5a 359
1dfcda06 360 return err;
9b847548
JA
361}
362
1da177e4
LT
363struct bus_type scsi_bus_type = {
364 .name = "scsi",
365 .match = scsi_bus_match,
d7b8bcb0 366 .uevent = scsi_bus_uevent,
9b847548
JA
367 .suspend = scsi_bus_suspend,
368 .resume = scsi_bus_resume,
1da177e4
LT
369};
370
371int scsi_sysfs_register(void)
372{
373 int error;
374
375 error = bus_register(&scsi_bus_type);
376 if (!error) {
377 error = class_register(&sdev_class);
378 if (error)
379 bus_unregister(&scsi_bus_type);
380 }
381
382 return error;
383}
384
385void scsi_sysfs_unregister(void)
386{
387 class_unregister(&sdev_class);
388 bus_unregister(&scsi_bus_type);
389}
390
391/*
392 * sdev_show_function: macro to create an attr function that can be used to
393 * show a non-bit field.
394 */
395#define sdev_show_function(field, format_string) \
396static ssize_t \
10523b3b 397sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
398{ \
399 struct scsi_device *sdev; \
400 sdev = to_scsi_device(dev); \
401 return snprintf (buf, 20, format_string, sdev->field); \
402} \
403
404/*
405 * sdev_rd_attr: macro to create a function and attribute variable for a
406 * read only field.
407 */
408#define sdev_rd_attr(field, format_string) \
409 sdev_show_function(field, format_string) \
410static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
411
412
413/*
414 * sdev_rd_attr: create a function and attribute variable for a
415 * read/write field.
416 */
417#define sdev_rw_attr(field, format_string) \
418 sdev_show_function(field, format_string) \
419 \
420static ssize_t \
10523b3b 421sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
422{ \
423 struct scsi_device *sdev; \
424 sdev = to_scsi_device(dev); \
425 snscanf (buf, 20, format_string, &sdev->field); \
426 return count; \
427} \
428static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
429
430/* Currently we don't export bit fields, but we might in future,
431 * so leave this code in */
432#if 0
433/*
434 * sdev_rd_attr: create a function and attribute variable for a
435 * read/write bit field.
436 */
437#define sdev_rw_attr_bit(field) \
438 sdev_show_function(field, "%d\n") \
439 \
440static ssize_t \
10523b3b 441sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
442{ \
443 int ret; \
444 struct scsi_device *sdev; \
445 ret = scsi_sdev_check_buf_bit(buf); \
446 if (ret >= 0) { \
447 sdev = to_scsi_device(dev); \
448 sdev->field = ret; \
449 ret = count; \
450 } \
451 return ret; \
452} \
453static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
454
455/*
456 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
457 * else return -EINVAL.
458 */
459static int scsi_sdev_check_buf_bit(const char *buf)
460{
461 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
462 if (buf[0] == '1')
463 return 1;
464 else if (buf[0] == '0')
465 return 0;
466 else
467 return -EINVAL;
468 } else
469 return -EINVAL;
470}
471#endif
472/*
473 * Create the actual show/store functions and data structures.
474 */
475sdev_rd_attr (device_blocked, "%d\n");
476sdev_rd_attr (queue_depth, "%d\n");
477sdev_rd_attr (type, "%d\n");
478sdev_rd_attr (scsi_level, "%d\n");
479sdev_rd_attr (vendor, "%.8s\n");
480sdev_rd_attr (model, "%.16s\n");
481sdev_rd_attr (rev, "%.4s\n");
482
483static ssize_t
10523b3b 484sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
485{
486 struct scsi_device *sdev;
487 sdev = to_scsi_device(dev);
488 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
489}
490
491static ssize_t
10523b3b 492sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
493{
494 struct scsi_device *sdev;
495 int timeout;
496 sdev = to_scsi_device(dev);
497 sscanf (buf, "%d\n", &timeout);
498 sdev->timeout = timeout * HZ;
499 return count;
500}
501static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
502
503static ssize_t
10523b3b 504store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
505{
506 scsi_rescan_device(dev);
507 return count;
508}
509static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
510
d9a9cdfb
AS
511static void sdev_store_delete_callback(struct device *dev)
512{
513 scsi_remove_device(to_scsi_device(dev));
514}
515
10523b3b 516static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
517 size_t count)
518{
d9a9cdfb
AS
519 int rc;
520
521 /* An attribute cannot be unregistered by one of its own methods,
522 * so we have to use this roundabout approach.
523 */
524 rc = device_schedule_callback(dev, sdev_store_delete_callback);
525 if (rc)
526 count = rc;
1da177e4
LT
527 return count;
528};
529static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
530
531static ssize_t
10523b3b 532store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
533{
534 int i;
535 struct scsi_device *sdev = to_scsi_device(dev);
536 enum scsi_device_state state = 0;
537
6391a113 538 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
1da177e4
LT
539 const int len = strlen(sdev_states[i].name);
540 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
541 buf[len] == '\n') {
542 state = sdev_states[i].value;
543 break;
544 }
545 }
546 if (!state)
547 return -EINVAL;
548
549 if (scsi_device_set_state(sdev, state))
550 return -EINVAL;
551 return count;
552}
553
554static ssize_t
10523b3b 555show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
556{
557 struct scsi_device *sdev = to_scsi_device(dev);
558 const char *name = scsi_device_state_name(sdev->sdev_state);
559
560 if (!name)
561 return -EINVAL;
562
563 return snprintf(buf, 20, "%s\n", name);
564}
565
566static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
567
568static ssize_t
10523b3b 569show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
570{
571 struct scsi_device *sdev = to_scsi_device(dev);
572 const char *name = "none";
573
574 if (sdev->ordered_tags)
575 name = "ordered";
576 else if (sdev->simple_tags)
577 name = "simple";
578
579 return snprintf(buf, 20, "%s\n", name);
580}
581
582static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
583
584static ssize_t
10523b3b 585show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
586{
587 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
588}
589
590static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
591
592#define show_sdev_iostat(field) \
593static ssize_t \
10523b3b 594show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
595{ \
596 struct scsi_device *sdev = to_scsi_device(dev); \
597 unsigned long long count = atomic_read(&sdev->field); \
598 return snprintf(buf, 20, "0x%llx\n", count); \
599} \
600static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
601
602show_sdev_iostat(iorequest_cnt);
603show_sdev_iostat(iodone_cnt);
604show_sdev_iostat(ioerr_cnt);
605
d7b8bcb0
MT
606static ssize_t
607sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
608{
609 struct scsi_device *sdev;
610 sdev = to_scsi_device(dev);
611 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
612}
613static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
1da177e4
LT
614
615/* Default template for device attributes. May NOT be modified */
bfd12944
KS
616static struct attribute *scsi_sdev_attrs[] = {
617 &dev_attr_device_blocked.attr,
618 &dev_attr_type.attr,
619 &dev_attr_scsi_level.attr,
620 &dev_attr_vendor.attr,
621 &dev_attr_model.attr,
622 &dev_attr_rev.attr,
623 &dev_attr_rescan.attr,
624 &dev_attr_delete.attr,
625 &dev_attr_state.attr,
626 &dev_attr_timeout.attr,
627 &dev_attr_iocounterbits.attr,
628 &dev_attr_iorequest_cnt.attr,
629 &dev_attr_iodone_cnt.attr,
630 &dev_attr_ioerr_cnt.attr,
631 &dev_attr_modalias.attr,
632 NULL
633};
634
635static struct attribute_group scsi_sdev_attr_group = {
636 .attrs = scsi_sdev_attrs,
637};
638
639static struct attribute_group *scsi_sdev_attr_groups[] = {
640 &scsi_sdev_attr_group,
1da177e4
LT
641 NULL
642};
643
10523b3b 644static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
645 size_t count)
646{
647 int depth, retval;
648 struct scsi_device *sdev = to_scsi_device(dev);
649 struct scsi_host_template *sht = sdev->host->hostt;
650
651 if (!sht->change_queue_depth)
652 return -EINVAL;
653
654 depth = simple_strtoul(buf, NULL, 0);
655
656 if (depth < 1)
657 return -EINVAL;
658
659 retval = sht->change_queue_depth(sdev, depth);
660 if (retval < 0)
661 return retval;
662
663 return count;
664}
665
666static struct device_attribute sdev_attr_queue_depth_rw =
667 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
668 sdev_store_queue_depth_rw);
669
10523b3b 670static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
671 size_t count)
672{
673 struct scsi_device *sdev = to_scsi_device(dev);
674 struct scsi_host_template *sht = sdev->host->hostt;
675 int tag_type = 0, retval;
676 int prev_tag_type = scsi_get_tag_type(sdev);
677
678 if (!sdev->tagged_supported || !sht->change_queue_type)
679 return -EINVAL;
680
681 if (strncmp(buf, "ordered", 7) == 0)
682 tag_type = MSG_ORDERED_TAG;
683 else if (strncmp(buf, "simple", 6) == 0)
684 tag_type = MSG_SIMPLE_TAG;
685 else if (strncmp(buf, "none", 4) != 0)
686 return -EINVAL;
687
688 if (tag_type == prev_tag_type)
689 return count;
690
691 retval = sht->change_queue_type(sdev, tag_type);
692 if (retval < 0)
693 return retval;
694
695 return count;
696}
697
698static struct device_attribute sdev_attr_queue_type_rw =
699 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
700 sdev_store_queue_type_rw);
701
1da177e4
LT
702/**
703 * scsi_sysfs_add_sdev - add scsi device to sysfs
704 * @sdev: scsi_device to add
705 *
706 * Return value:
707 * 0 on Success / non-zero on Failure
708 **/
709int scsi_sysfs_add_sdev(struct scsi_device *sdev)
710{
711 int error, i;
80ed71ce 712 struct request_queue *rq = sdev->request_queue;
1da177e4
LT
713
714 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
715 return error;
716
717 error = device_add(&sdev->sdev_gendev);
718 if (error) {
719 put_device(sdev->sdev_gendev.parent);
720 printk(KERN_INFO "error 1\n");
721 return error;
722 }
723 error = class_device_add(&sdev->sdev_classdev);
724 if (error) {
725 printk(KERN_INFO "error 2\n");
726 goto clean_device;
727 }
728
729 /* take a reference for the sdev_classdev; this is
730 * released by the sdev_class .release */
731 get_device(&sdev->sdev_gendev);
80ed71ce 732
bfd12944
KS
733 /* create queue files, which may be writable, depending on the host */
734 if (sdev->host->hostt->change_queue_depth)
735 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
736 else
737 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
738 if (error) {
739 __scsi_remove_device(sdev);
740 goto out;
741 }
742 if (sdev->host->hostt->change_queue_type)
743 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
744 else
745 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
746 if (error) {
747 __scsi_remove_device(sdev);
748 goto out;
749 }
750
39dca558 751 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
80ed71ce
JB
752
753 if (error)
754 sdev_printk(KERN_INFO, sdev,
755 "Failed to register bsg queue, errno=%d\n", error);
756
757 /* we're treating error on bsg register as non-fatal, so pretend
758 * nothing went wrong */
759 error = 0;
760
bfd12944 761 /* add additional host specific attributes */
1da177e4
LT
762 if (sdev->host->hostt->sdev_attrs) {
763 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
bfd12944 764 error = device_create_file(&sdev->sdev_gendev,
1da177e4
LT
765 sdev->host->hostt->sdev_attrs[i]);
766 if (error) {
903f4fed 767 __scsi_remove_device(sdev);
1da177e4
LT
768 goto out;
769 }
770 }
771 }
1da177e4
LT
772
773 transport_add_device(&sdev->sdev_gendev);
774 out:
775 return error;
776
777 clean_device:
778 scsi_device_set_state(sdev, SDEV_CANCEL);
779
780 device_del(&sdev->sdev_gendev);
781 transport_destroy_device(&sdev->sdev_gendev);
782 put_device(&sdev->sdev_gendev);
783
784 return error;
785}
786
903f4fed 787void __scsi_remove_device(struct scsi_device *sdev)
1da177e4 788{
df133c21
JB
789 struct device *dev = &sdev->sdev_gendev;
790
1da177e4 791 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
903f4fed 792 return;
1da177e4 793
80ed71ce 794 bsg_unregister_queue(sdev->request_queue);
1da177e4 795 class_device_unregister(&sdev->sdev_classdev);
df133c21
JB
796 transport_remove_device(dev);
797 device_del(dev);
1da177e4
LT
798 scsi_device_set_state(sdev, SDEV_DEL);
799 if (sdev->host->hostt->slave_destroy)
800 sdev->host->hostt->slave_destroy(sdev);
df133c21
JB
801 transport_destroy_device(dev);
802 put_device(dev);
903f4fed
AS
803}
804
805/**
806 * scsi_remove_device - unregister a device from the scsi bus
807 * @sdev: scsi_device to unregister
808 **/
809void scsi_remove_device(struct scsi_device *sdev)
810{
54195002
AS
811 struct Scsi_Host *shost = sdev->host;
812
0b950672 813 mutex_lock(&shost->scan_mutex);
903f4fed 814 __scsi_remove_device(sdev);
0b950672 815 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
816}
817EXPORT_SYMBOL(scsi_remove_device);
818
44818efb 819static void __scsi_remove_target(struct scsi_target *starget)
1da177e4
LT
820{
821 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
822 unsigned long flags;
a64358db 823 struct scsi_device *sdev;
1da177e4
LT
824
825 spin_lock_irqsave(shost->host_lock, flags);
826 starget->reap_ref++;
a64358db
AS
827 restart:
828 list_for_each_entry(sdev, &shost->__devices, siblings) {
1da177e4 829 if (sdev->channel != starget->channel ||
a64358db
AS
830 sdev->id != starget->id ||
831 sdev->sdev_state == SDEV_DEL)
1da177e4
LT
832 continue;
833 spin_unlock_irqrestore(shost->host_lock, flags);
834 scsi_remove_device(sdev);
835 spin_lock_irqsave(shost->host_lock, flags);
a64358db 836 goto restart;
1da177e4
LT
837 }
838 spin_unlock_irqrestore(shost->host_lock, flags);
839 scsi_target_reap(starget);
840}
841
20b1e674
PM
842static int __remove_child (struct device * dev, void * data)
843{
844 if (scsi_is_target_device(dev))
845 __scsi_remove_target(to_scsi_target(dev));
846 return 0;
847}
848
1da177e4
LT
849/**
850 * scsi_remove_target - try to remove a target and all its devices
851 * @dev: generic starget or parent of generic stargets to be removed
852 *
853 * Note: This is slightly racy. It is possible that if the user
854 * requests the addition of another device then the target won't be
855 * removed.
856 */
857void scsi_remove_target(struct device *dev)
858{
20b1e674 859 struct device *rdev;
1da177e4
LT
860
861 if (scsi_is_target_device(dev)) {
862 __scsi_remove_target(to_scsi_target(dev));
863 return;
864 }
865
866 rdev = get_device(dev);
20b1e674 867 device_for_each_child(dev, NULL, __remove_child);
1da177e4
LT
868 put_device(rdev);
869}
870EXPORT_SYMBOL(scsi_remove_target);
871
872int scsi_register_driver(struct device_driver *drv)
873{
874 drv->bus = &scsi_bus_type;
875
876 return driver_register(drv);
877}
878EXPORT_SYMBOL(scsi_register_driver);
879
880int scsi_register_interface(struct class_interface *intf)
881{
882 intf->class = &sdev_class;
883
884 return class_interface_register(intf);
885}
886EXPORT_SYMBOL(scsi_register_interface);
887
888
889static struct class_device_attribute *class_attr_overridden(
890 struct class_device_attribute **attrs,
891 struct class_device_attribute *attr)
892{
893 int i;
894
895 if (!attrs)
896 return NULL;
897 for (i = 0; attrs[i]; i++)
898 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
899 return attrs[i];
900 return NULL;
901}
902
903static int class_attr_add(struct class_device *classdev,
904 struct class_device_attribute *attr)
905{
906 struct class_device_attribute *base_attr;
907
908 /*
909 * Spare the caller from having to copy things it's not interested in.
910 */
911 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
912 if (base_attr) {
913 /* extend permissions */
914 attr->attr.mode |= base_attr->attr.mode;
915
916 /* override null show/store with default */
917 if (!attr->show)
918 attr->show = base_attr->show;
919 if (!attr->store)
920 attr->store = base_attr->store;
921 }
922
923 return class_device_create_file(classdev, attr);
924}
925
926/**
927 * scsi_sysfs_add_host - add scsi host to subsystem
928 * @shost: scsi host struct to add to subsystem
929 * @dev: parent struct device pointer
930 **/
931int scsi_sysfs_add_host(struct Scsi_Host *shost)
932{
933 int error, i;
934
935 if (shost->hostt->shost_attrs) {
936 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
937 error = class_attr_add(&shost->shost_classdev,
938 shost->hostt->shost_attrs[i]);
939 if (error)
940 return error;
941 }
942 }
943
944 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
945 if (!class_attr_overridden(shost->hostt->shost_attrs,
946 scsi_sysfs_shost_attrs[i])) {
947 error = class_device_create_file(&shost->shost_classdev,
948 scsi_sysfs_shost_attrs[i]);
949 if (error)
950 return error;
951 }
952 }
953
954 transport_register_device(&shost->shost_gendev);
955 return 0;
956}
957
bfd12944
KS
958static struct device_type scsi_dev_type = {
959 .name = "scsi_device",
960 .release = scsi_device_dev_release,
961 .groups = scsi_sdev_attr_groups,
962};
963
1da177e4
LT
964void scsi_sysfs_device_initialize(struct scsi_device *sdev)
965{
966 unsigned long flags;
967 struct Scsi_Host *shost = sdev->host;
968 struct scsi_target *starget = sdev->sdev_target;
969
970 device_initialize(&sdev->sdev_gendev);
971 sdev->sdev_gendev.bus = &scsi_bus_type;
bfd12944 972 sdev->sdev_gendev.type = &scsi_dev_type;
1da177e4
LT
973 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
974 sdev->host->host_no, sdev->channel, sdev->id,
975 sdev->lun);
976
977 class_device_initialize(&sdev->sdev_classdev);
978 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
979 sdev->sdev_classdev.class = &sdev_class;
980 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
981 "%d:%d:%d:%d", sdev->host->host_no,
982 sdev->channel, sdev->id, sdev->lun);
7c9d6f16 983 sdev->scsi_level = starget->scsi_level;
1da177e4
LT
984 transport_setup_device(&sdev->sdev_gendev);
985 spin_lock_irqsave(shost->host_lock, flags);
986 list_add_tail(&sdev->same_target_siblings, &starget->devices);
987 list_add_tail(&sdev->siblings, &shost->__devices);
988 spin_unlock_irqrestore(shost->host_lock, flags);
989}
990
991int scsi_is_sdev_device(const struct device *dev)
992{
13ba9bcb 993 return dev->type == &scsi_dev_type;
1da177e4
LT
994}
995EXPORT_SYMBOL(scsi_is_sdev_device);
996
997/* A blank transport template that is used in drivers that don't
998 * yet implement Transport Attributes */
999struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };