[SCSI] transport_srp: add rport roles attribute
[safe/jmp/linux-2.6] / drivers / scsi / scsi_transport_srp.c
1 /*
2  * SCSI RDMA (SRP) transport class
3  *
4  * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation, version 2 of the
9  * License.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * 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., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_srp.h>
33
34 struct srp_host_attrs {
35         atomic_t next_port_id;
36 };
37 #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
38
39 #define SRP_HOST_ATTRS 0
40 #define SRP_RPORT_ATTRS 2
41
42 struct srp_internal {
43         struct scsi_transport_template t;
44         struct srp_function_template *f;
45
46         struct class_device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
47
48         struct class_device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
49         struct class_device_attribute private_rport_attrs[SRP_RPORT_ATTRS];
50         struct transport_container rport_attr_cont;
51 };
52
53 #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
54
55 #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
56 #define transport_class_to_srp_rport(cdev) dev_to_rport((cdev)->dev)
57
58 static int srp_host_setup(struct transport_container *tc, struct device *dev,
59                           struct class_device *cdev)
60 {
61         struct Scsi_Host *shost = dev_to_shost(dev);
62         struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
63
64         atomic_set(&srp_host->next_port_id, 0);
65         return 0;
66 }
67
68 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
69                                NULL, NULL);
70
71 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
72                                NULL, NULL, NULL);
73
74 #define SETUP_TEMPLATE(attrb, field, perm, test, ro_test, ro_perm)      \
75         i->private_##attrb[count] = class_device_attr_##field;          \
76         i->private_##attrb[count].attr.mode = perm;                     \
77         if (ro_test) {                                                  \
78                 i->private_##attrb[count].attr.mode = ro_perm;          \
79                 i->private_##attrb[count].store = NULL;                 \
80         }                                                               \
81         i->attrb[count] = &i->private_##attrb[count];                   \
82         if (test)                                                       \
83                 count++
84
85 #define SETUP_RPORT_ATTRIBUTE_RD(field)                                 \
86         SETUP_TEMPLATE(rport_attrs, field, S_IRUGO, 1, 0, 0)
87
88 #define SETUP_RPORT_ATTRIBUTE_RW(field)                                 \
89         SETUP_TEMPLATE(rport_attrs, field, S_IRUGO | S_IWUSR,           \
90                        1, 1, S_IRUGO)
91
92 #define SRP_PID(p) \
93         (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
94         (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
95         (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
96         (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
97
98 #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
99         "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
100
101 static ssize_t
102 show_srp_rport_id(struct class_device *cdev, char *buf)
103 {
104         struct srp_rport *rport = transport_class_to_srp_rport(cdev);
105         return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
106 }
107
108 static CLASS_DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
109
110 static const struct {
111         u32 value;
112         char *name;
113 } srp_rport_role_names[] = {
114         {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
115         {SRP_RPORT_ROLE_TARGET, "SRP Target"},
116 };
117
118 static ssize_t
119 show_srp_rport_roles(struct class_device *cdev, char *buf)
120 {
121         struct srp_rport *rport = transport_class_to_srp_rport(cdev);
122         int i;
123         char *name = NULL;
124
125         for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
126                 if (srp_rport_role_names[i].value == rport->roles) {
127                         name = srp_rport_role_names[i].name;
128                         break;
129                 }
130         return sprintf(buf, "%s\n", name ? : "unknown");
131 }
132
133 static CLASS_DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
134
135 static void srp_rport_release(struct device *dev)
136 {
137         struct srp_rport *rport = dev_to_rport(dev);
138
139         put_device(dev->parent);
140         kfree(rport);
141 }
142
143 static int scsi_is_srp_rport(const struct device *dev)
144 {
145         return dev->release == srp_rport_release;
146 }
147
148 static int srp_rport_match(struct attribute_container *cont,
149                            struct device *dev)
150 {
151         struct Scsi_Host *shost;
152         struct srp_internal *i;
153
154         if (!scsi_is_srp_rport(dev))
155                 return 0;
156
157         shost = dev_to_shost(dev->parent);
158         if (!shost->transportt)
159                 return 0;
160         if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
161                 return 0;
162
163         i = to_srp_internal(shost->transportt);
164         return &i->rport_attr_cont.ac == cont;
165 }
166
167 static int srp_host_match(struct attribute_container *cont, struct device *dev)
168 {
169         struct Scsi_Host *shost;
170         struct srp_internal *i;
171
172         if (!scsi_is_host_device(dev))
173                 return 0;
174
175         shost = dev_to_shost(dev);
176         if (!shost->transportt)
177                 return 0;
178         if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
179                 return 0;
180
181         i = to_srp_internal(shost->transportt);
182         return &i->t.host_attrs.ac == cont;
183 }
184
185 /**
186  * srp_rport_add - add a SRP remote port to the device hierarchy
187  *
188  * @shost:      scsi host the remote port is connected to.
189  * @ids:        The port id for the remote port.
190  *
191  * publishes a port to the rest of the system
192  */
193 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
194                                 struct srp_rport_identifiers *ids)
195 {
196         struct srp_rport *rport;
197         struct device *parent = &shost->shost_gendev;
198         int id, ret;
199
200         rport = kzalloc(sizeof(*rport), GFP_KERNEL);
201         if (!rport)
202                 return ERR_PTR(-ENOMEM);
203
204         device_initialize(&rport->dev);
205
206         rport->dev.parent = get_device(parent);
207         rport->dev.release = srp_rport_release;
208
209         memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
210         rport->roles = ids->roles;
211
212         id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
213         sprintf(rport->dev.bus_id, "port-%d:%d", shost->host_no, id);
214
215         transport_setup_device(&rport->dev);
216
217         ret = device_add(&rport->dev);
218         if (ret) {
219                 transport_destroy_device(&rport->dev);
220                 put_device(&rport->dev);
221                 return ERR_PTR(ret);
222         }
223
224         transport_add_device(&rport->dev);
225         transport_configure_device(&rport->dev);
226
227         return rport;
228 }
229 EXPORT_SYMBOL_GPL(srp_rport_add);
230
231 /**
232  * srp_rport_del  --  remove a SRP remote port
233  * @port:       SRP remote port to remove
234  *
235  * Removes the specified SRP remote port.
236  */
237 void srp_rport_del(struct srp_rport *rport)
238 {
239         struct device *dev = &rport->dev;
240
241         transport_remove_device(dev);
242         device_del(dev);
243         transport_destroy_device(dev);
244         put_device(dev);
245 }
246 EXPORT_SYMBOL_GPL(srp_rport_del);
247
248 static int do_srp_rport_del(struct device *dev, void *data)
249 {
250         srp_rport_del(dev_to_rport(dev));
251         return 0;
252 }
253
254 /**
255  * srp_remove_host  --  tear down a Scsi_Host's SRP data structures
256  * @shost:      Scsi Host that is torn down
257  *
258  * Removes all SRP remote ports for a given Scsi_Host.
259  * Must be called just before scsi_remove_host for SRP HBAs.
260  */
261 void srp_remove_host(struct Scsi_Host *shost)
262 {
263         device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
264 }
265 EXPORT_SYMBOL_GPL(srp_remove_host);
266
267 /**
268  * srp_attach_transport  --  instantiate SRP transport template
269  * @ft:         SRP transport class function template
270  */
271 struct scsi_transport_template *
272 srp_attach_transport(struct srp_function_template *ft)
273 {
274         int count;
275         struct srp_internal *i;
276
277         i = kzalloc(sizeof(*i), GFP_KERNEL);
278         if (!i)
279                 return NULL;
280
281         i->t.host_size = sizeof(struct srp_host_attrs);
282         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
283         i->t.host_attrs.ac.class = &srp_host_class.class;
284         i->t.host_attrs.ac.match = srp_host_match;
285         i->host_attrs[0] = NULL;
286         transport_container_register(&i->t.host_attrs);
287
288         i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
289         i->rport_attr_cont.ac.class = &srp_rport_class.class;
290         i->rport_attr_cont.ac.match = srp_rport_match;
291         transport_container_register(&i->rport_attr_cont);
292
293         count = 0;
294         SETUP_RPORT_ATTRIBUTE_RD(port_id);
295         SETUP_RPORT_ATTRIBUTE_RD(roles);
296         i->rport_attrs[count] = NULL;
297
298         i->f = ft;
299
300         return &i->t;
301 }
302 EXPORT_SYMBOL_GPL(srp_attach_transport);
303
304 /**
305  * srp_release_transport  --  release SRP transport template instance
306  * @t:          transport template instance
307  */
308 void srp_release_transport(struct scsi_transport_template *t)
309 {
310         struct srp_internal *i = to_srp_internal(t);
311
312         transport_container_unregister(&i->t.host_attrs);
313         transport_container_unregister(&i->rport_attr_cont);
314
315         kfree(i);
316 }
317 EXPORT_SYMBOL_GPL(srp_release_transport);
318
319 static __init int srp_transport_init(void)
320 {
321         int ret;
322
323         ret = transport_class_register(&srp_host_class);
324         if (ret)
325                 return ret;
326         ret = transport_class_register(&srp_rport_class);
327         if (ret)
328                 goto unregister_host_class;
329
330         return 0;
331 unregister_host_class:
332         transport_class_unregister(&srp_host_class);
333         return ret;
334 }
335
336 static void __exit srp_transport_exit(void)
337 {
338         transport_class_unregister(&srp_host_class);
339         transport_class_unregister(&srp_rport_class);
340 }
341
342 MODULE_AUTHOR("FUJITA Tomonori");
343 MODULE_DESCRIPTION("SRP Transport Attributes");
344 MODULE_LICENSE("GPL");
345
346 module_init(srp_transport_init);
347 module_exit(srp_transport_exit);