blob: 5ec7a4fb0145bfc83b488cdae27a1e2a16e9a81a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_sysfs.c
3 *
4 * SCSI sysfs interface routines.
5 *
6 * Created to pull SCSI mid layer sysfs routines into one file.
7 */
8
9#include <linux/config.h>
10#include <linux/module.h>
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>
20
21#include "scsi_priv.h"
22#include "scsi_logging.h"
23
Arjan van de Ven0ad78202005-11-28 16:22:25 +010024static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 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
Tobias Klauser6391a112006-06-08 22:23:48 -070042 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (sdev_states[i].value == state) {
44 name = sdev_states[i].name;
45 break;
46 }
47 }
48 return name;
49}
50
Arjan van de Ven0ad78202005-11-28 16:22:25 +010051static const struct {
Mike Andersond3301872005-06-16 11:12:38 -070052 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" },
James Bottomley939647e2005-09-18 15:05:20 -050060 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
61 { SHOST_DEL_RECOVERY, "deleted/recovery", },
Mike Andersond3301872005-06-16 11:12:38 -070062};
63const char *scsi_host_state_name(enum scsi_host_state state)
64{
65 int i;
66 char *name = NULL;
67
Tobias Klauser6391a112006-06-08 22:23:48 -070068 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -070069 if (shost_states[i].value == state) {
70 name = shost_states[i].name;
71 break;
72 }
73 }
74 return name;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static 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;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100109 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 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
Mike Andersond3301872005-06-16 11:12:38 -0700156static 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
Tobias Klauser6391a112006-06-08 22:23:48 -0700163 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -0700164 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193shost_rd_attr(unique_id, "%u\n");
194shost_rd_attr(host_busy, "%hu\n");
195shost_rd_attr(cmd_per_lun, "%hd\n");
196shost_rd_attr(sg_tablesize, "%hu\n");
197shost_rd_attr(unchecked_isa_dma, "%d\n");
198shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
199
200static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
201 &class_device_attr_unique_id,
202 &class_device_attr_host_busy,
203 &class_device_attr_cmd_per_lun,
204 &class_device_attr_sg_tablesize,
205 &class_device_attr_unchecked_isa_dma,
206 &class_device_attr_proc_name,
207 &class_device_attr_scan,
Mike Andersond3301872005-06-16 11:12:38 -0700208 &class_device_attr_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 NULL
210};
211
212static void scsi_device_cls_release(struct class_device *class_dev)
213{
214 struct scsi_device *sdev;
215
216 sdev = class_to_sdev(class_dev);
217 put_device(&sdev->sdev_gendev);
218}
219
James Bottomley65110b22006-02-14 10:48:46 -0600220static void scsi_device_dev_release_usercontext(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
James Bottomley65110b22006-02-14 10:48:46 -0600222 struct device *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct scsi_device *sdev;
224 struct device *parent;
225 struct scsi_target *starget;
226 unsigned long flags;
227
228 parent = dev->parent;
229 sdev = to_scsi_device(dev);
230 starget = to_scsi_target(parent);
231
232 spin_lock_irqsave(sdev->host->host_lock, flags);
233 starget->reap_ref++;
234 list_del(&sdev->siblings);
235 list_del(&sdev->same_target_siblings);
236 list_del(&sdev->starved_entry);
237 spin_unlock_irqrestore(sdev->host->host_lock, flags);
238
239 if (sdev->request_queue) {
240 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600241 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500243 /* temporary expedient, try to catch use of queue lock
244 * after free of sdev */
245 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
248 scsi_target_reap(scsi_target(sdev));
249
250 kfree(sdev->inquiry);
251 kfree(sdev);
252
253 if (parent)
254 put_device(parent);
255}
256
James Bottomley65110b22006-02-14 10:48:46 -0600257static void scsi_device_dev_release(struct device *dev)
258{
James Bottomleyffedb452006-02-23 14:27:18 -0600259 struct scsi_device *sdp = to_scsi_device(dev);
260 execute_in_process_context(scsi_device_dev_release_usercontext, dev,
261 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600262}
263
Adrian Bunk52c1da32005-06-23 22:05:33 -0700264static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .name = "scsi_device",
266 .release = scsi_device_cls_release,
267};
268
269/* all probing is done in the individual ->probe routines */
270static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
271{
272 struct scsi_device *sdp = to_scsi_device(dev);
273 if (sdp->no_uld_attach)
274 return 0;
275 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
276}
277
Jens Axboe9b847542006-01-06 09:28:07 +0100278static int scsi_bus_suspend(struct device * dev, pm_message_t state)
279{
280 struct scsi_device *sdev = to_scsi_device(dev);
281 struct scsi_host_template *sht = sdev->host->hostt;
282 int err;
283
284 err = scsi_device_quiesce(sdev);
285 if (err)
286 return err;
287
288 if (sht->suspend)
Nigel Cunningham082776e2006-03-23 23:22:16 +1000289 err = sht->suspend(sdev, state);
Jens Axboe9b847542006-01-06 09:28:07 +0100290
291 return err;
292}
293
294static int scsi_bus_resume(struct device * dev)
295{
296 struct scsi_device *sdev = to_scsi_device(dev);
297 struct scsi_host_template *sht = sdev->host->hostt;
298 int err = 0;
299
300 if (sht->resume)
301 err = sht->resume(sdev);
302
303 scsi_device_resume(sdev);
304 return err;
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307struct bus_type scsi_bus_type = {
308 .name = "scsi",
309 .match = scsi_bus_match,
Jens Axboe9b847542006-01-06 09:28:07 +0100310 .suspend = scsi_bus_suspend,
311 .resume = scsi_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
314int scsi_sysfs_register(void)
315{
316 int error;
317
318 error = bus_register(&scsi_bus_type);
319 if (!error) {
320 error = class_register(&sdev_class);
321 if (error)
322 bus_unregister(&scsi_bus_type);
323 }
324
325 return error;
326}
327
328void scsi_sysfs_unregister(void)
329{
330 class_unregister(&sdev_class);
331 bus_unregister(&scsi_bus_type);
332}
333
334/*
335 * sdev_show_function: macro to create an attr function that can be used to
336 * show a non-bit field.
337 */
338#define sdev_show_function(field, format_string) \
339static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400340sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{ \
342 struct scsi_device *sdev; \
343 sdev = to_scsi_device(dev); \
344 return snprintf (buf, 20, format_string, sdev->field); \
345} \
346
347/*
348 * sdev_rd_attr: macro to create a function and attribute variable for a
349 * read only field.
350 */
351#define sdev_rd_attr(field, format_string) \
352 sdev_show_function(field, format_string) \
353static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
354
355
356/*
357 * sdev_rd_attr: create a function and attribute variable for a
358 * read/write field.
359 */
360#define sdev_rw_attr(field, format_string) \
361 sdev_show_function(field, format_string) \
362 \
363static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400364sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{ \
366 struct scsi_device *sdev; \
367 sdev = to_scsi_device(dev); \
368 snscanf (buf, 20, format_string, &sdev->field); \
369 return count; \
370} \
371static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
372
373/* Currently we don't export bit fields, but we might in future,
374 * so leave this code in */
375#if 0
376/*
377 * sdev_rd_attr: create a function and attribute variable for a
378 * read/write bit field.
379 */
380#define sdev_rw_attr_bit(field) \
381 sdev_show_function(field, "%d\n") \
382 \
383static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400384sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{ \
386 int ret; \
387 struct scsi_device *sdev; \
388 ret = scsi_sdev_check_buf_bit(buf); \
389 if (ret >= 0) { \
390 sdev = to_scsi_device(dev); \
391 sdev->field = ret; \
392 ret = count; \
393 } \
394 return ret; \
395} \
396static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
397
398/*
399 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
400 * else return -EINVAL.
401 */
402static int scsi_sdev_check_buf_bit(const char *buf)
403{
404 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
405 if (buf[0] == '1')
406 return 1;
407 else if (buf[0] == '0')
408 return 0;
409 else
410 return -EINVAL;
411 } else
412 return -EINVAL;
413}
414#endif
415/*
416 * Create the actual show/store functions and data structures.
417 */
418sdev_rd_attr (device_blocked, "%d\n");
419sdev_rd_attr (queue_depth, "%d\n");
420sdev_rd_attr (type, "%d\n");
421sdev_rd_attr (scsi_level, "%d\n");
422sdev_rd_attr (vendor, "%.8s\n");
423sdev_rd_attr (model, "%.16s\n");
424sdev_rd_attr (rev, "%.4s\n");
425
426static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400427sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct scsi_device *sdev;
430 sdev = to_scsi_device(dev);
431 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
432}
433
434static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400435sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct scsi_device *sdev;
438 int timeout;
439 sdev = to_scsi_device(dev);
440 sscanf (buf, "%d\n", &timeout);
441 sdev->timeout = timeout * HZ;
442 return count;
443}
444static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
445
446static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400447store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 scsi_rescan_device(dev);
450 return count;
451}
452static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
453
Yani Ioannou10523b32005-05-17 06:43:37 -0400454static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 size_t count)
456{
457 scsi_remove_device(to_scsi_device(dev));
458 return count;
459};
460static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
461
462static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400463store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int i;
466 struct scsi_device *sdev = to_scsi_device(dev);
467 enum scsi_device_state state = 0;
468
Tobias Klauser6391a112006-06-08 22:23:48 -0700469 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 const int len = strlen(sdev_states[i].name);
471 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
472 buf[len] == '\n') {
473 state = sdev_states[i].value;
474 break;
475 }
476 }
477 if (!state)
478 return -EINVAL;
479
480 if (scsi_device_set_state(sdev, state))
481 return -EINVAL;
482 return count;
483}
484
485static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400486show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct scsi_device *sdev = to_scsi_device(dev);
489 const char *name = scsi_device_state_name(sdev->sdev_state);
490
491 if (!name)
492 return -EINVAL;
493
494 return snprintf(buf, 20, "%s\n", name);
495}
496
497static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
498
499static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400500show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct scsi_device *sdev = to_scsi_device(dev);
503 const char *name = "none";
504
505 if (sdev->ordered_tags)
506 name = "ordered";
507 else if (sdev->simple_tags)
508 name = "simple";
509
510 return snprintf(buf, 20, "%s\n", name);
511}
512
513static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
514
515static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400516show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
519}
520
521static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
522
523#define show_sdev_iostat(field) \
524static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400525show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{ \
527 struct scsi_device *sdev = to_scsi_device(dev); \
528 unsigned long long count = atomic_read(&sdev->field); \
529 return snprintf(buf, 20, "0x%llx\n", count); \
530} \
531static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
532
533show_sdev_iostat(iorequest_cnt);
534show_sdev_iostat(iodone_cnt);
535show_sdev_iostat(ioerr_cnt);
536
537
538/* Default template for device attributes. May NOT be modified */
539static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
540 &dev_attr_device_blocked,
541 &dev_attr_queue_depth,
542 &dev_attr_queue_type,
543 &dev_attr_type,
544 &dev_attr_scsi_level,
545 &dev_attr_vendor,
546 &dev_attr_model,
547 &dev_attr_rev,
548 &dev_attr_rescan,
549 &dev_attr_delete,
550 &dev_attr_state,
551 &dev_attr_timeout,
552 &dev_attr_iocounterbits,
553 &dev_attr_iorequest_cnt,
554 &dev_attr_iodone_cnt,
555 &dev_attr_ioerr_cnt,
556 NULL
557};
558
Yani Ioannou10523b32005-05-17 06:43:37 -0400559static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 size_t count)
561{
562 int depth, retval;
563 struct scsi_device *sdev = to_scsi_device(dev);
564 struct scsi_host_template *sht = sdev->host->hostt;
565
566 if (!sht->change_queue_depth)
567 return -EINVAL;
568
569 depth = simple_strtoul(buf, NULL, 0);
570
571 if (depth < 1)
572 return -EINVAL;
573
574 retval = sht->change_queue_depth(sdev, depth);
575 if (retval < 0)
576 return retval;
577
578 return count;
579}
580
581static struct device_attribute sdev_attr_queue_depth_rw =
582 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
583 sdev_store_queue_depth_rw);
584
Yani Ioannou10523b32005-05-17 06:43:37 -0400585static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 size_t count)
587{
588 struct scsi_device *sdev = to_scsi_device(dev);
589 struct scsi_host_template *sht = sdev->host->hostt;
590 int tag_type = 0, retval;
591 int prev_tag_type = scsi_get_tag_type(sdev);
592
593 if (!sdev->tagged_supported || !sht->change_queue_type)
594 return -EINVAL;
595
596 if (strncmp(buf, "ordered", 7) == 0)
597 tag_type = MSG_ORDERED_TAG;
598 else if (strncmp(buf, "simple", 6) == 0)
599 tag_type = MSG_SIMPLE_TAG;
600 else if (strncmp(buf, "none", 4) != 0)
601 return -EINVAL;
602
603 if (tag_type == prev_tag_type)
604 return count;
605
606 retval = sht->change_queue_type(sdev, tag_type);
607 if (retval < 0)
608 return retval;
609
610 return count;
611}
612
613static struct device_attribute sdev_attr_queue_type_rw =
614 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
615 sdev_store_queue_type_rw);
616
617static struct device_attribute *attr_changed_internally(
618 struct Scsi_Host *shost,
619 struct device_attribute * attr)
620{
621 if (!strcmp("queue_depth", attr->attr.name)
622 && shost->hostt->change_queue_depth)
623 return &sdev_attr_queue_depth_rw;
624 else if (!strcmp("queue_type", attr->attr.name)
625 && shost->hostt->change_queue_type)
626 return &sdev_attr_queue_type_rw;
627 return attr;
628}
629
630
631static struct device_attribute *attr_overridden(
632 struct device_attribute **attrs,
633 struct device_attribute *attr)
634{
635 int i;
636
637 if (!attrs)
638 return NULL;
639 for (i = 0; attrs[i]; i++)
640 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
641 return attrs[i];
642 return NULL;
643}
644
645static int attr_add(struct device *dev, struct device_attribute *attr)
646{
647 struct device_attribute *base_attr;
648
649 /*
650 * Spare the caller from having to copy things it's not interested in.
651 */
652 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
653 if (base_attr) {
654 /* extend permissions */
655 attr->attr.mode |= base_attr->attr.mode;
656
657 /* override null show/store with default */
658 if (!attr->show)
659 attr->show = base_attr->show;
660 if (!attr->store)
661 attr->store = base_attr->store;
662 }
663
664 return device_create_file(dev, attr);
665}
666
667/**
668 * scsi_sysfs_add_sdev - add scsi device to sysfs
669 * @sdev: scsi_device to add
670 *
671 * Return value:
672 * 0 on Success / non-zero on Failure
673 **/
674int scsi_sysfs_add_sdev(struct scsi_device *sdev)
675{
676 int error, i;
677
678 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
679 return error;
680
681 error = device_add(&sdev->sdev_gendev);
682 if (error) {
683 put_device(sdev->sdev_gendev.parent);
684 printk(KERN_INFO "error 1\n");
685 return error;
686 }
687 error = class_device_add(&sdev->sdev_classdev);
688 if (error) {
689 printk(KERN_INFO "error 2\n");
690 goto clean_device;
691 }
692
693 /* take a reference for the sdev_classdev; this is
694 * released by the sdev_class .release */
695 get_device(&sdev->sdev_gendev);
696 if (sdev->host->hostt->sdev_attrs) {
697 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
698 error = attr_add(&sdev->sdev_gendev,
699 sdev->host->hostt->sdev_attrs[i]);
700 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400701 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto out;
703 }
704 }
705 }
706
707 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
708 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
709 scsi_sysfs_sdev_attrs[i])) {
710 struct device_attribute * attr =
711 attr_changed_internally(sdev->host,
712 scsi_sysfs_sdev_attrs[i]);
713 error = device_create_file(&sdev->sdev_gendev, attr);
714 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400715 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto out;
717 }
718 }
719 }
720
721 transport_add_device(&sdev->sdev_gendev);
722 out:
723 return error;
724
725 clean_device:
726 scsi_device_set_state(sdev, SDEV_CANCEL);
727
728 device_del(&sdev->sdev_gendev);
729 transport_destroy_device(&sdev->sdev_gendev);
730 put_device(&sdev->sdev_gendev);
731
732 return error;
733}
734
Alan Stern903f4fe2005-07-26 10:20:53 -0400735void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
James Bottomleydf133c22005-11-06 11:47:08 -0600737 struct device *dev = &sdev->sdev_gendev;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400740 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 class_device_unregister(&sdev->sdev_classdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600743 transport_remove_device(dev);
744 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 scsi_device_set_state(sdev, SDEV_DEL);
746 if (sdev->host->hostt->slave_destroy)
747 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600748 transport_destroy_device(dev);
749 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400750}
751
752/**
753 * scsi_remove_device - unregister a device from the scsi bus
754 * @sdev: scsi_device to unregister
755 **/
756void scsi_remove_device(struct scsi_device *sdev)
757{
Alan Stern54195002005-09-15 21:52:51 -0400758 struct Scsi_Host *shost = sdev->host;
759
Arjan van de Ven0b950672006-01-11 13:16:10 +0100760 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400761 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100762 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764EXPORT_SYMBOL(scsi_remove_device);
765
766void __scsi_remove_target(struct scsi_target *starget)
767{
768 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
769 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400770 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 spin_lock_irqsave(shost->host_lock, flags);
773 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400774 restart:
775 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400777 sdev->id != starget->id ||
778 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 continue;
780 spin_unlock_irqrestore(shost->host_lock, flags);
781 scsi_remove_device(sdev);
782 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400783 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 spin_unlock_irqrestore(shost->host_lock, flags);
786 scsi_target_reap(starget);
787}
788
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800789static int __remove_child (struct device * dev, void * data)
790{
791 if (scsi_is_target_device(dev))
792 __scsi_remove_target(to_scsi_target(dev));
793 return 0;
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/**
797 * scsi_remove_target - try to remove a target and all its devices
798 * @dev: generic starget or parent of generic stargets to be removed
799 *
800 * Note: This is slightly racy. It is possible that if the user
801 * requests the addition of another device then the target won't be
802 * removed.
803 */
804void scsi_remove_target(struct device *dev)
805{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800806 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (scsi_is_target_device(dev)) {
809 __scsi_remove_target(to_scsi_target(dev));
810 return;
811 }
812
813 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800814 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 put_device(rdev);
816}
817EXPORT_SYMBOL(scsi_remove_target);
818
819int scsi_register_driver(struct device_driver *drv)
820{
821 drv->bus = &scsi_bus_type;
822
823 return driver_register(drv);
824}
825EXPORT_SYMBOL(scsi_register_driver);
826
827int scsi_register_interface(struct class_interface *intf)
828{
829 intf->class = &sdev_class;
830
831 return class_interface_register(intf);
832}
833EXPORT_SYMBOL(scsi_register_interface);
834
835
836static struct class_device_attribute *class_attr_overridden(
837 struct class_device_attribute **attrs,
838 struct class_device_attribute *attr)
839{
840 int i;
841
842 if (!attrs)
843 return NULL;
844 for (i = 0; attrs[i]; i++)
845 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
846 return attrs[i];
847 return NULL;
848}
849
850static int class_attr_add(struct class_device *classdev,
851 struct class_device_attribute *attr)
852{
853 struct class_device_attribute *base_attr;
854
855 /*
856 * Spare the caller from having to copy things it's not interested in.
857 */
858 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
859 if (base_attr) {
860 /* extend permissions */
861 attr->attr.mode |= base_attr->attr.mode;
862
863 /* override null show/store with default */
864 if (!attr->show)
865 attr->show = base_attr->show;
866 if (!attr->store)
867 attr->store = base_attr->store;
868 }
869
870 return class_device_create_file(classdev, attr);
871}
872
873/**
874 * scsi_sysfs_add_host - add scsi host to subsystem
875 * @shost: scsi host struct to add to subsystem
876 * @dev: parent struct device pointer
877 **/
878int scsi_sysfs_add_host(struct Scsi_Host *shost)
879{
880 int error, i;
881
882 if (shost->hostt->shost_attrs) {
883 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
884 error = class_attr_add(&shost->shost_classdev,
885 shost->hostt->shost_attrs[i]);
886 if (error)
887 return error;
888 }
889 }
890
891 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
892 if (!class_attr_overridden(shost->hostt->shost_attrs,
893 scsi_sysfs_shost_attrs[i])) {
894 error = class_device_create_file(&shost->shost_classdev,
895 scsi_sysfs_shost_attrs[i]);
896 if (error)
897 return error;
898 }
899 }
900
901 transport_register_device(&shost->shost_gendev);
902 return 0;
903}
904
905void scsi_sysfs_device_initialize(struct scsi_device *sdev)
906{
907 unsigned long flags;
908 struct Scsi_Host *shost = sdev->host;
909 struct scsi_target *starget = sdev->sdev_target;
910
911 device_initialize(&sdev->sdev_gendev);
912 sdev->sdev_gendev.bus = &scsi_bus_type;
913 sdev->sdev_gendev.release = scsi_device_dev_release;
914 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
915 sdev->host->host_no, sdev->channel, sdev->id,
916 sdev->lun);
917
918 class_device_initialize(&sdev->sdev_classdev);
919 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
920 sdev->sdev_classdev.class = &sdev_class;
921 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
922 "%d:%d:%d:%d", sdev->host->host_no,
923 sdev->channel, sdev->id, sdev->lun);
924 sdev->scsi_level = SCSI_2;
925 transport_setup_device(&sdev->sdev_gendev);
926 spin_lock_irqsave(shost->host_lock, flags);
927 list_add_tail(&sdev->same_target_siblings, &starget->devices);
928 list_add_tail(&sdev->siblings, &shost->__devices);
929 spin_unlock_irqrestore(shost->host_lock, flags);
930}
931
932int scsi_is_sdev_device(const struct device *dev)
933{
934 return dev->release == scsi_device_dev_release;
935}
936EXPORT_SYMBOL(scsi_is_sdev_device);
937
938/* A blank transport template that is used in drivers that don't
939 * yet implement Transport Attributes */
940struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };