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->pelem.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 unsigned long flags; 387 struct rxe_srq *srq = to_rsrq(ibsrq); 388 389 spin_lock_irqsave(&srq->rq.producer_lock, flags); 390 391 while (wr) { 392 err = post_one_recv(&srq->rq, wr); 393 if (unlikely(err)) 394 break; 395 wr = wr->next; 396 } 397 398 spin_unlock_irqrestore(&srq->rq.producer_lock, flags); 399 400 if (err) 401 *bad_wr = wr; 402 403 return err; 404 } 405 406 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, 407 struct ib_udata *udata) 408 { 409 int err; 410 struct rxe_dev *rxe = to_rdev(ibqp->device); 411 struct rxe_pd *pd = to_rpd(ibqp->pd); 412 struct rxe_qp *qp = to_rqp(ibqp); 413 struct rxe_create_qp_resp __user *uresp = NULL; 414 415 if (udata) { 416 if (udata->outlen < sizeof(*uresp)) 417 return -EINVAL; 418 uresp = udata->outbuf; 419 } 420 421 if (init->create_flags) 422 return -EOPNOTSUPP; 423 424 err = rxe_qp_chk_init(rxe, init); 425 if (err) 426 return err; 427 428 if (udata) { 429 if (udata->inlen) 430 return -EINVAL; 431 432 qp->is_user = true; 433 } else { 434 qp->is_user = false; 435 } 436 437 err = rxe_add_to_pool(&rxe->qp_pool, qp); 438 if (err) 439 return err; 440 441 rxe_add_index(qp); 442 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); 443 if (err) 444 goto qp_init; 445 446 return 0; 447 448 qp_init: 449 rxe_drop_index(qp); 450 rxe_drop_ref(qp); 451 return err; 452 } 453 454 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 455 int mask, struct ib_udata *udata) 456 { 457 int err; 458 struct rxe_dev *rxe = to_rdev(ibqp->device); 459 struct rxe_qp *qp = to_rqp(ibqp); 460 461 if (mask & ~IB_QP_ATTR_STANDARD_BITS) 462 return -EOPNOTSUPP; 463 464 err = rxe_qp_chk_attr(rxe, qp, attr, mask); 465 if (err) 466 goto err1; 467 468 err = rxe_qp_from_attr(qp, attr, mask, udata); 469 if (err) 470 goto err1; 471 472 return 0; 473 474 err1: 475 return err; 476 } 477 478 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 479 int mask, struct ib_qp_init_attr *init) 480 { 481 struct rxe_qp *qp = to_rqp(ibqp); 482 483 rxe_qp_to_init(qp, init); 484 rxe_qp_to_attr(qp, attr, mask); 485 486 return 0; 487 } 488 489 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 490 { 491 struct rxe_qp *qp = to_rqp(ibqp); 492 493 rxe_qp_destroy(qp); 494 rxe_drop_index(qp); 495 rxe_drop_ref(qp); 496 return 0; 497 } 498 499 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 500 unsigned int mask, unsigned int length) 501 { 502 int num_sge = ibwr->num_sge; 503 struct rxe_sq *sq = &qp->sq; 504 505 if (unlikely(num_sge > sq->max_sge)) 506 goto err1; 507 508 if (unlikely(mask & WR_ATOMIC_MASK)) { 509 if (length < 8) 510 goto err1; 511 512 if (atomic_wr(ibwr)->remote_addr & 0x7) 513 goto err1; 514 } 515 516 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) && 517 (length > sq->max_inline))) 518 goto err1; 519 520 return 0; 521 522 err1: 523 return -EINVAL; 524 } 525 526 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr, 527 const struct ib_send_wr *ibwr) 528 { 529 wr->wr_id = ibwr->wr_id; 530 wr->num_sge = ibwr->num_sge; 531 wr->opcode = ibwr->opcode; 532 wr->send_flags = ibwr->send_flags; 533 534 if (qp_type(qp) == IB_QPT_UD || 535 qp_type(qp) == IB_QPT_SMI || 536 qp_type(qp) == IB_QPT_GSI) { 537 struct ib_ah *ibah = ud_wr(ibwr)->ah; 538 539 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn; 540 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey; 541 wr->wr.ud.ah_num = to_rah(ibah)->ah_num; 542 if (qp_type(qp) == IB_QPT_GSI) 543 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index; 544 if (wr->opcode == IB_WR_SEND_WITH_IMM) 545 wr->ex.imm_data = ibwr->ex.imm_data; 546 } else { 547 switch (wr->opcode) { 548 case IB_WR_RDMA_WRITE_WITH_IMM: 549 wr->ex.imm_data = ibwr->ex.imm_data; 550 fallthrough; 551 case IB_WR_RDMA_READ: 552 case IB_WR_RDMA_WRITE: 553 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr; 554 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey; 555 break; 556 case IB_WR_SEND_WITH_IMM: 557 wr->ex.imm_data = ibwr->ex.imm_data; 558 break; 559 case IB_WR_SEND_WITH_INV: 560 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 561 break; 562 case IB_WR_ATOMIC_CMP_AND_SWP: 563 case IB_WR_ATOMIC_FETCH_AND_ADD: 564 wr->wr.atomic.remote_addr = 565 atomic_wr(ibwr)->remote_addr; 566 wr->wr.atomic.compare_add = 567 atomic_wr(ibwr)->compare_add; 568 wr->wr.atomic.swap = atomic_wr(ibwr)->swap; 569 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey; 570 break; 571 case IB_WR_LOCAL_INV: 572 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 573 break; 574 case IB_WR_REG_MR: 575 wr->wr.reg.mr = reg_wr(ibwr)->mr; 576 wr->wr.reg.key = reg_wr(ibwr)->key; 577 wr->wr.reg.access = reg_wr(ibwr)->access; 578 break; 579 default: 580 break; 581 } 582 } 583 } 584 585 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe, 586 const struct ib_send_wr *ibwr) 587 { 588 struct ib_sge *sge = ibwr->sg_list; 589 u8 *p = wqe->dma.inline_data; 590 int i; 591 592 for (i = 0; i < ibwr->num_sge; i++, sge++) { 593 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length); 594 p += sge->length; 595 } 596 } 597 598 static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 599 unsigned int mask, unsigned int length, 600 struct rxe_send_wqe *wqe) 601 { 602 int num_sge = ibwr->num_sge; 603 604 init_send_wr(qp, &wqe->wr, ibwr); 605 606 /* local operation */ 607 if (unlikely(mask & WR_LOCAL_OP_MASK)) { 608 wqe->mask = mask; 609 wqe->state = wqe_state_posted; 610 return; 611 } 612 613 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) 614 copy_inline_data_to_wqe(wqe, ibwr); 615 else 616 memcpy(wqe->dma.sge, ibwr->sg_list, 617 num_sge * sizeof(struct ib_sge)); 618 619 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr : 620 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0; 621 wqe->mask = mask; 622 wqe->dma.length = length; 623 wqe->dma.resid = length; 624 wqe->dma.num_sge = num_sge; 625 wqe->dma.cur_sge = 0; 626 wqe->dma.sge_offset = 0; 627 wqe->state = wqe_state_posted; 628 wqe->ssn = atomic_add_return(1, &qp->ssn); 629 } 630 631 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 632 unsigned int mask, u32 length) 633 { 634 int err; 635 struct rxe_sq *sq = &qp->sq; 636 struct rxe_send_wqe *send_wqe; 637 unsigned long flags; 638 int full; 639 640 err = validate_send_wr(qp, ibwr, mask, length); 641 if (err) 642 return err; 643 644 spin_lock_irqsave(&qp->sq.sq_lock, flags); 645 646 full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER); 647 648 if (unlikely(full)) { 649 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 650 return -ENOMEM; 651 } 652 653 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER); 654 init_send_wqe(qp, ibwr, mask, length, send_wqe); 655 656 queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER); 657 658 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 659 660 return 0; 661 } 662 663 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr, 664 const struct ib_send_wr **bad_wr) 665 { 666 int err = 0; 667 unsigned int mask; 668 unsigned int length = 0; 669 int i; 670 struct ib_send_wr *next; 671 672 while (wr) { 673 mask = wr_opcode_mask(wr->opcode, qp); 674 if (unlikely(!mask)) { 675 err = -EINVAL; 676 *bad_wr = wr; 677 break; 678 } 679 680 if (unlikely((wr->send_flags & IB_SEND_INLINE) && 681 !(mask & WR_INLINE_MASK))) { 682 err = -EINVAL; 683 *bad_wr = wr; 684 break; 685 } 686 687 next = wr->next; 688 689 length = 0; 690 for (i = 0; i < wr->num_sge; i++) 691 length += wr->sg_list[i].length; 692 693 err = post_one_send(qp, wr, mask, length); 694 695 if (err) { 696 *bad_wr = wr; 697 break; 698 } 699 wr = next; 700 } 701 702 rxe_run_task(&qp->req.task, 1); 703 if (unlikely(qp->req.state == QP_STATE_ERROR)) 704 rxe_run_task(&qp->comp.task, 1); 705 706 return err; 707 } 708 709 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 710 const struct ib_send_wr **bad_wr) 711 { 712 struct rxe_qp *qp = to_rqp(ibqp); 713 714 if (unlikely(!qp->valid)) { 715 *bad_wr = wr; 716 return -EINVAL; 717 } 718 719 if (unlikely(qp->req.state < QP_STATE_READY)) { 720 *bad_wr = wr; 721 return -EINVAL; 722 } 723 724 if (qp->is_user) { 725 /* Utilize process context to do protocol processing */ 726 rxe_run_task(&qp->req.task, 0); 727 return 0; 728 } else 729 return rxe_post_send_kernel(qp, wr, bad_wr); 730 } 731 732 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 733 const struct ib_recv_wr **bad_wr) 734 { 735 int err = 0; 736 struct rxe_qp *qp = to_rqp(ibqp); 737 struct rxe_rq *rq = &qp->rq; 738 unsigned long flags; 739 740 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) { 741 *bad_wr = wr; 742 err = -EINVAL; 743 goto err1; 744 } 745 746 if (unlikely(qp->srq)) { 747 *bad_wr = wr; 748 err = -EINVAL; 749 goto err1; 750 } 751 752 spin_lock_irqsave(&rq->producer_lock, flags); 753 754 while (wr) { 755 err = post_one_recv(rq, wr); 756 if (unlikely(err)) { 757 *bad_wr = wr; 758 break; 759 } 760 wr = wr->next; 761 } 762 763 spin_unlock_irqrestore(&rq->producer_lock, flags); 764 765 if (qp->resp.state == QP_STATE_ERROR) 766 rxe_run_task(&qp->resp.task, 1); 767 768 err1: 769 return err; 770 } 771 772 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 773 struct ib_udata *udata) 774 { 775 int err; 776 struct ib_device *dev = ibcq->device; 777 struct rxe_dev *rxe = to_rdev(dev); 778 struct rxe_cq *cq = to_rcq(ibcq); 779 struct rxe_create_cq_resp __user *uresp = NULL; 780 781 if (udata) { 782 if (udata->outlen < sizeof(*uresp)) 783 return -EINVAL; 784 uresp = udata->outbuf; 785 } 786 787 if (attr->flags) 788 return -EOPNOTSUPP; 789 790 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector); 791 if (err) 792 return err; 793 794 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata, 795 uresp); 796 if (err) 797 return err; 798 799 return rxe_add_to_pool(&rxe->cq_pool, cq); 800 } 801 802 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 803 { 804 struct rxe_cq *cq = to_rcq(ibcq); 805 806 rxe_cq_disable(cq); 807 808 rxe_drop_ref(cq); 809 return 0; 810 } 811 812 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata) 813 { 814 int err; 815 struct rxe_cq *cq = to_rcq(ibcq); 816 struct rxe_dev *rxe = to_rdev(ibcq->device); 817 struct rxe_resize_cq_resp __user *uresp = NULL; 818 819 if (udata) { 820 if (udata->outlen < sizeof(*uresp)) 821 return -EINVAL; 822 uresp = udata->outbuf; 823 } 824 825 err = rxe_cq_chk_attr(rxe, cq, cqe, 0); 826 if (err) 827 goto err1; 828 829 err = rxe_cq_resize_queue(cq, cqe, uresp, udata); 830 if (err) 831 goto err1; 832 833 return 0; 834 835 err1: 836 return err; 837 } 838 839 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) 840 { 841 int i; 842 struct rxe_cq *cq = to_rcq(ibcq); 843 struct rxe_cqe *cqe; 844 unsigned long flags; 845 846 spin_lock_irqsave(&cq->cq_lock, flags); 847 for (i = 0; i < num_entries; i++) { 848 cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER); 849 if (!cqe) 850 break; 851 852 memcpy(wc++, &cqe->ibwc, sizeof(*wc)); 853 queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER); 854 } 855 spin_unlock_irqrestore(&cq->cq_lock, flags); 856 857 return i; 858 } 859 860 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt) 861 { 862 struct rxe_cq *cq = to_rcq(ibcq); 863 int count; 864 865 count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER); 866 867 return (count > wc_cnt) ? wc_cnt : count; 868 } 869 870 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) 871 { 872 struct rxe_cq *cq = to_rcq(ibcq); 873 unsigned long irq_flags; 874 int ret = 0; 875 int empty; 876 877 spin_lock_irqsave(&cq->cq_lock, irq_flags); 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_irqrestore(&cq->cq_lock, irq_flags); 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