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