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