]>
Commit | Line | Data |
---|---|---|
09345f65 FT |
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 3 | |
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 void srp_rport_release(struct device *dev) | |
111 | { | |
112 | struct srp_rport *rport = dev_to_rport(dev); | |
113 | ||
114 | put_device(dev->parent); | |
115 | kfree(rport); | |
116 | } | |
117 | ||
118 | static int scsi_is_srp_rport(const struct device *dev) | |
119 | { | |
120 | return dev->release == srp_rport_release; | |
121 | } | |
122 | ||
123 | static int srp_rport_match(struct attribute_container *cont, | |
124 | struct device *dev) | |
125 | { | |
126 | struct Scsi_Host *shost; | |
127 | struct srp_internal *i; | |
128 | ||
129 | if (!scsi_is_srp_rport(dev)) | |
130 | return 0; | |
131 | ||
132 | shost = dev_to_shost(dev->parent); | |
133 | if (!shost->transportt) | |
134 | return 0; | |
135 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) | |
136 | return 0; | |
137 | ||
138 | i = to_srp_internal(shost->transportt); | |
139 | return &i->rport_attr_cont.ac == cont; | |
140 | } | |
141 | ||
142 | static int srp_host_match(struct attribute_container *cont, struct device *dev) | |
143 | { | |
144 | struct Scsi_Host *shost; | |
145 | struct srp_internal *i; | |
146 | ||
147 | if (!scsi_is_host_device(dev)) | |
148 | return 0; | |
149 | ||
150 | shost = dev_to_shost(dev); | |
151 | if (!shost->transportt) | |
152 | return 0; | |
153 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) | |
154 | return 0; | |
155 | ||
156 | i = to_srp_internal(shost->transportt); | |
157 | return &i->t.host_attrs.ac == cont; | |
158 | } | |
159 | ||
160 | /** | |
161 | * srp_rport_add - add a SRP remote port to the device hierarchy | |
162 | * | |
163 | * @shost: scsi host the remote port is connected to. | |
164 | * @ids: The port id for the remote port. | |
165 | * | |
166 | * publishes a port to the rest of the system | |
167 | */ | |
168 | struct srp_rport *srp_rport_add(struct Scsi_Host *shost, | |
169 | struct srp_rport_identifiers *ids) | |
170 | { | |
171 | struct srp_rport *rport; | |
172 | struct device *parent = &shost->shost_gendev; | |
173 | int id, ret; | |
174 | ||
175 | rport = kzalloc(sizeof(*rport), GFP_KERNEL); | |
176 | if (!rport) | |
177 | return ERR_PTR(-ENOMEM); | |
178 | ||
179 | device_initialize(&rport->dev); | |
180 | ||
181 | rport->dev.parent = get_device(parent); | |
182 | rport->dev.release = srp_rport_release; | |
183 | ||
184 | memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); | |
185 | ||
186 | id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); | |
187 | sprintf(rport->dev.bus_id, "port-%d:%d", shost->host_no, id); | |
188 | ||
189 | transport_setup_device(&rport->dev); | |
190 | ||
191 | ret = device_add(&rport->dev); | |
192 | if (ret) { | |
193 | transport_destroy_device(&rport->dev); | |
194 | put_device(&rport->dev); | |
195 | return ERR_PTR(ret); | |
196 | } | |
197 | ||
198 | transport_add_device(&rport->dev); | |
199 | transport_configure_device(&rport->dev); | |
200 | ||
201 | return rport; | |
202 | } | |
203 | EXPORT_SYMBOL_GPL(srp_rport_add); | |
204 | ||
205 | /** | |
206 | * srp_rport_del -- remove a SRP remote port | |
207 | * @port: SRP remote port to remove | |
208 | * | |
209 | * Removes the specified SRP remote port. | |
210 | */ | |
211 | void srp_rport_del(struct srp_rport *rport) | |
212 | { | |
213 | struct device *dev = &rport->dev; | |
214 | ||
215 | transport_remove_device(dev); | |
216 | device_del(dev); | |
217 | transport_destroy_device(dev); | |
218 | put_device(dev); | |
219 | } | |
220 | EXPORT_SYMBOL_GPL(srp_rport_del); | |
221 | ||
222 | static int do_srp_rport_del(struct device *dev, void *data) | |
223 | { | |
224 | srp_rport_del(dev_to_rport(dev)); | |
225 | return 0; | |
226 | } | |
227 | ||
228 | /** | |
229 | * srp_remove_host -- tear down a Scsi_Host's SRP data structures | |
230 | * @shost: Scsi Host that is torn down | |
231 | * | |
232 | * Removes all SRP remote ports for a given Scsi_Host. | |
233 | * Must be called just before scsi_remove_host for SRP HBAs. | |
234 | */ | |
235 | void srp_remove_host(struct Scsi_Host *shost) | |
236 | { | |
237 | device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); | |
238 | } | |
239 | EXPORT_SYMBOL_GPL(srp_remove_host); | |
240 | ||
241 | /** | |
242 | * srp_attach_transport -- instantiate SRP transport template | |
243 | * @ft: SRP transport class function template | |
244 | */ | |
245 | struct scsi_transport_template * | |
246 | srp_attach_transport(struct srp_function_template *ft) | |
247 | { | |
248 | int count; | |
249 | struct srp_internal *i; | |
250 | ||
251 | i = kzalloc(sizeof(*i), GFP_KERNEL); | |
252 | if (!i) | |
253 | return NULL; | |
254 | ||
255 | i->t.host_size = sizeof(struct srp_host_attrs); | |
256 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; | |
257 | i->t.host_attrs.ac.class = &srp_host_class.class; | |
258 | i->t.host_attrs.ac.match = srp_host_match; | |
259 | i->host_attrs[0] = NULL; | |
260 | transport_container_register(&i->t.host_attrs); | |
261 | ||
262 | i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; | |
263 | i->rport_attr_cont.ac.class = &srp_rport_class.class; | |
264 | i->rport_attr_cont.ac.match = srp_rport_match; | |
265 | transport_container_register(&i->rport_attr_cont); | |
266 | ||
267 | count = 0; | |
268 | SETUP_RPORT_ATTRIBUTE_RD(port_id); | |
269 | i->rport_attrs[count] = NULL; | |
270 | ||
271 | i->f = ft; | |
272 | ||
273 | return &i->t; | |
274 | } | |
275 | EXPORT_SYMBOL_GPL(srp_attach_transport); | |
276 | ||
277 | /** | |
278 | * srp_release_transport -- release SRP transport template instance | |
279 | * @t: transport template instance | |
280 | */ | |
281 | void srp_release_transport(struct scsi_transport_template *t) | |
282 | { | |
283 | struct srp_internal *i = to_srp_internal(t); | |
284 | ||
285 | transport_container_unregister(&i->t.host_attrs); | |
286 | transport_container_unregister(&i->rport_attr_cont); | |
287 | ||
288 | kfree(i); | |
289 | } | |
290 | EXPORT_SYMBOL_GPL(srp_release_transport); | |
291 | ||
292 | static __init int srp_transport_init(void) | |
293 | { | |
294 | int ret; | |
295 | ||
296 | ret = transport_class_register(&srp_host_class); | |
297 | if (ret) | |
298 | return ret; | |
299 | ret = transport_class_register(&srp_rport_class); | |
300 | if (ret) | |
301 | goto unregister_host_class; | |
302 | ||
303 | return 0; | |
304 | unregister_host_class: | |
305 | transport_class_unregister(&srp_host_class); | |
306 | return ret; | |
307 | } | |
308 | ||
309 | static void __exit srp_transport_exit(void) | |
310 | { | |
311 | transport_class_unregister(&srp_host_class); | |
312 | transport_class_unregister(&srp_rport_class); | |
313 | } | |
314 | ||
315 | MODULE_AUTHOR("FUJITA Tomonori"); | |
316 | MODULE_DESCRIPTION("SRP Transport Attributes"); | |
317 | MODULE_LICENSE("GPL"); | |
318 | ||
319 | module_init(srp_transport_init); | |
320 | module_exit(srp_transport_exit); |