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