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 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 (udata) { 293 if (udata->outlen < sizeof(*uresp)) 294 return -EINVAL; 295 uresp = udata->outbuf; 296 } 297 298 if (init->srq_type != IB_SRQT_BASIC) 299 return -EOPNOTSUPP; 300 301 err = rxe_srq_chk_init(rxe, init); 302 if (err) 303 return err; 304 305 err = rxe_add_to_pool(&rxe->srq_pool, srq); 306 if (err) 307 return err; 308 309 rxe_get(pd); 310 srq->pd = pd; 311 312 err = rxe_srq_from_init(rxe, srq, init, udata, uresp); 313 if (err) 314 goto err_cleanup; 315 316 return 0; 317 318 err_cleanup: 319 rxe_cleanup(srq); 320 321 return err; 322 } 323 324 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 325 enum ib_srq_attr_mask mask, 326 struct ib_udata *udata) 327 { 328 int err; 329 struct rxe_srq *srq = to_rsrq(ibsrq); 330 struct rxe_dev *rxe = to_rdev(ibsrq->device); 331 struct rxe_modify_srq_cmd ucmd = {}; 332 333 if (udata) { 334 if (udata->inlen < sizeof(ucmd)) 335 return -EINVAL; 336 337 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)); 338 if (err) 339 return err; 340 } 341 342 err = rxe_srq_chk_attr(rxe, srq, attr, mask); 343 if (err) 344 return err; 345 346 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata); 347 if (err) 348 return err; 349 return 0; 350 } 351 352 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr) 353 { 354 struct rxe_srq *srq = to_rsrq(ibsrq); 355 356 if (srq->error) 357 return -EINVAL; 358 359 attr->max_wr = srq->rq.queue->buf->index_mask; 360 attr->max_sge = srq->rq.max_sge; 361 attr->srq_limit = srq->limit; 362 return 0; 363 } 364 365 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) 366 { 367 struct rxe_srq *srq = to_rsrq(ibsrq); 368 369 rxe_cleanup(srq); 370 return 0; 371 } 372 373 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, 374 const struct ib_recv_wr **bad_wr) 375 { 376 int err = 0; 377 struct rxe_srq *srq = to_rsrq(ibsrq); 378 unsigned long flags; 379 380 spin_lock_irqsave(&srq->rq.producer_lock, flags); 381 382 while (wr) { 383 err = post_one_recv(&srq->rq, wr); 384 if (unlikely(err)) 385 break; 386 wr = wr->next; 387 } 388 389 spin_unlock_irqrestore(&srq->rq.producer_lock, flags); 390 391 if (err) 392 *bad_wr = wr; 393 394 return err; 395 } 396 397 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, 398 struct ib_udata *udata) 399 { 400 int err; 401 struct rxe_dev *rxe = to_rdev(ibqp->device); 402 struct rxe_pd *pd = to_rpd(ibqp->pd); 403 struct rxe_qp *qp = to_rqp(ibqp); 404 struct rxe_create_qp_resp __user *uresp = NULL; 405 406 if (udata) { 407 if (udata->outlen < sizeof(*uresp)) 408 return -EINVAL; 409 uresp = udata->outbuf; 410 } 411 412 if (init->create_flags) 413 return -EOPNOTSUPP; 414 415 err = rxe_qp_chk_init(rxe, init); 416 if (err) 417 return err; 418 419 if (udata) { 420 if (udata->inlen) 421 return -EINVAL; 422 423 qp->is_user = true; 424 } else { 425 qp->is_user = false; 426 } 427 428 err = rxe_add_to_pool(&rxe->qp_pool, qp); 429 if (err) 430 return err; 431 432 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); 433 if (err) 434 goto qp_init; 435 436 rxe_finalize(qp); 437 return 0; 438 439 qp_init: 440 rxe_cleanup(qp); 441 return err; 442 } 443 444 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 445 int mask, struct ib_udata *udata) 446 { 447 int err; 448 struct rxe_dev *rxe = to_rdev(ibqp->device); 449 struct rxe_qp *qp = to_rqp(ibqp); 450 451 if (mask & ~IB_QP_ATTR_STANDARD_BITS) 452 return -EOPNOTSUPP; 453 454 err = rxe_qp_chk_attr(rxe, qp, attr, mask); 455 if (err) 456 goto err1; 457 458 err = rxe_qp_from_attr(qp, attr, mask, udata); 459 if (err) 460 goto err1; 461 462 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH)) 463 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label, 464 qp->ibqp.qp_num, 465 qp->attr.dest_qp_num); 466 467 return 0; 468 469 err1: 470 return err; 471 } 472 473 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 474 int mask, struct ib_qp_init_attr *init) 475 { 476 struct rxe_qp *qp = to_rqp(ibqp); 477 478 rxe_qp_to_init(qp, init); 479 rxe_qp_to_attr(qp, attr, mask); 480 481 return 0; 482 } 483 484 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 485 { 486 struct rxe_qp *qp = to_rqp(ibqp); 487 int ret; 488 489 ret = rxe_qp_chk_destroy(qp); 490 if (ret) 491 return ret; 492 493 rxe_cleanup(qp); 494 return 0; 495 } 496 497 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 498 unsigned int mask, unsigned int length) 499 { 500 int num_sge = ibwr->num_sge; 501 struct rxe_sq *sq = &qp->sq; 502 503 if (unlikely(num_sge > sq->max_sge)) 504 goto err1; 505 506 if (unlikely(mask & WR_ATOMIC_MASK)) { 507 if (length < 8) 508 goto err1; 509 510 if (atomic_wr(ibwr)->remote_addr & 0x7) 511 goto err1; 512 } 513 514 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) && 515 (length > sq->max_inline))) 516 goto err1; 517 518 return 0; 519 520 err1: 521 return -EINVAL; 522 } 523 524 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr, 525 const struct ib_send_wr *ibwr) 526 { 527 wr->wr_id = ibwr->wr_id; 528 wr->opcode = ibwr->opcode; 529 wr->send_flags = ibwr->send_flags; 530 531 if (qp_type(qp) == IB_QPT_UD || 532 qp_type(qp) == IB_QPT_GSI) { 533 struct ib_ah *ibah = ud_wr(ibwr)->ah; 534 535 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn; 536 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey; 537 wr->wr.ud.ah_num = to_rah(ibah)->ah_num; 538 if (qp_type(qp) == IB_QPT_GSI) 539 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index; 540 if (wr->opcode == IB_WR_SEND_WITH_IMM) 541 wr->ex.imm_data = ibwr->ex.imm_data; 542 } else { 543 switch (wr->opcode) { 544 case IB_WR_RDMA_WRITE_WITH_IMM: 545 wr->ex.imm_data = ibwr->ex.imm_data; 546 fallthrough; 547 case IB_WR_RDMA_READ: 548 case IB_WR_RDMA_WRITE: 549 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr; 550 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey; 551 break; 552 case IB_WR_SEND_WITH_IMM: 553 wr->ex.imm_data = ibwr->ex.imm_data; 554 break; 555 case IB_WR_SEND_WITH_INV: 556 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 557 break; 558 case IB_WR_ATOMIC_CMP_AND_SWP: 559 case IB_WR_ATOMIC_FETCH_AND_ADD: 560 wr->wr.atomic.remote_addr = 561 atomic_wr(ibwr)->remote_addr; 562 wr->wr.atomic.compare_add = 563 atomic_wr(ibwr)->compare_add; 564 wr->wr.atomic.swap = atomic_wr(ibwr)->swap; 565 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey; 566 break; 567 case IB_WR_LOCAL_INV: 568 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 569 break; 570 case IB_WR_REG_MR: 571 wr->wr.reg.mr = reg_wr(ibwr)->mr; 572 wr->wr.reg.key = reg_wr(ibwr)->key; 573 wr->wr.reg.access = reg_wr(ibwr)->access; 574 break; 575 default: 576 break; 577 } 578 } 579 } 580 581 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe, 582 const struct ib_send_wr *ibwr) 583 { 584 struct ib_sge *sge = ibwr->sg_list; 585 u8 *p = wqe->dma.inline_data; 586 int i; 587 588 for (i = 0; i < ibwr->num_sge; i++, sge++) { 589 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length); 590 p += sge->length; 591 } 592 } 593 594 static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 595 unsigned int mask, unsigned int length, 596 struct rxe_send_wqe *wqe) 597 { 598 int num_sge = ibwr->num_sge; 599 600 init_send_wr(qp, &wqe->wr, ibwr); 601 602 /* local operation */ 603 if (unlikely(mask & WR_LOCAL_OP_MASK)) { 604 wqe->mask = mask; 605 wqe->state = wqe_state_posted; 606 return; 607 } 608 609 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) 610 copy_inline_data_to_wqe(wqe, ibwr); 611 else 612 memcpy(wqe->dma.sge, ibwr->sg_list, 613 num_sge * sizeof(struct ib_sge)); 614 615 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr : 616 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0; 617 wqe->mask = mask; 618 wqe->dma.length = length; 619 wqe->dma.resid = length; 620 wqe->dma.num_sge = num_sge; 621 wqe->dma.cur_sge = 0; 622 wqe->dma.sge_offset = 0; 623 wqe->state = wqe_state_posted; 624 wqe->ssn = atomic_add_return(1, &qp->ssn); 625 } 626 627 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 628 unsigned int mask, u32 length) 629 { 630 int err; 631 struct rxe_sq *sq = &qp->sq; 632 struct rxe_send_wqe *send_wqe; 633 unsigned long flags; 634 int full; 635 636 err = validate_send_wr(qp, ibwr, mask, length); 637 if (err) 638 return err; 639 640 spin_lock_irqsave(&qp->sq.sq_lock, flags); 641 642 full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER); 643 644 if (unlikely(full)) { 645 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 646 return -ENOMEM; 647 } 648 649 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER); 650 init_send_wqe(qp, ibwr, mask, length, send_wqe); 651 652 queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER); 653 654 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 655 656 return 0; 657 } 658 659 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr, 660 const struct ib_send_wr **bad_wr) 661 { 662 int err = 0; 663 unsigned int mask; 664 unsigned int length = 0; 665 int i; 666 struct ib_send_wr *next; 667 668 while (wr) { 669 mask = wr_opcode_mask(wr->opcode, qp); 670 if (unlikely(!mask)) { 671 err = -EINVAL; 672 *bad_wr = wr; 673 break; 674 } 675 676 if (unlikely((wr->send_flags & IB_SEND_INLINE) && 677 !(mask & WR_INLINE_MASK))) { 678 err = -EINVAL; 679 *bad_wr = wr; 680 break; 681 } 682 683 next = wr->next; 684 685 length = 0; 686 for (i = 0; i < wr->num_sge; i++) 687 length += wr->sg_list[i].length; 688 689 err = post_one_send(qp, wr, mask, length); 690 691 if (err) { 692 *bad_wr = wr; 693 break; 694 } 695 wr = next; 696 } 697 698 rxe_run_task(&qp->req.task, 1); 699 if (unlikely(qp->req.state == QP_STATE_ERROR)) 700 rxe_run_task(&qp->comp.task, 1); 701 702 return err; 703 } 704 705 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 706 const struct ib_send_wr **bad_wr) 707 { 708 struct rxe_qp *qp = to_rqp(ibqp); 709 710 if (unlikely(!qp->valid)) { 711 *bad_wr = wr; 712 return -EINVAL; 713 } 714 715 if (unlikely(qp->req.state < QP_STATE_READY)) { 716 *bad_wr = wr; 717 return -EINVAL; 718 } 719 720 if (qp->is_user) { 721 /* Utilize process context to do protocol processing */ 722 rxe_run_task(&qp->req.task, 0); 723 return 0; 724 } else 725 return rxe_post_send_kernel(qp, wr, bad_wr); 726 } 727 728 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 729 const struct ib_recv_wr **bad_wr) 730 { 731 int err = 0; 732 struct rxe_qp *qp = to_rqp(ibqp); 733 struct rxe_rq *rq = &qp->rq; 734 unsigned long flags; 735 736 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) { 737 *bad_wr = wr; 738 err = -EINVAL; 739 goto err1; 740 } 741 742 if (unlikely(qp->srq)) { 743 *bad_wr = wr; 744 err = -EINVAL; 745 goto err1; 746 } 747 748 spin_lock_irqsave(&rq->producer_lock, flags); 749 750 while (wr) { 751 err = post_one_recv(rq, wr); 752 if (unlikely(err)) { 753 *bad_wr = wr; 754 break; 755 } 756 wr = wr->next; 757 } 758 759 spin_unlock_irqrestore(&rq->producer_lock, flags); 760 761 if (qp->resp.state == QP_STATE_ERROR) 762 rxe_run_task(&qp->resp.task, 1); 763 764 err1: 765 return err; 766 } 767 768 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 769 struct ib_udata *udata) 770 { 771 int err; 772 struct ib_device *dev = ibcq->device; 773 struct rxe_dev *rxe = to_rdev(dev); 774 struct rxe_cq *cq = to_rcq(ibcq); 775 struct rxe_create_cq_resp __user *uresp = NULL; 776 777 if (udata) { 778 if (udata->outlen < sizeof(*uresp)) 779 return -EINVAL; 780 uresp = udata->outbuf; 781 } 782 783 if (attr->flags) 784 return -EOPNOTSUPP; 785 786 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector); 787 if (err) 788 return err; 789 790 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata, 791 uresp); 792 if (err) 793 return err; 794 795 return rxe_add_to_pool(&rxe->cq_pool, cq); 796 } 797 798 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 799 { 800 struct rxe_cq *cq = to_rcq(ibcq); 801 802 /* See IBA C11-17: The CI shall return an error if this Verb is 803 * invoked while a Work Queue is still associated with the CQ. 804 */ 805 if (atomic_read(&cq->num_wq)) 806 return -EINVAL; 807 808 rxe_cq_disable(cq); 809 810 rxe_cleanup(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 unsigned long flags; 847 848 spin_lock_irqsave(&cq->cq_lock, flags); 849 for (i = 0; i < num_entries; i++) { 850 cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER); 851 if (!cqe) 852 break; 853 854 memcpy(wc++, &cqe->ibwc, sizeof(*wc)); 855 queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER); 856 } 857 spin_unlock_irqrestore(&cq->cq_lock, flags); 858 859 return i; 860 } 861 862 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt) 863 { 864 struct rxe_cq *cq = to_rcq(ibcq); 865 int count; 866 867 count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER); 868 869 return (count > wc_cnt) ? wc_cnt : count; 870 } 871 872 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) 873 { 874 struct rxe_cq *cq = to_rcq(ibcq); 875 int ret = 0; 876 int empty; 877 unsigned long irq_flags; 878 879 spin_lock_irqsave(&cq->cq_lock, irq_flags); 880 if (cq->notify != IB_CQ_NEXT_COMP) 881 cq->notify = flags & IB_CQ_SOLICITED_MASK; 882 883 empty = queue_empty(cq->queue, QUEUE_TYPE_FROM_DRIVER); 884 885 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty) 886 ret = 1; 887 888 spin_unlock_irqrestore(&cq->cq_lock, irq_flags); 889 890 return ret; 891 } 892 893 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access) 894 { 895 struct rxe_dev *rxe = to_rdev(ibpd->device); 896 struct rxe_pd *pd = to_rpd(ibpd); 897 struct rxe_mr *mr; 898 899 mr = rxe_alloc(&rxe->mr_pool); 900 if (!mr) 901 return ERR_PTR(-ENOMEM); 902 903 rxe_get(pd); 904 mr->ibmr.pd = ibpd; 905 906 rxe_mr_init_dma(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 mr->ibmr.pd = ibpd; 932 933 err = rxe_mr_init_user(rxe, start, length, iova, access, mr); 934 if (err) 935 goto err3; 936 937 rxe_finalize(mr); 938 939 return &mr->ibmr; 940 941 err3: 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 mr->ibmr.pd = ibpd; 966 967 err = rxe_mr_init_fast(max_num_sg, mr); 968 if (err) 969 goto err2; 970 971 rxe_finalize(mr); 972 973 return &mr->ibmr; 974 975 err2: 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->page_shift = ilog2(ibmr->page_size); 1011 mr->page_mask = ibmr->page_size - 1; 1012 mr->offset = ibmr->iova & mr->page_mask; 1013 1014 return n; 1015 } 1016 1017 static ssize_t parent_show(struct device *device, 1018 struct device_attribute *attr, char *buf) 1019 { 1020 struct rxe_dev *rxe = 1021 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev); 1022 1023 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1)); 1024 } 1025 1026 static DEVICE_ATTR_RO(parent); 1027 1028 static struct attribute *rxe_dev_attributes[] = { 1029 &dev_attr_parent.attr, 1030 NULL 1031 }; 1032 1033 static const struct attribute_group rxe_attr_group = { 1034 .attrs = rxe_dev_attributes, 1035 }; 1036 1037 static int rxe_enable_driver(struct ib_device *ib_dev) 1038 { 1039 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); 1040 1041 rxe_set_port_state(rxe); 1042 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev)); 1043 return 0; 1044 } 1045 1046 static const struct ib_device_ops rxe_dev_ops = { 1047 .owner = THIS_MODULE, 1048 .driver_id = RDMA_DRIVER_RXE, 1049 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION, 1050 1051 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats, 1052 .alloc_mr = rxe_alloc_mr, 1053 .alloc_mw = rxe_alloc_mw, 1054 .alloc_pd = rxe_alloc_pd, 1055 .alloc_ucontext = rxe_alloc_ucontext, 1056 .attach_mcast = rxe_attach_mcast, 1057 .create_ah = rxe_create_ah, 1058 .create_cq = rxe_create_cq, 1059 .create_qp = rxe_create_qp, 1060 .create_srq = rxe_create_srq, 1061 .create_user_ah = rxe_create_ah, 1062 .dealloc_driver = rxe_dealloc, 1063 .dealloc_mw = rxe_dealloc_mw, 1064 .dealloc_pd = rxe_dealloc_pd, 1065 .dealloc_ucontext = rxe_dealloc_ucontext, 1066 .dereg_mr = rxe_dereg_mr, 1067 .destroy_ah = rxe_destroy_ah, 1068 .destroy_cq = rxe_destroy_cq, 1069 .destroy_qp = rxe_destroy_qp, 1070 .destroy_srq = rxe_destroy_srq, 1071 .detach_mcast = rxe_detach_mcast, 1072 .device_group = &rxe_attr_group, 1073 .enable_driver = rxe_enable_driver, 1074 .get_dma_mr = rxe_get_dma_mr, 1075 .get_hw_stats = rxe_ib_get_hw_stats, 1076 .get_link_layer = rxe_get_link_layer, 1077 .get_port_immutable = rxe_port_immutable, 1078 .map_mr_sg = rxe_map_mr_sg, 1079 .mmap = rxe_mmap, 1080 .modify_ah = rxe_modify_ah, 1081 .modify_device = rxe_modify_device, 1082 .modify_port = rxe_modify_port, 1083 .modify_qp = rxe_modify_qp, 1084 .modify_srq = rxe_modify_srq, 1085 .peek_cq = rxe_peek_cq, 1086 .poll_cq = rxe_poll_cq, 1087 .post_recv = rxe_post_recv, 1088 .post_send = rxe_post_send, 1089 .post_srq_recv = rxe_post_srq_recv, 1090 .query_ah = rxe_query_ah, 1091 .query_device = rxe_query_device, 1092 .query_pkey = rxe_query_pkey, 1093 .query_port = rxe_query_port, 1094 .query_qp = rxe_query_qp, 1095 .query_srq = rxe_query_srq, 1096 .reg_user_mr = rxe_reg_user_mr, 1097 .req_notify_cq = rxe_req_notify_cq, 1098 .resize_cq = rxe_resize_cq, 1099 1100 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah), 1101 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq), 1102 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd), 1103 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp), 1104 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq), 1105 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc), 1106 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw), 1107 }; 1108 1109 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name) 1110 { 1111 int err; 1112 struct ib_device *dev = &rxe->ib_dev; 1113 1114 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc)); 1115 1116 dev->node_type = RDMA_NODE_IB_CA; 1117 dev->phys_port_cnt = 1; 1118 dev->num_comp_vectors = num_possible_cpus(); 1119 dev->local_dma_lkey = 0; 1120 addrconf_addr_eui48((unsigned char *)&dev->node_guid, 1121 rxe->ndev->dev_addr); 1122 1123 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) | 1124 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ); 1125 1126 ib_set_device_ops(dev, &rxe_dev_ops); 1127 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1); 1128 if (err) 1129 return err; 1130 1131 err = rxe_icrc_init(rxe); 1132 if (err) 1133 return err; 1134 1135 err = ib_register_device(dev, ibdev_name, NULL); 1136 if (err) 1137 pr_warn("%s failed with error %d\n", __func__, err); 1138 1139 /* 1140 * Note that rxe may be invalid at this point if another thread 1141 * unregistered it. 1142 */ 1143 return err; 1144 } 1145