2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/dma-mapping.h>
35 #include <net/addrconf.h>
36 #include <rdma/uverbs_ioctl.h>
39 #include "rxe_queue.h"
40 #include "rxe_hw_counters.h"
42 static int rxe_query_device(struct ib_device *dev,
43 struct ib_device_attr *attr,
46 struct rxe_dev *rxe = to_rdev(dev);
48 if (uhw->inlen || uhw->outlen)
55 static int rxe_query_port(struct ib_device *dev,
56 u8 port_num, struct ib_port_attr *attr)
58 struct rxe_dev *rxe = to_rdev(dev);
59 struct rxe_port *port;
64 /* *attr being zeroed by the caller, avoid zeroing it here */
67 mutex_lock(&rxe->usdev_lock);
68 rc = ib_get_eth_speed(dev, port_num, &attr->active_speed,
71 if (attr->state == IB_PORT_ACTIVE)
72 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
73 else if (dev_get_flags(rxe->ndev) & IFF_UP)
74 attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
76 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
78 mutex_unlock(&rxe->usdev_lock);
83 static int rxe_query_pkey(struct ib_device *device,
84 u8 port_num, u16 index, u16 *pkey)
86 struct rxe_dev *rxe = to_rdev(device);
87 struct rxe_port *port;
91 if (unlikely(index >= port->attr.pkey_tbl_len)) {
92 dev_warn(device->dev.parent, "invalid index = %d\n",
97 *pkey = port->pkey_tbl[index];
104 static int rxe_modify_device(struct ib_device *dev,
105 int mask, struct ib_device_modify *attr)
107 struct rxe_dev *rxe = to_rdev(dev);
109 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
110 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
112 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
113 memcpy(rxe->ib_dev.node_desc,
114 attr->node_desc, sizeof(rxe->ib_dev.node_desc));
120 static int rxe_modify_port(struct ib_device *dev,
121 u8 port_num, int mask, struct ib_port_modify *attr)
123 struct rxe_dev *rxe = to_rdev(dev);
124 struct rxe_port *port;
128 port->attr.port_cap_flags |= attr->set_port_cap_mask;
129 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
131 if (mask & IB_PORT_RESET_QKEY_CNTR)
132 port->attr.qkey_viol_cntr = 0;
137 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
140 struct rxe_dev *rxe = to_rdev(dev);
142 return rxe_link_layer(rxe, port_num);
145 static int rxe_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
147 struct rxe_dev *rxe = to_rdev(uctx->device);
148 struct rxe_ucontext *uc = to_ruc(uctx);
150 return rxe_add_to_pool(&rxe->uc_pool, &uc->pelem);
153 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
155 struct rxe_ucontext *uc = to_ruc(ibuc);
160 static int rxe_port_immutable(struct ib_device *dev, u8 port_num,
161 struct ib_port_immutable *immutable)
164 struct ib_port_attr attr;
166 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
168 err = ib_query_port(dev, port_num, &attr);
172 immutable->pkey_tbl_len = attr.pkey_tbl_len;
173 immutable->gid_tbl_len = attr.gid_tbl_len;
174 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
179 static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
181 struct rxe_dev *rxe = to_rdev(ibpd->device);
182 struct rxe_pd *pd = to_rpd(ibpd);
184 return rxe_add_to_pool(&rxe->pd_pool, &pd->pelem);
187 static void rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
189 struct rxe_pd *pd = to_rpd(ibpd);
194 static int rxe_create_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr,
195 u32 flags, struct ib_udata *udata)
199 struct rxe_dev *rxe = to_rdev(ibah->device);
200 struct rxe_ah *ah = to_rah(ibah);
202 err = rxe_av_chk_attr(rxe, attr);
206 err = rxe_add_to_pool(&rxe->ah_pool, &ah->pelem);
210 rxe_init_av(attr, &ah->av);
214 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
217 struct rxe_dev *rxe = to_rdev(ibah->device);
218 struct rxe_ah *ah = to_rah(ibah);
220 err = rxe_av_chk_attr(rxe, attr);
224 rxe_init_av(attr, &ah->av);
228 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
230 struct rxe_ah *ah = to_rah(ibah);
232 memset(attr, 0, sizeof(*attr));
233 attr->type = ibah->type;
234 rxe_av_to_attr(&ah->av, attr);
238 static void rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
240 struct rxe_ah *ah = to_rah(ibah);
245 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
250 struct rxe_recv_wqe *recv_wqe;
251 int num_sge = ibwr->num_sge;
253 if (unlikely(queue_full(rq->queue))) {
258 if (unlikely(num_sge > rq->max_sge)) {
264 for (i = 0; i < num_sge; i++)
265 length += ibwr->sg_list[i].length;
267 recv_wqe = producer_addr(rq->queue);
268 recv_wqe->wr_id = ibwr->wr_id;
269 recv_wqe->num_sge = num_sge;
271 memcpy(recv_wqe->dma.sge, ibwr->sg_list,
272 num_sge * sizeof(struct ib_sge));
274 recv_wqe->dma.length = length;
275 recv_wqe->dma.resid = length;
276 recv_wqe->dma.num_sge = num_sge;
277 recv_wqe->dma.cur_sge = 0;
278 recv_wqe->dma.sge_offset = 0;
280 /* make sure all changes to the work queue are written before we
281 * update the producer pointer
285 advance_producer(rq->queue);
292 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
293 struct ib_udata *udata)
296 struct rxe_dev *rxe = to_rdev(ibsrq->device);
297 struct rxe_pd *pd = to_rpd(ibsrq->pd);
298 struct rxe_srq *srq = to_rsrq(ibsrq);
299 struct rxe_create_srq_resp __user *uresp = NULL;
302 if (udata->outlen < sizeof(*uresp))
304 uresp = udata->outbuf;
307 err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
311 err = rxe_add_to_pool(&rxe->srq_pool, &srq->pelem);
318 err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
331 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
332 enum ib_srq_attr_mask mask,
333 struct ib_udata *udata)
336 struct rxe_srq *srq = to_rsrq(ibsrq);
337 struct rxe_dev *rxe = to_rdev(ibsrq->device);
338 struct rxe_modify_srq_cmd ucmd = {};
341 if (udata->inlen < sizeof(ucmd))
344 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
349 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
353 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata);
363 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
365 struct rxe_srq *srq = to_rsrq(ibsrq);
370 attr->max_wr = srq->rq.queue->buf->index_mask;
371 attr->max_sge = srq->rq.max_sge;
372 attr->srq_limit = srq->limit;
376 static void rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
378 struct rxe_srq *srq = to_rsrq(ibsrq);
381 rxe_queue_cleanup(srq->rq.queue);
383 rxe_drop_ref(srq->pd);
387 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
388 const struct ib_recv_wr **bad_wr)
392 struct rxe_srq *srq = to_rsrq(ibsrq);
394 spin_lock_irqsave(&srq->rq.producer_lock, flags);
397 err = post_one_recv(&srq->rq, wr);
403 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
411 static struct ib_qp *rxe_create_qp(struct ib_pd *ibpd,
412 struct ib_qp_init_attr *init,
413 struct ib_udata *udata)
416 struct rxe_dev *rxe = to_rdev(ibpd->device);
417 struct rxe_pd *pd = to_rpd(ibpd);
419 struct rxe_create_qp_resp __user *uresp = NULL;
422 if (udata->outlen < sizeof(*uresp))
423 return ERR_PTR(-EINVAL);
424 uresp = udata->outbuf;
427 err = rxe_qp_chk_init(rxe, init);
431 qp = rxe_alloc(&rxe->qp_pool);
447 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibpd, udata);
461 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
462 int mask, struct ib_udata *udata)
465 struct rxe_dev *rxe = to_rdev(ibqp->device);
466 struct rxe_qp *qp = to_rqp(ibqp);
468 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
472 err = rxe_qp_from_attr(qp, attr, mask, udata);
482 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
483 int mask, struct ib_qp_init_attr *init)
485 struct rxe_qp *qp = to_rqp(ibqp);
487 rxe_qp_to_init(qp, init);
488 rxe_qp_to_attr(qp, attr, mask);
493 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
495 struct rxe_qp *qp = to_rqp(ibqp);
503 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
504 unsigned int mask, unsigned int length)
506 int num_sge = ibwr->num_sge;
507 struct rxe_sq *sq = &qp->sq;
509 if (unlikely(num_sge > sq->max_sge))
512 if (unlikely(mask & WR_ATOMIC_MASK)) {
516 if (atomic_wr(ibwr)->remote_addr & 0x7)
520 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
521 (length > sq->max_inline)))
530 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
531 const struct ib_send_wr *ibwr)
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;
538 if (qp_type(qp) == IB_QPT_UD ||
539 qp_type(qp) == IB_QPT_SMI ||
540 qp_type(qp) == IB_QPT_GSI) {
541 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
542 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
543 if (qp_type(qp) == IB_QPT_GSI)
544 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
545 if (wr->opcode == IB_WR_SEND_WITH_IMM)
546 wr->ex.imm_data = ibwr->ex.imm_data;
548 switch (wr->opcode) {
549 case IB_WR_RDMA_WRITE_WITH_IMM:
550 wr->ex.imm_data = ibwr->ex.imm_data;
552 case IB_WR_RDMA_READ:
553 case IB_WR_RDMA_WRITE:
554 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
555 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
557 case IB_WR_SEND_WITH_IMM:
558 wr->ex.imm_data = ibwr->ex.imm_data;
560 case IB_WR_SEND_WITH_INV:
561 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
563 case IB_WR_ATOMIC_CMP_AND_SWP:
564 case IB_WR_ATOMIC_FETCH_AND_ADD:
565 wr->wr.atomic.remote_addr =
566 atomic_wr(ibwr)->remote_addr;
567 wr->wr.atomic.compare_add =
568 atomic_wr(ibwr)->compare_add;
569 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
570 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
572 case IB_WR_LOCAL_INV:
573 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
576 wr->wr.reg.mr = reg_wr(ibwr)->mr;
577 wr->wr.reg.key = reg_wr(ibwr)->key;
578 wr->wr.reg.access = reg_wr(ibwr)->access;
586 static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
587 unsigned int mask, unsigned int length,
588 struct rxe_send_wqe *wqe)
590 int num_sge = ibwr->num_sge;
595 init_send_wr(qp, &wqe->wr, ibwr);
597 if (qp_type(qp) == IB_QPT_UD ||
598 qp_type(qp) == IB_QPT_SMI ||
599 qp_type(qp) == IB_QPT_GSI)
600 memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
602 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) {
603 p = wqe->dma.inline_data;
606 for (i = 0; i < num_sge; i++, sge++) {
607 memcpy(p, (void *)(uintptr_t)sge->addr,
612 } else if (mask & WR_REG_MASK) {
614 wqe->state = wqe_state_posted;
617 memcpy(wqe->dma.sge, ibwr->sg_list,
618 num_sge * sizeof(struct ib_sge));
620 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
621 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
623 wqe->dma.length = length;
624 wqe->dma.resid = length;
625 wqe->dma.num_sge = num_sge;
626 wqe->dma.cur_sge = 0;
627 wqe->dma.sge_offset = 0;
628 wqe->state = wqe_state_posted;
629 wqe->ssn = atomic_add_return(1, &qp->ssn);
634 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
635 unsigned int mask, u32 length)
638 struct rxe_sq *sq = &qp->sq;
639 struct rxe_send_wqe *send_wqe;
642 err = validate_send_wr(qp, ibwr, mask, length);
646 spin_lock_irqsave(&qp->sq.sq_lock, flags);
648 if (unlikely(queue_full(sq->queue))) {
653 send_wqe = producer_addr(sq->queue);
655 err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
660 * make sure all changes to the work queue are
661 * written before we update the producer pointer
665 advance_producer(sq->queue);
666 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
671 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
675 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
676 const struct ib_send_wr **bad_wr)
680 unsigned int length = 0;
684 mask = wr_opcode_mask(wr->opcode, qp);
685 if (unlikely(!mask)) {
691 if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
692 !(mask & WR_INLINE_MASK))) {
699 for (i = 0; i < wr->num_sge; i++)
700 length += wr->sg_list[i].length;
702 err = post_one_send(qp, wr, mask, length);
711 rxe_run_task(&qp->req.task, 1);
712 if (unlikely(qp->req.state == QP_STATE_ERROR))
713 rxe_run_task(&qp->comp.task, 1);
718 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
719 const struct ib_send_wr **bad_wr)
721 struct rxe_qp *qp = to_rqp(ibqp);
723 if (unlikely(!qp->valid)) {
728 if (unlikely(qp->req.state < QP_STATE_READY)) {
734 /* Utilize process context to do protocol processing */
735 rxe_run_task(&qp->req.task, 0);
738 return rxe_post_send_kernel(qp, wr, bad_wr);
741 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
742 const struct ib_recv_wr **bad_wr)
745 struct rxe_qp *qp = to_rqp(ibqp);
746 struct rxe_rq *rq = &qp->rq;
749 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
755 if (unlikely(qp->srq)) {
761 spin_lock_irqsave(&rq->producer_lock, flags);
764 err = post_one_recv(rq, wr);
772 spin_unlock_irqrestore(&rq->producer_lock, flags);
774 if (qp->resp.state == QP_STATE_ERROR)
775 rxe_run_task(&qp->resp.task, 1);
781 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
782 struct ib_udata *udata)
785 struct ib_device *dev = ibcq->device;
786 struct rxe_dev *rxe = to_rdev(dev);
787 struct rxe_cq *cq = to_rcq(ibcq);
788 struct rxe_create_cq_resp __user *uresp = NULL;
791 if (udata->outlen < sizeof(*uresp))
793 uresp = udata->outbuf;
799 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
803 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
808 return rxe_add_to_pool(&rxe->cq_pool, &cq->pelem);
811 static void rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
813 struct rxe_cq *cq = to_rcq(ibcq);
820 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
823 struct rxe_cq *cq = to_rcq(ibcq);
824 struct rxe_dev *rxe = to_rdev(ibcq->device);
825 struct rxe_resize_cq_resp __user *uresp = NULL;
828 if (udata->outlen < sizeof(*uresp))
830 uresp = udata->outbuf;
833 err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
837 err = rxe_cq_resize_queue(cq, cqe, uresp, udata);
847 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
850 struct rxe_cq *cq = to_rcq(ibcq);
854 spin_lock_irqsave(&cq->cq_lock, flags);
855 for (i = 0; i < num_entries; i++) {
856 cqe = queue_head(cq->queue);
860 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
861 advance_consumer(cq->queue);
863 spin_unlock_irqrestore(&cq->cq_lock, flags);
868 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
870 struct rxe_cq *cq = to_rcq(ibcq);
871 int count = queue_count(cq->queue);
873 return (count > wc_cnt) ? wc_cnt : count;
876 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
878 struct rxe_cq *cq = to_rcq(ibcq);
879 unsigned long irq_flags;
882 spin_lock_irqsave(&cq->cq_lock, irq_flags);
883 if (cq->notify != IB_CQ_NEXT_COMP)
884 cq->notify = flags & IB_CQ_SOLICITED_MASK;
886 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !queue_empty(cq->queue))
889 spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
894 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
896 struct rxe_dev *rxe = to_rdev(ibpd->device);
897 struct rxe_pd *pd = to_rpd(ibpd);
901 mr = rxe_alloc(&rxe->mr_pool);
911 err = rxe_mem_init_dma(pd, access, mr);
925 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
929 int access, struct ib_udata *udata)
932 struct rxe_dev *rxe = to_rdev(ibpd->device);
933 struct rxe_pd *pd = to_rpd(ibpd);
936 mr = rxe_alloc(&rxe->mr_pool);
946 err = rxe_mem_init_user(pd, start, length, iova,
961 static int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
963 struct rxe_mem *mr = to_rmr(ibmr);
965 mr->state = RXE_MEM_STATE_ZOMBIE;
966 rxe_drop_ref(mr->pd);
972 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
973 u32 max_num_sg, struct ib_udata *udata)
975 struct rxe_dev *rxe = to_rdev(ibpd->device);
976 struct rxe_pd *pd = to_rpd(ibpd);
980 if (mr_type != IB_MR_TYPE_MEM_REG)
981 return ERR_PTR(-EINVAL);
983 mr = rxe_alloc(&rxe->mr_pool);
993 err = rxe_mem_init_fast(pd, max_num_sg, mr);
1004 return ERR_PTR(err);
1007 static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
1009 struct rxe_mem *mr = to_rmr(ibmr);
1010 struct rxe_map *map;
1011 struct rxe_phys_buf *buf;
1013 if (unlikely(mr->nbuf == mr->num_buf))
1016 map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
1017 buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
1020 buf->size = ibmr->page_size;
1026 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
1027 int sg_nents, unsigned int *sg_offset)
1029 struct rxe_mem *mr = to_rmr(ibmr);
1034 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
1036 mr->va = ibmr->iova;
1037 mr->iova = ibmr->iova;
1038 mr->length = ibmr->length;
1039 mr->page_shift = ilog2(ibmr->page_size);
1040 mr->page_mask = ibmr->page_size - 1;
1041 mr->offset = mr->iova & mr->page_mask;
1046 static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
1049 struct rxe_dev *rxe = to_rdev(ibqp->device);
1050 struct rxe_qp *qp = to_rqp(ibqp);
1051 struct rxe_mc_grp *grp;
1053 /* takes a ref on grp if successful */
1054 err = rxe_mcast_get_grp(rxe, mgid, &grp);
1058 err = rxe_mcast_add_grp_elem(rxe, qp, grp);
1064 static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
1066 struct rxe_dev *rxe = to_rdev(ibqp->device);
1067 struct rxe_qp *qp = to_rqp(ibqp);
1069 return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
1072 static ssize_t parent_show(struct device *device,
1073 struct device_attribute *attr, char *buf)
1075 struct rxe_dev *rxe =
1076 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
1078 return snprintf(buf, 16, "%s\n", rxe_parent_name(rxe, 1));
1081 static DEVICE_ATTR_RO(parent);
1083 static struct attribute *rxe_dev_attributes[] = {
1084 &dev_attr_parent.attr,
1088 static const struct attribute_group rxe_attr_group = {
1089 .attrs = rxe_dev_attributes,
1092 static int rxe_enable_driver(struct ib_device *ib_dev)
1094 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1096 rxe_set_port_state(rxe);
1097 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1101 static const struct ib_device_ops rxe_dev_ops = {
1102 .owner = THIS_MODULE,
1103 .driver_id = RDMA_DRIVER_RXE,
1104 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
1106 .alloc_hw_stats = rxe_ib_alloc_hw_stats,
1107 .alloc_mr = rxe_alloc_mr,
1108 .alloc_pd = rxe_alloc_pd,
1109 .alloc_ucontext = rxe_alloc_ucontext,
1110 .attach_mcast = rxe_attach_mcast,
1111 .create_ah = rxe_create_ah,
1112 .create_cq = rxe_create_cq,
1113 .create_qp = rxe_create_qp,
1114 .create_srq = rxe_create_srq,
1115 .dealloc_driver = rxe_dealloc,
1116 .dealloc_pd = rxe_dealloc_pd,
1117 .dealloc_ucontext = rxe_dealloc_ucontext,
1118 .dereg_mr = rxe_dereg_mr,
1119 .destroy_ah = rxe_destroy_ah,
1120 .destroy_cq = rxe_destroy_cq,
1121 .destroy_qp = rxe_destroy_qp,
1122 .destroy_srq = rxe_destroy_srq,
1123 .detach_mcast = rxe_detach_mcast,
1124 .enable_driver = rxe_enable_driver,
1125 .get_dma_mr = rxe_get_dma_mr,
1126 .get_hw_stats = rxe_ib_get_hw_stats,
1127 .get_link_layer = rxe_get_link_layer,
1128 .get_port_immutable = rxe_port_immutable,
1129 .map_mr_sg = rxe_map_mr_sg,
1131 .modify_ah = rxe_modify_ah,
1132 .modify_device = rxe_modify_device,
1133 .modify_port = rxe_modify_port,
1134 .modify_qp = rxe_modify_qp,
1135 .modify_srq = rxe_modify_srq,
1136 .peek_cq = rxe_peek_cq,
1137 .poll_cq = rxe_poll_cq,
1138 .post_recv = rxe_post_recv,
1139 .post_send = rxe_post_send,
1140 .post_srq_recv = rxe_post_srq_recv,
1141 .query_ah = rxe_query_ah,
1142 .query_device = rxe_query_device,
1143 .query_pkey = rxe_query_pkey,
1144 .query_port = rxe_query_port,
1145 .query_qp = rxe_query_qp,
1146 .query_srq = rxe_query_srq,
1147 .reg_user_mr = rxe_reg_user_mr,
1148 .req_notify_cq = rxe_req_notify_cq,
1149 .resize_cq = rxe_resize_cq,
1151 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
1152 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
1153 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
1154 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
1155 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1158 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
1161 struct ib_device *dev = &rxe->ib_dev;
1162 struct crypto_shash *tfm;
1164 strlcpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
1166 dev->node_type = RDMA_NODE_IB_CA;
1167 dev->phys_port_cnt = 1;
1168 dev->num_comp_vectors = num_possible_cpus();
1169 dev->dev.parent = rxe_dma_device(rxe);
1170 dev->local_dma_lkey = 0;
1171 addrconf_addr_eui48((unsigned char *)&dev->node_guid,
1172 rxe->ndev->dev_addr);
1173 dev->dev.dma_ops = &dma_virt_ops;
1174 dma_coerce_mask_and_coherent(&dev->dev,
1175 dma_get_required_mask(&dev->dev));
1177 dev->uverbs_cmd_mask = BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT)
1178 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
1179 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE)
1180 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT)
1181 | BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD)
1182 | BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD)
1183 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ)
1184 | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ)
1185 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ)
1186 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ)
1187 | BIT_ULL(IB_USER_VERBS_CMD_POST_SRQ_RECV)
1188 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP)
1189 | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP)
1190 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP)
1191 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP)
1192 | BIT_ULL(IB_USER_VERBS_CMD_POST_SEND)
1193 | BIT_ULL(IB_USER_VERBS_CMD_POST_RECV)
1194 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ)
1195 | BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ)
1196 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ)
1197 | BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ)
1198 | BIT_ULL(IB_USER_VERBS_CMD_PEEK_CQ)
1199 | BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)
1200 | BIT_ULL(IB_USER_VERBS_CMD_REG_MR)
1201 | BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR)
1202 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH)
1203 | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_AH)
1204 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_AH)
1205 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH)
1206 | BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST)
1207 | BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST)
1210 ib_set_device_ops(dev, &rxe_dev_ops);
1211 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
1215 tfm = crypto_alloc_shash("crc32", 0, 0);
1217 pr_err("failed to allocate crc algorithm err:%ld\n",
1219 return PTR_ERR(tfm);
1223 rdma_set_device_sysfs_group(dev, &rxe_attr_group);
1224 err = ib_register_device(dev, ibdev_name);
1226 pr_warn("%s failed with error %d\n", __func__, err);
1229 * Note that rxe may be invalid at this point if another thread