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 | ||
9595480c HS |
1182 | static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event) |
1183 | { | |
1184 | return (((ib_event->event == IB_CM_REQ_RECEIVED) || | |
1185 | (ib_event->param.req_rcvd.qp_type == id->qp_type)) || | |
1186 | ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) && | |
1187 | (id->qp_type == IB_QPT_UD)) || | |
1188 | (!id->qp_type)); | |
1189 | } | |
1190 | ||
e51060f0 SH |
1191 | static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
1192 | { | |
1193 | struct rdma_id_private *listen_id, *conn_id; | |
a1b1b61f | 1194 | struct rdma_cm_event event; |
e51060f0 SH |
1195 | int offset, ret; |
1196 | ||
1197 | listen_id = cm_id->context; | |
9595480c HS |
1198 | if (!cma_check_req_qp_type(&listen_id->id, ib_event)) |
1199 | return -EINVAL; | |
1200 | ||
550e5ca7 | 1201 | if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) |
8aa08602 | 1202 | return -ECONNABORTED; |
e51060f0 | 1203 | |
628e5f6d SH |
1204 | memset(&event, 0, sizeof event); |
1205 | offset = cma_user_data_offset(listen_id->id.ps); | |
1206 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; | |
9595480c | 1207 | if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) { |
628e5f6d SH |
1208 | conn_id = cma_new_udp_id(&listen_id->id, ib_event); |
1209 | event.param.ud.private_data = ib_event->private_data + offset; | |
1210 | event.param.ud.private_data_len = | |
1211 | IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset; | |
1212 | } else { | |
1213 | conn_id = cma_new_conn_id(&listen_id->id, ib_event); | |
1214 | cma_set_req_event_data(&event, &ib_event->param.req_rcvd, | |
1215 | ib_event->private_data, offset); | |
1216 | } | |
e51060f0 SH |
1217 | if (!conn_id) { |
1218 | ret = -ENOMEM; | |
1219 | goto out; | |
1220 | } | |
1221 | ||
de910bd9 | 1222 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
07ebafba | 1223 | ret = cma_acquire_dev(conn_id); |
a1a733f6 KK |
1224 | if (ret) |
1225 | goto release_conn_id; | |
e51060f0 SH |
1226 | |
1227 | conn_id->cm_id.ib = cm_id; | |
1228 | cm_id->context = conn_id; | |
1229 | cm_id->cm_handler = cma_ib_handler; | |
1230 | ||
25ae21a1 SH |
1231 | /* |
1232 | * Protect against the user destroying conn_id from another thread | |
1233 | * until we're done accessing it. | |
1234 | */ | |
1235 | atomic_inc(&conn_id->refcount); | |
a1b1b61f | 1236 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
45d9478d | 1237 | if (!ret) { |
ead595ae SH |
1238 | /* |
1239 | * Acquire mutex to prevent user executing rdma_destroy_id() | |
1240 | * while we're accessing the cm_id. | |
1241 | */ | |
1242 | mutex_lock(&lock); | |
b26f9b99 | 1243 | if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD)) |
ead595ae SH |
1244 | ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); |
1245 | mutex_unlock(&lock); | |
de910bd9 | 1246 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 1247 | cma_deref_id(conn_id); |
a1a733f6 | 1248 | goto out; |
45d9478d | 1249 | } |
25ae21a1 | 1250 | cma_deref_id(conn_id); |
a1a733f6 KK |
1251 | |
1252 | /* Destroy the CM ID by returning a non-zero value. */ | |
1253 | conn_id->cm_id.ib = NULL; | |
1254 | ||
1255 | release_conn_id: | |
550e5ca7 | 1256 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
de910bd9 | 1257 | mutex_unlock(&conn_id->handler_mutex); |
a1a733f6 KK |
1258 | rdma_destroy_id(&conn_id->id); |
1259 | ||
e51060f0 | 1260 | out: |
de910bd9 | 1261 | mutex_unlock(&listen_id->handler_mutex); |
e51060f0 SH |
1262 | return ret; |
1263 | } | |
1264 | ||
1265 | static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr) | |
1266 | { | |
628e5f6d | 1267 | return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr))); |
e51060f0 SH |
1268 | } |
1269 | ||
1270 | static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr, | |
1271 | struct ib_cm_compare_data *compare) | |
1272 | { | |
1273 | struct cma_hdr *cma_data, *cma_mask; | |
1274 | struct sdp_hh *sdp_data, *sdp_mask; | |
1b90c137 | 1275 | __be32 ip4_addr; |
e51060f0 SH |
1276 | struct in6_addr ip6_addr; |
1277 | ||
1278 | memset(compare, 0, sizeof *compare); | |
1279 | cma_data = (void *) compare->data; | |
1280 | cma_mask = (void *) compare->mask; | |
1281 | sdp_data = (void *) compare->data; | |
1282 | sdp_mask = (void *) compare->mask; | |
1283 | ||
1284 | switch (addr->sa_family) { | |
1285 | case AF_INET: | |
1286 | ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr; | |
1287 | if (ps == RDMA_PS_SDP) { | |
1288 | sdp_set_ip_ver(sdp_data, 4); | |
1289 | sdp_set_ip_ver(sdp_mask, 0xF); | |
1290 | sdp_data->dst_addr.ip4.addr = ip4_addr; | |
1b90c137 | 1291 | sdp_mask->dst_addr.ip4.addr = htonl(~0); |
e51060f0 SH |
1292 | } else { |
1293 | cma_set_ip_ver(cma_data, 4); | |
1294 | cma_set_ip_ver(cma_mask, 0xF); | |
1295 | cma_data->dst_addr.ip4.addr = ip4_addr; | |
1b90c137 | 1296 | cma_mask->dst_addr.ip4.addr = htonl(~0); |
e51060f0 SH |
1297 | } |
1298 | break; | |
1299 | case AF_INET6: | |
1300 | ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr; | |
1301 | if (ps == RDMA_PS_SDP) { | |
1302 | sdp_set_ip_ver(sdp_data, 6); | |
1303 | sdp_set_ip_ver(sdp_mask, 0xF); | |
1304 | sdp_data->dst_addr.ip6 = ip6_addr; | |
1305 | memset(&sdp_mask->dst_addr.ip6, 0xFF, | |
1306 | sizeof sdp_mask->dst_addr.ip6); | |
1307 | } else { | |
1308 | cma_set_ip_ver(cma_data, 6); | |
1309 | cma_set_ip_ver(cma_mask, 0xF); | |
1310 | cma_data->dst_addr.ip6 = ip6_addr; | |
1311 | memset(&cma_mask->dst_addr.ip6, 0xFF, | |
1312 | sizeof cma_mask->dst_addr.ip6); | |
1313 | } | |
1314 | break; | |
1315 | default: | |
1316 | break; | |
1317 | } | |
1318 | } | |
1319 | ||
07ebafba TT |
1320 | static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) |
1321 | { | |
1322 | struct rdma_id_private *id_priv = iw_id->context; | |
a1b1b61f | 1323 | struct rdma_cm_event event; |
07ebafba TT |
1324 | struct sockaddr_in *sin; |
1325 | int ret = 0; | |
1326 | ||
550e5ca7 | 1327 | if (cma_disable_callback(id_priv, RDMA_CM_CONNECT)) |
be65f086 | 1328 | return 0; |
07ebafba | 1329 | |
be65f086 | 1330 | memset(&event, 0, sizeof event); |
07ebafba TT |
1331 | switch (iw_event->event) { |
1332 | case IW_CM_EVENT_CLOSE: | |
a1b1b61f | 1333 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
07ebafba TT |
1334 | break; |
1335 | case IW_CM_EVENT_CONNECT_REPLY: | |
1336 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1337 | *sin = iw_event->local_addr; | |
1338 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr; | |
1339 | *sin = iw_event->remote_addr; | |
881a045f SW |
1340 | switch (iw_event->status) { |
1341 | case 0: | |
a1b1b61f | 1342 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
881a045f SW |
1343 | break; |
1344 | case -ECONNRESET: | |
1345 | case -ECONNREFUSED: | |
1346 | event.event = RDMA_CM_EVENT_REJECTED; | |
1347 | break; | |
1348 | case -ETIMEDOUT: | |
1349 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
1350 | break; | |
1351 | default: | |
1352 | event.event = RDMA_CM_EVENT_CONNECT_ERROR; | |
1353 | break; | |
1354 | } | |
07ebafba TT |
1355 | break; |
1356 | case IW_CM_EVENT_ESTABLISHED: | |
a1b1b61f | 1357 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
07ebafba TT |
1358 | break; |
1359 | default: | |
1360 | BUG_ON(1); | |
1361 | } | |
1362 | ||
a1b1b61f SH |
1363 | event.status = iw_event->status; |
1364 | event.param.conn.private_data = iw_event->private_data; | |
1365 | event.param.conn.private_data_len = iw_event->private_data_len; | |
1366 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
07ebafba TT |
1367 | if (ret) { |
1368 | /* Destroy the CM ID by returning a non-zero value. */ | |
1369 | id_priv->cm_id.iw = NULL; | |
550e5ca7 | 1370 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 1371 | mutex_unlock(&id_priv->handler_mutex); |
07ebafba TT |
1372 | rdma_destroy_id(&id_priv->id); |
1373 | return ret; | |
1374 | } | |
1375 | ||
de910bd9 | 1376 | mutex_unlock(&id_priv->handler_mutex); |
07ebafba TT |
1377 | return ret; |
1378 | } | |
1379 | ||
1380 | static int iw_conn_req_handler(struct iw_cm_id *cm_id, | |
1381 | struct iw_cm_event *iw_event) | |
1382 | { | |
1383 | struct rdma_cm_id *new_cm_id; | |
1384 | struct rdma_id_private *listen_id, *conn_id; | |
1385 | struct sockaddr_in *sin; | |
1386 | struct net_device *dev = NULL; | |
a1b1b61f | 1387 | struct rdma_cm_event event; |
07ebafba | 1388 | int ret; |
8d8293cf | 1389 | struct ib_device_attr attr; |
07ebafba TT |
1390 | |
1391 | listen_id = cm_id->context; | |
550e5ca7 | 1392 | if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) |
8aa08602 | 1393 | return -ECONNABORTED; |
07ebafba TT |
1394 | |
1395 | /* Create a new RDMA id for the new IW CM ID */ | |
1396 | new_cm_id = rdma_create_id(listen_id->id.event_handler, | |
1397 | listen_id->id.context, | |
b26f9b99 | 1398 | RDMA_PS_TCP, IB_QPT_RC); |
10f32065 | 1399 | if (IS_ERR(new_cm_id)) { |
07ebafba TT |
1400 | ret = -ENOMEM; |
1401 | goto out; | |
1402 | } | |
1403 | conn_id = container_of(new_cm_id, struct rdma_id_private, id); | |
de910bd9 | 1404 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
550e5ca7 | 1405 | conn_id->state = RDMA_CM_CONNECT; |
07ebafba | 1406 | |
1ab35276 | 1407 | dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr); |
07ebafba TT |
1408 | if (!dev) { |
1409 | ret = -EADDRNOTAVAIL; | |
de910bd9 | 1410 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
1411 | rdma_destroy_id(new_cm_id); |
1412 | goto out; | |
1413 | } | |
1414 | ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL); | |
1415 | if (ret) { | |
de910bd9 | 1416 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
1417 | rdma_destroy_id(new_cm_id); |
1418 | goto out; | |
1419 | } | |
1420 | ||
1421 | ret = cma_acquire_dev(conn_id); | |
1422 | if (ret) { | |
de910bd9 | 1423 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
1424 | rdma_destroy_id(new_cm_id); |
1425 | goto out; | |
1426 | } | |
1427 | ||
1428 | conn_id->cm_id.iw = cm_id; | |
1429 | cm_id->context = conn_id; | |
1430 | cm_id->cm_handler = cma_iw_handler; | |
1431 | ||
1432 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr; | |
1433 | *sin = iw_event->local_addr; | |
1434 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr; | |
1435 | *sin = iw_event->remote_addr; | |
1436 | ||
8d8293cf SW |
1437 | ret = ib_query_device(conn_id->id.device, &attr); |
1438 | if (ret) { | |
de910bd9 | 1439 | mutex_unlock(&conn_id->handler_mutex); |
8d8293cf SW |
1440 | rdma_destroy_id(new_cm_id); |
1441 | goto out; | |
1442 | } | |
1443 | ||
a1b1b61f SH |
1444 | memset(&event, 0, sizeof event); |
1445 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; | |
1446 | event.param.conn.private_data = iw_event->private_data; | |
1447 | event.param.conn.private_data_len = iw_event->private_data_len; | |
8d8293cf SW |
1448 | event.param.conn.initiator_depth = attr.max_qp_init_rd_atom; |
1449 | event.param.conn.responder_resources = attr.max_qp_rd_atom; | |
25ae21a1 SH |
1450 | |
1451 | /* | |
1452 | * Protect against the user destroying conn_id from another thread | |
1453 | * until we're done accessing it. | |
1454 | */ | |
1455 | atomic_inc(&conn_id->refcount); | |
a1b1b61f | 1456 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
07ebafba TT |
1457 | if (ret) { |
1458 | /* User wants to destroy the CM ID */ | |
1459 | conn_id->cm_id.iw = NULL; | |
550e5ca7 | 1460 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
de910bd9 | 1461 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 1462 | cma_deref_id(conn_id); |
07ebafba | 1463 | rdma_destroy_id(&conn_id->id); |
de910bd9 | 1464 | goto out; |
07ebafba TT |
1465 | } |
1466 | ||
de910bd9 | 1467 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 1468 | cma_deref_id(conn_id); |
de910bd9 | 1469 | |
07ebafba TT |
1470 | out: |
1471 | if (dev) | |
1472 | dev_put(dev); | |
de910bd9 | 1473 | mutex_unlock(&listen_id->handler_mutex); |
07ebafba TT |
1474 | return ret; |
1475 | } | |
1476 | ||
e51060f0 SH |
1477 | static int cma_ib_listen(struct rdma_id_private *id_priv) |
1478 | { | |
1479 | struct ib_cm_compare_data compare_data; | |
1480 | struct sockaddr *addr; | |
0c9361fc | 1481 | struct ib_cm_id *id; |
e51060f0 SH |
1482 | __be64 svc_id; |
1483 | int ret; | |
1484 | ||
0c9361fc JM |
1485 | id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv); |
1486 | if (IS_ERR(id)) | |
1487 | return PTR_ERR(id); | |
1488 | ||
1489 | id_priv->cm_id.ib = id; | |
e51060f0 | 1490 | |
3f446754 | 1491 | addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
e51060f0 SH |
1492 | svc_id = cma_get_service_id(id_priv->id.ps, addr); |
1493 | if (cma_any_addr(addr)) | |
1494 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL); | |
1495 | else { | |
1496 | cma_set_compare_data(id_priv->id.ps, addr, &compare_data); | |
1497 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data); | |
1498 | } | |
1499 | ||
1500 | if (ret) { | |
1501 | ib_destroy_cm_id(id_priv->cm_id.ib); | |
1502 | id_priv->cm_id.ib = NULL; | |
1503 | } | |
1504 | ||
1505 | return ret; | |
1506 | } | |
1507 | ||
07ebafba TT |
1508 | static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) |
1509 | { | |
1510 | int ret; | |
1511 | struct sockaddr_in *sin; | |
0c9361fc JM |
1512 | struct iw_cm_id *id; |
1513 | ||
1514 | id = iw_create_cm_id(id_priv->id.device, | |
1515 | iw_conn_req_handler, | |
1516 | id_priv); | |
1517 | if (IS_ERR(id)) | |
1518 | return PTR_ERR(id); | |
07ebafba | 1519 | |
0c9361fc | 1520 | id_priv->cm_id.iw = id; |
07ebafba TT |
1521 | |
1522 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
1523 | id_priv->cm_id.iw->local_addr = *sin; | |
1524 | ||
1525 | ret = iw_cm_listen(id_priv->cm_id.iw, backlog); | |
1526 | ||
1527 | if (ret) { | |
1528 | iw_destroy_cm_id(id_priv->cm_id.iw); | |
1529 | id_priv->cm_id.iw = NULL; | |
1530 | } | |
1531 | ||
1532 | return ret; | |
1533 | } | |
1534 | ||
e51060f0 SH |
1535 | static int cma_listen_handler(struct rdma_cm_id *id, |
1536 | struct rdma_cm_event *event) | |
1537 | { | |
1538 | struct rdma_id_private *id_priv = id->context; | |
1539 | ||
1540 | id->context = id_priv->id.context; | |
1541 | id->event_handler = id_priv->id.event_handler; | |
1542 | return id_priv->id.event_handler(id, event); | |
1543 | } | |
1544 | ||
1545 | static void cma_listen_on_dev(struct rdma_id_private *id_priv, | |
1546 | struct cma_device *cma_dev) | |
1547 | { | |
1548 | struct rdma_id_private *dev_id_priv; | |
1549 | struct rdma_cm_id *id; | |
1550 | int ret; | |
1551 | ||
b26f9b99 SH |
1552 | id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps, |
1553 | id_priv->id.qp_type); | |
e51060f0 SH |
1554 | if (IS_ERR(id)) |
1555 | return; | |
1556 | ||
1557 | dev_id_priv = container_of(id, struct rdma_id_private, id); | |
1558 | ||
550e5ca7 | 1559 | dev_id_priv->state = RDMA_CM_ADDR_BOUND; |
e51060f0 | 1560 | memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr, |
3f446754 | 1561 | ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr)); |
e51060f0 SH |
1562 | |
1563 | cma_attach_to_dev(dev_id_priv, cma_dev); | |
1564 | list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); | |
d02d1f53 SH |
1565 | atomic_inc(&id_priv->refcount); |
1566 | dev_id_priv->internal_id = 1; | |
e51060f0 SH |
1567 | |
1568 | ret = rdma_listen(id, id_priv->backlog); | |
1569 | if (ret) | |
d02d1f53 | 1570 | printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, " |
468f2239 | 1571 | "listening on device %s\n", ret, cma_dev->device->name); |
e51060f0 SH |
1572 | } |
1573 | ||
1574 | static void cma_listen_on_all(struct rdma_id_private *id_priv) | |
1575 | { | |
1576 | struct cma_device *cma_dev; | |
1577 | ||
1578 | mutex_lock(&lock); | |
1579 | list_add_tail(&id_priv->list, &listen_any_list); | |
1580 | list_for_each_entry(cma_dev, &dev_list, list) | |
1581 | cma_listen_on_dev(id_priv, cma_dev); | |
1582 | mutex_unlock(&lock); | |
1583 | } | |
1584 | ||
a81c994d SH |
1585 | void rdma_set_service_type(struct rdma_cm_id *id, int tos) |
1586 | { | |
1587 | struct rdma_id_private *id_priv; | |
1588 | ||
1589 | id_priv = container_of(id, struct rdma_id_private, id); | |
1590 | id_priv->tos = (u8) tos; | |
1591 | } | |
1592 | EXPORT_SYMBOL(rdma_set_service_type); | |
1593 | ||
e51060f0 SH |
1594 | static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec, |
1595 | void *context) | |
1596 | { | |
1597 | struct cma_work *work = context; | |
1598 | struct rdma_route *route; | |
1599 | ||
1600 | route = &work->id->id.route; | |
1601 | ||
1602 | if (!status) { | |
1603 | route->num_paths = 1; | |
1604 | *route->path_rec = *path_rec; | |
1605 | } else { | |
550e5ca7 NM |
1606 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1607 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
e51060f0 | 1608 | work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; |
8f0472d3 | 1609 | work->event.status = status; |
e51060f0 SH |
1610 | } |
1611 | ||
1612 | queue_work(cma_wq, &work->work); | |
1613 | } | |
1614 | ||
1615 | static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms, | |
1616 | struct cma_work *work) | |
1617 | { | |
a81c994d | 1618 | struct rdma_addr *addr = &id_priv->id.route.addr; |
e51060f0 | 1619 | struct ib_sa_path_rec path_rec; |
a81c994d SH |
1620 | ib_sa_comp_mask comp_mask; |
1621 | struct sockaddr_in6 *sin6; | |
e51060f0 SH |
1622 | |
1623 | memset(&path_rec, 0, sizeof path_rec); | |
6f8372b6 SH |
1624 | rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid); |
1625 | rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid); | |
a81c994d | 1626 | path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr)); |
e51060f0 | 1627 | path_rec.numb_path = 1; |
962063e6 | 1628 | path_rec.reversible = 1; |
3f446754 RD |
1629 | path_rec.service_id = cma_get_service_id(id_priv->id.ps, |
1630 | (struct sockaddr *) &addr->dst_addr); | |
a81c994d SH |
1631 | |
1632 | comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | | |
1633 | IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH | | |
1634 | IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID; | |
1635 | ||
3f446754 | 1636 | if (addr->src_addr.ss_family == AF_INET) { |
a81c994d SH |
1637 | path_rec.qos_class = cpu_to_be16((u16) id_priv->tos); |
1638 | comp_mask |= IB_SA_PATH_REC_QOS_CLASS; | |
1639 | } else { | |
1640 | sin6 = (struct sockaddr_in6 *) &addr->src_addr; | |
1641 | path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20); | |
1642 | comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; | |
1643 | } | |
e51060f0 | 1644 | |
c1a0b23b | 1645 | id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device, |
a81c994d SH |
1646 | id_priv->id.port_num, &path_rec, |
1647 | comp_mask, timeout_ms, | |
1648 | GFP_KERNEL, cma_query_handler, | |
1649 | work, &id_priv->query); | |
e51060f0 SH |
1650 | |
1651 | return (id_priv->query_id < 0) ? id_priv->query_id : 0; | |
1652 | } | |
1653 | ||
c4028958 | 1654 | static void cma_work_handler(struct work_struct *_work) |
e51060f0 | 1655 | { |
c4028958 | 1656 | struct cma_work *work = container_of(_work, struct cma_work, work); |
e51060f0 SH |
1657 | struct rdma_id_private *id_priv = work->id; |
1658 | int destroy = 0; | |
1659 | ||
de910bd9 | 1660 | mutex_lock(&id_priv->handler_mutex); |
e51060f0 SH |
1661 | if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) |
1662 | goto out; | |
1663 | ||
1664 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
550e5ca7 | 1665 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
e51060f0 SH |
1666 | destroy = 1; |
1667 | } | |
1668 | out: | |
de910bd9 | 1669 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1670 | cma_deref_id(id_priv); |
1671 | if (destroy) | |
1672 | rdma_destroy_id(&id_priv->id); | |
1673 | kfree(work); | |
1674 | } | |
1675 | ||
dd5bdff8 OG |
1676 | static void cma_ndev_work_handler(struct work_struct *_work) |
1677 | { | |
1678 | struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work); | |
1679 | struct rdma_id_private *id_priv = work->id; | |
1680 | int destroy = 0; | |
1681 | ||
1682 | mutex_lock(&id_priv->handler_mutex); | |
550e5ca7 NM |
1683 | if (id_priv->state == RDMA_CM_DESTROYING || |
1684 | id_priv->state == RDMA_CM_DEVICE_REMOVAL) | |
dd5bdff8 OG |
1685 | goto out; |
1686 | ||
1687 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
550e5ca7 | 1688 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
dd5bdff8 OG |
1689 | destroy = 1; |
1690 | } | |
1691 | ||
1692 | out: | |
1693 | mutex_unlock(&id_priv->handler_mutex); | |
1694 | cma_deref_id(id_priv); | |
1695 | if (destroy) | |
1696 | rdma_destroy_id(&id_priv->id); | |
1697 | kfree(work); | |
1698 | } | |
1699 | ||
e51060f0 SH |
1700 | static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms) |
1701 | { | |
1702 | struct rdma_route *route = &id_priv->id.route; | |
1703 | struct cma_work *work; | |
1704 | int ret; | |
1705 | ||
1706 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1707 | if (!work) | |
1708 | return -ENOMEM; | |
1709 | ||
1710 | work->id = id_priv; | |
c4028958 | 1711 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
1712 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1713 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
e51060f0 SH |
1714 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
1715 | ||
1716 | route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); | |
1717 | if (!route->path_rec) { | |
1718 | ret = -ENOMEM; | |
1719 | goto err1; | |
1720 | } | |
1721 | ||
1722 | ret = cma_query_ib_route(id_priv, timeout_ms, work); | |
1723 | if (ret) | |
1724 | goto err2; | |
1725 | ||
1726 | return 0; | |
1727 | err2: | |
1728 | kfree(route->path_rec); | |
1729 | route->path_rec = NULL; | |
1730 | err1: | |
1731 | kfree(work); | |
1732 | return ret; | |
1733 | } | |
1734 | ||
1735 | int rdma_set_ib_paths(struct rdma_cm_id *id, | |
1736 | struct ib_sa_path_rec *path_rec, int num_paths) | |
1737 | { | |
1738 | struct rdma_id_private *id_priv; | |
1739 | int ret; | |
1740 | ||
1741 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 NM |
1742 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
1743 | RDMA_CM_ROUTE_RESOLVED)) | |
e51060f0 SH |
1744 | return -EINVAL; |
1745 | ||
9893e742 JL |
1746 | id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths, |
1747 | GFP_KERNEL); | |
e51060f0 SH |
1748 | if (!id->route.path_rec) { |
1749 | ret = -ENOMEM; | |
1750 | goto err; | |
1751 | } | |
1752 | ||
ae2d9293 | 1753 | id->route.num_paths = num_paths; |
e51060f0 SH |
1754 | return 0; |
1755 | err: | |
550e5ca7 | 1756 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED); |
e51060f0 SH |
1757 | return ret; |
1758 | } | |
1759 | EXPORT_SYMBOL(rdma_set_ib_paths); | |
1760 | ||
07ebafba TT |
1761 | static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms) |
1762 | { | |
1763 | struct cma_work *work; | |
1764 | ||
1765 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1766 | if (!work) | |
1767 | return -ENOMEM; | |
1768 | ||
1769 | work->id = id_priv; | |
c4028958 | 1770 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
1771 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1772 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
07ebafba TT |
1773 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
1774 | queue_work(cma_wq, &work->work); | |
1775 | return 0; | |
1776 | } | |
1777 | ||
3c86aa70 EC |
1778 | static int cma_resolve_iboe_route(struct rdma_id_private *id_priv) |
1779 | { | |
1780 | struct rdma_route *route = &id_priv->id.route; | |
1781 | struct rdma_addr *addr = &route->addr; | |
1782 | struct cma_work *work; | |
1783 | int ret; | |
1784 | struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr; | |
1785 | struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr; | |
1786 | struct net_device *ndev = NULL; | |
af7bd463 | 1787 | u16 vid; |
3c86aa70 EC |
1788 | |
1789 | if (src_addr->sin_family != dst_addr->sin_family) | |
1790 | return -EINVAL; | |
1791 | ||
1792 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1793 | if (!work) | |
1794 | return -ENOMEM; | |
1795 | ||
1796 | work->id = id_priv; | |
1797 | INIT_WORK(&work->work, cma_work_handler); | |
1798 | ||
1799 | route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL); | |
1800 | if (!route->path_rec) { | |
1801 | ret = -ENOMEM; | |
1802 | goto err1; | |
1803 | } | |
1804 | ||
1805 | route->num_paths = 1; | |
1806 | ||
3c86aa70 EC |
1807 | if (addr->dev_addr.bound_dev_if) |
1808 | ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if); | |
1809 | if (!ndev) { | |
1810 | ret = -ENODEV; | |
1811 | goto err2; | |
1812 | } | |
1813 | ||
af7bd463 EC |
1814 | vid = rdma_vlan_dev_vlan_id(ndev); |
1815 | ||
1816 | iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid); | |
1817 | iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid); | |
1818 | ||
1819 | route->path_rec->hop_limit = 1; | |
1820 | route->path_rec->reversible = 1; | |
1821 | route->path_rec->pkey = cpu_to_be16(0xffff); | |
1822 | route->path_rec->mtu_selector = IB_SA_EQ; | |
1823 | route->path_rec->sl = id_priv->tos >> 5; | |
1824 | ||
3c86aa70 EC |
1825 | route->path_rec->mtu = iboe_get_mtu(ndev->mtu); |
1826 | route->path_rec->rate_selector = IB_SA_EQ; | |
1827 | route->path_rec->rate = iboe_get_rate(ndev); | |
1828 | dev_put(ndev); | |
1829 | route->path_rec->packet_life_time_selector = IB_SA_EQ; | |
1830 | route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME; | |
1831 | if (!route->path_rec->mtu) { | |
1832 | ret = -EINVAL; | |
1833 | goto err2; | |
1834 | } | |
1835 | ||
550e5ca7 NM |
1836 | work->old_state = RDMA_CM_ROUTE_QUERY; |
1837 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
3c86aa70 EC |
1838 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
1839 | work->event.status = 0; | |
1840 | ||
1841 | queue_work(cma_wq, &work->work); | |
1842 | ||
1843 | return 0; | |
1844 | ||
1845 | err2: | |
1846 | kfree(route->path_rec); | |
1847 | route->path_rec = NULL; | |
1848 | err1: | |
1849 | kfree(work); | |
1850 | return ret; | |
1851 | } | |
1852 | ||
e51060f0 SH |
1853 | int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) |
1854 | { | |
1855 | struct rdma_id_private *id_priv; | |
1856 | int ret; | |
1857 | ||
1858 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 1859 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY)) |
e51060f0 SH |
1860 | return -EINVAL; |
1861 | ||
1862 | atomic_inc(&id_priv->refcount); | |
07ebafba TT |
1863 | switch (rdma_node_get_transport(id->device->node_type)) { |
1864 | case RDMA_TRANSPORT_IB: | |
3c86aa70 EC |
1865 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
1866 | case IB_LINK_LAYER_INFINIBAND: | |
1867 | ret = cma_resolve_ib_route(id_priv, timeout_ms); | |
1868 | break; | |
1869 | case IB_LINK_LAYER_ETHERNET: | |
1870 | ret = cma_resolve_iboe_route(id_priv); | |
1871 | break; | |
1872 | default: | |
1873 | ret = -ENOSYS; | |
1874 | } | |
e51060f0 | 1875 | break; |
07ebafba TT |
1876 | case RDMA_TRANSPORT_IWARP: |
1877 | ret = cma_resolve_iw_route(id_priv, timeout_ms); | |
1878 | break; | |
e51060f0 SH |
1879 | default: |
1880 | ret = -ENOSYS; | |
1881 | break; | |
1882 | } | |
1883 | if (ret) | |
1884 | goto err; | |
1885 | ||
1886 | return 0; | |
1887 | err: | |
550e5ca7 | 1888 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED); |
e51060f0 SH |
1889 | cma_deref_id(id_priv); |
1890 | return ret; | |
1891 | } | |
1892 | EXPORT_SYMBOL(rdma_resolve_route); | |
1893 | ||
1894 | static int cma_bind_loopback(struct rdma_id_private *id_priv) | |
1895 | { | |
1896 | struct cma_device *cma_dev; | |
1897 | struct ib_port_attr port_attr; | |
f0ee3404 | 1898 | union ib_gid gid; |
e51060f0 SH |
1899 | u16 pkey; |
1900 | int ret; | |
1901 | u8 p; | |
1902 | ||
1903 | mutex_lock(&lock); | |
e82153b5 KK |
1904 | if (list_empty(&dev_list)) { |
1905 | ret = -ENODEV; | |
1906 | goto out; | |
1907 | } | |
e51060f0 SH |
1908 | list_for_each_entry(cma_dev, &dev_list, list) |
1909 | for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p) | |
e82153b5 | 1910 | if (!ib_query_port(cma_dev->device, p, &port_attr) && |
e51060f0 SH |
1911 | port_attr.state == IB_PORT_ACTIVE) |
1912 | goto port_found; | |
1913 | ||
e82153b5 KK |
1914 | p = 1; |
1915 | cma_dev = list_entry(dev_list.next, struct cma_device, list); | |
e51060f0 SH |
1916 | |
1917 | port_found: | |
f0ee3404 | 1918 | ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid); |
e51060f0 SH |
1919 | if (ret) |
1920 | goto out; | |
1921 | ||
1922 | ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); | |
1923 | if (ret) | |
1924 | goto out; | |
1925 | ||
6f8372b6 | 1926 | id_priv->id.route.addr.dev_addr.dev_type = |
3c86aa70 | 1927 | (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ? |
6f8372b6 SH |
1928 | ARPHRD_INFINIBAND : ARPHRD_ETHER; |
1929 | ||
1930 | rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 SH |
1931 | ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); |
1932 | id_priv->id.port_num = p; | |
1933 | cma_attach_to_dev(id_priv, cma_dev); | |
1934 | out: | |
1935 | mutex_unlock(&lock); | |
1936 | return ret; | |
1937 | } | |
1938 | ||
1939 | static void addr_handler(int status, struct sockaddr *src_addr, | |
1940 | struct rdma_dev_addr *dev_addr, void *context) | |
1941 | { | |
1942 | struct rdma_id_private *id_priv = context; | |
a1b1b61f | 1943 | struct rdma_cm_event event; |
e51060f0 | 1944 | |
a1b1b61f | 1945 | memset(&event, 0, sizeof event); |
de910bd9 | 1946 | mutex_lock(&id_priv->handler_mutex); |
550e5ca7 NM |
1947 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, |
1948 | RDMA_CM_ADDR_RESOLVED)) | |
61a73c70 | 1949 | goto out; |
61a73c70 SH |
1950 | |
1951 | if (!status && !id_priv->cma_dev) | |
e51060f0 SH |
1952 | status = cma_acquire_dev(id_priv); |
1953 | ||
1954 | if (status) { | |
550e5ca7 NM |
1955 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
1956 | RDMA_CM_ADDR_BOUND)) | |
e51060f0 | 1957 | goto out; |
a1b1b61f SH |
1958 | event.event = RDMA_CM_EVENT_ADDR_ERROR; |
1959 | event.status = status; | |
e51060f0 | 1960 | } else { |
e51060f0 SH |
1961 | memcpy(&id_priv->id.route.addr.src_addr, src_addr, |
1962 | ip_addr_size(src_addr)); | |
a1b1b61f | 1963 | event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
e51060f0 SH |
1964 | } |
1965 | ||
a1b1b61f | 1966 | if (id_priv->id.event_handler(&id_priv->id, &event)) { |
550e5ca7 | 1967 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 1968 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1969 | cma_deref_id(id_priv); |
1970 | rdma_destroy_id(&id_priv->id); | |
1971 | return; | |
1972 | } | |
1973 | out: | |
de910bd9 | 1974 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1975 | cma_deref_id(id_priv); |
1976 | } | |
1977 | ||
1978 | static int cma_resolve_loopback(struct rdma_id_private *id_priv) | |
1979 | { | |
1980 | struct cma_work *work; | |
6f8372b6 | 1981 | struct sockaddr *src, *dst; |
f0ee3404 | 1982 | union ib_gid gid; |
e51060f0 SH |
1983 | int ret; |
1984 | ||
1985 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
1986 | if (!work) | |
1987 | return -ENOMEM; | |
1988 | ||
1989 | if (!id_priv->cma_dev) { | |
1990 | ret = cma_bind_loopback(id_priv); | |
1991 | if (ret) | |
1992 | goto err; | |
1993 | } | |
1994 | ||
6f8372b6 SH |
1995 | rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
1996 | rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 | 1997 | |
6f8372b6 SH |
1998 | src = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
1999 | if (cma_zero_addr(src)) { | |
2000 | dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr; | |
2001 | if ((src->sa_family = dst->sa_family) == AF_INET) { | |
2002 | ((struct sockaddr_in *) src)->sin_addr.s_addr = | |
2003 | ((struct sockaddr_in *) dst)->sin_addr.s_addr; | |
2004 | } else { | |
2005 | ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr, | |
2006 | &((struct sockaddr_in6 *) dst)->sin6_addr); | |
2007 | } | |
e51060f0 SH |
2008 | } |
2009 | ||
2010 | work->id = id_priv; | |
c4028958 | 2011 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
2012 | work->old_state = RDMA_CM_ADDR_QUERY; |
2013 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
e51060f0 SH |
2014 | work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
2015 | queue_work(cma_wq, &work->work); | |
2016 | return 0; | |
2017 | err: | |
2018 | kfree(work); | |
2019 | return ret; | |
2020 | } | |
2021 | ||
2022 | static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
2023 | struct sockaddr *dst_addr) | |
2024 | { | |
d14714df SH |
2025 | if (!src_addr || !src_addr->sa_family) { |
2026 | src_addr = (struct sockaddr *) &id->route.addr.src_addr; | |
2027 | if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) { | |
2028 | ((struct sockaddr_in6 *) src_addr)->sin6_scope_id = | |
2029 | ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id; | |
2030 | } | |
2031 | } | |
2032 | return rdma_bind_addr(id, src_addr); | |
e51060f0 SH |
2033 | } |
2034 | ||
2035 | int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
2036 | struct sockaddr *dst_addr, int timeout_ms) | |
2037 | { | |
2038 | struct rdma_id_private *id_priv; | |
2039 | int ret; | |
2040 | ||
2041 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2042 | if (id_priv->state == RDMA_CM_IDLE) { |
e51060f0 SH |
2043 | ret = cma_bind_addr(id, src_addr, dst_addr); |
2044 | if (ret) | |
2045 | return ret; | |
2046 | } | |
2047 | ||
550e5ca7 | 2048 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) |
e51060f0 SH |
2049 | return -EINVAL; |
2050 | ||
2051 | atomic_inc(&id_priv->refcount); | |
2052 | memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr)); | |
2053 | if (cma_any_addr(dst_addr)) | |
2054 | ret = cma_resolve_loopback(id_priv); | |
2055 | else | |
3f446754 | 2056 | ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr, |
7a118df3 | 2057 | dst_addr, &id->route.addr.dev_addr, |
e51060f0 SH |
2058 | timeout_ms, addr_handler, id_priv); |
2059 | if (ret) | |
2060 | goto err; | |
2061 | ||
2062 | return 0; | |
2063 | err: | |
550e5ca7 | 2064 | cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND); |
e51060f0 SH |
2065 | cma_deref_id(id_priv); |
2066 | return ret; | |
2067 | } | |
2068 | EXPORT_SYMBOL(rdma_resolve_addr); | |
2069 | ||
a9bb7912 HS |
2070 | int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse) |
2071 | { | |
2072 | struct rdma_id_private *id_priv; | |
2073 | unsigned long flags; | |
2074 | int ret; | |
2075 | ||
2076 | id_priv = container_of(id, struct rdma_id_private, id); | |
2077 | spin_lock_irqsave(&id_priv->lock, flags); | |
550e5ca7 | 2078 | if (id_priv->state == RDMA_CM_IDLE) { |
a9bb7912 HS |
2079 | id_priv->reuseaddr = reuse; |
2080 | ret = 0; | |
2081 | } else { | |
2082 | ret = -EINVAL; | |
2083 | } | |
2084 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
2085 | return ret; | |
2086 | } | |
2087 | EXPORT_SYMBOL(rdma_set_reuseaddr); | |
2088 | ||
e51060f0 SH |
2089 | static void cma_bind_port(struct rdma_bind_list *bind_list, |
2090 | struct rdma_id_private *id_priv) | |
2091 | { | |
2092 | struct sockaddr_in *sin; | |
2093 | ||
2094 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; | |
2095 | sin->sin_port = htons(bind_list->port); | |
2096 | id_priv->bind_list = bind_list; | |
2097 | hlist_add_head(&id_priv->node, &bind_list->owners); | |
2098 | } | |
2099 | ||
2100 | static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv, | |
2101 | unsigned short snum) | |
2102 | { | |
2103 | struct rdma_bind_list *bind_list; | |
aedec080 | 2104 | int port, ret; |
e51060f0 | 2105 | |
cb164b8c | 2106 | bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); |
e51060f0 SH |
2107 | if (!bind_list) |
2108 | return -ENOMEM; | |
2109 | ||
aedec080 SH |
2110 | do { |
2111 | ret = idr_get_new_above(ps, bind_list, snum, &port); | |
2112 | } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL)); | |
2113 | ||
2114 | if (ret) | |
2115 | goto err1; | |
2116 | ||
2117 | if (port != snum) { | |
2118 | ret = -EADDRNOTAVAIL; | |
2119 | goto err2; | |
2120 | } | |
2121 | ||
2122 | bind_list->ps = ps; | |
2123 | bind_list->port = (unsigned short) port; | |
2124 | cma_bind_port(bind_list, id_priv); | |
2125 | return 0; | |
2126 | err2: | |
2127 | idr_remove(ps, port); | |
2128 | err1: | |
2129 | kfree(bind_list); | |
2130 | return ret; | |
2131 | } | |
e51060f0 | 2132 | |
aedec080 SH |
2133 | static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv) |
2134 | { | |
5d7220e8 TH |
2135 | static unsigned int last_used_port; |
2136 | int low, high, remaining; | |
2137 | unsigned int rover; | |
e51060f0 | 2138 | |
227b60f5 | 2139 | inet_get_local_port_range(&low, &high); |
5d7220e8 TH |
2140 | remaining = (high - low) + 1; |
2141 | rover = net_random() % remaining + low; | |
2142 | retry: | |
2143 | if (last_used_port != rover && | |
2144 | !idr_find(ps, (unsigned short) rover)) { | |
2145 | int ret = cma_alloc_port(ps, id_priv, rover); | |
2146 | /* | |
2147 | * Remember previously used port number in order to avoid | |
2148 | * re-using same port immediately after it is closed. | |
2149 | */ | |
2150 | if (!ret) | |
2151 | last_used_port = rover; | |
2152 | if (ret != -EADDRNOTAVAIL) | |
2153 | return ret; | |
e51060f0 | 2154 | } |
5d7220e8 TH |
2155 | if (--remaining) { |
2156 | rover++; | |
2157 | if ((rover < low) || (rover > high)) | |
2158 | rover = low; | |
2159 | goto retry; | |
2160 | } | |
2161 | return -EADDRNOTAVAIL; | |
e51060f0 SH |
2162 | } |
2163 | ||
a9bb7912 HS |
2164 | /* |
2165 | * Check that the requested port is available. This is called when trying to | |
2166 | * bind to a specific port, or when trying to listen on a bound port. In | |
2167 | * the latter case, the provided id_priv may already be on the bind_list, but | |
2168 | * we still need to check that it's okay to start listening. | |
2169 | */ | |
2170 | static int cma_check_port(struct rdma_bind_list *bind_list, | |
2171 | struct rdma_id_private *id_priv, uint8_t reuseaddr) | |
e51060f0 SH |
2172 | { |
2173 | struct rdma_id_private *cur_id; | |
43b752da | 2174 | struct sockaddr *addr, *cur_addr; |
e51060f0 | 2175 | struct hlist_node *node; |
e51060f0 | 2176 | |
43b752da | 2177 | addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
a9bb7912 | 2178 | if (cma_any_addr(addr) && !reuseaddr) |
e51060f0 SH |
2179 | return -EADDRNOTAVAIL; |
2180 | ||
2181 | hlist_for_each_entry(cur_id, node, &bind_list->owners, node) { | |
a9bb7912 HS |
2182 | if (id_priv == cur_id) |
2183 | continue; | |
3cd96564 | 2184 | |
550e5ca7 | 2185 | if ((cur_id->state == RDMA_CM_LISTEN) || |
a9bb7912 HS |
2186 | !reuseaddr || !cur_id->reuseaddr) { |
2187 | cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr; | |
2188 | if (cma_any_addr(cur_addr)) | |
2189 | return -EADDRNOTAVAIL; | |
e51060f0 | 2190 | |
a9bb7912 HS |
2191 | if (!cma_addr_cmp(addr, cur_addr)) |
2192 | return -EADDRINUSE; | |
2193 | } | |
2194 | } | |
e51060f0 SH |
2195 | return 0; |
2196 | } | |
2197 | ||
a9bb7912 HS |
2198 | static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv) |
2199 | { | |
2200 | struct rdma_bind_list *bind_list; | |
2201 | unsigned short snum; | |
2202 | int ret; | |
2203 | ||
2204 | snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr)); | |
2205 | if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) | |
2206 | return -EACCES; | |
2207 | ||
2208 | bind_list = idr_find(ps, snum); | |
2209 | if (!bind_list) { | |
2210 | ret = cma_alloc_port(ps, id_priv, snum); | |
2211 | } else { | |
2212 | ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr); | |
2213 | if (!ret) | |
2214 | cma_bind_port(bind_list, id_priv); | |
2215 | } | |
2216 | return ret; | |
2217 | } | |
2218 | ||
2219 | static int cma_bind_listen(struct rdma_id_private *id_priv) | |
2220 | { | |
2221 | struct rdma_bind_list *bind_list = id_priv->bind_list; | |
2222 | int ret = 0; | |
2223 | ||
2224 | mutex_lock(&lock); | |
2225 | if (bind_list->owners.first->next) | |
2226 | ret = cma_check_port(bind_list, id_priv, 0); | |
2227 | mutex_unlock(&lock); | |
2228 | return ret; | |
2229 | } | |
2230 | ||
e51060f0 SH |
2231 | static int cma_get_port(struct rdma_id_private *id_priv) |
2232 | { | |
2233 | struct idr *ps; | |
2234 | int ret; | |
2235 | ||
2236 | switch (id_priv->id.ps) { | |
2237 | case RDMA_PS_SDP: | |
2238 | ps = &sdp_ps; | |
2239 | break; | |
2240 | case RDMA_PS_TCP: | |
2241 | ps = &tcp_ps; | |
2242 | break; | |
628e5f6d SH |
2243 | case RDMA_PS_UDP: |
2244 | ps = &udp_ps; | |
2245 | break; | |
c8f6a362 SH |
2246 | case RDMA_PS_IPOIB: |
2247 | ps = &ipoib_ps; | |
2248 | break; | |
e51060f0 SH |
2249 | default: |
2250 | return -EPROTONOSUPPORT; | |
2251 | } | |
2252 | ||
2253 | mutex_lock(&lock); | |
3f446754 | 2254 | if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr)) |
aedec080 | 2255 | ret = cma_alloc_any_port(ps, id_priv); |
e51060f0 SH |
2256 | else |
2257 | ret = cma_use_port(ps, id_priv); | |
2258 | mutex_unlock(&lock); | |
2259 | ||
2260 | return ret; | |
2261 | } | |
2262 | ||
d14714df SH |
2263 | static int cma_check_linklocal(struct rdma_dev_addr *dev_addr, |
2264 | struct sockaddr *addr) | |
2265 | { | |
fd4582a3 | 2266 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
d14714df SH |
2267 | struct sockaddr_in6 *sin6; |
2268 | ||
2269 | if (addr->sa_family != AF_INET6) | |
2270 | return 0; | |
2271 | ||
2272 | sin6 = (struct sockaddr_in6 *) addr; | |
2273 | if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) && | |
2274 | !sin6->sin6_scope_id) | |
2275 | return -EINVAL; | |
2276 | ||
2277 | dev_addr->bound_dev_if = sin6->sin6_scope_id; | |
2278 | #endif | |
2279 | return 0; | |
2280 | } | |
2281 | ||
a9bb7912 HS |
2282 | int rdma_listen(struct rdma_cm_id *id, int backlog) |
2283 | { | |
2284 | struct rdma_id_private *id_priv; | |
2285 | int ret; | |
2286 | ||
2287 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2288 | if (id_priv->state == RDMA_CM_IDLE) { |
a9bb7912 HS |
2289 | ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET; |
2290 | ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr); | |
2291 | if (ret) | |
2292 | return ret; | |
2293 | } | |
2294 | ||
550e5ca7 | 2295 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) |
a9bb7912 HS |
2296 | return -EINVAL; |
2297 | ||
2298 | if (id_priv->reuseaddr) { | |
2299 | ret = cma_bind_listen(id_priv); | |
2300 | if (ret) | |
2301 | goto err; | |
2302 | } | |
2303 | ||
2304 | id_priv->backlog = backlog; | |
2305 | if (id->device) { | |
2306 | switch (rdma_node_get_transport(id->device->node_type)) { | |
2307 | case RDMA_TRANSPORT_IB: | |
2308 | ret = cma_ib_listen(id_priv); | |
2309 | if (ret) | |
2310 | goto err; | |
2311 | break; | |
2312 | case RDMA_TRANSPORT_IWARP: | |
2313 | ret = cma_iw_listen(id_priv, backlog); | |
2314 | if (ret) | |
2315 | goto err; | |
2316 | break; | |
2317 | default: | |
2318 | ret = -ENOSYS; | |
2319 | goto err; | |
2320 | } | |
2321 | } else | |
2322 | cma_listen_on_all(id_priv); | |
2323 | ||
2324 | return 0; | |
2325 | err: | |
2326 | id_priv->backlog = 0; | |
550e5ca7 | 2327 | cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND); |
a9bb7912 HS |
2328 | return ret; |
2329 | } | |
2330 | EXPORT_SYMBOL(rdma_listen); | |
2331 | ||
e51060f0 SH |
2332 | int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) |
2333 | { | |
2334 | struct rdma_id_private *id_priv; | |
2335 | int ret; | |
2336 | ||
1f5175ad | 2337 | if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) |
e51060f0 SH |
2338 | return -EAFNOSUPPORT; |
2339 | ||
2340 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2341 | if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND)) |
e51060f0 SH |
2342 | return -EINVAL; |
2343 | ||
d14714df SH |
2344 | ret = cma_check_linklocal(&id->route.addr.dev_addr, addr); |
2345 | if (ret) | |
2346 | goto err1; | |
2347 | ||
8523c048 | 2348 | if (!cma_any_addr(addr)) { |
e51060f0 | 2349 | ret = rdma_translate_ip(addr, &id->route.addr.dev_addr); |
e51060f0 | 2350 | if (ret) |
255d0c14 KK |
2351 | goto err1; |
2352 | ||
255d0c14 | 2353 | ret = cma_acquire_dev(id_priv); |
255d0c14 KK |
2354 | if (ret) |
2355 | goto err1; | |
e51060f0 SH |
2356 | } |
2357 | ||
2358 | memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr)); | |
2359 | ret = cma_get_port(id_priv); | |
2360 | if (ret) | |
255d0c14 | 2361 | goto err2; |
e51060f0 SH |
2362 | |
2363 | return 0; | |
255d0c14 | 2364 | err2: |
a396d43a SH |
2365 | if (id_priv->cma_dev) |
2366 | cma_release_dev(id_priv); | |
255d0c14 | 2367 | err1: |
550e5ca7 | 2368 | cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE); |
e51060f0 SH |
2369 | return ret; |
2370 | } | |
2371 | EXPORT_SYMBOL(rdma_bind_addr); | |
2372 | ||
2373 | static int cma_format_hdr(void *hdr, enum rdma_port_space ps, | |
2374 | struct rdma_route *route) | |
2375 | { | |
e51060f0 SH |
2376 | struct cma_hdr *cma_hdr; |
2377 | struct sdp_hh *sdp_hdr; | |
2378 | ||
1f5175ad AS |
2379 | if (route->addr.src_addr.ss_family == AF_INET) { |
2380 | struct sockaddr_in *src4, *dst4; | |
2381 | ||
2382 | src4 = (struct sockaddr_in *) &route->addr.src_addr; | |
2383 | dst4 = (struct sockaddr_in *) &route->addr.dst_addr; | |
2384 | ||
2385 | switch (ps) { | |
2386 | case RDMA_PS_SDP: | |
2387 | sdp_hdr = hdr; | |
2388 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) | |
2389 | return -EINVAL; | |
2390 | sdp_set_ip_ver(sdp_hdr, 4); | |
2391 | sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
2392 | sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
2393 | sdp_hdr->port = src4->sin_port; | |
2394 | break; | |
2395 | default: | |
2396 | cma_hdr = hdr; | |
2397 | cma_hdr->cma_version = CMA_VERSION; | |
2398 | cma_set_ip_ver(cma_hdr, 4); | |
2399 | cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
2400 | cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
2401 | cma_hdr->port = src4->sin_port; | |
2402 | break; | |
2403 | } | |
2404 | } else { | |
2405 | struct sockaddr_in6 *src6, *dst6; | |
2406 | ||
2407 | src6 = (struct sockaddr_in6 *) &route->addr.src_addr; | |
2408 | dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr; | |
2409 | ||
2410 | switch (ps) { | |
2411 | case RDMA_PS_SDP: | |
2412 | sdp_hdr = hdr; | |
2413 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) | |
2414 | return -EINVAL; | |
2415 | sdp_set_ip_ver(sdp_hdr, 6); | |
2416 | sdp_hdr->src_addr.ip6 = src6->sin6_addr; | |
2417 | sdp_hdr->dst_addr.ip6 = dst6->sin6_addr; | |
2418 | sdp_hdr->port = src6->sin6_port; | |
2419 | break; | |
2420 | default: | |
2421 | cma_hdr = hdr; | |
2422 | cma_hdr->cma_version = CMA_VERSION; | |
2423 | cma_set_ip_ver(cma_hdr, 6); | |
2424 | cma_hdr->src_addr.ip6 = src6->sin6_addr; | |
2425 | cma_hdr->dst_addr.ip6 = dst6->sin6_addr; | |
2426 | cma_hdr->port = src6->sin6_port; | |
2427 | break; | |
2428 | } | |
e51060f0 SH |
2429 | } |
2430 | return 0; | |
2431 | } | |
2432 | ||
628e5f6d SH |
2433 | static int cma_sidr_rep_handler(struct ib_cm_id *cm_id, |
2434 | struct ib_cm_event *ib_event) | |
2435 | { | |
2436 | struct rdma_id_private *id_priv = cm_id->context; | |
2437 | struct rdma_cm_event event; | |
2438 | struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd; | |
2439 | int ret = 0; | |
2440 | ||
550e5ca7 | 2441 | if (cma_disable_callback(id_priv, RDMA_CM_CONNECT)) |
8aa08602 | 2442 | return 0; |
628e5f6d | 2443 | |
8aa08602 | 2444 | memset(&event, 0, sizeof event); |
628e5f6d SH |
2445 | switch (ib_event->event) { |
2446 | case IB_CM_SIDR_REQ_ERROR: | |
2447 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
2448 | event.status = -ETIMEDOUT; | |
2449 | break; | |
2450 | case IB_CM_SIDR_REP_RECEIVED: | |
2451 | event.param.ud.private_data = ib_event->private_data; | |
2452 | event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; | |
2453 | if (rep->status != IB_SIDR_SUCCESS) { | |
2454 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
2455 | event.status = ib_event->param.sidr_rep_rcvd.status; | |
2456 | break; | |
2457 | } | |
d2ca39f2 YE |
2458 | ret = cma_set_qkey(id_priv); |
2459 | if (ret) { | |
2460 | event.event = RDMA_CM_EVENT_ADDR_ERROR; | |
2461 | event.status = -EINVAL; | |
2462 | break; | |
2463 | } | |
c8f6a362 | 2464 | if (id_priv->qkey != rep->qkey) { |
628e5f6d SH |
2465 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
2466 | event.status = -EINVAL; | |
2467 | break; | |
2468 | } | |
2469 | ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num, | |
2470 | id_priv->id.route.path_rec, | |
2471 | &event.param.ud.ah_attr); | |
2472 | event.param.ud.qp_num = rep->qpn; | |
2473 | event.param.ud.qkey = rep->qkey; | |
2474 | event.event = RDMA_CM_EVENT_ESTABLISHED; | |
2475 | event.status = 0; | |
2476 | break; | |
2477 | default: | |
468f2239 | 2478 | printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n", |
628e5f6d SH |
2479 | ib_event->event); |
2480 | goto out; | |
2481 | } | |
2482 | ||
2483 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
2484 | if (ret) { | |
2485 | /* Destroy the CM ID by returning a non-zero value. */ | |
2486 | id_priv->cm_id.ib = NULL; | |
550e5ca7 | 2487 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 2488 | mutex_unlock(&id_priv->handler_mutex); |
628e5f6d SH |
2489 | rdma_destroy_id(&id_priv->id); |
2490 | return ret; | |
2491 | } | |
2492 | out: | |
de910bd9 | 2493 | mutex_unlock(&id_priv->handler_mutex); |
628e5f6d SH |
2494 | return ret; |
2495 | } | |
2496 | ||
2497 | static int cma_resolve_ib_udp(struct rdma_id_private *id_priv, | |
2498 | struct rdma_conn_param *conn_param) | |
2499 | { | |
2500 | struct ib_cm_sidr_req_param req; | |
2501 | struct rdma_route *route; | |
0c9361fc | 2502 | struct ib_cm_id *id; |
628e5f6d SH |
2503 | int ret; |
2504 | ||
2505 | req.private_data_len = sizeof(struct cma_hdr) + | |
2506 | conn_param->private_data_len; | |
2507 | req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC); | |
2508 | if (!req.private_data) | |
2509 | return -ENOMEM; | |
2510 | ||
2511 | if (conn_param->private_data && conn_param->private_data_len) | |
2512 | memcpy((void *) req.private_data + sizeof(struct cma_hdr), | |
2513 | conn_param->private_data, conn_param->private_data_len); | |
2514 | ||
2515 | route = &id_priv->id.route; | |
2516 | ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route); | |
2517 | if (ret) | |
2518 | goto out; | |
2519 | ||
0c9361fc JM |
2520 | id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler, |
2521 | id_priv); | |
2522 | if (IS_ERR(id)) { | |
2523 | ret = PTR_ERR(id); | |
628e5f6d SH |
2524 | goto out; |
2525 | } | |
0c9361fc | 2526 | id_priv->cm_id.ib = id; |
628e5f6d SH |
2527 | |
2528 | req.path = route->path_rec; | |
2529 | req.service_id = cma_get_service_id(id_priv->id.ps, | |
3f446754 | 2530 | (struct sockaddr *) &route->addr.dst_addr); |
628e5f6d SH |
2531 | req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8); |
2532 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
2533 | ||
2534 | ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req); | |
2535 | if (ret) { | |
2536 | ib_destroy_cm_id(id_priv->cm_id.ib); | |
2537 | id_priv->cm_id.ib = NULL; | |
2538 | } | |
2539 | out: | |
2540 | kfree(req.private_data); | |
2541 | return ret; | |
2542 | } | |
2543 | ||
e51060f0 SH |
2544 | static int cma_connect_ib(struct rdma_id_private *id_priv, |
2545 | struct rdma_conn_param *conn_param) | |
2546 | { | |
2547 | struct ib_cm_req_param req; | |
2548 | struct rdma_route *route; | |
2549 | void *private_data; | |
0c9361fc | 2550 | struct ib_cm_id *id; |
e51060f0 SH |
2551 | int offset, ret; |
2552 | ||
2553 | memset(&req, 0, sizeof req); | |
2554 | offset = cma_user_data_offset(id_priv->id.ps); | |
2555 | req.private_data_len = offset + conn_param->private_data_len; | |
2556 | private_data = kzalloc(req.private_data_len, GFP_ATOMIC); | |
2557 | if (!private_data) | |
2558 | return -ENOMEM; | |
2559 | ||
2560 | if (conn_param->private_data && conn_param->private_data_len) | |
2561 | memcpy(private_data + offset, conn_param->private_data, | |
2562 | conn_param->private_data_len); | |
2563 | ||
0c9361fc JM |
2564 | id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv); |
2565 | if (IS_ERR(id)) { | |
2566 | ret = PTR_ERR(id); | |
e51060f0 SH |
2567 | goto out; |
2568 | } | |
0c9361fc | 2569 | id_priv->cm_id.ib = id; |
e51060f0 SH |
2570 | |
2571 | route = &id_priv->id.route; | |
2572 | ret = cma_format_hdr(private_data, id_priv->id.ps, route); | |
2573 | if (ret) | |
2574 | goto out; | |
2575 | req.private_data = private_data; | |
2576 | ||
2577 | req.primary_path = &route->path_rec[0]; | |
2578 | if (route->num_paths == 2) | |
2579 | req.alternate_path = &route->path_rec[1]; | |
2580 | ||
2581 | req.service_id = cma_get_service_id(id_priv->id.ps, | |
3f446754 | 2582 | (struct sockaddr *) &route->addr.dst_addr); |
e51060f0 | 2583 | req.qp_num = id_priv->qp_num; |
9b2e9c0c | 2584 | req.qp_type = IB_QPT_RC; |
e51060f0 SH |
2585 | req.starting_psn = id_priv->seq_num; |
2586 | req.responder_resources = conn_param->responder_resources; | |
2587 | req.initiator_depth = conn_param->initiator_depth; | |
2588 | req.flow_control = conn_param->flow_control; | |
2589 | req.retry_count = conn_param->retry_count; | |
2590 | req.rnr_retry_count = conn_param->rnr_retry_count; | |
2591 | req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
2592 | req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
2593 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
2594 | req.srq = id_priv->srq ? 1 : 0; | |
2595 | ||
2596 | ret = ib_send_cm_req(id_priv->cm_id.ib, &req); | |
2597 | out: | |
0c9361fc JM |
2598 | if (ret && !IS_ERR(id)) { |
2599 | ib_destroy_cm_id(id); | |
675a027c KK |
2600 | id_priv->cm_id.ib = NULL; |
2601 | } | |
2602 | ||
e51060f0 SH |
2603 | kfree(private_data); |
2604 | return ret; | |
2605 | } | |
2606 | ||
07ebafba TT |
2607 | static int cma_connect_iw(struct rdma_id_private *id_priv, |
2608 | struct rdma_conn_param *conn_param) | |
2609 | { | |
2610 | struct iw_cm_id *cm_id; | |
2611 | struct sockaddr_in* sin; | |
2612 | int ret; | |
2613 | struct iw_cm_conn_param iw_param; | |
2614 | ||
2615 | cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); | |
0c9361fc JM |
2616 | if (IS_ERR(cm_id)) |
2617 | return PTR_ERR(cm_id); | |
07ebafba TT |
2618 | |
2619 | id_priv->cm_id.iw = cm_id; | |
2620 | ||
2621 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr; | |
2622 | cm_id->local_addr = *sin; | |
2623 | ||
2624 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr; | |
2625 | cm_id->remote_addr = *sin; | |
2626 | ||
5851bb89 | 2627 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
675a027c KK |
2628 | if (ret) |
2629 | goto out; | |
07ebafba TT |
2630 | |
2631 | iw_param.ord = conn_param->initiator_depth; | |
2632 | iw_param.ird = conn_param->responder_resources; | |
2633 | iw_param.private_data = conn_param->private_data; | |
2634 | iw_param.private_data_len = conn_param->private_data_len; | |
2635 | if (id_priv->id.qp) | |
2636 | iw_param.qpn = id_priv->qp_num; | |
2637 | else | |
2638 | iw_param.qpn = conn_param->qp_num; | |
2639 | ret = iw_cm_connect(cm_id, &iw_param); | |
2640 | out: | |
0c9361fc | 2641 | if (ret) { |
675a027c KK |
2642 | iw_destroy_cm_id(cm_id); |
2643 | id_priv->cm_id.iw = NULL; | |
2644 | } | |
07ebafba TT |
2645 | return ret; |
2646 | } | |
2647 | ||
e51060f0 SH |
2648 | int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
2649 | { | |
2650 | struct rdma_id_private *id_priv; | |
2651 | int ret; | |
2652 | ||
2653 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2654 | if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) |
e51060f0 SH |
2655 | return -EINVAL; |
2656 | ||
2657 | if (!id->qp) { | |
2658 | id_priv->qp_num = conn_param->qp_num; | |
e51060f0 SH |
2659 | id_priv->srq = conn_param->srq; |
2660 | } | |
2661 | ||
07ebafba TT |
2662 | switch (rdma_node_get_transport(id->device->node_type)) { |
2663 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 2664 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
2665 | ret = cma_resolve_ib_udp(id_priv, conn_param); |
2666 | else | |
2667 | ret = cma_connect_ib(id_priv, conn_param); | |
e51060f0 | 2668 | break; |
07ebafba TT |
2669 | case RDMA_TRANSPORT_IWARP: |
2670 | ret = cma_connect_iw(id_priv, conn_param); | |
2671 | break; | |
e51060f0 SH |
2672 | default: |
2673 | ret = -ENOSYS; | |
2674 | break; | |
2675 | } | |
2676 | if (ret) | |
2677 | goto err; | |
2678 | ||
2679 | return 0; | |
2680 | err: | |
550e5ca7 | 2681 | cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED); |
e51060f0 SH |
2682 | return ret; |
2683 | } | |
2684 | EXPORT_SYMBOL(rdma_connect); | |
2685 | ||
2686 | static int cma_accept_ib(struct rdma_id_private *id_priv, | |
2687 | struct rdma_conn_param *conn_param) | |
2688 | { | |
2689 | struct ib_cm_rep_param rep; | |
5851bb89 | 2690 | int ret; |
0fe313b0 | 2691 | |
5851bb89 SH |
2692 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
2693 | if (ret) | |
2694 | goto out; | |
0fe313b0 | 2695 | |
5851bb89 SH |
2696 | ret = cma_modify_qp_rts(id_priv, conn_param); |
2697 | if (ret) | |
2698 | goto out; | |
e51060f0 SH |
2699 | |
2700 | memset(&rep, 0, sizeof rep); | |
2701 | rep.qp_num = id_priv->qp_num; | |
2702 | rep.starting_psn = id_priv->seq_num; | |
2703 | rep.private_data = conn_param->private_data; | |
2704 | rep.private_data_len = conn_param->private_data_len; | |
2705 | rep.responder_resources = conn_param->responder_resources; | |
2706 | rep.initiator_depth = conn_param->initiator_depth; | |
e51060f0 SH |
2707 | rep.failover_accepted = 0; |
2708 | rep.flow_control = conn_param->flow_control; | |
2709 | rep.rnr_retry_count = conn_param->rnr_retry_count; | |
2710 | rep.srq = id_priv->srq ? 1 : 0; | |
2711 | ||
0fe313b0 SH |
2712 | ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep); |
2713 | out: | |
2714 | return ret; | |
e51060f0 SH |
2715 | } |
2716 | ||
07ebafba TT |
2717 | static int cma_accept_iw(struct rdma_id_private *id_priv, |
2718 | struct rdma_conn_param *conn_param) | |
2719 | { | |
2720 | struct iw_cm_conn_param iw_param; | |
2721 | int ret; | |
2722 | ||
5851bb89 | 2723 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
07ebafba TT |
2724 | if (ret) |
2725 | return ret; | |
2726 | ||
2727 | iw_param.ord = conn_param->initiator_depth; | |
2728 | iw_param.ird = conn_param->responder_resources; | |
2729 | iw_param.private_data = conn_param->private_data; | |
2730 | iw_param.private_data_len = conn_param->private_data_len; | |
2731 | if (id_priv->id.qp) { | |
2732 | iw_param.qpn = id_priv->qp_num; | |
2733 | } else | |
2734 | iw_param.qpn = conn_param->qp_num; | |
2735 | ||
2736 | return iw_cm_accept(id_priv->cm_id.iw, &iw_param); | |
2737 | } | |
2738 | ||
628e5f6d SH |
2739 | static int cma_send_sidr_rep(struct rdma_id_private *id_priv, |
2740 | enum ib_cm_sidr_status status, | |
2741 | const void *private_data, int private_data_len) | |
2742 | { | |
2743 | struct ib_cm_sidr_rep_param rep; | |
d2ca39f2 | 2744 | int ret; |
628e5f6d SH |
2745 | |
2746 | memset(&rep, 0, sizeof rep); | |
2747 | rep.status = status; | |
2748 | if (status == IB_SIDR_SUCCESS) { | |
d2ca39f2 YE |
2749 | ret = cma_set_qkey(id_priv); |
2750 | if (ret) | |
2751 | return ret; | |
628e5f6d | 2752 | rep.qp_num = id_priv->qp_num; |
c8f6a362 | 2753 | rep.qkey = id_priv->qkey; |
628e5f6d SH |
2754 | } |
2755 | rep.private_data = private_data; | |
2756 | rep.private_data_len = private_data_len; | |
2757 | ||
2758 | return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep); | |
2759 | } | |
2760 | ||
e51060f0 SH |
2761 | int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
2762 | { | |
2763 | struct rdma_id_private *id_priv; | |
2764 | int ret; | |
2765 | ||
2766 | id_priv = container_of(id, struct rdma_id_private, id); | |
83e9502d NM |
2767 | |
2768 | id_priv->owner = task_pid_nr(current); | |
2769 | ||
550e5ca7 | 2770 | if (!cma_comp(id_priv, RDMA_CM_CONNECT)) |
e51060f0 SH |
2771 | return -EINVAL; |
2772 | ||
2773 | if (!id->qp && conn_param) { | |
2774 | id_priv->qp_num = conn_param->qp_num; | |
e51060f0 SH |
2775 | id_priv->srq = conn_param->srq; |
2776 | } | |
2777 | ||
07ebafba TT |
2778 | switch (rdma_node_get_transport(id->device->node_type)) { |
2779 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 2780 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
2781 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, |
2782 | conn_param->private_data, | |
2783 | conn_param->private_data_len); | |
2784 | else if (conn_param) | |
e51060f0 SH |
2785 | ret = cma_accept_ib(id_priv, conn_param); |
2786 | else | |
2787 | ret = cma_rep_recv(id_priv); | |
2788 | break; | |
07ebafba TT |
2789 | case RDMA_TRANSPORT_IWARP: |
2790 | ret = cma_accept_iw(id_priv, conn_param); | |
2791 | break; | |
e51060f0 SH |
2792 | default: |
2793 | ret = -ENOSYS; | |
2794 | break; | |
2795 | } | |
2796 | ||
2797 | if (ret) | |
2798 | goto reject; | |
2799 | ||
2800 | return 0; | |
2801 | reject: | |
c5483388 | 2802 | cma_modify_qp_err(id_priv); |
e51060f0 SH |
2803 | rdma_reject(id, NULL, 0); |
2804 | return ret; | |
2805 | } | |
2806 | EXPORT_SYMBOL(rdma_accept); | |
2807 | ||
0fe313b0 SH |
2808 | int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event) |
2809 | { | |
2810 | struct rdma_id_private *id_priv; | |
2811 | int ret; | |
2812 | ||
2813 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 2814 | if (!id_priv->cm_id.ib) |
0fe313b0 SH |
2815 | return -EINVAL; |
2816 | ||
2817 | switch (id->device->node_type) { | |
2818 | case RDMA_NODE_IB_CA: | |
2819 | ret = ib_cm_notify(id_priv->cm_id.ib, event); | |
2820 | break; | |
2821 | default: | |
2822 | ret = 0; | |
2823 | break; | |
2824 | } | |
2825 | return ret; | |
2826 | } | |
2827 | EXPORT_SYMBOL(rdma_notify); | |
2828 | ||
e51060f0 SH |
2829 | int rdma_reject(struct rdma_cm_id *id, const void *private_data, |
2830 | u8 private_data_len) | |
2831 | { | |
2832 | struct rdma_id_private *id_priv; | |
2833 | int ret; | |
2834 | ||
2835 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 2836 | if (!id_priv->cm_id.ib) |
e51060f0 SH |
2837 | return -EINVAL; |
2838 | ||
07ebafba TT |
2839 | switch (rdma_node_get_transport(id->device->node_type)) { |
2840 | case RDMA_TRANSPORT_IB: | |
b26f9b99 | 2841 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
2842 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, |
2843 | private_data, private_data_len); | |
2844 | else | |
2845 | ret = ib_send_cm_rej(id_priv->cm_id.ib, | |
2846 | IB_CM_REJ_CONSUMER_DEFINED, NULL, | |
2847 | 0, private_data, private_data_len); | |
e51060f0 | 2848 | break; |
07ebafba TT |
2849 | case RDMA_TRANSPORT_IWARP: |
2850 | ret = iw_cm_reject(id_priv->cm_id.iw, | |
2851 | private_data, private_data_len); | |
2852 | break; | |
e51060f0 SH |
2853 | default: |
2854 | ret = -ENOSYS; | |
2855 | break; | |
2856 | } | |
2857 | return ret; | |
2858 | } | |
2859 | EXPORT_SYMBOL(rdma_reject); | |
2860 | ||
2861 | int rdma_disconnect(struct rdma_cm_id *id) | |
2862 | { | |
2863 | struct rdma_id_private *id_priv; | |
2864 | int ret; | |
2865 | ||
2866 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 2867 | if (!id_priv->cm_id.ib) |
e51060f0 SH |
2868 | return -EINVAL; |
2869 | ||
07ebafba TT |
2870 | switch (rdma_node_get_transport(id->device->node_type)) { |
2871 | case RDMA_TRANSPORT_IB: | |
c5483388 | 2872 | ret = cma_modify_qp_err(id_priv); |
07ebafba TT |
2873 | if (ret) |
2874 | goto out; | |
e51060f0 SH |
2875 | /* Initiate or respond to a disconnect. */ |
2876 | if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) | |
2877 | ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0); | |
2878 | break; | |
07ebafba TT |
2879 | case RDMA_TRANSPORT_IWARP: |
2880 | ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); | |
2881 | break; | |
e51060f0 | 2882 | default: |
07ebafba | 2883 | ret = -EINVAL; |
e51060f0 SH |
2884 | break; |
2885 | } | |
2886 | out: | |
2887 | return ret; | |
2888 | } | |
2889 | EXPORT_SYMBOL(rdma_disconnect); | |
2890 | ||
c8f6a362 SH |
2891 | static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast) |
2892 | { | |
2893 | struct rdma_id_private *id_priv; | |
2894 | struct cma_multicast *mc = multicast->context; | |
2895 | struct rdma_cm_event event; | |
2896 | int ret; | |
2897 | ||
2898 | id_priv = mc->id_priv; | |
550e5ca7 NM |
2899 | if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) && |
2900 | cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED)) | |
8aa08602 | 2901 | return 0; |
c8f6a362 | 2902 | |
c5483388 | 2903 | mutex_lock(&id_priv->qp_mutex); |
c8f6a362 SH |
2904 | if (!status && id_priv->id.qp) |
2905 | status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid, | |
2906 | multicast->rec.mlid); | |
c5483388 | 2907 | mutex_unlock(&id_priv->qp_mutex); |
c8f6a362 SH |
2908 | |
2909 | memset(&event, 0, sizeof event); | |
2910 | event.status = status; | |
2911 | event.param.ud.private_data = mc->context; | |
2912 | if (!status) { | |
2913 | event.event = RDMA_CM_EVENT_MULTICAST_JOIN; | |
2914 | ib_init_ah_from_mcmember(id_priv->id.device, | |
2915 | id_priv->id.port_num, &multicast->rec, | |
2916 | &event.param.ud.ah_attr); | |
2917 | event.param.ud.qp_num = 0xFFFFFF; | |
2918 | event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey); | |
2919 | } else | |
2920 | event.event = RDMA_CM_EVENT_MULTICAST_ERROR; | |
2921 | ||
2922 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
2923 | if (ret) { | |
550e5ca7 | 2924 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 2925 | mutex_unlock(&id_priv->handler_mutex); |
c8f6a362 SH |
2926 | rdma_destroy_id(&id_priv->id); |
2927 | return 0; | |
2928 | } | |
8aa08602 | 2929 | |
de910bd9 | 2930 | mutex_unlock(&id_priv->handler_mutex); |
c8f6a362 SH |
2931 | return 0; |
2932 | } | |
2933 | ||
2934 | static void cma_set_mgid(struct rdma_id_private *id_priv, | |
2935 | struct sockaddr *addr, union ib_gid *mgid) | |
2936 | { | |
2937 | unsigned char mc_map[MAX_ADDR_LEN]; | |
2938 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
2939 | struct sockaddr_in *sin = (struct sockaddr_in *) addr; | |
2940 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr; | |
2941 | ||
2942 | if (cma_any_addr(addr)) { | |
2943 | memset(mgid, 0, sizeof *mgid); | |
2944 | } else if ((addr->sa_family == AF_INET6) && | |
1c9b2819 | 2945 | ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) == |
c8f6a362 SH |
2946 | 0xFF10A01B)) { |
2947 | /* IPv6 address is an SA assigned MGID. */ | |
2948 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); | |
e2e62697 JG |
2949 | } else if ((addr->sa_family == AF_INET6)) { |
2950 | ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map); | |
2951 | if (id_priv->id.ps == RDMA_PS_UDP) | |
2952 | mc_map[7] = 0x01; /* Use RDMA CM signature */ | |
2953 | *mgid = *(union ib_gid *) (mc_map + 4); | |
c8f6a362 | 2954 | } else { |
a9e527e3 | 2955 | ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map); |
c8f6a362 SH |
2956 | if (id_priv->id.ps == RDMA_PS_UDP) |
2957 | mc_map[7] = 0x01; /* Use RDMA CM signature */ | |
c8f6a362 SH |
2958 | *mgid = *(union ib_gid *) (mc_map + 4); |
2959 | } | |
2960 | } | |
2961 | ||
2962 | static int cma_join_ib_multicast(struct rdma_id_private *id_priv, | |
2963 | struct cma_multicast *mc) | |
2964 | { | |
2965 | struct ib_sa_mcmember_rec rec; | |
2966 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
2967 | ib_sa_comp_mask comp_mask; | |
2968 | int ret; | |
2969 | ||
2970 | ib_addr_get_mgid(dev_addr, &rec.mgid); | |
2971 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num, | |
2972 | &rec.mgid, &rec); | |
2973 | if (ret) | |
2974 | return ret; | |
2975 | ||
3f446754 | 2976 | cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid); |
c8f6a362 SH |
2977 | if (id_priv->id.ps == RDMA_PS_UDP) |
2978 | rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); | |
6f8372b6 | 2979 | rdma_addr_get_sgid(dev_addr, &rec.port_gid); |
c8f6a362 SH |
2980 | rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); |
2981 | rec.join_state = 1; | |
2982 | ||
2983 | comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID | | |
2984 | IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE | | |
2985 | IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL | | |
2986 | IB_SA_MCMEMBER_REC_FLOW_LABEL | | |
2987 | IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; | |
2988 | ||
84adeee9 YE |
2989 | if (id_priv->id.ps == RDMA_PS_IPOIB) |
2990 | comp_mask |= IB_SA_MCMEMBER_REC_RATE | | |
2991 | IB_SA_MCMEMBER_REC_RATE_SELECTOR; | |
2992 | ||
c8f6a362 SH |
2993 | mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device, |
2994 | id_priv->id.port_num, &rec, | |
2995 | comp_mask, GFP_KERNEL, | |
2996 | cma_ib_mc_handler, mc); | |
2997 | if (IS_ERR(mc->multicast.ib)) | |
2998 | return PTR_ERR(mc->multicast.ib); | |
2999 | ||
3000 | return 0; | |
3001 | } | |
3002 | ||
3c86aa70 EC |
3003 | static void iboe_mcast_work_handler(struct work_struct *work) |
3004 | { | |
3005 | struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work); | |
3006 | struct cma_multicast *mc = mw->mc; | |
3007 | struct ib_sa_multicast *m = mc->multicast.ib; | |
3008 | ||
3009 | mc->multicast.ib->context = mc; | |
3010 | cma_ib_mc_handler(0, m); | |
3011 | kref_put(&mc->mcref, release_mc); | |
3012 | kfree(mw); | |
3013 | } | |
3014 | ||
3015 | static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid) | |
3016 | { | |
3017 | struct sockaddr_in *sin = (struct sockaddr_in *)addr; | |
3018 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; | |
3019 | ||
3020 | if (cma_any_addr(addr)) { | |
3021 | memset(mgid, 0, sizeof *mgid); | |
3022 | } else if (addr->sa_family == AF_INET6) { | |
3023 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); | |
3024 | } else { | |
3025 | mgid->raw[0] = 0xff; | |
3026 | mgid->raw[1] = 0x0e; | |
3027 | mgid->raw[2] = 0; | |
3028 | mgid->raw[3] = 0; | |
3029 | mgid->raw[4] = 0; | |
3030 | mgid->raw[5] = 0; | |
3031 | mgid->raw[6] = 0; | |
3032 | mgid->raw[7] = 0; | |
3033 | mgid->raw[8] = 0; | |
3034 | mgid->raw[9] = 0; | |
3035 | mgid->raw[10] = 0xff; | |
3036 | mgid->raw[11] = 0xff; | |
3037 | *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr; | |
3038 | } | |
3039 | } | |
3040 | ||
3041 | static int cma_iboe_join_multicast(struct rdma_id_private *id_priv, | |
3042 | struct cma_multicast *mc) | |
3043 | { | |
3044 | struct iboe_mcast_work *work; | |
3045 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
3046 | int err; | |
3047 | struct sockaddr *addr = (struct sockaddr *)&mc->addr; | |
3048 | struct net_device *ndev = NULL; | |
3049 | ||
3050 | if (cma_zero_addr((struct sockaddr *)&mc->addr)) | |
3051 | return -EINVAL; | |
3052 | ||
3053 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
3054 | if (!work) | |
3055 | return -ENOMEM; | |
3056 | ||
3057 | mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL); | |
3058 | if (!mc->multicast.ib) { | |
3059 | err = -ENOMEM; | |
3060 | goto out1; | |
3061 | } | |
3062 | ||
3063 | cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid); | |
3064 | ||
3065 | mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff); | |
3066 | if (id_priv->id.ps == RDMA_PS_UDP) | |
3067 | mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); | |
3068 | ||
3069 | if (dev_addr->bound_dev_if) | |
3070 | ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); | |
3071 | if (!ndev) { | |
3072 | err = -ENODEV; | |
3073 | goto out2; | |
3074 | } | |
3075 | mc->multicast.ib->rec.rate = iboe_get_rate(ndev); | |
3076 | mc->multicast.ib->rec.hop_limit = 1; | |
3077 | mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu); | |
3078 | dev_put(ndev); | |
3079 | if (!mc->multicast.ib->rec.mtu) { | |
3080 | err = -EINVAL; | |
3081 | goto out2; | |
3082 | } | |
3083 | iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid); | |
3084 | work->id = id_priv; | |
3085 | work->mc = mc; | |
3086 | INIT_WORK(&work->work, iboe_mcast_work_handler); | |
3087 | kref_get(&mc->mcref); | |
3088 | queue_work(cma_wq, &work->work); | |
3089 | ||
3090 | return 0; | |
3091 | ||
3092 | out2: | |
3093 | kfree(mc->multicast.ib); | |
3094 | out1: | |
3095 | kfree(work); | |
3096 | return err; | |
3097 | } | |
3098 | ||
c8f6a362 SH |
3099 | int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, |
3100 | void *context) | |
3101 | { | |
3102 | struct rdma_id_private *id_priv; | |
3103 | struct cma_multicast *mc; | |
3104 | int ret; | |
3105 | ||
3106 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 NM |
3107 | if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) && |
3108 | !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED)) | |
c8f6a362 SH |
3109 | return -EINVAL; |
3110 | ||
3111 | mc = kmalloc(sizeof *mc, GFP_KERNEL); | |
3112 | if (!mc) | |
3113 | return -ENOMEM; | |
3114 | ||
3115 | memcpy(&mc->addr, addr, ip_addr_size(addr)); | |
3116 | mc->context = context; | |
3117 | mc->id_priv = id_priv; | |
3118 | ||
3119 | spin_lock(&id_priv->lock); | |
3120 | list_add(&mc->list, &id_priv->mc_list); | |
3121 | spin_unlock(&id_priv->lock); | |
3122 | ||
3123 | switch (rdma_node_get_transport(id->device->node_type)) { | |
3124 | case RDMA_TRANSPORT_IB: | |
3c86aa70 EC |
3125 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
3126 | case IB_LINK_LAYER_INFINIBAND: | |
3127 | ret = cma_join_ib_multicast(id_priv, mc); | |
3128 | break; | |
3129 | case IB_LINK_LAYER_ETHERNET: | |
3130 | kref_init(&mc->mcref); | |
3131 | ret = cma_iboe_join_multicast(id_priv, mc); | |
3132 | break; | |
3133 | default: | |
3134 | ret = -EINVAL; | |
3135 | } | |
c8f6a362 SH |
3136 | break; |
3137 | default: | |
3138 | ret = -ENOSYS; | |
3139 | break; | |
3140 | } | |
3141 | ||
3142 | if (ret) { | |
3143 | spin_lock_irq(&id_priv->lock); | |
3144 | list_del(&mc->list); | |
3145 | spin_unlock_irq(&id_priv->lock); | |
3146 | kfree(mc); | |
3147 | } | |
3148 | return ret; | |
3149 | } | |
3150 | EXPORT_SYMBOL(rdma_join_multicast); | |
3151 | ||
3152 | void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) | |
3153 | { | |
3154 | struct rdma_id_private *id_priv; | |
3155 | struct cma_multicast *mc; | |
3156 | ||
3157 | id_priv = container_of(id, struct rdma_id_private, id); | |
3158 | spin_lock_irq(&id_priv->lock); | |
3159 | list_for_each_entry(mc, &id_priv->mc_list, list) { | |
3160 | if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) { | |
3161 | list_del(&mc->list); | |
3162 | spin_unlock_irq(&id_priv->lock); | |
3163 | ||
3164 | if (id->qp) | |
3165 | ib_detach_mcast(id->qp, | |
3166 | &mc->multicast.ib->rec.mgid, | |
3167 | mc->multicast.ib->rec.mlid); | |
3c86aa70 EC |
3168 | if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) { |
3169 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { | |
3170 | case IB_LINK_LAYER_INFINIBAND: | |
3171 | ib_sa_free_multicast(mc->multicast.ib); | |
3172 | kfree(mc); | |
3173 | break; | |
3174 | case IB_LINK_LAYER_ETHERNET: | |
3175 | kref_put(&mc->mcref, release_mc); | |
3176 | break; | |
3177 | default: | |
3178 | break; | |
3179 | } | |
3180 | } | |
c8f6a362 SH |
3181 | return; |
3182 | } | |
3183 | } | |
3184 | spin_unlock_irq(&id_priv->lock); | |
3185 | } | |
3186 | EXPORT_SYMBOL(rdma_leave_multicast); | |
3187 | ||
dd5bdff8 OG |
3188 | static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv) |
3189 | { | |
3190 | struct rdma_dev_addr *dev_addr; | |
3191 | struct cma_ndev_work *work; | |
3192 | ||
3193 | dev_addr = &id_priv->id.route.addr.dev_addr; | |
3194 | ||
6266ed6e | 3195 | if ((dev_addr->bound_dev_if == ndev->ifindex) && |
dd5bdff8 OG |
3196 | memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) { |
3197 | printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n", | |
3198 | ndev->name, &id_priv->id); | |
3199 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
3200 | if (!work) | |
3201 | return -ENOMEM; | |
3202 | ||
3203 | INIT_WORK(&work->work, cma_ndev_work_handler); | |
3204 | work->id = id_priv; | |
3205 | work->event.event = RDMA_CM_EVENT_ADDR_CHANGE; | |
3206 | atomic_inc(&id_priv->refcount); | |
3207 | queue_work(cma_wq, &work->work); | |
3208 | } | |
3209 | ||
3210 | return 0; | |
3211 | } | |
3212 | ||
3213 | static int cma_netdev_callback(struct notifier_block *self, unsigned long event, | |
3214 | void *ctx) | |
3215 | { | |
3216 | struct net_device *ndev = (struct net_device *)ctx; | |
3217 | struct cma_device *cma_dev; | |
3218 | struct rdma_id_private *id_priv; | |
3219 | int ret = NOTIFY_DONE; | |
3220 | ||
3221 | if (dev_net(ndev) != &init_net) | |
3222 | return NOTIFY_DONE; | |
3223 | ||
3224 | if (event != NETDEV_BONDING_FAILOVER) | |
3225 | return NOTIFY_DONE; | |
3226 | ||
3227 | if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING)) | |
3228 | return NOTIFY_DONE; | |
3229 | ||
3230 | mutex_lock(&lock); | |
3231 | list_for_each_entry(cma_dev, &dev_list, list) | |
3232 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { | |
3233 | ret = cma_netdev_change(ndev, id_priv); | |
3234 | if (ret) | |
3235 | goto out; | |
3236 | } | |
3237 | ||
3238 | out: | |
3239 | mutex_unlock(&lock); | |
3240 | return ret; | |
3241 | } | |
3242 | ||
3243 | static struct notifier_block cma_nb = { | |
3244 | .notifier_call = cma_netdev_callback | |
3245 | }; | |
3246 | ||
e51060f0 SH |
3247 | static void cma_add_one(struct ib_device *device) |
3248 | { | |
3249 | struct cma_device *cma_dev; | |
3250 | struct rdma_id_private *id_priv; | |
3251 | ||
3252 | cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); | |
3253 | if (!cma_dev) | |
3254 | return; | |
3255 | ||
3256 | cma_dev->device = device; | |
e51060f0 SH |
3257 | |
3258 | init_completion(&cma_dev->comp); | |
3259 | atomic_set(&cma_dev->refcount, 1); | |
3260 | INIT_LIST_HEAD(&cma_dev->id_list); | |
3261 | ib_set_client_data(device, &cma_client, cma_dev); | |
3262 | ||
3263 | mutex_lock(&lock); | |
3264 | list_add_tail(&cma_dev->list, &dev_list); | |
3265 | list_for_each_entry(id_priv, &listen_any_list, list) | |
3266 | cma_listen_on_dev(id_priv, cma_dev); | |
3267 | mutex_unlock(&lock); | |
e51060f0 SH |
3268 | } |
3269 | ||
3270 | static int cma_remove_id_dev(struct rdma_id_private *id_priv) | |
3271 | { | |
a1b1b61f | 3272 | struct rdma_cm_event event; |
550e5ca7 | 3273 | enum rdma_cm_state state; |
de910bd9 | 3274 | int ret = 0; |
e51060f0 SH |
3275 | |
3276 | /* Record that we want to remove the device */ | |
550e5ca7 NM |
3277 | state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL); |
3278 | if (state == RDMA_CM_DESTROYING) | |
e51060f0 SH |
3279 | return 0; |
3280 | ||
3281 | cma_cancel_operation(id_priv, state); | |
de910bd9 | 3282 | mutex_lock(&id_priv->handler_mutex); |
e51060f0 SH |
3283 | |
3284 | /* Check for destruction from another callback. */ | |
550e5ca7 | 3285 | if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL)) |
de910bd9 | 3286 | goto out; |
e51060f0 | 3287 | |
a1b1b61f SH |
3288 | memset(&event, 0, sizeof event); |
3289 | event.event = RDMA_CM_EVENT_DEVICE_REMOVAL; | |
de910bd9 OG |
3290 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
3291 | out: | |
3292 | mutex_unlock(&id_priv->handler_mutex); | |
3293 | return ret; | |
e51060f0 SH |
3294 | } |
3295 | ||
3296 | static void cma_process_remove(struct cma_device *cma_dev) | |
3297 | { | |
e51060f0 SH |
3298 | struct rdma_id_private *id_priv; |
3299 | int ret; | |
3300 | ||
e51060f0 SH |
3301 | mutex_lock(&lock); |
3302 | while (!list_empty(&cma_dev->id_list)) { | |
3303 | id_priv = list_entry(cma_dev->id_list.next, | |
3304 | struct rdma_id_private, list); | |
3305 | ||
d02d1f53 | 3306 | list_del(&id_priv->listen_list); |
94de178a | 3307 | list_del_init(&id_priv->list); |
e51060f0 SH |
3308 | atomic_inc(&id_priv->refcount); |
3309 | mutex_unlock(&lock); | |
3310 | ||
d02d1f53 | 3311 | ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv); |
e51060f0 SH |
3312 | cma_deref_id(id_priv); |
3313 | if (ret) | |
3314 | rdma_destroy_id(&id_priv->id); | |
3315 | ||
3316 | mutex_lock(&lock); | |
3317 | } | |
3318 | mutex_unlock(&lock); | |
3319 | ||
3320 | cma_deref_dev(cma_dev); | |
3321 | wait_for_completion(&cma_dev->comp); | |
3322 | } | |
3323 | ||
3324 | static void cma_remove_one(struct ib_device *device) | |
3325 | { | |
3326 | struct cma_device *cma_dev; | |
3327 | ||
3328 | cma_dev = ib_get_client_data(device, &cma_client); | |
3329 | if (!cma_dev) | |
3330 | return; | |
3331 | ||
3332 | mutex_lock(&lock); | |
3333 | list_del(&cma_dev->list); | |
3334 | mutex_unlock(&lock); | |
3335 | ||
3336 | cma_process_remove(cma_dev); | |
3337 | kfree(cma_dev); | |
3338 | } | |
3339 | ||
753f618a NM |
3340 | static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb) |
3341 | { | |
3342 | struct nlmsghdr *nlh; | |
3343 | struct rdma_cm_id_stats *id_stats; | |
3344 | struct rdma_id_private *id_priv; | |
3345 | struct rdma_cm_id *id = NULL; | |
3346 | struct cma_device *cma_dev; | |
3347 | int i_dev = 0, i_id = 0; | |
3348 | ||
3349 | /* | |
3350 | * We export all of the IDs as a sequence of messages. Each | |
3351 | * ID gets its own netlink message. | |
3352 | */ | |
3353 | mutex_lock(&lock); | |
3354 | ||
3355 | list_for_each_entry(cma_dev, &dev_list, list) { | |
3356 | if (i_dev < cb->args[0]) { | |
3357 | i_dev++; | |
3358 | continue; | |
3359 | } | |
3360 | ||
3361 | i_id = 0; | |
3362 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { | |
3363 | if (i_id < cb->args[1]) { | |
3364 | i_id++; | |
3365 | continue; | |
3366 | } | |
3367 | ||
3368 | id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq, | |
3369 | sizeof *id_stats, RDMA_NL_RDMA_CM, | |
3370 | RDMA_NL_RDMA_CM_ID_STATS); | |
3371 | if (!id_stats) | |
3372 | goto out; | |
3373 | ||
3374 | memset(id_stats, 0, sizeof *id_stats); | |
3375 | id = &id_priv->id; | |
3376 | id_stats->node_type = id->route.addr.dev_addr.dev_type; | |
3377 | id_stats->port_num = id->port_num; | |
3378 | id_stats->bound_dev_if = | |
3379 | id->route.addr.dev_addr.bound_dev_if; | |
3380 | ||
3381 | if (id->route.addr.src_addr.ss_family == AF_INET) { | |
3382 | if (ibnl_put_attr(skb, nlh, | |
3383 | sizeof(struct sockaddr_in), | |
3384 | &id->route.addr.src_addr, | |
3385 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) { | |
3386 | goto out; | |
3387 | } | |
3388 | if (ibnl_put_attr(skb, nlh, | |
3389 | sizeof(struct sockaddr_in), | |
3390 | &id->route.addr.dst_addr, | |
3391 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) { | |
3392 | goto out; | |
3393 | } | |
3394 | } else if (id->route.addr.src_addr.ss_family == AF_INET6) { | |
3395 | if (ibnl_put_attr(skb, nlh, | |
3396 | sizeof(struct sockaddr_in6), | |
3397 | &id->route.addr.src_addr, | |
3398 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) { | |
3399 | goto out; | |
3400 | } | |
3401 | if (ibnl_put_attr(skb, nlh, | |
3402 | sizeof(struct sockaddr_in6), | |
3403 | &id->route.addr.dst_addr, | |
3404 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) { | |
3405 | goto out; | |
3406 | } | |
3407 | } | |
3408 | ||
83e9502d | 3409 | id_stats->pid = id_priv->owner; |
753f618a NM |
3410 | id_stats->port_space = id->ps; |
3411 | id_stats->cm_state = id_priv->state; | |
3412 | id_stats->qp_num = id_priv->qp_num; | |
3413 | id_stats->qp_type = id->qp_type; | |
3414 | ||
3415 | i_id++; | |
3416 | } | |
3417 | ||
3418 | cb->args[1] = 0; | |
3419 | i_dev++; | |
3420 | } | |
3421 | ||
3422 | out: | |
3423 | mutex_unlock(&lock); | |
3424 | cb->args[0] = i_dev; | |
3425 | cb->args[1] = i_id; | |
3426 | ||
3427 | return skb->len; | |
3428 | } | |
3429 | ||
3430 | static const struct ibnl_client_cbs cma_cb_table[] = { | |
3431 | [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats }, | |
3432 | }; | |
3433 | ||
716abb1f | 3434 | static int __init cma_init(void) |
e51060f0 | 3435 | { |
5d7220e8 | 3436 | int ret; |
227b60f5 | 3437 | |
c7f743a6 | 3438 | cma_wq = create_singlethread_workqueue("rdma_cm"); |
e51060f0 SH |
3439 | if (!cma_wq) |
3440 | return -ENOMEM; | |
3441 | ||
c1a0b23b | 3442 | ib_sa_register_client(&sa_client); |
7a118df3 | 3443 | rdma_addr_register_client(&addr_client); |
dd5bdff8 | 3444 | register_netdevice_notifier(&cma_nb); |
c1a0b23b | 3445 | |
e51060f0 SH |
3446 | ret = ib_register_client(&cma_client); |
3447 | if (ret) | |
3448 | goto err; | |
753f618a NM |
3449 | |
3450 | if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table)) | |
3451 | printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n"); | |
3452 | ||
e51060f0 SH |
3453 | return 0; |
3454 | ||
3455 | err: | |
dd5bdff8 | 3456 | unregister_netdevice_notifier(&cma_nb); |
7a118df3 | 3457 | rdma_addr_unregister_client(&addr_client); |
c1a0b23b | 3458 | ib_sa_unregister_client(&sa_client); |
e51060f0 SH |
3459 | destroy_workqueue(cma_wq); |
3460 | return ret; | |
3461 | } | |
3462 | ||
716abb1f | 3463 | static void __exit cma_cleanup(void) |
e51060f0 | 3464 | { |
753f618a | 3465 | ibnl_remove_client(RDMA_NL_RDMA_CM); |
e51060f0 | 3466 | ib_unregister_client(&cma_client); |
dd5bdff8 | 3467 | unregister_netdevice_notifier(&cma_nb); |
7a118df3 | 3468 | rdma_addr_unregister_client(&addr_client); |
c1a0b23b | 3469 | ib_sa_unregister_client(&sa_client); |
e51060f0 SH |
3470 | destroy_workqueue(cma_wq); |
3471 | idr_destroy(&sdp_ps); | |
3472 | idr_destroy(&tcp_ps); | |
628e5f6d | 3473 | idr_destroy(&udp_ps); |
c8f6a362 | 3474 | idr_destroy(&ipoib_ps); |
e51060f0 SH |
3475 | } |
3476 | ||
3477 | module_init(cma_init); | |
3478 | module_exit(cma_cleanup); |