]>
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> | |
bee3c3c9 | 41 | #include <linux/igmp.h> |
e51060f0 | 42 | #include <linux/idr.h> |
07ebafba | 43 | #include <linux/inetdevice.h> |
5a0e3ad6 | 44 | #include <linux/slab.h> |
e4dd23d7 | 45 | #include <linux/module.h> |
366cddb4 | 46 | #include <net/route.h> |
e51060f0 | 47 | |
4be74b42 HE |
48 | #include <net/net_namespace.h> |
49 | #include <net/netns/generic.h> | |
e51060f0 | 50 | #include <net/tcp.h> |
1f5175ad | 51 | #include <net/ipv6.h> |
f887f2ac HE |
52 | #include <net/ip_fib.h> |
53 | #include <net/ip6_route.h> | |
e51060f0 SH |
54 | |
55 | #include <rdma/rdma_cm.h> | |
56 | #include <rdma/rdma_cm_ib.h> | |
753f618a | 57 | #include <rdma/rdma_netlink.h> |
2e2d190c | 58 | #include <rdma/ib.h> |
e51060f0 SH |
59 | #include <rdma/ib_cache.h> |
60 | #include <rdma/ib_cm.h> | |
61 | #include <rdma/ib_sa.h> | |
07ebafba | 62 | #include <rdma/iw_cm.h> |
e51060f0 | 63 | |
218a773f MB |
64 | #include "core_priv.h" |
65 | ||
e51060f0 SH |
66 | MODULE_AUTHOR("Sean Hefty"); |
67 | MODULE_DESCRIPTION("Generic RDMA CM Agent"); | |
68 | MODULE_LICENSE("Dual BSD/GPL"); | |
69 | ||
70 | #define CMA_CM_RESPONSE_TIMEOUT 20 | |
ab15c95a | 71 | #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000 |
d5bb7599 | 72 | #define CMA_MAX_CM_RETRIES 15 |
dcb3f974 | 73 | #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24) |
3c86aa70 | 74 | #define CMA_IBOE_PACKET_LIFETIME 18 |
e51060f0 | 75 | |
2b1b5b60 SG |
76 | static const char * const cma_events[] = { |
77 | [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved", | |
78 | [RDMA_CM_EVENT_ADDR_ERROR] = "address error", | |
79 | [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ", | |
80 | [RDMA_CM_EVENT_ROUTE_ERROR] = "route error", | |
81 | [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request", | |
82 | [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response", | |
83 | [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error", | |
84 | [RDMA_CM_EVENT_UNREACHABLE] = "unreachable", | |
85 | [RDMA_CM_EVENT_REJECTED] = "rejected", | |
86 | [RDMA_CM_EVENT_ESTABLISHED] = "established", | |
87 | [RDMA_CM_EVENT_DISCONNECTED] = "disconnected", | |
88 | [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal", | |
89 | [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join", | |
90 | [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error", | |
91 | [RDMA_CM_EVENT_ADDR_CHANGE] = "address change", | |
92 | [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit", | |
93 | }; | |
94 | ||
db7489e0 | 95 | const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event) |
2b1b5b60 SG |
96 | { |
97 | size_t index = event; | |
98 | ||
99 | return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ? | |
100 | cma_events[index] : "unrecognized event"; | |
101 | } | |
102 | EXPORT_SYMBOL(rdma_event_msg); | |
103 | ||
77a5db13 SW |
104 | const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id, |
105 | int reason) | |
106 | { | |
107 | if (rdma_ib_or_roce(id->device, id->port_num)) | |
108 | return ibcm_reject_msg(reason); | |
109 | ||
110 | if (rdma_protocol_iwarp(id->device, id->port_num)) | |
111 | return iwcm_reject_msg(reason); | |
112 | ||
113 | WARN_ON_ONCE(1); | |
114 | return "unrecognized transport"; | |
115 | } | |
116 | EXPORT_SYMBOL(rdma_reject_msg); | |
117 | ||
5042a73d SW |
118 | bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason) |
119 | { | |
120 | if (rdma_ib_or_roce(id->device, id->port_num)) | |
121 | return reason == IB_CM_REJ_CONSUMER_DEFINED; | |
122 | ||
123 | if (rdma_protocol_iwarp(id->device, id->port_num)) | |
124 | return reason == -ECONNREFUSED; | |
125 | ||
126 | WARN_ON_ONCE(1); | |
127 | return false; | |
128 | } | |
129 | EXPORT_SYMBOL(rdma_is_consumer_reject); | |
130 | ||
5f244104 SW |
131 | const void *rdma_consumer_reject_data(struct rdma_cm_id *id, |
132 | struct rdma_cm_event *ev, u8 *data_len) | |
133 | { | |
134 | const void *p; | |
135 | ||
136 | if (rdma_is_consumer_reject(id, ev->status)) { | |
137 | *data_len = ev->param.conn.private_data_len; | |
138 | p = ev->param.conn.private_data; | |
139 | } else { | |
140 | *data_len = 0; | |
141 | p = NULL; | |
142 | } | |
143 | return p; | |
144 | } | |
145 | EXPORT_SYMBOL(rdma_consumer_reject_data); | |
146 | ||
e51060f0 | 147 | static void cma_add_one(struct ib_device *device); |
7c1eb45a | 148 | static void cma_remove_one(struct ib_device *device, void *client_data); |
e51060f0 SH |
149 | |
150 | static struct ib_client cma_client = { | |
151 | .name = "cma", | |
152 | .add = cma_add_one, | |
153 | .remove = cma_remove_one | |
154 | }; | |
155 | ||
c1a0b23b | 156 | static struct ib_sa_client sa_client; |
7a118df3 | 157 | static struct rdma_addr_client addr_client; |
e51060f0 SH |
158 | static LIST_HEAD(dev_list); |
159 | static LIST_HEAD(listen_any_list); | |
160 | static DEFINE_MUTEX(lock); | |
161 | static struct workqueue_struct *cma_wq; | |
c7d03a00 | 162 | static unsigned int cma_pernet_id; |
e51060f0 | 163 | |
4be74b42 HE |
164 | struct cma_pernet { |
165 | struct idr tcp_ps; | |
166 | struct idr udp_ps; | |
167 | struct idr ipoib_ps; | |
168 | struct idr ib_ps; | |
169 | }; | |
170 | ||
171 | static struct cma_pernet *cma_pernet(struct net *net) | |
172 | { | |
173 | return net_generic(net, cma_pernet_id); | |
174 | } | |
175 | ||
176 | static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps) | |
aac978e1 | 177 | { |
4be74b42 HE |
178 | struct cma_pernet *pernet = cma_pernet(net); |
179 | ||
aac978e1 HE |
180 | switch (ps) { |
181 | case RDMA_PS_TCP: | |
4be74b42 | 182 | return &pernet->tcp_ps; |
aac978e1 | 183 | case RDMA_PS_UDP: |
4be74b42 | 184 | return &pernet->udp_ps; |
aac978e1 | 185 | case RDMA_PS_IPOIB: |
4be74b42 | 186 | return &pernet->ipoib_ps; |
aac978e1 | 187 | case RDMA_PS_IB: |
4be74b42 | 188 | return &pernet->ib_ps; |
aac978e1 HE |
189 | default: |
190 | return NULL; | |
191 | } | |
192 | } | |
193 | ||
e51060f0 SH |
194 | struct cma_device { |
195 | struct list_head list; | |
196 | struct ib_device *device; | |
e51060f0 SH |
197 | struct completion comp; |
198 | atomic_t refcount; | |
199 | struct list_head id_list; | |
045959db | 200 | enum ib_gid_type *default_gid_type; |
89052d78 | 201 | u8 *default_roce_tos; |
e51060f0 SH |
202 | }; |
203 | ||
e51060f0 | 204 | struct rdma_bind_list { |
aac978e1 | 205 | enum rdma_port_space ps; |
e51060f0 SH |
206 | struct hlist_head owners; |
207 | unsigned short port; | |
208 | }; | |
209 | ||
ab15c95a AV |
210 | struct class_port_info_context { |
211 | struct ib_class_port_info *class_port_info; | |
212 | struct ib_device *device; | |
213 | struct completion done; | |
214 | struct ib_sa_query *sa_query; | |
215 | u8 port_num; | |
216 | }; | |
217 | ||
4be74b42 | 218 | static int cma_ps_alloc(struct net *net, enum rdma_port_space ps, |
aac978e1 HE |
219 | struct rdma_bind_list *bind_list, int snum) |
220 | { | |
4be74b42 | 221 | struct idr *idr = cma_pernet_idr(net, ps); |
aac978e1 HE |
222 | |
223 | return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL); | |
224 | } | |
225 | ||
4be74b42 HE |
226 | static struct rdma_bind_list *cma_ps_find(struct net *net, |
227 | enum rdma_port_space ps, int snum) | |
aac978e1 | 228 | { |
4be74b42 | 229 | struct idr *idr = cma_pernet_idr(net, ps); |
aac978e1 HE |
230 | |
231 | return idr_find(idr, snum); | |
232 | } | |
233 | ||
4be74b42 | 234 | static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum) |
aac978e1 | 235 | { |
4be74b42 | 236 | struct idr *idr = cma_pernet_idr(net, ps); |
aac978e1 HE |
237 | |
238 | idr_remove(idr, snum); | |
239 | } | |
240 | ||
68602120 SH |
241 | enum { |
242 | CMA_OPTION_AFONLY, | |
243 | }; | |
244 | ||
218a773f MB |
245 | void cma_ref_dev(struct cma_device *cma_dev) |
246 | { | |
247 | atomic_inc(&cma_dev->refcount); | |
248 | } | |
249 | ||
045959db MB |
250 | struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter, |
251 | void *cookie) | |
252 | { | |
253 | struct cma_device *cma_dev; | |
254 | struct cma_device *found_cma_dev = NULL; | |
255 | ||
256 | mutex_lock(&lock); | |
257 | ||
258 | list_for_each_entry(cma_dev, &dev_list, list) | |
259 | if (filter(cma_dev->device, cookie)) { | |
260 | found_cma_dev = cma_dev; | |
261 | break; | |
262 | } | |
263 | ||
264 | if (found_cma_dev) | |
265 | cma_ref_dev(found_cma_dev); | |
266 | mutex_unlock(&lock); | |
267 | return found_cma_dev; | |
268 | } | |
269 | ||
270 | int cma_get_default_gid_type(struct cma_device *cma_dev, | |
271 | unsigned int port) | |
272 | { | |
24dc831b | 273 | if (!rdma_is_port_valid(cma_dev->device, port)) |
045959db MB |
274 | return -EINVAL; |
275 | ||
276 | return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)]; | |
277 | } | |
278 | ||
279 | int cma_set_default_gid_type(struct cma_device *cma_dev, | |
280 | unsigned int port, | |
281 | enum ib_gid_type default_gid_type) | |
282 | { | |
283 | unsigned long supported_gids; | |
284 | ||
24dc831b | 285 | if (!rdma_is_port_valid(cma_dev->device, port)) |
045959db MB |
286 | return -EINVAL; |
287 | ||
288 | supported_gids = roce_gid_type_mask_support(cma_dev->device, port); | |
289 | ||
290 | if (!(supported_gids & 1 << default_gid_type)) | |
291 | return -EINVAL; | |
292 | ||
293 | cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] = | |
294 | default_gid_type; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
89052d78 MD |
299 | int cma_get_default_roce_tos(struct cma_device *cma_dev, unsigned int port) |
300 | { | |
301 | if (!rdma_is_port_valid(cma_dev->device, port)) | |
302 | return -EINVAL; | |
303 | ||
304 | return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)]; | |
305 | } | |
306 | ||
307 | int cma_set_default_roce_tos(struct cma_device *cma_dev, unsigned int port, | |
308 | u8 default_roce_tos) | |
309 | { | |
310 | if (!rdma_is_port_valid(cma_dev->device, port)) | |
311 | return -EINVAL; | |
312 | ||
313 | cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] = | |
314 | default_roce_tos; | |
315 | ||
316 | return 0; | |
317 | } | |
045959db MB |
318 | struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev) |
319 | { | |
320 | return cma_dev->device; | |
321 | } | |
322 | ||
e51060f0 SH |
323 | /* |
324 | * Device removal can occur at anytime, so we need extra handling to | |
325 | * serialize notifying the user of device removal with other callbacks. | |
326 | * We do this by disabling removal notification while a callback is in process, | |
327 | * and reporting it after the callback completes. | |
328 | */ | |
329 | struct rdma_id_private { | |
330 | struct rdma_cm_id id; | |
331 | ||
332 | struct rdma_bind_list *bind_list; | |
333 | struct hlist_node node; | |
d02d1f53 SH |
334 | struct list_head list; /* listen_any_list or cma_device.list */ |
335 | struct list_head listen_list; /* per device listens */ | |
e51060f0 | 336 | struct cma_device *cma_dev; |
c8f6a362 | 337 | struct list_head mc_list; |
e51060f0 | 338 | |
d02d1f53 | 339 | int internal_id; |
550e5ca7 | 340 | enum rdma_cm_state state; |
e51060f0 | 341 | spinlock_t lock; |
c5483388 SH |
342 | struct mutex qp_mutex; |
343 | ||
e51060f0 SH |
344 | struct completion comp; |
345 | atomic_t refcount; | |
de910bd9 | 346 | struct mutex handler_mutex; |
e51060f0 SH |
347 | |
348 | int backlog; | |
349 | int timeout_ms; | |
350 | struct ib_sa_query *query; | |
351 | int query_id; | |
352 | union { | |
353 | struct ib_cm_id *ib; | |
07ebafba | 354 | struct iw_cm_id *iw; |
e51060f0 SH |
355 | } cm_id; |
356 | ||
357 | u32 seq_num; | |
c8f6a362 | 358 | u32 qkey; |
e51060f0 | 359 | u32 qp_num; |
83e9502d | 360 | pid_t owner; |
68602120 | 361 | u32 options; |
e51060f0 | 362 | u8 srq; |
a81c994d | 363 | u8 tos; |
89052d78 | 364 | bool tos_set; |
a9bb7912 | 365 | u8 reuseaddr; |
5b0ec991 | 366 | u8 afonly; |
cb57bb84 | 367 | enum ib_gid_type gid_type; |
e51060f0 SH |
368 | }; |
369 | ||
c8f6a362 SH |
370 | struct cma_multicast { |
371 | struct rdma_id_private *id_priv; | |
372 | union { | |
373 | struct ib_sa_multicast *ib; | |
374 | } multicast; | |
375 | struct list_head list; | |
376 | void *context; | |
3f446754 | 377 | struct sockaddr_storage addr; |
3c86aa70 | 378 | struct kref mcref; |
bee3c3c9 | 379 | bool igmp_joined; |
ab15c95a | 380 | u8 join_state; |
c8f6a362 SH |
381 | }; |
382 | ||
e51060f0 SH |
383 | struct cma_work { |
384 | struct work_struct work; | |
385 | struct rdma_id_private *id; | |
550e5ca7 NM |
386 | enum rdma_cm_state old_state; |
387 | enum rdma_cm_state new_state; | |
e51060f0 SH |
388 | struct rdma_cm_event event; |
389 | }; | |
390 | ||
dd5bdff8 OG |
391 | struct cma_ndev_work { |
392 | struct work_struct work; | |
393 | struct rdma_id_private *id; | |
394 | struct rdma_cm_event event; | |
395 | }; | |
396 | ||
3c86aa70 EC |
397 | struct iboe_mcast_work { |
398 | struct work_struct work; | |
399 | struct rdma_id_private *id; | |
400 | struct cma_multicast *mc; | |
401 | }; | |
402 | ||
e51060f0 SH |
403 | union cma_ip_addr { |
404 | struct in6_addr ip6; | |
405 | struct { | |
1b90c137 AV |
406 | __be32 pad[3]; |
407 | __be32 addr; | |
e51060f0 SH |
408 | } ip4; |
409 | }; | |
410 | ||
411 | struct cma_hdr { | |
412 | u8 cma_version; | |
413 | u8 ip_version; /* IP version: 7:4 */ | |
1b90c137 | 414 | __be16 port; |
e51060f0 SH |
415 | union cma_ip_addr src_addr; |
416 | union cma_ip_addr dst_addr; | |
417 | }; | |
418 | ||
e51060f0 | 419 | #define CMA_VERSION 0x00 |
e51060f0 | 420 | |
4c21b5bc HE |
421 | struct cma_req_info { |
422 | struct ib_device *device; | |
423 | int port; | |
424 | union ib_gid local_gid; | |
425 | __be64 service_id; | |
426 | u16 pkey; | |
427 | bool has_gid:1; | |
428 | }; | |
429 | ||
550e5ca7 | 430 | static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp) |
e51060f0 SH |
431 | { |
432 | unsigned long flags; | |
433 | int ret; | |
434 | ||
435 | spin_lock_irqsave(&id_priv->lock, flags); | |
436 | ret = (id_priv->state == comp); | |
437 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
438 | return ret; | |
439 | } | |
440 | ||
441 | static int cma_comp_exch(struct rdma_id_private *id_priv, | |
550e5ca7 | 442 | enum rdma_cm_state comp, enum rdma_cm_state exch) |
e51060f0 SH |
443 | { |
444 | unsigned long flags; | |
445 | int ret; | |
446 | ||
447 | spin_lock_irqsave(&id_priv->lock, flags); | |
448 | if ((ret = (id_priv->state == comp))) | |
449 | id_priv->state = exch; | |
450 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
451 | return ret; | |
452 | } | |
453 | ||
550e5ca7 NM |
454 | static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv, |
455 | enum rdma_cm_state exch) | |
e51060f0 SH |
456 | { |
457 | unsigned long flags; | |
550e5ca7 | 458 | enum rdma_cm_state old; |
e51060f0 SH |
459 | |
460 | spin_lock_irqsave(&id_priv->lock, flags); | |
461 | old = id_priv->state; | |
462 | id_priv->state = exch; | |
463 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
464 | return old; | |
465 | } | |
466 | ||
4c21b5bc | 467 | static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr) |
e51060f0 SH |
468 | { |
469 | return hdr->ip_version >> 4; | |
470 | } | |
471 | ||
472 | static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver) | |
473 | { | |
474 | hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF); | |
475 | } | |
476 | ||
bee3c3c9 | 477 | static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join) |
e51060f0 | 478 | { |
bee3c3c9 MS |
479 | struct in_device *in_dev = NULL; |
480 | ||
481 | if (ndev) { | |
482 | rtnl_lock(); | |
483 | in_dev = __in_dev_get_rtnl(ndev); | |
484 | if (in_dev) { | |
485 | if (join) | |
486 | ip_mc_inc_group(in_dev, | |
487 | *(__be32 *)(mgid->raw + 12)); | |
488 | else | |
489 | ip_mc_dec_group(in_dev, | |
490 | *(__be32 *)(mgid->raw + 12)); | |
491 | } | |
492 | rtnl_unlock(); | |
493 | } | |
494 | return (in_dev) ? 0 : -ENODEV; | |
495 | } | |
496 | ||
045959db MB |
497 | static void _cma_attach_to_dev(struct rdma_id_private *id_priv, |
498 | struct cma_device *cma_dev) | |
e51060f0 | 499 | { |
218a773f | 500 | cma_ref_dev(cma_dev); |
e51060f0 | 501 | id_priv->cma_dev = cma_dev; |
045959db | 502 | id_priv->gid_type = 0; |
e51060f0 | 503 | id_priv->id.device = cma_dev->device; |
3c86aa70 EC |
504 | id_priv->id.route.addr.dev_addr.transport = |
505 | rdma_node_get_transport(cma_dev->device->node_type); | |
e51060f0 SH |
506 | list_add_tail(&id_priv->list, &cma_dev->id_list); |
507 | } | |
508 | ||
045959db MB |
509 | static void cma_attach_to_dev(struct rdma_id_private *id_priv, |
510 | struct cma_device *cma_dev) | |
511 | { | |
512 | _cma_attach_to_dev(id_priv, cma_dev); | |
513 | id_priv->gid_type = | |
514 | cma_dev->default_gid_type[id_priv->id.port_num - | |
515 | rdma_start_port(cma_dev->device)]; | |
516 | } | |
517 | ||
218a773f | 518 | void cma_deref_dev(struct cma_device *cma_dev) |
e51060f0 SH |
519 | { |
520 | if (atomic_dec_and_test(&cma_dev->refcount)) | |
521 | complete(&cma_dev->comp); | |
522 | } | |
523 | ||
3c86aa70 EC |
524 | static inline void release_mc(struct kref *kref) |
525 | { | |
526 | struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref); | |
527 | ||
528 | kfree(mc->multicast.ib); | |
529 | kfree(mc); | |
530 | } | |
531 | ||
a396d43a | 532 | static void cma_release_dev(struct rdma_id_private *id_priv) |
e51060f0 | 533 | { |
a396d43a | 534 | mutex_lock(&lock); |
e51060f0 SH |
535 | list_del(&id_priv->list); |
536 | cma_deref_dev(id_priv->cma_dev); | |
537 | id_priv->cma_dev = NULL; | |
a396d43a | 538 | mutex_unlock(&lock); |
e51060f0 SH |
539 | } |
540 | ||
f4753834 SH |
541 | static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv) |
542 | { | |
543 | return (struct sockaddr *) &id_priv->id.route.addr.src_addr; | |
544 | } | |
545 | ||
546 | static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv) | |
547 | { | |
548 | return (struct sockaddr *) &id_priv->id.route.addr.dst_addr; | |
549 | } | |
550 | ||
551 | static inline unsigned short cma_family(struct rdma_id_private *id_priv) | |
552 | { | |
553 | return id_priv->id.route.addr.src_addr.ss_family; | |
554 | } | |
555 | ||
5c438135 | 556 | static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey) |
c8f6a362 SH |
557 | { |
558 | struct ib_sa_mcmember_rec rec; | |
559 | int ret = 0; | |
560 | ||
5c438135 SH |
561 | if (id_priv->qkey) { |
562 | if (qkey && id_priv->qkey != qkey) | |
563 | return -EINVAL; | |
d2ca39f2 | 564 | return 0; |
5c438135 SH |
565 | } |
566 | ||
567 | if (qkey) { | |
568 | id_priv->qkey = qkey; | |
569 | return 0; | |
570 | } | |
d2ca39f2 YE |
571 | |
572 | switch (id_priv->id.ps) { | |
c8f6a362 | 573 | case RDMA_PS_UDP: |
5c438135 | 574 | case RDMA_PS_IB: |
d2ca39f2 | 575 | id_priv->qkey = RDMA_UDP_QKEY; |
c8f6a362 SH |
576 | break; |
577 | case RDMA_PS_IPOIB: | |
d2ca39f2 YE |
578 | ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid); |
579 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, | |
580 | id_priv->id.port_num, &rec.mgid, | |
581 | &rec); | |
582 | if (!ret) | |
583 | id_priv->qkey = be32_to_cpu(rec.qkey); | |
c8f6a362 SH |
584 | break; |
585 | default: | |
586 | break; | |
587 | } | |
588 | return ret; | |
589 | } | |
590 | ||
680f920a SH |
591 | static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr) |
592 | { | |
593 | dev_addr->dev_type = ARPHRD_INFINIBAND; | |
594 | rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr); | |
595 | ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey)); | |
596 | } | |
597 | ||
598 | static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr) | |
599 | { | |
600 | int ret; | |
601 | ||
602 | if (addr->sa_family != AF_IB) { | |
dd5f03be | 603 | ret = rdma_translate_ip(addr, dev_addr, NULL); |
680f920a SH |
604 | } else { |
605 | cma_translate_ib((struct sockaddr_ib *) addr, dev_addr); | |
606 | ret = 0; | |
607 | } | |
608 | ||
609 | return ret; | |
610 | } | |
611 | ||
7c11147d | 612 | static inline int cma_validate_port(struct ib_device *device, u8 port, |
045959db | 613 | enum ib_gid_type gid_type, |
abae1b71 MB |
614 | union ib_gid *gid, int dev_type, |
615 | int bound_if_index) | |
7c11147d | 616 | { |
7c11147d | 617 | int ret = -ENODEV; |
abae1b71 | 618 | struct net_device *ndev = NULL; |
7c11147d MW |
619 | |
620 | if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port)) | |
621 | return ret; | |
622 | ||
623 | if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port)) | |
624 | return ret; | |
625 | ||
cbd09aeb | 626 | if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) |
abae1b71 | 627 | ndev = dev_get_by_index(&init_net, bound_if_index); |
cbd09aeb | 628 | else |
045959db | 629 | gid_type = IB_GID_TYPE_IB; |
cbd09aeb | 630 | |
abae1b71 | 631 | |
045959db | 632 | ret = ib_find_cached_gid_by_port(device, gid, gid_type, port, |
b39ffa1d | 633 | ndev, NULL); |
abae1b71 MB |
634 | |
635 | if (ndev) | |
636 | dev_put(ndev); | |
7c11147d MW |
637 | |
638 | return ret; | |
639 | } | |
640 | ||
be9130cc DL |
641 | static int cma_acquire_dev(struct rdma_id_private *id_priv, |
642 | struct rdma_id_private *listen_id_priv) | |
e51060f0 | 643 | { |
c8f6a362 | 644 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
e51060f0 | 645 | struct cma_device *cma_dev; |
7c11147d | 646 | union ib_gid gid, iboe_gid, *gidp; |
e51060f0 | 647 | int ret = -ENODEV; |
7c11147d | 648 | u8 port; |
e51060f0 | 649 | |
7c11147d | 650 | if (dev_addr->dev_type != ARPHRD_INFINIBAND && |
2efdd6a0 MS |
651 | id_priv->id.ps == RDMA_PS_IPOIB) |
652 | return -EINVAL; | |
653 | ||
a396d43a | 654 | mutex_lock(&lock); |
7b85627b MS |
655 | rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, |
656 | &iboe_gid); | |
657 | ||
3c86aa70 EC |
658 | memcpy(&gid, dev_addr->src_dev_addr + |
659 | rdma_addr_gid_offset(dev_addr), sizeof gid); | |
7c11147d MW |
660 | |
661 | if (listen_id_priv) { | |
be9130cc DL |
662 | cma_dev = listen_id_priv->cma_dev; |
663 | port = listen_id_priv->id.port_num; | |
5d9fb044 | 664 | gidp = rdma_protocol_roce(cma_dev->device, port) ? |
7c11147d | 665 | &iboe_gid : &gid; |
be9130cc | 666 | |
045959db MB |
667 | ret = cma_validate_port(cma_dev->device, port, |
668 | rdma_protocol_ib(cma_dev->device, port) ? | |
669 | IB_GID_TYPE_IB : | |
670 | listen_id_priv->gid_type, gidp, | |
abae1b71 MB |
671 | dev_addr->dev_type, |
672 | dev_addr->bound_dev_if); | |
7c11147d MW |
673 | if (!ret) { |
674 | id_priv->id.port_num = port; | |
be9130cc DL |
675 | goto out; |
676 | } | |
677 | } | |
7c11147d | 678 | |
e51060f0 | 679 | list_for_each_entry(cma_dev, &dev_list, list) { |
3c86aa70 | 680 | for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) { |
be9130cc DL |
681 | if (listen_id_priv && |
682 | listen_id_priv->cma_dev == cma_dev && | |
683 | listen_id_priv->id.port_num == port) | |
684 | continue; | |
7c11147d | 685 | |
5d9fb044 | 686 | gidp = rdma_protocol_roce(cma_dev->device, port) ? |
7c11147d MW |
687 | &iboe_gid : &gid; |
688 | ||
045959db MB |
689 | ret = cma_validate_port(cma_dev->device, port, |
690 | rdma_protocol_ib(cma_dev->device, port) ? | |
691 | IB_GID_TYPE_IB : | |
692 | cma_dev->default_gid_type[port - 1], | |
693 | gidp, dev_addr->dev_type, | |
abae1b71 | 694 | dev_addr->bound_dev_if); |
7c11147d MW |
695 | if (!ret) { |
696 | id_priv->id.port_num = port; | |
697 | goto out; | |
3c86aa70 | 698 | } |
e51060f0 SH |
699 | } |
700 | } | |
3c86aa70 EC |
701 | |
702 | out: | |
703 | if (!ret) | |
704 | cma_attach_to_dev(id_priv, cma_dev); | |
705 | ||
a396d43a | 706 | mutex_unlock(&lock); |
e51060f0 SH |
707 | return ret; |
708 | } | |
709 | ||
f17df3b0 SH |
710 | /* |
711 | * Select the source IB device and address to reach the destination IB address. | |
712 | */ | |
713 | static int cma_resolve_ib_dev(struct rdma_id_private *id_priv) | |
714 | { | |
715 | struct cma_device *cma_dev, *cur_dev; | |
716 | struct sockaddr_ib *addr; | |
717 | union ib_gid gid, sgid, *dgid; | |
718 | u16 pkey, index; | |
8fb488d7 | 719 | u8 p; |
93b1f29d | 720 | enum ib_port_state port_state; |
f17df3b0 SH |
721 | int i; |
722 | ||
723 | cma_dev = NULL; | |
724 | addr = (struct sockaddr_ib *) cma_dst_addr(id_priv); | |
725 | dgid = (union ib_gid *) &addr->sib_addr; | |
726 | pkey = ntohs(addr->sib_pkey); | |
727 | ||
728 | list_for_each_entry(cur_dev, &dev_list, list) { | |
f17df3b0 | 729 | for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) { |
30a74ef4 | 730 | if (!rdma_cap_af_ib(cur_dev->device, p)) |
fef60902 MW |
731 | continue; |
732 | ||
f17df3b0 SH |
733 | if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index)) |
734 | continue; | |
735 | ||
93b1f29d JW |
736 | if (ib_get_cached_port_state(cur_dev->device, p, &port_state)) |
737 | continue; | |
55ee3ab2 MB |
738 | for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i, |
739 | &gid, NULL); | |
740 | i++) { | |
f17df3b0 SH |
741 | if (!memcmp(&gid, dgid, sizeof(gid))) { |
742 | cma_dev = cur_dev; | |
743 | sgid = gid; | |
8fb488d7 | 744 | id_priv->id.port_num = p; |
f17df3b0 SH |
745 | goto found; |
746 | } | |
747 | ||
748 | if (!cma_dev && (gid.global.subnet_prefix == | |
93b1f29d JW |
749 | dgid->global.subnet_prefix) && |
750 | port_state == IB_PORT_ACTIVE) { | |
f17df3b0 SH |
751 | cma_dev = cur_dev; |
752 | sgid = gid; | |
8fb488d7 | 753 | id_priv->id.port_num = p; |
f17df3b0 SH |
754 | } |
755 | } | |
756 | } | |
757 | } | |
758 | ||
759 | if (!cma_dev) | |
760 | return -ENODEV; | |
761 | ||
762 | found: | |
763 | cma_attach_to_dev(id_priv, cma_dev); | |
f17df3b0 SH |
764 | addr = (struct sockaddr_ib *) cma_src_addr(id_priv); |
765 | memcpy(&addr->sib_addr, &sgid, sizeof sgid); | |
766 | cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr); | |
767 | return 0; | |
768 | } | |
769 | ||
e51060f0 SH |
770 | static void cma_deref_id(struct rdma_id_private *id_priv) |
771 | { | |
772 | if (atomic_dec_and_test(&id_priv->refcount)) | |
773 | complete(&id_priv->comp); | |
774 | } | |
775 | ||
fa20105e GS |
776 | struct rdma_cm_id *rdma_create_id(struct net *net, |
777 | rdma_cm_event_handler event_handler, | |
b26f9b99 SH |
778 | void *context, enum rdma_port_space ps, |
779 | enum ib_qp_type qp_type) | |
e51060f0 SH |
780 | { |
781 | struct rdma_id_private *id_priv; | |
782 | ||
783 | id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL); | |
784 | if (!id_priv) | |
785 | return ERR_PTR(-ENOMEM); | |
786 | ||
83e9502d | 787 | id_priv->owner = task_pid_nr(current); |
550e5ca7 | 788 | id_priv->state = RDMA_CM_IDLE; |
e51060f0 SH |
789 | id_priv->id.context = context; |
790 | id_priv->id.event_handler = event_handler; | |
791 | id_priv->id.ps = ps; | |
b26f9b99 | 792 | id_priv->id.qp_type = qp_type; |
89052d78 | 793 | id_priv->tos_set = false; |
e51060f0 | 794 | spin_lock_init(&id_priv->lock); |
c5483388 | 795 | mutex_init(&id_priv->qp_mutex); |
e51060f0 SH |
796 | init_completion(&id_priv->comp); |
797 | atomic_set(&id_priv->refcount, 1); | |
de910bd9 | 798 | mutex_init(&id_priv->handler_mutex); |
e51060f0 | 799 | INIT_LIST_HEAD(&id_priv->listen_list); |
c8f6a362 | 800 | INIT_LIST_HEAD(&id_priv->mc_list); |
e51060f0 | 801 | get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num); |
fa20105e | 802 | id_priv->id.route.addr.dev_addr.net = get_net(net); |
e51060f0 SH |
803 | |
804 | return &id_priv->id; | |
805 | } | |
806 | EXPORT_SYMBOL(rdma_create_id); | |
807 | ||
c8f6a362 | 808 | static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
e51060f0 SH |
809 | { |
810 | struct ib_qp_attr qp_attr; | |
c8f6a362 | 811 | int qp_attr_mask, ret; |
e51060f0 | 812 | |
c8f6a362 SH |
813 | qp_attr.qp_state = IB_QPS_INIT; |
814 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); | |
e51060f0 SH |
815 | if (ret) |
816 | return ret; | |
817 | ||
c8f6a362 SH |
818 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); |
819 | if (ret) | |
820 | return ret; | |
821 | ||
822 | qp_attr.qp_state = IB_QPS_RTR; | |
823 | ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE); | |
824 | if (ret) | |
825 | return ret; | |
826 | ||
827 | qp_attr.qp_state = IB_QPS_RTS; | |
828 | qp_attr.sq_psn = 0; | |
829 | ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN); | |
830 | ||
831 | return ret; | |
e51060f0 SH |
832 | } |
833 | ||
c8f6a362 | 834 | static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
07ebafba TT |
835 | { |
836 | struct ib_qp_attr qp_attr; | |
c8f6a362 | 837 | int qp_attr_mask, ret; |
07ebafba TT |
838 | |
839 | qp_attr.qp_state = IB_QPS_INIT; | |
c8f6a362 SH |
840 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
841 | if (ret) | |
842 | return ret; | |
07ebafba | 843 | |
c8f6a362 | 844 | return ib_modify_qp(qp, &qp_attr, qp_attr_mask); |
07ebafba TT |
845 | } |
846 | ||
e51060f0 SH |
847 | int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, |
848 | struct ib_qp_init_attr *qp_init_attr) | |
849 | { | |
850 | struct rdma_id_private *id_priv; | |
851 | struct ib_qp *qp; | |
852 | int ret; | |
853 | ||
854 | id_priv = container_of(id, struct rdma_id_private, id); | |
855 | if (id->device != pd->device) | |
856 | return -EINVAL; | |
857 | ||
0691a286 | 858 | qp_init_attr->port_num = id->port_num; |
e51060f0 SH |
859 | qp = ib_create_qp(pd, qp_init_attr); |
860 | if (IS_ERR(qp)) | |
861 | return PTR_ERR(qp); | |
862 | ||
b26f9b99 | 863 | if (id->qp_type == IB_QPT_UD) |
c8f6a362 SH |
864 | ret = cma_init_ud_qp(id_priv, qp); |
865 | else | |
866 | ret = cma_init_conn_qp(id_priv, qp); | |
e51060f0 SH |
867 | if (ret) |
868 | goto err; | |
869 | ||
870 | id->qp = qp; | |
871 | id_priv->qp_num = qp->qp_num; | |
e51060f0 SH |
872 | id_priv->srq = (qp->srq != NULL); |
873 | return 0; | |
874 | err: | |
875 | ib_destroy_qp(qp); | |
876 | return ret; | |
877 | } | |
878 | EXPORT_SYMBOL(rdma_create_qp); | |
879 | ||
880 | void rdma_destroy_qp(struct rdma_cm_id *id) | |
881 | { | |
c5483388 SH |
882 | struct rdma_id_private *id_priv; |
883 | ||
884 | id_priv = container_of(id, struct rdma_id_private, id); | |
885 | mutex_lock(&id_priv->qp_mutex); | |
886 | ib_destroy_qp(id_priv->id.qp); | |
887 | id_priv->id.qp = NULL; | |
888 | mutex_unlock(&id_priv->qp_mutex); | |
e51060f0 SH |
889 | } |
890 | EXPORT_SYMBOL(rdma_destroy_qp); | |
891 | ||
5851bb89 SH |
892 | static int cma_modify_qp_rtr(struct rdma_id_private *id_priv, |
893 | struct rdma_conn_param *conn_param) | |
e51060f0 SH |
894 | { |
895 | struct ib_qp_attr qp_attr; | |
896 | int qp_attr_mask, ret; | |
dd5f03be | 897 | union ib_gid sgid; |
e51060f0 | 898 | |
c5483388 SH |
899 | mutex_lock(&id_priv->qp_mutex); |
900 | if (!id_priv->id.qp) { | |
901 | ret = 0; | |
902 | goto out; | |
903 | } | |
e51060f0 SH |
904 | |
905 | /* Need to update QP attributes from default values. */ | |
906 | qp_attr.qp_state = IB_QPS_INIT; | |
c5483388 | 907 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
e51060f0 | 908 | if (ret) |
c5483388 | 909 | goto out; |
e51060f0 | 910 | |
c5483388 | 911 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
e51060f0 | 912 | if (ret) |
c5483388 | 913 | goto out; |
e51060f0 SH |
914 | |
915 | qp_attr.qp_state = IB_QPS_RTR; | |
c5483388 | 916 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
e51060f0 | 917 | if (ret) |
c5483388 | 918 | goto out; |
e51060f0 | 919 | |
dd5f03be | 920 | ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num, |
d8966fcd DC |
921 | rdma_ah_read_grh(&qp_attr.ah_attr)->sgid_index, |
922 | &sgid, NULL); | |
dd5f03be MB |
923 | if (ret) |
924 | goto out; | |
925 | ||
fef60902 MW |
926 | BUG_ON(id_priv->cma_dev->device != id_priv->id.device); |
927 | ||
5851bb89 SH |
928 | if (conn_param) |
929 | qp_attr.max_dest_rd_atomic = conn_param->responder_resources; | |
c5483388 SH |
930 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
931 | out: | |
932 | mutex_unlock(&id_priv->qp_mutex); | |
933 | return ret; | |
e51060f0 SH |
934 | } |
935 | ||
5851bb89 SH |
936 | static int cma_modify_qp_rts(struct rdma_id_private *id_priv, |
937 | struct rdma_conn_param *conn_param) | |
e51060f0 SH |
938 | { |
939 | struct ib_qp_attr qp_attr; | |
940 | int qp_attr_mask, ret; | |
941 | ||
c5483388 SH |
942 | mutex_lock(&id_priv->qp_mutex); |
943 | if (!id_priv->id.qp) { | |
944 | ret = 0; | |
945 | goto out; | |
946 | } | |
e51060f0 SH |
947 | |
948 | qp_attr.qp_state = IB_QPS_RTS; | |
c5483388 | 949 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
e51060f0 | 950 | if (ret) |
c5483388 | 951 | goto out; |
e51060f0 | 952 | |
5851bb89 SH |
953 | if (conn_param) |
954 | qp_attr.max_rd_atomic = conn_param->initiator_depth; | |
c5483388 SH |
955 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
956 | out: | |
957 | mutex_unlock(&id_priv->qp_mutex); | |
958 | return ret; | |
e51060f0 SH |
959 | } |
960 | ||
c5483388 | 961 | static int cma_modify_qp_err(struct rdma_id_private *id_priv) |
e51060f0 SH |
962 | { |
963 | struct ib_qp_attr qp_attr; | |
c5483388 | 964 | int ret; |
e51060f0 | 965 | |
c5483388 SH |
966 | mutex_lock(&id_priv->qp_mutex); |
967 | if (!id_priv->id.qp) { | |
968 | ret = 0; | |
969 | goto out; | |
970 | } | |
e51060f0 SH |
971 | |
972 | qp_attr.qp_state = IB_QPS_ERR; | |
c5483388 SH |
973 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE); |
974 | out: | |
975 | mutex_unlock(&id_priv->qp_mutex); | |
976 | return ret; | |
e51060f0 SH |
977 | } |
978 | ||
c8f6a362 SH |
979 | static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv, |
980 | struct ib_qp_attr *qp_attr, int *qp_attr_mask) | |
981 | { | |
982 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
983 | int ret; | |
3c86aa70 EC |
984 | u16 pkey; |
985 | ||
227128fc | 986 | if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num)) |
3c86aa70 | 987 | pkey = 0xffff; |
fef60902 MW |
988 | else |
989 | pkey = ib_addr_get_pkey(dev_addr); | |
c8f6a362 SH |
990 | |
991 | ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num, | |
3c86aa70 | 992 | pkey, &qp_attr->pkey_index); |
c8f6a362 SH |
993 | if (ret) |
994 | return ret; | |
995 | ||
996 | qp_attr->port_num = id_priv->id.port_num; | |
997 | *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT; | |
998 | ||
b26f9b99 | 999 | if (id_priv->id.qp_type == IB_QPT_UD) { |
5c438135 | 1000 | ret = cma_set_qkey(id_priv, 0); |
d2ca39f2 YE |
1001 | if (ret) |
1002 | return ret; | |
1003 | ||
c8f6a362 SH |
1004 | qp_attr->qkey = id_priv->qkey; |
1005 | *qp_attr_mask |= IB_QP_QKEY; | |
1006 | } else { | |
1007 | qp_attr->qp_access_flags = 0; | |
1008 | *qp_attr_mask |= IB_QP_ACCESS_FLAGS; | |
1009 | } | |
1010 | return 0; | |
1011 | } | |
1012 | ||
e51060f0 SH |
1013 | int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, |
1014 | int *qp_attr_mask) | |
1015 | { | |
1016 | struct rdma_id_private *id_priv; | |
c8f6a362 | 1017 | int ret = 0; |
e51060f0 SH |
1018 | |
1019 | id_priv = container_of(id, struct rdma_id_private, id); | |
72219cea | 1020 | if (rdma_cap_ib_cm(id->device, id->port_num)) { |
b26f9b99 | 1021 | if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD)) |
c8f6a362 SH |
1022 | ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask); |
1023 | else | |
1024 | ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr, | |
1025 | qp_attr_mask); | |
dd5f03be | 1026 | |
e51060f0 SH |
1027 | if (qp_attr->qp_state == IB_QPS_RTR) |
1028 | qp_attr->rq_psn = id_priv->seq_num; | |
04215330 | 1029 | } else if (rdma_cap_iw_cm(id->device, id->port_num)) { |
c8f6a362 | 1030 | if (!id_priv->cm_id.iw) { |
8f076531 | 1031 | qp_attr->qp_access_flags = 0; |
c8f6a362 SH |
1032 | *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS; |
1033 | } else | |
1034 | ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr, | |
1035 | qp_attr_mask); | |
a62ab66b IM |
1036 | qp_attr->port_num = id_priv->id.port_num; |
1037 | *qp_attr_mask |= IB_QP_PORT; | |
21655afc | 1038 | } else |
e51060f0 | 1039 | ret = -ENOSYS; |
e51060f0 SH |
1040 | |
1041 | return ret; | |
1042 | } | |
1043 | EXPORT_SYMBOL(rdma_init_qp_attr); | |
1044 | ||
1045 | static inline int cma_zero_addr(struct sockaddr *addr) | |
1046 | { | |
2e2d190c SH |
1047 | switch (addr->sa_family) { |
1048 | case AF_INET: | |
1049 | return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr); | |
1050 | case AF_INET6: | |
1051 | return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr); | |
1052 | case AF_IB: | |
1053 | return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr); | |
1054 | default: | |
1055 | return 0; | |
e51060f0 SH |
1056 | } |
1057 | } | |
1058 | ||
1059 | static inline int cma_loopback_addr(struct sockaddr *addr) | |
1060 | { | |
2e2d190c SH |
1061 | switch (addr->sa_family) { |
1062 | case AF_INET: | |
1063 | return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr); | |
1064 | case AF_INET6: | |
1065 | return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr); | |
1066 | case AF_IB: | |
1067 | return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr); | |
1068 | default: | |
1069 | return 0; | |
1070 | } | |
e51060f0 SH |
1071 | } |
1072 | ||
1073 | static inline int cma_any_addr(struct sockaddr *addr) | |
1074 | { | |
1075 | return cma_zero_addr(addr) || cma_loopback_addr(addr); | |
1076 | } | |
1077 | ||
43b752da HS |
1078 | static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst) |
1079 | { | |
1080 | if (src->sa_family != dst->sa_family) | |
1081 | return -1; | |
1082 | ||
1083 | switch (src->sa_family) { | |
1084 | case AF_INET: | |
1085 | return ((struct sockaddr_in *) src)->sin_addr.s_addr != | |
1086 | ((struct sockaddr_in *) dst)->sin_addr.s_addr; | |
2e2d190c | 1087 | case AF_INET6: |
43b752da HS |
1088 | return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr, |
1089 | &((struct sockaddr_in6 *) dst)->sin6_addr); | |
2e2d190c SH |
1090 | default: |
1091 | return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr, | |
1092 | &((struct sockaddr_ib *) dst)->sib_addr); | |
43b752da HS |
1093 | } |
1094 | } | |
1095 | ||
58afdcb7 | 1096 | static __be16 cma_port(struct sockaddr *addr) |
628e5f6d | 1097 | { |
58afdcb7 SH |
1098 | struct sockaddr_ib *sib; |
1099 | ||
1100 | switch (addr->sa_family) { | |
1101 | case AF_INET: | |
628e5f6d | 1102 | return ((struct sockaddr_in *) addr)->sin_port; |
58afdcb7 | 1103 | case AF_INET6: |
628e5f6d | 1104 | return ((struct sockaddr_in6 *) addr)->sin6_port; |
58afdcb7 SH |
1105 | case AF_IB: |
1106 | sib = (struct sockaddr_ib *) addr; | |
1107 | return htons((u16) (be64_to_cpu(sib->sib_sid) & | |
1108 | be64_to_cpu(sib->sib_sid_mask))); | |
1109 | default: | |
1110 | return 0; | |
1111 | } | |
628e5f6d SH |
1112 | } |
1113 | ||
e51060f0 SH |
1114 | static inline int cma_any_port(struct sockaddr *addr) |
1115 | { | |
628e5f6d | 1116 | return !cma_port(addr); |
e51060f0 SH |
1117 | } |
1118 | ||
0c505f70 HE |
1119 | static void cma_save_ib_info(struct sockaddr *src_addr, |
1120 | struct sockaddr *dst_addr, | |
1121 | struct rdma_cm_id *listen_id, | |
c2f8fc4e | 1122 | struct sa_path_rec *path) |
e51060f0 | 1123 | { |
fbaa1a6d SH |
1124 | struct sockaddr_ib *listen_ib, *ib; |
1125 | ||
1126 | listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr; | |
0c505f70 HE |
1127 | if (src_addr) { |
1128 | ib = (struct sockaddr_ib *)src_addr; | |
1129 | ib->sib_family = AF_IB; | |
1130 | if (path) { | |
1131 | ib->sib_pkey = path->pkey; | |
1132 | ib->sib_flowinfo = path->flow_label; | |
1133 | memcpy(&ib->sib_addr, &path->sgid, 16); | |
d3957b86 | 1134 | ib->sib_sid = path->service_id; |
0c505f70 HE |
1135 | ib->sib_scope_id = 0; |
1136 | } else { | |
1137 | ib->sib_pkey = listen_ib->sib_pkey; | |
1138 | ib->sib_flowinfo = listen_ib->sib_flowinfo; | |
1139 | ib->sib_addr = listen_ib->sib_addr; | |
1140 | ib->sib_sid = listen_ib->sib_sid; | |
1141 | ib->sib_scope_id = listen_ib->sib_scope_id; | |
1142 | } | |
1143 | ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL); | |
c07678bb | 1144 | } |
0c505f70 HE |
1145 | if (dst_addr) { |
1146 | ib = (struct sockaddr_ib *)dst_addr; | |
1147 | ib->sib_family = AF_IB; | |
1148 | if (path) { | |
1149 | ib->sib_pkey = path->pkey; | |
1150 | ib->sib_flowinfo = path->flow_label; | |
1151 | memcpy(&ib->sib_addr, &path->dgid, 16); | |
1152 | } | |
c07678bb | 1153 | } |
fbaa1a6d | 1154 | } |
e51060f0 | 1155 | |
c50e90d0 AB |
1156 | static void cma_save_ip4_info(struct sockaddr_in *src_addr, |
1157 | struct sockaddr_in *dst_addr, | |
0c505f70 HE |
1158 | struct cma_hdr *hdr, |
1159 | __be16 local_port) | |
fbaa1a6d | 1160 | { |
0c505f70 | 1161 | if (src_addr) { |
c50e90d0 AB |
1162 | *src_addr = (struct sockaddr_in) { |
1163 | .sin_family = AF_INET, | |
1164 | .sin_addr.s_addr = hdr->dst_addr.ip4.addr, | |
1165 | .sin_port = local_port, | |
1166 | }; | |
0c505f70 | 1167 | } |
fbaa1a6d | 1168 | |
0c505f70 | 1169 | if (dst_addr) { |
c50e90d0 AB |
1170 | *dst_addr = (struct sockaddr_in) { |
1171 | .sin_family = AF_INET, | |
1172 | .sin_addr.s_addr = hdr->src_addr.ip4.addr, | |
1173 | .sin_port = hdr->port, | |
1174 | }; | |
0c505f70 | 1175 | } |
e51060f0 SH |
1176 | } |
1177 | ||
c50e90d0 AB |
1178 | static void cma_save_ip6_info(struct sockaddr_in6 *src_addr, |
1179 | struct sockaddr_in6 *dst_addr, | |
0c505f70 HE |
1180 | struct cma_hdr *hdr, |
1181 | __be16 local_port) | |
e51060f0 | 1182 | { |
0c505f70 | 1183 | if (src_addr) { |
c50e90d0 AB |
1184 | *src_addr = (struct sockaddr_in6) { |
1185 | .sin6_family = AF_INET6, | |
1186 | .sin6_addr = hdr->dst_addr.ip6, | |
1187 | .sin6_port = local_port, | |
1188 | }; | |
0c505f70 | 1189 | } |
fbaa1a6d | 1190 | |
0c505f70 | 1191 | if (dst_addr) { |
c50e90d0 AB |
1192 | *dst_addr = (struct sockaddr_in6) { |
1193 | .sin6_family = AF_INET6, | |
1194 | .sin6_addr = hdr->src_addr.ip6, | |
1195 | .sin6_port = hdr->port, | |
1196 | }; | |
0c505f70 | 1197 | } |
fbaa1a6d SH |
1198 | } |
1199 | ||
0c505f70 | 1200 | static u16 cma_port_from_service_id(__be64 service_id) |
fbaa1a6d | 1201 | { |
0c505f70 HE |
1202 | return (u16)be64_to_cpu(service_id); |
1203 | } | |
fbaa1a6d | 1204 | |
0c505f70 HE |
1205 | static int cma_save_ip_info(struct sockaddr *src_addr, |
1206 | struct sockaddr *dst_addr, | |
1207 | struct ib_cm_event *ib_event, | |
1208 | __be64 service_id) | |
1209 | { | |
1210 | struct cma_hdr *hdr; | |
1211 | __be16 port; | |
fbaa1a6d SH |
1212 | |
1213 | hdr = ib_event->private_data; | |
1214 | if (hdr->cma_version != CMA_VERSION) | |
1215 | return -EINVAL; | |
1216 | ||
0c505f70 HE |
1217 | port = htons(cma_port_from_service_id(service_id)); |
1218 | ||
fbaa1a6d | 1219 | switch (cma_get_ip_ver(hdr)) { |
e51060f0 | 1220 | case 4: |
c50e90d0 AB |
1221 | cma_save_ip4_info((struct sockaddr_in *)src_addr, |
1222 | (struct sockaddr_in *)dst_addr, hdr, port); | |
e51060f0 SH |
1223 | break; |
1224 | case 6: | |
c50e90d0 AB |
1225 | cma_save_ip6_info((struct sockaddr_in6 *)src_addr, |
1226 | (struct sockaddr_in6 *)dst_addr, hdr, port); | |
e51060f0 SH |
1227 | break; |
1228 | default: | |
4c21b5bc | 1229 | return -EAFNOSUPPORT; |
e51060f0 | 1230 | } |
0c505f70 | 1231 | |
fbaa1a6d | 1232 | return 0; |
e51060f0 SH |
1233 | } |
1234 | ||
0c505f70 HE |
1235 | static int cma_save_net_info(struct sockaddr *src_addr, |
1236 | struct sockaddr *dst_addr, | |
1237 | struct rdma_cm_id *listen_id, | |
1238 | struct ib_cm_event *ib_event, | |
1239 | sa_family_t sa_family, __be64 service_id) | |
1240 | { | |
1241 | if (sa_family == AF_IB) { | |
1242 | if (ib_event->event == IB_CM_REQ_RECEIVED) | |
1243 | cma_save_ib_info(src_addr, dst_addr, listen_id, | |
1244 | ib_event->param.req_rcvd.primary_path); | |
1245 | else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) | |
1246 | cma_save_ib_info(src_addr, dst_addr, listen_id, NULL); | |
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id); | |
1251 | } | |
1252 | ||
4c21b5bc HE |
1253 | static int cma_save_req_info(const struct ib_cm_event *ib_event, |
1254 | struct cma_req_info *req) | |
1255 | { | |
1256 | const struct ib_cm_req_event_param *req_param = | |
1257 | &ib_event->param.req_rcvd; | |
1258 | const struct ib_cm_sidr_req_event_param *sidr_param = | |
1259 | &ib_event->param.sidr_req_rcvd; | |
1260 | ||
1261 | switch (ib_event->event) { | |
1262 | case IB_CM_REQ_RECEIVED: | |
1263 | req->device = req_param->listen_id->device; | |
1264 | req->port = req_param->port; | |
1265 | memcpy(&req->local_gid, &req_param->primary_path->sgid, | |
1266 | sizeof(req->local_gid)); | |
1267 | req->has_gid = true; | |
d3957b86 | 1268 | req->service_id = req_param->primary_path->service_id; |
ab3964ad | 1269 | req->pkey = be16_to_cpu(req_param->primary_path->pkey); |
84424a7f HE |
1270 | if (req->pkey != req_param->bth_pkey) |
1271 | pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n" | |
1272 | "RDMA CMA: in the future this may cause the request to be dropped\n", | |
1273 | req_param->bth_pkey, req->pkey); | |
4c21b5bc HE |
1274 | break; |
1275 | case IB_CM_SIDR_REQ_RECEIVED: | |
1276 | req->device = sidr_param->listen_id->device; | |
1277 | req->port = sidr_param->port; | |
1278 | req->has_gid = false; | |
1279 | req->service_id = sidr_param->service_id; | |
ab3964ad | 1280 | req->pkey = sidr_param->pkey; |
84424a7f HE |
1281 | if (req->pkey != sidr_param->bth_pkey) |
1282 | pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n" | |
1283 | "RDMA CMA: in the future this may cause the request to be dropped\n", | |
1284 | sidr_param->bth_pkey, req->pkey); | |
4c21b5bc HE |
1285 | break; |
1286 | default: | |
1287 | return -EINVAL; | |
1288 | } | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
f887f2ac HE |
1293 | static bool validate_ipv4_net_dev(struct net_device *net_dev, |
1294 | const struct sockaddr_in *dst_addr, | |
1295 | const struct sockaddr_in *src_addr) | |
1296 | { | |
1297 | __be32 daddr = dst_addr->sin_addr.s_addr, | |
1298 | saddr = src_addr->sin_addr.s_addr; | |
1299 | struct fib_result res; | |
1300 | struct flowi4 fl4; | |
1301 | int err; | |
1302 | bool ret; | |
1303 | ||
1304 | if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || | |
1305 | ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) || | |
1306 | ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) || | |
1307 | ipv4_is_loopback(saddr)) | |
1308 | return false; | |
1309 | ||
1310 | memset(&fl4, 0, sizeof(fl4)); | |
1311 | fl4.flowi4_iif = net_dev->ifindex; | |
1312 | fl4.daddr = daddr; | |
1313 | fl4.saddr = saddr; | |
1314 | ||
1315 | rcu_read_lock(); | |
1316 | err = fib_lookup(dev_net(net_dev), &fl4, &res, 0); | |
d3632493 | 1317 | ret = err == 0 && FIB_RES_DEV(res) == net_dev; |
f887f2ac HE |
1318 | rcu_read_unlock(); |
1319 | ||
1320 | return ret; | |
1321 | } | |
1322 | ||
1323 | static bool validate_ipv6_net_dev(struct net_device *net_dev, | |
1324 | const struct sockaddr_in6 *dst_addr, | |
1325 | const struct sockaddr_in6 *src_addr) | |
1326 | { | |
1327 | #if IS_ENABLED(CONFIG_IPV6) | |
1328 | const int strict = ipv6_addr_type(&dst_addr->sin6_addr) & | |
1329 | IPV6_ADDR_LINKLOCAL; | |
1330 | struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr, | |
1331 | &src_addr->sin6_addr, net_dev->ifindex, | |
1332 | strict); | |
1333 | bool ret; | |
1334 | ||
1335 | if (!rt) | |
1336 | return false; | |
1337 | ||
1338 | ret = rt->rt6i_idev->dev == net_dev; | |
1339 | ip6_rt_put(rt); | |
1340 | ||
1341 | return ret; | |
1342 | #else | |
1343 | return false; | |
1344 | #endif | |
1345 | } | |
1346 | ||
1347 | static bool validate_net_dev(struct net_device *net_dev, | |
1348 | const struct sockaddr *daddr, | |
1349 | const struct sockaddr *saddr) | |
1350 | { | |
1351 | const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr; | |
1352 | const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr; | |
1353 | const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr; | |
1354 | const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr; | |
1355 | ||
1356 | switch (daddr->sa_family) { | |
1357 | case AF_INET: | |
1358 | return saddr->sa_family == AF_INET && | |
1359 | validate_ipv4_net_dev(net_dev, daddr4, saddr4); | |
1360 | ||
1361 | case AF_INET6: | |
1362 | return saddr->sa_family == AF_INET6 && | |
1363 | validate_ipv6_net_dev(net_dev, daddr6, saddr6); | |
1364 | ||
1365 | default: | |
1366 | return false; | |
1367 | } | |
1368 | } | |
1369 | ||
4c21b5bc HE |
1370 | static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event, |
1371 | const struct cma_req_info *req) | |
1372 | { | |
f887f2ac HE |
1373 | struct sockaddr_storage listen_addr_storage, src_addr_storage; |
1374 | struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage, | |
1375 | *src_addr = (struct sockaddr *)&src_addr_storage; | |
4c21b5bc HE |
1376 | struct net_device *net_dev; |
1377 | const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL; | |
1378 | int err; | |
1379 | ||
f887f2ac HE |
1380 | err = cma_save_ip_info(listen_addr, src_addr, ib_event, |
1381 | req->service_id); | |
4c21b5bc HE |
1382 | if (err) |
1383 | return ERR_PTR(err); | |
1384 | ||
1385 | net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey, | |
1386 | gid, listen_addr); | |
1387 | if (!net_dev) | |
1388 | return ERR_PTR(-ENODEV); | |
1389 | ||
f887f2ac HE |
1390 | if (!validate_net_dev(net_dev, listen_addr, src_addr)) { |
1391 | dev_put(net_dev); | |
1392 | return ERR_PTR(-EHOSTUNREACH); | |
1393 | } | |
1394 | ||
4c21b5bc HE |
1395 | return net_dev; |
1396 | } | |
1397 | ||
1398 | static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id) | |
1399 | { | |
1400 | return (be64_to_cpu(service_id) >> 16) & 0xffff; | |
1401 | } | |
1402 | ||
1403 | static bool cma_match_private_data(struct rdma_id_private *id_priv, | |
1404 | const struct cma_hdr *hdr) | |
1405 | { | |
1406 | struct sockaddr *addr = cma_src_addr(id_priv); | |
1407 | __be32 ip4_addr; | |
1408 | struct in6_addr ip6_addr; | |
1409 | ||
1410 | if (cma_any_addr(addr) && !id_priv->afonly) | |
1411 | return true; | |
1412 | ||
1413 | switch (addr->sa_family) { | |
1414 | case AF_INET: | |
1415 | ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; | |
1416 | if (cma_get_ip_ver(hdr) != 4) | |
1417 | return false; | |
1418 | if (!cma_any_addr(addr) && | |
1419 | hdr->dst_addr.ip4.addr != ip4_addr) | |
1420 | return false; | |
1421 | break; | |
1422 | case AF_INET6: | |
1423 | ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr; | |
1424 | if (cma_get_ip_ver(hdr) != 6) | |
1425 | return false; | |
1426 | if (!cma_any_addr(addr) && | |
1427 | memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr))) | |
1428 | return false; | |
1429 | break; | |
1430 | case AF_IB: | |
1431 | return true; | |
1432 | default: | |
1433 | return false; | |
1434 | } | |
1435 | ||
1436 | return true; | |
1437 | } | |
1438 | ||
b8cab5da HE |
1439 | static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num) |
1440 | { | |
1441 | enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num); | |
1442 | enum rdma_transport_type transport = | |
1443 | rdma_node_get_transport(device->node_type); | |
1444 | ||
1445 | return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB; | |
1446 | } | |
1447 | ||
1448 | static bool cma_protocol_roce(const struct rdma_cm_id *id) | |
1449 | { | |
1450 | struct ib_device *device = id->device; | |
1451 | const int port_num = id->port_num ?: rdma_start_port(device); | |
1452 | ||
1453 | return cma_protocol_roce_dev_port(device, port_num); | |
1454 | } | |
1455 | ||
fac51590 MB |
1456 | static bool cma_match_net_dev(const struct rdma_cm_id *id, |
1457 | const struct net_device *net_dev, | |
1458 | u8 port_num) | |
4c21b5bc | 1459 | { |
fac51590 | 1460 | const struct rdma_addr *addr = &id->route.addr; |
4c21b5bc HE |
1461 | |
1462 | if (!net_dev) | |
b8cab5da | 1463 | /* This request is an AF_IB request or a RoCE request */ |
fac51590 MB |
1464 | return (!id->port_num || id->port_num == port_num) && |
1465 | (addr->src_addr.ss_family == AF_IB || | |
1466 | cma_protocol_roce_dev_port(id->device, port_num)); | |
4c21b5bc HE |
1467 | |
1468 | return !addr->dev_addr.bound_dev_if || | |
fa20105e | 1469 | (net_eq(dev_net(net_dev), addr->dev_addr.net) && |
4c21b5bc HE |
1470 | addr->dev_addr.bound_dev_if == net_dev->ifindex); |
1471 | } | |
1472 | ||
1473 | static struct rdma_id_private *cma_find_listener( | |
1474 | const struct rdma_bind_list *bind_list, | |
1475 | const struct ib_cm_id *cm_id, | |
1476 | const struct ib_cm_event *ib_event, | |
1477 | const struct cma_req_info *req, | |
1478 | const struct net_device *net_dev) | |
1479 | { | |
1480 | struct rdma_id_private *id_priv, *id_priv_dev; | |
1481 | ||
1482 | if (!bind_list) | |
1483 | return ERR_PTR(-EINVAL); | |
1484 | ||
1485 | hlist_for_each_entry(id_priv, &bind_list->owners, node) { | |
1486 | if (cma_match_private_data(id_priv, ib_event->private_data)) { | |
1487 | if (id_priv->id.device == cm_id->device && | |
fac51590 | 1488 | cma_match_net_dev(&id_priv->id, net_dev, req->port)) |
4c21b5bc HE |
1489 | return id_priv; |
1490 | list_for_each_entry(id_priv_dev, | |
1491 | &id_priv->listen_list, | |
1492 | listen_list) { | |
1493 | if (id_priv_dev->id.device == cm_id->device && | |
fac51590 | 1494 | cma_match_net_dev(&id_priv_dev->id, net_dev, req->port)) |
4c21b5bc HE |
1495 | return id_priv_dev; |
1496 | } | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | return ERR_PTR(-EINVAL); | |
1501 | } | |
1502 | ||
1503 | static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id, | |
0b3ca768 HE |
1504 | struct ib_cm_event *ib_event, |
1505 | struct net_device **net_dev) | |
4c21b5bc HE |
1506 | { |
1507 | struct cma_req_info req; | |
1508 | struct rdma_bind_list *bind_list; | |
1509 | struct rdma_id_private *id_priv; | |
4c21b5bc HE |
1510 | int err; |
1511 | ||
1512 | err = cma_save_req_info(ib_event, &req); | |
1513 | if (err) | |
1514 | return ERR_PTR(err); | |
1515 | ||
0b3ca768 HE |
1516 | *net_dev = cma_get_net_dev(ib_event, &req); |
1517 | if (IS_ERR(*net_dev)) { | |
1518 | if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) { | |
4c21b5bc | 1519 | /* Assuming the protocol is AF_IB */ |
0b3ca768 | 1520 | *net_dev = NULL; |
b8cab5da HE |
1521 | } else if (cma_protocol_roce_dev_port(req.device, req.port)) { |
1522 | /* TODO find the net dev matching the request parameters | |
1523 | * through the RoCE GID table */ | |
1524 | *net_dev = NULL; | |
4c21b5bc | 1525 | } else { |
0b3ca768 | 1526 | return ERR_CAST(*net_dev); |
4c21b5bc HE |
1527 | } |
1528 | } | |
1529 | ||
fa20105e | 1530 | bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net, |
4be74b42 | 1531 | rdma_ps_from_service_id(req.service_id), |
4c21b5bc | 1532 | cma_port_from_service_id(req.service_id)); |
0b3ca768 | 1533 | id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev); |
b3b51f9f | 1534 | if (IS_ERR(id_priv) && *net_dev) { |
be688195 HE |
1535 | dev_put(*net_dev); |
1536 | *net_dev = NULL; | |
1537 | } | |
4c21b5bc HE |
1538 | |
1539 | return id_priv; | |
1540 | } | |
1541 | ||
e8160e15 | 1542 | static inline int cma_user_data_offset(struct rdma_id_private *id_priv) |
e51060f0 | 1543 | { |
e8160e15 | 1544 | return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr); |
e51060f0 SH |
1545 | } |
1546 | ||
e51060f0 SH |
1547 | static void cma_cancel_route(struct rdma_id_private *id_priv) |
1548 | { | |
fe53ba2f | 1549 | if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) { |
e51060f0 SH |
1550 | if (id_priv->query) |
1551 | ib_sa_cancel_query(id_priv->query_id, id_priv->query); | |
e51060f0 SH |
1552 | } |
1553 | } | |
1554 | ||
e51060f0 SH |
1555 | static void cma_cancel_listens(struct rdma_id_private *id_priv) |
1556 | { | |
1557 | struct rdma_id_private *dev_id_priv; | |
1558 | ||
d02d1f53 SH |
1559 | /* |
1560 | * Remove from listen_any_list to prevent added devices from spawning | |
1561 | * additional listen requests. | |
1562 | */ | |
e51060f0 SH |
1563 | mutex_lock(&lock); |
1564 | list_del(&id_priv->list); | |
1565 | ||
1566 | while (!list_empty(&id_priv->listen_list)) { | |
1567 | dev_id_priv = list_entry(id_priv->listen_list.next, | |
1568 | struct rdma_id_private, listen_list); | |
d02d1f53 SH |
1569 | /* sync with device removal to avoid duplicate destruction */ |
1570 | list_del_init(&dev_id_priv->list); | |
1571 | list_del(&dev_id_priv->listen_list); | |
1572 | mutex_unlock(&lock); | |
1573 | ||
1574 | rdma_destroy_id(&dev_id_priv->id); | |
1575 | mutex_lock(&lock); | |
e51060f0 SH |
1576 | } |
1577 | mutex_unlock(&lock); | |
1578 | } | |
1579 | ||
1580 | static void cma_cancel_operation(struct rdma_id_private *id_priv, | |
550e5ca7 | 1581 | enum rdma_cm_state state) |
e51060f0 SH |
1582 | { |
1583 | switch (state) { | |
550e5ca7 | 1584 | case RDMA_CM_ADDR_QUERY: |
e51060f0 SH |
1585 | rdma_addr_cancel(&id_priv->id.route.addr.dev_addr); |
1586 | break; | |
550e5ca7 | 1587 | case RDMA_CM_ROUTE_QUERY: |
e51060f0 SH |
1588 | cma_cancel_route(id_priv); |
1589 | break; | |
550e5ca7 | 1590 | case RDMA_CM_LISTEN: |
f4753834 | 1591 | if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev) |
e51060f0 SH |
1592 | cma_cancel_listens(id_priv); |
1593 | break; | |
1594 | default: | |
1595 | break; | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | static void cma_release_port(struct rdma_id_private *id_priv) | |
1600 | { | |
1601 | struct rdma_bind_list *bind_list = id_priv->bind_list; | |
fa20105e | 1602 | struct net *net = id_priv->id.route.addr.dev_addr.net; |
e51060f0 SH |
1603 | |
1604 | if (!bind_list) | |
1605 | return; | |
1606 | ||
1607 | mutex_lock(&lock); | |
1608 | hlist_del(&id_priv->node); | |
1609 | if (hlist_empty(&bind_list->owners)) { | |
fa20105e | 1610 | cma_ps_remove(net, bind_list->ps, bind_list->port); |
e51060f0 SH |
1611 | kfree(bind_list); |
1612 | } | |
1613 | mutex_unlock(&lock); | |
1614 | } | |
1615 | ||
c8f6a362 SH |
1616 | static void cma_leave_mc_groups(struct rdma_id_private *id_priv) |
1617 | { | |
1618 | struct cma_multicast *mc; | |
1619 | ||
1620 | while (!list_empty(&id_priv->mc_list)) { | |
1621 | mc = container_of(id_priv->mc_list.next, | |
1622 | struct cma_multicast, list); | |
1623 | list_del(&mc->list); | |
a31ad3b0 | 1624 | if (rdma_cap_ib_mcast(id_priv->cma_dev->device, |
5c9a5282 | 1625 | id_priv->id.port_num)) { |
3c86aa70 EC |
1626 | ib_sa_free_multicast(mc->multicast.ib); |
1627 | kfree(mc); | |
bee3c3c9 MS |
1628 | } else { |
1629 | if (mc->igmp_joined) { | |
1630 | struct rdma_dev_addr *dev_addr = | |
1631 | &id_priv->id.route.addr.dev_addr; | |
1632 | struct net_device *ndev = NULL; | |
1633 | ||
1634 | if (dev_addr->bound_dev_if) | |
1635 | ndev = dev_get_by_index(&init_net, | |
1636 | dev_addr->bound_dev_if); | |
1637 | if (ndev) { | |
1638 | cma_igmp_send(ndev, | |
1639 | &mc->multicast.ib->rec.mgid, | |
1640 | false); | |
1641 | dev_put(ndev); | |
1642 | } | |
1643 | } | |
3c86aa70 | 1644 | kref_put(&mc->mcref, release_mc); |
bee3c3c9 | 1645 | } |
c8f6a362 SH |
1646 | } |
1647 | } | |
1648 | ||
e51060f0 SH |
1649 | void rdma_destroy_id(struct rdma_cm_id *id) |
1650 | { | |
1651 | struct rdma_id_private *id_priv; | |
550e5ca7 | 1652 | enum rdma_cm_state state; |
e51060f0 SH |
1653 | |
1654 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 1655 | state = cma_exch(id_priv, RDMA_CM_DESTROYING); |
e51060f0 SH |
1656 | cma_cancel_operation(id_priv, state); |
1657 | ||
a396d43a SH |
1658 | /* |
1659 | * Wait for any active callback to finish. New callbacks will find | |
1660 | * the id_priv state set to destroying and abort. | |
1661 | */ | |
1662 | mutex_lock(&id_priv->handler_mutex); | |
1663 | mutex_unlock(&id_priv->handler_mutex); | |
1664 | ||
e51060f0 | 1665 | if (id_priv->cma_dev) { |
72219cea | 1666 | if (rdma_cap_ib_cm(id_priv->id.device, 1)) { |
0c9361fc | 1667 | if (id_priv->cm_id.ib) |
e51060f0 | 1668 | ib_destroy_cm_id(id_priv->cm_id.ib); |
04215330 | 1669 | } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) { |
0c9361fc | 1670 | if (id_priv->cm_id.iw) |
07ebafba | 1671 | iw_destroy_cm_id(id_priv->cm_id.iw); |
e51060f0 | 1672 | } |
c8f6a362 | 1673 | cma_leave_mc_groups(id_priv); |
a396d43a | 1674 | cma_release_dev(id_priv); |
e51060f0 SH |
1675 | } |
1676 | ||
1677 | cma_release_port(id_priv); | |
1678 | cma_deref_id(id_priv); | |
1679 | wait_for_completion(&id_priv->comp); | |
1680 | ||
d02d1f53 SH |
1681 | if (id_priv->internal_id) |
1682 | cma_deref_id(id_priv->id.context); | |
1683 | ||
e51060f0 | 1684 | kfree(id_priv->id.route.path_rec); |
fa20105e | 1685 | put_net(id_priv->id.route.addr.dev_addr.net); |
e51060f0 SH |
1686 | kfree(id_priv); |
1687 | } | |
1688 | EXPORT_SYMBOL(rdma_destroy_id); | |
1689 | ||
1690 | static int cma_rep_recv(struct rdma_id_private *id_priv) | |
1691 | { | |
1692 | int ret; | |
1693 | ||
5851bb89 | 1694 | ret = cma_modify_qp_rtr(id_priv, NULL); |
e51060f0 SH |
1695 | if (ret) |
1696 | goto reject; | |
1697 | ||
5851bb89 | 1698 | ret = cma_modify_qp_rts(id_priv, NULL); |
e51060f0 SH |
1699 | if (ret) |
1700 | goto reject; | |
1701 | ||
1702 | ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0); | |
1703 | if (ret) | |
1704 | goto reject; | |
1705 | ||
1706 | return 0; | |
1707 | reject: | |
498683c6 | 1708 | pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret); |
c5483388 | 1709 | cma_modify_qp_err(id_priv); |
e51060f0 SH |
1710 | ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, |
1711 | NULL, 0, NULL, 0); | |
1712 | return ret; | |
1713 | } | |
1714 | ||
a1b1b61f SH |
1715 | static void cma_set_rep_event_data(struct rdma_cm_event *event, |
1716 | struct ib_cm_rep_event_param *rep_data, | |
1717 | void *private_data) | |
1718 | { | |
1719 | event->param.conn.private_data = private_data; | |
1720 | event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE; | |
1721 | event->param.conn.responder_resources = rep_data->responder_resources; | |
1722 | event->param.conn.initiator_depth = rep_data->initiator_depth; | |
1723 | event->param.conn.flow_control = rep_data->flow_control; | |
1724 | event->param.conn.rnr_retry_count = rep_data->rnr_retry_count; | |
1725 | event->param.conn.srq = rep_data->srq; | |
1726 | event->param.conn.qp_num = rep_data->remote_qpn; | |
1727 | } | |
1728 | ||
e51060f0 SH |
1729 | static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
1730 | { | |
1731 | struct rdma_id_private *id_priv = cm_id->context; | |
a1b1b61f SH |
1732 | struct rdma_cm_event event; |
1733 | int ret = 0; | |
e51060f0 | 1734 | |
37e07cda | 1735 | mutex_lock(&id_priv->handler_mutex); |
38ca83a5 | 1736 | if ((ib_event->event != IB_CM_TIMEWAIT_EXIT && |
37e07cda | 1737 | id_priv->state != RDMA_CM_CONNECT) || |
38ca83a5 | 1738 | (ib_event->event == IB_CM_TIMEWAIT_EXIT && |
37e07cda BVA |
1739 | id_priv->state != RDMA_CM_DISCONNECT)) |
1740 | goto out; | |
e51060f0 | 1741 | |
a1b1b61f | 1742 | memset(&event, 0, sizeof event); |
e51060f0 SH |
1743 | switch (ib_event->event) { |
1744 | case IB_CM_REQ_ERROR: | |
1745 | case IB_CM_REP_ERROR: | |
a1b1b61f SH |
1746 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
1747 | event.status = -ETIMEDOUT; | |
e51060f0 SH |
1748 | break; |
1749 | case IB_CM_REP_RECEIVED: | |
61c0ddbe MS |
1750 | if (cma_comp(id_priv, RDMA_CM_CONNECT) && |
1751 | (id_priv->id.qp_type != IB_QPT_UD)) | |
1752 | ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); | |
01602f11 | 1753 | if (id_priv->id.qp) { |
a1b1b61f SH |
1754 | event.status = cma_rep_recv(id_priv); |
1755 | event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR : | |
1756 | RDMA_CM_EVENT_ESTABLISHED; | |
01602f11 | 1757 | } else { |
a1b1b61f | 1758 | event.event = RDMA_CM_EVENT_CONNECT_RESPONSE; |
01602f11 | 1759 | } |
a1b1b61f SH |
1760 | cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd, |
1761 | ib_event->private_data); | |
e51060f0 SH |
1762 | break; |
1763 | case IB_CM_RTU_RECEIVED: | |
0fe313b0 SH |
1764 | case IB_CM_USER_ESTABLISHED: |
1765 | event.event = RDMA_CM_EVENT_ESTABLISHED; | |
e51060f0 SH |
1766 | break; |
1767 | case IB_CM_DREQ_ERROR: | |
a1b1b61f | 1768 | event.status = -ETIMEDOUT; /* fall through */ |
e51060f0 SH |
1769 | case IB_CM_DREQ_RECEIVED: |
1770 | case IB_CM_DREP_RECEIVED: | |
550e5ca7 NM |
1771 | if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT, |
1772 | RDMA_CM_DISCONNECT)) | |
e51060f0 | 1773 | goto out; |
a1b1b61f | 1774 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
e51060f0 SH |
1775 | break; |
1776 | case IB_CM_TIMEWAIT_EXIT: | |
38ca83a5 AV |
1777 | event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT; |
1778 | break; | |
e51060f0 SH |
1779 | case IB_CM_MRA_RECEIVED: |
1780 | /* ignore event */ | |
1781 | goto out; | |
1782 | case IB_CM_REJ_RECEIVED: | |
498683c6 MS |
1783 | pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id, |
1784 | ib_event->param.rej_rcvd.reason)); | |
c5483388 | 1785 | cma_modify_qp_err(id_priv); |
a1b1b61f SH |
1786 | event.status = ib_event->param.rej_rcvd.reason; |
1787 | event.event = RDMA_CM_EVENT_REJECTED; | |
1788 | event.param.conn.private_data = ib_event->private_data; | |
1789 | event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; | |
e51060f0 SH |
1790 | break; |
1791 | default: | |
aba25a3e | 1792 | pr_err("RDMA CMA: unexpected IB CM event: %d\n", |
e51060f0 SH |
1793 | ib_event->event); |
1794 | goto out; | |
1795 | } | |
1796 | ||
a1b1b61f | 1797 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
e51060f0 SH |
1798 | if (ret) { |
1799 | /* Destroy the CM ID by returning a non-zero value. */ | |
1800 | id_priv->cm_id.ib = NULL; | |
550e5ca7 | 1801 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 1802 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1803 | rdma_destroy_id(&id_priv->id); |
1804 | return ret; | |
1805 | } | |
1806 | out: | |
de910bd9 | 1807 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
1808 | return ret; |
1809 | } | |
1810 | ||
628e5f6d | 1811 | static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id, |
0b3ca768 HE |
1812 | struct ib_cm_event *ib_event, |
1813 | struct net_device *net_dev) | |
e51060f0 SH |
1814 | { |
1815 | struct rdma_id_private *id_priv; | |
1816 | struct rdma_cm_id *id; | |
1817 | struct rdma_route *rt; | |
0c505f70 | 1818 | const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family; |
9fdca4da | 1819 | struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path; |
d3957b86 MD |
1820 | const __be64 service_id = |
1821 | ib_event->param.req_rcvd.primary_path->service_id; | |
64c5e613 | 1822 | int ret; |
e51060f0 | 1823 | |
fa20105e GS |
1824 | id = rdma_create_id(listen_id->route.addr.dev_addr.net, |
1825 | listen_id->event_handler, listen_id->context, | |
b26f9b99 | 1826 | listen_id->ps, ib_event->param.req_rcvd.qp_type); |
e51060f0 | 1827 | if (IS_ERR(id)) |
0c9361fc | 1828 | return NULL; |
3f168d2b | 1829 | |
f4753834 | 1830 | id_priv = container_of(id, struct rdma_id_private, id); |
0c505f70 HE |
1831 | if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr, |
1832 | (struct sockaddr *)&id->route.addr.dst_addr, | |
1833 | listen_id, ib_event, ss_family, service_id)) | |
fbaa1a6d | 1834 | goto err; |
e51060f0 SH |
1835 | |
1836 | rt = &id->route; | |
1837 | rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1; | |
3f168d2b KK |
1838 | rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths, |
1839 | GFP_KERNEL); | |
e51060f0 | 1840 | if (!rt->path_rec) |
0c9361fc | 1841 | goto err; |
e51060f0 | 1842 | |
9fdca4da | 1843 | rt->path_rec[0] = *path; |
e51060f0 SH |
1844 | if (rt->num_paths == 2) |
1845 | rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path; | |
1846 | ||
0b3ca768 HE |
1847 | if (net_dev) { |
1848 | ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL); | |
6f8372b6 | 1849 | if (ret) |
0c9361fc | 1850 | goto err; |
0b3ca768 | 1851 | } else { |
b8cab5da HE |
1852 | if (!cma_protocol_roce(listen_id) && |
1853 | cma_any_addr(cma_src_addr(id_priv))) { | |
1854 | rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND; | |
1855 | rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid); | |
1856 | ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey)); | |
1857 | } else if (!cma_any_addr(cma_src_addr(id_priv))) { | |
1858 | ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr); | |
1859 | if (ret) | |
1860 | goto err; | |
1861 | } | |
6f8372b6 SH |
1862 | } |
1863 | rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid); | |
e51060f0 | 1864 | |
550e5ca7 | 1865 | id_priv->state = RDMA_CM_CONNECT; |
e51060f0 | 1866 | return id_priv; |
3f168d2b | 1867 | |
3f168d2b | 1868 | err: |
0c9361fc | 1869 | rdma_destroy_id(id); |
e51060f0 SH |
1870 | return NULL; |
1871 | } | |
1872 | ||
628e5f6d | 1873 | static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id, |
0b3ca768 HE |
1874 | struct ib_cm_event *ib_event, |
1875 | struct net_device *net_dev) | |
628e5f6d SH |
1876 | { |
1877 | struct rdma_id_private *id_priv; | |
1878 | struct rdma_cm_id *id; | |
0c505f70 | 1879 | const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family; |
fa20105e | 1880 | struct net *net = listen_id->route.addr.dev_addr.net; |
628e5f6d SH |
1881 | int ret; |
1882 | ||
fa20105e | 1883 | id = rdma_create_id(net, listen_id->event_handler, listen_id->context, |
b26f9b99 | 1884 | listen_id->ps, IB_QPT_UD); |
628e5f6d SH |
1885 | if (IS_ERR(id)) |
1886 | return NULL; | |
1887 | ||
f4753834 | 1888 | id_priv = container_of(id, struct rdma_id_private, id); |
0c505f70 HE |
1889 | if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr, |
1890 | (struct sockaddr *)&id->route.addr.dst_addr, | |
1891 | listen_id, ib_event, ss_family, | |
1892 | ib_event->param.sidr_req_rcvd.service_id)) | |
628e5f6d SH |
1893 | goto err; |
1894 | ||
0b3ca768 HE |
1895 | if (net_dev) { |
1896 | ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL); | |
6f8372b6 SH |
1897 | if (ret) |
1898 | goto err; | |
0b3ca768 | 1899 | } else { |
b8cab5da HE |
1900 | if (!cma_any_addr(cma_src_addr(id_priv))) { |
1901 | ret = cma_translate_addr(cma_src_addr(id_priv), | |
1902 | &id->route.addr.dev_addr); | |
1903 | if (ret) | |
1904 | goto err; | |
1905 | } | |
6f8372b6 | 1906 | } |
628e5f6d | 1907 | |
550e5ca7 | 1908 | id_priv->state = RDMA_CM_CONNECT; |
628e5f6d SH |
1909 | return id_priv; |
1910 | err: | |
1911 | rdma_destroy_id(id); | |
1912 | return NULL; | |
1913 | } | |
1914 | ||
a1b1b61f SH |
1915 | static void cma_set_req_event_data(struct rdma_cm_event *event, |
1916 | struct ib_cm_req_event_param *req_data, | |
1917 | void *private_data, int offset) | |
1918 | { | |
1919 | event->param.conn.private_data = private_data + offset; | |
1920 | event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset; | |
1921 | event->param.conn.responder_resources = req_data->responder_resources; | |
1922 | event->param.conn.initiator_depth = req_data->initiator_depth; | |
1923 | event->param.conn.flow_control = req_data->flow_control; | |
1924 | event->param.conn.retry_count = req_data->retry_count; | |
1925 | event->param.conn.rnr_retry_count = req_data->rnr_retry_count; | |
1926 | event->param.conn.srq = req_data->srq; | |
1927 | event->param.conn.qp_num = req_data->remote_qpn; | |
1928 | } | |
1929 | ||
9595480c HS |
1930 | static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event) |
1931 | { | |
4dd81e89 | 1932 | return (((ib_event->event == IB_CM_REQ_RECEIVED) && |
9595480c HS |
1933 | (ib_event->param.req_rcvd.qp_type == id->qp_type)) || |
1934 | ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) && | |
1935 | (id->qp_type == IB_QPT_UD)) || | |
1936 | (!id->qp_type)); | |
1937 | } | |
1938 | ||
e51060f0 SH |
1939 | static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
1940 | { | |
37e07cda | 1941 | struct rdma_id_private *listen_id, *conn_id = NULL; |
a1b1b61f | 1942 | struct rdma_cm_event event; |
0b3ca768 | 1943 | struct net_device *net_dev; |
e51060f0 SH |
1944 | int offset, ret; |
1945 | ||
0b3ca768 | 1946 | listen_id = cma_id_from_event(cm_id, ib_event, &net_dev); |
4c21b5bc HE |
1947 | if (IS_ERR(listen_id)) |
1948 | return PTR_ERR(listen_id); | |
1949 | ||
0b3ca768 HE |
1950 | if (!cma_check_req_qp_type(&listen_id->id, ib_event)) { |
1951 | ret = -EINVAL; | |
1952 | goto net_dev_put; | |
1953 | } | |
9595480c | 1954 | |
37e07cda BVA |
1955 | mutex_lock(&listen_id->handler_mutex); |
1956 | if (listen_id->state != RDMA_CM_LISTEN) { | |
0b3ca768 | 1957 | ret = -ECONNABORTED; |
37e07cda | 1958 | goto err1; |
0b3ca768 | 1959 | } |
e51060f0 | 1960 | |
628e5f6d | 1961 | memset(&event, 0, sizeof event); |
e8160e15 | 1962 | offset = cma_user_data_offset(listen_id); |
628e5f6d | 1963 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; |
9595480c | 1964 | if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) { |
0b3ca768 | 1965 | conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev); |
628e5f6d SH |
1966 | event.param.ud.private_data = ib_event->private_data + offset; |
1967 | event.param.ud.private_data_len = | |
1968 | IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset; | |
1969 | } else { | |
0b3ca768 | 1970 | conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev); |
628e5f6d SH |
1971 | cma_set_req_event_data(&event, &ib_event->param.req_rcvd, |
1972 | ib_event->private_data, offset); | |
1973 | } | |
e51060f0 SH |
1974 | if (!conn_id) { |
1975 | ret = -ENOMEM; | |
b6cec8aa | 1976 | goto err1; |
e51060f0 SH |
1977 | } |
1978 | ||
de910bd9 | 1979 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
be9130cc | 1980 | ret = cma_acquire_dev(conn_id, listen_id); |
a1a733f6 | 1981 | if (ret) |
b6cec8aa | 1982 | goto err2; |
e51060f0 SH |
1983 | |
1984 | conn_id->cm_id.ib = cm_id; | |
1985 | cm_id->context = conn_id; | |
1986 | cm_id->cm_handler = cma_ib_handler; | |
1987 | ||
25ae21a1 SH |
1988 | /* |
1989 | * Protect against the user destroying conn_id from another thread | |
1990 | * until we're done accessing it. | |
1991 | */ | |
1992 | atomic_inc(&conn_id->refcount); | |
a1b1b61f | 1993 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
b6cec8aa SH |
1994 | if (ret) |
1995 | goto err3; | |
b6cec8aa SH |
1996 | /* |
1997 | * Acquire mutex to prevent user executing rdma_destroy_id() | |
1998 | * while we're accessing the cm_id. | |
1999 | */ | |
2000 | mutex_lock(&lock); | |
dd5f03be MB |
2001 | if (cma_comp(conn_id, RDMA_CM_CONNECT) && |
2002 | (conn_id->id.qp_type != IB_QPT_UD)) | |
b6cec8aa SH |
2003 | ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); |
2004 | mutex_unlock(&lock); | |
2005 | mutex_unlock(&conn_id->handler_mutex); | |
2006 | mutex_unlock(&listen_id->handler_mutex); | |
25ae21a1 | 2007 | cma_deref_id(conn_id); |
0b3ca768 HE |
2008 | if (net_dev) |
2009 | dev_put(net_dev); | |
b6cec8aa | 2010 | return 0; |
a1a733f6 | 2011 | |
b6cec8aa SH |
2012 | err3: |
2013 | cma_deref_id(conn_id); | |
a1a733f6 KK |
2014 | /* Destroy the CM ID by returning a non-zero value. */ |
2015 | conn_id->cm_id.ib = NULL; | |
b6cec8aa | 2016 | err2: |
550e5ca7 | 2017 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
de910bd9 | 2018 | mutex_unlock(&conn_id->handler_mutex); |
b6cec8aa | 2019 | err1: |
de910bd9 | 2020 | mutex_unlock(&listen_id->handler_mutex); |
b6cec8aa SH |
2021 | if (conn_id) |
2022 | rdma_destroy_id(&conn_id->id); | |
0b3ca768 HE |
2023 | |
2024 | net_dev_put: | |
2025 | if (net_dev) | |
2026 | dev_put(net_dev); | |
2027 | ||
e51060f0 SH |
2028 | return ret; |
2029 | } | |
2030 | ||
cf53936f | 2031 | __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr) |
e51060f0 | 2032 | { |
496ce3ce SH |
2033 | if (addr->sa_family == AF_IB) |
2034 | return ((struct sockaddr_ib *) addr)->sib_sid; | |
2035 | ||
cf53936f | 2036 | return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr))); |
e51060f0 | 2037 | } |
cf53936f | 2038 | EXPORT_SYMBOL(rdma_get_service_id); |
e51060f0 | 2039 | |
07ebafba TT |
2040 | static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) |
2041 | { | |
2042 | struct rdma_id_private *id_priv = iw_id->context; | |
a1b1b61f | 2043 | struct rdma_cm_event event; |
07ebafba | 2044 | int ret = 0; |
24d44a39 SW |
2045 | struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr; |
2046 | struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr; | |
07ebafba | 2047 | |
37e07cda BVA |
2048 | mutex_lock(&id_priv->handler_mutex); |
2049 | if (id_priv->state != RDMA_CM_CONNECT) | |
2050 | goto out; | |
07ebafba | 2051 | |
be65f086 | 2052 | memset(&event, 0, sizeof event); |
07ebafba TT |
2053 | switch (iw_event->event) { |
2054 | case IW_CM_EVENT_CLOSE: | |
a1b1b61f | 2055 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
07ebafba TT |
2056 | break; |
2057 | case IW_CM_EVENT_CONNECT_REPLY: | |
24d44a39 SW |
2058 | memcpy(cma_src_addr(id_priv), laddr, |
2059 | rdma_addr_size(laddr)); | |
2060 | memcpy(cma_dst_addr(id_priv), raddr, | |
2061 | rdma_addr_size(raddr)); | |
881a045f SW |
2062 | switch (iw_event->status) { |
2063 | case 0: | |
a1b1b61f | 2064 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
3ebeebc3 KS |
2065 | event.param.conn.initiator_depth = iw_event->ird; |
2066 | event.param.conn.responder_resources = iw_event->ord; | |
881a045f SW |
2067 | break; |
2068 | case -ECONNRESET: | |
2069 | case -ECONNREFUSED: | |
2070 | event.event = RDMA_CM_EVENT_REJECTED; | |
2071 | break; | |
2072 | case -ETIMEDOUT: | |
2073 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
2074 | break; | |
2075 | default: | |
2076 | event.event = RDMA_CM_EVENT_CONNECT_ERROR; | |
2077 | break; | |
2078 | } | |
07ebafba TT |
2079 | break; |
2080 | case IW_CM_EVENT_ESTABLISHED: | |
a1b1b61f | 2081 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
3ebeebc3 KS |
2082 | event.param.conn.initiator_depth = iw_event->ird; |
2083 | event.param.conn.responder_resources = iw_event->ord; | |
07ebafba TT |
2084 | break; |
2085 | default: | |
2086 | BUG_ON(1); | |
2087 | } | |
2088 | ||
a1b1b61f SH |
2089 | event.status = iw_event->status; |
2090 | event.param.conn.private_data = iw_event->private_data; | |
2091 | event.param.conn.private_data_len = iw_event->private_data_len; | |
2092 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
07ebafba TT |
2093 | if (ret) { |
2094 | /* Destroy the CM ID by returning a non-zero value. */ | |
2095 | id_priv->cm_id.iw = NULL; | |
550e5ca7 | 2096 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 2097 | mutex_unlock(&id_priv->handler_mutex); |
07ebafba TT |
2098 | rdma_destroy_id(&id_priv->id); |
2099 | return ret; | |
2100 | } | |
2101 | ||
37e07cda | 2102 | out: |
de910bd9 | 2103 | mutex_unlock(&id_priv->handler_mutex); |
07ebafba TT |
2104 | return ret; |
2105 | } | |
2106 | ||
2107 | static int iw_conn_req_handler(struct iw_cm_id *cm_id, | |
2108 | struct iw_cm_event *iw_event) | |
2109 | { | |
2110 | struct rdma_cm_id *new_cm_id; | |
2111 | struct rdma_id_private *listen_id, *conn_id; | |
a1b1b61f | 2112 | struct rdma_cm_event event; |
37e07cda | 2113 | int ret = -ECONNABORTED; |
24d44a39 SW |
2114 | struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr; |
2115 | struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr; | |
07ebafba TT |
2116 | |
2117 | listen_id = cm_id->context; | |
37e07cda BVA |
2118 | |
2119 | mutex_lock(&listen_id->handler_mutex); | |
2120 | if (listen_id->state != RDMA_CM_LISTEN) | |
2121 | goto out; | |
07ebafba TT |
2122 | |
2123 | /* Create a new RDMA id for the new IW CM ID */ | |
fa20105e GS |
2124 | new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net, |
2125 | listen_id->id.event_handler, | |
07ebafba | 2126 | listen_id->id.context, |
b26f9b99 | 2127 | RDMA_PS_TCP, IB_QPT_RC); |
10f32065 | 2128 | if (IS_ERR(new_cm_id)) { |
07ebafba TT |
2129 | ret = -ENOMEM; |
2130 | goto out; | |
2131 | } | |
2132 | conn_id = container_of(new_cm_id, struct rdma_id_private, id); | |
de910bd9 | 2133 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
550e5ca7 | 2134 | conn_id->state = RDMA_CM_CONNECT; |
07ebafba | 2135 | |
dd5f03be | 2136 | ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL); |
07ebafba | 2137 | if (ret) { |
de910bd9 | 2138 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
2139 | rdma_destroy_id(new_cm_id); |
2140 | goto out; | |
2141 | } | |
2142 | ||
be9130cc | 2143 | ret = cma_acquire_dev(conn_id, listen_id); |
07ebafba | 2144 | if (ret) { |
de910bd9 | 2145 | mutex_unlock(&conn_id->handler_mutex); |
07ebafba TT |
2146 | rdma_destroy_id(new_cm_id); |
2147 | goto out; | |
2148 | } | |
2149 | ||
2150 | conn_id->cm_id.iw = cm_id; | |
2151 | cm_id->context = conn_id; | |
2152 | cm_id->cm_handler = cma_iw_handler; | |
2153 | ||
24d44a39 SW |
2154 | memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr)); |
2155 | memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr)); | |
07ebafba | 2156 | |
a1b1b61f SH |
2157 | memset(&event, 0, sizeof event); |
2158 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; | |
2159 | event.param.conn.private_data = iw_event->private_data; | |
2160 | event.param.conn.private_data_len = iw_event->private_data_len; | |
3ebeebc3 KS |
2161 | event.param.conn.initiator_depth = iw_event->ird; |
2162 | event.param.conn.responder_resources = iw_event->ord; | |
25ae21a1 SH |
2163 | |
2164 | /* | |
2165 | * Protect against the user destroying conn_id from another thread | |
2166 | * until we're done accessing it. | |
2167 | */ | |
2168 | atomic_inc(&conn_id->refcount); | |
a1b1b61f | 2169 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
07ebafba TT |
2170 | if (ret) { |
2171 | /* User wants to destroy the CM ID */ | |
2172 | conn_id->cm_id.iw = NULL; | |
550e5ca7 | 2173 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
de910bd9 | 2174 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 2175 | cma_deref_id(conn_id); |
07ebafba | 2176 | rdma_destroy_id(&conn_id->id); |
de910bd9 | 2177 | goto out; |
07ebafba TT |
2178 | } |
2179 | ||
de910bd9 | 2180 | mutex_unlock(&conn_id->handler_mutex); |
25ae21a1 | 2181 | cma_deref_id(conn_id); |
de910bd9 | 2182 | |
07ebafba | 2183 | out: |
de910bd9 | 2184 | mutex_unlock(&listen_id->handler_mutex); |
07ebafba TT |
2185 | return ret; |
2186 | } | |
2187 | ||
e51060f0 SH |
2188 | static int cma_ib_listen(struct rdma_id_private *id_priv) |
2189 | { | |
e51060f0 | 2190 | struct sockaddr *addr; |
0c9361fc | 2191 | struct ib_cm_id *id; |
e51060f0 | 2192 | __be64 svc_id; |
e51060f0 | 2193 | |
51efe394 HE |
2194 | addr = cma_src_addr(id_priv); |
2195 | svc_id = rdma_get_service_id(&id_priv->id, addr); | |
2196 | id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id); | |
0c9361fc JM |
2197 | if (IS_ERR(id)) |
2198 | return PTR_ERR(id); | |
0c9361fc | 2199 | id_priv->cm_id.ib = id; |
e51060f0 | 2200 | |
51efe394 | 2201 | return 0; |
e51060f0 SH |
2202 | } |
2203 | ||
07ebafba TT |
2204 | static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) |
2205 | { | |
2206 | int ret; | |
0c9361fc JM |
2207 | struct iw_cm_id *id; |
2208 | ||
2209 | id = iw_create_cm_id(id_priv->id.device, | |
2210 | iw_conn_req_handler, | |
2211 | id_priv); | |
2212 | if (IS_ERR(id)) | |
2213 | return PTR_ERR(id); | |
07ebafba | 2214 | |
68cdba06 | 2215 | id->tos = id_priv->tos; |
0c9361fc | 2216 | id_priv->cm_id.iw = id; |
07ebafba | 2217 | |
24d44a39 SW |
2218 | memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv), |
2219 | rdma_addr_size(cma_src_addr(id_priv))); | |
07ebafba TT |
2220 | |
2221 | ret = iw_cm_listen(id_priv->cm_id.iw, backlog); | |
2222 | ||
2223 | if (ret) { | |
2224 | iw_destroy_cm_id(id_priv->cm_id.iw); | |
2225 | id_priv->cm_id.iw = NULL; | |
2226 | } | |
2227 | ||
2228 | return ret; | |
2229 | } | |
2230 | ||
e51060f0 SH |
2231 | static int cma_listen_handler(struct rdma_cm_id *id, |
2232 | struct rdma_cm_event *event) | |
2233 | { | |
2234 | struct rdma_id_private *id_priv = id->context; | |
2235 | ||
2236 | id->context = id_priv->id.context; | |
2237 | id->event_handler = id_priv->id.event_handler; | |
2238 | return id_priv->id.event_handler(id, event); | |
2239 | } | |
2240 | ||
2241 | static void cma_listen_on_dev(struct rdma_id_private *id_priv, | |
2242 | struct cma_device *cma_dev) | |
2243 | { | |
2244 | struct rdma_id_private *dev_id_priv; | |
2245 | struct rdma_cm_id *id; | |
fa20105e | 2246 | struct net *net = id_priv->id.route.addr.dev_addr.net; |
e51060f0 SH |
2247 | int ret; |
2248 | ||
72219cea | 2249 | if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1)) |
94d0c939 SH |
2250 | return; |
2251 | ||
fa20105e | 2252 | id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps, |
b26f9b99 | 2253 | id_priv->id.qp_type); |
e51060f0 SH |
2254 | if (IS_ERR(id)) |
2255 | return; | |
2256 | ||
2257 | dev_id_priv = container_of(id, struct rdma_id_private, id); | |
2258 | ||
550e5ca7 | 2259 | dev_id_priv->state = RDMA_CM_ADDR_BOUND; |
f4753834 SH |
2260 | memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv), |
2261 | rdma_addr_size(cma_src_addr(id_priv))); | |
e51060f0 | 2262 | |
045959db | 2263 | _cma_attach_to_dev(dev_id_priv, cma_dev); |
e51060f0 | 2264 | list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); |
d02d1f53 SH |
2265 | atomic_inc(&id_priv->refcount); |
2266 | dev_id_priv->internal_id = 1; | |
5b0ec991 | 2267 | dev_id_priv->afonly = id_priv->afonly; |
e51060f0 SH |
2268 | |
2269 | ret = rdma_listen(id, id_priv->backlog); | |
2270 | if (ret) | |
aba25a3e PP |
2271 | pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n", |
2272 | ret, cma_dev->device->name); | |
e51060f0 SH |
2273 | } |
2274 | ||
2275 | static void cma_listen_on_all(struct rdma_id_private *id_priv) | |
2276 | { | |
2277 | struct cma_device *cma_dev; | |
2278 | ||
2279 | mutex_lock(&lock); | |
2280 | list_add_tail(&id_priv->list, &listen_any_list); | |
2281 | list_for_each_entry(cma_dev, &dev_list, list) | |
2282 | cma_listen_on_dev(id_priv, cma_dev); | |
2283 | mutex_unlock(&lock); | |
2284 | } | |
2285 | ||
a81c994d SH |
2286 | void rdma_set_service_type(struct rdma_cm_id *id, int tos) |
2287 | { | |
2288 | struct rdma_id_private *id_priv; | |
2289 | ||
2290 | id_priv = container_of(id, struct rdma_id_private, id); | |
2291 | id_priv->tos = (u8) tos; | |
89052d78 | 2292 | id_priv->tos_set = true; |
a81c994d SH |
2293 | } |
2294 | EXPORT_SYMBOL(rdma_set_service_type); | |
2295 | ||
c2f8fc4e | 2296 | static void cma_query_handler(int status, struct sa_path_rec *path_rec, |
e51060f0 SH |
2297 | void *context) |
2298 | { | |
2299 | struct cma_work *work = context; | |
2300 | struct rdma_route *route; | |
2301 | ||
2302 | route = &work->id->id.route; | |
2303 | ||
2304 | if (!status) { | |
2305 | route->num_paths = 1; | |
2306 | *route->path_rec = *path_rec; | |
2307 | } else { | |
550e5ca7 NM |
2308 | work->old_state = RDMA_CM_ROUTE_QUERY; |
2309 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
e51060f0 | 2310 | work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; |
8f0472d3 | 2311 | work->event.status = status; |
498683c6 MS |
2312 | pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n", |
2313 | status); | |
e51060f0 SH |
2314 | } |
2315 | ||
2316 | queue_work(cma_wq, &work->work); | |
2317 | } | |
2318 | ||
2319 | static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms, | |
2320 | struct cma_work *work) | |
2321 | { | |
f4753834 | 2322 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
c2f8fc4e | 2323 | struct sa_path_rec path_rec; |
a81c994d SH |
2324 | ib_sa_comp_mask comp_mask; |
2325 | struct sockaddr_in6 *sin6; | |
f68194ca | 2326 | struct sockaddr_ib *sib; |
e51060f0 SH |
2327 | |
2328 | memset(&path_rec, 0, sizeof path_rec); | |
4c33bd19 DC |
2329 | |
2330 | if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num)) | |
2331 | path_rec.rec_type = SA_PATH_REC_TYPE_OPA; | |
2332 | else | |
2333 | path_rec.rec_type = SA_PATH_REC_TYPE_IB; | |
f4753834 SH |
2334 | rdma_addr_get_sgid(dev_addr, &path_rec.sgid); |
2335 | rdma_addr_get_dgid(dev_addr, &path_rec.dgid); | |
2336 | path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); | |
e51060f0 | 2337 | path_rec.numb_path = 1; |
962063e6 | 2338 | path_rec.reversible = 1; |
d3957b86 MD |
2339 | path_rec.service_id = rdma_get_service_id(&id_priv->id, |
2340 | cma_dst_addr(id_priv)); | |
a81c994d SH |
2341 | |
2342 | comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | | |
2343 | IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH | | |
2344 | IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID; | |
2345 | ||
f68194ca SH |
2346 | switch (cma_family(id_priv)) { |
2347 | case AF_INET: | |
a81c994d SH |
2348 | path_rec.qos_class = cpu_to_be16((u16) id_priv->tos); |
2349 | comp_mask |= IB_SA_PATH_REC_QOS_CLASS; | |
f68194ca SH |
2350 | break; |
2351 | case AF_INET6: | |
f4753834 | 2352 | sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv); |
a81c994d SH |
2353 | path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20); |
2354 | comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; | |
f68194ca SH |
2355 | break; |
2356 | case AF_IB: | |
2357 | sib = (struct sockaddr_ib *) cma_src_addr(id_priv); | |
2358 | path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20); | |
2359 | comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; | |
2360 | break; | |
a81c994d | 2361 | } |
e51060f0 | 2362 | |
c1a0b23b | 2363 | id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device, |
a81c994d SH |
2364 | id_priv->id.port_num, &path_rec, |
2365 | comp_mask, timeout_ms, | |
2366 | GFP_KERNEL, cma_query_handler, | |
2367 | work, &id_priv->query); | |
e51060f0 SH |
2368 | |
2369 | return (id_priv->query_id < 0) ? id_priv->query_id : 0; | |
2370 | } | |
2371 | ||
c4028958 | 2372 | static void cma_work_handler(struct work_struct *_work) |
e51060f0 | 2373 | { |
c4028958 | 2374 | struct cma_work *work = container_of(_work, struct cma_work, work); |
e51060f0 SH |
2375 | struct rdma_id_private *id_priv = work->id; |
2376 | int destroy = 0; | |
2377 | ||
de910bd9 | 2378 | mutex_lock(&id_priv->handler_mutex); |
e51060f0 SH |
2379 | if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) |
2380 | goto out; | |
2381 | ||
2382 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
550e5ca7 | 2383 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
e51060f0 SH |
2384 | destroy = 1; |
2385 | } | |
2386 | out: | |
de910bd9 | 2387 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
2388 | cma_deref_id(id_priv); |
2389 | if (destroy) | |
2390 | rdma_destroy_id(&id_priv->id); | |
2391 | kfree(work); | |
2392 | } | |
2393 | ||
dd5bdff8 OG |
2394 | static void cma_ndev_work_handler(struct work_struct *_work) |
2395 | { | |
2396 | struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work); | |
2397 | struct rdma_id_private *id_priv = work->id; | |
2398 | int destroy = 0; | |
2399 | ||
2400 | mutex_lock(&id_priv->handler_mutex); | |
550e5ca7 NM |
2401 | if (id_priv->state == RDMA_CM_DESTROYING || |
2402 | id_priv->state == RDMA_CM_DEVICE_REMOVAL) | |
dd5bdff8 OG |
2403 | goto out; |
2404 | ||
2405 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { | |
550e5ca7 | 2406 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
dd5bdff8 OG |
2407 | destroy = 1; |
2408 | } | |
2409 | ||
2410 | out: | |
2411 | mutex_unlock(&id_priv->handler_mutex); | |
2412 | cma_deref_id(id_priv); | |
2413 | if (destroy) | |
2414 | rdma_destroy_id(&id_priv->id); | |
2415 | kfree(work); | |
2416 | } | |
2417 | ||
e51060f0 SH |
2418 | static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms) |
2419 | { | |
2420 | struct rdma_route *route = &id_priv->id.route; | |
2421 | struct cma_work *work; | |
2422 | int ret; | |
2423 | ||
2424 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
2425 | if (!work) | |
2426 | return -ENOMEM; | |
2427 | ||
2428 | work->id = id_priv; | |
c4028958 | 2429 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
2430 | work->old_state = RDMA_CM_ROUTE_QUERY; |
2431 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
e51060f0 SH |
2432 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
2433 | ||
2434 | route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); | |
2435 | if (!route->path_rec) { | |
2436 | ret = -ENOMEM; | |
2437 | goto err1; | |
2438 | } | |
2439 | ||
2440 | ret = cma_query_ib_route(id_priv, timeout_ms, work); | |
2441 | if (ret) | |
2442 | goto err2; | |
2443 | ||
2444 | return 0; | |
2445 | err2: | |
2446 | kfree(route->path_rec); | |
2447 | route->path_rec = NULL; | |
2448 | err1: | |
2449 | kfree(work); | |
2450 | return ret; | |
2451 | } | |
2452 | ||
2453 | int rdma_set_ib_paths(struct rdma_cm_id *id, | |
c2f8fc4e | 2454 | struct sa_path_rec *path_rec, int num_paths) |
e51060f0 SH |
2455 | { |
2456 | struct rdma_id_private *id_priv; | |
2457 | int ret; | |
2458 | ||
2459 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 NM |
2460 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
2461 | RDMA_CM_ROUTE_RESOLVED)) | |
e51060f0 SH |
2462 | return -EINVAL; |
2463 | ||
9893e742 JL |
2464 | id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths, |
2465 | GFP_KERNEL); | |
e51060f0 SH |
2466 | if (!id->route.path_rec) { |
2467 | ret = -ENOMEM; | |
2468 | goto err; | |
2469 | } | |
2470 | ||
ae2d9293 | 2471 | id->route.num_paths = num_paths; |
e51060f0 SH |
2472 | return 0; |
2473 | err: | |
550e5ca7 | 2474 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED); |
e51060f0 SH |
2475 | return ret; |
2476 | } | |
2477 | EXPORT_SYMBOL(rdma_set_ib_paths); | |
2478 | ||
07ebafba TT |
2479 | static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms) |
2480 | { | |
2481 | struct cma_work *work; | |
2482 | ||
2483 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
2484 | if (!work) | |
2485 | return -ENOMEM; | |
2486 | ||
2487 | work->id = id_priv; | |
c4028958 | 2488 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
2489 | work->old_state = RDMA_CM_ROUTE_QUERY; |
2490 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
07ebafba TT |
2491 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
2492 | queue_work(cma_wq, &work->work); | |
2493 | return 0; | |
2494 | } | |
2495 | ||
eb072c4b EP |
2496 | static int iboe_tos_to_sl(struct net_device *ndev, int tos) |
2497 | { | |
2498 | int prio; | |
2499 | struct net_device *dev; | |
2500 | ||
2501 | prio = rt_tos2priority(tos); | |
d0d7b10b | 2502 | dev = is_vlan_dev(ndev) ? vlan_dev_real_dev(ndev) : ndev; |
eb072c4b EP |
2503 | if (dev->num_tc) |
2504 | return netdev_get_prio_tc_map(dev, prio); | |
2505 | ||
2506 | #if IS_ENABLED(CONFIG_VLAN_8021Q) | |
d0d7b10b | 2507 | if (is_vlan_dev(ndev)) |
eb072c4b EP |
2508 | return (vlan_dev_get_egress_qos_mask(ndev, prio) & |
2509 | VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; | |
2510 | #endif | |
2511 | return 0; | |
2512 | } | |
2513 | ||
aeb76df4 LR |
2514 | static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type, |
2515 | unsigned long supported_gids, | |
2516 | enum ib_gid_type default_gid) | |
2517 | { | |
2518 | if ((network_type == RDMA_NETWORK_IPV4 || | |
2519 | network_type == RDMA_NETWORK_IPV6) && | |
2520 | test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids)) | |
2521 | return IB_GID_TYPE_ROCE_UDP_ENCAP; | |
2522 | ||
2523 | return default_gid; | |
2524 | } | |
2525 | ||
3c86aa70 EC |
2526 | static int cma_resolve_iboe_route(struct rdma_id_private *id_priv) |
2527 | { | |
2528 | struct rdma_route *route = &id_priv->id.route; | |
2529 | struct rdma_addr *addr = &route->addr; | |
2530 | struct cma_work *work; | |
2531 | int ret; | |
3c86aa70 | 2532 | struct net_device *ndev = NULL; |
dfa834e1 | 2533 | enum ib_gid_type gid_type = IB_GID_TYPE_IB; |
89052d78 MD |
2534 | u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num - |
2535 | rdma_start_port(id_priv->cma_dev->device)]; | |
2536 | u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos; | |
dd5f03be | 2537 | |
3c86aa70 | 2538 | |
3c86aa70 EC |
2539 | work = kzalloc(sizeof *work, GFP_KERNEL); |
2540 | if (!work) | |
2541 | return -ENOMEM; | |
2542 | ||
2543 | work->id = id_priv; | |
2544 | INIT_WORK(&work->work, cma_work_handler); | |
2545 | ||
2546 | route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL); | |
2547 | if (!route->path_rec) { | |
2548 | ret = -ENOMEM; | |
2549 | goto err1; | |
2550 | } | |
2551 | ||
2552 | route->num_paths = 1; | |
2553 | ||
abae1b71 | 2554 | if (addr->dev_addr.bound_dev_if) { |
aeb76df4 LR |
2555 | unsigned long supported_gids; |
2556 | ||
3c86aa70 | 2557 | ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if); |
23d70503 WY |
2558 | if (!ndev) { |
2559 | ret = -ENODEV; | |
2560 | goto err2; | |
2561 | } | |
20029832 | 2562 | |
aeb76df4 LR |
2563 | supported_gids = roce_gid_type_mask_support(id_priv->id.device, |
2564 | id_priv->id.port_num); | |
dfa834e1 DC |
2565 | gid_type = cma_route_gid_type(addr->dev_addr.network, |
2566 | supported_gids, | |
2567 | id_priv->gid_type); | |
2568 | route->path_rec->rec_type = | |
2569 | sa_conv_gid_to_pathrec_type(gid_type); | |
9fdca4da DC |
2570 | sa_path_set_ndev(route->path_rec, &init_net); |
2571 | sa_path_set_ifindex(route->path_rec, ndev->ifindex); | |
abae1b71 | 2572 | } |
3c86aa70 EC |
2573 | if (!ndev) { |
2574 | ret = -ENODEV; | |
2575 | goto err2; | |
2576 | } | |
2577 | ||
9fdca4da | 2578 | sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr); |
af7bd463 | 2579 | |
7b85627b MS |
2580 | rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, |
2581 | &route->path_rec->sgid); | |
2582 | rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr, | |
2583 | &route->path_rec->dgid); | |
af7bd463 | 2584 | |
c865f246 | 2585 | /* Use the hint from IP Stack to select GID Type */ |
dfa834e1 DC |
2586 | if (gid_type < ib_network_to_gid_type(addr->dev_addr.network)) |
2587 | gid_type = ib_network_to_gid_type(addr->dev_addr.network); | |
2588 | route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type); | |
2589 | ||
c3efe750 | 2590 | if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB) |
c865f246 | 2591 | /* TODO: get the hoplimit from the inet/inet6 device */ |
c3efe750 MB |
2592 | route->path_rec->hop_limit = addr->dev_addr.hoplimit; |
2593 | else | |
c865f246 | 2594 | route->path_rec->hop_limit = 1; |
af7bd463 EC |
2595 | route->path_rec->reversible = 1; |
2596 | route->path_rec->pkey = cpu_to_be16(0xffff); | |
2597 | route->path_rec->mtu_selector = IB_SA_EQ; | |
89052d78 MD |
2598 | route->path_rec->sl = iboe_tos_to_sl(ndev, tos); |
2599 | route->path_rec->traffic_class = tos; | |
3c86aa70 EC |
2600 | route->path_rec->mtu = iboe_get_mtu(ndev->mtu); |
2601 | route->path_rec->rate_selector = IB_SA_EQ; | |
2602 | route->path_rec->rate = iboe_get_rate(ndev); | |
2603 | dev_put(ndev); | |
2604 | route->path_rec->packet_life_time_selector = IB_SA_EQ; | |
2605 | route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME; | |
2606 | if (!route->path_rec->mtu) { | |
2607 | ret = -EINVAL; | |
2608 | goto err2; | |
2609 | } | |
2610 | ||
550e5ca7 NM |
2611 | work->old_state = RDMA_CM_ROUTE_QUERY; |
2612 | work->new_state = RDMA_CM_ROUTE_RESOLVED; | |
3c86aa70 EC |
2613 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
2614 | work->event.status = 0; | |
2615 | ||
2616 | queue_work(cma_wq, &work->work); | |
2617 | ||
2618 | return 0; | |
2619 | ||
2620 | err2: | |
2621 | kfree(route->path_rec); | |
2622 | route->path_rec = NULL; | |
2623 | err1: | |
2624 | kfree(work); | |
2625 | return ret; | |
2626 | } | |
2627 | ||
e51060f0 SH |
2628 | int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) |
2629 | { | |
2630 | struct rdma_id_private *id_priv; | |
2631 | int ret; | |
2632 | ||
2633 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 2634 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY)) |
e51060f0 SH |
2635 | return -EINVAL; |
2636 | ||
2637 | atomic_inc(&id_priv->refcount); | |
fe53ba2f | 2638 | if (rdma_cap_ib_sa(id->device, id->port_num)) |
c72f2189 | 2639 | ret = cma_resolve_ib_route(id_priv, timeout_ms); |
5d9fb044 | 2640 | else if (rdma_protocol_roce(id->device, id->port_num)) |
c72f2189 MW |
2641 | ret = cma_resolve_iboe_route(id_priv); |
2642 | else if (rdma_protocol_iwarp(id->device, id->port_num)) | |
07ebafba | 2643 | ret = cma_resolve_iw_route(id_priv, timeout_ms); |
c72f2189 | 2644 | else |
e51060f0 | 2645 | ret = -ENOSYS; |
c72f2189 | 2646 | |
e51060f0 SH |
2647 | if (ret) |
2648 | goto err; | |
2649 | ||
2650 | return 0; | |
2651 | err: | |
550e5ca7 | 2652 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED); |
e51060f0 SH |
2653 | cma_deref_id(id_priv); |
2654 | return ret; | |
2655 | } | |
2656 | EXPORT_SYMBOL(rdma_resolve_route); | |
2657 | ||
6a3e362d SH |
2658 | static void cma_set_loopback(struct sockaddr *addr) |
2659 | { | |
2660 | switch (addr->sa_family) { | |
2661 | case AF_INET: | |
2662 | ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
2663 | break; | |
2664 | case AF_INET6: | |
2665 | ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr, | |
2666 | 0, 0, 0, htonl(1)); | |
2667 | break; | |
2668 | default: | |
2669 | ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr, | |
2670 | 0, 0, 0, htonl(1)); | |
2671 | break; | |
2672 | } | |
2673 | } | |
2674 | ||
e51060f0 SH |
2675 | static int cma_bind_loopback(struct rdma_id_private *id_priv) |
2676 | { | |
b0569e40 | 2677 | struct cma_device *cma_dev, *cur_dev; |
f0ee3404 | 2678 | union ib_gid gid; |
102c5ce0 | 2679 | enum ib_port_state port_state; |
e51060f0 SH |
2680 | u16 pkey; |
2681 | int ret; | |
2682 | u8 p; | |
2683 | ||
b0569e40 | 2684 | cma_dev = NULL; |
e51060f0 | 2685 | mutex_lock(&lock); |
b0569e40 SH |
2686 | list_for_each_entry(cur_dev, &dev_list, list) { |
2687 | if (cma_family(id_priv) == AF_IB && | |
72219cea | 2688 | !rdma_cap_ib_cm(cur_dev->device, 1)) |
b0569e40 SH |
2689 | continue; |
2690 | ||
2691 | if (!cma_dev) | |
2692 | cma_dev = cur_dev; | |
2693 | ||
2694 | for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) { | |
102c5ce0 JW |
2695 | if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) && |
2696 | port_state == IB_PORT_ACTIVE) { | |
b0569e40 SH |
2697 | cma_dev = cur_dev; |
2698 | goto port_found; | |
2699 | } | |
2700 | } | |
2701 | } | |
2702 | ||
2703 | if (!cma_dev) { | |
e82153b5 KK |
2704 | ret = -ENODEV; |
2705 | goto out; | |
2706 | } | |
e51060f0 | 2707 | |
e82153b5 | 2708 | p = 1; |
e51060f0 SH |
2709 | |
2710 | port_found: | |
55ee3ab2 | 2711 | ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL); |
e51060f0 SH |
2712 | if (ret) |
2713 | goto out; | |
2714 | ||
2715 | ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); | |
2716 | if (ret) | |
2717 | goto out; | |
2718 | ||
6f8372b6 | 2719 | id_priv->id.route.addr.dev_addr.dev_type = |
21655afc | 2720 | (rdma_protocol_ib(cma_dev->device, p)) ? |
6f8372b6 SH |
2721 | ARPHRD_INFINIBAND : ARPHRD_ETHER; |
2722 | ||
2723 | rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 SH |
2724 | ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); |
2725 | id_priv->id.port_num = p; | |
2726 | cma_attach_to_dev(id_priv, cma_dev); | |
f4753834 | 2727 | cma_set_loopback(cma_src_addr(id_priv)); |
e51060f0 SH |
2728 | out: |
2729 | mutex_unlock(&lock); | |
2730 | return ret; | |
2731 | } | |
2732 | ||
2733 | static void addr_handler(int status, struct sockaddr *src_addr, | |
2734 | struct rdma_dev_addr *dev_addr, void *context) | |
2735 | { | |
2736 | struct rdma_id_private *id_priv = context; | |
a1b1b61f | 2737 | struct rdma_cm_event event; |
e51060f0 | 2738 | |
a1b1b61f | 2739 | memset(&event, 0, sizeof event); |
de910bd9 | 2740 | mutex_lock(&id_priv->handler_mutex); |
550e5ca7 NM |
2741 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, |
2742 | RDMA_CM_ADDR_RESOLVED)) | |
61a73c70 | 2743 | goto out; |
61a73c70 | 2744 | |
7b85627b | 2745 | memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr)); |
498683c6 | 2746 | if (!status && !id_priv->cma_dev) { |
be9130cc | 2747 | status = cma_acquire_dev(id_priv, NULL); |
498683c6 MS |
2748 | if (status) |
2749 | pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n", | |
2750 | status); | |
2751 | } else { | |
2752 | pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status); | |
2753 | } | |
e51060f0 SH |
2754 | |
2755 | if (status) { | |
550e5ca7 NM |
2756 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
2757 | RDMA_CM_ADDR_BOUND)) | |
e51060f0 | 2758 | goto out; |
a1b1b61f SH |
2759 | event.event = RDMA_CM_EVENT_ADDR_ERROR; |
2760 | event.status = status; | |
7b85627b | 2761 | } else |
a1b1b61f | 2762 | event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
e51060f0 | 2763 | |
a1b1b61f | 2764 | if (id_priv->id.event_handler(&id_priv->id, &event)) { |
550e5ca7 | 2765 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 2766 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
2767 | cma_deref_id(id_priv); |
2768 | rdma_destroy_id(&id_priv->id); | |
2769 | return; | |
2770 | } | |
2771 | out: | |
de910bd9 | 2772 | mutex_unlock(&id_priv->handler_mutex); |
e51060f0 SH |
2773 | cma_deref_id(id_priv); |
2774 | } | |
2775 | ||
2776 | static int cma_resolve_loopback(struct rdma_id_private *id_priv) | |
2777 | { | |
2778 | struct cma_work *work; | |
f0ee3404 | 2779 | union ib_gid gid; |
e51060f0 SH |
2780 | int ret; |
2781 | ||
2782 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
2783 | if (!work) | |
2784 | return -ENOMEM; | |
2785 | ||
2786 | if (!id_priv->cma_dev) { | |
2787 | ret = cma_bind_loopback(id_priv); | |
2788 | if (ret) | |
2789 | goto err; | |
2790 | } | |
2791 | ||
6f8372b6 SH |
2792 | rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
2793 | rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); | |
e51060f0 | 2794 | |
e51060f0 | 2795 | work->id = id_priv; |
c4028958 | 2796 | INIT_WORK(&work->work, cma_work_handler); |
550e5ca7 NM |
2797 | work->old_state = RDMA_CM_ADDR_QUERY; |
2798 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
e51060f0 SH |
2799 | work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
2800 | queue_work(cma_wq, &work->work); | |
2801 | return 0; | |
2802 | err: | |
2803 | kfree(work); | |
2804 | return ret; | |
2805 | } | |
2806 | ||
f17df3b0 SH |
2807 | static int cma_resolve_ib_addr(struct rdma_id_private *id_priv) |
2808 | { | |
2809 | struct cma_work *work; | |
2810 | int ret; | |
2811 | ||
2812 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
2813 | if (!work) | |
2814 | return -ENOMEM; | |
2815 | ||
2816 | if (!id_priv->cma_dev) { | |
2817 | ret = cma_resolve_ib_dev(id_priv); | |
2818 | if (ret) | |
2819 | goto err; | |
2820 | } | |
2821 | ||
2822 | rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *) | |
2823 | &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr)); | |
2824 | ||
2825 | work->id = id_priv; | |
2826 | INIT_WORK(&work->work, cma_work_handler); | |
2827 | work->old_state = RDMA_CM_ADDR_QUERY; | |
2828 | work->new_state = RDMA_CM_ADDR_RESOLVED; | |
2829 | work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; | |
2830 | queue_work(cma_wq, &work->work); | |
2831 | return 0; | |
2832 | err: | |
2833 | kfree(work); | |
2834 | return ret; | |
2835 | } | |
2836 | ||
e51060f0 SH |
2837 | static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, |
2838 | struct sockaddr *dst_addr) | |
2839 | { | |
d14714df SH |
2840 | if (!src_addr || !src_addr->sa_family) { |
2841 | src_addr = (struct sockaddr *) &id->route.addr.src_addr; | |
f17df3b0 | 2842 | src_addr->sa_family = dst_addr->sa_family; |
b4cfe397 JM |
2843 | if (IS_ENABLED(CONFIG_IPV6) && |
2844 | dst_addr->sa_family == AF_INET6) { | |
6c26a771 SB |
2845 | struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr; |
2846 | struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr; | |
2847 | src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id; | |
2848 | if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
2849 | id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id; | |
f17df3b0 SH |
2850 | } else if (dst_addr->sa_family == AF_IB) { |
2851 | ((struct sockaddr_ib *) src_addr)->sib_pkey = | |
2852 | ((struct sockaddr_ib *) dst_addr)->sib_pkey; | |
d14714df SH |
2853 | } |
2854 | } | |
2855 | return rdma_bind_addr(id, src_addr); | |
e51060f0 SH |
2856 | } |
2857 | ||
2858 | int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, | |
2859 | struct sockaddr *dst_addr, int timeout_ms) | |
2860 | { | |
2861 | struct rdma_id_private *id_priv; | |
2862 | int ret; | |
2863 | ||
2864 | id_priv = container_of(id, struct rdma_id_private, id); | |
19b752a1 | 2865 | memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr)); |
550e5ca7 | 2866 | if (id_priv->state == RDMA_CM_IDLE) { |
e51060f0 | 2867 | ret = cma_bind_addr(id, src_addr, dst_addr); |
19b752a1 MS |
2868 | if (ret) { |
2869 | memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr)); | |
e51060f0 | 2870 | return ret; |
19b752a1 | 2871 | } |
e51060f0 SH |
2872 | } |
2873 | ||
19b752a1 MS |
2874 | if (cma_family(id_priv) != dst_addr->sa_family) { |
2875 | memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr)); | |
4ae7152e | 2876 | return -EINVAL; |
19b752a1 | 2877 | } |
4ae7152e | 2878 | |
19b752a1 MS |
2879 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) { |
2880 | memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr)); | |
e51060f0 | 2881 | return -EINVAL; |
19b752a1 | 2882 | } |
e51060f0 SH |
2883 | |
2884 | atomic_inc(&id_priv->refcount); | |
f17df3b0 | 2885 | if (cma_any_addr(dst_addr)) { |
e51060f0 | 2886 | ret = cma_resolve_loopback(id_priv); |
f17df3b0 SH |
2887 | } else { |
2888 | if (dst_addr->sa_family == AF_IB) { | |
2889 | ret = cma_resolve_ib_addr(id_priv); | |
2890 | } else { | |
2891 | ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv), | |
2892 | dst_addr, &id->route.addr.dev_addr, | |
2893 | timeout_ms, addr_handler, id_priv); | |
2894 | } | |
2895 | } | |
e51060f0 SH |
2896 | if (ret) |
2897 | goto err; | |
2898 | ||
2899 | return 0; | |
2900 | err: | |
550e5ca7 | 2901 | cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND); |
e51060f0 SH |
2902 | cma_deref_id(id_priv); |
2903 | return ret; | |
2904 | } | |
2905 | EXPORT_SYMBOL(rdma_resolve_addr); | |
2906 | ||
a9bb7912 HS |
2907 | int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse) |
2908 | { | |
2909 | struct rdma_id_private *id_priv; | |
2910 | unsigned long flags; | |
2911 | int ret; | |
2912 | ||
2913 | id_priv = container_of(id, struct rdma_id_private, id); | |
2914 | spin_lock_irqsave(&id_priv->lock, flags); | |
c8dea2f9 | 2915 | if (reuse || id_priv->state == RDMA_CM_IDLE) { |
a9bb7912 HS |
2916 | id_priv->reuseaddr = reuse; |
2917 | ret = 0; | |
2918 | } else { | |
2919 | ret = -EINVAL; | |
2920 | } | |
2921 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
2922 | return ret; | |
2923 | } | |
2924 | EXPORT_SYMBOL(rdma_set_reuseaddr); | |
2925 | ||
68602120 SH |
2926 | int rdma_set_afonly(struct rdma_cm_id *id, int afonly) |
2927 | { | |
2928 | struct rdma_id_private *id_priv; | |
2929 | unsigned long flags; | |
2930 | int ret; | |
2931 | ||
2932 | id_priv = container_of(id, struct rdma_id_private, id); | |
2933 | spin_lock_irqsave(&id_priv->lock, flags); | |
2934 | if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) { | |
2935 | id_priv->options |= (1 << CMA_OPTION_AFONLY); | |
2936 | id_priv->afonly = afonly; | |
2937 | ret = 0; | |
2938 | } else { | |
2939 | ret = -EINVAL; | |
2940 | } | |
2941 | spin_unlock_irqrestore(&id_priv->lock, flags); | |
2942 | return ret; | |
2943 | } | |
2944 | EXPORT_SYMBOL(rdma_set_afonly); | |
2945 | ||
e51060f0 SH |
2946 | static void cma_bind_port(struct rdma_bind_list *bind_list, |
2947 | struct rdma_id_private *id_priv) | |
2948 | { | |
58afdcb7 SH |
2949 | struct sockaddr *addr; |
2950 | struct sockaddr_ib *sib; | |
2951 | u64 sid, mask; | |
2952 | __be16 port; | |
e51060f0 | 2953 | |
f4753834 | 2954 | addr = cma_src_addr(id_priv); |
58afdcb7 SH |
2955 | port = htons(bind_list->port); |
2956 | ||
2957 | switch (addr->sa_family) { | |
2958 | case AF_INET: | |
2959 | ((struct sockaddr_in *) addr)->sin_port = port; | |
2960 | break; | |
2961 | case AF_INET6: | |
2962 | ((struct sockaddr_in6 *) addr)->sin6_port = port; | |
2963 | break; | |
2964 | case AF_IB: | |
2965 | sib = (struct sockaddr_ib *) addr; | |
2966 | sid = be64_to_cpu(sib->sib_sid); | |
2967 | mask = be64_to_cpu(sib->sib_sid_mask); | |
2968 | sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port)); | |
2969 | sib->sib_sid_mask = cpu_to_be64(~0ULL); | |
2970 | break; | |
2971 | } | |
e51060f0 SH |
2972 | id_priv->bind_list = bind_list; |
2973 | hlist_add_head(&id_priv->node, &bind_list->owners); | |
2974 | } | |
2975 | ||
aac978e1 HE |
2976 | static int cma_alloc_port(enum rdma_port_space ps, |
2977 | struct rdma_id_private *id_priv, unsigned short snum) | |
e51060f0 SH |
2978 | { |
2979 | struct rdma_bind_list *bind_list; | |
3b069c5d | 2980 | int ret; |
e51060f0 | 2981 | |
cb164b8c | 2982 | bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); |
e51060f0 SH |
2983 | if (!bind_list) |
2984 | return -ENOMEM; | |
2985 | ||
fa20105e GS |
2986 | ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list, |
2987 | snum); | |
3b069c5d TH |
2988 | if (ret < 0) |
2989 | goto err; | |
aedec080 SH |
2990 | |
2991 | bind_list->ps = ps; | |
3b069c5d | 2992 | bind_list->port = (unsigned short)ret; |
aedec080 SH |
2993 | cma_bind_port(bind_list, id_priv); |
2994 | return 0; | |
3b069c5d | 2995 | err: |
aedec080 | 2996 | kfree(bind_list); |
3b069c5d | 2997 | return ret == -ENOSPC ? -EADDRNOTAVAIL : ret; |
aedec080 | 2998 | } |
e51060f0 | 2999 | |
19b752a1 MS |
3000 | static int cma_port_is_unique(struct rdma_bind_list *bind_list, |
3001 | struct rdma_id_private *id_priv) | |
3002 | { | |
3003 | struct rdma_id_private *cur_id; | |
3004 | struct sockaddr *daddr = cma_dst_addr(id_priv); | |
3005 | struct sockaddr *saddr = cma_src_addr(id_priv); | |
3006 | __be16 dport = cma_port(daddr); | |
3007 | ||
3008 | hlist_for_each_entry(cur_id, &bind_list->owners, node) { | |
3009 | struct sockaddr *cur_daddr = cma_dst_addr(cur_id); | |
3010 | struct sockaddr *cur_saddr = cma_src_addr(cur_id); | |
3011 | __be16 cur_dport = cma_port(cur_daddr); | |
3012 | ||
3013 | if (id_priv == cur_id) | |
3014 | continue; | |
3015 | ||
3016 | /* different dest port -> unique */ | |
3017 | if (!cma_any_port(cur_daddr) && | |
3018 | (dport != cur_dport)) | |
3019 | continue; | |
3020 | ||
3021 | /* different src address -> unique */ | |
3022 | if (!cma_any_addr(saddr) && | |
3023 | !cma_any_addr(cur_saddr) && | |
3024 | cma_addr_cmp(saddr, cur_saddr)) | |
3025 | continue; | |
3026 | ||
3027 | /* different dst address -> unique */ | |
3028 | if (!cma_any_addr(cur_daddr) && | |
3029 | cma_addr_cmp(daddr, cur_daddr)) | |
3030 | continue; | |
3031 | ||
3032 | return -EADDRNOTAVAIL; | |
3033 | } | |
3034 | return 0; | |
3035 | } | |
3036 | ||
aac978e1 HE |
3037 | static int cma_alloc_any_port(enum rdma_port_space ps, |
3038 | struct rdma_id_private *id_priv) | |
aedec080 | 3039 | { |
5d7220e8 TH |
3040 | static unsigned int last_used_port; |
3041 | int low, high, remaining; | |
3042 | unsigned int rover; | |
fa20105e | 3043 | struct net *net = id_priv->id.route.addr.dev_addr.net; |
e51060f0 | 3044 | |
fa20105e | 3045 | inet_get_local_port_range(net, &low, &high); |
5d7220e8 | 3046 | remaining = (high - low) + 1; |
63862b5b | 3047 | rover = prandom_u32() % remaining + low; |
5d7220e8 | 3048 | retry: |
19b752a1 MS |
3049 | if (last_used_port != rover) { |
3050 | struct rdma_bind_list *bind_list; | |
3051 | int ret; | |
3052 | ||
3053 | bind_list = cma_ps_find(net, ps, (unsigned short)rover); | |
3054 | ||
3055 | if (!bind_list) { | |
3056 | ret = cma_alloc_port(ps, id_priv, rover); | |
3057 | } else { | |
3058 | ret = cma_port_is_unique(bind_list, id_priv); | |
3059 | if (!ret) | |
3060 | cma_bind_port(bind_list, id_priv); | |
3061 | } | |
5d7220e8 TH |
3062 | /* |
3063 | * Remember previously used port number in order to avoid | |
3064 | * re-using same port immediately after it is closed. | |
3065 | */ | |
3066 | if (!ret) | |
3067 | last_used_port = rover; | |
3068 | if (ret != -EADDRNOTAVAIL) | |
3069 | return ret; | |
e51060f0 | 3070 | } |
5d7220e8 TH |
3071 | if (--remaining) { |
3072 | rover++; | |
3073 | if ((rover < low) || (rover > high)) | |
3074 | rover = low; | |
3075 | goto retry; | |
3076 | } | |
3077 | return -EADDRNOTAVAIL; | |
e51060f0 SH |
3078 | } |
3079 | ||
a9bb7912 HS |
3080 | /* |
3081 | * Check that the requested port is available. This is called when trying to | |
3082 | * bind to a specific port, or when trying to listen on a bound port. In | |
3083 | * the latter case, the provided id_priv may already be on the bind_list, but | |
3084 | * we still need to check that it's okay to start listening. | |
3085 | */ | |
3086 | static int cma_check_port(struct rdma_bind_list *bind_list, | |
3087 | struct rdma_id_private *id_priv, uint8_t reuseaddr) | |
e51060f0 SH |
3088 | { |
3089 | struct rdma_id_private *cur_id; | |
43b752da | 3090 | struct sockaddr *addr, *cur_addr; |
e51060f0 | 3091 | |
f4753834 | 3092 | addr = cma_src_addr(id_priv); |
b67bfe0d | 3093 | hlist_for_each_entry(cur_id, &bind_list->owners, node) { |
a9bb7912 HS |
3094 | if (id_priv == cur_id) |
3095 | continue; | |
3cd96564 | 3096 | |
5b0ec991 SH |
3097 | if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr && |
3098 | cur_id->reuseaddr) | |
3099 | continue; | |
e51060f0 | 3100 | |
f4753834 | 3101 | cur_addr = cma_src_addr(cur_id); |
5b0ec991 SH |
3102 | if (id_priv->afonly && cur_id->afonly && |
3103 | (addr->sa_family != cur_addr->sa_family)) | |
3104 | continue; | |
3105 | ||
3106 | if (cma_any_addr(addr) || cma_any_addr(cur_addr)) | |
3107 | return -EADDRNOTAVAIL; | |
3108 | ||
3109 | if (!cma_addr_cmp(addr, cur_addr)) | |
3110 | return -EADDRINUSE; | |
a9bb7912 | 3111 | } |
e51060f0 SH |
3112 | return 0; |
3113 | } | |
3114 | ||
aac978e1 HE |
3115 | static int cma_use_port(enum rdma_port_space ps, |
3116 | struct rdma_id_private *id_priv) | |
a9bb7912 HS |
3117 | { |
3118 | struct rdma_bind_list *bind_list; | |
3119 | unsigned short snum; | |
3120 | int ret; | |
3121 | ||
f4753834 | 3122 | snum = ntohs(cma_port(cma_src_addr(id_priv))); |
a9bb7912 HS |
3123 | if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) |
3124 | return -EACCES; | |
3125 | ||
fa20105e | 3126 | bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum); |
a9bb7912 HS |
3127 | if (!bind_list) { |
3128 | ret = cma_alloc_port(ps, id_priv, snum); | |
3129 | } else { | |
3130 | ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr); | |
3131 | if (!ret) | |
3132 | cma_bind_port(bind_list, id_priv); | |
3133 | } | |
3134 | return ret; | |
3135 | } | |
3136 | ||
3137 | static int cma_bind_listen(struct rdma_id_private *id_priv) | |
3138 | { | |
3139 | struct rdma_bind_list *bind_list = id_priv->bind_list; | |
3140 | int ret = 0; | |
3141 | ||
3142 | mutex_lock(&lock); | |
3143 | if (bind_list->owners.first->next) | |
3144 | ret = cma_check_port(bind_list, id_priv, 0); | |
3145 | mutex_unlock(&lock); | |
3146 | return ret; | |
3147 | } | |
3148 | ||
aac978e1 HE |
3149 | static enum rdma_port_space cma_select_inet_ps( |
3150 | struct rdma_id_private *id_priv) | |
e51060f0 | 3151 | { |
e51060f0 | 3152 | switch (id_priv->id.ps) { |
e51060f0 | 3153 | case RDMA_PS_TCP: |
628e5f6d | 3154 | case RDMA_PS_UDP: |
c8f6a362 | 3155 | case RDMA_PS_IPOIB: |
2d2e9415 | 3156 | case RDMA_PS_IB: |
aac978e1 | 3157 | return id_priv->id.ps; |
e51060f0 | 3158 | default: |
aac978e1 HE |
3159 | |
3160 | return 0; | |
58afdcb7 SH |
3161 | } |
3162 | } | |
3163 | ||
aac978e1 | 3164 | static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv) |
58afdcb7 | 3165 | { |
aac978e1 | 3166 | enum rdma_port_space ps = 0; |
58afdcb7 SH |
3167 | struct sockaddr_ib *sib; |
3168 | u64 sid_ps, mask, sid; | |
3169 | ||
f4753834 | 3170 | sib = (struct sockaddr_ib *) cma_src_addr(id_priv); |
58afdcb7 SH |
3171 | mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK; |
3172 | sid = be64_to_cpu(sib->sib_sid) & mask; | |
3173 | ||
3174 | if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) { | |
3175 | sid_ps = RDMA_IB_IP_PS_IB; | |
aac978e1 | 3176 | ps = RDMA_PS_IB; |
58afdcb7 SH |
3177 | } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) && |
3178 | (sid == (RDMA_IB_IP_PS_TCP & mask))) { | |
3179 | sid_ps = RDMA_IB_IP_PS_TCP; | |
aac978e1 | 3180 | ps = RDMA_PS_TCP; |
58afdcb7 SH |
3181 | } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) && |
3182 | (sid == (RDMA_IB_IP_PS_UDP & mask))) { | |
3183 | sid_ps = RDMA_IB_IP_PS_UDP; | |
aac978e1 | 3184 | ps = RDMA_PS_UDP; |
e51060f0 SH |
3185 | } |
3186 | ||
58afdcb7 SH |
3187 | if (ps) { |
3188 | sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib))); | |
3189 | sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK | | |
3190 | be64_to_cpu(sib->sib_sid_mask)); | |
3191 | } | |
3192 | return ps; | |
3193 | } | |
3194 | ||
3195 | static int cma_get_port(struct rdma_id_private *id_priv) | |
3196 | { | |
aac978e1 | 3197 | enum rdma_port_space ps; |
58afdcb7 SH |
3198 | int ret; |
3199 | ||
f4753834 | 3200 | if (cma_family(id_priv) != AF_IB) |
58afdcb7 SH |
3201 | ps = cma_select_inet_ps(id_priv); |
3202 | else | |
3203 | ps = cma_select_ib_ps(id_priv); | |
3204 | if (!ps) | |
3205 | return -EPROTONOSUPPORT; | |
3206 | ||
e51060f0 | 3207 | mutex_lock(&lock); |
f4753834 | 3208 | if (cma_any_port(cma_src_addr(id_priv))) |
aedec080 | 3209 | ret = cma_alloc_any_port(ps, id_priv); |
e51060f0 SH |
3210 | else |
3211 | ret = cma_use_port(ps, id_priv); | |
3212 | mutex_unlock(&lock); | |
3213 | ||
3214 | return ret; | |
3215 | } | |
3216 | ||
d14714df SH |
3217 | static int cma_check_linklocal(struct rdma_dev_addr *dev_addr, |
3218 | struct sockaddr *addr) | |
3219 | { | |
d90f9b35 | 3220 | #if IS_ENABLED(CONFIG_IPV6) |
d14714df SH |
3221 | struct sockaddr_in6 *sin6; |
3222 | ||
3223 | if (addr->sa_family != AF_INET6) | |
3224 | return 0; | |
3225 | ||
3226 | sin6 = (struct sockaddr_in6 *) addr; | |
5462eddd SK |
3227 | |
3228 | if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)) | |
3229 | return 0; | |
3230 | ||
3231 | if (!sin6->sin6_scope_id) | |
d14714df SH |
3232 | return -EINVAL; |
3233 | ||
3234 | dev_addr->bound_dev_if = sin6->sin6_scope_id; | |
3235 | #endif | |
3236 | return 0; | |
3237 | } | |
3238 | ||
a9bb7912 HS |
3239 | int rdma_listen(struct rdma_cm_id *id, int backlog) |
3240 | { | |
3241 | struct rdma_id_private *id_priv; | |
3242 | int ret; | |
3243 | ||
3244 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 3245 | if (id_priv->state == RDMA_CM_IDLE) { |
f4753834 SH |
3246 | id->route.addr.src_addr.ss_family = AF_INET; |
3247 | ret = rdma_bind_addr(id, cma_src_addr(id_priv)); | |
a9bb7912 HS |
3248 | if (ret) |
3249 | return ret; | |
3250 | } | |
3251 | ||
550e5ca7 | 3252 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) |
a9bb7912 HS |
3253 | return -EINVAL; |
3254 | ||
3255 | if (id_priv->reuseaddr) { | |
3256 | ret = cma_bind_listen(id_priv); | |
3257 | if (ret) | |
3258 | goto err; | |
3259 | } | |
3260 | ||
3261 | id_priv->backlog = backlog; | |
3262 | if (id->device) { | |
72219cea | 3263 | if (rdma_cap_ib_cm(id->device, 1)) { |
a9bb7912 HS |
3264 | ret = cma_ib_listen(id_priv); |
3265 | if (ret) | |
3266 | goto err; | |
04215330 | 3267 | } else if (rdma_cap_iw_cm(id->device, 1)) { |
a9bb7912 HS |
3268 | ret = cma_iw_listen(id_priv, backlog); |
3269 | if (ret) | |
3270 | goto err; | |
21655afc | 3271 | } else { |
a9bb7912 HS |
3272 | ret = -ENOSYS; |
3273 | goto err; | |
3274 | } | |
3275 | } else | |
3276 | cma_listen_on_all(id_priv); | |
3277 | ||
3278 | return 0; | |
3279 | err: | |
3280 | id_priv->backlog = 0; | |
550e5ca7 | 3281 | cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND); |
a9bb7912 HS |
3282 | return ret; |
3283 | } | |
3284 | EXPORT_SYMBOL(rdma_listen); | |
3285 | ||
e51060f0 SH |
3286 | int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) |
3287 | { | |
3288 | struct rdma_id_private *id_priv; | |
3289 | int ret; | |
6df6b4a9 | 3290 | struct sockaddr *daddr; |
e51060f0 | 3291 | |
680f920a SH |
3292 | if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 && |
3293 | addr->sa_family != AF_IB) | |
e51060f0 SH |
3294 | return -EAFNOSUPPORT; |
3295 | ||
3296 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 3297 | if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND)) |
e51060f0 SH |
3298 | return -EINVAL; |
3299 | ||
d14714df SH |
3300 | ret = cma_check_linklocal(&id->route.addr.dev_addr, addr); |
3301 | if (ret) | |
3302 | goto err1; | |
3303 | ||
7b85627b | 3304 | memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr)); |
8523c048 | 3305 | if (!cma_any_addr(addr)) { |
680f920a | 3306 | ret = cma_translate_addr(addr, &id->route.addr.dev_addr); |
e51060f0 | 3307 | if (ret) |
255d0c14 KK |
3308 | goto err1; |
3309 | ||
be9130cc | 3310 | ret = cma_acquire_dev(id_priv, NULL); |
255d0c14 KK |
3311 | if (ret) |
3312 | goto err1; | |
e51060f0 SH |
3313 | } |
3314 | ||
68602120 SH |
3315 | if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) { |
3316 | if (addr->sa_family == AF_INET) | |
3317 | id_priv->afonly = 1; | |
5b0ec991 | 3318 | #if IS_ENABLED(CONFIG_IPV6) |
fa20105e GS |
3319 | else if (addr->sa_family == AF_INET6) { |
3320 | struct net *net = id_priv->id.route.addr.dev_addr.net; | |
3321 | ||
3322 | id_priv->afonly = net->ipv6.sysctl.bindv6only; | |
3323 | } | |
5b0ec991 | 3324 | #endif |
68602120 | 3325 | } |
e51060f0 SH |
3326 | ret = cma_get_port(id_priv); |
3327 | if (ret) | |
255d0c14 | 3328 | goto err2; |
e51060f0 | 3329 | |
6df6b4a9 MS |
3330 | daddr = cma_dst_addr(id_priv); |
3331 | daddr->sa_family = addr->sa_family; | |
3332 | ||
e51060f0 | 3333 | return 0; |
255d0c14 | 3334 | err2: |
a396d43a SH |
3335 | if (id_priv->cma_dev) |
3336 | cma_release_dev(id_priv); | |
255d0c14 | 3337 | err1: |
550e5ca7 | 3338 | cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE); |
e51060f0 SH |
3339 | return ret; |
3340 | } | |
3341 | EXPORT_SYMBOL(rdma_bind_addr); | |
3342 | ||
f4753834 | 3343 | static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv) |
e51060f0 | 3344 | { |
e51060f0 | 3345 | struct cma_hdr *cma_hdr; |
e51060f0 | 3346 | |
01602f11 SH |
3347 | cma_hdr = hdr; |
3348 | cma_hdr->cma_version = CMA_VERSION; | |
f4753834 | 3349 | if (cma_family(id_priv) == AF_INET) { |
1f5175ad AS |
3350 | struct sockaddr_in *src4, *dst4; |
3351 | ||
f4753834 SH |
3352 | src4 = (struct sockaddr_in *) cma_src_addr(id_priv); |
3353 | dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv); | |
1f5175ad | 3354 | |
01602f11 SH |
3355 | cma_set_ip_ver(cma_hdr, 4); |
3356 | cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; | |
3357 | cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; | |
3358 | cma_hdr->port = src4->sin_port; | |
e8160e15 | 3359 | } else if (cma_family(id_priv) == AF_INET6) { |
1f5175ad AS |
3360 | struct sockaddr_in6 *src6, *dst6; |
3361 | ||
f4753834 SH |
3362 | src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv); |
3363 | dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv); | |
1f5175ad | 3364 | |
01602f11 SH |
3365 | cma_set_ip_ver(cma_hdr, 6); |
3366 | cma_hdr->src_addr.ip6 = src6->sin6_addr; | |
3367 | cma_hdr->dst_addr.ip6 = dst6->sin6_addr; | |
3368 | cma_hdr->port = src6->sin6_port; | |
e51060f0 SH |
3369 | } |
3370 | return 0; | |
3371 | } | |
3372 | ||
628e5f6d SH |
3373 | static int cma_sidr_rep_handler(struct ib_cm_id *cm_id, |
3374 | struct ib_cm_event *ib_event) | |
3375 | { | |
3376 | struct rdma_id_private *id_priv = cm_id->context; | |
3377 | struct rdma_cm_event event; | |
3378 | struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd; | |
3379 | int ret = 0; | |
3380 | ||
37e07cda BVA |
3381 | mutex_lock(&id_priv->handler_mutex); |
3382 | if (id_priv->state != RDMA_CM_CONNECT) | |
3383 | goto out; | |
628e5f6d | 3384 | |
8aa08602 | 3385 | memset(&event, 0, sizeof event); |
628e5f6d SH |
3386 | switch (ib_event->event) { |
3387 | case IB_CM_SIDR_REQ_ERROR: | |
3388 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
3389 | event.status = -ETIMEDOUT; | |
3390 | break; | |
3391 | case IB_CM_SIDR_REP_RECEIVED: | |
3392 | event.param.ud.private_data = ib_event->private_data; | |
3393 | event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; | |
3394 | if (rep->status != IB_SIDR_SUCCESS) { | |
3395 | event.event = RDMA_CM_EVENT_UNREACHABLE; | |
3396 | event.status = ib_event->param.sidr_rep_rcvd.status; | |
498683c6 MS |
3397 | pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n", |
3398 | event.status); | |
628e5f6d SH |
3399 | break; |
3400 | } | |
5c438135 | 3401 | ret = cma_set_qkey(id_priv, rep->qkey); |
d2ca39f2 | 3402 | if (ret) { |
498683c6 | 3403 | pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret); |
d2ca39f2 | 3404 | event.event = RDMA_CM_EVENT_ADDR_ERROR; |
5c438135 | 3405 | event.status = ret; |
628e5f6d SH |
3406 | break; |
3407 | } | |
3408 | ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num, | |
3409 | id_priv->id.route.path_rec, | |
3410 | &event.param.ud.ah_attr); | |
3411 | event.param.ud.qp_num = rep->qpn; | |
3412 | event.param.ud.qkey = rep->qkey; | |
3413 | event.event = RDMA_CM_EVENT_ESTABLISHED; | |
3414 | event.status = 0; | |
3415 | break; | |
3416 | default: | |
aba25a3e | 3417 | pr_err("RDMA CMA: unexpected IB CM event: %d\n", |
628e5f6d SH |
3418 | ib_event->event); |
3419 | goto out; | |
3420 | } | |
3421 | ||
3422 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
3423 | if (ret) { | |
3424 | /* Destroy the CM ID by returning a non-zero value. */ | |
3425 | id_priv->cm_id.ib = NULL; | |
550e5ca7 | 3426 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 3427 | mutex_unlock(&id_priv->handler_mutex); |
628e5f6d SH |
3428 | rdma_destroy_id(&id_priv->id); |
3429 | return ret; | |
3430 | } | |
3431 | out: | |
de910bd9 | 3432 | mutex_unlock(&id_priv->handler_mutex); |
628e5f6d SH |
3433 | return ret; |
3434 | } | |
3435 | ||
3436 | static int cma_resolve_ib_udp(struct rdma_id_private *id_priv, | |
3437 | struct rdma_conn_param *conn_param) | |
3438 | { | |
3439 | struct ib_cm_sidr_req_param req; | |
0c9361fc | 3440 | struct ib_cm_id *id; |
e511d1ae | 3441 | void *private_data; |
e8160e15 | 3442 | int offset, ret; |
628e5f6d | 3443 | |
e511d1ae | 3444 | memset(&req, 0, sizeof req); |
e8160e15 SH |
3445 | offset = cma_user_data_offset(id_priv); |
3446 | req.private_data_len = offset + conn_param->private_data_len; | |
04ded167 SH |
3447 | if (req.private_data_len < conn_param->private_data_len) |
3448 | return -EINVAL; | |
3449 | ||
e8160e15 | 3450 | if (req.private_data_len) { |
e511d1ae SH |
3451 | private_data = kzalloc(req.private_data_len, GFP_ATOMIC); |
3452 | if (!private_data) | |
e8160e15 SH |
3453 | return -ENOMEM; |
3454 | } else { | |
e511d1ae | 3455 | private_data = NULL; |
e8160e15 | 3456 | } |
628e5f6d SH |
3457 | |
3458 | if (conn_param->private_data && conn_param->private_data_len) | |
e511d1ae SH |
3459 | memcpy(private_data + offset, conn_param->private_data, |
3460 | conn_param->private_data_len); | |
628e5f6d | 3461 | |
e511d1ae SH |
3462 | if (private_data) { |
3463 | ret = cma_format_hdr(private_data, id_priv); | |
e8160e15 SH |
3464 | if (ret) |
3465 | goto out; | |
e511d1ae | 3466 | req.private_data = private_data; |
e8160e15 | 3467 | } |
628e5f6d | 3468 | |
0c9361fc JM |
3469 | id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler, |
3470 | id_priv); | |
3471 | if (IS_ERR(id)) { | |
3472 | ret = PTR_ERR(id); | |
628e5f6d SH |
3473 | goto out; |
3474 | } | |
0c9361fc | 3475 | id_priv->cm_id.ib = id; |
628e5f6d | 3476 | |
f4753834 | 3477 | req.path = id_priv->id.route.path_rec; |
cf53936f | 3478 | req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); |
628e5f6d SH |
3479 | req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8); |
3480 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
3481 | ||
3482 | ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req); | |
3483 | if (ret) { | |
3484 | ib_destroy_cm_id(id_priv->cm_id.ib); | |
3485 | id_priv->cm_id.ib = NULL; | |
3486 | } | |
3487 | out: | |
e511d1ae | 3488 | kfree(private_data); |
628e5f6d SH |
3489 | return ret; |
3490 | } | |
3491 | ||
e51060f0 SH |
3492 | static int cma_connect_ib(struct rdma_id_private *id_priv, |
3493 | struct rdma_conn_param *conn_param) | |
3494 | { | |
3495 | struct ib_cm_req_param req; | |
3496 | struct rdma_route *route; | |
3497 | void *private_data; | |
0c9361fc | 3498 | struct ib_cm_id *id; |
e51060f0 SH |
3499 | int offset, ret; |
3500 | ||
3501 | memset(&req, 0, sizeof req); | |
e8160e15 | 3502 | offset = cma_user_data_offset(id_priv); |
e51060f0 | 3503 | req.private_data_len = offset + conn_param->private_data_len; |
04ded167 SH |
3504 | if (req.private_data_len < conn_param->private_data_len) |
3505 | return -EINVAL; | |
3506 | ||
e8160e15 SH |
3507 | if (req.private_data_len) { |
3508 | private_data = kzalloc(req.private_data_len, GFP_ATOMIC); | |
3509 | if (!private_data) | |
3510 | return -ENOMEM; | |
3511 | } else { | |
3512 | private_data = NULL; | |
3513 | } | |
e51060f0 SH |
3514 | |
3515 | if (conn_param->private_data && conn_param->private_data_len) | |
3516 | memcpy(private_data + offset, conn_param->private_data, | |
3517 | conn_param->private_data_len); | |
3518 | ||
0c9361fc JM |
3519 | id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv); |
3520 | if (IS_ERR(id)) { | |
3521 | ret = PTR_ERR(id); | |
e51060f0 SH |
3522 | goto out; |
3523 | } | |
0c9361fc | 3524 | id_priv->cm_id.ib = id; |
e51060f0 SH |
3525 | |
3526 | route = &id_priv->id.route; | |
e8160e15 SH |
3527 | if (private_data) { |
3528 | ret = cma_format_hdr(private_data, id_priv); | |
3529 | if (ret) | |
3530 | goto out; | |
3531 | req.private_data = private_data; | |
3532 | } | |
e51060f0 SH |
3533 | |
3534 | req.primary_path = &route->path_rec[0]; | |
3535 | if (route->num_paths == 2) | |
3536 | req.alternate_path = &route->path_rec[1]; | |
3537 | ||
cf53936f | 3538 | req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); |
e51060f0 | 3539 | req.qp_num = id_priv->qp_num; |
18c441a6 | 3540 | req.qp_type = id_priv->id.qp_type; |
e51060f0 SH |
3541 | req.starting_psn = id_priv->seq_num; |
3542 | req.responder_resources = conn_param->responder_resources; | |
3543 | req.initiator_depth = conn_param->initiator_depth; | |
3544 | req.flow_control = conn_param->flow_control; | |
4ede178a SH |
3545 | req.retry_count = min_t(u8, 7, conn_param->retry_count); |
3546 | req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count); | |
e51060f0 SH |
3547 | req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; |
3548 | req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; | |
3549 | req.max_cm_retries = CMA_MAX_CM_RETRIES; | |
3550 | req.srq = id_priv->srq ? 1 : 0; | |
3551 | ||
3552 | ret = ib_send_cm_req(id_priv->cm_id.ib, &req); | |
3553 | out: | |
0c9361fc JM |
3554 | if (ret && !IS_ERR(id)) { |
3555 | ib_destroy_cm_id(id); | |
675a027c KK |
3556 | id_priv->cm_id.ib = NULL; |
3557 | } | |
3558 | ||
e51060f0 SH |
3559 | kfree(private_data); |
3560 | return ret; | |
3561 | } | |
3562 | ||
07ebafba TT |
3563 | static int cma_connect_iw(struct rdma_id_private *id_priv, |
3564 | struct rdma_conn_param *conn_param) | |
3565 | { | |
3566 | struct iw_cm_id *cm_id; | |
07ebafba TT |
3567 | int ret; |
3568 | struct iw_cm_conn_param iw_param; | |
3569 | ||
3570 | cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); | |
0c9361fc JM |
3571 | if (IS_ERR(cm_id)) |
3572 | return PTR_ERR(cm_id); | |
07ebafba | 3573 | |
68cdba06 | 3574 | cm_id->tos = id_priv->tos; |
07ebafba TT |
3575 | id_priv->cm_id.iw = cm_id; |
3576 | ||
24d44a39 SW |
3577 | memcpy(&cm_id->local_addr, cma_src_addr(id_priv), |
3578 | rdma_addr_size(cma_src_addr(id_priv))); | |
3579 | memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv), | |
3580 | rdma_addr_size(cma_dst_addr(id_priv))); | |
07ebafba | 3581 | |
5851bb89 | 3582 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
675a027c KK |
3583 | if (ret) |
3584 | goto out; | |
07ebafba | 3585 | |
f45ee80e HS |
3586 | if (conn_param) { |
3587 | iw_param.ord = conn_param->initiator_depth; | |
3588 | iw_param.ird = conn_param->responder_resources; | |
3589 | iw_param.private_data = conn_param->private_data; | |
3590 | iw_param.private_data_len = conn_param->private_data_len; | |
3591 | iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num; | |
3592 | } else { | |
3593 | memset(&iw_param, 0, sizeof iw_param); | |
07ebafba | 3594 | iw_param.qpn = id_priv->qp_num; |
f45ee80e | 3595 | } |
07ebafba TT |
3596 | ret = iw_cm_connect(cm_id, &iw_param); |
3597 | out: | |
0c9361fc | 3598 | if (ret) { |
675a027c KK |
3599 | iw_destroy_cm_id(cm_id); |
3600 | id_priv->cm_id.iw = NULL; | |
3601 | } | |
07ebafba TT |
3602 | return ret; |
3603 | } | |
3604 | ||
e51060f0 SH |
3605 | int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
3606 | { | |
3607 | struct rdma_id_private *id_priv; | |
3608 | int ret; | |
3609 | ||
3610 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 | 3611 | if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) |
e51060f0 SH |
3612 | return -EINVAL; |
3613 | ||
3614 | if (!id->qp) { | |
3615 | id_priv->qp_num = conn_param->qp_num; | |
e51060f0 SH |
3616 | id_priv->srq = conn_param->srq; |
3617 | } | |
3618 | ||
72219cea | 3619 | if (rdma_cap_ib_cm(id->device, id->port_num)) { |
b26f9b99 | 3620 | if (id->qp_type == IB_QPT_UD) |
628e5f6d SH |
3621 | ret = cma_resolve_ib_udp(id_priv, conn_param); |
3622 | else | |
3623 | ret = cma_connect_ib(id_priv, conn_param); | |
04215330 | 3624 | } else if (rdma_cap_iw_cm(id->device, id->port_num)) |
07ebafba | 3625 | ret = cma_connect_iw(id_priv, conn_param); |
21655afc | 3626 | else |
e51060f0 | 3627 | ret = -ENOSYS; |
e51060f0 SH |
3628 | if (ret) |
3629 | goto err; | |
3630 | ||
3631 | return 0; | |
3632 | err: | |
550e5ca7 | 3633 | cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED); |
e51060f0 SH |
3634 | return ret; |
3635 | } | |
3636 | EXPORT_SYMBOL(rdma_connect); | |
3637 | ||
3638 | static int cma_accept_ib(struct rdma_id_private *id_priv, | |
3639 | struct rdma_conn_param *conn_param) | |
3640 | { | |
3641 | struct ib_cm_rep_param rep; | |
5851bb89 | 3642 | int ret; |
0fe313b0 | 3643 | |
5851bb89 SH |
3644 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
3645 | if (ret) | |
3646 | goto out; | |
0fe313b0 | 3647 | |
5851bb89 SH |
3648 | ret = cma_modify_qp_rts(id_priv, conn_param); |
3649 | if (ret) | |
3650 | goto out; | |
e51060f0 SH |
3651 | |
3652 | memset(&rep, 0, sizeof rep); | |
3653 | rep.qp_num = id_priv->qp_num; | |
3654 | rep.starting_psn = id_priv->seq_num; | |
3655 | rep.private_data = conn_param->private_data; | |
3656 | rep.private_data_len = conn_param->private_data_len; | |
3657 | rep.responder_resources = conn_param->responder_resources; | |
3658 | rep.initiator_depth = conn_param->initiator_depth; | |
e51060f0 SH |
3659 | rep.failover_accepted = 0; |
3660 | rep.flow_control = conn_param->flow_control; | |
4ede178a | 3661 | rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count); |
e51060f0 SH |
3662 | rep.srq = id_priv->srq ? 1 : 0; |
3663 | ||
0fe313b0 SH |
3664 | ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep); |
3665 | out: | |
3666 | return ret; | |
e51060f0 SH |
3667 | } |
3668 | ||
07ebafba TT |
3669 | static int cma_accept_iw(struct rdma_id_private *id_priv, |
3670 | struct rdma_conn_param *conn_param) | |
3671 | { | |
3672 | struct iw_cm_conn_param iw_param; | |
3673 | int ret; | |
3674 | ||
f2625f7d SW |
3675 | if (!conn_param) |
3676 | return -EINVAL; | |
3677 | ||
5851bb89 | 3678 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
07ebafba TT |
3679 | if (ret) |
3680 | return ret; | |
3681 | ||
3682 | iw_param.ord = conn_param->initiator_depth; | |
3683 | iw_param.ird = conn_param->responder_resources; | |
3684 | iw_param.private_data = conn_param->private_data; | |
3685 | iw_param.private_data_len = conn_param->private_data_len; | |
3686 | if (id_priv->id.qp) { | |
3687 | iw_param.qpn = id_priv->qp_num; | |
3688 | } else | |
3689 | iw_param.qpn = conn_param->qp_num; | |
3690 | ||
3691 | return iw_cm_accept(id_priv->cm_id.iw, &iw_param); | |
3692 | } | |
3693 | ||
628e5f6d | 3694 | static int cma_send_sidr_rep(struct rdma_id_private *id_priv, |
5c438135 | 3695 | enum ib_cm_sidr_status status, u32 qkey, |
628e5f6d SH |
3696 | const void *private_data, int private_data_len) |
3697 | { | |
3698 | struct ib_cm_sidr_rep_param rep; | |
d2ca39f2 | 3699 | int ret; |
628e5f6d SH |
3700 | |
3701 | memset(&rep, 0, sizeof rep); | |
3702 | rep.status = status; | |
3703 | if (status == IB_SIDR_SUCCESS) { | |
5c438135 | 3704 | ret = cma_set_qkey(id_priv, qkey); |
d2ca39f2 YE |
3705 | if (ret) |
3706 | return ret; | |
628e5f6d | 3707 | rep.qp_num = id_priv->qp_num; |
c8f6a362 | 3708 | rep.qkey = id_priv->qkey; |
628e5f6d SH |
3709 | } |
3710 | rep.private_data = private_data; | |
3711 | rep.private_data_len = private_data_len; | |
3712 | ||
3713 | return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep); | |
3714 | } | |
3715 | ||
e51060f0 SH |
3716 | int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
3717 | { | |
3718 | struct rdma_id_private *id_priv; | |
3719 | int ret; | |
3720 | ||
3721 | id_priv = container_of(id, struct rdma_id_private, id); | |
83e9502d NM |
3722 | |
3723 | id_priv->owner = task_pid_nr(current); | |
3724 | ||
550e5ca7 | 3725 | if (!cma_comp(id_priv, RDMA_CM_CONNECT)) |
e51060f0 SH |
3726 | return -EINVAL; |
3727 | ||
3728 | if (!id->qp && conn_param) { | |
3729 | id_priv->qp_num = conn_param->qp_num; | |
e51060f0 SH |
3730 | id_priv->srq = conn_param->srq; |
3731 | } | |
3732 | ||
72219cea | 3733 | if (rdma_cap_ib_cm(id->device, id->port_num)) { |
f45ee80e HS |
3734 | if (id->qp_type == IB_QPT_UD) { |
3735 | if (conn_param) | |
3736 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, | |
5c438135 | 3737 | conn_param->qkey, |
f45ee80e HS |
3738 | conn_param->private_data, |
3739 | conn_param->private_data_len); | |
3740 | else | |
3741 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, | |
5c438135 | 3742 | 0, NULL, 0); |
f45ee80e HS |
3743 | } else { |
3744 | if (conn_param) | |
3745 | ret = cma_accept_ib(id_priv, conn_param); | |
3746 | else | |
3747 | ret = cma_rep_recv(id_priv); | |
3748 | } | |
04215330 | 3749 | } else if (rdma_cap_iw_cm(id->device, id->port_num)) |
07ebafba | 3750 | ret = cma_accept_iw(id_priv, conn_param); |
21655afc | 3751 | else |
e51060f0 | 3752 | ret = -ENOSYS; |
e51060f0 SH |
3753 | |
3754 | if (ret) | |
3755 | goto reject; | |
3756 | ||
3757 | return 0; | |
3758 | reject: | |
c5483388 | 3759 | cma_modify_qp_err(id_priv); |
e51060f0 SH |
3760 | rdma_reject(id, NULL, 0); |
3761 | return ret; | |
3762 | } | |
3763 | EXPORT_SYMBOL(rdma_accept); | |
3764 | ||
0fe313b0 SH |
3765 | int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event) |
3766 | { | |
3767 | struct rdma_id_private *id_priv; | |
3768 | int ret; | |
3769 | ||
3770 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 3771 | if (!id_priv->cm_id.ib) |
0fe313b0 SH |
3772 | return -EINVAL; |
3773 | ||
3774 | switch (id->device->node_type) { | |
3775 | case RDMA_NODE_IB_CA: | |
3776 | ret = ib_cm_notify(id_priv->cm_id.ib, event); | |
3777 | break; | |
3778 | default: | |
3779 | ret = 0; | |
3780 | break; | |
3781 | } | |
3782 | return ret; | |
3783 | } | |
3784 | EXPORT_SYMBOL(rdma_notify); | |
3785 | ||
e51060f0 SH |
3786 | int rdma_reject(struct rdma_cm_id *id, const void *private_data, |
3787 | u8 private_data_len) | |
3788 | { | |
3789 | struct rdma_id_private *id_priv; | |
3790 | int ret; | |
3791 | ||
3792 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 3793 | if (!id_priv->cm_id.ib) |
e51060f0 SH |
3794 | return -EINVAL; |
3795 | ||
72219cea | 3796 | if (rdma_cap_ib_cm(id->device, id->port_num)) { |
b26f9b99 | 3797 | if (id->qp_type == IB_QPT_UD) |
5c438135 | 3798 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0, |
628e5f6d SH |
3799 | private_data, private_data_len); |
3800 | else | |
3801 | ret = ib_send_cm_rej(id_priv->cm_id.ib, | |
3802 | IB_CM_REJ_CONSUMER_DEFINED, NULL, | |
3803 | 0, private_data, private_data_len); | |
04215330 | 3804 | } else if (rdma_cap_iw_cm(id->device, id->port_num)) { |
07ebafba TT |
3805 | ret = iw_cm_reject(id_priv->cm_id.iw, |
3806 | private_data, private_data_len); | |
21655afc | 3807 | } else |
e51060f0 | 3808 | ret = -ENOSYS; |
21655afc | 3809 | |
e51060f0 SH |
3810 | return ret; |
3811 | } | |
3812 | EXPORT_SYMBOL(rdma_reject); | |
3813 | ||
3814 | int rdma_disconnect(struct rdma_cm_id *id) | |
3815 | { | |
3816 | struct rdma_id_private *id_priv; | |
3817 | int ret; | |
3818 | ||
3819 | id_priv = container_of(id, struct rdma_id_private, id); | |
0c9361fc | 3820 | if (!id_priv->cm_id.ib) |
e51060f0 SH |
3821 | return -EINVAL; |
3822 | ||
72219cea | 3823 | if (rdma_cap_ib_cm(id->device, id->port_num)) { |
c5483388 | 3824 | ret = cma_modify_qp_err(id_priv); |
07ebafba TT |
3825 | if (ret) |
3826 | goto out; | |
e51060f0 SH |
3827 | /* Initiate or respond to a disconnect. */ |
3828 | if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) | |
3829 | ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0); | |
04215330 | 3830 | } else if (rdma_cap_iw_cm(id->device, id->port_num)) { |
07ebafba | 3831 | ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); |
21655afc | 3832 | } else |
07ebafba | 3833 | ret = -EINVAL; |
21655afc | 3834 | |
e51060f0 SH |
3835 | out: |
3836 | return ret; | |
3837 | } | |
3838 | EXPORT_SYMBOL(rdma_disconnect); | |
3839 | ||
c8f6a362 SH |
3840 | static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast) |
3841 | { | |
3842 | struct rdma_id_private *id_priv; | |
3843 | struct cma_multicast *mc = multicast->context; | |
3844 | struct rdma_cm_event event; | |
37e07cda | 3845 | int ret = 0; |
c8f6a362 SH |
3846 | |
3847 | id_priv = mc->id_priv; | |
37e07cda BVA |
3848 | mutex_lock(&id_priv->handler_mutex); |
3849 | if (id_priv->state != RDMA_CM_ADDR_BOUND && | |
3850 | id_priv->state != RDMA_CM_ADDR_RESOLVED) | |
3851 | goto out; | |
c8f6a362 | 3852 | |
5c438135 SH |
3853 | if (!status) |
3854 | status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey)); | |
498683c6 MS |
3855 | else |
3856 | pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n", | |
3857 | status); | |
c5483388 | 3858 | mutex_lock(&id_priv->qp_mutex); |
498683c6 | 3859 | if (!status && id_priv->id.qp) { |
c8f6a362 | 3860 | status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid, |
46ea5061 | 3861 | be16_to_cpu(multicast->rec.mlid)); |
a3dd3a48 | 3862 | if (status) |
498683c6 MS |
3863 | pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n", |
3864 | status); | |
3865 | } | |
c5483388 | 3866 | mutex_unlock(&id_priv->qp_mutex); |
c8f6a362 SH |
3867 | |
3868 | memset(&event, 0, sizeof event); | |
3869 | event.status = status; | |
3870 | event.param.ud.private_data = mc->context; | |
3871 | if (!status) { | |
bee3c3c9 MS |
3872 | struct rdma_dev_addr *dev_addr = |
3873 | &id_priv->id.route.addr.dev_addr; | |
3874 | struct net_device *ndev = | |
3875 | dev_get_by_index(&init_net, dev_addr->bound_dev_if); | |
3876 | enum ib_gid_type gid_type = | |
3877 | id_priv->cma_dev->default_gid_type[id_priv->id.port_num - | |
3878 | rdma_start_port(id_priv->cma_dev->device)]; | |
3879 | ||
c8f6a362 SH |
3880 | event.event = RDMA_CM_EVENT_MULTICAST_JOIN; |
3881 | ib_init_ah_from_mcmember(id_priv->id.device, | |
3882 | id_priv->id.port_num, &multicast->rec, | |
bee3c3c9 | 3883 | ndev, gid_type, |
c8f6a362 SH |
3884 | &event.param.ud.ah_attr); |
3885 | event.param.ud.qp_num = 0xFFFFFF; | |
3886 | event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey); | |
bee3c3c9 MS |
3887 | if (ndev) |
3888 | dev_put(ndev); | |
c8f6a362 SH |
3889 | } else |
3890 | event.event = RDMA_CM_EVENT_MULTICAST_ERROR; | |
3891 | ||
3892 | ret = id_priv->id.event_handler(&id_priv->id, &event); | |
3893 | if (ret) { | |
550e5ca7 | 3894 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
de910bd9 | 3895 | mutex_unlock(&id_priv->handler_mutex); |
c8f6a362 SH |
3896 | rdma_destroy_id(&id_priv->id); |
3897 | return 0; | |
3898 | } | |
8aa08602 | 3899 | |
37e07cda | 3900 | out: |
de910bd9 | 3901 | mutex_unlock(&id_priv->handler_mutex); |
c8f6a362 SH |
3902 | return 0; |
3903 | } | |
3904 | ||
3905 | static void cma_set_mgid(struct rdma_id_private *id_priv, | |
3906 | struct sockaddr *addr, union ib_gid *mgid) | |
3907 | { | |
3908 | unsigned char mc_map[MAX_ADDR_LEN]; | |
3909 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
3910 | struct sockaddr_in *sin = (struct sockaddr_in *) addr; | |
3911 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr; | |
3912 | ||
3913 | if (cma_any_addr(addr)) { | |
3914 | memset(mgid, 0, sizeof *mgid); | |
3915 | } else if ((addr->sa_family == AF_INET6) && | |
1c9b2819 | 3916 | ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) == |
c8f6a362 SH |
3917 | 0xFF10A01B)) { |
3918 | /* IPv6 address is an SA assigned MGID. */ | |
3919 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); | |
5bc2b7b3 SH |
3920 | } else if (addr->sa_family == AF_IB) { |
3921 | memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid); | |
e2e62697 JG |
3922 | } else if ((addr->sa_family == AF_INET6)) { |
3923 | ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map); | |
3924 | if (id_priv->id.ps == RDMA_PS_UDP) | |
3925 | mc_map[7] = 0x01; /* Use RDMA CM signature */ | |
3926 | *mgid = *(union ib_gid *) (mc_map + 4); | |
c8f6a362 | 3927 | } else { |
a9e527e3 | 3928 | ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map); |
c8f6a362 SH |
3929 | if (id_priv->id.ps == RDMA_PS_UDP) |
3930 | mc_map[7] = 0x01; /* Use RDMA CM signature */ | |
c8f6a362 SH |
3931 | *mgid = *(union ib_gid *) (mc_map + 4); |
3932 | } | |
3933 | } | |
3934 | ||
3935 | static int cma_join_ib_multicast(struct rdma_id_private *id_priv, | |
3936 | struct cma_multicast *mc) | |
3937 | { | |
3938 | struct ib_sa_mcmember_rec rec; | |
3939 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
3940 | ib_sa_comp_mask comp_mask; | |
3941 | int ret; | |
3942 | ||
3943 | ib_addr_get_mgid(dev_addr, &rec.mgid); | |
3944 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num, | |
3945 | &rec.mgid, &rec); | |
3946 | if (ret) | |
3947 | return ret; | |
3948 | ||
5bc2b7b3 SH |
3949 | ret = cma_set_qkey(id_priv, 0); |
3950 | if (ret) | |
3951 | return ret; | |
3952 | ||
3f446754 | 3953 | cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid); |
5bc2b7b3 | 3954 | rec.qkey = cpu_to_be32(id_priv->qkey); |
6f8372b6 | 3955 | rdma_addr_get_sgid(dev_addr, &rec.port_gid); |
c8f6a362 | 3956 | rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); |
ab15c95a AV |
3957 | rec.join_state = mc->join_state; |
3958 | ||
ee1c60b1 DC |
3959 | if ((rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) && |
3960 | (!ib_sa_sendonly_fullmem_support(&sa_client, | |
3961 | id_priv->id.device, | |
3962 | id_priv->id.port_num))) { | |
3963 | pr_warn("RDMA CM: %s port %u Unable to multicast join\n" | |
3964 | "RDMA CM: SM doesn't support Send Only Full Member option\n", | |
3965 | id_priv->id.device->name, id_priv->id.port_num); | |
3966 | return -EOPNOTSUPP; | |
ab15c95a | 3967 | } |
c8f6a362 SH |
3968 | |
3969 | comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID | | |
3970 | IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE | | |
3971 | IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL | | |
3972 | IB_SA_MCMEMBER_REC_FLOW_LABEL | | |
3973 | IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; | |
3974 | ||
84adeee9 YE |
3975 | if (id_priv->id.ps == RDMA_PS_IPOIB) |
3976 | comp_mask |= IB_SA_MCMEMBER_REC_RATE | | |
2a22fb8c DB |
3977 | IB_SA_MCMEMBER_REC_RATE_SELECTOR | |
3978 | IB_SA_MCMEMBER_REC_MTU_SELECTOR | | |
3979 | IB_SA_MCMEMBER_REC_MTU | | |
3980 | IB_SA_MCMEMBER_REC_HOP_LIMIT; | |
84adeee9 | 3981 | |
c8f6a362 SH |
3982 | mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device, |
3983 | id_priv->id.port_num, &rec, | |
3984 | comp_mask, GFP_KERNEL, | |
3985 | cma_ib_mc_handler, mc); | |
8c6ffba0 | 3986 | return PTR_ERR_OR_ZERO(mc->multicast.ib); |
c8f6a362 SH |
3987 | } |
3988 | ||
3c86aa70 EC |
3989 | static void iboe_mcast_work_handler(struct work_struct *work) |
3990 | { | |
3991 | struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work); | |
3992 | struct cma_multicast *mc = mw->mc; | |
3993 | struct ib_sa_multicast *m = mc->multicast.ib; | |
3994 | ||
3995 | mc->multicast.ib->context = mc; | |
3996 | cma_ib_mc_handler(0, m); | |
3997 | kref_put(&mc->mcref, release_mc); | |
3998 | kfree(mw); | |
3999 | } | |
4000 | ||
4001 | static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid) | |
4002 | { | |
4003 | struct sockaddr_in *sin = (struct sockaddr_in *)addr; | |
4004 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; | |
4005 | ||
4006 | if (cma_any_addr(addr)) { | |
4007 | memset(mgid, 0, sizeof *mgid); | |
4008 | } else if (addr->sa_family == AF_INET6) { | |
4009 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); | |
4010 | } else { | |
4011 | mgid->raw[0] = 0xff; | |
4012 | mgid->raw[1] = 0x0e; | |
4013 | mgid->raw[2] = 0; | |
4014 | mgid->raw[3] = 0; | |
4015 | mgid->raw[4] = 0; | |
4016 | mgid->raw[5] = 0; | |
4017 | mgid->raw[6] = 0; | |
4018 | mgid->raw[7] = 0; | |
4019 | mgid->raw[8] = 0; | |
4020 | mgid->raw[9] = 0; | |
4021 | mgid->raw[10] = 0xff; | |
4022 | mgid->raw[11] = 0xff; | |
4023 | *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr; | |
4024 | } | |
4025 | } | |
4026 | ||
4027 | static int cma_iboe_join_multicast(struct rdma_id_private *id_priv, | |
4028 | struct cma_multicast *mc) | |
4029 | { | |
4030 | struct iboe_mcast_work *work; | |
4031 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; | |
bee3c3c9 | 4032 | int err = 0; |
3c86aa70 EC |
4033 | struct sockaddr *addr = (struct sockaddr *)&mc->addr; |
4034 | struct net_device *ndev = NULL; | |
bee3c3c9 | 4035 | enum ib_gid_type gid_type; |
ab15c95a AV |
4036 | bool send_only; |
4037 | ||
4038 | send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN); | |
3c86aa70 EC |
4039 | |
4040 | if (cma_zero_addr((struct sockaddr *)&mc->addr)) | |
4041 | return -EINVAL; | |
4042 | ||
4043 | work = kzalloc(sizeof *work, GFP_KERNEL); | |
4044 | if (!work) | |
4045 | return -ENOMEM; | |
4046 | ||
4047 | mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL); | |
4048 | if (!mc->multicast.ib) { | |
4049 | err = -ENOMEM; | |
4050 | goto out1; | |
4051 | } | |
4052 | ||
4053 | cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid); | |
4054 | ||
4055 | mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff); | |
4056 | if (id_priv->id.ps == RDMA_PS_UDP) | |
4057 | mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); | |
4058 | ||
4059 | if (dev_addr->bound_dev_if) | |
4060 | ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); | |
4061 | if (!ndev) { | |
4062 | err = -ENODEV; | |
4063 | goto out2; | |
4064 | } | |
4065 | mc->multicast.ib->rec.rate = iboe_get_rate(ndev); | |
4066 | mc->multicast.ib->rec.hop_limit = 1; | |
4067 | mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu); | |
bee3c3c9 MS |
4068 | |
4069 | gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num - | |
4070 | rdma_start_port(id_priv->cma_dev->device)]; | |
4071 | if (addr->sa_family == AF_INET) { | |
c65f6c5a AV |
4072 | if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) { |
4073 | mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT; | |
ab15c95a AV |
4074 | if (!send_only) { |
4075 | err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid, | |
4076 | true); | |
4077 | if (!err) | |
4078 | mc->igmp_joined = true; | |
4079 | } | |
bee3c3c9 MS |
4080 | } |
4081 | } else { | |
4082 | if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) | |
4083 | err = -ENOTSUPP; | |
4084 | } | |
3c86aa70 | 4085 | dev_put(ndev); |
bee3c3c9 MS |
4086 | if (err || !mc->multicast.ib->rec.mtu) { |
4087 | if (!err) | |
4088 | err = -EINVAL; | |
3c86aa70 EC |
4089 | goto out2; |
4090 | } | |
7b85627b MS |
4091 | rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, |
4092 | &mc->multicast.ib->rec.port_gid); | |
3c86aa70 EC |
4093 | work->id = id_priv; |
4094 | work->mc = mc; | |
4095 | INIT_WORK(&work->work, iboe_mcast_work_handler); | |
4096 | kref_get(&mc->mcref); | |
4097 | queue_work(cma_wq, &work->work); | |
4098 | ||
4099 | return 0; | |
4100 | ||
4101 | out2: | |
4102 | kfree(mc->multicast.ib); | |
4103 | out1: | |
4104 | kfree(work); | |
4105 | return err; | |
4106 | } | |
4107 | ||
c8f6a362 | 4108 | int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, |
ab15c95a | 4109 | u8 join_state, void *context) |
c8f6a362 SH |
4110 | { |
4111 | struct rdma_id_private *id_priv; | |
4112 | struct cma_multicast *mc; | |
4113 | int ret; | |
4114 | ||
4115 | id_priv = container_of(id, struct rdma_id_private, id); | |
550e5ca7 NM |
4116 | if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) && |
4117 | !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED)) | |
c8f6a362 SH |
4118 | return -EINVAL; |
4119 | ||
4120 | mc = kmalloc(sizeof *mc, GFP_KERNEL); | |
4121 | if (!mc) | |
4122 | return -ENOMEM; | |
4123 | ||
ef560861 | 4124 | memcpy(&mc->addr, addr, rdma_addr_size(addr)); |
c8f6a362 SH |
4125 | mc->context = context; |
4126 | mc->id_priv = id_priv; | |
bee3c3c9 | 4127 | mc->igmp_joined = false; |
ab15c95a | 4128 | mc->join_state = join_state; |
c8f6a362 SH |
4129 | spin_lock(&id_priv->lock); |
4130 | list_add(&mc->list, &id_priv->mc_list); | |
4131 | spin_unlock(&id_priv->lock); | |
4132 | ||
5d9fb044 | 4133 | if (rdma_protocol_roce(id->device, id->port_num)) { |
5c9a5282 MW |
4134 | kref_init(&mc->mcref); |
4135 | ret = cma_iboe_join_multicast(id_priv, mc); | |
a31ad3b0 | 4136 | } else if (rdma_cap_ib_mcast(id->device, id->port_num)) |
5c9a5282 MW |
4137 | ret = cma_join_ib_multicast(id_priv, mc); |
4138 | else | |
c8f6a362 | 4139 | ret = -ENOSYS; |
c8f6a362 SH |
4140 | |
4141 | if (ret) { | |
4142 | spin_lock_irq(&id_priv->lock); | |
4143 | list_del(&mc->list); | |
4144 | spin_unlock_irq(&id_priv->lock); | |
4145 | kfree(mc); | |
4146 | } | |
4147 | return ret; | |
4148 | } | |
4149 | EXPORT_SYMBOL(rdma_join_multicast); | |
4150 | ||
4151 | void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) | |
4152 | { | |
4153 | struct rdma_id_private *id_priv; | |
4154 | struct cma_multicast *mc; | |
4155 | ||
4156 | id_priv = container_of(id, struct rdma_id_private, id); | |
4157 | spin_lock_irq(&id_priv->lock); | |
4158 | list_for_each_entry(mc, &id_priv->mc_list, list) { | |
ef560861 | 4159 | if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) { |
c8f6a362 SH |
4160 | list_del(&mc->list); |
4161 | spin_unlock_irq(&id_priv->lock); | |
4162 | ||
4163 | if (id->qp) | |
4164 | ib_detach_mcast(id->qp, | |
4165 | &mc->multicast.ib->rec.mgid, | |
46ea5061 | 4166 | be16_to_cpu(mc->multicast.ib->rec.mlid)); |
5c9a5282 MW |
4167 | |
4168 | BUG_ON(id_priv->cma_dev->device != id->device); | |
4169 | ||
a31ad3b0 | 4170 | if (rdma_cap_ib_mcast(id->device, id->port_num)) { |
5c9a5282 MW |
4171 | ib_sa_free_multicast(mc->multicast.ib); |
4172 | kfree(mc); | |
bee3c3c9 MS |
4173 | } else if (rdma_protocol_roce(id->device, id->port_num)) { |
4174 | if (mc->igmp_joined) { | |
4175 | struct rdma_dev_addr *dev_addr = | |
4176 | &id->route.addr.dev_addr; | |
4177 | struct net_device *ndev = NULL; | |
4178 | ||
4179 | if (dev_addr->bound_dev_if) | |
4180 | ndev = dev_get_by_index(&init_net, | |
4181 | dev_addr->bound_dev_if); | |
4182 | if (ndev) { | |
4183 | cma_igmp_send(ndev, | |
4184 | &mc->multicast.ib->rec.mgid, | |
4185 | false); | |
4186 | dev_put(ndev); | |
4187 | } | |
4188 | mc->igmp_joined = false; | |
4189 | } | |
5c9a5282 | 4190 | kref_put(&mc->mcref, release_mc); |
bee3c3c9 | 4191 | } |
c8f6a362 SH |
4192 | return; |
4193 | } | |
4194 | } | |
4195 | spin_unlock_irq(&id_priv->lock); | |
4196 | } | |
4197 | EXPORT_SYMBOL(rdma_leave_multicast); | |
4198 | ||
dd5bdff8 OG |
4199 | static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv) |
4200 | { | |
4201 | struct rdma_dev_addr *dev_addr; | |
4202 | struct cma_ndev_work *work; | |
4203 | ||
4204 | dev_addr = &id_priv->id.route.addr.dev_addr; | |
4205 | ||
6266ed6e | 4206 | if ((dev_addr->bound_dev_if == ndev->ifindex) && |
fa20105e | 4207 | (net_eq(dev_net(ndev), dev_addr->net)) && |
dd5bdff8 | 4208 | memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) { |
aba25a3e PP |
4209 | pr_info("RDMA CM addr change for ndev %s used by id %p\n", |
4210 | ndev->name, &id_priv->id); | |
dd5bdff8 OG |
4211 | work = kzalloc(sizeof *work, GFP_KERNEL); |
4212 | if (!work) | |
4213 | return -ENOMEM; | |
4214 | ||
4215 | INIT_WORK(&work->work, cma_ndev_work_handler); | |
4216 | work->id = id_priv; | |
4217 | work->event.event = RDMA_CM_EVENT_ADDR_CHANGE; | |
4218 | atomic_inc(&id_priv->refcount); | |
4219 | queue_work(cma_wq, &work->work); | |
4220 | } | |
4221 | ||
4222 | return 0; | |
4223 | } | |
4224 | ||
4225 | static int cma_netdev_callback(struct notifier_block *self, unsigned long event, | |
351638e7 | 4226 | void *ptr) |
dd5bdff8 | 4227 | { |
351638e7 | 4228 | struct net_device *ndev = netdev_notifier_info_to_dev(ptr); |
dd5bdff8 OG |
4229 | struct cma_device *cma_dev; |
4230 | struct rdma_id_private *id_priv; | |
4231 | int ret = NOTIFY_DONE; | |
4232 | ||
dd5bdff8 OG |
4233 | if (event != NETDEV_BONDING_FAILOVER) |
4234 | return NOTIFY_DONE; | |
4235 | ||
4236 | if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING)) | |
4237 | return NOTIFY_DONE; | |
4238 | ||
4239 | mutex_lock(&lock); | |
4240 | list_for_each_entry(cma_dev, &dev_list, list) | |
4241 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { | |
4242 | ret = cma_netdev_change(ndev, id_priv); | |
4243 | if (ret) | |
4244 | goto out; | |
4245 | } | |
4246 | ||
4247 | out: | |
4248 | mutex_unlock(&lock); | |
4249 | return ret; | |
4250 | } | |
4251 | ||
4252 | static struct notifier_block cma_nb = { | |
4253 | .notifier_call = cma_netdev_callback | |
4254 | }; | |
4255 | ||
e51060f0 SH |
4256 | static void cma_add_one(struct ib_device *device) |
4257 | { | |
4258 | struct cma_device *cma_dev; | |
4259 | struct rdma_id_private *id_priv; | |
045959db MB |
4260 | unsigned int i; |
4261 | unsigned long supported_gids = 0; | |
e51060f0 SH |
4262 | |
4263 | cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); | |
4264 | if (!cma_dev) | |
4265 | return; | |
4266 | ||
4267 | cma_dev->device = device; | |
045959db MB |
4268 | cma_dev->default_gid_type = kcalloc(device->phys_port_cnt, |
4269 | sizeof(*cma_dev->default_gid_type), | |
4270 | GFP_KERNEL); | |
89052d78 MD |
4271 | if (!cma_dev->default_gid_type) |
4272 | goto free_cma_dev; | |
4273 | ||
4274 | cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt, | |
4275 | sizeof(*cma_dev->default_roce_tos), | |
4276 | GFP_KERNEL); | |
4277 | if (!cma_dev->default_roce_tos) | |
4278 | goto free_gid_type; | |
4279 | ||
045959db MB |
4280 | for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) { |
4281 | supported_gids = roce_gid_type_mask_support(device, i); | |
4282 | WARN_ON(!supported_gids); | |
4283 | cma_dev->default_gid_type[i - rdma_start_port(device)] = | |
4284 | find_first_bit(&supported_gids, BITS_PER_LONG); | |
89052d78 | 4285 | cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0; |
045959db | 4286 | } |
e51060f0 SH |
4287 | |
4288 | init_completion(&cma_dev->comp); | |
4289 | atomic_set(&cma_dev->refcount, 1); | |
4290 | INIT_LIST_HEAD(&cma_dev->id_list); | |
4291 | ib_set_client_data(device, &cma_client, cma_dev); | |
4292 | ||
4293 | mutex_lock(&lock); | |
4294 | list_add_tail(&cma_dev->list, &dev_list); | |
4295 | list_for_each_entry(id_priv, &listen_any_list, list) | |
4296 | cma_listen_on_dev(id_priv, cma_dev); | |
4297 | mutex_unlock(&lock); | |
89052d78 MD |
4298 | |
4299 | return; | |
4300 | ||
4301 | free_gid_type: | |
4302 | kfree(cma_dev->default_gid_type); | |
4303 | ||
4304 | free_cma_dev: | |
4305 | kfree(cma_dev); | |
4306 | ||
4307 | return; | |
e51060f0 SH |
4308 | } |
4309 | ||
4310 | static int cma_remove_id_dev(struct rdma_id_private *id_priv) | |
4311 | { | |
a1b1b61f | 4312 | struct rdma_cm_event event; |
550e5ca7 | 4313 | enum rdma_cm_state state; |
de910bd9 | 4314 | int ret = 0; |
e51060f0 SH |
4315 | |
4316 | /* Record that we want to remove the device */ | |
550e5ca7 NM |
4317 | state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL); |
4318 | if (state == RDMA_CM_DESTROYING) | |
e51060f0 SH |
4319 | return 0; |
4320 | ||
4321 | cma_cancel_operation(id_priv, state); | |
de910bd9 | 4322 | mutex_lock(&id_priv->handler_mutex); |
e51060f0 SH |
4323 | |
4324 | /* Check for destruction from another callback. */ | |
550e5ca7 | 4325 | if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL)) |
de910bd9 | 4326 | goto out; |
e51060f0 | 4327 | |
a1b1b61f SH |
4328 | memset(&event, 0, sizeof event); |
4329 | event.event = RDMA_CM_EVENT_DEVICE_REMOVAL; | |
de910bd9 OG |
4330 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
4331 | out: | |
4332 | mutex_unlock(&id_priv->handler_mutex); | |
4333 | return ret; | |
e51060f0 SH |
4334 | } |
4335 | ||
4336 | static void cma_process_remove(struct cma_device *cma_dev) | |
4337 | { | |
e51060f0 SH |
4338 | struct rdma_id_private *id_priv; |
4339 | int ret; | |
4340 | ||
e51060f0 SH |
4341 | mutex_lock(&lock); |
4342 | while (!list_empty(&cma_dev->id_list)) { | |
4343 | id_priv = list_entry(cma_dev->id_list.next, | |
4344 | struct rdma_id_private, list); | |
4345 | ||
d02d1f53 | 4346 | list_del(&id_priv->listen_list); |
94de178a | 4347 | list_del_init(&id_priv->list); |
e51060f0 SH |
4348 | atomic_inc(&id_priv->refcount); |
4349 | mutex_unlock(&lock); | |
4350 | ||
d02d1f53 | 4351 | ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv); |
e51060f0 SH |
4352 | cma_deref_id(id_priv); |
4353 | if (ret) | |
4354 | rdma_destroy_id(&id_priv->id); | |
4355 | ||
4356 | mutex_lock(&lock); | |
4357 | } | |
4358 | mutex_unlock(&lock); | |
4359 | ||
4360 | cma_deref_dev(cma_dev); | |
4361 | wait_for_completion(&cma_dev->comp); | |
4362 | } | |
4363 | ||
7c1eb45a | 4364 | static void cma_remove_one(struct ib_device *device, void *client_data) |
e51060f0 | 4365 | { |
7c1eb45a | 4366 | struct cma_device *cma_dev = client_data; |
e51060f0 | 4367 | |
e51060f0 SH |
4368 | if (!cma_dev) |
4369 | return; | |
4370 | ||
4371 | mutex_lock(&lock); | |
4372 | list_del(&cma_dev->list); | |
4373 | mutex_unlock(&lock); | |
4374 | ||
4375 | cma_process_remove(cma_dev); | |
89052d78 | 4376 | kfree(cma_dev->default_roce_tos); |
045959db | 4377 | kfree(cma_dev->default_gid_type); |
e51060f0 SH |
4378 | kfree(cma_dev); |
4379 | } | |
4380 | ||
753f618a NM |
4381 | static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb) |
4382 | { | |
4383 | struct nlmsghdr *nlh; | |
4384 | struct rdma_cm_id_stats *id_stats; | |
4385 | struct rdma_id_private *id_priv; | |
4386 | struct rdma_cm_id *id = NULL; | |
4387 | struct cma_device *cma_dev; | |
4388 | int i_dev = 0, i_id = 0; | |
4389 | ||
4390 | /* | |
4391 | * We export all of the IDs as a sequence of messages. Each | |
4392 | * ID gets its own netlink message. | |
4393 | */ | |
4394 | mutex_lock(&lock); | |
4395 | ||
4396 | list_for_each_entry(cma_dev, &dev_list, list) { | |
4397 | if (i_dev < cb->args[0]) { | |
4398 | i_dev++; | |
4399 | continue; | |
4400 | } | |
4401 | ||
4402 | i_id = 0; | |
4403 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { | |
4404 | if (i_id < cb->args[1]) { | |
4405 | i_id++; | |
4406 | continue; | |
4407 | } | |
4408 | ||
4409 | id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq, | |
4410 | sizeof *id_stats, RDMA_NL_RDMA_CM, | |
30dc5e63 TN |
4411 | RDMA_NL_RDMA_CM_ID_STATS, |
4412 | NLM_F_MULTI); | |
753f618a NM |
4413 | if (!id_stats) |
4414 | goto out; | |
4415 | ||
4416 | memset(id_stats, 0, sizeof *id_stats); | |
4417 | id = &id_priv->id; | |
4418 | id_stats->node_type = id->route.addr.dev_addr.dev_type; | |
4419 | id_stats->port_num = id->port_num; | |
4420 | id_stats->bound_dev_if = | |
4421 | id->route.addr.dev_addr.bound_dev_if; | |
4422 | ||
ce117ffa SH |
4423 | if (ibnl_put_attr(skb, nlh, |
4424 | rdma_addr_size(cma_src_addr(id_priv)), | |
4425 | cma_src_addr(id_priv), | |
4426 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) | |
4427 | goto out; | |
4428 | if (ibnl_put_attr(skb, nlh, | |
4429 | rdma_addr_size(cma_src_addr(id_priv)), | |
4430 | cma_dst_addr(id_priv), | |
4431 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) | |
4432 | goto out; | |
753f618a | 4433 | |
83e9502d | 4434 | id_stats->pid = id_priv->owner; |
753f618a NM |
4435 | id_stats->port_space = id->ps; |
4436 | id_stats->cm_state = id_priv->state; | |
4437 | id_stats->qp_num = id_priv->qp_num; | |
4438 | id_stats->qp_type = id->qp_type; | |
4439 | ||
4440 | i_id++; | |
4441 | } | |
4442 | ||
4443 | cb->args[1] = 0; | |
4444 | i_dev++; | |
4445 | } | |
4446 | ||
4447 | out: | |
4448 | mutex_unlock(&lock); | |
4449 | cb->args[0] = i_dev; | |
4450 | cb->args[1] = i_id; | |
4451 | ||
4452 | return skb->len; | |
4453 | } | |
4454 | ||
4455 | static const struct ibnl_client_cbs cma_cb_table[] = { | |
809d5fc9 G |
4456 | [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats, |
4457 | .module = THIS_MODULE }, | |
753f618a NM |
4458 | }; |
4459 | ||
4be74b42 HE |
4460 | static int cma_init_net(struct net *net) |
4461 | { | |
4462 | struct cma_pernet *pernet = cma_pernet(net); | |
4463 | ||
4464 | idr_init(&pernet->tcp_ps); | |
4465 | idr_init(&pernet->udp_ps); | |
4466 | idr_init(&pernet->ipoib_ps); | |
4467 | idr_init(&pernet->ib_ps); | |
4468 | ||
4469 | return 0; | |
4470 | } | |
4471 | ||
4472 | static void cma_exit_net(struct net *net) | |
4473 | { | |
4474 | struct cma_pernet *pernet = cma_pernet(net); | |
4475 | ||
4476 | idr_destroy(&pernet->tcp_ps); | |
4477 | idr_destroy(&pernet->udp_ps); | |
4478 | idr_destroy(&pernet->ipoib_ps); | |
4479 | idr_destroy(&pernet->ib_ps); | |
4480 | } | |
4481 | ||
4482 | static struct pernet_operations cma_pernet_operations = { | |
4483 | .init = cma_init_net, | |
4484 | .exit = cma_exit_net, | |
4485 | .id = &cma_pernet_id, | |
4486 | .size = sizeof(struct cma_pernet), | |
4487 | }; | |
4488 | ||
716abb1f | 4489 | static int __init cma_init(void) |
e51060f0 | 4490 | { |
5d7220e8 | 4491 | int ret; |
227b60f5 | 4492 | |
dee9acbb | 4493 | cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM); |
e51060f0 SH |
4494 | if (!cma_wq) |
4495 | return -ENOMEM; | |
4496 | ||
4be74b42 HE |
4497 | ret = register_pernet_subsys(&cma_pernet_operations); |
4498 | if (ret) | |
4499 | goto err_wq; | |
4500 | ||
c1a0b23b | 4501 | ib_sa_register_client(&sa_client); |
7a118df3 | 4502 | rdma_addr_register_client(&addr_client); |
dd5bdff8 | 4503 | register_netdevice_notifier(&cma_nb); |
c1a0b23b | 4504 | |
e51060f0 SH |
4505 | ret = ib_register_client(&cma_client); |
4506 | if (ret) | |
4507 | goto err; | |
753f618a | 4508 | |
2fa2d4fb MB |
4509 | if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table), |
4510 | cma_cb_table)) | |
aba25a3e | 4511 | pr_warn("RDMA CMA: failed to add netlink callback\n"); |
045959db | 4512 | cma_configfs_init(); |
753f618a | 4513 | |
e51060f0 SH |
4514 | return 0; |
4515 | ||
4516 | err: | |
dd5bdff8 | 4517 | unregister_netdevice_notifier(&cma_nb); |
7a118df3 | 4518 | rdma_addr_unregister_client(&addr_client); |
c1a0b23b | 4519 | ib_sa_unregister_client(&sa_client); |
4be74b42 | 4520 | err_wq: |
e51060f0 SH |
4521 | destroy_workqueue(cma_wq); |
4522 | return ret; | |
4523 | } | |
4524 | ||
716abb1f | 4525 | static void __exit cma_cleanup(void) |
e51060f0 | 4526 | { |
045959db | 4527 | cma_configfs_exit(); |
753f618a | 4528 | ibnl_remove_client(RDMA_NL_RDMA_CM); |
e51060f0 | 4529 | ib_unregister_client(&cma_client); |
dd5bdff8 | 4530 | unregister_netdevice_notifier(&cma_nb); |
7a118df3 | 4531 | rdma_addr_unregister_client(&addr_client); |
c1a0b23b | 4532 | ib_sa_unregister_client(&sa_client); |
4be74b42 | 4533 | unregister_pernet_subsys(&cma_pernet_operations); |
e51060f0 | 4534 | destroy_workqueue(cma_wq); |
e51060f0 SH |
4535 | } |
4536 | ||
4537 | module_init(cma_init); | |
4538 | module_exit(cma_cleanup); |