blob: 34e483d31c18a741a6d8f326c2b14c7414f97b59 [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>
Adrian Bunk44818ef2007-07-09 11:59:59 -070019#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
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");
James Bottomleyed632da2006-10-16 10:06:27 -0500196shost_rd_attr(can_queue, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197shost_rd_attr(sg_tablesize, "%hu\n");
198shost_rd_attr(unchecked_isa_dma, "%d\n");
199shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
200
201static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
202 &class_device_attr_unique_id,
203 &class_device_attr_host_busy,
204 &class_device_attr_cmd_per_lun,
James Bottomleyed632da2006-10-16 10:06:27 -0500205 &class_device_attr_can_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 &class_device_attr_sg_tablesize,
207 &class_device_attr_unchecked_isa_dma,
208 &class_device_attr_proc_name,
209 &class_device_attr_scan,
Mike Andersond3301872005-06-16 11:12:38 -0700210 &class_device_attr_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 NULL
212};
213
214static void scsi_device_cls_release(struct class_device *class_dev)
215{
216 struct scsi_device *sdev;
217
218 sdev = class_to_sdev(class_dev);
219 put_device(&sdev->sdev_gendev);
220}
221
David Howells65f27f32006-11-22 14:55:48 +0000222static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct scsi_device *sdev;
225 struct device *parent;
226 struct scsi_target *starget;
227 unsigned long flags;
228
David Howells65f27f32006-11-22 14:55:48 +0000229 sdev = container_of(work, struct scsi_device, ew.work);
230
231 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 starget = to_scsi_target(parent);
233
234 spin_lock_irqsave(sdev->host->host_lock, flags);
235 starget->reap_ref++;
236 list_del(&sdev->siblings);
237 list_del(&sdev->same_target_siblings);
238 list_del(&sdev->starved_entry);
239 spin_unlock_irqrestore(sdev->host->host_lock, flags);
240
241 if (sdev->request_queue) {
242 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600243 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500245 /* temporary expedient, try to catch use of queue lock
246 * after free of sdev */
247 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 scsi_target_reap(scsi_target(sdev));
251
252 kfree(sdev->inquiry);
253 kfree(sdev);
254
255 if (parent)
256 put_device(parent);
257}
258
James Bottomley65110b22006-02-14 10:48:46 -0600259static void scsi_device_dev_release(struct device *dev)
260{
James Bottomleyffedb452006-02-23 14:27:18 -0600261 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000262 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600263 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600264}
265
Adrian Bunk52c1da32005-06-23 22:05:33 -0700266static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .name = "scsi_device",
268 .release = scsi_device_cls_release,
269};
270
271/* all probing is done in the individual ->probe routines */
272static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
273{
274 struct scsi_device *sdp = to_scsi_device(dev);
275 if (sdp->no_uld_attach)
276 return 0;
277 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
278}
279
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400280static int scsi_bus_uevent(struct device *dev, char **envp, int num_envp,
281 char *buffer, int buffer_size)
282{
283 struct scsi_device *sdev = to_scsi_device(dev);
284 int i = 0;
285 int length = 0;
286
287 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
288 "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
289 envp[i] = NULL;
290 return 0;
291}
292
Jens Axboe9b847542006-01-06 09:28:07 +0100293static int scsi_bus_suspend(struct device * dev, pm_message_t state)
294{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900295 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100296 struct scsi_device *sdev = to_scsi_device(dev);
Jens Axboe9b847542006-01-06 09:28:07 +0100297 int err;
298
299 err = scsi_device_quiesce(sdev);
300 if (err)
301 return err;
302
Tejun Heoc3c94c52007-03-21 00:13:59 +0900303 if (drv && drv->suspend) {
304 err = drv->suspend(dev, state);
305 if (err)
306 return err;
307 }
Jens Axboe9b847542006-01-06 09:28:07 +0100308
Tejun Heoc3c94c52007-03-21 00:13:59 +0900309 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100310}
311
312static int scsi_bus_resume(struct device * dev)
313{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900314 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100315 struct scsi_device *sdev = to_scsi_device(dev);
Tejun Heo1dfcda02007-03-21 16:05:16 +0900316 int err = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100317
Tejun Heoc3c94c52007-03-21 00:13:59 +0900318 if (drv && drv->resume)
Tejun Heo1dfcda02007-03-21 16:05:16 +0900319 err = drv->resume(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900320
Jens Axboe9b847542006-01-06 09:28:07 +0100321 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900322
Tejun Heo1dfcda02007-03-21 16:05:16 +0900323 return err;
Jens Axboe9b847542006-01-06 09:28:07 +0100324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326struct bus_type scsi_bus_type = {
327 .name = "scsi",
328 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400329 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100330 .suspend = scsi_bus_suspend,
331 .resume = scsi_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332};
333
334int scsi_sysfs_register(void)
335{
336 int error;
337
338 error = bus_register(&scsi_bus_type);
339 if (!error) {
340 error = class_register(&sdev_class);
341 if (error)
342 bus_unregister(&scsi_bus_type);
343 }
344
345 return error;
346}
347
348void scsi_sysfs_unregister(void)
349{
350 class_unregister(&sdev_class);
351 bus_unregister(&scsi_bus_type);
352}
353
354/*
355 * sdev_show_function: macro to create an attr function that can be used to
356 * show a non-bit field.
357 */
358#define sdev_show_function(field, format_string) \
359static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400360sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{ \
362 struct scsi_device *sdev; \
363 sdev = to_scsi_device(dev); \
364 return snprintf (buf, 20, format_string, sdev->field); \
365} \
366
367/*
368 * sdev_rd_attr: macro to create a function and attribute variable for a
369 * read only field.
370 */
371#define sdev_rd_attr(field, format_string) \
372 sdev_show_function(field, format_string) \
373static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
374
375
376/*
377 * sdev_rd_attr: create a function and attribute variable for a
378 * read/write field.
379 */
380#define sdev_rw_attr(field, format_string) \
381 sdev_show_function(field, format_string) \
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 struct scsi_device *sdev; \
387 sdev = to_scsi_device(dev); \
388 snscanf (buf, 20, format_string, &sdev->field); \
389 return count; \
390} \
391static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
392
393/* Currently we don't export bit fields, but we might in future,
394 * so leave this code in */
395#if 0
396/*
397 * sdev_rd_attr: create a function and attribute variable for a
398 * read/write bit field.
399 */
400#define sdev_rw_attr_bit(field) \
401 sdev_show_function(field, "%d\n") \
402 \
403static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400404sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{ \
406 int ret; \
407 struct scsi_device *sdev; \
408 ret = scsi_sdev_check_buf_bit(buf); \
409 if (ret >= 0) { \
410 sdev = to_scsi_device(dev); \
411 sdev->field = ret; \
412 ret = count; \
413 } \
414 return ret; \
415} \
416static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
417
418/*
419 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
420 * else return -EINVAL.
421 */
422static int scsi_sdev_check_buf_bit(const char *buf)
423{
424 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
425 if (buf[0] == '1')
426 return 1;
427 else if (buf[0] == '0')
428 return 0;
429 else
430 return -EINVAL;
431 } else
432 return -EINVAL;
433}
434#endif
435/*
436 * Create the actual show/store functions and data structures.
437 */
438sdev_rd_attr (device_blocked, "%d\n");
439sdev_rd_attr (queue_depth, "%d\n");
440sdev_rd_attr (type, "%d\n");
441sdev_rd_attr (scsi_level, "%d\n");
442sdev_rd_attr (vendor, "%.8s\n");
443sdev_rd_attr (model, "%.16s\n");
444sdev_rd_attr (rev, "%.4s\n");
445
446static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400447sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct scsi_device *sdev;
450 sdev = to_scsi_device(dev);
451 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
452}
453
454static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400455sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct scsi_device *sdev;
458 int timeout;
459 sdev = to_scsi_device(dev);
460 sscanf (buf, "%d\n", &timeout);
461 sdev->timeout = timeout * HZ;
462 return count;
463}
464static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
465
466static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400467store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 scsi_rescan_device(dev);
470 return count;
471}
472static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
473
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400474static void sdev_store_delete_callback(struct device *dev)
475{
476 scsi_remove_device(to_scsi_device(dev));
477}
478
Yani Ioannou10523b32005-05-17 06:43:37 -0400479static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 size_t count)
481{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400482 int rc;
483
484 /* An attribute cannot be unregistered by one of its own methods,
485 * so we have to use this roundabout approach.
486 */
487 rc = device_schedule_callback(dev, sdev_store_delete_callback);
488 if (rc)
489 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return count;
491};
492static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
493
494static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400495store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 int i;
498 struct scsi_device *sdev = to_scsi_device(dev);
499 enum scsi_device_state state = 0;
500
Tobias Klauser6391a112006-06-08 22:23:48 -0700501 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 const int len = strlen(sdev_states[i].name);
503 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
504 buf[len] == '\n') {
505 state = sdev_states[i].value;
506 break;
507 }
508 }
509 if (!state)
510 return -EINVAL;
511
512 if (scsi_device_set_state(sdev, state))
513 return -EINVAL;
514 return count;
515}
516
517static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400518show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct scsi_device *sdev = to_scsi_device(dev);
521 const char *name = scsi_device_state_name(sdev->sdev_state);
522
523 if (!name)
524 return -EINVAL;
525
526 return snprintf(buf, 20, "%s\n", name);
527}
528
529static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
530
531static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400532show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct scsi_device *sdev = to_scsi_device(dev);
535 const char *name = "none";
536
537 if (sdev->ordered_tags)
538 name = "ordered";
539 else if (sdev->simple_tags)
540 name = "simple";
541
542 return snprintf(buf, 20, "%s\n", name);
543}
544
545static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
546
547static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400548show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
551}
552
553static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
554
555#define show_sdev_iostat(field) \
556static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400557show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{ \
559 struct scsi_device *sdev = to_scsi_device(dev); \
560 unsigned long long count = atomic_read(&sdev->field); \
561 return snprintf(buf, 20, "0x%llx\n", count); \
562} \
563static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
564
565show_sdev_iostat(iorequest_cnt);
566show_sdev_iostat(iodone_cnt);
567show_sdev_iostat(ioerr_cnt);
568
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400569static ssize_t
570sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
571{
572 struct scsi_device *sdev;
573 sdev = to_scsi_device(dev);
574 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
575}
576static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/* Default template for device attributes. May NOT be modified */
579static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
580 &dev_attr_device_blocked,
581 &dev_attr_queue_depth,
582 &dev_attr_queue_type,
583 &dev_attr_type,
584 &dev_attr_scsi_level,
585 &dev_attr_vendor,
586 &dev_attr_model,
587 &dev_attr_rev,
588 &dev_attr_rescan,
589 &dev_attr_delete,
590 &dev_attr_state,
591 &dev_attr_timeout,
592 &dev_attr_iocounterbits,
593 &dev_attr_iorequest_cnt,
594 &dev_attr_iodone_cnt,
595 &dev_attr_ioerr_cnt,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400596 &dev_attr_modalias,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 NULL
598};
599
Yani Ioannou10523b32005-05-17 06:43:37 -0400600static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 size_t count)
602{
603 int depth, retval;
604 struct scsi_device *sdev = to_scsi_device(dev);
605 struct scsi_host_template *sht = sdev->host->hostt;
606
607 if (!sht->change_queue_depth)
608 return -EINVAL;
609
610 depth = simple_strtoul(buf, NULL, 0);
611
612 if (depth < 1)
613 return -EINVAL;
614
615 retval = sht->change_queue_depth(sdev, depth);
616 if (retval < 0)
617 return retval;
618
619 return count;
620}
621
622static struct device_attribute sdev_attr_queue_depth_rw =
623 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
624 sdev_store_queue_depth_rw);
625
Yani Ioannou10523b32005-05-17 06:43:37 -0400626static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 size_t count)
628{
629 struct scsi_device *sdev = to_scsi_device(dev);
630 struct scsi_host_template *sht = sdev->host->hostt;
631 int tag_type = 0, retval;
632 int prev_tag_type = scsi_get_tag_type(sdev);
633
634 if (!sdev->tagged_supported || !sht->change_queue_type)
635 return -EINVAL;
636
637 if (strncmp(buf, "ordered", 7) == 0)
638 tag_type = MSG_ORDERED_TAG;
639 else if (strncmp(buf, "simple", 6) == 0)
640 tag_type = MSG_SIMPLE_TAG;
641 else if (strncmp(buf, "none", 4) != 0)
642 return -EINVAL;
643
644 if (tag_type == prev_tag_type)
645 return count;
646
647 retval = sht->change_queue_type(sdev, tag_type);
648 if (retval < 0)
649 return retval;
650
651 return count;
652}
653
654static struct device_attribute sdev_attr_queue_type_rw =
655 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
656 sdev_store_queue_type_rw);
657
658static struct device_attribute *attr_changed_internally(
659 struct Scsi_Host *shost,
660 struct device_attribute * attr)
661{
662 if (!strcmp("queue_depth", attr->attr.name)
663 && shost->hostt->change_queue_depth)
664 return &sdev_attr_queue_depth_rw;
665 else if (!strcmp("queue_type", attr->attr.name)
666 && shost->hostt->change_queue_type)
667 return &sdev_attr_queue_type_rw;
668 return attr;
669}
670
671
672static struct device_attribute *attr_overridden(
673 struct device_attribute **attrs,
674 struct device_attribute *attr)
675{
676 int i;
677
678 if (!attrs)
679 return NULL;
680 for (i = 0; attrs[i]; i++)
681 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
682 return attrs[i];
683 return NULL;
684}
685
686static int attr_add(struct device *dev, struct device_attribute *attr)
687{
688 struct device_attribute *base_attr;
689
690 /*
691 * Spare the caller from having to copy things it's not interested in.
692 */
693 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
694 if (base_attr) {
695 /* extend permissions */
696 attr->attr.mode |= base_attr->attr.mode;
697
698 /* override null show/store with default */
699 if (!attr->show)
700 attr->show = base_attr->show;
701 if (!attr->store)
702 attr->store = base_attr->store;
703 }
704
705 return device_create_file(dev, attr);
706}
707
708/**
709 * scsi_sysfs_add_sdev - add scsi device to sysfs
710 * @sdev: scsi_device to add
711 *
712 * Return value:
713 * 0 on Success / non-zero on Failure
714 **/
715int scsi_sysfs_add_sdev(struct scsi_device *sdev)
716{
717 int error, i;
718
719 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
720 return error;
721
722 error = device_add(&sdev->sdev_gendev);
723 if (error) {
724 put_device(sdev->sdev_gendev.parent);
725 printk(KERN_INFO "error 1\n");
726 return error;
727 }
728 error = class_device_add(&sdev->sdev_classdev);
729 if (error) {
730 printk(KERN_INFO "error 2\n");
731 goto clean_device;
732 }
733
734 /* take a reference for the sdev_classdev; this is
735 * released by the sdev_class .release */
736 get_device(&sdev->sdev_gendev);
737 if (sdev->host->hostt->sdev_attrs) {
738 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
739 error = attr_add(&sdev->sdev_gendev,
740 sdev->host->hostt->sdev_attrs[i]);
741 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400742 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto out;
744 }
745 }
746 }
747
748 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
749 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
750 scsi_sysfs_sdev_attrs[i])) {
751 struct device_attribute * attr =
752 attr_changed_internally(sdev->host,
753 scsi_sysfs_sdev_attrs[i]);
754 error = device_create_file(&sdev->sdev_gendev, attr);
755 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400756 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto out;
758 }
759 }
760 }
761
762 transport_add_device(&sdev->sdev_gendev);
763 out:
764 return error;
765
766 clean_device:
767 scsi_device_set_state(sdev, SDEV_CANCEL);
768
769 device_del(&sdev->sdev_gendev);
770 transport_destroy_device(&sdev->sdev_gendev);
771 put_device(&sdev->sdev_gendev);
772
773 return error;
774}
775
Alan Stern903f4fe2005-07-26 10:20:53 -0400776void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
James Bottomleydf133c22005-11-06 11:47:08 -0600778 struct device *dev = &sdev->sdev_gendev;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400781 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 class_device_unregister(&sdev->sdev_classdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600784 transport_remove_device(dev);
785 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 scsi_device_set_state(sdev, SDEV_DEL);
787 if (sdev->host->hostt->slave_destroy)
788 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600789 transport_destroy_device(dev);
790 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400791}
792
793/**
794 * scsi_remove_device - unregister a device from the scsi bus
795 * @sdev: scsi_device to unregister
796 **/
797void scsi_remove_device(struct scsi_device *sdev)
798{
Alan Stern54195002005-09-15 21:52:51 -0400799 struct Scsi_Host *shost = sdev->host;
800
Arjan van de Ven0b950672006-01-11 13:16:10 +0100801 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400802 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100803 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805EXPORT_SYMBOL(scsi_remove_device);
806
Adrian Bunk44818ef2007-07-09 11:59:59 -0700807static void __scsi_remove_target(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
810 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400811 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 spin_lock_irqsave(shost->host_lock, flags);
814 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400815 restart:
816 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400818 sdev->id != starget->id ||
819 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 continue;
821 spin_unlock_irqrestore(shost->host_lock, flags);
822 scsi_remove_device(sdev);
823 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400824 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 spin_unlock_irqrestore(shost->host_lock, flags);
827 scsi_target_reap(starget);
828}
829
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800830static int __remove_child (struct device * dev, void * data)
831{
832 if (scsi_is_target_device(dev))
833 __scsi_remove_target(to_scsi_target(dev));
834 return 0;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/**
838 * scsi_remove_target - try to remove a target and all its devices
839 * @dev: generic starget or parent of generic stargets to be removed
840 *
841 * Note: This is slightly racy. It is possible that if the user
842 * requests the addition of another device then the target won't be
843 * removed.
844 */
845void scsi_remove_target(struct device *dev)
846{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800847 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (scsi_is_target_device(dev)) {
850 __scsi_remove_target(to_scsi_target(dev));
851 return;
852 }
853
854 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800855 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 put_device(rdev);
857}
858EXPORT_SYMBOL(scsi_remove_target);
859
860int scsi_register_driver(struct device_driver *drv)
861{
862 drv->bus = &scsi_bus_type;
863
864 return driver_register(drv);
865}
866EXPORT_SYMBOL(scsi_register_driver);
867
868int scsi_register_interface(struct class_interface *intf)
869{
870 intf->class = &sdev_class;
871
872 return class_interface_register(intf);
873}
874EXPORT_SYMBOL(scsi_register_interface);
875
876
877static struct class_device_attribute *class_attr_overridden(
878 struct class_device_attribute **attrs,
879 struct class_device_attribute *attr)
880{
881 int i;
882
883 if (!attrs)
884 return NULL;
885 for (i = 0; attrs[i]; i++)
886 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
887 return attrs[i];
888 return NULL;
889}
890
891static int class_attr_add(struct class_device *classdev,
892 struct class_device_attribute *attr)
893{
894 struct class_device_attribute *base_attr;
895
896 /*
897 * Spare the caller from having to copy things it's not interested in.
898 */
899 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
900 if (base_attr) {
901 /* extend permissions */
902 attr->attr.mode |= base_attr->attr.mode;
903
904 /* override null show/store with default */
905 if (!attr->show)
906 attr->show = base_attr->show;
907 if (!attr->store)
908 attr->store = base_attr->store;
909 }
910
911 return class_device_create_file(classdev, attr);
912}
913
914/**
915 * scsi_sysfs_add_host - add scsi host to subsystem
916 * @shost: scsi host struct to add to subsystem
917 * @dev: parent struct device pointer
918 **/
919int scsi_sysfs_add_host(struct Scsi_Host *shost)
920{
921 int error, i;
922
923 if (shost->hostt->shost_attrs) {
924 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
925 error = class_attr_add(&shost->shost_classdev,
926 shost->hostt->shost_attrs[i]);
927 if (error)
928 return error;
929 }
930 }
931
932 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
933 if (!class_attr_overridden(shost->hostt->shost_attrs,
934 scsi_sysfs_shost_attrs[i])) {
935 error = class_device_create_file(&shost->shost_classdev,
936 scsi_sysfs_shost_attrs[i]);
937 if (error)
938 return error;
939 }
940 }
941
942 transport_register_device(&shost->shost_gendev);
943 return 0;
944}
945
946void scsi_sysfs_device_initialize(struct scsi_device *sdev)
947{
948 unsigned long flags;
949 struct Scsi_Host *shost = sdev->host;
950 struct scsi_target *starget = sdev->sdev_target;
951
952 device_initialize(&sdev->sdev_gendev);
953 sdev->sdev_gendev.bus = &scsi_bus_type;
954 sdev->sdev_gendev.release = scsi_device_dev_release;
955 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
956 sdev->host->host_no, sdev->channel, sdev->id,
957 sdev->lun);
958
959 class_device_initialize(&sdev->sdev_classdev);
960 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
961 sdev->sdev_classdev.class = &sdev_class;
962 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
963 "%d:%d:%d:%d", sdev->host->host_no,
964 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -0500965 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 transport_setup_device(&sdev->sdev_gendev);
967 spin_lock_irqsave(shost->host_lock, flags);
968 list_add_tail(&sdev->same_target_siblings, &starget->devices);
969 list_add_tail(&sdev->siblings, &shost->__devices);
970 spin_unlock_irqrestore(shost->host_lock, flags);
971}
972
973int scsi_is_sdev_device(const struct device *dev)
974{
975 return dev->release == scsi_device_dev_release;
976}
977EXPORT_SYMBOL(scsi_is_sdev_device);
978
979/* A blank transport template that is used in drivers that don't
980 * yet implement Transport Attributes */
981struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };