blob: c275dcac3f18b7b4abc1bd984cb1601de3f0a770 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
19
20#include "scsi_priv.h"
21#include "scsi_logging.h"
22
Arjan van de Ven0ad78202005-11-28 16:22:25 +010023static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 enum scsi_device_state value;
25 char *name;
26} sdev_states[] = {
27 { SDEV_CREATED, "created" },
28 { SDEV_RUNNING, "running" },
29 { SDEV_CANCEL, "cancel" },
30 { SDEV_DEL, "deleted" },
31 { SDEV_QUIESCE, "quiesce" },
32 { SDEV_OFFLINE, "offline" },
33 { SDEV_BLOCK, "blocked" },
34};
35
36const char *scsi_device_state_name(enum scsi_device_state state)
37{
38 int i;
39 char *name = NULL;
40
Tobias Klauser6391a112006-06-08 22:23:48 -070041 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (sdev_states[i].value == state) {
43 name = sdev_states[i].name;
44 break;
45 }
46 }
47 return name;
48}
49
Arjan van de Ven0ad78202005-11-28 16:22:25 +010050static const struct {
Mike Andersond3301872005-06-16 11:12:38 -070051 enum scsi_host_state value;
52 char *name;
53} shost_states[] = {
54 { SHOST_CREATED, "created" },
55 { SHOST_RUNNING, "running" },
56 { SHOST_CANCEL, "cancel" },
57 { SHOST_DEL, "deleted" },
58 { SHOST_RECOVERY, "recovery" },
James Bottomley939647e2005-09-18 15:05:20 -050059 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
60 { SHOST_DEL_RECOVERY, "deleted/recovery", },
Mike Andersond3301872005-06-16 11:12:38 -070061};
62const char *scsi_host_state_name(enum scsi_host_state state)
63{
64 int i;
65 char *name = NULL;
66
Tobias Klauser6391a112006-06-08 22:23:48 -070067 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -070068 if (shost_states[i].value == state) {
69 name = shost_states[i].name;
70 break;
71 }
72 }
73 return name;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int check_set(unsigned int *val, char *src)
77{
78 char *last;
79
80 if (strncmp(src, "-", 20) == 0) {
81 *val = SCAN_WILD_CARD;
82 } else {
83 /*
84 * Doesn't check for int overflow
85 */
86 *val = simple_strtoul(src, &last, 0);
87 if (*last != '\0')
88 return 1;
89 }
90 return 0;
91}
92
93static int scsi_scan(struct Scsi_Host *shost, const char *str)
94{
95 char s1[15], s2[15], s3[15], junk;
96 unsigned int channel, id, lun;
97 int res;
98
99 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
100 if (res != 3)
101 return -EINVAL;
102 if (check_set(&channel, s1))
103 return -EINVAL;
104 if (check_set(&id, s2))
105 return -EINVAL;
106 if (check_set(&lun, s3))
107 return -EINVAL;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100108 if (shost->transportt->user_scan)
109 res = shost->transportt->user_scan(shost, channel, id, lun);
110 else
111 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return res;
113}
114
115/*
116 * shost_show_function: macro to create an attr function that can be used to
117 * show a non-bit field.
118 */
119#define shost_show_function(name, field, format_string) \
120static ssize_t \
121show_##name (struct class_device *class_dev, char *buf) \
122{ \
123 struct Scsi_Host *shost = class_to_shost(class_dev); \
124 return snprintf (buf, 20, format_string, shost->field); \
125}
126
127/*
128 * shost_rd_attr: macro to create a function and attribute variable for a
129 * read only field.
130 */
131#define shost_rd_attr2(name, field, format_string) \
132 shost_show_function(name, field, format_string) \
133static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
134
135#define shost_rd_attr(field, format_string) \
136shost_rd_attr2(field, field, format_string)
137
138/*
139 * Create the actual show/store functions and data structures.
140 */
141
142static ssize_t store_scan(struct class_device *class_dev, const char *buf,
143 size_t count)
144{
145 struct Scsi_Host *shost = class_to_shost(class_dev);
146 int res;
147
148 res = scsi_scan(shost, buf);
149 if (res == 0)
150 res = count;
151 return res;
152};
153static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
154
Mike Andersond3301872005-06-16 11:12:38 -0700155static ssize_t
156store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
157{
158 int i;
159 struct Scsi_Host *shost = class_to_shost(class_dev);
160 enum scsi_host_state state = 0;
161
Tobias Klauser6391a112006-06-08 22:23:48 -0700162 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -0700163 const int len = strlen(shost_states[i].name);
164 if (strncmp(shost_states[i].name, buf, len) == 0 &&
165 buf[len] == '\n') {
166 state = shost_states[i].value;
167 break;
168 }
169 }
170 if (!state)
171 return -EINVAL;
172
173 if (scsi_host_set_state(shost, state))
174 return -EINVAL;
175 return count;
176}
177
178static ssize_t
179show_shost_state(struct class_device *class_dev, char *buf)
180{
181 struct Scsi_Host *shost = class_to_shost(class_dev);
182 const char *name = scsi_host_state_name(shost->shost_state);
183
184 if (!name)
185 return -EINVAL;
186
187 return snprintf(buf, 20, "%s\n", name);
188}
189
190static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192shost_rd_attr(unique_id, "%u\n");
193shost_rd_attr(host_busy, "%hu\n");
194shost_rd_attr(cmd_per_lun, "%hd\n");
James Bottomleyed632da2006-10-16 10:06:27 -0500195shost_rd_attr(can_queue, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196shost_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,
James Bottomleyed632da2006-10-16 10:06:27 -0500204 &class_device_attr_can_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 &class_device_attr_sg_tablesize,
206 &class_device_attr_unchecked_isa_dma,
207 &class_device_attr_proc_name,
208 &class_device_attr_scan,
Mike Andersond3301872005-06-16 11:12:38 -0700209 &class_device_attr_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 NULL
211};
212
213static void scsi_device_cls_release(struct class_device *class_dev)
214{
215 struct scsi_device *sdev;
216
217 sdev = class_to_sdev(class_dev);
218 put_device(&sdev->sdev_gendev);
219}
220
David Howells65f27f32006-11-22 14:55:48 +0000221static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct scsi_device *sdev;
224 struct device *parent;
225 struct scsi_target *starget;
226 unsigned long flags;
227
David Howells65f27f32006-11-22 14:55:48 +0000228 sdev = container_of(work, struct scsi_device, ew.work);
229
230 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 starget = to_scsi_target(parent);
232
233 spin_lock_irqsave(sdev->host->host_lock, flags);
234 starget->reap_ref++;
235 list_del(&sdev->siblings);
236 list_del(&sdev->same_target_siblings);
237 list_del(&sdev->starved_entry);
238 spin_unlock_irqrestore(sdev->host->host_lock, flags);
239
240 if (sdev->request_queue) {
241 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600242 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500244 /* temporary expedient, try to catch use of queue lock
245 * after free of sdev */
246 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 scsi_target_reap(scsi_target(sdev));
250
251 kfree(sdev->inquiry);
252 kfree(sdev);
253
254 if (parent)
255 put_device(parent);
256}
257
James Bottomley65110b22006-02-14 10:48:46 -0600258static void scsi_device_dev_release(struct device *dev)
259{
James Bottomleyffedb452006-02-23 14:27:18 -0600260 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000261 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600262 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600263}
264
Adrian Bunk52c1da32005-06-23 22:05:33 -0700265static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .name = "scsi_device",
267 .release = scsi_device_cls_release,
268};
269
270/* all probing is done in the individual ->probe routines */
271static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
272{
273 struct scsi_device *sdp = to_scsi_device(dev);
274 if (sdp->no_uld_attach)
275 return 0;
276 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
277}
278
Jens Axboe9b847542006-01-06 09:28:07 +0100279static int scsi_bus_suspend(struct device * dev, pm_message_t state)
280{
281 struct scsi_device *sdev = to_scsi_device(dev);
282 struct scsi_host_template *sht = sdev->host->hostt;
283 int err;
284
285 err = scsi_device_quiesce(sdev);
286 if (err)
287 return err;
288
289 if (sht->suspend)
Nigel Cunningham082776e2006-03-23 23:22:16 +1000290 err = sht->suspend(sdev, state);
Jens Axboe9b847542006-01-06 09:28:07 +0100291
292 return err;
293}
294
295static int scsi_bus_resume(struct device * dev)
296{
297 struct scsi_device *sdev = to_scsi_device(dev);
298 struct scsi_host_template *sht = sdev->host->hostt;
299 int err = 0;
300
301 if (sht->resume)
302 err = sht->resume(sdev);
303
304 scsi_device_resume(sdev);
305 return err;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308struct bus_type scsi_bus_type = {
309 .name = "scsi",
310 .match = scsi_bus_match,
Jens Axboe9b847542006-01-06 09:28:07 +0100311 .suspend = scsi_bus_suspend,
312 .resume = scsi_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
315int scsi_sysfs_register(void)
316{
317 int error;
318
319 error = bus_register(&scsi_bus_type);
320 if (!error) {
321 error = class_register(&sdev_class);
322 if (error)
323 bus_unregister(&scsi_bus_type);
324 }
325
326 return error;
327}
328
329void scsi_sysfs_unregister(void)
330{
331 class_unregister(&sdev_class);
332 bus_unregister(&scsi_bus_type);
333}
334
335/*
336 * sdev_show_function: macro to create an attr function that can be used to
337 * show a non-bit field.
338 */
339#define sdev_show_function(field, format_string) \
340static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400341sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{ \
343 struct scsi_device *sdev; \
344 sdev = to_scsi_device(dev); \
345 return snprintf (buf, 20, format_string, sdev->field); \
346} \
347
348/*
349 * sdev_rd_attr: macro to create a function and attribute variable for a
350 * read only field.
351 */
352#define sdev_rd_attr(field, format_string) \
353 sdev_show_function(field, format_string) \
354static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
355
356
357/*
358 * sdev_rd_attr: create a function and attribute variable for a
359 * read/write field.
360 */
361#define sdev_rw_attr(field, format_string) \
362 sdev_show_function(field, format_string) \
363 \
364static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400365sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{ \
367 struct scsi_device *sdev; \
368 sdev = to_scsi_device(dev); \
369 snscanf (buf, 20, format_string, &sdev->field); \
370 return count; \
371} \
372static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
373
374/* Currently we don't export bit fields, but we might in future,
375 * so leave this code in */
376#if 0
377/*
378 * sdev_rd_attr: create a function and attribute variable for a
379 * read/write bit field.
380 */
381#define sdev_rw_attr_bit(field) \
382 sdev_show_function(field, "%d\n") \
383 \
384static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400385sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{ \
387 int ret; \
388 struct scsi_device *sdev; \
389 ret = scsi_sdev_check_buf_bit(buf); \
390 if (ret >= 0) { \
391 sdev = to_scsi_device(dev); \
392 sdev->field = ret; \
393 ret = count; \
394 } \
395 return ret; \
396} \
397static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
398
399/*
400 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
401 * else return -EINVAL.
402 */
403static int scsi_sdev_check_buf_bit(const char *buf)
404{
405 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
406 if (buf[0] == '1')
407 return 1;
408 else if (buf[0] == '0')
409 return 0;
410 else
411 return -EINVAL;
412 } else
413 return -EINVAL;
414}
415#endif
416/*
417 * Create the actual show/store functions and data structures.
418 */
419sdev_rd_attr (device_blocked, "%d\n");
420sdev_rd_attr (queue_depth, "%d\n");
421sdev_rd_attr (type, "%d\n");
422sdev_rd_attr (scsi_level, "%d\n");
423sdev_rd_attr (vendor, "%.8s\n");
424sdev_rd_attr (model, "%.16s\n");
425sdev_rd_attr (rev, "%.4s\n");
426
427static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400428sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct scsi_device *sdev;
431 sdev = to_scsi_device(dev);
432 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
433}
434
435static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400436sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct scsi_device *sdev;
439 int timeout;
440 sdev = to_scsi_device(dev);
441 sscanf (buf, "%d\n", &timeout);
442 sdev->timeout = timeout * HZ;
443 return count;
444}
445static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
446
447static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400448store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 scsi_rescan_device(dev);
451 return count;
452}
453static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
454
Yani Ioannou10523b32005-05-17 06:43:37 -0400455static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 size_t count)
457{
458 scsi_remove_device(to_scsi_device(dev));
459 return count;
460};
461static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
462
463static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400464store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 int i;
467 struct scsi_device *sdev = to_scsi_device(dev);
468 enum scsi_device_state state = 0;
469
Tobias Klauser6391a112006-06-08 22:23:48 -0700470 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 const int len = strlen(sdev_states[i].name);
472 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
473 buf[len] == '\n') {
474 state = sdev_states[i].value;
475 break;
476 }
477 }
478 if (!state)
479 return -EINVAL;
480
481 if (scsi_device_set_state(sdev, state))
482 return -EINVAL;
483 return count;
484}
485
486static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400487show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct scsi_device *sdev = to_scsi_device(dev);
490 const char *name = scsi_device_state_name(sdev->sdev_state);
491
492 if (!name)
493 return -EINVAL;
494
495 return snprintf(buf, 20, "%s\n", name);
496}
497
498static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
499
500static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400501show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct scsi_device *sdev = to_scsi_device(dev);
504 const char *name = "none";
505
506 if (sdev->ordered_tags)
507 name = "ordered";
508 else if (sdev->simple_tags)
509 name = "simple";
510
511 return snprintf(buf, 20, "%s\n", name);
512}
513
514static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
515
516static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400517show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
520}
521
522static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
523
524#define show_sdev_iostat(field) \
525static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400526show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{ \
528 struct scsi_device *sdev = to_scsi_device(dev); \
529 unsigned long long count = atomic_read(&sdev->field); \
530 return snprintf(buf, 20, "0x%llx\n", count); \
531} \
532static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
533
534show_sdev_iostat(iorequest_cnt);
535show_sdev_iostat(iodone_cnt);
536show_sdev_iostat(ioerr_cnt);
537
538
539/* Default template for device attributes. May NOT be modified */
540static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
541 &dev_attr_device_blocked,
542 &dev_attr_queue_depth,
543 &dev_attr_queue_type,
544 &dev_attr_type,
545 &dev_attr_scsi_level,
546 &dev_attr_vendor,
547 &dev_attr_model,
548 &dev_attr_rev,
549 &dev_attr_rescan,
550 &dev_attr_delete,
551 &dev_attr_state,
552 &dev_attr_timeout,
553 &dev_attr_iocounterbits,
554 &dev_attr_iorequest_cnt,
555 &dev_attr_iodone_cnt,
556 &dev_attr_ioerr_cnt,
557 NULL
558};
559
Yani Ioannou10523b32005-05-17 06:43:37 -0400560static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 size_t count)
562{
563 int depth, retval;
564 struct scsi_device *sdev = to_scsi_device(dev);
565 struct scsi_host_template *sht = sdev->host->hostt;
566
567 if (!sht->change_queue_depth)
568 return -EINVAL;
569
570 depth = simple_strtoul(buf, NULL, 0);
571
572 if (depth < 1)
573 return -EINVAL;
574
575 retval = sht->change_queue_depth(sdev, depth);
576 if (retval < 0)
577 return retval;
578
579 return count;
580}
581
582static struct device_attribute sdev_attr_queue_depth_rw =
583 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
584 sdev_store_queue_depth_rw);
585
Yani Ioannou10523b32005-05-17 06:43:37 -0400586static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 size_t count)
588{
589 struct scsi_device *sdev = to_scsi_device(dev);
590 struct scsi_host_template *sht = sdev->host->hostt;
591 int tag_type = 0, retval;
592 int prev_tag_type = scsi_get_tag_type(sdev);
593
594 if (!sdev->tagged_supported || !sht->change_queue_type)
595 return -EINVAL;
596
597 if (strncmp(buf, "ordered", 7) == 0)
598 tag_type = MSG_ORDERED_TAG;
599 else if (strncmp(buf, "simple", 6) == 0)
600 tag_type = MSG_SIMPLE_TAG;
601 else if (strncmp(buf, "none", 4) != 0)
602 return -EINVAL;
603
604 if (tag_type == prev_tag_type)
605 return count;
606
607 retval = sht->change_queue_type(sdev, tag_type);
608 if (retval < 0)
609 return retval;
610
611 return count;
612}
613
614static struct device_attribute sdev_attr_queue_type_rw =
615 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
616 sdev_store_queue_type_rw);
617
618static struct device_attribute *attr_changed_internally(
619 struct Scsi_Host *shost,
620 struct device_attribute * attr)
621{
622 if (!strcmp("queue_depth", attr->attr.name)
623 && shost->hostt->change_queue_depth)
624 return &sdev_attr_queue_depth_rw;
625 else if (!strcmp("queue_type", attr->attr.name)
626 && shost->hostt->change_queue_type)
627 return &sdev_attr_queue_type_rw;
628 return attr;
629}
630
631
632static struct device_attribute *attr_overridden(
633 struct device_attribute **attrs,
634 struct device_attribute *attr)
635{
636 int i;
637
638 if (!attrs)
639 return NULL;
640 for (i = 0; attrs[i]; i++)
641 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
642 return attrs[i];
643 return NULL;
644}
645
646static int attr_add(struct device *dev, struct device_attribute *attr)
647{
648 struct device_attribute *base_attr;
649
650 /*
651 * Spare the caller from having to copy things it's not interested in.
652 */
653 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
654 if (base_attr) {
655 /* extend permissions */
656 attr->attr.mode |= base_attr->attr.mode;
657
658 /* override null show/store with default */
659 if (!attr->show)
660 attr->show = base_attr->show;
661 if (!attr->store)
662 attr->store = base_attr->store;
663 }
664
665 return device_create_file(dev, attr);
666}
667
668/**
669 * scsi_sysfs_add_sdev - add scsi device to sysfs
670 * @sdev: scsi_device to add
671 *
672 * Return value:
673 * 0 on Success / non-zero on Failure
674 **/
675int scsi_sysfs_add_sdev(struct scsi_device *sdev)
676{
677 int error, i;
678
679 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
680 return error;
681
682 error = device_add(&sdev->sdev_gendev);
683 if (error) {
684 put_device(sdev->sdev_gendev.parent);
685 printk(KERN_INFO "error 1\n");
686 return error;
687 }
688 error = class_device_add(&sdev->sdev_classdev);
689 if (error) {
690 printk(KERN_INFO "error 2\n");
691 goto clean_device;
692 }
693
694 /* take a reference for the sdev_classdev; this is
695 * released by the sdev_class .release */
696 get_device(&sdev->sdev_gendev);
697 if (sdev->host->hostt->sdev_attrs) {
698 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
699 error = attr_add(&sdev->sdev_gendev,
700 sdev->host->hostt->sdev_attrs[i]);
701 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400702 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto out;
704 }
705 }
706 }
707
708 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
709 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
710 scsi_sysfs_sdev_attrs[i])) {
711 struct device_attribute * attr =
712 attr_changed_internally(sdev->host,
713 scsi_sysfs_sdev_attrs[i]);
714 error = device_create_file(&sdev->sdev_gendev, attr);
715 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400716 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto out;
718 }
719 }
720 }
721
722 transport_add_device(&sdev->sdev_gendev);
723 out:
724 return error;
725
726 clean_device:
727 scsi_device_set_state(sdev, SDEV_CANCEL);
728
729 device_del(&sdev->sdev_gendev);
730 transport_destroy_device(&sdev->sdev_gendev);
731 put_device(&sdev->sdev_gendev);
732
733 return error;
734}
735
Alan Stern903f4fe2005-07-26 10:20:53 -0400736void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
James Bottomleydf133c22005-11-06 11:47:08 -0600738 struct device *dev = &sdev->sdev_gendev;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400741 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 class_device_unregister(&sdev->sdev_classdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600744 transport_remove_device(dev);
745 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 scsi_device_set_state(sdev, SDEV_DEL);
747 if (sdev->host->hostt->slave_destroy)
748 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600749 transport_destroy_device(dev);
750 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400751}
752
753/**
754 * scsi_remove_device - unregister a device from the scsi bus
755 * @sdev: scsi_device to unregister
756 **/
757void scsi_remove_device(struct scsi_device *sdev)
758{
Alan Stern54195002005-09-15 21:52:51 -0400759 struct Scsi_Host *shost = sdev->host;
760
Arjan van de Ven0b950672006-01-11 13:16:10 +0100761 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400762 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100763 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765EXPORT_SYMBOL(scsi_remove_device);
766
767void __scsi_remove_target(struct scsi_target *starget)
768{
769 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
770 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400771 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 spin_lock_irqsave(shost->host_lock, flags);
774 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400775 restart:
776 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400778 sdev->id != starget->id ||
779 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 continue;
781 spin_unlock_irqrestore(shost->host_lock, flags);
782 scsi_remove_device(sdev);
783 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400784 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 spin_unlock_irqrestore(shost->host_lock, flags);
787 scsi_target_reap(starget);
788}
789
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800790static int __remove_child (struct device * dev, void * data)
791{
792 if (scsi_is_target_device(dev))
793 __scsi_remove_target(to_scsi_target(dev));
794 return 0;
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/**
798 * scsi_remove_target - try to remove a target and all its devices
799 * @dev: generic starget or parent of generic stargets to be removed
800 *
801 * Note: This is slightly racy. It is possible that if the user
802 * requests the addition of another device then the target won't be
803 * removed.
804 */
805void scsi_remove_target(struct device *dev)
806{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800807 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (scsi_is_target_device(dev)) {
810 __scsi_remove_target(to_scsi_target(dev));
811 return;
812 }
813
814 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800815 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 put_device(rdev);
817}
818EXPORT_SYMBOL(scsi_remove_target);
819
820int scsi_register_driver(struct device_driver *drv)
821{
822 drv->bus = &scsi_bus_type;
823
824 return driver_register(drv);
825}
826EXPORT_SYMBOL(scsi_register_driver);
827
828int scsi_register_interface(struct class_interface *intf)
829{
830 intf->class = &sdev_class;
831
832 return class_interface_register(intf);
833}
834EXPORT_SYMBOL(scsi_register_interface);
835
836
837static struct class_device_attribute *class_attr_overridden(
838 struct class_device_attribute **attrs,
839 struct class_device_attribute *attr)
840{
841 int i;
842
843 if (!attrs)
844 return NULL;
845 for (i = 0; attrs[i]; i++)
846 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
847 return attrs[i];
848 return NULL;
849}
850
851static int class_attr_add(struct class_device *classdev,
852 struct class_device_attribute *attr)
853{
854 struct class_device_attribute *base_attr;
855
856 /*
857 * Spare the caller from having to copy things it's not interested in.
858 */
859 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
860 if (base_attr) {
861 /* extend permissions */
862 attr->attr.mode |= base_attr->attr.mode;
863
864 /* override null show/store with default */
865 if (!attr->show)
866 attr->show = base_attr->show;
867 if (!attr->store)
868 attr->store = base_attr->store;
869 }
870
871 return class_device_create_file(classdev, attr);
872}
873
874/**
875 * scsi_sysfs_add_host - add scsi host to subsystem
876 * @shost: scsi host struct to add to subsystem
877 * @dev: parent struct device pointer
878 **/
879int scsi_sysfs_add_host(struct Scsi_Host *shost)
880{
881 int error, i;
882
883 if (shost->hostt->shost_attrs) {
884 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
885 error = class_attr_add(&shost->shost_classdev,
886 shost->hostt->shost_attrs[i]);
887 if (error)
888 return error;
889 }
890 }
891
892 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
893 if (!class_attr_overridden(shost->hostt->shost_attrs,
894 scsi_sysfs_shost_attrs[i])) {
895 error = class_device_create_file(&shost->shost_classdev,
896 scsi_sysfs_shost_attrs[i]);
897 if (error)
898 return error;
899 }
900 }
901
902 transport_register_device(&shost->shost_gendev);
903 return 0;
904}
905
906void scsi_sysfs_device_initialize(struct scsi_device *sdev)
907{
908 unsigned long flags;
909 struct Scsi_Host *shost = sdev->host;
910 struct scsi_target *starget = sdev->sdev_target;
911
912 device_initialize(&sdev->sdev_gendev);
913 sdev->sdev_gendev.bus = &scsi_bus_type;
914 sdev->sdev_gendev.release = scsi_device_dev_release;
915 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
916 sdev->host->host_no, sdev->channel, sdev->id,
917 sdev->lun);
918
919 class_device_initialize(&sdev->sdev_classdev);
920 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
921 sdev->sdev_classdev.class = &sdev_class;
922 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
923 "%d:%d:%d:%d", sdev->host->host_no,
924 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -0500925 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 transport_setup_device(&sdev->sdev_gendev);
927 spin_lock_irqsave(shost->host_lock, flags);
928 list_add_tail(&sdev->same_target_siblings, &starget->devices);
929 list_add_tail(&sdev->siblings, &shost->__devices);
930 spin_unlock_irqrestore(shost->host_lock, flags);
931}
932
933int scsi_is_sdev_device(const struct device *dev)
934{
935 return dev->release == scsi_device_dev_release;
936}
937EXPORT_SYMBOL(scsi_is_sdev_device);
938
939/* A blank transport template that is used in drivers that don't
940 * yet implement Transport Attributes */
941struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };