gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / infiniband / core / cma.c
blob26e6f7df247b6bba8ea23f24bb36eb73edd618c6
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
4 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
5 * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
7 */
9 #include <linux/completion.h>
10 #include <linux/in.h>
11 #include <linux/in6.h>
12 #include <linux/mutex.h>
13 #include <linux/random.h>
14 #include <linux/igmp.h>
15 #include <linux/xarray.h>
16 #include <linux/inetdevice.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <net/route.h>
21 #include <net/net_namespace.h>
22 #include <net/netns/generic.h>
23 #include <net/tcp.h>
24 #include <net/ipv6.h>
25 #include <net/ip_fib.h>
26 #include <net/ip6_route.h>
28 #include <rdma/rdma_cm.h>
29 #include <rdma/rdma_cm_ib.h>
30 #include <rdma/rdma_netlink.h>
31 #include <rdma/ib.h>
32 #include <rdma/ib_cache.h>
33 #include <rdma/ib_cm.h>
34 #include <rdma/ib_sa.h>
35 #include <rdma/iw_cm.h>
37 #include "core_priv.h"
38 #include "cma_priv.h"
39 #include "cma_trace.h"
41 MODULE_AUTHOR("Sean Hefty");
42 MODULE_DESCRIPTION("Generic RDMA CM Agent");
43 MODULE_LICENSE("Dual BSD/GPL");
45 #define CMA_CM_RESPONSE_TIMEOUT 20
46 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
47 #define CMA_MAX_CM_RETRIES 15
48 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
49 #define CMA_IBOE_PACKET_LIFETIME 18
50 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
52 static const char * const cma_events[] = {
53 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
54 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
55 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
56 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
57 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
58 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
59 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
60 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
61 [RDMA_CM_EVENT_REJECTED] = "rejected",
62 [RDMA_CM_EVENT_ESTABLISHED] = "established",
63 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
64 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
65 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
66 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
67 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
68 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
71 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
73 size_t index = event;
75 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
76 cma_events[index] : "unrecognized event";
78 EXPORT_SYMBOL(rdma_event_msg);
80 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
81 int reason)
83 if (rdma_ib_or_roce(id->device, id->port_num))
84 return ibcm_reject_msg(reason);
86 if (rdma_protocol_iwarp(id->device, id->port_num))
87 return iwcm_reject_msg(reason);
89 WARN_ON_ONCE(1);
90 return "unrecognized transport";
92 EXPORT_SYMBOL(rdma_reject_msg);
94 bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
96 if (rdma_ib_or_roce(id->device, id->port_num))
97 return reason == IB_CM_REJ_CONSUMER_DEFINED;
99 if (rdma_protocol_iwarp(id->device, id->port_num))
100 return reason == -ECONNREFUSED;
102 WARN_ON_ONCE(1);
103 return false;
105 EXPORT_SYMBOL(rdma_is_consumer_reject);
107 const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
108 struct rdma_cm_event *ev, u8 *data_len)
110 const void *p;
112 if (rdma_is_consumer_reject(id, ev->status)) {
113 *data_len = ev->param.conn.private_data_len;
114 p = ev->param.conn.private_data;
115 } else {
116 *data_len = 0;
117 p = NULL;
119 return p;
121 EXPORT_SYMBOL(rdma_consumer_reject_data);
124 * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
125 * @id: Communication Identifier
127 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
129 struct rdma_id_private *id_priv;
131 id_priv = container_of(id, struct rdma_id_private, id);
132 if (id->device->node_type == RDMA_NODE_RNIC)
133 return id_priv->cm_id.iw;
134 return NULL;
136 EXPORT_SYMBOL(rdma_iw_cm_id);
139 * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
140 * @res: rdma resource tracking entry pointer
142 struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
144 struct rdma_id_private *id_priv =
145 container_of(res, struct rdma_id_private, res);
147 return &id_priv->id;
149 EXPORT_SYMBOL(rdma_res_to_id);
151 static void cma_add_one(struct ib_device *device);
152 static void cma_remove_one(struct ib_device *device, void *client_data);
154 static struct ib_client cma_client = {
155 .name = "cma",
156 .add = cma_add_one,
157 .remove = cma_remove_one
160 static struct ib_sa_client sa_client;
161 static LIST_HEAD(dev_list);
162 static LIST_HEAD(listen_any_list);
163 static DEFINE_MUTEX(lock);
164 static struct workqueue_struct *cma_wq;
165 static unsigned int cma_pernet_id;
167 struct cma_pernet {
168 struct xarray tcp_ps;
169 struct xarray udp_ps;
170 struct xarray ipoib_ps;
171 struct xarray ib_ps;
174 static struct cma_pernet *cma_pernet(struct net *net)
176 return net_generic(net, cma_pernet_id);
179 static
180 struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
182 struct cma_pernet *pernet = cma_pernet(net);
184 switch (ps) {
185 case RDMA_PS_TCP:
186 return &pernet->tcp_ps;
187 case RDMA_PS_UDP:
188 return &pernet->udp_ps;
189 case RDMA_PS_IPOIB:
190 return &pernet->ipoib_ps;
191 case RDMA_PS_IB:
192 return &pernet->ib_ps;
193 default:
194 return NULL;
198 struct cma_device {
199 struct list_head list;
200 struct ib_device *device;
201 struct completion comp;
202 refcount_t refcount;
203 struct list_head id_list;
204 enum ib_gid_type *default_gid_type;
205 u8 *default_roce_tos;
208 struct rdma_bind_list {
209 enum rdma_ucm_port_space ps;
210 struct hlist_head owners;
211 unsigned short port;
214 struct class_port_info_context {
215 struct ib_class_port_info *class_port_info;
216 struct ib_device *device;
217 struct completion done;
218 struct ib_sa_query *sa_query;
219 u8 port_num;
222 static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
223 struct rdma_bind_list *bind_list, int snum)
225 struct xarray *xa = cma_pernet_xa(net, ps);
227 return xa_insert(xa, snum, bind_list, GFP_KERNEL);
230 static struct rdma_bind_list *cma_ps_find(struct net *net,
231 enum rdma_ucm_port_space ps, int snum)
233 struct xarray *xa = cma_pernet_xa(net, ps);
235 return xa_load(xa, snum);
238 static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
239 int snum)
241 struct xarray *xa = cma_pernet_xa(net, ps);
243 xa_erase(xa, snum);
246 enum {
247 CMA_OPTION_AFONLY,
250 void cma_dev_get(struct cma_device *cma_dev)
252 refcount_inc(&cma_dev->refcount);
255 void cma_dev_put(struct cma_device *cma_dev)
257 if (refcount_dec_and_test(&cma_dev->refcount))
258 complete(&cma_dev->comp);
261 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
262 void *cookie)
264 struct cma_device *cma_dev;
265 struct cma_device *found_cma_dev = NULL;
267 mutex_lock(&lock);
269 list_for_each_entry(cma_dev, &dev_list, list)
270 if (filter(cma_dev->device, cookie)) {
271 found_cma_dev = cma_dev;
272 break;
275 if (found_cma_dev)
276 cma_dev_get(found_cma_dev);
277 mutex_unlock(&lock);
278 return found_cma_dev;
281 int cma_get_default_gid_type(struct cma_device *cma_dev,
282 unsigned int port)
284 if (!rdma_is_port_valid(cma_dev->device, port))
285 return -EINVAL;
287 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
290 int cma_set_default_gid_type(struct cma_device *cma_dev,
291 unsigned int port,
292 enum ib_gid_type default_gid_type)
294 unsigned long supported_gids;
296 if (!rdma_is_port_valid(cma_dev->device, port))
297 return -EINVAL;
299 supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
301 if (!(supported_gids & 1 << default_gid_type))
302 return -EINVAL;
304 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
305 default_gid_type;
307 return 0;
310 int cma_get_default_roce_tos(struct cma_device *cma_dev, unsigned int port)
312 if (!rdma_is_port_valid(cma_dev->device, port))
313 return -EINVAL;
315 return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
318 int cma_set_default_roce_tos(struct cma_device *cma_dev, unsigned int port,
319 u8 default_roce_tos)
321 if (!rdma_is_port_valid(cma_dev->device, port))
322 return -EINVAL;
324 cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
325 default_roce_tos;
327 return 0;
329 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
331 return cma_dev->device;
335 * Device removal can occur at anytime, so we need extra handling to
336 * serialize notifying the user of device removal with other callbacks.
337 * We do this by disabling removal notification while a callback is in process,
338 * and reporting it after the callback completes.
341 struct cma_multicast {
342 struct rdma_id_private *id_priv;
343 union {
344 struct ib_sa_multicast *ib;
345 } multicast;
346 struct list_head list;
347 void *context;
348 struct sockaddr_storage addr;
349 struct kref mcref;
350 u8 join_state;
353 struct cma_work {
354 struct work_struct work;
355 struct rdma_id_private *id;
356 enum rdma_cm_state old_state;
357 enum rdma_cm_state new_state;
358 struct rdma_cm_event event;
361 struct cma_ndev_work {
362 struct work_struct work;
363 struct rdma_id_private *id;
364 struct rdma_cm_event event;
367 struct iboe_mcast_work {
368 struct work_struct work;
369 struct rdma_id_private *id;
370 struct cma_multicast *mc;
373 union cma_ip_addr {
374 struct in6_addr ip6;
375 struct {
376 __be32 pad[3];
377 __be32 addr;
378 } ip4;
381 struct cma_hdr {
382 u8 cma_version;
383 u8 ip_version; /* IP version: 7:4 */
384 __be16 port;
385 union cma_ip_addr src_addr;
386 union cma_ip_addr dst_addr;
389 #define CMA_VERSION 0x00
391 struct cma_req_info {
392 struct sockaddr_storage listen_addr_storage;
393 struct sockaddr_storage src_addr_storage;
394 struct ib_device *device;
395 union ib_gid local_gid;
396 __be64 service_id;
397 int port;
398 bool has_gid;
399 u16 pkey;
402 static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
404 unsigned long flags;
405 int ret;
407 spin_lock_irqsave(&id_priv->lock, flags);
408 ret = (id_priv->state == comp);
409 spin_unlock_irqrestore(&id_priv->lock, flags);
410 return ret;
413 static int cma_comp_exch(struct rdma_id_private *id_priv,
414 enum rdma_cm_state comp, enum rdma_cm_state exch)
416 unsigned long flags;
417 int ret;
419 spin_lock_irqsave(&id_priv->lock, flags);
420 if ((ret = (id_priv->state == comp)))
421 id_priv->state = exch;
422 spin_unlock_irqrestore(&id_priv->lock, flags);
423 return ret;
426 static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
427 enum rdma_cm_state exch)
429 unsigned long flags;
430 enum rdma_cm_state old;
432 spin_lock_irqsave(&id_priv->lock, flags);
433 old = id_priv->state;
434 id_priv->state = exch;
435 spin_unlock_irqrestore(&id_priv->lock, flags);
436 return old;
439 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
441 return hdr->ip_version >> 4;
444 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
446 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
449 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
451 struct in_device *in_dev = NULL;
453 if (ndev) {
454 rtnl_lock();
455 in_dev = __in_dev_get_rtnl(ndev);
456 if (in_dev) {
457 if (join)
458 ip_mc_inc_group(in_dev,
459 *(__be32 *)(mgid->raw + 12));
460 else
461 ip_mc_dec_group(in_dev,
462 *(__be32 *)(mgid->raw + 12));
464 rtnl_unlock();
466 return (in_dev) ? 0 : -ENODEV;
469 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
470 struct cma_device *cma_dev)
472 cma_dev_get(cma_dev);
473 id_priv->cma_dev = cma_dev;
474 id_priv->id.device = cma_dev->device;
475 id_priv->id.route.addr.dev_addr.transport =
476 rdma_node_get_transport(cma_dev->device->node_type);
477 list_add_tail(&id_priv->list, &cma_dev->id_list);
478 if (id_priv->res.kern_name)
479 rdma_restrack_kadd(&id_priv->res);
480 else
481 rdma_restrack_uadd(&id_priv->res);
484 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
485 struct cma_device *cma_dev)
487 _cma_attach_to_dev(id_priv, cma_dev);
488 id_priv->gid_type =
489 cma_dev->default_gid_type[id_priv->id.port_num -
490 rdma_start_port(cma_dev->device)];
493 static inline void release_mc(struct kref *kref)
495 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
497 kfree(mc->multicast.ib);
498 kfree(mc);
501 static void cma_release_dev(struct rdma_id_private *id_priv)
503 mutex_lock(&lock);
504 list_del(&id_priv->list);
505 cma_dev_put(id_priv->cma_dev);
506 id_priv->cma_dev = NULL;
507 mutex_unlock(&lock);
510 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
512 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
515 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
517 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
520 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
522 return id_priv->id.route.addr.src_addr.ss_family;
525 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
527 struct ib_sa_mcmember_rec rec;
528 int ret = 0;
530 if (id_priv->qkey) {
531 if (qkey && id_priv->qkey != qkey)
532 return -EINVAL;
533 return 0;
536 if (qkey) {
537 id_priv->qkey = qkey;
538 return 0;
541 switch (id_priv->id.ps) {
542 case RDMA_PS_UDP:
543 case RDMA_PS_IB:
544 id_priv->qkey = RDMA_UDP_QKEY;
545 break;
546 case RDMA_PS_IPOIB:
547 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
548 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
549 id_priv->id.port_num, &rec.mgid,
550 &rec);
551 if (!ret)
552 id_priv->qkey = be32_to_cpu(rec.qkey);
553 break;
554 default:
555 break;
557 return ret;
560 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
562 dev_addr->dev_type = ARPHRD_INFINIBAND;
563 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
564 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
567 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
569 int ret;
571 if (addr->sa_family != AF_IB) {
572 ret = rdma_translate_ip(addr, dev_addr);
573 } else {
574 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
575 ret = 0;
578 return ret;
581 static const struct ib_gid_attr *
582 cma_validate_port(struct ib_device *device, u8 port,
583 enum ib_gid_type gid_type,
584 union ib_gid *gid,
585 struct rdma_id_private *id_priv)
587 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
588 int bound_if_index = dev_addr->bound_dev_if;
589 const struct ib_gid_attr *sgid_attr;
590 int dev_type = dev_addr->dev_type;
591 struct net_device *ndev = NULL;
593 if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
594 return ERR_PTR(-ENODEV);
596 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
597 return ERR_PTR(-ENODEV);
599 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
600 return ERR_PTR(-ENODEV);
602 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
603 ndev = dev_get_by_index(dev_addr->net, bound_if_index);
604 if (!ndev)
605 return ERR_PTR(-ENODEV);
606 } else {
607 gid_type = IB_GID_TYPE_IB;
610 sgid_attr = rdma_find_gid_by_port(device, gid, gid_type, port, ndev);
611 if (ndev)
612 dev_put(ndev);
613 return sgid_attr;
616 static void cma_bind_sgid_attr(struct rdma_id_private *id_priv,
617 const struct ib_gid_attr *sgid_attr)
619 WARN_ON(id_priv->id.route.addr.dev_addr.sgid_attr);
620 id_priv->id.route.addr.dev_addr.sgid_attr = sgid_attr;
624 * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
625 * based on source ip address.
626 * @id_priv: cm_id which should be bound to cma device
628 * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
629 * based on source IP address. It returns 0 on success or error code otherwise.
630 * It is applicable to active and passive side cm_id.
632 static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
634 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
635 const struct ib_gid_attr *sgid_attr;
636 union ib_gid gid, iboe_gid, *gidp;
637 struct cma_device *cma_dev;
638 enum ib_gid_type gid_type;
639 int ret = -ENODEV;
640 unsigned int port;
642 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
643 id_priv->id.ps == RDMA_PS_IPOIB)
644 return -EINVAL;
646 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
647 &iboe_gid);
649 memcpy(&gid, dev_addr->src_dev_addr +
650 rdma_addr_gid_offset(dev_addr), sizeof(gid));
652 mutex_lock(&lock);
653 list_for_each_entry(cma_dev, &dev_list, list) {
654 rdma_for_each_port (cma_dev->device, port) {
655 gidp = rdma_protocol_roce(cma_dev->device, port) ?
656 &iboe_gid : &gid;
657 gid_type = cma_dev->default_gid_type[port - 1];
658 sgid_attr = cma_validate_port(cma_dev->device, port,
659 gid_type, gidp, id_priv);
660 if (!IS_ERR(sgid_attr)) {
661 id_priv->id.port_num = port;
662 cma_bind_sgid_attr(id_priv, sgid_attr);
663 cma_attach_to_dev(id_priv, cma_dev);
664 ret = 0;
665 goto out;
669 out:
670 mutex_unlock(&lock);
671 return ret;
675 * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
676 * @id_priv: cm id to bind to cma device
677 * @listen_id_priv: listener cm id to match against
678 * @req: Pointer to req structure containaining incoming
679 * request information
680 * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
681 * rdma device matches for listen_id and incoming request. It also verifies
682 * that a GID table entry is present for the source address.
683 * Returns 0 on success, or returns error code otherwise.
685 static int cma_ib_acquire_dev(struct rdma_id_private *id_priv,
686 const struct rdma_id_private *listen_id_priv,
687 struct cma_req_info *req)
689 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
690 const struct ib_gid_attr *sgid_attr;
691 enum ib_gid_type gid_type;
692 union ib_gid gid;
694 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
695 id_priv->id.ps == RDMA_PS_IPOIB)
696 return -EINVAL;
698 if (rdma_protocol_roce(req->device, req->port))
699 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
700 &gid);
701 else
702 memcpy(&gid, dev_addr->src_dev_addr +
703 rdma_addr_gid_offset(dev_addr), sizeof(gid));
705 gid_type = listen_id_priv->cma_dev->default_gid_type[req->port - 1];
706 sgid_attr = cma_validate_port(req->device, req->port,
707 gid_type, &gid, id_priv);
708 if (IS_ERR(sgid_attr))
709 return PTR_ERR(sgid_attr);
711 id_priv->id.port_num = req->port;
712 cma_bind_sgid_attr(id_priv, sgid_attr);
713 /* Need to acquire lock to protect against reader
714 * of cma_dev->id_list such as cma_netdev_callback() and
715 * cma_process_remove().
717 mutex_lock(&lock);
718 cma_attach_to_dev(id_priv, listen_id_priv->cma_dev);
719 mutex_unlock(&lock);
720 return 0;
723 static int cma_iw_acquire_dev(struct rdma_id_private *id_priv,
724 const struct rdma_id_private *listen_id_priv)
726 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
727 const struct ib_gid_attr *sgid_attr;
728 struct cma_device *cma_dev;
729 enum ib_gid_type gid_type;
730 int ret = -ENODEV;
731 unsigned int port;
732 union ib_gid gid;
734 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
735 id_priv->id.ps == RDMA_PS_IPOIB)
736 return -EINVAL;
738 memcpy(&gid, dev_addr->src_dev_addr +
739 rdma_addr_gid_offset(dev_addr), sizeof(gid));
741 mutex_lock(&lock);
743 cma_dev = listen_id_priv->cma_dev;
744 port = listen_id_priv->id.port_num;
745 gid_type = listen_id_priv->gid_type;
746 sgid_attr = cma_validate_port(cma_dev->device, port,
747 gid_type, &gid, id_priv);
748 if (!IS_ERR(sgid_attr)) {
749 id_priv->id.port_num = port;
750 cma_bind_sgid_attr(id_priv, sgid_attr);
751 ret = 0;
752 goto out;
755 list_for_each_entry(cma_dev, &dev_list, list) {
756 rdma_for_each_port (cma_dev->device, port) {
757 if (listen_id_priv->cma_dev == cma_dev &&
758 listen_id_priv->id.port_num == port)
759 continue;
761 gid_type = cma_dev->default_gid_type[port - 1];
762 sgid_attr = cma_validate_port(cma_dev->device, port,
763 gid_type, &gid, id_priv);
764 if (!IS_ERR(sgid_attr)) {
765 id_priv->id.port_num = port;
766 cma_bind_sgid_attr(id_priv, sgid_attr);
767 ret = 0;
768 goto out;
773 out:
774 if (!ret)
775 cma_attach_to_dev(id_priv, cma_dev);
777 mutex_unlock(&lock);
778 return ret;
782 * Select the source IB device and address to reach the destination IB address.
784 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
786 struct cma_device *cma_dev, *cur_dev;
787 struct sockaddr_ib *addr;
788 union ib_gid gid, sgid, *dgid;
789 unsigned int p;
790 u16 pkey, index;
791 enum ib_port_state port_state;
792 int i;
794 cma_dev = NULL;
795 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
796 dgid = (union ib_gid *) &addr->sib_addr;
797 pkey = ntohs(addr->sib_pkey);
799 mutex_lock(&lock);
800 list_for_each_entry(cur_dev, &dev_list, list) {
801 rdma_for_each_port (cur_dev->device, p) {
802 if (!rdma_cap_af_ib(cur_dev->device, p))
803 continue;
805 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
806 continue;
808 if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
809 continue;
810 for (i = 0; !rdma_query_gid(cur_dev->device,
811 p, i, &gid);
812 i++) {
813 if (!memcmp(&gid, dgid, sizeof(gid))) {
814 cma_dev = cur_dev;
815 sgid = gid;
816 id_priv->id.port_num = p;
817 goto found;
820 if (!cma_dev && (gid.global.subnet_prefix ==
821 dgid->global.subnet_prefix) &&
822 port_state == IB_PORT_ACTIVE) {
823 cma_dev = cur_dev;
824 sgid = gid;
825 id_priv->id.port_num = p;
826 goto found;
831 mutex_unlock(&lock);
832 return -ENODEV;
834 found:
835 cma_attach_to_dev(id_priv, cma_dev);
836 mutex_unlock(&lock);
837 addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
838 memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
839 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
840 return 0;
843 static void cma_id_get(struct rdma_id_private *id_priv)
845 refcount_inc(&id_priv->refcount);
848 static void cma_id_put(struct rdma_id_private *id_priv)
850 if (refcount_dec_and_test(&id_priv->refcount))
851 complete(&id_priv->comp);
854 struct rdma_cm_id *__rdma_create_id(struct net *net,
855 rdma_cm_event_handler event_handler,
856 void *context, enum rdma_ucm_port_space ps,
857 enum ib_qp_type qp_type, const char *caller)
859 struct rdma_id_private *id_priv;
861 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
862 if (!id_priv)
863 return ERR_PTR(-ENOMEM);
865 rdma_restrack_set_task(&id_priv->res, caller);
866 id_priv->res.type = RDMA_RESTRACK_CM_ID;
867 id_priv->state = RDMA_CM_IDLE;
868 id_priv->id.context = context;
869 id_priv->id.event_handler = event_handler;
870 id_priv->id.ps = ps;
871 id_priv->id.qp_type = qp_type;
872 id_priv->tos_set = false;
873 id_priv->timeout_set = false;
874 id_priv->gid_type = IB_GID_TYPE_IB;
875 spin_lock_init(&id_priv->lock);
876 mutex_init(&id_priv->qp_mutex);
877 init_completion(&id_priv->comp);
878 refcount_set(&id_priv->refcount, 1);
879 mutex_init(&id_priv->handler_mutex);
880 INIT_LIST_HEAD(&id_priv->listen_list);
881 INIT_LIST_HEAD(&id_priv->mc_list);
882 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
883 id_priv->id.route.addr.dev_addr.net = get_net(net);
884 id_priv->seq_num &= 0x00ffffff;
886 trace_cm_id_create(id_priv);
887 return &id_priv->id;
889 EXPORT_SYMBOL(__rdma_create_id);
891 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
893 struct ib_qp_attr qp_attr;
894 int qp_attr_mask, ret;
896 qp_attr.qp_state = IB_QPS_INIT;
897 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
898 if (ret)
899 return ret;
901 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
902 if (ret)
903 return ret;
905 qp_attr.qp_state = IB_QPS_RTR;
906 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
907 if (ret)
908 return ret;
910 qp_attr.qp_state = IB_QPS_RTS;
911 qp_attr.sq_psn = 0;
912 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
914 return ret;
917 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
919 struct ib_qp_attr qp_attr;
920 int qp_attr_mask, ret;
922 qp_attr.qp_state = IB_QPS_INIT;
923 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
924 if (ret)
925 return ret;
927 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
930 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
931 struct ib_qp_init_attr *qp_init_attr)
933 struct rdma_id_private *id_priv;
934 struct ib_qp *qp;
935 int ret;
937 id_priv = container_of(id, struct rdma_id_private, id);
938 if (id->device != pd->device) {
939 ret = -EINVAL;
940 goto out_err;
943 qp_init_attr->port_num = id->port_num;
944 qp = ib_create_qp(pd, qp_init_attr);
945 if (IS_ERR(qp)) {
946 ret = PTR_ERR(qp);
947 goto out_err;
950 if (id->qp_type == IB_QPT_UD)
951 ret = cma_init_ud_qp(id_priv, qp);
952 else
953 ret = cma_init_conn_qp(id_priv, qp);
954 if (ret)
955 goto out_destroy;
957 id->qp = qp;
958 id_priv->qp_num = qp->qp_num;
959 id_priv->srq = (qp->srq != NULL);
960 trace_cm_qp_create(id_priv, pd, qp_init_attr, 0);
961 return 0;
962 out_destroy:
963 ib_destroy_qp(qp);
964 out_err:
965 trace_cm_qp_create(id_priv, pd, qp_init_attr, ret);
966 return ret;
968 EXPORT_SYMBOL(rdma_create_qp);
970 void rdma_destroy_qp(struct rdma_cm_id *id)
972 struct rdma_id_private *id_priv;
974 id_priv = container_of(id, struct rdma_id_private, id);
975 trace_cm_qp_destroy(id_priv);
976 mutex_lock(&id_priv->qp_mutex);
977 ib_destroy_qp(id_priv->id.qp);
978 id_priv->id.qp = NULL;
979 mutex_unlock(&id_priv->qp_mutex);
981 EXPORT_SYMBOL(rdma_destroy_qp);
983 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
984 struct rdma_conn_param *conn_param)
986 struct ib_qp_attr qp_attr;
987 int qp_attr_mask, ret;
989 mutex_lock(&id_priv->qp_mutex);
990 if (!id_priv->id.qp) {
991 ret = 0;
992 goto out;
995 /* Need to update QP attributes from default values. */
996 qp_attr.qp_state = IB_QPS_INIT;
997 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
998 if (ret)
999 goto out;
1001 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1002 if (ret)
1003 goto out;
1005 qp_attr.qp_state = IB_QPS_RTR;
1006 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1007 if (ret)
1008 goto out;
1010 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
1012 if (conn_param)
1013 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
1014 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1015 out:
1016 mutex_unlock(&id_priv->qp_mutex);
1017 return ret;
1020 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
1021 struct rdma_conn_param *conn_param)
1023 struct ib_qp_attr qp_attr;
1024 int qp_attr_mask, ret;
1026 mutex_lock(&id_priv->qp_mutex);
1027 if (!id_priv->id.qp) {
1028 ret = 0;
1029 goto out;
1032 qp_attr.qp_state = IB_QPS_RTS;
1033 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1034 if (ret)
1035 goto out;
1037 if (conn_param)
1038 qp_attr.max_rd_atomic = conn_param->initiator_depth;
1039 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1040 out:
1041 mutex_unlock(&id_priv->qp_mutex);
1042 return ret;
1045 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
1047 struct ib_qp_attr qp_attr;
1048 int ret;
1050 mutex_lock(&id_priv->qp_mutex);
1051 if (!id_priv->id.qp) {
1052 ret = 0;
1053 goto out;
1056 qp_attr.qp_state = IB_QPS_ERR;
1057 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
1058 out:
1059 mutex_unlock(&id_priv->qp_mutex);
1060 return ret;
1063 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
1064 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1066 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1067 int ret;
1068 u16 pkey;
1070 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
1071 pkey = 0xffff;
1072 else
1073 pkey = ib_addr_get_pkey(dev_addr);
1075 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
1076 pkey, &qp_attr->pkey_index);
1077 if (ret)
1078 return ret;
1080 qp_attr->port_num = id_priv->id.port_num;
1081 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1083 if (id_priv->id.qp_type == IB_QPT_UD) {
1084 ret = cma_set_qkey(id_priv, 0);
1085 if (ret)
1086 return ret;
1088 qp_attr->qkey = id_priv->qkey;
1089 *qp_attr_mask |= IB_QP_QKEY;
1090 } else {
1091 qp_attr->qp_access_flags = 0;
1092 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1094 return 0;
1097 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1098 int *qp_attr_mask)
1100 struct rdma_id_private *id_priv;
1101 int ret = 0;
1103 id_priv = container_of(id, struct rdma_id_private, id);
1104 if (rdma_cap_ib_cm(id->device, id->port_num)) {
1105 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1106 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1107 else
1108 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1109 qp_attr_mask);
1111 if (qp_attr->qp_state == IB_QPS_RTR)
1112 qp_attr->rq_psn = id_priv->seq_num;
1113 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1114 if (!id_priv->cm_id.iw) {
1115 qp_attr->qp_access_flags = 0;
1116 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1117 } else
1118 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1119 qp_attr_mask);
1120 qp_attr->port_num = id_priv->id.port_num;
1121 *qp_attr_mask |= IB_QP_PORT;
1122 } else
1123 ret = -ENOSYS;
1125 if ((*qp_attr_mask & IB_QP_TIMEOUT) && id_priv->timeout_set)
1126 qp_attr->timeout = id_priv->timeout;
1128 return ret;
1130 EXPORT_SYMBOL(rdma_init_qp_attr);
1132 static inline bool cma_zero_addr(const struct sockaddr *addr)
1134 switch (addr->sa_family) {
1135 case AF_INET:
1136 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1137 case AF_INET6:
1138 return ipv6_addr_any(&((struct sockaddr_in6 *)addr)->sin6_addr);
1139 case AF_IB:
1140 return ib_addr_any(&((struct sockaddr_ib *)addr)->sib_addr);
1141 default:
1142 return false;
1146 static inline bool cma_loopback_addr(const struct sockaddr *addr)
1148 switch (addr->sa_family) {
1149 case AF_INET:
1150 return ipv4_is_loopback(
1151 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
1152 case AF_INET6:
1153 return ipv6_addr_loopback(
1154 &((struct sockaddr_in6 *)addr)->sin6_addr);
1155 case AF_IB:
1156 return ib_addr_loopback(
1157 &((struct sockaddr_ib *)addr)->sib_addr);
1158 default:
1159 return false;
1163 static inline bool cma_any_addr(const struct sockaddr *addr)
1165 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1168 static int cma_addr_cmp(const struct sockaddr *src, const struct sockaddr *dst)
1170 if (src->sa_family != dst->sa_family)
1171 return -1;
1173 switch (src->sa_family) {
1174 case AF_INET:
1175 return ((struct sockaddr_in *)src)->sin_addr.s_addr !=
1176 ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1177 case AF_INET6: {
1178 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src;
1179 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst;
1180 bool link_local;
1182 if (ipv6_addr_cmp(&src_addr6->sin6_addr,
1183 &dst_addr6->sin6_addr))
1184 return 1;
1185 link_local = ipv6_addr_type(&dst_addr6->sin6_addr) &
1186 IPV6_ADDR_LINKLOCAL;
1187 /* Link local must match their scope_ids */
1188 return link_local ? (src_addr6->sin6_scope_id !=
1189 dst_addr6->sin6_scope_id) :
1193 default:
1194 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1195 &((struct sockaddr_ib *) dst)->sib_addr);
1199 static __be16 cma_port(const struct sockaddr *addr)
1201 struct sockaddr_ib *sib;
1203 switch (addr->sa_family) {
1204 case AF_INET:
1205 return ((struct sockaddr_in *) addr)->sin_port;
1206 case AF_INET6:
1207 return ((struct sockaddr_in6 *) addr)->sin6_port;
1208 case AF_IB:
1209 sib = (struct sockaddr_ib *) addr;
1210 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1211 be64_to_cpu(sib->sib_sid_mask)));
1212 default:
1213 return 0;
1217 static inline int cma_any_port(const struct sockaddr *addr)
1219 return !cma_port(addr);
1222 static void cma_save_ib_info(struct sockaddr *src_addr,
1223 struct sockaddr *dst_addr,
1224 const struct rdma_cm_id *listen_id,
1225 const struct sa_path_rec *path)
1227 struct sockaddr_ib *listen_ib, *ib;
1229 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1230 if (src_addr) {
1231 ib = (struct sockaddr_ib *)src_addr;
1232 ib->sib_family = AF_IB;
1233 if (path) {
1234 ib->sib_pkey = path->pkey;
1235 ib->sib_flowinfo = path->flow_label;
1236 memcpy(&ib->sib_addr, &path->sgid, 16);
1237 ib->sib_sid = path->service_id;
1238 ib->sib_scope_id = 0;
1239 } else {
1240 ib->sib_pkey = listen_ib->sib_pkey;
1241 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1242 ib->sib_addr = listen_ib->sib_addr;
1243 ib->sib_sid = listen_ib->sib_sid;
1244 ib->sib_scope_id = listen_ib->sib_scope_id;
1246 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1248 if (dst_addr) {
1249 ib = (struct sockaddr_ib *)dst_addr;
1250 ib->sib_family = AF_IB;
1251 if (path) {
1252 ib->sib_pkey = path->pkey;
1253 ib->sib_flowinfo = path->flow_label;
1254 memcpy(&ib->sib_addr, &path->dgid, 16);
1259 static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1260 struct sockaddr_in *dst_addr,
1261 struct cma_hdr *hdr,
1262 __be16 local_port)
1264 if (src_addr) {
1265 *src_addr = (struct sockaddr_in) {
1266 .sin_family = AF_INET,
1267 .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1268 .sin_port = local_port,
1272 if (dst_addr) {
1273 *dst_addr = (struct sockaddr_in) {
1274 .sin_family = AF_INET,
1275 .sin_addr.s_addr = hdr->src_addr.ip4.addr,
1276 .sin_port = hdr->port,
1281 static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1282 struct sockaddr_in6 *dst_addr,
1283 struct cma_hdr *hdr,
1284 __be16 local_port)
1286 if (src_addr) {
1287 *src_addr = (struct sockaddr_in6) {
1288 .sin6_family = AF_INET6,
1289 .sin6_addr = hdr->dst_addr.ip6,
1290 .sin6_port = local_port,
1294 if (dst_addr) {
1295 *dst_addr = (struct sockaddr_in6) {
1296 .sin6_family = AF_INET6,
1297 .sin6_addr = hdr->src_addr.ip6,
1298 .sin6_port = hdr->port,
1303 static u16 cma_port_from_service_id(__be64 service_id)
1305 return (u16)be64_to_cpu(service_id);
1308 static int cma_save_ip_info(struct sockaddr *src_addr,
1309 struct sockaddr *dst_addr,
1310 const struct ib_cm_event *ib_event,
1311 __be64 service_id)
1313 struct cma_hdr *hdr;
1314 __be16 port;
1316 hdr = ib_event->private_data;
1317 if (hdr->cma_version != CMA_VERSION)
1318 return -EINVAL;
1320 port = htons(cma_port_from_service_id(service_id));
1322 switch (cma_get_ip_ver(hdr)) {
1323 case 4:
1324 cma_save_ip4_info((struct sockaddr_in *)src_addr,
1325 (struct sockaddr_in *)dst_addr, hdr, port);
1326 break;
1327 case 6:
1328 cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1329 (struct sockaddr_in6 *)dst_addr, hdr, port);
1330 break;
1331 default:
1332 return -EAFNOSUPPORT;
1335 return 0;
1338 static int cma_save_net_info(struct sockaddr *src_addr,
1339 struct sockaddr *dst_addr,
1340 const struct rdma_cm_id *listen_id,
1341 const struct ib_cm_event *ib_event,
1342 sa_family_t sa_family, __be64 service_id)
1344 if (sa_family == AF_IB) {
1345 if (ib_event->event == IB_CM_REQ_RECEIVED)
1346 cma_save_ib_info(src_addr, dst_addr, listen_id,
1347 ib_event->param.req_rcvd.primary_path);
1348 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1349 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1350 return 0;
1353 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1356 static int cma_save_req_info(const struct ib_cm_event *ib_event,
1357 struct cma_req_info *req)
1359 const struct ib_cm_req_event_param *req_param =
1360 &ib_event->param.req_rcvd;
1361 const struct ib_cm_sidr_req_event_param *sidr_param =
1362 &ib_event->param.sidr_req_rcvd;
1364 switch (ib_event->event) {
1365 case IB_CM_REQ_RECEIVED:
1366 req->device = req_param->listen_id->device;
1367 req->port = req_param->port;
1368 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1369 sizeof(req->local_gid));
1370 req->has_gid = true;
1371 req->service_id = req_param->primary_path->service_id;
1372 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
1373 if (req->pkey != req_param->bth_pkey)
1374 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1375 "RDMA CMA: in the future this may cause the request to be dropped\n",
1376 req_param->bth_pkey, req->pkey);
1377 break;
1378 case IB_CM_SIDR_REQ_RECEIVED:
1379 req->device = sidr_param->listen_id->device;
1380 req->port = sidr_param->port;
1381 req->has_gid = false;
1382 req->service_id = sidr_param->service_id;
1383 req->pkey = sidr_param->pkey;
1384 if (req->pkey != sidr_param->bth_pkey)
1385 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1386 "RDMA CMA: in the future this may cause the request to be dropped\n",
1387 sidr_param->bth_pkey, req->pkey);
1388 break;
1389 default:
1390 return -EINVAL;
1393 return 0;
1396 static bool validate_ipv4_net_dev(struct net_device *net_dev,
1397 const struct sockaddr_in *dst_addr,
1398 const struct sockaddr_in *src_addr)
1400 __be32 daddr = dst_addr->sin_addr.s_addr,
1401 saddr = src_addr->sin_addr.s_addr;
1402 struct fib_result res;
1403 struct flowi4 fl4;
1404 int err;
1405 bool ret;
1407 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1408 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1409 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1410 ipv4_is_loopback(saddr))
1411 return false;
1413 memset(&fl4, 0, sizeof(fl4));
1414 fl4.flowi4_iif = net_dev->ifindex;
1415 fl4.daddr = daddr;
1416 fl4.saddr = saddr;
1418 rcu_read_lock();
1419 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1420 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1421 rcu_read_unlock();
1423 return ret;
1426 static bool validate_ipv6_net_dev(struct net_device *net_dev,
1427 const struct sockaddr_in6 *dst_addr,
1428 const struct sockaddr_in6 *src_addr)
1430 #if IS_ENABLED(CONFIG_IPV6)
1431 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1432 IPV6_ADDR_LINKLOCAL;
1433 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1434 &src_addr->sin6_addr, net_dev->ifindex,
1435 NULL, strict);
1436 bool ret;
1438 if (!rt)
1439 return false;
1441 ret = rt->rt6i_idev->dev == net_dev;
1442 ip6_rt_put(rt);
1444 return ret;
1445 #else
1446 return false;
1447 #endif
1450 static bool validate_net_dev(struct net_device *net_dev,
1451 const struct sockaddr *daddr,
1452 const struct sockaddr *saddr)
1454 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1455 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1456 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1457 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1459 switch (daddr->sa_family) {
1460 case AF_INET:
1461 return saddr->sa_family == AF_INET &&
1462 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1464 case AF_INET6:
1465 return saddr->sa_family == AF_INET6 &&
1466 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1468 default:
1469 return false;
1473 static struct net_device *
1474 roce_get_net_dev_by_cm_event(const struct ib_cm_event *ib_event)
1476 const struct ib_gid_attr *sgid_attr = NULL;
1477 struct net_device *ndev;
1479 if (ib_event->event == IB_CM_REQ_RECEIVED)
1480 sgid_attr = ib_event->param.req_rcvd.ppath_sgid_attr;
1481 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1482 sgid_attr = ib_event->param.sidr_req_rcvd.sgid_attr;
1484 if (!sgid_attr)
1485 return NULL;
1487 rcu_read_lock();
1488 ndev = rdma_read_gid_attr_ndev_rcu(sgid_attr);
1489 if (IS_ERR(ndev))
1490 ndev = NULL;
1491 else
1492 dev_hold(ndev);
1493 rcu_read_unlock();
1494 return ndev;
1497 static struct net_device *cma_get_net_dev(const struct ib_cm_event *ib_event,
1498 struct cma_req_info *req)
1500 struct sockaddr *listen_addr =
1501 (struct sockaddr *)&req->listen_addr_storage;
1502 struct sockaddr *src_addr = (struct sockaddr *)&req->src_addr_storage;
1503 struct net_device *net_dev;
1504 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1505 int err;
1507 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1508 req->service_id);
1509 if (err)
1510 return ERR_PTR(err);
1512 if (rdma_protocol_roce(req->device, req->port))
1513 net_dev = roce_get_net_dev_by_cm_event(ib_event);
1514 else
1515 net_dev = ib_get_net_dev_by_params(req->device, req->port,
1516 req->pkey,
1517 gid, listen_addr);
1518 if (!net_dev)
1519 return ERR_PTR(-ENODEV);
1521 return net_dev;
1524 static enum rdma_ucm_port_space rdma_ps_from_service_id(__be64 service_id)
1526 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1529 static bool cma_match_private_data(struct rdma_id_private *id_priv,
1530 const struct cma_hdr *hdr)
1532 struct sockaddr *addr = cma_src_addr(id_priv);
1533 __be32 ip4_addr;
1534 struct in6_addr ip6_addr;
1536 if (cma_any_addr(addr) && !id_priv->afonly)
1537 return true;
1539 switch (addr->sa_family) {
1540 case AF_INET:
1541 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1542 if (cma_get_ip_ver(hdr) != 4)
1543 return false;
1544 if (!cma_any_addr(addr) &&
1545 hdr->dst_addr.ip4.addr != ip4_addr)
1546 return false;
1547 break;
1548 case AF_INET6:
1549 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1550 if (cma_get_ip_ver(hdr) != 6)
1551 return false;
1552 if (!cma_any_addr(addr) &&
1553 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1554 return false;
1555 break;
1556 case AF_IB:
1557 return true;
1558 default:
1559 return false;
1562 return true;
1565 static bool cma_protocol_roce(const struct rdma_cm_id *id)
1567 struct ib_device *device = id->device;
1568 const int port_num = id->port_num ?: rdma_start_port(device);
1570 return rdma_protocol_roce(device, port_num);
1573 static bool cma_is_req_ipv6_ll(const struct cma_req_info *req)
1575 const struct sockaddr *daddr =
1576 (const struct sockaddr *)&req->listen_addr_storage;
1577 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1579 /* Returns true if the req is for IPv6 link local */
1580 return (daddr->sa_family == AF_INET6 &&
1581 (ipv6_addr_type(&daddr6->sin6_addr) & IPV6_ADDR_LINKLOCAL));
1584 static bool cma_match_net_dev(const struct rdma_cm_id *id,
1585 const struct net_device *net_dev,
1586 const struct cma_req_info *req)
1588 const struct rdma_addr *addr = &id->route.addr;
1590 if (!net_dev)
1591 /* This request is an AF_IB request */
1592 return (!id->port_num || id->port_num == req->port) &&
1593 (addr->src_addr.ss_family == AF_IB);
1596 * If the request is not for IPv6 link local, allow matching
1597 * request to any netdevice of the one or multiport rdma device.
1599 if (!cma_is_req_ipv6_ll(req))
1600 return true;
1602 * Net namespaces must match, and if the listner is listening
1603 * on a specific netdevice than netdevice must match as well.
1605 if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1606 (!!addr->dev_addr.bound_dev_if ==
1607 (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1608 return true;
1609 else
1610 return false;
1613 static struct rdma_id_private *cma_find_listener(
1614 const struct rdma_bind_list *bind_list,
1615 const struct ib_cm_id *cm_id,
1616 const struct ib_cm_event *ib_event,
1617 const struct cma_req_info *req,
1618 const struct net_device *net_dev)
1620 struct rdma_id_private *id_priv, *id_priv_dev;
1622 if (!bind_list)
1623 return ERR_PTR(-EINVAL);
1625 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1626 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1627 if (id_priv->id.device == cm_id->device &&
1628 cma_match_net_dev(&id_priv->id, net_dev, req))
1629 return id_priv;
1630 list_for_each_entry(id_priv_dev,
1631 &id_priv->listen_list,
1632 listen_list) {
1633 if (id_priv_dev->id.device == cm_id->device &&
1634 cma_match_net_dev(&id_priv_dev->id,
1635 net_dev, req))
1636 return id_priv_dev;
1641 return ERR_PTR(-EINVAL);
1644 static struct rdma_id_private *
1645 cma_ib_id_from_event(struct ib_cm_id *cm_id,
1646 const struct ib_cm_event *ib_event,
1647 struct cma_req_info *req,
1648 struct net_device **net_dev)
1650 struct rdma_bind_list *bind_list;
1651 struct rdma_id_private *id_priv;
1652 int err;
1654 err = cma_save_req_info(ib_event, req);
1655 if (err)
1656 return ERR_PTR(err);
1658 *net_dev = cma_get_net_dev(ib_event, req);
1659 if (IS_ERR(*net_dev)) {
1660 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1661 /* Assuming the protocol is AF_IB */
1662 *net_dev = NULL;
1663 } else {
1664 return ERR_CAST(*net_dev);
1669 * Net namespace might be getting deleted while route lookup,
1670 * cm_id lookup is in progress. Therefore, perform netdevice
1671 * validation, cm_id lookup under rcu lock.
1672 * RCU lock along with netdevice state check, synchronizes with
1673 * netdevice migrating to different net namespace and also avoids
1674 * case where net namespace doesn't get deleted while lookup is in
1675 * progress.
1676 * If the device state is not IFF_UP, its properties such as ifindex
1677 * and nd_net cannot be trusted to remain valid without rcu lock.
1678 * net/core/dev.c change_net_namespace() ensures to synchronize with
1679 * ongoing operations on net device after device is closed using
1680 * synchronize_net().
1682 rcu_read_lock();
1683 if (*net_dev) {
1685 * If netdevice is down, it is likely that it is administratively
1686 * down or it might be migrating to different namespace.
1687 * In that case avoid further processing, as the net namespace
1688 * or ifindex may change.
1690 if (((*net_dev)->flags & IFF_UP) == 0) {
1691 id_priv = ERR_PTR(-EHOSTUNREACH);
1692 goto err;
1695 if (!validate_net_dev(*net_dev,
1696 (struct sockaddr *)&req->listen_addr_storage,
1697 (struct sockaddr *)&req->src_addr_storage)) {
1698 id_priv = ERR_PTR(-EHOSTUNREACH);
1699 goto err;
1703 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1704 rdma_ps_from_service_id(req->service_id),
1705 cma_port_from_service_id(req->service_id));
1706 id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev);
1707 err:
1708 rcu_read_unlock();
1709 if (IS_ERR(id_priv) && *net_dev) {
1710 dev_put(*net_dev);
1711 *net_dev = NULL;
1713 return id_priv;
1716 static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1718 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1721 static void cma_cancel_route(struct rdma_id_private *id_priv)
1723 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1724 if (id_priv->query)
1725 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1729 static void cma_cancel_listens(struct rdma_id_private *id_priv)
1731 struct rdma_id_private *dev_id_priv;
1734 * Remove from listen_any_list to prevent added devices from spawning
1735 * additional listen requests.
1737 mutex_lock(&lock);
1738 list_del(&id_priv->list);
1740 while (!list_empty(&id_priv->listen_list)) {
1741 dev_id_priv = list_entry(id_priv->listen_list.next,
1742 struct rdma_id_private, listen_list);
1743 /* sync with device removal to avoid duplicate destruction */
1744 list_del_init(&dev_id_priv->list);
1745 list_del(&dev_id_priv->listen_list);
1746 mutex_unlock(&lock);
1748 rdma_destroy_id(&dev_id_priv->id);
1749 mutex_lock(&lock);
1751 mutex_unlock(&lock);
1754 static void cma_cancel_operation(struct rdma_id_private *id_priv,
1755 enum rdma_cm_state state)
1757 switch (state) {
1758 case RDMA_CM_ADDR_QUERY:
1759 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1760 break;
1761 case RDMA_CM_ROUTE_QUERY:
1762 cma_cancel_route(id_priv);
1763 break;
1764 case RDMA_CM_LISTEN:
1765 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1766 cma_cancel_listens(id_priv);
1767 break;
1768 default:
1769 break;
1773 static void cma_release_port(struct rdma_id_private *id_priv)
1775 struct rdma_bind_list *bind_list = id_priv->bind_list;
1776 struct net *net = id_priv->id.route.addr.dev_addr.net;
1778 if (!bind_list)
1779 return;
1781 mutex_lock(&lock);
1782 hlist_del(&id_priv->node);
1783 if (hlist_empty(&bind_list->owners)) {
1784 cma_ps_remove(net, bind_list->ps, bind_list->port);
1785 kfree(bind_list);
1787 mutex_unlock(&lock);
1790 static void cma_leave_roce_mc_group(struct rdma_id_private *id_priv,
1791 struct cma_multicast *mc)
1793 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1794 struct net_device *ndev = NULL;
1796 if (dev_addr->bound_dev_if)
1797 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
1798 if (ndev) {
1799 cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid, false);
1800 dev_put(ndev);
1802 kref_put(&mc->mcref, release_mc);
1805 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1807 struct cma_multicast *mc;
1809 while (!list_empty(&id_priv->mc_list)) {
1810 mc = container_of(id_priv->mc_list.next,
1811 struct cma_multicast, list);
1812 list_del(&mc->list);
1813 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
1814 id_priv->id.port_num)) {
1815 ib_sa_free_multicast(mc->multicast.ib);
1816 kfree(mc);
1817 } else {
1818 cma_leave_roce_mc_group(id_priv, mc);
1823 void rdma_destroy_id(struct rdma_cm_id *id)
1825 struct rdma_id_private *id_priv;
1826 enum rdma_cm_state state;
1828 id_priv = container_of(id, struct rdma_id_private, id);
1829 trace_cm_id_destroy(id_priv);
1830 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1831 cma_cancel_operation(id_priv, state);
1834 * Wait for any active callback to finish. New callbacks will find
1835 * the id_priv state set to destroying and abort.
1837 mutex_lock(&id_priv->handler_mutex);
1838 mutex_unlock(&id_priv->handler_mutex);
1840 rdma_restrack_del(&id_priv->res);
1841 if (id_priv->cma_dev) {
1842 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1843 if (id_priv->cm_id.ib)
1844 ib_destroy_cm_id(id_priv->cm_id.ib);
1845 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1846 if (id_priv->cm_id.iw)
1847 iw_destroy_cm_id(id_priv->cm_id.iw);
1849 cma_leave_mc_groups(id_priv);
1850 cma_release_dev(id_priv);
1853 cma_release_port(id_priv);
1854 cma_id_put(id_priv);
1855 wait_for_completion(&id_priv->comp);
1857 if (id_priv->internal_id)
1858 cma_id_put(id_priv->id.context);
1860 kfree(id_priv->id.route.path_rec);
1862 if (id_priv->id.route.addr.dev_addr.sgid_attr)
1863 rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr);
1865 put_net(id_priv->id.route.addr.dev_addr.net);
1866 kfree(id_priv);
1868 EXPORT_SYMBOL(rdma_destroy_id);
1870 static int cma_rep_recv(struct rdma_id_private *id_priv)
1872 int ret;
1874 ret = cma_modify_qp_rtr(id_priv, NULL);
1875 if (ret)
1876 goto reject;
1878 ret = cma_modify_qp_rts(id_priv, NULL);
1879 if (ret)
1880 goto reject;
1882 trace_cm_send_rtu(id_priv);
1883 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1884 if (ret)
1885 goto reject;
1887 return 0;
1888 reject:
1889 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
1890 cma_modify_qp_err(id_priv);
1891 trace_cm_send_rej(id_priv);
1892 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1893 NULL, 0, NULL, 0);
1894 return ret;
1897 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1898 const struct ib_cm_rep_event_param *rep_data,
1899 void *private_data)
1901 event->param.conn.private_data = private_data;
1902 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1903 event->param.conn.responder_resources = rep_data->responder_resources;
1904 event->param.conn.initiator_depth = rep_data->initiator_depth;
1905 event->param.conn.flow_control = rep_data->flow_control;
1906 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1907 event->param.conn.srq = rep_data->srq;
1908 event->param.conn.qp_num = rep_data->remote_qpn;
1911 static int cma_cm_event_handler(struct rdma_id_private *id_priv,
1912 struct rdma_cm_event *event)
1914 int ret;
1916 trace_cm_event_handler(id_priv, event);
1917 ret = id_priv->id.event_handler(&id_priv->id, event);
1918 trace_cm_event_done(id_priv, event, ret);
1919 return ret;
1922 static int cma_ib_handler(struct ib_cm_id *cm_id,
1923 const struct ib_cm_event *ib_event)
1925 struct rdma_id_private *id_priv = cm_id->context;
1926 struct rdma_cm_event event = {};
1927 int ret = 0;
1929 mutex_lock(&id_priv->handler_mutex);
1930 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1931 id_priv->state != RDMA_CM_CONNECT) ||
1932 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1933 id_priv->state != RDMA_CM_DISCONNECT))
1934 goto out;
1936 switch (ib_event->event) {
1937 case IB_CM_REQ_ERROR:
1938 case IB_CM_REP_ERROR:
1939 event.event = RDMA_CM_EVENT_UNREACHABLE;
1940 event.status = -ETIMEDOUT;
1941 break;
1942 case IB_CM_REP_RECEIVED:
1943 if (cma_comp(id_priv, RDMA_CM_CONNECT) &&
1944 (id_priv->id.qp_type != IB_QPT_UD)) {
1945 trace_cm_send_mra(id_priv);
1946 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1948 if (id_priv->id.qp) {
1949 event.status = cma_rep_recv(id_priv);
1950 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1951 RDMA_CM_EVENT_ESTABLISHED;
1952 } else {
1953 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1955 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1956 ib_event->private_data);
1957 break;
1958 case IB_CM_RTU_RECEIVED:
1959 case IB_CM_USER_ESTABLISHED:
1960 event.event = RDMA_CM_EVENT_ESTABLISHED;
1961 break;
1962 case IB_CM_DREQ_ERROR:
1963 event.status = -ETIMEDOUT; /* fall through */
1964 case IB_CM_DREQ_RECEIVED:
1965 case IB_CM_DREP_RECEIVED:
1966 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1967 RDMA_CM_DISCONNECT))
1968 goto out;
1969 event.event = RDMA_CM_EVENT_DISCONNECTED;
1970 break;
1971 case IB_CM_TIMEWAIT_EXIT:
1972 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1973 break;
1974 case IB_CM_MRA_RECEIVED:
1975 /* ignore event */
1976 goto out;
1977 case IB_CM_REJ_RECEIVED:
1978 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
1979 ib_event->param.rej_rcvd.reason));
1980 cma_modify_qp_err(id_priv);
1981 event.status = ib_event->param.rej_rcvd.reason;
1982 event.event = RDMA_CM_EVENT_REJECTED;
1983 event.param.conn.private_data = ib_event->private_data;
1984 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1985 break;
1986 default:
1987 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1988 ib_event->event);
1989 goto out;
1992 ret = cma_cm_event_handler(id_priv, &event);
1993 if (ret) {
1994 /* Destroy the CM ID by returning a non-zero value. */
1995 id_priv->cm_id.ib = NULL;
1996 cma_exch(id_priv, RDMA_CM_DESTROYING);
1997 mutex_unlock(&id_priv->handler_mutex);
1998 rdma_destroy_id(&id_priv->id);
1999 return ret;
2001 out:
2002 mutex_unlock(&id_priv->handler_mutex);
2003 return ret;
2006 static struct rdma_id_private *
2007 cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2008 const struct ib_cm_event *ib_event,
2009 struct net_device *net_dev)
2011 struct rdma_id_private *listen_id_priv;
2012 struct rdma_id_private *id_priv;
2013 struct rdma_cm_id *id;
2014 struct rdma_route *rt;
2015 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2016 struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2017 const __be64 service_id =
2018 ib_event->param.req_rcvd.primary_path->service_id;
2019 int ret;
2021 listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2022 id = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2023 listen_id->event_handler, listen_id->context,
2024 listen_id->ps, ib_event->param.req_rcvd.qp_type,
2025 listen_id_priv->res.kern_name);
2026 if (IS_ERR(id))
2027 return NULL;
2029 id_priv = container_of(id, struct rdma_id_private, id);
2030 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2031 (struct sockaddr *)&id->route.addr.dst_addr,
2032 listen_id, ib_event, ss_family, service_id))
2033 goto err;
2035 rt = &id->route;
2036 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
2037 rt->path_rec = kmalloc_array(rt->num_paths, sizeof(*rt->path_rec),
2038 GFP_KERNEL);
2039 if (!rt->path_rec)
2040 goto err;
2042 rt->path_rec[0] = *path;
2043 if (rt->num_paths == 2)
2044 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2046 if (net_dev) {
2047 rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
2048 } else {
2049 if (!cma_protocol_roce(listen_id) &&
2050 cma_any_addr(cma_src_addr(id_priv))) {
2051 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2052 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2053 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2054 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
2055 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2056 if (ret)
2057 goto err;
2060 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2062 id_priv->state = RDMA_CM_CONNECT;
2063 return id_priv;
2065 err:
2066 rdma_destroy_id(id);
2067 return NULL;
2070 static struct rdma_id_private *
2071 cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2072 const struct ib_cm_event *ib_event,
2073 struct net_device *net_dev)
2075 const struct rdma_id_private *listen_id_priv;
2076 struct rdma_id_private *id_priv;
2077 struct rdma_cm_id *id;
2078 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2079 struct net *net = listen_id->route.addr.dev_addr.net;
2080 int ret;
2082 listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2083 id = __rdma_create_id(net, listen_id->event_handler, listen_id->context,
2084 listen_id->ps, IB_QPT_UD,
2085 listen_id_priv->res.kern_name);
2086 if (IS_ERR(id))
2087 return NULL;
2089 id_priv = container_of(id, struct rdma_id_private, id);
2090 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2091 (struct sockaddr *)&id->route.addr.dst_addr,
2092 listen_id, ib_event, ss_family,
2093 ib_event->param.sidr_req_rcvd.service_id))
2094 goto err;
2096 if (net_dev) {
2097 rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
2098 } else {
2099 if (!cma_any_addr(cma_src_addr(id_priv))) {
2100 ret = cma_translate_addr(cma_src_addr(id_priv),
2101 &id->route.addr.dev_addr);
2102 if (ret)
2103 goto err;
2107 id_priv->state = RDMA_CM_CONNECT;
2108 return id_priv;
2109 err:
2110 rdma_destroy_id(id);
2111 return NULL;
2114 static void cma_set_req_event_data(struct rdma_cm_event *event,
2115 const struct ib_cm_req_event_param *req_data,
2116 void *private_data, int offset)
2118 event->param.conn.private_data = private_data + offset;
2119 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2120 event->param.conn.responder_resources = req_data->responder_resources;
2121 event->param.conn.initiator_depth = req_data->initiator_depth;
2122 event->param.conn.flow_control = req_data->flow_control;
2123 event->param.conn.retry_count = req_data->retry_count;
2124 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2125 event->param.conn.srq = req_data->srq;
2126 event->param.conn.qp_num = req_data->remote_qpn;
2129 static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2130 const struct ib_cm_event *ib_event)
2132 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
2133 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
2134 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
2135 (id->qp_type == IB_QPT_UD)) ||
2136 (!id->qp_type));
2139 static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2140 const struct ib_cm_event *ib_event)
2142 struct rdma_id_private *listen_id, *conn_id = NULL;
2143 struct rdma_cm_event event = {};
2144 struct cma_req_info req = {};
2145 struct net_device *net_dev;
2146 u8 offset;
2147 int ret;
2149 listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
2150 if (IS_ERR(listen_id))
2151 return PTR_ERR(listen_id);
2153 trace_cm_req_handler(listen_id, ib_event->event);
2154 if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
2155 ret = -EINVAL;
2156 goto net_dev_put;
2159 mutex_lock(&listen_id->handler_mutex);
2160 if (listen_id->state != RDMA_CM_LISTEN) {
2161 ret = -ECONNABORTED;
2162 goto err1;
2165 offset = cma_user_data_offset(listen_id);
2166 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2167 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2168 conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2169 event.param.ud.private_data = ib_event->private_data + offset;
2170 event.param.ud.private_data_len =
2171 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2172 } else {
2173 conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2174 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2175 ib_event->private_data, offset);
2177 if (!conn_id) {
2178 ret = -ENOMEM;
2179 goto err1;
2182 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2183 ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2184 if (ret)
2185 goto err2;
2187 conn_id->cm_id.ib = cm_id;
2188 cm_id->context = conn_id;
2189 cm_id->cm_handler = cma_ib_handler;
2192 * Protect against the user destroying conn_id from another thread
2193 * until we're done accessing it.
2195 cma_id_get(conn_id);
2196 ret = cma_cm_event_handler(conn_id, &event);
2197 if (ret)
2198 goto err3;
2200 * Acquire mutex to prevent user executing rdma_destroy_id()
2201 * while we're accessing the cm_id.
2203 mutex_lock(&lock);
2204 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
2205 (conn_id->id.qp_type != IB_QPT_UD)) {
2206 trace_cm_send_mra(cm_id->context);
2207 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2209 mutex_unlock(&lock);
2210 mutex_unlock(&conn_id->handler_mutex);
2211 mutex_unlock(&listen_id->handler_mutex);
2212 cma_id_put(conn_id);
2213 if (net_dev)
2214 dev_put(net_dev);
2215 return 0;
2217 err3:
2218 cma_id_put(conn_id);
2219 /* Destroy the CM ID by returning a non-zero value. */
2220 conn_id->cm_id.ib = NULL;
2221 err2:
2222 cma_exch(conn_id, RDMA_CM_DESTROYING);
2223 mutex_unlock(&conn_id->handler_mutex);
2224 err1:
2225 mutex_unlock(&listen_id->handler_mutex);
2226 if (conn_id)
2227 rdma_destroy_id(&conn_id->id);
2229 net_dev_put:
2230 if (net_dev)
2231 dev_put(net_dev);
2233 return ret;
2236 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2238 if (addr->sa_family == AF_IB)
2239 return ((struct sockaddr_ib *) addr)->sib_sid;
2241 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2243 EXPORT_SYMBOL(rdma_get_service_id);
2245 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2246 union ib_gid *dgid)
2248 struct rdma_addr *addr = &cm_id->route.addr;
2250 if (!cm_id->device) {
2251 if (sgid)
2252 memset(sgid, 0, sizeof(*sgid));
2253 if (dgid)
2254 memset(dgid, 0, sizeof(*dgid));
2255 return;
2258 if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2259 if (sgid)
2260 rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2261 if (dgid)
2262 rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2263 } else {
2264 if (sgid)
2265 rdma_addr_get_sgid(&addr->dev_addr, sgid);
2266 if (dgid)
2267 rdma_addr_get_dgid(&addr->dev_addr, dgid);
2270 EXPORT_SYMBOL(rdma_read_gids);
2272 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2274 struct rdma_id_private *id_priv = iw_id->context;
2275 struct rdma_cm_event event = {};
2276 int ret = 0;
2277 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2278 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2280 mutex_lock(&id_priv->handler_mutex);
2281 if (id_priv->state != RDMA_CM_CONNECT)
2282 goto out;
2284 switch (iw_event->event) {
2285 case IW_CM_EVENT_CLOSE:
2286 event.event = RDMA_CM_EVENT_DISCONNECTED;
2287 break;
2288 case IW_CM_EVENT_CONNECT_REPLY:
2289 memcpy(cma_src_addr(id_priv), laddr,
2290 rdma_addr_size(laddr));
2291 memcpy(cma_dst_addr(id_priv), raddr,
2292 rdma_addr_size(raddr));
2293 switch (iw_event->status) {
2294 case 0:
2295 event.event = RDMA_CM_EVENT_ESTABLISHED;
2296 event.param.conn.initiator_depth = iw_event->ird;
2297 event.param.conn.responder_resources = iw_event->ord;
2298 break;
2299 case -ECONNRESET:
2300 case -ECONNREFUSED:
2301 event.event = RDMA_CM_EVENT_REJECTED;
2302 break;
2303 case -ETIMEDOUT:
2304 event.event = RDMA_CM_EVENT_UNREACHABLE;
2305 break;
2306 default:
2307 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2308 break;
2310 break;
2311 case IW_CM_EVENT_ESTABLISHED:
2312 event.event = RDMA_CM_EVENT_ESTABLISHED;
2313 event.param.conn.initiator_depth = iw_event->ird;
2314 event.param.conn.responder_resources = iw_event->ord;
2315 break;
2316 default:
2317 goto out;
2320 event.status = iw_event->status;
2321 event.param.conn.private_data = iw_event->private_data;
2322 event.param.conn.private_data_len = iw_event->private_data_len;
2323 ret = cma_cm_event_handler(id_priv, &event);
2324 if (ret) {
2325 /* Destroy the CM ID by returning a non-zero value. */
2326 id_priv->cm_id.iw = NULL;
2327 cma_exch(id_priv, RDMA_CM_DESTROYING);
2328 mutex_unlock(&id_priv->handler_mutex);
2329 rdma_destroy_id(&id_priv->id);
2330 return ret;
2333 out:
2334 mutex_unlock(&id_priv->handler_mutex);
2335 return ret;
2338 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2339 struct iw_cm_event *iw_event)
2341 struct rdma_cm_id *new_cm_id;
2342 struct rdma_id_private *listen_id, *conn_id;
2343 struct rdma_cm_event event = {};
2344 int ret = -ECONNABORTED;
2345 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2346 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2348 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2349 event.param.conn.private_data = iw_event->private_data;
2350 event.param.conn.private_data_len = iw_event->private_data_len;
2351 event.param.conn.initiator_depth = iw_event->ird;
2352 event.param.conn.responder_resources = iw_event->ord;
2354 listen_id = cm_id->context;
2356 mutex_lock(&listen_id->handler_mutex);
2357 if (listen_id->state != RDMA_CM_LISTEN)
2358 goto out;
2360 /* Create a new RDMA id for the new IW CM ID */
2361 new_cm_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2362 listen_id->id.event_handler,
2363 listen_id->id.context,
2364 RDMA_PS_TCP, IB_QPT_RC,
2365 listen_id->res.kern_name);
2366 if (IS_ERR(new_cm_id)) {
2367 ret = -ENOMEM;
2368 goto out;
2370 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
2371 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2372 conn_id->state = RDMA_CM_CONNECT;
2374 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2375 if (ret) {
2376 mutex_unlock(&conn_id->handler_mutex);
2377 rdma_destroy_id(new_cm_id);
2378 goto out;
2381 ret = cma_iw_acquire_dev(conn_id, listen_id);
2382 if (ret) {
2383 mutex_unlock(&conn_id->handler_mutex);
2384 rdma_destroy_id(new_cm_id);
2385 goto out;
2388 conn_id->cm_id.iw = cm_id;
2389 cm_id->context = conn_id;
2390 cm_id->cm_handler = cma_iw_handler;
2392 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2393 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2396 * Protect against the user destroying conn_id from another thread
2397 * until we're done accessing it.
2399 cma_id_get(conn_id);
2400 ret = cma_cm_event_handler(conn_id, &event);
2401 if (ret) {
2402 /* User wants to destroy the CM ID */
2403 conn_id->cm_id.iw = NULL;
2404 cma_exch(conn_id, RDMA_CM_DESTROYING);
2405 mutex_unlock(&conn_id->handler_mutex);
2406 mutex_unlock(&listen_id->handler_mutex);
2407 cma_id_put(conn_id);
2408 rdma_destroy_id(&conn_id->id);
2409 return ret;
2412 mutex_unlock(&conn_id->handler_mutex);
2413 cma_id_put(conn_id);
2415 out:
2416 mutex_unlock(&listen_id->handler_mutex);
2417 return ret;
2420 static int cma_ib_listen(struct rdma_id_private *id_priv)
2422 struct sockaddr *addr;
2423 struct ib_cm_id *id;
2424 __be64 svc_id;
2426 addr = cma_src_addr(id_priv);
2427 svc_id = rdma_get_service_id(&id_priv->id, addr);
2428 id = ib_cm_insert_listen(id_priv->id.device,
2429 cma_ib_req_handler, svc_id);
2430 if (IS_ERR(id))
2431 return PTR_ERR(id);
2432 id_priv->cm_id.ib = id;
2434 return 0;
2437 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2439 int ret;
2440 struct iw_cm_id *id;
2442 id = iw_create_cm_id(id_priv->id.device,
2443 iw_conn_req_handler,
2444 id_priv);
2445 if (IS_ERR(id))
2446 return PTR_ERR(id);
2448 id->tos = id_priv->tos;
2449 id->tos_set = id_priv->tos_set;
2450 id_priv->cm_id.iw = id;
2452 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2453 rdma_addr_size(cma_src_addr(id_priv)));
2455 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2457 if (ret) {
2458 iw_destroy_cm_id(id_priv->cm_id.iw);
2459 id_priv->cm_id.iw = NULL;
2462 return ret;
2465 static int cma_listen_handler(struct rdma_cm_id *id,
2466 struct rdma_cm_event *event)
2468 struct rdma_id_private *id_priv = id->context;
2470 id->context = id_priv->id.context;
2471 id->event_handler = id_priv->id.event_handler;
2472 trace_cm_event_handler(id_priv, event);
2473 return id_priv->id.event_handler(id, event);
2476 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2477 struct cma_device *cma_dev)
2479 struct rdma_id_private *dev_id_priv;
2480 struct rdma_cm_id *id;
2481 struct net *net = id_priv->id.route.addr.dev_addr.net;
2482 int ret;
2484 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2485 return;
2487 id = __rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
2488 id_priv->id.qp_type, id_priv->res.kern_name);
2489 if (IS_ERR(id))
2490 return;
2492 dev_id_priv = container_of(id, struct rdma_id_private, id);
2494 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2495 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2496 rdma_addr_size(cma_src_addr(id_priv)));
2498 _cma_attach_to_dev(dev_id_priv, cma_dev);
2499 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2500 cma_id_get(id_priv);
2501 dev_id_priv->internal_id = 1;
2502 dev_id_priv->afonly = id_priv->afonly;
2503 dev_id_priv->tos_set = id_priv->tos_set;
2504 dev_id_priv->tos = id_priv->tos;
2506 ret = rdma_listen(id, id_priv->backlog);
2507 if (ret)
2508 dev_warn(&cma_dev->device->dev,
2509 "RDMA CMA: cma_listen_on_dev, error %d\n", ret);
2512 static void cma_listen_on_all(struct rdma_id_private *id_priv)
2514 struct cma_device *cma_dev;
2516 mutex_lock(&lock);
2517 list_add_tail(&id_priv->list, &listen_any_list);
2518 list_for_each_entry(cma_dev, &dev_list, list)
2519 cma_listen_on_dev(id_priv, cma_dev);
2520 mutex_unlock(&lock);
2523 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2525 struct rdma_id_private *id_priv;
2527 id_priv = container_of(id, struct rdma_id_private, id);
2528 id_priv->tos = (u8) tos;
2529 id_priv->tos_set = true;
2531 EXPORT_SYMBOL(rdma_set_service_type);
2534 * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2535 * with a connection identifier.
2536 * @id: Communication identifier to associated with service type.
2537 * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2539 * This function should be called before rdma_connect() on active side,
2540 * and on passive side before rdma_accept(). It is applicable to primary
2541 * path only. The timeout will affect the local side of the QP, it is not
2542 * negotiated with remote side and zero disables the timer. In case it is
2543 * set before rdma_resolve_route, the value will also be used to determine
2544 * PacketLifeTime for RoCE.
2546 * Return: 0 for success
2548 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2550 struct rdma_id_private *id_priv;
2552 if (id->qp_type != IB_QPT_RC)
2553 return -EINVAL;
2555 id_priv = container_of(id, struct rdma_id_private, id);
2556 id_priv->timeout = timeout;
2557 id_priv->timeout_set = true;
2559 return 0;
2561 EXPORT_SYMBOL(rdma_set_ack_timeout);
2563 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2564 void *context)
2566 struct cma_work *work = context;
2567 struct rdma_route *route;
2569 route = &work->id->id.route;
2571 if (!status) {
2572 route->num_paths = 1;
2573 *route->path_rec = *path_rec;
2574 } else {
2575 work->old_state = RDMA_CM_ROUTE_QUERY;
2576 work->new_state = RDMA_CM_ADDR_RESOLVED;
2577 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2578 work->event.status = status;
2579 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2580 status);
2583 queue_work(cma_wq, &work->work);
2586 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2587 unsigned long timeout_ms, struct cma_work *work)
2589 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2590 struct sa_path_rec path_rec;
2591 ib_sa_comp_mask comp_mask;
2592 struct sockaddr_in6 *sin6;
2593 struct sockaddr_ib *sib;
2595 memset(&path_rec, 0, sizeof path_rec);
2597 if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2598 path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2599 else
2600 path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2601 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2602 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2603 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2604 path_rec.numb_path = 1;
2605 path_rec.reversible = 1;
2606 path_rec.service_id = rdma_get_service_id(&id_priv->id,
2607 cma_dst_addr(id_priv));
2609 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2610 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2611 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2613 switch (cma_family(id_priv)) {
2614 case AF_INET:
2615 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2616 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2617 break;
2618 case AF_INET6:
2619 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2620 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2621 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2622 break;
2623 case AF_IB:
2624 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2625 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2626 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2627 break;
2630 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2631 id_priv->id.port_num, &path_rec,
2632 comp_mask, timeout_ms,
2633 GFP_KERNEL, cma_query_handler,
2634 work, &id_priv->query);
2636 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2639 static void cma_work_handler(struct work_struct *_work)
2641 struct cma_work *work = container_of(_work, struct cma_work, work);
2642 struct rdma_id_private *id_priv = work->id;
2643 int destroy = 0;
2645 mutex_lock(&id_priv->handler_mutex);
2646 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2647 goto out;
2649 if (cma_cm_event_handler(id_priv, &work->event)) {
2650 cma_exch(id_priv, RDMA_CM_DESTROYING);
2651 destroy = 1;
2653 out:
2654 mutex_unlock(&id_priv->handler_mutex);
2655 cma_id_put(id_priv);
2656 if (destroy)
2657 rdma_destroy_id(&id_priv->id);
2658 kfree(work);
2661 static void cma_ndev_work_handler(struct work_struct *_work)
2663 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2664 struct rdma_id_private *id_priv = work->id;
2665 int destroy = 0;
2667 mutex_lock(&id_priv->handler_mutex);
2668 if (id_priv->state == RDMA_CM_DESTROYING ||
2669 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
2670 goto out;
2672 if (cma_cm_event_handler(id_priv, &work->event)) {
2673 cma_exch(id_priv, RDMA_CM_DESTROYING);
2674 destroy = 1;
2677 out:
2678 mutex_unlock(&id_priv->handler_mutex);
2679 cma_id_put(id_priv);
2680 if (destroy)
2681 rdma_destroy_id(&id_priv->id);
2682 kfree(work);
2685 static void cma_init_resolve_route_work(struct cma_work *work,
2686 struct rdma_id_private *id_priv)
2688 work->id = id_priv;
2689 INIT_WORK(&work->work, cma_work_handler);
2690 work->old_state = RDMA_CM_ROUTE_QUERY;
2691 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2692 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2695 static void enqueue_resolve_addr_work(struct cma_work *work,
2696 struct rdma_id_private *id_priv)
2698 /* Balances with cma_id_put() in cma_work_handler */
2699 cma_id_get(id_priv);
2701 work->id = id_priv;
2702 INIT_WORK(&work->work, cma_work_handler);
2703 work->old_state = RDMA_CM_ADDR_QUERY;
2704 work->new_state = RDMA_CM_ADDR_RESOLVED;
2705 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2707 queue_work(cma_wq, &work->work);
2710 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
2711 unsigned long timeout_ms)
2713 struct rdma_route *route = &id_priv->id.route;
2714 struct cma_work *work;
2715 int ret;
2717 work = kzalloc(sizeof *work, GFP_KERNEL);
2718 if (!work)
2719 return -ENOMEM;
2721 cma_init_resolve_route_work(work, id_priv);
2723 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2724 if (!route->path_rec) {
2725 ret = -ENOMEM;
2726 goto err1;
2729 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2730 if (ret)
2731 goto err2;
2733 return 0;
2734 err2:
2735 kfree(route->path_rec);
2736 route->path_rec = NULL;
2737 err1:
2738 kfree(work);
2739 return ret;
2742 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2743 unsigned long supported_gids,
2744 enum ib_gid_type default_gid)
2746 if ((network_type == RDMA_NETWORK_IPV4 ||
2747 network_type == RDMA_NETWORK_IPV6) &&
2748 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2749 return IB_GID_TYPE_ROCE_UDP_ENCAP;
2751 return default_gid;
2755 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2756 * path record type based on GID type.
2757 * It also sets up other L2 fields which includes destination mac address
2758 * netdev ifindex, of the path record.
2759 * It returns the netdev of the bound interface for this path record entry.
2761 static struct net_device *
2762 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
2764 struct rdma_route *route = &id_priv->id.route;
2765 enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
2766 struct rdma_addr *addr = &route->addr;
2767 unsigned long supported_gids;
2768 struct net_device *ndev;
2770 if (!addr->dev_addr.bound_dev_if)
2771 return NULL;
2773 ndev = dev_get_by_index(addr->dev_addr.net,
2774 addr->dev_addr.bound_dev_if);
2775 if (!ndev)
2776 return NULL;
2778 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2779 id_priv->id.port_num);
2780 gid_type = cma_route_gid_type(addr->dev_addr.network,
2781 supported_gids,
2782 id_priv->gid_type);
2783 /* Use the hint from IP Stack to select GID Type */
2784 if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2785 gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2786 route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
2788 route->path_rec->roce.route_resolved = true;
2789 sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
2790 return ndev;
2793 int rdma_set_ib_path(struct rdma_cm_id *id,
2794 struct sa_path_rec *path_rec)
2796 struct rdma_id_private *id_priv;
2797 struct net_device *ndev;
2798 int ret;
2800 id_priv = container_of(id, struct rdma_id_private, id);
2801 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2802 RDMA_CM_ROUTE_RESOLVED))
2803 return -EINVAL;
2805 id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
2806 GFP_KERNEL);
2807 if (!id->route.path_rec) {
2808 ret = -ENOMEM;
2809 goto err;
2812 if (rdma_protocol_roce(id->device, id->port_num)) {
2813 ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2814 if (!ndev) {
2815 ret = -ENODEV;
2816 goto err_free;
2818 dev_put(ndev);
2821 id->route.num_paths = 1;
2822 return 0;
2824 err_free:
2825 kfree(id->route.path_rec);
2826 id->route.path_rec = NULL;
2827 err:
2828 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2829 return ret;
2831 EXPORT_SYMBOL(rdma_set_ib_path);
2833 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
2835 struct cma_work *work;
2837 work = kzalloc(sizeof *work, GFP_KERNEL);
2838 if (!work)
2839 return -ENOMEM;
2841 cma_init_resolve_route_work(work, id_priv);
2842 queue_work(cma_wq, &work->work);
2843 return 0;
2846 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
2848 struct net_device *dev;
2850 dev = vlan_dev_real_dev(vlan_ndev);
2851 if (dev->num_tc)
2852 return netdev_get_prio_tc_map(dev, prio);
2854 return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
2855 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2858 struct iboe_prio_tc_map {
2859 int input_prio;
2860 int output_tc;
2861 bool found;
2864 static int get_lower_vlan_dev_tc(struct net_device *dev, void *data)
2866 struct iboe_prio_tc_map *map = data;
2868 if (is_vlan_dev(dev))
2869 map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
2870 else if (dev->num_tc)
2871 map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
2872 else
2873 map->output_tc = 0;
2874 /* We are interested only in first level VLAN device, so always
2875 * return 1 to stop iterating over next level devices.
2877 map->found = true;
2878 return 1;
2881 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2883 struct iboe_prio_tc_map prio_tc_map = {};
2884 int prio = rt_tos2priority(tos);
2886 /* If VLAN device, get it directly from the VLAN netdev */
2887 if (is_vlan_dev(ndev))
2888 return get_vlan_ndev_tc(ndev, prio);
2890 prio_tc_map.input_prio = prio;
2891 rcu_read_lock();
2892 netdev_walk_all_lower_dev_rcu(ndev,
2893 get_lower_vlan_dev_tc,
2894 &prio_tc_map);
2895 rcu_read_unlock();
2896 /* If map is found from lower device, use it; Otherwise
2897 * continue with the current netdevice to get priority to tc map.
2899 if (prio_tc_map.found)
2900 return prio_tc_map.output_tc;
2901 else if (ndev->num_tc)
2902 return netdev_get_prio_tc_map(ndev, prio);
2903 else
2904 return 0;
2907 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2909 struct rdma_route *route = &id_priv->id.route;
2910 struct rdma_addr *addr = &route->addr;
2911 struct cma_work *work;
2912 int ret;
2913 struct net_device *ndev;
2915 u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
2916 rdma_start_port(id_priv->cma_dev->device)];
2917 u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
2920 work = kzalloc(sizeof *work, GFP_KERNEL);
2921 if (!work)
2922 return -ENOMEM;
2924 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2925 if (!route->path_rec) {
2926 ret = -ENOMEM;
2927 goto err1;
2930 route->num_paths = 1;
2932 ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2933 if (!ndev) {
2934 ret = -ENODEV;
2935 goto err2;
2938 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2939 &route->path_rec->sgid);
2940 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2941 &route->path_rec->dgid);
2943 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2944 /* TODO: get the hoplimit from the inet/inet6 device */
2945 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2946 else
2947 route->path_rec->hop_limit = 1;
2948 route->path_rec->reversible = 1;
2949 route->path_rec->pkey = cpu_to_be16(0xffff);
2950 route->path_rec->mtu_selector = IB_SA_EQ;
2951 route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
2952 route->path_rec->traffic_class = tos;
2953 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2954 route->path_rec->rate_selector = IB_SA_EQ;
2955 route->path_rec->rate = iboe_get_rate(ndev);
2956 dev_put(ndev);
2957 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2958 /* In case ACK timeout is set, use this value to calculate
2959 * PacketLifeTime. As per IBTA 12.7.34,
2960 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
2961 * Assuming a negligible local ACK delay, we can use
2962 * PacketLifeTime = local ACK timeout/2
2963 * as a reasonable approximation for RoCE networks.
2965 route->path_rec->packet_life_time = id_priv->timeout_set ?
2966 id_priv->timeout - 1 : CMA_IBOE_PACKET_LIFETIME;
2968 if (!route->path_rec->mtu) {
2969 ret = -EINVAL;
2970 goto err2;
2973 cma_init_resolve_route_work(work, id_priv);
2974 queue_work(cma_wq, &work->work);
2976 return 0;
2978 err2:
2979 kfree(route->path_rec);
2980 route->path_rec = NULL;
2981 route->num_paths = 0;
2982 err1:
2983 kfree(work);
2984 return ret;
2987 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
2989 struct rdma_id_private *id_priv;
2990 int ret;
2992 id_priv = container_of(id, struct rdma_id_private, id);
2993 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
2994 return -EINVAL;
2996 cma_id_get(id_priv);
2997 if (rdma_cap_ib_sa(id->device, id->port_num))
2998 ret = cma_resolve_ib_route(id_priv, timeout_ms);
2999 else if (rdma_protocol_roce(id->device, id->port_num))
3000 ret = cma_resolve_iboe_route(id_priv);
3001 else if (rdma_protocol_iwarp(id->device, id->port_num))
3002 ret = cma_resolve_iw_route(id_priv);
3003 else
3004 ret = -ENOSYS;
3006 if (ret)
3007 goto err;
3009 return 0;
3010 err:
3011 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3012 cma_id_put(id_priv);
3013 return ret;
3015 EXPORT_SYMBOL(rdma_resolve_route);
3017 static void cma_set_loopback(struct sockaddr *addr)
3019 switch (addr->sa_family) {
3020 case AF_INET:
3021 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3022 break;
3023 case AF_INET6:
3024 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3025 0, 0, 0, htonl(1));
3026 break;
3027 default:
3028 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3029 0, 0, 0, htonl(1));
3030 break;
3034 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3036 struct cma_device *cma_dev, *cur_dev;
3037 union ib_gid gid;
3038 enum ib_port_state port_state;
3039 unsigned int p;
3040 u16 pkey;
3041 int ret;
3043 cma_dev = NULL;
3044 mutex_lock(&lock);
3045 list_for_each_entry(cur_dev, &dev_list, list) {
3046 if (cma_family(id_priv) == AF_IB &&
3047 !rdma_cap_ib_cm(cur_dev->device, 1))
3048 continue;
3050 if (!cma_dev)
3051 cma_dev = cur_dev;
3053 rdma_for_each_port (cur_dev->device, p) {
3054 if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3055 port_state == IB_PORT_ACTIVE) {
3056 cma_dev = cur_dev;
3057 goto port_found;
3062 if (!cma_dev) {
3063 ret = -ENODEV;
3064 goto out;
3067 p = 1;
3069 port_found:
3070 ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3071 if (ret)
3072 goto out;
3074 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3075 if (ret)
3076 goto out;
3078 id_priv->id.route.addr.dev_addr.dev_type =
3079 (rdma_protocol_ib(cma_dev->device, p)) ?
3080 ARPHRD_INFINIBAND : ARPHRD_ETHER;
3082 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3083 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3084 id_priv->id.port_num = p;
3085 cma_attach_to_dev(id_priv, cma_dev);
3086 cma_set_loopback(cma_src_addr(id_priv));
3087 out:
3088 mutex_unlock(&lock);
3089 return ret;
3092 static void addr_handler(int status, struct sockaddr *src_addr,
3093 struct rdma_dev_addr *dev_addr, void *context)
3095 struct rdma_id_private *id_priv = context;
3096 struct rdma_cm_event event = {};
3097 struct sockaddr *addr;
3098 struct sockaddr_storage old_addr;
3100 mutex_lock(&id_priv->handler_mutex);
3101 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3102 RDMA_CM_ADDR_RESOLVED))
3103 goto out;
3106 * Store the previous src address, so that if we fail to acquire
3107 * matching rdma device, old address can be restored back, which helps
3108 * to cancel the cma listen operation correctly.
3110 addr = cma_src_addr(id_priv);
3111 memcpy(&old_addr, addr, rdma_addr_size(addr));
3112 memcpy(addr, src_addr, rdma_addr_size(src_addr));
3113 if (!status && !id_priv->cma_dev) {
3114 status = cma_acquire_dev_by_src_ip(id_priv);
3115 if (status)
3116 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3117 status);
3118 } else if (status) {
3119 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3122 if (status) {
3123 memcpy(addr, &old_addr,
3124 rdma_addr_size((struct sockaddr *)&old_addr));
3125 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3126 RDMA_CM_ADDR_BOUND))
3127 goto out;
3128 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3129 event.status = status;
3130 } else
3131 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3133 if (cma_cm_event_handler(id_priv, &event)) {
3134 cma_exch(id_priv, RDMA_CM_DESTROYING);
3135 mutex_unlock(&id_priv->handler_mutex);
3136 rdma_destroy_id(&id_priv->id);
3137 return;
3139 out:
3140 mutex_unlock(&id_priv->handler_mutex);
3143 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3145 struct cma_work *work;
3146 union ib_gid gid;
3147 int ret;
3149 work = kzalloc(sizeof *work, GFP_KERNEL);
3150 if (!work)
3151 return -ENOMEM;
3153 if (!id_priv->cma_dev) {
3154 ret = cma_bind_loopback(id_priv);
3155 if (ret)
3156 goto err;
3159 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3160 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3162 enqueue_resolve_addr_work(work, id_priv);
3163 return 0;
3164 err:
3165 kfree(work);
3166 return ret;
3169 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3171 struct cma_work *work;
3172 int ret;
3174 work = kzalloc(sizeof *work, GFP_KERNEL);
3175 if (!work)
3176 return -ENOMEM;
3178 if (!id_priv->cma_dev) {
3179 ret = cma_resolve_ib_dev(id_priv);
3180 if (ret)
3181 goto err;
3184 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3185 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3187 enqueue_resolve_addr_work(work, id_priv);
3188 return 0;
3189 err:
3190 kfree(work);
3191 return ret;
3194 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3195 const struct sockaddr *dst_addr)
3197 if (!src_addr || !src_addr->sa_family) {
3198 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
3199 src_addr->sa_family = dst_addr->sa_family;
3200 if (IS_ENABLED(CONFIG_IPV6) &&
3201 dst_addr->sa_family == AF_INET6) {
3202 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
3203 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
3204 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
3205 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
3206 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
3207 } else if (dst_addr->sa_family == AF_IB) {
3208 ((struct sockaddr_ib *) src_addr)->sib_pkey =
3209 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
3212 return rdma_bind_addr(id, src_addr);
3215 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3216 const struct sockaddr *dst_addr, unsigned long timeout_ms)
3218 struct rdma_id_private *id_priv;
3219 int ret;
3221 id_priv = container_of(id, struct rdma_id_private, id);
3222 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3223 if (id_priv->state == RDMA_CM_IDLE) {
3224 ret = cma_bind_addr(id, src_addr, dst_addr);
3225 if (ret) {
3226 memset(cma_dst_addr(id_priv), 0,
3227 rdma_addr_size(dst_addr));
3228 return ret;
3232 if (cma_family(id_priv) != dst_addr->sa_family) {
3233 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3234 return -EINVAL;
3237 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3238 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3239 return -EINVAL;
3242 if (cma_any_addr(dst_addr)) {
3243 ret = cma_resolve_loopback(id_priv);
3244 } else {
3245 if (dst_addr->sa_family == AF_IB) {
3246 ret = cma_resolve_ib_addr(id_priv);
3247 } else {
3248 ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
3249 &id->route.addr.dev_addr,
3250 timeout_ms, addr_handler,
3251 false, id_priv);
3254 if (ret)
3255 goto err;
3257 return 0;
3258 err:
3259 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3260 return ret;
3262 EXPORT_SYMBOL(rdma_resolve_addr);
3264 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3266 struct rdma_id_private *id_priv;
3267 unsigned long flags;
3268 int ret;
3270 id_priv = container_of(id, struct rdma_id_private, id);
3271 spin_lock_irqsave(&id_priv->lock, flags);
3272 if (reuse || id_priv->state == RDMA_CM_IDLE) {
3273 id_priv->reuseaddr = reuse;
3274 ret = 0;
3275 } else {
3276 ret = -EINVAL;
3278 spin_unlock_irqrestore(&id_priv->lock, flags);
3279 return ret;
3281 EXPORT_SYMBOL(rdma_set_reuseaddr);
3283 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3285 struct rdma_id_private *id_priv;
3286 unsigned long flags;
3287 int ret;
3289 id_priv = container_of(id, struct rdma_id_private, id);
3290 spin_lock_irqsave(&id_priv->lock, flags);
3291 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3292 id_priv->options |= (1 << CMA_OPTION_AFONLY);
3293 id_priv->afonly = afonly;
3294 ret = 0;
3295 } else {
3296 ret = -EINVAL;
3298 spin_unlock_irqrestore(&id_priv->lock, flags);
3299 return ret;
3301 EXPORT_SYMBOL(rdma_set_afonly);
3303 static void cma_bind_port(struct rdma_bind_list *bind_list,
3304 struct rdma_id_private *id_priv)
3306 struct sockaddr *addr;
3307 struct sockaddr_ib *sib;
3308 u64 sid, mask;
3309 __be16 port;
3311 addr = cma_src_addr(id_priv);
3312 port = htons(bind_list->port);
3314 switch (addr->sa_family) {
3315 case AF_INET:
3316 ((struct sockaddr_in *) addr)->sin_port = port;
3317 break;
3318 case AF_INET6:
3319 ((struct sockaddr_in6 *) addr)->sin6_port = port;
3320 break;
3321 case AF_IB:
3322 sib = (struct sockaddr_ib *) addr;
3323 sid = be64_to_cpu(sib->sib_sid);
3324 mask = be64_to_cpu(sib->sib_sid_mask);
3325 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3326 sib->sib_sid_mask = cpu_to_be64(~0ULL);
3327 break;
3329 id_priv->bind_list = bind_list;
3330 hlist_add_head(&id_priv->node, &bind_list->owners);
3333 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3334 struct rdma_id_private *id_priv, unsigned short snum)
3336 struct rdma_bind_list *bind_list;
3337 int ret;
3339 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3340 if (!bind_list)
3341 return -ENOMEM;
3343 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3344 snum);
3345 if (ret < 0)
3346 goto err;
3348 bind_list->ps = ps;
3349 bind_list->port = snum;
3350 cma_bind_port(bind_list, id_priv);
3351 return 0;
3352 err:
3353 kfree(bind_list);
3354 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3357 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3358 struct rdma_id_private *id_priv)
3360 struct rdma_id_private *cur_id;
3361 struct sockaddr *daddr = cma_dst_addr(id_priv);
3362 struct sockaddr *saddr = cma_src_addr(id_priv);
3363 __be16 dport = cma_port(daddr);
3365 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3366 struct sockaddr *cur_daddr = cma_dst_addr(cur_id);
3367 struct sockaddr *cur_saddr = cma_src_addr(cur_id);
3368 __be16 cur_dport = cma_port(cur_daddr);
3370 if (id_priv == cur_id)
3371 continue;
3373 /* different dest port -> unique */
3374 if (!cma_any_port(daddr) &&
3375 !cma_any_port(cur_daddr) &&
3376 (dport != cur_dport))
3377 continue;
3379 /* different src address -> unique */
3380 if (!cma_any_addr(saddr) &&
3381 !cma_any_addr(cur_saddr) &&
3382 cma_addr_cmp(saddr, cur_saddr))
3383 continue;
3385 /* different dst address -> unique */
3386 if (!cma_any_addr(daddr) &&
3387 !cma_any_addr(cur_daddr) &&
3388 cma_addr_cmp(daddr, cur_daddr))
3389 continue;
3391 return -EADDRNOTAVAIL;
3393 return 0;
3396 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3397 struct rdma_id_private *id_priv)
3399 static unsigned int last_used_port;
3400 int low, high, remaining;
3401 unsigned int rover;
3402 struct net *net = id_priv->id.route.addr.dev_addr.net;
3404 inet_get_local_port_range(net, &low, &high);
3405 remaining = (high - low) + 1;
3406 rover = prandom_u32() % remaining + low;
3407 retry:
3408 if (last_used_port != rover) {
3409 struct rdma_bind_list *bind_list;
3410 int ret;
3412 bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3414 if (!bind_list) {
3415 ret = cma_alloc_port(ps, id_priv, rover);
3416 } else {
3417 ret = cma_port_is_unique(bind_list, id_priv);
3418 if (!ret)
3419 cma_bind_port(bind_list, id_priv);
3422 * Remember previously used port number in order to avoid
3423 * re-using same port immediately after it is closed.
3425 if (!ret)
3426 last_used_port = rover;
3427 if (ret != -EADDRNOTAVAIL)
3428 return ret;
3430 if (--remaining) {
3431 rover++;
3432 if ((rover < low) || (rover > high))
3433 rover = low;
3434 goto retry;
3436 return -EADDRNOTAVAIL;
3440 * Check that the requested port is available. This is called when trying to
3441 * bind to a specific port, or when trying to listen on a bound port. In
3442 * the latter case, the provided id_priv may already be on the bind_list, but
3443 * we still need to check that it's okay to start listening.
3445 static int cma_check_port(struct rdma_bind_list *bind_list,
3446 struct rdma_id_private *id_priv, uint8_t reuseaddr)
3448 struct rdma_id_private *cur_id;
3449 struct sockaddr *addr, *cur_addr;
3451 addr = cma_src_addr(id_priv);
3452 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3453 if (id_priv == cur_id)
3454 continue;
3456 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
3457 cur_id->reuseaddr)
3458 continue;
3460 cur_addr = cma_src_addr(cur_id);
3461 if (id_priv->afonly && cur_id->afonly &&
3462 (addr->sa_family != cur_addr->sa_family))
3463 continue;
3465 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3466 return -EADDRNOTAVAIL;
3468 if (!cma_addr_cmp(addr, cur_addr))
3469 return -EADDRINUSE;
3471 return 0;
3474 static int cma_use_port(enum rdma_ucm_port_space ps,
3475 struct rdma_id_private *id_priv)
3477 struct rdma_bind_list *bind_list;
3478 unsigned short snum;
3479 int ret;
3481 snum = ntohs(cma_port(cma_src_addr(id_priv)));
3482 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3483 return -EACCES;
3485 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3486 if (!bind_list) {
3487 ret = cma_alloc_port(ps, id_priv, snum);
3488 } else {
3489 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3490 if (!ret)
3491 cma_bind_port(bind_list, id_priv);
3493 return ret;
3496 static int cma_bind_listen(struct rdma_id_private *id_priv)
3498 struct rdma_bind_list *bind_list = id_priv->bind_list;
3499 int ret = 0;
3501 mutex_lock(&lock);
3502 if (bind_list->owners.first->next)
3503 ret = cma_check_port(bind_list, id_priv, 0);
3504 mutex_unlock(&lock);
3505 return ret;
3508 static enum rdma_ucm_port_space
3509 cma_select_inet_ps(struct rdma_id_private *id_priv)
3511 switch (id_priv->id.ps) {
3512 case RDMA_PS_TCP:
3513 case RDMA_PS_UDP:
3514 case RDMA_PS_IPOIB:
3515 case RDMA_PS_IB:
3516 return id_priv->id.ps;
3517 default:
3519 return 0;
3523 static enum rdma_ucm_port_space
3524 cma_select_ib_ps(struct rdma_id_private *id_priv)
3526 enum rdma_ucm_port_space ps = 0;
3527 struct sockaddr_ib *sib;
3528 u64 sid_ps, mask, sid;
3530 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3531 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3532 sid = be64_to_cpu(sib->sib_sid) & mask;
3534 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3535 sid_ps = RDMA_IB_IP_PS_IB;
3536 ps = RDMA_PS_IB;
3537 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3538 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3539 sid_ps = RDMA_IB_IP_PS_TCP;
3540 ps = RDMA_PS_TCP;
3541 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3542 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3543 sid_ps = RDMA_IB_IP_PS_UDP;
3544 ps = RDMA_PS_UDP;
3547 if (ps) {
3548 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3549 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3550 be64_to_cpu(sib->sib_sid_mask));
3552 return ps;
3555 static int cma_get_port(struct rdma_id_private *id_priv)
3557 enum rdma_ucm_port_space ps;
3558 int ret;
3560 if (cma_family(id_priv) != AF_IB)
3561 ps = cma_select_inet_ps(id_priv);
3562 else
3563 ps = cma_select_ib_ps(id_priv);
3564 if (!ps)
3565 return -EPROTONOSUPPORT;
3567 mutex_lock(&lock);
3568 if (cma_any_port(cma_src_addr(id_priv)))
3569 ret = cma_alloc_any_port(ps, id_priv);
3570 else
3571 ret = cma_use_port(ps, id_priv);
3572 mutex_unlock(&lock);
3574 return ret;
3577 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3578 struct sockaddr *addr)
3580 #if IS_ENABLED(CONFIG_IPV6)
3581 struct sockaddr_in6 *sin6;
3583 if (addr->sa_family != AF_INET6)
3584 return 0;
3586 sin6 = (struct sockaddr_in6 *) addr;
3588 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3589 return 0;
3591 if (!sin6->sin6_scope_id)
3592 return -EINVAL;
3594 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3595 #endif
3596 return 0;
3599 int rdma_listen(struct rdma_cm_id *id, int backlog)
3601 struct rdma_id_private *id_priv;
3602 int ret;
3604 id_priv = container_of(id, struct rdma_id_private, id);
3605 if (id_priv->state == RDMA_CM_IDLE) {
3606 id->route.addr.src_addr.ss_family = AF_INET;
3607 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3608 if (ret)
3609 return ret;
3612 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3613 return -EINVAL;
3615 if (id_priv->reuseaddr) {
3616 ret = cma_bind_listen(id_priv);
3617 if (ret)
3618 goto err;
3621 id_priv->backlog = backlog;
3622 if (id->device) {
3623 if (rdma_cap_ib_cm(id->device, 1)) {
3624 ret = cma_ib_listen(id_priv);
3625 if (ret)
3626 goto err;
3627 } else if (rdma_cap_iw_cm(id->device, 1)) {
3628 ret = cma_iw_listen(id_priv, backlog);
3629 if (ret)
3630 goto err;
3631 } else {
3632 ret = -ENOSYS;
3633 goto err;
3635 } else
3636 cma_listen_on_all(id_priv);
3638 return 0;
3639 err:
3640 id_priv->backlog = 0;
3641 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3642 return ret;
3644 EXPORT_SYMBOL(rdma_listen);
3646 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3648 struct rdma_id_private *id_priv;
3649 int ret;
3650 struct sockaddr *daddr;
3652 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3653 addr->sa_family != AF_IB)
3654 return -EAFNOSUPPORT;
3656 id_priv = container_of(id, struct rdma_id_private, id);
3657 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3658 return -EINVAL;
3660 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3661 if (ret)
3662 goto err1;
3664 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3665 if (!cma_any_addr(addr)) {
3666 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3667 if (ret)
3668 goto err1;
3670 ret = cma_acquire_dev_by_src_ip(id_priv);
3671 if (ret)
3672 goto err1;
3675 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3676 if (addr->sa_family == AF_INET)
3677 id_priv->afonly = 1;
3678 #if IS_ENABLED(CONFIG_IPV6)
3679 else if (addr->sa_family == AF_INET6) {
3680 struct net *net = id_priv->id.route.addr.dev_addr.net;
3682 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3684 #endif
3686 daddr = cma_dst_addr(id_priv);
3687 daddr->sa_family = addr->sa_family;
3689 ret = cma_get_port(id_priv);
3690 if (ret)
3691 goto err2;
3693 return 0;
3694 err2:
3695 rdma_restrack_del(&id_priv->res);
3696 if (id_priv->cma_dev)
3697 cma_release_dev(id_priv);
3698 err1:
3699 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3700 return ret;
3702 EXPORT_SYMBOL(rdma_bind_addr);
3704 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3706 struct cma_hdr *cma_hdr;
3708 cma_hdr = hdr;
3709 cma_hdr->cma_version = CMA_VERSION;
3710 if (cma_family(id_priv) == AF_INET) {
3711 struct sockaddr_in *src4, *dst4;
3713 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3714 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3716 cma_set_ip_ver(cma_hdr, 4);
3717 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3718 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3719 cma_hdr->port = src4->sin_port;
3720 } else if (cma_family(id_priv) == AF_INET6) {
3721 struct sockaddr_in6 *src6, *dst6;
3723 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3724 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3726 cma_set_ip_ver(cma_hdr, 6);
3727 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3728 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3729 cma_hdr->port = src6->sin6_port;
3731 return 0;
3734 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3735 const struct ib_cm_event *ib_event)
3737 struct rdma_id_private *id_priv = cm_id->context;
3738 struct rdma_cm_event event = {};
3739 const struct ib_cm_sidr_rep_event_param *rep =
3740 &ib_event->param.sidr_rep_rcvd;
3741 int ret = 0;
3743 mutex_lock(&id_priv->handler_mutex);
3744 if (id_priv->state != RDMA_CM_CONNECT)
3745 goto out;
3747 switch (ib_event->event) {
3748 case IB_CM_SIDR_REQ_ERROR:
3749 event.event = RDMA_CM_EVENT_UNREACHABLE;
3750 event.status = -ETIMEDOUT;
3751 break;
3752 case IB_CM_SIDR_REP_RECEIVED:
3753 event.param.ud.private_data = ib_event->private_data;
3754 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3755 if (rep->status != IB_SIDR_SUCCESS) {
3756 event.event = RDMA_CM_EVENT_UNREACHABLE;
3757 event.status = ib_event->param.sidr_rep_rcvd.status;
3758 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3759 event.status);
3760 break;
3762 ret = cma_set_qkey(id_priv, rep->qkey);
3763 if (ret) {
3764 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3765 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3766 event.status = ret;
3767 break;
3769 ib_init_ah_attr_from_path(id_priv->id.device,
3770 id_priv->id.port_num,
3771 id_priv->id.route.path_rec,
3772 &event.param.ud.ah_attr,
3773 rep->sgid_attr);
3774 event.param.ud.qp_num = rep->qpn;
3775 event.param.ud.qkey = rep->qkey;
3776 event.event = RDMA_CM_EVENT_ESTABLISHED;
3777 event.status = 0;
3778 break;
3779 default:
3780 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3781 ib_event->event);
3782 goto out;
3785 ret = cma_cm_event_handler(id_priv, &event);
3787 rdma_destroy_ah_attr(&event.param.ud.ah_attr);
3788 if (ret) {
3789 /* Destroy the CM ID by returning a non-zero value. */
3790 id_priv->cm_id.ib = NULL;
3791 cma_exch(id_priv, RDMA_CM_DESTROYING);
3792 mutex_unlock(&id_priv->handler_mutex);
3793 rdma_destroy_id(&id_priv->id);
3794 return ret;
3796 out:
3797 mutex_unlock(&id_priv->handler_mutex);
3798 return ret;
3801 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3802 struct rdma_conn_param *conn_param)
3804 struct ib_cm_sidr_req_param req;
3805 struct ib_cm_id *id;
3806 void *private_data;
3807 u8 offset;
3808 int ret;
3810 memset(&req, 0, sizeof req);
3811 offset = cma_user_data_offset(id_priv);
3812 req.private_data_len = offset + conn_param->private_data_len;
3813 if (req.private_data_len < conn_param->private_data_len)
3814 return -EINVAL;
3816 if (req.private_data_len) {
3817 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3818 if (!private_data)
3819 return -ENOMEM;
3820 } else {
3821 private_data = NULL;
3824 if (conn_param->private_data && conn_param->private_data_len)
3825 memcpy(private_data + offset, conn_param->private_data,
3826 conn_param->private_data_len);
3828 if (private_data) {
3829 ret = cma_format_hdr(private_data, id_priv);
3830 if (ret)
3831 goto out;
3832 req.private_data = private_data;
3835 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3836 id_priv);
3837 if (IS_ERR(id)) {
3838 ret = PTR_ERR(id);
3839 goto out;
3841 id_priv->cm_id.ib = id;
3843 req.path = id_priv->id.route.path_rec;
3844 req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
3845 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3846 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3847 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3849 trace_cm_send_sidr_req(id_priv);
3850 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3851 if (ret) {
3852 ib_destroy_cm_id(id_priv->cm_id.ib);
3853 id_priv->cm_id.ib = NULL;
3855 out:
3856 kfree(private_data);
3857 return ret;
3860 static int cma_connect_ib(struct rdma_id_private *id_priv,
3861 struct rdma_conn_param *conn_param)
3863 struct ib_cm_req_param req;
3864 struct rdma_route *route;
3865 void *private_data;
3866 struct ib_cm_id *id;
3867 u8 offset;
3868 int ret;
3870 memset(&req, 0, sizeof req);
3871 offset = cma_user_data_offset(id_priv);
3872 req.private_data_len = offset + conn_param->private_data_len;
3873 if (req.private_data_len < conn_param->private_data_len)
3874 return -EINVAL;
3876 if (req.private_data_len) {
3877 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3878 if (!private_data)
3879 return -ENOMEM;
3880 } else {
3881 private_data = NULL;
3884 if (conn_param->private_data && conn_param->private_data_len)
3885 memcpy(private_data + offset, conn_param->private_data,
3886 conn_param->private_data_len);
3888 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3889 if (IS_ERR(id)) {
3890 ret = PTR_ERR(id);
3891 goto out;
3893 id_priv->cm_id.ib = id;
3895 route = &id_priv->id.route;
3896 if (private_data) {
3897 ret = cma_format_hdr(private_data, id_priv);
3898 if (ret)
3899 goto out;
3900 req.private_data = private_data;
3903 req.primary_path = &route->path_rec[0];
3904 if (route->num_paths == 2)
3905 req.alternate_path = &route->path_rec[1];
3907 req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
3908 /* Alternate path SGID attribute currently unsupported */
3909 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3910 req.qp_num = id_priv->qp_num;
3911 req.qp_type = id_priv->id.qp_type;
3912 req.starting_psn = id_priv->seq_num;
3913 req.responder_resources = conn_param->responder_resources;
3914 req.initiator_depth = conn_param->initiator_depth;
3915 req.flow_control = conn_param->flow_control;
3916 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3917 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3918 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3919 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3920 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3921 req.srq = id_priv->srq ? 1 : 0;
3923 trace_cm_send_req(id_priv);
3924 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3925 out:
3926 if (ret && !IS_ERR(id)) {
3927 ib_destroy_cm_id(id);
3928 id_priv->cm_id.ib = NULL;
3931 kfree(private_data);
3932 return ret;
3935 static int cma_connect_iw(struct rdma_id_private *id_priv,
3936 struct rdma_conn_param *conn_param)
3938 struct iw_cm_id *cm_id;
3939 int ret;
3940 struct iw_cm_conn_param iw_param;
3942 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
3943 if (IS_ERR(cm_id))
3944 return PTR_ERR(cm_id);
3946 cm_id->tos = id_priv->tos;
3947 cm_id->tos_set = id_priv->tos_set;
3948 id_priv->cm_id.iw = cm_id;
3950 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3951 rdma_addr_size(cma_src_addr(id_priv)));
3952 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3953 rdma_addr_size(cma_dst_addr(id_priv)));
3955 ret = cma_modify_qp_rtr(id_priv, conn_param);
3956 if (ret)
3957 goto out;
3959 if (conn_param) {
3960 iw_param.ord = conn_param->initiator_depth;
3961 iw_param.ird = conn_param->responder_resources;
3962 iw_param.private_data = conn_param->private_data;
3963 iw_param.private_data_len = conn_param->private_data_len;
3964 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3965 } else {
3966 memset(&iw_param, 0, sizeof iw_param);
3967 iw_param.qpn = id_priv->qp_num;
3969 ret = iw_cm_connect(cm_id, &iw_param);
3970 out:
3971 if (ret) {
3972 iw_destroy_cm_id(cm_id);
3973 id_priv->cm_id.iw = NULL;
3975 return ret;
3978 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3980 struct rdma_id_private *id_priv;
3981 int ret;
3983 id_priv = container_of(id, struct rdma_id_private, id);
3984 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
3985 return -EINVAL;
3987 if (!id->qp) {
3988 id_priv->qp_num = conn_param->qp_num;
3989 id_priv->srq = conn_param->srq;
3992 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3993 if (id->qp_type == IB_QPT_UD)
3994 ret = cma_resolve_ib_udp(id_priv, conn_param);
3995 else
3996 ret = cma_connect_ib(id_priv, conn_param);
3997 } else if (rdma_cap_iw_cm(id->device, id->port_num))
3998 ret = cma_connect_iw(id_priv, conn_param);
3999 else
4000 ret = -ENOSYS;
4001 if (ret)
4002 goto err;
4004 return 0;
4005 err:
4006 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4007 return ret;
4009 EXPORT_SYMBOL(rdma_connect);
4011 static int cma_accept_ib(struct rdma_id_private *id_priv,
4012 struct rdma_conn_param *conn_param)
4014 struct ib_cm_rep_param rep;
4015 int ret;
4017 ret = cma_modify_qp_rtr(id_priv, conn_param);
4018 if (ret)
4019 goto out;
4021 ret = cma_modify_qp_rts(id_priv, conn_param);
4022 if (ret)
4023 goto out;
4025 memset(&rep, 0, sizeof rep);
4026 rep.qp_num = id_priv->qp_num;
4027 rep.starting_psn = id_priv->seq_num;
4028 rep.private_data = conn_param->private_data;
4029 rep.private_data_len = conn_param->private_data_len;
4030 rep.responder_resources = conn_param->responder_resources;
4031 rep.initiator_depth = conn_param->initiator_depth;
4032 rep.failover_accepted = 0;
4033 rep.flow_control = conn_param->flow_control;
4034 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4035 rep.srq = id_priv->srq ? 1 : 0;
4037 trace_cm_send_rep(id_priv);
4038 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4039 out:
4040 return ret;
4043 static int cma_accept_iw(struct rdma_id_private *id_priv,
4044 struct rdma_conn_param *conn_param)
4046 struct iw_cm_conn_param iw_param;
4047 int ret;
4049 if (!conn_param)
4050 return -EINVAL;
4052 ret = cma_modify_qp_rtr(id_priv, conn_param);
4053 if (ret)
4054 return ret;
4056 iw_param.ord = conn_param->initiator_depth;
4057 iw_param.ird = conn_param->responder_resources;
4058 iw_param.private_data = conn_param->private_data;
4059 iw_param.private_data_len = conn_param->private_data_len;
4060 if (id_priv->id.qp) {
4061 iw_param.qpn = id_priv->qp_num;
4062 } else
4063 iw_param.qpn = conn_param->qp_num;
4065 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4068 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4069 enum ib_cm_sidr_status status, u32 qkey,
4070 const void *private_data, int private_data_len)
4072 struct ib_cm_sidr_rep_param rep;
4073 int ret;
4075 memset(&rep, 0, sizeof rep);
4076 rep.status = status;
4077 if (status == IB_SIDR_SUCCESS) {
4078 ret = cma_set_qkey(id_priv, qkey);
4079 if (ret)
4080 return ret;
4081 rep.qp_num = id_priv->qp_num;
4082 rep.qkey = id_priv->qkey;
4084 rep.private_data = private_data;
4085 rep.private_data_len = private_data_len;
4087 trace_cm_send_sidr_rep(id_priv);
4088 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4091 int __rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4092 const char *caller)
4094 struct rdma_id_private *id_priv;
4095 int ret;
4097 id_priv = container_of(id, struct rdma_id_private, id);
4099 rdma_restrack_set_task(&id_priv->res, caller);
4101 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
4102 return -EINVAL;
4104 if (!id->qp && conn_param) {
4105 id_priv->qp_num = conn_param->qp_num;
4106 id_priv->srq = conn_param->srq;
4109 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4110 if (id->qp_type == IB_QPT_UD) {
4111 if (conn_param)
4112 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4113 conn_param->qkey,
4114 conn_param->private_data,
4115 conn_param->private_data_len);
4116 else
4117 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4118 0, NULL, 0);
4119 } else {
4120 if (conn_param)
4121 ret = cma_accept_ib(id_priv, conn_param);
4122 else
4123 ret = cma_rep_recv(id_priv);
4125 } else if (rdma_cap_iw_cm(id->device, id->port_num))
4126 ret = cma_accept_iw(id_priv, conn_param);
4127 else
4128 ret = -ENOSYS;
4130 if (ret)
4131 goto reject;
4133 return 0;
4134 reject:
4135 cma_modify_qp_err(id_priv);
4136 rdma_reject(id, NULL, 0);
4137 return ret;
4139 EXPORT_SYMBOL(__rdma_accept);
4141 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4143 struct rdma_id_private *id_priv;
4144 int ret;
4146 id_priv = container_of(id, struct rdma_id_private, id);
4147 if (!id_priv->cm_id.ib)
4148 return -EINVAL;
4150 switch (id->device->node_type) {
4151 case RDMA_NODE_IB_CA:
4152 ret = ib_cm_notify(id_priv->cm_id.ib, event);
4153 break;
4154 default:
4155 ret = 0;
4156 break;
4158 return ret;
4160 EXPORT_SYMBOL(rdma_notify);
4162 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4163 u8 private_data_len)
4165 struct rdma_id_private *id_priv;
4166 int ret;
4168 id_priv = container_of(id, struct rdma_id_private, id);
4169 if (!id_priv->cm_id.ib)
4170 return -EINVAL;
4172 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4173 if (id->qp_type == IB_QPT_UD) {
4174 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4175 private_data, private_data_len);
4176 } else {
4177 trace_cm_send_rej(id_priv);
4178 ret = ib_send_cm_rej(id_priv->cm_id.ib,
4179 IB_CM_REJ_CONSUMER_DEFINED, NULL,
4180 0, private_data, private_data_len);
4182 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4183 ret = iw_cm_reject(id_priv->cm_id.iw,
4184 private_data, private_data_len);
4185 } else
4186 ret = -ENOSYS;
4188 return ret;
4190 EXPORT_SYMBOL(rdma_reject);
4192 int rdma_disconnect(struct rdma_cm_id *id)
4194 struct rdma_id_private *id_priv;
4195 int ret;
4197 id_priv = container_of(id, struct rdma_id_private, id);
4198 if (!id_priv->cm_id.ib)
4199 return -EINVAL;
4201 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4202 ret = cma_modify_qp_err(id_priv);
4203 if (ret)
4204 goto out;
4205 /* Initiate or respond to a disconnect. */
4206 trace_cm_disconnect(id_priv);
4207 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4208 if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4209 trace_cm_sent_drep(id_priv);
4210 } else {
4211 trace_cm_sent_dreq(id_priv);
4213 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4214 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4215 } else
4216 ret = -EINVAL;
4218 out:
4219 return ret;
4221 EXPORT_SYMBOL(rdma_disconnect);
4223 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4225 struct rdma_id_private *id_priv;
4226 struct cma_multicast *mc = multicast->context;
4227 struct rdma_cm_event event = {};
4228 int ret = 0;
4230 id_priv = mc->id_priv;
4231 mutex_lock(&id_priv->handler_mutex);
4232 if (id_priv->state != RDMA_CM_ADDR_BOUND &&
4233 id_priv->state != RDMA_CM_ADDR_RESOLVED)
4234 goto out;
4236 if (!status)
4237 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4238 else
4239 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4240 status);
4241 mutex_lock(&id_priv->qp_mutex);
4242 if (!status && id_priv->id.qp) {
4243 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
4244 be16_to_cpu(multicast->rec.mlid));
4245 if (status)
4246 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n",
4247 status);
4249 mutex_unlock(&id_priv->qp_mutex);
4251 event.status = status;
4252 event.param.ud.private_data = mc->context;
4253 if (!status) {
4254 struct rdma_dev_addr *dev_addr =
4255 &id_priv->id.route.addr.dev_addr;
4256 struct net_device *ndev =
4257 dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4258 enum ib_gid_type gid_type =
4259 id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4260 rdma_start_port(id_priv->cma_dev->device)];
4262 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
4263 ret = ib_init_ah_from_mcmember(id_priv->id.device,
4264 id_priv->id.port_num,
4265 &multicast->rec,
4266 ndev, gid_type,
4267 &event.param.ud.ah_attr);
4268 if (ret)
4269 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
4271 event.param.ud.qp_num = 0xFFFFFF;
4272 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
4273 if (ndev)
4274 dev_put(ndev);
4275 } else
4276 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
4278 ret = cma_cm_event_handler(id_priv, &event);
4280 rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4281 if (ret) {
4282 cma_exch(id_priv, RDMA_CM_DESTROYING);
4283 mutex_unlock(&id_priv->handler_mutex);
4284 rdma_destroy_id(&id_priv->id);
4285 return 0;
4288 out:
4289 mutex_unlock(&id_priv->handler_mutex);
4290 return 0;
4293 static void cma_set_mgid(struct rdma_id_private *id_priv,
4294 struct sockaddr *addr, union ib_gid *mgid)
4296 unsigned char mc_map[MAX_ADDR_LEN];
4297 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4298 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4299 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4301 if (cma_any_addr(addr)) {
4302 memset(mgid, 0, sizeof *mgid);
4303 } else if ((addr->sa_family == AF_INET6) &&
4304 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4305 0xFF10A01B)) {
4306 /* IPv6 address is an SA assigned MGID. */
4307 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4308 } else if (addr->sa_family == AF_IB) {
4309 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4310 } else if (addr->sa_family == AF_INET6) {
4311 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4312 if (id_priv->id.ps == RDMA_PS_UDP)
4313 mc_map[7] = 0x01; /* Use RDMA CM signature */
4314 *mgid = *(union ib_gid *) (mc_map + 4);
4315 } else {
4316 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4317 if (id_priv->id.ps == RDMA_PS_UDP)
4318 mc_map[7] = 0x01; /* Use RDMA CM signature */
4319 *mgid = *(union ib_gid *) (mc_map + 4);
4323 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4324 struct cma_multicast *mc)
4326 struct ib_sa_mcmember_rec rec;
4327 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4328 ib_sa_comp_mask comp_mask;
4329 int ret;
4331 ib_addr_get_mgid(dev_addr, &rec.mgid);
4332 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4333 &rec.mgid, &rec);
4334 if (ret)
4335 return ret;
4337 ret = cma_set_qkey(id_priv, 0);
4338 if (ret)
4339 return ret;
4341 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4342 rec.qkey = cpu_to_be32(id_priv->qkey);
4343 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4344 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4345 rec.join_state = mc->join_state;
4347 if ((rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) &&
4348 (!ib_sa_sendonly_fullmem_support(&sa_client,
4349 id_priv->id.device,
4350 id_priv->id.port_num))) {
4351 dev_warn(
4352 &id_priv->id.device->dev,
4353 "RDMA CM: port %u Unable to multicast join: SM doesn't support Send Only Full Member option\n",
4354 id_priv->id.port_num);
4355 return -EOPNOTSUPP;
4358 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4359 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4360 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4361 IB_SA_MCMEMBER_REC_FLOW_LABEL |
4362 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4364 if (id_priv->id.ps == RDMA_PS_IPOIB)
4365 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4366 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4367 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4368 IB_SA_MCMEMBER_REC_MTU |
4369 IB_SA_MCMEMBER_REC_HOP_LIMIT;
4371 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4372 id_priv->id.port_num, &rec,
4373 comp_mask, GFP_KERNEL,
4374 cma_ib_mc_handler, mc);
4375 return PTR_ERR_OR_ZERO(mc->multicast.ib);
4378 static void iboe_mcast_work_handler(struct work_struct *work)
4380 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
4381 struct cma_multicast *mc = mw->mc;
4382 struct ib_sa_multicast *m = mc->multicast.ib;
4384 mc->multicast.ib->context = mc;
4385 cma_ib_mc_handler(0, m);
4386 kref_put(&mc->mcref, release_mc);
4387 kfree(mw);
4390 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4391 enum ib_gid_type gid_type)
4393 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4394 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4396 if (cma_any_addr(addr)) {
4397 memset(mgid, 0, sizeof *mgid);
4398 } else if (addr->sa_family == AF_INET6) {
4399 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4400 } else {
4401 mgid->raw[0] =
4402 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4403 mgid->raw[1] =
4404 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4405 mgid->raw[2] = 0;
4406 mgid->raw[3] = 0;
4407 mgid->raw[4] = 0;
4408 mgid->raw[5] = 0;
4409 mgid->raw[6] = 0;
4410 mgid->raw[7] = 0;
4411 mgid->raw[8] = 0;
4412 mgid->raw[9] = 0;
4413 mgid->raw[10] = 0xff;
4414 mgid->raw[11] = 0xff;
4415 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4419 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4420 struct cma_multicast *mc)
4422 struct iboe_mcast_work *work;
4423 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4424 int err = 0;
4425 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4426 struct net_device *ndev = NULL;
4427 enum ib_gid_type gid_type;
4428 bool send_only;
4430 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4432 if (cma_zero_addr((struct sockaddr *)&mc->addr))
4433 return -EINVAL;
4435 work = kzalloc(sizeof *work, GFP_KERNEL);
4436 if (!work)
4437 return -ENOMEM;
4439 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
4440 if (!mc->multicast.ib) {
4441 err = -ENOMEM;
4442 goto out1;
4445 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4446 rdma_start_port(id_priv->cma_dev->device)];
4447 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid, gid_type);
4449 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
4450 if (id_priv->id.ps == RDMA_PS_UDP)
4451 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4453 if (dev_addr->bound_dev_if)
4454 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4455 if (!ndev) {
4456 err = -ENODEV;
4457 goto out2;
4459 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
4460 mc->multicast.ib->rec.hop_limit = 1;
4461 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
4463 if (addr->sa_family == AF_INET) {
4464 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4465 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4466 if (!send_only) {
4467 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
4468 true);
4471 } else {
4472 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4473 err = -ENOTSUPP;
4475 dev_put(ndev);
4476 if (err || !mc->multicast.ib->rec.mtu) {
4477 if (!err)
4478 err = -EINVAL;
4479 goto out2;
4481 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4482 &mc->multicast.ib->rec.port_gid);
4483 work->id = id_priv;
4484 work->mc = mc;
4485 INIT_WORK(&work->work, iboe_mcast_work_handler);
4486 kref_get(&mc->mcref);
4487 queue_work(cma_wq, &work->work);
4489 return 0;
4491 out2:
4492 kfree(mc->multicast.ib);
4493 out1:
4494 kfree(work);
4495 return err;
4498 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4499 u8 join_state, void *context)
4501 struct rdma_id_private *id_priv;
4502 struct cma_multicast *mc;
4503 int ret;
4505 if (!id->device)
4506 return -EINVAL;
4508 id_priv = container_of(id, struct rdma_id_private, id);
4509 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4510 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
4511 return -EINVAL;
4513 mc = kmalloc(sizeof *mc, GFP_KERNEL);
4514 if (!mc)
4515 return -ENOMEM;
4517 memcpy(&mc->addr, addr, rdma_addr_size(addr));
4518 mc->context = context;
4519 mc->id_priv = id_priv;
4520 mc->join_state = join_state;
4522 if (rdma_protocol_roce(id->device, id->port_num)) {
4523 kref_init(&mc->mcref);
4524 ret = cma_iboe_join_multicast(id_priv, mc);
4525 if (ret)
4526 goto out_err;
4527 } else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4528 ret = cma_join_ib_multicast(id_priv, mc);
4529 if (ret)
4530 goto out_err;
4531 } else {
4532 ret = -ENOSYS;
4533 goto out_err;
4536 spin_lock(&id_priv->lock);
4537 list_add(&mc->list, &id_priv->mc_list);
4538 spin_unlock(&id_priv->lock);
4540 return 0;
4541 out_err:
4542 kfree(mc);
4543 return ret;
4545 EXPORT_SYMBOL(rdma_join_multicast);
4547 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4549 struct rdma_id_private *id_priv;
4550 struct cma_multicast *mc;
4552 id_priv = container_of(id, struct rdma_id_private, id);
4553 spin_lock_irq(&id_priv->lock);
4554 list_for_each_entry(mc, &id_priv->mc_list, list) {
4555 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
4556 list_del(&mc->list);
4557 spin_unlock_irq(&id_priv->lock);
4559 if (id->qp)
4560 ib_detach_mcast(id->qp,
4561 &mc->multicast.ib->rec.mgid,
4562 be16_to_cpu(mc->multicast.ib->rec.mlid));
4564 BUG_ON(id_priv->cma_dev->device != id->device);
4566 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4567 ib_sa_free_multicast(mc->multicast.ib);
4568 kfree(mc);
4569 } else if (rdma_protocol_roce(id->device, id->port_num)) {
4570 cma_leave_roce_mc_group(id_priv, mc);
4572 return;
4575 spin_unlock_irq(&id_priv->lock);
4577 EXPORT_SYMBOL(rdma_leave_multicast);
4579 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4581 struct rdma_dev_addr *dev_addr;
4582 struct cma_ndev_work *work;
4584 dev_addr = &id_priv->id.route.addr.dev_addr;
4586 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4587 (net_eq(dev_net(ndev), dev_addr->net)) &&
4588 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4589 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4590 ndev->name, &id_priv->id);
4591 work = kzalloc(sizeof *work, GFP_KERNEL);
4592 if (!work)
4593 return -ENOMEM;
4595 INIT_WORK(&work->work, cma_ndev_work_handler);
4596 work->id = id_priv;
4597 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4598 cma_id_get(id_priv);
4599 queue_work(cma_wq, &work->work);
4602 return 0;
4605 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4606 void *ptr)
4608 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4609 struct cma_device *cma_dev;
4610 struct rdma_id_private *id_priv;
4611 int ret = NOTIFY_DONE;
4613 if (event != NETDEV_BONDING_FAILOVER)
4614 return NOTIFY_DONE;
4616 if (!netif_is_bond_master(ndev))
4617 return NOTIFY_DONE;
4619 mutex_lock(&lock);
4620 list_for_each_entry(cma_dev, &dev_list, list)
4621 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4622 ret = cma_netdev_change(ndev, id_priv);
4623 if (ret)
4624 goto out;
4627 out:
4628 mutex_unlock(&lock);
4629 return ret;
4632 static struct notifier_block cma_nb = {
4633 .notifier_call = cma_netdev_callback
4636 static void cma_add_one(struct ib_device *device)
4638 struct cma_device *cma_dev;
4639 struct rdma_id_private *id_priv;
4640 unsigned int i;
4641 unsigned long supported_gids = 0;
4643 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4644 if (!cma_dev)
4645 return;
4647 cma_dev->device = device;
4648 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4649 sizeof(*cma_dev->default_gid_type),
4650 GFP_KERNEL);
4651 if (!cma_dev->default_gid_type)
4652 goto free_cma_dev;
4654 cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
4655 sizeof(*cma_dev->default_roce_tos),
4656 GFP_KERNEL);
4657 if (!cma_dev->default_roce_tos)
4658 goto free_gid_type;
4660 rdma_for_each_port (device, i) {
4661 supported_gids = roce_gid_type_mask_support(device, i);
4662 WARN_ON(!supported_gids);
4663 if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
4664 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4665 CMA_PREFERRED_ROCE_GID_TYPE;
4666 else
4667 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4668 find_first_bit(&supported_gids, BITS_PER_LONG);
4669 cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
4672 init_completion(&cma_dev->comp);
4673 refcount_set(&cma_dev->refcount, 1);
4674 INIT_LIST_HEAD(&cma_dev->id_list);
4675 ib_set_client_data(device, &cma_client, cma_dev);
4677 mutex_lock(&lock);
4678 list_add_tail(&cma_dev->list, &dev_list);
4679 list_for_each_entry(id_priv, &listen_any_list, list)
4680 cma_listen_on_dev(id_priv, cma_dev);
4681 mutex_unlock(&lock);
4683 trace_cm_add_one(device);
4684 return;
4686 free_gid_type:
4687 kfree(cma_dev->default_gid_type);
4689 free_cma_dev:
4690 kfree(cma_dev);
4692 return;
4695 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4697 struct rdma_cm_event event = {};
4698 enum rdma_cm_state state;
4699 int ret = 0;
4701 /* Record that we want to remove the device */
4702 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4703 if (state == RDMA_CM_DESTROYING)
4704 return 0;
4706 cma_cancel_operation(id_priv, state);
4707 mutex_lock(&id_priv->handler_mutex);
4709 /* Check for destruction from another callback. */
4710 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
4711 goto out;
4713 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
4714 ret = cma_cm_event_handler(id_priv, &event);
4715 out:
4716 mutex_unlock(&id_priv->handler_mutex);
4717 return ret;
4720 static void cma_process_remove(struct cma_device *cma_dev)
4722 struct rdma_id_private *id_priv;
4723 int ret;
4725 mutex_lock(&lock);
4726 while (!list_empty(&cma_dev->id_list)) {
4727 id_priv = list_entry(cma_dev->id_list.next,
4728 struct rdma_id_private, list);
4730 list_del(&id_priv->listen_list);
4731 list_del_init(&id_priv->list);
4732 cma_id_get(id_priv);
4733 mutex_unlock(&lock);
4735 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
4736 cma_id_put(id_priv);
4737 if (ret)
4738 rdma_destroy_id(&id_priv->id);
4740 mutex_lock(&lock);
4742 mutex_unlock(&lock);
4744 cma_dev_put(cma_dev);
4745 wait_for_completion(&cma_dev->comp);
4748 static void cma_remove_one(struct ib_device *device, void *client_data)
4750 struct cma_device *cma_dev = client_data;
4752 trace_cm_remove_one(device);
4754 if (!cma_dev)
4755 return;
4757 mutex_lock(&lock);
4758 list_del(&cma_dev->list);
4759 mutex_unlock(&lock);
4761 cma_process_remove(cma_dev);
4762 kfree(cma_dev->default_roce_tos);
4763 kfree(cma_dev->default_gid_type);
4764 kfree(cma_dev);
4767 static int cma_init_net(struct net *net)
4769 struct cma_pernet *pernet = cma_pernet(net);
4771 xa_init(&pernet->tcp_ps);
4772 xa_init(&pernet->udp_ps);
4773 xa_init(&pernet->ipoib_ps);
4774 xa_init(&pernet->ib_ps);
4776 return 0;
4779 static void cma_exit_net(struct net *net)
4781 struct cma_pernet *pernet = cma_pernet(net);
4783 WARN_ON(!xa_empty(&pernet->tcp_ps));
4784 WARN_ON(!xa_empty(&pernet->udp_ps));
4785 WARN_ON(!xa_empty(&pernet->ipoib_ps));
4786 WARN_ON(!xa_empty(&pernet->ib_ps));
4789 static struct pernet_operations cma_pernet_operations = {
4790 .init = cma_init_net,
4791 .exit = cma_exit_net,
4792 .id = &cma_pernet_id,
4793 .size = sizeof(struct cma_pernet),
4796 static int __init cma_init(void)
4798 int ret;
4801 * There is a rare lock ordering dependency in cma_netdev_callback()
4802 * that only happens when bonding is enabled. Teach lockdep that rtnl
4803 * must never be nested under lock so it can find these without having
4804 * to test with bonding.
4806 if (IS_ENABLED(CONFIG_LOCKDEP)) {
4807 rtnl_lock();
4808 mutex_lock(&lock);
4809 mutex_unlock(&lock);
4810 rtnl_unlock();
4813 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4814 if (!cma_wq)
4815 return -ENOMEM;
4817 ret = register_pernet_subsys(&cma_pernet_operations);
4818 if (ret)
4819 goto err_wq;
4821 ib_sa_register_client(&sa_client);
4822 register_netdevice_notifier(&cma_nb);
4824 ret = ib_register_client(&cma_client);
4825 if (ret)
4826 goto err;
4828 ret = cma_configfs_init();
4829 if (ret)
4830 goto err_ib;
4832 return 0;
4834 err_ib:
4835 ib_unregister_client(&cma_client);
4836 err:
4837 unregister_netdevice_notifier(&cma_nb);
4838 ib_sa_unregister_client(&sa_client);
4839 unregister_pernet_subsys(&cma_pernet_operations);
4840 err_wq:
4841 destroy_workqueue(cma_wq);
4842 return ret;
4845 static void __exit cma_cleanup(void)
4847 cma_configfs_exit();
4848 ib_unregister_client(&cma_client);
4849 unregister_netdevice_notifier(&cma_nb);
4850 ib_sa_unregister_client(&sa_client);
4851 unregister_pernet_subsys(&cma_pernet_operations);
4852 destroy_workqueue(cma_wq);
4855 module_init(cma_init);
4856 module_exit(cma_cleanup);