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 attr->state = ib_get_curr_port_state(ndev);
66 if (attr->state == IB_PORT_ACTIVE)
67 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
68 else if (dev_get_flags(ndev) & IFF_UP)
69 attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
71 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
73 mutex_unlock(&rxe->usdev_lock);
79 rxe_err_dev(rxe, "returned err = %d\n", err);
83 static int rxe_query_pkey(struct ib_device *ibdev,
84 u32 port_num, u16 index, u16 *pkey)
86 struct rxe_dev *rxe = to_rdev(ibdev);
91 rxe_dbg_dev(rxe, "bad pkey index = %d\n", index);
95 *pkey = IB_DEFAULT_PKEY_FULL;
99 rxe_err_dev(rxe, "returned err = %d\n", err);
103 static int rxe_modify_device(struct ib_device *ibdev,
104 int mask, struct ib_device_modify *attr)
106 struct rxe_dev *rxe = to_rdev(ibdev);
109 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
110 IB_DEVICE_MODIFY_NODE_DESC)) {
112 rxe_dbg_dev(rxe, "unsupported mask = 0x%x\n", mask);
116 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
117 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
119 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
120 memcpy(rxe->ib_dev.node_desc,
121 attr->node_desc, sizeof(rxe->ib_dev.node_desc));
127 rxe_err_dev(rxe, "returned err = %d\n", err);
131 static int rxe_modify_port(struct ib_device *ibdev, u32 port_num,
132 int mask, struct ib_port_modify *attr)
134 struct rxe_dev *rxe = to_rdev(ibdev);
135 struct rxe_port *port;
140 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
144 //TODO is shutdown useful
145 if (mask & ~(IB_PORT_RESET_QKEY_CNTR)) {
147 rxe_dbg_dev(rxe, "unsupported mask = 0x%x\n", mask);
152 port->attr.port_cap_flags |= attr->set_port_cap_mask;
153 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
155 if (mask & IB_PORT_RESET_QKEY_CNTR)
156 port->attr.qkey_viol_cntr = 0;
161 rxe_err_dev(rxe, "returned err = %d\n", err);
165 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *ibdev,
168 struct rxe_dev *rxe = to_rdev(ibdev);
173 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
177 return IB_LINK_LAYER_ETHERNET;
180 rxe_err_dev(rxe, "returned err = %d\n", err);
184 static int rxe_port_immutable(struct ib_device *ibdev, u32 port_num,
185 struct ib_port_immutable *immutable)
187 struct rxe_dev *rxe = to_rdev(ibdev);
188 struct ib_port_attr attr = {};
193 rxe_dbg_dev(rxe, "bad port_num = %d\n", port_num);
197 err = ib_query_port(ibdev, port_num, &attr);
201 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
202 immutable->pkey_tbl_len = attr.pkey_tbl_len;
203 immutable->gid_tbl_len = attr.gid_tbl_len;
204 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
209 rxe_err_dev(rxe, "returned err = %d\n", err);
214 static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
216 struct rxe_dev *rxe = to_rdev(ibuc->device);
217 struct rxe_ucontext *uc = to_ruc(ibuc);
220 err = rxe_add_to_pool(&rxe->uc_pool, uc);
222 rxe_err_dev(rxe, "unable to create uc\n");
227 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
229 struct rxe_ucontext *uc = to_ruc(ibuc);
232 err = rxe_cleanup(uc);
234 rxe_err_uc(uc, "cleanup failed, err = %d\n", err);
238 static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
240 struct rxe_dev *rxe = to_rdev(ibpd->device);
241 struct rxe_pd *pd = to_rpd(ibpd);
244 err = rxe_add_to_pool(&rxe->pd_pool, pd);
246 rxe_dbg_dev(rxe, "unable to alloc pd\n");
253 rxe_err_dev(rxe, "returned err = %d\n", err);
257 static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
259 struct rxe_pd *pd = to_rpd(ibpd);
262 err = rxe_cleanup(pd);
264 rxe_err_pd(pd, "cleanup failed, err = %d\n", err);
270 static int rxe_create_ah(struct ib_ah *ibah,
271 struct rdma_ah_init_attr *init_attr,
272 struct ib_udata *udata)
274 struct rxe_dev *rxe = to_rdev(ibah->device);
275 struct rxe_ah *ah = to_rah(ibah);
276 struct rxe_create_ah_resp __user *uresp = NULL;
277 int err, cleanup_err;
280 /* test if new user provider */
281 if (udata->outlen >= sizeof(*uresp))
282 uresp = udata->outbuf;
288 err = rxe_add_to_pool_ah(&rxe->ah_pool, ah,
289 init_attr->flags & RDMA_CREATE_AH_SLEEPABLE);
291 rxe_dbg_dev(rxe, "unable to create ah\n");
295 /* create index > 0 */
296 ah->ah_num = ah->elem.index;
298 err = rxe_ah_chk_attr(ah, init_attr->ah_attr);
300 rxe_dbg_ah(ah, "bad attr\n");
305 /* only if new user provider */
306 err = copy_to_user(&uresp->ah_num, &ah->ah_num,
307 sizeof(uresp->ah_num));
310 rxe_dbg_ah(ah, "unable to copy to user\n");
313 } else if (ah->is_user) {
314 /* only if old user provider */
318 rxe_init_av(init_attr->ah_attr, &ah->av);
324 cleanup_err = rxe_cleanup(ah);
326 rxe_err_ah(ah, "cleanup failed, err = %d\n", cleanup_err);
328 rxe_err_ah(ah, "returned err = %d\n", err);
332 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
334 struct rxe_ah *ah = to_rah(ibah);
337 err = rxe_ah_chk_attr(ah, attr);
339 rxe_dbg_ah(ah, "bad attr\n");
343 rxe_init_av(attr, &ah->av);
348 rxe_err_ah(ah, "returned err = %d\n", err);
352 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
354 struct rxe_ah *ah = to_rah(ibah);
356 memset(attr, 0, sizeof(*attr));
357 attr->type = ibah->type;
358 rxe_av_to_attr(&ah->av, attr);
363 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
365 struct rxe_ah *ah = to_rah(ibah);
368 err = rxe_cleanup_ah(ah, flags & RDMA_DESTROY_AH_SLEEPABLE);
370 rxe_err_ah(ah, "cleanup failed, err = %d\n", err);
376 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
377 struct ib_udata *udata)
379 struct rxe_dev *rxe = to_rdev(ibsrq->device);
380 struct rxe_pd *pd = to_rpd(ibsrq->pd);
381 struct rxe_srq *srq = to_rsrq(ibsrq);
382 struct rxe_create_srq_resp __user *uresp = NULL;
383 int err, cleanup_err;
386 if (udata->outlen < sizeof(*uresp)) {
388 rxe_err_dev(rxe, "malformed udata\n");
391 uresp = udata->outbuf;
394 if (init->srq_type != IB_SRQT_BASIC) {
396 rxe_dbg_dev(rxe, "srq type = %d, not supported\n",
401 err = rxe_srq_chk_init(rxe, init);
403 rxe_dbg_dev(rxe, "invalid init attributes\n");
407 err = rxe_add_to_pool(&rxe->srq_pool, srq);
409 rxe_dbg_dev(rxe, "unable to create srq, err = %d\n", err);
416 err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
418 rxe_dbg_srq(srq, "create srq failed, err = %d\n", err);
425 cleanup_err = rxe_cleanup(srq);
427 rxe_err_srq(srq, "cleanup failed, err = %d\n", cleanup_err);
429 rxe_err_dev(rxe, "returned err = %d\n", err);
433 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
434 enum ib_srq_attr_mask mask,
435 struct ib_udata *udata)
437 struct rxe_srq *srq = to_rsrq(ibsrq);
438 struct rxe_dev *rxe = to_rdev(ibsrq->device);
439 struct rxe_modify_srq_cmd cmd = {};
443 if (udata->inlen < sizeof(cmd)) {
445 rxe_dbg_srq(srq, "malformed udata\n");
449 err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
452 rxe_dbg_srq(srq, "unable to read udata\n");
457 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
459 rxe_dbg_srq(srq, "bad init attributes\n");
463 err = rxe_srq_from_attr(rxe, srq, attr, mask, &cmd, udata);
465 rxe_dbg_srq(srq, "bad attr\n");
472 rxe_err_srq(srq, "returned err = %d\n", err);
476 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
478 struct rxe_srq *srq = to_rsrq(ibsrq);
483 rxe_dbg_srq(srq, "srq in error state\n");
487 attr->max_wr = srq->rq.queue->buf->index_mask;
488 attr->max_sge = srq->rq.max_sge;
489 attr->srq_limit = srq->limit;
493 rxe_err_srq(srq, "returned err = %d\n", err);
497 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
498 const struct ib_recv_wr **bad_wr)
501 struct rxe_srq *srq = to_rsrq(ibsrq);
504 spin_lock_irqsave(&srq->rq.producer_lock, flags);
507 err = post_one_recv(&srq->rq, wr);
513 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
517 rxe_err_srq(srq, "returned err = %d\n", err);
523 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
525 struct rxe_srq *srq = to_rsrq(ibsrq);
528 err = rxe_cleanup(srq);
530 rxe_err_srq(srq, "cleanup failed, err = %d\n", err);
536 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init,
537 struct ib_udata *udata)
539 struct rxe_dev *rxe = to_rdev(ibqp->device);
540 struct rxe_pd *pd = to_rpd(ibqp->pd);
541 struct rxe_qp *qp = to_rqp(ibqp);
542 struct rxe_create_qp_resp __user *uresp = NULL;
543 int err, cleanup_err;
548 rxe_dbg_dev(rxe, "malformed udata, err = %d\n", err);
552 if (udata->outlen < sizeof(*uresp)) {
554 rxe_dbg_dev(rxe, "malformed udata, err = %d\n", err);
559 uresp = udata->outbuf;
564 if (init->create_flags) {
566 rxe_dbg_dev(rxe, "unsupported create_flags, err = %d\n", err);
570 err = rxe_qp_chk_init(rxe, init);
572 rxe_dbg_dev(rxe, "bad init attr, err = %d\n", err);
576 err = rxe_add_to_pool(&rxe->qp_pool, qp);
578 rxe_dbg_dev(rxe, "unable to create qp, err = %d\n", err);
582 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata);
584 rxe_dbg_qp(qp, "create qp failed, err = %d\n", err);
592 cleanup_err = rxe_cleanup(qp);
594 rxe_err_qp(qp, "cleanup failed, err = %d\n", cleanup_err);
596 rxe_err_dev(rxe, "returned err = %d\n", err);
600 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
601 int mask, struct ib_udata *udata)
603 struct rxe_dev *rxe = to_rdev(ibqp->device);
604 struct rxe_qp *qp = to_rqp(ibqp);
607 if (mask & ~IB_QP_ATTR_STANDARD_BITS) {
609 rxe_dbg_qp(qp, "unsupported mask = 0x%x, err = %d\n",
614 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
616 rxe_dbg_qp(qp, "bad mask/attr, err = %d\n", err);
620 err = rxe_qp_from_attr(qp, attr, mask, udata);
622 rxe_dbg_qp(qp, "modify qp failed, err = %d\n", err);
626 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH))
627 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label,
629 qp->attr.dest_qp_num);
634 rxe_err_qp(qp, "returned err = %d\n", err);
638 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
639 int mask, struct ib_qp_init_attr *init)
641 struct rxe_qp *qp = to_rqp(ibqp);
643 rxe_qp_to_init(qp, init);
644 rxe_qp_to_attr(qp, attr, mask);
649 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
651 struct rxe_qp *qp = to_rqp(ibqp);
654 err = rxe_qp_chk_destroy(qp);
656 rxe_dbg_qp(qp, "unable to destroy qp, err = %d\n", err);
660 err = rxe_cleanup(qp);
662 rxe_err_qp(qp, "cleanup failed, err = %d\n", err);
667 rxe_err_qp(qp, "returned err = %d\n", err);
673 /* sanity check incoming send work request */
674 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
675 unsigned int *maskp, unsigned int *lengthp)
677 int num_sge = ibwr->num_sge;
678 struct rxe_sq *sq = &qp->sq;
679 unsigned int mask = 0;
680 unsigned long length = 0;
685 mask = wr_opcode_mask(ibwr->opcode, qp);
687 rxe_err_qp(qp, "bad wr opcode for qp type\n");
691 if (num_sge > sq->max_sge) {
692 rxe_err_qp(qp, "num_sge > max_sge\n");
697 for (i = 0; i < ibwr->num_sge; i++)
698 length += ibwr->sg_list[i].length;
700 if (length > RXE_PORT_MAX_MSG_SZ) {
701 rxe_err_qp(qp, "message length too long\n");
705 if (mask & WR_ATOMIC_MASK) {
707 rxe_err_qp(qp, "atomic length != 8\n");
710 if (atomic_wr(ibwr)->remote_addr & 0x7) {
711 rxe_err_qp(qp, "misaligned atomic address\n");
715 if (ibwr->send_flags & IB_SEND_INLINE) {
716 if (!(mask & WR_INLINE_MASK)) {
717 rxe_err_qp(qp, "opcode doesn't support inline data\n");
720 if (length > sq->max_inline) {
721 rxe_err_qp(qp, "inline length too big\n");
730 *lengthp = (int)length;
735 static int init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
736 const struct ib_send_wr *ibwr)
738 wr->wr_id = ibwr->wr_id;
739 wr->opcode = ibwr->opcode;
740 wr->send_flags = ibwr->send_flags;
742 if (qp_type(qp) == IB_QPT_UD ||
743 qp_type(qp) == IB_QPT_GSI) {
744 struct ib_ah *ibah = ud_wr(ibwr)->ah;
746 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
747 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
748 wr->wr.ud.ah_num = to_rah(ibah)->ah_num;
749 if (qp_type(qp) == IB_QPT_GSI)
750 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
752 switch (wr->opcode) {
753 case IB_WR_SEND_WITH_IMM:
754 wr->ex.imm_data = ibwr->ex.imm_data;
759 rxe_err_qp(qp, "bad wr opcode %d for UD/GSI QP\n",
764 switch (wr->opcode) {
765 case IB_WR_RDMA_WRITE_WITH_IMM:
766 wr->ex.imm_data = ibwr->ex.imm_data;
768 case IB_WR_RDMA_READ:
769 case IB_WR_RDMA_WRITE:
770 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
771 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
773 case IB_WR_SEND_WITH_IMM:
774 wr->ex.imm_data = ibwr->ex.imm_data;
776 case IB_WR_SEND_WITH_INV:
777 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
779 case IB_WR_RDMA_READ_WITH_INV:
780 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
781 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
782 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
784 case IB_WR_ATOMIC_CMP_AND_SWP:
785 case IB_WR_ATOMIC_FETCH_AND_ADD:
786 wr->wr.atomic.remote_addr =
787 atomic_wr(ibwr)->remote_addr;
788 wr->wr.atomic.compare_add =
789 atomic_wr(ibwr)->compare_add;
790 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
791 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
793 case IB_WR_LOCAL_INV:
794 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
797 wr->wr.reg.mr = reg_wr(ibwr)->mr;
798 wr->wr.reg.key = reg_wr(ibwr)->key;
799 wr->wr.reg.access = reg_wr(ibwr)->access;
804 case IB_WR_ATOMIC_WRITE:
807 rxe_err_qp(qp, "unsupported wr opcode %d\n",
816 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe,
817 const struct ib_send_wr *ibwr)
819 struct ib_sge *sge = ibwr->sg_list;
820 u8 *p = wqe->dma.inline_data;
823 for (i = 0; i < ibwr->num_sge; i++, sge++) {
824 memcpy(p, ib_virt_dma_to_ptr(sge->addr), sge->length);
829 static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
830 unsigned int mask, unsigned int length,
831 struct rxe_send_wqe *wqe)
833 int num_sge = ibwr->num_sge;
836 err = init_send_wr(qp, &wqe->wr, ibwr);
840 /* local operation */
841 if (unlikely(mask & WR_LOCAL_OP_MASK)) {
843 wqe->state = wqe_state_posted;
847 if (unlikely(ibwr->send_flags & IB_SEND_INLINE))
848 copy_inline_data_to_wqe(wqe, ibwr);
850 memcpy(wqe->dma.sge, ibwr->sg_list,
851 num_sge * sizeof(struct ib_sge));
853 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
854 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
856 wqe->dma.length = length;
857 wqe->dma.resid = length;
858 wqe->dma.num_sge = num_sge;
859 wqe->dma.cur_sge = 0;
860 wqe->dma.sge_offset = 0;
861 wqe->state = wqe_state_posted;
862 wqe->ssn = atomic_add_return(1, &qp->ssn);
867 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr)
870 struct rxe_sq *sq = &qp->sq;
871 struct rxe_send_wqe *send_wqe;
876 err = validate_send_wr(qp, ibwr, &mask, &length);
880 full = queue_full(sq->queue, QUEUE_TYPE_FROM_ULP);
881 if (unlikely(full)) {
882 rxe_err_qp(qp, "send queue full\n");
886 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_FROM_ULP);
887 err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
889 queue_advance_producer(sq->queue, QUEUE_TYPE_FROM_ULP);
894 static int rxe_post_send_kernel(struct rxe_qp *qp,
895 const struct ib_send_wr *ibwr,
896 const struct ib_send_wr **bad_wr)
902 spin_lock_irqsave(&qp->sq.sq_lock, flags);
904 err = post_one_send(qp, ibwr);
913 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
915 /* kickoff processing of any posted wqes */
917 rxe_sched_task(&qp->send_task);
922 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
923 const struct ib_send_wr **bad_wr)
925 struct rxe_qp *qp = to_rqp(ibqp);
929 spin_lock_irqsave(&qp->state_lock, flags);
930 /* caller has already called destroy_qp */
931 if (WARN_ON_ONCE(!qp->valid)) {
932 spin_unlock_irqrestore(&qp->state_lock, flags);
933 rxe_err_qp(qp, "qp has been destroyed\n");
937 if (unlikely(qp_state(qp) < IB_QPS_RTS)) {
938 spin_unlock_irqrestore(&qp->state_lock, flags);
940 rxe_err_qp(qp, "qp not ready to send\n");
943 spin_unlock_irqrestore(&qp->state_lock, flags);
946 /* Utilize process context to do protocol processing */
947 rxe_sched_task(&qp->send_task);
949 err = rxe_post_send_kernel(qp, wr, bad_wr);
958 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
961 unsigned long length;
962 struct rxe_recv_wqe *recv_wqe;
963 int num_sge = ibwr->num_sge;
967 full = queue_full(rq->queue, QUEUE_TYPE_FROM_ULP);
968 if (unlikely(full)) {
970 rxe_dbg("queue full\n");
974 if (unlikely(num_sge > rq->max_sge)) {
976 rxe_dbg("bad num_sge > max_sge\n");
981 for (i = 0; i < num_sge; i++)
982 length += ibwr->sg_list[i].length;
984 if (length > RXE_PORT_MAX_MSG_SZ) {
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