1 /*
2  * Copyright (c) 2016-2017 VMware, Inc.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of EITHER the GNU General Public License
6  * version 2 as published by the Free Software Foundation or the BSD
7  * 2-Clause License. This program is distributed in the hope that it
8  * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
9  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License version 2 for more details at
11  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program available in the file COPYING in the main
15  * directory of this source tree.
16  *
17  * The BSD 2-Clause License
18  *
19  *     Redistribution and use in source and binary forms, with or
20  *     without modification, are permitted provided that the following
21  *     conditions are met:
22  *
23  *      - Redistributions of source code must retain the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer.
26  *
27  *      - Redistributions in binary form must reproduce the above
28  *        copyright notice, this list of conditions and the following
29  *        disclaimer in the documentation and/or other materials
30  *        provided with the distribution.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  * OF THE POSSIBILITY OF SUCH DAMAGE.
44  */
45 
46 #include <asm/page.h>
47 #include <linux/io.h>
48 #include <linux/wait.h>
49 #include <rdma/ib_addr.h>
50 #include <rdma/ib_smi.h>
51 #include <rdma/ib_user_verbs.h>
52 
53 #include "pvrdma.h"
54 
55 /**
56  * pvrdma_query_srq - query shared receive queue
57  * @ibsrq: the shared receive queue to query
58  * @srq_attr: attributes to query and return to client
59  *
60  * @return: 0 for success, otherwise returns an errno.
61  */
62 int pvrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
63 {
64 	struct pvrdma_dev *dev = to_vdev(ibsrq->device);
65 	struct pvrdma_srq *srq = to_vsrq(ibsrq);
66 	union pvrdma_cmd_req req;
67 	union pvrdma_cmd_resp rsp;
68 	struct pvrdma_cmd_query_srq *cmd = &req.query_srq;
69 	struct pvrdma_cmd_query_srq_resp *resp = &rsp.query_srq_resp;
70 	int ret;
71 
72 	memset(cmd, 0, sizeof(*cmd));
73 	cmd->hdr.cmd = PVRDMA_CMD_QUERY_SRQ;
74 	cmd->srq_handle = srq->srq_handle;
75 
76 	ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_SRQ_RESP);
77 	if (ret < 0) {
78 		dev_warn(&dev->pdev->dev,
79 			 "could not query shared receive queue, error: %d\n",
80 			 ret);
81 		return -EINVAL;
82 	}
83 
84 	srq_attr->srq_limit = resp->attrs.srq_limit;
85 	srq_attr->max_wr = resp->attrs.max_wr;
86 	srq_attr->max_sge = resp->attrs.max_sge;
87 
88 	return 0;
89 }
90 
91 /**
92  * pvrdma_create_srq - create shared receive queue
93  * @pd: protection domain
94  * @init_attr: shared receive queue attributes
95  * @udata: user data
96  *
97  * @return: the ib_srq pointer on success, otherwise returns an errno.
98  */
99 struct ib_srq *pvrdma_create_srq(struct ib_pd *pd,
100 				 struct ib_srq_init_attr *init_attr,
101 				 struct ib_udata *udata)
102 {
103 	struct pvrdma_srq *srq = NULL;
104 	struct pvrdma_dev *dev = to_vdev(pd->device);
105 	union pvrdma_cmd_req req;
106 	union pvrdma_cmd_resp rsp;
107 	struct pvrdma_cmd_create_srq *cmd = &req.create_srq;
108 	struct pvrdma_cmd_create_srq_resp *resp = &rsp.create_srq_resp;
109 	struct pvrdma_create_srq_resp srq_resp = {0};
110 	struct pvrdma_create_srq ucmd;
111 	unsigned long flags;
112 	int ret;
113 
114 	if (!(pd->uobject && udata)) {
115 		/* No support for kernel clients. */
116 		dev_warn(&dev->pdev->dev,
117 			 "no shared receive queue support for kernel client\n");
118 		return ERR_PTR(-EOPNOTSUPP);
119 	}
120 
121 	if (init_attr->srq_type != IB_SRQT_BASIC) {
122 		dev_warn(&dev->pdev->dev,
123 			 "shared receive queue type %d not supported\n",
124 			 init_attr->srq_type);
125 		return ERR_PTR(-EINVAL);
126 	}
127 
128 	if (init_attr->attr.max_wr  > dev->dsr->caps.max_srq_wr ||
129 	    init_attr->attr.max_sge > dev->dsr->caps.max_srq_sge) {
130 		dev_warn(&dev->pdev->dev,
131 			 "shared receive queue size invalid\n");
132 		return ERR_PTR(-EINVAL);
133 	}
134 
135 	if (!atomic_add_unless(&dev->num_srqs, 1, dev->dsr->caps.max_srq))
136 		return ERR_PTR(-ENOMEM);
137 
138 	srq = kmalloc(sizeof(*srq), GFP_KERNEL);
139 	if (!srq) {
140 		ret = -ENOMEM;
141 		goto err_srq;
142 	}
143 
144 	spin_lock_init(&srq->lock);
145 	refcount_set(&srq->refcnt, 1);
146 	init_completion(&srq->free);
147 
148 	dev_dbg(&dev->pdev->dev,
149 		"create shared receive queue from user space\n");
150 
151 	if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
152 		ret = -EFAULT;
153 		goto err_srq;
154 	}
155 
156 	srq->umem = ib_umem_get(pd->uobject->context,
157 				ucmd.buf_addr,
158 				ucmd.buf_size, 0, 0);
159 	if (IS_ERR(srq->umem)) {
160 		ret = PTR_ERR(srq->umem);
161 		goto err_srq;
162 	}
163 
164 	srq->npages = ib_umem_page_count(srq->umem);
165 
166 	if (srq->npages < 0 || srq->npages > PVRDMA_PAGE_DIR_MAX_PAGES) {
167 		dev_warn(&dev->pdev->dev,
168 			 "overflow pages in shared receive queue\n");
169 		ret = -EINVAL;
170 		goto err_umem;
171 	}
172 
173 	ret = pvrdma_page_dir_init(dev, &srq->pdir, srq->npages, false);
174 	if (ret) {
175 		dev_warn(&dev->pdev->dev,
176 			 "could not allocate page directory\n");
177 		goto err_umem;
178 	}
179 
180 	pvrdma_page_dir_insert_umem(&srq->pdir, srq->umem, 0);
181 
182 	memset(cmd, 0, sizeof(*cmd));
183 	cmd->hdr.cmd = PVRDMA_CMD_CREATE_SRQ;
184 	cmd->srq_type = init_attr->srq_type;
185 	cmd->nchunks = srq->npages;
186 	cmd->pd_handle = to_vpd(pd)->pd_handle;
187 	cmd->attrs.max_wr = init_attr->attr.max_wr;
188 	cmd->attrs.max_sge = init_attr->attr.max_sge;
189 	cmd->attrs.srq_limit = init_attr->attr.srq_limit;
190 	cmd->pdir_dma = srq->pdir.dir_dma;
191 
192 	ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_SRQ_RESP);
193 	if (ret < 0) {
194 		dev_warn(&dev->pdev->dev,
195 			 "could not create shared receive queue, error: %d\n",
196 			 ret);
197 		goto err_page_dir;
198 	}
199 
200 	srq->srq_handle = resp->srqn;
201 	srq_resp.srqn = resp->srqn;
202 	spin_lock_irqsave(&dev->srq_tbl_lock, flags);
203 	dev->srq_tbl[srq->srq_handle % dev->dsr->caps.max_srq] = srq;
204 	spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
205 
206 	/* Copy udata back. */
207 	if (ib_copy_to_udata(udata, &srq_resp, sizeof(srq_resp))) {
208 		dev_warn(&dev->pdev->dev, "failed to copy back udata\n");
209 		pvrdma_destroy_srq(&srq->ibsrq);
210 		return ERR_PTR(-EINVAL);
211 	}
212 
213 	return &srq->ibsrq;
214 
215 err_page_dir:
216 	pvrdma_page_dir_cleanup(dev, &srq->pdir);
217 err_umem:
218 	ib_umem_release(srq->umem);
219 err_srq:
220 	kfree(srq);
221 	atomic_dec(&dev->num_srqs);
222 
223 	return ERR_PTR(ret);
224 }
225 
226 static void pvrdma_free_srq(struct pvrdma_dev *dev, struct pvrdma_srq *srq)
227 {
228 	unsigned long flags;
229 
230 	spin_lock_irqsave(&dev->srq_tbl_lock, flags);
231 	dev->srq_tbl[srq->srq_handle] = NULL;
232 	spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
233 
234 	if (refcount_dec_and_test(&srq->refcnt))
235 		complete(&srq->free);
236 	wait_for_completion(&srq->free);
237 
238 	/* There is no support for kernel clients, so this is safe. */
239 	ib_umem_release(srq->umem);
240 
241 	pvrdma_page_dir_cleanup(dev, &srq->pdir);
242 
243 	kfree(srq);
244 
245 	atomic_dec(&dev->num_srqs);
246 }
247 
248 /**
249  * pvrdma_destroy_srq - destroy shared receive queue
250  * @srq: the shared receive queue to destroy
251  *
252  * @return: 0 for success.
253  */
254 int pvrdma_destroy_srq(struct ib_srq *srq)
255 {
256 	struct pvrdma_srq *vsrq = to_vsrq(srq);
257 	union pvrdma_cmd_req req;
258 	struct pvrdma_cmd_destroy_srq *cmd = &req.destroy_srq;
259 	struct pvrdma_dev *dev = to_vdev(srq->device);
260 	int ret;
261 
262 	memset(cmd, 0, sizeof(*cmd));
263 	cmd->hdr.cmd = PVRDMA_CMD_DESTROY_SRQ;
264 	cmd->srq_handle = vsrq->srq_handle;
265 
266 	ret = pvrdma_cmd_post(dev, &req, NULL, 0);
267 	if (ret < 0)
268 		dev_warn(&dev->pdev->dev,
269 			 "destroy shared receive queue failed, error: %d\n",
270 			 ret);
271 
272 	pvrdma_free_srq(dev, vsrq);
273 
274 	return 0;
275 }
276 
277 /**
278  * pvrdma_modify_srq - modify shared receive queue attributes
279  * @ibsrq: the shared receive queue to modify
280  * @attr: the shared receive queue's new attributes
281  * @attr_mask: attributes mask
282  * @udata: user data
283  *
284  * @returns 0 on success, otherwise returns an errno.
285  */
286 int pvrdma_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
287 		      enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
288 {
289 	struct pvrdma_srq *vsrq = to_vsrq(ibsrq);
290 	union pvrdma_cmd_req req;
291 	struct pvrdma_cmd_modify_srq *cmd = &req.modify_srq;
292 	struct pvrdma_dev *dev = to_vdev(ibsrq->device);
293 	int ret;
294 
295 	/* Only support SRQ limit. */
296 	if (!(attr_mask & IB_SRQ_LIMIT))
297 		return -EINVAL;
298 
299 	memset(cmd, 0, sizeof(*cmd));
300 	cmd->hdr.cmd = PVRDMA_CMD_MODIFY_SRQ;
301 	cmd->srq_handle = vsrq->srq_handle;
302 	cmd->attrs.srq_limit = attr->srq_limit;
303 	cmd->attr_mask = attr_mask;
304 
305 	ret = pvrdma_cmd_post(dev, &req, NULL, 0);
306 	if (ret < 0) {
307 		dev_warn(&dev->pdev->dev,
308 			 "could not modify shared receive queue, error: %d\n",
309 			 ret);
310 
311 		return -EINVAL;
312 	}
313 
314 	return ret;
315 }
316