blob: a3d227f3f520459227f1929cde660736a0d9ef20 [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
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900193static ssize_t
194show_shost_mode(unsigned int mode, char *buf)
195{
196 ssize_t len = 0;
197
198 if (mode & MODE_INITIATOR)
199 len = sprintf(buf, "%s", "Initiator");
200
201 if (mode & MODE_TARGET)
202 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
203
204 len += sprintf(buf + len, "\n");
205
206 return len;
207}
208
209static ssize_t show_shost_supported_mode(struct class_device *class_dev, char *buf)
210{
211 struct Scsi_Host *shost = class_to_shost(class_dev);
212
213 if (shost->hostt->supported_mode == MODE_UNKNOWN)
214 return snprintf(buf, 20, "unknown\n");
215 else
216 return show_shost_mode(shost->hostt->supported_mode, buf);
217}
218
219static CLASS_DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
220
221static ssize_t show_shost_active_mode(struct class_device *class_dev, char *buf)
222{
223 struct Scsi_Host *shost = class_to_shost(class_dev);
224
225 if (shost->active_mode == MODE_UNKNOWN)
226 return snprintf(buf, 20, "unknown\n");
227 else
228 return show_shost_mode(shost->active_mode, buf);
229}
230
231static CLASS_DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233shost_rd_attr(unique_id, "%u\n");
234shost_rd_attr(host_busy, "%hu\n");
235shost_rd_attr(cmd_per_lun, "%hd\n");
James Bottomleyed632da2006-10-16 10:06:27 -0500236shost_rd_attr(can_queue, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237shost_rd_attr(sg_tablesize, "%hu\n");
238shost_rd_attr(unchecked_isa_dma, "%d\n");
239shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
240
241static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
242 &class_device_attr_unique_id,
243 &class_device_attr_host_busy,
244 &class_device_attr_cmd_per_lun,
James Bottomleyed632da2006-10-16 10:06:27 -0500245 &class_device_attr_can_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 &class_device_attr_sg_tablesize,
247 &class_device_attr_unchecked_isa_dma,
248 &class_device_attr_proc_name,
249 &class_device_attr_scan,
Mike Andersond3301872005-06-16 11:12:38 -0700250 &class_device_attr_state,
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900251 &class_device_attr_supported_mode,
252 &class_device_attr_active_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 NULL
254};
255
256static void scsi_device_cls_release(struct class_device *class_dev)
257{
258 struct scsi_device *sdev;
259
260 sdev = class_to_sdev(class_dev);
261 put_device(&sdev->sdev_gendev);
262}
263
David Howells65f27f32006-11-22 14:55:48 +0000264static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct scsi_device *sdev;
267 struct device *parent;
268 struct scsi_target *starget;
269 unsigned long flags;
270
David Howells65f27f32006-11-22 14:55:48 +0000271 sdev = container_of(work, struct scsi_device, ew.work);
272
273 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 starget = to_scsi_target(parent);
275
276 spin_lock_irqsave(sdev->host->host_lock, flags);
277 starget->reap_ref++;
278 list_del(&sdev->siblings);
279 list_del(&sdev->same_target_siblings);
280 list_del(&sdev->starved_entry);
281 spin_unlock_irqrestore(sdev->host->host_lock, flags);
282
283 if (sdev->request_queue) {
284 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600285 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500287 /* temporary expedient, try to catch use of queue lock
288 * after free of sdev */
289 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
292 scsi_target_reap(scsi_target(sdev));
293
294 kfree(sdev->inquiry);
295 kfree(sdev);
296
297 if (parent)
298 put_device(parent);
299}
300
James Bottomley65110b22006-02-14 10:48:46 -0600301static void scsi_device_dev_release(struct device *dev)
302{
James Bottomleyffedb452006-02-23 14:27:18 -0600303 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000304 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600305 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600306}
307
Adrian Bunk52c1da32005-06-23 22:05:33 -0700308static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 .name = "scsi_device",
310 .release = scsi_device_cls_release,
311};
312
313/* all probing is done in the individual ->probe routines */
314static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
315{
316 struct scsi_device *sdp = to_scsi_device(dev);
317 if (sdp->no_uld_attach)
318 return 0;
319 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
320}
321
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400322static int scsi_bus_uevent(struct device *dev, char **envp, int num_envp,
323 char *buffer, int buffer_size)
324{
325 struct scsi_device *sdev = to_scsi_device(dev);
326 int i = 0;
327 int length = 0;
328
329 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
330 "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
331 envp[i] = NULL;
332 return 0;
333}
334
Jens Axboe9b847542006-01-06 09:28:07 +0100335static int scsi_bus_suspend(struct device * dev, pm_message_t state)
336{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900337 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100338 struct scsi_device *sdev = to_scsi_device(dev);
Jens Axboe9b847542006-01-06 09:28:07 +0100339 int err;
340
341 err = scsi_device_quiesce(sdev);
342 if (err)
343 return err;
344
Tejun Heoc3c94c52007-03-21 00:13:59 +0900345 if (drv && drv->suspend) {
346 err = drv->suspend(dev, state);
347 if (err)
348 return err;
349 }
Jens Axboe9b847542006-01-06 09:28:07 +0100350
Tejun Heoc3c94c52007-03-21 00:13:59 +0900351 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100352}
353
354static int scsi_bus_resume(struct device * dev)
355{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900356 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100357 struct scsi_device *sdev = to_scsi_device(dev);
Tejun Heo1dfcda02007-03-21 16:05:16 +0900358 int err = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100359
Tejun Heoc3c94c52007-03-21 00:13:59 +0900360 if (drv && drv->resume)
Tejun Heo1dfcda02007-03-21 16:05:16 +0900361 err = drv->resume(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900362
Jens Axboe9b847542006-01-06 09:28:07 +0100363 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900364
Tejun Heo1dfcda02007-03-21 16:05:16 +0900365 return err;
Jens Axboe9b847542006-01-06 09:28:07 +0100366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368struct bus_type scsi_bus_type = {
369 .name = "scsi",
370 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400371 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100372 .suspend = scsi_bus_suspend,
373 .resume = scsi_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
375
376int scsi_sysfs_register(void)
377{
378 int error;
379
380 error = bus_register(&scsi_bus_type);
381 if (!error) {
382 error = class_register(&sdev_class);
383 if (error)
384 bus_unregister(&scsi_bus_type);
385 }
386
387 return error;
388}
389
390void scsi_sysfs_unregister(void)
391{
392 class_unregister(&sdev_class);
393 bus_unregister(&scsi_bus_type);
394}
395
396/*
397 * sdev_show_function: macro to create an attr function that can be used to
398 * show a non-bit field.
399 */
400#define sdev_show_function(field, format_string) \
401static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400402sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{ \
404 struct scsi_device *sdev; \
405 sdev = to_scsi_device(dev); \
406 return snprintf (buf, 20, format_string, sdev->field); \
407} \
408
409/*
410 * sdev_rd_attr: macro to create a function and attribute variable for a
411 * read only field.
412 */
413#define sdev_rd_attr(field, format_string) \
414 sdev_show_function(field, format_string) \
415static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
416
417
418/*
419 * sdev_rd_attr: create a function and attribute variable for a
420 * read/write field.
421 */
422#define sdev_rw_attr(field, format_string) \
423 sdev_show_function(field, format_string) \
424 \
425static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400426sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{ \
428 struct scsi_device *sdev; \
429 sdev = to_scsi_device(dev); \
430 snscanf (buf, 20, format_string, &sdev->field); \
431 return count; \
432} \
433static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
434
435/* Currently we don't export bit fields, but we might in future,
436 * so leave this code in */
437#if 0
438/*
439 * sdev_rd_attr: create a function and attribute variable for a
440 * read/write bit field.
441 */
442#define sdev_rw_attr_bit(field) \
443 sdev_show_function(field, "%d\n") \
444 \
445static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400446sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{ \
448 int ret; \
449 struct scsi_device *sdev; \
450 ret = scsi_sdev_check_buf_bit(buf); \
451 if (ret >= 0) { \
452 sdev = to_scsi_device(dev); \
453 sdev->field = ret; \
454 ret = count; \
455 } \
456 return ret; \
457} \
458static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
459
460/*
461 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
462 * else return -EINVAL.
463 */
464static int scsi_sdev_check_buf_bit(const char *buf)
465{
466 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
467 if (buf[0] == '1')
468 return 1;
469 else if (buf[0] == '0')
470 return 0;
471 else
472 return -EINVAL;
473 } else
474 return -EINVAL;
475}
476#endif
477/*
478 * Create the actual show/store functions and data structures.
479 */
480sdev_rd_attr (device_blocked, "%d\n");
481sdev_rd_attr (queue_depth, "%d\n");
482sdev_rd_attr (type, "%d\n");
483sdev_rd_attr (scsi_level, "%d\n");
484sdev_rd_attr (vendor, "%.8s\n");
485sdev_rd_attr (model, "%.16s\n");
486sdev_rd_attr (rev, "%.4s\n");
487
488static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400489sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct scsi_device *sdev;
492 sdev = to_scsi_device(dev);
493 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
494}
495
496static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400497sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct scsi_device *sdev;
500 int timeout;
501 sdev = to_scsi_device(dev);
502 sscanf (buf, "%d\n", &timeout);
503 sdev->timeout = timeout * HZ;
504 return count;
505}
506static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
507
508static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400509store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 scsi_rescan_device(dev);
512 return count;
513}
514static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
515
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400516static void sdev_store_delete_callback(struct device *dev)
517{
518 scsi_remove_device(to_scsi_device(dev));
519}
520
Yani Ioannou10523b32005-05-17 06:43:37 -0400521static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 size_t count)
523{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400524 int rc;
525
526 /* An attribute cannot be unregistered by one of its own methods,
527 * so we have to use this roundabout approach.
528 */
529 rc = device_schedule_callback(dev, sdev_store_delete_callback);
530 if (rc)
531 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return count;
533};
534static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
535
536static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400537store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int i;
540 struct scsi_device *sdev = to_scsi_device(dev);
541 enum scsi_device_state state = 0;
542
Tobias Klauser6391a112006-06-08 22:23:48 -0700543 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 const int len = strlen(sdev_states[i].name);
545 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
546 buf[len] == '\n') {
547 state = sdev_states[i].value;
548 break;
549 }
550 }
551 if (!state)
552 return -EINVAL;
553
554 if (scsi_device_set_state(sdev, state))
555 return -EINVAL;
556 return count;
557}
558
559static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400560show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct scsi_device *sdev = to_scsi_device(dev);
563 const char *name = scsi_device_state_name(sdev->sdev_state);
564
565 if (!name)
566 return -EINVAL;
567
568 return snprintf(buf, 20, "%s\n", name);
569}
570
571static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
572
573static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400574show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct scsi_device *sdev = to_scsi_device(dev);
577 const char *name = "none";
578
579 if (sdev->ordered_tags)
580 name = "ordered";
581 else if (sdev->simple_tags)
582 name = "simple";
583
584 return snprintf(buf, 20, "%s\n", name);
585}
586
587static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
588
589static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400590show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
593}
594
595static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
596
597#define show_sdev_iostat(field) \
598static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400599show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{ \
601 struct scsi_device *sdev = to_scsi_device(dev); \
602 unsigned long long count = atomic_read(&sdev->field); \
603 return snprintf(buf, 20, "0x%llx\n", count); \
604} \
605static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
606
607show_sdev_iostat(iorequest_cnt);
608show_sdev_iostat(iodone_cnt);
609show_sdev_iostat(ioerr_cnt);
610
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400611static ssize_t
612sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
613{
614 struct scsi_device *sdev;
615 sdev = to_scsi_device(dev);
616 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
617}
618static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620/* Default template for device attributes. May NOT be modified */
621static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
622 &dev_attr_device_blocked,
623 &dev_attr_queue_depth,
624 &dev_attr_queue_type,
625 &dev_attr_type,
626 &dev_attr_scsi_level,
627 &dev_attr_vendor,
628 &dev_attr_model,
629 &dev_attr_rev,
630 &dev_attr_rescan,
631 &dev_attr_delete,
632 &dev_attr_state,
633 &dev_attr_timeout,
634 &dev_attr_iocounterbits,
635 &dev_attr_iorequest_cnt,
636 &dev_attr_iodone_cnt,
637 &dev_attr_ioerr_cnt,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400638 &dev_attr_modalias,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 NULL
640};
641
Yani Ioannou10523b32005-05-17 06:43:37 -0400642static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 size_t count)
644{
645 int depth, retval;
646 struct scsi_device *sdev = to_scsi_device(dev);
647 struct scsi_host_template *sht = sdev->host->hostt;
648
649 if (!sht->change_queue_depth)
650 return -EINVAL;
651
652 depth = simple_strtoul(buf, NULL, 0);
653
654 if (depth < 1)
655 return -EINVAL;
656
657 retval = sht->change_queue_depth(sdev, depth);
658 if (retval < 0)
659 return retval;
660
661 return count;
662}
663
664static struct device_attribute sdev_attr_queue_depth_rw =
665 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
666 sdev_store_queue_depth_rw);
667
Yani Ioannou10523b32005-05-17 06:43:37 -0400668static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 size_t count)
670{
671 struct scsi_device *sdev = to_scsi_device(dev);
672 struct scsi_host_template *sht = sdev->host->hostt;
673 int tag_type = 0, retval;
674 int prev_tag_type = scsi_get_tag_type(sdev);
675
676 if (!sdev->tagged_supported || !sht->change_queue_type)
677 return -EINVAL;
678
679 if (strncmp(buf, "ordered", 7) == 0)
680 tag_type = MSG_ORDERED_TAG;
681 else if (strncmp(buf, "simple", 6) == 0)
682 tag_type = MSG_SIMPLE_TAG;
683 else if (strncmp(buf, "none", 4) != 0)
684 return -EINVAL;
685
686 if (tag_type == prev_tag_type)
687 return count;
688
689 retval = sht->change_queue_type(sdev, tag_type);
690 if (retval < 0)
691 return retval;
692
693 return count;
694}
695
696static struct device_attribute sdev_attr_queue_type_rw =
697 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
698 sdev_store_queue_type_rw);
699
700static struct device_attribute *attr_changed_internally(
701 struct Scsi_Host *shost,
702 struct device_attribute * attr)
703{
704 if (!strcmp("queue_depth", attr->attr.name)
705 && shost->hostt->change_queue_depth)
706 return &sdev_attr_queue_depth_rw;
707 else if (!strcmp("queue_type", attr->attr.name)
708 && shost->hostt->change_queue_type)
709 return &sdev_attr_queue_type_rw;
710 return attr;
711}
712
713
714static struct device_attribute *attr_overridden(
715 struct device_attribute **attrs,
716 struct device_attribute *attr)
717{
718 int i;
719
720 if (!attrs)
721 return NULL;
722 for (i = 0; attrs[i]; i++)
723 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
724 return attrs[i];
725 return NULL;
726}
727
728static int attr_add(struct device *dev, struct device_attribute *attr)
729{
730 struct device_attribute *base_attr;
731
732 /*
733 * Spare the caller from having to copy things it's not interested in.
734 */
735 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
736 if (base_attr) {
737 /* extend permissions */
738 attr->attr.mode |= base_attr->attr.mode;
739
740 /* override null show/store with default */
741 if (!attr->show)
742 attr->show = base_attr->show;
743 if (!attr->store)
744 attr->store = base_attr->store;
745 }
746
747 return device_create_file(dev, attr);
748}
749
750/**
751 * scsi_sysfs_add_sdev - add scsi device to sysfs
752 * @sdev: scsi_device to add
753 *
754 * Return value:
755 * 0 on Success / non-zero on Failure
756 **/
757int scsi_sysfs_add_sdev(struct scsi_device *sdev)
758{
759 int error, i;
James Bottomley80ed71c2007-07-19 10:15:10 -0500760 struct request_queue *rq = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
763 return error;
764
765 error = device_add(&sdev->sdev_gendev);
766 if (error) {
767 put_device(sdev->sdev_gendev.parent);
768 printk(KERN_INFO "error 1\n");
769 return error;
770 }
771 error = class_device_add(&sdev->sdev_classdev);
772 if (error) {
773 printk(KERN_INFO "error 2\n");
774 goto clean_device;
775 }
776
777 /* take a reference for the sdev_classdev; this is
778 * released by the sdev_class .release */
779 get_device(&sdev->sdev_gendev);
James Bottomley80ed71c2007-07-19 10:15:10 -0500780
James Bottomley39dca552007-07-20 18:22:17 -0500781 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
James Bottomley80ed71c2007-07-19 10:15:10 -0500782
783 if (error)
784 sdev_printk(KERN_INFO, sdev,
785 "Failed to register bsg queue, errno=%d\n", error);
786
787 /* we're treating error on bsg register as non-fatal, so pretend
788 * nothing went wrong */
789 error = 0;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (sdev->host->hostt->sdev_attrs) {
792 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
793 error = attr_add(&sdev->sdev_gendev,
794 sdev->host->hostt->sdev_attrs[i]);
795 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400796 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto out;
798 }
799 }
800 }
801
802 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
803 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
804 scsi_sysfs_sdev_attrs[i])) {
805 struct device_attribute * attr =
806 attr_changed_internally(sdev->host,
807 scsi_sysfs_sdev_attrs[i]);
808 error = device_create_file(&sdev->sdev_gendev, attr);
809 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400810 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto out;
812 }
813 }
814 }
815
816 transport_add_device(&sdev->sdev_gendev);
817 out:
818 return error;
819
820 clean_device:
821 scsi_device_set_state(sdev, SDEV_CANCEL);
822
823 device_del(&sdev->sdev_gendev);
824 transport_destroy_device(&sdev->sdev_gendev);
825 put_device(&sdev->sdev_gendev);
826
827 return error;
828}
829
Alan Stern903f4fe2005-07-26 10:20:53 -0400830void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
James Bottomleydf133c22005-11-06 11:47:08 -0600832 struct device *dev = &sdev->sdev_gendev;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400835 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
James Bottomley80ed71c2007-07-19 10:15:10 -0500837 bsg_unregister_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 class_device_unregister(&sdev->sdev_classdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600839 transport_remove_device(dev);
840 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 scsi_device_set_state(sdev, SDEV_DEL);
842 if (sdev->host->hostt->slave_destroy)
843 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600844 transport_destroy_device(dev);
845 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400846}
847
848/**
849 * scsi_remove_device - unregister a device from the scsi bus
850 * @sdev: scsi_device to unregister
851 **/
852void scsi_remove_device(struct scsi_device *sdev)
853{
Alan Stern54195002005-09-15 21:52:51 -0400854 struct Scsi_Host *shost = sdev->host;
855
Arjan van de Ven0b950672006-01-11 13:16:10 +0100856 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400857 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100858 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860EXPORT_SYMBOL(scsi_remove_device);
861
Adrian Bunk44818ef2007-07-09 11:59:59 -0700862static void __scsi_remove_target(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
865 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400866 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 spin_lock_irqsave(shost->host_lock, flags);
869 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400870 restart:
871 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400873 sdev->id != starget->id ||
874 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 continue;
876 spin_unlock_irqrestore(shost->host_lock, flags);
877 scsi_remove_device(sdev);
878 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400879 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 spin_unlock_irqrestore(shost->host_lock, flags);
882 scsi_target_reap(starget);
883}
884
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800885static int __remove_child (struct device * dev, void * data)
886{
887 if (scsi_is_target_device(dev))
888 __scsi_remove_target(to_scsi_target(dev));
889 return 0;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/**
893 * scsi_remove_target - try to remove a target and all its devices
894 * @dev: generic starget or parent of generic stargets to be removed
895 *
896 * Note: This is slightly racy. It is possible that if the user
897 * requests the addition of another device then the target won't be
898 * removed.
899 */
900void scsi_remove_target(struct device *dev)
901{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800902 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (scsi_is_target_device(dev)) {
905 __scsi_remove_target(to_scsi_target(dev));
906 return;
907 }
908
909 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800910 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 put_device(rdev);
912}
913EXPORT_SYMBOL(scsi_remove_target);
914
915int scsi_register_driver(struct device_driver *drv)
916{
917 drv->bus = &scsi_bus_type;
918
919 return driver_register(drv);
920}
921EXPORT_SYMBOL(scsi_register_driver);
922
923int scsi_register_interface(struct class_interface *intf)
924{
925 intf->class = &sdev_class;
926
927 return class_interface_register(intf);
928}
929EXPORT_SYMBOL(scsi_register_interface);
930
931
932static struct class_device_attribute *class_attr_overridden(
933 struct class_device_attribute **attrs,
934 struct class_device_attribute *attr)
935{
936 int i;
937
938 if (!attrs)
939 return NULL;
940 for (i = 0; attrs[i]; i++)
941 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
942 return attrs[i];
943 return NULL;
944}
945
946static int class_attr_add(struct class_device *classdev,
947 struct class_device_attribute *attr)
948{
949 struct class_device_attribute *base_attr;
950
951 /*
952 * Spare the caller from having to copy things it's not interested in.
953 */
954 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
955 if (base_attr) {
956 /* extend permissions */
957 attr->attr.mode |= base_attr->attr.mode;
958
959 /* override null show/store with default */
960 if (!attr->show)
961 attr->show = base_attr->show;
962 if (!attr->store)
963 attr->store = base_attr->store;
964 }
965
966 return class_device_create_file(classdev, attr);
967}
968
969/**
970 * scsi_sysfs_add_host - add scsi host to subsystem
971 * @shost: scsi host struct to add to subsystem
972 * @dev: parent struct device pointer
973 **/
974int scsi_sysfs_add_host(struct Scsi_Host *shost)
975{
976 int error, i;
977
978 if (shost->hostt->shost_attrs) {
979 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
980 error = class_attr_add(&shost->shost_classdev,
981 shost->hostt->shost_attrs[i]);
982 if (error)
983 return error;
984 }
985 }
986
987 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
988 if (!class_attr_overridden(shost->hostt->shost_attrs,
989 scsi_sysfs_shost_attrs[i])) {
990 error = class_device_create_file(&shost->shost_classdev,
991 scsi_sysfs_shost_attrs[i]);
992 if (error)
993 return error;
994 }
995 }
996
997 transport_register_device(&shost->shost_gendev);
998 return 0;
999}
1000
1001void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1002{
1003 unsigned long flags;
1004 struct Scsi_Host *shost = sdev->host;
1005 struct scsi_target *starget = sdev->sdev_target;
1006
1007 device_initialize(&sdev->sdev_gendev);
1008 sdev->sdev_gendev.bus = &scsi_bus_type;
1009 sdev->sdev_gendev.release = scsi_device_dev_release;
1010 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1011 sdev->host->host_no, sdev->channel, sdev->id,
1012 sdev->lun);
1013
1014 class_device_initialize(&sdev->sdev_classdev);
1015 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
1016 sdev->sdev_classdev.class = &sdev_class;
1017 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
1018 "%d:%d:%d:%d", sdev->host->host_no,
1019 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -05001020 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 transport_setup_device(&sdev->sdev_gendev);
1022 spin_lock_irqsave(shost->host_lock, flags);
1023 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1024 list_add_tail(&sdev->siblings, &shost->__devices);
1025 spin_unlock_irqrestore(shost->host_lock, flags);
1026}
1027
1028int scsi_is_sdev_device(const struct device *dev)
1029{
1030 return dev->release == scsi_device_dev_release;
1031}
1032EXPORT_SYMBOL(scsi_is_sdev_device);
1033
1034/* A blank transport template that is used in drivers that don't
1035 * yet implement Transport Attributes */
1036struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };