Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / infiniband / core / cma.c
blob31bb82d8ecd7f19bbee90bd95a83cec7fe5abca7
1 /*
2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
36 #include <linux/completion.h>
37 #include <linux/in.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/igmp.h>
42 #include <linux/idr.h>
43 #include <linux/inetdevice.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <net/route.h>
48 #include <net/net_namespace.h>
49 #include <net/netns/generic.h>
50 #include <net/tcp.h>
51 #include <net/ipv6.h>
52 #include <net/ip_fib.h>
53 #include <net/ip6_route.h>
55 #include <rdma/rdma_cm.h>
56 #include <rdma/rdma_cm_ib.h>
57 #include <rdma/rdma_netlink.h>
58 #include <rdma/ib.h>
59 #include <rdma/ib_cache.h>
60 #include <rdma/ib_cm.h>
61 #include <rdma/ib_sa.h>
62 #include <rdma/iw_cm.h>
64 #include "core_priv.h"
66 MODULE_AUTHOR("Sean Hefty");
67 MODULE_DESCRIPTION("Generic RDMA CM Agent");
68 MODULE_LICENSE("Dual BSD/GPL");
70 #define CMA_CM_RESPONSE_TIMEOUT 20
71 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
72 #define CMA_MAX_CM_RETRIES 15
73 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
74 #define CMA_IBOE_PACKET_LIFETIME 18
76 static const char * const cma_events[] = {
77 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
78 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
79 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
80 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
81 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
82 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
83 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
84 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
85 [RDMA_CM_EVENT_REJECTED] = "rejected",
86 [RDMA_CM_EVENT_ESTABLISHED] = "established",
87 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
88 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
89 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
90 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
91 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
92 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
95 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
97 size_t index = event;
99 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
100 cma_events[index] : "unrecognized event";
102 EXPORT_SYMBOL(rdma_event_msg);
104 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
105 int reason)
107 if (rdma_ib_or_roce(id->device, id->port_num))
108 return ibcm_reject_msg(reason);
110 if (rdma_protocol_iwarp(id->device, id->port_num))
111 return iwcm_reject_msg(reason);
113 WARN_ON_ONCE(1);
114 return "unrecognized transport";
116 EXPORT_SYMBOL(rdma_reject_msg);
118 bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
120 if (rdma_ib_or_roce(id->device, id->port_num))
121 return reason == IB_CM_REJ_CONSUMER_DEFINED;
123 if (rdma_protocol_iwarp(id->device, id->port_num))
124 return reason == -ECONNREFUSED;
126 WARN_ON_ONCE(1);
127 return false;
129 EXPORT_SYMBOL(rdma_is_consumer_reject);
131 const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
132 struct rdma_cm_event *ev, u8 *data_len)
134 const void *p;
136 if (rdma_is_consumer_reject(id, ev->status)) {
137 *data_len = ev->param.conn.private_data_len;
138 p = ev->param.conn.private_data;
139 } else {
140 *data_len = 0;
141 p = NULL;
143 return p;
145 EXPORT_SYMBOL(rdma_consumer_reject_data);
147 static void cma_add_one(struct ib_device *device);
148 static void cma_remove_one(struct ib_device *device, void *client_data);
150 static struct ib_client cma_client = {
151 .name = "cma",
152 .add = cma_add_one,
153 .remove = cma_remove_one
156 static struct ib_sa_client sa_client;
157 static struct rdma_addr_client addr_client;
158 static LIST_HEAD(dev_list);
159 static LIST_HEAD(listen_any_list);
160 static DEFINE_MUTEX(lock);
161 static struct workqueue_struct *cma_wq;
162 static unsigned int cma_pernet_id;
164 struct cma_pernet {
165 struct idr tcp_ps;
166 struct idr udp_ps;
167 struct idr ipoib_ps;
168 struct idr ib_ps;
171 static struct cma_pernet *cma_pernet(struct net *net)
173 return net_generic(net, cma_pernet_id);
176 static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
178 struct cma_pernet *pernet = cma_pernet(net);
180 switch (ps) {
181 case RDMA_PS_TCP:
182 return &pernet->tcp_ps;
183 case RDMA_PS_UDP:
184 return &pernet->udp_ps;
185 case RDMA_PS_IPOIB:
186 return &pernet->ipoib_ps;
187 case RDMA_PS_IB:
188 return &pernet->ib_ps;
189 default:
190 return NULL;
194 struct cma_device {
195 struct list_head list;
196 struct ib_device *device;
197 struct completion comp;
198 atomic_t refcount;
199 struct list_head id_list;
200 enum ib_gid_type *default_gid_type;
201 u8 *default_roce_tos;
204 struct rdma_bind_list {
205 enum rdma_port_space ps;
206 struct hlist_head owners;
207 unsigned short port;
210 struct class_port_info_context {
211 struct ib_class_port_info *class_port_info;
212 struct ib_device *device;
213 struct completion done;
214 struct ib_sa_query *sa_query;
215 u8 port_num;
218 static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
219 struct rdma_bind_list *bind_list, int snum)
221 struct idr *idr = cma_pernet_idr(net, ps);
223 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
226 static struct rdma_bind_list *cma_ps_find(struct net *net,
227 enum rdma_port_space ps, int snum)
229 struct idr *idr = cma_pernet_idr(net, ps);
231 return idr_find(idr, snum);
234 static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
236 struct idr *idr = cma_pernet_idr(net, ps);
238 idr_remove(idr, snum);
241 enum {
242 CMA_OPTION_AFONLY,
245 void cma_ref_dev(struct cma_device *cma_dev)
247 atomic_inc(&cma_dev->refcount);
250 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
251 void *cookie)
253 struct cma_device *cma_dev;
254 struct cma_device *found_cma_dev = NULL;
256 mutex_lock(&lock);
258 list_for_each_entry(cma_dev, &dev_list, list)
259 if (filter(cma_dev->device, cookie)) {
260 found_cma_dev = cma_dev;
261 break;
264 if (found_cma_dev)
265 cma_ref_dev(found_cma_dev);
266 mutex_unlock(&lock);
267 return found_cma_dev;
270 int cma_get_default_gid_type(struct cma_device *cma_dev,
271 unsigned int port)
273 if (!rdma_is_port_valid(cma_dev->device, port))
274 return -EINVAL;
276 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
279 int cma_set_default_gid_type(struct cma_device *cma_dev,
280 unsigned int port,
281 enum ib_gid_type default_gid_type)
283 unsigned long supported_gids;
285 if (!rdma_is_port_valid(cma_dev->device, port))
286 return -EINVAL;
288 supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
290 if (!(supported_gids & 1 << default_gid_type))
291 return -EINVAL;
293 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
294 default_gid_type;
296 return 0;
299 int cma_get_default_roce_tos(struct cma_device *cma_dev, unsigned int port)
301 if (!rdma_is_port_valid(cma_dev->device, port))
302 return -EINVAL;
304 return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
307 int cma_set_default_roce_tos(struct cma_device *cma_dev, unsigned int port,
308 u8 default_roce_tos)
310 if (!rdma_is_port_valid(cma_dev->device, port))
311 return -EINVAL;
313 cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
314 default_roce_tos;
316 return 0;
318 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
320 return cma_dev->device;
324 * Device removal can occur at anytime, so we need extra handling to
325 * serialize notifying the user of device removal with other callbacks.
326 * We do this by disabling removal notification while a callback is in process,
327 * and reporting it after the callback completes.
329 struct rdma_id_private {
330 struct rdma_cm_id id;
332 struct rdma_bind_list *bind_list;
333 struct hlist_node node;
334 struct list_head list; /* listen_any_list or cma_device.list */
335 struct list_head listen_list; /* per device listens */
336 struct cma_device *cma_dev;
337 struct list_head mc_list;
339 int internal_id;
340 enum rdma_cm_state state;
341 spinlock_t lock;
342 struct mutex qp_mutex;
344 struct completion comp;
345 atomic_t refcount;
346 struct mutex handler_mutex;
348 int backlog;
349 int timeout_ms;
350 struct ib_sa_query *query;
351 int query_id;
352 union {
353 struct ib_cm_id *ib;
354 struct iw_cm_id *iw;
355 } cm_id;
357 u32 seq_num;
358 u32 qkey;
359 u32 qp_num;
360 pid_t owner;
361 u32 options;
362 u8 srq;
363 u8 tos;
364 bool tos_set;
365 u8 reuseaddr;
366 u8 afonly;
367 enum ib_gid_type gid_type;
370 struct cma_multicast {
371 struct rdma_id_private *id_priv;
372 union {
373 struct ib_sa_multicast *ib;
374 } multicast;
375 struct list_head list;
376 void *context;
377 struct sockaddr_storage addr;
378 struct kref mcref;
379 bool igmp_joined;
380 u8 join_state;
383 struct cma_work {
384 struct work_struct work;
385 struct rdma_id_private *id;
386 enum rdma_cm_state old_state;
387 enum rdma_cm_state new_state;
388 struct rdma_cm_event event;
391 struct cma_ndev_work {
392 struct work_struct work;
393 struct rdma_id_private *id;
394 struct rdma_cm_event event;
397 struct iboe_mcast_work {
398 struct work_struct work;
399 struct rdma_id_private *id;
400 struct cma_multicast *mc;
403 union cma_ip_addr {
404 struct in6_addr ip6;
405 struct {
406 __be32 pad[3];
407 __be32 addr;
408 } ip4;
411 struct cma_hdr {
412 u8 cma_version;
413 u8 ip_version; /* IP version: 7:4 */
414 __be16 port;
415 union cma_ip_addr src_addr;
416 union cma_ip_addr dst_addr;
419 #define CMA_VERSION 0x00
421 struct cma_req_info {
422 struct ib_device *device;
423 int port;
424 union ib_gid local_gid;
425 __be64 service_id;
426 u16 pkey;
427 bool has_gid:1;
430 static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
432 unsigned long flags;
433 int ret;
435 spin_lock_irqsave(&id_priv->lock, flags);
436 ret = (id_priv->state == comp);
437 spin_unlock_irqrestore(&id_priv->lock, flags);
438 return ret;
441 static int cma_comp_exch(struct rdma_id_private *id_priv,
442 enum rdma_cm_state comp, enum rdma_cm_state exch)
444 unsigned long flags;
445 int ret;
447 spin_lock_irqsave(&id_priv->lock, flags);
448 if ((ret = (id_priv->state == comp)))
449 id_priv->state = exch;
450 spin_unlock_irqrestore(&id_priv->lock, flags);
451 return ret;
454 static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
455 enum rdma_cm_state exch)
457 unsigned long flags;
458 enum rdma_cm_state old;
460 spin_lock_irqsave(&id_priv->lock, flags);
461 old = id_priv->state;
462 id_priv->state = exch;
463 spin_unlock_irqrestore(&id_priv->lock, flags);
464 return old;
467 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
469 return hdr->ip_version >> 4;
472 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
474 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
477 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
479 struct in_device *in_dev = NULL;
481 if (ndev) {
482 rtnl_lock();
483 in_dev = __in_dev_get_rtnl(ndev);
484 if (in_dev) {
485 if (join)
486 ip_mc_inc_group(in_dev,
487 *(__be32 *)(mgid->raw + 12));
488 else
489 ip_mc_dec_group(in_dev,
490 *(__be32 *)(mgid->raw + 12));
492 rtnl_unlock();
494 return (in_dev) ? 0 : -ENODEV;
497 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
498 struct cma_device *cma_dev)
500 cma_ref_dev(cma_dev);
501 id_priv->cma_dev = cma_dev;
502 id_priv->gid_type = 0;
503 id_priv->id.device = cma_dev->device;
504 id_priv->id.route.addr.dev_addr.transport =
505 rdma_node_get_transport(cma_dev->device->node_type);
506 list_add_tail(&id_priv->list, &cma_dev->id_list);
509 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
510 struct cma_device *cma_dev)
512 _cma_attach_to_dev(id_priv, cma_dev);
513 id_priv->gid_type =
514 cma_dev->default_gid_type[id_priv->id.port_num -
515 rdma_start_port(cma_dev->device)];
518 void cma_deref_dev(struct cma_device *cma_dev)
520 if (atomic_dec_and_test(&cma_dev->refcount))
521 complete(&cma_dev->comp);
524 static inline void release_mc(struct kref *kref)
526 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
528 kfree(mc->multicast.ib);
529 kfree(mc);
532 static void cma_release_dev(struct rdma_id_private *id_priv)
534 mutex_lock(&lock);
535 list_del(&id_priv->list);
536 cma_deref_dev(id_priv->cma_dev);
537 id_priv->cma_dev = NULL;
538 mutex_unlock(&lock);
541 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
543 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
546 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
548 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
551 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
553 return id_priv->id.route.addr.src_addr.ss_family;
556 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
558 struct ib_sa_mcmember_rec rec;
559 int ret = 0;
561 if (id_priv->qkey) {
562 if (qkey && id_priv->qkey != qkey)
563 return -EINVAL;
564 return 0;
567 if (qkey) {
568 id_priv->qkey = qkey;
569 return 0;
572 switch (id_priv->id.ps) {
573 case RDMA_PS_UDP:
574 case RDMA_PS_IB:
575 id_priv->qkey = RDMA_UDP_QKEY;
576 break;
577 case RDMA_PS_IPOIB:
578 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
579 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
580 id_priv->id.port_num, &rec.mgid,
581 &rec);
582 if (!ret)
583 id_priv->qkey = be32_to_cpu(rec.qkey);
584 break;
585 default:
586 break;
588 return ret;
591 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
593 dev_addr->dev_type = ARPHRD_INFINIBAND;
594 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
595 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
598 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
600 int ret;
602 if (addr->sa_family != AF_IB) {
603 ret = rdma_translate_ip(addr, dev_addr, NULL);
604 } else {
605 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
606 ret = 0;
609 return ret;
612 static inline int cma_validate_port(struct ib_device *device, u8 port,
613 enum ib_gid_type gid_type,
614 union ib_gid *gid, int dev_type,
615 int bound_if_index)
617 int ret = -ENODEV;
618 struct net_device *ndev = NULL;
620 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
621 return ret;
623 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
624 return ret;
626 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
627 ndev = dev_get_by_index(&init_net, bound_if_index);
628 if (ndev && ndev->flags & IFF_LOOPBACK) {
629 pr_info("detected loopback device\n");
630 dev_put(ndev);
632 if (!device->get_netdev)
633 return -EOPNOTSUPP;
635 ndev = device->get_netdev(device, port);
636 if (!ndev)
637 return -ENODEV;
639 } else {
640 gid_type = IB_GID_TYPE_IB;
643 ret = ib_find_cached_gid_by_port(device, gid, gid_type, port,
644 ndev, NULL);
646 if (ndev)
647 dev_put(ndev);
649 return ret;
652 static int cma_acquire_dev(struct rdma_id_private *id_priv,
653 struct rdma_id_private *listen_id_priv)
655 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
656 struct cma_device *cma_dev;
657 union ib_gid gid, iboe_gid, *gidp;
658 int ret = -ENODEV;
659 u8 port;
661 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
662 id_priv->id.ps == RDMA_PS_IPOIB)
663 return -EINVAL;
665 mutex_lock(&lock);
666 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
667 &iboe_gid);
669 memcpy(&gid, dev_addr->src_dev_addr +
670 rdma_addr_gid_offset(dev_addr), sizeof gid);
672 if (listen_id_priv) {
673 cma_dev = listen_id_priv->cma_dev;
674 port = listen_id_priv->id.port_num;
675 gidp = rdma_protocol_roce(cma_dev->device, port) ?
676 &iboe_gid : &gid;
678 ret = cma_validate_port(cma_dev->device, port,
679 rdma_protocol_ib(cma_dev->device, port) ?
680 IB_GID_TYPE_IB :
681 listen_id_priv->gid_type, gidp,
682 dev_addr->dev_type,
683 dev_addr->bound_dev_if);
684 if (!ret) {
685 id_priv->id.port_num = port;
686 goto out;
690 list_for_each_entry(cma_dev, &dev_list, list) {
691 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
692 if (listen_id_priv &&
693 listen_id_priv->cma_dev == cma_dev &&
694 listen_id_priv->id.port_num == port)
695 continue;
697 gidp = rdma_protocol_roce(cma_dev->device, port) ?
698 &iboe_gid : &gid;
700 ret = cma_validate_port(cma_dev->device, port,
701 rdma_protocol_ib(cma_dev->device, port) ?
702 IB_GID_TYPE_IB :
703 cma_dev->default_gid_type[port - 1],
704 gidp, dev_addr->dev_type,
705 dev_addr->bound_dev_if);
706 if (!ret) {
707 id_priv->id.port_num = port;
708 goto out;
713 out:
714 if (!ret)
715 cma_attach_to_dev(id_priv, cma_dev);
717 mutex_unlock(&lock);
718 return ret;
722 * Select the source IB device and address to reach the destination IB address.
724 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
726 struct cma_device *cma_dev, *cur_dev;
727 struct sockaddr_ib *addr;
728 union ib_gid gid, sgid, *dgid;
729 u16 pkey, index;
730 u8 p;
731 enum ib_port_state port_state;
732 int i;
734 cma_dev = NULL;
735 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
736 dgid = (union ib_gid *) &addr->sib_addr;
737 pkey = ntohs(addr->sib_pkey);
739 list_for_each_entry(cur_dev, &dev_list, list) {
740 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
741 if (!rdma_cap_af_ib(cur_dev->device, p))
742 continue;
744 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
745 continue;
747 if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
748 continue;
749 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
750 &gid, NULL);
751 i++) {
752 if (!memcmp(&gid, dgid, sizeof(gid))) {
753 cma_dev = cur_dev;
754 sgid = gid;
755 id_priv->id.port_num = p;
756 goto found;
759 if (!cma_dev && (gid.global.subnet_prefix ==
760 dgid->global.subnet_prefix) &&
761 port_state == IB_PORT_ACTIVE) {
762 cma_dev = cur_dev;
763 sgid = gid;
764 id_priv->id.port_num = p;
770 if (!cma_dev)
771 return -ENODEV;
773 found:
774 cma_attach_to_dev(id_priv, cma_dev);
775 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
776 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
777 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
778 return 0;
781 static void cma_deref_id(struct rdma_id_private *id_priv)
783 if (atomic_dec_and_test(&id_priv->refcount))
784 complete(&id_priv->comp);
787 struct rdma_cm_id *rdma_create_id(struct net *net,
788 rdma_cm_event_handler event_handler,
789 void *context, enum rdma_port_space ps,
790 enum ib_qp_type qp_type)
792 struct rdma_id_private *id_priv;
794 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
795 if (!id_priv)
796 return ERR_PTR(-ENOMEM);
798 id_priv->owner = task_pid_nr(current);
799 id_priv->state = RDMA_CM_IDLE;
800 id_priv->id.context = context;
801 id_priv->id.event_handler = event_handler;
802 id_priv->id.ps = ps;
803 id_priv->id.qp_type = qp_type;
804 id_priv->tos_set = false;
805 spin_lock_init(&id_priv->lock);
806 mutex_init(&id_priv->qp_mutex);
807 init_completion(&id_priv->comp);
808 atomic_set(&id_priv->refcount, 1);
809 mutex_init(&id_priv->handler_mutex);
810 INIT_LIST_HEAD(&id_priv->listen_list);
811 INIT_LIST_HEAD(&id_priv->mc_list);
812 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
813 id_priv->id.route.addr.dev_addr.net = get_net(net);
815 return &id_priv->id;
817 EXPORT_SYMBOL(rdma_create_id);
819 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
821 struct ib_qp_attr qp_attr;
822 int qp_attr_mask, ret;
824 qp_attr.qp_state = IB_QPS_INIT;
825 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
826 if (ret)
827 return ret;
829 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
830 if (ret)
831 return ret;
833 qp_attr.qp_state = IB_QPS_RTR;
834 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
835 if (ret)
836 return ret;
838 qp_attr.qp_state = IB_QPS_RTS;
839 qp_attr.sq_psn = 0;
840 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
842 return ret;
845 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
847 struct ib_qp_attr qp_attr;
848 int qp_attr_mask, ret;
850 qp_attr.qp_state = IB_QPS_INIT;
851 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
852 if (ret)
853 return ret;
855 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
858 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
859 struct ib_qp_init_attr *qp_init_attr)
861 struct rdma_id_private *id_priv;
862 struct ib_qp *qp;
863 int ret;
865 id_priv = container_of(id, struct rdma_id_private, id);
866 if (id->device != pd->device)
867 return -EINVAL;
869 qp_init_attr->port_num = id->port_num;
870 qp = ib_create_qp(pd, qp_init_attr);
871 if (IS_ERR(qp))
872 return PTR_ERR(qp);
874 if (id->qp_type == IB_QPT_UD)
875 ret = cma_init_ud_qp(id_priv, qp);
876 else
877 ret = cma_init_conn_qp(id_priv, qp);
878 if (ret)
879 goto err;
881 id->qp = qp;
882 id_priv->qp_num = qp->qp_num;
883 id_priv->srq = (qp->srq != NULL);
884 return 0;
885 err:
886 ib_destroy_qp(qp);
887 return ret;
889 EXPORT_SYMBOL(rdma_create_qp);
891 void rdma_destroy_qp(struct rdma_cm_id *id)
893 struct rdma_id_private *id_priv;
895 id_priv = container_of(id, struct rdma_id_private, id);
896 mutex_lock(&id_priv->qp_mutex);
897 ib_destroy_qp(id_priv->id.qp);
898 id_priv->id.qp = NULL;
899 mutex_unlock(&id_priv->qp_mutex);
901 EXPORT_SYMBOL(rdma_destroy_qp);
903 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
904 struct rdma_conn_param *conn_param)
906 struct ib_qp_attr qp_attr;
907 int qp_attr_mask, ret;
908 union ib_gid sgid;
910 mutex_lock(&id_priv->qp_mutex);
911 if (!id_priv->id.qp) {
912 ret = 0;
913 goto out;
916 /* Need to update QP attributes from default values. */
917 qp_attr.qp_state = IB_QPS_INIT;
918 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
919 if (ret)
920 goto out;
922 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
923 if (ret)
924 goto out;
926 qp_attr.qp_state = IB_QPS_RTR;
927 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
928 if (ret)
929 goto out;
931 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
932 rdma_ah_read_grh(&qp_attr.ah_attr)->sgid_index,
933 &sgid, NULL);
934 if (ret)
935 goto out;
937 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
939 if (conn_param)
940 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
941 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
942 out:
943 mutex_unlock(&id_priv->qp_mutex);
944 return ret;
947 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
948 struct rdma_conn_param *conn_param)
950 struct ib_qp_attr qp_attr;
951 int qp_attr_mask, ret;
953 mutex_lock(&id_priv->qp_mutex);
954 if (!id_priv->id.qp) {
955 ret = 0;
956 goto out;
959 qp_attr.qp_state = IB_QPS_RTS;
960 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
961 if (ret)
962 goto out;
964 if (conn_param)
965 qp_attr.max_rd_atomic = conn_param->initiator_depth;
966 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
967 out:
968 mutex_unlock(&id_priv->qp_mutex);
969 return ret;
972 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
974 struct ib_qp_attr qp_attr;
975 int ret;
977 mutex_lock(&id_priv->qp_mutex);
978 if (!id_priv->id.qp) {
979 ret = 0;
980 goto out;
983 qp_attr.qp_state = IB_QPS_ERR;
984 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
985 out:
986 mutex_unlock(&id_priv->qp_mutex);
987 return ret;
990 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
991 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
993 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
994 int ret;
995 u16 pkey;
997 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
998 pkey = 0xffff;
999 else
1000 pkey = ib_addr_get_pkey(dev_addr);
1002 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
1003 pkey, &qp_attr->pkey_index);
1004 if (ret)
1005 return ret;
1007 qp_attr->port_num = id_priv->id.port_num;
1008 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1010 if (id_priv->id.qp_type == IB_QPT_UD) {
1011 ret = cma_set_qkey(id_priv, 0);
1012 if (ret)
1013 return ret;
1015 qp_attr->qkey = id_priv->qkey;
1016 *qp_attr_mask |= IB_QP_QKEY;
1017 } else {
1018 qp_attr->qp_access_flags = 0;
1019 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1021 return 0;
1024 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1025 int *qp_attr_mask)
1027 struct rdma_id_private *id_priv;
1028 int ret = 0;
1030 id_priv = container_of(id, struct rdma_id_private, id);
1031 if (rdma_cap_ib_cm(id->device, id->port_num)) {
1032 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1033 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1034 else
1035 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1036 qp_attr_mask);
1038 if (qp_attr->qp_state == IB_QPS_RTR)
1039 qp_attr->rq_psn = id_priv->seq_num;
1040 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1041 if (!id_priv->cm_id.iw) {
1042 qp_attr->qp_access_flags = 0;
1043 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1044 } else
1045 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1046 qp_attr_mask);
1047 } else
1048 ret = -ENOSYS;
1050 return ret;
1052 EXPORT_SYMBOL(rdma_init_qp_attr);
1054 static inline int cma_zero_addr(struct sockaddr *addr)
1056 switch (addr->sa_family) {
1057 case AF_INET:
1058 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1059 case AF_INET6:
1060 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
1061 case AF_IB:
1062 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
1063 default:
1064 return 0;
1068 static inline int cma_loopback_addr(struct sockaddr *addr)
1070 switch (addr->sa_family) {
1071 case AF_INET:
1072 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
1073 case AF_INET6:
1074 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
1075 case AF_IB:
1076 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
1077 default:
1078 return 0;
1082 static inline int cma_any_addr(struct sockaddr *addr)
1084 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1087 static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
1089 if (src->sa_family != dst->sa_family)
1090 return -1;
1092 switch (src->sa_family) {
1093 case AF_INET:
1094 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
1095 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
1096 case AF_INET6:
1097 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
1098 &((struct sockaddr_in6 *) dst)->sin6_addr);
1099 default:
1100 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1101 &((struct sockaddr_ib *) dst)->sib_addr);
1105 static __be16 cma_port(struct sockaddr *addr)
1107 struct sockaddr_ib *sib;
1109 switch (addr->sa_family) {
1110 case AF_INET:
1111 return ((struct sockaddr_in *) addr)->sin_port;
1112 case AF_INET6:
1113 return ((struct sockaddr_in6 *) addr)->sin6_port;
1114 case AF_IB:
1115 sib = (struct sockaddr_ib *) addr;
1116 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1117 be64_to_cpu(sib->sib_sid_mask)));
1118 default:
1119 return 0;
1123 static inline int cma_any_port(struct sockaddr *addr)
1125 return !cma_port(addr);
1128 static void cma_save_ib_info(struct sockaddr *src_addr,
1129 struct sockaddr *dst_addr,
1130 struct rdma_cm_id *listen_id,
1131 struct sa_path_rec *path)
1133 struct sockaddr_ib *listen_ib, *ib;
1135 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1136 if (src_addr) {
1137 ib = (struct sockaddr_ib *)src_addr;
1138 ib->sib_family = AF_IB;
1139 if (path) {
1140 ib->sib_pkey = path->pkey;
1141 ib->sib_flowinfo = path->flow_label;
1142 memcpy(&ib->sib_addr, &path->sgid, 16);
1143 ib->sib_sid = path->service_id;
1144 ib->sib_scope_id = 0;
1145 } else {
1146 ib->sib_pkey = listen_ib->sib_pkey;
1147 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1148 ib->sib_addr = listen_ib->sib_addr;
1149 ib->sib_sid = listen_ib->sib_sid;
1150 ib->sib_scope_id = listen_ib->sib_scope_id;
1152 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1154 if (dst_addr) {
1155 ib = (struct sockaddr_ib *)dst_addr;
1156 ib->sib_family = AF_IB;
1157 if (path) {
1158 ib->sib_pkey = path->pkey;
1159 ib->sib_flowinfo = path->flow_label;
1160 memcpy(&ib->sib_addr, &path->dgid, 16);
1165 static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1166 struct sockaddr_in *dst_addr,
1167 struct cma_hdr *hdr,
1168 __be16 local_port)
1170 if (src_addr) {
1171 *src_addr = (struct sockaddr_in) {
1172 .sin_family = AF_INET,
1173 .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1174 .sin_port = local_port,
1178 if (dst_addr) {
1179 *dst_addr = (struct sockaddr_in) {
1180 .sin_family = AF_INET,
1181 .sin_addr.s_addr = hdr->src_addr.ip4.addr,
1182 .sin_port = hdr->port,
1187 static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1188 struct sockaddr_in6 *dst_addr,
1189 struct cma_hdr *hdr,
1190 __be16 local_port)
1192 if (src_addr) {
1193 *src_addr = (struct sockaddr_in6) {
1194 .sin6_family = AF_INET6,
1195 .sin6_addr = hdr->dst_addr.ip6,
1196 .sin6_port = local_port,
1200 if (dst_addr) {
1201 *dst_addr = (struct sockaddr_in6) {
1202 .sin6_family = AF_INET6,
1203 .sin6_addr = hdr->src_addr.ip6,
1204 .sin6_port = hdr->port,
1209 static u16 cma_port_from_service_id(__be64 service_id)
1211 return (u16)be64_to_cpu(service_id);
1214 static int cma_save_ip_info(struct sockaddr *src_addr,
1215 struct sockaddr *dst_addr,
1216 struct ib_cm_event *ib_event,
1217 __be64 service_id)
1219 struct cma_hdr *hdr;
1220 __be16 port;
1222 hdr = ib_event->private_data;
1223 if (hdr->cma_version != CMA_VERSION)
1224 return -EINVAL;
1226 port = htons(cma_port_from_service_id(service_id));
1228 switch (cma_get_ip_ver(hdr)) {
1229 case 4:
1230 cma_save_ip4_info((struct sockaddr_in *)src_addr,
1231 (struct sockaddr_in *)dst_addr, hdr, port);
1232 break;
1233 case 6:
1234 cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1235 (struct sockaddr_in6 *)dst_addr, hdr, port);
1236 break;
1237 default:
1238 return -EAFNOSUPPORT;
1241 return 0;
1244 static int cma_save_net_info(struct sockaddr *src_addr,
1245 struct sockaddr *dst_addr,
1246 struct rdma_cm_id *listen_id,
1247 struct ib_cm_event *ib_event,
1248 sa_family_t sa_family, __be64 service_id)
1250 if (sa_family == AF_IB) {
1251 if (ib_event->event == IB_CM_REQ_RECEIVED)
1252 cma_save_ib_info(src_addr, dst_addr, listen_id,
1253 ib_event->param.req_rcvd.primary_path);
1254 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1255 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1256 return 0;
1259 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1262 static int cma_save_req_info(const struct ib_cm_event *ib_event,
1263 struct cma_req_info *req)
1265 const struct ib_cm_req_event_param *req_param =
1266 &ib_event->param.req_rcvd;
1267 const struct ib_cm_sidr_req_event_param *sidr_param =
1268 &ib_event->param.sidr_req_rcvd;
1270 switch (ib_event->event) {
1271 case IB_CM_REQ_RECEIVED:
1272 req->device = req_param->listen_id->device;
1273 req->port = req_param->port;
1274 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1275 sizeof(req->local_gid));
1276 req->has_gid = true;
1277 req->service_id = req_param->primary_path->service_id;
1278 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
1279 if (req->pkey != req_param->bth_pkey)
1280 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1281 "RDMA CMA: in the future this may cause the request to be dropped\n",
1282 req_param->bth_pkey, req->pkey);
1283 break;
1284 case IB_CM_SIDR_REQ_RECEIVED:
1285 req->device = sidr_param->listen_id->device;
1286 req->port = sidr_param->port;
1287 req->has_gid = false;
1288 req->service_id = sidr_param->service_id;
1289 req->pkey = sidr_param->pkey;
1290 if (req->pkey != sidr_param->bth_pkey)
1291 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1292 "RDMA CMA: in the future this may cause the request to be dropped\n",
1293 sidr_param->bth_pkey, req->pkey);
1294 break;
1295 default:
1296 return -EINVAL;
1299 return 0;
1302 static bool validate_ipv4_net_dev(struct net_device *net_dev,
1303 const struct sockaddr_in *dst_addr,
1304 const struct sockaddr_in *src_addr)
1306 __be32 daddr = dst_addr->sin_addr.s_addr,
1307 saddr = src_addr->sin_addr.s_addr;
1308 struct fib_result res;
1309 struct flowi4 fl4;
1310 int err;
1311 bool ret;
1313 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1314 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1315 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1316 ipv4_is_loopback(saddr))
1317 return false;
1319 memset(&fl4, 0, sizeof(fl4));
1320 fl4.flowi4_iif = net_dev->ifindex;
1321 fl4.daddr = daddr;
1322 fl4.saddr = saddr;
1324 rcu_read_lock();
1325 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1326 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1327 rcu_read_unlock();
1329 return ret;
1332 static bool validate_ipv6_net_dev(struct net_device *net_dev,
1333 const struct sockaddr_in6 *dst_addr,
1334 const struct sockaddr_in6 *src_addr)
1336 #if IS_ENABLED(CONFIG_IPV6)
1337 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1338 IPV6_ADDR_LINKLOCAL;
1339 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1340 &src_addr->sin6_addr, net_dev->ifindex,
1341 strict);
1342 bool ret;
1344 if (!rt)
1345 return false;
1347 ret = rt->rt6i_idev->dev == net_dev;
1348 ip6_rt_put(rt);
1350 return ret;
1351 #else
1352 return false;
1353 #endif
1356 static bool validate_net_dev(struct net_device *net_dev,
1357 const struct sockaddr *daddr,
1358 const struct sockaddr *saddr)
1360 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1361 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1362 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1363 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1365 switch (daddr->sa_family) {
1366 case AF_INET:
1367 return saddr->sa_family == AF_INET &&
1368 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1370 case AF_INET6:
1371 return saddr->sa_family == AF_INET6 &&
1372 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1374 default:
1375 return false;
1379 static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1380 const struct cma_req_info *req)
1382 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1383 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1384 *src_addr = (struct sockaddr *)&src_addr_storage;
1385 struct net_device *net_dev;
1386 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1387 int err;
1389 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1390 req->service_id);
1391 if (err)
1392 return ERR_PTR(err);
1394 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1395 gid, listen_addr);
1396 if (!net_dev)
1397 return ERR_PTR(-ENODEV);
1399 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1400 dev_put(net_dev);
1401 return ERR_PTR(-EHOSTUNREACH);
1404 return net_dev;
1407 static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1409 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1412 static bool cma_match_private_data(struct rdma_id_private *id_priv,
1413 const struct cma_hdr *hdr)
1415 struct sockaddr *addr = cma_src_addr(id_priv);
1416 __be32 ip4_addr;
1417 struct in6_addr ip6_addr;
1419 if (cma_any_addr(addr) && !id_priv->afonly)
1420 return true;
1422 switch (addr->sa_family) {
1423 case AF_INET:
1424 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1425 if (cma_get_ip_ver(hdr) != 4)
1426 return false;
1427 if (!cma_any_addr(addr) &&
1428 hdr->dst_addr.ip4.addr != ip4_addr)
1429 return false;
1430 break;
1431 case AF_INET6:
1432 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1433 if (cma_get_ip_ver(hdr) != 6)
1434 return false;
1435 if (!cma_any_addr(addr) &&
1436 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1437 return false;
1438 break;
1439 case AF_IB:
1440 return true;
1441 default:
1442 return false;
1445 return true;
1448 static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1450 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1451 enum rdma_transport_type transport =
1452 rdma_node_get_transport(device->node_type);
1454 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1457 static bool cma_protocol_roce(const struct rdma_cm_id *id)
1459 struct ib_device *device = id->device;
1460 const int port_num = id->port_num ?: rdma_start_port(device);
1462 return cma_protocol_roce_dev_port(device, port_num);
1465 static bool cma_match_net_dev(const struct rdma_cm_id *id,
1466 const struct net_device *net_dev,
1467 u8 port_num)
1469 const struct rdma_addr *addr = &id->route.addr;
1471 if (!net_dev)
1472 /* This request is an AF_IB request or a RoCE request */
1473 return (!id->port_num || id->port_num == port_num) &&
1474 (addr->src_addr.ss_family == AF_IB ||
1475 cma_protocol_roce_dev_port(id->device, port_num));
1477 return !addr->dev_addr.bound_dev_if ||
1478 (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1479 addr->dev_addr.bound_dev_if == net_dev->ifindex);
1482 static struct rdma_id_private *cma_find_listener(
1483 const struct rdma_bind_list *bind_list,
1484 const struct ib_cm_id *cm_id,
1485 const struct ib_cm_event *ib_event,
1486 const struct cma_req_info *req,
1487 const struct net_device *net_dev)
1489 struct rdma_id_private *id_priv, *id_priv_dev;
1491 if (!bind_list)
1492 return ERR_PTR(-EINVAL);
1494 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1495 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1496 if (id_priv->id.device == cm_id->device &&
1497 cma_match_net_dev(&id_priv->id, net_dev, req->port))
1498 return id_priv;
1499 list_for_each_entry(id_priv_dev,
1500 &id_priv->listen_list,
1501 listen_list) {
1502 if (id_priv_dev->id.device == cm_id->device &&
1503 cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
1504 return id_priv_dev;
1509 return ERR_PTR(-EINVAL);
1512 static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
1513 struct ib_cm_event *ib_event,
1514 struct net_device **net_dev)
1516 struct cma_req_info req;
1517 struct rdma_bind_list *bind_list;
1518 struct rdma_id_private *id_priv;
1519 int err;
1521 err = cma_save_req_info(ib_event, &req);
1522 if (err)
1523 return ERR_PTR(err);
1525 *net_dev = cma_get_net_dev(ib_event, &req);
1526 if (IS_ERR(*net_dev)) {
1527 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1528 /* Assuming the protocol is AF_IB */
1529 *net_dev = NULL;
1530 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1531 /* TODO find the net dev matching the request parameters
1532 * through the RoCE GID table */
1533 *net_dev = NULL;
1534 } else {
1535 return ERR_CAST(*net_dev);
1539 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1540 rdma_ps_from_service_id(req.service_id),
1541 cma_port_from_service_id(req.service_id));
1542 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
1543 if (IS_ERR(id_priv) && *net_dev) {
1544 dev_put(*net_dev);
1545 *net_dev = NULL;
1548 return id_priv;
1551 static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
1553 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1556 static void cma_cancel_route(struct rdma_id_private *id_priv)
1558 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1559 if (id_priv->query)
1560 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1564 static void cma_cancel_listens(struct rdma_id_private *id_priv)
1566 struct rdma_id_private *dev_id_priv;
1569 * Remove from listen_any_list to prevent added devices from spawning
1570 * additional listen requests.
1572 mutex_lock(&lock);
1573 list_del(&id_priv->list);
1575 while (!list_empty(&id_priv->listen_list)) {
1576 dev_id_priv = list_entry(id_priv->listen_list.next,
1577 struct rdma_id_private, listen_list);
1578 /* sync with device removal to avoid duplicate destruction */
1579 list_del_init(&dev_id_priv->list);
1580 list_del(&dev_id_priv->listen_list);
1581 mutex_unlock(&lock);
1583 rdma_destroy_id(&dev_id_priv->id);
1584 mutex_lock(&lock);
1586 mutex_unlock(&lock);
1589 static void cma_cancel_operation(struct rdma_id_private *id_priv,
1590 enum rdma_cm_state state)
1592 switch (state) {
1593 case RDMA_CM_ADDR_QUERY:
1594 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1595 break;
1596 case RDMA_CM_ROUTE_QUERY:
1597 cma_cancel_route(id_priv);
1598 break;
1599 case RDMA_CM_LISTEN:
1600 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1601 cma_cancel_listens(id_priv);
1602 break;
1603 default:
1604 break;
1608 static void cma_release_port(struct rdma_id_private *id_priv)
1610 struct rdma_bind_list *bind_list = id_priv->bind_list;
1611 struct net *net = id_priv->id.route.addr.dev_addr.net;
1613 if (!bind_list)
1614 return;
1616 mutex_lock(&lock);
1617 hlist_del(&id_priv->node);
1618 if (hlist_empty(&bind_list->owners)) {
1619 cma_ps_remove(net, bind_list->ps, bind_list->port);
1620 kfree(bind_list);
1622 mutex_unlock(&lock);
1625 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1627 struct cma_multicast *mc;
1629 while (!list_empty(&id_priv->mc_list)) {
1630 mc = container_of(id_priv->mc_list.next,
1631 struct cma_multicast, list);
1632 list_del(&mc->list);
1633 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
1634 id_priv->id.port_num)) {
1635 ib_sa_free_multicast(mc->multicast.ib);
1636 kfree(mc);
1637 } else {
1638 if (mc->igmp_joined) {
1639 struct rdma_dev_addr *dev_addr =
1640 &id_priv->id.route.addr.dev_addr;
1641 struct net_device *ndev = NULL;
1643 if (dev_addr->bound_dev_if)
1644 ndev = dev_get_by_index(&init_net,
1645 dev_addr->bound_dev_if);
1646 if (ndev) {
1647 cma_igmp_send(ndev,
1648 &mc->multicast.ib->rec.mgid,
1649 false);
1650 dev_put(ndev);
1653 kref_put(&mc->mcref, release_mc);
1658 void rdma_destroy_id(struct rdma_cm_id *id)
1660 struct rdma_id_private *id_priv;
1661 enum rdma_cm_state state;
1663 id_priv = container_of(id, struct rdma_id_private, id);
1664 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1665 cma_cancel_operation(id_priv, state);
1668 * Wait for any active callback to finish. New callbacks will find
1669 * the id_priv state set to destroying and abort.
1671 mutex_lock(&id_priv->handler_mutex);
1672 mutex_unlock(&id_priv->handler_mutex);
1674 if (id_priv->cma_dev) {
1675 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1676 if (id_priv->cm_id.ib)
1677 ib_destroy_cm_id(id_priv->cm_id.ib);
1678 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1679 if (id_priv->cm_id.iw)
1680 iw_destroy_cm_id(id_priv->cm_id.iw);
1682 cma_leave_mc_groups(id_priv);
1683 cma_release_dev(id_priv);
1686 cma_release_port(id_priv);
1687 cma_deref_id(id_priv);
1688 wait_for_completion(&id_priv->comp);
1690 if (id_priv->internal_id)
1691 cma_deref_id(id_priv->id.context);
1693 kfree(id_priv->id.route.path_rec);
1694 put_net(id_priv->id.route.addr.dev_addr.net);
1695 kfree(id_priv);
1697 EXPORT_SYMBOL(rdma_destroy_id);
1699 static int cma_rep_recv(struct rdma_id_private *id_priv)
1701 int ret;
1703 ret = cma_modify_qp_rtr(id_priv, NULL);
1704 if (ret)
1705 goto reject;
1707 ret = cma_modify_qp_rts(id_priv, NULL);
1708 if (ret)
1709 goto reject;
1711 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1712 if (ret)
1713 goto reject;
1715 return 0;
1716 reject:
1717 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
1718 cma_modify_qp_err(id_priv);
1719 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1720 NULL, 0, NULL, 0);
1721 return ret;
1724 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1725 struct ib_cm_rep_event_param *rep_data,
1726 void *private_data)
1728 event->param.conn.private_data = private_data;
1729 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1730 event->param.conn.responder_resources = rep_data->responder_resources;
1731 event->param.conn.initiator_depth = rep_data->initiator_depth;
1732 event->param.conn.flow_control = rep_data->flow_control;
1733 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1734 event->param.conn.srq = rep_data->srq;
1735 event->param.conn.qp_num = rep_data->remote_qpn;
1738 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1740 struct rdma_id_private *id_priv = cm_id->context;
1741 struct rdma_cm_event event;
1742 int ret = 0;
1744 mutex_lock(&id_priv->handler_mutex);
1745 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1746 id_priv->state != RDMA_CM_CONNECT) ||
1747 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1748 id_priv->state != RDMA_CM_DISCONNECT))
1749 goto out;
1751 memset(&event, 0, sizeof event);
1752 switch (ib_event->event) {
1753 case IB_CM_REQ_ERROR:
1754 case IB_CM_REP_ERROR:
1755 event.event = RDMA_CM_EVENT_UNREACHABLE;
1756 event.status = -ETIMEDOUT;
1757 break;
1758 case IB_CM_REP_RECEIVED:
1759 if (cma_comp(id_priv, RDMA_CM_CONNECT) &&
1760 (id_priv->id.qp_type != IB_QPT_UD))
1761 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1762 if (id_priv->id.qp) {
1763 event.status = cma_rep_recv(id_priv);
1764 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1765 RDMA_CM_EVENT_ESTABLISHED;
1766 } else {
1767 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1769 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1770 ib_event->private_data);
1771 break;
1772 case IB_CM_RTU_RECEIVED:
1773 case IB_CM_USER_ESTABLISHED:
1774 event.event = RDMA_CM_EVENT_ESTABLISHED;
1775 break;
1776 case IB_CM_DREQ_ERROR:
1777 event.status = -ETIMEDOUT; /* fall through */
1778 case IB_CM_DREQ_RECEIVED:
1779 case IB_CM_DREP_RECEIVED:
1780 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1781 RDMA_CM_DISCONNECT))
1782 goto out;
1783 event.event = RDMA_CM_EVENT_DISCONNECTED;
1784 break;
1785 case IB_CM_TIMEWAIT_EXIT:
1786 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1787 break;
1788 case IB_CM_MRA_RECEIVED:
1789 /* ignore event */
1790 goto out;
1791 case IB_CM_REJ_RECEIVED:
1792 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
1793 ib_event->param.rej_rcvd.reason));
1794 cma_modify_qp_err(id_priv);
1795 event.status = ib_event->param.rej_rcvd.reason;
1796 event.event = RDMA_CM_EVENT_REJECTED;
1797 event.param.conn.private_data = ib_event->private_data;
1798 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1799 break;
1800 default:
1801 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1802 ib_event->event);
1803 goto out;
1806 ret = id_priv->id.event_handler(&id_priv->id, &event);
1807 if (ret) {
1808 /* Destroy the CM ID by returning a non-zero value. */
1809 id_priv->cm_id.ib = NULL;
1810 cma_exch(id_priv, RDMA_CM_DESTROYING);
1811 mutex_unlock(&id_priv->handler_mutex);
1812 rdma_destroy_id(&id_priv->id);
1813 return ret;
1815 out:
1816 mutex_unlock(&id_priv->handler_mutex);
1817 return ret;
1820 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1821 struct ib_cm_event *ib_event,
1822 struct net_device *net_dev)
1824 struct rdma_id_private *id_priv;
1825 struct rdma_cm_id *id;
1826 struct rdma_route *rt;
1827 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1828 struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
1829 const __be64 service_id =
1830 ib_event->param.req_rcvd.primary_path->service_id;
1831 int ret;
1833 id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1834 listen_id->event_handler, listen_id->context,
1835 listen_id->ps, ib_event->param.req_rcvd.qp_type);
1836 if (IS_ERR(id))
1837 return NULL;
1839 id_priv = container_of(id, struct rdma_id_private, id);
1840 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1841 (struct sockaddr *)&id->route.addr.dst_addr,
1842 listen_id, ib_event, ss_family, service_id))
1843 goto err;
1845 rt = &id->route;
1846 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1847 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1848 GFP_KERNEL);
1849 if (!rt->path_rec)
1850 goto err;
1852 rt->path_rec[0] = *path;
1853 if (rt->num_paths == 2)
1854 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1856 if (net_dev) {
1857 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
1858 if (ret)
1859 goto err;
1860 } else {
1861 if (!cma_protocol_roce(listen_id) &&
1862 cma_any_addr(cma_src_addr(id_priv))) {
1863 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1864 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1865 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1866 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1867 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1868 if (ret)
1869 goto err;
1872 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1874 id_priv->state = RDMA_CM_CONNECT;
1875 return id_priv;
1877 err:
1878 rdma_destroy_id(id);
1879 return NULL;
1882 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1883 struct ib_cm_event *ib_event,
1884 struct net_device *net_dev)
1886 struct rdma_id_private *id_priv;
1887 struct rdma_cm_id *id;
1888 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1889 struct net *net = listen_id->route.addr.dev_addr.net;
1890 int ret;
1892 id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
1893 listen_id->ps, IB_QPT_UD);
1894 if (IS_ERR(id))
1895 return NULL;
1897 id_priv = container_of(id, struct rdma_id_private, id);
1898 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1899 (struct sockaddr *)&id->route.addr.dst_addr,
1900 listen_id, ib_event, ss_family,
1901 ib_event->param.sidr_req_rcvd.service_id))
1902 goto err;
1904 if (net_dev) {
1905 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
1906 if (ret)
1907 goto err;
1908 } else {
1909 if (!cma_any_addr(cma_src_addr(id_priv))) {
1910 ret = cma_translate_addr(cma_src_addr(id_priv),
1911 &id->route.addr.dev_addr);
1912 if (ret)
1913 goto err;
1917 id_priv->state = RDMA_CM_CONNECT;
1918 return id_priv;
1919 err:
1920 rdma_destroy_id(id);
1921 return NULL;
1924 static void cma_set_req_event_data(struct rdma_cm_event *event,
1925 struct ib_cm_req_event_param *req_data,
1926 void *private_data, int offset)
1928 event->param.conn.private_data = private_data + offset;
1929 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1930 event->param.conn.responder_resources = req_data->responder_resources;
1931 event->param.conn.initiator_depth = req_data->initiator_depth;
1932 event->param.conn.flow_control = req_data->flow_control;
1933 event->param.conn.retry_count = req_data->retry_count;
1934 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1935 event->param.conn.srq = req_data->srq;
1936 event->param.conn.qp_num = req_data->remote_qpn;
1939 static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1941 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
1942 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1943 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1944 (id->qp_type == IB_QPT_UD)) ||
1945 (!id->qp_type));
1948 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1950 struct rdma_id_private *listen_id, *conn_id = NULL;
1951 struct rdma_cm_event event;
1952 struct net_device *net_dev;
1953 int offset, ret;
1955 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
1956 if (IS_ERR(listen_id))
1957 return PTR_ERR(listen_id);
1959 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1960 ret = -EINVAL;
1961 goto net_dev_put;
1964 mutex_lock(&listen_id->handler_mutex);
1965 if (listen_id->state != RDMA_CM_LISTEN) {
1966 ret = -ECONNABORTED;
1967 goto err1;
1970 memset(&event, 0, sizeof event);
1971 offset = cma_user_data_offset(listen_id);
1972 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1973 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
1974 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
1975 event.param.ud.private_data = ib_event->private_data + offset;
1976 event.param.ud.private_data_len =
1977 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1978 } else {
1979 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
1980 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1981 ib_event->private_data, offset);
1983 if (!conn_id) {
1984 ret = -ENOMEM;
1985 goto err1;
1988 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1989 ret = cma_acquire_dev(conn_id, listen_id);
1990 if (ret)
1991 goto err2;
1993 conn_id->cm_id.ib = cm_id;
1994 cm_id->context = conn_id;
1995 cm_id->cm_handler = cma_ib_handler;
1998 * Protect against the user destroying conn_id from another thread
1999 * until we're done accessing it.
2001 atomic_inc(&conn_id->refcount);
2002 ret = conn_id->id.event_handler(&conn_id->id, &event);
2003 if (ret)
2004 goto err3;
2006 * Acquire mutex to prevent user executing rdma_destroy_id()
2007 * while we're accessing the cm_id.
2009 mutex_lock(&lock);
2010 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
2011 (conn_id->id.qp_type != IB_QPT_UD))
2012 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2013 mutex_unlock(&lock);
2014 mutex_unlock(&conn_id->handler_mutex);
2015 mutex_unlock(&listen_id->handler_mutex);
2016 cma_deref_id(conn_id);
2017 if (net_dev)
2018 dev_put(net_dev);
2019 return 0;
2021 err3:
2022 cma_deref_id(conn_id);
2023 /* Destroy the CM ID by returning a non-zero value. */
2024 conn_id->cm_id.ib = NULL;
2025 err2:
2026 cma_exch(conn_id, RDMA_CM_DESTROYING);
2027 mutex_unlock(&conn_id->handler_mutex);
2028 err1:
2029 mutex_unlock(&listen_id->handler_mutex);
2030 if (conn_id)
2031 rdma_destroy_id(&conn_id->id);
2033 net_dev_put:
2034 if (net_dev)
2035 dev_put(net_dev);
2037 return ret;
2040 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2042 if (addr->sa_family == AF_IB)
2043 return ((struct sockaddr_ib *) addr)->sib_sid;
2045 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2047 EXPORT_SYMBOL(rdma_get_service_id);
2049 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2051 struct rdma_id_private *id_priv = iw_id->context;
2052 struct rdma_cm_event event;
2053 int ret = 0;
2054 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2055 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2057 mutex_lock(&id_priv->handler_mutex);
2058 if (id_priv->state != RDMA_CM_CONNECT)
2059 goto out;
2061 memset(&event, 0, sizeof event);
2062 switch (iw_event->event) {
2063 case IW_CM_EVENT_CLOSE:
2064 event.event = RDMA_CM_EVENT_DISCONNECTED;
2065 break;
2066 case IW_CM_EVENT_CONNECT_REPLY:
2067 memcpy(cma_src_addr(id_priv), laddr,
2068 rdma_addr_size(laddr));
2069 memcpy(cma_dst_addr(id_priv), raddr,
2070 rdma_addr_size(raddr));
2071 switch (iw_event->status) {
2072 case 0:
2073 event.event = RDMA_CM_EVENT_ESTABLISHED;
2074 event.param.conn.initiator_depth = iw_event->ird;
2075 event.param.conn.responder_resources = iw_event->ord;
2076 break;
2077 case -ECONNRESET:
2078 case -ECONNREFUSED:
2079 event.event = RDMA_CM_EVENT_REJECTED;
2080 break;
2081 case -ETIMEDOUT:
2082 event.event = RDMA_CM_EVENT_UNREACHABLE;
2083 break;
2084 default:
2085 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2086 break;
2088 break;
2089 case IW_CM_EVENT_ESTABLISHED:
2090 event.event = RDMA_CM_EVENT_ESTABLISHED;
2091 event.param.conn.initiator_depth = iw_event->ird;
2092 event.param.conn.responder_resources = iw_event->ord;
2093 break;
2094 default:
2095 BUG_ON(1);
2098 event.status = iw_event->status;
2099 event.param.conn.private_data = iw_event->private_data;
2100 event.param.conn.private_data_len = iw_event->private_data_len;
2101 ret = id_priv->id.event_handler(&id_priv->id, &event);
2102 if (ret) {
2103 /* Destroy the CM ID by returning a non-zero value. */
2104 id_priv->cm_id.iw = NULL;
2105 cma_exch(id_priv, RDMA_CM_DESTROYING);
2106 mutex_unlock(&id_priv->handler_mutex);
2107 rdma_destroy_id(&id_priv->id);
2108 return ret;
2111 out:
2112 mutex_unlock(&id_priv->handler_mutex);
2113 return ret;
2116 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2117 struct iw_cm_event *iw_event)
2119 struct rdma_cm_id *new_cm_id;
2120 struct rdma_id_private *listen_id, *conn_id;
2121 struct rdma_cm_event event;
2122 int ret = -ECONNABORTED;
2123 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2124 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2126 listen_id = cm_id->context;
2128 mutex_lock(&listen_id->handler_mutex);
2129 if (listen_id->state != RDMA_CM_LISTEN)
2130 goto out;
2132 /* Create a new RDMA id for the new IW CM ID */
2133 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2134 listen_id->id.event_handler,
2135 listen_id->id.context,
2136 RDMA_PS_TCP, IB_QPT_RC);
2137 if (IS_ERR(new_cm_id)) {
2138 ret = -ENOMEM;
2139 goto out;
2141 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
2142 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2143 conn_id->state = RDMA_CM_CONNECT;
2145 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
2146 if (ret) {
2147 mutex_unlock(&conn_id->handler_mutex);
2148 rdma_destroy_id(new_cm_id);
2149 goto out;
2152 ret = cma_acquire_dev(conn_id, listen_id);
2153 if (ret) {
2154 mutex_unlock(&conn_id->handler_mutex);
2155 rdma_destroy_id(new_cm_id);
2156 goto out;
2159 conn_id->cm_id.iw = cm_id;
2160 cm_id->context = conn_id;
2161 cm_id->cm_handler = cma_iw_handler;
2163 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2164 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2166 memset(&event, 0, sizeof event);
2167 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2168 event.param.conn.private_data = iw_event->private_data;
2169 event.param.conn.private_data_len = iw_event->private_data_len;
2170 event.param.conn.initiator_depth = iw_event->ird;
2171 event.param.conn.responder_resources = iw_event->ord;
2174 * Protect against the user destroying conn_id from another thread
2175 * until we're done accessing it.
2177 atomic_inc(&conn_id->refcount);
2178 ret = conn_id->id.event_handler(&conn_id->id, &event);
2179 if (ret) {
2180 /* User wants to destroy the CM ID */
2181 conn_id->cm_id.iw = NULL;
2182 cma_exch(conn_id, RDMA_CM_DESTROYING);
2183 mutex_unlock(&conn_id->handler_mutex);
2184 cma_deref_id(conn_id);
2185 rdma_destroy_id(&conn_id->id);
2186 goto out;
2189 mutex_unlock(&conn_id->handler_mutex);
2190 cma_deref_id(conn_id);
2192 out:
2193 mutex_unlock(&listen_id->handler_mutex);
2194 return ret;
2197 static int cma_ib_listen(struct rdma_id_private *id_priv)
2199 struct sockaddr *addr;
2200 struct ib_cm_id *id;
2201 __be64 svc_id;
2203 addr = cma_src_addr(id_priv);
2204 svc_id = rdma_get_service_id(&id_priv->id, addr);
2205 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2206 if (IS_ERR(id))
2207 return PTR_ERR(id);
2208 id_priv->cm_id.ib = id;
2210 return 0;
2213 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2215 int ret;
2216 struct iw_cm_id *id;
2218 id = iw_create_cm_id(id_priv->id.device,
2219 iw_conn_req_handler,
2220 id_priv);
2221 if (IS_ERR(id))
2222 return PTR_ERR(id);
2224 id->tos = id_priv->tos;
2225 id_priv->cm_id.iw = id;
2227 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2228 rdma_addr_size(cma_src_addr(id_priv)));
2230 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2232 if (ret) {
2233 iw_destroy_cm_id(id_priv->cm_id.iw);
2234 id_priv->cm_id.iw = NULL;
2237 return ret;
2240 static int cma_listen_handler(struct rdma_cm_id *id,
2241 struct rdma_cm_event *event)
2243 struct rdma_id_private *id_priv = id->context;
2245 id->context = id_priv->id.context;
2246 id->event_handler = id_priv->id.event_handler;
2247 return id_priv->id.event_handler(id, event);
2250 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2251 struct cma_device *cma_dev)
2253 struct rdma_id_private *dev_id_priv;
2254 struct rdma_cm_id *id;
2255 struct net *net = id_priv->id.route.addr.dev_addr.net;
2256 int ret;
2258 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2259 return;
2261 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
2262 id_priv->id.qp_type);
2263 if (IS_ERR(id))
2264 return;
2266 dev_id_priv = container_of(id, struct rdma_id_private, id);
2268 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2269 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2270 rdma_addr_size(cma_src_addr(id_priv)));
2272 _cma_attach_to_dev(dev_id_priv, cma_dev);
2273 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2274 atomic_inc(&id_priv->refcount);
2275 dev_id_priv->internal_id = 1;
2276 dev_id_priv->afonly = id_priv->afonly;
2278 ret = rdma_listen(id, id_priv->backlog);
2279 if (ret)
2280 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2281 ret, cma_dev->device->name);
2284 static void cma_listen_on_all(struct rdma_id_private *id_priv)
2286 struct cma_device *cma_dev;
2288 mutex_lock(&lock);
2289 list_add_tail(&id_priv->list, &listen_any_list);
2290 list_for_each_entry(cma_dev, &dev_list, list)
2291 cma_listen_on_dev(id_priv, cma_dev);
2292 mutex_unlock(&lock);
2295 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2297 struct rdma_id_private *id_priv;
2299 id_priv = container_of(id, struct rdma_id_private, id);
2300 id_priv->tos = (u8) tos;
2301 id_priv->tos_set = true;
2303 EXPORT_SYMBOL(rdma_set_service_type);
2305 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2306 void *context)
2308 struct cma_work *work = context;
2309 struct rdma_route *route;
2311 route = &work->id->id.route;
2313 if (!status) {
2314 route->num_paths = 1;
2315 *route->path_rec = *path_rec;
2316 } else {
2317 work->old_state = RDMA_CM_ROUTE_QUERY;
2318 work->new_state = RDMA_CM_ADDR_RESOLVED;
2319 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2320 work->event.status = status;
2321 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2322 status);
2325 queue_work(cma_wq, &work->work);
2328 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2329 struct cma_work *work)
2331 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2332 struct sa_path_rec path_rec;
2333 ib_sa_comp_mask comp_mask;
2334 struct sockaddr_in6 *sin6;
2335 struct sockaddr_ib *sib;
2337 memset(&path_rec, 0, sizeof path_rec);
2339 if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2340 path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2341 else
2342 path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2343 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2344 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2345 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2346 path_rec.numb_path = 1;
2347 path_rec.reversible = 1;
2348 path_rec.service_id = rdma_get_service_id(&id_priv->id,
2349 cma_dst_addr(id_priv));
2351 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2352 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2353 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2355 switch (cma_family(id_priv)) {
2356 case AF_INET:
2357 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2358 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2359 break;
2360 case AF_INET6:
2361 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2362 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2363 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2364 break;
2365 case AF_IB:
2366 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2367 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2368 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2369 break;
2372 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2373 id_priv->id.port_num, &path_rec,
2374 comp_mask, timeout_ms,
2375 GFP_KERNEL, cma_query_handler,
2376 work, &id_priv->query);
2378 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2381 static void cma_work_handler(struct work_struct *_work)
2383 struct cma_work *work = container_of(_work, struct cma_work, work);
2384 struct rdma_id_private *id_priv = work->id;
2385 int destroy = 0;
2387 mutex_lock(&id_priv->handler_mutex);
2388 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2389 goto out;
2391 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2392 cma_exch(id_priv, RDMA_CM_DESTROYING);
2393 destroy = 1;
2395 out:
2396 mutex_unlock(&id_priv->handler_mutex);
2397 cma_deref_id(id_priv);
2398 if (destroy)
2399 rdma_destroy_id(&id_priv->id);
2400 kfree(work);
2403 static void cma_ndev_work_handler(struct work_struct *_work)
2405 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2406 struct rdma_id_private *id_priv = work->id;
2407 int destroy = 0;
2409 mutex_lock(&id_priv->handler_mutex);
2410 if (id_priv->state == RDMA_CM_DESTROYING ||
2411 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
2412 goto out;
2414 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2415 cma_exch(id_priv, RDMA_CM_DESTROYING);
2416 destroy = 1;
2419 out:
2420 mutex_unlock(&id_priv->handler_mutex);
2421 cma_deref_id(id_priv);
2422 if (destroy)
2423 rdma_destroy_id(&id_priv->id);
2424 kfree(work);
2427 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2429 struct rdma_route *route = &id_priv->id.route;
2430 struct cma_work *work;
2431 int ret;
2433 work = kzalloc(sizeof *work, GFP_KERNEL);
2434 if (!work)
2435 return -ENOMEM;
2437 work->id = id_priv;
2438 INIT_WORK(&work->work, cma_work_handler);
2439 work->old_state = RDMA_CM_ROUTE_QUERY;
2440 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2441 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2443 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2444 if (!route->path_rec) {
2445 ret = -ENOMEM;
2446 goto err1;
2449 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2450 if (ret)
2451 goto err2;
2453 return 0;
2454 err2:
2455 kfree(route->path_rec);
2456 route->path_rec = NULL;
2457 err1:
2458 kfree(work);
2459 return ret;
2462 int rdma_set_ib_paths(struct rdma_cm_id *id,
2463 struct sa_path_rec *path_rec, int num_paths)
2465 struct rdma_id_private *id_priv;
2466 int ret;
2468 id_priv = container_of(id, struct rdma_id_private, id);
2469 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2470 RDMA_CM_ROUTE_RESOLVED))
2471 return -EINVAL;
2473 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2474 GFP_KERNEL);
2475 if (!id->route.path_rec) {
2476 ret = -ENOMEM;
2477 goto err;
2480 id->route.num_paths = num_paths;
2481 return 0;
2482 err:
2483 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2484 return ret;
2486 EXPORT_SYMBOL(rdma_set_ib_paths);
2488 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2490 struct cma_work *work;
2492 work = kzalloc(sizeof *work, GFP_KERNEL);
2493 if (!work)
2494 return -ENOMEM;
2496 work->id = id_priv;
2497 INIT_WORK(&work->work, cma_work_handler);
2498 work->old_state = RDMA_CM_ROUTE_QUERY;
2499 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2500 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2501 queue_work(cma_wq, &work->work);
2502 return 0;
2505 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2507 int prio;
2508 struct net_device *dev;
2510 prio = rt_tos2priority(tos);
2511 dev = is_vlan_dev(ndev) ? vlan_dev_real_dev(ndev) : ndev;
2512 if (dev->num_tc)
2513 return netdev_get_prio_tc_map(dev, prio);
2515 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2516 if (is_vlan_dev(ndev))
2517 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2518 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2519 #endif
2520 return 0;
2523 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2524 unsigned long supported_gids,
2525 enum ib_gid_type default_gid)
2527 if ((network_type == RDMA_NETWORK_IPV4 ||
2528 network_type == RDMA_NETWORK_IPV6) &&
2529 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2530 return IB_GID_TYPE_ROCE_UDP_ENCAP;
2532 return default_gid;
2535 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2537 struct rdma_route *route = &id_priv->id.route;
2538 struct rdma_addr *addr = &route->addr;
2539 struct cma_work *work;
2540 int ret;
2541 struct net_device *ndev = NULL;
2542 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
2543 u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
2544 rdma_start_port(id_priv->cma_dev->device)];
2545 u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
2548 work = kzalloc(sizeof *work, GFP_KERNEL);
2549 if (!work)
2550 return -ENOMEM;
2552 work->id = id_priv;
2553 INIT_WORK(&work->work, cma_work_handler);
2555 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2556 if (!route->path_rec) {
2557 ret = -ENOMEM;
2558 goto err1;
2561 route->num_paths = 1;
2563 if (addr->dev_addr.bound_dev_if) {
2564 unsigned long supported_gids;
2566 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
2567 if (!ndev) {
2568 ret = -ENODEV;
2569 goto err2;
2572 if (ndev->flags & IFF_LOOPBACK) {
2573 dev_put(ndev);
2574 if (!id_priv->id.device->get_netdev) {
2575 ret = -EOPNOTSUPP;
2576 goto err2;
2579 ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2580 id_priv->id.port_num);
2581 if (!ndev) {
2582 ret = -ENODEV;
2583 goto err2;
2587 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2588 id_priv->id.port_num);
2589 gid_type = cma_route_gid_type(addr->dev_addr.network,
2590 supported_gids,
2591 id_priv->gid_type);
2592 route->path_rec->rec_type =
2593 sa_conv_gid_to_pathrec_type(gid_type);
2594 sa_path_set_ndev(route->path_rec, &init_net);
2595 sa_path_set_ifindex(route->path_rec, ndev->ifindex);
2597 if (!ndev) {
2598 ret = -ENODEV;
2599 goto err2;
2602 sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
2604 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2605 &route->path_rec->sgid);
2606 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2607 &route->path_rec->dgid);
2609 /* Use the hint from IP Stack to select GID Type */
2610 if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2611 gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2612 route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
2614 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2615 /* TODO: get the hoplimit from the inet/inet6 device */
2616 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2617 else
2618 route->path_rec->hop_limit = 1;
2619 route->path_rec->reversible = 1;
2620 route->path_rec->pkey = cpu_to_be16(0xffff);
2621 route->path_rec->mtu_selector = IB_SA_EQ;
2622 route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
2623 route->path_rec->traffic_class = tos;
2624 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2625 route->path_rec->rate_selector = IB_SA_EQ;
2626 route->path_rec->rate = iboe_get_rate(ndev);
2627 dev_put(ndev);
2628 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2629 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2630 if (!route->path_rec->mtu) {
2631 ret = -EINVAL;
2632 goto err2;
2635 work->old_state = RDMA_CM_ROUTE_QUERY;
2636 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2637 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2638 work->event.status = 0;
2640 queue_work(cma_wq, &work->work);
2642 return 0;
2644 err2:
2645 kfree(route->path_rec);
2646 route->path_rec = NULL;
2647 err1:
2648 kfree(work);
2649 return ret;
2652 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2654 struct rdma_id_private *id_priv;
2655 int ret;
2657 id_priv = container_of(id, struct rdma_id_private, id);
2658 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
2659 return -EINVAL;
2661 atomic_inc(&id_priv->refcount);
2662 if (rdma_cap_ib_sa(id->device, id->port_num))
2663 ret = cma_resolve_ib_route(id_priv, timeout_ms);
2664 else if (rdma_protocol_roce(id->device, id->port_num))
2665 ret = cma_resolve_iboe_route(id_priv);
2666 else if (rdma_protocol_iwarp(id->device, id->port_num))
2667 ret = cma_resolve_iw_route(id_priv, timeout_ms);
2668 else
2669 ret = -ENOSYS;
2671 if (ret)
2672 goto err;
2674 return 0;
2675 err:
2676 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
2677 cma_deref_id(id_priv);
2678 return ret;
2680 EXPORT_SYMBOL(rdma_resolve_route);
2682 static void cma_set_loopback(struct sockaddr *addr)
2684 switch (addr->sa_family) {
2685 case AF_INET:
2686 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2687 break;
2688 case AF_INET6:
2689 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2690 0, 0, 0, htonl(1));
2691 break;
2692 default:
2693 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2694 0, 0, 0, htonl(1));
2695 break;
2699 static int cma_bind_loopback(struct rdma_id_private *id_priv)
2701 struct cma_device *cma_dev, *cur_dev;
2702 union ib_gid gid;
2703 enum ib_port_state port_state;
2704 u16 pkey;
2705 int ret;
2706 u8 p;
2708 cma_dev = NULL;
2709 mutex_lock(&lock);
2710 list_for_each_entry(cur_dev, &dev_list, list) {
2711 if (cma_family(id_priv) == AF_IB &&
2712 !rdma_cap_ib_cm(cur_dev->device, 1))
2713 continue;
2715 if (!cma_dev)
2716 cma_dev = cur_dev;
2718 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2719 if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
2720 port_state == IB_PORT_ACTIVE) {
2721 cma_dev = cur_dev;
2722 goto port_found;
2727 if (!cma_dev) {
2728 ret = -ENODEV;
2729 goto out;
2732 p = 1;
2734 port_found:
2735 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
2736 if (ret)
2737 goto out;
2739 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2740 if (ret)
2741 goto out;
2743 id_priv->id.route.addr.dev_addr.dev_type =
2744 (rdma_protocol_ib(cma_dev->device, p)) ?
2745 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2747 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2748 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2749 id_priv->id.port_num = p;
2750 cma_attach_to_dev(id_priv, cma_dev);
2751 cma_set_loopback(cma_src_addr(id_priv));
2752 out:
2753 mutex_unlock(&lock);
2754 return ret;
2757 static void addr_handler(int status, struct sockaddr *src_addr,
2758 struct rdma_dev_addr *dev_addr, void *context)
2760 struct rdma_id_private *id_priv = context;
2761 struct rdma_cm_event event;
2763 memset(&event, 0, sizeof event);
2764 mutex_lock(&id_priv->handler_mutex);
2765 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2766 RDMA_CM_ADDR_RESOLVED))
2767 goto out;
2769 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
2770 if (!status && !id_priv->cma_dev) {
2771 status = cma_acquire_dev(id_priv, NULL);
2772 if (status)
2773 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
2774 status);
2775 } else {
2776 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
2779 if (status) {
2780 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2781 RDMA_CM_ADDR_BOUND))
2782 goto out;
2783 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2784 event.status = status;
2785 } else
2786 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2788 if (id_priv->id.event_handler(&id_priv->id, &event)) {
2789 cma_exch(id_priv, RDMA_CM_DESTROYING);
2790 mutex_unlock(&id_priv->handler_mutex);
2791 cma_deref_id(id_priv);
2792 rdma_destroy_id(&id_priv->id);
2793 return;
2795 out:
2796 mutex_unlock(&id_priv->handler_mutex);
2797 cma_deref_id(id_priv);
2800 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2802 struct cma_work *work;
2803 union ib_gid gid;
2804 int ret;
2806 work = kzalloc(sizeof *work, GFP_KERNEL);
2807 if (!work)
2808 return -ENOMEM;
2810 if (!id_priv->cma_dev) {
2811 ret = cma_bind_loopback(id_priv);
2812 if (ret)
2813 goto err;
2816 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2817 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2819 work->id = id_priv;
2820 INIT_WORK(&work->work, cma_work_handler);
2821 work->old_state = RDMA_CM_ADDR_QUERY;
2822 work->new_state = RDMA_CM_ADDR_RESOLVED;
2823 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2824 queue_work(cma_wq, &work->work);
2825 return 0;
2826 err:
2827 kfree(work);
2828 return ret;
2831 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2833 struct cma_work *work;
2834 int ret;
2836 work = kzalloc(sizeof *work, GFP_KERNEL);
2837 if (!work)
2838 return -ENOMEM;
2840 if (!id_priv->cma_dev) {
2841 ret = cma_resolve_ib_dev(id_priv);
2842 if (ret)
2843 goto err;
2846 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2847 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2849 work->id = id_priv;
2850 INIT_WORK(&work->work, cma_work_handler);
2851 work->old_state = RDMA_CM_ADDR_QUERY;
2852 work->new_state = RDMA_CM_ADDR_RESOLVED;
2853 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2854 queue_work(cma_wq, &work->work);
2855 return 0;
2856 err:
2857 kfree(work);
2858 return ret;
2861 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2862 struct sockaddr *dst_addr)
2864 if (!src_addr || !src_addr->sa_family) {
2865 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2866 src_addr->sa_family = dst_addr->sa_family;
2867 if (IS_ENABLED(CONFIG_IPV6) &&
2868 dst_addr->sa_family == AF_INET6) {
2869 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2870 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2871 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2872 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2873 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
2874 } else if (dst_addr->sa_family == AF_IB) {
2875 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2876 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
2879 return rdma_bind_addr(id, src_addr);
2882 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2883 struct sockaddr *dst_addr, int timeout_ms)
2885 struct rdma_id_private *id_priv;
2886 int ret;
2888 id_priv = container_of(id, struct rdma_id_private, id);
2889 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
2890 if (id_priv->state == RDMA_CM_IDLE) {
2891 ret = cma_bind_addr(id, src_addr, dst_addr);
2892 if (ret) {
2893 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
2894 return ret;
2898 if (cma_family(id_priv) != dst_addr->sa_family) {
2899 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
2900 return -EINVAL;
2903 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
2904 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
2905 return -EINVAL;
2908 atomic_inc(&id_priv->refcount);
2909 if (cma_any_addr(dst_addr)) {
2910 ret = cma_resolve_loopback(id_priv);
2911 } else {
2912 if (dst_addr->sa_family == AF_IB) {
2913 ret = cma_resolve_ib_addr(id_priv);
2914 } else {
2915 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2916 dst_addr, &id->route.addr.dev_addr,
2917 timeout_ms, addr_handler, id_priv);
2920 if (ret)
2921 goto err;
2923 return 0;
2924 err:
2925 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2926 cma_deref_id(id_priv);
2927 return ret;
2929 EXPORT_SYMBOL(rdma_resolve_addr);
2931 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2933 struct rdma_id_private *id_priv;
2934 unsigned long flags;
2935 int ret;
2937 id_priv = container_of(id, struct rdma_id_private, id);
2938 spin_lock_irqsave(&id_priv->lock, flags);
2939 if (reuse || id_priv->state == RDMA_CM_IDLE) {
2940 id_priv->reuseaddr = reuse;
2941 ret = 0;
2942 } else {
2943 ret = -EINVAL;
2945 spin_unlock_irqrestore(&id_priv->lock, flags);
2946 return ret;
2948 EXPORT_SYMBOL(rdma_set_reuseaddr);
2950 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2952 struct rdma_id_private *id_priv;
2953 unsigned long flags;
2954 int ret;
2956 id_priv = container_of(id, struct rdma_id_private, id);
2957 spin_lock_irqsave(&id_priv->lock, flags);
2958 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2959 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2960 id_priv->afonly = afonly;
2961 ret = 0;
2962 } else {
2963 ret = -EINVAL;
2965 spin_unlock_irqrestore(&id_priv->lock, flags);
2966 return ret;
2968 EXPORT_SYMBOL(rdma_set_afonly);
2970 static void cma_bind_port(struct rdma_bind_list *bind_list,
2971 struct rdma_id_private *id_priv)
2973 struct sockaddr *addr;
2974 struct sockaddr_ib *sib;
2975 u64 sid, mask;
2976 __be16 port;
2978 addr = cma_src_addr(id_priv);
2979 port = htons(bind_list->port);
2981 switch (addr->sa_family) {
2982 case AF_INET:
2983 ((struct sockaddr_in *) addr)->sin_port = port;
2984 break;
2985 case AF_INET6:
2986 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2987 break;
2988 case AF_IB:
2989 sib = (struct sockaddr_ib *) addr;
2990 sid = be64_to_cpu(sib->sib_sid);
2991 mask = be64_to_cpu(sib->sib_sid_mask);
2992 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2993 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2994 break;
2996 id_priv->bind_list = bind_list;
2997 hlist_add_head(&id_priv->node, &bind_list->owners);
3000 static int cma_alloc_port(enum rdma_port_space ps,
3001 struct rdma_id_private *id_priv, unsigned short snum)
3003 struct rdma_bind_list *bind_list;
3004 int ret;
3006 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3007 if (!bind_list)
3008 return -ENOMEM;
3010 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3011 snum);
3012 if (ret < 0)
3013 goto err;
3015 bind_list->ps = ps;
3016 bind_list->port = (unsigned short)ret;
3017 cma_bind_port(bind_list, id_priv);
3018 return 0;
3019 err:
3020 kfree(bind_list);
3021 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3024 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3025 struct rdma_id_private *id_priv)
3027 struct rdma_id_private *cur_id;
3028 struct sockaddr *daddr = cma_dst_addr(id_priv);
3029 struct sockaddr *saddr = cma_src_addr(id_priv);
3030 __be16 dport = cma_port(daddr);
3032 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3033 struct sockaddr *cur_daddr = cma_dst_addr(cur_id);
3034 struct sockaddr *cur_saddr = cma_src_addr(cur_id);
3035 __be16 cur_dport = cma_port(cur_daddr);
3037 if (id_priv == cur_id)
3038 continue;
3040 /* different dest port -> unique */
3041 if (!cma_any_port(cur_daddr) &&
3042 (dport != cur_dport))
3043 continue;
3045 /* different src address -> unique */
3046 if (!cma_any_addr(saddr) &&
3047 !cma_any_addr(cur_saddr) &&
3048 cma_addr_cmp(saddr, cur_saddr))
3049 continue;
3051 /* different dst address -> unique */
3052 if (!cma_any_addr(cur_daddr) &&
3053 cma_addr_cmp(daddr, cur_daddr))
3054 continue;
3056 return -EADDRNOTAVAIL;
3058 return 0;
3061 static int cma_alloc_any_port(enum rdma_port_space ps,
3062 struct rdma_id_private *id_priv)
3064 static unsigned int last_used_port;
3065 int low, high, remaining;
3066 unsigned int rover;
3067 struct net *net = id_priv->id.route.addr.dev_addr.net;
3069 inet_get_local_port_range(net, &low, &high);
3070 remaining = (high - low) + 1;
3071 rover = prandom_u32() % remaining + low;
3072 retry:
3073 if (last_used_port != rover) {
3074 struct rdma_bind_list *bind_list;
3075 int ret;
3077 bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3079 if (!bind_list) {
3080 ret = cma_alloc_port(ps, id_priv, rover);
3081 } else {
3082 ret = cma_port_is_unique(bind_list, id_priv);
3083 if (!ret)
3084 cma_bind_port(bind_list, id_priv);
3087 * Remember previously used port number in order to avoid
3088 * re-using same port immediately after it is closed.
3090 if (!ret)
3091 last_used_port = rover;
3092 if (ret != -EADDRNOTAVAIL)
3093 return ret;
3095 if (--remaining) {
3096 rover++;
3097 if ((rover < low) || (rover > high))
3098 rover = low;
3099 goto retry;
3101 return -EADDRNOTAVAIL;
3105 * Check that the requested port is available. This is called when trying to
3106 * bind to a specific port, or when trying to listen on a bound port. In
3107 * the latter case, the provided id_priv may already be on the bind_list, but
3108 * we still need to check that it's okay to start listening.
3110 static int cma_check_port(struct rdma_bind_list *bind_list,
3111 struct rdma_id_private *id_priv, uint8_t reuseaddr)
3113 struct rdma_id_private *cur_id;
3114 struct sockaddr *addr, *cur_addr;
3116 addr = cma_src_addr(id_priv);
3117 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3118 if (id_priv == cur_id)
3119 continue;
3121 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
3122 cur_id->reuseaddr)
3123 continue;
3125 cur_addr = cma_src_addr(cur_id);
3126 if (id_priv->afonly && cur_id->afonly &&
3127 (addr->sa_family != cur_addr->sa_family))
3128 continue;
3130 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3131 return -EADDRNOTAVAIL;
3133 if (!cma_addr_cmp(addr, cur_addr))
3134 return -EADDRINUSE;
3136 return 0;
3139 static int cma_use_port(enum rdma_port_space ps,
3140 struct rdma_id_private *id_priv)
3142 struct rdma_bind_list *bind_list;
3143 unsigned short snum;
3144 int ret;
3146 snum = ntohs(cma_port(cma_src_addr(id_priv)));
3147 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3148 return -EACCES;
3150 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3151 if (!bind_list) {
3152 ret = cma_alloc_port(ps, id_priv, snum);
3153 } else {
3154 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3155 if (!ret)
3156 cma_bind_port(bind_list, id_priv);
3158 return ret;
3161 static int cma_bind_listen(struct rdma_id_private *id_priv)
3163 struct rdma_bind_list *bind_list = id_priv->bind_list;
3164 int ret = 0;
3166 mutex_lock(&lock);
3167 if (bind_list->owners.first->next)
3168 ret = cma_check_port(bind_list, id_priv, 0);
3169 mutex_unlock(&lock);
3170 return ret;
3173 static enum rdma_port_space cma_select_inet_ps(
3174 struct rdma_id_private *id_priv)
3176 switch (id_priv->id.ps) {
3177 case RDMA_PS_TCP:
3178 case RDMA_PS_UDP:
3179 case RDMA_PS_IPOIB:
3180 case RDMA_PS_IB:
3181 return id_priv->id.ps;
3182 default:
3184 return 0;
3188 static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
3190 enum rdma_port_space ps = 0;
3191 struct sockaddr_ib *sib;
3192 u64 sid_ps, mask, sid;
3194 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3195 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3196 sid = be64_to_cpu(sib->sib_sid) & mask;
3198 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3199 sid_ps = RDMA_IB_IP_PS_IB;
3200 ps = RDMA_PS_IB;
3201 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3202 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3203 sid_ps = RDMA_IB_IP_PS_TCP;
3204 ps = RDMA_PS_TCP;
3205 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3206 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3207 sid_ps = RDMA_IB_IP_PS_UDP;
3208 ps = RDMA_PS_UDP;
3211 if (ps) {
3212 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3213 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3214 be64_to_cpu(sib->sib_sid_mask));
3216 return ps;
3219 static int cma_get_port(struct rdma_id_private *id_priv)
3221 enum rdma_port_space ps;
3222 int ret;
3224 if (cma_family(id_priv) != AF_IB)
3225 ps = cma_select_inet_ps(id_priv);
3226 else
3227 ps = cma_select_ib_ps(id_priv);
3228 if (!ps)
3229 return -EPROTONOSUPPORT;
3231 mutex_lock(&lock);
3232 if (cma_any_port(cma_src_addr(id_priv)))
3233 ret = cma_alloc_any_port(ps, id_priv);
3234 else
3235 ret = cma_use_port(ps, id_priv);
3236 mutex_unlock(&lock);
3238 return ret;
3241 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3242 struct sockaddr *addr)
3244 #if IS_ENABLED(CONFIG_IPV6)
3245 struct sockaddr_in6 *sin6;
3247 if (addr->sa_family != AF_INET6)
3248 return 0;
3250 sin6 = (struct sockaddr_in6 *) addr;
3252 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3253 return 0;
3255 if (!sin6->sin6_scope_id)
3256 return -EINVAL;
3258 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3259 #endif
3260 return 0;
3263 int rdma_listen(struct rdma_cm_id *id, int backlog)
3265 struct rdma_id_private *id_priv;
3266 int ret;
3268 id_priv = container_of(id, struct rdma_id_private, id);
3269 if (id_priv->state == RDMA_CM_IDLE) {
3270 id->route.addr.src_addr.ss_family = AF_INET;
3271 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3272 if (ret)
3273 return ret;
3276 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3277 return -EINVAL;
3279 if (id_priv->reuseaddr) {
3280 ret = cma_bind_listen(id_priv);
3281 if (ret)
3282 goto err;
3285 id_priv->backlog = backlog;
3286 if (id->device) {
3287 if (rdma_cap_ib_cm(id->device, 1)) {
3288 ret = cma_ib_listen(id_priv);
3289 if (ret)
3290 goto err;
3291 } else if (rdma_cap_iw_cm(id->device, 1)) {
3292 ret = cma_iw_listen(id_priv, backlog);
3293 if (ret)
3294 goto err;
3295 } else {
3296 ret = -ENOSYS;
3297 goto err;
3299 } else
3300 cma_listen_on_all(id_priv);
3302 return 0;
3303 err:
3304 id_priv->backlog = 0;
3305 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3306 return ret;
3308 EXPORT_SYMBOL(rdma_listen);
3310 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3312 struct rdma_id_private *id_priv;
3313 int ret;
3314 struct sockaddr *daddr;
3316 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3317 addr->sa_family != AF_IB)
3318 return -EAFNOSUPPORT;
3320 id_priv = container_of(id, struct rdma_id_private, id);
3321 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3322 return -EINVAL;
3324 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3325 if (ret)
3326 goto err1;
3328 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3329 if (!cma_any_addr(addr)) {
3330 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3331 if (ret)
3332 goto err1;
3334 ret = cma_acquire_dev(id_priv, NULL);
3335 if (ret)
3336 goto err1;
3339 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3340 if (addr->sa_family == AF_INET)
3341 id_priv->afonly = 1;
3342 #if IS_ENABLED(CONFIG_IPV6)
3343 else if (addr->sa_family == AF_INET6) {
3344 struct net *net = id_priv->id.route.addr.dev_addr.net;
3346 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3348 #endif
3350 ret = cma_get_port(id_priv);
3351 if (ret)
3352 goto err2;
3354 daddr = cma_dst_addr(id_priv);
3355 daddr->sa_family = addr->sa_family;
3357 return 0;
3358 err2:
3359 if (id_priv->cma_dev)
3360 cma_release_dev(id_priv);
3361 err1:
3362 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3363 return ret;
3365 EXPORT_SYMBOL(rdma_bind_addr);
3367 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3369 struct cma_hdr *cma_hdr;
3371 cma_hdr = hdr;
3372 cma_hdr->cma_version = CMA_VERSION;
3373 if (cma_family(id_priv) == AF_INET) {
3374 struct sockaddr_in *src4, *dst4;
3376 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3377 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3379 cma_set_ip_ver(cma_hdr, 4);
3380 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3381 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3382 cma_hdr->port = src4->sin_port;
3383 } else if (cma_family(id_priv) == AF_INET6) {
3384 struct sockaddr_in6 *src6, *dst6;
3386 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3387 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3389 cma_set_ip_ver(cma_hdr, 6);
3390 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3391 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3392 cma_hdr->port = src6->sin6_port;
3394 return 0;
3397 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3398 struct ib_cm_event *ib_event)
3400 struct rdma_id_private *id_priv = cm_id->context;
3401 struct rdma_cm_event event;
3402 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3403 int ret = 0;
3405 mutex_lock(&id_priv->handler_mutex);
3406 if (id_priv->state != RDMA_CM_CONNECT)
3407 goto out;
3409 memset(&event, 0, sizeof event);
3410 switch (ib_event->event) {
3411 case IB_CM_SIDR_REQ_ERROR:
3412 event.event = RDMA_CM_EVENT_UNREACHABLE;
3413 event.status = -ETIMEDOUT;
3414 break;
3415 case IB_CM_SIDR_REP_RECEIVED:
3416 event.param.ud.private_data = ib_event->private_data;
3417 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3418 if (rep->status != IB_SIDR_SUCCESS) {
3419 event.event = RDMA_CM_EVENT_UNREACHABLE;
3420 event.status = ib_event->param.sidr_rep_rcvd.status;
3421 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3422 event.status);
3423 break;
3425 ret = cma_set_qkey(id_priv, rep->qkey);
3426 if (ret) {
3427 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3428 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3429 event.status = ret;
3430 break;
3432 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3433 id_priv->id.route.path_rec,
3434 &event.param.ud.ah_attr);
3435 event.param.ud.qp_num = rep->qpn;
3436 event.param.ud.qkey = rep->qkey;
3437 event.event = RDMA_CM_EVENT_ESTABLISHED;
3438 event.status = 0;
3439 break;
3440 default:
3441 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3442 ib_event->event);
3443 goto out;
3446 ret = id_priv->id.event_handler(&id_priv->id, &event);
3447 if (ret) {
3448 /* Destroy the CM ID by returning a non-zero value. */
3449 id_priv->cm_id.ib = NULL;
3450 cma_exch(id_priv, RDMA_CM_DESTROYING);
3451 mutex_unlock(&id_priv->handler_mutex);
3452 rdma_destroy_id(&id_priv->id);
3453 return ret;
3455 out:
3456 mutex_unlock(&id_priv->handler_mutex);
3457 return ret;
3460 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3461 struct rdma_conn_param *conn_param)
3463 struct ib_cm_sidr_req_param req;
3464 struct ib_cm_id *id;
3465 void *private_data;
3466 int offset, ret;
3468 memset(&req, 0, sizeof req);
3469 offset = cma_user_data_offset(id_priv);
3470 req.private_data_len = offset + conn_param->private_data_len;
3471 if (req.private_data_len < conn_param->private_data_len)
3472 return -EINVAL;
3474 if (req.private_data_len) {
3475 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3476 if (!private_data)
3477 return -ENOMEM;
3478 } else {
3479 private_data = NULL;
3482 if (conn_param->private_data && conn_param->private_data_len)
3483 memcpy(private_data + offset, conn_param->private_data,
3484 conn_param->private_data_len);
3486 if (private_data) {
3487 ret = cma_format_hdr(private_data, id_priv);
3488 if (ret)
3489 goto out;
3490 req.private_data = private_data;
3493 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3494 id_priv);
3495 if (IS_ERR(id)) {
3496 ret = PTR_ERR(id);
3497 goto out;
3499 id_priv->cm_id.ib = id;
3501 req.path = id_priv->id.route.path_rec;
3502 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3503 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3504 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3506 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3507 if (ret) {
3508 ib_destroy_cm_id(id_priv->cm_id.ib);
3509 id_priv->cm_id.ib = NULL;
3511 out:
3512 kfree(private_data);
3513 return ret;
3516 static int cma_connect_ib(struct rdma_id_private *id_priv,
3517 struct rdma_conn_param *conn_param)
3519 struct ib_cm_req_param req;
3520 struct rdma_route *route;
3521 void *private_data;
3522 struct ib_cm_id *id;
3523 int offset, ret;
3525 memset(&req, 0, sizeof req);
3526 offset = cma_user_data_offset(id_priv);
3527 req.private_data_len = offset + conn_param->private_data_len;
3528 if (req.private_data_len < conn_param->private_data_len)
3529 return -EINVAL;
3531 if (req.private_data_len) {
3532 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3533 if (!private_data)
3534 return -ENOMEM;
3535 } else {
3536 private_data = NULL;
3539 if (conn_param->private_data && conn_param->private_data_len)
3540 memcpy(private_data + offset, conn_param->private_data,
3541 conn_param->private_data_len);
3543 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3544 if (IS_ERR(id)) {
3545 ret = PTR_ERR(id);
3546 goto out;
3548 id_priv->cm_id.ib = id;
3550 route = &id_priv->id.route;
3551 if (private_data) {
3552 ret = cma_format_hdr(private_data, id_priv);
3553 if (ret)
3554 goto out;
3555 req.private_data = private_data;
3558 req.primary_path = &route->path_rec[0];
3559 if (route->num_paths == 2)
3560 req.alternate_path = &route->path_rec[1];
3562 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3563 req.qp_num = id_priv->qp_num;
3564 req.qp_type = id_priv->id.qp_type;
3565 req.starting_psn = id_priv->seq_num;
3566 req.responder_resources = conn_param->responder_resources;
3567 req.initiator_depth = conn_param->initiator_depth;
3568 req.flow_control = conn_param->flow_control;
3569 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3570 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3571 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3572 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3573 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3574 req.srq = id_priv->srq ? 1 : 0;
3576 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3577 out:
3578 if (ret && !IS_ERR(id)) {
3579 ib_destroy_cm_id(id);
3580 id_priv->cm_id.ib = NULL;
3583 kfree(private_data);
3584 return ret;
3587 static int cma_connect_iw(struct rdma_id_private *id_priv,
3588 struct rdma_conn_param *conn_param)
3590 struct iw_cm_id *cm_id;
3591 int ret;
3592 struct iw_cm_conn_param iw_param;
3594 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
3595 if (IS_ERR(cm_id))
3596 return PTR_ERR(cm_id);
3598 cm_id->tos = id_priv->tos;
3599 id_priv->cm_id.iw = cm_id;
3601 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3602 rdma_addr_size(cma_src_addr(id_priv)));
3603 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3604 rdma_addr_size(cma_dst_addr(id_priv)));
3606 ret = cma_modify_qp_rtr(id_priv, conn_param);
3607 if (ret)
3608 goto out;
3610 if (conn_param) {
3611 iw_param.ord = conn_param->initiator_depth;
3612 iw_param.ird = conn_param->responder_resources;
3613 iw_param.private_data = conn_param->private_data;
3614 iw_param.private_data_len = conn_param->private_data_len;
3615 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3616 } else {
3617 memset(&iw_param, 0, sizeof iw_param);
3618 iw_param.qpn = id_priv->qp_num;
3620 ret = iw_cm_connect(cm_id, &iw_param);
3621 out:
3622 if (ret) {
3623 iw_destroy_cm_id(cm_id);
3624 id_priv->cm_id.iw = NULL;
3626 return ret;
3629 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3631 struct rdma_id_private *id_priv;
3632 int ret;
3634 id_priv = container_of(id, struct rdma_id_private, id);
3635 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
3636 return -EINVAL;
3638 if (!id->qp) {
3639 id_priv->qp_num = conn_param->qp_num;
3640 id_priv->srq = conn_param->srq;
3643 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3644 if (id->qp_type == IB_QPT_UD)
3645 ret = cma_resolve_ib_udp(id_priv, conn_param);
3646 else
3647 ret = cma_connect_ib(id_priv, conn_param);
3648 } else if (rdma_cap_iw_cm(id->device, id->port_num))
3649 ret = cma_connect_iw(id_priv, conn_param);
3650 else
3651 ret = -ENOSYS;
3652 if (ret)
3653 goto err;
3655 return 0;
3656 err:
3657 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
3658 return ret;
3660 EXPORT_SYMBOL(rdma_connect);
3662 static int cma_accept_ib(struct rdma_id_private *id_priv,
3663 struct rdma_conn_param *conn_param)
3665 struct ib_cm_rep_param rep;
3666 int ret;
3668 ret = cma_modify_qp_rtr(id_priv, conn_param);
3669 if (ret)
3670 goto out;
3672 ret = cma_modify_qp_rts(id_priv, conn_param);
3673 if (ret)
3674 goto out;
3676 memset(&rep, 0, sizeof rep);
3677 rep.qp_num = id_priv->qp_num;
3678 rep.starting_psn = id_priv->seq_num;
3679 rep.private_data = conn_param->private_data;
3680 rep.private_data_len = conn_param->private_data_len;
3681 rep.responder_resources = conn_param->responder_resources;
3682 rep.initiator_depth = conn_param->initiator_depth;
3683 rep.failover_accepted = 0;
3684 rep.flow_control = conn_param->flow_control;
3685 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3686 rep.srq = id_priv->srq ? 1 : 0;
3688 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3689 out:
3690 return ret;
3693 static int cma_accept_iw(struct rdma_id_private *id_priv,
3694 struct rdma_conn_param *conn_param)
3696 struct iw_cm_conn_param iw_param;
3697 int ret;
3699 if (!conn_param)
3700 return -EINVAL;
3702 ret = cma_modify_qp_rtr(id_priv, conn_param);
3703 if (ret)
3704 return ret;
3706 iw_param.ord = conn_param->initiator_depth;
3707 iw_param.ird = conn_param->responder_resources;
3708 iw_param.private_data = conn_param->private_data;
3709 iw_param.private_data_len = conn_param->private_data_len;
3710 if (id_priv->id.qp) {
3711 iw_param.qpn = id_priv->qp_num;
3712 } else
3713 iw_param.qpn = conn_param->qp_num;
3715 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3718 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
3719 enum ib_cm_sidr_status status, u32 qkey,
3720 const void *private_data, int private_data_len)
3722 struct ib_cm_sidr_rep_param rep;
3723 int ret;
3725 memset(&rep, 0, sizeof rep);
3726 rep.status = status;
3727 if (status == IB_SIDR_SUCCESS) {
3728 ret = cma_set_qkey(id_priv, qkey);
3729 if (ret)
3730 return ret;
3731 rep.qp_num = id_priv->qp_num;
3732 rep.qkey = id_priv->qkey;
3734 rep.private_data = private_data;
3735 rep.private_data_len = private_data_len;
3737 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3740 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3742 struct rdma_id_private *id_priv;
3743 int ret;
3745 id_priv = container_of(id, struct rdma_id_private, id);
3747 id_priv->owner = task_pid_nr(current);
3749 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
3750 return -EINVAL;
3752 if (!id->qp && conn_param) {
3753 id_priv->qp_num = conn_param->qp_num;
3754 id_priv->srq = conn_param->srq;
3757 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3758 if (id->qp_type == IB_QPT_UD) {
3759 if (conn_param)
3760 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3761 conn_param->qkey,
3762 conn_param->private_data,
3763 conn_param->private_data_len);
3764 else
3765 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3766 0, NULL, 0);
3767 } else {
3768 if (conn_param)
3769 ret = cma_accept_ib(id_priv, conn_param);
3770 else
3771 ret = cma_rep_recv(id_priv);
3773 } else if (rdma_cap_iw_cm(id->device, id->port_num))
3774 ret = cma_accept_iw(id_priv, conn_param);
3775 else
3776 ret = -ENOSYS;
3778 if (ret)
3779 goto reject;
3781 return 0;
3782 reject:
3783 cma_modify_qp_err(id_priv);
3784 rdma_reject(id, NULL, 0);
3785 return ret;
3787 EXPORT_SYMBOL(rdma_accept);
3789 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3791 struct rdma_id_private *id_priv;
3792 int ret;
3794 id_priv = container_of(id, struct rdma_id_private, id);
3795 if (!id_priv->cm_id.ib)
3796 return -EINVAL;
3798 switch (id->device->node_type) {
3799 case RDMA_NODE_IB_CA:
3800 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3801 break;
3802 default:
3803 ret = 0;
3804 break;
3806 return ret;
3808 EXPORT_SYMBOL(rdma_notify);
3810 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3811 u8 private_data_len)
3813 struct rdma_id_private *id_priv;
3814 int ret;
3816 id_priv = container_of(id, struct rdma_id_private, id);
3817 if (!id_priv->cm_id.ib)
3818 return -EINVAL;
3820 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3821 if (id->qp_type == IB_QPT_UD)
3822 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
3823 private_data, private_data_len);
3824 else
3825 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3826 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3827 0, private_data, private_data_len);
3828 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3829 ret = iw_cm_reject(id_priv->cm_id.iw,
3830 private_data, private_data_len);
3831 } else
3832 ret = -ENOSYS;
3834 return ret;
3836 EXPORT_SYMBOL(rdma_reject);
3838 int rdma_disconnect(struct rdma_cm_id *id)
3840 struct rdma_id_private *id_priv;
3841 int ret;
3843 id_priv = container_of(id, struct rdma_id_private, id);
3844 if (!id_priv->cm_id.ib)
3845 return -EINVAL;
3847 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3848 ret = cma_modify_qp_err(id_priv);
3849 if (ret)
3850 goto out;
3851 /* Initiate or respond to a disconnect. */
3852 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3853 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
3854 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3855 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3856 } else
3857 ret = -EINVAL;
3859 out:
3860 return ret;
3862 EXPORT_SYMBOL(rdma_disconnect);
3864 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3866 struct rdma_id_private *id_priv;
3867 struct cma_multicast *mc = multicast->context;
3868 struct rdma_cm_event event;
3869 int ret = 0;
3871 id_priv = mc->id_priv;
3872 mutex_lock(&id_priv->handler_mutex);
3873 if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3874 id_priv->state != RDMA_CM_ADDR_RESOLVED)
3875 goto out;
3877 if (!status)
3878 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
3879 else
3880 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
3881 status);
3882 mutex_lock(&id_priv->qp_mutex);
3883 if (!status && id_priv->id.qp) {
3884 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
3885 be16_to_cpu(multicast->rec.mlid));
3886 if (status)
3887 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n",
3888 status);
3890 mutex_unlock(&id_priv->qp_mutex);
3892 memset(&event, 0, sizeof event);
3893 event.status = status;
3894 event.param.ud.private_data = mc->context;
3895 if (!status) {
3896 struct rdma_dev_addr *dev_addr =
3897 &id_priv->id.route.addr.dev_addr;
3898 struct net_device *ndev =
3899 dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3900 enum ib_gid_type gid_type =
3901 id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3902 rdma_start_port(id_priv->cma_dev->device)];
3904 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3905 ib_init_ah_from_mcmember(id_priv->id.device,
3906 id_priv->id.port_num, &multicast->rec,
3907 ndev, gid_type,
3908 &event.param.ud.ah_attr);
3909 event.param.ud.qp_num = 0xFFFFFF;
3910 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3911 if (ndev)
3912 dev_put(ndev);
3913 } else
3914 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3916 ret = id_priv->id.event_handler(&id_priv->id, &event);
3917 if (ret) {
3918 cma_exch(id_priv, RDMA_CM_DESTROYING);
3919 mutex_unlock(&id_priv->handler_mutex);
3920 rdma_destroy_id(&id_priv->id);
3921 return 0;
3924 out:
3925 mutex_unlock(&id_priv->handler_mutex);
3926 return 0;
3929 static void cma_set_mgid(struct rdma_id_private *id_priv,
3930 struct sockaddr *addr, union ib_gid *mgid)
3932 unsigned char mc_map[MAX_ADDR_LEN];
3933 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3934 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3935 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3937 if (cma_any_addr(addr)) {
3938 memset(mgid, 0, sizeof *mgid);
3939 } else if ((addr->sa_family == AF_INET6) &&
3940 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
3941 0xFF10A01B)) {
3942 /* IPv6 address is an SA assigned MGID. */
3943 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3944 } else if (addr->sa_family == AF_IB) {
3945 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
3946 } else if ((addr->sa_family == AF_INET6)) {
3947 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3948 if (id_priv->id.ps == RDMA_PS_UDP)
3949 mc_map[7] = 0x01; /* Use RDMA CM signature */
3950 *mgid = *(union ib_gid *) (mc_map + 4);
3951 } else {
3952 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
3953 if (id_priv->id.ps == RDMA_PS_UDP)
3954 mc_map[7] = 0x01; /* Use RDMA CM signature */
3955 *mgid = *(union ib_gid *) (mc_map + 4);
3959 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3960 struct cma_multicast *mc)
3962 struct ib_sa_mcmember_rec rec;
3963 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3964 ib_sa_comp_mask comp_mask;
3965 int ret;
3967 ib_addr_get_mgid(dev_addr, &rec.mgid);
3968 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3969 &rec.mgid, &rec);
3970 if (ret)
3971 return ret;
3973 ret = cma_set_qkey(id_priv, 0);
3974 if (ret)
3975 return ret;
3977 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
3978 rec.qkey = cpu_to_be32(id_priv->qkey);
3979 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
3980 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3981 rec.join_state = mc->join_state;
3983 if ((rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) &&
3984 (!ib_sa_sendonly_fullmem_support(&sa_client,
3985 id_priv->id.device,
3986 id_priv->id.port_num))) {
3987 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3988 "RDMA CM: SM doesn't support Send Only Full Member option\n",
3989 id_priv->id.device->name, id_priv->id.port_num);
3990 return -EOPNOTSUPP;
3993 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3994 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3995 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3996 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3997 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3999 if (id_priv->id.ps == RDMA_PS_IPOIB)
4000 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4001 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4002 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4003 IB_SA_MCMEMBER_REC_MTU |
4004 IB_SA_MCMEMBER_REC_HOP_LIMIT;
4006 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4007 id_priv->id.port_num, &rec,
4008 comp_mask, GFP_KERNEL,
4009 cma_ib_mc_handler, mc);
4010 return PTR_ERR_OR_ZERO(mc->multicast.ib);
4013 static void iboe_mcast_work_handler(struct work_struct *work)
4015 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
4016 struct cma_multicast *mc = mw->mc;
4017 struct ib_sa_multicast *m = mc->multicast.ib;
4019 mc->multicast.ib->context = mc;
4020 cma_ib_mc_handler(0, m);
4021 kref_put(&mc->mcref, release_mc);
4022 kfree(mw);
4025 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
4027 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4028 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4030 if (cma_any_addr(addr)) {
4031 memset(mgid, 0, sizeof *mgid);
4032 } else if (addr->sa_family == AF_INET6) {
4033 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4034 } else {
4035 mgid->raw[0] = 0xff;
4036 mgid->raw[1] = 0x0e;
4037 mgid->raw[2] = 0;
4038 mgid->raw[3] = 0;
4039 mgid->raw[4] = 0;
4040 mgid->raw[5] = 0;
4041 mgid->raw[6] = 0;
4042 mgid->raw[7] = 0;
4043 mgid->raw[8] = 0;
4044 mgid->raw[9] = 0;
4045 mgid->raw[10] = 0xff;
4046 mgid->raw[11] = 0xff;
4047 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4051 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4052 struct cma_multicast *mc)
4054 struct iboe_mcast_work *work;
4055 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4056 int err = 0;
4057 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4058 struct net_device *ndev = NULL;
4059 enum ib_gid_type gid_type;
4060 bool send_only;
4062 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4064 if (cma_zero_addr((struct sockaddr *)&mc->addr))
4065 return -EINVAL;
4067 work = kzalloc(sizeof *work, GFP_KERNEL);
4068 if (!work)
4069 return -ENOMEM;
4071 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
4072 if (!mc->multicast.ib) {
4073 err = -ENOMEM;
4074 goto out1;
4077 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
4079 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
4080 if (id_priv->id.ps == RDMA_PS_UDP)
4081 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4083 if (dev_addr->bound_dev_if)
4084 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
4085 if (!ndev) {
4086 err = -ENODEV;
4087 goto out2;
4089 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
4090 mc->multicast.ib->rec.hop_limit = 1;
4091 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
4093 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4094 rdma_start_port(id_priv->cma_dev->device)];
4095 if (addr->sa_family == AF_INET) {
4096 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4097 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4098 if (!send_only) {
4099 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
4100 true);
4101 if (!err)
4102 mc->igmp_joined = true;
4105 } else {
4106 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4107 err = -ENOTSUPP;
4109 dev_put(ndev);
4110 if (err || !mc->multicast.ib->rec.mtu) {
4111 if (!err)
4112 err = -EINVAL;
4113 goto out2;
4115 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4116 &mc->multicast.ib->rec.port_gid);
4117 work->id = id_priv;
4118 work->mc = mc;
4119 INIT_WORK(&work->work, iboe_mcast_work_handler);
4120 kref_get(&mc->mcref);
4121 queue_work(cma_wq, &work->work);
4123 return 0;
4125 out2:
4126 kfree(mc->multicast.ib);
4127 out1:
4128 kfree(work);
4129 return err;
4132 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4133 u8 join_state, void *context)
4135 struct rdma_id_private *id_priv;
4136 struct cma_multicast *mc;
4137 int ret;
4139 id_priv = container_of(id, struct rdma_id_private, id);
4140 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4141 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
4142 return -EINVAL;
4144 mc = kmalloc(sizeof *mc, GFP_KERNEL);
4145 if (!mc)
4146 return -ENOMEM;
4148 memcpy(&mc->addr, addr, rdma_addr_size(addr));
4149 mc->context = context;
4150 mc->id_priv = id_priv;
4151 mc->igmp_joined = false;
4152 mc->join_state = join_state;
4153 spin_lock(&id_priv->lock);
4154 list_add(&mc->list, &id_priv->mc_list);
4155 spin_unlock(&id_priv->lock);
4157 if (rdma_protocol_roce(id->device, id->port_num)) {
4158 kref_init(&mc->mcref);
4159 ret = cma_iboe_join_multicast(id_priv, mc);
4160 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
4161 ret = cma_join_ib_multicast(id_priv, mc);
4162 else
4163 ret = -ENOSYS;
4165 if (ret) {
4166 spin_lock_irq(&id_priv->lock);
4167 list_del(&mc->list);
4168 spin_unlock_irq(&id_priv->lock);
4169 kfree(mc);
4171 return ret;
4173 EXPORT_SYMBOL(rdma_join_multicast);
4175 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4177 struct rdma_id_private *id_priv;
4178 struct cma_multicast *mc;
4180 id_priv = container_of(id, struct rdma_id_private, id);
4181 spin_lock_irq(&id_priv->lock);
4182 list_for_each_entry(mc, &id_priv->mc_list, list) {
4183 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
4184 list_del(&mc->list);
4185 spin_unlock_irq(&id_priv->lock);
4187 if (id->qp)
4188 ib_detach_mcast(id->qp,
4189 &mc->multicast.ib->rec.mgid,
4190 be16_to_cpu(mc->multicast.ib->rec.mlid));
4192 BUG_ON(id_priv->cma_dev->device != id->device);
4194 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4195 ib_sa_free_multicast(mc->multicast.ib);
4196 kfree(mc);
4197 } else if (rdma_protocol_roce(id->device, id->port_num)) {
4198 if (mc->igmp_joined) {
4199 struct rdma_dev_addr *dev_addr =
4200 &id->route.addr.dev_addr;
4201 struct net_device *ndev = NULL;
4203 if (dev_addr->bound_dev_if)
4204 ndev = dev_get_by_index(&init_net,
4205 dev_addr->bound_dev_if);
4206 if (ndev) {
4207 cma_igmp_send(ndev,
4208 &mc->multicast.ib->rec.mgid,
4209 false);
4210 dev_put(ndev);
4212 mc->igmp_joined = false;
4214 kref_put(&mc->mcref, release_mc);
4216 return;
4219 spin_unlock_irq(&id_priv->lock);
4221 EXPORT_SYMBOL(rdma_leave_multicast);
4223 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4225 struct rdma_dev_addr *dev_addr;
4226 struct cma_ndev_work *work;
4228 dev_addr = &id_priv->id.route.addr.dev_addr;
4230 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4231 (net_eq(dev_net(ndev), dev_addr->net)) &&
4232 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4233 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4234 ndev->name, &id_priv->id);
4235 work = kzalloc(sizeof *work, GFP_KERNEL);
4236 if (!work)
4237 return -ENOMEM;
4239 INIT_WORK(&work->work, cma_ndev_work_handler);
4240 work->id = id_priv;
4241 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4242 atomic_inc(&id_priv->refcount);
4243 queue_work(cma_wq, &work->work);
4246 return 0;
4249 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4250 void *ptr)
4252 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4253 struct cma_device *cma_dev;
4254 struct rdma_id_private *id_priv;
4255 int ret = NOTIFY_DONE;
4257 if (event != NETDEV_BONDING_FAILOVER)
4258 return NOTIFY_DONE;
4260 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
4261 return NOTIFY_DONE;
4263 mutex_lock(&lock);
4264 list_for_each_entry(cma_dev, &dev_list, list)
4265 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4266 ret = cma_netdev_change(ndev, id_priv);
4267 if (ret)
4268 goto out;
4271 out:
4272 mutex_unlock(&lock);
4273 return ret;
4276 static struct notifier_block cma_nb = {
4277 .notifier_call = cma_netdev_callback
4280 static void cma_add_one(struct ib_device *device)
4282 struct cma_device *cma_dev;
4283 struct rdma_id_private *id_priv;
4284 unsigned int i;
4285 unsigned long supported_gids = 0;
4287 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4288 if (!cma_dev)
4289 return;
4291 cma_dev->device = device;
4292 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4293 sizeof(*cma_dev->default_gid_type),
4294 GFP_KERNEL);
4295 if (!cma_dev->default_gid_type)
4296 goto free_cma_dev;
4298 cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
4299 sizeof(*cma_dev->default_roce_tos),
4300 GFP_KERNEL);
4301 if (!cma_dev->default_roce_tos)
4302 goto free_gid_type;
4304 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4305 supported_gids = roce_gid_type_mask_support(device, i);
4306 WARN_ON(!supported_gids);
4307 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4308 find_first_bit(&supported_gids, BITS_PER_LONG);
4309 cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
4312 init_completion(&cma_dev->comp);
4313 atomic_set(&cma_dev->refcount, 1);
4314 INIT_LIST_HEAD(&cma_dev->id_list);
4315 ib_set_client_data(device, &cma_client, cma_dev);
4317 mutex_lock(&lock);
4318 list_add_tail(&cma_dev->list, &dev_list);
4319 list_for_each_entry(id_priv, &listen_any_list, list)
4320 cma_listen_on_dev(id_priv, cma_dev);
4321 mutex_unlock(&lock);
4323 return;
4325 free_gid_type:
4326 kfree(cma_dev->default_gid_type);
4328 free_cma_dev:
4329 kfree(cma_dev);
4331 return;
4334 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4336 struct rdma_cm_event event;
4337 enum rdma_cm_state state;
4338 int ret = 0;
4340 /* Record that we want to remove the device */
4341 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4342 if (state == RDMA_CM_DESTROYING)
4343 return 0;
4345 cma_cancel_operation(id_priv, state);
4346 mutex_lock(&id_priv->handler_mutex);
4348 /* Check for destruction from another callback. */
4349 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
4350 goto out;
4352 memset(&event, 0, sizeof event);
4353 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
4354 ret = id_priv->id.event_handler(&id_priv->id, &event);
4355 out:
4356 mutex_unlock(&id_priv->handler_mutex);
4357 return ret;
4360 static void cma_process_remove(struct cma_device *cma_dev)
4362 struct rdma_id_private *id_priv;
4363 int ret;
4365 mutex_lock(&lock);
4366 while (!list_empty(&cma_dev->id_list)) {
4367 id_priv = list_entry(cma_dev->id_list.next,
4368 struct rdma_id_private, list);
4370 list_del(&id_priv->listen_list);
4371 list_del_init(&id_priv->list);
4372 atomic_inc(&id_priv->refcount);
4373 mutex_unlock(&lock);
4375 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
4376 cma_deref_id(id_priv);
4377 if (ret)
4378 rdma_destroy_id(&id_priv->id);
4380 mutex_lock(&lock);
4382 mutex_unlock(&lock);
4384 cma_deref_dev(cma_dev);
4385 wait_for_completion(&cma_dev->comp);
4388 static void cma_remove_one(struct ib_device *device, void *client_data)
4390 struct cma_device *cma_dev = client_data;
4392 if (!cma_dev)
4393 return;
4395 mutex_lock(&lock);
4396 list_del(&cma_dev->list);
4397 mutex_unlock(&lock);
4399 cma_process_remove(cma_dev);
4400 kfree(cma_dev->default_roce_tos);
4401 kfree(cma_dev->default_gid_type);
4402 kfree(cma_dev);
4405 static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
4407 struct nlmsghdr *nlh;
4408 struct rdma_cm_id_stats *id_stats;
4409 struct rdma_id_private *id_priv;
4410 struct rdma_cm_id *id = NULL;
4411 struct cma_device *cma_dev;
4412 int i_dev = 0, i_id = 0;
4415 * We export all of the IDs as a sequence of messages. Each
4416 * ID gets its own netlink message.
4418 mutex_lock(&lock);
4420 list_for_each_entry(cma_dev, &dev_list, list) {
4421 if (i_dev < cb->args[0]) {
4422 i_dev++;
4423 continue;
4426 i_id = 0;
4427 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4428 if (i_id < cb->args[1]) {
4429 i_id++;
4430 continue;
4433 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
4434 sizeof *id_stats, RDMA_NL_RDMA_CM,
4435 RDMA_NL_RDMA_CM_ID_STATS,
4436 NLM_F_MULTI);
4437 if (!id_stats)
4438 goto out;
4440 memset(id_stats, 0, sizeof *id_stats);
4441 id = &id_priv->id;
4442 id_stats->node_type = id->route.addr.dev_addr.dev_type;
4443 id_stats->port_num = id->port_num;
4444 id_stats->bound_dev_if =
4445 id->route.addr.dev_addr.bound_dev_if;
4447 if (ibnl_put_attr(skb, nlh,
4448 rdma_addr_size(cma_src_addr(id_priv)),
4449 cma_src_addr(id_priv),
4450 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
4451 goto out;
4452 if (ibnl_put_attr(skb, nlh,
4453 rdma_addr_size(cma_src_addr(id_priv)),
4454 cma_dst_addr(id_priv),
4455 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4456 goto out;
4458 id_stats->pid = id_priv->owner;
4459 id_stats->port_space = id->ps;
4460 id_stats->cm_state = id_priv->state;
4461 id_stats->qp_num = id_priv->qp_num;
4462 id_stats->qp_type = id->qp_type;
4464 i_id++;
4467 cb->args[1] = 0;
4468 i_dev++;
4471 out:
4472 mutex_unlock(&lock);
4473 cb->args[0] = i_dev;
4474 cb->args[1] = i_id;
4476 return skb->len;
4479 static const struct ibnl_client_cbs cma_cb_table[] = {
4480 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4481 .module = THIS_MODULE },
4484 static int cma_init_net(struct net *net)
4486 struct cma_pernet *pernet = cma_pernet(net);
4488 idr_init(&pernet->tcp_ps);
4489 idr_init(&pernet->udp_ps);
4490 idr_init(&pernet->ipoib_ps);
4491 idr_init(&pernet->ib_ps);
4493 return 0;
4496 static void cma_exit_net(struct net *net)
4498 struct cma_pernet *pernet = cma_pernet(net);
4500 idr_destroy(&pernet->tcp_ps);
4501 idr_destroy(&pernet->udp_ps);
4502 idr_destroy(&pernet->ipoib_ps);
4503 idr_destroy(&pernet->ib_ps);
4506 static struct pernet_operations cma_pernet_operations = {
4507 .init = cma_init_net,
4508 .exit = cma_exit_net,
4509 .id = &cma_pernet_id,
4510 .size = sizeof(struct cma_pernet),
4513 static int __init cma_init(void)
4515 int ret;
4517 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4518 if (!cma_wq)
4519 return -ENOMEM;
4521 ret = register_pernet_subsys(&cma_pernet_operations);
4522 if (ret)
4523 goto err_wq;
4525 ib_sa_register_client(&sa_client);
4526 rdma_addr_register_client(&addr_client);
4527 register_netdevice_notifier(&cma_nb);
4529 ret = ib_register_client(&cma_client);
4530 if (ret)
4531 goto err;
4533 if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
4534 cma_cb_table))
4535 pr_warn("RDMA CMA: failed to add netlink callback\n");
4536 cma_configfs_init();
4538 return 0;
4540 err:
4541 unregister_netdevice_notifier(&cma_nb);
4542 rdma_addr_unregister_client(&addr_client);
4543 ib_sa_unregister_client(&sa_client);
4544 err_wq:
4545 destroy_workqueue(cma_wq);
4546 return ret;
4549 static void __exit cma_cleanup(void)
4551 cma_configfs_exit();
4552 ibnl_remove_client(RDMA_NL_RDMA_CM);
4553 ib_unregister_client(&cma_client);
4554 unregister_netdevice_notifier(&cma_nb);
4555 rdma_addr_unregister_client(&addr_client);
4556 ib_sa_unregister_client(&sa_client);
4557 unregister_pernet_subsys(&cma_pernet_operations);
4558 destroy_workqueue(cma_wq);
4561 module_init(cma_init);
4562 module_exit(cma_cleanup);