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>
368700e3e7SMoni Shoua #include "rxe.h"
378700e3e7SMoni Shoua #include "rxe_loc.h"
388700e3e7SMoni Shoua #include "rxe_queue.h"
390b1e5b99SYonatan Cohen #include "rxe_hw_counters.h"
408700e3e7SMoni Shoua 
418700e3e7SMoni Shoua static int rxe_query_device(struct ib_device *dev,
428700e3e7SMoni Shoua 			    struct ib_device_attr *attr,
438700e3e7SMoni Shoua 			    struct ib_udata *uhw)
448700e3e7SMoni Shoua {
458700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
468700e3e7SMoni Shoua 
478700e3e7SMoni Shoua 	if (uhw->inlen || uhw->outlen)
488700e3e7SMoni Shoua 		return -EINVAL;
498700e3e7SMoni Shoua 
508700e3e7SMoni Shoua 	*attr = rxe->attr;
518700e3e7SMoni Shoua 	return 0;
528700e3e7SMoni Shoua }
538700e3e7SMoni Shoua 
548700e3e7SMoni Shoua static int rxe_query_port(struct ib_device *dev,
558700e3e7SMoni Shoua 			  u8 port_num, struct ib_port_attr *attr)
568700e3e7SMoni Shoua {
578700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
588700e3e7SMoni Shoua 	struct rxe_port *port;
59d4186194SYuval Shaia 	int rc = -EINVAL;
608700e3e7SMoni Shoua 
618700e3e7SMoni Shoua 	if (unlikely(port_num != 1)) {
628700e3e7SMoni Shoua 		pr_warn("invalid port_number %d\n", port_num);
63d4186194SYuval Shaia 		goto out;
648700e3e7SMoni Shoua 	}
658700e3e7SMoni Shoua 
668700e3e7SMoni Shoua 	port = &rxe->port;
678700e3e7SMoni Shoua 
68c4550c63SOr Gerlitz 	/* *attr being zeroed by the caller, avoid zeroing it here */
698700e3e7SMoni Shoua 	*attr = port->attr;
708700e3e7SMoni Shoua 
718700e3e7SMoni Shoua 	mutex_lock(&rxe->usdev_lock);
72d4186194SYuval Shaia 	rc = ib_get_eth_speed(dev, port_num, &attr->active_speed,
73e404f945SParav Pandit 			      &attr->active_width);
748700e3e7SMoni Shoua 	mutex_unlock(&rxe->usdev_lock);
758700e3e7SMoni Shoua 
76d4186194SYuval Shaia out:
77d4186194SYuval Shaia 	return rc;
788700e3e7SMoni Shoua }
798700e3e7SMoni Shoua 
808700e3e7SMoni Shoua static struct net_device *rxe_get_netdev(struct ib_device *device,
818700e3e7SMoni Shoua 					 u8 port_num)
828700e3e7SMoni Shoua {
838700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(device);
848700e3e7SMoni Shoua 
858700e3e7SMoni Shoua 	if (rxe->ndev) {
868700e3e7SMoni Shoua 		dev_hold(rxe->ndev);
878700e3e7SMoni Shoua 		return rxe->ndev;
888700e3e7SMoni Shoua 	}
898700e3e7SMoni Shoua 
908700e3e7SMoni Shoua 	return NULL;
918700e3e7SMoni Shoua }
928700e3e7SMoni Shoua 
938700e3e7SMoni Shoua static int rxe_query_pkey(struct ib_device *device,
948700e3e7SMoni Shoua 			  u8 port_num, u16 index, u16 *pkey)
958700e3e7SMoni Shoua {
968700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(device);
978700e3e7SMoni Shoua 	struct rxe_port *port;
988700e3e7SMoni Shoua 
998700e3e7SMoni Shoua 	if (unlikely(port_num != 1)) {
10085e9f1dbSBart Van Assche 		dev_warn(device->dev.parent, "invalid port_num = %d\n",
1018700e3e7SMoni Shoua 			 port_num);
1028700e3e7SMoni Shoua 		goto err1;
1038700e3e7SMoni Shoua 	}
1048700e3e7SMoni Shoua 
1058700e3e7SMoni Shoua 	port = &rxe->port;
1068700e3e7SMoni Shoua 
1078700e3e7SMoni Shoua 	if (unlikely(index >= port->attr.pkey_tbl_len)) {
10885e9f1dbSBart Van Assche 		dev_warn(device->dev.parent, "invalid index = %d\n",
1098700e3e7SMoni Shoua 			 index);
1108700e3e7SMoni Shoua 		goto err1;
1118700e3e7SMoni Shoua 	}
1128700e3e7SMoni Shoua 
1138700e3e7SMoni Shoua 	*pkey = port->pkey_tbl[index];
1148700e3e7SMoni Shoua 	return 0;
1158700e3e7SMoni Shoua 
1168700e3e7SMoni Shoua err1:
1178700e3e7SMoni Shoua 	return -EINVAL;
1188700e3e7SMoni Shoua }
1198700e3e7SMoni Shoua 
1208700e3e7SMoni Shoua static int rxe_modify_device(struct ib_device *dev,
1218700e3e7SMoni Shoua 			     int mask, struct ib_device_modify *attr)
1228700e3e7SMoni Shoua {
1238700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
1248700e3e7SMoni Shoua 
1258700e3e7SMoni Shoua 	if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1268700e3e7SMoni Shoua 		rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
1278700e3e7SMoni Shoua 
1288700e3e7SMoni Shoua 	if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
1298700e3e7SMoni Shoua 		memcpy(rxe->ib_dev.node_desc,
1308700e3e7SMoni Shoua 		       attr->node_desc, sizeof(rxe->ib_dev.node_desc));
1318700e3e7SMoni Shoua 	}
1328700e3e7SMoni Shoua 
1338700e3e7SMoni Shoua 	return 0;
1348700e3e7SMoni Shoua }
1358700e3e7SMoni Shoua 
1368700e3e7SMoni Shoua static int rxe_modify_port(struct ib_device *dev,
1378700e3e7SMoni Shoua 			   u8 port_num, int mask, struct ib_port_modify *attr)
1388700e3e7SMoni Shoua {
1398700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
1408700e3e7SMoni Shoua 	struct rxe_port *port;
1418700e3e7SMoni Shoua 
1428700e3e7SMoni Shoua 	if (unlikely(port_num != 1)) {
1438700e3e7SMoni Shoua 		pr_warn("invalid port_num = %d\n", port_num);
1448700e3e7SMoni Shoua 		goto err1;
1458700e3e7SMoni Shoua 	}
1468700e3e7SMoni Shoua 
1478700e3e7SMoni Shoua 	port = &rxe->port;
1488700e3e7SMoni Shoua 
1498700e3e7SMoni Shoua 	port->attr.port_cap_flags |= attr->set_port_cap_mask;
1508700e3e7SMoni Shoua 	port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
1518700e3e7SMoni Shoua 
1528700e3e7SMoni Shoua 	if (mask & IB_PORT_RESET_QKEY_CNTR)
1538700e3e7SMoni Shoua 		port->attr.qkey_viol_cntr = 0;
1548700e3e7SMoni Shoua 
1558700e3e7SMoni Shoua 	return 0;
1568700e3e7SMoni Shoua 
1578700e3e7SMoni Shoua err1:
1588700e3e7SMoni Shoua 	return -EINVAL;
1598700e3e7SMoni Shoua }
1608700e3e7SMoni Shoua 
1618700e3e7SMoni Shoua static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
1628700e3e7SMoni Shoua 					       u8 port_num)
1638700e3e7SMoni Shoua {
1648700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
1658700e3e7SMoni Shoua 
166839f5ac0SBart Van Assche 	return rxe_link_layer(rxe, port_num);
1678700e3e7SMoni Shoua }
1688700e3e7SMoni Shoua 
1698700e3e7SMoni Shoua static struct ib_ucontext *rxe_alloc_ucontext(struct ib_device *dev,
1708700e3e7SMoni Shoua 					      struct ib_udata *udata)
1718700e3e7SMoni Shoua {
1728700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
1738700e3e7SMoni Shoua 	struct rxe_ucontext *uc;
1748700e3e7SMoni Shoua 
1758700e3e7SMoni Shoua 	uc = rxe_alloc(&rxe->uc_pool);
1768700e3e7SMoni Shoua 	return uc ? &uc->ibuc : ERR_PTR(-ENOMEM);
1778700e3e7SMoni Shoua }
1788700e3e7SMoni Shoua 
1798700e3e7SMoni Shoua static int rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
1808700e3e7SMoni Shoua {
1818700e3e7SMoni Shoua 	struct rxe_ucontext *uc = to_ruc(ibuc);
1828700e3e7SMoni Shoua 
1838700e3e7SMoni Shoua 	rxe_drop_ref(uc);
1848700e3e7SMoni Shoua 	return 0;
1858700e3e7SMoni Shoua }
1868700e3e7SMoni Shoua 
1878700e3e7SMoni Shoua static int rxe_port_immutable(struct ib_device *dev, u8 port_num,
1888700e3e7SMoni Shoua 			      struct ib_port_immutable *immutable)
1898700e3e7SMoni Shoua {
1908700e3e7SMoni Shoua 	int err;
1918700e3e7SMoni Shoua 	struct ib_port_attr attr;
1928700e3e7SMoni Shoua 
193c4550c63SOr Gerlitz 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
194c4550c63SOr Gerlitz 
195c4550c63SOr Gerlitz 	err = ib_query_port(dev, port_num, &attr);
1968700e3e7SMoni Shoua 	if (err)
1978700e3e7SMoni Shoua 		return err;
1988700e3e7SMoni Shoua 
1998700e3e7SMoni Shoua 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
2008700e3e7SMoni Shoua 	immutable->gid_tbl_len = attr.gid_tbl_len;
2018700e3e7SMoni Shoua 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
2028700e3e7SMoni Shoua 
2038700e3e7SMoni Shoua 	return 0;
2048700e3e7SMoni Shoua }
2058700e3e7SMoni Shoua 
2068700e3e7SMoni Shoua static struct ib_pd *rxe_alloc_pd(struct ib_device *dev,
2078700e3e7SMoni Shoua 				  struct ib_ucontext *context,
2088700e3e7SMoni Shoua 				  struct ib_udata *udata)
2098700e3e7SMoni Shoua {
2108700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
2118700e3e7SMoni Shoua 	struct rxe_pd *pd;
2128700e3e7SMoni Shoua 
2138700e3e7SMoni Shoua 	pd = rxe_alloc(&rxe->pd_pool);
2148700e3e7SMoni Shoua 	return pd ? &pd->ibpd : ERR_PTR(-ENOMEM);
2158700e3e7SMoni Shoua }
2168700e3e7SMoni Shoua 
2178700e3e7SMoni Shoua static int rxe_dealloc_pd(struct ib_pd *ibpd)
2188700e3e7SMoni Shoua {
2198700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
2208700e3e7SMoni Shoua 
2218700e3e7SMoni Shoua 	rxe_drop_ref(pd);
2228700e3e7SMoni Shoua 	return 0;
2238700e3e7SMoni Shoua }
2248700e3e7SMoni Shoua 
22547ec3866SParav Pandit static void rxe_init_av(struct rxe_dev *rxe, struct rdma_ah_attr *attr,
2268700e3e7SMoni Shoua 			struct rxe_av *av)
2278700e3e7SMoni Shoua {
228ca3d9feeSZhu Yanjun 	rxe_av_from_attr(rdma_ah_get_port_num(attr), av, attr);
22947ec3866SParav Pandit 	rxe_av_fill_ip_info(av, attr);
2308700e3e7SMoni Shoua }
2318700e3e7SMoni Shoua 
23290898850SDasaratharaman Chandramouli static struct ib_ah *rxe_create_ah(struct ib_pd *ibpd,
23390898850SDasaratharaman Chandramouli 				   struct rdma_ah_attr *attr,
234477864c8SMoni Shoua 				   struct ib_udata *udata)
235477864c8SMoni Shoua 
2368700e3e7SMoni Shoua {
2378700e3e7SMoni Shoua 	int err;
2388700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
2398700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
2408700e3e7SMoni Shoua 	struct rxe_ah *ah;
2418700e3e7SMoni Shoua 
2428700e3e7SMoni Shoua 	err = rxe_av_chk_attr(rxe, attr);
2438700e3e7SMoni Shoua 	if (err)
2442f229bcfSBart Van Assche 		return ERR_PTR(err);
2458700e3e7SMoni Shoua 
2468700e3e7SMoni Shoua 	ah = rxe_alloc(&rxe->ah_pool);
2472f229bcfSBart Van Assche 	if (!ah)
2482f229bcfSBart Van Assche 		return ERR_PTR(-ENOMEM);
2498700e3e7SMoni Shoua 
2508700e3e7SMoni Shoua 	rxe_add_ref(pd);
2518700e3e7SMoni Shoua 	ah->pd = pd;
2528700e3e7SMoni Shoua 
25347ec3866SParav Pandit 	rxe_init_av(rxe, attr, &ah->av);
2548700e3e7SMoni Shoua 	return &ah->ibah;
2558700e3e7SMoni Shoua }
2568700e3e7SMoni Shoua 
25790898850SDasaratharaman Chandramouli static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
2588700e3e7SMoni Shoua {
2598700e3e7SMoni Shoua 	int err;
2608700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibah->device);
2618700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2628700e3e7SMoni Shoua 
2638700e3e7SMoni Shoua 	err = rxe_av_chk_attr(rxe, attr);
2648700e3e7SMoni Shoua 	if (err)
2658700e3e7SMoni Shoua 		return err;
2668700e3e7SMoni Shoua 
26747ec3866SParav Pandit 	rxe_init_av(rxe, attr, &ah->av);
2688700e3e7SMoni Shoua 	return 0;
2698700e3e7SMoni Shoua }
2708700e3e7SMoni Shoua 
27190898850SDasaratharaman Chandramouli static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
2728700e3e7SMoni Shoua {
2738700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2748700e3e7SMoni Shoua 
275eca7ddf9SDasaratharaman Chandramouli 	memset(attr, 0, sizeof(*attr));
27644c58487SDasaratharaman Chandramouli 	attr->type = ibah->type;
2779c96f3d4SZhu Yanjun 	rxe_av_to_attr(&ah->av, attr);
2788700e3e7SMoni Shoua 	return 0;
2798700e3e7SMoni Shoua }
2808700e3e7SMoni Shoua 
2818700e3e7SMoni Shoua static int rxe_destroy_ah(struct ib_ah *ibah)
2828700e3e7SMoni Shoua {
2838700e3e7SMoni Shoua 	struct rxe_ah *ah = to_rah(ibah);
2848700e3e7SMoni Shoua 
2858700e3e7SMoni Shoua 	rxe_drop_ref(ah->pd);
2868700e3e7SMoni Shoua 	rxe_drop_ref(ah);
2878700e3e7SMoni Shoua 	return 0;
2888700e3e7SMoni Shoua }
2898700e3e7SMoni Shoua 
290d34ac5cdSBart Van Assche static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
2918700e3e7SMoni Shoua {
2928700e3e7SMoni Shoua 	int err;
2938700e3e7SMoni Shoua 	int i;
2948700e3e7SMoni Shoua 	u32 length;
2958700e3e7SMoni Shoua 	struct rxe_recv_wqe *recv_wqe;
2968700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
2978700e3e7SMoni Shoua 
2988700e3e7SMoni Shoua 	if (unlikely(queue_full(rq->queue))) {
2998700e3e7SMoni Shoua 		err = -ENOMEM;
3008700e3e7SMoni Shoua 		goto err1;
3018700e3e7SMoni Shoua 	}
3028700e3e7SMoni Shoua 
3038700e3e7SMoni Shoua 	if (unlikely(num_sge > rq->max_sge)) {
3048700e3e7SMoni Shoua 		err = -EINVAL;
3058700e3e7SMoni Shoua 		goto err1;
3068700e3e7SMoni Shoua 	}
3078700e3e7SMoni Shoua 
3088700e3e7SMoni Shoua 	length = 0;
3098700e3e7SMoni Shoua 	for (i = 0; i < num_sge; i++)
3108700e3e7SMoni Shoua 		length += ibwr->sg_list[i].length;
3118700e3e7SMoni Shoua 
3128700e3e7SMoni Shoua 	recv_wqe = producer_addr(rq->queue);
3138700e3e7SMoni Shoua 	recv_wqe->wr_id = ibwr->wr_id;
3148700e3e7SMoni Shoua 	recv_wqe->num_sge = num_sge;
3158700e3e7SMoni Shoua 
3168700e3e7SMoni Shoua 	memcpy(recv_wqe->dma.sge, ibwr->sg_list,
3178700e3e7SMoni Shoua 	       num_sge * sizeof(struct ib_sge));
3188700e3e7SMoni Shoua 
3198700e3e7SMoni Shoua 	recv_wqe->dma.length		= length;
3208700e3e7SMoni Shoua 	recv_wqe->dma.resid		= length;
3218700e3e7SMoni Shoua 	recv_wqe->dma.num_sge		= num_sge;
3228700e3e7SMoni Shoua 	recv_wqe->dma.cur_sge		= 0;
3238700e3e7SMoni Shoua 	recv_wqe->dma.sge_offset	= 0;
3248700e3e7SMoni Shoua 
3258700e3e7SMoni Shoua 	/* make sure all changes to the work queue are written before we
3268700e3e7SMoni Shoua 	 * update the producer pointer
3278700e3e7SMoni Shoua 	 */
3288700e3e7SMoni Shoua 	smp_wmb();
3298700e3e7SMoni Shoua 
3308700e3e7SMoni Shoua 	advance_producer(rq->queue);
3318700e3e7SMoni Shoua 	return 0;
3328700e3e7SMoni Shoua 
3338700e3e7SMoni Shoua err1:
3348700e3e7SMoni Shoua 	return err;
3358700e3e7SMoni Shoua }
3368700e3e7SMoni Shoua 
3378700e3e7SMoni Shoua static struct ib_srq *rxe_create_srq(struct ib_pd *ibpd,
3388700e3e7SMoni Shoua 				     struct ib_srq_init_attr *init,
3398700e3e7SMoni Shoua 				     struct ib_udata *udata)
3408700e3e7SMoni Shoua {
3418700e3e7SMoni Shoua 	int err;
3428700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
3438700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
3448700e3e7SMoni Shoua 	struct rxe_srq *srq;
3458700e3e7SMoni Shoua 	struct ib_ucontext *context = udata ? ibpd->uobject->context : NULL;
3460c43ab37SJason Gunthorpe 	struct rxe_create_srq_resp __user *uresp = NULL;
3470c43ab37SJason Gunthorpe 
3480c43ab37SJason Gunthorpe 	if (udata) {
3490c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
3500c43ab37SJason Gunthorpe 			return ERR_PTR(-EINVAL);
3510c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
3520c43ab37SJason Gunthorpe 	}
3538700e3e7SMoni Shoua 
3548700e3e7SMoni Shoua 	err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
3558700e3e7SMoni Shoua 	if (err)
3568700e3e7SMoni Shoua 		goto err1;
3578700e3e7SMoni Shoua 
3588700e3e7SMoni Shoua 	srq = rxe_alloc(&rxe->srq_pool);
3598700e3e7SMoni Shoua 	if (!srq) {
3608700e3e7SMoni Shoua 		err = -ENOMEM;
3618700e3e7SMoni Shoua 		goto err1;
3628700e3e7SMoni Shoua 	}
3638700e3e7SMoni Shoua 
3648700e3e7SMoni Shoua 	rxe_add_index(srq);
3658700e3e7SMoni Shoua 	rxe_add_ref(pd);
3668700e3e7SMoni Shoua 	srq->pd = pd;
3678700e3e7SMoni Shoua 
3680c43ab37SJason Gunthorpe 	err = rxe_srq_from_init(rxe, srq, init, context, uresp);
3698700e3e7SMoni Shoua 	if (err)
3708700e3e7SMoni Shoua 		goto err2;
3718700e3e7SMoni Shoua 
3728700e3e7SMoni Shoua 	return &srq->ibsrq;
3738700e3e7SMoni Shoua 
3748700e3e7SMoni Shoua err2:
3758700e3e7SMoni Shoua 	rxe_drop_ref(pd);
3768700e3e7SMoni Shoua 	rxe_drop_index(srq);
3778700e3e7SMoni Shoua 	rxe_drop_ref(srq);
3788700e3e7SMoni Shoua err1:
3798700e3e7SMoni Shoua 	return ERR_PTR(err);
3808700e3e7SMoni Shoua }
3818700e3e7SMoni Shoua 
3828700e3e7SMoni Shoua static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
3838700e3e7SMoni Shoua 			  enum ib_srq_attr_mask mask,
3848700e3e7SMoni Shoua 			  struct ib_udata *udata)
3858700e3e7SMoni Shoua {
3868700e3e7SMoni Shoua 	int err;
3878700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
3888700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibsrq->device);
3890c43ab37SJason Gunthorpe 	struct rxe_modify_srq_cmd ucmd = {};
3900c43ab37SJason Gunthorpe 
3910c43ab37SJason Gunthorpe 	if (udata) {
3920c43ab37SJason Gunthorpe 		if (udata->inlen < sizeof(ucmd))
3930c43ab37SJason Gunthorpe 			return -EINVAL;
3940c43ab37SJason Gunthorpe 
3950c43ab37SJason Gunthorpe 		err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
3960c43ab37SJason Gunthorpe 		if (err)
3970c43ab37SJason Gunthorpe 			return err;
3980c43ab37SJason Gunthorpe 	}
3998700e3e7SMoni Shoua 
4008700e3e7SMoni Shoua 	err = rxe_srq_chk_attr(rxe, srq, attr, mask);
4018700e3e7SMoni Shoua 	if (err)
4028700e3e7SMoni Shoua 		goto err1;
4038700e3e7SMoni Shoua 
4040c43ab37SJason Gunthorpe 	err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd);
4058700e3e7SMoni Shoua 	if (err)
4068700e3e7SMoni Shoua 		goto err1;
4078700e3e7SMoni Shoua 
4088700e3e7SMoni Shoua 	return 0;
4098700e3e7SMoni Shoua 
4108700e3e7SMoni Shoua err1:
4118700e3e7SMoni Shoua 	return err;
4128700e3e7SMoni Shoua }
4138700e3e7SMoni Shoua 
4148700e3e7SMoni Shoua static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
4158700e3e7SMoni Shoua {
4168700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
4178700e3e7SMoni Shoua 
4188700e3e7SMoni Shoua 	if (srq->error)
4198700e3e7SMoni Shoua 		return -EINVAL;
4208700e3e7SMoni Shoua 
4218700e3e7SMoni Shoua 	attr->max_wr = srq->rq.queue->buf->index_mask;
4228700e3e7SMoni Shoua 	attr->max_sge = srq->rq.max_sge;
4238700e3e7SMoni Shoua 	attr->srq_limit = srq->limit;
4248700e3e7SMoni Shoua 	return 0;
4258700e3e7SMoni Shoua }
4268700e3e7SMoni Shoua 
4278700e3e7SMoni Shoua static int rxe_destroy_srq(struct ib_srq *ibsrq)
4288700e3e7SMoni Shoua {
4298700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
4308700e3e7SMoni Shoua 
4318700e3e7SMoni Shoua 	if (srq->rq.queue)
4328700e3e7SMoni Shoua 		rxe_queue_cleanup(srq->rq.queue);
4338700e3e7SMoni Shoua 
4348700e3e7SMoni Shoua 	rxe_drop_ref(srq->pd);
4358700e3e7SMoni Shoua 	rxe_drop_index(srq);
4368700e3e7SMoni Shoua 	rxe_drop_ref(srq);
4378700e3e7SMoni Shoua 
4388700e3e7SMoni Shoua 	return 0;
4398700e3e7SMoni Shoua }
4408700e3e7SMoni Shoua 
441d34ac5cdSBart Van Assche static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
442d34ac5cdSBart Van Assche 			     const struct ib_recv_wr **bad_wr)
4438700e3e7SMoni Shoua {
4448700e3e7SMoni Shoua 	int err = 0;
4458700e3e7SMoni Shoua 	unsigned long flags;
4468700e3e7SMoni Shoua 	struct rxe_srq *srq = to_rsrq(ibsrq);
4478700e3e7SMoni Shoua 
4488700e3e7SMoni Shoua 	spin_lock_irqsave(&srq->rq.producer_lock, flags);
4498700e3e7SMoni Shoua 
4508700e3e7SMoni Shoua 	while (wr) {
4518700e3e7SMoni Shoua 		err = post_one_recv(&srq->rq, wr);
4528700e3e7SMoni Shoua 		if (unlikely(err))
4538700e3e7SMoni Shoua 			break;
4548700e3e7SMoni Shoua 		wr = wr->next;
4558700e3e7SMoni Shoua 	}
4568700e3e7SMoni Shoua 
4578700e3e7SMoni Shoua 	spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
4588700e3e7SMoni Shoua 
4598700e3e7SMoni Shoua 	if (err)
4608700e3e7SMoni Shoua 		*bad_wr = wr;
4618700e3e7SMoni Shoua 
4628700e3e7SMoni Shoua 	return err;
4638700e3e7SMoni Shoua }
4648700e3e7SMoni Shoua 
4658700e3e7SMoni Shoua static struct ib_qp *rxe_create_qp(struct ib_pd *ibpd,
4668700e3e7SMoni Shoua 				   struct ib_qp_init_attr *init,
4678700e3e7SMoni Shoua 				   struct ib_udata *udata)
4688700e3e7SMoni Shoua {
4698700e3e7SMoni Shoua 	int err;
4708700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
4718700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
4728700e3e7SMoni Shoua 	struct rxe_qp *qp;
4730c43ab37SJason Gunthorpe 	struct rxe_create_qp_resp __user *uresp = NULL;
4740c43ab37SJason Gunthorpe 
4750c43ab37SJason Gunthorpe 	if (udata) {
4760c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
4770c43ab37SJason Gunthorpe 			return ERR_PTR(-EINVAL);
4780c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
4790c43ab37SJason Gunthorpe 	}
4808700e3e7SMoni Shoua 
4818700e3e7SMoni Shoua 	err = rxe_qp_chk_init(rxe, init);
4828700e3e7SMoni Shoua 	if (err)
4838700e3e7SMoni Shoua 		goto err1;
4848700e3e7SMoni Shoua 
4858700e3e7SMoni Shoua 	qp = rxe_alloc(&rxe->qp_pool);
4868700e3e7SMoni Shoua 	if (!qp) {
4878700e3e7SMoni Shoua 		err = -ENOMEM;
4888700e3e7SMoni Shoua 		goto err1;
4898700e3e7SMoni Shoua 	}
4908700e3e7SMoni Shoua 
4918700e3e7SMoni Shoua 	if (udata) {
4928700e3e7SMoni Shoua 		if (udata->inlen) {
4938700e3e7SMoni Shoua 			err = -EINVAL;
4945b9ea16cSAndrew Boyer 			goto err2;
4958700e3e7SMoni Shoua 		}
4968700e3e7SMoni Shoua 		qp->is_user = 1;
4978700e3e7SMoni Shoua 	}
4988700e3e7SMoni Shoua 
4998700e3e7SMoni Shoua 	rxe_add_index(qp);
5008700e3e7SMoni Shoua 
5010c43ab37SJason Gunthorpe 	err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibpd);
5028700e3e7SMoni Shoua 	if (err)
5035b9ea16cSAndrew Boyer 		goto err3;
5048700e3e7SMoni Shoua 
5058700e3e7SMoni Shoua 	return &qp->ibqp;
5068700e3e7SMoni Shoua 
5075b9ea16cSAndrew Boyer err3:
5088700e3e7SMoni Shoua 	rxe_drop_index(qp);
5095b9ea16cSAndrew Boyer err2:
5108700e3e7SMoni Shoua 	rxe_drop_ref(qp);
5118700e3e7SMoni Shoua err1:
5128700e3e7SMoni Shoua 	return ERR_PTR(err);
5138700e3e7SMoni Shoua }
5148700e3e7SMoni Shoua 
5158700e3e7SMoni Shoua static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
5168700e3e7SMoni Shoua 			 int mask, struct ib_udata *udata)
5178700e3e7SMoni Shoua {
5188700e3e7SMoni Shoua 	int err;
5198700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
5208700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
5218700e3e7SMoni Shoua 
5228700e3e7SMoni Shoua 	err = rxe_qp_chk_attr(rxe, qp, attr, mask);
5238700e3e7SMoni Shoua 	if (err)
5248700e3e7SMoni Shoua 		goto err1;
5258700e3e7SMoni Shoua 
5268700e3e7SMoni Shoua 	err = rxe_qp_from_attr(qp, attr, mask, udata);
5278700e3e7SMoni Shoua 	if (err)
5288700e3e7SMoni Shoua 		goto err1;
5298700e3e7SMoni Shoua 
5308700e3e7SMoni Shoua 	return 0;
5318700e3e7SMoni Shoua 
5328700e3e7SMoni Shoua err1:
5338700e3e7SMoni Shoua 	return err;
5348700e3e7SMoni Shoua }
5358700e3e7SMoni Shoua 
5368700e3e7SMoni Shoua static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
5378700e3e7SMoni Shoua 			int mask, struct ib_qp_init_attr *init)
5388700e3e7SMoni Shoua {
5398700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
5408700e3e7SMoni Shoua 
5418700e3e7SMoni Shoua 	rxe_qp_to_init(qp, init);
5428700e3e7SMoni Shoua 	rxe_qp_to_attr(qp, attr, mask);
5438700e3e7SMoni Shoua 
5448700e3e7SMoni Shoua 	return 0;
5458700e3e7SMoni Shoua }
5468700e3e7SMoni Shoua 
5478700e3e7SMoni Shoua static int rxe_destroy_qp(struct ib_qp *ibqp)
5488700e3e7SMoni Shoua {
5498700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
5508700e3e7SMoni Shoua 
5518700e3e7SMoni Shoua 	rxe_qp_destroy(qp);
5528700e3e7SMoni Shoua 	rxe_drop_index(qp);
5538700e3e7SMoni Shoua 	rxe_drop_ref(qp);
5548700e3e7SMoni Shoua 	return 0;
5558700e3e7SMoni Shoua }
5568700e3e7SMoni Shoua 
557f696bf6dSBart Van Assche static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
5588700e3e7SMoni Shoua 			    unsigned int mask, unsigned int length)
5598700e3e7SMoni Shoua {
5608700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
5618700e3e7SMoni Shoua 	struct rxe_sq *sq = &qp->sq;
5628700e3e7SMoni Shoua 
5638700e3e7SMoni Shoua 	if (unlikely(num_sge > sq->max_sge))
5648700e3e7SMoni Shoua 		goto err1;
5658700e3e7SMoni Shoua 
5668700e3e7SMoni Shoua 	if (unlikely(mask & WR_ATOMIC_MASK)) {
5678700e3e7SMoni Shoua 		if (length < 8)
5688700e3e7SMoni Shoua 			goto err1;
5698700e3e7SMoni Shoua 
5708700e3e7SMoni Shoua 		if (atomic_wr(ibwr)->remote_addr & 0x7)
5718700e3e7SMoni Shoua 			goto err1;
5728700e3e7SMoni Shoua 	}
5738700e3e7SMoni Shoua 
5748700e3e7SMoni Shoua 	if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
5758700e3e7SMoni Shoua 		     (length > sq->max_inline)))
5768700e3e7SMoni Shoua 		goto err1;
5778700e3e7SMoni Shoua 
5788700e3e7SMoni Shoua 	return 0;
5798700e3e7SMoni Shoua 
5808700e3e7SMoni Shoua err1:
5818700e3e7SMoni Shoua 	return -EINVAL;
5828700e3e7SMoni Shoua }
5838700e3e7SMoni Shoua 
5848700e3e7SMoni Shoua static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
585f696bf6dSBart Van Assche 			 const struct ib_send_wr *ibwr)
5868700e3e7SMoni Shoua {
5878700e3e7SMoni Shoua 	wr->wr_id = ibwr->wr_id;
5888700e3e7SMoni Shoua 	wr->num_sge = ibwr->num_sge;
5898700e3e7SMoni Shoua 	wr->opcode = ibwr->opcode;
5908700e3e7SMoni Shoua 	wr->send_flags = ibwr->send_flags;
5918700e3e7SMoni Shoua 
5928700e3e7SMoni Shoua 	if (qp_type(qp) == IB_QPT_UD ||
5938700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_SMI ||
5948700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_GSI) {
5958700e3e7SMoni Shoua 		wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
5968700e3e7SMoni Shoua 		wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
5978700e3e7SMoni Shoua 		if (qp_type(qp) == IB_QPT_GSI)
5988700e3e7SMoni Shoua 			wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
5998700e3e7SMoni Shoua 		if (wr->opcode == IB_WR_SEND_WITH_IMM)
6008700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
6018700e3e7SMoni Shoua 	} else {
6028700e3e7SMoni Shoua 		switch (wr->opcode) {
6038700e3e7SMoni Shoua 		case IB_WR_RDMA_WRITE_WITH_IMM:
6048700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
605ea6ee93bSBart Van Assche 			/* fall through */
6068700e3e7SMoni Shoua 		case IB_WR_RDMA_READ:
6078700e3e7SMoni Shoua 		case IB_WR_RDMA_WRITE:
6088700e3e7SMoni Shoua 			wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
6098700e3e7SMoni Shoua 			wr->wr.rdma.rkey	= rdma_wr(ibwr)->rkey;
6108700e3e7SMoni Shoua 			break;
6118700e3e7SMoni Shoua 		case IB_WR_SEND_WITH_IMM:
6128700e3e7SMoni Shoua 			wr->ex.imm_data = ibwr->ex.imm_data;
6138700e3e7SMoni Shoua 			break;
6148700e3e7SMoni Shoua 		case IB_WR_SEND_WITH_INV:
6158700e3e7SMoni Shoua 			wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
6168700e3e7SMoni Shoua 			break;
6178700e3e7SMoni Shoua 		case IB_WR_ATOMIC_CMP_AND_SWP:
6188700e3e7SMoni Shoua 		case IB_WR_ATOMIC_FETCH_AND_ADD:
6198700e3e7SMoni Shoua 			wr->wr.atomic.remote_addr =
6208700e3e7SMoni Shoua 				atomic_wr(ibwr)->remote_addr;
6218700e3e7SMoni Shoua 			wr->wr.atomic.compare_add =
6228700e3e7SMoni Shoua 				atomic_wr(ibwr)->compare_add;
6238700e3e7SMoni Shoua 			wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
6248700e3e7SMoni Shoua 			wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
6258700e3e7SMoni Shoua 			break;
6268700e3e7SMoni Shoua 		case IB_WR_LOCAL_INV:
6278700e3e7SMoni Shoua 			wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
6288700e3e7SMoni Shoua 		break;
6298700e3e7SMoni Shoua 		case IB_WR_REG_MR:
6308700e3e7SMoni Shoua 			wr->wr.reg.mr = reg_wr(ibwr)->mr;
6318700e3e7SMoni Shoua 			wr->wr.reg.key = reg_wr(ibwr)->key;
6328700e3e7SMoni Shoua 			wr->wr.reg.access = reg_wr(ibwr)->access;
6338700e3e7SMoni Shoua 		break;
6348700e3e7SMoni Shoua 		default:
6358700e3e7SMoni Shoua 			break;
6368700e3e7SMoni Shoua 		}
6378700e3e7SMoni Shoua 	}
6388700e3e7SMoni Shoua }
6398700e3e7SMoni Shoua 
640f696bf6dSBart Van Assche static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
6418700e3e7SMoni Shoua 			 unsigned int mask, unsigned int length,
6428700e3e7SMoni Shoua 			 struct rxe_send_wqe *wqe)
6438700e3e7SMoni Shoua {
6448700e3e7SMoni Shoua 	int num_sge = ibwr->num_sge;
6458700e3e7SMoni Shoua 	struct ib_sge *sge;
6468700e3e7SMoni Shoua 	int i;
6478700e3e7SMoni Shoua 	u8 *p;
6488700e3e7SMoni Shoua 
6498700e3e7SMoni Shoua 	init_send_wr(qp, &wqe->wr, ibwr);
6508700e3e7SMoni Shoua 
6518700e3e7SMoni Shoua 	if (qp_type(qp) == IB_QPT_UD ||
6528700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_SMI ||
6538700e3e7SMoni Shoua 	    qp_type(qp) == IB_QPT_GSI)
6548700e3e7SMoni Shoua 		memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
6558700e3e7SMoni Shoua 
6568700e3e7SMoni Shoua 	if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) {
6578700e3e7SMoni Shoua 		p = wqe->dma.inline_data;
6588700e3e7SMoni Shoua 
6598700e3e7SMoni Shoua 		sge = ibwr->sg_list;
6608700e3e7SMoni Shoua 		for (i = 0; i < num_sge; i++, sge++) {
6618700e3e7SMoni Shoua 			memcpy(p, (void *)(uintptr_t)sge->addr,
6628700e3e7SMoni Shoua 					sge->length);
6638700e3e7SMoni Shoua 
6648700e3e7SMoni Shoua 			p += sge->length;
6658700e3e7SMoni Shoua 		}
6668700e3e7SMoni Shoua 	} else if (mask & WR_REG_MASK) {
6678700e3e7SMoni Shoua 		wqe->mask = mask;
6688700e3e7SMoni Shoua 		wqe->state = wqe_state_posted;
6698700e3e7SMoni Shoua 		return 0;
6708700e3e7SMoni Shoua 	} else
6718700e3e7SMoni Shoua 		memcpy(wqe->dma.sge, ibwr->sg_list,
6728700e3e7SMoni Shoua 		       num_sge * sizeof(struct ib_sge));
6738700e3e7SMoni Shoua 
674a6544a62SBart Van Assche 	wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
675a6544a62SBart Van Assche 		mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
6768700e3e7SMoni Shoua 	wqe->mask		= mask;
6778700e3e7SMoni Shoua 	wqe->dma.length		= length;
6788700e3e7SMoni Shoua 	wqe->dma.resid		= length;
6798700e3e7SMoni Shoua 	wqe->dma.num_sge	= num_sge;
6808700e3e7SMoni Shoua 	wqe->dma.cur_sge	= 0;
6818700e3e7SMoni Shoua 	wqe->dma.sge_offset	= 0;
6828700e3e7SMoni Shoua 	wqe->state		= wqe_state_posted;
6838700e3e7SMoni Shoua 	wqe->ssn		= atomic_add_return(1, &qp->ssn);
6848700e3e7SMoni Shoua 
6858700e3e7SMoni Shoua 	return 0;
6868700e3e7SMoni Shoua }
6878700e3e7SMoni Shoua 
688f696bf6dSBart Van Assche static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
689e404f945SParav Pandit 			 unsigned int mask, u32 length)
6908700e3e7SMoni Shoua {
6918700e3e7SMoni Shoua 	int err;
6928700e3e7SMoni Shoua 	struct rxe_sq *sq = &qp->sq;
6938700e3e7SMoni Shoua 	struct rxe_send_wqe *send_wqe;
6948700e3e7SMoni Shoua 	unsigned long flags;
6958700e3e7SMoni Shoua 
6968700e3e7SMoni Shoua 	err = validate_send_wr(qp, ibwr, mask, length);
6978700e3e7SMoni Shoua 	if (err)
6988700e3e7SMoni Shoua 		return err;
6998700e3e7SMoni Shoua 
7008700e3e7SMoni Shoua 	spin_lock_irqsave(&qp->sq.sq_lock, flags);
7018700e3e7SMoni Shoua 
7028700e3e7SMoni Shoua 	if (unlikely(queue_full(sq->queue))) {
7038700e3e7SMoni Shoua 		err = -ENOMEM;
7048700e3e7SMoni Shoua 		goto err1;
7058700e3e7SMoni Shoua 	}
7068700e3e7SMoni Shoua 
7078700e3e7SMoni Shoua 	send_wqe = producer_addr(sq->queue);
7088700e3e7SMoni Shoua 
7098700e3e7SMoni Shoua 	err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
7108700e3e7SMoni Shoua 	if (unlikely(err))
7118700e3e7SMoni Shoua 		goto err1;
7128700e3e7SMoni Shoua 
7138700e3e7SMoni Shoua 	/*
7148700e3e7SMoni Shoua 	 * make sure all changes to the work queue are
7158700e3e7SMoni Shoua 	 * written before we update the producer pointer
7168700e3e7SMoni Shoua 	 */
7178700e3e7SMoni Shoua 	smp_wmb();
7188700e3e7SMoni Shoua 
7198700e3e7SMoni Shoua 	advance_producer(sq->queue);
7208700e3e7SMoni Shoua 	spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
7218700e3e7SMoni Shoua 
7228700e3e7SMoni Shoua 	return 0;
7238700e3e7SMoni Shoua 
7248700e3e7SMoni Shoua err1:
7258700e3e7SMoni Shoua 	spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
7268700e3e7SMoni Shoua 	return err;
7278700e3e7SMoni Shoua }
7288700e3e7SMoni Shoua 
729d34ac5cdSBart Van Assche static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
730d34ac5cdSBart Van Assche 				const struct ib_send_wr **bad_wr)
7318700e3e7SMoni Shoua {
7328700e3e7SMoni Shoua 	int err = 0;
7338700e3e7SMoni Shoua 	unsigned int mask;
7348700e3e7SMoni Shoua 	unsigned int length = 0;
7358700e3e7SMoni Shoua 	int i;
7368700e3e7SMoni Shoua 
7378700e3e7SMoni Shoua 	while (wr) {
7388700e3e7SMoni Shoua 		mask = wr_opcode_mask(wr->opcode, qp);
7398700e3e7SMoni Shoua 		if (unlikely(!mask)) {
7408700e3e7SMoni Shoua 			err = -EINVAL;
7418700e3e7SMoni Shoua 			*bad_wr = wr;
7428700e3e7SMoni Shoua 			break;
7438700e3e7SMoni Shoua 		}
7448700e3e7SMoni Shoua 
7458700e3e7SMoni Shoua 		if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
7468700e3e7SMoni Shoua 			     !(mask & WR_INLINE_MASK))) {
7478700e3e7SMoni Shoua 			err = -EINVAL;
7488700e3e7SMoni Shoua 			*bad_wr = wr;
7498700e3e7SMoni Shoua 			break;
7508700e3e7SMoni Shoua 		}
7518700e3e7SMoni Shoua 
7528700e3e7SMoni Shoua 		length = 0;
7538700e3e7SMoni Shoua 		for (i = 0; i < wr->num_sge; i++)
7548700e3e7SMoni Shoua 			length += wr->sg_list[i].length;
7558700e3e7SMoni Shoua 
7568700e3e7SMoni Shoua 		err = post_one_send(qp, wr, mask, length);
7578700e3e7SMoni Shoua 
7588700e3e7SMoni Shoua 		if (err) {
7598700e3e7SMoni Shoua 			*bad_wr = wr;
7608700e3e7SMoni Shoua 			break;
7618700e3e7SMoni Shoua 		}
7628700e3e7SMoni Shoua 		wr = wr->next;
7638700e3e7SMoni Shoua 	}
7648700e3e7SMoni Shoua 
7651661d3b0SAlexandru Moise 	rxe_run_task(&qp->req.task, 1);
7666f301e06SBart Van Assche 	if (unlikely(qp->req.state == QP_STATE_ERROR))
7676f301e06SBart Van Assche 		rxe_run_task(&qp->comp.task, 1);
7688700e3e7SMoni Shoua 
7698700e3e7SMoni Shoua 	return err;
7708700e3e7SMoni Shoua }
7718700e3e7SMoni Shoua 
772d34ac5cdSBart Van Assche static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
773d34ac5cdSBart Van Assche 			 const struct ib_send_wr **bad_wr)
774063af595SParav Pandit {
775063af595SParav Pandit 	struct rxe_qp *qp = to_rqp(ibqp);
776063af595SParav Pandit 
777063af595SParav Pandit 	if (unlikely(!qp->valid)) {
778063af595SParav Pandit 		*bad_wr = wr;
779063af595SParav Pandit 		return -EINVAL;
780063af595SParav Pandit 	}
781063af595SParav Pandit 
782063af595SParav Pandit 	if (unlikely(qp->req.state < QP_STATE_READY)) {
783063af595SParav Pandit 		*bad_wr = wr;
784063af595SParav Pandit 		return -EINVAL;
785063af595SParav Pandit 	}
786063af595SParav Pandit 
787063af595SParav Pandit 	if (qp->is_user) {
788063af595SParav Pandit 		/* Utilize process context to do protocol processing */
789063af595SParav Pandit 		rxe_run_task(&qp->req.task, 0);
790063af595SParav Pandit 		return 0;
791063af595SParav Pandit 	} else
792063af595SParav Pandit 		return rxe_post_send_kernel(qp, wr, bad_wr);
793063af595SParav Pandit }
794063af595SParav Pandit 
795d34ac5cdSBart Van Assche static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
796d34ac5cdSBart Van Assche 			 const struct ib_recv_wr **bad_wr)
7978700e3e7SMoni Shoua {
7988700e3e7SMoni Shoua 	int err = 0;
7998700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
8008700e3e7SMoni Shoua 	struct rxe_rq *rq = &qp->rq;
8018700e3e7SMoni Shoua 	unsigned long flags;
8028700e3e7SMoni Shoua 
8038700e3e7SMoni Shoua 	if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
8048700e3e7SMoni Shoua 		*bad_wr = wr;
8058700e3e7SMoni Shoua 		err = -EINVAL;
8068700e3e7SMoni Shoua 		goto err1;
8078700e3e7SMoni Shoua 	}
8088700e3e7SMoni Shoua 
8098700e3e7SMoni Shoua 	if (unlikely(qp->srq)) {
8108700e3e7SMoni Shoua 		*bad_wr = wr;
8118700e3e7SMoni Shoua 		err = -EINVAL;
8128700e3e7SMoni Shoua 		goto err1;
8138700e3e7SMoni Shoua 	}
8148700e3e7SMoni Shoua 
8158700e3e7SMoni Shoua 	spin_lock_irqsave(&rq->producer_lock, flags);
8168700e3e7SMoni Shoua 
8178700e3e7SMoni Shoua 	while (wr) {
8188700e3e7SMoni Shoua 		err = post_one_recv(rq, wr);
8198700e3e7SMoni Shoua 		if (unlikely(err)) {
8208700e3e7SMoni Shoua 			*bad_wr = wr;
8218700e3e7SMoni Shoua 			break;
8228700e3e7SMoni Shoua 		}
8238700e3e7SMoni Shoua 		wr = wr->next;
8248700e3e7SMoni Shoua 	}
8258700e3e7SMoni Shoua 
8268700e3e7SMoni Shoua 	spin_unlock_irqrestore(&rq->producer_lock, flags);
8278700e3e7SMoni Shoua 
82812171971SVijay Immanuel 	if (qp->resp.state == QP_STATE_ERROR)
82912171971SVijay Immanuel 		rxe_run_task(&qp->resp.task, 1);
83012171971SVijay Immanuel 
8318700e3e7SMoni Shoua err1:
8328700e3e7SMoni Shoua 	return err;
8338700e3e7SMoni Shoua }
8348700e3e7SMoni Shoua 
8358700e3e7SMoni Shoua static struct ib_cq *rxe_create_cq(struct ib_device *dev,
8368700e3e7SMoni Shoua 				   const struct ib_cq_init_attr *attr,
8378700e3e7SMoni Shoua 				   struct ib_ucontext *context,
8388700e3e7SMoni Shoua 				   struct ib_udata *udata)
8398700e3e7SMoni Shoua {
8408700e3e7SMoni Shoua 	int err;
8418700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(dev);
8428700e3e7SMoni Shoua 	struct rxe_cq *cq;
8430c43ab37SJason Gunthorpe 	struct rxe_create_cq_resp __user *uresp = NULL;
8440c43ab37SJason Gunthorpe 
8450c43ab37SJason Gunthorpe 	if (udata) {
8460c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
8470c43ab37SJason Gunthorpe 			return ERR_PTR(-EINVAL);
8480c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
8490c43ab37SJason Gunthorpe 	}
8508700e3e7SMoni Shoua 
8518700e3e7SMoni Shoua 	if (attr->flags)
8528700e3e7SMoni Shoua 		return ERR_PTR(-EINVAL);
8538700e3e7SMoni Shoua 
854b92ec0feSJason Gunthorpe 	err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
8558700e3e7SMoni Shoua 	if (err)
8568700e3e7SMoni Shoua 		goto err1;
8578700e3e7SMoni Shoua 
8588700e3e7SMoni Shoua 	cq = rxe_alloc(&rxe->cq_pool);
8598700e3e7SMoni Shoua 	if (!cq) {
8608700e3e7SMoni Shoua 		err = -ENOMEM;
8618700e3e7SMoni Shoua 		goto err1;
8628700e3e7SMoni Shoua 	}
8638700e3e7SMoni Shoua 
8648700e3e7SMoni Shoua 	err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector,
8650c43ab37SJason Gunthorpe 			       context, uresp);
8668700e3e7SMoni Shoua 	if (err)
8678700e3e7SMoni Shoua 		goto err2;
8688700e3e7SMoni Shoua 
8698700e3e7SMoni Shoua 	return &cq->ibcq;
8708700e3e7SMoni Shoua 
8718700e3e7SMoni Shoua err2:
8728700e3e7SMoni Shoua 	rxe_drop_ref(cq);
8738700e3e7SMoni Shoua err1:
8748700e3e7SMoni Shoua 	return ERR_PTR(err);
8758700e3e7SMoni Shoua }
8768700e3e7SMoni Shoua 
8778700e3e7SMoni Shoua static int rxe_destroy_cq(struct ib_cq *ibcq)
8788700e3e7SMoni Shoua {
8798700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8808700e3e7SMoni Shoua 
881bfc3ae05SAndrew Boyer 	rxe_cq_disable(cq);
882bfc3ae05SAndrew Boyer 
8838700e3e7SMoni Shoua 	rxe_drop_ref(cq);
8848700e3e7SMoni Shoua 	return 0;
8858700e3e7SMoni Shoua }
8868700e3e7SMoni Shoua 
8878700e3e7SMoni Shoua static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
8888700e3e7SMoni Shoua {
8898700e3e7SMoni Shoua 	int err;
8908700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
8918700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibcq->device);
8920c43ab37SJason Gunthorpe 	struct rxe_resize_cq_resp __user *uresp = NULL;
8930c43ab37SJason Gunthorpe 
8940c43ab37SJason Gunthorpe 	if (udata) {
8950c43ab37SJason Gunthorpe 		if (udata->outlen < sizeof(*uresp))
8960c43ab37SJason Gunthorpe 			return -EINVAL;
8970c43ab37SJason Gunthorpe 		uresp = udata->outbuf;
8980c43ab37SJason Gunthorpe 	}
8998700e3e7SMoni Shoua 
900b92ec0feSJason Gunthorpe 	err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
9018700e3e7SMoni Shoua 	if (err)
9028700e3e7SMoni Shoua 		goto err1;
9038700e3e7SMoni Shoua 
9040c43ab37SJason Gunthorpe 	err = rxe_cq_resize_queue(cq, cqe, uresp);
9058700e3e7SMoni Shoua 	if (err)
9068700e3e7SMoni Shoua 		goto err1;
9078700e3e7SMoni Shoua 
9088700e3e7SMoni Shoua 	return 0;
9098700e3e7SMoni Shoua 
9108700e3e7SMoni Shoua err1:
9118700e3e7SMoni Shoua 	return err;
9128700e3e7SMoni Shoua }
9138700e3e7SMoni Shoua 
9148700e3e7SMoni Shoua static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
9158700e3e7SMoni Shoua {
9168700e3e7SMoni Shoua 	int i;
9178700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
9188700e3e7SMoni Shoua 	struct rxe_cqe *cqe;
9198700e3e7SMoni Shoua 	unsigned long flags;
9208700e3e7SMoni Shoua 
9218700e3e7SMoni Shoua 	spin_lock_irqsave(&cq->cq_lock, flags);
9228700e3e7SMoni Shoua 	for (i = 0; i < num_entries; i++) {
9238700e3e7SMoni Shoua 		cqe = queue_head(cq->queue);
9248700e3e7SMoni Shoua 		if (!cqe)
9258700e3e7SMoni Shoua 			break;
9268700e3e7SMoni Shoua 
9278700e3e7SMoni Shoua 		memcpy(wc++, &cqe->ibwc, sizeof(*wc));
9288700e3e7SMoni Shoua 		advance_consumer(cq->queue);
9298700e3e7SMoni Shoua 	}
9308700e3e7SMoni Shoua 	spin_unlock_irqrestore(&cq->cq_lock, flags);
9318700e3e7SMoni Shoua 
9328700e3e7SMoni Shoua 	return i;
9338700e3e7SMoni Shoua }
9348700e3e7SMoni Shoua 
9358700e3e7SMoni Shoua static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
9368700e3e7SMoni Shoua {
9378700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
9388700e3e7SMoni Shoua 	int count = queue_count(cq->queue);
9398700e3e7SMoni Shoua 
9408700e3e7SMoni Shoua 	return (count > wc_cnt) ? wc_cnt : count;
9418700e3e7SMoni Shoua }
9428700e3e7SMoni Shoua 
9438700e3e7SMoni Shoua static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
9448700e3e7SMoni Shoua {
9458700e3e7SMoni Shoua 	struct rxe_cq *cq = to_rcq(ibcq);
946accacb8fSAndrew Boyer 	unsigned long irq_flags;
947accacb8fSAndrew Boyer 	int ret = 0;
9488700e3e7SMoni Shoua 
949accacb8fSAndrew Boyer 	spin_lock_irqsave(&cq->cq_lock, irq_flags);
9508700e3e7SMoni Shoua 	if (cq->notify != IB_CQ_NEXT_COMP)
9518700e3e7SMoni Shoua 		cq->notify = flags & IB_CQ_SOLICITED_MASK;
9528700e3e7SMoni Shoua 
953accacb8fSAndrew Boyer 	if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !queue_empty(cq->queue))
954accacb8fSAndrew Boyer 		ret = 1;
955accacb8fSAndrew Boyer 
956accacb8fSAndrew Boyer 	spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
957accacb8fSAndrew Boyer 
958accacb8fSAndrew Boyer 	return ret;
9598700e3e7SMoni Shoua }
9608700e3e7SMoni Shoua 
9618700e3e7SMoni Shoua static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
9628700e3e7SMoni Shoua {
9638700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
9648700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
9658700e3e7SMoni Shoua 	struct rxe_mem *mr;
9668700e3e7SMoni Shoua 	int err;
9678700e3e7SMoni Shoua 
9688700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
9698700e3e7SMoni Shoua 	if (!mr) {
9708700e3e7SMoni Shoua 		err = -ENOMEM;
9718700e3e7SMoni Shoua 		goto err1;
9728700e3e7SMoni Shoua 	}
9738700e3e7SMoni Shoua 
9748700e3e7SMoni Shoua 	rxe_add_index(mr);
9758700e3e7SMoni Shoua 
9768700e3e7SMoni Shoua 	rxe_add_ref(pd);
9778700e3e7SMoni Shoua 
978e12ee8ceSZhu Yanjun 	err = rxe_mem_init_dma(pd, access, mr);
9798700e3e7SMoni Shoua 	if (err)
9808700e3e7SMoni Shoua 		goto err2;
9818700e3e7SMoni Shoua 
9828700e3e7SMoni Shoua 	return &mr->ibmr;
9838700e3e7SMoni Shoua 
9848700e3e7SMoni Shoua err2:
9858700e3e7SMoni Shoua 	rxe_drop_ref(pd);
9868700e3e7SMoni Shoua 	rxe_drop_index(mr);
9878700e3e7SMoni Shoua 	rxe_drop_ref(mr);
9888700e3e7SMoni Shoua err1:
9898700e3e7SMoni Shoua 	return ERR_PTR(err);
9908700e3e7SMoni Shoua }
9918700e3e7SMoni Shoua 
9928700e3e7SMoni Shoua static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
9938700e3e7SMoni Shoua 				     u64 start,
9948700e3e7SMoni Shoua 				     u64 length,
9958700e3e7SMoni Shoua 				     u64 iova,
9968700e3e7SMoni Shoua 				     int access, struct ib_udata *udata)
9978700e3e7SMoni Shoua {
9988700e3e7SMoni Shoua 	int err;
9998700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
10008700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
10018700e3e7SMoni Shoua 	struct rxe_mem *mr;
10028700e3e7SMoni Shoua 
10038700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
10048700e3e7SMoni Shoua 	if (!mr) {
10058700e3e7SMoni Shoua 		err = -ENOMEM;
10068700e3e7SMoni Shoua 		goto err2;
10078700e3e7SMoni Shoua 	}
10088700e3e7SMoni Shoua 
10098700e3e7SMoni Shoua 	rxe_add_index(mr);
10108700e3e7SMoni Shoua 
10118700e3e7SMoni Shoua 	rxe_add_ref(pd);
10128700e3e7SMoni Shoua 
1013e12ee8ceSZhu Yanjun 	err = rxe_mem_init_user(pd, start, length, iova,
10148700e3e7SMoni Shoua 				access, udata, mr);
10158700e3e7SMoni Shoua 	if (err)
10168700e3e7SMoni Shoua 		goto err3;
10178700e3e7SMoni Shoua 
10188700e3e7SMoni Shoua 	return &mr->ibmr;
10198700e3e7SMoni Shoua 
10208700e3e7SMoni Shoua err3:
10218700e3e7SMoni Shoua 	rxe_drop_ref(pd);
10228700e3e7SMoni Shoua 	rxe_drop_index(mr);
10238700e3e7SMoni Shoua 	rxe_drop_ref(mr);
10248700e3e7SMoni Shoua err2:
10258700e3e7SMoni Shoua 	return ERR_PTR(err);
10268700e3e7SMoni Shoua }
10278700e3e7SMoni Shoua 
10288700e3e7SMoni Shoua static int rxe_dereg_mr(struct ib_mr *ibmr)
10298700e3e7SMoni Shoua {
10308700e3e7SMoni Shoua 	struct rxe_mem *mr = to_rmr(ibmr);
10318700e3e7SMoni Shoua 
10328700e3e7SMoni Shoua 	mr->state = RXE_MEM_STATE_ZOMBIE;
10338700e3e7SMoni Shoua 	rxe_drop_ref(mr->pd);
10348700e3e7SMoni Shoua 	rxe_drop_index(mr);
10358700e3e7SMoni Shoua 	rxe_drop_ref(mr);
10368700e3e7SMoni Shoua 	return 0;
10378700e3e7SMoni Shoua }
10388700e3e7SMoni Shoua 
10398700e3e7SMoni Shoua static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd,
10408700e3e7SMoni Shoua 				  enum ib_mr_type mr_type,
10418700e3e7SMoni Shoua 				  u32 max_num_sg)
10428700e3e7SMoni Shoua {
10438700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibpd->device);
10448700e3e7SMoni Shoua 	struct rxe_pd *pd = to_rpd(ibpd);
10458700e3e7SMoni Shoua 	struct rxe_mem *mr;
10468700e3e7SMoni Shoua 	int err;
10478700e3e7SMoni Shoua 
10488700e3e7SMoni Shoua 	if (mr_type != IB_MR_TYPE_MEM_REG)
10498700e3e7SMoni Shoua 		return ERR_PTR(-EINVAL);
10508700e3e7SMoni Shoua 
10518700e3e7SMoni Shoua 	mr = rxe_alloc(&rxe->mr_pool);
10528700e3e7SMoni Shoua 	if (!mr) {
10538700e3e7SMoni Shoua 		err = -ENOMEM;
10548700e3e7SMoni Shoua 		goto err1;
10558700e3e7SMoni Shoua 	}
10568700e3e7SMoni Shoua 
10578700e3e7SMoni Shoua 	rxe_add_index(mr);
10588700e3e7SMoni Shoua 
10598700e3e7SMoni Shoua 	rxe_add_ref(pd);
10608700e3e7SMoni Shoua 
1061e12ee8ceSZhu Yanjun 	err = rxe_mem_init_fast(pd, max_num_sg, mr);
10628700e3e7SMoni Shoua 	if (err)
10638700e3e7SMoni Shoua 		goto err2;
10648700e3e7SMoni Shoua 
10658700e3e7SMoni Shoua 	return &mr->ibmr;
10668700e3e7SMoni Shoua 
10678700e3e7SMoni Shoua err2:
10688700e3e7SMoni Shoua 	rxe_drop_ref(pd);
10698700e3e7SMoni Shoua 	rxe_drop_index(mr);
10708700e3e7SMoni Shoua 	rxe_drop_ref(mr);
10718700e3e7SMoni Shoua err1:
10728700e3e7SMoni Shoua 	return ERR_PTR(err);
10738700e3e7SMoni Shoua }
10748700e3e7SMoni Shoua 
10758700e3e7SMoni Shoua static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
10768700e3e7SMoni Shoua {
10778700e3e7SMoni Shoua 	struct rxe_mem *mr = to_rmr(ibmr);
10788700e3e7SMoni Shoua 	struct rxe_map *map;
10798700e3e7SMoni Shoua 	struct rxe_phys_buf *buf;
10808700e3e7SMoni Shoua 
10818700e3e7SMoni Shoua 	if (unlikely(mr->nbuf == mr->num_buf))
10828700e3e7SMoni Shoua 		return -ENOMEM;
10838700e3e7SMoni Shoua 
10848700e3e7SMoni Shoua 	map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
10858700e3e7SMoni Shoua 	buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
10868700e3e7SMoni Shoua 
10878700e3e7SMoni Shoua 	buf->addr = addr;
10888700e3e7SMoni Shoua 	buf->size = ibmr->page_size;
10898700e3e7SMoni Shoua 	mr->nbuf++;
10908700e3e7SMoni Shoua 
10918700e3e7SMoni Shoua 	return 0;
10928700e3e7SMoni Shoua }
10938700e3e7SMoni Shoua 
1094e404f945SParav Pandit static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
1095e404f945SParav Pandit 			 int sg_nents, unsigned int *sg_offset)
10968700e3e7SMoni Shoua {
10978700e3e7SMoni Shoua 	struct rxe_mem *mr = to_rmr(ibmr);
10988700e3e7SMoni Shoua 	int n;
10998700e3e7SMoni Shoua 
11008700e3e7SMoni Shoua 	mr->nbuf = 0;
11018700e3e7SMoni Shoua 
11028700e3e7SMoni Shoua 	n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
11038700e3e7SMoni Shoua 
11048700e3e7SMoni Shoua 	mr->va = ibmr->iova;
11058700e3e7SMoni Shoua 	mr->iova = ibmr->iova;
11068700e3e7SMoni Shoua 	mr->length = ibmr->length;
11078700e3e7SMoni Shoua 	mr->page_shift = ilog2(ibmr->page_size);
11088700e3e7SMoni Shoua 	mr->page_mask = ibmr->page_size - 1;
11098700e3e7SMoni Shoua 	mr->offset = mr->iova & mr->page_mask;
11108700e3e7SMoni Shoua 
11118700e3e7SMoni Shoua 	return n;
11128700e3e7SMoni Shoua }
11138700e3e7SMoni Shoua 
11148700e3e7SMoni Shoua static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
11158700e3e7SMoni Shoua {
11168700e3e7SMoni Shoua 	int err;
11178700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
11188700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
11198700e3e7SMoni Shoua 	struct rxe_mc_grp *grp;
11208700e3e7SMoni Shoua 
11218700e3e7SMoni Shoua 	/* takes a ref on grp if successful */
11228700e3e7SMoni Shoua 	err = rxe_mcast_get_grp(rxe, mgid, &grp);
11238700e3e7SMoni Shoua 	if (err)
11248700e3e7SMoni Shoua 		return err;
11258700e3e7SMoni Shoua 
11268700e3e7SMoni Shoua 	err = rxe_mcast_add_grp_elem(rxe, qp, grp);
11278700e3e7SMoni Shoua 
11288700e3e7SMoni Shoua 	rxe_drop_ref(grp);
11298700e3e7SMoni Shoua 	return err;
11308700e3e7SMoni Shoua }
11318700e3e7SMoni Shoua 
11328700e3e7SMoni Shoua static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
11338700e3e7SMoni Shoua {
11348700e3e7SMoni Shoua 	struct rxe_dev *rxe = to_rdev(ibqp->device);
11358700e3e7SMoni Shoua 	struct rxe_qp *qp = to_rqp(ibqp);
11368700e3e7SMoni Shoua 
11378700e3e7SMoni Shoua 	return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
11388700e3e7SMoni Shoua }
11398700e3e7SMoni Shoua 
1140c05d2664SKamal Heib static ssize_t parent_show(struct device *device,
11418700e3e7SMoni Shoua 			   struct device_attribute *attr, char *buf)
11428700e3e7SMoni Shoua {
11438700e3e7SMoni Shoua 	struct rxe_dev *rxe = container_of(device, struct rxe_dev,
11448700e3e7SMoni Shoua 					   ib_dev.dev);
11458700e3e7SMoni Shoua 
1146839f5ac0SBart Van Assche 	return snprintf(buf, 16, "%s\n", rxe_parent_name(rxe, 1));
11478700e3e7SMoni Shoua }
11488700e3e7SMoni Shoua 
1149c05d2664SKamal Heib static DEVICE_ATTR_RO(parent);
11508700e3e7SMoni Shoua 
1151508a523fSParav Pandit static struct attribute *rxe_dev_attributes[] = {
1152508a523fSParav Pandit 	&dev_attr_parent.attr,
1153508a523fSParav Pandit 	NULL
1154508a523fSParav Pandit };
1155508a523fSParav Pandit 
1156508a523fSParav Pandit static const struct attribute_group rxe_attr_group = {
1157508a523fSParav Pandit 	.attrs = rxe_dev_attributes,
11588700e3e7SMoni Shoua };
11598700e3e7SMoni Shoua 
11608700e3e7SMoni Shoua int rxe_register_device(struct rxe_dev *rxe)
11618700e3e7SMoni Shoua {
11628700e3e7SMoni Shoua 	int err;
11638700e3e7SMoni Shoua 	struct ib_device *dev = &rxe->ib_dev;
11643192c53eSThomas Bogendoerfer 	struct crypto_shash *tfm;
11658700e3e7SMoni Shoua 
11668700e3e7SMoni Shoua 	strlcpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
11678700e3e7SMoni Shoua 
11688700e3e7SMoni Shoua 	dev->owner = THIS_MODULE;
11698700e3e7SMoni Shoua 	dev->node_type = RDMA_NODE_IB_CA;
11708700e3e7SMoni Shoua 	dev->phys_port_cnt = 1;
117167cf3623SSagi Grimberg 	dev->num_comp_vectors = num_possible_cpus();
117285e9f1dbSBart Van Assche 	dev->dev.parent = rxe_dma_device(rxe);
11738700e3e7SMoni Shoua 	dev->local_dma_lkey = 0;
11744d6f2859SYuval Shaia 	addrconf_addr_eui48((unsigned char *)&dev->node_guid,
11754d6f2859SYuval Shaia 			    rxe->ndev->dev_addr);
11760bbb3b74SBart Van Assche 	dev->dev.dma_ops = &dma_virt_ops;
117756012e1cSyonatanc 	dma_coerce_mask_and_coherent(&dev->dev,
1178efc365e7SMikhail Malygin 				     dma_get_required_mask(&dev->dev));
11798700e3e7SMoni Shoua 
11808700e3e7SMoni Shoua 	dev->uverbs_abi_ver = RXE_UVERBS_ABI_VERSION;
11818700e3e7SMoni Shoua 	dev->uverbs_cmd_mask = BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT)
11828700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
11838700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE)
11848700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT)
11858700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD)
11868700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD)
11878700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ)
11888700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ)
11898700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ)
11908700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ)
11918700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POST_SRQ_RECV)
11928700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP)
11938700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP)
11948700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP)
11958700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP)
11968700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POST_SEND)
11978700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POST_RECV)
11988700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ)
11998700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ)
12008700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ)
12018700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ)
12028700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_PEEK_CQ)
12038700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)
12048700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_REG_MR)
12058700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR)
12068700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH)
12078700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_AH)
12088700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_QUERY_AH)
12098700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH)
12108700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST)
12118700e3e7SMoni Shoua 	    | BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST)
12128700e3e7SMoni Shoua 	    ;
12138700e3e7SMoni Shoua 
12148700e3e7SMoni Shoua 	dev->query_device = rxe_query_device;
12158700e3e7SMoni Shoua 	dev->modify_device = rxe_modify_device;
12168700e3e7SMoni Shoua 	dev->query_port = rxe_query_port;
12178700e3e7SMoni Shoua 	dev->modify_port = rxe_modify_port;
12188700e3e7SMoni Shoua 	dev->get_link_layer = rxe_get_link_layer;
12198700e3e7SMoni Shoua 	dev->get_netdev = rxe_get_netdev;
12208700e3e7SMoni Shoua 	dev->query_pkey = rxe_query_pkey;
12218700e3e7SMoni Shoua 	dev->alloc_ucontext = rxe_alloc_ucontext;
12228700e3e7SMoni Shoua 	dev->dealloc_ucontext = rxe_dealloc_ucontext;
12238700e3e7SMoni Shoua 	dev->mmap = rxe_mmap;
12248700e3e7SMoni Shoua 	dev->get_port_immutable = rxe_port_immutable;
12258700e3e7SMoni Shoua 	dev->alloc_pd = rxe_alloc_pd;
12268700e3e7SMoni Shoua 	dev->dealloc_pd = rxe_dealloc_pd;
12278700e3e7SMoni Shoua 	dev->create_ah = rxe_create_ah;
12288700e3e7SMoni Shoua 	dev->modify_ah = rxe_modify_ah;
12298700e3e7SMoni Shoua 	dev->query_ah = rxe_query_ah;
12308700e3e7SMoni Shoua 	dev->destroy_ah = rxe_destroy_ah;
12318700e3e7SMoni Shoua 	dev->create_srq = rxe_create_srq;
12328700e3e7SMoni Shoua 	dev->modify_srq = rxe_modify_srq;
12338700e3e7SMoni Shoua 	dev->query_srq = rxe_query_srq;
12348700e3e7SMoni Shoua 	dev->destroy_srq = rxe_destroy_srq;
12358700e3e7SMoni Shoua 	dev->post_srq_recv = rxe_post_srq_recv;
12368700e3e7SMoni Shoua 	dev->create_qp = rxe_create_qp;
12378700e3e7SMoni Shoua 	dev->modify_qp = rxe_modify_qp;
12388700e3e7SMoni Shoua 	dev->query_qp = rxe_query_qp;
12398700e3e7SMoni Shoua 	dev->destroy_qp = rxe_destroy_qp;
12408700e3e7SMoni Shoua 	dev->post_send = rxe_post_send;
12418700e3e7SMoni Shoua 	dev->post_recv = rxe_post_recv;
12428700e3e7SMoni Shoua 	dev->create_cq = rxe_create_cq;
12438700e3e7SMoni Shoua 	dev->destroy_cq = rxe_destroy_cq;
12448700e3e7SMoni Shoua 	dev->resize_cq = rxe_resize_cq;
12458700e3e7SMoni Shoua 	dev->poll_cq = rxe_poll_cq;
12468700e3e7SMoni Shoua 	dev->peek_cq = rxe_peek_cq;
12478700e3e7SMoni Shoua 	dev->req_notify_cq = rxe_req_notify_cq;
12488700e3e7SMoni Shoua 	dev->get_dma_mr = rxe_get_dma_mr;
12498700e3e7SMoni Shoua 	dev->reg_user_mr = rxe_reg_user_mr;
12508700e3e7SMoni Shoua 	dev->dereg_mr = rxe_dereg_mr;
12518700e3e7SMoni Shoua 	dev->alloc_mr = rxe_alloc_mr;
12528700e3e7SMoni Shoua 	dev->map_mr_sg = rxe_map_mr_sg;
12538700e3e7SMoni Shoua 	dev->attach_mcast = rxe_attach_mcast;
12548700e3e7SMoni Shoua 	dev->detach_mcast = rxe_detach_mcast;
12550b1e5b99SYonatan Cohen 	dev->get_hw_stats = rxe_ib_get_hw_stats;
12560b1e5b99SYonatan Cohen 	dev->alloc_hw_stats = rxe_ib_alloc_hw_stats;
12578700e3e7SMoni Shoua 
12583192c53eSThomas Bogendoerfer 	tfm = crypto_alloc_shash("crc32", 0, 0);
12593192c53eSThomas Bogendoerfer 	if (IS_ERR(tfm)) {
126027b0b832SColin Ian King 		pr_err("failed to allocate crc algorithm err:%ld\n",
12613192c53eSThomas Bogendoerfer 		       PTR_ERR(tfm));
12623192c53eSThomas Bogendoerfer 		return PTR_ERR(tfm);
1263cee2688eSyonatanc 	}
12643192c53eSThomas Bogendoerfer 	rxe->tfm = tfm;
1265cee2688eSyonatanc 
1266508a523fSParav Pandit 	rdma_set_device_sysfs_group(dev, &rxe_attr_group);
12670ede73bcSMatan Barak 	dev->driver_id = RDMA_DRIVER_RXE;
1268e349f858SJason Gunthorpe 	err = ib_register_device(dev, "rxe%d", NULL);
12698700e3e7SMoni Shoua 	if (err) {
127061013828SKamal Heib 		pr_warn("%s failed with error %d\n", __func__, err);
12718700e3e7SMoni Shoua 		goto err1;
12728700e3e7SMoni Shoua 	}
12738700e3e7SMoni Shoua 
12748700e3e7SMoni Shoua 	return 0;
12758700e3e7SMoni Shoua 
12768700e3e7SMoni Shoua err1:
1277cee2688eSyonatanc 	crypto_free_shash(rxe->tfm);
1278cee2688eSyonatanc 
12798700e3e7SMoni Shoua 	return err;
12808700e3e7SMoni Shoua }
12818700e3e7SMoni Shoua 
12828700e3e7SMoni Shoua int rxe_unregister_device(struct rxe_dev *rxe)
12838700e3e7SMoni Shoua {
12848700e3e7SMoni Shoua 	struct ib_device *dev = &rxe->ib_dev;
12858700e3e7SMoni Shoua 
12868700e3e7SMoni Shoua 	ib_unregister_device(dev);
12878700e3e7SMoni Shoua 
12888700e3e7SMoni Shoua 	return 0;
12898700e3e7SMoni Shoua }
1290