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 11 #include "rxe.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_cleanup(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_cleanup(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_ah(&rxe->ah_pool, ah, 180 init_attr->flags & RDMA_CREATE_AH_SLEEPABLE); 181 if (err) 182 return err; 183 184 /* create index > 0 */ 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_cleanup(ah); 193 return -EFAULT; 194 } 195 } else if (ah->is_user) { 196 /* only if old user provider */ 197 ah->ah_num = 0; 198 } 199 200 rxe_init_av(init_attr->ah_attr, &ah->av); 201 rxe_finalize(ah); 202 203 return 0; 204 } 205 206 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr) 207 { 208 int err; 209 struct rxe_dev *rxe = to_rdev(ibah->device); 210 struct rxe_ah *ah = to_rah(ibah); 211 212 err = rxe_av_chk_attr(rxe, attr); 213 if (err) 214 return err; 215 216 rxe_init_av(attr, &ah->av); 217 return 0; 218 } 219 220 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr) 221 { 222 struct rxe_ah *ah = to_rah(ibah); 223 224 memset(attr, 0, sizeof(*attr)); 225 attr->type = ibah->type; 226 rxe_av_to_attr(&ah->av, attr); 227 return 0; 228 } 229 230 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags) 231 { 232 struct rxe_ah *ah = to_rah(ibah); 233 234 rxe_cleanup_ah(ah, flags & RDMA_DESTROY_AH_SLEEPABLE); 235 236 return 0; 237 } 238 239 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr) 240 { 241 int err; 242 int i; 243 u32 length; 244 struct rxe_recv_wqe *recv_wqe; 245 int num_sge = ibwr->num_sge; 246 int full; 247 248 full = queue_full(rq->queue, QUEUE_TYPE_TO_DRIVER); 249 if (unlikely(full)) { 250 err = -ENOMEM; 251 goto err1; 252 } 253 254 if (unlikely(num_sge > rq->max_sge)) { 255 err = -EINVAL; 256 goto err1; 257 } 258 259 length = 0; 260 for (i = 0; i < num_sge; i++) 261 length += ibwr->sg_list[i].length; 262 263 recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_TO_DRIVER); 264 recv_wqe->wr_id = ibwr->wr_id; 265 recv_wqe->num_sge = num_sge; 266 267 memcpy(recv_wqe->dma.sge, ibwr->sg_list, 268 num_sge * sizeof(struct ib_sge)); 269 270 recv_wqe->dma.length = length; 271 recv_wqe->dma.resid = length; 272 recv_wqe->dma.num_sge = num_sge; 273 recv_wqe->dma.cur_sge = 0; 274 recv_wqe->dma.sge_offset = 0; 275 276 queue_advance_producer(rq->queue, QUEUE_TYPE_TO_DRIVER); 277 278 return 0; 279 280 err1: 281 return err; 282 } 283 284 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init, 285 struct ib_udata *udata) 286 { 287 int err; 288 struct rxe_dev *rxe = to_rdev(ibsrq->device); 289 struct rxe_pd *pd = to_rpd(ibsrq->pd); 290 struct rxe_srq *srq = to_rsrq(ibsrq); 291 struct rxe_create_srq_resp __user *uresp = NULL; 292 293 if (udata) { 294 if (udata->outlen < sizeof(*uresp)) 295 return -EINVAL; 296 uresp = udata->outbuf; 297 } 298 299 if (init->srq_type != IB_SRQT_BASIC) 300 return -EOPNOTSUPP; 301 302 err = rxe_srq_chk_init(rxe, init); 303 if (err) 304 return err; 305 306 err = rxe_add_to_pool(&rxe->srq_pool, srq); 307 if (err) 308 return err; 309 310 rxe_get(pd); 311 srq->pd = pd; 312 313 err = rxe_srq_from_init(rxe, srq, init, udata, uresp); 314 if (err) 315 goto err_cleanup; 316 317 return 0; 318 319 err_cleanup: 320 rxe_cleanup(srq); 321 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 return err; 346 347 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata); 348 if (err) 349 return err; 350 return 0; 351 } 352 353 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr) 354 { 355 struct rxe_srq *srq = to_rsrq(ibsrq); 356 357 if (srq->error) 358 return -EINVAL; 359 360 attr->max_wr = srq->rq.queue->buf->index_mask; 361 attr->max_sge = srq->rq.max_sge; 362 attr->srq_limit = srq->limit; 363 return 0; 364 } 365 366 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) 367 { 368 struct rxe_srq *srq = to_rsrq(ibsrq); 369 370 rxe_cleanup(srq); 371 return 0; 372 } 373 374 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, 375 const struct ib_recv_wr **bad_wr) 376 { 377 int err = 0; 378 struct rxe_srq *srq = to_rsrq(ibsrq); 379 unsigned long flags; 380 381 spin_lock_irqsave(&srq->rq.producer_lock, flags); 382 383 while (wr) { 384 err = post_one_recv(&srq->rq, wr); 385 if (unlikely(err)) 386 break; 387 wr = wr->next; 388 } 389 390 spin_unlock_irqrestore(&srq->rq.producer_lock, flags); 391 392 if (err) 393 *bad_wr = wr; 394 395 return err; 396 } 397 398 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, 399 struct ib_udata *udata) 400 { 401 int err; 402 struct rxe_dev *rxe = to_rdev(ibqp->device); 403 struct rxe_pd *pd = to_rpd(ibqp->pd); 404 struct rxe_qp *qp = to_rqp(ibqp); 405 struct rxe_create_qp_resp __user *uresp = NULL; 406 407 if (udata) { 408 if (udata->outlen < sizeof(*uresp)) 409 return -EINVAL; 410 uresp = udata->outbuf; 411 } 412 413 if (init->create_flags) 414 return -EOPNOTSUPP; 415 416 err = rxe_qp_chk_init(rxe, init); 417 if (err) 418 return err; 419 420 if (udata) { 421 if (udata->inlen) 422 return -EINVAL; 423 424 qp->is_user = true; 425 } else { 426 qp->is_user = false; 427 } 428 429 err = rxe_add_to_pool(&rxe->qp_pool, qp); 430 if (err) 431 return err; 432 433 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); 434 if (err) 435 goto qp_init; 436 437 rxe_finalize(qp); 438 return 0; 439 440 qp_init: 441 rxe_cleanup(qp); 442 return err; 443 } 444 445 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 446 int mask, struct ib_udata *udata) 447 { 448 int err; 449 struct rxe_dev *rxe = to_rdev(ibqp->device); 450 struct rxe_qp *qp = to_rqp(ibqp); 451 452 if (mask & ~IB_QP_ATTR_STANDARD_BITS) 453 return -EOPNOTSUPP; 454 455 err = rxe_qp_chk_attr(rxe, qp, attr, mask); 456 if (err) 457 goto err1; 458 459 err = rxe_qp_from_attr(qp, attr, mask, udata); 460 if (err) 461 goto err1; 462 463 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH)) 464 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label, 465 qp->ibqp.qp_num, 466 qp->attr.dest_qp_num); 467 468 return 0; 469 470 err1: 471 return err; 472 } 473 474 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 475 int mask, struct ib_qp_init_attr *init) 476 { 477 struct rxe_qp *qp = to_rqp(ibqp); 478 479 rxe_qp_to_init(qp, init); 480 rxe_qp_to_attr(qp, attr, mask); 481 482 return 0; 483 } 484 485 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 486 { 487 struct rxe_qp *qp = to_rqp(ibqp); 488 int ret; 489 490 ret = rxe_qp_chk_destroy(qp); 491 if (ret) 492 return ret; 493 494 rxe_cleanup(qp); 495 return 0; 496 } 497 498 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 499 unsigned int mask, unsigned int length) 500 { 501 int num_sge = ibwr->num_sge; 502 struct rxe_sq *sq = &qp->sq; 503 504 if (unlikely(num_sge > sq->max_sge)) 505 goto err1; 506 507 if (unlikely(mask & WR_ATOMIC_MASK)) { 508 if (length < 8) 509 goto err1; 510 511 if (atomic_wr(ibwr)->remote_addr & 0x7) 512 goto err1; 513 } 514 515 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) && 516 (length > sq->max_inline))) 517 goto err1; 518 519 return 0; 520 521 err1: 522 return -EINVAL; 523 } 524 525 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr, 526 const struct ib_send_wr *ibwr) 527 { 528 wr->wr_id = ibwr->wr_id; 529 wr->num_sge = ibwr->num_sge; 530 wr->opcode = ibwr->opcode; 531 wr->send_flags = ibwr->send_flags; 532 533 if (qp_type(qp) == IB_QPT_UD || 534 qp_type(qp) == IB_QPT_GSI) { 535 struct ib_ah *ibah = ud_wr(ibwr)->ah; 536 537 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn; 538 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey; 539 wr->wr.ud.ah_num = to_rah(ibah)->ah_num; 540 if (qp_type(qp) == IB_QPT_GSI) 541 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index; 542 if (wr->opcode == IB_WR_SEND_WITH_IMM) 543 wr->ex.imm_data = ibwr->ex.imm_data; 544 } else { 545 switch (wr->opcode) { 546 case IB_WR_RDMA_WRITE_WITH_IMM: 547 wr->ex.imm_data = ibwr->ex.imm_data; 548 fallthrough; 549 case IB_WR_RDMA_READ: 550 case IB_WR_RDMA_WRITE: 551 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr; 552 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey; 553 break; 554 case IB_WR_SEND_WITH_IMM: 555 wr->ex.imm_data = ibwr->ex.imm_data; 556 break; 557 case IB_WR_SEND_WITH_INV: 558 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 559 break; 560 case IB_WR_ATOMIC_CMP_AND_SWP: 561 case IB_WR_ATOMIC_FETCH_AND_ADD: 562 wr->wr.atomic.remote_addr = 563 atomic_wr(ibwr)->remote_addr; 564 wr->wr.atomic.compare_add = 565 atomic_wr(ibwr)->compare_add; 566 wr->wr.atomic.swap = atomic_wr(ibwr)->swap; 567 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey; 568 break; 569 case IB_WR_LOCAL_INV: 570 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 571 break; 572 case IB_WR_REG_MR: 573 wr->wr.reg.mr = reg_wr(ibwr)->mr; 574 wr->wr.reg.key = reg_wr(ibwr)->key; 575 wr->wr.reg.access = reg_wr(ibwr)->access; 576 break; 577 default: 578 break; 579 } 580 } 581 } 582 583 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe, 584 const struct ib_send_wr *ibwr) 585 { 586 struct ib_sge *sge = ibwr->sg_list; 587 u8 *p = wqe->dma.inline_data; 588 int i; 589 590 for (i = 0; i < ibwr->num_sge; i++, sge++) { 591 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length); 592 p += sge->length; 593 } 594 } 595 596 static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 597 unsigned int mask, unsigned int length, 598 struct rxe_send_wqe *wqe) 599 { 600 int num_sge = ibwr->num_sge; 601 602 init_send_wr(qp, &wqe->wr, ibwr); 603 604 /* local operation */ 605 if (unlikely(mask & WR_LOCAL_OP_MASK)) { 606 wqe->mask = mask; 607 wqe->state = wqe_state_posted; 608 return; 609 } 610 611 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) 612 copy_inline_data_to_wqe(wqe, ibwr); 613 else 614 memcpy(wqe->dma.sge, ibwr->sg_list, 615 num_sge * sizeof(struct ib_sge)); 616 617 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr : 618 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0; 619 wqe->mask = mask; 620 wqe->dma.length = length; 621 wqe->dma.resid = length; 622 wqe->dma.num_sge = num_sge; 623 wqe->dma.cur_sge = 0; 624 wqe->dma.sge_offset = 0; 625 wqe->state = wqe_state_posted; 626 wqe->ssn = atomic_add_return(1, &qp->ssn); 627 } 628 629 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 630 unsigned int mask, u32 length) 631 { 632 int err; 633 struct rxe_sq *sq = &qp->sq; 634 struct rxe_send_wqe *send_wqe; 635 unsigned long flags; 636 int full; 637 638 err = validate_send_wr(qp, ibwr, mask, length); 639 if (err) 640 return err; 641 642 spin_lock_irqsave(&qp->sq.sq_lock, flags); 643 644 full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER); 645 646 if (unlikely(full)) { 647 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 648 return -ENOMEM; 649 } 650 651 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER); 652 init_send_wqe(qp, ibwr, mask, length, send_wqe); 653 654 queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER); 655 656 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 657 658 return 0; 659 } 660 661 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr, 662 const struct ib_send_wr **bad_wr) 663 { 664 int err = 0; 665 unsigned int mask; 666 unsigned int length = 0; 667 int i; 668 struct ib_send_wr *next; 669 670 while (wr) { 671 mask = wr_opcode_mask(wr->opcode, qp); 672 if (unlikely(!mask)) { 673 err = -EINVAL; 674 *bad_wr = wr; 675 break; 676 } 677 678 if (unlikely((wr->send_flags & IB_SEND_INLINE) && 679 !(mask & WR_INLINE_MASK))) { 680 err = -EINVAL; 681 *bad_wr = wr; 682 break; 683 } 684 685 next = wr->next; 686 687 length = 0; 688 for (i = 0; i < wr->num_sge; i++) 689 length += wr->sg_list[i].length; 690 691 err = post_one_send(qp, wr, mask, length); 692 693 if (err) { 694 *bad_wr = wr; 695 break; 696 } 697 wr = next; 698 } 699 700 rxe_run_task(&qp->req.task, 1); 701 if (unlikely(qp->req.state == QP_STATE_ERROR)) 702 rxe_run_task(&qp->comp.task, 1); 703 704 return err; 705 } 706 707 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 708 const struct ib_send_wr **bad_wr) 709 { 710 struct rxe_qp *qp = to_rqp(ibqp); 711 712 if (unlikely(!qp->valid)) { 713 *bad_wr = wr; 714 return -EINVAL; 715 } 716 717 if (unlikely(qp->req.state < QP_STATE_READY)) { 718 *bad_wr = wr; 719 return -EINVAL; 720 } 721 722 if (qp->is_user) { 723 /* Utilize process context to do protocol processing */ 724 rxe_run_task(&qp->req.task, 0); 725 return 0; 726 } else 727 return rxe_post_send_kernel(qp, wr, bad_wr); 728 } 729 730 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 731 const struct ib_recv_wr **bad_wr) 732 { 733 int err = 0; 734 struct rxe_qp *qp = to_rqp(ibqp); 735 struct rxe_rq *rq = &qp->rq; 736 unsigned long flags; 737 738 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) { 739 *bad_wr = wr; 740 err = -EINVAL; 741 goto err1; 742 } 743 744 if (unlikely(qp->srq)) { 745 *bad_wr = wr; 746 err = -EINVAL; 747 goto err1; 748 } 749 750 spin_lock_irqsave(&rq->producer_lock, flags); 751 752 while (wr) { 753 err = post_one_recv(rq, wr); 754 if (unlikely(err)) { 755 *bad_wr = wr; 756 break; 757 } 758 wr = wr->next; 759 } 760 761 spin_unlock_irqrestore(&rq->producer_lock, flags); 762 763 if (qp->resp.state == QP_STATE_ERROR) 764 rxe_run_task(&qp->resp.task, 1); 765 766 err1: 767 return err; 768 } 769 770 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 771 struct ib_udata *udata) 772 { 773 int err; 774 struct ib_device *dev = ibcq->device; 775 struct rxe_dev *rxe = to_rdev(dev); 776 struct rxe_cq *cq = to_rcq(ibcq); 777 struct rxe_create_cq_resp __user *uresp = NULL; 778 779 if (udata) { 780 if (udata->outlen < sizeof(*uresp)) 781 return -EINVAL; 782 uresp = udata->outbuf; 783 } 784 785 if (attr->flags) 786 return -EOPNOTSUPP; 787 788 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector); 789 if (err) 790 return err; 791 792 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata, 793 uresp); 794 if (err) 795 return err; 796 797 return rxe_add_to_pool(&rxe->cq_pool, cq); 798 } 799 800 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 801 { 802 struct rxe_cq *cq = to_rcq(ibcq); 803 804 /* See IBA C11-17: The CI shall return an error if this Verb is 805 * invoked while a Work Queue is still associated with the CQ. 806 */ 807 if (atomic_read(&cq->num_wq)) 808 return -EINVAL; 809 810 rxe_cq_disable(cq); 811 812 rxe_cleanup(cq); 813 return 0; 814 } 815 816 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata) 817 { 818 int err; 819 struct rxe_cq *cq = to_rcq(ibcq); 820 struct rxe_dev *rxe = to_rdev(ibcq->device); 821 struct rxe_resize_cq_resp __user *uresp = NULL; 822 823 if (udata) { 824 if (udata->outlen < sizeof(*uresp)) 825 return -EINVAL; 826 uresp = udata->outbuf; 827 } 828 829 err = rxe_cq_chk_attr(rxe, cq, cqe, 0); 830 if (err) 831 goto err1; 832 833 err = rxe_cq_resize_queue(cq, cqe, uresp, udata); 834 if (err) 835 goto err1; 836 837 return 0; 838 839 err1: 840 return err; 841 } 842 843 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) 844 { 845 int i; 846 struct rxe_cq *cq = to_rcq(ibcq); 847 struct rxe_cqe *cqe; 848 unsigned long flags; 849 850 spin_lock_irqsave(&cq->cq_lock, flags); 851 for (i = 0; i < num_entries; i++) { 852 cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER); 853 if (!cqe) 854 break; 855 856 memcpy(wc++, &cqe->ibwc, sizeof(*wc)); 857 queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER); 858 } 859 spin_unlock_irqrestore(&cq->cq_lock, flags); 860 861 return i; 862 } 863 864 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt) 865 { 866 struct rxe_cq *cq = to_rcq(ibcq); 867 int count; 868 869 count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER); 870 871 return (count > wc_cnt) ? wc_cnt : count; 872 } 873 874 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) 875 { 876 struct rxe_cq *cq = to_rcq(ibcq); 877 int ret = 0; 878 int empty; 879 unsigned long irq_flags; 880 881 spin_lock_irqsave(&cq->cq_lock, irq_flags); 882 if (cq->notify != IB_CQ_NEXT_COMP) 883 cq->notify = flags & IB_CQ_SOLICITED_MASK; 884 885 empty = queue_empty(cq->queue, QUEUE_TYPE_FROM_DRIVER); 886 887 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty) 888 ret = 1; 889 890 spin_unlock_irqrestore(&cq->cq_lock, irq_flags); 891 892 return ret; 893 } 894 895 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access) 896 { 897 struct rxe_dev *rxe = to_rdev(ibpd->device); 898 struct rxe_pd *pd = to_rpd(ibpd); 899 struct rxe_mr *mr; 900 901 mr = rxe_alloc(&rxe->mr_pool); 902 if (!mr) 903 return ERR_PTR(-ENOMEM); 904 905 rxe_get(pd); 906 rxe_mr_init_dma(pd, access, mr); 907 rxe_finalize(mr); 908 909 return &mr->ibmr; 910 } 911 912 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, 913 u64 start, 914 u64 length, 915 u64 iova, 916 int access, struct ib_udata *udata) 917 { 918 int err; 919 struct rxe_dev *rxe = to_rdev(ibpd->device); 920 struct rxe_pd *pd = to_rpd(ibpd); 921 struct rxe_mr *mr; 922 923 mr = rxe_alloc(&rxe->mr_pool); 924 if (!mr) { 925 err = -ENOMEM; 926 goto err2; 927 } 928 929 930 rxe_get(pd); 931 932 err = rxe_mr_init_user(pd, start, length, iova, access, mr); 933 if (err) 934 goto err3; 935 936 rxe_finalize(mr); 937 938 return &mr->ibmr; 939 940 err3: 941 rxe_put(pd); 942 rxe_cleanup(mr); 943 err2: 944 return ERR_PTR(err); 945 } 946 947 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, 948 u32 max_num_sg) 949 { 950 struct rxe_dev *rxe = to_rdev(ibpd->device); 951 struct rxe_pd *pd = to_rpd(ibpd); 952 struct rxe_mr *mr; 953 int err; 954 955 if (mr_type != IB_MR_TYPE_MEM_REG) 956 return ERR_PTR(-EINVAL); 957 958 mr = rxe_alloc(&rxe->mr_pool); 959 if (!mr) { 960 err = -ENOMEM; 961 goto err1; 962 } 963 964 rxe_get(pd); 965 966 err = rxe_mr_init_fast(pd, max_num_sg, mr); 967 if (err) 968 goto err2; 969 970 rxe_finalize(mr); 971 972 return &mr->ibmr; 973 974 err2: 975 rxe_put(pd); 976 rxe_cleanup(mr); 977 err1: 978 return ERR_PTR(err); 979 } 980 981 static int rxe_set_page(struct ib_mr *ibmr, u64 addr) 982 { 983 struct rxe_mr *mr = to_rmr(ibmr); 984 struct rxe_map *map; 985 struct rxe_phys_buf *buf; 986 987 if (unlikely(mr->nbuf == mr->num_buf)) 988 return -ENOMEM; 989 990 map = mr->map[mr->nbuf / RXE_BUF_PER_MAP]; 991 buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP]; 992 993 buf->addr = addr; 994 buf->size = ibmr->page_size; 995 mr->nbuf++; 996 997 return 0; 998 } 999 1000 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, 1001 int sg_nents, unsigned int *sg_offset) 1002 { 1003 struct rxe_mr *mr = to_rmr(ibmr); 1004 int n; 1005 1006 mr->nbuf = 0; 1007 1008 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page); 1009 1010 mr->va = ibmr->iova; 1011 mr->iova = ibmr->iova; 1012 mr->length = ibmr->length; 1013 mr->page_shift = ilog2(ibmr->page_size); 1014 mr->page_mask = ibmr->page_size - 1; 1015 mr->offset = mr->iova & mr->page_mask; 1016 1017 return n; 1018 } 1019 1020 static ssize_t parent_show(struct device *device, 1021 struct device_attribute *attr, char *buf) 1022 { 1023 struct rxe_dev *rxe = 1024 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev); 1025 1026 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1)); 1027 } 1028 1029 static DEVICE_ATTR_RO(parent); 1030 1031 static struct attribute *rxe_dev_attributes[] = { 1032 &dev_attr_parent.attr, 1033 NULL 1034 }; 1035 1036 static const struct attribute_group rxe_attr_group = { 1037 .attrs = rxe_dev_attributes, 1038 }; 1039 1040 static int rxe_enable_driver(struct ib_device *ib_dev) 1041 { 1042 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); 1043 1044 rxe_set_port_state(rxe); 1045 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev)); 1046 return 0; 1047 } 1048 1049 static const struct ib_device_ops rxe_dev_ops = { 1050 .owner = THIS_MODULE, 1051 .driver_id = RDMA_DRIVER_RXE, 1052 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION, 1053 1054 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats, 1055 .alloc_mr = rxe_alloc_mr, 1056 .alloc_mw = rxe_alloc_mw, 1057 .alloc_pd = rxe_alloc_pd, 1058 .alloc_ucontext = rxe_alloc_ucontext, 1059 .attach_mcast = rxe_attach_mcast, 1060 .create_ah = rxe_create_ah, 1061 .create_cq = rxe_create_cq, 1062 .create_qp = rxe_create_qp, 1063 .create_srq = rxe_create_srq, 1064 .create_user_ah = rxe_create_ah, 1065 .dealloc_driver = rxe_dealloc, 1066 .dealloc_mw = rxe_dealloc_mw, 1067 .dealloc_pd = rxe_dealloc_pd, 1068 .dealloc_ucontext = rxe_dealloc_ucontext, 1069 .dereg_mr = rxe_dereg_mr, 1070 .destroy_ah = rxe_destroy_ah, 1071 .destroy_cq = rxe_destroy_cq, 1072 .destroy_qp = rxe_destroy_qp, 1073 .destroy_srq = rxe_destroy_srq, 1074 .detach_mcast = rxe_detach_mcast, 1075 .device_group = &rxe_attr_group, 1076 .enable_driver = rxe_enable_driver, 1077 .get_dma_mr = rxe_get_dma_mr, 1078 .get_hw_stats = rxe_ib_get_hw_stats, 1079 .get_link_layer = rxe_get_link_layer, 1080 .get_port_immutable = rxe_port_immutable, 1081 .map_mr_sg = rxe_map_mr_sg, 1082 .mmap = rxe_mmap, 1083 .modify_ah = rxe_modify_ah, 1084 .modify_device = rxe_modify_device, 1085 .modify_port = rxe_modify_port, 1086 .modify_qp = rxe_modify_qp, 1087 .modify_srq = rxe_modify_srq, 1088 .peek_cq = rxe_peek_cq, 1089 .poll_cq = rxe_poll_cq, 1090 .post_recv = rxe_post_recv, 1091 .post_send = rxe_post_send, 1092 .post_srq_recv = rxe_post_srq_recv, 1093 .query_ah = rxe_query_ah, 1094 .query_device = rxe_query_device, 1095 .query_pkey = rxe_query_pkey, 1096 .query_port = rxe_query_port, 1097 .query_qp = rxe_query_qp, 1098 .query_srq = rxe_query_srq, 1099 .reg_user_mr = rxe_reg_user_mr, 1100 .req_notify_cq = rxe_req_notify_cq, 1101 .resize_cq = rxe_resize_cq, 1102 1103 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah), 1104 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq), 1105 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd), 1106 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp), 1107 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq), 1108 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc), 1109 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw), 1110 }; 1111 1112 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name) 1113 { 1114 int err; 1115 struct ib_device *dev = &rxe->ib_dev; 1116 1117 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc)); 1118 1119 dev->node_type = RDMA_NODE_IB_CA; 1120 dev->phys_port_cnt = 1; 1121 dev->num_comp_vectors = num_possible_cpus(); 1122 dev->local_dma_lkey = 0; 1123 addrconf_addr_eui48((unsigned char *)&dev->node_guid, 1124 rxe->ndev->dev_addr); 1125 1126 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) | 1127 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ); 1128 1129 ib_set_device_ops(dev, &rxe_dev_ops); 1130 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1); 1131 if (err) 1132 return err; 1133 1134 err = rxe_icrc_init(rxe); 1135 if (err) 1136 return err; 1137 1138 err = ib_register_device(dev, ibdev_name, NULL); 1139 if (err) 1140 pr_warn("%s failed with error %d\n", __func__, err); 1141 1142 /* 1143 * Note that rxe may be invalid at this point if another thread 1144 * unregistered it. 1145 */ 1146 return err; 1147 } 1148