163fa15dbSBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
28700e3e7SMoni Shoua /*
38700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
48700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
58700e3e7SMoni Shoua  */
68700e3e7SMoni Shoua 
70bbb3b74SBart Van Assche #include <linux/dma-mapping.h>
84d6f2859SYuval Shaia #include <net/addrconf.h>
989944450SShamir Rabinovitch #include <rdma/uverbs_ioctl.h>
108700e3e7SMoni Shoua #include "rxe.h"
118700e3e7SMoni Shoua #include "rxe_loc.h"
128700e3e7SMoni Shoua #include "rxe_queue.h"
130b1e5b99SYonatan Cohen #include "rxe_hw_counters.h"
148700e3e7SMoni Shoua 
158700e3e7SMoni Shoua static int rxe_query_device(struct ib_device *dev,
168700e3e7SMoni Shoua 			    struct ib_device_attr *attr,
178700e3e7SMoni Shoua 			    struct ib_udata *uhw)
188700e3e7SMoni Shoua {
198700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
208700e3e7SMoni Shoua 
218700e3e7SMoni Shoua 	if (uhw->inlen || uhw->outlen)
228700e3e7SMoni Shoua 		return -EINVAL;
238700e3e7SMoni Shoua 
248700e3e7SMoni Shoua 	*attr = rxe->attr;
258700e3e7SMoni Shoua 	return 0;
268700e3e7SMoni Shoua }
278700e3e7SMoni Shoua 
288700e3e7SMoni Shoua static int rxe_query_port(struct ib_device *dev,
291fb7f897SMark Bloch 			  u32 port_num, struct ib_port_attr *attr)
308700e3e7SMoni Shoua {
318700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
3259590b8aSYuval Shaia 	int rc;
338700e3e7SMoni Shoua 
34c4550c63SOr Gerlitz 	/* *attr being zeroed by the caller, avoid zeroing it here */
35d12faf2dSZhu Yanjun 	*attr = rxe->port.attr;
368700e3e7SMoni Shoua 
378700e3e7SMoni Shoua 	mutex_lock(&rxe->usdev_lock);
38d4186194SYuval Shaia 	rc = ib_get_eth_speed(dev, port_num, &attr->active_speed,
39e404f945SParav Pandit 			      &attr->active_width);
405736c7c4SAndrew Boyer 
415736c7c4SAndrew Boyer 	if (attr->state == IB_PORT_ACTIVE)
4272a7720fSKamal Heib 		attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
435736c7c4SAndrew Boyer 	else if (dev_get_flags(rxe->ndev) & IFF_UP)
4472a7720fSKamal Heib 		attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
455736c7c4SAndrew Boyer 	else
4672a7720fSKamal Heib 		attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
475736c7c4SAndrew Boyer 
488700e3e7SMoni Shoua 	mutex_unlock(&rxe->usdev_lock);
498700e3e7SMoni Shoua 
50d4186194SYuval Shaia 	return rc;
518700e3e7SMoni Shoua }
528700e3e7SMoni Shoua 
538700e3e7SMoni Shoua static int rxe_query_pkey(struct ib_device *device,
541fb7f897SMark Bloch 			  u32 port_num, u16 index, u16 *pkey)
558700e3e7SMoni Shoua {
5676251e15SKamal Heib 	if (index > 0)
578700e3e7SMoni Shoua 		return -EINVAL;
5876251e15SKamal Heib 
5976251e15SKamal Heib 	*pkey = IB_DEFAULT_PKEY_FULL;
6076251e15SKamal Heib 	return 0;
618700e3e7SMoni Shoua }
628700e3e7SMoni Shoua 
638700e3e7SMoni Shoua static int rxe_modify_device(struct ib_device *dev,
648700e3e7SMoni Shoua 			     int mask, struct ib_device_modify *attr)
658700e3e7SMoni Shoua {
668700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
678700e3e7SMoni Shoua 
68f3fceba5SKamal Heib 	if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
69f3fceba5SKamal Heib 		     IB_DEVICE_MODIFY_NODE_DESC))
70f3fceba5SKamal Heib 		return -EOPNOTSUPP;
71f3fceba5SKamal Heib 
728700e3e7SMoni Shoua 	if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
738700e3e7SMoni Shoua 		rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
748700e3e7SMoni Shoua 
758700e3e7SMoni Shoua 	if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
768700e3e7SMoni Shoua 		memcpy(rxe->ib_dev.node_desc,
778700e3e7SMoni Shoua 		       attr->node_desc, sizeof(rxe->ib_dev.node_desc));
788700e3e7SMoni Shoua 	}
798700e3e7SMoni Shoua 
808700e3e7SMoni Shoua 	return 0;
818700e3e7SMoni Shoua }
828700e3e7SMoni Shoua 
838700e3e7SMoni Shoua static int rxe_modify_port(struct ib_device *dev,
841fb7f897SMark Bloch 			   u32 port_num, int mask, struct ib_port_modify *attr)
858700e3e7SMoni Shoua {
868700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
878700e3e7SMoni Shoua 	struct rxe_port *port;
888700e3e7SMoni Shoua 
898700e3e7SMoni Shoua 	port = &rxe->port;
908700e3e7SMoni Shoua 
918700e3e7SMoni Shoua 	port->attr.port_cap_flags |= attr->set_port_cap_mask;
928700e3e7SMoni Shoua 	port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
938700e3e7SMoni Shoua 
948700e3e7SMoni Shoua 	if (mask & IB_PORT_RESET_QKEY_CNTR)
958700e3e7SMoni Shoua 		port->attr.qkey_viol_cntr = 0;
968700e3e7SMoni Shoua 
978700e3e7SMoni Shoua 	return 0;
988700e3e7SMoni Shoua }
998700e3e7SMoni Shoua 
1008700e3e7SMoni Shoua static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
1011fb7f897SMark Bloch 					       u32 port_num)
1028700e3e7SMoni Shoua {
103420bd9e2SKamal Heib 	return IB_LINK_LAYER_ETHERNET;
1048700e3e7SMoni Shoua }
1058700e3e7SMoni Shoua 
10691a42c5bSBob Pearson static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
1078700e3e7SMoni Shoua {
10891a42c5bSBob Pearson 	struct rxe_dev *rxe = to_rdev(ibuc->device);
10991a42c5bSBob Pearson 	struct rxe_ucontext *uc = to_ruc(ibuc);
1108700e3e7SMoni Shoua 
11191a42c5bSBob Pearson 	return rxe_add_to_pool(&rxe->uc_pool, uc);
1128700e3e7SMoni Shoua }
1138700e3e7SMoni Shoua 
114a2a074efSLeon Romanovsky static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
1158700e3e7SMoni Shoua {
1168700e3e7SMoni Shoua 	struct rxe_ucontext *uc = to_ruc(ibuc);
1178700e3e7SMoni Shoua 
1188700e3e7SMoni Shoua 	rxe_drop_ref(uc);
1198700e3e7SMoni Shoua }
1208700e3e7SMoni Shoua 
1211fb7f897SMark Bloch static int rxe_port_immutable(struct ib_device *dev, u32 port_num,
1228700e3e7SMoni Shoua 			      struct ib_port_immutable *immutable)
1238700e3e7SMoni Shoua {
1248700e3e7SMoni Shoua 	int err;
1258700e3e7SMoni Shoua 	struct ib_port_attr attr;
1268700e3e7SMoni Shoua 
127c4550c63SOr Gerlitz 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
128c4550c63SOr Gerlitz 
129c4550c63SOr Gerlitz 	err = ib_query_port(dev, port_num, &attr);
1308700e3e7SMoni Shoua 	if (err)
1318700e3e7SMoni Shoua 		return err;
1328700e3e7SMoni Shoua 
1338700e3e7SMoni Shoua 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
1348700e3e7SMoni Shoua 	immutable->gid_tbl_len = attr.gid_tbl_len;
1358700e3e7SMoni Shoua 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
1368700e3e7SMoni Shoua 
1378700e3e7SMoni Shoua 	return 0;
1388700e3e7SMoni Shoua }
1398700e3e7SMoni Shoua 
140ff23dfa1SShamir Rabinovitch static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
1418700e3e7SMoni Shoua {
14221a428a0SLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibpd->device);
14321a428a0SLeon Romanovsky 	struct rxe_pd *pd = to_rpd(ibpd);
1448700e3e7SMoni Shoua 
14591a42c5bSBob Pearson 	return rxe_add_to_pool(&rxe->pd_pool, pd);
1468700e3e7SMoni Shoua }
1478700e3e7SMoni Shoua 
14891a7c58fSLeon Romanovsky static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
1498700e3e7SMoni Shoua {
1508700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
1518700e3e7SMoni Shoua 
1528700e3e7SMoni Shoua 	rxe_drop_ref(pd);
15391a7c58fSLeon Romanovsky 	return 0;
1548700e3e7SMoni Shoua }
1558700e3e7SMoni Shoua 
156fa5d010cSMaor Gottlieb static int rxe_create_ah(struct ib_ah *ibah,
157fa5d010cSMaor Gottlieb 			 struct rdma_ah_init_attr *init_attr,
158fa5d010cSMaor Gottlieb 			 struct ib_udata *udata)
159477864c8SMoni Shoua 
1608700e3e7SMoni Shoua {
1618700e3e7SMoni Shoua 	int err;
162d3456914SLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibah->device);
163d3456914SLeon Romanovsky 	struct rxe_ah *ah = to_rah(ibah);
1648700e3e7SMoni Shoua 
165fa5d010cSMaor Gottlieb 	err = rxe_av_chk_attr(rxe, init_attr->ah_attr);
1668700e3e7SMoni Shoua 	if (err)
167d3456914SLeon Romanovsky 		return err;
1688700e3e7SMoni Shoua 
16991a42c5bSBob Pearson 	err = rxe_add_to_pool(&rxe->ah_pool, ah);
170d3456914SLeon Romanovsky 	if (err)
171d3456914SLeon Romanovsky 		return err;
1728700e3e7SMoni Shoua 
173fa5d010cSMaor Gottlieb 	rxe_init_av(init_attr->ah_attr, &ah->av);
174d3456914SLeon Romanovsky 	return 0;
1758700e3e7SMoni Shoua }
1768700e3e7SMoni Shoua 
17790898850SDasaratharaman Chandramouli static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
1788700e3e7SMoni Shoua {
1798700e3e7SMoni Shoua 	int err;
1808700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibah->device);
1818700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
1828700e3e7SMoni Shoua 
1838700e3e7SMoni Shoua 	err = rxe_av_chk_attr(rxe, attr);
1848700e3e7SMoni Shoua 	if (err)
1858700e3e7SMoni Shoua 		return err;
1868700e3e7SMoni Shoua 
187fa407188SKamal Heib 	rxe_init_av(attr, &ah->av);
1888700e3e7SMoni Shoua 	return 0;
1898700e3e7SMoni Shoua }
1908700e3e7SMoni Shoua 
19190898850SDasaratharaman Chandramouli static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
1928700e3e7SMoni Shoua {
1938700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
1948700e3e7SMoni Shoua 
195eca7ddf9SDasaratharaman Chandramouli 	memset(attr, 0, sizeof(*attr));
19644c58487SDasaratharaman Chandramouli 	attr->type = ibah->type;
1979c96f3d4SZhu Yanjun 	rxe_av_to_attr(&ah->av, attr);
1988700e3e7SMoni Shoua 	return 0;
1998700e3e7SMoni Shoua }
2008700e3e7SMoni Shoua 
2019a9ebf8cSLeon Romanovsky static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
2028700e3e7SMoni Shoua {
2038700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2048700e3e7SMoni Shoua 
2058700e3e7SMoni Shoua 	rxe_drop_ref(ah);
2069a9ebf8cSLeon Romanovsky 	return 0;
2078700e3e7SMoni Shoua }
2088700e3e7SMoni Shoua 
209d34ac5cdSBart Van Assche static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
2108700e3e7SMoni Shoua {
2118700e3e7SMoni Shoua 	int err;
2128700e3e7SMoni Shoua 	int i;
2138700e3e7SMoni Shoua 	u32 length;
2148700e3e7SMoni Shoua 	struct rxe_recv_wqe *recv_wqe;
2158700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
2165bcf5a59SBob Pearson 	int full;
2178700e3e7SMoni Shoua 
218*ae6e843fSBob Pearson 	full = queue_full(rq->queue, QUEUE_TYPE_TO_DRIVER);
2195bcf5a59SBob Pearson 	if (unlikely(full)) {
2208700e3e7SMoni Shoua 		err = -ENOMEM;
2218700e3e7SMoni Shoua 		goto err1;
2228700e3e7SMoni Shoua 	}
2238700e3e7SMoni Shoua 
2248700e3e7SMoni Shoua 	if (unlikely(num_sge > rq->max_sge)) {
2258700e3e7SMoni Shoua 		err = -EINVAL;
2268700e3e7SMoni Shoua 		goto err1;
2278700e3e7SMoni Shoua 	}
2288700e3e7SMoni Shoua 
2298700e3e7SMoni Shoua 	length = 0;
2308700e3e7SMoni Shoua 	for (i = 0; i < num_sge; i++)
2318700e3e7SMoni Shoua 		length += ibwr->sg_list[i].length;
2328700e3e7SMoni Shoua 
233*ae6e843fSBob Pearson 	recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_TO_DRIVER);
2348700e3e7SMoni Shoua 	recv_wqe->wr_id = ibwr->wr_id;
2358700e3e7SMoni Shoua 	recv_wqe->num_sge = num_sge;
2368700e3e7SMoni Shoua 
2378700e3e7SMoni Shoua 	memcpy(recv_wqe->dma.sge, ibwr->sg_list,
2388700e3e7SMoni Shoua 	       num_sge * sizeof(struct ib_sge));
2398700e3e7SMoni Shoua 
2408700e3e7SMoni Shoua 	recv_wqe->dma.length		= length;
2418700e3e7SMoni Shoua 	recv_wqe->dma.resid		= length;
2428700e3e7SMoni Shoua 	recv_wqe->dma.num_sge		= num_sge;
2438700e3e7SMoni Shoua 	recv_wqe->dma.cur_sge		= 0;
2448700e3e7SMoni Shoua 	recv_wqe->dma.sge_offset	= 0;
2458700e3e7SMoni Shoua 
246*ae6e843fSBob Pearson 	queue_advance_producer(rq->queue, QUEUE_TYPE_TO_DRIVER);
2475bcf5a59SBob Pearson 
2488700e3e7SMoni Shoua 	return 0;
2498700e3e7SMoni Shoua 
2508700e3e7SMoni Shoua err1:
2518700e3e7SMoni Shoua 	return err;
2528700e3e7SMoni Shoua }
2538700e3e7SMoni Shoua 
25468e326deSLeon Romanovsky static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
2558700e3e7SMoni Shoua 			  struct ib_udata *udata)
2568700e3e7SMoni Shoua {
2578700e3e7SMoni Shoua 	int err;
25868e326deSLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibsrq->device);
25968e326deSLeon Romanovsky 	struct rxe_pd *pd = to_rpd(ibsrq->pd);
26068e326deSLeon Romanovsky 	struct rxe_srq *srq = to_rsrq(ibsrq);
2610c43ab37SJason Gunthorpe 	struct rxe_create_srq_resp __user *uresp = NULL;
2620c43ab37SJason Gunthorpe 
263652caba5SJason Gunthorpe 	if (init->srq_type != IB_SRQT_BASIC)
264652caba5SJason Gunthorpe 		return -EOPNOTSUPP;
265652caba5SJason Gunthorpe 
2660c43ab37SJason Gunthorpe 	if (udata) {
2670c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
26868e326deSLeon Romanovsky 			return -EINVAL;
2690c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
2705bcf5a59SBob Pearson 		srq->is_user = true;
2715bcf5a59SBob Pearson 	} else {
2725bcf5a59SBob Pearson 		srq->is_user = false;
2730c43ab37SJason Gunthorpe 	}
2748700e3e7SMoni Shoua 
2758700e3e7SMoni Shoua 	err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
2768700e3e7SMoni Shoua 	if (err)
2778700e3e7SMoni Shoua 		goto err1;
2788700e3e7SMoni Shoua 
27991a42c5bSBob Pearson 	err = rxe_add_to_pool(&rxe->srq_pool, srq);
28068e326deSLeon Romanovsky 	if (err)
2818700e3e7SMoni Shoua 		goto err1;
2828700e3e7SMoni Shoua 
2838700e3e7SMoni Shoua 	rxe_add_ref(pd);
2848700e3e7SMoni Shoua 	srq->pd = pd;
2858700e3e7SMoni Shoua 
286ff23dfa1SShamir Rabinovitch 	err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
2878700e3e7SMoni Shoua 	if (err)
2888700e3e7SMoni Shoua 		goto err2;
2898700e3e7SMoni Shoua 
29068e326deSLeon Romanovsky 	return 0;
2918700e3e7SMoni Shoua 
2928700e3e7SMoni Shoua err2:
2938700e3e7SMoni Shoua 	rxe_drop_ref(pd);
2948700e3e7SMoni Shoua 	rxe_drop_ref(srq);
2958700e3e7SMoni Shoua err1:
29668e326deSLeon Romanovsky 	return err;
2978700e3e7SMoni Shoua }
2988700e3e7SMoni Shoua 
2998700e3e7SMoni Shoua static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
3008700e3e7SMoni Shoua 			  enum ib_srq_attr_mask mask,
3018700e3e7SMoni Shoua 			  struct ib_udata *udata)
3028700e3e7SMoni Shoua {
3038700e3e7SMoni Shoua 	int err;
3048700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3058700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibsrq->device);
3060c43ab37SJason Gunthorpe 	struct rxe_modify_srq_cmd ucmd = {};
3070c43ab37SJason Gunthorpe 
3080c43ab37SJason Gunthorpe 	if (udata) {
3090c43ab37SJason Gunthorpe 		if (udata->inlen < sizeof(ucmd))
3100c43ab37SJason Gunthorpe 			return -EINVAL;
3110c43ab37SJason Gunthorpe 
3120c43ab37SJason Gunthorpe 		err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
3130c43ab37SJason Gunthorpe 		if (err)
3140c43ab37SJason Gunthorpe 			return err;
3150c43ab37SJason Gunthorpe 	}
3168700e3e7SMoni Shoua 
3178700e3e7SMoni Shoua 	err = rxe_srq_chk_attr(rxe, srq, attr, mask);
3188700e3e7SMoni Shoua 	if (err)
3198700e3e7SMoni Shoua 		goto err1;
3208700e3e7SMoni Shoua 
321ff23dfa1SShamir Rabinovitch 	err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata);
3228700e3e7SMoni Shoua 	if (err)
3238700e3e7SMoni Shoua 		goto err1;
3248700e3e7SMoni Shoua 
3258700e3e7SMoni Shoua 	return 0;
3268700e3e7SMoni Shoua 
3278700e3e7SMoni Shoua err1:
3288700e3e7SMoni Shoua 	return err;
3298700e3e7SMoni Shoua }
3308700e3e7SMoni Shoua 
3318700e3e7SMoni Shoua static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
3328700e3e7SMoni Shoua {
3338700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3348700e3e7SMoni Shoua 
3358700e3e7SMoni Shoua 	if (srq->error)
3368700e3e7SMoni Shoua 		return -EINVAL;
3378700e3e7SMoni Shoua 
3388700e3e7SMoni Shoua 	attr->max_wr = srq->rq.queue->buf->index_mask;
3398700e3e7SMoni Shoua 	attr->max_sge = srq->rq.max_sge;
3408700e3e7SMoni Shoua 	attr->srq_limit = srq->limit;
3418700e3e7SMoni Shoua 	return 0;
3428700e3e7SMoni Shoua }
3438700e3e7SMoni Shoua 
344119181d1SLeon Romanovsky static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
3458700e3e7SMoni Shoua {
3468700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3478700e3e7SMoni Shoua 
3488700e3e7SMoni Shoua 	if (srq->rq.queue)
3498700e3e7SMoni Shoua 		rxe_queue_cleanup(srq->rq.queue);
3508700e3e7SMoni Shoua 
3518700e3e7SMoni Shoua 	rxe_drop_ref(srq->pd);
3528700e3e7SMoni Shoua 	rxe_drop_ref(srq);
353119181d1SLeon Romanovsky 	return 0;
3548700e3e7SMoni Shoua }
3558700e3e7SMoni Shoua 
356d34ac5cdSBart Van Assche static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
357d34ac5cdSBart Van Assche 			     const struct ib_recv_wr **bad_wr)
3588700e3e7SMoni Shoua {
3598700e3e7SMoni Shoua 	int err = 0;
3608700e3e7SMoni Shoua 	unsigned long flags;
3618700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3628700e3e7SMoni Shoua 
3638700e3e7SMoni Shoua 	spin_lock_irqsave(&srq->rq.producer_lock, flags);
3648700e3e7SMoni Shoua 
3658700e3e7SMoni Shoua 	while (wr) {
3668700e3e7SMoni Shoua 		err = post_one_recv(&srq->rq, wr);
3678700e3e7SMoni Shoua 		if (unlikely(err))
3688700e3e7SMoni Shoua 			break;
3698700e3e7SMoni Shoua 		wr = wr->next;
3708700e3e7SMoni Shoua 	}
3718700e3e7SMoni Shoua 
3728700e3e7SMoni Shoua 	spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
3738700e3e7SMoni Shoua 
3748700e3e7SMoni Shoua 	if (err)
3758700e3e7SMoni Shoua 		*bad_wr = wr;
3768700e3e7SMoni Shoua 
3778700e3e7SMoni Shoua 	return err;
3788700e3e7SMoni Shoua }
3798700e3e7SMoni Shoua 
380514aee66SLeon Romanovsky static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init,
3818700e3e7SMoni Shoua 			 struct ib_udata *udata)
3828700e3e7SMoni Shoua {
3838700e3e7SMoni Shoua 	int err;
384514aee66SLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibqp->device);
385514aee66SLeon Romanovsky 	struct rxe_pd *pd = to_rpd(ibqp->pd);
386514aee66SLeon Romanovsky 	struct rxe_qp *qp = to_rqp(ibqp);
3870c43ab37SJason Gunthorpe 	struct rxe_create_qp_resp __user *uresp = NULL;
3880c43ab37SJason Gunthorpe 
3890c43ab37SJason Gunthorpe 	if (udata) {
3900c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
391514aee66SLeon Romanovsky 			return -EINVAL;
3920c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
3930c43ab37SJason Gunthorpe 	}
3948700e3e7SMoni Shoua 
3951f11a761SJason Gunthorpe 	if (init->create_flags)
396514aee66SLeon Romanovsky 		return -EOPNOTSUPP;
3971f11a761SJason Gunthorpe 
3988700e3e7SMoni Shoua 	err = rxe_qp_chk_init(rxe, init);
3998700e3e7SMoni Shoua 	if (err)
400514aee66SLeon Romanovsky 		return err;
4018700e3e7SMoni Shoua 
4028700e3e7SMoni Shoua 	if (udata) {
403514aee66SLeon Romanovsky 		if (udata->inlen)
404514aee66SLeon Romanovsky 			return -EINVAL;
405514aee66SLeon Romanovsky 
4065bcf5a59SBob Pearson 		qp->is_user = true;
4075bcf5a59SBob Pearson 	} else {
4085bcf5a59SBob Pearson 		qp->is_user = false;
4098700e3e7SMoni Shoua 	}
4108700e3e7SMoni Shoua 
411514aee66SLeon Romanovsky 	err = rxe_add_to_pool(&rxe->qp_pool, qp);
4128700e3e7SMoni Shoua 	if (err)
413514aee66SLeon Romanovsky 		return err;
4148700e3e7SMoni Shoua 
415514aee66SLeon Romanovsky 	rxe_add_index(qp);
416514aee66SLeon Romanovsky 	err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata);
417514aee66SLeon Romanovsky 	if (err)
418514aee66SLeon Romanovsky 		goto qp_init;
4198700e3e7SMoni Shoua 
420514aee66SLeon Romanovsky 	return 0;
421514aee66SLeon Romanovsky 
422514aee66SLeon Romanovsky qp_init:
4238700e3e7SMoni Shoua 	rxe_drop_index(qp);
4248700e3e7SMoni Shoua 	rxe_drop_ref(qp);
425514aee66SLeon Romanovsky 	return err;
4268700e3e7SMoni Shoua }
4278700e3e7SMoni Shoua 
4288700e3e7SMoni Shoua static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4298700e3e7SMoni Shoua 			 int mask, struct ib_udata *udata)
4308700e3e7SMoni Shoua {
4318700e3e7SMoni Shoua 	int err;
4328700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
4338700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
4348700e3e7SMoni Shoua 
43526e990baSJason Gunthorpe 	if (mask & ~IB_QP_ATTR_STANDARD_BITS)
43626e990baSJason Gunthorpe 		return -EOPNOTSUPP;
43726e990baSJason Gunthorpe 
4388700e3e7SMoni Shoua 	err = rxe_qp_chk_attr(rxe, qp, attr, mask);
4398700e3e7SMoni Shoua 	if (err)
4408700e3e7SMoni Shoua 		goto err1;
4418700e3e7SMoni Shoua 
4428700e3e7SMoni Shoua 	err = rxe_qp_from_attr(qp, attr, mask, udata);
4438700e3e7SMoni Shoua 	if (err)
4448700e3e7SMoni Shoua 		goto err1;
4458700e3e7SMoni Shoua 
4468700e3e7SMoni Shoua 	return 0;
4478700e3e7SMoni Shoua 
4488700e3e7SMoni Shoua err1:
4498700e3e7SMoni Shoua 	return err;
4508700e3e7SMoni Shoua }
4518700e3e7SMoni Shoua 
4528700e3e7SMoni Shoua static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4538700e3e7SMoni Shoua 			int mask, struct ib_qp_init_attr *init)
4548700e3e7SMoni Shoua {
4558700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
4568700e3e7SMoni Shoua 
4578700e3e7SMoni Shoua 	rxe_qp_to_init(qp, init);
4588700e3e7SMoni Shoua 	rxe_qp_to_attr(qp, attr, mask);
4598700e3e7SMoni Shoua 
4608700e3e7SMoni Shoua 	return 0;
4618700e3e7SMoni Shoua }
4628700e3e7SMoni Shoua 
463c4367a26SShamir Rabinovitch static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
4648700e3e7SMoni Shoua {
4658700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
4668700e3e7SMoni Shoua 
4678700e3e7SMoni Shoua 	rxe_qp_destroy(qp);
4688700e3e7SMoni Shoua 	rxe_drop_index(qp);
4698700e3e7SMoni Shoua 	rxe_drop_ref(qp);
4708700e3e7SMoni Shoua 	return 0;
4718700e3e7SMoni Shoua }
4728700e3e7SMoni Shoua 
473f696bf6dSBart Van Assche static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
4748700e3e7SMoni Shoua 			    unsigned int mask, unsigned int length)
4758700e3e7SMoni Shoua {
4768700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
4778700e3e7SMoni Shoua 	struct rxe_sq *sq = &qp->sq;
4788700e3e7SMoni Shoua 
4798700e3e7SMoni Shoua 	if (unlikely(num_sge > sq->max_sge))
4808700e3e7SMoni Shoua 		goto err1;
4818700e3e7SMoni Shoua 
4828700e3e7SMoni Shoua 	if (unlikely(mask & WR_ATOMIC_MASK)) {
4838700e3e7SMoni Shoua 		if (length < 8)
4848700e3e7SMoni Shoua 			goto err1;
4858700e3e7SMoni Shoua 
4868700e3e7SMoni Shoua 		if (atomic_wr(ibwr)->remote_addr & 0x7)
4878700e3e7SMoni Shoua 			goto err1;
4888700e3e7SMoni Shoua 	}
4898700e3e7SMoni Shoua 
4908700e3e7SMoni Shoua 	if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
4918700e3e7SMoni Shoua 		     (length > sq->max_inline)))
4928700e3e7SMoni Shoua 		goto err1;
4938700e3e7SMoni Shoua 
4948700e3e7SMoni Shoua 	return 0;
4958700e3e7SMoni Shoua 
4968700e3e7SMoni Shoua err1:
4978700e3e7SMoni Shoua 	return -EINVAL;
4988700e3e7SMoni Shoua }
4998700e3e7SMoni Shoua 
5008700e3e7SMoni Shoua static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
501f696bf6dSBart Van Assche 			 const struct ib_send_wr *ibwr)
5028700e3e7SMoni Shoua {
5038700e3e7SMoni Shoua 	wr->wr_id = ibwr->wr_id;
5048700e3e7SMoni Shoua 	wr->num_sge = ibwr->num_sge;
5058700e3e7SMoni Shoua 	wr->opcode = ibwr->opcode;
5068700e3e7SMoni Shoua 	wr->send_flags = ibwr->send_flags;
5078700e3e7SMoni Shoua 
5088700e3e7SMoni Shoua 	if (qp_type(qp) == IB_QPT_UD ||
5098700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_SMI ||
5108700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_GSI) {
5118700e3e7SMoni Shoua 		wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
5128700e3e7SMoni Shoua 		wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
5138700e3e7SMoni Shoua 		if (qp_type(qp) == IB_QPT_GSI)
5148700e3e7SMoni Shoua 			wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
5158700e3e7SMoni Shoua 		if (wr->opcode == IB_WR_SEND_WITH_IMM)
5168700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
5178700e3e7SMoni Shoua 	} else {
5188700e3e7SMoni Shoua 		switch (wr->opcode) {
5198700e3e7SMoni Shoua 		case IB_WR_RDMA_WRITE_WITH_IMM:
5208700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
521df561f66SGustavo A. R. Silva 			fallthrough;
5228700e3e7SMoni Shoua 		case IB_WR_RDMA_READ:
5238700e3e7SMoni Shoua 		case IB_WR_RDMA_WRITE:
5248700e3e7SMoni Shoua 			wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
5258700e3e7SMoni Shoua 			wr->wr.rdma.rkey	= rdma_wr(ibwr)->rkey;
5268700e3e7SMoni Shoua 			break;
5278700e3e7SMoni Shoua 		case IB_WR_SEND_WITH_IMM:
5288700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
5298700e3e7SMoni Shoua 			break;
5308700e3e7SMoni Shoua 		case IB_WR_SEND_WITH_INV:
5318700e3e7SMoni Shoua 			wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
5328700e3e7SMoni Shoua 			break;
5338700e3e7SMoni Shoua 		case IB_WR_ATOMIC_CMP_AND_SWP:
5348700e3e7SMoni Shoua 		case IB_WR_ATOMIC_FETCH_AND_ADD:
5358700e3e7SMoni Shoua 			wr->wr.atomic.remote_addr =
5368700e3e7SMoni Shoua 				atomic_wr(ibwr)->remote_addr;
5378700e3e7SMoni Shoua 			wr->wr.atomic.compare_add =
5388700e3e7SMoni Shoua 				atomic_wr(ibwr)->compare_add;
5398700e3e7SMoni Shoua 			wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
5408700e3e7SMoni Shoua 			wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
5418700e3e7SMoni Shoua 			break;
5428700e3e7SMoni Shoua 		case IB_WR_LOCAL_INV:
5438700e3e7SMoni Shoua 			wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
5448700e3e7SMoni Shoua 		break;
5458700e3e7SMoni Shoua 		case IB_WR_REG_MR:
5468700e3e7SMoni Shoua 			wr->wr.reg.mr = reg_wr(ibwr)->mr;
5478700e3e7SMoni Shoua 			wr->wr.reg.key = reg_wr(ibwr)->key;
5488700e3e7SMoni Shoua 			wr->wr.reg.access = reg_wr(ibwr)->access;
5498700e3e7SMoni Shoua 		break;
5508700e3e7SMoni Shoua 		default:
5518700e3e7SMoni Shoua 			break;
5528700e3e7SMoni Shoua 		}
5538700e3e7SMoni Shoua 	}
5548700e3e7SMoni Shoua }
5558700e3e7SMoni Shoua 
556086f580cSBob Pearson static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe,
557086f580cSBob Pearson 				    const struct ib_send_wr *ibwr)
558086f580cSBob Pearson {
559086f580cSBob Pearson 	struct ib_sge *sge = ibwr->sg_list;
560086f580cSBob Pearson 	u8 *p = wqe->dma.inline_data;
561086f580cSBob Pearson 	int i;
562086f580cSBob Pearson 
563086f580cSBob Pearson 	for (i = 0; i < ibwr->num_sge; i++, sge++) {
564086f580cSBob Pearson 		memcpy(p, (void *)(uintptr_t)sge->addr, sge->length);
565086f580cSBob Pearson 		p += sge->length;
566086f580cSBob Pearson 	}
567086f580cSBob Pearson }
568086f580cSBob Pearson 
569086f580cSBob Pearson static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
5708700e3e7SMoni Shoua 			 unsigned int mask, unsigned int length,
5718700e3e7SMoni Shoua 			 struct rxe_send_wqe *wqe)
5728700e3e7SMoni Shoua {
5738700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
5748700e3e7SMoni Shoua 
5758700e3e7SMoni Shoua 	init_send_wr(qp, &wqe->wr, ibwr);
5768700e3e7SMoni Shoua 
577dc78074aSBob Pearson 	/* local operation */
578886441fbSBob Pearson 	if (unlikely(mask & WR_LOCAL_OP_MASK)) {
579dc78074aSBob Pearson 		wqe->mask = mask;
580dc78074aSBob Pearson 		wqe->state = wqe_state_posted;
581086f580cSBob Pearson 		return;
582dc78074aSBob Pearson 	}
583dc78074aSBob Pearson 
5848700e3e7SMoni Shoua 	if (qp_type(qp) == IB_QPT_UD ||
5858700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_SMI ||
5868700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_GSI)
5878700e3e7SMoni Shoua 		memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
5888700e3e7SMoni Shoua 
589086f580cSBob Pearson 	if (unlikely(ibwr->send_flags & IB_SEND_INLINE))
590086f580cSBob Pearson 		copy_inline_data_to_wqe(wqe, ibwr);
591086f580cSBob Pearson 	else
5928700e3e7SMoni Shoua 		memcpy(wqe->dma.sge, ibwr->sg_list,
5938700e3e7SMoni Shoua 		       num_sge * sizeof(struct ib_sge));
5948700e3e7SMoni Shoua 
595a6544a62SBart Van Assche 	wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
596a6544a62SBart Van Assche 		mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
5978700e3e7SMoni Shoua 	wqe->mask		= mask;
5988700e3e7SMoni Shoua 	wqe->dma.length		= length;
5998700e3e7SMoni Shoua 	wqe->dma.resid		= length;
6008700e3e7SMoni Shoua 	wqe->dma.num_sge	= num_sge;
6018700e3e7SMoni Shoua 	wqe->dma.cur_sge	= 0;
6028700e3e7SMoni Shoua 	wqe->dma.sge_offset	= 0;
6038700e3e7SMoni Shoua 	wqe->state		= wqe_state_posted;
6048700e3e7SMoni Shoua 	wqe->ssn		= atomic_add_return(1, &qp->ssn);
6058700e3e7SMoni Shoua }
6068700e3e7SMoni Shoua 
607f696bf6dSBart Van Assche static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
608e404f945SParav Pandit 			 unsigned int mask, u32 length)
6098700e3e7SMoni Shoua {
6108700e3e7SMoni Shoua 	int err;
6118700e3e7SMoni Shoua 	struct rxe_sq *sq = &qp->sq;
6128700e3e7SMoni Shoua 	struct rxe_send_wqe *send_wqe;
6138700e3e7SMoni Shoua 	unsigned long flags;
6145bcf5a59SBob Pearson 	int full;
6158700e3e7SMoni Shoua 
6168700e3e7SMoni Shoua 	err = validate_send_wr(qp, ibwr, mask, length);
6178700e3e7SMoni Shoua 	if (err)
6188700e3e7SMoni Shoua 		return err;
6198700e3e7SMoni Shoua 
6208700e3e7SMoni Shoua 	spin_lock_irqsave(&qp->sq.sq_lock, flags);
6218700e3e7SMoni Shoua 
622*ae6e843fSBob Pearson 	full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER);
6235bcf5a59SBob Pearson 
6245bcf5a59SBob Pearson 	if (unlikely(full)) {
6255bcf5a59SBob Pearson 		spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
6265bcf5a59SBob Pearson 		return -ENOMEM;
6278700e3e7SMoni Shoua 	}
6288700e3e7SMoni Shoua 
629*ae6e843fSBob Pearson 	send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER);
630086f580cSBob Pearson 	init_send_wqe(qp, ibwr, mask, length, send_wqe);
6318700e3e7SMoni Shoua 
632*ae6e843fSBob Pearson 	queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER);
6335bcf5a59SBob Pearson 
6348700e3e7SMoni Shoua 	spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
6358700e3e7SMoni Shoua 
6368700e3e7SMoni Shoua 	return 0;
6378700e3e7SMoni Shoua }
6388700e3e7SMoni Shoua 
639d34ac5cdSBart Van Assche static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
640d34ac5cdSBart Van Assche 				const struct ib_send_wr **bad_wr)
6418700e3e7SMoni Shoua {
6428700e3e7SMoni Shoua 	int err = 0;
6438700e3e7SMoni Shoua 	unsigned int mask;
6448700e3e7SMoni Shoua 	unsigned int length = 0;
6458700e3e7SMoni Shoua 	int i;
6465f0b2a60SMikhail Malygin 	struct ib_send_wr *next;
6478700e3e7SMoni Shoua 
6488700e3e7SMoni Shoua 	while (wr) {
6498700e3e7SMoni Shoua 		mask = wr_opcode_mask(wr->opcode, qp);
6508700e3e7SMoni Shoua 		if (unlikely(!mask)) {
6518700e3e7SMoni Shoua 			err = -EINVAL;
6528700e3e7SMoni Shoua 			*bad_wr = wr;
6538700e3e7SMoni Shoua 			break;
6548700e3e7SMoni Shoua 		}
6558700e3e7SMoni Shoua 
6568700e3e7SMoni Shoua 		if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
6578700e3e7SMoni Shoua 			     !(mask & WR_INLINE_MASK))) {
6588700e3e7SMoni Shoua 			err = -EINVAL;
6598700e3e7SMoni Shoua 			*bad_wr = wr;
6608700e3e7SMoni Shoua 			break;
6618700e3e7SMoni Shoua 		}
6628700e3e7SMoni Shoua 
6635f0b2a60SMikhail Malygin 		next = wr->next;
6645f0b2a60SMikhail Malygin 
6658700e3e7SMoni Shoua 		length = 0;
6668700e3e7SMoni Shoua 		for (i = 0; i < wr->num_sge; i++)
6678700e3e7SMoni Shoua 			length += wr->sg_list[i].length;
6688700e3e7SMoni Shoua 
6698700e3e7SMoni Shoua 		err = post_one_send(qp, wr, mask, length);
6708700e3e7SMoni Shoua 
6718700e3e7SMoni Shoua 		if (err) {
6728700e3e7SMoni Shoua 			*bad_wr = wr;
6738700e3e7SMoni Shoua 			break;
6748700e3e7SMoni Shoua 		}
6755f0b2a60SMikhail Malygin 		wr = next;
6768700e3e7SMoni Shoua 	}
6778700e3e7SMoni Shoua 
6781661d3b0SAlexandru Moise 	rxe_run_task(&qp->req.task, 1);
6796f301e06SBart Van Assche 	if (unlikely(qp->req.state == QP_STATE_ERROR))
6806f301e06SBart Van Assche 		rxe_run_task(&qp->comp.task, 1);
6818700e3e7SMoni Shoua 
6828700e3e7SMoni Shoua 	return err;
6838700e3e7SMoni Shoua }
6848700e3e7SMoni Shoua 
685d34ac5cdSBart Van Assche static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
686d34ac5cdSBart Van Assche 			 const struct ib_send_wr **bad_wr)
687063af595SParav Pandit {
688063af595SParav Pandit 	struct rxe_qp *qp = to_rqp(ibqp);
689063af595SParav Pandit 
690063af595SParav Pandit 	if (unlikely(!qp->valid)) {
691063af595SParav Pandit 		*bad_wr = wr;
692063af595SParav Pandit 		return -EINVAL;
693063af595SParav Pandit 	}
694063af595SParav Pandit 
695063af595SParav Pandit 	if (unlikely(qp->req.state < QP_STATE_READY)) {
696063af595SParav Pandit 		*bad_wr = wr;
697063af595SParav Pandit 		return -EINVAL;
698063af595SParav Pandit 	}
699063af595SParav Pandit 
700063af595SParav Pandit 	if (qp->is_user) {
701063af595SParav Pandit 		/* Utilize process context to do protocol processing */
702063af595SParav Pandit 		rxe_run_task(&qp->req.task, 0);
703063af595SParav Pandit 		return 0;
704063af595SParav Pandit 	} else
705063af595SParav Pandit 		return rxe_post_send_kernel(qp, wr, bad_wr);
706063af595SParav Pandit }
707063af595SParav Pandit 
708d34ac5cdSBart Van Assche static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
709d34ac5cdSBart Van Assche 			 const struct ib_recv_wr **bad_wr)
7108700e3e7SMoni Shoua {
7118700e3e7SMoni Shoua 	int err = 0;
7128700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
7138700e3e7SMoni Shoua 	struct rxe_rq *rq = &qp->rq;
7148700e3e7SMoni Shoua 	unsigned long flags;
7158700e3e7SMoni Shoua 
7168700e3e7SMoni Shoua 	if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
7178700e3e7SMoni Shoua 		*bad_wr = wr;
7188700e3e7SMoni Shoua 		err = -EINVAL;
7198700e3e7SMoni Shoua 		goto err1;
7208700e3e7SMoni Shoua 	}
7218700e3e7SMoni Shoua 
7228700e3e7SMoni Shoua 	if (unlikely(qp->srq)) {
7238700e3e7SMoni Shoua 		*bad_wr = wr;
7248700e3e7SMoni Shoua 		err = -EINVAL;
7258700e3e7SMoni Shoua 		goto err1;
7268700e3e7SMoni Shoua 	}
7278700e3e7SMoni Shoua 
7288700e3e7SMoni Shoua 	spin_lock_irqsave(&rq->producer_lock, flags);
7298700e3e7SMoni Shoua 
7308700e3e7SMoni Shoua 	while (wr) {
7318700e3e7SMoni Shoua 		err = post_one_recv(rq, wr);
7328700e3e7SMoni Shoua 		if (unlikely(err)) {
7338700e3e7SMoni Shoua 			*bad_wr = wr;
7348700e3e7SMoni Shoua 			break;
7358700e3e7SMoni Shoua 		}
7368700e3e7SMoni Shoua 		wr = wr->next;
7378700e3e7SMoni Shoua 	}
7388700e3e7SMoni Shoua 
7398700e3e7SMoni Shoua 	spin_unlock_irqrestore(&rq->producer_lock, flags);
7408700e3e7SMoni Shoua 
74112171971SVijay Immanuel 	if (qp->resp.state == QP_STATE_ERROR)
74212171971SVijay Immanuel 		rxe_run_task(&qp->resp.task, 1);
74312171971SVijay Immanuel 
7448700e3e7SMoni Shoua err1:
7458700e3e7SMoni Shoua 	return err;
7468700e3e7SMoni Shoua }
7478700e3e7SMoni Shoua 
748e39afe3dSLeon Romanovsky static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
7498700e3e7SMoni Shoua 			 struct ib_udata *udata)
7508700e3e7SMoni Shoua {
7518700e3e7SMoni Shoua 	int err;
752e39afe3dSLeon Romanovsky 	struct ib_device *dev = ibcq->device;
7538700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
754e39afe3dSLeon Romanovsky 	struct rxe_cq *cq = to_rcq(ibcq);
7550c43ab37SJason Gunthorpe 	struct rxe_create_cq_resp __user *uresp = NULL;
7560c43ab37SJason Gunthorpe 
7570c43ab37SJason Gunthorpe 	if (udata) {
7580c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
759e39afe3dSLeon Romanovsky 			return -EINVAL;
7600c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
7610c43ab37SJason Gunthorpe 	}
7628700e3e7SMoni Shoua 
7638700e3e7SMoni Shoua 	if (attr->flags)
7641c407cb5SJason Gunthorpe 		return -EOPNOTSUPP;
7658700e3e7SMoni Shoua 
766b92ec0feSJason Gunthorpe 	err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
7678700e3e7SMoni Shoua 	if (err)
768e39afe3dSLeon Romanovsky 		return err;
7698700e3e7SMoni Shoua 
770ff23dfa1SShamir Rabinovitch 	err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
771ff23dfa1SShamir Rabinovitch 			       uresp);
7728700e3e7SMoni Shoua 	if (err)
773e39afe3dSLeon Romanovsky 		return err;
7748700e3e7SMoni Shoua 
77591a42c5bSBob Pearson 	return rxe_add_to_pool(&rxe->cq_pool, cq);
7768700e3e7SMoni Shoua }
7778700e3e7SMoni Shoua 
77843d781b9SLeon Romanovsky static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
7798700e3e7SMoni Shoua {
7808700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
7818700e3e7SMoni Shoua 
782bfc3ae05SAndrew Boyer 	rxe_cq_disable(cq);
783bfc3ae05SAndrew Boyer 
7848700e3e7SMoni Shoua 	rxe_drop_ref(cq);
78543d781b9SLeon Romanovsky 	return 0;
7868700e3e7SMoni Shoua }
7878700e3e7SMoni Shoua 
7888700e3e7SMoni Shoua static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
7898700e3e7SMoni Shoua {
7908700e3e7SMoni Shoua 	int err;
7918700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
7928700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibcq->device);
7930c43ab37SJason Gunthorpe 	struct rxe_resize_cq_resp __user *uresp = NULL;
7940c43ab37SJason Gunthorpe 
7950c43ab37SJason Gunthorpe 	if (udata) {
7960c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
7970c43ab37SJason Gunthorpe 			return -EINVAL;
7980c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
7990c43ab37SJason Gunthorpe 	}
8008700e3e7SMoni Shoua 
801b92ec0feSJason Gunthorpe 	err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
8028700e3e7SMoni Shoua 	if (err)
8038700e3e7SMoni Shoua 		goto err1;
8048700e3e7SMoni Shoua 
805ff23dfa1SShamir Rabinovitch 	err = rxe_cq_resize_queue(cq, cqe, uresp, udata);
8068700e3e7SMoni Shoua 	if (err)
8078700e3e7SMoni Shoua 		goto err1;
8088700e3e7SMoni Shoua 
8098700e3e7SMoni Shoua 	return 0;
8108700e3e7SMoni Shoua 
8118700e3e7SMoni Shoua err1:
8128700e3e7SMoni Shoua 	return err;
8138700e3e7SMoni Shoua }
8148700e3e7SMoni Shoua 
8158700e3e7SMoni Shoua static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
8168700e3e7SMoni Shoua {
8178700e3e7SMoni Shoua 	int i;
8188700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8198700e3e7SMoni Shoua 	struct rxe_cqe *cqe;
8208700e3e7SMoni Shoua 	unsigned long flags;
8218700e3e7SMoni Shoua 
8228700e3e7SMoni Shoua 	spin_lock_irqsave(&cq->cq_lock, flags);
8238700e3e7SMoni Shoua 	for (i = 0; i < num_entries; i++) {
824*ae6e843fSBob Pearson 		cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER);
8258700e3e7SMoni Shoua 		if (!cqe)
8268700e3e7SMoni Shoua 			break;
8278700e3e7SMoni Shoua 
8288700e3e7SMoni Shoua 		memcpy(wc++, &cqe->ibwc, sizeof(*wc));
829*ae6e843fSBob Pearson 		queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER);
8308700e3e7SMoni Shoua 	}
8318700e3e7SMoni Shoua 	spin_unlock_irqrestore(&cq->cq_lock, flags);
8328700e3e7SMoni Shoua 
8338700e3e7SMoni Shoua 	return i;
8348700e3e7SMoni Shoua }
8358700e3e7SMoni Shoua 
8368700e3e7SMoni Shoua static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
8378700e3e7SMoni Shoua {
8388700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8395bcf5a59SBob Pearson 	int count;
8405bcf5a59SBob Pearson 
841*ae6e843fSBob Pearson 	count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER);
8428700e3e7SMoni Shoua 
8438700e3e7SMoni Shoua 	return (count > wc_cnt) ? wc_cnt : count;
8448700e3e7SMoni Shoua }
8458700e3e7SMoni Shoua 
8468700e3e7SMoni Shoua static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
8478700e3e7SMoni Shoua {
8488700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
849accacb8fSAndrew Boyer 	unsigned long irq_flags;
850accacb8fSAndrew Boyer 	int ret = 0;
8515bcf5a59SBob Pearson 	int empty;
8528700e3e7SMoni Shoua 
853accacb8fSAndrew Boyer 	spin_lock_irqsave(&cq->cq_lock, irq_flags);
8548700e3e7SMoni Shoua 	if (cq->notify != IB_CQ_NEXT_COMP)
8558700e3e7SMoni Shoua 		cq->notify = flags & IB_CQ_SOLICITED_MASK;
8568700e3e7SMoni Shoua 
857*ae6e843fSBob Pearson 	empty = queue_empty(cq->queue, QUEUE_TYPE_FROM_DRIVER);
8585bcf5a59SBob Pearson 
8595bcf5a59SBob Pearson 	if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty)
860accacb8fSAndrew Boyer 		ret = 1;
861accacb8fSAndrew Boyer 
862accacb8fSAndrew Boyer 	spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
863accacb8fSAndrew Boyer 
864accacb8fSAndrew Boyer 	return ret;
8658700e3e7SMoni Shoua }
8668700e3e7SMoni Shoua 
8678700e3e7SMoni Shoua static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
8688700e3e7SMoni Shoua {
8698700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
8708700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
871364e282cSBob Pearson 	struct rxe_mr *mr;
8728700e3e7SMoni Shoua 
8738700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
874293d8440SKamal Heib 	if (!mr)
875293d8440SKamal Heib 		return ERR_PTR(-ENOMEM);
8768700e3e7SMoni Shoua 
8778700e3e7SMoni Shoua 	rxe_add_index(mr);
8788700e3e7SMoni Shoua 	rxe_add_ref(pd);
879364e282cSBob Pearson 	rxe_mr_init_dma(pd, access, mr);
8808700e3e7SMoni Shoua 
8818700e3e7SMoni Shoua 	return &mr->ibmr;
8828700e3e7SMoni Shoua }
8838700e3e7SMoni Shoua 
8848700e3e7SMoni Shoua static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
8858700e3e7SMoni Shoua 				     u64 start,
8868700e3e7SMoni Shoua 				     u64 length,
8878700e3e7SMoni Shoua 				     u64 iova,
8888700e3e7SMoni Shoua 				     int access, struct ib_udata *udata)
8898700e3e7SMoni Shoua {
8908700e3e7SMoni Shoua 	int err;
8918700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
8928700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
893364e282cSBob Pearson 	struct rxe_mr *mr;
8948700e3e7SMoni Shoua 
8958700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
8968700e3e7SMoni Shoua 	if (!mr) {
8978700e3e7SMoni Shoua 		err = -ENOMEM;
8988700e3e7SMoni Shoua 		goto err2;
8998700e3e7SMoni Shoua 	}
9008700e3e7SMoni Shoua 
9018700e3e7SMoni Shoua 	rxe_add_index(mr);
9028700e3e7SMoni Shoua 
9038700e3e7SMoni Shoua 	rxe_add_ref(pd);
9048700e3e7SMoni Shoua 
905cd5b010fSLang Cheng 	err = rxe_mr_init_user(pd, start, length, iova, access, mr);
9068700e3e7SMoni Shoua 	if (err)
9078700e3e7SMoni Shoua 		goto err3;
9088700e3e7SMoni Shoua 
9098700e3e7SMoni Shoua 	return &mr->ibmr;
9108700e3e7SMoni Shoua 
9118700e3e7SMoni Shoua err3:
9128700e3e7SMoni Shoua 	rxe_drop_ref(pd);
9138700e3e7SMoni Shoua 	rxe_drop_index(mr);
9148700e3e7SMoni Shoua 	rxe_drop_ref(mr);
9158700e3e7SMoni Shoua err2:
9168700e3e7SMoni Shoua 	return ERR_PTR(err);
9178700e3e7SMoni Shoua }
9188700e3e7SMoni Shoua 
919c4367a26SShamir Rabinovitch static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
92042a3b153SGal Pressman 				  u32 max_num_sg)
9218700e3e7SMoni Shoua {
9228700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
9238700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
924364e282cSBob Pearson 	struct rxe_mr *mr;
9258700e3e7SMoni Shoua 	int err;
9268700e3e7SMoni Shoua 
9278700e3e7SMoni Shoua 	if (mr_type != IB_MR_TYPE_MEM_REG)
9288700e3e7SMoni Shoua 		return ERR_PTR(-EINVAL);
9298700e3e7SMoni Shoua 
9308700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
9318700e3e7SMoni Shoua 	if (!mr) {
9328700e3e7SMoni Shoua 		err = -ENOMEM;
9338700e3e7SMoni Shoua 		goto err1;
9348700e3e7SMoni Shoua 	}
9358700e3e7SMoni Shoua 
9368700e3e7SMoni Shoua 	rxe_add_index(mr);
9378700e3e7SMoni Shoua 
9388700e3e7SMoni Shoua 	rxe_add_ref(pd);
9398700e3e7SMoni Shoua 
940364e282cSBob Pearson 	err = rxe_mr_init_fast(pd, max_num_sg, mr);
9418700e3e7SMoni Shoua 	if (err)
9428700e3e7SMoni Shoua 		goto err2;
9438700e3e7SMoni Shoua 
9448700e3e7SMoni Shoua 	return &mr->ibmr;
9458700e3e7SMoni Shoua 
9468700e3e7SMoni Shoua err2:
9478700e3e7SMoni Shoua 	rxe_drop_ref(pd);
9488700e3e7SMoni Shoua 	rxe_drop_index(mr);
9498700e3e7SMoni Shoua 	rxe_drop_ref(mr);
9508700e3e7SMoni Shoua err1:
9518700e3e7SMoni Shoua 	return ERR_PTR(err);
9528700e3e7SMoni Shoua }
9538700e3e7SMoni Shoua 
9548700e3e7SMoni Shoua static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
9558700e3e7SMoni Shoua {
956364e282cSBob Pearson 	struct rxe_mr *mr = to_rmr(ibmr);
9578700e3e7SMoni Shoua 	struct rxe_map *map;
9588700e3e7SMoni Shoua 	struct rxe_phys_buf *buf;
9598700e3e7SMoni Shoua 
9608700e3e7SMoni Shoua 	if (unlikely(mr->nbuf == mr->num_buf))
9618700e3e7SMoni Shoua 		return -ENOMEM;
9628700e3e7SMoni Shoua 
9638700e3e7SMoni Shoua 	map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
9648700e3e7SMoni Shoua 	buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
9658700e3e7SMoni Shoua 
9668700e3e7SMoni Shoua 	buf->addr = addr;
9678700e3e7SMoni Shoua 	buf->size = ibmr->page_size;
9688700e3e7SMoni Shoua 	mr->nbuf++;
9698700e3e7SMoni Shoua 
9708700e3e7SMoni Shoua 	return 0;
9718700e3e7SMoni Shoua }
9728700e3e7SMoni Shoua 
973e404f945SParav Pandit static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
974e404f945SParav Pandit 			 int sg_nents, unsigned int *sg_offset)
9758700e3e7SMoni Shoua {
976364e282cSBob Pearson 	struct rxe_mr *mr = to_rmr(ibmr);
9778700e3e7SMoni Shoua 	int n;
9788700e3e7SMoni Shoua 
9798700e3e7SMoni Shoua 	mr->nbuf = 0;
9808700e3e7SMoni Shoua 
9818700e3e7SMoni Shoua 	n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
9828700e3e7SMoni Shoua 
9838700e3e7SMoni Shoua 	mr->va = ibmr->iova;
9848700e3e7SMoni Shoua 	mr->iova = ibmr->iova;
9858700e3e7SMoni Shoua 	mr->length = ibmr->length;
9868700e3e7SMoni Shoua 	mr->page_shift = ilog2(ibmr->page_size);
9878700e3e7SMoni Shoua 	mr->page_mask = ibmr->page_size - 1;
9888700e3e7SMoni Shoua 	mr->offset = mr->iova & mr->page_mask;
9898700e3e7SMoni Shoua 
9908700e3e7SMoni Shoua 	return n;
9918700e3e7SMoni Shoua }
9928700e3e7SMoni Shoua 
9938700e3e7SMoni Shoua static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
9948700e3e7SMoni Shoua {
9958700e3e7SMoni Shoua 	int err;
9968700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
9978700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
9988700e3e7SMoni Shoua 	struct rxe_mc_grp *grp;
9998700e3e7SMoni Shoua 
10008700e3e7SMoni Shoua 	/* takes a ref on grp if successful */
10018700e3e7SMoni Shoua 	err = rxe_mcast_get_grp(rxe, mgid, &grp);
10028700e3e7SMoni Shoua 	if (err)
10038700e3e7SMoni Shoua 		return err;
10048700e3e7SMoni Shoua 
10058700e3e7SMoni Shoua 	err = rxe_mcast_add_grp_elem(rxe, qp, grp);
10068700e3e7SMoni Shoua 
10078700e3e7SMoni Shoua 	rxe_drop_ref(grp);
10088700e3e7SMoni Shoua 	return err;
10098700e3e7SMoni Shoua }
10108700e3e7SMoni Shoua 
10118700e3e7SMoni Shoua static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
10128700e3e7SMoni Shoua {
10138700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
10148700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
10158700e3e7SMoni Shoua 
10168700e3e7SMoni Shoua 	return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
10178700e3e7SMoni Shoua }
10188700e3e7SMoni Shoua 
1019c05d2664SKamal Heib static ssize_t parent_show(struct device *device,
10208700e3e7SMoni Shoua 			   struct device_attribute *attr, char *buf)
10218700e3e7SMoni Shoua {
102254747231SParav Pandit 	struct rxe_dev *rxe =
102354747231SParav Pandit 		rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
10248700e3e7SMoni Shoua 
10251c7fd726SJoe Perches 	return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1));
10268700e3e7SMoni Shoua }
10278700e3e7SMoni Shoua 
1028c05d2664SKamal Heib static DEVICE_ATTR_RO(parent);
10298700e3e7SMoni Shoua 
1030508a523fSParav Pandit static struct attribute *rxe_dev_attributes[] = {
1031508a523fSParav Pandit 	&dev_attr_parent.attr,
1032508a523fSParav Pandit 	NULL
1033508a523fSParav Pandit };
1034508a523fSParav Pandit 
1035508a523fSParav Pandit static const struct attribute_group rxe_attr_group = {
1036508a523fSParav Pandit 	.attrs = rxe_dev_attributes,
10378700e3e7SMoni Shoua };
10388700e3e7SMoni Shoua 
1039ca22354bSJason Gunthorpe static int rxe_enable_driver(struct ib_device *ib_dev)
1040ca22354bSJason Gunthorpe {
1041ca22354bSJason Gunthorpe 	struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1042ca22354bSJason Gunthorpe 
1043ca22354bSJason Gunthorpe 	rxe_set_port_state(rxe);
1044ca22354bSJason Gunthorpe 	dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1045ca22354bSJason Gunthorpe 	return 0;
1046ca22354bSJason Gunthorpe }
1047ca22354bSJason Gunthorpe 
1048573efc4bSKamal Heib static const struct ib_device_ops rxe_dev_ops = {
10497a154142SJason Gunthorpe 	.owner = THIS_MODULE,
1050b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_RXE,
105172c6ec18SJason Gunthorpe 	.uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
1052b9560a41SJason Gunthorpe 
10534b5f4d3fSJason Gunthorpe 	.alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats,
1054573efc4bSKamal Heib 	.alloc_mr = rxe_alloc_mr,
1055beec0239SBob Pearson 	.alloc_mw = rxe_alloc_mw,
1056573efc4bSKamal Heib 	.alloc_pd = rxe_alloc_pd,
1057573efc4bSKamal Heib 	.alloc_ucontext = rxe_alloc_ucontext,
1058573efc4bSKamal Heib 	.attach_mcast = rxe_attach_mcast,
1059573efc4bSKamal Heib 	.create_ah = rxe_create_ah,
1060573efc4bSKamal Heib 	.create_cq = rxe_create_cq,
1061573efc4bSKamal Heib 	.create_qp = rxe_create_qp,
1062573efc4bSKamal Heib 	.create_srq = rxe_create_srq,
1063676a80adSJason Gunthorpe 	.create_user_ah = rxe_create_ah,
1064c367074bSJason Gunthorpe 	.dealloc_driver = rxe_dealloc,
1065beec0239SBob Pearson 	.dealloc_mw = rxe_dealloc_mw,
1066573efc4bSKamal Heib 	.dealloc_pd = rxe_dealloc_pd,
1067573efc4bSKamal Heib 	.dealloc_ucontext = rxe_dealloc_ucontext,
1068573efc4bSKamal Heib 	.dereg_mr = rxe_dereg_mr,
1069573efc4bSKamal Heib 	.destroy_ah = rxe_destroy_ah,
1070573efc4bSKamal Heib 	.destroy_cq = rxe_destroy_cq,
1071573efc4bSKamal Heib 	.destroy_qp = rxe_destroy_qp,
1072573efc4bSKamal Heib 	.destroy_srq = rxe_destroy_srq,
1073573efc4bSKamal Heib 	.detach_mcast = rxe_detach_mcast,
1074915e4af5SJason Gunthorpe 	.device_group = &rxe_attr_group,
1075ca22354bSJason Gunthorpe 	.enable_driver = rxe_enable_driver,
1076573efc4bSKamal Heib 	.get_dma_mr = rxe_get_dma_mr,
1077573efc4bSKamal Heib 	.get_hw_stats = rxe_ib_get_hw_stats,
1078573efc4bSKamal Heib 	.get_link_layer = rxe_get_link_layer,
1079573efc4bSKamal Heib 	.get_port_immutable = rxe_port_immutable,
1080573efc4bSKamal Heib 	.map_mr_sg = rxe_map_mr_sg,
1081573efc4bSKamal Heib 	.mmap = rxe_mmap,
1082573efc4bSKamal Heib 	.modify_ah = rxe_modify_ah,
1083573efc4bSKamal Heib 	.modify_device = rxe_modify_device,
1084573efc4bSKamal Heib 	.modify_port = rxe_modify_port,
1085573efc4bSKamal Heib 	.modify_qp = rxe_modify_qp,
1086573efc4bSKamal Heib 	.modify_srq = rxe_modify_srq,
1087573efc4bSKamal Heib 	.peek_cq = rxe_peek_cq,
1088573efc4bSKamal Heib 	.poll_cq = rxe_poll_cq,
1089573efc4bSKamal Heib 	.post_recv = rxe_post_recv,
1090573efc4bSKamal Heib 	.post_send = rxe_post_send,
1091573efc4bSKamal Heib 	.post_srq_recv = rxe_post_srq_recv,
1092573efc4bSKamal Heib 	.query_ah = rxe_query_ah,
1093573efc4bSKamal Heib 	.query_device = rxe_query_device,
1094573efc4bSKamal Heib 	.query_pkey = rxe_query_pkey,
1095573efc4bSKamal Heib 	.query_port = rxe_query_port,
1096573efc4bSKamal Heib 	.query_qp = rxe_query_qp,
1097573efc4bSKamal Heib 	.query_srq = rxe_query_srq,
1098573efc4bSKamal Heib 	.reg_user_mr = rxe_reg_user_mr,
1099573efc4bSKamal Heib 	.req_notify_cq = rxe_req_notify_cq,
1100573efc4bSKamal Heib 	.resize_cq = rxe_resize_cq,
1101d3456914SLeon Romanovsky 
1102d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
1103e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
110421a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
1105514aee66SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp),
110668e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
1107a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1108364e282cSBob Pearson 	INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw),
1109573efc4bSKamal Heib };
1110573efc4bSKamal Heib 
111166920e1bSSteve Wise int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
11128700e3e7SMoni Shoua {
11138700e3e7SMoni Shoua 	int err;
11148700e3e7SMoni Shoua 	struct ib_device *dev = &rxe->ib_dev;
11158700e3e7SMoni Shoua 
1116dc78074aSBob Pearson 	strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
11178700e3e7SMoni Shoua 
11188700e3e7SMoni Shoua 	dev->node_type = RDMA_NODE_IB_CA;
11198700e3e7SMoni Shoua 	dev->phys_port_cnt = 1;
112067cf3623SSagi Grimberg 	dev->num_comp_vectors = num_possible_cpus();
11218700e3e7SMoni Shoua 	dev->local_dma_lkey = 0;
11224d6f2859SYuval Shaia 	addrconf_addr_eui48((unsigned char *)&dev->node_guid,
11234d6f2859SYuval Shaia 			    rxe->ndev->dev_addr);
11248700e3e7SMoni Shoua 
11255c419366SJason Gunthorpe 	dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) |
11265c419366SJason Gunthorpe 				BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ);
11278700e3e7SMoni Shoua 
1128573efc4bSKamal Heib 	ib_set_device_ops(dev, &rxe_dev_ops);
11294c173f59SJason Gunthorpe 	err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
11304c173f59SJason Gunthorpe 	if (err)
11314c173f59SJason Gunthorpe 		return err;
11328700e3e7SMoni Shoua 
1133add2b3b8SBob Pearson 	err = rxe_icrc_init(rxe);
1134add2b3b8SBob Pearson 	if (err)
1135add2b3b8SBob Pearson 		return err;
1136cee2688eSyonatanc 
1137e0477b34SJason Gunthorpe 	err = ib_register_device(dev, ibdev_name, NULL);
1138c367074bSJason Gunthorpe 	if (err)
113961013828SKamal Heib 		pr_warn("%s failed with error %d\n", __func__, err);
1140cee2688eSyonatanc 
1141ca22354bSJason Gunthorpe 	/*
1142ca22354bSJason Gunthorpe 	 * Note that rxe may be invalid at this point if another thread
1143ca22354bSJason Gunthorpe 	 * unregistered it.
1144ca22354bSJason Gunthorpe 	 */
11458700e3e7SMoni Shoua 	return err;
11468700e3e7SMoni Shoua }
1147