ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / scsi_transport_fc.c
1 /* 
2  *  FiberChannel transport specific attributes exported to sysfs.
3  *
4  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_transport.h>
25 #include <scsi/scsi_transport_fc.h>
26
27 #define FC_PRINTK(x, l, f, a...)        printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
28
29 static void transport_class_release(struct class_device *class_dev);
30
31 #define FC_NUM_ATTRS    3       /* increase this if you add attributes */
32 #define FC_OTHER_ATTRS  0       /* increase this if you add "always on"
33                                  * attributes */
34
35 struct fc_internal {
36         struct scsi_transport_template t;
37         struct fc_function_template *f;
38         /* The actual attributes */
39         struct class_device_attribute private_attrs[FC_NUM_ATTRS];
40         /* The array of null terminated pointers to attributes
41          * needed by scsi_sysfs.c */
42         struct class_device_attribute *attrs[FC_NUM_ATTRS + FC_OTHER_ATTRS + 1];
43 };
44
45 #define to_fc_internal(tmpl)    container_of(tmpl, struct fc_internal, t)
46
47 struct class fc_transport_class = {
48         .name = "fc_transport",
49         .release = transport_class_release,
50 };
51
52 static __init int fc_transport_init(void)
53 {
54         return class_register(&fc_transport_class);
55 }
56
57 static void __exit fc_transport_exit(void)
58 {
59         class_unregister(&fc_transport_class);
60 }
61
62 static int fc_setup_transport_attrs(struct scsi_device *sdev)
63 {
64         /* I'm not sure what values are invalid.  We should pick some invalid
65          * values for the defaults */
66         fc_node_name(sdev) = -1;
67         fc_port_name(sdev) = -1;
68         fc_port_id(sdev) = -1;
69
70         return 0;
71 }
72
73 static void transport_class_release(struct class_device *class_dev)
74 {
75         struct scsi_device *sdev = transport_class_to_sdev(class_dev);
76         put_device(&sdev->sdev_gendev);
77 }
78
79 #define fc_transport_show_function(field, format_string, cast)          \
80                                                                         \
81 static ssize_t                                                          \
82 show_fc_transport_##field (struct class_device *cdev, char *buf)        \
83 {                                                                       \
84         struct scsi_device *sdev = transport_class_to_sdev(cdev);       \
85         struct fc_transport_attrs *tp;                                  \
86         struct fc_internal *i = to_fc_internal(sdev->host->transportt); \
87         tp = (struct fc_transport_attrs *)&sdev->transport_data;        \
88         if (i->f->get_##field)                                          \
89                 i->f->get_##field(sdev);                                \
90         return snprintf(buf, 20, format_string, cast tp->field);        \
91 }
92
93 #define fc_transport_store_function(field, format_string)               \
94 static ssize_t                                                          \
95 store_fc_transport_##field(struct class_device *cdev, const char *buf,  \
96                            size_t count)                                \
97 {                                                                       \
98         int val;                                                        \
99         struct scsi_device *sdev = transport_class_to_sdev(cdev);       \
100         struct fc_internal *i = to_fc_internal(sdev->host->transportt); \
101                                                                         \
102         val = simple_strtoul(buf, NULL, 0);                             \
103         i->f->set_##field(sdev, val);                                   \
104         return count;                                                   \
105 }
106
107 #define fc_transport_rd_attr(field, format_string)                      \
108         fc_transport_show_function(field, format_string, )              \
109 static CLASS_DEVICE_ATTR(field, S_IRUGO,                                \
110                          show_fc_transport_##field, NULL)
111
112 #define fc_transport_rd_attr_cast(field, format_string, cast)           \
113         fc_transport_show_function(field, format_string, (cast))        \
114 static CLASS_DEVICE_ATTR( field, S_IRUGO,                               \
115                           show_fc_transport_##field, NULL)
116
117 #define fc_transport_rw_attr(field, format_string)                      \
118         fc_transport_show_function(field, format_string, )              \
119         fc_transport_store_function(field, format_string)               \
120 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR,                      \
121                         show_fc_transport_##field,                      \
122                         store_fc_transport_##field)
123
124 /* the FiberChannel Tranport Attributes: */
125 fc_transport_rd_attr_cast(node_name, "0x%llx\n", unsigned long long);
126 fc_transport_rd_attr_cast(port_name, "0x%llx\n", unsigned long long);
127 fc_transport_rd_attr(port_id, "0x%06x\n");
128
129 #define SETUP_ATTRIBUTE_RD(field)                               \
130         i->private_attrs[count] = class_device_attr_##field;    \
131         i->private_attrs[count].attr.mode = S_IRUGO;            \
132         i->private_attrs[count].store = NULL;                   \
133         i->attrs[count] = &i->private_attrs[count];             \
134         if (i->f->show_##field)                                 \
135                 count++
136
137 #define SETUP_ATTRIBUTE_RW(field)                               \
138         i->private_attrs[count] = class_device_attr_##field;    \
139         if (!i->f->set_##field) {                               \
140                 i->private_attrs[count].attr.mode = S_IRUGO;    \
141                 i->private_attrs[count].store = NULL;           \
142         }                                                       \
143         i->attrs[count] = &i->private_attrs[count];             \
144         if (i->f->show_##field)                                 \
145                 count++
146
147 struct scsi_transport_template *
148 fc_attach_transport(struct fc_function_template *ft)
149 {
150         struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
151                                         GFP_KERNEL);
152         int count = 0;
153
154         if (unlikely(!i))
155                 return NULL;
156
157         memset(i, 0, sizeof(struct fc_internal));
158
159         i->t.attrs = &i->attrs[0];
160         i->t.class = &fc_transport_class;
161         i->t.setup = &fc_setup_transport_attrs;
162         i->t.size = sizeof(struct fc_transport_attrs);
163         i->f = ft;
164
165         SETUP_ATTRIBUTE_RD(port_id);
166         SETUP_ATTRIBUTE_RD(port_name);
167         SETUP_ATTRIBUTE_RD(node_name);
168
169         BUG_ON(count > FC_NUM_ATTRS);
170
171         /* Setup the always-on attributes here */
172
173         i->attrs[count] = NULL;
174
175         return &i->t;
176 }
177 EXPORT_SYMBOL(fc_attach_transport);
178
179 void fc_release_transport(struct scsi_transport_template *t)
180 {
181         struct fc_internal *i = to_fc_internal(t);
182
183         kfree(i);
184 }
185 EXPORT_SYMBOL(fc_release_transport);
186
187
188 MODULE_AUTHOR("Martin Hicks");
189 MODULE_DESCRIPTION("FC Transport Attributes");
190 MODULE_LICENSE("GPL");
191
192 module_init(fc_transport_init);
193 module_exit(fc_transport_exit);