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