1 /*
2  * Copyright (c) 2012-2016 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 static inline void get_cqs(struct pvrdma_qp *qp, struct pvrdma_cq **send_cq,
56 			   struct pvrdma_cq **recv_cq)
57 {
58 	*send_cq = to_vcq(qp->ibqp.send_cq);
59 	*recv_cq = to_vcq(qp->ibqp.recv_cq);
60 }
61 
62 static void pvrdma_lock_cqs(struct pvrdma_cq *scq, struct pvrdma_cq *rcq,
63 			    unsigned long *scq_flags,
64 			    unsigned long *rcq_flags)
65 	__acquires(scq->cq_lock) __acquires(rcq->cq_lock)
66 {
67 	if (scq == rcq) {
68 		spin_lock_irqsave(&scq->cq_lock, *scq_flags);
69 		__acquire(rcq->cq_lock);
70 	} else if (scq->cq_handle < rcq->cq_handle) {
71 		spin_lock_irqsave(&scq->cq_lock, *scq_flags);
72 		spin_lock_irqsave_nested(&rcq->cq_lock, *rcq_flags,
73 					 SINGLE_DEPTH_NESTING);
74 	} else {
75 		spin_lock_irqsave(&rcq->cq_lock, *rcq_flags);
76 		spin_lock_irqsave_nested(&scq->cq_lock, *scq_flags,
77 					 SINGLE_DEPTH_NESTING);
78 	}
79 }
80 
81 static void pvrdma_unlock_cqs(struct pvrdma_cq *scq, struct pvrdma_cq *rcq,
82 			      unsigned long *scq_flags,
83 			      unsigned long *rcq_flags)
84 	__releases(scq->cq_lock) __releases(rcq->cq_lock)
85 {
86 	if (scq == rcq) {
87 		__release(rcq->cq_lock);
88 		spin_unlock_irqrestore(&scq->cq_lock, *scq_flags);
89 	} else if (scq->cq_handle < rcq->cq_handle) {
90 		spin_unlock_irqrestore(&rcq->cq_lock, *rcq_flags);
91 		spin_unlock_irqrestore(&scq->cq_lock, *scq_flags);
92 	} else {
93 		spin_unlock_irqrestore(&scq->cq_lock, *scq_flags);
94 		spin_unlock_irqrestore(&rcq->cq_lock, *rcq_flags);
95 	}
96 }
97 
98 static void pvrdma_reset_qp(struct pvrdma_qp *qp)
99 {
100 	struct pvrdma_cq *scq, *rcq;
101 	unsigned long scq_flags, rcq_flags;
102 
103 	/* Clean up cqes */
104 	get_cqs(qp, &scq, &rcq);
105 	pvrdma_lock_cqs(scq, rcq, &scq_flags, &rcq_flags);
106 
107 	_pvrdma_flush_cqe(qp, scq);
108 	if (scq != rcq)
109 		_pvrdma_flush_cqe(qp, rcq);
110 
111 	pvrdma_unlock_cqs(scq, rcq, &scq_flags, &rcq_flags);
112 
113 	/*
114 	 * Reset queuepair. The checks are because usermode queuepairs won't
115 	 * have kernel ringstates.
116 	 */
117 	if (qp->rq.ring) {
118 		atomic_set(&qp->rq.ring->cons_head, 0);
119 		atomic_set(&qp->rq.ring->prod_tail, 0);
120 	}
121 	if (qp->sq.ring) {
122 		atomic_set(&qp->sq.ring->cons_head, 0);
123 		atomic_set(&qp->sq.ring->prod_tail, 0);
124 	}
125 }
126 
127 static int pvrdma_set_rq_size(struct pvrdma_dev *dev,
128 			      struct ib_qp_cap *req_cap,
129 			      struct pvrdma_qp *qp)
130 {
131 	if (req_cap->max_recv_wr > dev->dsr->caps.max_qp_wr ||
132 	    req_cap->max_recv_sge > dev->dsr->caps.max_sge) {
133 		dev_warn(&dev->pdev->dev, "recv queue size invalid\n");
134 		return -EINVAL;
135 	}
136 
137 	qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, req_cap->max_recv_wr));
138 	qp->rq.max_sg = roundup_pow_of_two(max(1U, req_cap->max_recv_sge));
139 
140 	/* Write back */
141 	req_cap->max_recv_wr = qp->rq.wqe_cnt;
142 	req_cap->max_recv_sge = qp->rq.max_sg;
143 
144 	qp->rq.wqe_size = roundup_pow_of_two(sizeof(struct pvrdma_rq_wqe_hdr) +
145 					     sizeof(struct pvrdma_sge) *
146 					     qp->rq.max_sg);
147 	qp->npages_recv = (qp->rq.wqe_cnt * qp->rq.wqe_size + PAGE_SIZE - 1) /
148 			  PAGE_SIZE;
149 
150 	return 0;
151 }
152 
153 static int pvrdma_set_sq_size(struct pvrdma_dev *dev, struct ib_qp_cap *req_cap,
154 			      struct pvrdma_qp *qp)
155 {
156 	if (req_cap->max_send_wr > dev->dsr->caps.max_qp_wr ||
157 	    req_cap->max_send_sge > dev->dsr->caps.max_sge) {
158 		dev_warn(&dev->pdev->dev, "send queue size invalid\n");
159 		return -EINVAL;
160 	}
161 
162 	qp->sq.wqe_cnt = roundup_pow_of_two(max(1U, req_cap->max_send_wr));
163 	qp->sq.max_sg = roundup_pow_of_two(max(1U, req_cap->max_send_sge));
164 
165 	/* Write back */
166 	req_cap->max_send_wr = qp->sq.wqe_cnt;
167 	req_cap->max_send_sge = qp->sq.max_sg;
168 
169 	qp->sq.wqe_size = roundup_pow_of_two(sizeof(struct pvrdma_sq_wqe_hdr) +
170 					     sizeof(struct pvrdma_sge) *
171 					     qp->sq.max_sg);
172 	/* Note: one extra page for the header. */
173 	qp->npages_send = 1 + (qp->sq.wqe_cnt * qp->sq.wqe_size +
174 			       PAGE_SIZE - 1) / PAGE_SIZE;
175 
176 	return 0;
177 }
178 
179 /**
180  * pvrdma_create_qp - create queue pair
181  * @pd: protection domain
182  * @init_attr: queue pair attributes
183  * @udata: user data
184  *
185  * @return: the ib_qp pointer on success, otherwise returns an errno.
186  */
187 struct ib_qp *pvrdma_create_qp(struct ib_pd *pd,
188 			       struct ib_qp_init_attr *init_attr,
189 			       struct ib_udata *udata)
190 {
191 	struct pvrdma_qp *qp = NULL;
192 	struct pvrdma_dev *dev = to_vdev(pd->device);
193 	union pvrdma_cmd_req req;
194 	union pvrdma_cmd_resp rsp;
195 	struct pvrdma_cmd_create_qp *cmd = &req.create_qp;
196 	struct pvrdma_cmd_create_qp_resp *resp = &rsp.create_qp_resp;
197 	struct pvrdma_create_qp ucmd;
198 	unsigned long flags;
199 	int ret;
200 
201 	if (init_attr->create_flags) {
202 		dev_warn(&dev->pdev->dev,
203 			 "invalid create queuepair flags %#x\n",
204 			 init_attr->create_flags);
205 		return ERR_PTR(-EINVAL);
206 	}
207 
208 	if (init_attr->qp_type != IB_QPT_RC &&
209 	    init_attr->qp_type != IB_QPT_UD &&
210 	    init_attr->qp_type != IB_QPT_GSI) {
211 		dev_warn(&dev->pdev->dev, "queuepair type %d not supported\n",
212 			 init_attr->qp_type);
213 		return ERR_PTR(-EINVAL);
214 	}
215 
216 	if (!atomic_add_unless(&dev->num_qps, 1, dev->dsr->caps.max_qp))
217 		return ERR_PTR(-ENOMEM);
218 
219 	switch (init_attr->qp_type) {
220 	case IB_QPT_GSI:
221 		if (init_attr->port_num == 0 ||
222 		    init_attr->port_num > pd->device->phys_port_cnt ||
223 		    udata) {
224 			dev_warn(&dev->pdev->dev, "invalid queuepair attrs\n");
225 			ret = -EINVAL;
226 			goto err_qp;
227 		}
228 		/* fall through */
229 	case IB_QPT_RC:
230 	case IB_QPT_UD:
231 		qp = kzalloc(sizeof(*qp), GFP_KERNEL);
232 		if (!qp) {
233 			ret = -ENOMEM;
234 			goto err_qp;
235 		}
236 
237 		spin_lock_init(&qp->sq.lock);
238 		spin_lock_init(&qp->rq.lock);
239 		mutex_init(&qp->mutex);
240 		atomic_set(&qp->refcnt, 1);
241 		init_waitqueue_head(&qp->wait);
242 
243 		qp->state = IB_QPS_RESET;
244 
245 		if (pd->uobject && udata) {
246 			dev_dbg(&dev->pdev->dev,
247 				"create queuepair from user space\n");
248 
249 			if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
250 				ret = -EFAULT;
251 				goto err_qp;
252 			}
253 
254 			/* set qp->sq.wqe_cnt, shift, buf_size.. */
255 			qp->rumem = ib_umem_get(pd->uobject->context,
256 						ucmd.rbuf_addr,
257 						ucmd.rbuf_size, 0, 0);
258 			if (IS_ERR(qp->rumem)) {
259 				ret = PTR_ERR(qp->rumem);
260 				goto err_qp;
261 			}
262 
263 			qp->sumem = ib_umem_get(pd->uobject->context,
264 						ucmd.sbuf_addr,
265 						ucmd.sbuf_size, 0, 0);
266 			if (IS_ERR(qp->sumem)) {
267 				ib_umem_release(qp->rumem);
268 				ret = PTR_ERR(qp->sumem);
269 				goto err_qp;
270 			}
271 
272 			qp->npages_send = ib_umem_page_count(qp->sumem);
273 			qp->npages_recv = ib_umem_page_count(qp->rumem);
274 			qp->npages = qp->npages_send + qp->npages_recv;
275 		} else {
276 			qp->is_kernel = true;
277 
278 			ret = pvrdma_set_sq_size(to_vdev(pd->device),
279 						 &init_attr->cap, qp);
280 			if (ret)
281 				goto err_qp;
282 
283 			ret = pvrdma_set_rq_size(to_vdev(pd->device),
284 						 &init_attr->cap, qp);
285 			if (ret)
286 				goto err_qp;
287 
288 			qp->npages = qp->npages_send + qp->npages_recv;
289 
290 			/* Skip header page. */
291 			qp->sq.offset = PAGE_SIZE;
292 
293 			/* Recv queue pages are after send pages. */
294 			qp->rq.offset = qp->npages_send * PAGE_SIZE;
295 		}
296 
297 		if (qp->npages < 0 || qp->npages > PVRDMA_PAGE_DIR_MAX_PAGES) {
298 			dev_warn(&dev->pdev->dev,
299 				 "overflow pages in queuepair\n");
300 			ret = -EINVAL;
301 			goto err_umem;
302 		}
303 
304 		ret = pvrdma_page_dir_init(dev, &qp->pdir, qp->npages,
305 					   qp->is_kernel);
306 		if (ret) {
307 			dev_warn(&dev->pdev->dev,
308 				 "could not allocate page directory\n");
309 			goto err_umem;
310 		}
311 
312 		if (!qp->is_kernel) {
313 			pvrdma_page_dir_insert_umem(&qp->pdir, qp->sumem, 0);
314 			pvrdma_page_dir_insert_umem(&qp->pdir, qp->rumem,
315 						    qp->npages_send);
316 		} else {
317 			/* Ring state is always the first page. */
318 			qp->sq.ring = qp->pdir.pages[0];
319 			qp->rq.ring = &qp->sq.ring[1];
320 		}
321 		break;
322 	default:
323 		ret = -EINVAL;
324 		goto err_qp;
325 	}
326 
327 	/* Not supported */
328 	init_attr->cap.max_inline_data = 0;
329 
330 	memset(cmd, 0, sizeof(*cmd));
331 	cmd->hdr.cmd = PVRDMA_CMD_CREATE_QP;
332 	cmd->pd_handle = to_vpd(pd)->pd_handle;
333 	cmd->send_cq_handle = to_vcq(init_attr->send_cq)->cq_handle;
334 	cmd->recv_cq_handle = to_vcq(init_attr->recv_cq)->cq_handle;
335 	cmd->max_send_wr = init_attr->cap.max_send_wr;
336 	cmd->max_recv_wr = init_attr->cap.max_recv_wr;
337 	cmd->max_send_sge = init_attr->cap.max_send_sge;
338 	cmd->max_recv_sge = init_attr->cap.max_recv_sge;
339 	cmd->max_inline_data = init_attr->cap.max_inline_data;
340 	cmd->sq_sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) ? 1 : 0;
341 	cmd->qp_type = ib_qp_type_to_pvrdma(init_attr->qp_type);
342 	cmd->access_flags = IB_ACCESS_LOCAL_WRITE;
343 	cmd->total_chunks = qp->npages;
344 	cmd->send_chunks = qp->npages_send - 1;
345 	cmd->pdir_dma = qp->pdir.dir_dma;
346 
347 	dev_dbg(&dev->pdev->dev, "create queuepair with %d, %d, %d, %d\n",
348 		cmd->max_send_wr, cmd->max_recv_wr, cmd->max_send_sge,
349 		cmd->max_recv_sge);
350 
351 	ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_QP_RESP);
352 	if (ret < 0) {
353 		dev_warn(&dev->pdev->dev,
354 			 "could not create queuepair, error: %d\n", ret);
355 		goto err_pdir;
356 	}
357 
358 	/* max_send_wr/_recv_wr/_send_sge/_recv_sge/_inline_data */
359 	qp->qp_handle = resp->qpn;
360 	qp->port = init_attr->port_num;
361 	qp->ibqp.qp_num = resp->qpn;
362 	spin_lock_irqsave(&dev->qp_tbl_lock, flags);
363 	dev->qp_tbl[qp->qp_handle % dev->dsr->caps.max_qp] = qp;
364 	spin_unlock_irqrestore(&dev->qp_tbl_lock, flags);
365 
366 	return &qp->ibqp;
367 
368 err_pdir:
369 	pvrdma_page_dir_cleanup(dev, &qp->pdir);
370 err_umem:
371 	if (pd->uobject && udata) {
372 		if (qp->rumem)
373 			ib_umem_release(qp->rumem);
374 		if (qp->sumem)
375 			ib_umem_release(qp->sumem);
376 	}
377 err_qp:
378 	kfree(qp);
379 	atomic_dec(&dev->num_qps);
380 
381 	return ERR_PTR(ret);
382 }
383 
384 static void pvrdma_free_qp(struct pvrdma_qp *qp)
385 {
386 	struct pvrdma_dev *dev = to_vdev(qp->ibqp.device);
387 	struct pvrdma_cq *scq;
388 	struct pvrdma_cq *rcq;
389 	unsigned long flags, scq_flags, rcq_flags;
390 
391 	/* In case cq is polling */
392 	get_cqs(qp, &scq, &rcq);
393 	pvrdma_lock_cqs(scq, rcq, &scq_flags, &rcq_flags);
394 
395 	_pvrdma_flush_cqe(qp, scq);
396 	if (scq != rcq)
397 		_pvrdma_flush_cqe(qp, rcq);
398 
399 	spin_lock_irqsave(&dev->qp_tbl_lock, flags);
400 	dev->qp_tbl[qp->qp_handle] = NULL;
401 	spin_unlock_irqrestore(&dev->qp_tbl_lock, flags);
402 
403 	pvrdma_unlock_cqs(scq, rcq, &scq_flags, &rcq_flags);
404 
405 	atomic_dec(&qp->refcnt);
406 	wait_event(qp->wait, !atomic_read(&qp->refcnt));
407 
408 	pvrdma_page_dir_cleanup(dev, &qp->pdir);
409 
410 	kfree(qp);
411 
412 	atomic_dec(&dev->num_qps);
413 }
414 
415 /**
416  * pvrdma_destroy_qp - destroy a queue pair
417  * @qp: the queue pair to destroy
418  *
419  * @return: 0 on success.
420  */
421 int pvrdma_destroy_qp(struct ib_qp *qp)
422 {
423 	struct pvrdma_qp *vqp = to_vqp(qp);
424 	union pvrdma_cmd_req req;
425 	struct pvrdma_cmd_destroy_qp *cmd = &req.destroy_qp;
426 	int ret;
427 
428 	memset(cmd, 0, sizeof(*cmd));
429 	cmd->hdr.cmd = PVRDMA_CMD_DESTROY_QP;
430 	cmd->qp_handle = vqp->qp_handle;
431 
432 	ret = pvrdma_cmd_post(to_vdev(qp->device), &req, NULL, 0);
433 	if (ret < 0)
434 		dev_warn(&to_vdev(qp->device)->pdev->dev,
435 			 "destroy queuepair failed, error: %d\n", ret);
436 
437 	pvrdma_free_qp(vqp);
438 
439 	return 0;
440 }
441 
442 /**
443  * pvrdma_modify_qp - modify queue pair attributes
444  * @ibqp: the queue pair
445  * @attr: the new queue pair's attributes
446  * @attr_mask: attributes mask
447  * @udata: user data
448  *
449  * @returns 0 on success, otherwise returns an errno.
450  */
451 int pvrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
452 		     int attr_mask, struct ib_udata *udata)
453 {
454 	struct pvrdma_dev *dev = to_vdev(ibqp->device);
455 	struct pvrdma_qp *qp = to_vqp(ibqp);
456 	union pvrdma_cmd_req req;
457 	union pvrdma_cmd_resp rsp;
458 	struct pvrdma_cmd_modify_qp *cmd = &req.modify_qp;
459 	int cur_state, next_state;
460 	int ret;
461 
462 	/* Sanity checking. Should need lock here */
463 	mutex_lock(&qp->mutex);
464 	cur_state = (attr_mask & IB_QP_CUR_STATE) ? attr->cur_qp_state :
465 		qp->state;
466 	next_state = (attr_mask & IB_QP_STATE) ? attr->qp_state : cur_state;
467 
468 	if (!ib_modify_qp_is_ok(cur_state, next_state, ibqp->qp_type,
469 				attr_mask, IB_LINK_LAYER_ETHERNET)) {
470 		ret = -EINVAL;
471 		goto out;
472 	}
473 
474 	if (attr_mask & IB_QP_PORT) {
475 		if (attr->port_num == 0 ||
476 		    attr->port_num > ibqp->device->phys_port_cnt) {
477 			ret = -EINVAL;
478 			goto out;
479 		}
480 	}
481 
482 	if (attr_mask & IB_QP_MIN_RNR_TIMER) {
483 		if (attr->min_rnr_timer > 31) {
484 			ret = -EINVAL;
485 			goto out;
486 		}
487 	}
488 
489 	if (attr_mask & IB_QP_PKEY_INDEX) {
490 		if (attr->pkey_index >= dev->dsr->caps.max_pkeys) {
491 			ret = -EINVAL;
492 			goto out;
493 		}
494 	}
495 
496 	if (attr_mask & IB_QP_QKEY)
497 		qp->qkey = attr->qkey;
498 
499 	if (cur_state == next_state && cur_state == IB_QPS_RESET) {
500 		ret = 0;
501 		goto out;
502 	}
503 
504 	qp->state = next_state;
505 	memset(cmd, 0, sizeof(*cmd));
506 	cmd->hdr.cmd = PVRDMA_CMD_MODIFY_QP;
507 	cmd->qp_handle = qp->qp_handle;
508 	cmd->attr_mask = ib_qp_attr_mask_to_pvrdma(attr_mask);
509 	cmd->attrs.qp_state = ib_qp_state_to_pvrdma(attr->qp_state);
510 	cmd->attrs.cur_qp_state =
511 		ib_qp_state_to_pvrdma(attr->cur_qp_state);
512 	cmd->attrs.path_mtu = ib_mtu_to_pvrdma(attr->path_mtu);
513 	cmd->attrs.path_mig_state =
514 		ib_mig_state_to_pvrdma(attr->path_mig_state);
515 	cmd->attrs.qkey = attr->qkey;
516 	cmd->attrs.rq_psn = attr->rq_psn;
517 	cmd->attrs.sq_psn = attr->sq_psn;
518 	cmd->attrs.dest_qp_num = attr->dest_qp_num;
519 	cmd->attrs.qp_access_flags =
520 		ib_access_flags_to_pvrdma(attr->qp_access_flags);
521 	cmd->attrs.pkey_index = attr->pkey_index;
522 	cmd->attrs.alt_pkey_index = attr->alt_pkey_index;
523 	cmd->attrs.en_sqd_async_notify = attr->en_sqd_async_notify;
524 	cmd->attrs.sq_draining = attr->sq_draining;
525 	cmd->attrs.max_rd_atomic = attr->max_rd_atomic;
526 	cmd->attrs.max_dest_rd_atomic = attr->max_dest_rd_atomic;
527 	cmd->attrs.min_rnr_timer = attr->min_rnr_timer;
528 	cmd->attrs.port_num = attr->port_num;
529 	cmd->attrs.timeout = attr->timeout;
530 	cmd->attrs.retry_cnt = attr->retry_cnt;
531 	cmd->attrs.rnr_retry = attr->rnr_retry;
532 	cmd->attrs.alt_port_num = attr->alt_port_num;
533 	cmd->attrs.alt_timeout = attr->alt_timeout;
534 	ib_qp_cap_to_pvrdma(&cmd->attrs.cap, &attr->cap);
535 	ib_ah_attr_to_pvrdma(&cmd->attrs.ah_attr, &attr->ah_attr);
536 	ib_ah_attr_to_pvrdma(&cmd->attrs.alt_ah_attr, &attr->alt_ah_attr);
537 
538 	ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_MODIFY_QP_RESP);
539 	if (ret < 0) {
540 		dev_warn(&dev->pdev->dev,
541 			 "could not modify queuepair, error: %d\n", ret);
542 	} else if (rsp.hdr.err > 0) {
543 		dev_warn(&dev->pdev->dev,
544 			 "cannot modify queuepair, error: %d\n", rsp.hdr.err);
545 		ret = -EINVAL;
546 	}
547 
548 	if (ret == 0 && next_state == IB_QPS_RESET)
549 		pvrdma_reset_qp(qp);
550 
551 out:
552 	mutex_unlock(&qp->mutex);
553 
554 	return ret;
555 }
556 
557 static inline void *get_sq_wqe(struct pvrdma_qp *qp, int n)
558 {
559 	return pvrdma_page_dir_get_ptr(&qp->pdir,
560 				       qp->sq.offset + n * qp->sq.wqe_size);
561 }
562 
563 static inline void *get_rq_wqe(struct pvrdma_qp *qp, int n)
564 {
565 	return pvrdma_page_dir_get_ptr(&qp->pdir,
566 				       qp->rq.offset + n * qp->rq.wqe_size);
567 }
568 
569 static int set_reg_seg(struct pvrdma_sq_wqe_hdr *wqe_hdr, struct ib_reg_wr *wr)
570 {
571 	struct pvrdma_user_mr *mr = to_vmr(wr->mr);
572 
573 	wqe_hdr->wr.fast_reg.iova_start = mr->ibmr.iova;
574 	wqe_hdr->wr.fast_reg.pl_pdir_dma = mr->pdir.dir_dma;
575 	wqe_hdr->wr.fast_reg.page_shift = mr->page_shift;
576 	wqe_hdr->wr.fast_reg.page_list_len = mr->npages;
577 	wqe_hdr->wr.fast_reg.length = mr->ibmr.length;
578 	wqe_hdr->wr.fast_reg.access_flags = wr->access;
579 	wqe_hdr->wr.fast_reg.rkey = wr->key;
580 
581 	return pvrdma_page_dir_insert_page_list(&mr->pdir, mr->pages,
582 						mr->npages);
583 }
584 
585 /**
586  * pvrdma_post_send - post send work request entries on a QP
587  * @ibqp: the QP
588  * @wr: work request list to post
589  * @bad_wr: the first bad WR returned
590  *
591  * @return: 0 on success, otherwise errno returned.
592  */
593 int pvrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
594 		     struct ib_send_wr **bad_wr)
595 {
596 	struct pvrdma_qp *qp = to_vqp(ibqp);
597 	struct pvrdma_dev *dev = to_vdev(ibqp->device);
598 	unsigned long flags;
599 	struct pvrdma_sq_wqe_hdr *wqe_hdr;
600 	struct pvrdma_sge *sge;
601 	int i, index;
602 	int nreq;
603 	int ret;
604 
605 	/*
606 	 * In states lower than RTS, we can fail immediately. In other states,
607 	 * just post and let the device figure it out.
608 	 */
609 	if (qp->state < IB_QPS_RTS) {
610 		*bad_wr = wr;
611 		return -EINVAL;
612 	}
613 
614 	spin_lock_irqsave(&qp->sq.lock, flags);
615 
616 	index = pvrdma_idx(&qp->sq.ring->prod_tail, qp->sq.wqe_cnt);
617 	for (nreq = 0; wr; nreq++, wr = wr->next) {
618 		unsigned int tail;
619 
620 		if (unlikely(!pvrdma_idx_ring_has_space(
621 				qp->sq.ring, qp->sq.wqe_cnt, &tail))) {
622 			dev_warn_ratelimited(&dev->pdev->dev,
623 					     "send queue is full\n");
624 			*bad_wr = wr;
625 			ret = -ENOMEM;
626 			goto out;
627 		}
628 
629 		if (unlikely(wr->num_sge > qp->sq.max_sg || wr->num_sge < 0)) {
630 			dev_warn_ratelimited(&dev->pdev->dev,
631 					     "send SGE overflow\n");
632 			*bad_wr = wr;
633 			ret = -EINVAL;
634 			goto out;
635 		}
636 
637 		if (unlikely(wr->opcode < 0)) {
638 			dev_warn_ratelimited(&dev->pdev->dev,
639 					     "invalid send opcode\n");
640 			*bad_wr = wr;
641 			ret = -EINVAL;
642 			goto out;
643 		}
644 
645 		/*
646 		 * Only support UD, RC.
647 		 * Need to check opcode table for thorough checking.
648 		 * opcode		_UD	_UC	_RC
649 		 * _SEND		x	x	x
650 		 * _SEND_WITH_IMM	x	x	x
651 		 * _RDMA_WRITE			x	x
652 		 * _RDMA_WRITE_WITH_IMM		x	x
653 		 * _LOCAL_INV			x	x
654 		 * _SEND_WITH_INV		x	x
655 		 * _RDMA_READ				x
656 		 * _ATOMIC_CMP_AND_SWP			x
657 		 * _ATOMIC_FETCH_AND_ADD		x
658 		 * _MASK_ATOMIC_CMP_AND_SWP		x
659 		 * _MASK_ATOMIC_FETCH_AND_ADD		x
660 		 * _REG_MR				x
661 		 *
662 		 */
663 		if (qp->ibqp.qp_type != IB_QPT_UD &&
664 		    qp->ibqp.qp_type != IB_QPT_RC &&
665 			wr->opcode != IB_WR_SEND) {
666 			dev_warn_ratelimited(&dev->pdev->dev,
667 					     "unsupported queuepair type\n");
668 			*bad_wr = wr;
669 			ret = -EINVAL;
670 			goto out;
671 		} else if (qp->ibqp.qp_type == IB_QPT_UD ||
672 			   qp->ibqp.qp_type == IB_QPT_GSI) {
673 			if (wr->opcode != IB_WR_SEND &&
674 			    wr->opcode != IB_WR_SEND_WITH_IMM) {
675 				dev_warn_ratelimited(&dev->pdev->dev,
676 						     "invalid send opcode\n");
677 				*bad_wr = wr;
678 				ret = -EINVAL;
679 				goto out;
680 			}
681 		}
682 
683 		wqe_hdr = (struct pvrdma_sq_wqe_hdr *)get_sq_wqe(qp, index);
684 		memset(wqe_hdr, 0, sizeof(*wqe_hdr));
685 		wqe_hdr->wr_id = wr->wr_id;
686 		wqe_hdr->num_sge = wr->num_sge;
687 		wqe_hdr->opcode = ib_wr_opcode_to_pvrdma(wr->opcode);
688 		wqe_hdr->send_flags = ib_send_flags_to_pvrdma(wr->send_flags);
689 		if (wr->opcode == IB_WR_SEND_WITH_IMM ||
690 		    wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
691 			wqe_hdr->ex.imm_data = wr->ex.imm_data;
692 
693 		switch (qp->ibqp.qp_type) {
694 		case IB_QPT_GSI:
695 		case IB_QPT_UD:
696 			if (unlikely(!ud_wr(wr)->ah)) {
697 				dev_warn_ratelimited(&dev->pdev->dev,
698 						     "invalid address handle\n");
699 				*bad_wr = wr;
700 				ret = -EINVAL;
701 				goto out;
702 			}
703 
704 			/*
705 			 * Use qkey from qp context if high order bit set,
706 			 * otherwise from work request.
707 			 */
708 			wqe_hdr->wr.ud.remote_qpn = ud_wr(wr)->remote_qpn;
709 			wqe_hdr->wr.ud.remote_qkey =
710 				ud_wr(wr)->remote_qkey & 0x80000000 ?
711 				qp->qkey : ud_wr(wr)->remote_qkey;
712 			wqe_hdr->wr.ud.av = to_vah(ud_wr(wr)->ah)->av;
713 
714 			break;
715 		case IB_QPT_RC:
716 			switch (wr->opcode) {
717 			case IB_WR_RDMA_READ:
718 			case IB_WR_RDMA_WRITE:
719 			case IB_WR_RDMA_WRITE_WITH_IMM:
720 				wqe_hdr->wr.rdma.remote_addr =
721 					rdma_wr(wr)->remote_addr;
722 				wqe_hdr->wr.rdma.rkey = rdma_wr(wr)->rkey;
723 				break;
724 			case IB_WR_LOCAL_INV:
725 			case IB_WR_SEND_WITH_INV:
726 				wqe_hdr->ex.invalidate_rkey =
727 					wr->ex.invalidate_rkey;
728 				break;
729 			case IB_WR_ATOMIC_CMP_AND_SWP:
730 			case IB_WR_ATOMIC_FETCH_AND_ADD:
731 				wqe_hdr->wr.atomic.remote_addr =
732 					atomic_wr(wr)->remote_addr;
733 				wqe_hdr->wr.atomic.rkey = atomic_wr(wr)->rkey;
734 				wqe_hdr->wr.atomic.compare_add =
735 					atomic_wr(wr)->compare_add;
736 				if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP)
737 					wqe_hdr->wr.atomic.swap =
738 						atomic_wr(wr)->swap;
739 				break;
740 			case IB_WR_REG_MR:
741 				ret = set_reg_seg(wqe_hdr, reg_wr(wr));
742 				if (ret < 0) {
743 					dev_warn_ratelimited(&dev->pdev->dev,
744 							     "Failed to set fast register work request\n");
745 					*bad_wr = wr;
746 					goto out;
747 				}
748 				break;
749 			default:
750 				break;
751 			}
752 
753 			break;
754 		default:
755 			dev_warn_ratelimited(&dev->pdev->dev,
756 					     "invalid queuepair type\n");
757 			ret = -EINVAL;
758 			*bad_wr = wr;
759 			goto out;
760 		}
761 
762 		sge = (struct pvrdma_sge *)(wqe_hdr + 1);
763 		for (i = 0; i < wr->num_sge; i++) {
764 			/* Need to check wqe_size 0 or max size */
765 			sge->addr = wr->sg_list[i].addr;
766 			sge->length = wr->sg_list[i].length;
767 			sge->lkey = wr->sg_list[i].lkey;
768 			sge++;
769 		}
770 
771 		/* Make sure wqe is written before index update */
772 		smp_wmb();
773 
774 		index++;
775 		if (unlikely(index >= qp->sq.wqe_cnt))
776 			index = 0;
777 		/* Update shared sq ring */
778 		pvrdma_idx_ring_inc(&qp->sq.ring->prod_tail,
779 				    qp->sq.wqe_cnt);
780 	}
781 
782 	ret = 0;
783 
784 out:
785 	spin_unlock_irqrestore(&qp->sq.lock, flags);
786 
787 	if (!ret)
788 		pvrdma_write_uar_qp(dev, PVRDMA_UAR_QP_SEND | qp->qp_handle);
789 
790 	return ret;
791 }
792 
793 /**
794  * pvrdma_post_receive - post receive work request entries on a QP
795  * @ibqp: the QP
796  * @wr: the work request list to post
797  * @bad_wr: the first bad WR returned
798  *
799  * @return: 0 on success, otherwise errno returned.
800  */
801 int pvrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
802 		     struct ib_recv_wr **bad_wr)
803 {
804 	struct pvrdma_dev *dev = to_vdev(ibqp->device);
805 	unsigned long flags;
806 	struct pvrdma_qp *qp = to_vqp(ibqp);
807 	struct pvrdma_rq_wqe_hdr *wqe_hdr;
808 	struct pvrdma_sge *sge;
809 	int index, nreq;
810 	int ret = 0;
811 	int i;
812 
813 	/*
814 	 * In the RESET state, we can fail immediately. For other states,
815 	 * just post and let the device figure it out.
816 	 */
817 	if (qp->state == IB_QPS_RESET) {
818 		*bad_wr = wr;
819 		return -EINVAL;
820 	}
821 
822 	spin_lock_irqsave(&qp->rq.lock, flags);
823 
824 	index = pvrdma_idx(&qp->rq.ring->prod_tail, qp->rq.wqe_cnt);
825 	for (nreq = 0; wr; nreq++, wr = wr->next) {
826 		unsigned int tail;
827 
828 		if (unlikely(wr->num_sge > qp->rq.max_sg ||
829 			     wr->num_sge < 0)) {
830 			ret = -EINVAL;
831 			*bad_wr = wr;
832 			dev_warn_ratelimited(&dev->pdev->dev,
833 					     "recv SGE overflow\n");
834 			goto out;
835 		}
836 
837 		if (unlikely(!pvrdma_idx_ring_has_space(
838 				qp->rq.ring, qp->rq.wqe_cnt, &tail))) {
839 			ret = -ENOMEM;
840 			*bad_wr = wr;
841 			dev_warn_ratelimited(&dev->pdev->dev,
842 					     "recv queue full\n");
843 			goto out;
844 		}
845 
846 		wqe_hdr = (struct pvrdma_rq_wqe_hdr *)get_rq_wqe(qp, index);
847 		wqe_hdr->wr_id = wr->wr_id;
848 		wqe_hdr->num_sge = wr->num_sge;
849 		wqe_hdr->total_len = 0;
850 
851 		sge = (struct pvrdma_sge *)(wqe_hdr + 1);
852 		for (i = 0; i < wr->num_sge; i++) {
853 			sge->addr = wr->sg_list[i].addr;
854 			sge->length = wr->sg_list[i].length;
855 			sge->lkey = wr->sg_list[i].lkey;
856 			sge++;
857 		}
858 
859 		/* Make sure wqe is written before index update */
860 		smp_wmb();
861 
862 		index++;
863 		if (unlikely(index >= qp->rq.wqe_cnt))
864 			index = 0;
865 		/* Update shared rq ring */
866 		pvrdma_idx_ring_inc(&qp->rq.ring->prod_tail,
867 				    qp->rq.wqe_cnt);
868 	}
869 
870 	spin_unlock_irqrestore(&qp->rq.lock, flags);
871 
872 	pvrdma_write_uar_qp(dev, PVRDMA_UAR_QP_RECV | qp->qp_handle);
873 
874 	return ret;
875 
876 out:
877 	spin_unlock_irqrestore(&qp->rq.lock, flags);
878 
879 	return ret;
880 }
881 
882 /**
883  * pvrdma_query_qp - query a queue pair's attributes
884  * @ibqp: the queue pair to query
885  * @attr: the queue pair's attributes
886  * @attr_mask: attributes mask
887  * @init_attr: initial queue pair attributes
888  *
889  * @returns 0 on success, otherwise returns an errno.
890  */
891 int pvrdma_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
892 		    int attr_mask, struct ib_qp_init_attr *init_attr)
893 {
894 	struct pvrdma_dev *dev = to_vdev(ibqp->device);
895 	struct pvrdma_qp *qp = to_vqp(ibqp);
896 	union pvrdma_cmd_req req;
897 	union pvrdma_cmd_resp rsp;
898 	struct pvrdma_cmd_query_qp *cmd = &req.query_qp;
899 	struct pvrdma_cmd_query_qp_resp *resp = &rsp.query_qp_resp;
900 	int ret = 0;
901 
902 	mutex_lock(&qp->mutex);
903 
904 	if (qp->state == IB_QPS_RESET) {
905 		attr->qp_state = IB_QPS_RESET;
906 		goto out;
907 	}
908 
909 	memset(cmd, 0, sizeof(*cmd));
910 	cmd->hdr.cmd = PVRDMA_CMD_QUERY_QP;
911 	cmd->qp_handle = qp->qp_handle;
912 	cmd->attr_mask = ib_qp_attr_mask_to_pvrdma(attr_mask);
913 
914 	ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_QP_RESP);
915 	if (ret < 0) {
916 		dev_warn(&dev->pdev->dev,
917 			 "could not query queuepair, error: %d\n", ret);
918 		goto out;
919 	}
920 
921 	attr->qp_state = pvrdma_qp_state_to_ib(resp->attrs.qp_state);
922 	attr->cur_qp_state =
923 		pvrdma_qp_state_to_ib(resp->attrs.cur_qp_state);
924 	attr->path_mtu = pvrdma_mtu_to_ib(resp->attrs.path_mtu);
925 	attr->path_mig_state =
926 		pvrdma_mig_state_to_ib(resp->attrs.path_mig_state);
927 	attr->qkey = resp->attrs.qkey;
928 	attr->rq_psn = resp->attrs.rq_psn;
929 	attr->sq_psn = resp->attrs.sq_psn;
930 	attr->dest_qp_num = resp->attrs.dest_qp_num;
931 	attr->qp_access_flags =
932 		pvrdma_access_flags_to_ib(resp->attrs.qp_access_flags);
933 	attr->pkey_index = resp->attrs.pkey_index;
934 	attr->alt_pkey_index = resp->attrs.alt_pkey_index;
935 	attr->en_sqd_async_notify = resp->attrs.en_sqd_async_notify;
936 	attr->sq_draining = resp->attrs.sq_draining;
937 	attr->max_rd_atomic = resp->attrs.max_rd_atomic;
938 	attr->max_dest_rd_atomic = resp->attrs.max_dest_rd_atomic;
939 	attr->min_rnr_timer = resp->attrs.min_rnr_timer;
940 	attr->port_num = resp->attrs.port_num;
941 	attr->timeout = resp->attrs.timeout;
942 	attr->retry_cnt = resp->attrs.retry_cnt;
943 	attr->rnr_retry = resp->attrs.rnr_retry;
944 	attr->alt_port_num = resp->attrs.alt_port_num;
945 	attr->alt_timeout = resp->attrs.alt_timeout;
946 	pvrdma_qp_cap_to_ib(&attr->cap, &resp->attrs.cap);
947 	pvrdma_ah_attr_to_ib(&attr->ah_attr, &resp->attrs.ah_attr);
948 	pvrdma_ah_attr_to_ib(&attr->alt_ah_attr, &resp->attrs.alt_ah_attr);
949 
950 	qp->state = attr->qp_state;
951 
952 	ret = 0;
953 
954 out:
955 	attr->cur_qp_state = attr->qp_state;
956 
957 	init_attr->event_handler = qp->ibqp.event_handler;
958 	init_attr->qp_context = qp->ibqp.qp_context;
959 	init_attr->send_cq = qp->ibqp.send_cq;
960 	init_attr->recv_cq = qp->ibqp.recv_cq;
961 	init_attr->srq = qp->ibqp.srq;
962 	init_attr->xrcd = NULL;
963 	init_attr->cap = attr->cap;
964 	init_attr->sq_sig_type = 0;
965 	init_attr->qp_type = qp->ibqp.qp_type;
966 	init_attr->create_flags = 0;
967 	init_attr->port_num = qp->port;
968 
969 	mutex_unlock(&qp->mutex);
970 	return ret;
971 }
972