Commit | Line | Data |
---|---|---|
e51060f0 SH |
1 | /* |
2 | * Copyright (c) 2005 Voltaire Inc. All rights reserved. | |
3 | * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. | |
4 | * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved. | |
5 | * Copyright (c) 2005-2006 Intel Corporation. All rights reserved. | |
6 | * | |
7 | * This Software is licensed under one of the following licenses: | |
8 | * | |
9 | * 1) under the terms of the "Common Public License 1.0" a copy of which is | |
10 | * available from the Open Source Initiative, see | |
11 | * http://www.opensource.org/licenses/cpl.php. | |
12 | * | |
13 | * 2) under the terms of the "The BSD License" a copy of which is | |
14 | * available from the Open Source Initiative, see | |
15 | * http://www.opensource.org/licenses/bsd-license.php. | |
16 | * | |
17 | * 3) under the terms of the "GNU General Public License (GPL) Version 2" a | |
18 | * copy of which is available from the Open Source Initiative, see | |
19 | * http://www.opensource.org/licenses/gpl-license.php. | |
20 | * | |
21 | * Licensee has the right to choose one of the above licenses. | |
22 | * | |
23 | * Redistributions of source code must retain the above copyright | |
24 | * notice and one of the license notices. | |
25 | * | |
26 | * Redistributions in binary form must reproduce both the above copyright | |
27 | * notice, one of the license notices in the documentation | |
28 | * and/or other materials provided with the distribution. | |
29 | * | |
30 | */ | |
31 | ||
32 | #include <linux/completion.h> | |
33 | #include <linux/in.h> | |
34 | #include <linux/in6.h> | |
35 | #include <linux/mutex.h> | |
36 | #include <linux/random.h> | |
37 | #include <linux/idr.h> | |
07ebafba | 38 | #include <linux/inetdevice.h> |
e51060f0 SH |
39 | |
40 | #include <net/tcp.h> | |
41 | ||
42 | #include <rdma/rdma_cm.h> | |
43 | #include <rdma/rdma_cm_ib.h> | |
44 | #include <rdma/ib_cache.h> | |
45 | #include <rdma/ib_cm.h> | |
46 | #include <rdma/ib_sa.h> | |
07ebafba | 47 | #include <rdma/iw_cm.h> |
e51060f0 SH |
48 | |
49 | MODULE_AUTHOR("Sean Hefty"); | |
50 | MODULE_DESCRIPTION("Generic RDMA CM Agent"); | |
51 | MODULE_LICENSE("Dual BSD/GPL"); | |
52 | ||
53 | #define CMA_CM_RESPONSE_TIMEOUT 20 | |
d5bb7599 | 54 | #define CMA_MAX_CM_RETRIES 15 |
e51060f0 SH |
55 | |
56 | static void cma_add_one(struct ib_device *device); | |
57 | static void cma_remove_one(struct ib_device *device); | |
58 | ||
59 | static struct ib_client cma_client = { | |
60 | .name = "cma", | |
61 | .add = cma_add_one, | |
62 | .remove = cma_remove_one | |
63 | }; | |
64 | ||
65 | static LIST_HEAD(dev_list); | |
66 | static LIST_HEAD(listen_any_list); | |
67 | static DEFINE_MUTEX(lock); | |
68 | static struct workqueue_struct *cma_wq; | |
69 | static DEFINE_IDR(sdp_ps); | |
70 | static DEFINE_IDR(tcp_ps); | |
71 | ||
72 | struct cma_device { | |
73 | struct list_head list; | |
74 | struct ib_device *device; | |
75 | __be64 node_guid; | |
76 | struct completion comp; | |
77 | atomic_t refcount; | |
78 | struct list_head id_list; | |
79 | }; | |
80 | ||
81 | enum cma_state { | |
82 | CMA_IDLE, | |
83 | CMA_ADDR_QUERY, | |
84 | CMA_ADDR_RESOLVED, | |
85 | CMA_ROUTE_QUERY, | |
86 | CMA_ROUTE_RESOLVED, | |
87 | CMA_CONNECT, | |
88 | CMA_DISCONNECT, | |
89 | CMA_ADDR_BOUND, | |
90 | CMA_LISTEN, | |
91 | CMA_DEVICE_REMOVAL, | |
92 | CMA_DESTROYING | |
93 | }; | |
94 | ||
95 | struct rdma_bind_list { | |
96 | struct idr *ps; | |
97 | struct hlist_head owners; | |
98 | unsigned short port; | |
99 | }; | |
100 | ||
101 | /* | |
102 | * Device removal can occur at anytime, so we need extra handling to | |
103 | * serialize notifying the user of device removal with other callbacks. | |
104 | * We do this by disabling removal notification while a callback is in process, | |
105 | * and reporting it after the callback completes. | |
106 | */ | |
107 | struct rdma_id_private { | |
108 | struct rdma_cm_id id; | |
109 | ||
110 | struct rdma_bind_list *bind_list; | |
111 | struct hlist_node node; | |
112 | struct list_head list; | |
113 | struct list_head listen_list; | |
114 | struct cma_device *cma_dev; | |
115 | ||
116 | enum cma_state state; | |
117 | spinlock_t lock; | |
118 | struct completion comp; | |
119 | atomic_t refcount; | |
120 | wait_queue_head_t wait_remove; | |
121 | atomic_t dev_remove; | |
122 | ||
123 | int backlog; | |
124 | int timeout_ms; | |
125 | struct ib_sa_query *query; | |
126 | int query_id; | |
127 | union { | |
128 | struct ib_cm_id *ib; | |
07ebafba | 129 | struct iw_cm_id *iw; |
e51060f0 SH |
130 | } cm_id; |
131 | ||
132 | u32 seq_num; | |
133 | u32 qp_num; | |
134 | enum ib_qp_type qp_type; | |
135 | u8 srq; | |
136 | }; | |
137 | ||
138 | struct cma_work { | |
139 | struct work_struct work; | |
140 | struct rdma_id_private *id; | |
141 | enum cma_state old_state; | |
142 | enum cma_state new_state; | |
143 | struct rdma_cm_event event; | |
144 | }; | |
145 | ||
146 | union cma_ip_addr { | |
147 | struct in6_addr ip6; | |
148 | struct { | |
149 | __u32 pad[3]; | |
150 | __u32 addr; | |
151 | } ip4; | |
152 | }; | |
153 | ||
154 | struct cma_hdr { | |
155 | u8 cma_version; | |
156 | u8 ip_version; /* IP version: 7:4 */ | |
157 | __u16 port; | |
158 | union cma_ip_addr src_addr; | |
159 | union cma_ip_addr dst_addr; | |
160 | }; | |
161 | ||
162 | struct sdp_hh { | |
163 | u8 bsdh[16]; | |
164 | u8 sdp_version; /* Major version: 7:4 */ | |
165 | u8 ip_version; /* IP version: 7:4 */ | |
166 | u8 sdp_specific1[10]; | |
167 | __u16 port; | |
168 | __u16 sdp_specific2; | |
169 | union cma_ip_addr src_addr; | |
170 | union cma_ip_addr dst_addr; | |
171 | }; | |
172 | ||
173 | struct sdp_hah { | |
174 | u8 bsdh[16]; | |
175 | u8 sdp_version; | |
176 | }; | |
177 | ||
178 | #define CMA_VERSION 0x00 | |
179 | #define SDP_MAJ_VERSION 0x2 | |
180 | ||
181 | static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp) | |
182 | { | |
183 | unsigned long flags; | |
184 | int ret; | |
185 | ||
186 | spin_lock_irqsave(&id_priv->lock, flags); | |
187 | ret = (id_priv->state == comp); | |
188 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
189 | return ret; | |
190 | } | |
191 | ||
192 | static int cma_comp_exch(struct rdma_id_private *id_priv, | |
193 | enum cma_state comp, enum cma_state exch) | |
194 | { | |
195 | unsigned long flags; | |
196 | int ret; | |
197 | ||
198 | spin_lock_irqsave(&id_priv->lock, flags); | |
199 | if ((ret = (id_priv->state == comp))) | |
200 | id_priv->state = exch; | |
201 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
202 | return ret; | |
203 | } | |
204 | ||
205 | static enum cma_state cma_exch(struct rdma_id_private *id_priv, | |
206 | enum cma_state exch) | |
207 | { | |
208 | unsigned long flags; | |
209 | enum cma_state old; | |
210 | ||
211 | spin_lock_irqsave(&id_priv->lock, flags); | |
212 | old = id_priv->state; | |
213 | id_priv->state = exch; | |
214 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
215 | return old; | |
216 | } | |
217 | ||
218 | static inline u8 cma_get_ip_ver(struct cma_hdr *hdr) | |
219 | { | |
220 | return hdr->ip_version >> 4; | |
221 | } | |
222 | ||
223 | static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver) | |
224 | { | |
225 | hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF); | |
226 | } | |
227 | ||
228 | static inline u8 sdp_get_majv(u8 sdp_version) | |
229 | { | |
230 | return sdp_version >> 4; | |
231 | } | |
232 | ||
233 | static inline u8 sdp_get_ip_ver(struct sdp_hh *hh) | |
234 | { | |
235 | return hh->ip_version >> 4; | |
236 | } | |
237 | ||
238 | static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver) | |
239 | { | |
240 | hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF); | |
241 | } | |
242 | ||
243 | static void cma_attach_to_dev(struct rdma_id_private *id_priv, | |
244 | struct cma_device *cma_dev) | |
245 | { | |
246 | atomic_inc(&cma_dev->refcount); | |
247 | id_priv->cma_dev = cma_dev; | |
248 | id_priv->id.device = cma_dev->device; | |
249 | list_add_tail(&id_priv->list, &cma_dev->id_list); | |
250 | } | |
251 | ||
252 | static inline void cma_deref_dev(struct cma_device *cma_dev) | |
253 | { | |
254 | if (atomic_dec_and_test(&cma_dev->refcount)) | |
255 | complete(&cma_dev->comp); | |
256 | } | |
257 | ||
258 | static void cma_detach_from_dev(struct rdma_id_private *id_priv) | |
259 | { | |
260 | list_del(&id_priv->list); | |
261 | cma_deref_dev(id_priv->cma_dev); | |
262 | id_priv->cma_dev = NULL; | |
263 | } | |
264 | ||
07ebafba | 265 | static int cma_acquire_dev(struct rdma_id_private *id_priv) |
e51060f0 | 266 | { |
07ebafba | 267 | enum rdma_node_type dev_type = id_priv->id.route.addr.dev_addr.dev_type; |
e51060f0 | 268 | struct cma_device *cma_dev; |
f0ee3404 | 269 | union ib_gid gid; |
e51060f0 SH |
270 | int ret = -ENODEV; |
271 | ||
07ebafba TT |
272 | switch (rdma_node_get_transport(dev_type)) { |
273 | case RDMA_TRANSPORT_IB: | |
274 | ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); | |
275 | break; | |
276 | case RDMA_TRANSPORT_IWARP: | |
277 | iw_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); | |
278 | break; | |
279 | default: | |
280 | return -ENODEV; | |
281 | } | |
61a73c70 | 282 | |
e51060f0 | 283 | list_for_each_entry(cma_dev, &dev_list, list) { |
f0ee3404 | 284 | ret = ib_find_cached_gid(cma_dev->device, &gid, |
e51060f0 SH |
285 | &id_priv->id.port_num, NULL); |
286 | if (!ret) { | |
287 | cma_attach_to_dev(id_priv, cma_dev); | |
288 | break; | |
289 | } | |
290 | } | |
e51060f0 SH |
291 | return ret; |
292 | } | |
293 | ||
e51060f0 SH |
294 | static void cma_deref_id(struct rdma_id_private *id_priv) |
295 | { | |
296 | if (atomic_dec_and_test(&id_priv->refcount)) | |
297 | complete(&id_priv->comp); | |
298 | } | |
299 | ||
300 | static void cma_release_remove(struct rdma_id_private *id_priv) | |
301 | { | |
302 | if (atomic_dec_and_test(&id_priv->dev_remove)) | |
303 | wake_up(&id_priv->wait_remove); | |
304 | } | |
305 | ||
306 | struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler, | |
307 | void *context, enum rdma_port_space ps) | |
308 | { | |
309 | struct rdma_id_private *id_priv; | |
310 | ||
311 | id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL); | |
312 | if (!id_priv) | |
313 | return ERR_PTR(-ENOMEM); | |
314 | ||
315 | id_priv->state = CMA_IDLE; | |
316 | id_priv->id.context = context; | |
317 | id_priv->id.event_handler = event_handler; | |
318 | id_priv->id.ps = ps; | |
319 | spin_lock_init(&id_priv->lock); | |
320 | init_completion(&id_priv->comp); | |
321 | atomic_set(&id_priv->refcount, 1); | |
322 | init_waitqueue_head(&id_priv->wait_remove); | |
323 | atomic_set(&id_priv->dev_remove, 0); | |
324 | INIT_LIST_HEAD(&id_priv->listen_list); | |
325 | get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num); | |
326 | ||
327 | return &id_priv->id; | |
328 | } | |
329 | EXPORT_SYMBOL(rdma_create_id); | |
330 | ||
331 | static int cma_init_ib_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) | |
332 | { | |
333 | struct ib_qp_attr qp_attr; | |
334 | struct rdma_dev_addr *dev_addr; | |
335 | int ret; | |
336 | ||
337 | dev_addr = &id_priv->id.route.addr.dev_addr; | |
338 | ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num, | |
339 | ib_addr_get_pkey(dev_addr), | |
340 | &qp_attr.pkey_index); | |
341 | if (ret) | |
342 | return ret; | |
343 | ||
344 | qp_attr.qp_state = IB_QPS_INIT; | |
345 | qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE; | |
346 | qp_attr.port_num = id_priv->id.port_num; | |
347 | return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS | | |
348 | IB_QP_PKEY_INDEX | IB_QP_PORT); | |
349 | } | |
350 | ||
07ebafba TT |
351 | static int cma_init_iw_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
352 | { | |
353 | struct ib_qp_attr qp_attr; | |
354 | ||
355 | qp_attr.qp_state = IB_QPS_INIT; | |
356 | qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE; | |
357 | ||
358 | return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS); | |
359 | } | |
360 | ||
e51060f0 SH |
361 | int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, |
362 | struct ib_qp_init_attr *qp_init_attr) | |
363 | { | |
364 | struct rdma_id_private *id_priv; | |
365 | struct ib_qp *qp; | |
366 | int ret; | |
367 | ||
368 | id_priv = container_of(id, struct rdma_id_private, id); | |
369 | if (id->device != pd->device) | |
370 | return -EINVAL; | |
371 | ||
372 | qp = ib_create_qp(pd, qp_init_attr); | |
373 | if (IS_ERR(qp)) | |
374 | return PTR_ERR(qp); | |
375 | ||
07ebafba TT |
376 | switch (rdma_node_get_transport(id->device->node_type)) { |
377 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
378 | ret = cma_init_ib_qp(id_priv, qp); |
379 | break; | |
07ebafba TT |
380 | case RDMA_TRANSPORT_IWARP: |
381 | ret = cma_init_iw_qp(id_priv, qp); | |
382 | break; | |
e51060f0 SH |
383 | default: |
384 | ret = -ENOSYS; | |
385 | break; | |
386 | } | |
387 | ||
388 | if (ret) | |
389 | goto err; | |
390 | ||
391 | id->qp = qp; | |
392 | id_priv->qp_num = qp->qp_num; | |
393 | id_priv->qp_type = qp->qp_type; | |
394 | id_priv->srq = (qp->srq != NULL); | |
395 | return 0; | |
396 | err: | |
397 | ib_destroy_qp(qp); | |
398 | return ret; | |
399 | } | |
400 | EXPORT_SYMBOL(rdma_create_qp); | |
401 | ||
402 | void rdma_destroy_qp(struct rdma_cm_id *id) | |
403 | { | |
404 | ib_destroy_qp(id->qp); | |
405 | } | |
406 | EXPORT_SYMBOL(rdma_destroy_qp); | |
407 | ||
408 | static int cma_modify_qp_rtr(struct rdma_cm_id *id) | |
409 | { | |
410 | struct ib_qp_attr qp_attr; | |
411 | int qp_attr_mask, ret; | |
412 | ||
413 | if (!id->qp) | |
414 | return 0; | |
415 | ||
416 | /* Need to update QP attributes from default values. */ | |
417 | qp_attr.qp_state = IB_QPS_INIT; | |
418 | ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask); | |
419 | if (ret) | |
420 | return ret; | |
421 | ||
422 | ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask); | |
423 | if (ret) | |
424 | return ret; | |
425 | ||
426 | qp_attr.qp_state = IB_QPS_RTR; | |
427 | ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask); | |
428 | if (ret) | |
429 | return ret; | |
430 | ||
431 | return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask); | |
432 | } | |
433 | ||
434 | static int cma_modify_qp_rts(struct rdma_cm_id *id) | |
435 | { | |
436 | struct ib_qp_attr qp_attr; | |
437 | int qp_attr_mask, ret; | |
438 | ||
439 | if (!id->qp) | |
440 | return 0; | |
441 | ||
442 | qp_attr.qp_state = IB_QPS_RTS; | |
443 | ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask); | |
444 | if (ret) | |
445 | return ret; | |
446 | ||
447 | return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask); | |
448 | } | |
449 | ||
450 | static int cma_modify_qp_err(struct rdma_cm_id *id) | |
451 | { | |
452 | struct ib_qp_attr qp_attr; | |
453 | ||
454 | if (!id->qp) | |
455 | return 0; | |
456 | ||
457 | qp_attr.qp_state = IB_QPS_ERR; | |
458 | return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE); | |
459 | } | |
460 | ||
461 | int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, | |
462 | int *qp_attr_mask) | |
463 | { | |
464 | struct rdma_id_private *id_priv; | |
465 | int ret; | |
466 | ||
467 | id_priv = container_of(id, struct rdma_id_private, id); | |
07ebafba TT |
468 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
469 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
470 | ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr, |
471 | qp_attr_mask); | |
472 | if (qp_attr->qp_state == IB_QPS_RTR) | |
473 | qp_attr->rq_psn = id_priv->seq_num; | |
474 | break; | |
07ebafba TT |
475 | case RDMA_TRANSPORT_IWARP: |
476 | ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr, | |
477 | qp_attr_mask); | |
478 | break; | |
e51060f0 SH |
479 | default: |
480 | ret = -ENOSYS; | |
481 | break; | |
482 | } | |
483 | ||
484 | return ret; | |
485 | } | |
486 | EXPORT_SYMBOL(rdma_init_qp_attr); | |
487 | ||
488 | static inline int cma_zero_addr(struct sockaddr *addr) | |
489 | { | |
490 | struct in6_addr *ip6; | |
491 | ||
492 | if (addr->sa_family == AF_INET) | |
493 | return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr); | |
494 | else { | |
495 | ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr; | |
496 | return (ip6->s6_addr32[0] | ip6->s6_addr32[1] | | |
5fd571cb | 497 | ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0; |
e51060f0 SH |
498 | } |
499 | } | |
500 | ||
501 | static inline int cma_loopback_addr(struct sockaddr *addr) | |
502 | { | |
503 | return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr); | |
504 | } | |
505 | ||
506 | static inline int cma_any_addr(struct sockaddr *addr) | |
507 | { | |
508 | return cma_zero_addr(addr) || cma_loopback_addr(addr); | |
509 | } | |
510 | ||
511 | static inline int cma_any_port(struct sockaddr *addr) | |
512 | { | |
513 | return !((struct sockaddr_in *) addr)->sin_port; | |
514 | } | |
515 | ||
516 | static int cma_get_net_info(void *hdr, enum rdma_port_space ps, | |
517 | u8 *ip_ver, __u16 *port, | |
518 | union cma_ip_addr **src, union cma_ip_addr **dst) | |
519 | { | |
520 | switch (ps) { | |
521 | case RDMA_PS_SDP: | |
522 | if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) != | |
523 | SDP_MAJ_VERSION) | |
524 | return -EINVAL; | |
525 | ||
526 | *ip_ver = sdp_get_ip_ver(hdr); | |
527 | *port = ((struct sdp_hh *) hdr)->port; | |
528 | *src = &((struct sdp_hh *) hdr)->src_addr; | |
529 | *dst = &((struct sdp_hh *) hdr)->dst_addr; | |
530 | break; | |
531 | default: | |
532 | if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION) | |
533 | return -EINVAL; | |
534 | ||
535 | *ip_ver = cma_get_ip_ver(hdr); | |
536 | *port = ((struct cma_hdr *) hdr)->port; | |
537 | *src = &((struct cma_hdr *) hdr)->src_addr; | |
538 | *dst = &((struct cma_hdr *) hdr)->dst_addr; | |
539 | break; | |
540 | } | |
541 | ||
542 | if (*ip_ver != 4 && *ip_ver != 6) | |
543 | return -EINVAL; | |
544 | return 0; | |
545 | } | |
546 | ||
547 | static void cma_save_net_info(struct rdma_addr *addr, | |
548 | struct rdma_addr *listen_addr, | |
549 | u8 ip_ver, __u16 port, | |
550 | union cma_ip_addr *src, union cma_ip_addr *dst) | |
551 | { | |
552 | struct sockaddr_in *listen4, *ip4; | |
553 | struct sockaddr_in6 *listen6, *ip6; | |
554 | ||
555 | switch (ip_ver) { | |
556 | case 4: | |
557 | listen4 = (struct sockaddr_in *) &listen_addr->src_addr; | |
558 | ip4 = (struct sockaddr_in *) &addr->src_addr; | |
559 | ip4->sin_family = listen4->sin_family; | |
560 | ip4->sin_addr.s_addr = dst->ip4.addr; | |
561 | ip4->sin_port = listen4->sin_port; | |
562 | ||
563 | ip4 = (struct sockaddr_in *) &addr->dst_addr; | |
564 | ip4->sin_family = listen4->sin_family; | |
565 | ip4->sin_addr.s_addr = src->ip4.addr; | |
566 | ip4->sin_port = port; | |
567 | break; | |
568 | case 6: | |
569 | listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr; | |
570 | ip6 = (struct sockaddr_in6 *) &addr->src_addr; | |
571 | ip6->sin6_family = listen6->sin6_family; | |
572 | ip6->sin6_addr = dst->ip6; | |
573 | ip6->sin6_port = listen6->sin6_port; | |
574 | ||
575 | ip6 = (struct sockaddr_in6 *) &addr->dst_addr; | |
576 | ip6->sin6_family = listen6->sin6_family; | |
577 | ip6->sin6_addr = src->ip6; | |
578 | ip6->sin6_port = port; | |
579 | break; | |
580 | default: | |
581 | break; | |
582 | } | |
583 | } | |
584 | ||
585 | static inline int cma_user_data_offset(enum rdma_port_space ps) | |
586 | { | |
587 | switch (ps) { | |
588 | case RDMA_PS_SDP: | |
589 | return 0; | |
590 | default: | |
591 | return sizeof(struct cma_hdr); | |
592 | } | |
593 | } | |
594 | ||
595 | static int cma_notify_user(struct rdma_id_private *id_priv, | |
596 | enum rdma_cm_event_type type, int status, | |
597 | void *data, u8 data_len) | |
598 | { | |
599 | struct rdma_cm_event event; | |
600 | ||
601 | event.event = type; | |
602 | event.status = status; | |
603 | event.private_data = data; | |
604 | event.private_data_len = data_len; | |
605 | ||
606 | return id_priv->id.event_handler(&id_priv->id, &event); | |
607 | } | |
608 | ||
609 | static void cma_cancel_route(struct rdma_id_private *id_priv) | |
610 | { | |
07ebafba TT |
611 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
612 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
613 | if (id_priv->query) |
614 | ib_sa_cancel_query(id_priv->query_id, id_priv->query); | |
615 | break; | |
616 | default: | |
617 | break; | |
618 | } | |
619 | } | |
620 | ||
621 | static inline int cma_internal_listen(struct rdma_id_private *id_priv) | |
622 | { | |
623 | return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev && | |
624 | cma_any_addr(&id_priv->id.route.addr.src_addr); | |
625 | } | |
626 | ||
627 | static void cma_destroy_listen(struct rdma_id_private *id_priv) | |
628 | { | |
629 | cma_exch(id_priv, CMA_DESTROYING); | |
630 | ||
631 | if (id_priv->cma_dev) { | |
07ebafba TT |
632 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
633 | case RDMA_TRANSPORT_IB: | |
3cd96564 | 634 | if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib)) |
e51060f0 SH |
635 | ib_destroy_cm_id(id_priv->cm_id.ib); |
636 | break; | |
07ebafba TT |
637 | case RDMA_TRANSPORT_IWARP: |
638 | if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw)) | |
639 | iw_destroy_cm_id(id_priv->cm_id.iw); | |
640 | break; | |
e51060f0 SH |
641 | default: |
642 | break; | |
643 | } | |
644 | cma_detach_from_dev(id_priv); | |
645 | } | |
646 | list_del(&id_priv->listen_list); | |
647 | ||
648 | cma_deref_id(id_priv); | |
649 | wait_for_completion(&id_priv->comp); | |
650 | ||
651 | kfree(id_priv); | |
652 | } | |
653 | ||
654 | static void cma_cancel_listens(struct rdma_id_private *id_priv) | |
655 | { | |
656 | struct rdma_id_private *dev_id_priv; | |
657 | ||
658 | mutex_lock(&lock); | |
659 | list_del(&id_priv->list); | |
660 | ||
661 | while (!list_empty(&id_priv->listen_list)) { | |
662 | dev_id_priv = list_entry(id_priv->listen_list.next, | |
663 | struct rdma_id_private, listen_list); | |
664 | cma_destroy_listen(dev_id_priv); | |
665 | } | |
666 | mutex_unlock(&lock); | |
667 | } | |
668 | ||
669 | static void cma_cancel_operation(struct rdma_id_private *id_priv, | |
670 | enum cma_state state) | |
671 | { | |
672 | switch (state) { | |
673 | case CMA_ADDR_QUERY: | |
674 | rdma_addr_cancel(&id_priv->id.route.addr.dev_addr); | |
675 | break; | |
676 | case CMA_ROUTE_QUERY: | |
677 | cma_cancel_route(id_priv); | |
678 | break; | |
679 | case CMA_LISTEN: | |
680 | if (cma_any_addr(&id_priv->id.route.addr.src_addr) && | |
681 | !id_priv->cma_dev) | |
682 | cma_cancel_listens(id_priv); | |
683 | break; | |
684 | default: | |
685 | break; | |
686 | } | |
687 | } | |
688 | ||
689 | static void cma_release_port(struct rdma_id_private *id_priv) | |
690 | { | |
691 | struct rdma_bind_list *bind_list = id_priv->bind_list; | |
692 | ||
693 | if (!bind_list) | |
694 | return; | |
695 | ||
696 | mutex_lock(&lock); | |
697 | hlist_del(&id_priv->node); | |
698 | if (hlist_empty(&bind_list->owners)) { | |
699 | idr_remove(bind_list->ps, bind_list->port); | |
700 | kfree(bind_list); | |
701 | } | |
702 | mutex_unlock(&lock); | |
703 | } | |
704 | ||
705 | void rdma_destroy_id(struct rdma_cm_id *id) | |
706 | { | |
707 | struct rdma_id_private *id_priv; | |
708 | enum cma_state state; | |
709 | ||
710 | id_priv = container_of(id, struct rdma_id_private, id); | |
711 | state = cma_exch(id_priv, CMA_DESTROYING); | |
712 | cma_cancel_operation(id_priv, state); | |
713 | ||
61a73c70 | 714 | mutex_lock(&lock); |
e51060f0 | 715 | if (id_priv->cma_dev) { |
61a73c70 | 716 | mutex_unlock(&lock); |
07ebafba TT |
717 | switch (rdma_node_get_transport(id->device->node_type)) { |
718 | case RDMA_TRANSPORT_IB: | |
3cd96564 | 719 | if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib)) |
e51060f0 SH |
720 | ib_destroy_cm_id(id_priv->cm_id.ib); |
721 | break; | |
07ebafba TT |
722 | case RDMA_TRANSPORT_IWARP: |
723 | if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw)) | |
724 | iw_destroy_cm_id(id_priv->cm_id.iw); | |
725 | break; | |
e51060f0 SH |
726 | default: |
727 | break; | |
728 | } | |
3cd96564 | 729 | mutex_lock(&lock); |
e51060f0 | 730 | cma_detach_from_dev(id_priv); |
e51060f0 | 731 | } |
61a73c70 | 732 | mutex_unlock(&lock); |
e51060f0 SH |
733 | |
734 | cma_release_port(id_priv); | |
735 | cma_deref_id(id_priv); | |
736 | wait_for_completion(&id_priv->comp); | |
737 | ||
738 | kfree(id_priv->id.route.path_rec); | |
739 | kfree(id_priv); | |
740 | } | |
741 | EXPORT_SYMBOL(rdma_destroy_id); | |
742 | ||
743 | static int cma_rep_recv(struct rdma_id_private *id_priv) | |
744 | { | |
745 | int ret; | |
746 | ||
747 | ret = cma_modify_qp_rtr(&id_priv->id); | |
748 | if (ret) | |
749 | goto reject; | |
750 | ||
751 | ret = cma_modify_qp_rts(&id_priv->id); | |
752 | if (ret) | |
753 | goto reject; | |
754 | ||
755 | ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0); | |
756 | if (ret) | |
757 | goto reject; | |
758 | ||
759 | return 0; | |
760 | reject: | |
761 | cma_modify_qp_err(&id_priv->id); | |
762 | ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, | |
763 | NULL, 0, NULL, 0); | |
764 | return ret; | |
765 | } | |
766 | ||
767 | static int cma_verify_rep(struct rdma_id_private *id_priv, void *data) | |
768 | { | |
769 | if (id_priv->id.ps == RDMA_PS_SDP && | |
770 | sdp_get_majv(((struct sdp_hah *) data)->sdp_version) != | |
771 | SDP_MAJ_VERSION) | |
772 | return -EINVAL; | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
777 | static int cma_rtu_recv(struct rdma_id_private *id_priv) | |
778 | { | |
779 | int ret; | |
780 | ||
781 | ret = cma_modify_qp_rts(&id_priv->id); | |
782 | if (ret) | |
783 | goto reject; | |
784 | ||
785 | return 0; | |
786 | reject: | |
787 | cma_modify_qp_err(&id_priv->id); | |
788 | ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, | |
789 | NULL, 0, NULL, 0); | |
790 | return ret; | |
791 | } | |
792 | ||
793 | static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) | |
794 | { | |
795 | struct rdma_id_private *id_priv = cm_id->context; | |
796 | enum rdma_cm_event_type event; | |
797 | u8 private_data_len = 0; | |
798 | int ret = 0, status = 0; | |
799 | ||
800 | atomic_inc(&id_priv->dev_remove); | |
801 | if (!cma_comp(id_priv, CMA_CONNECT)) | |
802 | goto out; | |
803 | ||
804 | switch (ib_event->event) { | |
805 | case IB_CM_REQ_ERROR: | |
806 | case IB_CM_REP_ERROR: | |
807 | event = RDMA_CM_EVENT_UNREACHABLE; | |
808 | status = -ETIMEDOUT; | |
809 | break; | |
810 | case IB_CM_REP_RECEIVED: | |
811 | status = cma_verify_rep(id_priv, ib_event->private_data); | |
812 | if (status) | |
813 | event = RDMA_CM_EVENT_CONNECT_ERROR; | |
814 | else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) { | |
815 | status = cma_rep_recv(id_priv); | |
816 | event = status ? RDMA_CM_EVENT_CONNECT_ERROR : | |
817 | RDMA_CM_EVENT_ESTABLISHED; | |
818 | } else | |
819 | event = RDMA_CM_EVENT_CONNECT_RESPONSE; | |
820 | private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE; | |
821 | break; | |
822 | case IB_CM_RTU_RECEIVED: | |
823 | status = cma_rtu_recv(id_priv); | |
824 | event = status ? RDMA_CM_EVENT_CONNECT_ERROR : | |
825 | RDMA_CM_EVENT_ESTABLISHED; | |
826 | break; | |
827 | case IB_CM_DREQ_ERROR: | |
828 | status = -ETIMEDOUT; /* fall through */ | |
829 | case IB_CM_DREQ_RECEIVED: | |
830 | case IB_CM_DREP_RECEIVED: | |
831 | if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT)) | |
832 | goto out; | |
833 | event = RDMA_CM_EVENT_DISCONNECTED; | |
834 | break; | |
835 | case IB_CM_TIMEWAIT_EXIT: | |
836 | case IB_CM_MRA_RECEIVED: | |
837 | /* ignore event */ | |
838 | goto out; | |
839 | case IB_CM_REJ_RECEIVED: | |
840 | cma_modify_qp_err(&id_priv->id); | |
841 | status = ib_event->param.rej_rcvd.reason; | |
842 | event = RDMA_CM_EVENT_REJECTED; | |
74f76fba | 843 | private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; |
e51060f0 SH |
844 | break; |
845 | default: | |
846 | printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d", | |
847 | ib_event->event); | |
848 | goto out; | |
849 | } | |
850 | ||
851 | ret = cma_notify_user(id_priv, event, status, ib_event->private_data, | |
852 | private_data_len); | |
853 | if (ret) { | |
854 | /* Destroy the CM ID by returning a non-zero value. */ | |
855 | id_priv->cm_id.ib = NULL; | |
856 | cma_exch(id_priv, CMA_DESTROYING); | |
857 | cma_release_remove(id_priv); | |
858 | rdma_destroy_id(&id_priv->id); | |
859 | return ret; | |
860 | } | |
861 | out: | |
862 | cma_release_remove(id_priv); | |
863 | return ret; | |
864 | } | |
865 | ||
866 | static struct rdma_id_private *cma_new_id(struct rdma_cm_id *listen_id, | |
867 | struct ib_cm_event *ib_event) | |
868 | { | |
869 | struct rdma_id_private *id_priv; | |
870 | struct rdma_cm_id *id; | |
871 | struct rdma_route *rt; | |
872 | union cma_ip_addr *src, *dst; | |
873 | __u16 port; | |
874 | u8 ip_ver; | |
875 | ||
876 | id = rdma_create_id(listen_id->event_handler, listen_id->context, | |
877 | listen_id->ps); | |
878 | if (IS_ERR(id)) | |
879 | return NULL; | |
880 | ||
881 | rt = &id->route; | |
882 | rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1; | |
883 | rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths, GFP_KERNEL); | |
884 | if (!rt->path_rec) | |
885 | goto err; | |
886 | ||
887 | if (cma_get_net_info(ib_event->private_data, listen_id->ps, | |
888 | &ip_ver, &port, &src, &dst)) | |
889 | goto err; | |
890 | ||
891 | cma_save_net_info(&id->route.addr, &listen_id->route.addr, | |
892 | ip_ver, port, src, dst); | |
893 | rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path; | |
894 | if (rt->num_paths == 2) | |
895 | rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path; | |
896 | ||
897 | ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid); | |
898 | ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid); | |
899 | ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey)); | |
07ebafba | 900 | rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA; |
e51060f0 SH |
901 | |
902 | id_priv = container_of(id, struct rdma_id_private, id); | |
903 | id_priv->state = CMA_CONNECT; | |
904 | return id_priv; | |
905 | err: | |
906 | rdma_destroy_id(id); | |
907 | return NULL; | |
908 | } | |
909 | ||
910 | static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) | |
911 | { | |
912 | struct rdma_id_private *listen_id, *conn_id; | |
913 | int offset, ret; | |
914 | ||
915 | listen_id = cm_id->context; | |
916 | atomic_inc(&listen_id->dev_remove); | |
917 | if (!cma_comp(listen_id, CMA_LISTEN)) { | |
918 | ret = -ECONNABORTED; | |
919 | goto out; | |
920 | } | |
921 | ||
922 | conn_id = cma_new_id(&listen_id->id, ib_event); | |
923 | if (!conn_id) { | |
924 | ret = -ENOMEM; | |
925 | goto out; | |
926 | } | |
927 | ||
928 | atomic_inc(&conn_id->dev_remove); | |
61a73c70 | 929 | mutex_lock(&lock); |
07ebafba | 930 | ret = cma_acquire_dev(conn_id); |
61a73c70 | 931 | mutex_unlock(&lock); |
e51060f0 SH |
932 | if (ret) { |
933 | ret = -ENODEV; | |
934 | cma_release_remove(conn_id); | |
935 | rdma_destroy_id(&conn_id->id); | |
936 | goto out; | |
937 | } | |
938 | ||
939 | conn_id->cm_id.ib = cm_id; | |
940 | cm_id->context = conn_id; | |
941 | cm_id->cm_handler = cma_ib_handler; | |
942 | ||
943 | offset = cma_user_data_offset(listen_id->id.ps); | |
944 | ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0, | |
945 | ib_event->private_data + offset, | |
946 | IB_CM_REQ_PRIVATE_DATA_SIZE - offset); | |
947 | if (ret) { | |
948 | /* Destroy the CM ID by returning a non-zero value. */ | |
949 | conn_id->cm_id.ib = NULL; | |
950 | cma_exch(conn_id, CMA_DESTROYING); | |
951 | cma_release_remove(conn_id); | |
952 | rdma_destroy_id(&conn_id->id); | |
953 | } | |
954 | out: | |
955 | cma_release_remove(listen_id); | |
956 | return ret; | |
957 | } | |
958 | ||
959 | static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr) | |
960 | { | |
961 | return cpu_to_be64(((u64)ps << 16) + | |
962 | be16_to_cpu(((struct sockaddr_in *) addr)->sin_port)); | |
963 | } | |
964 | ||
965 | static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr, | |
966 | struct ib_cm_compare_data *compare) | |
967 | { | |
968 | struct cma_hdr *cma_data, *cma_mask; | |
969 | struct sdp_hh *sdp_data, *sdp_mask; | |
970 | __u32 ip4_addr; | |
971 | struct in6_addr ip6_addr; | |
972 | ||
973 | memset(compare, 0, sizeof *compare); | |
974 | cma_data = (void *) compare->data; | |
975 | cma_mask = (void *) compare->mask; | |
976 | sdp_data = (void *) compare->data; | |
977 | sdp_mask = (void *) compare->mask; | |
978 | ||
979 | switch (addr->sa_family) { | |
980 | case AF_INET: | |
981 | ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr; | |
982 | if (ps == RDMA_PS_SDP) { | |
983 | sdp_set_ip_ver(sdp_data, 4); | |
984 | sdp_set_ip_ver(sdp_mask, 0xF); | |
985 | sdp_data->dst_addr.ip4.addr = ip4_addr; | |
986 | sdp_mask->dst_addr.ip4.addr = ~0; | |
987 | } else { | |
988 | cma_set_ip_ver(cma_data, 4); | |
989 | cma_set_ip_ver(cma_mask, 0xF); | |
990 | cma_data->dst_addr.ip4.addr = ip4_addr; | |
991 | cma_mask->dst_addr.ip4.addr = ~0; | |
992 | } | |
993 | break; | |
994 | case AF_INET6: | |
995 | ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr; | |
996 | if (ps == RDMA_PS_SDP) { | |
997 | sdp_set_ip_ver(sdp_data, 6); | |
998 | sdp_set_ip_ver(sdp_mask, 0xF); | |
999 | sdp_data->dst_addr.ip6 = ip6_addr; | |
1000 | memset(&sdp_mask->dst_addr.ip6, 0xFF, | |
1001 | sizeof sdp_mask->dst_addr.ip6); | |
1002 | } else { | |
1003 | cma_set_ip_ver(cma_data, 6); | |
1004 | cma_set_ip_ver(cma_mask, 0xF); | |
1005 | cma_data->dst_addr.ip6 = ip6_addr; | |
1006 | memset(&cma_mask->dst_addr.ip6, 0xFF, | |
1007 | sizeof cma_mask->dst_addr.ip6); | |
1008 | } | |
1009 | break; | |
1010 | default: | |
1011 | break; | |
1012 | } | |
1013 | } | |
1014 | ||
07ebafba TT |
1015 | static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) |
1016 | { | |
1017 | struct rdma_id_private *id_priv = iw_id->context; | |
1018 | enum rdma_cm_event_type event = 0; | |
1019 | struct sockaddr_in *sin; | |
1020 | int ret = 0; | |
1021 | ||
1022 | atomic_inc(&id_priv->dev_remove); | |
1023 | ||
1024 | switch (iw_event->event) { | |
1025 | case IW_CM_EVENT_CLOSE: | |
1026 | event = RDMA_CM_EVENT_DISCONNECTED; | |
1027 | break; | |
1028 | case IW_CM_EVENT_CONNECT_REPLY: | |
1029 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1030 | *sin = iw_event->local_addr; | |
1031 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr; | |
1032 | *sin = iw_event->remote_addr; | |
1033 | if (iw_event->status) | |
1034 | event = RDMA_CM_EVENT_REJECTED; | |
1035 | else | |
1036 | event = RDMA_CM_EVENT_ESTABLISHED; | |
1037 | break; | |
1038 | case IW_CM_EVENT_ESTABLISHED: | |
1039 | event = RDMA_CM_EVENT_ESTABLISHED; | |
1040 | break; | |
1041 | default: | |
1042 | BUG_ON(1); | |
1043 | } | |
1044 | ||
1045 | ret = cma_notify_user(id_priv, event, iw_event->status, | |
1046 | iw_event->private_data, | |
1047 | iw_event->private_data_len); | |
1048 | if (ret) { | |
1049 | /* Destroy the CM ID by returning a non-zero value. */ | |
1050 | id_priv->cm_id.iw = NULL; | |
1051 | cma_exch(id_priv, CMA_DESTROYING); | |
1052 | cma_release_remove(id_priv); | |
1053 | rdma_destroy_id(&id_priv->id); | |
1054 | return ret; | |
1055 | } | |
1056 | ||
1057 | cma_release_remove(id_priv); | |
1058 | return ret; | |
1059 | } | |
1060 | ||
1061 | static int iw_conn_req_handler(struct iw_cm_id *cm_id, | |
1062 | struct iw_cm_event *iw_event) | |
1063 | { | |
1064 | struct rdma_cm_id *new_cm_id; | |
1065 | struct rdma_id_private *listen_id, *conn_id; | |
1066 | struct sockaddr_in *sin; | |
1067 | struct net_device *dev = NULL; | |
1068 | int ret; | |
1069 | ||
1070 | listen_id = cm_id->context; | |
1071 | atomic_inc(&listen_id->dev_remove); | |
1072 | if (!cma_comp(listen_id, CMA_LISTEN)) { | |
1073 | ret = -ECONNABORTED; | |
1074 | goto out; | |
1075 | } | |
1076 | ||
1077 | /* Create a new RDMA id for the new IW CM ID */ | |
1078 | new_cm_id = rdma_create_id(listen_id->id.event_handler, | |
1079 | listen_id->id.context, | |
1080 | RDMA_PS_TCP); | |
1081 | if (!new_cm_id) { | |
1082 | ret = -ENOMEM; | |
1083 | goto out; | |
1084 | } | |
1085 | conn_id = container_of(new_cm_id, struct rdma_id_private, id); | |
1086 | atomic_inc(&conn_id->dev_remove); | |
1087 | conn_id->state = CMA_CONNECT; | |
1088 | ||
1089 | dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr); | |
1090 | if (!dev) { | |
1091 | ret = -EADDRNOTAVAIL; | |
1092 | cma_release_remove(conn_id); | |
1093 | rdma_destroy_id(new_cm_id); | |
1094 | goto out; | |
1095 | } | |
1096 | ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL); | |
1097 | if (ret) { | |
1098 | cma_release_remove(conn_id); | |
1099 | rdma_destroy_id(new_cm_id); | |
1100 | goto out; | |
1101 | } | |
1102 | ||
61a73c70 | 1103 | mutex_lock(&lock); |
07ebafba | 1104 | ret = cma_acquire_dev(conn_id); |
61a73c70 | 1105 | mutex_unlock(&lock); |
07ebafba TT |
1106 | if (ret) { |
1107 | cma_release_remove(conn_id); | |
1108 | rdma_destroy_id(new_cm_id); | |
1109 | goto out; | |
1110 | } | |
1111 | ||
1112 | conn_id->cm_id.iw = cm_id; | |
1113 | cm_id->context = conn_id; | |
1114 | cm_id->cm_handler = cma_iw_handler; | |
1115 | ||
1116 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr; | |
1117 | *sin = iw_event->local_addr; | |
1118 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr; | |
1119 | *sin = iw_event->remote_addr; | |
1120 | ||
1121 | ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0, | |
1122 | iw_event->private_data, | |
1123 | iw_event->private_data_len); | |
1124 | if (ret) { | |
1125 | /* User wants to destroy the CM ID */ | |
1126 | conn_id->cm_id.iw = NULL; | |
1127 | cma_exch(conn_id, CMA_DESTROYING); | |
1128 | cma_release_remove(conn_id); | |
1129 | rdma_destroy_id(&conn_id->id); | |
1130 | } | |
1131 | ||
1132 | out: | |
1133 | if (dev) | |
1134 | dev_put(dev); | |
1135 | cma_release_remove(listen_id); | |
1136 | return ret; | |
1137 | } | |
1138 | ||
e51060f0 SH |
1139 | static int cma_ib_listen(struct rdma_id_private *id_priv) |
1140 | { | |
1141 | struct ib_cm_compare_data compare_data; | |
1142 | struct sockaddr *addr; | |
1143 | __be64 svc_id; | |
1144 | int ret; | |
1145 | ||
1146 | id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler, | |
1147 | id_priv); | |
1148 | if (IS_ERR(id_priv->cm_id.ib)) | |
1149 | return PTR_ERR(id_priv->cm_id.ib); | |
1150 | ||
1151 | addr = &id_priv->id.route.addr.src_addr; | |
1152 | svc_id = cma_get_service_id(id_priv->id.ps, addr); | |
1153 | if (cma_any_addr(addr)) | |
1154 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL); | |
1155 | else { | |
1156 | cma_set_compare_data(id_priv->id.ps, addr, &compare_data); | |
1157 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data); | |
1158 | } | |
1159 | ||
1160 | if (ret) { | |
1161 | ib_destroy_cm_id(id_priv->cm_id.ib); | |
1162 | id_priv->cm_id.ib = NULL; | |
1163 | } | |
1164 | ||
1165 | return ret; | |
1166 | } | |
1167 | ||
07ebafba TT |
1168 | static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) |
1169 | { | |
1170 | int ret; | |
1171 | struct sockaddr_in *sin; | |
1172 | ||
1173 | id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device, | |
1174 | iw_conn_req_handler, | |
1175 | id_priv); | |
1176 | if (IS_ERR(id_priv->cm_id.iw)) | |
1177 | return PTR_ERR(id_priv->cm_id.iw); | |
1178 | ||
1179 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1180 | id_priv->cm_id.iw->local_addr = *sin; | |
1181 | ||
1182 | ret = iw_cm_listen(id_priv->cm_id.iw, backlog); | |
1183 | ||
1184 | if (ret) { | |
1185 | iw_destroy_cm_id(id_priv->cm_id.iw); | |
1186 | id_priv->cm_id.iw = NULL; | |
1187 | } | |
1188 | ||
1189 | return ret; | |
1190 | } | |
1191 | ||
e51060f0 SH |
1192 | static int cma_listen_handler(struct rdma_cm_id *id, |
1193 | struct rdma_cm_event *event) | |
1194 | { | |
1195 | struct rdma_id_private *id_priv = id->context; | |
1196 | ||
1197 | id->context = id_priv->id.context; | |
1198 | id->event_handler = id_priv->id.event_handler; | |
1199 | return id_priv->id.event_handler(id, event); | |
1200 | } | |
1201 | ||
1202 | static void cma_listen_on_dev(struct rdma_id_private *id_priv, | |
1203 | struct cma_device *cma_dev) | |
1204 | { | |
1205 | struct rdma_id_private *dev_id_priv; | |
1206 | struct rdma_cm_id *id; | |
1207 | int ret; | |
1208 | ||
1209 | id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps); | |
1210 | if (IS_ERR(id)) | |
1211 | return; | |
1212 | ||
1213 | dev_id_priv = container_of(id, struct rdma_id_private, id); | |
1214 | ||
1215 | dev_id_priv->state = CMA_ADDR_BOUND; | |
1216 | memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr, | |
1217 | ip_addr_size(&id_priv->id.route.addr.src_addr)); | |
1218 | ||
1219 | cma_attach_to_dev(dev_id_priv, cma_dev); | |
1220 | list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); | |
1221 | ||
1222 | ret = rdma_listen(id, id_priv->backlog); | |
1223 | if (ret) | |
1224 | goto err; | |
1225 | ||
1226 | return; | |
1227 | err: | |
1228 | cma_destroy_listen(dev_id_priv); | |
1229 | } | |
1230 | ||
1231 | static void cma_listen_on_all(struct rdma_id_private *id_priv) | |
1232 | { | |
1233 | struct cma_device *cma_dev; | |
1234 | ||
1235 | mutex_lock(&lock); | |
1236 | list_add_tail(&id_priv->list, &listen_any_list); | |
1237 | list_for_each_entry(cma_dev, &dev_list, list) | |
1238 | cma_listen_on_dev(id_priv, cma_dev); | |
1239 | mutex_unlock(&lock); | |
1240 | } | |
1241 | ||
1242 | static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af) | |
1243 | { | |
1244 | struct sockaddr_in addr_in; | |
1245 | ||
1246 | memset(&addr_in, 0, sizeof addr_in); | |
1247 | addr_in.sin_family = af; | |
1248 | return rdma_bind_addr(id, (struct sockaddr *) &addr_in); | |
1249 | } | |
1250 | ||
1251 | int rdma_listen(struct rdma_cm_id *id, int backlog) | |
1252 | { | |
1253 | struct rdma_id_private *id_priv; | |
1254 | int ret; | |
1255 | ||
1256 | id_priv = container_of(id, struct rdma_id_private, id); | |
1257 | if (id_priv->state == CMA_IDLE) { | |
1258 | ret = cma_bind_any(id, AF_INET); | |
1259 | if (ret) | |
1260 | return ret; | |
1261 | } | |
1262 | ||
1263 | if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN)) | |
1264 | return -EINVAL; | |
1265 | ||
1266 | id_priv->backlog = backlog; | |
1267 | if (id->device) { | |
07ebafba TT |
1268 | switch (rdma_node_get_transport(id->device->node_type)) { |
1269 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
1270 | ret = cma_ib_listen(id_priv); |
1271 | if (ret) | |
1272 | goto err; | |
1273 | break; | |
07ebafba TT |
1274 | case RDMA_TRANSPORT_IWARP: |
1275 | ret = cma_iw_listen(id_priv, backlog); | |
1276 | if (ret) | |
1277 | goto err; | |
1278 | break; | |
e51060f0 SH |
1279 | default: |
1280 | ret = -ENOSYS; | |
1281 | goto err; | |
1282 | } | |
1283 | } else | |
1284 | cma_listen_on_all(id_priv); | |
1285 | ||
1286 | return 0; | |
1287 | err: | |
1288 | id_priv->backlog = 0; | |
1289 | cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND); | |
1290 | return ret; | |
1291 | } | |
1292 | EXPORT_SYMBOL(rdma_listen); | |
1293 | ||
1294 | static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec, | |
1295 | void *context) | |
1296 | { | |
1297 | struct cma_work *work = context; | |
1298 | struct rdma_route *route; | |
1299 | ||
1300 | route = &work->id->id.route; | |
1301 | ||
1302 | if (!status) { | |
1303 | route->num_paths = 1; | |
1304 | *route->path_rec = *path_rec; | |
1305 | } else { | |
1306 | work->old_state = CMA_ROUTE_QUERY; | |
1307 | work->new_state = CMA_ADDR_RESOLVED; | |
1308 | work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; | |
1309 | } | |
1310 | ||
1311 | queue_work(cma_wq, &work->work); | |
1312 | } | |
1313 | ||
1314 | static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms, | |
1315 | struct cma_work *work) | |
1316 | { | |
1317 | struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr; | |
1318 | struct ib_sa_path_rec path_rec; | |
1319 | ||
1320 | memset(&path_rec, 0, sizeof path_rec); | |
f0ee3404 MT |
1321 | ib_addr_get_sgid(addr, &path_rec.sgid); |
1322 | ib_addr_get_dgid(addr, &path_rec.dgid); | |
e51060f0 SH |
1323 | path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr)); |
1324 | path_rec.numb_path = 1; | |
1325 | ||
1326 | id_priv->query_id = ib_sa_path_rec_get(id_priv->id.device, | |
1327 | id_priv->id.port_num, &path_rec, | |
1328 | IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | | |
1329 | IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH, | |
1330 | timeout_ms, GFP_KERNEL, | |
1331 | cma_query_handler, work, &id_priv->query); | |
1332 | ||
1333 | return (id_priv->query_id < 0) ? id_priv->query_id : 0; | |
1334 | } | |
1335 | ||
1336 | static void cma_work_handler(void *data) | |
1337 | { | |
1338 | struct cma_work *work = data; | |
1339 | struct rdma_id_private *id_priv = work->id; | |
1340 | int destroy = 0; | |
1341 | ||
1342 | atomic_inc(&id_priv->dev_remove); | |
1343 | if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) | |
1344 | goto out; | |
1345 | ||
1346 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
1347 | cma_exch(id_priv, CMA_DESTROYING); | |
1348 | destroy = 1; | |
1349 | } | |
1350 | out: | |
1351 | cma_release_remove(id_priv); | |
1352 | cma_deref_id(id_priv); | |
1353 | if (destroy) | |
1354 | rdma_destroy_id(&id_priv->id); | |
1355 | kfree(work); | |
1356 | } | |
1357 | ||
1358 | static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms) | |
1359 | { | |
1360 | struct rdma_route *route = &id_priv->id.route; | |
1361 | struct cma_work *work; | |
1362 | int ret; | |
1363 | ||
1364 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1365 | if (!work) | |
1366 | return -ENOMEM; | |
1367 | ||
1368 | work->id = id_priv; | |
1369 | INIT_WORK(&work->work, cma_work_handler, work); | |
1370 | work->old_state = CMA_ROUTE_QUERY; | |
1371 | work->new_state = CMA_ROUTE_RESOLVED; | |
1372 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; | |
1373 | ||
1374 | route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); | |
1375 | if (!route->path_rec) { | |
1376 | ret = -ENOMEM; | |
1377 | goto err1; | |
1378 | } | |
1379 | ||
1380 | ret = cma_query_ib_route(id_priv, timeout_ms, work); | |
1381 | if (ret) | |
1382 | goto err2; | |
1383 | ||
1384 | return 0; | |
1385 | err2: | |
1386 | kfree(route->path_rec); | |
1387 | route->path_rec = NULL; | |
1388 | err1: | |
1389 | kfree(work); | |
1390 | return ret; | |
1391 | } | |
1392 | ||
1393 | int rdma_set_ib_paths(struct rdma_cm_id *id, | |
1394 | struct ib_sa_path_rec *path_rec, int num_paths) | |
1395 | { | |
1396 | struct rdma_id_private *id_priv; | |
1397 | int ret; | |
1398 | ||
1399 | id_priv = container_of(id, struct rdma_id_private, id); | |
1400 | if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED)) | |
1401 | return -EINVAL; | |
1402 | ||
1403 | id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL); | |
1404 | if (!id->route.path_rec) { | |
1405 | ret = -ENOMEM; | |
1406 | goto err; | |
1407 | } | |
1408 | ||
1409 | memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths); | |
1410 | return 0; | |
1411 | err: | |
1412 | cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED); | |
1413 | return ret; | |
1414 | } | |
1415 | EXPORT_SYMBOL(rdma_set_ib_paths); | |
1416 | ||
07ebafba TT |
1417 | static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms) |
1418 | { | |
1419 | struct cma_work *work; | |
1420 | ||
1421 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1422 | if (!work) | |
1423 | return -ENOMEM; | |
1424 | ||
1425 | work->id = id_priv; | |
1426 | INIT_WORK(&work->work, cma_work_handler, work); | |
1427 | work->old_state = CMA_ROUTE_QUERY; | |
1428 | work->new_state = CMA_ROUTE_RESOLVED; | |
1429 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; | |
1430 | queue_work(cma_wq, &work->work); | |
1431 | return 0; | |
1432 | } | |
1433 | ||
e51060f0 SH |
1434 | int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) |
1435 | { | |
1436 | struct rdma_id_private *id_priv; | |
1437 | int ret; | |
1438 | ||
1439 | id_priv = container_of(id, struct rdma_id_private, id); | |
1440 | if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY)) | |
1441 | return -EINVAL; | |
1442 | ||
1443 | atomic_inc(&id_priv->refcount); | |
07ebafba TT |
1444 | switch (rdma_node_get_transport(id->device->node_type)) { |
1445 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
1446 | ret = cma_resolve_ib_route(id_priv, timeout_ms); |
1447 | break; | |
07ebafba TT |
1448 | case RDMA_TRANSPORT_IWARP: |
1449 | ret = cma_resolve_iw_route(id_priv, timeout_ms); | |
1450 | break; | |
e51060f0 SH |
1451 | default: |
1452 | ret = -ENOSYS; | |
1453 | break; | |
1454 | } | |
1455 | if (ret) | |
1456 | goto err; | |
1457 | ||
1458 | return 0; | |
1459 | err: | |
1460 | cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED); | |
1461 | cma_deref_id(id_priv); | |
1462 | return ret; | |
1463 | } | |
1464 | EXPORT_SYMBOL(rdma_resolve_route); | |
1465 | ||
1466 | static int cma_bind_loopback(struct rdma_id_private *id_priv) | |
1467 | { | |
1468 | struct cma_device *cma_dev; | |
1469 | struct ib_port_attr port_attr; | |
f0ee3404 | 1470 | union ib_gid gid; |
e51060f0 SH |
1471 | u16 pkey; |
1472 | int ret; | |
1473 | u8 p; | |
1474 | ||
1475 | mutex_lock(&lock); | |
1476 | list_for_each_entry(cma_dev, &dev_list, list) | |
1477 | for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p) | |
1478 | if (!ib_query_port (cma_dev->device, p, &port_attr) && | |
1479 | port_attr.state == IB_PORT_ACTIVE) | |
1480 | goto port_found; | |
1481 | ||
1482 | if (!list_empty(&dev_list)) { | |
1483 | p = 1; | |
1484 | cma_dev = list_entry(dev_list.next, struct cma_device, list); | |
1485 | } else { | |
1486 | ret = -ENODEV; | |
1487 | goto out; | |
1488 | } | |
1489 | ||
1490 | port_found: | |
f0ee3404 | 1491 | ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid); |
e51060f0 SH |
1492 | if (ret) |
1493 | goto out; | |
1494 | ||
1495 | ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); | |
1496 | if (ret) | |
1497 | goto out; | |
1498 | ||
f0ee3404 | 1499 | ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
e51060f0 SH |
1500 | ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); |
1501 | id_priv->id.port_num = p; | |
1502 | cma_attach_to_dev(id_priv, cma_dev); | |
1503 | out: | |
1504 | mutex_unlock(&lock); | |
1505 | return ret; | |
1506 | } | |
1507 | ||
1508 | static void addr_handler(int status, struct sockaddr *src_addr, | |
1509 | struct rdma_dev_addr *dev_addr, void *context) | |
1510 | { | |
1511 | struct rdma_id_private *id_priv = context; | |
1512 | enum rdma_cm_event_type event; | |
1513 | ||
1514 | atomic_inc(&id_priv->dev_remove); | |
61a73c70 SH |
1515 | |
1516 | /* | |
1517 | * Grab mutex to block rdma_destroy_id() from removing the device while | |
1518 | * we're trying to acquire it. | |
1519 | */ | |
1520 | mutex_lock(&lock); | |
1521 | if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) { | |
1522 | mutex_unlock(&lock); | |
1523 | goto out; | |
1524 | } | |
1525 | ||
1526 | if (!status && !id_priv->cma_dev) | |
e51060f0 | 1527 | status = cma_acquire_dev(id_priv); |
61a73c70 | 1528 | mutex_unlock(&lock); |
e51060f0 SH |
1529 | |
1530 | if (status) { | |
61a73c70 | 1531 | if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND)) |
e51060f0 SH |
1532 | goto out; |
1533 | event = RDMA_CM_EVENT_ADDR_ERROR; | |
1534 | } else { | |
e51060f0 SH |
1535 | memcpy(&id_priv->id.route.addr.src_addr, src_addr, |
1536 | ip_addr_size(src_addr)); | |
1537 | event = RDMA_CM_EVENT_ADDR_RESOLVED; | |
1538 | } | |
1539 | ||
1540 | if (cma_notify_user(id_priv, event, status, NULL, 0)) { | |
1541 | cma_exch(id_priv, CMA_DESTROYING); | |
1542 | cma_release_remove(id_priv); | |
1543 | cma_deref_id(id_priv); | |
1544 | rdma_destroy_id(&id_priv->id); | |
1545 | return; | |
1546 | } | |
1547 | out: | |
1548 | cma_release_remove(id_priv); | |
1549 | cma_deref_id(id_priv); | |
1550 | } | |
1551 | ||
1552 | static int cma_resolve_loopback(struct rdma_id_private *id_priv) | |
1553 | { | |
1554 | struct cma_work *work; | |
1555 | struct sockaddr_in *src_in, *dst_in; | |
f0ee3404 | 1556 | union ib_gid gid; |
e51060f0 SH |
1557 | int ret; |
1558 | ||
1559 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1560 | if (!work) | |
1561 | return -ENOMEM; | |
1562 | ||
1563 | if (!id_priv->cma_dev) { | |
1564 | ret = cma_bind_loopback(id_priv); | |
1565 | if (ret) | |
1566 | goto err; | |
1567 | } | |
1568 | ||
f0ee3404 MT |
1569 | ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
1570 | ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 SH |
1571 | |
1572 | if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) { | |
1573 | src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr; | |
1574 | dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr; | |
1575 | src_in->sin_family = dst_in->sin_family; | |
1576 | src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr; | |
1577 | } | |
1578 | ||
1579 | work->id = id_priv; | |
1580 | INIT_WORK(&work->work, cma_work_handler, work); | |
1581 | work->old_state = CMA_ADDR_QUERY; | |
1582 | work->new_state = CMA_ADDR_RESOLVED; | |
1583 | work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; | |
1584 | queue_work(cma_wq, &work->work); | |
1585 | return 0; | |
1586 | err: | |
1587 | kfree(work); | |
1588 | return ret; | |
1589 | } | |
1590 | ||
1591 | static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
1592 | struct sockaddr *dst_addr) | |
1593 | { | |
1594 | if (src_addr && src_addr->sa_family) | |
1595 | return rdma_bind_addr(id, src_addr); | |
1596 | else | |
1597 | return cma_bind_any(id, dst_addr->sa_family); | |
1598 | } | |
1599 | ||
1600 | int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
1601 | struct sockaddr *dst_addr, int timeout_ms) | |
1602 | { | |
1603 | struct rdma_id_private *id_priv; | |
1604 | int ret; | |
1605 | ||
1606 | id_priv = container_of(id, struct rdma_id_private, id); | |
1607 | if (id_priv->state == CMA_IDLE) { | |
1608 | ret = cma_bind_addr(id, src_addr, dst_addr); | |
1609 | if (ret) | |
1610 | return ret; | |
1611 | } | |
1612 | ||
1613 | if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY)) | |
1614 | return -EINVAL; | |
1615 | ||
1616 | atomic_inc(&id_priv->refcount); | |
1617 | memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr)); | |
1618 | if (cma_any_addr(dst_addr)) | |
1619 | ret = cma_resolve_loopback(id_priv); | |
1620 | else | |
1621 | ret = rdma_resolve_ip(&id->route.addr.src_addr, dst_addr, | |
1622 | &id->route.addr.dev_addr, | |
1623 | timeout_ms, addr_handler, id_priv); | |
1624 | if (ret) | |
1625 | goto err; | |
1626 | ||
1627 | return 0; | |
1628 | err: | |
1629 | cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND); | |
1630 | cma_deref_id(id_priv); | |
1631 | return ret; | |
1632 | } | |
1633 | EXPORT_SYMBOL(rdma_resolve_addr); | |
1634 | ||
1635 | static void cma_bind_port(struct rdma_bind_list *bind_list, | |
1636 | struct rdma_id_private *id_priv) | |
1637 | { | |
1638 | struct sockaddr_in *sin; | |
1639 | ||
1640 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1641 | sin->sin_port = htons(bind_list->port); | |
1642 | id_priv->bind_list = bind_list; | |
1643 | hlist_add_head(&id_priv->node, &bind_list->owners); | |
1644 | } | |
1645 | ||
1646 | static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv, | |
1647 | unsigned short snum) | |
1648 | { | |
1649 | struct rdma_bind_list *bind_list; | |
1650 | int port, start, ret; | |
1651 | ||
1652 | bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); | |
1653 | if (!bind_list) | |
1654 | return -ENOMEM; | |
1655 | ||
1656 | start = snum ? snum : sysctl_local_port_range[0]; | |
1657 | ||
1658 | do { | |
1659 | ret = idr_get_new_above(ps, bind_list, start, &port); | |
1660 | } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL)); | |
1661 | ||
1662 | if (ret) | |
1663 | goto err; | |
1664 | ||
1665 | if ((snum && port != snum) || | |
1666 | (!snum && port > sysctl_local_port_range[1])) { | |
1667 | idr_remove(ps, port); | |
1668 | ret = -EADDRNOTAVAIL; | |
1669 | goto err; | |
1670 | } | |
1671 | ||
1672 | bind_list->ps = ps; | |
1673 | bind_list->port = (unsigned short) port; | |
1674 | cma_bind_port(bind_list, id_priv); | |
1675 | return 0; | |
1676 | err: | |
1677 | kfree(bind_list); | |
1678 | return ret; | |
1679 | } | |
1680 | ||
1681 | static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv) | |
1682 | { | |
1683 | struct rdma_id_private *cur_id; | |
1684 | struct sockaddr_in *sin, *cur_sin; | |
1685 | struct rdma_bind_list *bind_list; | |
1686 | struct hlist_node *node; | |
1687 | unsigned short snum; | |
1688 | ||
1689 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1690 | snum = ntohs(sin->sin_port); | |
1691 | if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) | |
1692 | return -EACCES; | |
1693 | ||
1694 | bind_list = idr_find(ps, snum); | |
1695 | if (!bind_list) | |
1696 | return cma_alloc_port(ps, id_priv, snum); | |
1697 | ||
1698 | /* | |
1699 | * We don't support binding to any address if anyone is bound to | |
1700 | * a specific address on the same port. | |
1701 | */ | |
1702 | if (cma_any_addr(&id_priv->id.route.addr.src_addr)) | |
1703 | return -EADDRNOTAVAIL; | |
1704 | ||
1705 | hlist_for_each_entry(cur_id, node, &bind_list->owners, node) { | |
1706 | if (cma_any_addr(&cur_id->id.route.addr.src_addr)) | |
1707 | return -EADDRNOTAVAIL; | |
3cd96564 | 1708 | |
e51060f0 SH |
1709 | cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr; |
1710 | if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr) | |
1711 | return -EADDRINUSE; | |
1712 | } | |
1713 | ||
1714 | cma_bind_port(bind_list, id_priv); | |
1715 | return 0; | |
1716 | } | |
1717 | ||
1718 | static int cma_get_port(struct rdma_id_private *id_priv) | |
1719 | { | |
1720 | struct idr *ps; | |
1721 | int ret; | |
1722 | ||
1723 | switch (id_priv->id.ps) { | |
1724 | case RDMA_PS_SDP: | |
1725 | ps = &sdp_ps; | |
1726 | break; | |
1727 | case RDMA_PS_TCP: | |
1728 | ps = &tcp_ps; | |
1729 | break; | |
1730 | default: | |
1731 | return -EPROTONOSUPPORT; | |
1732 | } | |
1733 | ||
1734 | mutex_lock(&lock); | |
1735 | if (cma_any_port(&id_priv->id.route.addr.src_addr)) | |
1736 | ret = cma_alloc_port(ps, id_priv, 0); | |
1737 | else | |
1738 | ret = cma_use_port(ps, id_priv); | |
1739 | mutex_unlock(&lock); | |
1740 | ||
1741 | return ret; | |
1742 | } | |
1743 | ||
1744 | int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) | |
1745 | { | |
1746 | struct rdma_id_private *id_priv; | |
1747 | int ret; | |
1748 | ||
1749 | if (addr->sa_family != AF_INET) | |
1750 | return -EAFNOSUPPORT; | |
1751 | ||
1752 | id_priv = container_of(id, struct rdma_id_private, id); | |
1753 | if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND)) | |
1754 | return -EINVAL; | |
1755 | ||
1756 | if (!cma_any_addr(addr)) { | |
1757 | ret = rdma_translate_ip(addr, &id->route.addr.dev_addr); | |
61a73c70 SH |
1758 | if (!ret) { |
1759 | mutex_lock(&lock); | |
e51060f0 | 1760 | ret = cma_acquire_dev(id_priv); |
61a73c70 SH |
1761 | mutex_unlock(&lock); |
1762 | } | |
e51060f0 SH |
1763 | if (ret) |
1764 | goto err; | |
1765 | } | |
1766 | ||
1767 | memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr)); | |
1768 | ret = cma_get_port(id_priv); | |
1769 | if (ret) | |
1770 | goto err; | |
1771 | ||
1772 | return 0; | |
1773 | err: | |
1774 | cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE); | |
1775 | return ret; | |
1776 | } | |
1777 | EXPORT_SYMBOL(rdma_bind_addr); | |
1778 | ||
1779 | static int cma_format_hdr(void *hdr, enum rdma_port_space ps, | |
1780 | struct rdma_route *route) | |
1781 | { | |
1782 | struct sockaddr_in *src4, *dst4; | |
1783 | struct cma_hdr *cma_hdr; | |
1784 | struct sdp_hh *sdp_hdr; | |
1785 | ||
1786 | src4 = (struct sockaddr_in *) &route->addr.src_addr; | |
1787 | dst4 = (struct sockaddr_in *) &route->addr.dst_addr; | |
1788 | ||
1789 | switch (ps) { | |
1790 | case RDMA_PS_SDP: | |
1791 | sdp_hdr = hdr; | |
1792 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) | |
1793 | return -EINVAL; | |
1794 | sdp_set_ip_ver(sdp_hdr, 4); | |
1795 | sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
1796 | sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
1797 | sdp_hdr->port = src4->sin_port; | |
1798 | break; | |
1799 | default: | |
1800 | cma_hdr = hdr; | |
1801 | cma_hdr->cma_version = CMA_VERSION; | |
1802 | cma_set_ip_ver(cma_hdr, 4); | |
1803 | cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
1804 | cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
1805 | cma_hdr->port = src4->sin_port; | |
1806 | break; | |
1807 | } | |
1808 | return 0; | |
1809 | } | |
1810 | ||
1811 | static int cma_connect_ib(struct rdma_id_private *id_priv, | |
1812 | struct rdma_conn_param *conn_param) | |
1813 | { | |
1814 | struct ib_cm_req_param req; | |
1815 | struct rdma_route *route; | |
1816 | void *private_data; | |
1817 | int offset, ret; | |
1818 | ||
1819 | memset(&req, 0, sizeof req); | |
1820 | offset = cma_user_data_offset(id_priv->id.ps); | |
1821 | req.private_data_len = offset + conn_param->private_data_len; | |
1822 | private_data = kzalloc(req.private_data_len, GFP_ATOMIC); | |
1823 | if (!private_data) | |
1824 | return -ENOMEM; | |
1825 | ||
1826 | if (conn_param->private_data && conn_param->private_data_len) | |
1827 | memcpy(private_data + offset, conn_param->private_data, | |
1828 | conn_param->private_data_len); | |
1829 | ||
1830 | id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler, | |
1831 | id_priv); | |
1832 | if (IS_ERR(id_priv->cm_id.ib)) { | |
1833 | ret = PTR_ERR(id_priv->cm_id.ib); | |
1834 | goto out; | |
1835 | } | |
1836 | ||
1837 | route = &id_priv->id.route; | |
1838 | ret = cma_format_hdr(private_data, id_priv->id.ps, route); | |
1839 | if (ret) | |
1840 | goto out; | |
1841 | req.private_data = private_data; | |
1842 | ||
1843 | req.primary_path = &route->path_rec[0]; | |
1844 | if (route->num_paths == 2) | |
1845 | req.alternate_path = &route->path_rec[1]; | |
1846 | ||
1847 | req.service_id = cma_get_service_id(id_priv->id.ps, | |
1848 | &route->addr.dst_addr); | |
1849 | req.qp_num = id_priv->qp_num; | |
1850 | req.qp_type = id_priv->qp_type; | |
1851 | req.starting_psn = id_priv->seq_num; | |
1852 | req.responder_resources = conn_param->responder_resources; | |
1853 | req.initiator_depth = conn_param->initiator_depth; | |
1854 | req.flow_control = conn_param->flow_control; | |
1855 | req.retry_count = conn_param->retry_count; | |
1856 | req.rnr_retry_count = conn_param->rnr_retry_count; | |
1857 | req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
1858 | req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
1859 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
1860 | req.srq = id_priv->srq ? 1 : 0; | |
1861 | ||
1862 | ret = ib_send_cm_req(id_priv->cm_id.ib, &req); | |
1863 | out: | |
1864 | kfree(private_data); | |
1865 | return ret; | |
1866 | } | |
1867 | ||
07ebafba TT |
1868 | static int cma_connect_iw(struct rdma_id_private *id_priv, |
1869 | struct rdma_conn_param *conn_param) | |
1870 | { | |
1871 | struct iw_cm_id *cm_id; | |
1872 | struct sockaddr_in* sin; | |
1873 | int ret; | |
1874 | struct iw_cm_conn_param iw_param; | |
1875 | ||
1876 | cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); | |
1877 | if (IS_ERR(cm_id)) { | |
1878 | ret = PTR_ERR(cm_id); | |
1879 | goto out; | |
1880 | } | |
1881 | ||
1882 | id_priv->cm_id.iw = cm_id; | |
1883 | ||
1884 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr; | |
1885 | cm_id->local_addr = *sin; | |
1886 | ||
1887 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr; | |
1888 | cm_id->remote_addr = *sin; | |
1889 | ||
1890 | ret = cma_modify_qp_rtr(&id_priv->id); | |
1891 | if (ret) { | |
1892 | iw_destroy_cm_id(cm_id); | |
1893 | return ret; | |
1894 | } | |
1895 | ||
1896 | iw_param.ord = conn_param->initiator_depth; | |
1897 | iw_param.ird = conn_param->responder_resources; | |
1898 | iw_param.private_data = conn_param->private_data; | |
1899 | iw_param.private_data_len = conn_param->private_data_len; | |
1900 | if (id_priv->id.qp) | |
1901 | iw_param.qpn = id_priv->qp_num; | |
1902 | else | |
1903 | iw_param.qpn = conn_param->qp_num; | |
1904 | ret = iw_cm_connect(cm_id, &iw_param); | |
1905 | out: | |
1906 | return ret; | |
1907 | } | |
1908 | ||
e51060f0 SH |
1909 | int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
1910 | { | |
1911 | struct rdma_id_private *id_priv; | |
1912 | int ret; | |
1913 | ||
1914 | id_priv = container_of(id, struct rdma_id_private, id); | |
1915 | if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT)) | |
1916 | return -EINVAL; | |
1917 | ||
1918 | if (!id->qp) { | |
1919 | id_priv->qp_num = conn_param->qp_num; | |
1920 | id_priv->qp_type = conn_param->qp_type; | |
1921 | id_priv->srq = conn_param->srq; | |
1922 | } | |
1923 | ||
07ebafba TT |
1924 | switch (rdma_node_get_transport(id->device->node_type)) { |
1925 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
1926 | ret = cma_connect_ib(id_priv, conn_param); |
1927 | break; | |
07ebafba TT |
1928 | case RDMA_TRANSPORT_IWARP: |
1929 | ret = cma_connect_iw(id_priv, conn_param); | |
1930 | break; | |
e51060f0 SH |
1931 | default: |
1932 | ret = -ENOSYS; | |
1933 | break; | |
1934 | } | |
1935 | if (ret) | |
1936 | goto err; | |
1937 | ||
1938 | return 0; | |
1939 | err: | |
1940 | cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED); | |
1941 | return ret; | |
1942 | } | |
1943 | EXPORT_SYMBOL(rdma_connect); | |
1944 | ||
1945 | static int cma_accept_ib(struct rdma_id_private *id_priv, | |
1946 | struct rdma_conn_param *conn_param) | |
1947 | { | |
1948 | struct ib_cm_rep_param rep; | |
1949 | int ret; | |
1950 | ||
1951 | ret = cma_modify_qp_rtr(&id_priv->id); | |
1952 | if (ret) | |
1953 | return ret; | |
1954 | ||
1955 | memset(&rep, 0, sizeof rep); | |
1956 | rep.qp_num = id_priv->qp_num; | |
1957 | rep.starting_psn = id_priv->seq_num; | |
1958 | rep.private_data = conn_param->private_data; | |
1959 | rep.private_data_len = conn_param->private_data_len; | |
1960 | rep.responder_resources = conn_param->responder_resources; | |
1961 | rep.initiator_depth = conn_param->initiator_depth; | |
1962 | rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT; | |
1963 | rep.failover_accepted = 0; | |
1964 | rep.flow_control = conn_param->flow_control; | |
1965 | rep.rnr_retry_count = conn_param->rnr_retry_count; | |
1966 | rep.srq = id_priv->srq ? 1 : 0; | |
1967 | ||
1968 | return ib_send_cm_rep(id_priv->cm_id.ib, &rep); | |
1969 | } | |
1970 | ||
07ebafba TT |
1971 | static int cma_accept_iw(struct rdma_id_private *id_priv, |
1972 | struct rdma_conn_param *conn_param) | |
1973 | { | |
1974 | struct iw_cm_conn_param iw_param; | |
1975 | int ret; | |
1976 | ||
1977 | ret = cma_modify_qp_rtr(&id_priv->id); | |
1978 | if (ret) | |
1979 | return ret; | |
1980 | ||
1981 | iw_param.ord = conn_param->initiator_depth; | |
1982 | iw_param.ird = conn_param->responder_resources; | |
1983 | iw_param.private_data = conn_param->private_data; | |
1984 | iw_param.private_data_len = conn_param->private_data_len; | |
1985 | if (id_priv->id.qp) { | |
1986 | iw_param.qpn = id_priv->qp_num; | |
1987 | } else | |
1988 | iw_param.qpn = conn_param->qp_num; | |
1989 | ||
1990 | return iw_cm_accept(id_priv->cm_id.iw, &iw_param); | |
1991 | } | |
1992 | ||
e51060f0 SH |
1993 | int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
1994 | { | |
1995 | struct rdma_id_private *id_priv; | |
1996 | int ret; | |
1997 | ||
1998 | id_priv = container_of(id, struct rdma_id_private, id); | |
1999 | if (!cma_comp(id_priv, CMA_CONNECT)) | |
2000 | return -EINVAL; | |
2001 | ||
2002 | if (!id->qp && conn_param) { | |
2003 | id_priv->qp_num = conn_param->qp_num; | |
2004 | id_priv->qp_type = conn_param->qp_type; | |
2005 | id_priv->srq = conn_param->srq; | |
2006 | } | |
2007 | ||
07ebafba TT |
2008 | switch (rdma_node_get_transport(id->device->node_type)) { |
2009 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
2010 | if (conn_param) |
2011 | ret = cma_accept_ib(id_priv, conn_param); | |
2012 | else | |
2013 | ret = cma_rep_recv(id_priv); | |
2014 | break; | |
07ebafba TT |
2015 | case RDMA_TRANSPORT_IWARP: |
2016 | ret = cma_accept_iw(id_priv, conn_param); | |
2017 | break; | |
e51060f0 SH |
2018 | default: |
2019 | ret = -ENOSYS; | |
2020 | break; | |
2021 | } | |
2022 | ||
2023 | if (ret) | |
2024 | goto reject; | |
2025 | ||
2026 | return 0; | |
2027 | reject: | |
2028 | cma_modify_qp_err(id); | |
2029 | rdma_reject(id, NULL, 0); | |
2030 | return ret; | |
2031 | } | |
2032 | EXPORT_SYMBOL(rdma_accept); | |
2033 | ||
2034 | int rdma_reject(struct rdma_cm_id *id, const void *private_data, | |
2035 | u8 private_data_len) | |
2036 | { | |
2037 | struct rdma_id_private *id_priv; | |
2038 | int ret; | |
2039 | ||
2040 | id_priv = container_of(id, struct rdma_id_private, id); | |
2041 | if (!cma_comp(id_priv, CMA_CONNECT)) | |
2042 | return -EINVAL; | |
2043 | ||
07ebafba TT |
2044 | switch (rdma_node_get_transport(id->device->node_type)) { |
2045 | case RDMA_TRANSPORT_IB: | |
e51060f0 SH |
2046 | ret = ib_send_cm_rej(id_priv->cm_id.ib, |
2047 | IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, | |
2048 | private_data, private_data_len); | |
2049 | break; | |
07ebafba TT |
2050 | case RDMA_TRANSPORT_IWARP: |
2051 | ret = iw_cm_reject(id_priv->cm_id.iw, | |
2052 | private_data, private_data_len); | |
2053 | break; | |
e51060f0 SH |
2054 | default: |
2055 | ret = -ENOSYS; | |
2056 | break; | |
2057 | } | |
2058 | return ret; | |
2059 | } | |
2060 | EXPORT_SYMBOL(rdma_reject); | |
2061 | ||
2062 | int rdma_disconnect(struct rdma_cm_id *id) | |
2063 | { | |
2064 | struct rdma_id_private *id_priv; | |
2065 | int ret; | |
2066 | ||
2067 | id_priv = container_of(id, struct rdma_id_private, id); | |
2068 | if (!cma_comp(id_priv, CMA_CONNECT) && | |
2069 | !cma_comp(id_priv, CMA_DISCONNECT)) | |
2070 | return -EINVAL; | |
2071 | ||
07ebafba TT |
2072 | switch (rdma_node_get_transport(id->device->node_type)) { |
2073 | case RDMA_TRANSPORT_IB: | |
2074 | ret = cma_modify_qp_err(id); | |
2075 | if (ret) | |
2076 | goto out; | |
e51060f0 SH |
2077 | /* Initiate or respond to a disconnect. */ |
2078 | if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) | |
2079 | ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0); | |
2080 | break; | |
07ebafba TT |
2081 | case RDMA_TRANSPORT_IWARP: |
2082 | ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); | |
2083 | break; | |
e51060f0 | 2084 | default: |
07ebafba | 2085 | ret = -EINVAL; |
e51060f0 SH |
2086 | break; |
2087 | } | |
2088 | out: | |
2089 | return ret; | |
2090 | } | |
2091 | EXPORT_SYMBOL(rdma_disconnect); | |
2092 | ||
2093 | static void cma_add_one(struct ib_device *device) | |
2094 | { | |
2095 | struct cma_device *cma_dev; | |
2096 | struct rdma_id_private *id_priv; | |
2097 | ||
2098 | cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); | |
2099 | if (!cma_dev) | |
2100 | return; | |
2101 | ||
2102 | cma_dev->device = device; | |
2103 | cma_dev->node_guid = device->node_guid; | |
2104 | if (!cma_dev->node_guid) | |
2105 | goto err; | |
2106 | ||
2107 | init_completion(&cma_dev->comp); | |
2108 | atomic_set(&cma_dev->refcount, 1); | |
2109 | INIT_LIST_HEAD(&cma_dev->id_list); | |
2110 | ib_set_client_data(device, &cma_client, cma_dev); | |
2111 | ||
2112 | mutex_lock(&lock); | |
2113 | list_add_tail(&cma_dev->list, &dev_list); | |
2114 | list_for_each_entry(id_priv, &listen_any_list, list) | |
2115 | cma_listen_on_dev(id_priv, cma_dev); | |
2116 | mutex_unlock(&lock); | |
2117 | return; | |
2118 | err: | |
2119 | kfree(cma_dev); | |
2120 | } | |
2121 | ||
2122 | static int cma_remove_id_dev(struct rdma_id_private *id_priv) | |
2123 | { | |
2124 | enum cma_state state; | |
2125 | ||
2126 | /* Record that we want to remove the device */ | |
2127 | state = cma_exch(id_priv, CMA_DEVICE_REMOVAL); | |
2128 | if (state == CMA_DESTROYING) | |
2129 | return 0; | |
2130 | ||
2131 | cma_cancel_operation(id_priv, state); | |
2132 | wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove)); | |
2133 | ||
2134 | /* Check for destruction from another callback. */ | |
2135 | if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL)) | |
2136 | return 0; | |
2137 | ||
2138 | return cma_notify_user(id_priv, RDMA_CM_EVENT_DEVICE_REMOVAL, | |
2139 | 0, NULL, 0); | |
2140 | } | |
2141 | ||
2142 | static void cma_process_remove(struct cma_device *cma_dev) | |
2143 | { | |
2144 | struct list_head remove_list; | |
2145 | struct rdma_id_private *id_priv; | |
2146 | int ret; | |
2147 | ||
2148 | INIT_LIST_HEAD(&remove_list); | |
2149 | ||
2150 | mutex_lock(&lock); | |
2151 | while (!list_empty(&cma_dev->id_list)) { | |
2152 | id_priv = list_entry(cma_dev->id_list.next, | |
2153 | struct rdma_id_private, list); | |
2154 | ||
2155 | if (cma_internal_listen(id_priv)) { | |
2156 | cma_destroy_listen(id_priv); | |
2157 | continue; | |
2158 | } | |
2159 | ||
2160 | list_del(&id_priv->list); | |
2161 | list_add_tail(&id_priv->list, &remove_list); | |
2162 | atomic_inc(&id_priv->refcount); | |
2163 | mutex_unlock(&lock); | |
2164 | ||
2165 | ret = cma_remove_id_dev(id_priv); | |
2166 | cma_deref_id(id_priv); | |
2167 | if (ret) | |
2168 | rdma_destroy_id(&id_priv->id); | |
2169 | ||
2170 | mutex_lock(&lock); | |
2171 | } | |
2172 | mutex_unlock(&lock); | |
2173 | ||
2174 | cma_deref_dev(cma_dev); | |
2175 | wait_for_completion(&cma_dev->comp); | |
2176 | } | |
2177 | ||
2178 | static void cma_remove_one(struct ib_device *device) | |
2179 | { | |
2180 | struct cma_device *cma_dev; | |
2181 | ||
2182 | cma_dev = ib_get_client_data(device, &cma_client); | |
2183 | if (!cma_dev) | |
2184 | return; | |
2185 | ||
2186 | mutex_lock(&lock); | |
2187 | list_del(&cma_dev->list); | |
2188 | mutex_unlock(&lock); | |
2189 | ||
2190 | cma_process_remove(cma_dev); | |
2191 | kfree(cma_dev); | |
2192 | } | |
2193 | ||
2194 | static int cma_init(void) | |
2195 | { | |
2196 | int ret; | |
2197 | ||
2198 | cma_wq = create_singlethread_workqueue("rdma_cm_wq"); | |
2199 | if (!cma_wq) | |
2200 | return -ENOMEM; | |
2201 | ||
2202 | ret = ib_register_client(&cma_client); | |
2203 | if (ret) | |
2204 | goto err; | |
2205 | return 0; | |
2206 | ||
2207 | err: | |
2208 | destroy_workqueue(cma_wq); | |
2209 | return ret; | |
2210 | } | |
2211 | ||
2212 | static void cma_cleanup(void) | |
2213 | { | |
2214 | ib_unregister_client(&cma_client); | |
2215 | destroy_workqueue(cma_wq); | |
2216 | idr_destroy(&sdp_ps); | |
2217 | idr_destroy(&tcp_ps); | |
2218 | } | |
2219 | ||
2220 | module_init(cma_init); | |
2221 | module_exit(cma_cleanup); |