163fa15dbSBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
28700e3e7SMoni Shoua /*
38700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
48700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
58700e3e7SMoni Shoua  */
68700e3e7SMoni Shoua 
7aae0484eSZhu Yanjun #include <linux/vmalloc.h>
88700e3e7SMoni Shoua #include "rxe.h"
98700e3e7SMoni Shoua #include "rxe_queue.h"
108700e3e7SMoni Shoua 
rxe_srq_chk_init(struct rxe_dev * rxe,struct ib_srq_init_attr * init)110b1fbfb9SBob Pearson int rxe_srq_chk_init(struct rxe_dev *rxe, struct ib_srq_init_attr *init)
128700e3e7SMoni Shoua {
130b1fbfb9SBob Pearson 	struct ib_srq_attr *attr = &init->attr;
148700e3e7SMoni Shoua 
158700e3e7SMoni Shoua 	if (attr->max_wr > rxe->attr.max_srq_wr) {
16a9fb3287SBob Pearson 		rxe_dbg_dev(rxe, "max_wr(%d) > max_srq_wr(%d)\n",
178700e3e7SMoni Shoua 			attr->max_wr, rxe->attr.max_srq_wr);
188700e3e7SMoni Shoua 		goto err1;
198700e3e7SMoni Shoua 	}
208700e3e7SMoni Shoua 
218700e3e7SMoni Shoua 	if (attr->max_wr <= 0) {
22a9fb3287SBob Pearson 		rxe_dbg_dev(rxe, "max_wr(%d) <= 0\n", attr->max_wr);
238700e3e7SMoni Shoua 		goto err1;
248700e3e7SMoni Shoua 	}
258700e3e7SMoni Shoua 
268700e3e7SMoni Shoua 	if (attr->max_wr < RXE_MIN_SRQ_WR)
278700e3e7SMoni Shoua 		attr->max_wr = RXE_MIN_SRQ_WR;
288700e3e7SMoni Shoua 
298700e3e7SMoni Shoua 	if (attr->max_sge > rxe->attr.max_srq_sge) {
30a9fb3287SBob Pearson 		rxe_dbg_dev(rxe, "max_sge(%d) > max_srq_sge(%d)\n",
318700e3e7SMoni Shoua 			attr->max_sge, rxe->attr.max_srq_sge);
328700e3e7SMoni Shoua 		goto err1;
338700e3e7SMoni Shoua 	}
348700e3e7SMoni Shoua 
358700e3e7SMoni Shoua 	if (attr->max_sge < RXE_MIN_SRQ_SGE)
368700e3e7SMoni Shoua 		attr->max_sge = RXE_MIN_SRQ_SGE;
378700e3e7SMoni Shoua 
388700e3e7SMoni Shoua 	return 0;
398700e3e7SMoni Shoua 
408700e3e7SMoni Shoua err1:
418700e3e7SMoni Shoua 	return -EINVAL;
428700e3e7SMoni Shoua }
438700e3e7SMoni Shoua 
rxe_srq_from_init(struct rxe_dev * rxe,struct rxe_srq * srq,struct ib_srq_init_attr * init,struct ib_udata * udata,struct rxe_create_srq_resp __user * uresp)448700e3e7SMoni Shoua int rxe_srq_from_init(struct rxe_dev *rxe, struct rxe_srq *srq,
45ff23dfa1SShamir Rabinovitch 		      struct ib_srq_init_attr *init, struct ib_udata *udata,
460c43ab37SJason Gunthorpe 		      struct rxe_create_srq_resp __user *uresp)
478700e3e7SMoni Shoua {
488700e3e7SMoni Shoua 	struct rxe_queue *q;
49*cc28f351SBob Pearson 	int wqe_size;
50*cc28f351SBob Pearson 	int err;
518700e3e7SMoni Shoua 
528700e3e7SMoni Shoua 	srq->ibsrq.event_handler = init->event_handler;
538700e3e7SMoni Shoua 	srq->ibsrq.srq_context = init->srq_context;
548700e3e7SMoni Shoua 	srq->limit = init->attr.srq_limit;
5502827b67SBob Pearson 	srq->srq_num = srq->elem.index;
568700e3e7SMoni Shoua 	srq->rq.max_wr = init->attr.max_wr;
578700e3e7SMoni Shoua 	srq->rq.max_sge = init->attr.max_sge;
588700e3e7SMoni Shoua 
59*cc28f351SBob Pearson 	wqe_size = sizeof(struct rxe_recv_wqe) +
60*cc28f351SBob Pearson 			srq->rq.max_sge*sizeof(struct ib_sge);
618700e3e7SMoni Shoua 
628700e3e7SMoni Shoua 	spin_lock_init(&srq->rq.producer_lock);
638700e3e7SMoni Shoua 	spin_lock_init(&srq->rq.consumer_lock);
648700e3e7SMoni Shoua 
65*cc28f351SBob Pearson 	q = rxe_queue_init(rxe, &srq->rq.max_wr, wqe_size,
66*cc28f351SBob Pearson 			   QUEUE_TYPE_FROM_CLIENT);
678700e3e7SMoni Shoua 	if (!q) {
680e609002SBob Pearson 		rxe_dbg_srq(srq, "Unable to allocate queue\n");
69*cc28f351SBob Pearson 		err = -ENOMEM;
70*cc28f351SBob Pearson 		goto err_out;
718700e3e7SMoni Shoua 	}
728700e3e7SMoni Shoua 
73ff23dfa1SShamir Rabinovitch 	err = do_mmap_info(rxe, uresp ? &uresp->mi : NULL, udata, q->buf,
748700e3e7SMoni Shoua 			   q->buf_size, &q->ip);
75aae0484eSZhu Yanjun 	if (err) {
76*cc28f351SBob Pearson 		rxe_dbg_srq(srq, "Unable to init mmap info for caller\n");
77*cc28f351SBob Pearson 		goto err_free;
78aae0484eSZhu Yanjun 	}
798700e3e7SMoni Shoua 
80*cc28f351SBob Pearson 	srq->rq.queue = q;
81*cc28f351SBob Pearson 	init->attr.max_wr = srq->rq.max_wr;
82*cc28f351SBob Pearson 
830c43ab37SJason Gunthorpe 	if (uresp) {
840c43ab37SJason Gunthorpe 		if (copy_to_user(&uresp->srq_num, &srq->srq_num,
85aae0484eSZhu Yanjun 				 sizeof(uresp->srq_num))) {
86aae0484eSZhu Yanjun 			rxe_queue_cleanup(q);
878700e3e7SMoni Shoua 			return -EFAULT;
888700e3e7SMoni Shoua 		}
89aae0484eSZhu Yanjun 	}
900c43ab37SJason Gunthorpe 
918700e3e7SMoni Shoua 	return 0;
92*cc28f351SBob Pearson 
93*cc28f351SBob Pearson err_free:
94*cc28f351SBob Pearson 	vfree(q->buf);
95*cc28f351SBob Pearson 	kfree(q);
96*cc28f351SBob Pearson err_out:
97*cc28f351SBob Pearson 	return err;
988700e3e7SMoni Shoua }
998700e3e7SMoni Shoua 
rxe_srq_chk_attr(struct rxe_dev * rxe,struct rxe_srq * srq,struct ib_srq_attr * attr,enum ib_srq_attr_mask mask)1000b1fbfb9SBob Pearson int rxe_srq_chk_attr(struct rxe_dev *rxe, struct rxe_srq *srq,
1010b1fbfb9SBob Pearson 		     struct ib_srq_attr *attr, enum ib_srq_attr_mask mask)
1020b1fbfb9SBob Pearson {
1030b1fbfb9SBob Pearson 	if (srq->error) {
1040e609002SBob Pearson 		rxe_dbg_srq(srq, "in error state\n");
1050b1fbfb9SBob Pearson 		goto err1;
1060b1fbfb9SBob Pearson 	}
1070b1fbfb9SBob Pearson 
1080b1fbfb9SBob Pearson 	if (mask & IB_SRQ_MAX_WR) {
1090b1fbfb9SBob Pearson 		if (attr->max_wr > rxe->attr.max_srq_wr) {
1100e609002SBob Pearson 			rxe_dbg_srq(srq, "max_wr(%d) > max_srq_wr(%d)\n",
1110b1fbfb9SBob Pearson 				attr->max_wr, rxe->attr.max_srq_wr);
1120b1fbfb9SBob Pearson 			goto err1;
1130b1fbfb9SBob Pearson 		}
1140b1fbfb9SBob Pearson 
1150b1fbfb9SBob Pearson 		if (attr->max_wr <= 0) {
1160e609002SBob Pearson 			rxe_dbg_srq(srq, "max_wr(%d) <= 0\n", attr->max_wr);
1170b1fbfb9SBob Pearson 			goto err1;
1180b1fbfb9SBob Pearson 		}
1190b1fbfb9SBob Pearson 
1200b1fbfb9SBob Pearson 		if (srq->limit && (attr->max_wr < srq->limit)) {
1210e609002SBob Pearson 			rxe_dbg_srq(srq, "max_wr (%d) < srq->limit (%d)\n",
1220b1fbfb9SBob Pearson 				attr->max_wr, srq->limit);
1230b1fbfb9SBob Pearson 			goto err1;
1240b1fbfb9SBob Pearson 		}
1250b1fbfb9SBob Pearson 
1260b1fbfb9SBob Pearson 		if (attr->max_wr < RXE_MIN_SRQ_WR)
1270b1fbfb9SBob Pearson 			attr->max_wr = RXE_MIN_SRQ_WR;
1280b1fbfb9SBob Pearson 	}
1290b1fbfb9SBob Pearson 
1300b1fbfb9SBob Pearson 	if (mask & IB_SRQ_LIMIT) {
1310b1fbfb9SBob Pearson 		if (attr->srq_limit > rxe->attr.max_srq_wr) {
1320e609002SBob Pearson 			rxe_dbg_srq(srq, "srq_limit(%d) > max_srq_wr(%d)\n",
1330b1fbfb9SBob Pearson 				attr->srq_limit, rxe->attr.max_srq_wr);
1340b1fbfb9SBob Pearson 			goto err1;
1350b1fbfb9SBob Pearson 		}
1360b1fbfb9SBob Pearson 
1370b1fbfb9SBob Pearson 		if (attr->srq_limit > srq->rq.queue->buf->index_mask) {
1380e609002SBob Pearson 			rxe_dbg_srq(srq, "srq_limit (%d) > cur limit(%d)\n",
1390b1fbfb9SBob Pearson 				attr->srq_limit,
1400b1fbfb9SBob Pearson 				srq->rq.queue->buf->index_mask);
1410b1fbfb9SBob Pearson 			goto err1;
1420b1fbfb9SBob Pearson 		}
1430b1fbfb9SBob Pearson 	}
1440b1fbfb9SBob Pearson 
1450b1fbfb9SBob Pearson 	return 0;
1460b1fbfb9SBob Pearson 
1470b1fbfb9SBob Pearson err1:
1480b1fbfb9SBob Pearson 	return -EINVAL;
1490b1fbfb9SBob Pearson }
1500b1fbfb9SBob Pearson 
rxe_srq_from_attr(struct rxe_dev * rxe,struct rxe_srq * srq,struct ib_srq_attr * attr,enum ib_srq_attr_mask mask,struct rxe_modify_srq_cmd * ucmd,struct ib_udata * udata)1518700e3e7SMoni Shoua int rxe_srq_from_attr(struct rxe_dev *rxe, struct rxe_srq *srq,
1528700e3e7SMoni Shoua 		      struct ib_srq_attr *attr, enum ib_srq_attr_mask mask,
153ff23dfa1SShamir Rabinovitch 		      struct rxe_modify_srq_cmd *ucmd, struct ib_udata *udata)
1548700e3e7SMoni Shoua {
1558700e3e7SMoni Shoua 	struct rxe_queue *q = srq->rq.queue;
1560c43ab37SJason Gunthorpe 	struct mminfo __user *mi = NULL;
157*cc28f351SBob Pearson 	int wqe_size;
158*cc28f351SBob Pearson 	int err;
1598700e3e7SMoni Shoua 
1608700e3e7SMoni Shoua 	if (mask & IB_SRQ_MAX_WR) {
1610c43ab37SJason Gunthorpe 		/*
1620c43ab37SJason Gunthorpe 		 * This is completely screwed up, the response is supposed to
1630c43ab37SJason Gunthorpe 		 * be in the outbuf not like this.
1640c43ab37SJason Gunthorpe 		 */
1650c43ab37SJason Gunthorpe 		mi = u64_to_user_ptr(ucmd->mmap_info_addr);
1668700e3e7SMoni Shoua 
167*cc28f351SBob Pearson 		wqe_size = sizeof(struct rxe_recv_wqe) +
168*cc28f351SBob Pearson 				srq->rq.max_sge*sizeof(struct ib_sge);
169*cc28f351SBob Pearson 
170*cc28f351SBob Pearson 		err = rxe_queue_resize(q, &attr->max_wr, wqe_size,
171*cc28f351SBob Pearson 				       udata, mi, &srq->rq.producer_lock,
1728700e3e7SMoni Shoua 				       &srq->rq.consumer_lock);
1738700e3e7SMoni Shoua 		if (err)
174*cc28f351SBob Pearson 			goto err_free;
175*cc28f351SBob Pearson 
176*cc28f351SBob Pearson 		srq->rq.max_wr = attr->max_wr;
1778700e3e7SMoni Shoua 	}
1788700e3e7SMoni Shoua 
1798700e3e7SMoni Shoua 	if (mask & IB_SRQ_LIMIT)
1808700e3e7SMoni Shoua 		srq->limit = attr->srq_limit;
1818700e3e7SMoni Shoua 
1828700e3e7SMoni Shoua 	return 0;
1838700e3e7SMoni Shoua 
184*cc28f351SBob Pearson err_free:
1858700e3e7SMoni Shoua 	rxe_queue_cleanup(q);
1868700e3e7SMoni Shoua 	srq->rq.queue = NULL;
1878700e3e7SMoni Shoua 	return err;
1888700e3e7SMoni Shoua }
189b2a41678SBob Pearson 
rxe_srq_cleanup(struct rxe_pool_elem * elem)190b2a41678SBob Pearson void rxe_srq_cleanup(struct rxe_pool_elem *elem)
191b2a41678SBob Pearson {
192b2a41678SBob Pearson 	struct rxe_srq *srq = container_of(elem, typeof(*srq), elem);
193b2a41678SBob Pearson 
194b2a41678SBob Pearson 	if (srq->pd)
195b2a41678SBob Pearson 		rxe_put(srq->pd);
196b2a41678SBob Pearson 
197b2a41678SBob Pearson 	if (srq->rq.queue)
198b2a41678SBob Pearson 		rxe_queue_cleanup(srq->rq.queue);
199b2a41678SBob Pearson }
200