1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
7 #include <linux/dma-mapping.h>
8 #include <net/addrconf.h>
9 #include <rdma/uverbs_ioctl.h>
12 #include "rxe_queue.h"
13 #include "rxe_hw_counters.h"
15 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr);
18 static int rxe_query_device(struct ib_device *ibdev,
19 struct ib_device_attr *attr,
20 struct ib_udata *udata)
22 struct rxe_dev *rxe = to_rdev(ibdev);
25 if (udata->inlen || udata->outlen) {
26 rxe_dbg_dev(rxe, "malformed udata\n");
31 memcpy(attr, &rxe->attr, sizeof(*attr));
36 rxe_err_dev(rxe, "returned err = %d\n", err);
40 static int rxe_query_port(struct ib_device *ibdev,
41 u32 port_num, struct ib_port_attr *attr)
43 struct rxe_dev *rxe = to_rdev(ibdev);
44 struct net_device *ndev;
49 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
53 ndev = rxe_ib_device_get_netdev(ibdev);
59 memcpy(attr, &rxe->port.attr, sizeof(*attr));
61 mutex_lock(&rxe->usdev_lock);
62 ret = ib_get_eth_speed(ibdev, port_num, &attr->active_speed,
65 if (attr->state == IB_PORT_ACTIVE)
66 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
67 else if (dev_get_flags(ndev) & IFF_UP)
68 attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
70 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
72 mutex_unlock(&rxe->usdev_lock);
78 rxe_err_dev(rxe, "returned err = %d\n", err);
82 static int rxe_query_pkey(struct ib_device *ibdev,
83 u32 port_num, u16 index, u16 *pkey)
85 struct rxe_dev *rxe = to_rdev(ibdev);
90 rxe_dbg_dev(rxe, "bad pkey index = %d\n", index);
94 *pkey = IB_DEFAULT_PKEY_FULL;
98 rxe_err_dev(rxe, "returned err = %d\n", err);
102 static int rxe_modify_device(struct ib_device *ibdev,
103 int mask, struct ib_device_modify *attr)
105 struct rxe_dev *rxe = to_rdev(ibdev);
108 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
109 IB_DEVICE_MODIFY_NODE_DESC)) {
111 rxe_dbg_dev(rxe, "unsupported mask = 0x%x\n", mask);
115 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
116 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
118 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
119 memcpy(rxe->ib_dev.node_desc,
120 attr->node_desc, sizeof(rxe->ib_dev.node_desc));
126 rxe_err_dev(rxe, "returned err = %d\n", err);
130 static int rxe_modify_port(struct ib_device *ibdev, u32 port_num,
131 int mask, struct ib_port_modify *attr)
133 struct rxe_dev *rxe = to_rdev(ibdev);
134 struct rxe_port *port;
139 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
143 //TODO is shutdown useful
144 if (mask & ~(IB_PORT_RESET_QKEY_CNTR)) {
146 rxe_dbg_dev(rxe, "unsupported mask = 0x%x\n", mask);
151 port->attr.port_cap_flags |= attr->set_port_cap_mask;
152 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
154 if (mask & IB_PORT_RESET_QKEY_CNTR)
155 port->attr.qkey_viol_cntr = 0;
160 rxe_err_dev(rxe, "returned err = %d\n", err);
164 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *ibdev,
167 struct rxe_dev *rxe = to_rdev(ibdev);
172 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
176 return IB_LINK_LAYER_ETHERNET;
179 rxe_err_dev(rxe, "returned err = %d\n", err);
183 static int rxe_port_immutable(struct ib_device *ibdev, u32 port_num,
184 struct ib_port_immutable *immutable)
186 struct rxe_dev *rxe = to_rdev(ibdev);
187 struct ib_port_attr attr = {};
192 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
196 err = ib_query_port(ibdev, port_num, &attr);
200 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
201 immutable->pkey_tbl_len = attr.pkey_tbl_len;
202 immutable->gid_tbl_len = attr.gid_tbl_len;
203 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
208 rxe_err_dev(rxe, "returned err = %d\n", err);
213 static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
215 struct rxe_dev *rxe = to_rdev(ibuc->device);
216 struct rxe_ucontext *uc = to_ruc(ibuc);
219 err = rxe_add_to_pool(&rxe->uc_pool, uc);
221 rxe_err_dev(rxe, "unable to create uc\n");
226 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
228 struct rxe_ucontext *uc = to_ruc(ibuc);
231 err = rxe_cleanup(uc);
233 rxe_err_uc(uc, "cleanup failed, err = %d\n", err);
237 static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
239 struct rxe_dev *rxe = to_rdev(ibpd->device);
240 struct rxe_pd *pd = to_rpd(ibpd);
243 err = rxe_add_to_pool(&rxe->pd_pool, pd);
245 rxe_dbg_dev(rxe, "unable to alloc pd\n");
252 rxe_err_dev(rxe, "returned err = %d\n", err);
256 static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
258 struct rxe_pd *pd = to_rpd(ibpd);
261 err = rxe_cleanup(pd);
263 rxe_err_pd(pd, "cleanup failed, err = %d\n", err);
269 static int rxe_create_ah(struct ib_ah *ibah,
270 struct rdma_ah_init_attr *init_attr,
271 struct ib_udata *udata)
273 struct rxe_dev *rxe = to_rdev(ibah->device);
274 struct rxe_ah *ah = to_rah(ibah);
275 struct rxe_create_ah_resp __user *uresp = NULL;
276 int err, cleanup_err;
279 /* test if new user provider */
280 if (udata->outlen >= sizeof(*uresp))
281 uresp = udata->outbuf;
287 err = rxe_add_to_pool_ah(&rxe->ah_pool, ah,
288 init_attr->flags & RDMA_CREATE_AH_SLEEPABLE);
290 rxe_dbg_dev(rxe, "unable to create ah\n");
294 /* create index > 0 */
295 ah->ah_num = ah->elem.index;
297 err = rxe_ah_chk_attr(ah, init_attr->ah_attr);
299 rxe_dbg_ah(ah, "bad attr\n");
304 /* only if new user provider */
305 err = copy_to_user(&uresp->ah_num, &ah->ah_num,
306 sizeof(uresp->ah_num));
309 rxe_dbg_ah(ah, "unable to copy to user\n");
312 } else if (ah->is_user) {
313 /* only if old user provider */
317 rxe_init_av(init_attr->ah_attr, &ah->av);
323 cleanup_err = rxe_cleanup(ah);
325 rxe_err_ah(ah, "cleanup failed, err = %d\n", cleanup_err);
327 rxe_err_ah(ah, "returned err = %d\n", err);
331 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
333 struct rxe_ah *ah = to_rah(ibah);
336 err = rxe_ah_chk_attr(ah, attr);
338 rxe_dbg_ah(ah, "bad attr\n");
342 rxe_init_av(attr, &ah->av);
347 rxe_err_ah(ah, "returned err = %d\n", err);
351 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
353 struct rxe_ah *ah = to_rah(ibah);
355 memset(attr, 0, sizeof(*attr));
356 attr->type = ibah->type;
357 rxe_av_to_attr(&ah->av, attr);
362 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
364 struct rxe_ah *ah = to_rah(ibah);
367 err = rxe_cleanup_ah(ah, flags & RDMA_DESTROY_AH_SLEEPABLE);
369 rxe_err_ah(ah, "cleanup failed, err = %d\n", err);
375 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
376 struct ib_udata *udata)
378 struct rxe_dev *rxe = to_rdev(ibsrq->device);
379 struct rxe_pd *pd = to_rpd(ibsrq->pd);
380 struct rxe_srq *srq = to_rsrq(ibsrq);
381 struct rxe_create_srq_resp __user *uresp = NULL;
382 int err, cleanup_err;
385 if (udata->outlen < sizeof(*uresp)) {
387 rxe_err_dev(rxe, "malformed udata\n");
390 uresp = udata->outbuf;
393 if (init->srq_type != IB_SRQT_BASIC) {
395 rxe_dbg_dev(rxe, "srq type = %d, not supported\n",
400 err = rxe_srq_chk_init(rxe, init);
402 rxe_dbg_dev(rxe, "invalid init attributes\n");
406 err = rxe_add_to_pool(&rxe->srq_pool, srq);
408 rxe_dbg_dev(rxe, "unable to create srq, err = %d\n", err);
415 err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
417 rxe_dbg_srq(srq, "create srq failed, err = %d\n", err);
424 cleanup_err = rxe_cleanup(srq);
426 rxe_err_srq(srq, "cleanup failed, err = %d\n", cleanup_err);
428 rxe_err_dev(rxe, "returned err = %d\n", err);
432 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
433 enum ib_srq_attr_mask mask,
434 struct ib_udata *udata)
436 struct rxe_srq *srq = to_rsrq(ibsrq);
437 struct rxe_dev *rxe = to_rdev(ibsrq->device);
438 struct rxe_modify_srq_cmd cmd = {};
442 if (udata->inlen < sizeof(cmd)) {
444 rxe_dbg_srq(srq, "malformed udata\n");
448 err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
451 rxe_dbg_srq(srq, "unable to read udata\n");
456 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
458 rxe_dbg_srq(srq, "bad init attributes\n");
462 err = rxe_srq_from_attr(rxe, srq, attr, mask, &cmd, udata);
464 rxe_dbg_srq(srq, "bad attr\n");
471 rxe_err_srq(srq, "returned err = %d\n", err);
475 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
477 struct rxe_srq *srq = to_rsrq(ibsrq);
482 rxe_dbg_srq(srq, "srq in error state\n");
486 attr->max_wr = srq->rq.queue->buf->index_mask;
487 attr->max_sge = srq->rq.max_sge;
488 attr->srq_limit = srq->limit;
492 rxe_err_srq(srq, "returned err = %d\n", err);
496 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
497 const struct ib_recv_wr **bad_wr)
500 struct rxe_srq *srq = to_rsrq(ibsrq);
503 spin_lock_irqsave(&srq->rq.producer_lock, flags);
506 err = post_one_recv(&srq->rq, wr);
512 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
516 rxe_err_srq(srq, "returned err = %d\n", err);
522 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
524 struct rxe_srq *srq = to_rsrq(ibsrq);
527 err = rxe_cleanup(srq);
529 rxe_err_srq(srq, "cleanup failed, err = %d\n", err);
535 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init,
536 struct ib_udata *udata)
538 struct rxe_dev *rxe = to_rdev(ibqp->device);
539 struct rxe_pd *pd = to_rpd(ibqp->pd);
540 struct rxe_qp *qp = to_rqp(ibqp);
541 struct rxe_create_qp_resp __user *uresp = NULL;
542 int err, cleanup_err;
547 rxe_dbg_dev(rxe, "malformed udata, err = %d\n", err);
551 if (udata->outlen < sizeof(*uresp)) {
553 rxe_dbg_dev(rxe, "malformed udata, err = %d\n", err);
558 uresp = udata->outbuf;
563 if (init->create_flags) {
565 rxe_dbg_dev(rxe, "unsupported create_flags, err = %d\n", err);
569 err = rxe_qp_chk_init(rxe, init);
571 rxe_dbg_dev(rxe, "bad init attr, err = %d\n", err);
575 err = rxe_add_to_pool(&rxe->qp_pool, qp);
577 rxe_dbg_dev(rxe, "unable to create qp, err = %d\n", err);
581 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata);
583 rxe_dbg_qp(qp, "create qp failed, err = %d\n", err);
591 cleanup_err = rxe_cleanup(qp);
593 rxe_err_qp(qp, "cleanup failed, err = %d\n", cleanup_err);
595 rxe_err_dev(rxe, "returned err = %d\n", err);
599 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
600 int mask, struct ib_udata *udata)
602 struct rxe_dev *rxe = to_rdev(ibqp->device);
603 struct rxe_qp *qp = to_rqp(ibqp);
606 if (mask & ~IB_QP_ATTR_STANDARD_BITS) {
608 rxe_dbg_qp(qp, "unsupported mask = 0x%x, err = %d\n",
613 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
615 rxe_dbg_qp(qp, "bad mask/attr, err = %d\n", err);
619 err = rxe_qp_from_attr(qp, attr, mask, udata);
621 rxe_dbg_qp(qp, "modify qp failed, err = %d\n", err);
625 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH))
626 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label,
628 qp->attr.dest_qp_num);
633 rxe_err_qp(qp, "returned err = %d\n", err);
637 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
638 int mask, struct ib_qp_init_attr *init)
640 struct rxe_qp *qp = to_rqp(ibqp);
642 rxe_qp_to_init(qp, init);
643 rxe_qp_to_attr(qp, attr, mask);
648 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
650 struct rxe_qp *qp = to_rqp(ibqp);
653 err = rxe_qp_chk_destroy(qp);
655 rxe_dbg_qp(qp, "unable to destroy qp, err = %d\n", err);
659 err = rxe_cleanup(qp);
661 rxe_err_qp(qp, "cleanup failed, err = %d\n", err);
666 rxe_err_qp(qp, "returned err = %d\n", err);
672 /* sanity check incoming send work request */
673 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
674 unsigned int *maskp, unsigned int *lengthp)
676 int num_sge = ibwr->num_sge;
677 struct rxe_sq *sq = &qp->sq;
678 unsigned int mask = 0;
679 unsigned long length = 0;
684 mask = wr_opcode_mask(ibwr->opcode, qp);
686 rxe_err_qp(qp, "bad wr opcode for qp type\n");
690 if (num_sge > sq->max_sge) {
691 rxe_err_qp(qp, "num_sge > max_sge\n");
696 for (i = 0; i < ibwr->num_sge; i++)
697 length += ibwr->sg_list[i].length;
699 if (length > (1UL << 31)) {
700 rxe_err_qp(qp, "message length too long\n");
704 if (mask & WR_ATOMIC_MASK) {
706 rxe_err_qp(qp, "atomic length != 8\n");
709 if (atomic_wr(ibwr)->remote_addr & 0x7) {
710 rxe_err_qp(qp, "misaligned atomic address\n");
714 if (ibwr->send_flags & IB_SEND_INLINE) {
715 if (!(mask & WR_INLINE_MASK)) {
716 rxe_err_qp(qp, "opcode doesn't support inline data\n");
719 if (length > sq->max_inline) {
720 rxe_err_qp(qp, "inline length too big\n");
729 *lengthp = (int)length;
734 static int init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
735 const struct ib_send_wr *ibwr)
737 wr->wr_id = ibwr->wr_id;
738 wr->opcode = ibwr->opcode;
739 wr->send_flags = ibwr->send_flags;
741 if (qp_type(qp) == IB_QPT_UD ||
742 qp_type(qp) == IB_QPT_GSI) {
743 struct ib_ah *ibah = ud_wr(ibwr)->ah;
745 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
746 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
747 wr->wr.ud.ah_num = to_rah(ibah)->ah_num;
748 if (qp_type(qp) == IB_QPT_GSI)
749 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
751 switch (wr->opcode) {
752 case IB_WR_SEND_WITH_IMM:
753 wr->ex.imm_data = ibwr->ex.imm_data;
758 rxe_err_qp(qp, "bad wr opcode %d for UD/GSI QP\n",
763 switch (wr->opcode) {
764 case IB_WR_RDMA_WRITE_WITH_IMM:
765 wr->ex.imm_data = ibwr->ex.imm_data;
767 case IB_WR_RDMA_READ:
768 case IB_WR_RDMA_WRITE:
769 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
770 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
772 case IB_WR_SEND_WITH_IMM:
773 wr->ex.imm_data = ibwr->ex.imm_data;
775 case IB_WR_SEND_WITH_INV:
776 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
778 case IB_WR_RDMA_READ_WITH_INV:
779 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
780 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
781 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
783 case IB_WR_ATOMIC_CMP_AND_SWP:
784 case IB_WR_ATOMIC_FETCH_AND_ADD:
785 wr->wr.atomic.remote_addr =
786 atomic_wr(ibwr)->remote_addr;
787 wr->wr.atomic.compare_add =
788 atomic_wr(ibwr)->compare_add;
789 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
790 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
792 case IB_WR_LOCAL_INV:
793 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
796 wr->wr.reg.mr = reg_wr(ibwr)->mr;
797 wr->wr.reg.key = reg_wr(ibwr)->key;
798 wr->wr.reg.access = reg_wr(ibwr)->access;
803 case IB_WR_ATOMIC_WRITE:
806 rxe_err_qp(qp, "unsupported wr opcode %d\n",
815 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe,
816 const struct ib_send_wr *ibwr)
818 struct ib_sge *sge = ibwr->sg_list;
819 u8 *p = wqe->dma.inline_data;
822 for (i = 0; i < ibwr->num_sge; i++, sge++) {
823 memcpy(p, ib_virt_dma_to_ptr(sge->addr), sge->length);
828 static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
829 unsigned int mask, unsigned int length,
830 struct rxe_send_wqe *wqe)
832 int num_sge = ibwr->num_sge;
835 err = init_send_wr(qp, &wqe->wr, ibwr);
839 /* local operation */
840 if (unlikely(mask & WR_LOCAL_OP_MASK)) {
842 wqe->state = wqe_state_posted;
846 if (unlikely(ibwr->send_flags & IB_SEND_INLINE))
847 copy_inline_data_to_wqe(wqe, ibwr);
849 memcpy(wqe->dma.sge, ibwr->sg_list,
850 num_sge * sizeof(struct ib_sge));
852 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
853 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
855 wqe->dma.length = length;
856 wqe->dma.resid = length;
857 wqe->dma.num_sge = num_sge;
858 wqe->dma.cur_sge = 0;
859 wqe->dma.sge_offset = 0;
860 wqe->state = wqe_state_posted;
861 wqe->ssn = atomic_add_return(1, &qp->ssn);
866 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr)
869 struct rxe_sq *sq = &qp->sq;
870 struct rxe_send_wqe *send_wqe;
875 err = validate_send_wr(qp, ibwr, &mask, &length);
879 full = queue_full(sq->queue, QUEUE_TYPE_FROM_ULP);
880 if (unlikely(full)) {
881 rxe_err_qp(qp, "send queue full\n");
885 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_FROM_ULP);
886 err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
888 queue_advance_producer(sq->queue, QUEUE_TYPE_FROM_ULP);
893 static int rxe_post_send_kernel(struct rxe_qp *qp,
894 const struct ib_send_wr *ibwr,
895 const struct ib_send_wr **bad_wr)
901 spin_lock_irqsave(&qp->sq.sq_lock, flags);
903 err = post_one_send(qp, ibwr);
912 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
914 /* kickoff processing of any posted wqes */
916 rxe_sched_task(&qp->send_task);
921 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
922 const struct ib_send_wr **bad_wr)
924 struct rxe_qp *qp = to_rqp(ibqp);
928 spin_lock_irqsave(&qp->state_lock, flags);
929 /* caller has already called destroy_qp */
930 if (WARN_ON_ONCE(!qp->valid)) {
931 spin_unlock_irqrestore(&qp->state_lock, flags);
932 rxe_err_qp(qp, "qp has been destroyed\n");
936 if (unlikely(qp_state(qp) < IB_QPS_RTS)) {
937 spin_unlock_irqrestore(&qp->state_lock, flags);
939 rxe_err_qp(qp, "qp not ready to send\n");
942 spin_unlock_irqrestore(&qp->state_lock, flags);
945 /* Utilize process context to do protocol processing */
946 rxe_sched_task(&qp->send_task);
948 err = rxe_post_send_kernel(qp, wr, bad_wr);
957 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
960 unsigned long length;
961 struct rxe_recv_wqe *recv_wqe;
962 int num_sge = ibwr->num_sge;
966 full = queue_full(rq->queue, QUEUE_TYPE_FROM_ULP);
967 if (unlikely(full)) {
969 rxe_dbg("queue full\n");
973 if (unlikely(num_sge > rq->max_sge)) {
975 rxe_dbg("bad num_sge > max_sge\n");
980 for (i = 0; i < num_sge; i++)
981 length += ibwr->sg_list[i].length;
983 /* IBA max message size is 2^31 */
984 if (length >= (1UL<<31)) {
986 rxe_dbg("message length too long\n");
990 recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_FROM_ULP);
992 recv_wqe->wr_id = ibwr->wr_id;
993 recv_wqe->dma.length = length;
994 recv_wqe->dma.resid = length;
995 recv_wqe->dma.num_sge = num_sge;
996 recv_wqe->dma.cur_sge = 0;
997 recv_wqe->dma.sge_offset = 0;
998 memcpy(recv_wqe->dma.sge, ibwr->sg_list,
999 num_sge * sizeof(struct ib_sge));
1001 queue_advance_producer(rq->queue, QUEUE_TYPE_FROM_ULP);
1006 rxe_dbg("returned err = %d\n", err);
1010 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1011 const struct ib_recv_wr **bad_wr)
1014 struct rxe_qp *qp = to_rqp(ibqp);
1015 struct rxe_rq *rq = &qp->rq;
1016 unsigned long flags;
1018 spin_lock_irqsave(&qp->state_lock, flags);
1019 /* caller has already called destroy_qp */
1020 if (WARN_ON_ONCE(!qp->valid)) {
1021 spin_unlock_irqrestore(&qp->state_lock, flags);
1022 rxe_err_qp(qp, "qp has been destroyed\n");
1026 /* see C10-97.2.1 */
1027 if (unlikely((qp_state(qp) < IB_QPS_INIT))) {
1028 spin_unlock_irqrestore(&qp->state_lock, flags);
1030 rxe_dbg_qp(qp, "qp not ready to post recv\n");
1033 spin_unlock_irqrestore(&qp->state_lock, flags);
1035 if (unlikely(qp->srq)) {
1037 rxe_dbg_qp(qp, "qp has srq, use post_srq_recv instead\n");
1041 spin_lock_irqsave(&rq->producer_lock, flags);
1044 err = post_one_recv(rq, wr);
1045 if (unlikely(err)) {
1052 spin_unlock_irqrestore(&rq->producer_lock, flags);
1054 spin_lock_irqsave(&qp->state_lock, flags);
1055 if (qp_state(qp) == IB_QPS_ERR)
1056 rxe_sched_task(&qp->recv_task);
1057 spin_unlock_irqrestore(&qp->state_lock, flags);
1063 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
1064 struct uverbs_attr_bundle *attrs)
1066 struct ib_udata *udata = &attrs->driver_udata;
1067 struct ib_device *dev = ibcq->device;
1068 struct rxe_dev *rxe = to_rdev(dev);
1069 struct rxe_cq *cq = to_rcq(ibcq);
1070 struct rxe_create_cq_resp __user *uresp = NULL;
1071 int err, cleanup_err;
1074 if (udata->outlen < sizeof(*uresp)) {
1076 rxe_dbg_dev(rxe, "malformed udata, err = %d\n", err);
1079 uresp = udata->outbuf;
1084 rxe_dbg_dev(rxe, "bad attr->flags, err = %d\n", err);
1088 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
1090 rxe_dbg_dev(rxe, "bad init attributes, err = %d\n", err);
1094 err = rxe_add_to_pool(&rxe->cq_pool, cq);
1096 rxe_dbg_dev(rxe, "unable to create cq, err = %d\n", err);
1100 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
1103 rxe_dbg_cq(cq, "create cq failed, err = %d\n", err);
1110 cleanup_err = rxe_cleanup(cq);
1112 rxe_err_cq(cq, "cleanup failed, err = %d\n", cleanup_err);
1114 rxe_err_dev(rxe, "returned err = %d\n", err);
1118 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
1120 struct rxe_cq *cq = to_rcq(ibcq);
1121 struct rxe_dev *rxe = to_rdev(ibcq->device);
1122 struct rxe_resize_cq_resp __user *uresp = NULL;
1126 if (udata->outlen < sizeof(*uresp)) {
1128 rxe_dbg_cq(cq, "malformed udata\n");
1131 uresp = udata->outbuf;
1134 err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
1136 rxe_dbg_cq(cq, "bad attr, err = %d\n", err);
1140 err = rxe_cq_resize_queue(cq, cqe, uresp, udata);
1142 rxe_dbg_cq(cq, "resize cq failed, err = %d\n", err);
1149 rxe_err_cq(cq, "returned err = %d\n", err);
1153 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
1156 struct rxe_cq *cq = to_rcq(ibcq);
1157 struct rxe_cqe *cqe;
1158 unsigned long flags;
1160 spin_lock_irqsave(&cq->cq_lock, flags);
1161 for (i = 0; i < num_entries; i++) {
1162 cqe = queue_head(cq->queue, QUEUE_TYPE_TO_ULP);
1164 break; /* queue empty */
1166 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
1167 queue_advance_consumer(cq->queue, QUEUE_TYPE_TO_ULP);
1169 spin_unlock_irqrestore(&cq->cq_lock, flags);
1174 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
1176 struct rxe_cq *cq = to_rcq(ibcq);
1179 count = queue_count(cq->queue, QUEUE_TYPE_TO_ULP);
1181 return (count > wc_cnt) ? wc_cnt : count;
1184 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
1186 struct rxe_cq *cq = to_rcq(ibcq);
1189 unsigned long irq_flags;
1191 spin_lock_irqsave(&cq->cq_lock, irq_flags);
1192 cq->notify |= flags & IB_CQ_SOLICITED_MASK;
1193 empty = queue_empty(cq->queue, QUEUE_TYPE_TO_ULP);
1195 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty)
1198 spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
1203 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
1205 struct rxe_cq *cq = to_rcq(ibcq);
1208 /* See IBA C11-17: The CI shall return an error if this Verb is
1209 * invoked while a Work Queue is still associated with the CQ.
1211 if (atomic_read(&cq->num_wq)) {
1213 rxe_dbg_cq(cq, "still in use\n");
1217 err = rxe_cleanup(cq);
1219 rxe_err_cq(cq, "cleanup failed, err = %d\n", err);
1224 rxe_err_cq(cq, "returned err = %d\n", err);
1229 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
1231 struct rxe_dev *rxe = to_rdev(ibpd->device);
1232 struct rxe_pd *pd = to_rpd(ibpd);
1236 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1238 return ERR_PTR(-ENOMEM);
1240 err = rxe_add_to_pool(&rxe->mr_pool, mr);
1242 rxe_dbg_dev(rxe, "unable to create mr\n");
1248 mr->ibmr.device = ibpd->device;
1250 rxe_mr_init_dma(access, mr);
1256 rxe_err_pd(pd, "returned err = %d\n", err);
1257 return ERR_PTR(err);
1260 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, u64 start,
1261 u64 length, u64 iova, int access,
1262 struct ib_udata *udata)
1264 struct rxe_dev *rxe = to_rdev(ibpd->device);
1265 struct rxe_pd *pd = to_rpd(ibpd);
1267 int err, cleanup_err;
1269 if (access & ~RXE_ACCESS_SUPPORTED_MR) {
1270 rxe_err_pd(pd, "access = %#x not supported (%#x)\n", access,
1271 RXE_ACCESS_SUPPORTED_MR);
1272 return ERR_PTR(-EOPNOTSUPP);
1275 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1277 return ERR_PTR(-ENOMEM);
1279 err = rxe_add_to_pool(&rxe->mr_pool, mr);
1281 rxe_dbg_pd(pd, "unable to create mr\n");
1287 mr->ibmr.device = ibpd->device;
1289 err = rxe_mr_init_user(rxe, start, length, access, mr);
1291 rxe_dbg_mr(mr, "reg_user_mr failed, err = %d\n", err);
1299 cleanup_err = rxe_cleanup(mr);
1301 rxe_err_mr(mr, "cleanup failed, err = %d\n", cleanup_err);
1304 rxe_err_pd(pd, "returned err = %d\n", err);
1305 return ERR_PTR(err);
1308 static struct ib_mr *rxe_rereg_user_mr(struct ib_mr *ibmr, int flags,
1309 u64 start, u64 length, u64 iova,
1310 int access, struct ib_pd *ibpd,
1311 struct ib_udata *udata)
1313 struct rxe_mr *mr = to_rmr(ibmr);
1314 struct rxe_pd *old_pd = to_rpd(ibmr->pd);
1315 struct rxe_pd *pd = to_rpd(ibpd);
1317 /* for now only support the two easy cases:
1318 * rereg_pd and rereg_access
1320 if (flags & ~RXE_MR_REREG_SUPPORTED) {
1321 rxe_err_mr(mr, "flags = %#x not supported\n", flags);
1322 return ERR_PTR(-EOPNOTSUPP);
1325 if (flags & IB_MR_REREG_PD) {
1331 if (flags & IB_MR_REREG_ACCESS) {
1332 if (access & ~RXE_ACCESS_SUPPORTED_MR) {
1333 rxe_err_mr(mr, "access = %#x not supported\n", access);
1334 return ERR_PTR(-EOPNOTSUPP);
1336 mr->access = access;
1342 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
1345 struct rxe_dev *rxe = to_rdev(ibpd->device);
1346 struct rxe_pd *pd = to_rpd(ibpd);
1348 int err, cleanup_err;
1350 if (mr_type != IB_MR_TYPE_MEM_REG) {
1352 rxe_dbg_pd(pd, "mr type %d not supported, err = %d\n",
1357 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1359 return ERR_PTR(-ENOMEM);
1361 err = rxe_add_to_pool(&rxe->mr_pool, mr);
1367 mr->ibmr.device = ibpd->device;
1369 err = rxe_mr_init_fast(max_num_sg, mr);
1371 rxe_dbg_mr(mr, "alloc_mr failed, err = %d\n", err);
1379 cleanup_err = rxe_cleanup(mr);
1381 rxe_err_mr(mr, "cleanup failed, err = %d\n", err);
1385 rxe_err_pd(pd, "returned err = %d\n", err);
1386 return ERR_PTR(err);
1389 static int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
1391 struct rxe_mr *mr = to_rmr(ibmr);
1392 int err, cleanup_err;
1394 /* See IBA 10.6.7.2.6 */
1395 if (atomic_read(&mr->num_mw) > 0) {
1397 rxe_dbg_mr(mr, "mr has mw's bound\n");
1401 cleanup_err = rxe_cleanup(mr);
1403 rxe_err_mr(mr, "cleanup failed, err = %d\n", cleanup_err);
1405 kfree_rcu_mightsleep(mr);
1409 rxe_err_mr(mr, "returned err = %d\n", err);
1413 static ssize_t parent_show(struct device *device,
1414 struct device_attribute *attr, char *buf)
1416 struct rxe_dev *rxe =
1417 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
1419 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1));
1422 static DEVICE_ATTR_RO(parent);
1424 static struct attribute *rxe_dev_attributes[] = {
1425 &dev_attr_parent.attr,
1429 static const struct attribute_group rxe_attr_group = {
1430 .attrs = rxe_dev_attributes,
1433 static int rxe_enable_driver(struct ib_device *ib_dev)
1435 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1436 struct net_device *ndev;
1438 ndev = rxe_ib_device_get_netdev(ib_dev);
1442 rxe_set_port_state(rxe);
1443 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(ndev));
1449 static const struct ib_device_ops rxe_dev_ops = {
1450 .owner = THIS_MODULE,
1451 .driver_id = RDMA_DRIVER_RXE,
1452 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
1454 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats,
1455 .alloc_mr = rxe_alloc_mr,
1456 .alloc_mw = rxe_alloc_mw,
1457 .alloc_pd = rxe_alloc_pd,
1458 .alloc_ucontext = rxe_alloc_ucontext,
1459 .attach_mcast = rxe_attach_mcast,
1460 .create_ah = rxe_create_ah,
1461 .create_cq = rxe_create_cq,
1462 .create_qp = rxe_create_qp,
1463 .create_srq = rxe_create_srq,
1464 .create_user_ah = rxe_create_ah,
1465 .dealloc_driver = rxe_dealloc,
1466 .dealloc_mw = rxe_dealloc_mw,
1467 .dealloc_pd = rxe_dealloc_pd,
1468 .dealloc_ucontext = rxe_dealloc_ucontext,
1469 .dereg_mr = rxe_dereg_mr,
1470 .destroy_ah = rxe_destroy_ah,
1471 .destroy_cq = rxe_destroy_cq,
1472 .destroy_qp = rxe_destroy_qp,
1473 .destroy_srq = rxe_destroy_srq,
1474 .detach_mcast = rxe_detach_mcast,
1475 .device_group = &rxe_attr_group,
1476 .enable_driver = rxe_enable_driver,
1477 .get_dma_mr = rxe_get_dma_mr,
1478 .get_hw_stats = rxe_ib_get_hw_stats,
1479 .get_link_layer = rxe_get_link_layer,
1480 .get_port_immutable = rxe_port_immutable,
1481 .map_mr_sg = rxe_map_mr_sg,
1483 .modify_ah = rxe_modify_ah,
1484 .modify_device = rxe_modify_device,
1485 .modify_port = rxe_modify_port,
1486 .modify_qp = rxe_modify_qp,
1487 .modify_srq = rxe_modify_srq,
1488 .peek_cq = rxe_peek_cq,
1489 .poll_cq = rxe_poll_cq,
1490 .post_recv = rxe_post_recv,
1491 .post_send = rxe_post_send,
1492 .post_srq_recv = rxe_post_srq_recv,
1493 .query_ah = rxe_query_ah,
1494 .query_device = rxe_query_device,
1495 .query_pkey = rxe_query_pkey,
1496 .query_port = rxe_query_port,
1497 .query_qp = rxe_query_qp,
1498 .query_srq = rxe_query_srq,
1499 .reg_user_mr = rxe_reg_user_mr,
1500 .req_notify_cq = rxe_req_notify_cq,
1501 .rereg_user_mr = rxe_rereg_user_mr,
1502 .resize_cq = rxe_resize_cq,
1504 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
1505 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
1506 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
1507 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp),
1508 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
1509 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1510 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw),
1513 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name,
1514 struct net_device *ndev)
1517 struct ib_device *dev = &rxe->ib_dev;
1519 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
1521 dev->node_type = RDMA_NODE_IB_CA;
1522 dev->phys_port_cnt = 1;
1523 dev->num_comp_vectors = num_possible_cpus();
1524 dev->local_dma_lkey = 0;
1525 addrconf_addr_eui48((unsigned char *)&dev->node_guid,
1528 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) |
1529 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ);
1531 ib_set_device_ops(dev, &rxe_dev_ops);
1532 err = ib_device_set_netdev(&rxe->ib_dev, ndev, 1);
1536 err = rxe_icrc_init(rxe);
1540 err = ib_register_device(dev, ibdev_name, NULL);
1542 rxe_dbg_dev(rxe, "failed with error %d\n", err);
1545 * Note that rxe may be invalid at this point if another thread