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