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 | * | |
a9474917 SH |
7 | * This software is available to you under a choice of one of two |
8 | * licenses. You may choose to be licensed under the terms of the GNU | |
9 | * General Public License (GPL) Version 2, available from the file | |
10 | * COPYING in the main directory of this source tree, or the | |
11 | * OpenIB.org BSD license below: | |
e51060f0 | 12 | * |
a9474917 SH |
13 | * Redistribution and use in source and binary forms, with or |
14 | * without modification, are permitted provided that the following | |
15 | * conditions are met: | |
e51060f0 | 16 | * |
a9474917 SH |
17 | * - Redistributions of source code must retain the above |
18 | * copyright notice, this list of conditions and the following | |
19 | * disclaimer. | |
e51060f0 | 20 | * |
a9474917 SH |
21 | * - Redistributions in binary form must reproduce the above |
22 | * copyright notice, this list of conditions and the following | |
23 | * disclaimer in the documentation and/or other materials | |
24 | * provided with the distribution. | |
e51060f0 | 25 | * |
a9474917 SH |
26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
27 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
29 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
30 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
31 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
32 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
33 | * SOFTWARE. | |
e51060f0 SH |
34 | */ |
35 | ||
36 | #include <linux/completion.h> | |
37 | #include <linux/in.h> | |
38 | #include <linux/in6.h> | |
39 | #include <linux/mutex.h> | |
40 | #include <linux/random.h> | |
41 | #include <linux/idr.h> | |
07ebafba | 42 | #include <linux/inetdevice.h> |
5a0e3ad6 | 43 | #include <linux/slab.h> |
e51060f0 SH |
44 | |
45 | #include <net/tcp.h> | |
1f5175ad | 46 | #include <net/ipv6.h> |
e51060f0 SH |
47 | |
48 | #include <rdma/rdma_cm.h> | |
49 | #include <rdma/rdma_cm_ib.h> | |
753f618a | 50 | #include <rdma/rdma_netlink.h> |
e51060f0 SH |
51 | #include <rdma/ib_cache.h> |
52 | #include <rdma/ib_cm.h> | |
53 | #include <rdma/ib_sa.h> | |
07ebafba | 54 | #include <rdma/iw_cm.h> |
e51060f0 SH |
55 | |
56 | MODULE_AUTHOR("Sean Hefty"); | |
57 | MODULE_DESCRIPTION("Generic RDMA CM Agent"); | |
58 | MODULE_LICENSE("Dual BSD/GPL"); | |
59 | ||
60 | #define CMA_CM_RESPONSE_TIMEOUT 20 | |
d5bb7599 | 61 | #define CMA_MAX_CM_RETRIES 15 |
dcb3f974 | 62 | #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24) |
3c86aa70 | 63 | #define CMA_IBOE_PACKET_LIFETIME 18 |
e51060f0 SH |
64 | |
65 | static void cma_add_one(struct ib_device *device); | |
66 | static void cma_remove_one(struct ib_device *device); | |
67 | ||
68 | static struct ib_client cma_client = { | |
69 | .name = "cma", | |
70 | .add = cma_add_one, | |
71 | .remove = cma_remove_one | |
72 | }; | |
73 | ||
c1a0b23b | 74 | static struct ib_sa_client sa_client; |
7a118df3 | 75 | static struct rdma_addr_client addr_client; |
e51060f0 SH |
76 | static LIST_HEAD(dev_list); |
77 | static LIST_HEAD(listen_any_list); | |
78 | static DEFINE_MUTEX(lock); | |
79 | static struct workqueue_struct *cma_wq; | |
80 | static DEFINE_IDR(sdp_ps); | |
81 | static DEFINE_IDR(tcp_ps); | |
628e5f6d | 82 | static DEFINE_IDR(udp_ps); |
c8f6a362 | 83 | static DEFINE_IDR(ipoib_ps); |
e51060f0 SH |
84 | |
85 | struct cma_device { | |
86 | struct list_head list; | |
87 | struct ib_device *device; | |
e51060f0 SH |
88 | struct completion comp; |
89 | atomic_t refcount; | |
90 | struct list_head id_list; | |
91 | }; | |
92 | ||
e51060f0 SH |
93 | struct rdma_bind_list { |
94 | struct idr *ps; | |
95 | struct hlist_head owners; | |
96 | unsigned short port; | |
97 | }; | |
98 | ||
99 | /* | |
100 | * Device removal can occur at anytime, so we need extra handling to | |
101 | * serialize notifying the user of device removal with other callbacks. | |
102 | * We do this by disabling removal notification while a callback is in process, | |
103 | * and reporting it after the callback completes. | |
104 | */ | |
105 | struct rdma_id_private { | |
106 | struct rdma_cm_id id; | |
107 | ||
108 | struct rdma_bind_list *bind_list; | |
109 | struct hlist_node node; | |
d02d1f53 SH |
110 | struct list_head list; /* listen_any_list or cma_device.list */ |
111 | struct list_head listen_list; /* per device listens */ | |
e51060f0 | 112 | struct cma_device *cma_dev; |
c8f6a362 | 113 | struct list_head mc_list; |
e51060f0 | 114 | |
d02d1f53 | 115 | int internal_id; |
550e5ca7 | 116 | enum rdma_cm_state state; |
e51060f0 | 117 | spinlock_t lock; |
c5483388 SH |
118 | struct mutex qp_mutex; |
119 | ||
e51060f0 SH |
120 | struct completion comp; |
121 | atomic_t refcount; | |
de910bd9 | 122 | struct mutex handler_mutex; |
e51060f0 SH |
123 | |
124 | int backlog; | |
125 | int timeout_ms; | |
126 | struct ib_sa_query *query; | |
127 | int query_id; | |
128 | union { | |
129 | struct ib_cm_id *ib; | |
07ebafba | 130 | struct iw_cm_id *iw; |
e51060f0 SH |
131 | } cm_id; |
132 | ||
133 | u32 seq_num; | |
c8f6a362 | 134 | u32 qkey; |
e51060f0 | 135 | u32 qp_num; |
83e9502d | 136 | pid_t owner; |
e51060f0 | 137 | u8 srq; |
a81c994d | 138 | u8 tos; |
a9bb7912 | 139 | u8 reuseaddr; |
e51060f0 SH |
140 | }; |
141 | ||
c8f6a362 SH |
142 | struct cma_multicast { |
143 | struct rdma_id_private *id_priv; | |
144 | union { | |
145 | struct ib_sa_multicast *ib; | |
146 | } multicast; | |
147 | struct list_head list; | |
148 | void *context; | |
3f446754 | 149 | struct sockaddr_storage addr; |
3c86aa70 | 150 | struct kref mcref; |
c8f6a362 SH |
151 | }; |
152 | ||
e51060f0 SH |
153 | struct cma_work { |
154 | struct work_struct work; | |
155 | struct rdma_id_private *id; | |
550e5ca7 NM |
156 | enum rdma_cm_state old_state; |
157 | enum rdma_cm_state new_state; | |
e51060f0 SH |
158 | struct rdma_cm_event event; |
159 | }; | |
160 | ||
dd5bdff8 OG |
161 | struct cma_ndev_work { |
162 | struct work_struct work; | |
163 | struct rdma_id_private *id; | |
164 | struct rdma_cm_event event; | |
165 | }; | |
166 | ||
3c86aa70 EC |
167 | struct iboe_mcast_work { |
168 | struct work_struct work; | |
169 | struct rdma_id_private *id; | |
170 | struct cma_multicast *mc; | |
171 | }; | |
172 | ||
e51060f0 SH |
173 | union cma_ip_addr { |
174 | struct in6_addr ip6; | |
175 | struct { | |
1b90c137 AV |
176 | __be32 pad[3]; |
177 | __be32 addr; | |
e51060f0 SH |
178 | } ip4; |
179 | }; | |
180 | ||
181 | struct cma_hdr { | |
182 | u8 cma_version; | |
183 | u8 ip_version; /* IP version: 7:4 */ | |
1b90c137 | 184 | __be16 port; |
e51060f0 SH |
185 | union cma_ip_addr src_addr; |
186 | union cma_ip_addr dst_addr; | |
187 | }; | |
188 | ||
189 | struct sdp_hh { | |
190 | u8 bsdh[16]; | |
191 | u8 sdp_version; /* Major version: 7:4 */ | |
192 | u8 ip_version; /* IP version: 7:4 */ | |
193 | u8 sdp_specific1[10]; | |
1b90c137 AV |
194 | __be16 port; |
195 | __be16 sdp_specific2; | |
e51060f0 SH |
196 | union cma_ip_addr src_addr; |
197 | union cma_ip_addr dst_addr; | |
198 | }; | |
199 | ||
200 | struct sdp_hah { | |
201 | u8 bsdh[16]; | |
202 | u8 sdp_version; | |
203 | }; | |
204 | ||
205 | #define CMA_VERSION 0x00 | |
206 | #define SDP_MAJ_VERSION 0x2 | |
207 | ||
550e5ca7 | 208 | static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp) |
e51060f0 SH |
209 | { |
210 | unsigned long flags; | |
211 | int ret; | |
212 | ||
213 | spin_lock_irqsave(&id_priv->lock, flags); | |
214 | ret = (id_priv->state == comp); | |
215 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
216 | return ret; | |
217 | } | |
218 | ||
219 | static int cma_comp_exch(struct rdma_id_private *id_priv, | |
550e5ca7 | 220 | enum rdma_cm_state comp, enum rdma_cm_state exch) |
e51060f0 SH |
221 | { |
222 | unsigned long flags; | |
223 | int ret; | |
224 | ||
225 | spin_lock_irqsave(&id_priv->lock, flags); | |
226 | if ((ret = (id_priv->state == comp))) | |
227 | id_priv->state = exch; | |
228 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
229 | return ret; | |
230 | } | |
231 | ||
550e5ca7 NM |
232 | static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv, |
233 | enum rdma_cm_state exch) | |
e51060f0 SH |
234 | { |
235 | unsigned long flags; | |
550e5ca7 | 236 | enum rdma_cm_state old; |
e51060f0 SH |
237 | |
238 | spin_lock_irqsave(&id_priv->lock, flags); | |
239 | old = id_priv->state; | |
240 | id_priv->state = exch; | |
241 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
242 | return old; | |
243 | } | |
244 | ||
245 | static inline u8 cma_get_ip_ver(struct cma_hdr *hdr) | |
246 | { | |
247 | return hdr->ip_version >> 4; | |
248 | } | |
249 | ||
250 | static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver) | |
251 | { | |
252 | hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF); | |
253 | } | |
254 | ||
255 | static inline u8 sdp_get_majv(u8 sdp_version) | |
256 | { | |
257 | return sdp_version >> 4; | |
258 | } | |
259 | ||
260 | static inline u8 sdp_get_ip_ver(struct sdp_hh *hh) | |
261 | { | |
262 | return hh->ip_version >> 4; | |
263 | } | |
264 | ||
265 | static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver) | |
266 | { | |
267 | hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF); | |
268 | } | |
269 | ||
270 | static void cma_attach_to_dev(struct rdma_id_private *id_priv, | |
271 | struct cma_device *cma_dev) | |
272 | { | |
273 | atomic_inc(&cma_dev->refcount); | |
274 | id_priv->cma_dev = cma_dev; | |
275 | id_priv->id.device = cma_dev->device; | |
3c86aa70 EC |
276 | id_priv->id.route.addr.dev_addr.transport = |
277 | rdma_node_get_transport(cma_dev->device->node_type); | |
e51060f0 SH |
278 | list_add_tail(&id_priv->list, &cma_dev->id_list); |
279 | } | |
280 | ||
281 | static inline void cma_deref_dev(struct cma_device *cma_dev) | |
282 | { | |
283 | if (atomic_dec_and_test(&cma_dev->refcount)) | |
284 | complete(&cma_dev->comp); | |
285 | } | |
286 | ||
3c86aa70 EC |
287 | static inline void release_mc(struct kref *kref) |
288 | { | |
289 | struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref); | |
290 | ||
291 | kfree(mc->multicast.ib); | |
292 | kfree(mc); | |
293 | } | |
294 | ||
a396d43a | 295 | static void cma_release_dev(struct rdma_id_private *id_priv) |
e51060f0 | 296 | { |
a396d43a | 297 | mutex_lock(&lock); |
e51060f0 SH |
298 | list_del(&id_priv->list); |
299 | cma_deref_dev(id_priv->cma_dev); | |
300 | id_priv->cma_dev = NULL; | |
a396d43a | 301 | mutex_unlock(&lock); |
e51060f0 SH |
302 | } |
303 | ||
d2ca39f2 | 304 | static int cma_set_qkey(struct rdma_id_private *id_priv) |
c8f6a362 SH |
305 | { |
306 | struct ib_sa_mcmember_rec rec; | |
307 | int ret = 0; | |
308 | ||
d2ca39f2 YE |
309 | if (id_priv->qkey) |
310 | return 0; | |
311 | ||
312 | switch (id_priv->id.ps) { | |
c8f6a362 | 313 | case RDMA_PS_UDP: |
d2ca39f2 | 314 | id_priv->qkey = RDMA_UDP_QKEY; |
c8f6a362 SH |
315 | break; |
316 | case RDMA_PS_IPOIB: | |
d2ca39f2 YE |
317 | ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid); |
318 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, | |
319 | id_priv->id.port_num, &rec.mgid, | |
320 | &rec); | |
321 | if (!ret) | |
322 | id_priv->qkey = be32_to_cpu(rec.qkey); | |
c8f6a362 SH |
323 | break; |
324 | default: | |
325 | break; | |
326 | } | |
327 | return ret; | |
328 | } | |
329 | ||
3c86aa70 EC |
330 | static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num) |
331 | { | |
332 | int i; | |
333 | int err; | |
334 | struct ib_port_attr props; | |
335 | union ib_gid tmp; | |
336 | ||
337 | err = ib_query_port(device, port_num, &props); | |
338 | if (err) | |
339 | return 1; | |
340 | ||
341 | for (i = 0; i < props.gid_tbl_len; ++i) { | |
342 | err = ib_query_gid(device, port_num, i, &tmp); | |
343 | if (err) | |
344 | return 1; | |
345 | if (!memcmp(&tmp, gid, sizeof tmp)) | |
346 | return 0; | |
347 | } | |
348 | ||
349 | return -EAGAIN; | |
350 | } | |
351 | ||
07ebafba | 352 | static int cma_acquire_dev(struct rdma_id_private *id_priv) |
e51060f0 | 353 | { |
c8f6a362 | 354 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
e51060f0 | 355 | struct cma_device *cma_dev; |
3c86aa70 | 356 | union ib_gid gid, iboe_gid; |
e51060f0 | 357 | int ret = -ENODEV; |
3c86aa70 EC |
358 | u8 port; |
359 | enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ? | |
360 | IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET; | |
e51060f0 | 361 | |
a396d43a | 362 | mutex_lock(&lock); |
3c86aa70 EC |
363 | iboe_addr_get_sgid(dev_addr, &iboe_gid); |
364 | memcpy(&gid, dev_addr->src_dev_addr + | |
365 | rdma_addr_gid_offset(dev_addr), sizeof gid); | |
e51060f0 | 366 | list_for_each_entry(cma_dev, &dev_list, list) { |
3c86aa70 EC |
367 | for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) { |
368 | if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) { | |
369 | if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB && | |
370 | rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET) | |
371 | ret = find_gid_port(cma_dev->device, &iboe_gid, port); | |
372 | else | |
373 | ret = find_gid_port(cma_dev->device, &gid, port); | |
374 | ||
375 | if (!ret) { | |
376 | id_priv->id.port_num = port; | |
377 | goto out; | |
378 | } else if (ret == 1) | |
379 | break; | |
380 | } | |
e51060f0 SH |
381 | } |
382 | } | |
3c86aa70 EC |
383 | |
384 | out: | |
385 | if (!ret) | |
386 | cma_attach_to_dev(id_priv, cma_dev); | |
387 | ||
a396d43a | 388 | mutex_unlock(&lock); |
e51060f0 SH |
389 | return ret; |
390 | } | |
391 | ||
e51060f0 SH |
392 | static void cma_deref_id(struct rdma_id_private *id_priv) |
393 | { | |
394 | if (atomic_dec_and_test(&id_priv->refcount)) | |
395 | complete(&id_priv->comp); | |
396 | } | |
397 | ||
de910bd9 | 398 | static int cma_disable_callback(struct rdma_id_private *id_priv, |
550e5ca7 | 399 | enum rdma_cm_state state) |
8aa08602 | 400 | { |
de910bd9 OG |
401 | mutex_lock(&id_priv->handler_mutex); |
402 | if (id_priv->state != state) { | |
403 | mutex_unlock(&id_priv->handler_mutex); | |
404 | return -EINVAL; | |
405 | } | |
406 | return 0; | |
e51060f0 SH |
407 | } |
408 | ||
409 | struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler, | |
b26f9b99 SH |
410 | void *context, enum rdma_port_space ps, |
411 | enum ib_qp_type qp_type) | |
e51060f0 SH |
412 | { |
413 | struct rdma_id_private *id_priv; | |
414 | ||
415 | id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL); | |
416 | if (!id_priv) | |
417 | return ERR_PTR(-ENOMEM); | |
418 | ||
83e9502d | 419 | id_priv->owner = task_pid_nr(current); |
550e5ca7 | 420 | id_priv->state = RDMA_CM_IDLE; |
e51060f0 SH |
421 | id_priv->id.context = context; |
422 | id_priv->id.event_handler = event_handler; | |
423 | id_priv->id.ps = ps; | |
b26f9b99 | 424 | id_priv->id.qp_type = qp_type; |
e51060f0 | 425 | spin_lock_init(&id_priv->lock); |
c5483388 | 426 | mutex_init(&id_priv->qp_mutex); |
e51060f0 SH |
427 | init_completion(&id_priv->comp); |
428 | atomic_set(&id_priv->refcount, 1); | |
de910bd9 | 429 | mutex_init(&id_priv->handler_mutex); |
e51060f0 | 430 | INIT_LIST_HEAD(&id_priv->listen_list); |
c8f6a362 | 431 | INIT_LIST_HEAD(&id_priv->mc_list); |
e51060f0 SH |
432 | get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num); |
433 | ||
434 | return &id_priv->id; | |
435 | } | |
436 | EXPORT_SYMBOL(rdma_create_id); | |
437 | ||
c8f6a362 | 438 | static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
e51060f0 SH |
439 | { |
440 | struct ib_qp_attr qp_attr; | |
c8f6a362 | 441 | int qp_attr_mask, ret; |
e51060f0 | 442 | |
c8f6a362 SH |
443 | qp_attr.qp_state = IB_QPS_INIT; |
444 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); | |
e51060f0 SH |
445 | if (ret) |
446 | return ret; | |
447 | ||
c8f6a362 SH |
448 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); |
449 | if (ret) | |
450 | return ret; | |
451 | ||
452 | qp_attr.qp_state = IB_QPS_RTR; | |
453 | ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE); | |
454 | if (ret) | |
455 | return ret; | |
456 | ||
457 | qp_attr.qp_state = IB_QPS_RTS; | |
458 | qp_attr.sq_psn = 0; | |
459 | ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN); | |
460 | ||
461 | return ret; | |
e51060f0 SH |
462 | } |
463 | ||
c8f6a362 | 464 | static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
07ebafba TT |
465 | { |
466 | struct ib_qp_attr qp_attr; | |
c8f6a362 | 467 | int qp_attr_mask, ret; |
07ebafba TT |
468 | |
469 | qp_attr.qp_state = IB_QPS_INIT; | |
c8f6a362 SH |
470 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
471 | if (ret) | |
472 | return ret; | |
07ebafba | 473 | |
c8f6a362 | 474 | return ib_modify_qp(qp, &qp_attr, qp_attr_mask); |
07ebafba TT |
475 | } |
476 | ||
e51060f0 SH |
477 | int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, |
478 | struct ib_qp_init_attr *qp_init_attr) | |
479 | { | |
480 | struct rdma_id_private *id_priv; | |
481 | struct ib_qp *qp; | |
482 | int ret; | |
483 | ||
484 | id_priv = container_of(id, struct rdma_id_private, id); | |
485 | if (id->device != pd->device) | |
486 | return -EINVAL; | |
487 | ||
488 | qp = ib_create_qp(pd, qp_init_attr); | |
489 | if (IS_ERR(qp)) | |
490 | return PTR_ERR(qp); | |
491 | ||
b26f9b99 | 492 | if (id->qp_type == IB_QPT_UD) |
c8f6a362 SH |
493 | ret = cma_init_ud_qp(id_priv, qp); |
494 | else | |
495 | ret = cma_init_conn_qp(id_priv, qp); | |
e51060f0 SH |
496 | if (ret) |
497 | goto err; | |
498 | ||
499 | id->qp = qp; | |
500 | id_priv->qp_num = qp->qp_num; | |
e51060f0 SH |
501 | id_priv->srq = (qp->srq != NULL); |
502 | return 0; | |
503 | err: | |
504 | ib_destroy_qp(qp); | |
505 | return ret; | |
506 | } | |
507 | EXPORT_SYMBOL(rdma_create_qp); | |
508 | ||
509 | void rdma_destroy_qp(struct rdma_cm_id *id) | |
510 | { | |
c5483388 SH |
511 | struct rdma_id_private *id_priv; |
512 | ||
513 | id_priv = container_of(id, struct rdma_id_private, id); | |
514 | mutex_lock(&id_priv->qp_mutex); | |
515 | ib_destroy_qp(id_priv->id.qp); | |
516 | id_priv->id.qp = NULL; | |
517 | mutex_unlock(&id_priv->qp_mutex); | |
e51060f0 SH |
518 | } |
519 | EXPORT_SYMBOL(rdma_destroy_qp); | |
520 | ||
5851bb89 SH |
521 | static int cma_modify_qp_rtr(struct rdma_id_private *id_priv, |
522 | struct rdma_conn_param *conn_param) | |
e51060f0 SH |
523 | { |
524 | struct ib_qp_attr qp_attr; | |
525 | int qp_attr_mask, ret; | |
526 | ||
c5483388 SH |
527 | mutex_lock(&id_priv->qp_mutex); |
528 | if (!id_priv->id.qp) { | |
529 | ret = 0; | |
530 | goto out; | |
531 | } | |
e51060f0 SH |
532 | |
533 | /* Need to update QP attributes from default values. */ | |
534 | qp_attr.qp_state = IB_QPS_INIT; | |
c5483388 | 535 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
e51060f0 | 536 | if (ret) |
c5483388 | 537 | goto out; |
e51060f0 | 538 | |
c5483388 | 539 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
e51060f0 | 540 | if (ret) |
c5483388 | 541 | goto out; |
e51060f0 SH |
542 | |
543 | qp_attr.qp_state = IB_QPS_RTR; | |
c5483388 | 544 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
e51060f0 | 545 | if (ret) |
c5483388 | 546 | goto out; |
e51060f0 | 547 | |
5851bb89 SH |
548 | if (conn_param) |
549 | qp_attr.max_dest_rd_atomic = conn_param->responder_resources; | |
c5483388 SH |
550 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
551 | out: | |
552 | mutex_unlock(&id_priv->qp_mutex); | |
553 | return ret; | |
e51060f0 SH |
554 | } |
555 | ||
5851bb89 SH |
556 | static int cma_modify_qp_rts(struct rdma_id_private *id_priv, |
557 | struct rdma_conn_param *conn_param) | |
e51060f0 SH |
558 | { |
559 | struct ib_qp_attr qp_attr; | |
560 | int qp_attr_mask, ret; | |
561 | ||
c5483388 SH |
562 | mutex_lock(&id_priv->qp_mutex); |
563 | if (!id_priv->id.qp) { | |
564 | ret = 0; | |
565 | goto out; | |
566 | } | |
e51060f0 SH |
567 | |
568 | qp_attr.qp_state = IB_QPS_RTS; | |
c5483388 | 569 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
e51060f0 | 570 | if (ret) |
c5483388 | 571 | goto out; |
e51060f0 | 572 | |
5851bb89 SH |
573 | if (conn_param) |
574 | qp_attr.max_rd_atomic = conn_param->initiator_depth; | |
c5483388 SH |
575 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
576 | out: | |
577 | mutex_unlock(&id_priv->qp_mutex); | |
578 | return ret; | |
e51060f0 SH |
579 | } |
580 | ||
c5483388 | 581 | static int cma_modify_qp_err(struct rdma_id_private *id_priv) |
e51060f0 SH |
582 | { |
583 | struct ib_qp_attr qp_attr; | |
c5483388 | 584 | int ret; |
e51060f0 | 585 | |
c5483388 SH |
586 | mutex_lock(&id_priv->qp_mutex); |
587 | if (!id_priv->id.qp) { | |
588 | ret = 0; | |
589 | goto out; | |
590 | } | |
e51060f0 SH |
591 | |
592 | qp_attr.qp_state = IB_QPS_ERR; | |
c5483388 SH |
593 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE); |
594 | out: | |
595 | mutex_unlock(&id_priv->qp_mutex); | |
596 | return ret; | |
e51060f0 SH |
597 | } |
598 | ||
c8f6a362 SH |
599 | static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv, |
600 | struct ib_qp_attr *qp_attr, int *qp_attr_mask) | |
601 | { | |
602 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
603 | int ret; | |
3c86aa70 EC |
604 | u16 pkey; |
605 | ||
606 | if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) == | |
607 | IB_LINK_LAYER_INFINIBAND) | |
608 | pkey = ib_addr_get_pkey(dev_addr); | |
609 | else | |
610 | pkey = 0xffff; | |
c8f6a362 SH |
611 | |
612 | ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num, | |
3c86aa70 | 613 | pkey, &qp_attr->pkey_index); |
c8f6a362 SH |
614 | if (ret) |
615 | return ret; | |
616 | ||
617 | qp_attr->port_num = id_priv->id.port_num; | |
618 | *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT; | |
619 | ||
b26f9b99 | 620 | if (id_priv->id.qp_type == IB_QPT_UD) { |
d2ca39f2 YE |
621 | ret = cma_set_qkey(id_priv); |
622 | if (ret) | |
623 | return ret; | |
624 | ||
c8f6a362 SH |
625 | qp_attr->qkey = id_priv->qkey; |
626 | *qp_attr_mask |= IB_QP_QKEY; | |
627 | } else { | |
628 | qp_attr->qp_access_flags = 0; | |
629 | *qp_attr_mask |= IB_QP_ACCESS_FLAGS; | |
630 | } | |
631 | return 0; | |
632 | } | |
633 | ||
e51060f0 SH |
634 | int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, |
635 | int *qp_attr_mask) | |
636 | { | |
637 | struct rdma_id_private *id_priv; | |
c8f6a362 | 638 | int ret = 0; |
e51060f0 SH |
639 | |
640 | id_priv = container_of(id, struct rdma_id_private, id); | |
07ebafba TT |
641 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
642 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 643 | if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD)) |
c8f6a362 SH |
644 | ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask); |
645 | else | |
646 | ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr, | |
647 | qp_attr_mask); | |
e51060f0 SH |
648 | if (qp_attr->qp_state == IB_QPS_RTR) |
649 | qp_attr->rq_psn = id_priv->seq_num; | |
650 | break; | |
07ebafba | 651 | case RDMA_TRANSPORT_IWARP: |
c8f6a362 | 652 | if (!id_priv->cm_id.iw) { |
8f076531 | 653 | qp_attr->qp_access_flags = 0; |
c8f6a362 SH |
654 | *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS; |
655 | } else | |
656 | ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr, | |
657 | qp_attr_mask); | |
07ebafba | 658 | break; |
e51060f0 SH |
659 | default: |
660 | ret = -ENOSYS; | |
661 | break; | |
662 | } | |
663 | ||
664 | return ret; | |
665 | } | |
666 | EXPORT_SYMBOL(rdma_init_qp_attr); | |
667 | ||
668 | static inline int cma_zero_addr(struct sockaddr *addr) | |
669 | { | |
670 | struct in6_addr *ip6; | |
671 | ||
672 | if (addr->sa_family == AF_INET) | |
6360a02a JP |
673 | return ipv4_is_zeronet( |
674 | ((struct sockaddr_in *)addr)->sin_addr.s_addr); | |
e51060f0 SH |
675 | else { |
676 | ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr; | |
677 | return (ip6->s6_addr32[0] | ip6->s6_addr32[1] | | |
5fd571cb | 678 | ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0; |
e51060f0 SH |
679 | } |
680 | } | |
681 | ||
682 | static inline int cma_loopback_addr(struct sockaddr *addr) | |
683 | { | |
1f5175ad AS |
684 | if (addr->sa_family == AF_INET) |
685 | return ipv4_is_loopback( | |
686 | ((struct sockaddr_in *) addr)->sin_addr.s_addr); | |
687 | else | |
688 | return ipv6_addr_loopback( | |
689 | &((struct sockaddr_in6 *) addr)->sin6_addr); | |
e51060f0 SH |
690 | } |
691 | ||
692 | static inline int cma_any_addr(struct sockaddr *addr) | |
693 | { | |
694 | return cma_zero_addr(addr) || cma_loopback_addr(addr); | |
695 | } | |
696 | ||
43b752da HS |
697 | static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst) |
698 | { | |
699 | if (src->sa_family != dst->sa_family) | |
700 | return -1; | |
701 | ||
702 | switch (src->sa_family) { | |
703 | case AF_INET: | |
704 | return ((struct sockaddr_in *) src)->sin_addr.s_addr != | |
705 | ((struct sockaddr_in *) dst)->sin_addr.s_addr; | |
706 | default: | |
707 | return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr, | |
708 | &((struct sockaddr_in6 *) dst)->sin6_addr); | |
709 | } | |
710 | } | |
711 | ||
628e5f6d SH |
712 | static inline __be16 cma_port(struct sockaddr *addr) |
713 | { | |
714 | if (addr->sa_family == AF_INET) | |
715 | return ((struct sockaddr_in *) addr)->sin_port; | |
716 | else | |
717 | return ((struct sockaddr_in6 *) addr)->sin6_port; | |
718 | } | |
719 | ||
e51060f0 SH |
720 | static inline int cma_any_port(struct sockaddr *addr) |
721 | { | |
628e5f6d | 722 | return !cma_port(addr); |
e51060f0 SH |
723 | } |
724 | ||
725 | static int cma_get_net_info(void *hdr, enum rdma_port_space ps, | |
1b90c137 | 726 | u8 *ip_ver, __be16 *port, |
e51060f0 SH |
727 | union cma_ip_addr **src, union cma_ip_addr **dst) |
728 | { | |
729 | switch (ps) { | |
730 | case RDMA_PS_SDP: | |
731 | if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) != | |
732 | SDP_MAJ_VERSION) | |
733 | return -EINVAL; | |
734 | ||
735 | *ip_ver = sdp_get_ip_ver(hdr); | |
736 | *port = ((struct sdp_hh *) hdr)->port; | |
737 | *src = &((struct sdp_hh *) hdr)->src_addr; | |
738 | *dst = &((struct sdp_hh *) hdr)->dst_addr; | |
739 | break; | |
740 | default: | |
741 | if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION) | |
742 | return -EINVAL; | |
743 | ||
744 | *ip_ver = cma_get_ip_ver(hdr); | |
745 | *port = ((struct cma_hdr *) hdr)->port; | |
746 | *src = &((struct cma_hdr *) hdr)->src_addr; | |
747 | *dst = &((struct cma_hdr *) hdr)->dst_addr; | |
748 | break; | |
749 | } | |
750 | ||
751 | if (*ip_ver != 4 && *ip_ver != 6) | |
752 | return -EINVAL; | |
753 | return 0; | |
754 | } | |
755 | ||
756 | static void cma_save_net_info(struct rdma_addr *addr, | |
757 | struct rdma_addr *listen_addr, | |
1b90c137 | 758 | u8 ip_ver, __be16 port, |
e51060f0 SH |
759 | union cma_ip_addr *src, union cma_ip_addr *dst) |
760 | { | |
761 | struct sockaddr_in *listen4, *ip4; | |
762 | struct sockaddr_in6 *listen6, *ip6; | |
763 | ||
764 | switch (ip_ver) { | |
765 | case 4: | |
766 | listen4 = (struct sockaddr_in *) &listen_addr->src_addr; | |
767 | ip4 = (struct sockaddr_in *) &addr->src_addr; | |
768 | ip4->sin_family = listen4->sin_family; | |
769 | ip4->sin_addr.s_addr = dst->ip4.addr; | |
770 | ip4->sin_port = listen4->sin_port; | |
771 | ||
772 | ip4 = (struct sockaddr_in *) &addr->dst_addr; | |
773 | ip4->sin_family = listen4->sin_family; | |
774 | ip4->sin_addr.s_addr = src->ip4.addr; | |
775 | ip4->sin_port = port; | |
776 | break; | |
777 | case 6: | |
778 | listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr; | |
779 | ip6 = (struct sockaddr_in6 *) &addr->src_addr; | |
780 | ip6->sin6_family = listen6->sin6_family; | |
781 | ip6->sin6_addr = dst->ip6; | |
782 | ip6->sin6_port = listen6->sin6_port; | |
783 | ||
784 | ip6 = (struct sockaddr_in6 *) &addr->dst_addr; | |
785 | ip6->sin6_family = listen6->sin6_family; | |
786 | ip6->sin6_addr = src->ip6; | |
787 | ip6->sin6_port = port; | |
788 | break; | |
789 | default: | |
790 | break; | |
791 | } | |
792 | } | |
793 | ||
794 | static inline int cma_user_data_offset(enum rdma_port_space ps) | |
795 | { | |
796 | switch (ps) { | |
797 | case RDMA_PS_SDP: | |
798 | return 0; | |
799 | default: | |
800 | return sizeof(struct cma_hdr); | |
801 | } | |
802 | } | |
803 | ||
e51060f0 SH |
804 | static void cma_cancel_route(struct rdma_id_private *id_priv) |
805 | { | |
3c86aa70 EC |
806 | switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) { |
807 | case IB_LINK_LAYER_INFINIBAND: | |
e51060f0 SH |
808 | if (id_priv->query) |
809 | ib_sa_cancel_query(id_priv->query_id, id_priv->query); | |
810 | break; | |
811 | default: | |
812 | break; | |
813 | } | |
814 | } | |
815 | ||
e51060f0 SH |
816 | static void cma_cancel_listens(struct rdma_id_private *id_priv) |
817 | { | |
818 | struct rdma_id_private *dev_id_priv; | |
819 | ||
d02d1f53 SH |
820 | /* |
821 | * Remove from listen_any_list to prevent added devices from spawning | |
822 | * additional listen requests. | |
823 | */ | |
e51060f0 SH |
824 | mutex_lock(&lock); |
825 | list_del(&id_priv->list); | |
826 | ||
827 | while (!list_empty(&id_priv->listen_list)) { | |
828 | dev_id_priv = list_entry(id_priv->listen_list.next, | |
829 | struct rdma_id_private, listen_list); | |
d02d1f53 SH |
830 | /* sync with device removal to avoid duplicate destruction */ |
831 | list_del_init(&dev_id_priv->list); | |
832 | list_del(&dev_id_priv->listen_list); | |
833 | mutex_unlock(&lock); | |
834 | ||
835 | rdma_destroy_id(&dev_id_priv->id); | |
836 | mutex_lock(&lock); | |
e51060f0 SH |
837 | } |
838 | mutex_unlock(&lock); | |
839 | } | |
840 | ||
841 | static void cma_cancel_operation(struct rdma_id_private *id_priv, | |
550e5ca7 | 842 | enum rdma_cm_state state) |
e51060f0 SH |
843 | { |
844 | switch (state) { | |
550e5ca7 | 845 | case RDMA_CM_ADDR_QUERY: |
e51060f0 SH |
846 | rdma_addr_cancel(&id_priv->id.route.addr.dev_addr); |
847 | break; | |
550e5ca7 | 848 | case RDMA_CM_ROUTE_QUERY: |
e51060f0 SH |
849 | cma_cancel_route(id_priv); |
850 | break; | |
550e5ca7 | 851 | case RDMA_CM_LISTEN: |
3f446754 RD |
852 | if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr) |
853 | && !id_priv->cma_dev) | |
e51060f0 SH |
854 | cma_cancel_listens(id_priv); |
855 | break; | |
856 | default: | |
857 | break; | |
858 | } | |
859 | } | |
860 | ||
861 | static void cma_release_port(struct rdma_id_private *id_priv) | |
862 | { | |
863 | struct rdma_bind_list *bind_list = id_priv->bind_list; | |
864 | ||
865 | if (!bind_list) | |
866 | return; | |
867 | ||
868 | mutex_lock(&lock); | |
869 | hlist_del(&id_priv->node); | |
870 | if (hlist_empty(&bind_list->owners)) { | |
871 | idr_remove(bind_list->ps, bind_list->port); | |
872 | kfree(bind_list); | |
873 | } | |
874 | mutex_unlock(&lock); | |
875 | } | |
876 | ||
c8f6a362 SH |
877 | static void cma_leave_mc_groups(struct rdma_id_private *id_priv) |
878 | { | |
879 | struct cma_multicast *mc; | |
880 | ||
881 | while (!list_empty(&id_priv->mc_list)) { | |
882 | mc = container_of(id_priv->mc_list.next, | |
883 | struct cma_multicast, list); | |
884 | list_del(&mc->list); | |
3c86aa70 EC |
885 | switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) { |
886 | case IB_LINK_LAYER_INFINIBAND: | |
887 | ib_sa_free_multicast(mc->multicast.ib); | |
888 | kfree(mc); | |
889 | break; | |
890 | case IB_LINK_LAYER_ETHERNET: | |
891 | kref_put(&mc->mcref, release_mc); | |
892 | break; | |
893 | default: | |
894 | break; | |
895 | } | |
c8f6a362 SH |
896 | } |
897 | } | |
898 | ||
e51060f0 SH |
899 | void rdma_destroy_id(struct rdma_cm_id *id) |
900 | { | |
901 | struct rdma_id_private *id_priv; | |
550e5ca7 | 902 | enum rdma_cm_state state; |
e51060f0 SH |
903 | |
904 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 905 | state = cma_exch(id_priv, RDMA_CM_DESTROYING); |
e51060f0 SH |
906 | cma_cancel_operation(id_priv, state); |
907 | ||
a396d43a SH |
908 | /* |
909 | * Wait for any active callback to finish. New callbacks will find | |
910 | * the id_priv state set to destroying and abort. | |
911 | */ | |
912 | mutex_lock(&id_priv->handler_mutex); | |
913 | mutex_unlock(&id_priv->handler_mutex); | |
914 | ||
e51060f0 | 915 | if (id_priv->cma_dev) { |
3c86aa70 | 916 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
07ebafba | 917 | case RDMA_TRANSPORT_IB: |
0c9361fc | 918 | if (id_priv->cm_id.ib) |
e51060f0 SH |
919 | ib_destroy_cm_id(id_priv->cm_id.ib); |
920 | break; | |
07ebafba | 921 | case RDMA_TRANSPORT_IWARP: |
0c9361fc | 922 | if (id_priv->cm_id.iw) |
07ebafba TT |
923 | iw_destroy_cm_id(id_priv->cm_id.iw); |
924 | break; | |
e51060f0 SH |
925 | default: |
926 | break; | |
927 | } | |
c8f6a362 | 928 | cma_leave_mc_groups(id_priv); |
a396d43a | 929 | cma_release_dev(id_priv); |
e51060f0 SH |
930 | } |
931 | ||
932 | cma_release_port(id_priv); | |
933 | cma_deref_id(id_priv); | |
934 | wait_for_completion(&id_priv->comp); | |
935 | ||
d02d1f53 SH |
936 | if (id_priv->internal_id) |
937 | cma_deref_id(id_priv->id.context); | |
938 | ||
e51060f0 SH |
939 | kfree(id_priv->id.route.path_rec); |
940 | kfree(id_priv); | |
941 | } | |
942 | EXPORT_SYMBOL(rdma_destroy_id); | |
943 | ||
944 | static int cma_rep_recv(struct rdma_id_private *id_priv) | |
945 | { | |
946 | int ret; | |
947 | ||
5851bb89 | 948 | ret = cma_modify_qp_rtr(id_priv, NULL); |
e51060f0 SH |
949 | if (ret) |
950 | goto reject; | |
951 | ||
5851bb89 | 952 | ret = cma_modify_qp_rts(id_priv, NULL); |
e51060f0 SH |
953 | if (ret) |
954 | goto reject; | |
955 | ||
956 | ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0); | |
957 | if (ret) | |
958 | goto reject; | |
959 | ||
960 | return 0; | |
961 | reject: | |
c5483388 | 962 | cma_modify_qp_err(id_priv); |
e51060f0 SH |
963 | ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, |
964 | NULL, 0, NULL, 0); | |
965 | return ret; | |
966 | } | |
967 | ||
968 | static int cma_verify_rep(struct rdma_id_private *id_priv, void *data) | |
969 | { | |
970 | if (id_priv->id.ps == RDMA_PS_SDP && | |
971 | sdp_get_majv(((struct sdp_hah *) data)->sdp_version) != | |
972 | SDP_MAJ_VERSION) | |
973 | return -EINVAL; | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
a1b1b61f SH |
978 | static void cma_set_rep_event_data(struct rdma_cm_event *event, |
979 | struct ib_cm_rep_event_param *rep_data, | |
980 | void *private_data) | |
981 | { | |
982 | event->param.conn.private_data = private_data; | |
983 | event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE; | |
984 | event->param.conn.responder_resources = rep_data->responder_resources; | |
985 | event->param.conn.initiator_depth = rep_data->initiator_depth; | |
986 | event->param.conn.flow_control = rep_data->flow_control; | |
987 | event->param.conn.rnr_retry_count = rep_data->rnr_retry_count; | |
988 | event->param.conn.srq = rep_data->srq; | |
989 | event->param.conn.qp_num = rep_data->remote_qpn; | |
990 | } | |
991 | ||
e51060f0 SH |
992 | static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
993 | { | |
994 | struct rdma_id_private *id_priv = cm_id->context; | |
a1b1b61f SH |
995 | struct rdma_cm_event event; |
996 | int ret = 0; | |
e51060f0 | 997 | |
38ca83a5 | 998 | if ((ib_event->event != IB_CM_TIMEWAIT_EXIT && |
550e5ca7 | 999 | cma_disable_callback(id_priv, RDMA_CM_CONNECT)) || |
38ca83a5 | 1000 | (ib_event->event == IB_CM_TIMEWAIT_EXIT && |
550e5ca7 | 1001 | cma_disable_callback(id_priv, RDMA_CM_DISCONNECT))) |
8aa08602 | 1002 | return 0; |
e51060f0 | 1003 | |
a1b1b61f | 1004 | memset(&event, 0, sizeof event); |
e51060f0 SH |
1005 | switch (ib_event->event) { |
1006 | case IB_CM_REQ_ERROR: | |
1007 | case IB_CM_REP_ERROR: | |
a1b1b61f SH |
1008 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
1009 | event.status = -ETIMEDOUT; | |
e51060f0 SH |
1010 | break; |
1011 | case IB_CM_REP_RECEIVED: | |
a1b1b61f SH |
1012 | event.status = cma_verify_rep(id_priv, ib_event->private_data); |
1013 | if (event.status) | |
1014 | event.event = RDMA_CM_EVENT_CONNECT_ERROR; | |
e51060f0 | 1015 | else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) { |
a1b1b61f SH |
1016 | event.status = cma_rep_recv(id_priv); |
1017 | event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR : | |
1018 | RDMA_CM_EVENT_ESTABLISHED; | |
e51060f0 | 1019 | } else |
a1b1b61f SH |
1020 | event.event = RDMA_CM_EVENT_CONNECT_RESPONSE; |
1021 | cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd, | |
1022 | ib_event->private_data); | |
e51060f0 SH |
1023 | break; |
1024 | case IB_CM_RTU_RECEIVED: | |
0fe313b0 SH |
1025 | case IB_CM_USER_ESTABLISHED: |
1026 | event.event = RDMA_CM_EVENT_ESTABLISHED; | |
e51060f0 SH |
1027 | break; |
1028 | case IB_CM_DREQ_ERROR: | |
a1b1b61f | 1029 | event.status = -ETIMEDOUT; /* fall through */ |
e51060f0 SH |
1030 | case IB_CM_DREQ_RECEIVED: |
1031 | case IB_CM_DREP_RECEIVED: | |
550e5ca7 NM |
1032 | if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT, |
1033 | RDMA_CM_DISCONNECT)) | |
e51060f0 | 1034 | goto out; |
a1b1b61f | 1035 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
e51060f0 SH |
1036 | break; |
1037 | case IB_CM_TIMEWAIT_EXIT: | |
38ca83a5 AV |
1038 | event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT; |
1039 | break; | |
e51060f0 SH |
1040 | case IB_CM_MRA_RECEIVED: |
1041 | /* ignore event */ | |
1042 | goto out; | |
1043 | case IB_CM_REJ_RECEIVED: | |
c5483388 | 1044 | cma_modify_qp_err(id_priv); |
a1b1b61f SH |
1045 | event.status = ib_event->param.rej_rcvd.reason; |
1046 | event.event = RDMA_CM_EVENT_REJECTED; | |
1047 | event.param.conn.private_data = ib_event->private_data; | |
1048 | event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; | |
e51060f0 SH |
1049 | break; |
1050 | default: | |
468f2239 | 1051 | printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n", |
e51060f0 SH |
1052 | ib_event->event); |
1053 | goto out; | |
1054 | } | |
1055 | ||
a1b1b61f | 1056 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
e51060f0 SH |
1057 | if (ret) { |
1058 | /* Destroy the CM ID by returning a non-zero value. */ | |
1059 | id_priv->cm_id.ib = NULL; | |
550e5ca7 | 1060 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 1061 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1062 | rdma_destroy_id(&id_priv->id); |
1063 | return ret; | |
1064 | } | |
1065 | out: | |
de910bd9 | 1066 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1067 | return ret; |
1068 | } | |
1069 | ||
628e5f6d SH |
1070 | static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id, |
1071 | struct ib_cm_event *ib_event) | |
e51060f0 SH |
1072 | { |
1073 | struct rdma_id_private *id_priv; | |
1074 | struct rdma_cm_id *id; | |
1075 | struct rdma_route *rt; | |
1076 | union cma_ip_addr *src, *dst; | |
1b90c137 | 1077 | __be16 port; |
e51060f0 | 1078 | u8 ip_ver; |
64c5e613 | 1079 | int ret; |
e51060f0 | 1080 | |
3f168d2b KK |
1081 | if (cma_get_net_info(ib_event->private_data, listen_id->ps, |
1082 | &ip_ver, &port, &src, &dst)) | |
0c9361fc | 1083 | return NULL; |
3f168d2b | 1084 | |
e51060f0 | 1085 | id = rdma_create_id(listen_id->event_handler, listen_id->context, |
b26f9b99 | 1086 | listen_id->ps, ib_event->param.req_rcvd.qp_type); |
e51060f0 | 1087 | if (IS_ERR(id)) |
0c9361fc | 1088 | return NULL; |
3f168d2b KK |
1089 | |
1090 | cma_save_net_info(&id->route.addr, &listen_id->route.addr, | |
1091 | ip_ver, port, src, dst); | |
e51060f0 SH |
1092 | |
1093 | rt = &id->route; | |
1094 | rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1; | |
3f168d2b KK |
1095 | rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths, |
1096 | GFP_KERNEL); | |
e51060f0 | 1097 | if (!rt->path_rec) |
0c9361fc | 1098 | goto err; |
e51060f0 | 1099 | |
e51060f0 SH |
1100 | rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path; |
1101 | if (rt->num_paths == 2) | |
1102 | rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path; | |
1103 | ||
6f8372b6 SH |
1104 | if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) { |
1105 | rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND; | |
1106 | rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid); | |
1107 | ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey); | |
1108 | } else { | |
1109 | ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr, | |
1110 | &rt->addr.dev_addr); | |
1111 | if (ret) | |
0c9361fc | 1112 | goto err; |
6f8372b6 SH |
1113 | } |
1114 | rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid); | |
e51060f0 SH |
1115 | |
1116 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 1117 | id_priv->state = RDMA_CM_CONNECT; |
e51060f0 | 1118 | return id_priv; |
3f168d2b | 1119 | |
3f168d2b | 1120 | err: |
0c9361fc | 1121 | rdma_destroy_id(id); |
e51060f0 SH |
1122 | return NULL; |
1123 | } | |
1124 | ||
628e5f6d SH |
1125 | static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id, |
1126 | struct ib_cm_event *ib_event) | |
1127 | { | |
1128 | struct rdma_id_private *id_priv; | |
1129 | struct rdma_cm_id *id; | |
1130 | union cma_ip_addr *src, *dst; | |
1b90c137 | 1131 | __be16 port; |
628e5f6d SH |
1132 | u8 ip_ver; |
1133 | int ret; | |
1134 | ||
1135 | id = rdma_create_id(listen_id->event_handler, listen_id->context, | |
b26f9b99 | 1136 | listen_id->ps, IB_QPT_UD); |
628e5f6d SH |
1137 | if (IS_ERR(id)) |
1138 | return NULL; | |
1139 | ||
1140 | ||
1141 | if (cma_get_net_info(ib_event->private_data, listen_id->ps, | |
1142 | &ip_ver, &port, &src, &dst)) | |
1143 | goto err; | |
1144 | ||
1145 | cma_save_net_info(&id->route.addr, &listen_id->route.addr, | |
1146 | ip_ver, port, src, dst); | |
1147 | ||
6f8372b6 SH |
1148 | if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) { |
1149 | ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr, | |
1150 | &id->route.addr.dev_addr); | |
1151 | if (ret) | |
1152 | goto err; | |
1153 | } | |
628e5f6d SH |
1154 | |
1155 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 1156 | id_priv->state = RDMA_CM_CONNECT; |
628e5f6d SH |
1157 | return id_priv; |
1158 | err: | |
1159 | rdma_destroy_id(id); | |
1160 | return NULL; | |
1161 | } | |
1162 | ||
a1b1b61f SH |
1163 | static void cma_set_req_event_data(struct rdma_cm_event *event, |
1164 | struct ib_cm_req_event_param *req_data, | |
1165 | void *private_data, int offset) | |
1166 | { | |
1167 | event->param.conn.private_data = private_data + offset; | |
1168 | event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset; | |
1169 | event->param.conn.responder_resources = req_data->responder_resources; | |
1170 | event->param.conn.initiator_depth = req_data->initiator_depth; | |
1171 | event->param.conn.flow_control = req_data->flow_control; | |
1172 | event->param.conn.retry_count = req_data->retry_count; | |
1173 | event->param.conn.rnr_retry_count = req_data->rnr_retry_count; | |
1174 | event->param.conn.srq = req_data->srq; | |
1175 | event->param.conn.qp_num = req_data->remote_qpn; | |
1176 | } | |
1177 | ||
e51060f0 SH |
1178 | static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
1179 | { | |
1180 | struct rdma_id_private *listen_id, *conn_id; | |
a1b1b61f | 1181 | struct rdma_cm_event event; |
e51060f0 SH |
1182 | int offset, ret; |
1183 | ||
1184 | listen_id = cm_id->context; | |
550e5ca7 | 1185 | if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) |
8aa08602 | 1186 | return -ECONNABORTED; |
e51060f0 | 1187 | |
628e5f6d SH |
1188 | memset(&event, 0, sizeof event); |
1189 | offset = cma_user_data_offset(listen_id->id.ps); | |
1190 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; | |
b26f9b99 | 1191 | if (listen_id->id.qp_type == IB_QPT_UD) { |
628e5f6d SH |
1192 | conn_id = cma_new_udp_id(&listen_id->id, ib_event); |
1193 | event.param.ud.private_data = ib_event->private_data + offset; | |
1194 | event.param.ud.private_data_len = | |
1195 | IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset; | |
1196 | } else { | |
1197 | conn_id = cma_new_conn_id(&listen_id->id, ib_event); | |
1198 | cma_set_req_event_data(&event, &ib_event->param.req_rcvd, | |
1199 | ib_event->private_data, offset); | |
1200 | } | |
e51060f0 SH |
1201 | if (!conn_id) { |
1202 | ret = -ENOMEM; | |
1203 | goto out; | |
1204 | } | |
1205 | ||
de910bd9 | 1206 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
07ebafba | 1207 | ret = cma_acquire_dev(conn_id); |
a1a733f6 KK |
1208 | if (ret) |
1209 | goto release_conn_id; | |
e51060f0 SH |
1210 | |
1211 | conn_id->cm_id.ib = cm_id; | |
1212 | cm_id->context = conn_id; | |
1213 | cm_id->cm_handler = cma_ib_handler; | |
1214 | ||
25ae21a1 SH |
1215 | /* |
1216 | * Protect against the user destroying conn_id from another thread | |
1217 | * until we're done accessing it. | |
1218 | */ | |
1219 | atomic_inc(&conn_id->refcount); | |
a1b1b61f | 1220 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
45d9478d | 1221 | if (!ret) { |
ead595ae SH |
1222 | /* |
1223 | * Acquire mutex to prevent user executing rdma_destroy_id() | |
1224 | * while we're accessing the cm_id. | |
1225 | */ | |
1226 | mutex_lock(&lock); | |
b26f9b99 | 1227 | if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD)) |
ead595ae SH |
1228 | ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); |
1229 | mutex_unlock(&lock); | |
de910bd9 | 1230 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 1231 | cma_deref_id(conn_id); |
a1a733f6 | 1232 | goto out; |
45d9478d | 1233 | } |
25ae21a1 | 1234 | cma_deref_id(conn_id); |
a1a733f6 KK |
1235 | |
1236 | /* Destroy the CM ID by returning a non-zero value. */ | |
1237 | conn_id->cm_id.ib = NULL; | |
1238 | ||
1239 | release_conn_id: | |
550e5ca7 | 1240 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
de910bd9 | 1241 | mutex_unlock(&conn_id->handler_mutex); |
a1a733f6 KK |
1242 | rdma_destroy_id(&conn_id->id); |
1243 | ||
e51060f0 | 1244 | out: |
de910bd9 | 1245 | mutex_unlock(&listen_id->handler_mutex); |
e51060f0 SH |
1246 | return ret; |
1247 | } | |
1248 | ||
1249 | static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr) | |
1250 | { | |
628e5f6d | 1251 | return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr))); |
e51060f0 SH |
1252 | } |
1253 | ||
1254 | static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr, | |
1255 | struct ib_cm_compare_data *compare) | |
1256 | { | |
1257 | struct cma_hdr *cma_data, *cma_mask; | |
1258 | struct sdp_hh *sdp_data, *sdp_mask; | |
1b90c137 | 1259 | __be32 ip4_addr; |
e51060f0 SH |
1260 | struct in6_addr ip6_addr; |
1261 | ||
1262 | memset(compare, 0, sizeof *compare); | |
1263 | cma_data = (void *) compare->data; | |
1264 | cma_mask = (void *) compare->mask; | |
1265 | sdp_data = (void *) compare->data; | |
1266 | sdp_mask = (void *) compare->mask; | |
1267 | ||
1268 | switch (addr->sa_family) { | |
1269 | case AF_INET: | |
1270 | ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr; | |
1271 | if (ps == RDMA_PS_SDP) { | |
1272 | sdp_set_ip_ver(sdp_data, 4); | |
1273 | sdp_set_ip_ver(sdp_mask, 0xF); | |
1274 | sdp_data->dst_addr.ip4.addr = ip4_addr; | |
1b90c137 | 1275 | sdp_mask->dst_addr.ip4.addr = htonl(~0); |
e51060f0 SH |
1276 | } else { |
1277 | cma_set_ip_ver(cma_data, 4); | |
1278 | cma_set_ip_ver(cma_mask, 0xF); | |
1279 | cma_data->dst_addr.ip4.addr = ip4_addr; | |
1b90c137 | 1280 | cma_mask->dst_addr.ip4.addr = htonl(~0); |
e51060f0 SH |
1281 | } |
1282 | break; | |
1283 | case AF_INET6: | |
1284 | ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr; | |
1285 | if (ps == RDMA_PS_SDP) { | |
1286 | sdp_set_ip_ver(sdp_data, 6); | |
1287 | sdp_set_ip_ver(sdp_mask, 0xF); | |
1288 | sdp_data->dst_addr.ip6 = ip6_addr; | |
1289 | memset(&sdp_mask->dst_addr.ip6, 0xFF, | |
1290 | sizeof sdp_mask->dst_addr.ip6); | |
1291 | } else { | |
1292 | cma_set_ip_ver(cma_data, 6); | |
1293 | cma_set_ip_ver(cma_mask, 0xF); | |
1294 | cma_data->dst_addr.ip6 = ip6_addr; | |
1295 | memset(&cma_mask->dst_addr.ip6, 0xFF, | |
1296 | sizeof cma_mask->dst_addr.ip6); | |
1297 | } | |
1298 | break; | |
1299 | default: | |
1300 | break; | |
1301 | } | |
1302 | } | |
1303 | ||
07ebafba TT |
1304 | static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) |
1305 | { | |
1306 | struct rdma_id_private *id_priv = iw_id->context; | |
a1b1b61f | 1307 | struct rdma_cm_event event; |
07ebafba TT |
1308 | struct sockaddr_in *sin; |
1309 | int ret = 0; | |
1310 | ||
550e5ca7 | 1311 | if (cma_disable_callback(id_priv, RDMA_CM_CONNECT)) |
be65f086 | 1312 | return 0; |
07ebafba | 1313 | |
be65f086 | 1314 | memset(&event, 0, sizeof event); |
07ebafba TT |
1315 | switch (iw_event->event) { |
1316 | case IW_CM_EVENT_CLOSE: | |
a1b1b61f | 1317 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
07ebafba TT |
1318 | break; |
1319 | case IW_CM_EVENT_CONNECT_REPLY: | |
1320 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1321 | *sin = iw_event->local_addr; | |
1322 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr; | |
1323 | *sin = iw_event->remote_addr; | |
881a045f SW |
1324 | switch (iw_event->status) { |
1325 | case 0: | |
a1b1b61f | 1326 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
881a045f SW |
1327 | break; |
1328 | case -ECONNRESET: | |
1329 | case -ECONNREFUSED: | |
1330 | event.event = RDMA_CM_EVENT_REJECTED; | |
1331 | break; | |
1332 | case -ETIMEDOUT: | |
1333 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
1334 | break; | |
1335 | default: | |
1336 | event.event = RDMA_CM_EVENT_CONNECT_ERROR; | |
1337 | break; | |
1338 | } | |
07ebafba TT |
1339 | break; |
1340 | case IW_CM_EVENT_ESTABLISHED: | |
a1b1b61f | 1341 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
07ebafba TT |
1342 | break; |
1343 | default: | |
1344 | BUG_ON(1); | |
1345 | } | |
1346 | ||
a1b1b61f SH |
1347 | event.status = iw_event->status; |
1348 | event.param.conn.private_data = iw_event->private_data; | |
1349 | event.param.conn.private_data_len = iw_event->private_data_len; | |
1350 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
07ebafba TT |
1351 | if (ret) { |
1352 | /* Destroy the CM ID by returning a non-zero value. */ | |
1353 | id_priv->cm_id.iw = NULL; | |
550e5ca7 | 1354 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 1355 | mutex_unlock(&id_priv->handler_mutex); |
07ebafba TT |
1356 | rdma_destroy_id(&id_priv->id); |
1357 | return ret; | |
1358 | } | |
1359 | ||
de910bd9 | 1360 | mutex_unlock(&id_priv->handler_mutex); |
07ebafba TT |
1361 | return ret; |
1362 | } | |
1363 | ||
1364 | static int iw_conn_req_handler(struct iw_cm_id *cm_id, | |
1365 | struct iw_cm_event *iw_event) | |
1366 | { | |
1367 | struct rdma_cm_id *new_cm_id; | |
1368 | struct rdma_id_private *listen_id, *conn_id; | |
1369 | struct sockaddr_in *sin; | |
1370 | struct net_device *dev = NULL; | |
a1b1b61f | 1371 | struct rdma_cm_event event; |
07ebafba | 1372 | int ret; |
8d8293cf | 1373 | struct ib_device_attr attr; |
07ebafba TT |
1374 | |
1375 | listen_id = cm_id->context; | |
550e5ca7 | 1376 | if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) |
8aa08602 | 1377 | return -ECONNABORTED; |
07ebafba TT |
1378 | |
1379 | /* Create a new RDMA id for the new IW CM ID */ | |
1380 | new_cm_id = rdma_create_id(listen_id->id.event_handler, | |
1381 | listen_id->id.context, | |
b26f9b99 | 1382 | RDMA_PS_TCP, IB_QPT_RC); |
10f32065 | 1383 | if (IS_ERR(new_cm_id)) { |
07ebafba TT |
1384 | ret = -ENOMEM; |
1385 | goto out; | |
1386 | } | |
1387 | conn_id = container_of(new_cm_id, struct rdma_id_private, id); | |
de910bd9 | 1388 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
550e5ca7 | 1389 | conn_id->state = RDMA_CM_CONNECT; |
07ebafba | 1390 | |
1ab35276 | 1391 | dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr); |
07ebafba TT |
1392 | if (!dev) { |
1393 | ret = -EADDRNOTAVAIL; | |
de910bd9 | 1394 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
1395 | rdma_destroy_id(new_cm_id); |
1396 | goto out; | |
1397 | } | |
1398 | ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL); | |
1399 | if (ret) { | |
de910bd9 | 1400 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
1401 | rdma_destroy_id(new_cm_id); |
1402 | goto out; | |
1403 | } | |
1404 | ||
1405 | ret = cma_acquire_dev(conn_id); | |
1406 | if (ret) { | |
de910bd9 | 1407 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
1408 | rdma_destroy_id(new_cm_id); |
1409 | goto out; | |
1410 | } | |
1411 | ||
1412 | conn_id->cm_id.iw = cm_id; | |
1413 | cm_id->context = conn_id; | |
1414 | cm_id->cm_handler = cma_iw_handler; | |
1415 | ||
1416 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr; | |
1417 | *sin = iw_event->local_addr; | |
1418 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr; | |
1419 | *sin = iw_event->remote_addr; | |
1420 | ||
8d8293cf SW |
1421 | ret = ib_query_device(conn_id->id.device, &attr); |
1422 | if (ret) { | |
de910bd9 | 1423 | mutex_unlock(&conn_id->handler_mutex); |
8d8293cf SW |
1424 | rdma_destroy_id(new_cm_id); |
1425 | goto out; | |
1426 | } | |
1427 | ||
a1b1b61f SH |
1428 | memset(&event, 0, sizeof event); |
1429 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; | |
1430 | event.param.conn.private_data = iw_event->private_data; | |
1431 | event.param.conn.private_data_len = iw_event->private_data_len; | |
8d8293cf SW |
1432 | event.param.conn.initiator_depth = attr.max_qp_init_rd_atom; |
1433 | event.param.conn.responder_resources = attr.max_qp_rd_atom; | |
25ae21a1 SH |
1434 | |
1435 | /* | |
1436 | * Protect against the user destroying conn_id from another thread | |
1437 | * until we're done accessing it. | |
1438 | */ | |
1439 | atomic_inc(&conn_id->refcount); | |
a1b1b61f | 1440 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
07ebafba TT |
1441 | if (ret) { |
1442 | /* User wants to destroy the CM ID */ | |
1443 | conn_id->cm_id.iw = NULL; | |
550e5ca7 | 1444 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
de910bd9 | 1445 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 1446 | cma_deref_id(conn_id); |
07ebafba | 1447 | rdma_destroy_id(&conn_id->id); |
de910bd9 | 1448 | goto out; |
07ebafba TT |
1449 | } |
1450 | ||
de910bd9 | 1451 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 1452 | cma_deref_id(conn_id); |
de910bd9 | 1453 | |
07ebafba TT |
1454 | out: |
1455 | if (dev) | |
1456 | dev_put(dev); | |
de910bd9 | 1457 | mutex_unlock(&listen_id->handler_mutex); |
07ebafba TT |
1458 | return ret; |
1459 | } | |
1460 | ||
e51060f0 SH |
1461 | static int cma_ib_listen(struct rdma_id_private *id_priv) |
1462 | { | |
1463 | struct ib_cm_compare_data compare_data; | |
1464 | struct sockaddr *addr; | |
0c9361fc | 1465 | struct ib_cm_id *id; |
e51060f0 SH |
1466 | __be64 svc_id; |
1467 | int ret; | |
1468 | ||
0c9361fc JM |
1469 | id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv); |
1470 | if (IS_ERR(id)) | |
1471 | return PTR_ERR(id); | |
1472 | ||
1473 | id_priv->cm_id.ib = id; | |
e51060f0 | 1474 | |
3f446754 | 1475 | addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
e51060f0 SH |
1476 | svc_id = cma_get_service_id(id_priv->id.ps, addr); |
1477 | if (cma_any_addr(addr)) | |
1478 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL); | |
1479 | else { | |
1480 | cma_set_compare_data(id_priv->id.ps, addr, &compare_data); | |
1481 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data); | |
1482 | } | |
1483 | ||
1484 | if (ret) { | |
1485 | ib_destroy_cm_id(id_priv->cm_id.ib); | |
1486 | id_priv->cm_id.ib = NULL; | |
1487 | } | |
1488 | ||
1489 | return ret; | |
1490 | } | |
1491 | ||
07ebafba TT |
1492 | static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) |
1493 | { | |
1494 | int ret; | |
1495 | struct sockaddr_in *sin; | |
0c9361fc JM |
1496 | struct iw_cm_id *id; |
1497 | ||
1498 | id = iw_create_cm_id(id_priv->id.device, | |
1499 | iw_conn_req_handler, | |
1500 | id_priv); | |
1501 | if (IS_ERR(id)) | |
1502 | return PTR_ERR(id); | |
07ebafba | 1503 | |
0c9361fc | 1504 | id_priv->cm_id.iw = id; |
07ebafba TT |
1505 | |
1506 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1507 | id_priv->cm_id.iw->local_addr = *sin; | |
1508 | ||
1509 | ret = iw_cm_listen(id_priv->cm_id.iw, backlog); | |
1510 | ||
1511 | if (ret) { | |
1512 | iw_destroy_cm_id(id_priv->cm_id.iw); | |
1513 | id_priv->cm_id.iw = NULL; | |
1514 | } | |
1515 | ||
1516 | return ret; | |
1517 | } | |
1518 | ||
e51060f0 SH |
1519 | static int cma_listen_handler(struct rdma_cm_id *id, |
1520 | struct rdma_cm_event *event) | |
1521 | { | |
1522 | struct rdma_id_private *id_priv = id->context; | |
1523 | ||
1524 | id->context = id_priv->id.context; | |
1525 | id->event_handler = id_priv->id.event_handler; | |
1526 | return id_priv->id.event_handler(id, event); | |
1527 | } | |
1528 | ||
1529 | static void cma_listen_on_dev(struct rdma_id_private *id_priv, | |
1530 | struct cma_device *cma_dev) | |
1531 | { | |
1532 | struct rdma_id_private *dev_id_priv; | |
1533 | struct rdma_cm_id *id; | |
1534 | int ret; | |
1535 | ||
b26f9b99 SH |
1536 | id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps, |
1537 | id_priv->id.qp_type); | |
e51060f0 SH |
1538 | if (IS_ERR(id)) |
1539 | return; | |
1540 | ||
1541 | dev_id_priv = container_of(id, struct rdma_id_private, id); | |
1542 | ||
550e5ca7 | 1543 | dev_id_priv->state = RDMA_CM_ADDR_BOUND; |
e51060f0 | 1544 | memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr, |
3f446754 | 1545 | ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr)); |
e51060f0 SH |
1546 | |
1547 | cma_attach_to_dev(dev_id_priv, cma_dev); | |
1548 | list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); | |
d02d1f53 SH |
1549 | atomic_inc(&id_priv->refcount); |
1550 | dev_id_priv->internal_id = 1; | |
e51060f0 SH |
1551 | |
1552 | ret = rdma_listen(id, id_priv->backlog); | |
1553 | if (ret) | |
d02d1f53 | 1554 | printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, " |
468f2239 | 1555 | "listening on device %s\n", ret, cma_dev->device->name); |
e51060f0 SH |
1556 | } |
1557 | ||
1558 | static void cma_listen_on_all(struct rdma_id_private *id_priv) | |
1559 | { | |
1560 | struct cma_device *cma_dev; | |
1561 | ||
1562 | mutex_lock(&lock); | |
1563 | list_add_tail(&id_priv->list, &listen_any_list); | |
1564 | list_for_each_entry(cma_dev, &dev_list, list) | |
1565 | cma_listen_on_dev(id_priv, cma_dev); | |
1566 | mutex_unlock(&lock); | |
1567 | } | |
1568 | ||
a81c994d SH |
1569 | void rdma_set_service_type(struct rdma_cm_id *id, int tos) |
1570 | { | |
1571 | struct rdma_id_private *id_priv; | |
1572 | ||
1573 | id_priv = container_of(id, struct rdma_id_private, id); | |
1574 | id_priv->tos = (u8) tos; | |
1575 | } | |
1576 | EXPORT_SYMBOL(rdma_set_service_type); | |
1577 | ||
e51060f0 SH |
1578 | static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec, |
1579 | void *context) | |
1580 | { | |
1581 | struct cma_work *work = context; | |
1582 | struct rdma_route *route; | |
1583 | ||
1584 | route = &work->id->id.route; | |
1585 | ||
1586 | if (!status) { | |
1587 | route->num_paths = 1; | |
1588 | *route->path_rec = *path_rec; | |
1589 | } else { | |
550e5ca7 NM |
1590 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1591 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
e51060f0 | 1592 | work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; |
8f0472d3 | 1593 | work->event.status = status; |
e51060f0 SH |
1594 | } |
1595 | ||
1596 | queue_work(cma_wq, &work->work); | |
1597 | } | |
1598 | ||
1599 | static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms, | |
1600 | struct cma_work *work) | |
1601 | { | |
a81c994d | 1602 | struct rdma_addr *addr = &id_priv->id.route.addr; |
e51060f0 | 1603 | struct ib_sa_path_rec path_rec; |
a81c994d SH |
1604 | ib_sa_comp_mask comp_mask; |
1605 | struct sockaddr_in6 *sin6; | |
e51060f0 SH |
1606 | |
1607 | memset(&path_rec, 0, sizeof path_rec); | |
6f8372b6 SH |
1608 | rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid); |
1609 | rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid); | |
a81c994d | 1610 | path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr)); |
e51060f0 | 1611 | path_rec.numb_path = 1; |
962063e6 | 1612 | path_rec.reversible = 1; |
3f446754 RD |
1613 | path_rec.service_id = cma_get_service_id(id_priv->id.ps, |
1614 | (struct sockaddr *) &addr->dst_addr); | |
a81c994d SH |
1615 | |
1616 | comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | | |
1617 | IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH | | |
1618 | IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID; | |
1619 | ||
3f446754 | 1620 | if (addr->src_addr.ss_family == AF_INET) { |
a81c994d SH |
1621 | path_rec.qos_class = cpu_to_be16((u16) id_priv->tos); |
1622 | comp_mask |= IB_SA_PATH_REC_QOS_CLASS; | |
1623 | } else { | |
1624 | sin6 = (struct sockaddr_in6 *) &addr->src_addr; | |
1625 | path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20); | |
1626 | comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; | |
1627 | } | |
e51060f0 | 1628 | |
c1a0b23b | 1629 | id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device, |
a81c994d SH |
1630 | id_priv->id.port_num, &path_rec, |
1631 | comp_mask, timeout_ms, | |
1632 | GFP_KERNEL, cma_query_handler, | |
1633 | work, &id_priv->query); | |
e51060f0 SH |
1634 | |
1635 | return (id_priv->query_id < 0) ? id_priv->query_id : 0; | |
1636 | } | |
1637 | ||
c4028958 | 1638 | static void cma_work_handler(struct work_struct *_work) |
e51060f0 | 1639 | { |
c4028958 | 1640 | struct cma_work *work = container_of(_work, struct cma_work, work); |
e51060f0 SH |
1641 | struct rdma_id_private *id_priv = work->id; |
1642 | int destroy = 0; | |
1643 | ||
de910bd9 | 1644 | mutex_lock(&id_priv->handler_mutex); |
e51060f0 SH |
1645 | if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) |
1646 | goto out; | |
1647 | ||
1648 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
550e5ca7 | 1649 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
e51060f0 SH |
1650 | destroy = 1; |
1651 | } | |
1652 | out: | |
de910bd9 | 1653 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1654 | cma_deref_id(id_priv); |
1655 | if (destroy) | |
1656 | rdma_destroy_id(&id_priv->id); | |
1657 | kfree(work); | |
1658 | } | |
1659 | ||
dd5bdff8 OG |
1660 | static void cma_ndev_work_handler(struct work_struct *_work) |
1661 | { | |
1662 | struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work); | |
1663 | struct rdma_id_private *id_priv = work->id; | |
1664 | int destroy = 0; | |
1665 | ||
1666 | mutex_lock(&id_priv->handler_mutex); | |
550e5ca7 NM |
1667 | if (id_priv->state == RDMA_CM_DESTROYING || |
1668 | id_priv->state == RDMA_CM_DEVICE_REMOVAL) | |
dd5bdff8 OG |
1669 | goto out; |
1670 | ||
1671 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
550e5ca7 | 1672 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
dd5bdff8 OG |
1673 | destroy = 1; |
1674 | } | |
1675 | ||
1676 | out: | |
1677 | mutex_unlock(&id_priv->handler_mutex); | |
1678 | cma_deref_id(id_priv); | |
1679 | if (destroy) | |
1680 | rdma_destroy_id(&id_priv->id); | |
1681 | kfree(work); | |
1682 | } | |
1683 | ||
e51060f0 SH |
1684 | static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms) |
1685 | { | |
1686 | struct rdma_route *route = &id_priv->id.route; | |
1687 | struct cma_work *work; | |
1688 | int ret; | |
1689 | ||
1690 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1691 | if (!work) | |
1692 | return -ENOMEM; | |
1693 | ||
1694 | work->id = id_priv; | |
c4028958 | 1695 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
1696 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1697 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
e51060f0 SH |
1698 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
1699 | ||
1700 | route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); | |
1701 | if (!route->path_rec) { | |
1702 | ret = -ENOMEM; | |
1703 | goto err1; | |
1704 | } | |
1705 | ||
1706 | ret = cma_query_ib_route(id_priv, timeout_ms, work); | |
1707 | if (ret) | |
1708 | goto err2; | |
1709 | ||
1710 | return 0; | |
1711 | err2: | |
1712 | kfree(route->path_rec); | |
1713 | route->path_rec = NULL; | |
1714 | err1: | |
1715 | kfree(work); | |
1716 | return ret; | |
1717 | } | |
1718 | ||
1719 | int rdma_set_ib_paths(struct rdma_cm_id *id, | |
1720 | struct ib_sa_path_rec *path_rec, int num_paths) | |
1721 | { | |
1722 | struct rdma_id_private *id_priv; | |
1723 | int ret; | |
1724 | ||
1725 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 NM |
1726 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
1727 | RDMA_CM_ROUTE_RESOLVED)) | |
e51060f0 SH |
1728 | return -EINVAL; |
1729 | ||
9893e742 JL |
1730 | id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths, |
1731 | GFP_KERNEL); | |
e51060f0 SH |
1732 | if (!id->route.path_rec) { |
1733 | ret = -ENOMEM; | |
1734 | goto err; | |
1735 | } | |
1736 | ||
ae2d9293 | 1737 | id->route.num_paths = num_paths; |
e51060f0 SH |
1738 | return 0; |
1739 | err: | |
550e5ca7 | 1740 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED); |
e51060f0 SH |
1741 | return ret; |
1742 | } | |
1743 | EXPORT_SYMBOL(rdma_set_ib_paths); | |
1744 | ||
07ebafba TT |
1745 | static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms) |
1746 | { | |
1747 | struct cma_work *work; | |
1748 | ||
1749 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1750 | if (!work) | |
1751 | return -ENOMEM; | |
1752 | ||
1753 | work->id = id_priv; | |
c4028958 | 1754 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
1755 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1756 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
07ebafba TT |
1757 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
1758 | queue_work(cma_wq, &work->work); | |
1759 | return 0; | |
1760 | } | |
1761 | ||
3c86aa70 EC |
1762 | static int cma_resolve_iboe_route(struct rdma_id_private *id_priv) |
1763 | { | |
1764 | struct rdma_route *route = &id_priv->id.route; | |
1765 | struct rdma_addr *addr = &route->addr; | |
1766 | struct cma_work *work; | |
1767 | int ret; | |
1768 | struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr; | |
1769 | struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr; | |
1770 | struct net_device *ndev = NULL; | |
af7bd463 | 1771 | u16 vid; |
3c86aa70 EC |
1772 | |
1773 | if (src_addr->sin_family != dst_addr->sin_family) | |
1774 | return -EINVAL; | |
1775 | ||
1776 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1777 | if (!work) | |
1778 | return -ENOMEM; | |
1779 | ||
1780 | work->id = id_priv; | |
1781 | INIT_WORK(&work->work, cma_work_handler); | |
1782 | ||
1783 | route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL); | |
1784 | if (!route->path_rec) { | |
1785 | ret = -ENOMEM; | |
1786 | goto err1; | |
1787 | } | |
1788 | ||
1789 | route->num_paths = 1; | |
1790 | ||
3c86aa70 EC |
1791 | if (addr->dev_addr.bound_dev_if) |
1792 | ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if); | |
1793 | if (!ndev) { | |
1794 | ret = -ENODEV; | |
1795 | goto err2; | |
1796 | } | |
1797 | ||
af7bd463 EC |
1798 | vid = rdma_vlan_dev_vlan_id(ndev); |
1799 | ||
1800 | iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid); | |
1801 | iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid); | |
1802 | ||
1803 | route->path_rec->hop_limit = 1; | |
1804 | route->path_rec->reversible = 1; | |
1805 | route->path_rec->pkey = cpu_to_be16(0xffff); | |
1806 | route->path_rec->mtu_selector = IB_SA_EQ; | |
1807 | route->path_rec->sl = id_priv->tos >> 5; | |
1808 | ||
3c86aa70 EC |
1809 | route->path_rec->mtu = iboe_get_mtu(ndev->mtu); |
1810 | route->path_rec->rate_selector = IB_SA_EQ; | |
1811 | route->path_rec->rate = iboe_get_rate(ndev); | |
1812 | dev_put(ndev); | |
1813 | route->path_rec->packet_life_time_selector = IB_SA_EQ; | |
1814 | route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME; | |
1815 | if (!route->path_rec->mtu) { | |
1816 | ret = -EINVAL; | |
1817 | goto err2; | |
1818 | } | |
1819 | ||
550e5ca7 NM |
1820 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1821 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
3c86aa70 EC |
1822 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
1823 | work->event.status = 0; | |
1824 | ||
1825 | queue_work(cma_wq, &work->work); | |
1826 | ||
1827 | return 0; | |
1828 | ||
1829 | err2: | |
1830 | kfree(route->path_rec); | |
1831 | route->path_rec = NULL; | |
1832 | err1: | |
1833 | kfree(work); | |
1834 | return ret; | |
1835 | } | |
1836 | ||
e51060f0 SH |
1837 | int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) |
1838 | { | |
1839 | struct rdma_id_private *id_priv; | |
1840 | int ret; | |
1841 | ||
1842 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 1843 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY)) |
e51060f0 SH |
1844 | return -EINVAL; |
1845 | ||
1846 | atomic_inc(&id_priv->refcount); | |
07ebafba TT |
1847 | switch (rdma_node_get_transport(id->device->node_type)) { |
1848 | case RDMA_TRANSPORT_IB: | |
3c86aa70 EC |
1849 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
1850 | case IB_LINK_LAYER_INFINIBAND: | |
1851 | ret = cma_resolve_ib_route(id_priv, timeout_ms); | |
1852 | break; | |
1853 | case IB_LINK_LAYER_ETHERNET: | |
1854 | ret = cma_resolve_iboe_route(id_priv); | |
1855 | break; | |
1856 | default: | |
1857 | ret = -ENOSYS; | |
1858 | } | |
e51060f0 | 1859 | break; |
07ebafba TT |
1860 | case RDMA_TRANSPORT_IWARP: |
1861 | ret = cma_resolve_iw_route(id_priv, timeout_ms); | |
1862 | break; | |
e51060f0 SH |
1863 | default: |
1864 | ret = -ENOSYS; | |
1865 | break; | |
1866 | } | |
1867 | if (ret) | |
1868 | goto err; | |
1869 | ||
1870 | return 0; | |
1871 | err: | |
550e5ca7 | 1872 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED); |
e51060f0 SH |
1873 | cma_deref_id(id_priv); |
1874 | return ret; | |
1875 | } | |
1876 | EXPORT_SYMBOL(rdma_resolve_route); | |
1877 | ||
1878 | static int cma_bind_loopback(struct rdma_id_private *id_priv) | |
1879 | { | |
1880 | struct cma_device *cma_dev; | |
1881 | struct ib_port_attr port_attr; | |
f0ee3404 | 1882 | union ib_gid gid; |
e51060f0 SH |
1883 | u16 pkey; |
1884 | int ret; | |
1885 | u8 p; | |
1886 | ||
1887 | mutex_lock(&lock); | |
e82153b5 KK |
1888 | if (list_empty(&dev_list)) { |
1889 | ret = -ENODEV; | |
1890 | goto out; | |
1891 | } | |
e51060f0 SH |
1892 | list_for_each_entry(cma_dev, &dev_list, list) |
1893 | for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p) | |
e82153b5 | 1894 | if (!ib_query_port(cma_dev->device, p, &port_attr) && |
e51060f0 SH |
1895 | port_attr.state == IB_PORT_ACTIVE) |
1896 | goto port_found; | |
1897 | ||
e82153b5 KK |
1898 | p = 1; |
1899 | cma_dev = list_entry(dev_list.next, struct cma_device, list); | |
e51060f0 SH |
1900 | |
1901 | port_found: | |
f0ee3404 | 1902 | ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid); |
e51060f0 SH |
1903 | if (ret) |
1904 | goto out; | |
1905 | ||
1906 | ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); | |
1907 | if (ret) | |
1908 | goto out; | |
1909 | ||
6f8372b6 | 1910 | id_priv->id.route.addr.dev_addr.dev_type = |
3c86aa70 | 1911 | (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ? |
6f8372b6 SH |
1912 | ARPHRD_INFINIBAND : ARPHRD_ETHER; |
1913 | ||
1914 | rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 SH |
1915 | ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); |
1916 | id_priv->id.port_num = p; | |
1917 | cma_attach_to_dev(id_priv, cma_dev); | |
1918 | out: | |
1919 | mutex_unlock(&lock); | |
1920 | return ret; | |
1921 | } | |
1922 | ||
1923 | static void addr_handler(int status, struct sockaddr *src_addr, | |
1924 | struct rdma_dev_addr *dev_addr, void *context) | |
1925 | { | |
1926 | struct rdma_id_private *id_priv = context; | |
a1b1b61f | 1927 | struct rdma_cm_event event; |
e51060f0 | 1928 | |
a1b1b61f | 1929 | memset(&event, 0, sizeof event); |
de910bd9 | 1930 | mutex_lock(&id_priv->handler_mutex); |
550e5ca7 NM |
1931 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, |
1932 | RDMA_CM_ADDR_RESOLVED)) | |
61a73c70 | 1933 | goto out; |
61a73c70 SH |
1934 | |
1935 | if (!status && !id_priv->cma_dev) | |
e51060f0 SH |
1936 | status = cma_acquire_dev(id_priv); |
1937 | ||
1938 | if (status) { | |
550e5ca7 NM |
1939 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
1940 | RDMA_CM_ADDR_BOUND)) | |
e51060f0 | 1941 | goto out; |
a1b1b61f SH |
1942 | event.event = RDMA_CM_EVENT_ADDR_ERROR; |
1943 | event.status = status; | |
e51060f0 | 1944 | } else { |
e51060f0 SH |
1945 | memcpy(&id_priv->id.route.addr.src_addr, src_addr, |
1946 | ip_addr_size(src_addr)); | |
a1b1b61f | 1947 | event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
e51060f0 SH |
1948 | } |
1949 | ||
a1b1b61f | 1950 | if (id_priv->id.event_handler(&id_priv->id, &event)) { |
550e5ca7 | 1951 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 1952 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1953 | cma_deref_id(id_priv); |
1954 | rdma_destroy_id(&id_priv->id); | |
1955 | return; | |
1956 | } | |
1957 | out: | |
de910bd9 | 1958 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1959 | cma_deref_id(id_priv); |
1960 | } | |
1961 | ||
1962 | static int cma_resolve_loopback(struct rdma_id_private *id_priv) | |
1963 | { | |
1964 | struct cma_work *work; | |
6f8372b6 | 1965 | struct sockaddr *src, *dst; |
f0ee3404 | 1966 | union ib_gid gid; |
e51060f0 SH |
1967 | int ret; |
1968 | ||
1969 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1970 | if (!work) | |
1971 | return -ENOMEM; | |
1972 | ||
1973 | if (!id_priv->cma_dev) { | |
1974 | ret = cma_bind_loopback(id_priv); | |
1975 | if (ret) | |
1976 | goto err; | |
1977 | } | |
1978 | ||
6f8372b6 SH |
1979 | rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
1980 | rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 | 1981 | |
6f8372b6 SH |
1982 | src = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
1983 | if (cma_zero_addr(src)) { | |
1984 | dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr; | |
1985 | if ((src->sa_family = dst->sa_family) == AF_INET) { | |
1986 | ((struct sockaddr_in *) src)->sin_addr.s_addr = | |
1987 | ((struct sockaddr_in *) dst)->sin_addr.s_addr; | |
1988 | } else { | |
1989 | ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr, | |
1990 | &((struct sockaddr_in6 *) dst)->sin6_addr); | |
1991 | } | |
e51060f0 SH |
1992 | } |
1993 | ||
1994 | work->id = id_priv; | |
c4028958 | 1995 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
1996 | work->old_state = RDMA_CM_ADDR_QUERY; |
1997 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
e51060f0 SH |
1998 | work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
1999 | queue_work(cma_wq, &work->work); | |
2000 | return 0; | |
2001 | err: | |
2002 | kfree(work); | |
2003 | return ret; | |
2004 | } | |
2005 | ||
2006 | static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
2007 | struct sockaddr *dst_addr) | |
2008 | { | |
d14714df SH |
2009 | if (!src_addr || !src_addr->sa_family) { |
2010 | src_addr = (struct sockaddr *) &id->route.addr.src_addr; | |
2011 | if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) { | |
2012 | ((struct sockaddr_in6 *) src_addr)->sin6_scope_id = | |
2013 | ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id; | |
2014 | } | |
2015 | } | |
2016 | return rdma_bind_addr(id, src_addr); | |
e51060f0 SH |
2017 | } |
2018 | ||
2019 | int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
2020 | struct sockaddr *dst_addr, int timeout_ms) | |
2021 | { | |
2022 | struct rdma_id_private *id_priv; | |
2023 | int ret; | |
2024 | ||
2025 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2026 | if (id_priv->state == RDMA_CM_IDLE) { |
e51060f0 SH |
2027 | ret = cma_bind_addr(id, src_addr, dst_addr); |
2028 | if (ret) | |
2029 | return ret; | |
2030 | } | |
2031 | ||
550e5ca7 | 2032 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) |
e51060f0 SH |
2033 | return -EINVAL; |
2034 | ||
2035 | atomic_inc(&id_priv->refcount); | |
2036 | memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr)); | |
2037 | if (cma_any_addr(dst_addr)) | |
2038 | ret = cma_resolve_loopback(id_priv); | |
2039 | else | |
3f446754 | 2040 | ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr, |
7a118df3 | 2041 | dst_addr, &id->route.addr.dev_addr, |
e51060f0 SH |
2042 | timeout_ms, addr_handler, id_priv); |
2043 | if (ret) | |
2044 | goto err; | |
2045 | ||
2046 | return 0; | |
2047 | err: | |
550e5ca7 | 2048 | cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND); |
e51060f0 SH |
2049 | cma_deref_id(id_priv); |
2050 | return ret; | |
2051 | } | |
2052 | EXPORT_SYMBOL(rdma_resolve_addr); | |
2053 | ||
a9bb7912 HS |
2054 | int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse) |
2055 | { | |
2056 | struct rdma_id_private *id_priv; | |
2057 | unsigned long flags; | |
2058 | int ret; | |
2059 | ||
2060 | id_priv = container_of(id, struct rdma_id_private, id); | |
2061 | spin_lock_irqsave(&id_priv->lock, flags); | |
550e5ca7 | 2062 | if (id_priv->state == RDMA_CM_IDLE) { |
a9bb7912 HS |
2063 | id_priv->reuseaddr = reuse; |
2064 | ret = 0; | |
2065 | } else { | |
2066 | ret = -EINVAL; | |
2067 | } | |
2068 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
2069 | return ret; | |
2070 | } | |
2071 | EXPORT_SYMBOL(rdma_set_reuseaddr); | |
2072 | ||
e51060f0 SH |
2073 | static void cma_bind_port(struct rdma_bind_list *bind_list, |
2074 | struct rdma_id_private *id_priv) | |
2075 | { | |
2076 | struct sockaddr_in *sin; | |
2077 | ||
2078 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
2079 | sin->sin_port = htons(bind_list->port); | |
2080 | id_priv->bind_list = bind_list; | |
2081 | hlist_add_head(&id_priv->node, &bind_list->owners); | |
2082 | } | |
2083 | ||
2084 | static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv, | |
2085 | unsigned short snum) | |
2086 | { | |
2087 | struct rdma_bind_list *bind_list; | |
aedec080 | 2088 | int port, ret; |
e51060f0 | 2089 | |
cb164b8c | 2090 | bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); |
e51060f0 SH |
2091 | if (!bind_list) |
2092 | return -ENOMEM; | |
2093 | ||
aedec080 SH |
2094 | do { |
2095 | ret = idr_get_new_above(ps, bind_list, snum, &port); | |
2096 | } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL)); | |
2097 | ||
2098 | if (ret) | |
2099 | goto err1; | |
2100 | ||
2101 | if (port != snum) { | |
2102 | ret = -EADDRNOTAVAIL; | |
2103 | goto err2; | |
2104 | } | |
2105 | ||
2106 | bind_list->ps = ps; | |
2107 | bind_list->port = (unsigned short) port; | |
2108 | cma_bind_port(bind_list, id_priv); | |
2109 | return 0; | |
2110 | err2: | |
2111 | idr_remove(ps, port); | |
2112 | err1: | |
2113 | kfree(bind_list); | |
2114 | return ret; | |
2115 | } | |
e51060f0 | 2116 | |
aedec080 SH |
2117 | static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv) |
2118 | { | |
5d7220e8 TH |
2119 | static unsigned int last_used_port; |
2120 | int low, high, remaining; | |
2121 | unsigned int rover; | |
e51060f0 | 2122 | |
227b60f5 | 2123 | inet_get_local_port_range(&low, &high); |
5d7220e8 TH |
2124 | remaining = (high - low) + 1; |
2125 | rover = net_random() % remaining + low; | |
2126 | retry: | |
2127 | if (last_used_port != rover && | |
2128 | !idr_find(ps, (unsigned short) rover)) { | |
2129 | int ret = cma_alloc_port(ps, id_priv, rover); | |
2130 | /* | |
2131 | * Remember previously used port number in order to avoid | |
2132 | * re-using same port immediately after it is closed. | |
2133 | */ | |
2134 | if (!ret) | |
2135 | last_used_port = rover; | |
2136 | if (ret != -EADDRNOTAVAIL) | |
2137 | return ret; | |
e51060f0 | 2138 | } |
5d7220e8 TH |
2139 | if (--remaining) { |
2140 | rover++; | |
2141 | if ((rover < low) || (rover > high)) | |
2142 | rover = low; | |
2143 | goto retry; | |
2144 | } | |
2145 | return -EADDRNOTAVAIL; | |
e51060f0 SH |
2146 | } |
2147 | ||
a9bb7912 HS |
2148 | /* |
2149 | * Check that the requested port is available. This is called when trying to | |
2150 | * bind to a specific port, or when trying to listen on a bound port. In | |
2151 | * the latter case, the provided id_priv may already be on the bind_list, but | |
2152 | * we still need to check that it's okay to start listening. | |
2153 | */ | |
2154 | static int cma_check_port(struct rdma_bind_list *bind_list, | |
2155 | struct rdma_id_private *id_priv, uint8_t reuseaddr) | |
e51060f0 SH |
2156 | { |
2157 | struct rdma_id_private *cur_id; | |
43b752da | 2158 | struct sockaddr *addr, *cur_addr; |
e51060f0 | 2159 | struct hlist_node *node; |
e51060f0 | 2160 | |
43b752da | 2161 | addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
a9bb7912 | 2162 | if (cma_any_addr(addr) && !reuseaddr) |
e51060f0 SH |
2163 | return -EADDRNOTAVAIL; |
2164 | ||
2165 | hlist_for_each_entry(cur_id, node, &bind_list->owners, node) { | |
a9bb7912 HS |
2166 | if (id_priv == cur_id) |
2167 | continue; | |
3cd96564 | 2168 | |
550e5ca7 | 2169 | if ((cur_id->state == RDMA_CM_LISTEN) || |
a9bb7912 HS |
2170 | !reuseaddr || !cur_id->reuseaddr) { |
2171 | cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr; | |
2172 | if (cma_any_addr(cur_addr)) | |
2173 | return -EADDRNOTAVAIL; | |
e51060f0 | 2174 | |
a9bb7912 HS |
2175 | if (!cma_addr_cmp(addr, cur_addr)) |
2176 | return -EADDRINUSE; | |
2177 | } | |
2178 | } | |
e51060f0 SH |
2179 | return 0; |
2180 | } | |
2181 | ||
a9bb7912 HS |
2182 | static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv) |
2183 | { | |
2184 | struct rdma_bind_list *bind_list; | |
2185 | unsigned short snum; | |
2186 | int ret; | |
2187 | ||
2188 | snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr)); | |
2189 | if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) | |
2190 | return -EACCES; | |
2191 | ||
2192 | bind_list = idr_find(ps, snum); | |
2193 | if (!bind_list) { | |
2194 | ret = cma_alloc_port(ps, id_priv, snum); | |
2195 | } else { | |
2196 | ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr); | |
2197 | if (!ret) | |
2198 | cma_bind_port(bind_list, id_priv); | |
2199 | } | |
2200 | return ret; | |
2201 | } | |
2202 | ||
2203 | static int cma_bind_listen(struct rdma_id_private *id_priv) | |
2204 | { | |
2205 | struct rdma_bind_list *bind_list = id_priv->bind_list; | |
2206 | int ret = 0; | |
2207 | ||
2208 | mutex_lock(&lock); | |
2209 | if (bind_list->owners.first->next) | |
2210 | ret = cma_check_port(bind_list, id_priv, 0); | |
2211 | mutex_unlock(&lock); | |
2212 | return ret; | |
2213 | } | |
2214 | ||
e51060f0 SH |
2215 | static int cma_get_port(struct rdma_id_private *id_priv) |
2216 | { | |
2217 | struct idr *ps; | |
2218 | int ret; | |
2219 | ||
2220 | switch (id_priv->id.ps) { | |
2221 | case RDMA_PS_SDP: | |
2222 | ps = &sdp_ps; | |
2223 | break; | |
2224 | case RDMA_PS_TCP: | |
2225 | ps = &tcp_ps; | |
2226 | break; | |
628e5f6d SH |
2227 | case RDMA_PS_UDP: |
2228 | ps = &udp_ps; | |
2229 | break; | |
c8f6a362 SH |
2230 | case RDMA_PS_IPOIB: |
2231 | ps = &ipoib_ps; | |
2232 | break; | |
e51060f0 SH |
2233 | default: |
2234 | return -EPROTONOSUPPORT; | |
2235 | } | |
2236 | ||
2237 | mutex_lock(&lock); | |
3f446754 | 2238 | if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr)) |
aedec080 | 2239 | ret = cma_alloc_any_port(ps, id_priv); |
e51060f0 SH |
2240 | else |
2241 | ret = cma_use_port(ps, id_priv); | |
2242 | mutex_unlock(&lock); | |
2243 | ||
2244 | return ret; | |
2245 | } | |
2246 | ||
d14714df SH |
2247 | static int cma_check_linklocal(struct rdma_dev_addr *dev_addr, |
2248 | struct sockaddr *addr) | |
2249 | { | |
fd4582a3 | 2250 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
d14714df SH |
2251 | struct sockaddr_in6 *sin6; |
2252 | ||
2253 | if (addr->sa_family != AF_INET6) | |
2254 | return 0; | |
2255 | ||
2256 | sin6 = (struct sockaddr_in6 *) addr; | |
2257 | if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) && | |
2258 | !sin6->sin6_scope_id) | |
2259 | return -EINVAL; | |
2260 | ||
2261 | dev_addr->bound_dev_if = sin6->sin6_scope_id; | |
2262 | #endif | |
2263 | return 0; | |
2264 | } | |
2265 | ||
a9bb7912 HS |
2266 | int rdma_listen(struct rdma_cm_id *id, int backlog) |
2267 | { | |
2268 | struct rdma_id_private *id_priv; | |
2269 | int ret; | |
2270 | ||
2271 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2272 | if (id_priv->state == RDMA_CM_IDLE) { |
a9bb7912 HS |
2273 | ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET; |
2274 | ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr); | |
2275 | if (ret) | |
2276 | return ret; | |
2277 | } | |
2278 | ||
550e5ca7 | 2279 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) |
a9bb7912 HS |
2280 | return -EINVAL; |
2281 | ||
2282 | if (id_priv->reuseaddr) { | |
2283 | ret = cma_bind_listen(id_priv); | |
2284 | if (ret) | |
2285 | goto err; | |
2286 | } | |
2287 | ||
2288 | id_priv->backlog = backlog; | |
2289 | if (id->device) { | |
2290 | switch (rdma_node_get_transport(id->device->node_type)) { | |
2291 | case RDMA_TRANSPORT_IB: | |
2292 | ret = cma_ib_listen(id_priv); | |
2293 | if (ret) | |
2294 | goto err; | |
2295 | break; | |
2296 | case RDMA_TRANSPORT_IWARP: | |
2297 | ret = cma_iw_listen(id_priv, backlog); | |
2298 | if (ret) | |
2299 | goto err; | |
2300 | break; | |
2301 | default: | |
2302 | ret = -ENOSYS; | |
2303 | goto err; | |
2304 | } | |
2305 | } else | |
2306 | cma_listen_on_all(id_priv); | |
2307 | ||
2308 | return 0; | |
2309 | err: | |
2310 | id_priv->backlog = 0; | |
550e5ca7 | 2311 | cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND); |
a9bb7912 HS |
2312 | return ret; |
2313 | } | |
2314 | EXPORT_SYMBOL(rdma_listen); | |
2315 | ||
e51060f0 SH |
2316 | int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) |
2317 | { | |
2318 | struct rdma_id_private *id_priv; | |
2319 | int ret; | |
2320 | ||
1f5175ad | 2321 | if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) |
e51060f0 SH |
2322 | return -EAFNOSUPPORT; |
2323 | ||
2324 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2325 | if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND)) |
e51060f0 SH |
2326 | return -EINVAL; |
2327 | ||
d14714df SH |
2328 | ret = cma_check_linklocal(&id->route.addr.dev_addr, addr); |
2329 | if (ret) | |
2330 | goto err1; | |
2331 | ||
8523c048 | 2332 | if (!cma_any_addr(addr)) { |
e51060f0 | 2333 | ret = rdma_translate_ip(addr, &id->route.addr.dev_addr); |
e51060f0 | 2334 | if (ret) |
255d0c14 KK |
2335 | goto err1; |
2336 | ||
255d0c14 | 2337 | ret = cma_acquire_dev(id_priv); |
255d0c14 KK |
2338 | if (ret) |
2339 | goto err1; | |
e51060f0 SH |
2340 | } |
2341 | ||
2342 | memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr)); | |
2343 | ret = cma_get_port(id_priv); | |
2344 | if (ret) | |
255d0c14 | 2345 | goto err2; |
e51060f0 SH |
2346 | |
2347 | return 0; | |
255d0c14 | 2348 | err2: |
a396d43a SH |
2349 | if (id_priv->cma_dev) |
2350 | cma_release_dev(id_priv); | |
255d0c14 | 2351 | err1: |
550e5ca7 | 2352 | cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE); |
e51060f0 SH |
2353 | return ret; |
2354 | } | |
2355 | EXPORT_SYMBOL(rdma_bind_addr); | |
2356 | ||
2357 | static int cma_format_hdr(void *hdr, enum rdma_port_space ps, | |
2358 | struct rdma_route *route) | |
2359 | { | |
e51060f0 SH |
2360 | struct cma_hdr *cma_hdr; |
2361 | struct sdp_hh *sdp_hdr; | |
2362 | ||
1f5175ad AS |
2363 | if (route->addr.src_addr.ss_family == AF_INET) { |
2364 | struct sockaddr_in *src4, *dst4; | |
2365 | ||
2366 | src4 = (struct sockaddr_in *) &route->addr.src_addr; | |
2367 | dst4 = (struct sockaddr_in *) &route->addr.dst_addr; | |
2368 | ||
2369 | switch (ps) { | |
2370 | case RDMA_PS_SDP: | |
2371 | sdp_hdr = hdr; | |
2372 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) | |
2373 | return -EINVAL; | |
2374 | sdp_set_ip_ver(sdp_hdr, 4); | |
2375 | sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
2376 | sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
2377 | sdp_hdr->port = src4->sin_port; | |
2378 | break; | |
2379 | default: | |
2380 | cma_hdr = hdr; | |
2381 | cma_hdr->cma_version = CMA_VERSION; | |
2382 | cma_set_ip_ver(cma_hdr, 4); | |
2383 | cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
2384 | cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
2385 | cma_hdr->port = src4->sin_port; | |
2386 | break; | |
2387 | } | |
2388 | } else { | |
2389 | struct sockaddr_in6 *src6, *dst6; | |
2390 | ||
2391 | src6 = (struct sockaddr_in6 *) &route->addr.src_addr; | |
2392 | dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr; | |
2393 | ||
2394 | switch (ps) { | |
2395 | case RDMA_PS_SDP: | |
2396 | sdp_hdr = hdr; | |
2397 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) | |
2398 | return -EINVAL; | |
2399 | sdp_set_ip_ver(sdp_hdr, 6); | |
2400 | sdp_hdr->src_addr.ip6 = src6->sin6_addr; | |
2401 | sdp_hdr->dst_addr.ip6 = dst6->sin6_addr; | |
2402 | sdp_hdr->port = src6->sin6_port; | |
2403 | break; | |
2404 | default: | |
2405 | cma_hdr = hdr; | |
2406 | cma_hdr->cma_version = CMA_VERSION; | |
2407 | cma_set_ip_ver(cma_hdr, 6); | |
2408 | cma_hdr->src_addr.ip6 = src6->sin6_addr; | |
2409 | cma_hdr->dst_addr.ip6 = dst6->sin6_addr; | |
2410 | cma_hdr->port = src6->sin6_port; | |
2411 | break; | |
2412 | } | |
e51060f0 SH |
2413 | } |
2414 | return 0; | |
2415 | } | |
2416 | ||
628e5f6d SH |
2417 | static int cma_sidr_rep_handler(struct ib_cm_id *cm_id, |
2418 | struct ib_cm_event *ib_event) | |
2419 | { | |
2420 | struct rdma_id_private *id_priv = cm_id->context; | |
2421 | struct rdma_cm_event event; | |
2422 | struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd; | |
2423 | int ret = 0; | |
2424 | ||
550e5ca7 | 2425 | if (cma_disable_callback(id_priv, RDMA_CM_CONNECT)) |
8aa08602 | 2426 | return 0; |
628e5f6d | 2427 | |
8aa08602 | 2428 | memset(&event, 0, sizeof event); |
628e5f6d SH |
2429 | switch (ib_event->event) { |
2430 | case IB_CM_SIDR_REQ_ERROR: | |
2431 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
2432 | event.status = -ETIMEDOUT; | |
2433 | break; | |
2434 | case IB_CM_SIDR_REP_RECEIVED: | |
2435 | event.param.ud.private_data = ib_event->private_data; | |
2436 | event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; | |
2437 | if (rep->status != IB_SIDR_SUCCESS) { | |
2438 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
2439 | event.status = ib_event->param.sidr_rep_rcvd.status; | |
2440 | break; | |
2441 | } | |
d2ca39f2 YE |
2442 | ret = cma_set_qkey(id_priv); |
2443 | if (ret) { | |
2444 | event.event = RDMA_CM_EVENT_ADDR_ERROR; | |
2445 | event.status = -EINVAL; | |
2446 | break; | |
2447 | } | |
c8f6a362 | 2448 | if (id_priv->qkey != rep->qkey) { |
628e5f6d SH |
2449 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
2450 | event.status = -EINVAL; | |
2451 | break; | |
2452 | } | |
2453 | ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num, | |
2454 | id_priv->id.route.path_rec, | |
2455 | &event.param.ud.ah_attr); | |
2456 | event.param.ud.qp_num = rep->qpn; | |
2457 | event.param.ud.qkey = rep->qkey; | |
2458 | event.event = RDMA_CM_EVENT_ESTABLISHED; | |
2459 | event.status = 0; | |
2460 | break; | |
2461 | default: | |
468f2239 | 2462 | printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n", |
628e5f6d SH |
2463 | ib_event->event); |
2464 | goto out; | |
2465 | } | |
2466 | ||
2467 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
2468 | if (ret) { | |
2469 | /* Destroy the CM ID by returning a non-zero value. */ | |
2470 | id_priv->cm_id.ib = NULL; | |
550e5ca7 | 2471 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 2472 | mutex_unlock(&id_priv->handler_mutex); |
628e5f6d SH |
2473 | rdma_destroy_id(&id_priv->id); |
2474 | return ret; | |
2475 | } | |
2476 | out: | |
de910bd9 | 2477 | mutex_unlock(&id_priv->handler_mutex); |
628e5f6d SH |
2478 | return ret; |
2479 | } | |
2480 | ||
2481 | static int cma_resolve_ib_udp(struct rdma_id_private *id_priv, | |
2482 | struct rdma_conn_param *conn_param) | |
2483 | { | |
2484 | struct ib_cm_sidr_req_param req; | |
2485 | struct rdma_route *route; | |
0c9361fc | 2486 | struct ib_cm_id *id; |
628e5f6d SH |
2487 | int ret; |
2488 | ||
2489 | req.private_data_len = sizeof(struct cma_hdr) + | |
2490 | conn_param->private_data_len; | |
2491 | req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC); | |
2492 | if (!req.private_data) | |
2493 | return -ENOMEM; | |
2494 | ||
2495 | if (conn_param->private_data && conn_param->private_data_len) | |
2496 | memcpy((void *) req.private_data + sizeof(struct cma_hdr), | |
2497 | conn_param->private_data, conn_param->private_data_len); | |
2498 | ||
2499 | route = &id_priv->id.route; | |
2500 | ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route); | |
2501 | if (ret) | |
2502 | goto out; | |
2503 | ||
0c9361fc JM |
2504 | id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler, |
2505 | id_priv); | |
2506 | if (IS_ERR(id)) { | |
2507 | ret = PTR_ERR(id); | |
628e5f6d SH |
2508 | goto out; |
2509 | } | |
0c9361fc | 2510 | id_priv->cm_id.ib = id; |
628e5f6d SH |
2511 | |
2512 | req.path = route->path_rec; | |
2513 | req.service_id = cma_get_service_id(id_priv->id.ps, | |
3f446754 | 2514 | (struct sockaddr *) &route->addr.dst_addr); |
628e5f6d SH |
2515 | req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8); |
2516 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
2517 | ||
2518 | ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req); | |
2519 | if (ret) { | |
2520 | ib_destroy_cm_id(id_priv->cm_id.ib); | |
2521 | id_priv->cm_id.ib = NULL; | |
2522 | } | |
2523 | out: | |
2524 | kfree(req.private_data); | |
2525 | return ret; | |
2526 | } | |
2527 | ||
e51060f0 SH |
2528 | static int cma_connect_ib(struct rdma_id_private *id_priv, |
2529 | struct rdma_conn_param *conn_param) | |
2530 | { | |
2531 | struct ib_cm_req_param req; | |
2532 | struct rdma_route *route; | |
2533 | void *private_data; | |
0c9361fc | 2534 | struct ib_cm_id *id; |
e51060f0 SH |
2535 | int offset, ret; |
2536 | ||
2537 | memset(&req, 0, sizeof req); | |
2538 | offset = cma_user_data_offset(id_priv->id.ps); | |
2539 | req.private_data_len = offset + conn_param->private_data_len; | |
2540 | private_data = kzalloc(req.private_data_len, GFP_ATOMIC); | |
2541 | if (!private_data) | |
2542 | return -ENOMEM; | |
2543 | ||
2544 | if (conn_param->private_data && conn_param->private_data_len) | |
2545 | memcpy(private_data + offset, conn_param->private_data, | |
2546 | conn_param->private_data_len); | |
2547 | ||
0c9361fc JM |
2548 | id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv); |
2549 | if (IS_ERR(id)) { | |
2550 | ret = PTR_ERR(id); | |
e51060f0 SH |
2551 | goto out; |
2552 | } | |
0c9361fc | 2553 | id_priv->cm_id.ib = id; |
e51060f0 SH |
2554 | |
2555 | route = &id_priv->id.route; | |
2556 | ret = cma_format_hdr(private_data, id_priv->id.ps, route); | |
2557 | if (ret) | |
2558 | goto out; | |
2559 | req.private_data = private_data; | |
2560 | ||
2561 | req.primary_path = &route->path_rec[0]; | |
2562 | if (route->num_paths == 2) | |
2563 | req.alternate_path = &route->path_rec[1]; | |
2564 | ||
2565 | req.service_id = cma_get_service_id(id_priv->id.ps, | |
3f446754 | 2566 | (struct sockaddr *) &route->addr.dst_addr); |
e51060f0 | 2567 | req.qp_num = id_priv->qp_num; |
9b2e9c0c | 2568 | req.qp_type = IB_QPT_RC; |
e51060f0 SH |
2569 | req.starting_psn = id_priv->seq_num; |
2570 | req.responder_resources = conn_param->responder_resources; | |
2571 | req.initiator_depth = conn_param->initiator_depth; | |
2572 | req.flow_control = conn_param->flow_control; | |
2573 | req.retry_count = conn_param->retry_count; | |
2574 | req.rnr_retry_count = conn_param->rnr_retry_count; | |
2575 | req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
2576 | req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
2577 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
2578 | req.srq = id_priv->srq ? 1 : 0; | |
2579 | ||
2580 | ret = ib_send_cm_req(id_priv->cm_id.ib, &req); | |
2581 | out: | |
0c9361fc JM |
2582 | if (ret && !IS_ERR(id)) { |
2583 | ib_destroy_cm_id(id); | |
675a027c KK |
2584 | id_priv->cm_id.ib = NULL; |
2585 | } | |
2586 | ||
e51060f0 SH |
2587 | kfree(private_data); |
2588 | return ret; | |
2589 | } | |
2590 | ||
07ebafba TT |
2591 | static int cma_connect_iw(struct rdma_id_private *id_priv, |
2592 | struct rdma_conn_param *conn_param) | |
2593 | { | |
2594 | struct iw_cm_id *cm_id; | |
2595 | struct sockaddr_in* sin; | |
2596 | int ret; | |
2597 | struct iw_cm_conn_param iw_param; | |
2598 | ||
2599 | cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); | |
0c9361fc JM |
2600 | if (IS_ERR(cm_id)) |
2601 | return PTR_ERR(cm_id); | |
07ebafba TT |
2602 | |
2603 | id_priv->cm_id.iw = cm_id; | |
2604 | ||
2605 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr; | |
2606 | cm_id->local_addr = *sin; | |
2607 | ||
2608 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr; | |
2609 | cm_id->remote_addr = *sin; | |
2610 | ||
5851bb89 | 2611 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
675a027c KK |
2612 | if (ret) |
2613 | goto out; | |
07ebafba TT |
2614 | |
2615 | iw_param.ord = conn_param->initiator_depth; | |
2616 | iw_param.ird = conn_param->responder_resources; | |
2617 | iw_param.private_data = conn_param->private_data; | |
2618 | iw_param.private_data_len = conn_param->private_data_len; | |
2619 | if (id_priv->id.qp) | |
2620 | iw_param.qpn = id_priv->qp_num; | |
2621 | else | |
2622 | iw_param.qpn = conn_param->qp_num; | |
2623 | ret = iw_cm_connect(cm_id, &iw_param); | |
2624 | out: | |
0c9361fc | 2625 | if (ret) { |
675a027c KK |
2626 | iw_destroy_cm_id(cm_id); |
2627 | id_priv->cm_id.iw = NULL; | |
2628 | } | |
07ebafba TT |
2629 | return ret; |
2630 | } | |
2631 | ||
e51060f0 SH |
2632 | int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
2633 | { | |
2634 | struct rdma_id_private *id_priv; | |
2635 | int ret; | |
2636 | ||
2637 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2638 | if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) |
e51060f0 SH |
2639 | return -EINVAL; |
2640 | ||
2641 | if (!id->qp) { | |
2642 | id_priv->qp_num = conn_param->qp_num; | |
e51060f0 SH |
2643 | id_priv->srq = conn_param->srq; |
2644 | } | |
2645 | ||
07ebafba TT |
2646 | switch (rdma_node_get_transport(id->device->node_type)) { |
2647 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 2648 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
2649 | ret = cma_resolve_ib_udp(id_priv, conn_param); |
2650 | else | |
2651 | ret = cma_connect_ib(id_priv, conn_param); | |
e51060f0 | 2652 | break; |
07ebafba TT |
2653 | case RDMA_TRANSPORT_IWARP: |
2654 | ret = cma_connect_iw(id_priv, conn_param); | |
2655 | break; | |
e51060f0 SH |
2656 | default: |
2657 | ret = -ENOSYS; | |
2658 | break; | |
2659 | } | |
2660 | if (ret) | |
2661 | goto err; | |
2662 | ||
2663 | return 0; | |
2664 | err: | |
550e5ca7 | 2665 | cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED); |
e51060f0 SH |
2666 | return ret; |
2667 | } | |
2668 | EXPORT_SYMBOL(rdma_connect); | |
2669 | ||
2670 | static int cma_accept_ib(struct rdma_id_private *id_priv, | |
2671 | struct rdma_conn_param *conn_param) | |
2672 | { | |
2673 | struct ib_cm_rep_param rep; | |
5851bb89 | 2674 | int ret; |
0fe313b0 | 2675 | |
5851bb89 SH |
2676 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
2677 | if (ret) | |
2678 | goto out; | |
0fe313b0 | 2679 | |
5851bb89 SH |
2680 | ret = cma_modify_qp_rts(id_priv, conn_param); |
2681 | if (ret) | |
2682 | goto out; | |
e51060f0 SH |
2683 | |
2684 | memset(&rep, 0, sizeof rep); | |
2685 | rep.qp_num = id_priv->qp_num; | |
2686 | rep.starting_psn = id_priv->seq_num; | |
2687 | rep.private_data = conn_param->private_data; | |
2688 | rep.private_data_len = conn_param->private_data_len; | |
2689 | rep.responder_resources = conn_param->responder_resources; | |
2690 | rep.initiator_depth = conn_param->initiator_depth; | |
e51060f0 SH |
2691 | rep.failover_accepted = 0; |
2692 | rep.flow_control = conn_param->flow_control; | |
2693 | rep.rnr_retry_count = conn_param->rnr_retry_count; | |
2694 | rep.srq = id_priv->srq ? 1 : 0; | |
2695 | ||
0fe313b0 SH |
2696 | ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep); |
2697 | out: | |
2698 | return ret; | |
e51060f0 SH |
2699 | } |
2700 | ||
07ebafba TT |
2701 | static int cma_accept_iw(struct rdma_id_private *id_priv, |
2702 | struct rdma_conn_param *conn_param) | |
2703 | { | |
2704 | struct iw_cm_conn_param iw_param; | |
2705 | int ret; | |
2706 | ||
5851bb89 | 2707 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
07ebafba TT |
2708 | if (ret) |
2709 | return ret; | |
2710 | ||
2711 | iw_param.ord = conn_param->initiator_depth; | |
2712 | iw_param.ird = conn_param->responder_resources; | |
2713 | iw_param.private_data = conn_param->private_data; | |
2714 | iw_param.private_data_len = conn_param->private_data_len; | |
2715 | if (id_priv->id.qp) { | |
2716 | iw_param.qpn = id_priv->qp_num; | |
2717 | } else | |
2718 | iw_param.qpn = conn_param->qp_num; | |
2719 | ||
2720 | return iw_cm_accept(id_priv->cm_id.iw, &iw_param); | |
2721 | } | |
2722 | ||
628e5f6d SH |
2723 | static int cma_send_sidr_rep(struct rdma_id_private *id_priv, |
2724 | enum ib_cm_sidr_status status, | |
2725 | const void *private_data, int private_data_len) | |
2726 | { | |
2727 | struct ib_cm_sidr_rep_param rep; | |
d2ca39f2 | 2728 | int ret; |
628e5f6d SH |
2729 | |
2730 | memset(&rep, 0, sizeof rep); | |
2731 | rep.status = status; | |
2732 | if (status == IB_SIDR_SUCCESS) { | |
d2ca39f2 YE |
2733 | ret = cma_set_qkey(id_priv); |
2734 | if (ret) | |
2735 | return ret; | |
628e5f6d | 2736 | rep.qp_num = id_priv->qp_num; |
c8f6a362 | 2737 | rep.qkey = id_priv->qkey; |
628e5f6d SH |
2738 | } |
2739 | rep.private_data = private_data; | |
2740 | rep.private_data_len = private_data_len; | |
2741 | ||
2742 | return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep); | |
2743 | } | |
2744 | ||
e51060f0 SH |
2745 | int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
2746 | { | |
2747 | struct rdma_id_private *id_priv; | |
2748 | int ret; | |
2749 | ||
2750 | id_priv = container_of(id, struct rdma_id_private, id); | |
83e9502d NM |
2751 | |
2752 | id_priv->owner = task_pid_nr(current); | |
2753 | ||
550e5ca7 | 2754 | if (!cma_comp(id_priv, RDMA_CM_CONNECT)) |
e51060f0 SH |
2755 | return -EINVAL; |
2756 | ||
2757 | if (!id->qp && conn_param) { | |
2758 | id_priv->qp_num = conn_param->qp_num; | |
e51060f0 SH |
2759 | id_priv->srq = conn_param->srq; |
2760 | } | |
2761 | ||
07ebafba TT |
2762 | switch (rdma_node_get_transport(id->device->node_type)) { |
2763 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 2764 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
2765 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, |
2766 | conn_param->private_data, | |
2767 | conn_param->private_data_len); | |
2768 | else if (conn_param) | |
e51060f0 SH |
2769 | ret = cma_accept_ib(id_priv, conn_param); |
2770 | else | |
2771 | ret = cma_rep_recv(id_priv); | |
2772 | break; | |
07ebafba TT |
2773 | case RDMA_TRANSPORT_IWARP: |
2774 | ret = cma_accept_iw(id_priv, conn_param); | |
2775 | break; | |
e51060f0 SH |
2776 | default: |
2777 | ret = -ENOSYS; | |
2778 | break; | |
2779 | } | |
2780 | ||
2781 | if (ret) | |
2782 | goto reject; | |
2783 | ||
2784 | return 0; | |
2785 | reject: | |
c5483388 | 2786 | cma_modify_qp_err(id_priv); |
e51060f0 SH |
2787 | rdma_reject(id, NULL, 0); |
2788 | return ret; | |
2789 | } | |
2790 | EXPORT_SYMBOL(rdma_accept); | |
2791 | ||
0fe313b0 SH |
2792 | int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event) |
2793 | { | |
2794 | struct rdma_id_private *id_priv; | |
2795 | int ret; | |
2796 | ||
2797 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 2798 | if (!id_priv->cm_id.ib) |
0fe313b0 SH |
2799 | return -EINVAL; |
2800 | ||
2801 | switch (id->device->node_type) { | |
2802 | case RDMA_NODE_IB_CA: | |
2803 | ret = ib_cm_notify(id_priv->cm_id.ib, event); | |
2804 | break; | |
2805 | default: | |
2806 | ret = 0; | |
2807 | break; | |
2808 | } | |
2809 | return ret; | |
2810 | } | |
2811 | EXPORT_SYMBOL(rdma_notify); | |
2812 | ||
e51060f0 SH |
2813 | int rdma_reject(struct rdma_cm_id *id, const void *private_data, |
2814 | u8 private_data_len) | |
2815 | { | |
2816 | struct rdma_id_private *id_priv; | |
2817 | int ret; | |
2818 | ||
2819 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 2820 | if (!id_priv->cm_id.ib) |
e51060f0 SH |
2821 | return -EINVAL; |
2822 | ||
07ebafba TT |
2823 | switch (rdma_node_get_transport(id->device->node_type)) { |
2824 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 2825 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
2826 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, |
2827 | private_data, private_data_len); | |
2828 | else | |
2829 | ret = ib_send_cm_rej(id_priv->cm_id.ib, | |
2830 | IB_CM_REJ_CONSUMER_DEFINED, NULL, | |
2831 | 0, private_data, private_data_len); | |
e51060f0 | 2832 | break; |
07ebafba TT |
2833 | case RDMA_TRANSPORT_IWARP: |
2834 | ret = iw_cm_reject(id_priv->cm_id.iw, | |
2835 | private_data, private_data_len); | |
2836 | break; | |
e51060f0 SH |
2837 | default: |
2838 | ret = -ENOSYS; | |
2839 | break; | |
2840 | } | |
2841 | return ret; | |
2842 | } | |
2843 | EXPORT_SYMBOL(rdma_reject); | |
2844 | ||
2845 | int rdma_disconnect(struct rdma_cm_id *id) | |
2846 | { | |
2847 | struct rdma_id_private *id_priv; | |
2848 | int ret; | |
2849 | ||
2850 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 2851 | if (!id_priv->cm_id.ib) |
e51060f0 SH |
2852 | return -EINVAL; |
2853 | ||
07ebafba TT |
2854 | switch (rdma_node_get_transport(id->device->node_type)) { |
2855 | case RDMA_TRANSPORT_IB: | |
c5483388 | 2856 | ret = cma_modify_qp_err(id_priv); |
07ebafba TT |
2857 | if (ret) |
2858 | goto out; | |
e51060f0 SH |
2859 | /* Initiate or respond to a disconnect. */ |
2860 | if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) | |
2861 | ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0); | |
2862 | break; | |
07ebafba TT |
2863 | case RDMA_TRANSPORT_IWARP: |
2864 | ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); | |
2865 | break; | |
e51060f0 | 2866 | default: |
07ebafba | 2867 | ret = -EINVAL; |
e51060f0 SH |
2868 | break; |
2869 | } | |
2870 | out: | |
2871 | return ret; | |
2872 | } | |
2873 | EXPORT_SYMBOL(rdma_disconnect); | |
2874 | ||
c8f6a362 SH |
2875 | static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast) |
2876 | { | |
2877 | struct rdma_id_private *id_priv; | |
2878 | struct cma_multicast *mc = multicast->context; | |
2879 | struct rdma_cm_event event; | |
2880 | int ret; | |
2881 | ||
2882 | id_priv = mc->id_priv; | |
550e5ca7 NM |
2883 | if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) && |
2884 | cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED)) | |
8aa08602 | 2885 | return 0; |
c8f6a362 | 2886 | |
c5483388 | 2887 | mutex_lock(&id_priv->qp_mutex); |
c8f6a362 SH |
2888 | if (!status && id_priv->id.qp) |
2889 | status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid, | |
2890 | multicast->rec.mlid); | |
c5483388 | 2891 | mutex_unlock(&id_priv->qp_mutex); |
c8f6a362 SH |
2892 | |
2893 | memset(&event, 0, sizeof event); | |
2894 | event.status = status; | |
2895 | event.param.ud.private_data = mc->context; | |
2896 | if (!status) { | |
2897 | event.event = RDMA_CM_EVENT_MULTICAST_JOIN; | |
2898 | ib_init_ah_from_mcmember(id_priv->id.device, | |
2899 | id_priv->id.port_num, &multicast->rec, | |
2900 | &event.param.ud.ah_attr); | |
2901 | event.param.ud.qp_num = 0xFFFFFF; | |
2902 | event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey); | |
2903 | } else | |
2904 | event.event = RDMA_CM_EVENT_MULTICAST_ERROR; | |
2905 | ||
2906 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
2907 | if (ret) { | |
550e5ca7 | 2908 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 2909 | mutex_unlock(&id_priv->handler_mutex); |
c8f6a362 SH |
2910 | rdma_destroy_id(&id_priv->id); |
2911 | return 0; | |
2912 | } | |
8aa08602 | 2913 | |
de910bd9 | 2914 | mutex_unlock(&id_priv->handler_mutex); |
c8f6a362 SH |
2915 | return 0; |
2916 | } | |
2917 | ||
2918 | static void cma_set_mgid(struct rdma_id_private *id_priv, | |
2919 | struct sockaddr *addr, union ib_gid *mgid) | |
2920 | { | |
2921 | unsigned char mc_map[MAX_ADDR_LEN]; | |
2922 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
2923 | struct sockaddr_in *sin = (struct sockaddr_in *) addr; | |
2924 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr; | |
2925 | ||
2926 | if (cma_any_addr(addr)) { | |
2927 | memset(mgid, 0, sizeof *mgid); | |
2928 | } else if ((addr->sa_family == AF_INET6) && | |
1c9b2819 | 2929 | ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) == |
c8f6a362 SH |
2930 | 0xFF10A01B)) { |
2931 | /* IPv6 address is an SA assigned MGID. */ | |
2932 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); | |
e2e62697 JG |
2933 | } else if ((addr->sa_family == AF_INET6)) { |
2934 | ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map); | |
2935 | if (id_priv->id.ps == RDMA_PS_UDP) | |
2936 | mc_map[7] = 0x01; /* Use RDMA CM signature */ | |
2937 | *mgid = *(union ib_gid *) (mc_map + 4); | |
c8f6a362 | 2938 | } else { |
a9e527e3 | 2939 | ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map); |
c8f6a362 SH |
2940 | if (id_priv->id.ps == RDMA_PS_UDP) |
2941 | mc_map[7] = 0x01; /* Use RDMA CM signature */ | |
c8f6a362 SH |
2942 | *mgid = *(union ib_gid *) (mc_map + 4); |
2943 | } | |
2944 | } | |
2945 | ||
2946 | static int cma_join_ib_multicast(struct rdma_id_private *id_priv, | |
2947 | struct cma_multicast *mc) | |
2948 | { | |
2949 | struct ib_sa_mcmember_rec rec; | |
2950 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
2951 | ib_sa_comp_mask comp_mask; | |
2952 | int ret; | |
2953 | ||
2954 | ib_addr_get_mgid(dev_addr, &rec.mgid); | |
2955 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num, | |
2956 | &rec.mgid, &rec); | |
2957 | if (ret) | |
2958 | return ret; | |
2959 | ||
3f446754 | 2960 | cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid); |
c8f6a362 SH |
2961 | if (id_priv->id.ps == RDMA_PS_UDP) |
2962 | rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); | |
6f8372b6 | 2963 | rdma_addr_get_sgid(dev_addr, &rec.port_gid); |
c8f6a362 SH |
2964 | rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); |
2965 | rec.join_state = 1; | |
2966 | ||
2967 | comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID | | |
2968 | IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE | | |
2969 | IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL | | |
2970 | IB_SA_MCMEMBER_REC_FLOW_LABEL | | |
2971 | IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; | |
2972 | ||
84adeee9 YE |
2973 | if (id_priv->id.ps == RDMA_PS_IPOIB) |
2974 | comp_mask |= IB_SA_MCMEMBER_REC_RATE | | |
2975 | IB_SA_MCMEMBER_REC_RATE_SELECTOR; | |
2976 | ||
c8f6a362 SH |
2977 | mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device, |
2978 | id_priv->id.port_num, &rec, | |
2979 | comp_mask, GFP_KERNEL, | |
2980 | cma_ib_mc_handler, mc); | |
2981 | if (IS_ERR(mc->multicast.ib)) | |
2982 | return PTR_ERR(mc->multicast.ib); | |
2983 | ||
2984 | return 0; | |
2985 | } | |
2986 | ||
3c86aa70 EC |
2987 | static void iboe_mcast_work_handler(struct work_struct *work) |
2988 | { | |
2989 | struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work); | |
2990 | struct cma_multicast *mc = mw->mc; | |
2991 | struct ib_sa_multicast *m = mc->multicast.ib; | |
2992 | ||
2993 | mc->multicast.ib->context = mc; | |
2994 | cma_ib_mc_handler(0, m); | |
2995 | kref_put(&mc->mcref, release_mc); | |
2996 | kfree(mw); | |
2997 | } | |
2998 | ||
2999 | static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid) | |
3000 | { | |
3001 | struct sockaddr_in *sin = (struct sockaddr_in *)addr; | |
3002 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; | |
3003 | ||
3004 | if (cma_any_addr(addr)) { | |
3005 | memset(mgid, 0, sizeof *mgid); | |
3006 | } else if (addr->sa_family == AF_INET6) { | |
3007 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); | |
3008 | } else { | |
3009 | mgid->raw[0] = 0xff; | |
3010 | mgid->raw[1] = 0x0e; | |
3011 | mgid->raw[2] = 0; | |
3012 | mgid->raw[3] = 0; | |
3013 | mgid->raw[4] = 0; | |
3014 | mgid->raw[5] = 0; | |
3015 | mgid->raw[6] = 0; | |
3016 | mgid->raw[7] = 0; | |
3017 | mgid->raw[8] = 0; | |
3018 | mgid->raw[9] = 0; | |
3019 | mgid->raw[10] = 0xff; | |
3020 | mgid->raw[11] = 0xff; | |
3021 | *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr; | |
3022 | } | |
3023 | } | |
3024 | ||
3025 | static int cma_iboe_join_multicast(struct rdma_id_private *id_priv, | |
3026 | struct cma_multicast *mc) | |
3027 | { | |
3028 | struct iboe_mcast_work *work; | |
3029 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
3030 | int err; | |
3031 | struct sockaddr *addr = (struct sockaddr *)&mc->addr; | |
3032 | struct net_device *ndev = NULL; | |
3033 | ||
3034 | if (cma_zero_addr((struct sockaddr *)&mc->addr)) | |
3035 | return -EINVAL; | |
3036 | ||
3037 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
3038 | if (!work) | |
3039 | return -ENOMEM; | |
3040 | ||
3041 | mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL); | |
3042 | if (!mc->multicast.ib) { | |
3043 | err = -ENOMEM; | |
3044 | goto out1; | |
3045 | } | |
3046 | ||
3047 | cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid); | |
3048 | ||
3049 | mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff); | |
3050 | if (id_priv->id.ps == RDMA_PS_UDP) | |
3051 | mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); | |
3052 | ||
3053 | if (dev_addr->bound_dev_if) | |
3054 | ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); | |
3055 | if (!ndev) { | |
3056 | err = -ENODEV; | |
3057 | goto out2; | |
3058 | } | |
3059 | mc->multicast.ib->rec.rate = iboe_get_rate(ndev); | |
3060 | mc->multicast.ib->rec.hop_limit = 1; | |
3061 | mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu); | |
3062 | dev_put(ndev); | |
3063 | if (!mc->multicast.ib->rec.mtu) { | |
3064 | err = -EINVAL; | |
3065 | goto out2; | |
3066 | } | |
3067 | iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid); | |
3068 | work->id = id_priv; | |
3069 | work->mc = mc; | |
3070 | INIT_WORK(&work->work, iboe_mcast_work_handler); | |
3071 | kref_get(&mc->mcref); | |
3072 | queue_work(cma_wq, &work->work); | |
3073 | ||
3074 | return 0; | |
3075 | ||
3076 | out2: | |
3077 | kfree(mc->multicast.ib); | |
3078 | out1: | |
3079 | kfree(work); | |
3080 | return err; | |
3081 | } | |
3082 | ||
c8f6a362 SH |
3083 | int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, |
3084 | void *context) | |
3085 | { | |
3086 | struct rdma_id_private *id_priv; | |
3087 | struct cma_multicast *mc; | |
3088 | int ret; | |
3089 | ||
3090 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 NM |
3091 | if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) && |
3092 | !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED)) | |
c8f6a362 SH |
3093 | return -EINVAL; |
3094 | ||
3095 | mc = kmalloc(sizeof *mc, GFP_KERNEL); | |
3096 | if (!mc) | |
3097 | return -ENOMEM; | |
3098 | ||
3099 | memcpy(&mc->addr, addr, ip_addr_size(addr)); | |
3100 | mc->context = context; | |
3101 | mc->id_priv = id_priv; | |
3102 | ||
3103 | spin_lock(&id_priv->lock); | |
3104 | list_add(&mc->list, &id_priv->mc_list); | |
3105 | spin_unlock(&id_priv->lock); | |
3106 | ||
3107 | switch (rdma_node_get_transport(id->device->node_type)) { | |
3108 | case RDMA_TRANSPORT_IB: | |
3c86aa70 EC |
3109 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
3110 | case IB_LINK_LAYER_INFINIBAND: | |
3111 | ret = cma_join_ib_multicast(id_priv, mc); | |
3112 | break; | |
3113 | case IB_LINK_LAYER_ETHERNET: | |
3114 | kref_init(&mc->mcref); | |
3115 | ret = cma_iboe_join_multicast(id_priv, mc); | |
3116 | break; | |
3117 | default: | |
3118 | ret = -EINVAL; | |
3119 | } | |
c8f6a362 SH |
3120 | break; |
3121 | default: | |
3122 | ret = -ENOSYS; | |
3123 | break; | |
3124 | } | |
3125 | ||
3126 | if (ret) { | |
3127 | spin_lock_irq(&id_priv->lock); | |
3128 | list_del(&mc->list); | |
3129 | spin_unlock_irq(&id_priv->lock); | |
3130 | kfree(mc); | |
3131 | } | |
3132 | return ret; | |
3133 | } | |
3134 | EXPORT_SYMBOL(rdma_join_multicast); | |
3135 | ||
3136 | void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) | |
3137 | { | |
3138 | struct rdma_id_private *id_priv; | |
3139 | struct cma_multicast *mc; | |
3140 | ||
3141 | id_priv = container_of(id, struct rdma_id_private, id); | |
3142 | spin_lock_irq(&id_priv->lock); | |
3143 | list_for_each_entry(mc, &id_priv->mc_list, list) { | |
3144 | if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) { | |
3145 | list_del(&mc->list); | |
3146 | spin_unlock_irq(&id_priv->lock); | |
3147 | ||
3148 | if (id->qp) | |
3149 | ib_detach_mcast(id->qp, | |
3150 | &mc->multicast.ib->rec.mgid, | |
3151 | mc->multicast.ib->rec.mlid); | |
3c86aa70 EC |
3152 | if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) { |
3153 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { | |
3154 | case IB_LINK_LAYER_INFINIBAND: | |
3155 | ib_sa_free_multicast(mc->multicast.ib); | |
3156 | kfree(mc); | |
3157 | break; | |
3158 | case IB_LINK_LAYER_ETHERNET: | |
3159 | kref_put(&mc->mcref, release_mc); | |
3160 | break; | |
3161 | default: | |
3162 | break; | |
3163 | } | |
3164 | } | |
c8f6a362 SH |
3165 | return; |
3166 | } | |
3167 | } | |
3168 | spin_unlock_irq(&id_priv->lock); | |
3169 | } | |
3170 | EXPORT_SYMBOL(rdma_leave_multicast); | |
3171 | ||
dd5bdff8 OG |
3172 | static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv) |
3173 | { | |
3174 | struct rdma_dev_addr *dev_addr; | |
3175 | struct cma_ndev_work *work; | |
3176 | ||
3177 | dev_addr = &id_priv->id.route.addr.dev_addr; | |
3178 | ||
6266ed6e | 3179 | if ((dev_addr->bound_dev_if == ndev->ifindex) && |
dd5bdff8 OG |
3180 | memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) { |
3181 | printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n", | |
3182 | ndev->name, &id_priv->id); | |
3183 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
3184 | if (!work) | |
3185 | return -ENOMEM; | |
3186 | ||
3187 | INIT_WORK(&work->work, cma_ndev_work_handler); | |
3188 | work->id = id_priv; | |
3189 | work->event.event = RDMA_CM_EVENT_ADDR_CHANGE; | |
3190 | atomic_inc(&id_priv->refcount); | |
3191 | queue_work(cma_wq, &work->work); | |
3192 | } | |
3193 | ||
3194 | return 0; | |
3195 | } | |
3196 | ||
3197 | static int cma_netdev_callback(struct notifier_block *self, unsigned long event, | |
3198 | void *ctx) | |
3199 | { | |
3200 | struct net_device *ndev = (struct net_device *)ctx; | |
3201 | struct cma_device *cma_dev; | |
3202 | struct rdma_id_private *id_priv; | |
3203 | int ret = NOTIFY_DONE; | |
3204 | ||
3205 | if (dev_net(ndev) != &init_net) | |
3206 | return NOTIFY_DONE; | |
3207 | ||
3208 | if (event != NETDEV_BONDING_FAILOVER) | |
3209 | return NOTIFY_DONE; | |
3210 | ||
3211 | if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING)) | |
3212 | return NOTIFY_DONE; | |
3213 | ||
3214 | mutex_lock(&lock); | |
3215 | list_for_each_entry(cma_dev, &dev_list, list) | |
3216 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { | |
3217 | ret = cma_netdev_change(ndev, id_priv); | |
3218 | if (ret) | |
3219 | goto out; | |
3220 | } | |
3221 | ||
3222 | out: | |
3223 | mutex_unlock(&lock); | |
3224 | return ret; | |
3225 | } | |
3226 | ||
3227 | static struct notifier_block cma_nb = { | |
3228 | .notifier_call = cma_netdev_callback | |
3229 | }; | |
3230 | ||
e51060f0 SH |
3231 | static void cma_add_one(struct ib_device *device) |
3232 | { | |
3233 | struct cma_device *cma_dev; | |
3234 | struct rdma_id_private *id_priv; | |
3235 | ||
3236 | cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); | |
3237 | if (!cma_dev) | |
3238 | return; | |
3239 | ||
3240 | cma_dev->device = device; | |
e51060f0 SH |
3241 | |
3242 | init_completion(&cma_dev->comp); | |
3243 | atomic_set(&cma_dev->refcount, 1); | |
3244 | INIT_LIST_HEAD(&cma_dev->id_list); | |
3245 | ib_set_client_data(device, &cma_client, cma_dev); | |
3246 | ||
3247 | mutex_lock(&lock); | |
3248 | list_add_tail(&cma_dev->list, &dev_list); | |
3249 | list_for_each_entry(id_priv, &listen_any_list, list) | |
3250 | cma_listen_on_dev(id_priv, cma_dev); | |
3251 | mutex_unlock(&lock); | |
e51060f0 SH |
3252 | } |
3253 | ||
3254 | static int cma_remove_id_dev(struct rdma_id_private *id_priv) | |
3255 | { | |
a1b1b61f | 3256 | struct rdma_cm_event event; |
550e5ca7 | 3257 | enum rdma_cm_state state; |
de910bd9 | 3258 | int ret = 0; |
e51060f0 SH |
3259 | |
3260 | /* Record that we want to remove the device */ | |
550e5ca7 NM |
3261 | state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL); |
3262 | if (state == RDMA_CM_DESTROYING) | |
e51060f0 SH |
3263 | return 0; |
3264 | ||
3265 | cma_cancel_operation(id_priv, state); | |
de910bd9 | 3266 | mutex_lock(&id_priv->handler_mutex); |
e51060f0 SH |
3267 | |
3268 | /* Check for destruction from another callback. */ | |
550e5ca7 | 3269 | if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL)) |
de910bd9 | 3270 | goto out; |
e51060f0 | 3271 | |
a1b1b61f SH |
3272 | memset(&event, 0, sizeof event); |
3273 | event.event = RDMA_CM_EVENT_DEVICE_REMOVAL; | |
de910bd9 OG |
3274 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
3275 | out: | |
3276 | mutex_unlock(&id_priv->handler_mutex); | |
3277 | return ret; | |
e51060f0 SH |
3278 | } |
3279 | ||
3280 | static void cma_process_remove(struct cma_device *cma_dev) | |
3281 | { | |
e51060f0 SH |
3282 | struct rdma_id_private *id_priv; |
3283 | int ret; | |
3284 | ||
e51060f0 SH |
3285 | mutex_lock(&lock); |
3286 | while (!list_empty(&cma_dev->id_list)) { | |
3287 | id_priv = list_entry(cma_dev->id_list.next, | |
3288 | struct rdma_id_private, list); | |
3289 | ||
d02d1f53 | 3290 | list_del(&id_priv->listen_list); |
94de178a | 3291 | list_del_init(&id_priv->list); |
e51060f0 SH |
3292 | atomic_inc(&id_priv->refcount); |
3293 | mutex_unlock(&lock); | |
3294 | ||
d02d1f53 | 3295 | ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv); |
e51060f0 SH |
3296 | cma_deref_id(id_priv); |
3297 | if (ret) | |
3298 | rdma_destroy_id(&id_priv->id); | |
3299 | ||
3300 | mutex_lock(&lock); | |
3301 | } | |
3302 | mutex_unlock(&lock); | |
3303 | ||
3304 | cma_deref_dev(cma_dev); | |
3305 | wait_for_completion(&cma_dev->comp); | |
3306 | } | |
3307 | ||
3308 | static void cma_remove_one(struct ib_device *device) | |
3309 | { | |
3310 | struct cma_device *cma_dev; | |
3311 | ||
3312 | cma_dev = ib_get_client_data(device, &cma_client); | |
3313 | if (!cma_dev) | |
3314 | return; | |
3315 | ||
3316 | mutex_lock(&lock); | |
3317 | list_del(&cma_dev->list); | |
3318 | mutex_unlock(&lock); | |
3319 | ||
3320 | cma_process_remove(cma_dev); | |
3321 | kfree(cma_dev); | |
3322 | } | |
3323 | ||
753f618a NM |
3324 | static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb) |
3325 | { | |
3326 | struct nlmsghdr *nlh; | |
3327 | struct rdma_cm_id_stats *id_stats; | |
3328 | struct rdma_id_private *id_priv; | |
3329 | struct rdma_cm_id *id = NULL; | |
3330 | struct cma_device *cma_dev; | |
3331 | int i_dev = 0, i_id = 0; | |
3332 | ||
3333 | /* | |
3334 | * We export all of the IDs as a sequence of messages. Each | |
3335 | * ID gets its own netlink message. | |
3336 | */ | |
3337 | mutex_lock(&lock); | |
3338 | ||
3339 | list_for_each_entry(cma_dev, &dev_list, list) { | |
3340 | if (i_dev < cb->args[0]) { | |
3341 | i_dev++; | |
3342 | continue; | |
3343 | } | |
3344 | ||
3345 | i_id = 0; | |
3346 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { | |
3347 | if (i_id < cb->args[1]) { | |
3348 | i_id++; | |
3349 | continue; | |
3350 | } | |
3351 | ||
3352 | id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq, | |
3353 | sizeof *id_stats, RDMA_NL_RDMA_CM, | |
3354 | RDMA_NL_RDMA_CM_ID_STATS); | |
3355 | if (!id_stats) | |
3356 | goto out; | |
3357 | ||
3358 | memset(id_stats, 0, sizeof *id_stats); | |
3359 | id = &id_priv->id; | |
3360 | id_stats->node_type = id->route.addr.dev_addr.dev_type; | |
3361 | id_stats->port_num = id->port_num; | |
3362 | id_stats->bound_dev_if = | |
3363 | id->route.addr.dev_addr.bound_dev_if; | |
3364 | ||
3365 | if (id->route.addr.src_addr.ss_family == AF_INET) { | |
3366 | if (ibnl_put_attr(skb, nlh, | |
3367 | sizeof(struct sockaddr_in), | |
3368 | &id->route.addr.src_addr, | |
3369 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) { | |
3370 | goto out; | |
3371 | } | |
3372 | if (ibnl_put_attr(skb, nlh, | |
3373 | sizeof(struct sockaddr_in), | |
3374 | &id->route.addr.dst_addr, | |
3375 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) { | |
3376 | goto out; | |
3377 | } | |
3378 | } else if (id->route.addr.src_addr.ss_family == AF_INET6) { | |
3379 | if (ibnl_put_attr(skb, nlh, | |
3380 | sizeof(struct sockaddr_in6), | |
3381 | &id->route.addr.src_addr, | |
3382 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) { | |
3383 | goto out; | |
3384 | } | |
3385 | if (ibnl_put_attr(skb, nlh, | |
3386 | sizeof(struct sockaddr_in6), | |
3387 | &id->route.addr.dst_addr, | |
3388 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) { | |
3389 | goto out; | |
3390 | } | |
3391 | } | |
3392 | ||
83e9502d | 3393 | id_stats->pid = id_priv->owner; |
753f618a NM |
3394 | id_stats->port_space = id->ps; |
3395 | id_stats->cm_state = id_priv->state; | |
3396 | id_stats->qp_num = id_priv->qp_num; | |
3397 | id_stats->qp_type = id->qp_type; | |
3398 | ||
3399 | i_id++; | |
3400 | } | |
3401 | ||
3402 | cb->args[1] = 0; | |
3403 | i_dev++; | |
3404 | } | |
3405 | ||
3406 | out: | |
3407 | mutex_unlock(&lock); | |
3408 | cb->args[0] = i_dev; | |
3409 | cb->args[1] = i_id; | |
3410 | ||
3411 | return skb->len; | |
3412 | } | |
3413 | ||
3414 | static const struct ibnl_client_cbs cma_cb_table[] = { | |
3415 | [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats }, | |
3416 | }; | |
3417 | ||
716abb1f | 3418 | static int __init cma_init(void) |
e51060f0 | 3419 | { |
5d7220e8 | 3420 | int ret; |
227b60f5 | 3421 | |
c7f743a6 | 3422 | cma_wq = create_singlethread_workqueue("rdma_cm"); |
e51060f0 SH |
3423 | if (!cma_wq) |
3424 | return -ENOMEM; | |
3425 | ||
c1a0b23b | 3426 | ib_sa_register_client(&sa_client); |
7a118df3 | 3427 | rdma_addr_register_client(&addr_client); |
dd5bdff8 | 3428 | register_netdevice_notifier(&cma_nb); |
c1a0b23b | 3429 | |
e51060f0 SH |
3430 | ret = ib_register_client(&cma_client); |
3431 | if (ret) | |
3432 | goto err; | |
753f618a NM |
3433 | |
3434 | if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table)) | |
3435 | printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n"); | |
3436 | ||
e51060f0 SH |
3437 | return 0; |
3438 | ||
3439 | err: | |
dd5bdff8 | 3440 | unregister_netdevice_notifier(&cma_nb); |
7a118df3 | 3441 | rdma_addr_unregister_client(&addr_client); |
c1a0b23b | 3442 | ib_sa_unregister_client(&sa_client); |
e51060f0 SH |
3443 | destroy_workqueue(cma_wq); |
3444 | return ret; | |
3445 | } | |
3446 | ||
716abb1f | 3447 | static void __exit cma_cleanup(void) |
e51060f0 | 3448 | { |
753f618a | 3449 | ibnl_remove_client(RDMA_NL_RDMA_CM); |
e51060f0 | 3450 | ib_unregister_client(&cma_client); |
dd5bdff8 | 3451 | unregister_netdevice_notifier(&cma_nb); |
7a118df3 | 3452 | rdma_addr_unregister_client(&addr_client); |
c1a0b23b | 3453 | ib_sa_unregister_client(&sa_client); |
e51060f0 SH |
3454 | destroy_workqueue(cma_wq); |
3455 | idr_destroy(&sdp_ps); | |
3456 | idr_destroy(&tcp_ps); | |
628e5f6d | 3457 | idr_destroy(&udp_ps); |
c8f6a362 | 3458 | idr_destroy(&ipoib_ps); |
e51060f0 SH |
3459 | } |
3460 | ||
3461 | module_init(cma_init); | |
3462 | module_exit(cma_cleanup); |