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