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