VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / scsi / scsi_sysfs.c
1 /*
2  * scsi_sysfs.c
3  *
4  * SCSI sysfs interface routines.
5  *
6  * Created to pull SCSI mid layer sysfs routines into one file.
7  */
8
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/device.h>
14
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_device.h>
17 #include <scsi/scsi_host.h>
18 #include <scsi/scsi_transport.h>
19
20 #include "scsi_priv.h"
21 #include "scsi_logging.h"
22
23 static struct {
24         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 };
34
35 const char *scsi_device_state_name(enum scsi_device_state state)
36 {
37         int i;
38         char *name = NULL;
39
40         for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
41                 if (sdev_states[i].value == state) {
42                         name = sdev_states[i].name;
43                         break;
44                 }
45         }
46         return name;
47 }
48
49 static int check_set(unsigned int *val, char *src)
50 {
51         char *last;
52
53         if (strncmp(src, "-", 20) == 0) {
54                 *val = SCAN_WILD_CARD;
55         } else {
56                 /*
57                  * Doesn't check for int overflow
58                  */
59                 *val = simple_strtoul(src, &last, 0);
60                 if (*last != '\0')
61                         return 1;
62         }
63         return 0;
64 }
65
66 static int scsi_scan(struct Scsi_Host *shost, const char *str)
67 {
68         char s1[15], s2[15], s3[15], junk;
69         unsigned int channel, id, lun;
70         int res;
71
72         res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
73         if (res != 3)
74                 return -EINVAL;
75         if (check_set(&channel, s1))
76                 return -EINVAL;
77         if (check_set(&id, s2))
78                 return -EINVAL;
79         if (check_set(&lun, s3))
80                 return -EINVAL;
81         res = scsi_scan_host_selected(shost, channel, id, lun, 1);
82         return res;
83 }
84
85 /*
86  * shost_show_function: macro to create an attr function that can be used to
87  * show a non-bit field.
88  */
89 #define shost_show_function(name, field, format_string)                 \
90 static ssize_t                                                          \
91 show_##name (struct class_device *class_dev, char *buf)                 \
92 {                                                                       \
93         struct Scsi_Host *shost = class_to_shost(class_dev);            \
94         return snprintf (buf, 20, format_string, shost->field);         \
95 }
96
97 /*
98  * shost_rd_attr: macro to create a function and attribute variable for a
99  * read only field.
100  */
101 #define shost_rd_attr2(name, field, format_string)                      \
102         shost_show_function(name, field, format_string)                 \
103 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
104
105 #define shost_rd_attr(field, format_string) \
106 shost_rd_attr2(field, field, format_string)
107
108 /*
109  * Create the actual show/store functions and data structures.
110  */
111
112 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
113                           size_t count)
114 {
115         struct Scsi_Host *shost = class_to_shost(class_dev);
116         int res;
117
118         res = scsi_scan(shost, buf);
119         if (res == 0)
120                 res = count;
121         return res;
122 };
123 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
124
125 shost_rd_attr(unique_id, "%u\n");
126 shost_rd_attr(host_busy, "%hu\n");
127 shost_rd_attr(cmd_per_lun, "%hd\n");
128 shost_rd_attr(sg_tablesize, "%hu\n");
129 shost_rd_attr(unchecked_isa_dma, "%d\n");
130 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
131
132 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
133         &class_device_attr_unique_id,
134         &class_device_attr_host_busy,
135         &class_device_attr_cmd_per_lun,
136         &class_device_attr_sg_tablesize,
137         &class_device_attr_unchecked_isa_dma,
138         &class_device_attr_proc_name,
139         &class_device_attr_scan,
140         NULL
141 };
142
143 static void scsi_device_cls_release(struct class_device *class_dev)
144 {
145         struct scsi_device *sdev;
146
147         sdev = class_to_sdev(class_dev);
148         put_device(&sdev->sdev_gendev);
149 }
150
151 void scsi_device_dev_release(struct device *dev)
152 {
153         struct scsi_device *sdev;
154         struct device *parent;
155         unsigned long flags;
156
157         parent = dev->parent;
158         sdev = to_scsi_device(dev);
159
160         spin_lock_irqsave(sdev->host->host_lock, flags);
161         list_del(&sdev->siblings);
162         list_del(&sdev->same_target_siblings);
163         list_del(&sdev->starved_entry);
164         if (sdev->single_lun && --sdev->sdev_target->starget_refcnt == 0)
165                 kfree(sdev->sdev_target);
166         spin_unlock_irqrestore(sdev->host->host_lock, flags);
167
168         if (sdev->request_queue)
169                 scsi_free_queue(sdev->request_queue);
170
171         kfree(sdev->inquiry);
172         kfree(sdev);
173
174         put_device(parent);
175 }
176
177 struct class sdev_class = {
178         .name           = "scsi_device",
179         .release        = scsi_device_cls_release,
180 };
181
182 /* all probing is done in the individual ->probe routines */
183 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
184 {
185         struct scsi_device *sdp = to_scsi_device(dev);
186         return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
187 }
188
189 struct bus_type scsi_bus_type = {
190         .name           = "scsi",
191         .match          = scsi_bus_match,
192 };
193
194 int scsi_sysfs_register(void)
195 {
196         int error;
197
198         error = bus_register(&scsi_bus_type);
199         if (!error) {
200                 error = class_register(&sdev_class);
201                 if (error)
202                         bus_unregister(&scsi_bus_type);
203         }
204
205         return error;
206 }
207
208 void scsi_sysfs_unregister(void)
209 {
210         class_unregister(&sdev_class);
211         bus_unregister(&scsi_bus_type);
212 }
213
214 /*
215  * sdev_show_function: macro to create an attr function that can be used to
216  * show a non-bit field.
217  */
218 #define sdev_show_function(field, format_string)                                \
219 static ssize_t                                                          \
220 sdev_show_##field (struct device *dev, char *buf)                               \
221 {                                                                       \
222         struct scsi_device *sdev;                                       \
223         sdev = to_scsi_device(dev);                                     \
224         return snprintf (buf, 20, format_string, sdev->field);          \
225 }                                                                       \
226
227 /*
228  * sdev_rd_attr: macro to create a function and attribute variable for a
229  * read only field.
230  */
231 #define sdev_rd_attr(field, format_string)                              \
232         sdev_show_function(field, format_string)                        \
233 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
234
235
236 /*
237  * sdev_rd_attr: create a function and attribute variable for a
238  * read/write field.
239  */
240 #define sdev_rw_attr(field, format_string)                              \
241         sdev_show_function(field, format_string)                                \
242                                                                         \
243 static ssize_t                                                          \
244 sdev_store_##field (struct device *dev, const char *buf, size_t count)  \
245 {                                                                       \
246         struct scsi_device *sdev;                                       \
247         sdev = to_scsi_device(dev);                                     \
248         snscanf (buf, 20, format_string, &sdev->field);                 \
249         return count;                                                   \
250 }                                                                       \
251 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
252
253 /* Currently we don't export bit fields, but we might in future,
254  * so leave this code in */
255 #if 0
256 /*
257  * sdev_rd_attr: create a function and attribute variable for a
258  * read/write bit field.
259  */
260 #define sdev_rw_attr_bit(field)                                         \
261         sdev_show_function(field, "%d\n")                                       \
262                                                                         \
263 static ssize_t                                                          \
264 sdev_store_##field (struct device *dev, const char *buf, size_t count)  \
265 {                                                                       \
266         int ret;                                                        \
267         struct scsi_device *sdev;                                       \
268         ret = scsi_sdev_check_buf_bit(buf);                             \
269         if (ret >= 0)   {                                               \
270                 sdev = to_scsi_device(dev);                             \
271                 sdev->field = ret;                                      \
272                 ret = count;                                            \
273         }                                                               \
274         return ret;                                                     \
275 }                                                                       \
276 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
277
278 /*
279  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
280  * else return -EINVAL.
281  */
282 static int scsi_sdev_check_buf_bit(const char *buf)
283 {
284         if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
285                 if (buf[0] == '1')
286                         return 1;
287                 else if (buf[0] == '0')
288                         return 0;
289                 else 
290                         return -EINVAL;
291         } else
292                 return -EINVAL;
293 }
294 #endif
295 /*
296  * Create the actual show/store functions and data structures.
297  */
298 sdev_rd_attr (device_blocked, "%d\n");
299 sdev_rd_attr (queue_depth, "%d\n");
300 sdev_rd_attr (type, "%d\n");
301 sdev_rd_attr (scsi_level, "%d\n");
302 sdev_rd_attr (vendor, "%.8s\n");
303 sdev_rd_attr (model, "%.16s\n");
304 sdev_rd_attr (rev, "%.4s\n");
305
306 static ssize_t
307 sdev_show_timeout (struct device *dev, char *buf)
308 {
309         struct scsi_device *sdev;
310         sdev = to_scsi_device(dev);
311         return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
312 }
313
314 static ssize_t
315 sdev_store_timeout (struct device *dev, const char *buf, size_t count)
316 {
317         struct scsi_device *sdev;
318         int timeout;
319         sdev = to_scsi_device(dev);
320         sscanf (buf, "%d\n", &timeout);
321         sdev->timeout = timeout * HZ;
322         return count;
323 }
324 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
325
326 static ssize_t
327 store_rescan_field (struct device *dev, const char *buf, size_t count) 
328 {
329         scsi_rescan_device(dev);
330         return count;
331 }
332 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
333
334 static ssize_t sdev_store_delete(struct device *dev, const char *buf,
335                                  size_t count)
336 {
337         scsi_remove_device(to_scsi_device(dev));
338         return count;
339 };
340 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
341
342 static ssize_t
343 store_state_field(struct device *dev, const char *buf, size_t count)
344 {
345         int i;
346         struct scsi_device *sdev = to_scsi_device(dev);
347         enum scsi_device_state state = 0;
348
349         for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
350                 const int len = strlen(sdev_states[i].name);
351                 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
352                    buf[len] == '\n') {
353                         state = sdev_states[i].value;
354                         break;
355                 }
356         }
357         if (!state)
358                 return -EINVAL;
359
360         if (scsi_device_set_state(sdev, state))
361                 return -EINVAL;
362         return count;
363 }
364
365 static ssize_t
366 show_state_field(struct device *dev, char *buf)
367 {
368         struct scsi_device *sdev = to_scsi_device(dev);
369         const char *name = scsi_device_state_name(sdev->sdev_state);
370
371         if (!name)
372                 return -EINVAL;
373
374         return snprintf(buf, 20, "%s\n", name);
375 }
376
377 DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
378
379
380 /* Default template for device attributes.  May NOT be modified */
381 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
382         &dev_attr_device_blocked,
383         &dev_attr_queue_depth,
384         &dev_attr_type,
385         &dev_attr_scsi_level,
386         &dev_attr_vendor,
387         &dev_attr_model,
388         &dev_attr_rev,
389         &dev_attr_rescan,
390         &dev_attr_delete,
391         &dev_attr_state,
392         &dev_attr_timeout,
393         NULL
394 };
395
396
397 static struct device_attribute *attr_overridden(
398                 struct device_attribute **attrs,
399                 struct device_attribute *attr)
400 {
401         int i;
402
403         if (!attrs)
404                 return NULL;
405         for (i = 0; attrs[i]; i++)
406                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
407                         return attrs[i];
408         return NULL;
409 }
410
411 static int attr_add(struct device *dev, struct device_attribute *attr)
412 {
413         struct device_attribute *base_attr;
414
415         /*
416          * Spare the caller from having to copy things it's not interested in.
417          */
418         base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
419         if (base_attr) {
420                 /* extend permissions */
421                 attr->attr.mode |= base_attr->attr.mode;
422
423                 /* override null show/store with default */
424                 if (!attr->show)
425                         attr->show = base_attr->show;
426                 if (!attr->store)
427                         attr->store = base_attr->store;
428         }
429
430         return device_create_file(dev, attr);
431 }
432
433 /**
434  * scsi_sysfs_add_sdev - add scsi device to sysfs
435  * @sdev:       scsi_device to add
436  *
437  * Return value:
438  *      0 on Success / non-zero on Failure
439  **/
440 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
441 {
442         struct class_device_attribute **attrs;
443         int error, i;
444
445         if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
446                 return error;
447
448         error = device_add(&sdev->sdev_gendev);
449         if (error) {
450                 printk(KERN_INFO "error 1\n");
451                 return error;
452         }
453
454         error = class_device_add(&sdev->sdev_classdev);
455         if (error) {
456                 printk(KERN_INFO "error 2\n");
457                 goto clean_device;
458         }
459         /* take a reference for the sdev_classdev; this is
460          * released by the sdev_class .release */
461         get_device(&sdev->sdev_gendev);
462
463         if (sdev->transport_classdev.class) {
464                 error = class_device_add(&sdev->transport_classdev);
465                 if (error)
466                         goto clean_device2;
467                 /* take a reference for the transport_classdev; this
468                  * is released by the transport_class .release */
469                 get_device(&sdev->sdev_gendev);
470                 
471         }
472
473         if (sdev->host->hostt->sdev_attrs) {
474                 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
475                         error = attr_add(&sdev->sdev_gendev,
476                                         sdev->host->hostt->sdev_attrs[i]);
477                         if (error) {
478                                 scsi_remove_device(sdev);
479                                 goto out;
480                         }
481                 }
482         }
483         
484         for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
485                 if (!attr_overridden(sdev->host->hostt->sdev_attrs,
486                                         scsi_sysfs_sdev_attrs[i])) {
487                         error = device_create_file(&sdev->sdev_gendev,
488                                         scsi_sysfs_sdev_attrs[i]);
489                         if (error) {
490                                 scsi_remove_device(sdev);
491                                 goto out;
492                         }
493                 }
494         }
495
496         if (sdev->transport_classdev.class) {
497                 attrs = sdev->host->transportt->attrs;
498                 for (i = 0; attrs[i]; i++) {
499                         error = class_device_create_file(&sdev->transport_classdev,
500                                                          attrs[i]);
501                         if (error) {
502                                 scsi_remove_device(sdev);
503                                 goto out;
504                         }
505                 }
506         }
507
508  out:
509         return error;
510
511  clean_device2:
512         class_device_del(&sdev->sdev_classdev);
513  clean_device:
514         scsi_device_set_state(sdev, SDEV_CANCEL);
515
516         device_del(&sdev->sdev_gendev);
517         put_device(&sdev->sdev_gendev);
518
519         return error;
520 }
521
522 /**
523  * scsi_remove_device - unregister a device from the scsi bus
524  * @sdev:       scsi_device to unregister
525  **/
526 void scsi_remove_device(struct scsi_device *sdev)
527 {
528         if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
529                 return;
530
531         class_device_unregister(&sdev->sdev_classdev);
532         if (sdev->transport_classdev.class)
533                 class_device_unregister(&sdev->transport_classdev);
534         device_del(&sdev->sdev_gendev);
535         scsi_device_set_state(sdev, SDEV_DEL);
536         if (sdev->host->hostt->slave_destroy)
537                 sdev->host->hostt->slave_destroy(sdev);
538         if (sdev->host->transportt->cleanup)
539                 sdev->host->transportt->cleanup(sdev);
540         put_device(&sdev->sdev_gendev);
541 }
542
543 int scsi_register_driver(struct device_driver *drv)
544 {
545         drv->bus = &scsi_bus_type;
546
547         return driver_register(drv);
548 }
549
550 int scsi_register_interface(struct class_interface *intf)
551 {
552         intf->class = &sdev_class;
553
554         return class_interface_register(intf);
555 }
556
557
558 static struct class_device_attribute *class_attr_overridden(
559                 struct class_device_attribute **attrs,
560                 struct class_device_attribute *attr)
561 {
562         int i;
563
564         if (!attrs)
565                 return NULL;
566         for (i = 0; attrs[i]; i++)
567                 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
568                         return attrs[i];
569         return NULL;
570 }
571
572 static int class_attr_add(struct class_device *classdev,
573                 struct class_device_attribute *attr)
574 {
575         struct class_device_attribute *base_attr;
576
577         /*
578          * Spare the caller from having to copy things it's not interested in.
579          */
580         base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
581         if (base_attr) {
582                 /* extend permissions */
583                 attr->attr.mode |= base_attr->attr.mode;
584
585                 /* override null show/store with default */
586                 if (!attr->show)
587                         attr->show = base_attr->show;
588                 if (!attr->store)
589                         attr->store = base_attr->store;
590         }
591
592         return class_device_create_file(classdev, attr);
593 }
594
595 /**
596  * scsi_sysfs_add_host - add scsi host to subsystem
597  * @shost:     scsi host struct to add to subsystem
598  * @dev:       parent struct device pointer
599  **/
600 int scsi_sysfs_add_host(struct Scsi_Host *shost)
601 {
602         int error, i;
603
604         if (shost->hostt->shost_attrs) {
605                 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
606                         error = class_attr_add(&shost->shost_classdev,
607                                         shost->hostt->shost_attrs[i]);
608                         if (error)
609                                 return error;
610                 }
611         }
612
613         for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
614                 if (!class_attr_overridden(shost->hostt->shost_attrs,
615                                         scsi_sysfs_shost_attrs[i])) {
616                         error = class_device_create_file(&shost->shost_classdev,
617                                         scsi_sysfs_shost_attrs[i]);
618                         if (error)
619                                 return error;
620                 }
621         }
622
623         return 0;
624 }
625
626 /* A blank transport template that is used in drivers that don't
627  * yet implement Transport Attributes */
628 struct scsi_transport_template blank_transport_template = { NULL, };