1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 5 */ 6 7 #include <linux/dma-mapping.h> 8 #include <net/addrconf.h> 9 #include <rdma/uverbs_ioctl.h> 10 #include "rxe.h" 11 #include "rxe_loc.h" 12 #include "rxe_queue.h" 13 #include "rxe_hw_counters.h" 14 15 static int rxe_query_device(struct ib_device *dev, 16 struct ib_device_attr *attr, 17 struct ib_udata *uhw) 18 { 19 struct rxe_dev *rxe = to_rdev(dev); 20 21 if (uhw->inlen || uhw->outlen) 22 return -EINVAL; 23 24 *attr = rxe->attr; 25 return 0; 26 } 27 28 static int rxe_query_port(struct ib_device *dev, 29 u32 port_num, struct ib_port_attr *attr) 30 { 31 struct rxe_dev *rxe = to_rdev(dev); 32 int rc; 33 34 /* *attr being zeroed by the caller, avoid zeroing it here */ 35 *attr = rxe->port.attr; 36 37 mutex_lock(&rxe->usdev_lock); 38 rc = ib_get_eth_speed(dev, port_num, &attr->active_speed, 39 &attr->active_width); 40 41 if (attr->state == IB_PORT_ACTIVE) 42 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 43 else if (dev_get_flags(rxe->ndev) & IFF_UP) 44 attr->phys_state = IB_PORT_PHYS_STATE_POLLING; 45 else 46 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED; 47 48 mutex_unlock(&rxe->usdev_lock); 49 50 return rc; 51 } 52 53 static int rxe_query_pkey(struct ib_device *device, 54 u32 port_num, u16 index, u16 *pkey) 55 { 56 if (index > 0) 57 return -EINVAL; 58 59 *pkey = IB_DEFAULT_PKEY_FULL; 60 return 0; 61 } 62 63 static int rxe_modify_device(struct ib_device *dev, 64 int mask, struct ib_device_modify *attr) 65 { 66 struct rxe_dev *rxe = to_rdev(dev); 67 68 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID | 69 IB_DEVICE_MODIFY_NODE_DESC)) 70 return -EOPNOTSUPP; 71 72 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) 73 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid); 74 75 if (mask & IB_DEVICE_MODIFY_NODE_DESC) { 76 memcpy(rxe->ib_dev.node_desc, 77 attr->node_desc, sizeof(rxe->ib_dev.node_desc)); 78 } 79 80 return 0; 81 } 82 83 static int rxe_modify_port(struct ib_device *dev, 84 u32 port_num, int mask, struct ib_port_modify *attr) 85 { 86 struct rxe_dev *rxe = to_rdev(dev); 87 struct rxe_port *port; 88 89 port = &rxe->port; 90 91 port->attr.port_cap_flags |= attr->set_port_cap_mask; 92 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask; 93 94 if (mask & IB_PORT_RESET_QKEY_CNTR) 95 port->attr.qkey_viol_cntr = 0; 96 97 return 0; 98 } 99 100 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev, 101 u32 port_num) 102 { 103 return IB_LINK_LAYER_ETHERNET; 104 } 105 106 static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata) 107 { 108 struct rxe_dev *rxe = to_rdev(ibuc->device); 109 struct rxe_ucontext *uc = to_ruc(ibuc); 110 111 return rxe_add_to_pool(&rxe->uc_pool, uc); 112 } 113 114 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) 115 { 116 struct rxe_ucontext *uc = to_ruc(ibuc); 117 118 rxe_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 (init->srq_type != IB_SRQT_BASIC) 290 return -EOPNOTSUPP; 291 292 if (udata) { 293 if (udata->outlen < sizeof(*uresp)) 294 return -EINVAL; 295 uresp = udata->outbuf; 296 } 297 298 err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK); 299 if (err) 300 goto err1; 301 302 err = rxe_add_to_pool(&rxe->srq_pool, srq); 303 if (err) 304 goto err1; 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 err2; 312 313 return 0; 314 315 err2: 316 rxe_put(pd); 317 rxe_put(srq); 318 err1: 319 return err; 320 } 321 322 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 323 enum ib_srq_attr_mask mask, 324 struct ib_udata *udata) 325 { 326 int err; 327 struct rxe_srq *srq = to_rsrq(ibsrq); 328 struct rxe_dev *rxe = to_rdev(ibsrq->device); 329 struct rxe_modify_srq_cmd ucmd = {}; 330 331 if (udata) { 332 if (udata->inlen < sizeof(ucmd)) 333 return -EINVAL; 334 335 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)); 336 if (err) 337 return err; 338 } 339 340 err = rxe_srq_chk_attr(rxe, srq, attr, mask); 341 if (err) 342 goto err1; 343 344 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata); 345 if (err) 346 goto err1; 347 348 return 0; 349 350 err1: 351 return err; 352 } 353 354 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr) 355 { 356 struct rxe_srq *srq = to_rsrq(ibsrq); 357 358 if (srq->error) 359 return -EINVAL; 360 361 attr->max_wr = srq->rq.queue->buf->index_mask; 362 attr->max_sge = srq->rq.max_sge; 363 attr->srq_limit = srq->limit; 364 return 0; 365 } 366 367 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) 368 { 369 struct rxe_srq *srq = to_rsrq(ibsrq); 370 371 if (srq->rq.queue) 372 rxe_queue_cleanup(srq->rq.queue); 373 374 rxe_put(srq->pd); 375 rxe_put(srq); 376 return 0; 377 } 378 379 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, 380 const struct ib_recv_wr **bad_wr) 381 { 382 int err = 0; 383 struct rxe_srq *srq = to_rsrq(ibsrq); 384 unsigned long flags; 385 386 spin_lock_irqsave(&srq->rq.producer_lock, flags); 387 388 while (wr) { 389 err = post_one_recv(&srq->rq, wr); 390 if (unlikely(err)) 391 break; 392 wr = wr->next; 393 } 394 395 spin_unlock_irqrestore(&srq->rq.producer_lock, flags); 396 397 if (err) 398 *bad_wr = wr; 399 400 return err; 401 } 402 403 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, 404 struct ib_udata *udata) 405 { 406 int err; 407 struct rxe_dev *rxe = to_rdev(ibqp->device); 408 struct rxe_pd *pd = to_rpd(ibqp->pd); 409 struct rxe_qp *qp = to_rqp(ibqp); 410 struct rxe_create_qp_resp __user *uresp = NULL; 411 412 if (udata) { 413 if (udata->outlen < sizeof(*uresp)) 414 return -EINVAL; 415 uresp = udata->outbuf; 416 } 417 418 if (init->create_flags) 419 return -EOPNOTSUPP; 420 421 err = rxe_qp_chk_init(rxe, init); 422 if (err) 423 return err; 424 425 if (udata) { 426 if (udata->inlen) 427 return -EINVAL; 428 429 qp->is_user = true; 430 } else { 431 qp->is_user = false; 432 } 433 434 err = rxe_add_to_pool(&rxe->qp_pool, qp); 435 if (err) 436 return err; 437 438 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); 439 if (err) 440 goto qp_init; 441 442 return 0; 443 444 qp_init: 445 rxe_put(qp); 446 return err; 447 } 448 449 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 450 int mask, struct ib_udata *udata) 451 { 452 int err; 453 struct rxe_dev *rxe = to_rdev(ibqp->device); 454 struct rxe_qp *qp = to_rqp(ibqp); 455 456 if (mask & ~IB_QP_ATTR_STANDARD_BITS) 457 return -EOPNOTSUPP; 458 459 err = rxe_qp_chk_attr(rxe, qp, attr, mask); 460 if (err) 461 goto err1; 462 463 err = rxe_qp_from_attr(qp, attr, mask, udata); 464 if (err) 465 goto err1; 466 467 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH)) 468 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label, 469 qp->ibqp.qp_num, 470 qp->attr.dest_qp_num); 471 472 return 0; 473 474 err1: 475 return err; 476 } 477 478 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 479 int mask, struct ib_qp_init_attr *init) 480 { 481 struct rxe_qp *qp = to_rqp(ibqp); 482 483 rxe_qp_to_init(qp, init); 484 rxe_qp_to_attr(qp, attr, mask); 485 486 return 0; 487 } 488 489 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 490 { 491 struct rxe_qp *qp = to_rqp(ibqp); 492 int ret; 493 494 ret = rxe_qp_chk_destroy(qp); 495 if (ret) 496 return ret; 497 498 rxe_qp_destroy(qp); 499 rxe_put(qp); 500 return 0; 501 } 502 503 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 504 unsigned int mask, unsigned int length) 505 { 506 int num_sge = ibwr->num_sge; 507 struct rxe_sq *sq = &qp->sq; 508 509 if (unlikely(num_sge > sq->max_sge)) 510 goto err1; 511 512 if (unlikely(mask & WR_ATOMIC_MASK)) { 513 if (length < 8) 514 goto err1; 515 516 if (atomic_wr(ibwr)->remote_addr & 0x7) 517 goto err1; 518 } 519 520 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) && 521 (length > sq->max_inline))) 522 goto err1; 523 524 return 0; 525 526 err1: 527 return -EINVAL; 528 } 529 530 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr, 531 const struct ib_send_wr *ibwr) 532 { 533 wr->wr_id = ibwr->wr_id; 534 wr->num_sge = ibwr->num_sge; 535 wr->opcode = ibwr->opcode; 536 wr->send_flags = ibwr->send_flags; 537 538 if (qp_type(qp) == IB_QPT_UD || 539 qp_type(qp) == IB_QPT_SMI || 540 qp_type(qp) == IB_QPT_GSI) { 541 struct ib_ah *ibah = ud_wr(ibwr)->ah; 542 543 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn; 544 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey; 545 wr->wr.ud.ah_num = to_rah(ibah)->ah_num; 546 if (qp_type(qp) == IB_QPT_GSI) 547 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index; 548 if (wr->opcode == IB_WR_SEND_WITH_IMM) 549 wr->ex.imm_data = ibwr->ex.imm_data; 550 } else { 551 switch (wr->opcode) { 552 case IB_WR_RDMA_WRITE_WITH_IMM: 553 wr->ex.imm_data = ibwr->ex.imm_data; 554 fallthrough; 555 case IB_WR_RDMA_READ: 556 case IB_WR_RDMA_WRITE: 557 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr; 558 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey; 559 break; 560 case IB_WR_SEND_WITH_IMM: 561 wr->ex.imm_data = ibwr->ex.imm_data; 562 break; 563 case IB_WR_SEND_WITH_INV: 564 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 565 break; 566 case IB_WR_ATOMIC_CMP_AND_SWP: 567 case IB_WR_ATOMIC_FETCH_AND_ADD: 568 wr->wr.atomic.remote_addr = 569 atomic_wr(ibwr)->remote_addr; 570 wr->wr.atomic.compare_add = 571 atomic_wr(ibwr)->compare_add; 572 wr->wr.atomic.swap = atomic_wr(ibwr)->swap; 573 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey; 574 break; 575 case IB_WR_LOCAL_INV: 576 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 577 break; 578 case IB_WR_REG_MR: 579 wr->wr.reg.mr = reg_wr(ibwr)->mr; 580 wr->wr.reg.key = reg_wr(ibwr)->key; 581 wr->wr.reg.access = reg_wr(ibwr)->access; 582 break; 583 default: 584 break; 585 } 586 } 587 } 588 589 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe, 590 const struct ib_send_wr *ibwr) 591 { 592 struct ib_sge *sge = ibwr->sg_list; 593 u8 *p = wqe->dma.inline_data; 594 int i; 595 596 for (i = 0; i < ibwr->num_sge; i++, sge++) { 597 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length); 598 p += sge->length; 599 } 600 } 601 602 static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 603 unsigned int mask, unsigned int length, 604 struct rxe_send_wqe *wqe) 605 { 606 int num_sge = ibwr->num_sge; 607 608 init_send_wr(qp, &wqe->wr, ibwr); 609 610 /* local operation */ 611 if (unlikely(mask & WR_LOCAL_OP_MASK)) { 612 wqe->mask = mask; 613 wqe->state = wqe_state_posted; 614 return; 615 } 616 617 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) 618 copy_inline_data_to_wqe(wqe, ibwr); 619 else 620 memcpy(wqe->dma.sge, ibwr->sg_list, 621 num_sge * sizeof(struct ib_sge)); 622 623 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr : 624 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0; 625 wqe->mask = mask; 626 wqe->dma.length = length; 627 wqe->dma.resid = length; 628 wqe->dma.num_sge = num_sge; 629 wqe->dma.cur_sge = 0; 630 wqe->dma.sge_offset = 0; 631 wqe->state = wqe_state_posted; 632 wqe->ssn = atomic_add_return(1, &qp->ssn); 633 } 634 635 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 636 unsigned int mask, u32 length) 637 { 638 int err; 639 struct rxe_sq *sq = &qp->sq; 640 struct rxe_send_wqe *send_wqe; 641 unsigned long flags; 642 int full; 643 644 err = validate_send_wr(qp, ibwr, mask, length); 645 if (err) 646 return err; 647 648 spin_lock_irqsave(&qp->sq.sq_lock, flags); 649 650 full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER); 651 652 if (unlikely(full)) { 653 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 654 return -ENOMEM; 655 } 656 657 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER); 658 init_send_wqe(qp, ibwr, mask, length, send_wqe); 659 660 queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER); 661 662 spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 663 664 return 0; 665 } 666 667 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr, 668 const struct ib_send_wr **bad_wr) 669 { 670 int err = 0; 671 unsigned int mask; 672 unsigned int length = 0; 673 int i; 674 struct ib_send_wr *next; 675 676 while (wr) { 677 mask = wr_opcode_mask(wr->opcode, qp); 678 if (unlikely(!mask)) { 679 err = -EINVAL; 680 *bad_wr = wr; 681 break; 682 } 683 684 if (unlikely((wr->send_flags & IB_SEND_INLINE) && 685 !(mask & WR_INLINE_MASK))) { 686 err = -EINVAL; 687 *bad_wr = wr; 688 break; 689 } 690 691 next = wr->next; 692 693 length = 0; 694 for (i = 0; i < wr->num_sge; i++) 695 length += wr->sg_list[i].length; 696 697 err = post_one_send(qp, wr, mask, length); 698 699 if (err) { 700 *bad_wr = wr; 701 break; 702 } 703 wr = next; 704 } 705 706 rxe_run_task(&qp->req.task, 1); 707 if (unlikely(qp->req.state == QP_STATE_ERROR)) 708 rxe_run_task(&qp->comp.task, 1); 709 710 return err; 711 } 712 713 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 714 const struct ib_send_wr **bad_wr) 715 { 716 struct rxe_qp *qp = to_rqp(ibqp); 717 718 if (unlikely(!qp->valid)) { 719 *bad_wr = wr; 720 return -EINVAL; 721 } 722 723 if (unlikely(qp->req.state < QP_STATE_READY)) { 724 *bad_wr = wr; 725 return -EINVAL; 726 } 727 728 if (qp->is_user) { 729 /* Utilize process context to do protocol processing */ 730 rxe_run_task(&qp->req.task, 0); 731 return 0; 732 } else 733 return rxe_post_send_kernel(qp, wr, bad_wr); 734 } 735 736 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 737 const struct ib_recv_wr **bad_wr) 738 { 739 int err = 0; 740 struct rxe_qp *qp = to_rqp(ibqp); 741 struct rxe_rq *rq = &qp->rq; 742 unsigned long flags; 743 744 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) { 745 *bad_wr = wr; 746 err = -EINVAL; 747 goto err1; 748 } 749 750 if (unlikely(qp->srq)) { 751 *bad_wr = wr; 752 err = -EINVAL; 753 goto err1; 754 } 755 756 spin_lock_irqsave(&rq->producer_lock, flags); 757 758 while (wr) { 759 err = post_one_recv(rq, wr); 760 if (unlikely(err)) { 761 *bad_wr = wr; 762 break; 763 } 764 wr = wr->next; 765 } 766 767 spin_unlock_irqrestore(&rq->producer_lock, flags); 768 769 if (qp->resp.state == QP_STATE_ERROR) 770 rxe_run_task(&qp->resp.task, 1); 771 772 err1: 773 return err; 774 } 775 776 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 777 struct ib_udata *udata) 778 { 779 int err; 780 struct ib_device *dev = ibcq->device; 781 struct rxe_dev *rxe = to_rdev(dev); 782 struct rxe_cq *cq = to_rcq(ibcq); 783 struct rxe_create_cq_resp __user *uresp = NULL; 784 785 if (udata) { 786 if (udata->outlen < sizeof(*uresp)) 787 return -EINVAL; 788 uresp = udata->outbuf; 789 } 790 791 if (attr->flags) 792 return -EOPNOTSUPP; 793 794 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector); 795 if (err) 796 return err; 797 798 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata, 799 uresp); 800 if (err) 801 return err; 802 803 return rxe_add_to_pool(&rxe->cq_pool, cq); 804 } 805 806 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 807 { 808 struct rxe_cq *cq = to_rcq(ibcq); 809 810 rxe_cq_disable(cq); 811 812 rxe_put(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 908 return &mr->ibmr; 909 } 910 911 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, 912 u64 start, 913 u64 length, 914 u64 iova, 915 int access, struct ib_udata *udata) 916 { 917 int err; 918 struct rxe_dev *rxe = to_rdev(ibpd->device); 919 struct rxe_pd *pd = to_rpd(ibpd); 920 struct rxe_mr *mr; 921 922 mr = rxe_alloc(&rxe->mr_pool); 923 if (!mr) { 924 err = -ENOMEM; 925 goto err2; 926 } 927 928 929 rxe_get(pd); 930 931 err = rxe_mr_init_user(pd, start, length, iova, access, mr); 932 if (err) 933 goto err3; 934 935 return &mr->ibmr; 936 937 err3: 938 rxe_put(pd); 939 rxe_put(mr); 940 err2: 941 return ERR_PTR(err); 942 } 943 944 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, 945 u32 max_num_sg) 946 { 947 struct rxe_dev *rxe = to_rdev(ibpd->device); 948 struct rxe_pd *pd = to_rpd(ibpd); 949 struct rxe_mr *mr; 950 int err; 951 952 if (mr_type != IB_MR_TYPE_MEM_REG) 953 return ERR_PTR(-EINVAL); 954 955 mr = rxe_alloc(&rxe->mr_pool); 956 if (!mr) { 957 err = -ENOMEM; 958 goto err1; 959 } 960 961 rxe_get(pd); 962 963 err = rxe_mr_init_fast(pd, max_num_sg, mr); 964 if (err) 965 goto err2; 966 967 return &mr->ibmr; 968 969 err2: 970 rxe_put(pd); 971 rxe_put(mr); 972 err1: 973 return ERR_PTR(err); 974 } 975 976 /* build next_map_set from scatterlist 977 * The IB_WR_REG_MR WR will swap map_sets 978 */ 979 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, 980 int sg_nents, unsigned int *sg_offset) 981 { 982 struct rxe_mr *mr = to_rmr(ibmr); 983 struct rxe_map_set *set = mr->next_map_set; 984 int n; 985 986 set->nbuf = 0; 987 988 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_mr_set_page); 989 990 set->va = ibmr->iova; 991 set->iova = ibmr->iova; 992 set->length = ibmr->length; 993 set->page_shift = ilog2(ibmr->page_size); 994 set->page_mask = ibmr->page_size - 1; 995 set->offset = set->iova & set->page_mask; 996 997 return n; 998 } 999 1000 static ssize_t parent_show(struct device *device, 1001 struct device_attribute *attr, char *buf) 1002 { 1003 struct rxe_dev *rxe = 1004 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev); 1005 1006 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1)); 1007 } 1008 1009 static DEVICE_ATTR_RO(parent); 1010 1011 static struct attribute *rxe_dev_attributes[] = { 1012 &dev_attr_parent.attr, 1013 NULL 1014 }; 1015 1016 static const struct attribute_group rxe_attr_group = { 1017 .attrs = rxe_dev_attributes, 1018 }; 1019 1020 static int rxe_enable_driver(struct ib_device *ib_dev) 1021 { 1022 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); 1023 1024 rxe_set_port_state(rxe); 1025 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev)); 1026 return 0; 1027 } 1028 1029 static const struct ib_device_ops rxe_dev_ops = { 1030 .owner = THIS_MODULE, 1031 .driver_id = RDMA_DRIVER_RXE, 1032 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION, 1033 1034 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats, 1035 .alloc_mr = rxe_alloc_mr, 1036 .alloc_mw = rxe_alloc_mw, 1037 .alloc_pd = rxe_alloc_pd, 1038 .alloc_ucontext = rxe_alloc_ucontext, 1039 .attach_mcast = rxe_attach_mcast, 1040 .create_ah = rxe_create_ah, 1041 .create_cq = rxe_create_cq, 1042 .create_qp = rxe_create_qp, 1043 .create_srq = rxe_create_srq, 1044 .create_user_ah = rxe_create_ah, 1045 .dealloc_driver = rxe_dealloc, 1046 .dealloc_mw = rxe_dealloc_mw, 1047 .dealloc_pd = rxe_dealloc_pd, 1048 .dealloc_ucontext = rxe_dealloc_ucontext, 1049 .dereg_mr = rxe_dereg_mr, 1050 .destroy_ah = rxe_destroy_ah, 1051 .destroy_cq = rxe_destroy_cq, 1052 .destroy_qp = rxe_destroy_qp, 1053 .destroy_srq = rxe_destroy_srq, 1054 .detach_mcast = rxe_detach_mcast, 1055 .device_group = &rxe_attr_group, 1056 .enable_driver = rxe_enable_driver, 1057 .get_dma_mr = rxe_get_dma_mr, 1058 .get_hw_stats = rxe_ib_get_hw_stats, 1059 .get_link_layer = rxe_get_link_layer, 1060 .get_port_immutable = rxe_port_immutable, 1061 .map_mr_sg = rxe_map_mr_sg, 1062 .mmap = rxe_mmap, 1063 .modify_ah = rxe_modify_ah, 1064 .modify_device = rxe_modify_device, 1065 .modify_port = rxe_modify_port, 1066 .modify_qp = rxe_modify_qp, 1067 .modify_srq = rxe_modify_srq, 1068 .peek_cq = rxe_peek_cq, 1069 .poll_cq = rxe_poll_cq, 1070 .post_recv = rxe_post_recv, 1071 .post_send = rxe_post_send, 1072 .post_srq_recv = rxe_post_srq_recv, 1073 .query_ah = rxe_query_ah, 1074 .query_device = rxe_query_device, 1075 .query_pkey = rxe_query_pkey, 1076 .query_port = rxe_query_port, 1077 .query_qp = rxe_query_qp, 1078 .query_srq = rxe_query_srq, 1079 .reg_user_mr = rxe_reg_user_mr, 1080 .req_notify_cq = rxe_req_notify_cq, 1081 .resize_cq = rxe_resize_cq, 1082 1083 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah), 1084 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq), 1085 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd), 1086 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp), 1087 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq), 1088 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc), 1089 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw), 1090 }; 1091 1092 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name) 1093 { 1094 int err; 1095 struct ib_device *dev = &rxe->ib_dev; 1096 1097 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc)); 1098 1099 dev->node_type = RDMA_NODE_IB_CA; 1100 dev->phys_port_cnt = 1; 1101 dev->num_comp_vectors = num_possible_cpus(); 1102 dev->local_dma_lkey = 0; 1103 addrconf_addr_eui48((unsigned char *)&dev->node_guid, 1104 rxe->ndev->dev_addr); 1105 1106 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) | 1107 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ); 1108 1109 ib_set_device_ops(dev, &rxe_dev_ops); 1110 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1); 1111 if (err) 1112 return err; 1113 1114 err = rxe_icrc_init(rxe); 1115 if (err) 1116 return err; 1117 1118 err = ib_register_device(dev, ibdev_name, NULL); 1119 if (err) 1120 pr_warn("%s failed with error %d\n", __func__, err); 1121 1122 /* 1123 * Note that rxe may be invalid at this point if another thread 1124 * unregistered it. 1125 */ 1126 return err; 1127 } 1128