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> | |
62fe8826 | 33 | #include <scsi/scsi_tgt.h> |
09345f65 FT |
34 | |
35 | struct srp_host_attrs { | |
36 | atomic_t next_port_id; | |
37 | }; | |
38 | #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data) | |
39 | ||
40 | #define SRP_HOST_ATTRS 0 | |
aebd5e47 | 41 | #define SRP_RPORT_ATTRS 2 |
09345f65 FT |
42 | |
43 | struct srp_internal { | |
44 | struct scsi_transport_template t; | |
45 | struct srp_function_template *f; | |
46 | ||
47 | struct class_device_attribute *host_attrs[SRP_HOST_ATTRS + 1]; | |
48 | ||
49 | struct class_device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1]; | |
50 | struct class_device_attribute private_rport_attrs[SRP_RPORT_ATTRS]; | |
51 | struct transport_container rport_attr_cont; | |
52 | }; | |
53 | ||
54 | #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t) | |
55 | ||
56 | #define dev_to_rport(d) container_of(d, struct srp_rport, dev) | |
57 | #define transport_class_to_srp_rport(cdev) dev_to_rport((cdev)->dev) | |
58 | ||
59 | static int srp_host_setup(struct transport_container *tc, struct device *dev, | |
60 | struct class_device *cdev) | |
61 | { | |
62 | struct Scsi_Host *shost = dev_to_shost(dev); | |
63 | struct srp_host_attrs *srp_host = to_srp_host_attrs(shost); | |
64 | ||
65 | atomic_set(&srp_host->next_port_id, 0); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup, | |
70 | NULL, NULL); | |
71 | ||
72 | static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports", | |
73 | NULL, NULL, NULL); | |
74 | ||
75 | #define SETUP_TEMPLATE(attrb, field, perm, test, ro_test, ro_perm) \ | |
76 | i->private_##attrb[count] = class_device_attr_##field; \ | |
77 | i->private_##attrb[count].attr.mode = perm; \ | |
78 | if (ro_test) { \ | |
79 | i->private_##attrb[count].attr.mode = ro_perm; \ | |
80 | i->private_##attrb[count].store = NULL; \ | |
81 | } \ | |
82 | i->attrb[count] = &i->private_##attrb[count]; \ | |
83 | if (test) \ | |
84 | count++ | |
85 | ||
86 | #define SETUP_RPORT_ATTRIBUTE_RD(field) \ | |
87 | SETUP_TEMPLATE(rport_attrs, field, S_IRUGO, 1, 0, 0) | |
88 | ||
89 | #define SETUP_RPORT_ATTRIBUTE_RW(field) \ | |
90 | SETUP_TEMPLATE(rport_attrs, field, S_IRUGO | S_IWUSR, \ | |
91 | 1, 1, S_IRUGO) | |
92 | ||
93 | #define SRP_PID(p) \ | |
94 | (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \ | |
95 | (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \ | |
96 | (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \ | |
97 | (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15] | |
98 | ||
99 | #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \ | |
100 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x" | |
101 | ||
102 | static ssize_t | |
103 | show_srp_rport_id(struct class_device *cdev, char *buf) | |
104 | { | |
105 | struct srp_rport *rport = transport_class_to_srp_rport(cdev); | |
106 | return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport)); | |
107 | } | |
108 | ||
109 | static CLASS_DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL); | |
110 | ||
aebd5e47 FT |
111 | static const struct { |
112 | u32 value; | |
113 | char *name; | |
114 | } srp_rport_role_names[] = { | |
115 | {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"}, | |
116 | {SRP_RPORT_ROLE_TARGET, "SRP Target"}, | |
117 | }; | |
118 | ||
119 | static ssize_t | |
120 | show_srp_rport_roles(struct class_device *cdev, char *buf) | |
121 | { | |
122 | struct srp_rport *rport = transport_class_to_srp_rport(cdev); | |
123 | int i; | |
124 | char *name = NULL; | |
125 | ||
126 | for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++) | |
127 | if (srp_rport_role_names[i].value == rport->roles) { | |
128 | name = srp_rport_role_names[i].name; | |
129 | break; | |
130 | } | |
131 | return sprintf(buf, "%s\n", name ? : "unknown"); | |
132 | } | |
133 | ||
134 | static CLASS_DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL); | |
135 | ||
09345f65 FT |
136 | static void srp_rport_release(struct device *dev) |
137 | { | |
138 | struct srp_rport *rport = dev_to_rport(dev); | |
139 | ||
140 | put_device(dev->parent); | |
141 | kfree(rport); | |
142 | } | |
143 | ||
144 | static int scsi_is_srp_rport(const struct device *dev) | |
145 | { | |
146 | return dev->release == srp_rport_release; | |
147 | } | |
148 | ||
149 | static int srp_rport_match(struct attribute_container *cont, | |
150 | struct device *dev) | |
151 | { | |
152 | struct Scsi_Host *shost; | |
153 | struct srp_internal *i; | |
154 | ||
155 | if (!scsi_is_srp_rport(dev)) | |
156 | return 0; | |
157 | ||
158 | shost = dev_to_shost(dev->parent); | |
159 | if (!shost->transportt) | |
160 | return 0; | |
161 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) | |
162 | return 0; | |
163 | ||
164 | i = to_srp_internal(shost->transportt); | |
165 | return &i->rport_attr_cont.ac == cont; | |
166 | } | |
167 | ||
168 | static int srp_host_match(struct attribute_container *cont, struct device *dev) | |
169 | { | |
170 | struct Scsi_Host *shost; | |
171 | struct srp_internal *i; | |
172 | ||
173 | if (!scsi_is_host_device(dev)) | |
174 | return 0; | |
175 | ||
176 | shost = dev_to_shost(dev); | |
177 | if (!shost->transportt) | |
178 | return 0; | |
179 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) | |
180 | return 0; | |
181 | ||
182 | i = to_srp_internal(shost->transportt); | |
183 | return &i->t.host_attrs.ac == cont; | |
184 | } | |
185 | ||
186 | /** | |
187 | * srp_rport_add - add a SRP remote port to the device hierarchy | |
188 | * | |
189 | * @shost: scsi host the remote port is connected to. | |
190 | * @ids: The port id for the remote port. | |
191 | * | |
192 | * publishes a port to the rest of the system | |
193 | */ | |
194 | struct srp_rport *srp_rport_add(struct Scsi_Host *shost, | |
195 | struct srp_rport_identifiers *ids) | |
196 | { | |
197 | struct srp_rport *rport; | |
198 | struct device *parent = &shost->shost_gendev; | |
199 | int id, ret; | |
200 | ||
201 | rport = kzalloc(sizeof(*rport), GFP_KERNEL); | |
202 | if (!rport) | |
203 | return ERR_PTR(-ENOMEM); | |
204 | ||
205 | device_initialize(&rport->dev); | |
206 | ||
207 | rport->dev.parent = get_device(parent); | |
208 | rport->dev.release = srp_rport_release; | |
209 | ||
210 | memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); | |
aebd5e47 | 211 | rport->roles = ids->roles; |
09345f65 FT |
212 | |
213 | id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); | |
214 | sprintf(rport->dev.bus_id, "port-%d:%d", shost->host_no, id); | |
215 | ||
216 | transport_setup_device(&rport->dev); | |
217 | ||
218 | ret = device_add(&rport->dev); | |
219 | if (ret) { | |
220 | transport_destroy_device(&rport->dev); | |
221 | put_device(&rport->dev); | |
222 | return ERR_PTR(ret); | |
223 | } | |
224 | ||
62fe8826 FT |
225 | if (ids->roles == SRP_RPORT_ROLE_INITIATOR) { |
226 | ret = scsi_tgt_it_nexus_create(shost, (unsigned long)rport, | |
227 | rport->port_id); | |
228 | if (ret) { | |
229 | device_del(&rport->dev); | |
230 | transport_destroy_device(&rport->dev); | |
231 | put_device(&rport->dev); | |
232 | return ERR_PTR(ret); | |
233 | } | |
234 | } | |
235 | ||
09345f65 FT |
236 | transport_add_device(&rport->dev); |
237 | transport_configure_device(&rport->dev); | |
238 | ||
239 | return rport; | |
240 | } | |
241 | EXPORT_SYMBOL_GPL(srp_rport_add); | |
242 | ||
243 | /** | |
244 | * srp_rport_del -- remove a SRP remote port | |
245 | * @port: SRP remote port to remove | |
246 | * | |
247 | * Removes the specified SRP remote port. | |
248 | */ | |
249 | void srp_rport_del(struct srp_rport *rport) | |
250 | { | |
251 | struct device *dev = &rport->dev; | |
252 | ||
62fe8826 FT |
253 | if (rport->roles == SRP_RPORT_ROLE_INITIATOR) |
254 | scsi_tgt_it_nexus_destroy(dev_to_shost(dev->parent), | |
255 | (unsigned long)rport); | |
256 | ||
09345f65 FT |
257 | transport_remove_device(dev); |
258 | device_del(dev); | |
259 | transport_destroy_device(dev); | |
260 | put_device(dev); | |
261 | } | |
262 | EXPORT_SYMBOL_GPL(srp_rport_del); | |
263 | ||
264 | static int do_srp_rport_del(struct device *dev, void *data) | |
265 | { | |
266 | srp_rport_del(dev_to_rport(dev)); | |
267 | return 0; | |
268 | } | |
269 | ||
270 | /** | |
271 | * srp_remove_host -- tear down a Scsi_Host's SRP data structures | |
272 | * @shost: Scsi Host that is torn down | |
273 | * | |
274 | * Removes all SRP remote ports for a given Scsi_Host. | |
275 | * Must be called just before scsi_remove_host for SRP HBAs. | |
276 | */ | |
277 | void srp_remove_host(struct Scsi_Host *shost) | |
278 | { | |
279 | device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); | |
280 | } | |
281 | EXPORT_SYMBOL_GPL(srp_remove_host); | |
282 | ||
bfb74374 FT |
283 | static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id, |
284 | int result) | |
62fe8826 FT |
285 | { |
286 | struct srp_internal *i = to_srp_internal(shost->transportt); | |
bfb74374 FT |
287 | return i->f->tsk_mgmt_response(shost, nexus, tm_id, result); |
288 | } | |
289 | ||
290 | static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result) | |
291 | { | |
292 | struct srp_internal *i = to_srp_internal(shost->transportt); | |
293 | return i->f->it_nexus_response(shost, nexus, result); | |
62fe8826 FT |
294 | } |
295 | ||
09345f65 FT |
296 | /** |
297 | * srp_attach_transport -- instantiate SRP transport template | |
298 | * @ft: SRP transport class function template | |
299 | */ | |
300 | struct scsi_transport_template * | |
301 | srp_attach_transport(struct srp_function_template *ft) | |
302 | { | |
303 | int count; | |
304 | struct srp_internal *i; | |
305 | ||
306 | i = kzalloc(sizeof(*i), GFP_KERNEL); | |
307 | if (!i) | |
308 | return NULL; | |
309 | ||
bfb74374 | 310 | i->t.tsk_mgmt_response = srp_tsk_mgmt_response; |
62fe8826 FT |
311 | i->t.it_nexus_response = srp_it_nexus_response; |
312 | ||
09345f65 FT |
313 | i->t.host_size = sizeof(struct srp_host_attrs); |
314 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; | |
315 | i->t.host_attrs.ac.class = &srp_host_class.class; | |
316 | i->t.host_attrs.ac.match = srp_host_match; | |
317 | i->host_attrs[0] = NULL; | |
318 | transport_container_register(&i->t.host_attrs); | |
319 | ||
320 | i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; | |
321 | i->rport_attr_cont.ac.class = &srp_rport_class.class; | |
322 | i->rport_attr_cont.ac.match = srp_rport_match; | |
323 | transport_container_register(&i->rport_attr_cont); | |
324 | ||
325 | count = 0; | |
326 | SETUP_RPORT_ATTRIBUTE_RD(port_id); | |
aebd5e47 | 327 | SETUP_RPORT_ATTRIBUTE_RD(roles); |
09345f65 FT |
328 | i->rport_attrs[count] = NULL; |
329 | ||
330 | i->f = ft; | |
331 | ||
332 | return &i->t; | |
333 | } | |
334 | EXPORT_SYMBOL_GPL(srp_attach_transport); | |
335 | ||
336 | /** | |
337 | * srp_release_transport -- release SRP transport template instance | |
338 | * @t: transport template instance | |
339 | */ | |
340 | void srp_release_transport(struct scsi_transport_template *t) | |
341 | { | |
342 | struct srp_internal *i = to_srp_internal(t); | |
343 | ||
344 | transport_container_unregister(&i->t.host_attrs); | |
345 | transport_container_unregister(&i->rport_attr_cont); | |
346 | ||
347 | kfree(i); | |
348 | } | |
349 | EXPORT_SYMBOL_GPL(srp_release_transport); | |
350 | ||
351 | static __init int srp_transport_init(void) | |
352 | { | |
353 | int ret; | |
354 | ||
355 | ret = transport_class_register(&srp_host_class); | |
356 | if (ret) | |
357 | return ret; | |
358 | ret = transport_class_register(&srp_rport_class); | |
359 | if (ret) | |
360 | goto unregister_host_class; | |
361 | ||
362 | return 0; | |
363 | unregister_host_class: | |
364 | transport_class_unregister(&srp_host_class); | |
365 | return ret; | |
366 | } | |
367 | ||
368 | static void __exit srp_transport_exit(void) | |
369 | { | |
370 | transport_class_unregister(&srp_host_class); | |
371 | transport_class_unregister(&srp_rport_class); | |
372 | } | |
373 | ||
374 | MODULE_AUTHOR("FUJITA Tomonori"); | |
375 | MODULE_DESCRIPTION("SRP Transport Attributes"); | |
376 | MODULE_LICENSE("GPL"); | |
377 | ||
378 | module_init(srp_transport_init); | |
379 | module_exit(srp_transport_exit); |