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 = RDMA_LINK_PHYS_STATE_LINK_UP;
73 else if (dev_get_flags(rxe->ndev) & IFF_UP)
74 attr->phys_state = RDMA_LINK_PHYS_STATE_POLLING;
76 attr->phys_state = RDMA_LINK_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_ucontext *context,
180 struct ib_udata *udata)
182 struct rxe_dev *rxe = to_rdev(ibpd->device);
183 struct rxe_pd *pd = to_rpd(ibpd);
185 return rxe_add_to_pool(&rxe->pd_pool, &pd->pelem);
188 static void rxe_dealloc_pd(struct ib_pd *ibpd)
190 struct rxe_pd *pd = to_rpd(ibpd);
195 static struct ib_ah *rxe_create_ah(struct ib_pd *ibpd,
196 struct rdma_ah_attr *attr,
198 struct ib_udata *udata)
202 struct rxe_dev *rxe = to_rdev(ibpd->device);
203 struct rxe_pd *pd = to_rpd(ibpd);
206 err = rxe_av_chk_attr(rxe, attr);
210 ah = rxe_alloc(&rxe->ah_pool);
212 return ERR_PTR(-ENOMEM);
217 rxe_init_av(attr, &ah->av);
221 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
224 struct rxe_dev *rxe = to_rdev(ibah->device);
225 struct rxe_ah *ah = to_rah(ibah);
227 err = rxe_av_chk_attr(rxe, attr);
231 rxe_init_av(attr, &ah->av);
235 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
237 struct rxe_ah *ah = to_rah(ibah);
239 memset(attr, 0, sizeof(*attr));
240 attr->type = ibah->type;
241 rxe_av_to_attr(&ah->av, attr);
245 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
247 struct rxe_ah *ah = to_rah(ibah);
249 rxe_drop_ref(ah->pd);
254 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
259 struct rxe_recv_wqe *recv_wqe;
260 int num_sge = ibwr->num_sge;
262 if (unlikely(queue_full(rq->queue))) {
267 if (unlikely(num_sge > rq->max_sge)) {
273 for (i = 0; i < num_sge; i++)
274 length += ibwr->sg_list[i].length;
276 recv_wqe = producer_addr(rq->queue);
277 recv_wqe->wr_id = ibwr->wr_id;
278 recv_wqe->num_sge = num_sge;
280 memcpy(recv_wqe->dma.sge, ibwr->sg_list,
281 num_sge * sizeof(struct ib_sge));
283 recv_wqe->dma.length = length;
284 recv_wqe->dma.resid = length;
285 recv_wqe->dma.num_sge = num_sge;
286 recv_wqe->dma.cur_sge = 0;
287 recv_wqe->dma.sge_offset = 0;
289 /* make sure all changes to the work queue are written before we
290 * update the producer pointer
294 advance_producer(rq->queue);
301 static struct ib_srq *rxe_create_srq(struct ib_pd *ibpd,
302 struct ib_srq_init_attr *init,
303 struct ib_udata *udata)
306 struct rxe_dev *rxe = to_rdev(ibpd->device);
307 struct rxe_pd *pd = to_rpd(ibpd);
308 struct rxe_ucontext *ucontext =
309 rdma_udata_to_drv_context(udata, struct rxe_ucontext, ibuc);
311 struct rxe_create_srq_resp __user *uresp = NULL;
314 if (udata->outlen < sizeof(*uresp))
315 return ERR_PTR(-EINVAL);
316 uresp = udata->outbuf;
319 err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
323 srq = rxe_alloc(&rxe->srq_pool);
333 err = rxe_srq_from_init(rxe, srq, init, &ucontext->ibuc, uresp);
347 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
348 enum ib_srq_attr_mask mask,
349 struct ib_udata *udata)
352 struct rxe_srq *srq = to_rsrq(ibsrq);
353 struct rxe_dev *rxe = to_rdev(ibsrq->device);
354 struct rxe_modify_srq_cmd ucmd = {};
357 if (udata->inlen < sizeof(ucmd))
360 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
365 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
369 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd);
379 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
381 struct rxe_srq *srq = to_rsrq(ibsrq);
386 attr->max_wr = srq->rq.queue->buf->index_mask;
387 attr->max_sge = srq->rq.max_sge;
388 attr->srq_limit = srq->limit;
392 static int rxe_destroy_srq(struct ib_srq *ibsrq)
394 struct rxe_srq *srq = to_rsrq(ibsrq);
397 rxe_queue_cleanup(srq->rq.queue);
399 rxe_drop_ref(srq->pd);
406 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
407 const struct ib_recv_wr **bad_wr)
411 struct rxe_srq *srq = to_rsrq(ibsrq);
413 spin_lock_irqsave(&srq->rq.producer_lock, flags);
416 err = post_one_recv(&srq->rq, wr);
422 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
430 static struct ib_qp *rxe_create_qp(struct ib_pd *ibpd,
431 struct ib_qp_init_attr *init,
432 struct ib_udata *udata)
435 struct rxe_dev *rxe = to_rdev(ibpd->device);
436 struct rxe_pd *pd = to_rpd(ibpd);
438 struct rxe_create_qp_resp __user *uresp = NULL;
441 if (udata->outlen < sizeof(*uresp))
442 return ERR_PTR(-EINVAL);
443 uresp = udata->outbuf;
446 err = rxe_qp_chk_init(rxe, init);
450 qp = rxe_alloc(&rxe->qp_pool);
466 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibpd, udata);
480 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
481 int mask, struct ib_udata *udata)
484 struct rxe_dev *rxe = to_rdev(ibqp->device);
485 struct rxe_qp *qp = to_rqp(ibqp);
487 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
491 err = rxe_qp_from_attr(qp, attr, mask, udata);
501 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
502 int mask, struct ib_qp_init_attr *init)
504 struct rxe_qp *qp = to_rqp(ibqp);
506 rxe_qp_to_init(qp, init);
507 rxe_qp_to_attr(qp, attr, mask);
512 static int rxe_destroy_qp(struct ib_qp *ibqp)
514 struct rxe_qp *qp = to_rqp(ibqp);
522 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
523 unsigned int mask, unsigned int length)
525 int num_sge = ibwr->num_sge;
526 struct rxe_sq *sq = &qp->sq;
528 if (unlikely(num_sge > sq->max_sge))
531 if (unlikely(mask & WR_ATOMIC_MASK)) {
535 if (atomic_wr(ibwr)->remote_addr & 0x7)
539 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
540 (length > sq->max_inline)))
549 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
550 const struct ib_send_wr *ibwr)
552 wr->wr_id = ibwr->wr_id;
553 wr->num_sge = ibwr->num_sge;
554 wr->opcode = ibwr->opcode;
555 wr->send_flags = ibwr->send_flags;
557 if (qp_type(qp) == IB_QPT_UD ||
558 qp_type(qp) == IB_QPT_SMI ||
559 qp_type(qp) == IB_QPT_GSI) {
560 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
561 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
562 if (qp_type(qp) == IB_QPT_GSI)
563 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
564 if (wr->opcode == IB_WR_SEND_WITH_IMM)
565 wr->ex.imm_data = ibwr->ex.imm_data;
567 switch (wr->opcode) {
568 case IB_WR_RDMA_WRITE_WITH_IMM:
569 wr->ex.imm_data = ibwr->ex.imm_data;
571 case IB_WR_RDMA_READ:
572 case IB_WR_RDMA_WRITE:
573 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
574 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
576 case IB_WR_SEND_WITH_IMM:
577 wr->ex.imm_data = ibwr->ex.imm_data;
579 case IB_WR_SEND_WITH_INV:
580 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
582 case IB_WR_ATOMIC_CMP_AND_SWP:
583 case IB_WR_ATOMIC_FETCH_AND_ADD:
584 wr->wr.atomic.remote_addr =
585 atomic_wr(ibwr)->remote_addr;
586 wr->wr.atomic.compare_add =
587 atomic_wr(ibwr)->compare_add;
588 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
589 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
591 case IB_WR_LOCAL_INV:
592 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
595 wr->wr.reg.mr = reg_wr(ibwr)->mr;
596 wr->wr.reg.key = reg_wr(ibwr)->key;
597 wr->wr.reg.access = reg_wr(ibwr)->access;
605 static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
606 unsigned int mask, unsigned int length,
607 struct rxe_send_wqe *wqe)
609 int num_sge = ibwr->num_sge;
614 init_send_wr(qp, &wqe->wr, ibwr);
616 if (qp_type(qp) == IB_QPT_UD ||
617 qp_type(qp) == IB_QPT_SMI ||
618 qp_type(qp) == IB_QPT_GSI)
619 memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
621 if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) {
622 p = wqe->dma.inline_data;
625 for (i = 0; i < num_sge; i++, sge++) {
626 memcpy(p, (void *)(uintptr_t)sge->addr,
631 } else if (mask & WR_REG_MASK) {
633 wqe->state = wqe_state_posted;
636 memcpy(wqe->dma.sge, ibwr->sg_list,
637 num_sge * sizeof(struct ib_sge));
639 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
640 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
642 wqe->dma.length = length;
643 wqe->dma.resid = length;
644 wqe->dma.num_sge = num_sge;
645 wqe->dma.cur_sge = 0;
646 wqe->dma.sge_offset = 0;
647 wqe->state = wqe_state_posted;
648 wqe->ssn = atomic_add_return(1, &qp->ssn);
653 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
654 unsigned int mask, u32 length)
657 struct rxe_sq *sq = &qp->sq;
658 struct rxe_send_wqe *send_wqe;
661 err = validate_send_wr(qp, ibwr, mask, length);
665 spin_lock_irqsave(&qp->sq.sq_lock, flags);
667 if (unlikely(queue_full(sq->queue))) {
672 send_wqe = producer_addr(sq->queue);
674 err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
679 * make sure all changes to the work queue are
680 * written before we update the producer pointer
684 advance_producer(sq->queue);
685 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
690 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
694 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
695 const struct ib_send_wr **bad_wr)
699 unsigned int length = 0;
703 mask = wr_opcode_mask(wr->opcode, qp);
704 if (unlikely(!mask)) {
710 if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
711 !(mask & WR_INLINE_MASK))) {
718 for (i = 0; i < wr->num_sge; i++)
719 length += wr->sg_list[i].length;
721 err = post_one_send(qp, wr, mask, length);
730 rxe_run_task(&qp->req.task, 1);
731 if (unlikely(qp->req.state == QP_STATE_ERROR))
732 rxe_run_task(&qp->comp.task, 1);
737 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
738 const struct ib_send_wr **bad_wr)
740 struct rxe_qp *qp = to_rqp(ibqp);
742 if (unlikely(!qp->valid)) {
747 if (unlikely(qp->req.state < QP_STATE_READY)) {
753 /* Utilize process context to do protocol processing */
754 rxe_run_task(&qp->req.task, 0);
757 return rxe_post_send_kernel(qp, wr, bad_wr);
760 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
761 const struct ib_recv_wr **bad_wr)
764 struct rxe_qp *qp = to_rqp(ibqp);
765 struct rxe_rq *rq = &qp->rq;
768 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
774 if (unlikely(qp->srq)) {
780 spin_lock_irqsave(&rq->producer_lock, flags);
783 err = post_one_recv(rq, wr);
791 spin_unlock_irqrestore(&rq->producer_lock, flags);
793 if (qp->resp.state == QP_STATE_ERROR)
794 rxe_run_task(&qp->resp.task, 1);
800 static struct ib_cq *rxe_create_cq(struct ib_device *dev,
801 const struct ib_cq_init_attr *attr,
802 struct ib_ucontext *context,
803 struct ib_udata *udata)
806 struct rxe_dev *rxe = to_rdev(dev);
808 struct rxe_create_cq_resp __user *uresp = NULL;
811 if (udata->outlen < sizeof(*uresp))
812 return ERR_PTR(-EINVAL);
813 uresp = udata->outbuf;
817 return ERR_PTR(-EINVAL);
819 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
823 cq = rxe_alloc(&rxe->cq_pool);
829 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector,
842 static int rxe_destroy_cq(struct ib_cq *ibcq)
844 struct rxe_cq *cq = to_rcq(ibcq);
852 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
855 struct rxe_cq *cq = to_rcq(ibcq);
856 struct rxe_dev *rxe = to_rdev(ibcq->device);
857 struct rxe_resize_cq_resp __user *uresp = NULL;
860 if (udata->outlen < sizeof(*uresp))
862 uresp = udata->outbuf;
865 err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
869 err = rxe_cq_resize_queue(cq, cqe, uresp);
879 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
882 struct rxe_cq *cq = to_rcq(ibcq);
886 spin_lock_irqsave(&cq->cq_lock, flags);
887 for (i = 0; i < num_entries; i++) {
888 cqe = queue_head(cq->queue);
892 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
893 advance_consumer(cq->queue);
895 spin_unlock_irqrestore(&cq->cq_lock, flags);
900 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
902 struct rxe_cq *cq = to_rcq(ibcq);
903 int count = queue_count(cq->queue);
905 return (count > wc_cnt) ? wc_cnt : count;
908 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
910 struct rxe_cq *cq = to_rcq(ibcq);
911 unsigned long irq_flags;
914 spin_lock_irqsave(&cq->cq_lock, irq_flags);
915 if (cq->notify != IB_CQ_NEXT_COMP)
916 cq->notify = flags & IB_CQ_SOLICITED_MASK;
918 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !queue_empty(cq->queue))
921 spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
926 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
928 struct rxe_dev *rxe = to_rdev(ibpd->device);
929 struct rxe_pd *pd = to_rpd(ibpd);
933 mr = rxe_alloc(&rxe->mr_pool);
943 err = rxe_mem_init_dma(pd, access, mr);
957 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
961 int access, struct ib_udata *udata)
964 struct rxe_dev *rxe = to_rdev(ibpd->device);
965 struct rxe_pd *pd = to_rpd(ibpd);
968 mr = rxe_alloc(&rxe->mr_pool);
978 err = rxe_mem_init_user(pd, start, length, iova,
993 static int rxe_dereg_mr(struct ib_mr *ibmr)
995 struct rxe_mem *mr = to_rmr(ibmr);
997 mr->state = RXE_MEM_STATE_ZOMBIE;
998 rxe_drop_ref(mr->pd);
1004 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd,
1005 enum ib_mr_type mr_type,
1008 struct rxe_dev *rxe = to_rdev(ibpd->device);
1009 struct rxe_pd *pd = to_rpd(ibpd);
1013 if (mr_type != IB_MR_TYPE_MEM_REG)
1014 return ERR_PTR(-EINVAL);
1016 mr = rxe_alloc(&rxe->mr_pool);
1026 err = rxe_mem_init_fast(pd, max_num_sg, mr);
1037 return ERR_PTR(err);
1040 static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
1042 struct rxe_mem *mr = to_rmr(ibmr);
1043 struct rxe_map *map;
1044 struct rxe_phys_buf *buf;
1046 if (unlikely(mr->nbuf == mr->num_buf))
1049 map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
1050 buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
1053 buf->size = ibmr->page_size;
1059 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
1060 int sg_nents, unsigned int *sg_offset)
1062 struct rxe_mem *mr = to_rmr(ibmr);
1067 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
1069 mr->va = ibmr->iova;
1070 mr->iova = ibmr->iova;
1071 mr->length = ibmr->length;
1072 mr->page_shift = ilog2(ibmr->page_size);
1073 mr->page_mask = ibmr->page_size - 1;
1074 mr->offset = mr->iova & mr->page_mask;
1079 static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
1082 struct rxe_dev *rxe = to_rdev(ibqp->device);
1083 struct rxe_qp *qp = to_rqp(ibqp);
1084 struct rxe_mc_grp *grp;
1086 /* takes a ref on grp if successful */
1087 err = rxe_mcast_get_grp(rxe, mgid, &grp);
1091 err = rxe_mcast_add_grp_elem(rxe, qp, grp);
1097 static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
1099 struct rxe_dev *rxe = to_rdev(ibqp->device);
1100 struct rxe_qp *qp = to_rqp(ibqp);
1102 return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
1105 static ssize_t parent_show(struct device *device,
1106 struct device_attribute *attr, char *buf)
1108 struct rxe_dev *rxe =
1109 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
1111 return snprintf(buf, 16, "%s\n", rxe_parent_name(rxe, 1));
1114 static DEVICE_ATTR_RO(parent);
1116 static struct attribute *rxe_dev_attributes[] = {
1117 &dev_attr_parent.attr,
1121 static const struct attribute_group rxe_attr_group = {
1122 .attrs = rxe_dev_attributes,
1125 static int rxe_enable_driver(struct ib_device *ib_dev)
1127 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1129 rxe_set_port_state(rxe);
1130 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1134 static const struct ib_device_ops rxe_dev_ops = {
1135 .alloc_hw_stats = rxe_ib_alloc_hw_stats,
1136 .alloc_mr = rxe_alloc_mr,
1137 .alloc_pd = rxe_alloc_pd,
1138 .alloc_ucontext = rxe_alloc_ucontext,
1139 .attach_mcast = rxe_attach_mcast,
1140 .create_ah = rxe_create_ah,
1141 .create_cq = rxe_create_cq,
1142 .create_qp = rxe_create_qp,
1143 .create_srq = rxe_create_srq,
1144 .dealloc_driver = rxe_dealloc,
1145 .dealloc_pd = rxe_dealloc_pd,
1146 .dealloc_ucontext = rxe_dealloc_ucontext,
1147 .dereg_mr = rxe_dereg_mr,
1148 .destroy_ah = rxe_destroy_ah,
1149 .destroy_cq = rxe_destroy_cq,
1150 .destroy_qp = rxe_destroy_qp,
1151 .destroy_srq = rxe_destroy_srq,
1152 .detach_mcast = rxe_detach_mcast,
1153 .enable_driver = rxe_enable_driver,
1154 .get_dma_mr = rxe_get_dma_mr,
1155 .get_hw_stats = rxe_ib_get_hw_stats,
1156 .get_link_layer = rxe_get_link_layer,
1157 .get_port_immutable = rxe_port_immutable,
1158 .map_mr_sg = rxe_map_mr_sg,
1160 .modify_ah = rxe_modify_ah,
1161 .modify_device = rxe_modify_device,
1162 .modify_port = rxe_modify_port,
1163 .modify_qp = rxe_modify_qp,
1164 .modify_srq = rxe_modify_srq,
1165 .peek_cq = rxe_peek_cq,
1166 .poll_cq = rxe_poll_cq,
1167 .post_recv = rxe_post_recv,
1168 .post_send = rxe_post_send,
1169 .post_srq_recv = rxe_post_srq_recv,
1170 .query_ah = rxe_query_ah,
1171 .query_device = rxe_query_device,
1172 .query_pkey = rxe_query_pkey,
1173 .query_port = rxe_query_port,
1174 .query_qp = rxe_query_qp,
1175 .query_srq = rxe_query_srq,
1176 .reg_user_mr = rxe_reg_user_mr,
1177 .req_notify_cq = rxe_req_notify_cq,
1178 .resize_cq = rxe_resize_cq,
1179 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
1180 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1183 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
1186 struct ib_device *dev = &rxe->ib_dev;
1187 struct crypto_shash *tfm;
1189 strlcpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
1191 dev->owner = THIS_MODULE;
1192 dev->node_type = RDMA_NODE_IB_CA;
1193 dev->phys_port_cnt = 1;
1194 dev->num_comp_vectors = num_possible_cpus();
1195 dev->dev.parent = rxe_dma_device(rxe);
1196 dev->local_dma_lkey = 0;
1197 addrconf_addr_eui48((unsigned char *)&dev->node_guid,
1198 rxe->ndev->dev_addr);
1199 dev->dev.dma_ops = &dma_virt_ops;
1200 dma_coerce_mask_and_coherent(&dev->dev,
1201 dma_get_required_mask(&dev->dev));
1203 dev->uverbs_abi_ver = RXE_UVERBS_ABI_VERSION;
1204 dev->uverbs_cmd_mask = BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT)
1205 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
1206 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE)
1207 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT)
1208 | BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD)
1209 | BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD)
1210 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ)
1211 | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ)
1212 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ)
1213 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ)
1214 | BIT_ULL(IB_USER_VERBS_CMD_POST_SRQ_RECV)
1215 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP)
1216 | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP)
1217 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP)
1218 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP)
1219 | BIT_ULL(IB_USER_VERBS_CMD_POST_SEND)
1220 | BIT_ULL(IB_USER_VERBS_CMD_POST_RECV)
1221 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ)
1222 | BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ)
1223 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ)
1224 | BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ)
1225 | BIT_ULL(IB_USER_VERBS_CMD_PEEK_CQ)
1226 | BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)
1227 | BIT_ULL(IB_USER_VERBS_CMD_REG_MR)
1228 | BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR)
1229 | BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH)
1230 | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_AH)
1231 | BIT_ULL(IB_USER_VERBS_CMD_QUERY_AH)
1232 | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH)
1233 | BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST)
1234 | BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST)
1237 ib_set_device_ops(dev, &rxe_dev_ops);
1238 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
1242 tfm = crypto_alloc_shash("crc32", 0, 0);
1244 pr_err("failed to allocate crc algorithm err:%ld\n",
1246 return PTR_ERR(tfm);
1250 rdma_set_device_sysfs_group(dev, &rxe_attr_group);
1251 dev->driver_id = RDMA_DRIVER_RXE;
1252 err = ib_register_device(dev, ibdev_name);
1254 pr_warn("%s failed with error %d\n", __func__, err);
1257 * Note that rxe may be invalid at this point if another thread