qib_qp.c 35.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2012, 2013 Intel Corporation.  All rights reserved.
3
 * Copyright (c) 2006 - 2012 QLogic Corporation.  * All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/err.h>
#include <linux/vmalloc.h>
M
Mike Marciniszyn 已提交
37
#include <linux/jhash.h>
38
#include <rdma/rdma_vt.h>
39 40 41
#ifdef CONFIG_DEBUG_FS
#include <linux/seq_file.h>
#endif
42 43 44

#include "qib.h"

45 46 47 48 49 50 51
/*
 * mask field which was present in now deleted qib_qpn_table
 * is not present in rvt_qpn_table. Defining the same field
 * as qpt_mask here instead of adding the mask field to
 * rvt_qpn_table.
 */
static u16 qpt_mask;
52

53 54
static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
			      struct rvt_qpn_map *map, unsigned off)
55
{
56
	return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
57 58
}

59 60
static inline unsigned find_next_offset(struct rvt_qpn_table *qpt,
					struct rvt_qpn_map *map, unsigned off,
61
					unsigned n)
62
{
63
	if (qpt_mask) {
64
		off++;
65 66 67 68 69
		if (((off & qpt_mask) >> 1) >= n)
			off = (off | qpt_mask) + 2;
	} else {
		off = find_next_zero_bit(map->page, RVT_BITS_PER_PAGE, off);
	}
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	return off;
}

/*
 * Convert the AETH credit code into the number of credits.
 */
static u32 credit_table[31] = {
	0,                      /* 0 */
	1,                      /* 1 */
	2,                      /* 2 */
	3,                      /* 3 */
	4,                      /* 4 */
	6,                      /* 5 */
	8,                      /* 6 */
	12,                     /* 7 */
	16,                     /* 8 */
	24,                     /* 9 */
	32,                     /* A */
	48,                     /* B */
	64,                     /* C */
	96,                     /* D */
	128,                    /* E */
	192,                    /* F */
	256,                    /* 10 */
	384,                    /* 11 */
	512,                    /* 12 */
	768,                    /* 13 */
	1024,                   /* 14 */
	1536,                   /* 15 */
	2048,                   /* 16 */
	3072,                   /* 17 */
	4096,                   /* 18 */
	6144,                   /* 19 */
	8192,                   /* 1A */
	12288,                  /* 1B */
	16384,                  /* 1C */
	24576,                  /* 1D */
	32768                   /* 1E */
};

110
static void get_map_page(struct rvt_qpn_table *qpt, struct rvt_qpn_map *map,
111
			 gfp_t gfp)
112
{
113
	unsigned long page = get_zeroed_page(gfp);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

	/*
	 * Free the page if someone raced with us installing it.
	 */

	spin_lock(&qpt->lock);
	if (map->page)
		free_page(page);
	else
		map->page = (void *)page;
	spin_unlock(&qpt->lock);
}

/*
 * Allocate the next available QPN or
 * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
 */
131
static int alloc_qpn(struct qib_devdata *dd, struct rvt_qpn_table *qpt,
132
		     enum ib_qp_type type, u8 port, gfp_t gfp)
133 134
{
	u32 i, offset, max_scan, qpn;
135
	struct rvt_qpn_map *map;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	u32 ret;

	if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
		unsigned n;

		ret = type == IB_QPT_GSI;
		n = 1 << (ret + 2 * (port - 1));
		spin_lock(&qpt->lock);
		if (qpt->flags & n)
			ret = -EINVAL;
		else
			qpt->flags |= n;
		spin_unlock(&qpt->lock);
		goto bail;
	}

M
Mike Marciniszyn 已提交
152
	qpn = qpt->last + 2;
153
	if (qpn >= RVT_QPN_MAX)
154
		qpn = 2;
155 156 157 158
	if (qpt_mask && ((qpn & qpt_mask) >> 1) >= dd->n_krcv_queues)
		qpn = (qpn | qpt_mask) + 2;
	offset = qpn & RVT_BITS_PER_PAGE_MASK;
	map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
159 160 161
	max_scan = qpt->nmaps - !offset;
	for (i = 0;;) {
		if (unlikely(!map->page)) {
162
			get_map_page(qpt, map, gfp);
163 164 165 166 167 168 169 170 171
			if (unlikely(!map->page))
				break;
		}
		do {
			if (!test_and_set_bit(offset, map->page)) {
				qpt->last = qpn;
				ret = qpn;
				goto bail;
			}
172 173
			offset = find_next_offset(qpt, map, offset,
				dd->n_krcv_queues);
174 175 176 177 178 179 180 181 182
			qpn = mk_qpn(qpt, map, offset);
			/*
			 * This test differs from alloc_pidmap().
			 * If find_next_offset() does find a zero
			 * bit, we don't need to check for QPN
			 * wrapping around past our starting QPN.
			 * We just need to be sure we don't loop
			 * forever.
			 */
183
		} while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
184 185 186 187 188 189
		/*
		 * In order to keep the number of pages allocated to a
		 * minimum, we scan the all existing pages before increasing
		 * the size of the bitmap table.
		 */
		if (++i > max_scan) {
190
			if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
191 192
				break;
			map = &qpt->map[qpt->nmaps++];
193
			offset = 0;
194 195
		} else if (map < &qpt->map[qpt->nmaps]) {
			++map;
196
			offset = 0;
197 198
		} else {
			map = &qpt->map[0];
199
			offset = 2;
200 201 202 203 204 205 206 207 208 209
		}
		qpn = mk_qpn(qpt, map, offset);
	}

	ret = -ENOMEM;

bail:
	return ret;
}

210
static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
211
{
212
	struct rvt_qpn_map *map;
213

214
	map = qpt->map + qpn / RVT_BITS_PER_PAGE;
215
	if (map->page)
216
		clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
217 218
}

M
Mike Marciniszyn 已提交
219 220 221
static inline unsigned qpn_hash(struct qib_ibdev *dev, u32 qpn)
{
	return jhash_1word(qpn, dev->qp_rnd) &
222
		(dev->rdi.qp_dev->qp_table_size - 1);
M
Mike Marciniszyn 已提交
223 224 225
}


226 227 228 229
/*
 * Put the QP into the hash table.
 * The hash table holds a reference to the QP.
 */
230
static void insert_qp(struct qib_ibdev *dev, struct rvt_qp *qp)
231 232 233
{
	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
	unsigned long flags;
M
Mike Marciniszyn 已提交
234
	unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
235

M
Mike Marciniszyn 已提交
236
	atomic_inc(&qp->refcount);
237
	spin_lock_irqsave(&dev->rdi.qp_dev->qpt_lock, flags);
238 239

	if (qp->ibqp.qp_num == 0)
240
		rcu_assign_pointer(ibp->rvp.qp[0], qp);
241
	else if (qp->ibqp.qp_num == 1)
242
		rcu_assign_pointer(ibp->rvp.qp[1], qp);
243
	else {
244 245
		qp->next = dev->rdi.qp_dev->qp_table[n];
		rcu_assign_pointer(dev->rdi.qp_dev->qp_table[n], qp);
246 247
	}

248
	spin_unlock_irqrestore(&dev->rdi.qp_dev->qpt_lock, flags);
249 250 251 252 253 254
}

/*
 * Remove the QP from the table so it can't be found asynchronously by
 * the receive interrupt routine.
 */
255
static void remove_qp(struct qib_ibdev *dev, struct rvt_qp *qp)
256 257
{
	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
M
Mike Marciniszyn 已提交
258
	unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
259
	unsigned long flags;
260
	int removed = 1;
261
	spinlock_t *qpt_lock_ptr; /* Pointer to make checkpatch happy */
262

263
	spin_lock_irqsave(&dev->rdi.qp_dev->qpt_lock, flags);
264

265
	qpt_lock_ptr = &dev->rdi.qp_dev->qpt_lock;
266
	if (rcu_dereference_protected(ibp->rvp.qp[0],
267
				      lockdep_is_held(qpt_lock_ptr)) == qp) {
268 269
		RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
	} else if (rcu_dereference_protected(ibp->rvp.qp[1],
270
			lockdep_is_held(&dev->rdi.qp_dev->qpt_lock)) == qp) {
271
		RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
M
Mike Marciniszyn 已提交
272
	} else {
273 274
		struct rvt_qp *q;
		struct rvt_qp __rcu **qpp;
M
Mike Marciniszyn 已提交
275

276
		removed = 0;
277
		qpp = &dev->rdi.qp_dev->qp_table[n];
278
		for (; (q = rcu_dereference_protected(*qpp,
279
				lockdep_is_held(qpt_lock_ptr))) != NULL;
280
				qpp = &q->next)
281
			if (q == qp) {
282
				RCU_INIT_POINTER(*qpp,
283
					rcu_dereference_protected(qp->next,
284
					 lockdep_is_held(qpt_lock_ptr)));
285
				removed = 1;
286 287
				break;
			}
M
Mike Marciniszyn 已提交
288
	}
289

290
	spin_unlock_irqrestore(&dev->rdi.qp_dev->qpt_lock, flags);
291 292 293 294
	if (removed) {
		synchronize_rcu();
		atomic_dec(&qp->refcount);
	}
295 296 297 298 299 300 301 302 303 304 305 306 307
}

/**
 * qib_free_all_qps - check for QPs still in use
 * @qpt: the QP table to empty
 *
 * There should not be any QPs still in use.
 * Free memory for table.
 */
unsigned qib_free_all_qps(struct qib_devdata *dd)
{
	struct qib_ibdev *dev = &dd->verbs_dev;
	unsigned long flags;
308
	struct rvt_qp *qp;
309
	unsigned n, qp_inuse = 0;
310
	spinlock_t *qpt_lock_ptr; /* Pointer to make checkpatch happy */
311 312 313 314 315 316

	for (n = 0; n < dd->num_pports; n++) {
		struct qib_ibport *ibp = &dd->pport[n].ibport_data;

		if (!qib_mcast_tree_empty(ibp))
			qp_inuse++;
M
Mike Marciniszyn 已提交
317
		rcu_read_lock();
318
		if (rcu_dereference(ibp->rvp.qp[0]))
319
			qp_inuse++;
320
		if (rcu_dereference(ibp->rvp.qp[1]))
321
			qp_inuse++;
M
Mike Marciniszyn 已提交
322
		rcu_read_unlock();
323 324
	}

325 326 327 328 329 330
	spin_lock_irqsave(&dev->rdi.qp_dev->qpt_lock, flags);
	qpt_lock_ptr = &dev->rdi.qp_dev->qpt_lock;
	for (n = 0; n < dev->rdi.qp_dev->qp_table_size; n++) {
		qp = rcu_dereference_protected(dev->rdi.qp_dev->qp_table[n],
					       lockdep_is_held(qpt_lock_ptr));
		RCU_INIT_POINTER(dev->rdi.qp_dev->qp_table[n], NULL);
331

332
		for (; qp; qp = rcu_dereference_protected(qp->next,
333
					lockdep_is_held(qpt_lock_ptr)))
334 335
			qp_inuse++;
	}
336
	spin_unlock_irqrestore(&dev->rdi.qp_dev->qpt_lock, flags);
M
Mike Marciniszyn 已提交
337
	synchronize_rcu();
338 339 340 341 342 343 344 345 346 347 348 349

	return qp_inuse;
}

/**
 * qib_lookup_qpn - return the QP with the given QPN
 * @qpt: the QP table
 * @qpn: the QP number to look up
 *
 * The caller is responsible for decrementing the QP reference count
 * when done.
 */
350
struct rvt_qp *qib_lookup_qpn(struct qib_ibport *ibp, u32 qpn)
351
{
352
	struct rvt_qp *qp = NULL;
353

354
	rcu_read_lock();
M
Mike Marciniszyn 已提交
355 356
	if (unlikely(qpn <= 1)) {
		if (qpn == 0)
357
			qp = rcu_dereference(ibp->rvp.qp[0]);
M
Mike Marciniszyn 已提交
358
		else
359
			qp = rcu_dereference(ibp->rvp.qp[1]);
360 361
		if (qp)
			atomic_inc(&qp->refcount);
M
Mike Marciniszyn 已提交
362 363 364
	} else {
		struct qib_ibdev *dev = &ppd_from_ibp(ibp)->dd->verbs_dev;
		unsigned n = qpn_hash(dev, qpn);
365

366
		for (qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]); qp;
367
			qp = rcu_dereference(qp->next))
368 369
			if (qp->ibqp.qp_num == qpn) {
				atomic_inc(&qp->refcount);
370
				break;
371
			}
M
Mike Marciniszyn 已提交
372 373
	}
	rcu_read_unlock();
374 375 376 377 378 379 380 381
	return qp;
}

/**
 * qib_reset_qp - initialize the QP state to the reset state
 * @qp: the QP to reset
 * @type: the QP type
 */
382
static void qib_reset_qp(struct rvt_qp *qp, enum ib_qp_type type)
383
{
384
	struct qib_qp_priv *priv = qp->priv;
385 386 387
	qp->remote_qpn = 0;
	qp->qkey = 0;
	qp->qp_access_flags = 0;
388
	atomic_set(&priv->s_dma_busy, 0);
389
	qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	qp->s_hdrwords = 0;
	qp->s_wqe = NULL;
	qp->s_draining = 0;
	qp->s_next_psn = 0;
	qp->s_last_psn = 0;
	qp->s_sending_psn = 0;
	qp->s_sending_hpsn = 0;
	qp->s_psn = 0;
	qp->r_psn = 0;
	qp->r_msn = 0;
	if (type == IB_QPT_RC) {
		qp->s_state = IB_OPCODE_RC_SEND_LAST;
		qp->r_state = IB_OPCODE_RC_SEND_LAST;
	} else {
		qp->s_state = IB_OPCODE_UC_SEND_LAST;
		qp->r_state = IB_OPCODE_UC_SEND_LAST;
	}
	qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
	qp->r_nak_state = 0;
	qp->r_aflags = 0;
	qp->r_flags = 0;
	qp->s_head = 0;
	qp->s_tail = 0;
	qp->s_cur = 0;
	qp->s_acked = 0;
	qp->s_last = 0;
	qp->s_ssn = 1;
	qp->s_lsn = 0;
	qp->s_mig_state = IB_MIG_MIGRATED;
	memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
	qp->r_head_ack_queue = 0;
	qp->s_tail_ack_queue = 0;
	qp->s_num_rd_atomic = 0;
	if (qp->r_rq.wq) {
		qp->r_rq.wq->head = 0;
		qp->r_rq.wq->tail = 0;
	}
	qp->r_sge.num_sge = 0;
}

430
static void clear_mr_refs(struct rvt_qp *qp, int clr_sends)
431 432 433
{
	unsigned n;

434
	if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
435
		qib_put_ss(&qp->s_rdma_read_sge);
436

437
	qib_put_ss(&qp->r_sge);
438 439 440

	if (clr_sends) {
		while (qp->s_last != qp->s_head) {
441
			struct rvt_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
442 443 444
			unsigned i;

			for (i = 0; i < wqe->wr.num_sge; i++) {
445
				struct rvt_sge *sge = &wqe->sg_list[i];
446

447
				rvt_put_mr(sge->mr);
448 449 450 451
			}
			if (qp->ibqp.qp_type == IB_QPT_UD ||
			    qp->ibqp.qp_type == IB_QPT_SMI ||
			    qp->ibqp.qp_type == IB_QPT_GSI)
452 453
				atomic_dec(
				 &ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
454 455 456 457
			if (++qp->s_last >= qp->s_size)
				qp->s_last = 0;
		}
		if (qp->s_rdma_mr) {
458
			rvt_put_mr(qp->s_rdma_mr);
459 460 461 462 463 464 465 466
			qp->s_rdma_mr = NULL;
		}
	}

	if (qp->ibqp.qp_type != IB_QPT_RC)
		return;

	for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
467
		struct rvt_ack_entry *e = &qp->s_ack_queue[n];
468 469 470

		if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
		    e->rdma_sge.mr) {
471
			rvt_put_mr(e->rdma_sge.mr);
472 473 474 475 476 477 478 479 480 481 482 483
			e->rdma_sge.mr = NULL;
		}
	}
}

/**
 * qib_error_qp - put a QP into the error state
 * @qp: the QP to put into the error state
 * @err: the receive completion error to signal if a RWQE is active
 *
 * Flushes both send and receive work queues.
 * Returns true if last WQE event should be generated.
484
 * The QP r_lock and s_lock should be held and interrupts disabled.
485 486
 * If we are already in error state, just return.
 */
487
int qib_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
488
{
489
	struct qib_qp_priv *priv = qp->priv;
490 491 492 493 494 495 496 497 498
	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
	struct ib_wc wc;
	int ret = 0;

	if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
		goto bail;

	qp->state = IB_QPS_ERR;

499 500
	if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
		qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
501 502
		del_timer(&qp->s_timer);
	}
503

504 505
	if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
		qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
506

H
Harish Chegondi 已提交
507
	spin_lock(&dev->rdi.pending_lock);
508 509
	if (!list_empty(&priv->iowait) && !(qp->s_flags & RVT_S_BUSY)) {
		qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
510
		list_del_init(&priv->iowait);
511
	}
H
Harish Chegondi 已提交
512
	spin_unlock(&dev->rdi.pending_lock);
513

514
	if (!(qp->s_flags & RVT_S_BUSY)) {
515 516
		qp->s_hdrwords = 0;
		if (qp->s_rdma_mr) {
517
			rvt_put_mr(qp->s_rdma_mr);
518 519
			qp->s_rdma_mr = NULL;
		}
520 521 522
		if (priv->s_tx) {
			qib_put_txreq(priv->s_tx);
			priv->s_tx = NULL;
523 524 525 526 527 528 529 530 531 532 533 534 535
		}
	}

	/* Schedule the sending tasklet to drain the send work queue. */
	if (qp->s_last != qp->s_head)
		qib_schedule_send(qp);

	clear_mr_refs(qp, 0);

	memset(&wc, 0, sizeof(wc));
	wc.qp = &qp->ibqp;
	wc.opcode = IB_WC_RECV;

536
	if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
537 538 539 540 541 542 543
		wc.wr_id = qp->r_wr_id;
		wc.status = err;
		qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
	}
	wc.status = IB_WC_WR_FLUSH_ERR;

	if (qp->r_rq.wq) {
544
		struct rvt_rwq *wq;
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		u32 head;
		u32 tail;

		spin_lock(&qp->r_rq.lock);

		/* sanity check pointers before trusting them */
		wq = qp->r_rq.wq;
		head = wq->head;
		if (head >= qp->r_rq.size)
			head = 0;
		tail = wq->tail;
		if (tail >= qp->r_rq.size)
			tail = 0;
		while (tail != head) {
			wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
			if (++tail >= qp->r_rq.size)
				tail = 0;
			qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
		}
		wq->tail = tail;

		spin_unlock(&qp->r_rq.lock);
	} else if (qp->ibqp.event_handler)
		ret = 1;

bail:
	return ret;
}

/**
 * qib_modify_qp - modify the attributes of a queue pair
 * @ibqp: the queue pair who's attributes we're modifying
 * @attr: the new attributes
 * @attr_mask: the mask of attributes to modify
 * @udata: user data for libibverbs.so
 *
 * Returns 0 on success, otherwise returns an errno.
 */
int qib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
		  int attr_mask, struct ib_udata *udata)
{
	struct qib_ibdev *dev = to_idev(ibqp->device);
587
	struct rvt_qp *qp = to_iqp(ibqp);
588
	struct qib_qp_priv *priv = qp->priv;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	enum ib_qp_state cur_state, new_state;
	struct ib_event ev;
	int lastwqe = 0;
	int mig = 0;
	int ret;
	u32 pmtu = 0; /* for gcc warning only */

	spin_lock_irq(&qp->r_lock);
	spin_lock(&qp->s_lock);

	cur_state = attr_mask & IB_QP_CUR_STATE ?
		attr->cur_qp_state : qp->state;
	new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;

	if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
604
				attr_mask, IB_LINK_LAYER_UNSPECIFIED))
605 606 607
		goto inval;

	if (attr_mask & IB_QP_AV) {
608
		if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
609
			goto inval;
610
		if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
611 612 613 614
			goto inval;
	}

	if (attr_mask & IB_QP_ALT_PATH) {
615 616
		if (attr->alt_ah_attr.dlid >=
		    be16_to_cpu(IB_MULTICAST_LID_BASE))
617
			goto inval;
618
		if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
			goto inval;
		if (attr->alt_pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
			goto inval;
	}

	if (attr_mask & IB_QP_PKEY_INDEX)
		if (attr->pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
			goto inval;

	if (attr_mask & IB_QP_MIN_RNR_TIMER)
		if (attr->min_rnr_timer > 31)
			goto inval;

	if (attr_mask & IB_QP_PORT)
		if (qp->ibqp.qp_type == IB_QPT_SMI ||
		    qp->ibqp.qp_type == IB_QPT_GSI ||
		    attr->port_num == 0 ||
		    attr->port_num > ibqp->device->phys_port_cnt)
			goto inval;

	if (attr_mask & IB_QP_DEST_QPN)
		if (attr->dest_qp_num > QIB_QPN_MASK)
			goto inval;

	if (attr_mask & IB_QP_RETRY_CNT)
		if (attr->retry_cnt > 7)
			goto inval;

	if (attr_mask & IB_QP_RNR_RETRY)
		if (attr->rnr_retry > 7)
			goto inval;

	/*
	 * Don't allow invalid path_mtu values.  OK to set greater
	 * than the active mtu (or even the max_cap, if we have tuned
	 * that to a small mtu.  We'll set qp->path_mtu
	 * to the lesser of requested attribute mtu and active,
	 * for packetizing messages.
	 * Note that the QP port has to be set in INIT and MTU in RTR.
	 */
	if (attr_mask & IB_QP_PATH_MTU) {
		struct qib_devdata *dd = dd_from_dev(dev);
		int mtu, pidx = qp->port_num - 1;

		mtu = ib_mtu_enum_to_int(attr->path_mtu);
		if (mtu == -1)
			goto inval;
		if (mtu > dd->pport[pidx].ibmtu) {
			switch (dd->pport[pidx].ibmtu) {
			case 4096:
				pmtu = IB_MTU_4096;
				break;
			case 2048:
				pmtu = IB_MTU_2048;
				break;
			case 1024:
				pmtu = IB_MTU_1024;
				break;
			case 512:
				pmtu = IB_MTU_512;
				break;
			case 256:
				pmtu = IB_MTU_256;
				break;
			default:
				pmtu = IB_MTU_2048;
			}
		} else
			pmtu = attr->path_mtu;
	}

	if (attr_mask & IB_QP_PATH_MIG_STATE) {
		if (attr->path_mig_state == IB_MIG_REARM) {
			if (qp->s_mig_state == IB_MIG_ARMED)
				goto inval;
			if (new_state != IB_QPS_RTS)
				goto inval;
		} else if (attr->path_mig_state == IB_MIG_MIGRATED) {
			if (qp->s_mig_state == IB_MIG_REARM)
				goto inval;
			if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
				goto inval;
			if (qp->s_mig_state == IB_MIG_ARMED)
				mig = 1;
		} else
			goto inval;
	}

	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
		if (attr->max_dest_rd_atomic > QIB_MAX_RDMA_ATOMIC)
			goto inval;

	switch (new_state) {
	case IB_QPS_RESET:
		if (qp->state != IB_QPS_RESET) {
			qp->state = IB_QPS_RESET;
H
Harish Chegondi 已提交
715
			spin_lock(&dev->rdi.pending_lock);
716 717
			if (!list_empty(&priv->iowait))
				list_del_init(&priv->iowait);
H
Harish Chegondi 已提交
718
			spin_unlock(&dev->rdi.pending_lock);
719
			qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
720 721 722
			spin_unlock(&qp->s_lock);
			spin_unlock_irq(&qp->r_lock);
			/* Stop the sending work queue and retry timer */
723
			cancel_work_sync(&priv->s_work);
724
			del_timer_sync(&qp->s_timer);
725 726 727 728 729
			wait_event(priv->wait_dma,
				   !atomic_read(&priv->s_dma_busy));
			if (priv->s_tx) {
				qib_put_txreq(priv->s_tx);
				priv->s_tx = NULL;
730 731 732 733 734 735 736 737 738 739 740 741
			}
			remove_qp(dev, qp);
			wait_event(qp->wait, !atomic_read(&qp->refcount));
			spin_lock_irq(&qp->r_lock);
			spin_lock(&qp->s_lock);
			clear_mr_refs(qp, 1);
			qib_reset_qp(qp, ibqp->qp_type);
		}
		break;

	case IB_QPS_RTR:
		/* Allow event to retrigger if QP set to RTR more than once */
742
		qp->r_flags &= ~RVT_R_COMM_EST;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
		qp->state = new_state;
		break;

	case IB_QPS_SQD:
		qp->s_draining = qp->s_last != qp->s_cur;
		qp->state = new_state;
		break;

	case IB_QPS_SQE:
		if (qp->ibqp.qp_type == IB_QPT_RC)
			goto inval;
		qp->state = new_state;
		break;

	case IB_QPS_ERR:
		lastwqe = qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
		break;

	default:
		qp->state = new_state;
		break;
	}

	if (attr_mask & IB_QP_PKEY_INDEX)
		qp->s_pkey_index = attr->pkey_index;

	if (attr_mask & IB_QP_PORT)
		qp->port_num = attr->port_num;

	if (attr_mask & IB_QP_DEST_QPN)
		qp->remote_qpn = attr->dest_qp_num;

	if (attr_mask & IB_QP_SQ_PSN) {
		qp->s_next_psn = attr->sq_psn & QIB_PSN_MASK;
		qp->s_psn = qp->s_next_psn;
		qp->s_sending_psn = qp->s_next_psn;
		qp->s_last_psn = qp->s_next_psn - 1;
		qp->s_sending_hpsn = qp->s_last_psn;
	}

	if (attr_mask & IB_QP_RQ_PSN)
		qp->r_psn = attr->rq_psn & QIB_PSN_MASK;

	if (attr_mask & IB_QP_ACCESS_FLAGS)
		qp->qp_access_flags = attr->qp_access_flags;

	if (attr_mask & IB_QP_AV) {
		qp->remote_ah_attr = attr->ah_attr;
		qp->s_srate = attr->ah_attr.static_rate;
	}

	if (attr_mask & IB_QP_ALT_PATH) {
		qp->alt_ah_attr = attr->alt_ah_attr;
		qp->s_alt_pkey_index = attr->alt_pkey_index;
	}

	if (attr_mask & IB_QP_PATH_MIG_STATE) {
		qp->s_mig_state = attr->path_mig_state;
		if (mig) {
			qp->remote_ah_attr = qp->alt_ah_attr;
			qp->port_num = qp->alt_ah_attr.port_num;
			qp->s_pkey_index = qp->s_alt_pkey_index;
		}
	}

808
	if (attr_mask & IB_QP_PATH_MTU) {
809
		qp->path_mtu = pmtu;
810 811
		qp->pmtu = ib_mtu_enum_to_int(pmtu);
	}
812 813 814 815 816 817 818 819 820 821 822 823 824 825

	if (attr_mask & IB_QP_RETRY_CNT) {
		qp->s_retry_cnt = attr->retry_cnt;
		qp->s_retry = attr->retry_cnt;
	}

	if (attr_mask & IB_QP_RNR_RETRY) {
		qp->s_rnr_retry_cnt = attr->rnr_retry;
		qp->s_rnr_retry = attr->rnr_retry;
	}

	if (attr_mask & IB_QP_MIN_RNR_TIMER)
		qp->r_min_rnr_timer = attr->min_rnr_timer;

826
	if (attr_mask & IB_QP_TIMEOUT) {
827
		qp->timeout = attr->timeout;
828 829 830 831
		qp->timeout_jiffies =
			usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
				1000UL);
	}
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

	if (attr_mask & IB_QP_QKEY)
		qp->qkey = attr->qkey;

	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
		qp->r_max_rd_atomic = attr->max_dest_rd_atomic;

	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
		qp->s_max_rd_atomic = attr->max_rd_atomic;

	spin_unlock(&qp->s_lock);
	spin_unlock_irq(&qp->r_lock);

	if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
		insert_qp(dev, qp);

	if (lastwqe) {
		ev.device = qp->ibqp.device;
		ev.element.qp = &qp->ibqp;
		ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
		qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
	}
	if (mig) {
		ev.device = qp->ibqp.device;
		ev.element.qp = &qp->ibqp;
		ev.event = IB_EVENT_PATH_MIG;
		qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
	}
	ret = 0;
	goto bail;

inval:
	spin_unlock(&qp->s_lock);
	spin_unlock_irq(&qp->r_lock);
	ret = -EINVAL;

bail:
	return ret;
}

int qib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
		 int attr_mask, struct ib_qp_init_attr *init_attr)
{
875
	struct rvt_qp *qp = to_iqp(ibqp);
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912

	attr->qp_state = qp->state;
	attr->cur_qp_state = attr->qp_state;
	attr->path_mtu = qp->path_mtu;
	attr->path_mig_state = qp->s_mig_state;
	attr->qkey = qp->qkey;
	attr->rq_psn = qp->r_psn & QIB_PSN_MASK;
	attr->sq_psn = qp->s_next_psn & QIB_PSN_MASK;
	attr->dest_qp_num = qp->remote_qpn;
	attr->qp_access_flags = qp->qp_access_flags;
	attr->cap.max_send_wr = qp->s_size - 1;
	attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
	attr->cap.max_send_sge = qp->s_max_sge;
	attr->cap.max_recv_sge = qp->r_rq.max_sge;
	attr->cap.max_inline_data = 0;
	attr->ah_attr = qp->remote_ah_attr;
	attr->alt_ah_attr = qp->alt_ah_attr;
	attr->pkey_index = qp->s_pkey_index;
	attr->alt_pkey_index = qp->s_alt_pkey_index;
	attr->en_sqd_async_notify = 0;
	attr->sq_draining = qp->s_draining;
	attr->max_rd_atomic = qp->s_max_rd_atomic;
	attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
	attr->min_rnr_timer = qp->r_min_rnr_timer;
	attr->port_num = qp->port_num;
	attr->timeout = qp->timeout;
	attr->retry_cnt = qp->s_retry_cnt;
	attr->rnr_retry = qp->s_rnr_retry_cnt;
	attr->alt_port_num = qp->alt_ah_attr.port_num;
	attr->alt_timeout = qp->alt_timeout;

	init_attr->event_handler = qp->ibqp.event_handler;
	init_attr->qp_context = qp->ibqp.qp_context;
	init_attr->send_cq = qp->ibqp.send_cq;
	init_attr->recv_cq = qp->ibqp.recv_cq;
	init_attr->srq = qp->ibqp.srq;
	init_attr->cap = attr->cap;
913
	if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
914 915 916 917 918 919 920 921 922 923 924 925 926 927
		init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
	else
		init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
	init_attr->qp_type = qp->ibqp.qp_type;
	init_attr->port_num = qp->port_num;
	return 0;
}

/**
 * qib_compute_aeth - compute the AETH (syndrome + MSN)
 * @qp: the queue pair to compute the AETH for
 *
 * Returns the AETH.
 */
928
__be32 qib_compute_aeth(struct rvt_qp *qp)
929 930 931 932 933 934 935 936 937 938 939 940
{
	u32 aeth = qp->r_msn & QIB_MSN_MASK;

	if (qp->ibqp.srq) {
		/*
		 * Shared receive queues don't generate credits.
		 * Set the credit field to the invalid value.
		 */
		aeth |= QIB_AETH_CREDIT_INVAL << QIB_AETH_CREDIT_SHIFT;
	} else {
		u32 min, max, x;
		u32 credits;
941
		struct rvt_rwq *wq = qp->r_rq.wq;
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
		u32 head;
		u32 tail;

		/* sanity check pointers before trusting them */
		head = wq->head;
		if (head >= qp->r_rq.size)
			head = 0;
		tail = wq->tail;
		if (tail >= qp->r_rq.size)
			tail = 0;
		/*
		 * Compute the number of credits available (RWQEs).
		 * XXX Not holding the r_rq.lock here so there is a small
		 * chance that the pair of reads are not atomic.
		 */
		credits = head - tail;
		if ((int)credits < 0)
			credits += qp->r_rq.size;
		/*
		 * Binary search the credit table to find the code to
		 * use.
		 */
		min = 0;
		max = 31;
		for (;;) {
			x = (min + max) / 2;
			if (credit_table[x] == credits)
				break;
			if (credit_table[x] > credits)
				max = x;
			else if (min == x)
				break;
			else
				min = x;
		}
		aeth |= x << QIB_AETH_CREDIT_SHIFT;
	}
	return cpu_to_be32(aeth);
}

/**
 * qib_create_qp - create a queue pair for a device
 * @ibpd: the protection domain who's device we create the queue pair for
 * @init_attr: the attributes of the queue pair
 * @udata: user data for libibverbs.so
 *
 * Returns the queue pair on success, otherwise returns an errno.
 *
 * Called by the ib_create_qp() core verbs function.
 */
struct ib_qp *qib_create_qp(struct ib_pd *ibpd,
			    struct ib_qp_init_attr *init_attr,
			    struct ib_udata *udata)
{
996
	struct rvt_qp *qp;
997
	int err;
998
	struct rvt_swqe *swq = NULL;
999 1000 1001 1002 1003
	struct qib_ibdev *dev;
	struct qib_devdata *dd;
	size_t sz;
	size_t sg_list_sz;
	struct ib_qp *ret;
1004
	gfp_t gfp;
1005
	struct qib_qp_priv *priv;
1006 1007

	if (init_attr->cap.max_send_sge > ib_qib_max_sges ||
1008
	    init_attr->cap.max_send_wr > ib_qib_max_qp_wrs ||
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	    init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
		return ERR_PTR(-EINVAL);

	/* GFP_NOIO is applicable in RC QPs only */
	if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
	    init_attr->qp_type != IB_QPT_RC)
		return ERR_PTR(-EINVAL);

	gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
			GFP_NOIO : GFP_KERNEL;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

	/* Check receive queue parameters if no SRQ is specified. */
	if (!init_attr->srq) {
		if (init_attr->cap.max_recv_sge > ib_qib_max_sges ||
		    init_attr->cap.max_recv_wr > ib_qib_max_qp_wrs) {
			ret = ERR_PTR(-EINVAL);
			goto bail;
		}
		if (init_attr->cap.max_send_sge +
		    init_attr->cap.max_send_wr +
		    init_attr->cap.max_recv_sge +
		    init_attr->cap.max_recv_wr == 0) {
			ret = ERR_PTR(-EINVAL);
			goto bail;
		}
	}

	switch (init_attr->qp_type) {
	case IB_QPT_SMI:
	case IB_QPT_GSI:
		if (init_attr->port_num == 0 ||
		    init_attr->port_num > ibpd->device->phys_port_cnt) {
			ret = ERR_PTR(-EINVAL);
			goto bail;
		}
	case IB_QPT_UC:
	case IB_QPT_RC:
	case IB_QPT_UD:
1047
		sz = sizeof(struct rvt_sge) *
1048
			init_attr->cap.max_send_sge +
1049
			sizeof(struct rvt_swqe);
1050 1051
		swq = __vmalloc((init_attr->cap.max_send_wr + 1) * sz,
				gfp, PAGE_KERNEL);
1052 1053 1054 1055 1056 1057 1058
		if (swq == NULL) {
			ret = ERR_PTR(-ENOMEM);
			goto bail;
		}
		sz = sizeof(*qp);
		sg_list_sz = 0;
		if (init_attr->srq) {
D
Dennis Dalessandro 已提交
1059
			struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
1060 1061 1062 1063 1064 1065 1066

			if (srq->rq.max_sge > 1)
				sg_list_sz = sizeof(*qp->r_sg_list) *
					(srq->rq.max_sge - 1);
		} else if (init_attr->cap.max_recv_sge > 1)
			sg_list_sz = sizeof(*qp->r_sg_list) *
				(init_attr->cap.max_recv_sge - 1);
1067
		qp = kzalloc(sz + sg_list_sz, gfp);
1068 1069 1070 1071
		if (!qp) {
			ret = ERR_PTR(-ENOMEM);
			goto bail_swq;
		}
M
Mike Marciniszyn 已提交
1072
		RCU_INIT_POINTER(qp->next, NULL);
1073 1074 1075 1076 1077 1078 1079 1080
		priv = kzalloc(sizeof(*priv), gfp);
		if (!priv) {
			ret = ERR_PTR(-ENOMEM);
			goto bail_qp_hdr;
		}
		priv->owner = qp;
		priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), gfp);
		if (!priv->s_hdr) {
1081 1082 1083
			ret = ERR_PTR(-ENOMEM);
			goto bail_qp;
		}
1084
		qp->priv = priv;
1085 1086 1087
		qp->timeout_jiffies =
			usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
				1000UL);
1088 1089 1090 1091 1092 1093
		if (init_attr->srq)
			sz = 0;
		else {
			qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
			qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
			sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
1094
				sizeof(struct rvt_rwqe);
1095 1096
			if (gfp != GFP_NOIO)
				qp->r_rq.wq = vmalloc_user(
1097
						sizeof(struct rvt_rwq) +
1098 1099 1100
						qp->r_rq.size * sz);
			else
				qp->r_rq.wq = __vmalloc(
1101
						sizeof(struct rvt_rwq) +
1102 1103 1104
						qp->r_rq.size * sz,
						gfp, PAGE_KERNEL);

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
			if (!qp->r_rq.wq) {
				ret = ERR_PTR(-ENOMEM);
				goto bail_qp;
			}
		}

		/*
		 * ib_create_qp() will initialize qp->ibqp
		 * except for qp->ibqp.qp_num.
		 */
		spin_lock_init(&qp->r_lock);
		spin_lock_init(&qp->s_lock);
		spin_lock_init(&qp->r_rq.lock);
		atomic_set(&qp->refcount, 0);
		init_waitqueue_head(&qp->wait);
1120
		init_waitqueue_head(&priv->wait_dma);
1121 1122
		init_timer(&qp->s_timer);
		qp->s_timer.data = (unsigned long)qp;
1123 1124
		INIT_WORK(&priv->s_work, qib_do_send);
		INIT_LIST_HEAD(&priv->iowait);
1125 1126 1127 1128 1129 1130
		INIT_LIST_HEAD(&qp->rspwait);
		qp->state = IB_QPS_RESET;
		qp->s_wq = swq;
		qp->s_size = init_attr->cap.max_send_wr + 1;
		qp->s_max_sge = init_attr->cap.max_send_sge;
		if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
1131
			qp->s_flags = RVT_S_SIGNAL_REQ_WR;
1132 1133
		dev = to_idev(ibpd->device);
		dd = dd_from_dev(dev);
1134 1135
		err = alloc_qpn(dd, &dev->rdi.qp_dev->qpn_table,
				init_attr->qp_type, init_attr->port_num, gfp);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
		if (err < 0) {
			ret = ERR_PTR(err);
			vfree(qp->r_rq.wq);
			goto bail_qp;
		}
		qp->ibqp.qp_num = err;
		qp->port_num = init_attr->port_num;
		qib_reset_qp(qp, init_attr->qp_type);
		break;

	default:
		/* Don't support raw QPs */
		ret = ERR_PTR(-ENOSYS);
		goto bail;
	}

	init_attr->cap.max_inline_data = 0;

	/*
	 * Return the address of the RWQ as the offset to mmap.
	 * See qib_mmap() for details.
	 */
	if (udata && udata->outlen >= sizeof(__u64)) {
		if (!qp->r_rq.wq) {
			__u64 offset = 0;

			err = ib_copy_to_udata(udata, &offset,
					       sizeof(offset));
			if (err) {
				ret = ERR_PTR(err);
				goto bail_ip;
			}
		} else {
1169
			u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
1170

H
Harish Chegondi 已提交
1171
			qp->ip = rvt_create_mmap_info(&dev->rdi, s,
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
						      ibpd->uobject->context,
						      qp->r_rq.wq);
			if (!qp->ip) {
				ret = ERR_PTR(-ENOMEM);
				goto bail_ip;
			}

			err = ib_copy_to_udata(udata, &(qp->ip->offset),
					       sizeof(qp->ip->offset));
			if (err) {
				ret = ERR_PTR(err);
				goto bail_ip;
			}
		}
	}

	spin_lock(&dev->n_qps_lock);
	if (dev->n_qps_allocated == ib_qib_max_qps) {
		spin_unlock(&dev->n_qps_lock);
		ret = ERR_PTR(-ENOMEM);
		goto bail_ip;
	}

	dev->n_qps_allocated++;
	spin_unlock(&dev->n_qps_lock);

	if (qp->ip) {
H
Harish Chegondi 已提交
1199 1200 1201
		spin_lock_irq(&dev->rdi.pending_lock);
		list_add(&qp->ip->pending_mmaps, &dev->rdi.pending_mmaps);
		spin_unlock_irq(&dev->rdi.pending_lock);
1202 1203 1204 1205 1206 1207 1208
	}

	ret = &qp->ibqp;
	goto bail;

bail_ip:
	if (qp->ip)
H
Harish Chegondi 已提交
1209
		kref_put(&qp->ip->ref, rvt_release_mmap_info);
1210 1211
	else
		vfree(qp->r_rq.wq);
1212
	free_qpn(&dev->rdi.qp_dev->qpn_table, qp->ibqp.qp_num);
1213
bail_qp:
1214 1215 1216
	kfree(priv->s_hdr);
	kfree(priv);
bail_qp_hdr:
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	kfree(qp);
bail_swq:
	vfree(swq);
bail:
	return ret;
}

/**
 * qib_destroy_qp - destroy a queue pair
 * @ibqp: the queue pair to destroy
 *
 * Returns 0 on success.
 *
 * Note that this can be called while the QP is actively sending or
 * receiving!
 */
int qib_destroy_qp(struct ib_qp *ibqp)
{
1235
	struct rvt_qp *qp = to_iqp(ibqp);
1236
	struct qib_ibdev *dev = to_idev(ibqp->device);
1237
	struct qib_qp_priv *priv = qp->priv;
1238 1239 1240 1241 1242

	/* Make sure HW and driver activity is stopped. */
	spin_lock_irq(&qp->s_lock);
	if (qp->state != IB_QPS_RESET) {
		qp->state = IB_QPS_RESET;
H
Harish Chegondi 已提交
1243
		spin_lock(&dev->rdi.pending_lock);
1244 1245
		if (!list_empty(&priv->iowait))
			list_del_init(&priv->iowait);
H
Harish Chegondi 已提交
1246
		spin_unlock(&dev->rdi.pending_lock);
1247
		qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
1248
		spin_unlock_irq(&qp->s_lock);
1249
		cancel_work_sync(&priv->s_work);
1250
		del_timer_sync(&qp->s_timer);
1251 1252 1253 1254
		wait_event(priv->wait_dma, !atomic_read(&priv->s_dma_busy));
		if (priv->s_tx) {
			qib_put_txreq(priv->s_tx);
			priv->s_tx = NULL;
1255 1256 1257 1258 1259 1260 1261 1262
		}
		remove_qp(dev, qp);
		wait_event(qp->wait, !atomic_read(&qp->refcount));
		clear_mr_refs(qp, 1);
	} else
		spin_unlock_irq(&qp->s_lock);

	/* all user's cleaned up, mark it available */
1263
	free_qpn(&dev->rdi.qp_dev->qpn_table, qp->ibqp.qp_num);
1264 1265 1266 1267 1268
	spin_lock(&dev->n_qps_lock);
	dev->n_qps_allocated--;
	spin_unlock(&dev->n_qps_lock);

	if (qp->ip)
H
Harish Chegondi 已提交
1269
		kref_put(&qp->ip->ref, rvt_release_mmap_info);
1270 1271 1272
	else
		vfree(qp->r_rq.wq);
	vfree(qp->s_wq);
1273 1274
	kfree(priv->s_hdr);
	kfree(priv);
1275 1276 1277 1278 1279 1280 1281 1282
	kfree(qp);
	return 0;
}

/**
 * qib_init_qpn_table - initialize the QP number table for a device
 * @qpt: the QPN table
 */
1283
void qib_init_qpn_table(struct qib_devdata *dd, struct rvt_qpn_table *qpt)
1284 1285 1286 1287
{
	spin_lock_init(&qpt->lock);
	qpt->last = 1;          /* start with QPN 2 */
	qpt->nmaps = 1;
1288
	qpt_mask = dd->qpn_mask;
1289 1290 1291 1292 1293 1294
}

/**
 * qib_free_qpn_table - free the QP number table for a device
 * @qpt: the QPN table
 */
1295
void qib_free_qpn_table(struct rvt_qpn_table *qpt)
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
{
	int i;

	for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
		if (qpt->map[i].page)
			free_page((unsigned long) qpt->map[i].page);
}

/**
 * qib_get_credit - flush the send work queue of a QP
 * @qp: the qp who's send work queue to flush
 * @aeth: the Acknowledge Extended Transport Header
 *
 * The QP s_lock should be held.
 */
1311
void qib_get_credit(struct rvt_qp *qp, u32 aeth)
1312 1313 1314 1315 1316 1317 1318 1319 1320
{
	u32 credit = (aeth >> QIB_AETH_CREDIT_SHIFT) & QIB_AETH_CREDIT_MASK;

	/*
	 * If the credit is invalid, we can send
	 * as many packets as we like.  Otherwise, we have to
	 * honor the credit field.
	 */
	if (credit == QIB_AETH_CREDIT_INVAL) {
1321 1322 1323 1324
		if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
			qp->s_flags |= RVT_S_UNLIMITED_CREDIT;
			if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
				qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
1325 1326 1327
				qib_schedule_send(qp);
			}
		}
1328
	} else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
1329 1330 1331 1332
		/* Compute new LSN (i.e., MSN + credit) */
		credit = (aeth + credit_table[credit]) & QIB_MSN_MASK;
		if (qib_cmp24(credit, qp->s_lsn) > 0) {
			qp->s_lsn = credit;
1333 1334
			if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
				qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
1335 1336 1337 1338 1339
				qib_schedule_send(qp);
			}
		}
	}
}
1340 1341 1342 1343 1344

#ifdef CONFIG_DEBUG_FS

struct qib_qp_iter {
	struct qib_ibdev *dev;
1345
	struct rvt_qp *qp;
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	int n;
};

struct qib_qp_iter *qib_qp_iter_init(struct qib_ibdev *dev)
{
	struct qib_qp_iter *iter;

	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
	if (!iter)
		return NULL;

	iter->dev = dev;
	if (qib_qp_iter_next(iter)) {
		kfree(iter);
		return NULL;
	}

	return iter;
}

int qib_qp_iter_next(struct qib_qp_iter *iter)
{
	struct qib_ibdev *dev = iter->dev;
	int n = iter->n;
	int ret = 1;
1371 1372
	struct rvt_qp *pqp = iter->qp;
	struct rvt_qp *qp;
1373

1374
	for (; n < dev->rdi.qp_dev->qp_table_size; n++) {
1375 1376 1377
		if (pqp)
			qp = rcu_dereference(pqp->next);
		else
1378
			qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]);
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
		pqp = qp;
		if (qp) {
			iter->qp = qp;
			iter->n = n;
			return 0;
		}
	}
	return ret;
}

static const char * const qp_type_str[] = {
	"SMI", "GSI", "RC", "UC", "UD",
};

void qib_qp_iter_print(struct seq_file *s, struct qib_qp_iter *iter)
{
1395 1396
	struct rvt_swqe *wqe;
	struct rvt_qp *qp = iter->qp;
1397
	struct qib_qp_priv *priv = qp->priv;
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408

	wqe = get_swqe_ptr(qp, qp->s_last);
	seq_printf(s,
		   "N %d QP%u %s %u %u %u f=%x %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u) QP%u LID %x\n",
		   iter->n,
		   qp->ibqp.qp_num,
		   qp_type_str[qp->ibqp.qp_type],
		   qp->state,
		   wqe->wr.opcode,
		   qp->s_hdrwords,
		   qp->s_flags,
1409 1410
		   atomic_read(&priv->s_dma_busy),
		   !list_empty(&priv->iowait),
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
		   qp->timeout,
		   wqe->ssn,
		   qp->s_lsn,
		   qp->s_last_psn,
		   qp->s_psn, qp->s_next_psn,
		   qp->s_sending_psn, qp->s_sending_hpsn,
		   qp->s_last, qp->s_acked, qp->s_cur,
		   qp->s_tail, qp->s_head, qp->s_size,
		   qp->remote_qpn,
		   qp->remote_ah_attr.dlid);
}

#endif