1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 5 */ 6 7 #include <linux/dma-mapping.h> 8 #include <net/addrconf.h> 9 #include <rdma/uverbs_ioctl.h> 10 #include "rxe.h" 11 #include "rxe_loc.h" 12 #include "rxe_queue.h" 13 #include "rxe_hw_counters.h" 14 15 static int rxe_query_device(struct ib_device *dev, 16 struct ib_device_attr *attr, 17 struct ib_udata *uhw) 18 { 19 struct rxe_dev *rxe = to_rdev(dev); 20 21 if (uhw->inlen || uhw->outlen) 22 return -EINVAL; 23 24 *attr = rxe->attr; 25 return 0; 26 } 27 28 static int rxe_query_port(struct ib_device *dev, 29 u32 port_num, struct ib_port_attr *attr) 30 { 31 struct rxe_dev *rxe = to_rdev(dev); 32 int rc; 33 34 /* *attr being zeroed by the caller, avoid zeroing it here */ 35 *attr = rxe->port.attr; 36 37 mutex_lock(&rxe->usdev_lock); 38 rc = ib_get_eth_speed(dev, port_num, &attr->active_speed, 39 &attr->active_width); 40 41 if (attr->state == IB_PORT_ACTIVE) 42 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 43 else if (dev_get_flags(rxe->ndev) & IFF_UP) 44 attr->phys_state = IB_PORT_PHYS_STATE_POLLING; 45 else 46 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED; 47 48 mutex_unlock(&rxe->usdev_lock); 49 50 return rc; 51 } 52 53 static int rxe_query_pkey(struct ib_device *device, 54 u32 port_num, u16 index, u16 *pkey) 55 { 56 if (index > 0) 57 return -EINVAL; 58 59 *pkey = IB_DEFAULT_PKEY_FULL; 60 return 0; 61 } 62 63 static int rxe_modify_device(struct ib_device *dev, 64 int mask, struct ib_device_modify *attr) 65 { 66 struct rxe_dev *rxe = to_rdev(dev); 67 68 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID | 69 IB_DEVICE_MODIFY_NODE_DESC)) 70 return -EOPNOTSUPP; 71 72 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) 73 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid); 74 75 if (mask & IB_DEVICE_MODIFY_NODE_DESC) { 76 memcpy(rxe->ib_dev.node_desc, 77 attr->node_desc, sizeof(rxe->ib_dev.node_desc)); 78 } 79 80 return 0; 81 } 82 83 static int rxe_modify_port(struct ib_device *dev, 84 u32 port_num, int mask, struct ib_port_modify *attr) 85 { 86 struct rxe_dev *rxe = to_rdev(dev); 87 struct rxe_port *port; 88 89 port = &rxe->port; 90 91 port->attr.port_cap_flags |= attr->set_port_cap_mask; 92 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask; 93 94 if (mask & IB_PORT_RESET_QKEY_CNTR) 95 port->attr.qkey_viol_cntr = 0; 96 97 return 0; 98 } 99 100 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev, 101 u32 port_num) 102 { 103 return IB_LINK_LAYER_ETHERNET; 104 } 105 106 static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata) 107 { 108 struct rxe_dev *rxe = to_rdev(ibuc->device); 109 struct rxe_ucontext *uc = to_ruc(ibuc); 110 111 return rxe_add_to_pool(&rxe->uc_pool, uc); 112 } 113 114 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) 115 { 116 struct rxe_ucontext *uc = to_ruc(ibuc); 117 118 rxe_drop_ref(uc); 119 } 120 121 static int rxe_port_immutable(struct ib_device *dev, u32 port_num, 122 struct ib_port_immutable *immutable) 123 { 124 int err; 125 struct ib_port_attr attr; 126 127 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 128 129 err = ib_query_port(dev, port_num, &attr); 130 if (err) 131 return err; 132 133 immutable->pkey_tbl_len = attr.pkey_tbl_len; 134 immutable->gid_tbl_len = attr.gid_tbl_len; 135 immutable->max_mad_size = IB_MGMT_MAD_SIZE; 136 137 return 0; 138 } 139 140 static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 141 { 142 struct rxe_dev *rxe = to_rdev(ibpd->device); 143 struct rxe_pd *pd = to_rpd(ibpd); 144 145 return rxe_add_to_pool(&rxe->pd_pool, pd); 146 } 147 148 static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 149 { 150 struct rxe_pd *pd = to_rpd(ibpd); 151 152 rxe_drop_ref(pd); 153 return 0; 154 } 155 156 static int rxe_create_ah(struct ib_ah *ibah, 157 struct rdma_ah_init_attr *init_attr, 158 struct ib_udata *udata) 159 160 { 161 struct rxe_dev *rxe = to_rdev(ibah->device); 162 struct rxe_ah *ah = to_rah(ibah); 163 struct rxe_create_ah_resp __user *uresp = NULL; 164 int err; 165 166 if (udata) { 167 /* test if new user provider */ 168 if (udata->outlen >= sizeof(*uresp)) 169 uresp = udata->outbuf; 170 ah->is_user = true; 171 } else { 172 ah->is_user = false; 173 } 174 175 err = rxe_av_chk_attr(rxe, init_attr->ah_attr); 176 if (err) 177 return err; 178 179 err = rxe_add_to_pool(&rxe->ah_pool, ah); 180 if (err) 181 return err; 182 183 /* create index > 0 */ 184 rxe_add_index(ah); 185 ah->ah_num = ah->elem.index; 186 187 if (uresp) { 188 /* only if new user provider */ 189 err = copy_to_user(&uresp->ah_num, &ah->ah_num, 190 sizeof(uresp->ah_num)); 191 if (err) { 192 rxe_drop_index(ah); 193 rxe_drop_ref(ah); 194 return -EFAULT; 195 } 196 } else if (ah->is_user) { 197 /* only if old user provider */ 198 ah->ah_num = 0; 199 } 200 201 rxe_init_av(init_attr->ah_attr, &ah->av); 202 return 0; 203 } 204 205 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr) 206 { 207 int err; 208 struct rxe_dev *rxe = to_rdev(ibah->device); 209 struct rxe_ah *ah = to_rah(ibah); 210 211 err = rxe_av_chk_attr(rxe, attr); 212 if (err) 213 return err; 214 215 rxe_init_av(attr, &ah->av); 216 return 0; 217 } 218 219 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr) 220 { 221 struct rxe_ah *ah = to_rah(ibah); 222 223 memset(attr, 0, sizeof(*attr)); 224 attr->type = ibah->type; 225 rxe_av_to_attr(&ah->av, attr); 226 return 0; 227 } 228 229 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags) 230 { 231 struct rxe_ah *ah = to_rah(ibah); 232 233 rxe_drop_index(ah); 234 rxe_drop_ref(ah); 235 return 0; 236 } 237 238 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr) 239 { 240 int err; 241 int i; 242 u32 length; 243 struct rxe_recv_wqe *recv_wqe; 244 int num_sge = ibwr->num_sge; 245 int full; 246 247 full = queue_full(rq->queue, QUEUE_TYPE_TO_DRIVER); 248 if (unlikely(full)) { 249 err = -ENOMEM; 250 goto err1; 251 } 252 253 if (unlikely(num_sge > rq->max_sge)) { 254 err = -EINVAL; 255 goto err1; 256 } 257 258 length = 0; 259 for (i = 0; i < num_sge; i++) 260 length += ibwr->sg_list[i].length; 261 262 recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_TO_DRIVER); 263 recv_wqe->wr_id = ibwr->wr_id; 264 recv_wqe->num_sge = num_sge; 265 266 memcpy(recv_wqe->dma.sge, ibwr->sg_list, 267 num_sge * sizeof(struct ib_sge)); 268 269 recv_wqe->dma.length = length; 270 recv_wqe->dma.resid = length; 271 recv_wqe->dma.num_sge = num_sge; 272 recv_wqe->dma.cur_sge = 0; 273 recv_wqe->dma.sge_offset = 0; 274 275 queue_advance_producer(rq->queue, QUEUE_TYPE_TO_DRIVER); 276 277 return 0; 278 279 err1: 280 return err; 281 } 282 283 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init, 284 struct ib_udata *udata) 285 { 286 int err; 287 struct rxe_dev *rxe = to_rdev(ibsrq->device); 288 struct rxe_pd *pd = to_rpd(ibsrq->pd); 289 struct rxe_srq *srq = to_rsrq(ibsrq); 290 struct rxe_create_srq_resp __user *uresp = NULL; 291 292 if (init->srq_type != IB_SRQT_BASIC) 293 return -EOPNOTSUPP; 294 295 if (udata) { 296 if (udata->outlen < sizeof(*uresp)) 297 return -EINVAL; 298 uresp = udata->outbuf; 299 } 300 301 err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK); 302 if (err) 303 goto err1; 304 305 err = rxe_add_to_pool(&rxe->srq_pool, srq); 306 if (err) 307 goto err1; 308 309 rxe_add_ref(pd); 310 srq->pd = pd; 311 312 err = rxe_srq_from_init(rxe, srq, init, udata, uresp); 313 if (err) 314 goto err2; 315 316 return 0; 317 318 err2: 319 rxe_drop_ref(pd); 320 rxe_drop_ref(srq); 321 err1: 322 return err; 323 } 324 325 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 326 enum ib_srq_attr_mask mask, 327 struct ib_udata *udata) 328 { 329 int err; 330 struct rxe_srq *srq = to_rsrq(ibsrq); 331 struct rxe_dev *rxe = to_rdev(ibsrq->device); 332 struct rxe_modify_srq_cmd ucmd = {}; 333 334 if (udata) { 335 if (udata->inlen < sizeof(ucmd)) 336 return -EINVAL; 337 338 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)); 339 if (err) 340 return err; 341 } 342 343 err = rxe_srq_chk_attr(rxe, srq, attr, mask); 344 if (err) 345 goto err1; 346 347 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata); 348 if (err) 349 goto err1; 350 351 return 0; 352 353 err1: 354 return err; 355 } 356 357 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr) 358 { 359 struct rxe_srq *srq = to_rsrq(ibsrq); 360 361 if (srq->error) 362 return -EINVAL; 363 364 attr->max_wr = srq->rq.queue->buf->index_mask; 365 attr->max_sge = srq->rq.max_sge; 366 attr->srq_limit = srq->limit; 367 return 0; 368 } 369 370 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) 371 { 372 struct rxe_srq *srq = to_rsrq(ibsrq); 373 374 if (srq->rq.queue) 375 rxe_queue_cleanup(srq->rq.queue); 376 377 rxe_drop_ref(srq->pd); 378 rxe_drop_ref(srq); 379 return 0; 380 } 381 382 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, 383 const struct ib_recv_wr **bad_wr) 384 { 385 int err = 0; 386 struct rxe_srq *srq = to_rsrq(ibsrq); 387 388 spin_lock_bh(&srq->rq.producer_lock); 389 390 while (wr) { 391 err = post_one_recv(&srq->rq, wr); 392 if (unlikely(err)) 393 break; 394 wr = wr->next; 395 } 396 397 spin_unlock_bh(&srq->rq.producer_lock); 398 399 if (err) 400 *bad_wr = wr; 401 402 return err; 403 } 404 405 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, 406 struct ib_udata *udata) 407 { 408 int err; 409 struct rxe_dev *rxe = to_rdev(ibqp->device); 410 struct rxe_pd *pd = to_rpd(ibqp->pd); 411 struct rxe_qp *qp = to_rqp(ibqp); 412 struct rxe_create_qp_resp __user *uresp = NULL; 413 414 if (udata) { 415 if (udata->outlen < sizeof(*uresp)) 416 return -EINVAL; 417 uresp = udata->outbuf; 418 } 419 420 if (init->create_flags) 421 return -EOPNOTSUPP; 422 423 err = rxe_qp_chk_init(rxe, init); 424 if (err) 425 return err; 426 427 if (udata) { 428 if (udata->inlen) 429 return -EINVAL; 430 431 qp->is_user = true; 432 } else { 433 qp->is_user = false; 434 } 435 436 err = rxe_add_to_pool(&rxe->qp_pool, qp); 437 if (err) 438 return err; 439 440 rxe_add_index(qp); 441 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); 442 if (err) 443 goto qp_init; 444 445 return 0; 446 447 qp_init: 448 rxe_drop_index(qp); 449 rxe_drop_ref(qp); 450 return err; 451 } 452 453 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 454 int mask, struct ib_udata *udata) 455 { 456 int err; 457 struct rxe_dev *rxe = to_rdev(ibqp->device); 458 struct rxe_qp *qp = to_rqp(ibqp); 459 460 if (mask & ~IB_QP_ATTR_STANDARD_BITS) 461 return -EOPNOTSUPP; 462 463 err = rxe_qp_chk_attr(rxe, qp, attr, mask); 464 if (err) 465 goto err1; 466 467 err = rxe_qp_from_attr(qp, attr, mask, udata); 468 if (err) 469 goto err1; 470 471 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH)) 472 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label, 473 qp->ibqp.qp_num, 474 qp->attr.dest_qp_num); 475 476 return 0; 477 478 err1: 479 return err; 480 } 481 482 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 483 int mask, struct ib_qp_init_attr *init) 484 { 485 struct rxe_qp *qp = to_rqp(ibqp); 486 487 rxe_qp_to_init(qp, init); 488 rxe_qp_to_attr(qp, attr, mask); 489 490 return 0; 491 } 492 493 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 494 { 495 struct rxe_qp *qp = to_rqp(ibqp); 496 497 rxe_qp_destroy(qp); 498 rxe_drop_index(qp); 499 rxe_drop_ref(qp); 500 return 0; 501 } 502 503 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 504 unsigned int mask, unsigned int length) 505 { 506 int num_sge = ibwr->num_sge; 507 struct rxe_sq *sq = &qp->sq; 508 509 if (unlikely(num_sge > sq->max_sge)) 510 goto err1; 511 512 if (unlikely(mask & WR_ATOMIC_MASK)) { 513 if (length < 8) 514 goto err1; 515 516 if (atomic_wr(ibwr)->remote_addr & 0x7) 517 goto err1; 518 } 519 520 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) && 521 (length > sq->max_inline))) 522 goto err1; 523 524 return 0; 525 526 err1: 527 return -EINVAL; 528 } 529 530 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr, 531 const struct ib_send_wr *ibwr) 532 { 533 wr->wr_id = ibwr->wr_id; 534 wr->num_sge = ibwr->num_sge; 535 wr->opcode = ibwr->opcode; 536 wr->send_flags = ibwr->send_flags; 537 538 if (qp_type(qp) == IB_QPT_UD || 539 qp_type(qp) == IB_QPT_SMI || 540 qp_type(qp) == IB_QPT_GSI) { 541 struct ib_ah *ibah = ud_wr(ibwr)->ah; 542 543 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn; 544 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey; 545 wr->wr.ud.ah_num = to_rah(ibah)->ah_num; 546 if (qp_type(qp) == IB_QPT_GSI) 547 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index; 548 if (wr->opcode == IB_WR_SEND_WITH_IMM) 549 wr->ex.imm_data = ibwr->ex.imm_data; 550 } else { 551 switch (wr->opcode) { 552 case IB_WR_RDMA_WRITE_WITH_IMM: 553 wr->ex.imm_data = ibwr->ex.imm_data; 554 fallthrough; 555 case IB_WR_RDMA_READ: 556 case IB_WR_RDMA_WRITE: 557 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr; 558 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey; 559 break; 560 case IB_WR_SEND_WITH_IMM: 561 wr->ex.imm_data = ibwr->ex.imm_data; 562 break; 563 case IB_WR_SEND_WITH_INV: 564 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 565 break; 566 case IB_WR_ATOMIC_CMP_AND_SWP: 567 case IB_WR_ATOMIC_FETCH_AND_ADD: 568 wr->wr.atomic.remote_addr = 569 atomic_wr(ibwr)->remote_addr; 570 wr->wr.atomic.compare_add = 571 atomic_wr(ibwr)->compare_add; 572 wr->wr.atomic.swap = atomic_wr(ibwr)->swap; 573 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey; 574 break; 575 case IB_WR_LOCAL_INV: 576 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 577 break; 578 case IB_WR_REG_MR: 579 wr->wr.reg.mr = reg_wr(ibwr)->mr; 580 wr->wr.reg.key = reg_wr(ibwr)->key; 581 wr->wr.reg.access = reg_wr(ibwr)->access; 582 break; 583 default: 584 break; 585 } 586 } 587 } 588 589 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe, 590 const struct ib_send_wr *ibwr) 591 { 592 struct ib_sge *sge = ibwr->sg_list; 593 u8 *p = wqe->dma.inline_data; 594 int i; 595 596 for (i = 0; i < ibwr->num_sge; i++, sge++) { 597 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length); 598 p += sge->length; 599 } 600 } 601 602 static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 603 unsigned int mask, unsigned int length, 604 struct rxe_send_wqe *wqe) 605 { 606 int num_sge = ibwr->num_sge; 607 608 init_send_wr(qp, &wqe->wr, ibwr); 609 610 /* local operation */ 611 if (unlikely(mask & WR_LOCAL_OP_MASK)) { 612 wqe->mask = mask; 613 wqe->state = wqe_state_posted; 614 return; 615 } 616 617 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) 618 copy_inline_data_to_wqe(wqe, ibwr); 619 else 620 memcpy(wqe->dma.sge, ibwr->sg_list, 621 num_sge * sizeof(struct ib_sge)); 622 623 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr : 624 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0; 625 wqe->mask = mask; 626 wqe->dma.length = length; 627 wqe->dma.resid = length; 628 wqe->dma.num_sge = num_sge; 629 wqe->dma.cur_sge = 0; 630 wqe->dma.sge_offset = 0; 631 wqe->state = wqe_state_posted; 632 wqe->ssn = atomic_add_return(1, &qp->ssn); 633 } 634 635 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 636 unsigned int mask, u32 length) 637 { 638 int err; 639 struct rxe_sq *sq = &qp->sq; 640 struct rxe_send_wqe *send_wqe; 641 int full; 642 643 err = validate_send_wr(qp, ibwr, mask, length); 644 if (err) 645 return err; 646 647 spin_lock_bh(&qp->sq.sq_lock); 648 649 full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER); 650 651 if (unlikely(full)) { 652 spin_unlock_bh(&qp->sq.sq_lock); 653 return -ENOMEM; 654 } 655 656 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER); 657 init_send_wqe(qp, ibwr, mask, length, send_wqe); 658 659 queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER); 660 661 spin_unlock_bh(&qp->sq.sq_lock); 662 663 return 0; 664 } 665 666 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr, 667 const struct ib_send_wr **bad_wr) 668 { 669 int err = 0; 670 unsigned int mask; 671 unsigned int length = 0; 672 int i; 673 struct ib_send_wr *next; 674 675 while (wr) { 676 mask = wr_opcode_mask(wr->opcode, qp); 677 if (unlikely(!mask)) { 678 err = -EINVAL; 679 *bad_wr = wr; 680 break; 681 } 682 683 if (unlikely((wr->send_flags & IB_SEND_INLINE) && 684 !(mask & WR_INLINE_MASK))) { 685 err = -EINVAL; 686 *bad_wr = wr; 687 break; 688 } 689 690 next = wr->next; 691 692 length = 0; 693 for (i = 0; i < wr->num_sge; i++) 694 length += wr->sg_list[i].length; 695 696 err = post_one_send(qp, wr, mask, length); 697 698 if (err) { 699 *bad_wr = wr; 700 break; 701 } 702 wr = next; 703 } 704 705 rxe_run_task(&qp->req.task, 1); 706 if (unlikely(qp->req.state == QP_STATE_ERROR)) 707 rxe_run_task(&qp->comp.task, 1); 708 709 return err; 710 } 711 712 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 713 const struct ib_send_wr **bad_wr) 714 { 715 struct rxe_qp *qp = to_rqp(ibqp); 716 717 if (unlikely(!qp->valid)) { 718 *bad_wr = wr; 719 return -EINVAL; 720 } 721 722 if (unlikely(qp->req.state < QP_STATE_READY)) { 723 *bad_wr = wr; 724 return -EINVAL; 725 } 726 727 if (qp->is_user) { 728 /* Utilize process context to do protocol processing */ 729 rxe_run_task(&qp->req.task, 0); 730 return 0; 731 } else 732 return rxe_post_send_kernel(qp, wr, bad_wr); 733 } 734 735 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 736 const struct ib_recv_wr **bad_wr) 737 { 738 int err = 0; 739 struct rxe_qp *qp = to_rqp(ibqp); 740 struct rxe_rq *rq = &qp->rq; 741 742 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) { 743 *bad_wr = wr; 744 err = -EINVAL; 745 goto err1; 746 } 747 748 if (unlikely(qp->srq)) { 749 *bad_wr = wr; 750 err = -EINVAL; 751 goto err1; 752 } 753 754 spin_lock_bh(&rq->producer_lock); 755 756 while (wr) { 757 err = post_one_recv(rq, wr); 758 if (unlikely(err)) { 759 *bad_wr = wr; 760 break; 761 } 762 wr = wr->next; 763 } 764 765 spin_unlock_bh(&rq->producer_lock); 766 767 if (qp->resp.state == QP_STATE_ERROR) 768 rxe_run_task(&qp->resp.task, 1); 769 770 err1: 771 return err; 772 } 773 774 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 775 struct ib_udata *udata) 776 { 777 int err; 778 struct ib_device *dev = ibcq->device; 779 struct rxe_dev *rxe = to_rdev(dev); 780 struct rxe_cq *cq = to_rcq(ibcq); 781 struct rxe_create_cq_resp __user *uresp = NULL; 782 783 if (udata) { 784 if (udata->outlen < sizeof(*uresp)) 785 return -EINVAL; 786 uresp = udata->outbuf; 787 } 788 789 if (attr->flags) 790 return -EOPNOTSUPP; 791 792 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector); 793 if (err) 794 return err; 795 796 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata, 797 uresp); 798 if (err) 799 return err; 800 801 return rxe_add_to_pool(&rxe->cq_pool, cq); 802 } 803 804 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 805 { 806 struct rxe_cq *cq = to_rcq(ibcq); 807 808 rxe_cq_disable(cq); 809 810 rxe_drop_ref(cq); 811 return 0; 812 } 813 814 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata) 815 { 816 int err; 817 struct rxe_cq *cq = to_rcq(ibcq); 818 struct rxe_dev *rxe = to_rdev(ibcq->device); 819 struct rxe_resize_cq_resp __user *uresp = NULL; 820 821 if (udata) { 822 if (udata->outlen < sizeof(*uresp)) 823 return -EINVAL; 824 uresp = udata->outbuf; 825 } 826 827 err = rxe_cq_chk_attr(rxe, cq, cqe, 0); 828 if (err) 829 goto err1; 830 831 err = rxe_cq_resize_queue(cq, cqe, uresp, udata); 832 if (err) 833 goto err1; 834 835 return 0; 836 837 err1: 838 return err; 839 } 840 841 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) 842 { 843 int i; 844 struct rxe_cq *cq = to_rcq(ibcq); 845 struct rxe_cqe *cqe; 846 847 spin_lock_bh(&cq->cq_lock); 848 for (i = 0; i < num_entries; i++) { 849 cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER); 850 if (!cqe) 851 break; 852 853 memcpy(wc++, &cqe->ibwc, sizeof(*wc)); 854 queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER); 855 } 856 spin_unlock_bh(&cq->cq_lock); 857 858 return i; 859 } 860 861 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt) 862 { 863 struct rxe_cq *cq = to_rcq(ibcq); 864 int count; 865 866 count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER); 867 868 return (count > wc_cnt) ? wc_cnt : count; 869 } 870 871 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) 872 { 873 struct rxe_cq *cq = to_rcq(ibcq); 874 int ret = 0; 875 int empty; 876 877 spin_lock_bh(&cq->cq_lock); 878 if (cq->notify != IB_CQ_NEXT_COMP) 879 cq->notify = flags & IB_CQ_SOLICITED_MASK; 880 881 empty = queue_empty(cq->queue, QUEUE_TYPE_FROM_DRIVER); 882 883 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty) 884 ret = 1; 885 886 spin_unlock_bh(&cq->cq_lock); 887 888 return ret; 889 } 890 891 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access) 892 { 893 struct rxe_dev *rxe = to_rdev(ibpd->device); 894 struct rxe_pd *pd = to_rpd(ibpd); 895 struct rxe_mr *mr; 896 897 mr = rxe_alloc(&rxe->mr_pool); 898 if (!mr) 899 return ERR_PTR(-ENOMEM); 900 901 rxe_add_index(mr); 902 rxe_add_ref(pd); 903 rxe_mr_init_dma(pd, access, mr); 904 905 return &mr->ibmr; 906 } 907 908 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, 909 u64 start, 910 u64 length, 911 u64 iova, 912 int access, struct ib_udata *udata) 913 { 914 int err; 915 struct rxe_dev *rxe = to_rdev(ibpd->device); 916 struct rxe_pd *pd = to_rpd(ibpd); 917 struct rxe_mr *mr; 918 919 mr = rxe_alloc(&rxe->mr_pool); 920 if (!mr) { 921 err = -ENOMEM; 922 goto err2; 923 } 924 925 rxe_add_index(mr); 926 927 rxe_add_ref(pd); 928 929 err = rxe_mr_init_user(pd, start, length, iova, access, mr); 930 if (err) 931 goto err3; 932 933 return &mr->ibmr; 934 935 err3: 936 rxe_drop_ref(pd); 937 rxe_drop_index(mr); 938 rxe_drop_ref(mr); 939 err2: 940 return ERR_PTR(err); 941 } 942 943 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, 944 u32 max_num_sg) 945 { 946 struct rxe_dev *rxe = to_rdev(ibpd->device); 947 struct rxe_pd *pd = to_rpd(ibpd); 948 struct rxe_mr *mr; 949 int err; 950 951 if (mr_type != IB_MR_TYPE_MEM_REG) 952 return ERR_PTR(-EINVAL); 953 954 mr = rxe_alloc(&rxe->mr_pool); 955 if (!mr) { 956 err = -ENOMEM; 957 goto err1; 958 } 959 960 rxe_add_index(mr); 961 962 rxe_add_ref(pd); 963 964 err = rxe_mr_init_fast(pd, max_num_sg, mr); 965 if (err) 966 goto err2; 967 968 return &mr->ibmr; 969 970 err2: 971 rxe_drop_ref(pd); 972 rxe_drop_index(mr); 973 rxe_drop_ref(mr); 974 err1: 975 return ERR_PTR(err); 976 } 977 978 /* build next_map_set from scatterlist 979 * The IB_WR_REG_MR WR will swap map_sets 980 */ 981 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, 982 int sg_nents, unsigned int *sg_offset) 983 { 984 struct rxe_mr *mr = to_rmr(ibmr); 985 struct rxe_map_set *set = mr->next_map_set; 986 int n; 987 988 set->nbuf = 0; 989 990 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_mr_set_page); 991 992 set->va = ibmr->iova; 993 set->iova = ibmr->iova; 994 set->length = ibmr->length; 995 set->page_shift = ilog2(ibmr->page_size); 996 set->page_mask = ibmr->page_size - 1; 997 set->offset = set->iova & set->page_mask; 998 999 return n; 1000 } 1001 1002 static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid) 1003 { 1004 int err; 1005 struct rxe_dev *rxe = to_rdev(ibqp->device); 1006 struct rxe_qp *qp = to_rqp(ibqp); 1007 struct rxe_mc_grp *grp; 1008 1009 /* takes a ref on grp if successful */ 1010 err = rxe_mcast_get_grp(rxe, mgid, &grp); 1011 if (err) 1012 return err; 1013 1014 err = rxe_mcast_add_grp_elem(rxe, qp, grp); 1015 1016 rxe_drop_ref(grp); 1017 return err; 1018 } 1019 1020 static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid) 1021 { 1022 struct rxe_dev *rxe = to_rdev(ibqp->device); 1023 struct rxe_qp *qp = to_rqp(ibqp); 1024 1025 return rxe_mcast_drop_grp_elem(rxe, qp, mgid); 1026 } 1027 1028 static ssize_t parent_show(struct device *device, 1029 struct device_attribute *attr, char *buf) 1030 { 1031 struct rxe_dev *rxe = 1032 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev); 1033 1034 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1)); 1035 } 1036 1037 static DEVICE_ATTR_RO(parent); 1038 1039 static struct attribute *rxe_dev_attributes[] = { 1040 &dev_attr_parent.attr, 1041 NULL 1042 }; 1043 1044 static const struct attribute_group rxe_attr_group = { 1045 .attrs = rxe_dev_attributes, 1046 }; 1047 1048 static int rxe_enable_driver(struct ib_device *ib_dev) 1049 { 1050 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); 1051 1052 rxe_set_port_state(rxe); 1053 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev)); 1054 return 0; 1055 } 1056 1057 static const struct ib_device_ops rxe_dev_ops = { 1058 .owner = THIS_MODULE, 1059 .driver_id = RDMA_DRIVER_RXE, 1060 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION, 1061 1062 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats, 1063 .alloc_mr = rxe_alloc_mr, 1064 .alloc_mw = rxe_alloc_mw, 1065 .alloc_pd = rxe_alloc_pd, 1066 .alloc_ucontext = rxe_alloc_ucontext, 1067 .attach_mcast = rxe_attach_mcast, 1068 .create_ah = rxe_create_ah, 1069 .create_cq = rxe_create_cq, 1070 .create_qp = rxe_create_qp, 1071 .create_srq = rxe_create_srq, 1072 .create_user_ah = rxe_create_ah, 1073 .dealloc_driver = rxe_dealloc, 1074 .dealloc_mw = rxe_dealloc_mw, 1075 .dealloc_pd = rxe_dealloc_pd, 1076 .dealloc_ucontext = rxe_dealloc_ucontext, 1077 .dereg_mr = rxe_dereg_mr, 1078 .destroy_ah = rxe_destroy_ah, 1079 .destroy_cq = rxe_destroy_cq, 1080 .destroy_qp = rxe_destroy_qp, 1081 .destroy_srq = rxe_destroy_srq, 1082 .detach_mcast = rxe_detach_mcast, 1083 .device_group = &rxe_attr_group, 1084 .enable_driver = rxe_enable_driver, 1085 .get_dma_mr = rxe_get_dma_mr, 1086 .get_hw_stats = rxe_ib_get_hw_stats, 1087 .get_link_layer = rxe_get_link_layer, 1088 .get_port_immutable = rxe_port_immutable, 1089 .map_mr_sg = rxe_map_mr_sg, 1090 .mmap = rxe_mmap, 1091 .modify_ah = rxe_modify_ah, 1092 .modify_device = rxe_modify_device, 1093 .modify_port = rxe_modify_port, 1094 .modify_qp = rxe_modify_qp, 1095 .modify_srq = rxe_modify_srq, 1096 .peek_cq = rxe_peek_cq, 1097 .poll_cq = rxe_poll_cq, 1098 .post_recv = rxe_post_recv, 1099 .post_send = rxe_post_send, 1100 .post_srq_recv = rxe_post_srq_recv, 1101 .query_ah = rxe_query_ah, 1102 .query_device = rxe_query_device, 1103 .query_pkey = rxe_query_pkey, 1104 .query_port = rxe_query_port, 1105 .query_qp = rxe_query_qp, 1106 .query_srq = rxe_query_srq, 1107 .reg_user_mr = rxe_reg_user_mr, 1108 .req_notify_cq = rxe_req_notify_cq, 1109 .resize_cq = rxe_resize_cq, 1110 1111 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah), 1112 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq), 1113 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd), 1114 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp), 1115 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq), 1116 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc), 1117 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw), 1118 }; 1119 1120 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name) 1121 { 1122 int err; 1123 struct ib_device *dev = &rxe->ib_dev; 1124 1125 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc)); 1126 1127 dev->node_type = RDMA_NODE_IB_CA; 1128 dev->phys_port_cnt = 1; 1129 dev->num_comp_vectors = num_possible_cpus(); 1130 dev->local_dma_lkey = 0; 1131 addrconf_addr_eui48((unsigned char *)&dev->node_guid, 1132 rxe->ndev->dev_addr); 1133 1134 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) | 1135 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ); 1136 1137 ib_set_device_ops(dev, &rxe_dev_ops); 1138 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1); 1139 if (err) 1140 return err; 1141 1142 err = rxe_icrc_init(rxe); 1143 if (err) 1144 return err; 1145 1146 err = ib_register_device(dev, ibdev_name, NULL); 1147 if (err) 1148 pr_warn("%s failed with error %d\n", __func__, err); 1149 1150 /* 1151 * Note that rxe may be invalid at this point if another thread 1152 * unregistered it. 1153 */ 1154 return err; 1155 } 1156