18700e3e7SMoni Shoua /*
28700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
38700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
48700e3e7SMoni Shoua  *
58700e3e7SMoni Shoua  * This software is available to you under a choice of one of two
68700e3e7SMoni Shoua  * licenses.  You may choose to be licensed under the terms of the GNU
78700e3e7SMoni Shoua  * General Public License (GPL) Version 2, available from the file
88700e3e7SMoni Shoua  * COPYING in the main directory of this source tree, or the
98700e3e7SMoni Shoua  * OpenIB.org BSD license below:
108700e3e7SMoni Shoua  *
118700e3e7SMoni Shoua  *     Redistribution and use in source and binary forms, with or
128700e3e7SMoni Shoua  *     without modification, are permitted provided that the following
138700e3e7SMoni Shoua  *     conditions are met:
148700e3e7SMoni Shoua  *
158700e3e7SMoni Shoua  *	- Redistributions of source code must retain the above
168700e3e7SMoni Shoua  *	  copyright notice, this list of conditions and the following
178700e3e7SMoni Shoua  *	  disclaimer.
188700e3e7SMoni Shoua  *
198700e3e7SMoni Shoua  *	- Redistributions in binary form must reproduce the above
208700e3e7SMoni Shoua  *	  copyright notice, this list of conditions and the following
218700e3e7SMoni Shoua  *	  disclaimer in the documentation and/or other materials
228700e3e7SMoni Shoua  *	  provided with the distribution.
238700e3e7SMoni Shoua  *
248700e3e7SMoni Shoua  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
258700e3e7SMoni Shoua  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
268700e3e7SMoni Shoua  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
278700e3e7SMoni Shoua  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
288700e3e7SMoni Shoua  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
298700e3e7SMoni Shoua  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
308700e3e7SMoni Shoua  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
318700e3e7SMoni Shoua  * SOFTWARE.
328700e3e7SMoni Shoua  */
338700e3e7SMoni Shoua 
340bbb3b74SBart Van Assche #include <linux/dma-mapping.h>
354d6f2859SYuval Shaia #include <net/addrconf.h>
3689944450SShamir Rabinovitch #include <rdma/uverbs_ioctl.h>
378700e3e7SMoni Shoua #include "rxe.h"
388700e3e7SMoni Shoua #include "rxe_loc.h"
398700e3e7SMoni Shoua #include "rxe_queue.h"
400b1e5b99SYonatan Cohen #include "rxe_hw_counters.h"
418700e3e7SMoni Shoua 
428700e3e7SMoni Shoua static int rxe_query_device(struct ib_device *dev,
438700e3e7SMoni Shoua 			    struct ib_device_attr *attr,
448700e3e7SMoni Shoua 			    struct ib_udata *uhw)
458700e3e7SMoni Shoua {
468700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
478700e3e7SMoni Shoua 
488700e3e7SMoni Shoua 	if (uhw->inlen || uhw->outlen)
498700e3e7SMoni Shoua 		return -EINVAL;
508700e3e7SMoni Shoua 
518700e3e7SMoni Shoua 	*attr = rxe->attr;
528700e3e7SMoni Shoua 	return 0;
538700e3e7SMoni Shoua }
548700e3e7SMoni Shoua 
558700e3e7SMoni Shoua static int rxe_query_port(struct ib_device *dev,
568700e3e7SMoni Shoua 			  u8 port_num, struct ib_port_attr *attr)
578700e3e7SMoni Shoua {
588700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
598700e3e7SMoni Shoua 	struct rxe_port *port;
6059590b8aSYuval Shaia 	int rc;
618700e3e7SMoni Shoua 
628700e3e7SMoni Shoua 	port = &rxe->port;
638700e3e7SMoni Shoua 
64c4550c63SOr Gerlitz 	/* *attr being zeroed by the caller, avoid zeroing it here */
658700e3e7SMoni Shoua 	*attr = port->attr;
668700e3e7SMoni Shoua 
678700e3e7SMoni Shoua 	mutex_lock(&rxe->usdev_lock);
68d4186194SYuval Shaia 	rc = ib_get_eth_speed(dev, port_num, &attr->active_speed,
69e404f945SParav Pandit 			      &attr->active_width);
705736c7c4SAndrew Boyer 
715736c7c4SAndrew Boyer 	if (attr->state == IB_PORT_ACTIVE)
7272a7720fSKamal Heib 		attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
735736c7c4SAndrew Boyer 	else if (dev_get_flags(rxe->ndev) & IFF_UP)
7472a7720fSKamal Heib 		attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
755736c7c4SAndrew Boyer 	else
7672a7720fSKamal Heib 		attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
775736c7c4SAndrew Boyer 
788700e3e7SMoni Shoua 	mutex_unlock(&rxe->usdev_lock);
798700e3e7SMoni Shoua 
80d4186194SYuval Shaia 	return rc;
818700e3e7SMoni Shoua }
828700e3e7SMoni Shoua 
838700e3e7SMoni Shoua static int rxe_query_pkey(struct ib_device *device,
848700e3e7SMoni Shoua 			  u8 port_num, u16 index, u16 *pkey)
858700e3e7SMoni Shoua {
868700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(device);
878700e3e7SMoni Shoua 	struct rxe_port *port;
888700e3e7SMoni Shoua 
898700e3e7SMoni Shoua 	port = &rxe->port;
908700e3e7SMoni Shoua 
918700e3e7SMoni Shoua 	if (unlikely(index >= port->attr.pkey_tbl_len)) {
9285e9f1dbSBart Van Assche 		dev_warn(device->dev.parent, "invalid index = %d\n",
938700e3e7SMoni Shoua 			 index);
948700e3e7SMoni Shoua 		goto err1;
958700e3e7SMoni Shoua 	}
968700e3e7SMoni Shoua 
978700e3e7SMoni Shoua 	*pkey = port->pkey_tbl[index];
988700e3e7SMoni Shoua 	return 0;
998700e3e7SMoni Shoua 
1008700e3e7SMoni Shoua err1:
1018700e3e7SMoni Shoua 	return -EINVAL;
1028700e3e7SMoni Shoua }
1038700e3e7SMoni Shoua 
1048700e3e7SMoni Shoua static int rxe_modify_device(struct ib_device *dev,
1058700e3e7SMoni Shoua 			     int mask, struct ib_device_modify *attr)
1068700e3e7SMoni Shoua {
1078700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
1088700e3e7SMoni Shoua 
109f3fceba5SKamal Heib 	if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
110f3fceba5SKamal Heib 		     IB_DEVICE_MODIFY_NODE_DESC))
111f3fceba5SKamal Heib 		return -EOPNOTSUPP;
112f3fceba5SKamal Heib 
1138700e3e7SMoni Shoua 	if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1148700e3e7SMoni Shoua 		rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
1158700e3e7SMoni Shoua 
1168700e3e7SMoni Shoua 	if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
1178700e3e7SMoni Shoua 		memcpy(rxe->ib_dev.node_desc,
1188700e3e7SMoni Shoua 		       attr->node_desc, sizeof(rxe->ib_dev.node_desc));
1198700e3e7SMoni Shoua 	}
1208700e3e7SMoni Shoua 
1218700e3e7SMoni Shoua 	return 0;
1228700e3e7SMoni Shoua }
1238700e3e7SMoni Shoua 
1248700e3e7SMoni Shoua static int rxe_modify_port(struct ib_device *dev,
1258700e3e7SMoni Shoua 			   u8 port_num, int mask, struct ib_port_modify *attr)
1268700e3e7SMoni Shoua {
1278700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
1288700e3e7SMoni Shoua 	struct rxe_port *port;
1298700e3e7SMoni Shoua 
1308700e3e7SMoni Shoua 	port = &rxe->port;
1318700e3e7SMoni Shoua 
1328700e3e7SMoni Shoua 	port->attr.port_cap_flags |= attr->set_port_cap_mask;
1338700e3e7SMoni Shoua 	port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
1348700e3e7SMoni Shoua 
1358700e3e7SMoni Shoua 	if (mask & IB_PORT_RESET_QKEY_CNTR)
1368700e3e7SMoni Shoua 		port->attr.qkey_viol_cntr = 0;
1378700e3e7SMoni Shoua 
1388700e3e7SMoni Shoua 	return 0;
1398700e3e7SMoni Shoua }
1408700e3e7SMoni Shoua 
1418700e3e7SMoni Shoua static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
1428700e3e7SMoni Shoua 					       u8 port_num)
1438700e3e7SMoni Shoua {
144420bd9e2SKamal Heib 	return IB_LINK_LAYER_ETHERNET;
1458700e3e7SMoni Shoua }
1468700e3e7SMoni Shoua 
147a2a074efSLeon Romanovsky static int rxe_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
1488700e3e7SMoni Shoua {
149a2a074efSLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(uctx->device);
150a2a074efSLeon Romanovsky 	struct rxe_ucontext *uc = to_ruc(uctx);
1518700e3e7SMoni Shoua 
152a2a074efSLeon Romanovsky 	return rxe_add_to_pool(&rxe->uc_pool, &uc->pelem);
1538700e3e7SMoni Shoua }
1548700e3e7SMoni Shoua 
155a2a074efSLeon Romanovsky static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
1568700e3e7SMoni Shoua {
1578700e3e7SMoni Shoua 	struct rxe_ucontext *uc = to_ruc(ibuc);
1588700e3e7SMoni Shoua 
1598700e3e7SMoni Shoua 	rxe_drop_ref(uc);
1608700e3e7SMoni Shoua }
1618700e3e7SMoni Shoua 
1628700e3e7SMoni Shoua static int rxe_port_immutable(struct ib_device *dev, u8 port_num,
1638700e3e7SMoni Shoua 			      struct ib_port_immutable *immutable)
1648700e3e7SMoni Shoua {
1658700e3e7SMoni Shoua 	int err;
1668700e3e7SMoni Shoua 	struct ib_port_attr attr;
1678700e3e7SMoni Shoua 
168c4550c63SOr Gerlitz 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
169c4550c63SOr Gerlitz 
170c4550c63SOr Gerlitz 	err = ib_query_port(dev, port_num, &attr);
1718700e3e7SMoni Shoua 	if (err)
1728700e3e7SMoni Shoua 		return err;
1738700e3e7SMoni Shoua 
1748700e3e7SMoni Shoua 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
1758700e3e7SMoni Shoua 	immutable->gid_tbl_len = attr.gid_tbl_len;
1768700e3e7SMoni Shoua 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
1778700e3e7SMoni Shoua 
1788700e3e7SMoni Shoua 	return 0;
1798700e3e7SMoni Shoua }
1808700e3e7SMoni Shoua 
181ff23dfa1SShamir Rabinovitch static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
1828700e3e7SMoni Shoua {
18321a428a0SLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibpd->device);
18421a428a0SLeon Romanovsky 	struct rxe_pd *pd = to_rpd(ibpd);
1858700e3e7SMoni Shoua 
18621a428a0SLeon Romanovsky 	return rxe_add_to_pool(&rxe->pd_pool, &pd->pelem);
1878700e3e7SMoni Shoua }
1888700e3e7SMoni Shoua 
189c4367a26SShamir Rabinovitch static void rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
1908700e3e7SMoni Shoua {
1918700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
1928700e3e7SMoni Shoua 
1938700e3e7SMoni Shoua 	rxe_drop_ref(pd);
1948700e3e7SMoni Shoua }
1958700e3e7SMoni Shoua 
196fa5d010cSMaor Gottlieb static int rxe_create_ah(struct ib_ah *ibah,
197fa5d010cSMaor Gottlieb 			 struct rdma_ah_init_attr *init_attr,
198fa5d010cSMaor Gottlieb 			 struct ib_udata *udata)
199477864c8SMoni Shoua 
2008700e3e7SMoni Shoua {
2018700e3e7SMoni Shoua 	int err;
202d3456914SLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibah->device);
203d3456914SLeon Romanovsky 	struct rxe_ah *ah = to_rah(ibah);
2048700e3e7SMoni Shoua 
205fa5d010cSMaor Gottlieb 	err = rxe_av_chk_attr(rxe, init_attr->ah_attr);
2068700e3e7SMoni Shoua 	if (err)
207d3456914SLeon Romanovsky 		return err;
2088700e3e7SMoni Shoua 
209d3456914SLeon Romanovsky 	err = rxe_add_to_pool(&rxe->ah_pool, &ah->pelem);
210d3456914SLeon Romanovsky 	if (err)
211d3456914SLeon Romanovsky 		return err;
2128700e3e7SMoni Shoua 
213fa5d010cSMaor Gottlieb 	rxe_init_av(init_attr->ah_attr, &ah->av);
214d3456914SLeon Romanovsky 	return 0;
2158700e3e7SMoni Shoua }
2168700e3e7SMoni Shoua 
21790898850SDasaratharaman Chandramouli static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
2188700e3e7SMoni Shoua {
2198700e3e7SMoni Shoua 	int err;
2208700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibah->device);
2218700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2228700e3e7SMoni Shoua 
2238700e3e7SMoni Shoua 	err = rxe_av_chk_attr(rxe, attr);
2248700e3e7SMoni Shoua 	if (err)
2258700e3e7SMoni Shoua 		return err;
2268700e3e7SMoni Shoua 
227fa407188SKamal Heib 	rxe_init_av(attr, &ah->av);
2288700e3e7SMoni Shoua 	return 0;
2298700e3e7SMoni Shoua }
2308700e3e7SMoni Shoua 
23190898850SDasaratharaman Chandramouli static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
2328700e3e7SMoni Shoua {
2338700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2348700e3e7SMoni Shoua 
235eca7ddf9SDasaratharaman Chandramouli 	memset(attr, 0, sizeof(*attr));
23644c58487SDasaratharaman Chandramouli 	attr->type = ibah->type;
2379c96f3d4SZhu Yanjun 	rxe_av_to_attr(&ah->av, attr);
2388700e3e7SMoni Shoua 	return 0;
2398700e3e7SMoni Shoua }
2408700e3e7SMoni Shoua 
241d3456914SLeon Romanovsky static void rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
2428700e3e7SMoni Shoua {
2438700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2448700e3e7SMoni Shoua 
2458700e3e7SMoni Shoua 	rxe_drop_ref(ah);
2468700e3e7SMoni Shoua }
2478700e3e7SMoni Shoua 
248d34ac5cdSBart Van Assche static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
2498700e3e7SMoni Shoua {
2508700e3e7SMoni Shoua 	int err;
2518700e3e7SMoni Shoua 	int i;
2528700e3e7SMoni Shoua 	u32 length;
2538700e3e7SMoni Shoua 	struct rxe_recv_wqe *recv_wqe;
2548700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
2558700e3e7SMoni Shoua 
2568700e3e7SMoni Shoua 	if (unlikely(queue_full(rq->queue))) {
2578700e3e7SMoni Shoua 		err = -ENOMEM;
2588700e3e7SMoni Shoua 		goto err1;
2598700e3e7SMoni Shoua 	}
2608700e3e7SMoni Shoua 
2618700e3e7SMoni Shoua 	if (unlikely(num_sge > rq->max_sge)) {
2628700e3e7SMoni Shoua 		err = -EINVAL;
2638700e3e7SMoni Shoua 		goto err1;
2648700e3e7SMoni Shoua 	}
2658700e3e7SMoni Shoua 
2668700e3e7SMoni Shoua 	length = 0;
2678700e3e7SMoni Shoua 	for (i = 0; i < num_sge; i++)
2688700e3e7SMoni Shoua 		length += ibwr->sg_list[i].length;
2698700e3e7SMoni Shoua 
2708700e3e7SMoni Shoua 	recv_wqe = producer_addr(rq->queue);
2718700e3e7SMoni Shoua 	recv_wqe->wr_id = ibwr->wr_id;
2728700e3e7SMoni Shoua 	recv_wqe->num_sge = num_sge;
2738700e3e7SMoni Shoua 
2748700e3e7SMoni Shoua 	memcpy(recv_wqe->dma.sge, ibwr->sg_list,
2758700e3e7SMoni Shoua 	       num_sge * sizeof(struct ib_sge));
2768700e3e7SMoni Shoua 
2778700e3e7SMoni Shoua 	recv_wqe->dma.length		= length;
2788700e3e7SMoni Shoua 	recv_wqe->dma.resid		= length;
2798700e3e7SMoni Shoua 	recv_wqe->dma.num_sge		= num_sge;
2808700e3e7SMoni Shoua 	recv_wqe->dma.cur_sge		= 0;
2818700e3e7SMoni Shoua 	recv_wqe->dma.sge_offset	= 0;
2828700e3e7SMoni Shoua 
2838700e3e7SMoni Shoua 	/* make sure all changes to the work queue are written before we
2848700e3e7SMoni Shoua 	 * update the producer pointer
2858700e3e7SMoni Shoua 	 */
2868700e3e7SMoni Shoua 	smp_wmb();
2878700e3e7SMoni Shoua 
2888700e3e7SMoni Shoua 	advance_producer(rq->queue);
2898700e3e7SMoni Shoua 	return 0;
2908700e3e7SMoni Shoua 
2918700e3e7SMoni Shoua err1:
2928700e3e7SMoni Shoua 	return err;
2938700e3e7SMoni Shoua }
2948700e3e7SMoni Shoua 
29568e326deSLeon Romanovsky static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
2968700e3e7SMoni Shoua 			  struct ib_udata *udata)
2978700e3e7SMoni Shoua {
2988700e3e7SMoni Shoua 	int err;
29968e326deSLeon Romanovsky 	struct rxe_dev *rxe = to_rdev(ibsrq->device);
30068e326deSLeon Romanovsky 	struct rxe_pd *pd = to_rpd(ibsrq->pd);
30168e326deSLeon Romanovsky 	struct rxe_srq *srq = to_rsrq(ibsrq);
3020c43ab37SJason Gunthorpe 	struct rxe_create_srq_resp __user *uresp = NULL;
3030c43ab37SJason Gunthorpe 
3040c43ab37SJason Gunthorpe 	if (udata) {
3050c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
30668e326deSLeon Romanovsky 			return -EINVAL;
3070c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
3080c43ab37SJason Gunthorpe 	}
3098700e3e7SMoni Shoua 
3108700e3e7SMoni Shoua 	err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
3118700e3e7SMoni Shoua 	if (err)
3128700e3e7SMoni Shoua 		goto err1;
3138700e3e7SMoni Shoua 
31468e326deSLeon Romanovsky 	err = rxe_add_to_pool(&rxe->srq_pool, &srq->pelem);
31568e326deSLeon Romanovsky 	if (err)
3168700e3e7SMoni Shoua 		goto err1;
3178700e3e7SMoni Shoua 
3188700e3e7SMoni Shoua 	rxe_add_ref(pd);
3198700e3e7SMoni Shoua 	srq->pd = pd;
3208700e3e7SMoni Shoua 
321ff23dfa1SShamir Rabinovitch 	err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
3228700e3e7SMoni Shoua 	if (err)
3238700e3e7SMoni Shoua 		goto err2;
3248700e3e7SMoni Shoua 
32568e326deSLeon Romanovsky 	return 0;
3268700e3e7SMoni Shoua 
3278700e3e7SMoni Shoua err2:
3288700e3e7SMoni Shoua 	rxe_drop_ref(pd);
3298700e3e7SMoni Shoua 	rxe_drop_ref(srq);
3308700e3e7SMoni Shoua err1:
33168e326deSLeon Romanovsky 	return err;
3328700e3e7SMoni Shoua }
3338700e3e7SMoni Shoua 
3348700e3e7SMoni Shoua static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
3358700e3e7SMoni Shoua 			  enum ib_srq_attr_mask mask,
3368700e3e7SMoni Shoua 			  struct ib_udata *udata)
3378700e3e7SMoni Shoua {
3388700e3e7SMoni Shoua 	int err;
3398700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3408700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibsrq->device);
3410c43ab37SJason Gunthorpe 	struct rxe_modify_srq_cmd ucmd = {};
3420c43ab37SJason Gunthorpe 
3430c43ab37SJason Gunthorpe 	if (udata) {
3440c43ab37SJason Gunthorpe 		if (udata->inlen < sizeof(ucmd))
3450c43ab37SJason Gunthorpe 			return -EINVAL;
3460c43ab37SJason Gunthorpe 
3470c43ab37SJason Gunthorpe 		err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
3480c43ab37SJason Gunthorpe 		if (err)
3490c43ab37SJason Gunthorpe 			return err;
3500c43ab37SJason Gunthorpe 	}
3518700e3e7SMoni Shoua 
3528700e3e7SMoni Shoua 	err = rxe_srq_chk_attr(rxe, srq, attr, mask);
3538700e3e7SMoni Shoua 	if (err)
3548700e3e7SMoni Shoua 		goto err1;
3558700e3e7SMoni Shoua 
356ff23dfa1SShamir Rabinovitch 	err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata);
3578700e3e7SMoni Shoua 	if (err)
3588700e3e7SMoni Shoua 		goto err1;
3598700e3e7SMoni Shoua 
3608700e3e7SMoni Shoua 	return 0;
3618700e3e7SMoni Shoua 
3628700e3e7SMoni Shoua err1:
3638700e3e7SMoni Shoua 	return err;
3648700e3e7SMoni Shoua }
3658700e3e7SMoni Shoua 
3668700e3e7SMoni Shoua static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
3678700e3e7SMoni Shoua {
3688700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3698700e3e7SMoni Shoua 
3708700e3e7SMoni Shoua 	if (srq->error)
3718700e3e7SMoni Shoua 		return -EINVAL;
3728700e3e7SMoni Shoua 
3738700e3e7SMoni Shoua 	attr->max_wr = srq->rq.queue->buf->index_mask;
3748700e3e7SMoni Shoua 	attr->max_sge = srq->rq.max_sge;
3758700e3e7SMoni Shoua 	attr->srq_limit = srq->limit;
3768700e3e7SMoni Shoua 	return 0;
3778700e3e7SMoni Shoua }
3788700e3e7SMoni Shoua 
37968e326deSLeon Romanovsky static void rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
3808700e3e7SMoni Shoua {
3818700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3828700e3e7SMoni Shoua 
3838700e3e7SMoni Shoua 	if (srq->rq.queue)
3848700e3e7SMoni Shoua 		rxe_queue_cleanup(srq->rq.queue);
3858700e3e7SMoni Shoua 
3868700e3e7SMoni Shoua 	rxe_drop_ref(srq->pd);
3878700e3e7SMoni Shoua 	rxe_drop_ref(srq);
3888700e3e7SMoni Shoua }
3898700e3e7SMoni Shoua 
390d34ac5cdSBart Van Assche static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
391d34ac5cdSBart Van Assche 			     const struct ib_recv_wr **bad_wr)
3928700e3e7SMoni Shoua {
3938700e3e7SMoni Shoua 	int err = 0;
3948700e3e7SMoni Shoua 	unsigned long flags;
3958700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3968700e3e7SMoni Shoua 
3978700e3e7SMoni Shoua 	spin_lock_irqsave(&srq->rq.producer_lock, flags);
3988700e3e7SMoni Shoua 
3998700e3e7SMoni Shoua 	while (wr) {
4008700e3e7SMoni Shoua 		err = post_one_recv(&srq->rq, wr);
4018700e3e7SMoni Shoua 		if (unlikely(err))
4028700e3e7SMoni Shoua 			break;
4038700e3e7SMoni Shoua 		wr = wr->next;
4048700e3e7SMoni Shoua 	}
4058700e3e7SMoni Shoua 
4068700e3e7SMoni Shoua 	spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
4078700e3e7SMoni Shoua 
4088700e3e7SMoni Shoua 	if (err)
4098700e3e7SMoni Shoua 		*bad_wr = wr;
4108700e3e7SMoni Shoua 
4118700e3e7SMoni Shoua 	return err;
4128700e3e7SMoni Shoua }
4138700e3e7SMoni Shoua 
4148700e3e7SMoni Shoua static struct ib_qp *rxe_create_qp(struct ib_pd *ibpd,
4158700e3e7SMoni Shoua 				   struct ib_qp_init_attr *init,
4168700e3e7SMoni Shoua 				   struct ib_udata *udata)
4178700e3e7SMoni Shoua {
4188700e3e7SMoni Shoua 	int err;
4198700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
4208700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
4218700e3e7SMoni Shoua 	struct rxe_qp *qp;
4220c43ab37SJason Gunthorpe 	struct rxe_create_qp_resp __user *uresp = NULL;
4230c43ab37SJason Gunthorpe 
4240c43ab37SJason Gunthorpe 	if (udata) {
4250c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
4260c43ab37SJason Gunthorpe 			return ERR_PTR(-EINVAL);
4270c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
4280c43ab37SJason Gunthorpe 	}
4298700e3e7SMoni Shoua 
4308700e3e7SMoni Shoua 	err = rxe_qp_chk_init(rxe, init);
4318700e3e7SMoni Shoua 	if (err)
4328700e3e7SMoni Shoua 		goto err1;
4338700e3e7SMoni Shoua 
4348700e3e7SMoni Shoua 	qp = rxe_alloc(&rxe->qp_pool);
4358700e3e7SMoni Shoua 	if (!qp) {
4368700e3e7SMoni Shoua 		err = -ENOMEM;
4378700e3e7SMoni Shoua 		goto err1;
4388700e3e7SMoni Shoua 	}
4398700e3e7SMoni Shoua 
4408700e3e7SMoni Shoua 	if (udata) {
4418700e3e7SMoni Shoua 		if (udata->inlen) {
4428700e3e7SMoni Shoua 			err = -EINVAL;
4435b9ea16cSAndrew Boyer 			goto err2;
4448700e3e7SMoni Shoua 		}
4458700e3e7SMoni Shoua 		qp->is_user = 1;
4468700e3e7SMoni Shoua 	}
4478700e3e7SMoni Shoua 
4488700e3e7SMoni Shoua 	rxe_add_index(qp);
4498700e3e7SMoni Shoua 
450e00b64f7SShamir Rabinovitch 	err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibpd, udata);
4518700e3e7SMoni Shoua 	if (err)
4525b9ea16cSAndrew Boyer 		goto err3;
4538700e3e7SMoni Shoua 
4548700e3e7SMoni Shoua 	return &qp->ibqp;
4558700e3e7SMoni Shoua 
4565b9ea16cSAndrew Boyer err3:
4578700e3e7SMoni Shoua 	rxe_drop_index(qp);
4585b9ea16cSAndrew Boyer err2:
4598700e3e7SMoni Shoua 	rxe_drop_ref(qp);
4608700e3e7SMoni Shoua err1:
4618700e3e7SMoni Shoua 	return ERR_PTR(err);
4628700e3e7SMoni Shoua }
4638700e3e7SMoni Shoua 
4648700e3e7SMoni Shoua static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4658700e3e7SMoni Shoua 			 int mask, struct ib_udata *udata)
4668700e3e7SMoni Shoua {
4678700e3e7SMoni Shoua 	int err;
4688700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
4698700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
4708700e3e7SMoni Shoua 
4718700e3e7SMoni Shoua 	err = rxe_qp_chk_attr(rxe, qp, attr, mask);
4728700e3e7SMoni Shoua 	if (err)
4738700e3e7SMoni Shoua 		goto err1;
4748700e3e7SMoni Shoua 
4758700e3e7SMoni Shoua 	err = rxe_qp_from_attr(qp, attr, mask, udata);
4768700e3e7SMoni Shoua 	if (err)
4778700e3e7SMoni Shoua 		goto err1;
4788700e3e7SMoni Shoua 
4798700e3e7SMoni Shoua 	return 0;
4808700e3e7SMoni Shoua 
4818700e3e7SMoni Shoua err1:
4828700e3e7SMoni Shoua 	return err;
4838700e3e7SMoni Shoua }
4848700e3e7SMoni Shoua 
4858700e3e7SMoni Shoua static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4868700e3e7SMoni Shoua 			int mask, struct ib_qp_init_attr *init)
4878700e3e7SMoni Shoua {
4888700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
4898700e3e7SMoni Shoua 
4908700e3e7SMoni Shoua 	rxe_qp_to_init(qp, init);
4918700e3e7SMoni Shoua 	rxe_qp_to_attr(qp, attr, mask);
4928700e3e7SMoni Shoua 
4938700e3e7SMoni Shoua 	return 0;
4948700e3e7SMoni Shoua }
4958700e3e7SMoni Shoua 
496c4367a26SShamir Rabinovitch static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
4978700e3e7SMoni Shoua {
4988700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
4998700e3e7SMoni Shoua 
5008700e3e7SMoni Shoua 	rxe_qp_destroy(qp);
5018700e3e7SMoni Shoua 	rxe_drop_index(qp);
5028700e3e7SMoni Shoua 	rxe_drop_ref(qp);
5038700e3e7SMoni Shoua 	return 0;
5048700e3e7SMoni Shoua }
5058700e3e7SMoni Shoua 
506f696bf6dSBart Van Assche static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
5078700e3e7SMoni Shoua 			    unsigned int mask, unsigned int length)
5088700e3e7SMoni Shoua {
5098700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
5108700e3e7SMoni Shoua 	struct rxe_sq *sq = &qp->sq;
5118700e3e7SMoni Shoua 
5128700e3e7SMoni Shoua 	if (unlikely(num_sge > sq->max_sge))
5138700e3e7SMoni Shoua 		goto err1;
5148700e3e7SMoni Shoua 
5158700e3e7SMoni Shoua 	if (unlikely(mask & WR_ATOMIC_MASK)) {
5168700e3e7SMoni Shoua 		if (length < 8)
5178700e3e7SMoni Shoua 			goto err1;
5188700e3e7SMoni Shoua 
5198700e3e7SMoni Shoua 		if (atomic_wr(ibwr)->remote_addr & 0x7)
5208700e3e7SMoni Shoua 			goto err1;
5218700e3e7SMoni Shoua 	}
5228700e3e7SMoni Shoua 
5238700e3e7SMoni Shoua 	if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
5248700e3e7SMoni Shoua 		     (length > sq->max_inline)))
5258700e3e7SMoni Shoua 		goto err1;
5268700e3e7SMoni Shoua 
5278700e3e7SMoni Shoua 	return 0;
5288700e3e7SMoni Shoua 
5298700e3e7SMoni Shoua err1:
5308700e3e7SMoni Shoua 	return -EINVAL;
5318700e3e7SMoni Shoua }
5328700e3e7SMoni Shoua 
5338700e3e7SMoni Shoua static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
534f696bf6dSBart Van Assche 			 const struct ib_send_wr *ibwr)
5358700e3e7SMoni Shoua {
5368700e3e7SMoni Shoua 	wr->wr_id = ibwr->wr_id;
5378700e3e7SMoni Shoua 	wr->num_sge = ibwr->num_sge;
5388700e3e7SMoni Shoua 	wr->opcode = ibwr->opcode;
5398700e3e7SMoni Shoua 	wr->send_flags = ibwr->send_flags;
5408700e3e7SMoni Shoua 
5418700e3e7SMoni Shoua 	if (qp_type(qp) == IB_QPT_UD ||
5428700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_SMI ||
5438700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_GSI) {
5448700e3e7SMoni Shoua 		wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
5458700e3e7SMoni Shoua 		wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
5468700e3e7SMoni Shoua 		if (qp_type(qp) == IB_QPT_GSI)
5478700e3e7SMoni Shoua 			wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
5488700e3e7SMoni Shoua 		if (wr->opcode == IB_WR_SEND_WITH_IMM)
5498700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
5508700e3e7SMoni Shoua 	} else {
5518700e3e7SMoni Shoua 		switch (wr->opcode) {
5528700e3e7SMoni Shoua 		case IB_WR_RDMA_WRITE_WITH_IMM:
5538700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
554ea6ee93bSBart Van Assche 			/* fall through */
5558700e3e7SMoni Shoua 		case IB_WR_RDMA_READ:
5568700e3e7SMoni Shoua 		case IB_WR_RDMA_WRITE:
5578700e3e7SMoni Shoua 			wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
5588700e3e7SMoni Shoua 			wr->wr.rdma.rkey	= rdma_wr(ibwr)->rkey;
5598700e3e7SMoni Shoua 			break;
5608700e3e7SMoni Shoua 		case IB_WR_SEND_WITH_IMM:
5618700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
5628700e3e7SMoni Shoua 			break;
5638700e3e7SMoni Shoua 		case IB_WR_SEND_WITH_INV:
5648700e3e7SMoni Shoua 			wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
5658700e3e7SMoni Shoua 			break;
5668700e3e7SMoni Shoua 		case IB_WR_ATOMIC_CMP_AND_SWP:
5678700e3e7SMoni Shoua 		case IB_WR_ATOMIC_FETCH_AND_ADD:
5688700e3e7SMoni Shoua 			wr->wr.atomic.remote_addr =
5698700e3e7SMoni Shoua 				atomic_wr(ibwr)->remote_addr;
5708700e3e7SMoni Shoua 			wr->wr.atomic.compare_add =
5718700e3e7SMoni Shoua 				atomic_wr(ibwr)->compare_add;
5728700e3e7SMoni Shoua 			wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
5738700e3e7SMoni Shoua 			wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
5748700e3e7SMoni Shoua 			break;
5758700e3e7SMoni Shoua 		case IB_WR_LOCAL_INV:
5768700e3e7SMoni Shoua 			wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
5778700e3e7SMoni Shoua 		break;
5788700e3e7SMoni Shoua 		case IB_WR_REG_MR:
5798700e3e7SMoni Shoua 			wr->wr.reg.mr = reg_wr(ibwr)->mr;
5808700e3e7SMoni Shoua 			wr->wr.reg.key = reg_wr(ibwr)->key;
5818700e3e7SMoni Shoua 			wr->wr.reg.access = reg_wr(ibwr)->access;
5828700e3e7SMoni Shoua 		break;
5838700e3e7SMoni Shoua 		default:
5848700e3e7SMoni Shoua 			break;
5858700e3e7SMoni Shoua 		}
5868700e3e7SMoni Shoua 	}
5878700e3e7SMoni Shoua }
5888700e3e7SMoni Shoua 
589f696bf6dSBart Van Assche static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
5908700e3e7SMoni Shoua 			 unsigned int mask, unsigned int length,
5918700e3e7SMoni Shoua 			 struct rxe_send_wqe *wqe)
5928700e3e7SMoni Shoua {
5938700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
5948700e3e7SMoni Shoua 	struct ib_sge *sge;
5958700e3e7SMoni Shoua 	int i;
5968700e3e7SMoni Shoua 	u8 *p;
5978700e3e7SMoni Shoua 
5988700e3e7SMoni Shoua 	init_send_wr(qp, &wqe->wr, ibwr);
5998700e3e7SMoni Shoua 
6008700e3e7SMoni Shoua 	if (qp_type(qp) == IB_QPT_UD ||
6018700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_SMI ||
6028700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_GSI)
6038700e3e7SMoni Shoua 		memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
6048700e3e7SMoni Shoua 
6058700e3e7SMoni Shoua 	if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) {
6068700e3e7SMoni Shoua 		p = wqe->dma.inline_data;
6078700e3e7SMoni Shoua 
6088700e3e7SMoni Shoua 		sge = ibwr->sg_list;
6098700e3e7SMoni Shoua 		for (i = 0; i < num_sge; i++, sge++) {
6108700e3e7SMoni Shoua 			memcpy(p, (void *)(uintptr_t)sge->addr,
6118700e3e7SMoni Shoua 					sge->length);
6128700e3e7SMoni Shoua 
6138700e3e7SMoni Shoua 			p += sge->length;
6148700e3e7SMoni Shoua 		}
6158700e3e7SMoni Shoua 	} else if (mask & WR_REG_MASK) {
6168700e3e7SMoni Shoua 		wqe->mask = mask;
6178700e3e7SMoni Shoua 		wqe->state = wqe_state_posted;
6188700e3e7SMoni Shoua 		return 0;
6198700e3e7SMoni Shoua 	} else
6208700e3e7SMoni Shoua 		memcpy(wqe->dma.sge, ibwr->sg_list,
6218700e3e7SMoni Shoua 		       num_sge * sizeof(struct ib_sge));
6228700e3e7SMoni Shoua 
623a6544a62SBart Van Assche 	wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
624a6544a62SBart Van Assche 		mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
6258700e3e7SMoni Shoua 	wqe->mask		= mask;
6268700e3e7SMoni Shoua 	wqe->dma.length		= length;
6278700e3e7SMoni Shoua 	wqe->dma.resid		= length;
6288700e3e7SMoni Shoua 	wqe->dma.num_sge	= num_sge;
6298700e3e7SMoni Shoua 	wqe->dma.cur_sge	= 0;
6308700e3e7SMoni Shoua 	wqe->dma.sge_offset	= 0;
6318700e3e7SMoni Shoua 	wqe->state		= wqe_state_posted;
6328700e3e7SMoni Shoua 	wqe->ssn		= atomic_add_return(1, &qp->ssn);
6338700e3e7SMoni Shoua 
6348700e3e7SMoni Shoua 	return 0;
6358700e3e7SMoni Shoua }
6368700e3e7SMoni Shoua 
637f696bf6dSBart Van Assche static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
638e404f945SParav Pandit 			 unsigned int mask, u32 length)
6398700e3e7SMoni Shoua {
6408700e3e7SMoni Shoua 	int err;
6418700e3e7SMoni Shoua 	struct rxe_sq *sq = &qp->sq;
6428700e3e7SMoni Shoua 	struct rxe_send_wqe *send_wqe;
6438700e3e7SMoni Shoua 	unsigned long flags;
6448700e3e7SMoni Shoua 
6458700e3e7SMoni Shoua 	err = validate_send_wr(qp, ibwr, mask, length);
6468700e3e7SMoni Shoua 	if (err)
6478700e3e7SMoni Shoua 		return err;
6488700e3e7SMoni Shoua 
6498700e3e7SMoni Shoua 	spin_lock_irqsave(&qp->sq.sq_lock, flags);
6508700e3e7SMoni Shoua 
6518700e3e7SMoni Shoua 	if (unlikely(queue_full(sq->queue))) {
6528700e3e7SMoni Shoua 		err = -ENOMEM;
6538700e3e7SMoni Shoua 		goto err1;
6548700e3e7SMoni Shoua 	}
6558700e3e7SMoni Shoua 
6568700e3e7SMoni Shoua 	send_wqe = producer_addr(sq->queue);
6578700e3e7SMoni Shoua 
6588700e3e7SMoni Shoua 	err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
6598700e3e7SMoni Shoua 	if (unlikely(err))
6608700e3e7SMoni Shoua 		goto err1;
6618700e3e7SMoni Shoua 
6628700e3e7SMoni Shoua 	/*
6638700e3e7SMoni Shoua 	 * make sure all changes to the work queue are
6648700e3e7SMoni Shoua 	 * written before we update the producer pointer
6658700e3e7SMoni Shoua 	 */
6668700e3e7SMoni Shoua 	smp_wmb();
6678700e3e7SMoni Shoua 
6688700e3e7SMoni Shoua 	advance_producer(sq->queue);
6698700e3e7SMoni Shoua 	spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
6708700e3e7SMoni Shoua 
6718700e3e7SMoni Shoua 	return 0;
6728700e3e7SMoni Shoua 
6738700e3e7SMoni Shoua err1:
6748700e3e7SMoni Shoua 	spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
6758700e3e7SMoni Shoua 	return err;
6768700e3e7SMoni Shoua }
6778700e3e7SMoni Shoua 
678d34ac5cdSBart Van Assche static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
679d34ac5cdSBart Van Assche 				const struct ib_send_wr **bad_wr)
6808700e3e7SMoni Shoua {
6818700e3e7SMoni Shoua 	int err = 0;
6828700e3e7SMoni Shoua 	unsigned int mask;
6838700e3e7SMoni Shoua 	unsigned int length = 0;
6848700e3e7SMoni Shoua 	int i;
6858700e3e7SMoni Shoua 
6868700e3e7SMoni Shoua 	while (wr) {
6878700e3e7SMoni Shoua 		mask = wr_opcode_mask(wr->opcode, qp);
6888700e3e7SMoni Shoua 		if (unlikely(!mask)) {
6898700e3e7SMoni Shoua 			err = -EINVAL;
6908700e3e7SMoni Shoua 			*bad_wr = wr;
6918700e3e7SMoni Shoua 			break;
6928700e3e7SMoni Shoua 		}
6938700e3e7SMoni Shoua 
6948700e3e7SMoni Shoua 		if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
6958700e3e7SMoni Shoua 			     !(mask & WR_INLINE_MASK))) {
6968700e3e7SMoni Shoua 			err = -EINVAL;
6978700e3e7SMoni Shoua 			*bad_wr = wr;
6988700e3e7SMoni Shoua 			break;
6998700e3e7SMoni Shoua 		}
7008700e3e7SMoni Shoua 
7018700e3e7SMoni Shoua 		length = 0;
7028700e3e7SMoni Shoua 		for (i = 0; i < wr->num_sge; i++)
7038700e3e7SMoni Shoua 			length += wr->sg_list[i].length;
7048700e3e7SMoni Shoua 
7058700e3e7SMoni Shoua 		err = post_one_send(qp, wr, mask, length);
7068700e3e7SMoni Shoua 
7078700e3e7SMoni Shoua 		if (err) {
7088700e3e7SMoni Shoua 			*bad_wr = wr;
7098700e3e7SMoni Shoua 			break;
7108700e3e7SMoni Shoua 		}
7118700e3e7SMoni Shoua 		wr = wr->next;
7128700e3e7SMoni Shoua 	}
7138700e3e7SMoni Shoua 
7141661d3b0SAlexandru Moise 	rxe_run_task(&qp->req.task, 1);
7156f301e06SBart Van Assche 	if (unlikely(qp->req.state == QP_STATE_ERROR))
7166f301e06SBart Van Assche 		rxe_run_task(&qp->comp.task, 1);
7178700e3e7SMoni Shoua 
7188700e3e7SMoni Shoua 	return err;
7198700e3e7SMoni Shoua }
7208700e3e7SMoni Shoua 
721d34ac5cdSBart Van Assche static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
722d34ac5cdSBart Van Assche 			 const struct ib_send_wr **bad_wr)
723063af595SParav Pandit {
724063af595SParav Pandit 	struct rxe_qp *qp = to_rqp(ibqp);
725063af595SParav Pandit 
726063af595SParav Pandit 	if (unlikely(!qp->valid)) {
727063af595SParav Pandit 		*bad_wr = wr;
728063af595SParav Pandit 		return -EINVAL;
729063af595SParav Pandit 	}
730063af595SParav Pandit 
731063af595SParav Pandit 	if (unlikely(qp->req.state < QP_STATE_READY)) {
732063af595SParav Pandit 		*bad_wr = wr;
733063af595SParav Pandit 		return -EINVAL;
734063af595SParav Pandit 	}
735063af595SParav Pandit 
736063af595SParav Pandit 	if (qp->is_user) {
737063af595SParav Pandit 		/* Utilize process context to do protocol processing */
738063af595SParav Pandit 		rxe_run_task(&qp->req.task, 0);
739063af595SParav Pandit 		return 0;
740063af595SParav Pandit 	} else
741063af595SParav Pandit 		return rxe_post_send_kernel(qp, wr, bad_wr);
742063af595SParav Pandit }
743063af595SParav Pandit 
744d34ac5cdSBart Van Assche static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
745d34ac5cdSBart Van Assche 			 const struct ib_recv_wr **bad_wr)
7468700e3e7SMoni Shoua {
7478700e3e7SMoni Shoua 	int err = 0;
7488700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
7498700e3e7SMoni Shoua 	struct rxe_rq *rq = &qp->rq;
7508700e3e7SMoni Shoua 	unsigned long flags;
7518700e3e7SMoni Shoua 
7528700e3e7SMoni Shoua 	if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
7538700e3e7SMoni Shoua 		*bad_wr = wr;
7548700e3e7SMoni Shoua 		err = -EINVAL;
7558700e3e7SMoni Shoua 		goto err1;
7568700e3e7SMoni Shoua 	}
7578700e3e7SMoni Shoua 
7588700e3e7SMoni Shoua 	if (unlikely(qp->srq)) {
7598700e3e7SMoni Shoua 		*bad_wr = wr;
7608700e3e7SMoni Shoua 		err = -EINVAL;
7618700e3e7SMoni Shoua 		goto err1;
7628700e3e7SMoni Shoua 	}
7638700e3e7SMoni Shoua 
7648700e3e7SMoni Shoua 	spin_lock_irqsave(&rq->producer_lock, flags);
7658700e3e7SMoni Shoua 
7668700e3e7SMoni Shoua 	while (wr) {
7678700e3e7SMoni Shoua 		err = post_one_recv(rq, wr);
7688700e3e7SMoni Shoua 		if (unlikely(err)) {
7698700e3e7SMoni Shoua 			*bad_wr = wr;
7708700e3e7SMoni Shoua 			break;
7718700e3e7SMoni Shoua 		}
7728700e3e7SMoni Shoua 		wr = wr->next;
7738700e3e7SMoni Shoua 	}
7748700e3e7SMoni Shoua 
7758700e3e7SMoni Shoua 	spin_unlock_irqrestore(&rq->producer_lock, flags);
7768700e3e7SMoni Shoua 
77712171971SVijay Immanuel 	if (qp->resp.state == QP_STATE_ERROR)
77812171971SVijay Immanuel 		rxe_run_task(&qp->resp.task, 1);
77912171971SVijay Immanuel 
7808700e3e7SMoni Shoua err1:
7818700e3e7SMoni Shoua 	return err;
7828700e3e7SMoni Shoua }
7838700e3e7SMoni Shoua 
784e39afe3dSLeon Romanovsky static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
7858700e3e7SMoni Shoua 			 struct ib_udata *udata)
7868700e3e7SMoni Shoua {
7878700e3e7SMoni Shoua 	int err;
788e39afe3dSLeon Romanovsky 	struct ib_device *dev = ibcq->device;
7898700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
790e39afe3dSLeon Romanovsky 	struct rxe_cq *cq = to_rcq(ibcq);
7910c43ab37SJason Gunthorpe 	struct rxe_create_cq_resp __user *uresp = NULL;
7920c43ab37SJason Gunthorpe 
7930c43ab37SJason Gunthorpe 	if (udata) {
7940c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
795e39afe3dSLeon Romanovsky 			return -EINVAL;
7960c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
7970c43ab37SJason Gunthorpe 	}
7988700e3e7SMoni Shoua 
7998700e3e7SMoni Shoua 	if (attr->flags)
800e39afe3dSLeon Romanovsky 		return -EINVAL;
8018700e3e7SMoni Shoua 
802b92ec0feSJason Gunthorpe 	err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
8038700e3e7SMoni Shoua 	if (err)
804e39afe3dSLeon Romanovsky 		return err;
8058700e3e7SMoni Shoua 
806ff23dfa1SShamir Rabinovitch 	err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
807ff23dfa1SShamir Rabinovitch 			       uresp);
8088700e3e7SMoni Shoua 	if (err)
809e39afe3dSLeon Romanovsky 		return err;
8108700e3e7SMoni Shoua 
811e39afe3dSLeon Romanovsky 	return rxe_add_to_pool(&rxe->cq_pool, &cq->pelem);
8128700e3e7SMoni Shoua }
8138700e3e7SMoni Shoua 
814a52c8e24SLeon Romanovsky static void rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
8158700e3e7SMoni Shoua {
8168700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8178700e3e7SMoni Shoua 
818bfc3ae05SAndrew Boyer 	rxe_cq_disable(cq);
819bfc3ae05SAndrew Boyer 
8208700e3e7SMoni Shoua 	rxe_drop_ref(cq);
8218700e3e7SMoni Shoua }
8228700e3e7SMoni Shoua 
8238700e3e7SMoni Shoua static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
8248700e3e7SMoni Shoua {
8258700e3e7SMoni Shoua 	int err;
8268700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8278700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibcq->device);
8280c43ab37SJason Gunthorpe 	struct rxe_resize_cq_resp __user *uresp = NULL;
8290c43ab37SJason Gunthorpe 
8300c43ab37SJason Gunthorpe 	if (udata) {
8310c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
8320c43ab37SJason Gunthorpe 			return -EINVAL;
8330c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
8340c43ab37SJason Gunthorpe 	}
8358700e3e7SMoni Shoua 
836b92ec0feSJason Gunthorpe 	err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
8378700e3e7SMoni Shoua 	if (err)
8388700e3e7SMoni Shoua 		goto err1;
8398700e3e7SMoni Shoua 
840ff23dfa1SShamir Rabinovitch 	err = rxe_cq_resize_queue(cq, cqe, uresp, udata);
8418700e3e7SMoni Shoua 	if (err)
8428700e3e7SMoni Shoua 		goto err1;
8438700e3e7SMoni Shoua 
8448700e3e7SMoni Shoua 	return 0;
8458700e3e7SMoni Shoua 
8468700e3e7SMoni Shoua err1:
8478700e3e7SMoni Shoua 	return err;
8488700e3e7SMoni Shoua }
8498700e3e7SMoni Shoua 
8508700e3e7SMoni Shoua static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
8518700e3e7SMoni Shoua {
8528700e3e7SMoni Shoua 	int i;
8538700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8548700e3e7SMoni Shoua 	struct rxe_cqe *cqe;
8558700e3e7SMoni Shoua 	unsigned long flags;
8568700e3e7SMoni Shoua 
8578700e3e7SMoni Shoua 	spin_lock_irqsave(&cq->cq_lock, flags);
8588700e3e7SMoni Shoua 	for (i = 0; i < num_entries; i++) {
8598700e3e7SMoni Shoua 		cqe = queue_head(cq->queue);
8608700e3e7SMoni Shoua 		if (!cqe)
8618700e3e7SMoni Shoua 			break;
8628700e3e7SMoni Shoua 
8638700e3e7SMoni Shoua 		memcpy(wc++, &cqe->ibwc, sizeof(*wc));
8648700e3e7SMoni Shoua 		advance_consumer(cq->queue);
8658700e3e7SMoni Shoua 	}
8668700e3e7SMoni Shoua 	spin_unlock_irqrestore(&cq->cq_lock, flags);
8678700e3e7SMoni Shoua 
8688700e3e7SMoni Shoua 	return i;
8698700e3e7SMoni Shoua }
8708700e3e7SMoni Shoua 
8718700e3e7SMoni Shoua static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
8728700e3e7SMoni Shoua {
8738700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8748700e3e7SMoni Shoua 	int count = queue_count(cq->queue);
8758700e3e7SMoni Shoua 
8768700e3e7SMoni Shoua 	return (count > wc_cnt) ? wc_cnt : count;
8778700e3e7SMoni Shoua }
8788700e3e7SMoni Shoua 
8798700e3e7SMoni Shoua static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
8808700e3e7SMoni Shoua {
8818700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
882accacb8fSAndrew Boyer 	unsigned long irq_flags;
883accacb8fSAndrew Boyer 	int ret = 0;
8848700e3e7SMoni Shoua 
885accacb8fSAndrew Boyer 	spin_lock_irqsave(&cq->cq_lock, irq_flags);
8868700e3e7SMoni Shoua 	if (cq->notify != IB_CQ_NEXT_COMP)
8878700e3e7SMoni Shoua 		cq->notify = flags & IB_CQ_SOLICITED_MASK;
8888700e3e7SMoni Shoua 
889accacb8fSAndrew Boyer 	if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !queue_empty(cq->queue))
890accacb8fSAndrew Boyer 		ret = 1;
891accacb8fSAndrew Boyer 
892accacb8fSAndrew Boyer 	spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
893accacb8fSAndrew Boyer 
894accacb8fSAndrew Boyer 	return ret;
8958700e3e7SMoni Shoua }
8968700e3e7SMoni Shoua 
8978700e3e7SMoni Shoua static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
8988700e3e7SMoni Shoua {
8998700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
9008700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
9018700e3e7SMoni Shoua 	struct rxe_mem *mr;
9028700e3e7SMoni Shoua 
9038700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
904293d8440SKamal Heib 	if (!mr)
905293d8440SKamal Heib 		return ERR_PTR(-ENOMEM);
9068700e3e7SMoni Shoua 
9078700e3e7SMoni Shoua 	rxe_add_index(mr);
9088700e3e7SMoni Shoua 	rxe_add_ref(pd);
909293d8440SKamal Heib 	rxe_mem_init_dma(pd, access, mr);
9108700e3e7SMoni Shoua 
9118700e3e7SMoni Shoua 	return &mr->ibmr;
9128700e3e7SMoni Shoua }
9138700e3e7SMoni Shoua 
9148700e3e7SMoni Shoua static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
9158700e3e7SMoni Shoua 				     u64 start,
9168700e3e7SMoni Shoua 				     u64 length,
9178700e3e7SMoni Shoua 				     u64 iova,
9188700e3e7SMoni Shoua 				     int access, struct ib_udata *udata)
9198700e3e7SMoni Shoua {
9208700e3e7SMoni Shoua 	int err;
9218700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
9228700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
9238700e3e7SMoni Shoua 	struct rxe_mem *mr;
9248700e3e7SMoni Shoua 
9258700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
9268700e3e7SMoni Shoua 	if (!mr) {
9278700e3e7SMoni Shoua 		err = -ENOMEM;
9288700e3e7SMoni Shoua 		goto err2;
9298700e3e7SMoni Shoua 	}
9308700e3e7SMoni Shoua 
9318700e3e7SMoni Shoua 	rxe_add_index(mr);
9328700e3e7SMoni Shoua 
9338700e3e7SMoni Shoua 	rxe_add_ref(pd);
9348700e3e7SMoni Shoua 
935e12ee8ceSZhu Yanjun 	err = rxe_mem_init_user(pd, start, length, iova,
9368700e3e7SMoni Shoua 				access, udata, mr);
9378700e3e7SMoni Shoua 	if (err)
9388700e3e7SMoni Shoua 		goto err3;
9398700e3e7SMoni Shoua 
9408700e3e7SMoni Shoua 	return &mr->ibmr;
9418700e3e7SMoni Shoua 
9428700e3e7SMoni Shoua err3:
9438700e3e7SMoni Shoua 	rxe_drop_ref(pd);
9448700e3e7SMoni Shoua 	rxe_drop_index(mr);
9458700e3e7SMoni Shoua 	rxe_drop_ref(mr);
9468700e3e7SMoni Shoua err2:
9478700e3e7SMoni Shoua 	return ERR_PTR(err);
9488700e3e7SMoni Shoua }
9498700e3e7SMoni Shoua 
950c4367a26SShamir Rabinovitch static int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
9518700e3e7SMoni Shoua {
9528700e3e7SMoni Shoua 	struct rxe_mem *mr = to_rmr(ibmr);
9538700e3e7SMoni Shoua 
9548700e3e7SMoni Shoua 	mr->state = RXE_MEM_STATE_ZOMBIE;
9558700e3e7SMoni Shoua 	rxe_drop_ref(mr->pd);
9568700e3e7SMoni Shoua 	rxe_drop_index(mr);
9578700e3e7SMoni Shoua 	rxe_drop_ref(mr);
9588700e3e7SMoni Shoua 	return 0;
9598700e3e7SMoni Shoua }
9608700e3e7SMoni Shoua 
961c4367a26SShamir Rabinovitch static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
96242a3b153SGal Pressman 				  u32 max_num_sg)
9638700e3e7SMoni Shoua {
9648700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
9658700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
9668700e3e7SMoni Shoua 	struct rxe_mem *mr;
9678700e3e7SMoni Shoua 	int err;
9688700e3e7SMoni Shoua 
9698700e3e7SMoni Shoua 	if (mr_type != IB_MR_TYPE_MEM_REG)
9708700e3e7SMoni Shoua 		return ERR_PTR(-EINVAL);
9718700e3e7SMoni Shoua 
9728700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
9738700e3e7SMoni Shoua 	if (!mr) {
9748700e3e7SMoni Shoua 		err = -ENOMEM;
9758700e3e7SMoni Shoua 		goto err1;
9768700e3e7SMoni Shoua 	}
9778700e3e7SMoni Shoua 
9788700e3e7SMoni Shoua 	rxe_add_index(mr);
9798700e3e7SMoni Shoua 
9808700e3e7SMoni Shoua 	rxe_add_ref(pd);
9818700e3e7SMoni Shoua 
982e12ee8ceSZhu Yanjun 	err = rxe_mem_init_fast(pd, max_num_sg, mr);
9838700e3e7SMoni Shoua 	if (err)
9848700e3e7SMoni Shoua 		goto err2;
9858700e3e7SMoni Shoua 
9868700e3e7SMoni Shoua 	return &mr->ibmr;
9878700e3e7SMoni Shoua 
9888700e3e7SMoni Shoua err2:
9898700e3e7SMoni Shoua 	rxe_drop_ref(pd);
9908700e3e7SMoni Shoua 	rxe_drop_index(mr);
9918700e3e7SMoni Shoua 	rxe_drop_ref(mr);
9928700e3e7SMoni Shoua err1:
9938700e3e7SMoni Shoua 	return ERR_PTR(err);
9948700e3e7SMoni Shoua }
9958700e3e7SMoni Shoua 
9968700e3e7SMoni Shoua static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
9978700e3e7SMoni Shoua {
9988700e3e7SMoni Shoua 	struct rxe_mem *mr = to_rmr(ibmr);
9998700e3e7SMoni Shoua 	struct rxe_map *map;
10008700e3e7SMoni Shoua 	struct rxe_phys_buf *buf;
10018700e3e7SMoni Shoua 
10028700e3e7SMoni Shoua 	if (unlikely(mr->nbuf == mr->num_buf))
10038700e3e7SMoni Shoua 		return -ENOMEM;
10048700e3e7SMoni Shoua 
10058700e3e7SMoni Shoua 	map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
10068700e3e7SMoni Shoua 	buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
10078700e3e7SMoni Shoua 
10088700e3e7SMoni Shoua 	buf->addr = addr;
10098700e3e7SMoni Shoua 	buf->size = ibmr->page_size;
10108700e3e7SMoni Shoua 	mr->nbuf++;
10118700e3e7SMoni Shoua 
10128700e3e7SMoni Shoua 	return 0;
10138700e3e7SMoni Shoua }
10148700e3e7SMoni Shoua 
1015e404f945SParav Pandit static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
1016e404f945SParav Pandit 			 int sg_nents, unsigned int *sg_offset)
10178700e3e7SMoni Shoua {
10188700e3e7SMoni Shoua 	struct rxe_mem *mr = to_rmr(ibmr);
10198700e3e7SMoni Shoua 	int n;
10208700e3e7SMoni Shoua 
10218700e3e7SMoni Shoua 	mr->nbuf = 0;
10228700e3e7SMoni Shoua 
10238700e3e7SMoni Shoua 	n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
10248700e3e7SMoni Shoua 
10258700e3e7SMoni Shoua 	mr->va = ibmr->iova;
10268700e3e7SMoni Shoua 	mr->iova = ibmr->iova;
10278700e3e7SMoni Shoua 	mr->length = ibmr->length;
10288700e3e7SMoni Shoua 	mr->page_shift = ilog2(ibmr->page_size);
10298700e3e7SMoni Shoua 	mr->page_mask = ibmr->page_size - 1;
10308700e3e7SMoni Shoua 	mr->offset = mr->iova & mr->page_mask;
10318700e3e7SMoni Shoua 
10328700e3e7SMoni Shoua 	return n;
10338700e3e7SMoni Shoua }
10348700e3e7SMoni Shoua 
10358700e3e7SMoni Shoua static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
10368700e3e7SMoni Shoua {
10378700e3e7SMoni Shoua 	int err;
10388700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
10398700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
10408700e3e7SMoni Shoua 	struct rxe_mc_grp *grp;
10418700e3e7SMoni Shoua 
10428700e3e7SMoni Shoua 	/* takes a ref on grp if successful */
10438700e3e7SMoni Shoua 	err = rxe_mcast_get_grp(rxe, mgid, &grp);
10448700e3e7SMoni Shoua 	if (err)
10458700e3e7SMoni Shoua 		return err;
10468700e3e7SMoni Shoua 
10478700e3e7SMoni Shoua 	err = rxe_mcast_add_grp_elem(rxe, qp, grp);
10488700e3e7SMoni Shoua 
10498700e3e7SMoni Shoua 	rxe_drop_ref(grp);
10508700e3e7SMoni Shoua 	return err;
10518700e3e7SMoni Shoua }
10528700e3e7SMoni Shoua 
10538700e3e7SMoni Shoua static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
10548700e3e7SMoni Shoua {
10558700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
10568700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
10578700e3e7SMoni Shoua 
10588700e3e7SMoni Shoua 	return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
10598700e3e7SMoni Shoua }
10608700e3e7SMoni Shoua 
1061c05d2664SKamal Heib static ssize_t parent_show(struct device *device,
10628700e3e7SMoni Shoua 			   struct device_attribute *attr, char *buf)
10638700e3e7SMoni Shoua {
106454747231SParav Pandit 	struct rxe_dev *rxe =
106554747231SParav Pandit 		rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
10668700e3e7SMoni Shoua 
1067839f5ac0SBart Van Assche 	return snprintf(buf, 16, "%s\n", rxe_parent_name(rxe, 1));
10688700e3e7SMoni Shoua }
10698700e3e7SMoni Shoua 
1070c05d2664SKamal Heib static DEVICE_ATTR_RO(parent);
10718700e3e7SMoni Shoua 
1072508a523fSParav Pandit static struct attribute *rxe_dev_attributes[] = {
1073508a523fSParav Pandit 	&dev_attr_parent.attr,
1074508a523fSParav Pandit 	NULL
1075508a523fSParav Pandit };
1076508a523fSParav Pandit 
1077508a523fSParav Pandit static const struct attribute_group rxe_attr_group = {
1078508a523fSParav Pandit 	.attrs = rxe_dev_attributes,
10798700e3e7SMoni Shoua };
10808700e3e7SMoni Shoua 
1081ca22354bSJason Gunthorpe static int rxe_enable_driver(struct ib_device *ib_dev)
1082ca22354bSJason Gunthorpe {
1083ca22354bSJason Gunthorpe 	struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1084ca22354bSJason Gunthorpe 
1085ca22354bSJason Gunthorpe 	rxe_set_port_state(rxe);
1086ca22354bSJason Gunthorpe 	dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1087ca22354bSJason Gunthorpe 	return 0;
1088ca22354bSJason Gunthorpe }
1089ca22354bSJason Gunthorpe 
1090573efc4bSKamal Heib static const struct ib_device_ops rxe_dev_ops = {
10917a154142SJason Gunthorpe 	.owner = THIS_MODULE,
1092b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_RXE,
109372c6ec18SJason Gunthorpe 	.uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
1094b9560a41SJason Gunthorpe 
1095573efc4bSKamal Heib 	.alloc_hw_stats = rxe_ib_alloc_hw_stats,
1096573efc4bSKamal Heib 	.alloc_mr = rxe_alloc_mr,
1097573efc4bSKamal Heib 	.alloc_pd = rxe_alloc_pd,
1098573efc4bSKamal Heib 	.alloc_ucontext = rxe_alloc_ucontext,
1099573efc4bSKamal Heib 	.attach_mcast = rxe_attach_mcast,
1100573efc4bSKamal Heib 	.create_ah = rxe_create_ah,
1101573efc4bSKamal Heib 	.create_cq = rxe_create_cq,
1102573efc4bSKamal Heib 	.create_qp = rxe_create_qp,
1103573efc4bSKamal Heib 	.create_srq = rxe_create_srq,
1104c367074bSJason Gunthorpe 	.dealloc_driver = rxe_dealloc,
1105573efc4bSKamal Heib 	.dealloc_pd = rxe_dealloc_pd,
1106573efc4bSKamal Heib 	.dealloc_ucontext = rxe_dealloc_ucontext,
1107573efc4bSKamal Heib 	.dereg_mr = rxe_dereg_mr,
1108573efc4bSKamal Heib 	.destroy_ah = rxe_destroy_ah,
1109573efc4bSKamal Heib 	.destroy_cq = rxe_destroy_cq,
1110573efc4bSKamal Heib 	.destroy_qp = rxe_destroy_qp,
1111573efc4bSKamal Heib 	.destroy_srq = rxe_destroy_srq,
1112573efc4bSKamal Heib 	.detach_mcast = rxe_detach_mcast,
1113ca22354bSJason Gunthorpe 	.enable_driver = rxe_enable_driver,
1114573efc4bSKamal Heib 	.get_dma_mr = rxe_get_dma_mr,
1115573efc4bSKamal Heib 	.get_hw_stats = rxe_ib_get_hw_stats,
1116573efc4bSKamal Heib 	.get_link_layer = rxe_get_link_layer,
1117573efc4bSKamal Heib 	.get_port_immutable = rxe_port_immutable,
1118573efc4bSKamal Heib 	.map_mr_sg = rxe_map_mr_sg,
1119573efc4bSKamal Heib 	.mmap = rxe_mmap,
1120573efc4bSKamal Heib 	.modify_ah = rxe_modify_ah,
1121573efc4bSKamal Heib 	.modify_device = rxe_modify_device,
1122573efc4bSKamal Heib 	.modify_port = rxe_modify_port,
1123573efc4bSKamal Heib 	.modify_qp = rxe_modify_qp,
1124573efc4bSKamal Heib 	.modify_srq = rxe_modify_srq,
1125573efc4bSKamal Heib 	.peek_cq = rxe_peek_cq,
1126573efc4bSKamal Heib 	.poll_cq = rxe_poll_cq,
1127573efc4bSKamal Heib 	.post_recv = rxe_post_recv,
1128573efc4bSKamal Heib 	.post_send = rxe_post_send,
1129573efc4bSKamal Heib 	.post_srq_recv = rxe_post_srq_recv,
1130573efc4bSKamal Heib 	.query_ah = rxe_query_ah,
1131573efc4bSKamal Heib 	.query_device = rxe_query_device,
1132573efc4bSKamal Heib 	.query_pkey = rxe_query_pkey,
1133573efc4bSKamal Heib 	.query_port = rxe_query_port,
1134573efc4bSKamal Heib 	.query_qp = rxe_query_qp,
1135573efc4bSKamal Heib 	.query_srq = rxe_query_srq,
1136573efc4bSKamal Heib 	.reg_user_mr = rxe_reg_user_mr,
1137573efc4bSKamal Heib 	.req_notify_cq = rxe_req_notify_cq,
1138573efc4bSKamal Heib 	.resize_cq = rxe_resize_cq,
1139d3456914SLeon Romanovsky 
1140d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
1141e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
114221a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
114368e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
1144a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1145573efc4bSKamal Heib };
1146573efc4bSKamal Heib 
114766920e1bSSteve Wise int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
11488700e3e7SMoni Shoua {
11498700e3e7SMoni Shoua 	int err;
11508700e3e7SMoni Shoua 	struct ib_device *dev = &rxe->ib_dev;
11513192c53eSThomas Bogendoerfer 	struct crypto_shash *tfm;
11528700e3e7SMoni Shoua 
11538700e3e7SMoni Shoua 	strlcpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
11548700e3e7SMoni Shoua 
11558700e3e7SMoni Shoua 	dev->node_type = RDMA_NODE_IB_CA;
11568700e3e7SMoni Shoua 	dev->phys_port_cnt = 1;
115767cf3623SSagi Grimberg 	dev->num_comp_vectors = num_possible_cpus();
115885e9f1dbSBart Van Assche 	dev->dev.parent = rxe_dma_device(rxe);
11598700e3e7SMoni Shoua 	dev->local_dma_lkey = 0;
11604d6f2859SYuval Shaia 	addrconf_addr_eui48((unsigned char *)&dev->node_guid,
11614d6f2859SYuval Shaia 			    rxe->ndev->dev_addr);
11620bbb3b74SBart Van Assche 	dev->dev.dma_ops = &dma_virt_ops;
116397458fd5SBart Van Assche 	dev->dev.dma_parms = &rxe->dma_parms;
116497458fd5SBart Van Assche 	rxe->dma_parms = (struct device_dma_parameters)
116597458fd5SBart Van Assche 		{ .max_segment_size = SZ_2G };
116656012e1cSyonatanc 	dma_coerce_mask_and_coherent(&dev->dev,
1167efc365e7SMikhail Malygin 				     dma_get_required_mask(&dev->dev));
11688700e3e7SMoni Shoua 
11698700e3e7SMoni Shoua 	dev->uverbs_cmd_mask = BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT)
11708700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
11718700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE)
11728700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT)
11738700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD)
11748700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD)
11758700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ)
11768700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ)
11778700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ)
11788700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ)
11798700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POST_SRQ_RECV)
11808700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP)
11818700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP)
11828700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP)
11838700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP)
11848700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POST_SEND)
11858700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POST_RECV)
11868700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ)
11878700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ)
11888700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ)
11898700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ)
11908700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_PEEK_CQ)
11918700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)
11928700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_REG_MR)
11938700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR)
11948700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH)
11958700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_AH)
11968700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_AH)
11978700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH)
11988700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST)
11998700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST)
12008700e3e7SMoni Shoua 	    ;
12018700e3e7SMoni Shoua 
1202573efc4bSKamal Heib 	ib_set_device_ops(dev, &rxe_dev_ops);
12034c173f59SJason Gunthorpe 	err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
12044c173f59SJason Gunthorpe 	if (err)
12054c173f59SJason Gunthorpe 		return err;
12068700e3e7SMoni Shoua 
12073192c53eSThomas Bogendoerfer 	tfm = crypto_alloc_shash("crc32", 0, 0);
12083192c53eSThomas Bogendoerfer 	if (IS_ERR(tfm)) {
120927b0b832SColin Ian King 		pr_err("failed to allocate crc algorithm err:%ld\n",
12103192c53eSThomas Bogendoerfer 		       PTR_ERR(tfm));
12113192c53eSThomas Bogendoerfer 		return PTR_ERR(tfm);
1212cee2688eSyonatanc 	}
12133192c53eSThomas Bogendoerfer 	rxe->tfm = tfm;
1214cee2688eSyonatanc 
1215508a523fSParav Pandit 	rdma_set_device_sysfs_group(dev, &rxe_attr_group);
121666920e1bSSteve Wise 	err = ib_register_device(dev, ibdev_name);
1217c367074bSJason Gunthorpe 	if (err)
121861013828SKamal Heib 		pr_warn("%s failed with error %d\n", __func__, err);
1219cee2688eSyonatanc 
1220ca22354bSJason Gunthorpe 	/*
1221ca22354bSJason Gunthorpe 	 * Note that rxe may be invalid at this point if another thread
1222ca22354bSJason Gunthorpe 	 * unregistered it.
1223ca22354bSJason Gunthorpe 	 */
12248700e3e7SMoni Shoua 	return err;
12258700e3e7SMoni Shoua }
1226