xref: /openbmc/linux/drivers/infiniband/sw/rdmavt/qp.c (revision d2b8d4da)
1 /*
2  * Copyright(c) 2015 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 
48 #include <linux/bitops.h>
49 #include <linux/lockdep.h>
50 #include <linux/vmalloc.h>
51 #include <linux/slab.h>
52 #include <rdma/ib_verbs.h>
53 #include "qp.h"
54 #include "vt.h"
55 
56 static void get_map_page(struct rvt_qpn_table *qpt,
57 			 struct rvt_qpn_map *map,
58 			 gfp_t gfp)
59 {
60 	unsigned long page = get_zeroed_page(gfp);
61 
62 	/*
63 	 * Free the page if someone raced with us installing it.
64 	 */
65 
66 	spin_lock(&qpt->lock);
67 	if (map->page)
68 		free_page(page);
69 	else
70 		map->page = (void *)page;
71 	spin_unlock(&qpt->lock);
72 }
73 
74 /**
75  * init_qpn_table - initialize the QP number table for a device
76  * @qpt: the QPN table
77  */
78 static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
79 {
80 	u32 offset, i;
81 	struct rvt_qpn_map *map;
82 	int ret = 0;
83 
84 	if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
85 		return -EINVAL;
86 
87 	spin_lock_init(&qpt->lock);
88 
89 	qpt->last = rdi->dparms.qpn_start;
90 	qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
91 
92 	/*
93 	 * Drivers may want some QPs beyond what we need for verbs let them use
94 	 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
95 	 * for those. The reserved range must be *after* the range which verbs
96 	 * will pick from.
97 	 */
98 
99 	/* Figure out number of bit maps needed before reserved range */
100 	qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
101 
102 	/* This should always be zero */
103 	offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
104 
105 	/* Starting with the first reserved bit map */
106 	map = &qpt->map[qpt->nmaps];
107 
108 	rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
109 		    rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
110 	for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
111 		if (!map->page) {
112 			get_map_page(qpt, map, GFP_KERNEL);
113 			if (!map->page) {
114 				ret = -ENOMEM;
115 				break;
116 			}
117 		}
118 		set_bit(offset, map->page);
119 		offset++;
120 		if (offset == RVT_BITS_PER_PAGE) {
121 			/* next page */
122 			qpt->nmaps++;
123 			map++;
124 			offset = 0;
125 		}
126 	}
127 	return ret;
128 }
129 
130 /**
131  * free_qpn_table - free the QP number table for a device
132  * @qpt: the QPN table
133  */
134 static void free_qpn_table(struct rvt_qpn_table *qpt)
135 {
136 	int i;
137 
138 	for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
139 		free_page((unsigned long)qpt->map[i].page);
140 }
141 
142 int rvt_driver_qp_init(struct rvt_dev_info *rdi)
143 {
144 	int i;
145 	int ret = -ENOMEM;
146 
147 	if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER) {
148 		rvt_pr_info(rdi, "Driver is doing QP init.\n");
149 		return 0;
150 	}
151 
152 	if (!rdi->dparms.qp_table_size)
153 		return -EINVAL;
154 
155 	/*
156 	 * If driver is not doing any QP allocation then make sure it is
157 	 * providing the necessary QP functions.
158 	 */
159 	if (!rdi->driver_f.free_all_qps ||
160 	    !rdi->driver_f.qp_priv_alloc ||
161 	    !rdi->driver_f.qp_priv_free ||
162 	    !rdi->driver_f.notify_qp_reset)
163 		return -EINVAL;
164 
165 	/* allocate parent object */
166 	rdi->qp_dev = kzalloc(sizeof(*rdi->qp_dev), GFP_KERNEL);
167 	if (!rdi->qp_dev)
168 		return -ENOMEM;
169 
170 	/* allocate hash table */
171 	rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
172 	rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
173 	rdi->qp_dev->qp_table =
174 		kmalloc(rdi->qp_dev->qp_table_size *
175 			sizeof(*rdi->qp_dev->qp_table),
176 			GFP_KERNEL);
177 	if (!rdi->qp_dev->qp_table)
178 		goto no_qp_table;
179 
180 	for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
181 		RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
182 
183 	spin_lock_init(&rdi->qp_dev->qpt_lock);
184 
185 	/* initialize qpn map */
186 	if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
187 		goto fail_table;
188 
189 	spin_lock_init(&rdi->n_qps_lock);
190 
191 	return 0;
192 
193 fail_table:
194 	kfree(rdi->qp_dev->qp_table);
195 	free_qpn_table(&rdi->qp_dev->qpn_table);
196 
197 no_qp_table:
198 	kfree(rdi->qp_dev);
199 
200 	return ret;
201 }
202 
203 /**
204  * free_all_qps - check for QPs still in use
205  * @qpt: the QP table to empty
206  *
207  * There should not be any QPs still in use.
208  * Free memory for table.
209  */
210 static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
211 {
212 	unsigned long flags;
213 	struct rvt_qp *qp;
214 	unsigned n, qp_inuse = 0;
215 	spinlock_t *ql; /* work around too long line below */
216 
217 	if (rdi->driver_f.free_all_qps)
218 		qp_inuse = rdi->driver_f.free_all_qps(rdi);
219 
220 	if (!rdi->qp_dev)
221 		return qp_inuse;
222 
223 	ql = &rdi->qp_dev->qpt_lock;
224 	spin_lock_irqsave(ql, flags);
225 	for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
226 		qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
227 					       lockdep_is_held(ql));
228 		RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
229 
230 		for (; qp; qp = rcu_dereference_protected(qp->next,
231 							  lockdep_is_held(ql)))
232 			qp_inuse++;
233 	}
234 	spin_unlock_irqrestore(ql, flags);
235 	synchronize_rcu();
236 	return qp_inuse;
237 }
238 
239 void rvt_qp_exit(struct rvt_dev_info *rdi)
240 {
241 	u32 qps_inuse = rvt_free_all_qps(rdi);
242 
243 	if (qps_inuse)
244 		rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
245 			   qps_inuse);
246 	if (!rdi->qp_dev)
247 		return;
248 
249 	if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER)
250 		return; /* driver did the qp init so nothing else to do */
251 
252 	kfree(rdi->qp_dev->qp_table);
253 	free_qpn_table(&rdi->qp_dev->qpn_table);
254 	kfree(rdi->qp_dev);
255 }
256 
257 static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
258 			      struct rvt_qpn_map *map, unsigned off)
259 {
260 	return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
261 }
262 
263 /*
264  * Allocate the next available QPN or
265  * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
266  */
267 static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
268 		     enum ib_qp_type type, u8 port, gfp_t gfp)
269 {
270 	u32 i, offset, max_scan, qpn;
271 	struct rvt_qpn_map *map;
272 	u32 ret;
273 
274 	if (rdi->driver_f.alloc_qpn)
275 		return rdi->driver_f.alloc_qpn(rdi, qpt, type, port,
276 					       GFP_KERNEL);
277 
278 	if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
279 		unsigned n;
280 
281 		ret = type == IB_QPT_GSI;
282 		n = 1 << (ret + 2 * (port - 1));
283 		spin_lock(&qpt->lock);
284 		if (qpt->flags & n)
285 			ret = -EINVAL;
286 		else
287 			qpt->flags |= n;
288 		spin_unlock(&qpt->lock);
289 		goto bail;
290 	}
291 
292 	qpn = qpt->last + qpt->incr;
293 	if (qpn >= RVT_QPN_MAX)
294 		qpn = qpt->incr | ((qpt->last & 1) ^ 1);
295 	/* offset carries bit 0 */
296 	offset = qpn & RVT_BITS_PER_PAGE_MASK;
297 	map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
298 	max_scan = qpt->nmaps - !offset;
299 	for (i = 0;;) {
300 		if (unlikely(!map->page)) {
301 			get_map_page(qpt, map, gfp);
302 			if (unlikely(!map->page))
303 				break;
304 		}
305 		do {
306 			if (!test_and_set_bit(offset, map->page)) {
307 				qpt->last = qpn;
308 				ret = qpn;
309 				goto bail;
310 			}
311 			offset += qpt->incr;
312 			/*
313 			 * This qpn might be bogus if offset >= BITS_PER_PAGE.
314 			 * That is OK.   It gets re-assigned below
315 			 */
316 			qpn = mk_qpn(qpt, map, offset);
317 		} while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
318 		/*
319 		 * In order to keep the number of pages allocated to a
320 		 * minimum, we scan the all existing pages before increasing
321 		 * the size of the bitmap table.
322 		 */
323 		if (++i > max_scan) {
324 			if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
325 				break;
326 			map = &qpt->map[qpt->nmaps++];
327 			/* start at incr with current bit 0 */
328 			offset = qpt->incr | (offset & 1);
329 		} else if (map < &qpt->map[qpt->nmaps]) {
330 			++map;
331 			/* start at incr with current bit 0 */
332 			offset = qpt->incr | (offset & 1);
333 		} else {
334 			map = &qpt->map[0];
335 			/* wrap to first map page, invert bit 0 */
336 			offset = qpt->incr | ((offset & 1) ^ 1);
337 		}
338 		/* there can be no bits at shift and below */
339 		WARN_ON(offset & (rdi->dparms.qos_shift - 1));
340 		qpn = mk_qpn(qpt, map, offset);
341 	}
342 
343 	ret = -ENOMEM;
344 
345 bail:
346 	return ret;
347 }
348 
349 static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
350 {
351 	struct rvt_qpn_map *map;
352 
353 	map = qpt->map + qpn / RVT_BITS_PER_PAGE;
354 	if (map->page)
355 		clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
356 }
357 
358 /**
359  * reset_qp - initialize the QP state to the reset state
360  * @qp: the QP to reset
361  * @type: the QP type
362  */
363 void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
364 		  enum ib_qp_type type)
365 {
366 	qp->remote_qpn = 0;
367 	qp->qkey = 0;
368 	qp->qp_access_flags = 0;
369 
370 	/*
371 	 * Let driver do anything it needs to for a new/reset qp
372 	 */
373 	rdi->driver_f.notify_qp_reset(qp);
374 
375 	qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
376 	qp->s_hdrwords = 0;
377 	qp->s_wqe = NULL;
378 	qp->s_draining = 0;
379 	qp->s_next_psn = 0;
380 	qp->s_last_psn = 0;
381 	qp->s_sending_psn = 0;
382 	qp->s_sending_hpsn = 0;
383 	qp->s_psn = 0;
384 	qp->r_psn = 0;
385 	qp->r_msn = 0;
386 	if (type == IB_QPT_RC) {
387 		qp->s_state = IB_OPCODE_RC_SEND_LAST;
388 		qp->r_state = IB_OPCODE_RC_SEND_LAST;
389 	} else {
390 		qp->s_state = IB_OPCODE_UC_SEND_LAST;
391 		qp->r_state = IB_OPCODE_UC_SEND_LAST;
392 	}
393 	qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
394 	qp->r_nak_state = 0;
395 	qp->r_aflags = 0;
396 	qp->r_flags = 0;
397 	qp->s_head = 0;
398 	qp->s_tail = 0;
399 	qp->s_cur = 0;
400 	qp->s_acked = 0;
401 	qp->s_last = 0;
402 	qp->s_ssn = 1;
403 	qp->s_lsn = 0;
404 	qp->s_mig_state = IB_MIG_MIGRATED;
405 	memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
406 	qp->r_head_ack_queue = 0;
407 	qp->s_tail_ack_queue = 0;
408 	qp->s_num_rd_atomic = 0;
409 	if (qp->r_rq.wq) {
410 		qp->r_rq.wq->head = 0;
411 		qp->r_rq.wq->tail = 0;
412 	}
413 	qp->r_sge.num_sge = 0;
414 }
415 EXPORT_SYMBOL(rvt_reset_qp);
416 
417 /**
418  * rvt_create_qp - create a queue pair for a device
419  * @ibpd: the protection domain who's device we create the queue pair for
420  * @init_attr: the attributes of the queue pair
421  * @udata: user data for libibverbs.so
422  *
423  * Queue pair creation is mostly an rvt issue. However, drivers have their own
424  * unique idea of what queue pair numbers mean. For instance there is a reserved
425  * range for PSM.
426  *
427  * Returns the queue pair on success, otherwise returns an errno.
428  *
429  * Called by the ib_create_qp() core verbs function.
430  */
431 struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
432 			    struct ib_qp_init_attr *init_attr,
433 			    struct ib_udata *udata)
434 {
435 	struct rvt_qp *qp;
436 	int err;
437 	struct rvt_swqe *swq = NULL;
438 	size_t sz;
439 	size_t sg_list_sz;
440 	struct ib_qp *ret = ERR_PTR(-ENOMEM);
441 	struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
442 	void *priv = NULL;
443 	gfp_t gfp;
444 
445 	if (!rdi)
446 		return ERR_PTR(-EINVAL);
447 
448 	if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
449 	    init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
450 	    init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
451 		return ERR_PTR(-EINVAL);
452 
453 	/* GFP_NOIO is applicable to RC QP's only */
454 
455 	if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
456 	    init_attr->qp_type != IB_QPT_RC)
457 		return ERR_PTR(-EINVAL);
458 
459 	gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
460 						GFP_NOIO : GFP_KERNEL;
461 
462 	/* Check receive queue parameters if no SRQ is specified. */
463 	if (!init_attr->srq) {
464 		if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
465 		    init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
466 			return ERR_PTR(-EINVAL);
467 
468 		if (init_attr->cap.max_send_sge +
469 		    init_attr->cap.max_send_wr +
470 		    init_attr->cap.max_recv_sge +
471 		    init_attr->cap.max_recv_wr == 0)
472 			return ERR_PTR(-EINVAL);
473 	}
474 
475 	switch (init_attr->qp_type) {
476 	case IB_QPT_SMI:
477 	case IB_QPT_GSI:
478 		if (init_attr->port_num == 0 ||
479 		    init_attr->port_num > ibpd->device->phys_port_cnt)
480 			return ERR_PTR(-EINVAL);
481 	case IB_QPT_UC:
482 	case IB_QPT_RC:
483 	case IB_QPT_UD:
484 		sz = sizeof(struct rvt_sge) *
485 			init_attr->cap.max_send_sge +
486 			sizeof(struct rvt_swqe);
487 		if (gfp == GFP_NOIO)
488 			swq = __vmalloc(
489 				(init_attr->cap.max_send_wr + 1) * sz,
490 				gfp, PAGE_KERNEL);
491 		else
492 			swq = vmalloc(
493 				(init_attr->cap.max_send_wr + 1) * sz);
494 		if (!swq)
495 			return ERR_PTR(-ENOMEM);
496 
497 		sz = sizeof(*qp);
498 		sg_list_sz = 0;
499 		if (init_attr->srq) {
500 			struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
501 
502 			if (srq->rq.max_sge > 1)
503 				sg_list_sz = sizeof(*qp->r_sg_list) *
504 					(srq->rq.max_sge - 1);
505 		} else if (init_attr->cap.max_recv_sge > 1)
506 			sg_list_sz = sizeof(*qp->r_sg_list) *
507 				(init_attr->cap.max_recv_sge - 1);
508 		qp = kzalloc(sz + sg_list_sz, gfp);
509 		if (!qp)
510 			goto bail_swq;
511 
512 		RCU_INIT_POINTER(qp->next, NULL);
513 
514 		/*
515 		 * Driver needs to set up it's private QP structure and do any
516 		 * initialization that is needed.
517 		 */
518 		priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
519 		if (!priv)
520 			goto bail_qp;
521 		qp->priv = priv;
522 		qp->timeout_jiffies =
523 			usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
524 				1000UL);
525 		if (init_attr->srq) {
526 			sz = 0;
527 		} else {
528 			qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
529 			qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
530 			sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
531 				sizeof(struct rvt_rwqe);
532 			if (udata)
533 				qp->r_rq.wq = vmalloc_user(
534 						sizeof(struct rvt_rwq) +
535 						qp->r_rq.size * sz);
536 			else if (gfp == GFP_NOIO)
537 				qp->r_rq.wq = __vmalloc(
538 						sizeof(struct rvt_rwq) +
539 						qp->r_rq.size * sz,
540 						gfp, PAGE_KERNEL);
541 			else
542 				qp->r_rq.wq = vmalloc(
543 						sizeof(struct rvt_rwq) +
544 						qp->r_rq.size * sz);
545 			if (!qp->r_rq.wq)
546 				goto bail_driver_priv;
547 		}
548 
549 		/*
550 		 * ib_create_qp() will initialize qp->ibqp
551 		 * except for qp->ibqp.qp_num.
552 		 */
553 		spin_lock_init(&qp->r_lock);
554 		spin_lock_init(&qp->s_lock);
555 		spin_lock_init(&qp->r_rq.lock);
556 		atomic_set(&qp->refcount, 0);
557 		init_waitqueue_head(&qp->wait);
558 		init_timer(&qp->s_timer);
559 		qp->s_timer.data = (unsigned long)qp;
560 		INIT_LIST_HEAD(&qp->rspwait);
561 		qp->state = IB_QPS_RESET;
562 		qp->s_wq = swq;
563 		qp->s_size = init_attr->cap.max_send_wr + 1;
564 		qp->s_max_sge = init_attr->cap.max_send_sge;
565 		if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
566 			qp->s_flags = RVT_S_SIGNAL_REQ_WR;
567 
568 		err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
569 				init_attr->qp_type,
570 				init_attr->port_num, gfp);
571 		if (err < 0) {
572 			ret = ERR_PTR(err);
573 			goto bail_rq_wq;
574 		}
575 		qp->ibqp.qp_num = err;
576 		qp->port_num = init_attr->port_num;
577 		rvt_reset_qp(rdi, qp, init_attr->qp_type);
578 		break;
579 
580 	default:
581 		/* Don't support raw QPs */
582 		return ERR_PTR(-EINVAL);
583 	}
584 
585 	init_attr->cap.max_inline_data = 0;
586 
587 	/*
588 	 * Return the address of the RWQ as the offset to mmap.
589 	 * See hfi1_mmap() for details.
590 	 */
591 	if (udata && udata->outlen >= sizeof(__u64)) {
592 		if (!qp->r_rq.wq) {
593 			__u64 offset = 0;
594 
595 			err = ib_copy_to_udata(udata, &offset,
596 					       sizeof(offset));
597 			if (err) {
598 				ret = ERR_PTR(err);
599 				goto bail_qpn;
600 			}
601 		} else {
602 			u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
603 
604 			qp->ip = rvt_create_mmap_info(rdi, s,
605 						      ibpd->uobject->context,
606 						      qp->r_rq.wq);
607 			if (!qp->ip) {
608 				ret = ERR_PTR(-ENOMEM);
609 				goto bail_qpn;
610 			}
611 
612 			err = ib_copy_to_udata(udata, &qp->ip->offset,
613 					       sizeof(qp->ip->offset));
614 			if (err) {
615 				ret = ERR_PTR(err);
616 				goto bail_ip;
617 			}
618 		}
619 	}
620 
621 	spin_lock(&rdi->n_qps_lock);
622 	if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
623 		spin_unlock(&rdi->n_qps_lock);
624 		ret = ERR_PTR(-ENOMEM);
625 		goto bail_ip;
626 	}
627 
628 	rdi->n_qps_allocated++;
629 	spin_unlock(&rdi->n_qps_lock);
630 
631 	if (qp->ip) {
632 		spin_lock_irq(&rdi->pending_lock);
633 		list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
634 		spin_unlock_irq(&rdi->pending_lock);
635 	}
636 
637 	ret = &qp->ibqp;
638 
639 	/*
640 	 * We have our QP and its good, now keep track of what types of opcodes
641 	 * can be processed on this QP. We do this by keeping track of what the
642 	 * 3 high order bits of the opcode are.
643 	 */
644 	switch (init_attr->qp_type) {
645 	case IB_QPT_SMI:
646 	case IB_QPT_GSI:
647 	case IB_QPT_UD:
648 		qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
649 		break;
650 	case IB_QPT_RC:
651 		qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
652 		break;
653 	case IB_QPT_UC:
654 		qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
655 		break;
656 	default:
657 		ret = ERR_PTR(-EINVAL);
658 		goto bail_ip;
659 	}
660 
661 	return ret;
662 
663 bail_ip:
664 	kref_put(&qp->ip->ref, rvt_release_mmap_info);
665 
666 bail_qpn:
667 	free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
668 
669 bail_rq_wq:
670 	vfree(qp->r_rq.wq);
671 
672 bail_driver_priv:
673 	rdi->driver_f.qp_priv_free(rdi, qp);
674 
675 bail_qp:
676 	kfree(qp);
677 
678 bail_swq:
679 	vfree(swq);
680 
681 	return ret;
682 }
683 
684 /**
685  * qib_modify_qp - modify the attributes of a queue pair
686  * @ibqp: the queue pair who's attributes we're modifying
687  * @attr: the new attributes
688  * @attr_mask: the mask of attributes to modify
689  * @udata: user data for libibverbs.so
690  *
691  * Returns 0 on success, otherwise returns an errno.
692  */
693 int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
694 		  int attr_mask, struct ib_udata *udata)
695 {
696 	/*
697 	 * VT-DRIVER-API: qp_mtu()
698 	 * OPA devices have a per VL MTU the driver has a mapping of IB SL to SC
699 	 * to VL and the mapping table of MTUs per VL. This is not something
700 	 * that IB has and should not live in the rvt.
701 	 */
702 	return -EOPNOTSUPP;
703 }
704 
705 /**
706  * rvt_destroy_qp - destroy a queue pair
707  * @ibqp: the queue pair to destroy
708  *
709  * Returns 0 on success.
710  *
711  * Note that this can be called while the QP is actively sending or
712  * receiving!
713  */
714 int rvt_destroy_qp(struct ib_qp *ibqp)
715 {
716 	/*
717 	 * VT-DRIVER-API: qp_flush()
718 	 * Driver provies a mechanism to flush and wait for that flush to
719 	 * finish.
720 	 */
721 
722 	return -EOPNOTSUPP;
723 }
724 
725 int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
726 		 int attr_mask, struct ib_qp_init_attr *init_attr)
727 {
728 	return -EOPNOTSUPP;
729 }
730 
731 /**
732  * rvt_post_receive - post a receive on a QP
733  * @ibqp: the QP to post the receive on
734  * @wr: the WR to post
735  * @bad_wr: the first bad WR is put here
736  *
737  * This may be called from interrupt context.
738  */
739 int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
740 		  struct ib_recv_wr **bad_wr)
741 {
742 	/*
743 	 * When a packet arrives the driver needs to call up to rvt to process
744 	 * the packet. The UD, RC, UC processing will be done in rvt, however
745 	 * the driver should be able to override this if it so choses. Perhaps a
746 	 * set of function pointers set up at registration time.
747 	 */
748 
749 	return -EOPNOTSUPP;
750 }
751 
752 /**
753  * rvt_post_send - post a send on a QP
754  * @ibqp: the QP to post the send on
755  * @wr: the list of work requests to post
756  * @bad_wr: the first bad WR is put here
757  *
758  * This may be called from interrupt context.
759  */
760 int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
761 		  struct ib_send_wr **bad_wr)
762 {
763 	/*
764 	 * VT-DRIVER-API: do_send()
765 	 * Driver needs to have a do_send() call which is a single entry point
766 	 * to take an already formed packet and throw it out on the wire. Once
767 	 * the packet is sent the driver needs to make an upcall to rvt so the
768 	 * completion queue can be notified and/or any other outstanding
769 	 * work/book keeping can be finished.
770 	 *
771 	 * Note that there should also be a way for rvt to protect itself
772 	 * against hangs in the driver layer. If a send doesn't actually
773 	 * complete in a timely manor rvt needs to return an error event.
774 	 */
775 
776 	return -EOPNOTSUPP;
777 }
778 
779 /**
780  * rvt_post_srq_receive - post a receive on a shared receive queue
781  * @ibsrq: the SRQ to post the receive on
782  * @wr: the list of work requests to post
783  * @bad_wr: A pointer to the first WR to cause a problem is put here
784  *
785  * This may be called from interrupt context.
786  */
787 int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
788 		      struct ib_recv_wr **bad_wr)
789 {
790 	return -EOPNOTSUPP;
791 }
792