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