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