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