blob: 67a38a1409ba0e8274b2d24292a88aea959000ac [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
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400279static int scsi_bus_uevent(struct device *dev, char **envp, int num_envp,
280 char *buffer, int buffer_size)
281{
282 struct scsi_device *sdev = to_scsi_device(dev);
283 int i = 0;
284 int length = 0;
285
286 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
287 "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
288 envp[i] = NULL;
289 return 0;
290}
291
Jens Axboe9b847542006-01-06 09:28:07 +0100292static int scsi_bus_suspend(struct device * dev, pm_message_t state)
293{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900294 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100295 struct scsi_device *sdev = to_scsi_device(dev);
296 struct scsi_host_template *sht = sdev->host->hostt;
297 int err;
298
299 err = scsi_device_quiesce(sdev);
300 if (err)
301 return err;
302
Tejun Heoc3c94c52007-03-21 00:13:59 +0900303 /* call HLD suspend first */
304 if (drv && drv->suspend) {
305 err = drv->suspend(dev, state);
306 if (err)
307 return err;
308 }
Jens Axboe9b847542006-01-06 09:28:07 +0100309
Tejun Heoc3c94c52007-03-21 00:13:59 +0900310 /* then, call host suspend */
311 if (sht->suspend) {
312 err = sht->suspend(sdev, state);
313 if (err) {
314 if (drv && drv->resume)
315 drv->resume(dev);
316 return err;
317 }
318 }
319
320 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100321}
322
323static int scsi_bus_resume(struct device * dev)
324{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900325 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100326 struct scsi_device *sdev = to_scsi_device(dev);
327 struct scsi_host_template *sht = sdev->host->hostt;
Tejun Heoc3c94c52007-03-21 00:13:59 +0900328 int err = 0, err2 = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100329
Tejun Heoc3c94c52007-03-21 00:13:59 +0900330 /* call host resume first */
Jens Axboe9b847542006-01-06 09:28:07 +0100331 if (sht->resume)
332 err = sht->resume(sdev);
333
Tejun Heoc3c94c52007-03-21 00:13:59 +0900334 /* then, call HLD resume */
335 if (drv && drv->resume)
336 err2 = drv->resume(dev);
337
Jens Axboe9b847542006-01-06 09:28:07 +0100338 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900339
340 /* favor LLD failure */
341 return err ? err : err2;;
Jens Axboe9b847542006-01-06 09:28:07 +0100342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344struct bus_type scsi_bus_type = {
345 .name = "scsi",
346 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400347 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100348 .suspend = scsi_bus_suspend,
349 .resume = scsi_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
352int scsi_sysfs_register(void)
353{
354 int error;
355
356 error = bus_register(&scsi_bus_type);
357 if (!error) {
358 error = class_register(&sdev_class);
359 if (error)
360 bus_unregister(&scsi_bus_type);
361 }
362
363 return error;
364}
365
366void scsi_sysfs_unregister(void)
367{
368 class_unregister(&sdev_class);
369 bus_unregister(&scsi_bus_type);
370}
371
372/*
373 * sdev_show_function: macro to create an attr function that can be used to
374 * show a non-bit field.
375 */
376#define sdev_show_function(field, format_string) \
377static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400378sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{ \
380 struct scsi_device *sdev; \
381 sdev = to_scsi_device(dev); \
382 return snprintf (buf, 20, format_string, sdev->field); \
383} \
384
385/*
386 * sdev_rd_attr: macro to create a function and attribute variable for a
387 * read only field.
388 */
389#define sdev_rd_attr(field, format_string) \
390 sdev_show_function(field, format_string) \
391static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
392
393
394/*
395 * sdev_rd_attr: create a function and attribute variable for a
396 * read/write field.
397 */
398#define sdev_rw_attr(field, format_string) \
399 sdev_show_function(field, format_string) \
400 \
401static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400402sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{ \
404 struct scsi_device *sdev; \
405 sdev = to_scsi_device(dev); \
406 snscanf (buf, 20, format_string, &sdev->field); \
407 return count; \
408} \
409static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
410
411/* Currently we don't export bit fields, but we might in future,
412 * so leave this code in */
413#if 0
414/*
415 * sdev_rd_attr: create a function and attribute variable for a
416 * read/write bit field.
417 */
418#define sdev_rw_attr_bit(field) \
419 sdev_show_function(field, "%d\n") \
420 \
421static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400422sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{ \
424 int ret; \
425 struct scsi_device *sdev; \
426 ret = scsi_sdev_check_buf_bit(buf); \
427 if (ret >= 0) { \
428 sdev = to_scsi_device(dev); \
429 sdev->field = ret; \
430 ret = count; \
431 } \
432 return ret; \
433} \
434static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
435
436/*
437 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
438 * else return -EINVAL.
439 */
440static int scsi_sdev_check_buf_bit(const char *buf)
441{
442 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
443 if (buf[0] == '1')
444 return 1;
445 else if (buf[0] == '0')
446 return 0;
447 else
448 return -EINVAL;
449 } else
450 return -EINVAL;
451}
452#endif
453/*
454 * Create the actual show/store functions and data structures.
455 */
456sdev_rd_attr (device_blocked, "%d\n");
457sdev_rd_attr (queue_depth, "%d\n");
458sdev_rd_attr (type, "%d\n");
459sdev_rd_attr (scsi_level, "%d\n");
460sdev_rd_attr (vendor, "%.8s\n");
461sdev_rd_attr (model, "%.16s\n");
462sdev_rd_attr (rev, "%.4s\n");
463
464static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400465sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct scsi_device *sdev;
468 sdev = to_scsi_device(dev);
469 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
470}
471
472static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400473sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct scsi_device *sdev;
476 int timeout;
477 sdev = to_scsi_device(dev);
478 sscanf (buf, "%d\n", &timeout);
479 sdev->timeout = timeout * HZ;
480 return count;
481}
482static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
483
484static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400485store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 scsi_rescan_device(dev);
488 return count;
489}
490static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
491
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400492static void sdev_store_delete_callback(struct device *dev)
493{
494 scsi_remove_device(to_scsi_device(dev));
495}
496
Yani Ioannou10523b32005-05-17 06:43:37 -0400497static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 size_t count)
499{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400500 int rc;
501
502 /* An attribute cannot be unregistered by one of its own methods,
503 * so we have to use this roundabout approach.
504 */
505 rc = device_schedule_callback(dev, sdev_store_delete_callback);
506 if (rc)
507 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return count;
509};
510static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
511
512static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400513store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 int i;
516 struct scsi_device *sdev = to_scsi_device(dev);
517 enum scsi_device_state state = 0;
518
Tobias Klauser6391a112006-06-08 22:23:48 -0700519 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 const int len = strlen(sdev_states[i].name);
521 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
522 buf[len] == '\n') {
523 state = sdev_states[i].value;
524 break;
525 }
526 }
527 if (!state)
528 return -EINVAL;
529
530 if (scsi_device_set_state(sdev, state))
531 return -EINVAL;
532 return count;
533}
534
535static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400536show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct scsi_device *sdev = to_scsi_device(dev);
539 const char *name = scsi_device_state_name(sdev->sdev_state);
540
541 if (!name)
542 return -EINVAL;
543
544 return snprintf(buf, 20, "%s\n", name);
545}
546
547static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
548
549static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400550show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct scsi_device *sdev = to_scsi_device(dev);
553 const char *name = "none";
554
555 if (sdev->ordered_tags)
556 name = "ordered";
557 else if (sdev->simple_tags)
558 name = "simple";
559
560 return snprintf(buf, 20, "%s\n", name);
561}
562
563static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
564
565static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400566show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
569}
570
571static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
572
573#define show_sdev_iostat(field) \
574static ssize_t \
Yani Ioannou10523b32005-05-17 06:43:37 -0400575show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{ \
577 struct scsi_device *sdev = to_scsi_device(dev); \
578 unsigned long long count = atomic_read(&sdev->field); \
579 return snprintf(buf, 20, "0x%llx\n", count); \
580} \
581static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
582
583show_sdev_iostat(iorequest_cnt);
584show_sdev_iostat(iodone_cnt);
585show_sdev_iostat(ioerr_cnt);
586
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400587static ssize_t
588sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
589{
590 struct scsi_device *sdev;
591 sdev = to_scsi_device(dev);
592 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
593}
594static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/* Default template for device attributes. May NOT be modified */
597static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
598 &dev_attr_device_blocked,
599 &dev_attr_queue_depth,
600 &dev_attr_queue_type,
601 &dev_attr_type,
602 &dev_attr_scsi_level,
603 &dev_attr_vendor,
604 &dev_attr_model,
605 &dev_attr_rev,
606 &dev_attr_rescan,
607 &dev_attr_delete,
608 &dev_attr_state,
609 &dev_attr_timeout,
610 &dev_attr_iocounterbits,
611 &dev_attr_iorequest_cnt,
612 &dev_attr_iodone_cnt,
613 &dev_attr_ioerr_cnt,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400614 &dev_attr_modalias,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 NULL
616};
617
Yani Ioannou10523b32005-05-17 06:43:37 -0400618static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 size_t count)
620{
621 int depth, retval;
622 struct scsi_device *sdev = to_scsi_device(dev);
623 struct scsi_host_template *sht = sdev->host->hostt;
624
625 if (!sht->change_queue_depth)
626 return -EINVAL;
627
628 depth = simple_strtoul(buf, NULL, 0);
629
630 if (depth < 1)
631 return -EINVAL;
632
633 retval = sht->change_queue_depth(sdev, depth);
634 if (retval < 0)
635 return retval;
636
637 return count;
638}
639
640static struct device_attribute sdev_attr_queue_depth_rw =
641 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
642 sdev_store_queue_depth_rw);
643
Yani Ioannou10523b32005-05-17 06:43:37 -0400644static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 size_t count)
646{
647 struct scsi_device *sdev = to_scsi_device(dev);
648 struct scsi_host_template *sht = sdev->host->hostt;
649 int tag_type = 0, retval;
650 int prev_tag_type = scsi_get_tag_type(sdev);
651
652 if (!sdev->tagged_supported || !sht->change_queue_type)
653 return -EINVAL;
654
655 if (strncmp(buf, "ordered", 7) == 0)
656 tag_type = MSG_ORDERED_TAG;
657 else if (strncmp(buf, "simple", 6) == 0)
658 tag_type = MSG_SIMPLE_TAG;
659 else if (strncmp(buf, "none", 4) != 0)
660 return -EINVAL;
661
662 if (tag_type == prev_tag_type)
663 return count;
664
665 retval = sht->change_queue_type(sdev, tag_type);
666 if (retval < 0)
667 return retval;
668
669 return count;
670}
671
672static struct device_attribute sdev_attr_queue_type_rw =
673 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
674 sdev_store_queue_type_rw);
675
676static struct device_attribute *attr_changed_internally(
677 struct Scsi_Host *shost,
678 struct device_attribute * attr)
679{
680 if (!strcmp("queue_depth", attr->attr.name)
681 && shost->hostt->change_queue_depth)
682 return &sdev_attr_queue_depth_rw;
683 else if (!strcmp("queue_type", attr->attr.name)
684 && shost->hostt->change_queue_type)
685 return &sdev_attr_queue_type_rw;
686 return attr;
687}
688
689
690static struct device_attribute *attr_overridden(
691 struct device_attribute **attrs,
692 struct device_attribute *attr)
693{
694 int i;
695
696 if (!attrs)
697 return NULL;
698 for (i = 0; attrs[i]; i++)
699 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
700 return attrs[i];
701 return NULL;
702}
703
704static int attr_add(struct device *dev, struct device_attribute *attr)
705{
706 struct device_attribute *base_attr;
707
708 /*
709 * Spare the caller from having to copy things it's not interested in.
710 */
711 base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
712 if (base_attr) {
713 /* extend permissions */
714 attr->attr.mode |= base_attr->attr.mode;
715
716 /* override null show/store with default */
717 if (!attr->show)
718 attr->show = base_attr->show;
719 if (!attr->store)
720 attr->store = base_attr->store;
721 }
722
723 return device_create_file(dev, attr);
724}
725
726/**
727 * scsi_sysfs_add_sdev - add scsi device to sysfs
728 * @sdev: scsi_device to add
729 *
730 * Return value:
731 * 0 on Success / non-zero on Failure
732 **/
733int scsi_sysfs_add_sdev(struct scsi_device *sdev)
734{
735 int error, i;
736
737 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
738 return error;
739
740 error = device_add(&sdev->sdev_gendev);
741 if (error) {
742 put_device(sdev->sdev_gendev.parent);
743 printk(KERN_INFO "error 1\n");
744 return error;
745 }
746 error = class_device_add(&sdev->sdev_classdev);
747 if (error) {
748 printk(KERN_INFO "error 2\n");
749 goto clean_device;
750 }
751
752 /* take a reference for the sdev_classdev; this is
753 * released by the sdev_class .release */
754 get_device(&sdev->sdev_gendev);
755 if (sdev->host->hostt->sdev_attrs) {
756 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
757 error = attr_add(&sdev->sdev_gendev,
758 sdev->host->hostt->sdev_attrs[i]);
759 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400760 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto out;
762 }
763 }
764 }
765
766 for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
767 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
768 scsi_sysfs_sdev_attrs[i])) {
769 struct device_attribute * attr =
770 attr_changed_internally(sdev->host,
771 scsi_sysfs_sdev_attrs[i]);
772 error = device_create_file(&sdev->sdev_gendev, attr);
773 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400774 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 goto out;
776 }
777 }
778 }
779
780 transport_add_device(&sdev->sdev_gendev);
781 out:
782 return error;
783
784 clean_device:
785 scsi_device_set_state(sdev, SDEV_CANCEL);
786
787 device_del(&sdev->sdev_gendev);
788 transport_destroy_device(&sdev->sdev_gendev);
789 put_device(&sdev->sdev_gendev);
790
791 return error;
792}
793
Alan Stern903f4fe2005-07-26 10:20:53 -0400794void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
James Bottomleydf133c22005-11-06 11:47:08 -0600796 struct device *dev = &sdev->sdev_gendev;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400799 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 class_device_unregister(&sdev->sdev_classdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600802 transport_remove_device(dev);
803 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 scsi_device_set_state(sdev, SDEV_DEL);
805 if (sdev->host->hostt->slave_destroy)
806 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600807 transport_destroy_device(dev);
808 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400809}
810
811/**
812 * scsi_remove_device - unregister a device from the scsi bus
813 * @sdev: scsi_device to unregister
814 **/
815void scsi_remove_device(struct scsi_device *sdev)
816{
Alan Stern54195002005-09-15 21:52:51 -0400817 struct Scsi_Host *shost = sdev->host;
818
Arjan van de Ven0b950672006-01-11 13:16:10 +0100819 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400820 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100821 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823EXPORT_SYMBOL(scsi_remove_device);
824
825void __scsi_remove_target(struct scsi_target *starget)
826{
827 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
828 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400829 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 spin_lock_irqsave(shost->host_lock, flags);
832 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400833 restart:
834 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400836 sdev->id != starget->id ||
837 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 continue;
839 spin_unlock_irqrestore(shost->host_lock, flags);
840 scsi_remove_device(sdev);
841 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400842 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 spin_unlock_irqrestore(shost->host_lock, flags);
845 scsi_target_reap(starget);
846}
847
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800848static int __remove_child (struct device * dev, void * data)
849{
850 if (scsi_is_target_device(dev))
851 __scsi_remove_target(to_scsi_target(dev));
852 return 0;
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/**
856 * scsi_remove_target - try to remove a target and all its devices
857 * @dev: generic starget or parent of generic stargets to be removed
858 *
859 * Note: This is slightly racy. It is possible that if the user
860 * requests the addition of another device then the target won't be
861 * removed.
862 */
863void scsi_remove_target(struct device *dev)
864{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800865 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (scsi_is_target_device(dev)) {
868 __scsi_remove_target(to_scsi_target(dev));
869 return;
870 }
871
872 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800873 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 put_device(rdev);
875}
876EXPORT_SYMBOL(scsi_remove_target);
877
878int scsi_register_driver(struct device_driver *drv)
879{
880 drv->bus = &scsi_bus_type;
881
882 return driver_register(drv);
883}
884EXPORT_SYMBOL(scsi_register_driver);
885
886int scsi_register_interface(struct class_interface *intf)
887{
888 intf->class = &sdev_class;
889
890 return class_interface_register(intf);
891}
892EXPORT_SYMBOL(scsi_register_interface);
893
894
895static struct class_device_attribute *class_attr_overridden(
896 struct class_device_attribute **attrs,
897 struct class_device_attribute *attr)
898{
899 int i;
900
901 if (!attrs)
902 return NULL;
903 for (i = 0; attrs[i]; i++)
904 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
905 return attrs[i];
906 return NULL;
907}
908
909static int class_attr_add(struct class_device *classdev,
910 struct class_device_attribute *attr)
911{
912 struct class_device_attribute *base_attr;
913
914 /*
915 * Spare the caller from having to copy things it's not interested in.
916 */
917 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
918 if (base_attr) {
919 /* extend permissions */
920 attr->attr.mode |= base_attr->attr.mode;
921
922 /* override null show/store with default */
923 if (!attr->show)
924 attr->show = base_attr->show;
925 if (!attr->store)
926 attr->store = base_attr->store;
927 }
928
929 return class_device_create_file(classdev, attr);
930}
931
932/**
933 * scsi_sysfs_add_host - add scsi host to subsystem
934 * @shost: scsi host struct to add to subsystem
935 * @dev: parent struct device pointer
936 **/
937int scsi_sysfs_add_host(struct Scsi_Host *shost)
938{
939 int error, i;
940
941 if (shost->hostt->shost_attrs) {
942 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
943 error = class_attr_add(&shost->shost_classdev,
944 shost->hostt->shost_attrs[i]);
945 if (error)
946 return error;
947 }
948 }
949
950 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
951 if (!class_attr_overridden(shost->hostt->shost_attrs,
952 scsi_sysfs_shost_attrs[i])) {
953 error = class_device_create_file(&shost->shost_classdev,
954 scsi_sysfs_shost_attrs[i]);
955 if (error)
956 return error;
957 }
958 }
959
960 transport_register_device(&shost->shost_gendev);
961 return 0;
962}
963
964void scsi_sysfs_device_initialize(struct scsi_device *sdev)
965{
966 unsigned long flags;
967 struct Scsi_Host *shost = sdev->host;
968 struct scsi_target *starget = sdev->sdev_target;
969
970 device_initialize(&sdev->sdev_gendev);
971 sdev->sdev_gendev.bus = &scsi_bus_type;
972 sdev->sdev_gendev.release = scsi_device_dev_release;
973 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
974 sdev->host->host_no, sdev->channel, sdev->id,
975 sdev->lun);
976
977 class_device_initialize(&sdev->sdev_classdev);
978 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
979 sdev->sdev_classdev.class = &sdev_class;
980 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
981 "%d:%d:%d:%d", sdev->host->host_no,
982 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -0500983 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 transport_setup_device(&sdev->sdev_gendev);
985 spin_lock_irqsave(shost->host_lock, flags);
986 list_add_tail(&sdev->same_target_siblings, &starget->devices);
987 list_add_tail(&sdev->siblings, &shost->__devices);
988 spin_unlock_irqrestore(shost->host_lock, flags);
989}
990
991int scsi_is_sdev_device(const struct device *dev)
992{
993 return dev->release == scsi_device_dev_release;
994}
995EXPORT_SYMBOL(scsi_is_sdev_device);
996
997/* A blank transport template that is used in drivers that don't
998 * yet implement Transport Attributes */
999struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };