blob: 68f01513214f83b9bbf32c25188d6846b1b89d16 [file] [log] [blame]
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/err.h>
35#include <linux/vmalloc.h>
36
37#include "ipath_verbs.h"
38#include "ips_common.h"
39
40#define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
41#define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
42#define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \
43 (off))
44#define find_next_offset(map, off) find_next_zero_bit((map)->page, \
45 BITS_PER_PAGE, off)
46
47#define TRANS_INVALID 0
48#define TRANS_ANY2RST 1
49#define TRANS_RST2INIT 2
50#define TRANS_INIT2INIT 3
51#define TRANS_INIT2RTR 4
52#define TRANS_RTR2RTS 5
53#define TRANS_RTS2RTS 6
54#define TRANS_SQERR2RTS 7
55#define TRANS_ANY2ERR 8
56#define TRANS_RTS2SQD 9 /* XXX Wait for expected ACKs & signal event */
57#define TRANS_SQD2SQD 10 /* error if not drained & parameter change */
58#define TRANS_SQD2RTS 11 /* error if not drained */
59
60/*
61 * Convert the AETH credit code into the number of credits.
62 */
63static u32 credit_table[31] = {
64 0, /* 0 */
65 1, /* 1 */
66 2, /* 2 */
67 3, /* 3 */
68 4, /* 4 */
69 6, /* 5 */
70 8, /* 6 */
71 12, /* 7 */
72 16, /* 8 */
73 24, /* 9 */
74 32, /* A */
75 48, /* B */
76 64, /* C */
77 96, /* D */
78 128, /* E */
79 192, /* F */
80 256, /* 10 */
81 384, /* 11 */
82 512, /* 12 */
83 768, /* 13 */
84 1024, /* 14 */
85 1536, /* 15 */
86 2048, /* 16 */
87 3072, /* 17 */
88 4096, /* 18 */
89 6144, /* 19 */
90 8192, /* 1A */
91 12288, /* 1B */
92 16384, /* 1C */
93 24576, /* 1D */
94 32768 /* 1E */
95};
96
97static u32 alloc_qpn(struct ipath_qp_table *qpt)
98{
99 u32 i, offset, max_scan, qpn;
100 struct qpn_map *map;
101 u32 ret;
102
103 qpn = qpt->last + 1;
104 if (qpn >= QPN_MAX)
105 qpn = 2;
106 offset = qpn & BITS_PER_PAGE_MASK;
107 map = &qpt->map[qpn / BITS_PER_PAGE];
108 max_scan = qpt->nmaps - !offset;
109 for (i = 0;;) {
110 if (unlikely(!map->page)) {
111 unsigned long page = get_zeroed_page(GFP_KERNEL);
112 unsigned long flags;
113
114 /*
115 * Free the page if someone raced with us
116 * installing it:
117 */
118 spin_lock_irqsave(&qpt->lock, flags);
119 if (map->page)
120 free_page(page);
121 else
122 map->page = (void *)page;
123 spin_unlock_irqrestore(&qpt->lock, flags);
124 if (unlikely(!map->page))
125 break;
126 }
127 if (likely(atomic_read(&map->n_free))) {
128 do {
129 if (!test_and_set_bit(offset, map->page)) {
130 atomic_dec(&map->n_free);
131 qpt->last = qpn;
132 ret = qpn;
133 goto bail;
134 }
135 offset = find_next_offset(map, offset);
136 qpn = mk_qpn(qpt, map, offset);
137 /*
138 * This test differs from alloc_pidmap().
139 * If find_next_offset() does find a zero
140 * bit, we don't need to check for QPN
141 * wrapping around past our starting QPN.
142 * We just need to be sure we don't loop
143 * forever.
144 */
145 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
146 }
147 /*
148 * In order to keep the number of pages allocated to a
149 * minimum, we scan the all existing pages before increasing
150 * the size of the bitmap table.
151 */
152 if (++i > max_scan) {
153 if (qpt->nmaps == QPNMAP_ENTRIES)
154 break;
155 map = &qpt->map[qpt->nmaps++];
156 offset = 0;
157 } else if (map < &qpt->map[qpt->nmaps]) {
158 ++map;
159 offset = 0;
160 } else {
161 map = &qpt->map[0];
162 offset = 2;
163 }
164 qpn = mk_qpn(qpt, map, offset);
165 }
166
167 ret = 0;
168
169bail:
170 return ret;
171}
172
173static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
174{
175 struct qpn_map *map;
176
177 map = qpt->map + qpn / BITS_PER_PAGE;
178 if (map->page)
179 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
180 atomic_inc(&map->n_free);
181}
182
183/**
184 * ipath_alloc_qpn - allocate a QP number
185 * @qpt: the QP table
186 * @qp: the QP
187 * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
188 *
189 * Allocate the next available QPN and put the QP into the hash table.
190 * The hash table holds a reference to the QP.
191 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700192static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
193 enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800194{
195 unsigned long flags;
196 u32 qpn;
197 int ret;
198
199 if (type == IB_QPT_SMI)
200 qpn = 0;
201 else if (type == IB_QPT_GSI)
202 qpn = 1;
203 else {
204 /* Allocate the next available QPN */
205 qpn = alloc_qpn(qpt);
206 if (qpn == 0) {
207 ret = -ENOMEM;
208 goto bail;
209 }
210 }
211 qp->ibqp.qp_num = qpn;
212
213 /* Add the QP to the hash table. */
214 spin_lock_irqsave(&qpt->lock, flags);
215
216 qpn %= qpt->max;
217 qp->next = qpt->table[qpn];
218 qpt->table[qpn] = qp;
219 atomic_inc(&qp->refcount);
220
221 spin_unlock_irqrestore(&qpt->lock, flags);
222 ret = 0;
223
224bail:
225 return ret;
226}
227
228/**
229 * ipath_free_qp - remove a QP from the QP table
230 * @qpt: the QP table
231 * @qp: the QP to remove
232 *
233 * Remove the QP from the table so it can't be found asynchronously by
234 * the receive interrupt routine.
235 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700236static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800237{
238 struct ipath_qp *q, **qpp;
239 unsigned long flags;
240 int fnd = 0;
241
242 spin_lock_irqsave(&qpt->lock, flags);
243
244 /* Remove QP from the hash table. */
245 qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
246 for (; (q = *qpp) != NULL; qpp = &q->next) {
247 if (q == qp) {
248 *qpp = qp->next;
249 qp->next = NULL;
250 atomic_dec(&qp->refcount);
251 fnd = 1;
252 break;
253 }
254 }
255
256 spin_unlock_irqrestore(&qpt->lock, flags);
257
258 if (!fnd)
259 return;
260
261 /* If QPN is not reserved, mark QPN free in the bitmap. */
262 if (qp->ibqp.qp_num > 1)
263 free_qpn(qpt, qp->ibqp.qp_num);
264
265 wait_event(qp->wait, !atomic_read(&qp->refcount));
266}
267
268/**
269 * ipath_free_all_qps - remove all QPs from the table
270 * @qpt: the QP table to empty
271 */
272void ipath_free_all_qps(struct ipath_qp_table *qpt)
273{
274 unsigned long flags;
275 struct ipath_qp *qp, *nqp;
276 u32 n;
277
278 for (n = 0; n < qpt->max; n++) {
279 spin_lock_irqsave(&qpt->lock, flags);
280 qp = qpt->table[n];
281 qpt->table[n] = NULL;
282 spin_unlock_irqrestore(&qpt->lock, flags);
283
284 while (qp) {
285 nqp = qp->next;
286 if (qp->ibqp.qp_num > 1)
287 free_qpn(qpt, qp->ibqp.qp_num);
288 if (!atomic_dec_and_test(&qp->refcount) ||
289 !ipath_destroy_qp(&qp->ibqp))
290 _VERBS_INFO("QP memory leak!\n");
291 qp = nqp;
292 }
293 }
294
295 for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
296 if (qpt->map[n].page)
297 free_page((unsigned long)qpt->map[n].page);
298 }
299}
300
301/**
302 * ipath_lookup_qpn - return the QP with the given QPN
303 * @qpt: the QP table
304 * @qpn: the QP number to look up
305 *
306 * The caller is responsible for decrementing the QP reference count
307 * when done.
308 */
309struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
310{
311 unsigned long flags;
312 struct ipath_qp *qp;
313
314 spin_lock_irqsave(&qpt->lock, flags);
315
316 for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
317 if (qp->ibqp.qp_num == qpn) {
318 atomic_inc(&qp->refcount);
319 break;
320 }
321 }
322
323 spin_unlock_irqrestore(&qpt->lock, flags);
324 return qp;
325}
326
327/**
328 * ipath_reset_qp - initialize the QP state to the reset state
329 * @qp: the QP to reset
330 */
331static void ipath_reset_qp(struct ipath_qp *qp)
332{
333 qp->remote_qpn = 0;
334 qp->qkey = 0;
335 qp->qp_access_flags = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700336 clear_bit(IPATH_S_BUSY, &qp->s_flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800337 qp->s_hdrwords = 0;
338 qp->s_psn = 0;
339 qp->r_psn = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700340 qp->r_msn = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800341 if (qp->ibqp.qp_type == IB_QPT_RC) {
342 qp->s_state = IB_OPCODE_RC_SEND_LAST;
343 qp->r_state = IB_OPCODE_RC_SEND_LAST;
344 } else {
345 qp->s_state = IB_OPCODE_UC_SEND_LAST;
346 qp->r_state = IB_OPCODE_UC_SEND_LAST;
347 }
348 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700349 qp->r_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
350 qp->r_nak_state = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800351 qp->s_rnr_timeout = 0;
352 qp->s_head = 0;
353 qp->s_tail = 0;
354 qp->s_cur = 0;
355 qp->s_last = 0;
356 qp->s_ssn = 1;
357 qp->s_lsn = 0;
358 qp->r_rq.head = 0;
359 qp->r_rq.tail = 0;
360 qp->r_reuse_sge = 0;
361}
362
363/**
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700364 * ipath_error_qp - put a QP into an error state
365 * @qp: the QP to put into an error state
366 *
367 * Flushes both send and receive work queues.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700368 * QP s_lock should be held and interrupts disabled.
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700369 */
370
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700371void ipath_error_qp(struct ipath_qp *qp)
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700372{
373 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
374 struct ib_wc wc;
375
376 _VERBS_INFO("QP%d/%d in error state\n",
377 qp->ibqp.qp_num, qp->remote_qpn);
378
379 spin_lock(&dev->pending_lock);
380 /* XXX What if its already removed by the timeout code? */
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700381 if (!list_empty(&qp->timerwait))
382 list_del_init(&qp->timerwait);
383 if (!list_empty(&qp->piowait))
384 list_del_init(&qp->piowait);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700385 spin_unlock(&dev->pending_lock);
386
387 wc.status = IB_WC_WR_FLUSH_ERR;
388 wc.vendor_err = 0;
389 wc.byte_len = 0;
390 wc.imm_data = 0;
391 wc.qp_num = qp->ibqp.qp_num;
392 wc.src_qp = 0;
393 wc.wc_flags = 0;
394 wc.pkey_index = 0;
395 wc.slid = 0;
396 wc.sl = 0;
397 wc.dlid_path_bits = 0;
398 wc.port_num = 0;
399
400 while (qp->s_last != qp->s_head) {
401 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
402
403 wc.wr_id = wqe->wr.wr_id;
404 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
405 if (++qp->s_last >= qp->s_size)
406 qp->s_last = 0;
407 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
408 }
409 qp->s_cur = qp->s_tail = qp->s_head;
410 qp->s_hdrwords = 0;
411 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
412
413 wc.opcode = IB_WC_RECV;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700414 spin_lock(&qp->r_rq.lock);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700415 while (qp->r_rq.tail != qp->r_rq.head) {
416 wc.wr_id = get_rwqe_ptr(&qp->r_rq, qp->r_rq.tail)->wr_id;
417 if (++qp->r_rq.tail >= qp->r_rq.size)
418 qp->r_rq.tail = 0;
419 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
420 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700421 spin_unlock(&qp->r_rq.lock);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700422}
423
424/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800425 * ipath_modify_qp - modify the attributes of a queue pair
426 * @ibqp: the queue pair who's attributes we're modifying
427 * @attr: the new attributes
428 * @attr_mask: the mask of attributes to modify
429 *
430 * Returns 0 on success, otherwise returns an errno.
431 */
432int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
433 int attr_mask)
434{
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700435 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800436 struct ipath_qp *qp = to_iqp(ibqp);
437 enum ib_qp_state cur_state, new_state;
438 unsigned long flags;
439 int ret;
440
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700441 spin_lock_irqsave(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800442
443 cur_state = attr_mask & IB_QP_CUR_STATE ?
444 attr->cur_qp_state : qp->state;
445 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
446
447 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
448 attr_mask))
449 goto inval;
450
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700451 if (attr_mask & IB_QP_AV)
452 if (attr->ah_attr.dlid == 0 ||
453 attr->ah_attr.dlid >= IPS_MULTICAST_LID_BASE)
454 goto inval;
455
456 if (attr_mask & IB_QP_PKEY_INDEX)
457 if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd))
458 goto inval;
459
460 if (attr_mask & IB_QP_MIN_RNR_TIMER)
461 if (attr->min_rnr_timer > 31)
462 goto inval;
463
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800464 switch (new_state) {
465 case IB_QPS_RESET:
466 ipath_reset_qp(qp);
467 break;
468
469 case IB_QPS_ERR:
470 ipath_error_qp(qp);
471 break;
472
473 default:
474 break;
475
476 }
477
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700478 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800479 qp->s_pkey_index = attr->pkey_index;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800480
481 if (attr_mask & IB_QP_DEST_QPN)
482 qp->remote_qpn = attr->dest_qp_num;
483
484 if (attr_mask & IB_QP_SQ_PSN) {
485 qp->s_next_psn = attr->sq_psn;
486 qp->s_last_psn = qp->s_next_psn - 1;
487 }
488
489 if (attr_mask & IB_QP_RQ_PSN)
490 qp->r_psn = attr->rq_psn;
491
492 if (attr_mask & IB_QP_ACCESS_FLAGS)
493 qp->qp_access_flags = attr->qp_access_flags;
494
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700495 if (attr_mask & IB_QP_AV)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800496 qp->remote_ah_attr = attr->ah_attr;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800497
498 if (attr_mask & IB_QP_PATH_MTU)
499 qp->path_mtu = attr->path_mtu;
500
501 if (attr_mask & IB_QP_RETRY_CNT)
502 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
503
504 if (attr_mask & IB_QP_RNR_RETRY) {
505 qp->s_rnr_retry = attr->rnr_retry;
506 if (qp->s_rnr_retry > 7)
507 qp->s_rnr_retry = 7;
508 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
509 }
510
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700511 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700512 qp->r_min_rnr_timer = attr->min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800513
514 if (attr_mask & IB_QP_QKEY)
515 qp->qkey = attr->qkey;
516
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800517 qp->state = new_state;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700518 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800519
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800520 ret = 0;
521 goto bail;
522
523inval:
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700524 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800525 ret = -EINVAL;
526
527bail:
528 return ret;
529}
530
531int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
532 int attr_mask, struct ib_qp_init_attr *init_attr)
533{
534 struct ipath_qp *qp = to_iqp(ibqp);
535
536 attr->qp_state = qp->state;
537 attr->cur_qp_state = attr->qp_state;
538 attr->path_mtu = qp->path_mtu;
539 attr->path_mig_state = 0;
540 attr->qkey = qp->qkey;
541 attr->rq_psn = qp->r_psn;
542 attr->sq_psn = qp->s_next_psn;
543 attr->dest_qp_num = qp->remote_qpn;
544 attr->qp_access_flags = qp->qp_access_flags;
545 attr->cap.max_send_wr = qp->s_size - 1;
546 attr->cap.max_recv_wr = qp->r_rq.size - 1;
547 attr->cap.max_send_sge = qp->s_max_sge;
548 attr->cap.max_recv_sge = qp->r_rq.max_sge;
549 attr->cap.max_inline_data = 0;
550 attr->ah_attr = qp->remote_ah_attr;
551 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
552 attr->pkey_index = qp->s_pkey_index;
553 attr->alt_pkey_index = 0;
554 attr->en_sqd_async_notify = 0;
555 attr->sq_draining = 0;
556 attr->max_rd_atomic = 1;
557 attr->max_dest_rd_atomic = 1;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700558 attr->min_rnr_timer = qp->r_min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800559 attr->port_num = 1;
560 attr->timeout = 0;
561 attr->retry_cnt = qp->s_retry_cnt;
562 attr->rnr_retry = qp->s_rnr_retry;
563 attr->alt_port_num = 0;
564 attr->alt_timeout = 0;
565
566 init_attr->event_handler = qp->ibqp.event_handler;
567 init_attr->qp_context = qp->ibqp.qp_context;
568 init_attr->send_cq = qp->ibqp.send_cq;
569 init_attr->recv_cq = qp->ibqp.recv_cq;
570 init_attr->srq = qp->ibqp.srq;
571 init_attr->cap = attr->cap;
572 init_attr->sq_sig_type =
573 (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR))
574 ? IB_SIGNAL_REQ_WR : 0;
575 init_attr->qp_type = qp->ibqp.qp_type;
576 init_attr->port_num = 1;
577 return 0;
578}
579
580/**
581 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
582 * @qp: the queue pair to compute the AETH for
583 *
584 * Returns the AETH.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800585 */
586__be32 ipath_compute_aeth(struct ipath_qp *qp)
587{
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700588 u32 aeth = qp->r_msn & IPS_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800589
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700590 if (qp->ibqp.srq) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800591 /*
592 * Shared receive queues don't generate credits.
593 * Set the credit field to the invalid value.
594 */
595 aeth |= IPS_AETH_CREDIT_INVAL << IPS_AETH_CREDIT_SHIFT;
596 } else {
597 u32 min, max, x;
598 u32 credits;
599
600 /*
601 * Compute the number of credits available (RWQEs).
602 * XXX Not holding the r_rq.lock here so there is a small
603 * chance that the pair of reads are not atomic.
604 */
605 credits = qp->r_rq.head - qp->r_rq.tail;
606 if ((int)credits < 0)
607 credits += qp->r_rq.size;
608 /*
609 * Binary search the credit table to find the code to
610 * use.
611 */
612 min = 0;
613 max = 31;
614 for (;;) {
615 x = (min + max) / 2;
616 if (credit_table[x] == credits)
617 break;
618 if (credit_table[x] > credits)
619 max = x;
620 else if (min == x)
621 break;
622 else
623 min = x;
624 }
625 aeth |= x << IPS_AETH_CREDIT_SHIFT;
626 }
627 return cpu_to_be32(aeth);
628}
629
630/**
631 * ipath_create_qp - create a queue pair for a device
632 * @ibpd: the protection domain who's device we create the queue pair for
633 * @init_attr: the attributes of the queue pair
634 * @udata: unused by InfiniPath
635 *
636 * Returns the queue pair on success, otherwise returns an errno.
637 *
638 * Called by the ib_create_qp() core verbs function.
639 */
640struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
641 struct ib_qp_init_attr *init_attr,
642 struct ib_udata *udata)
643{
644 struct ipath_qp *qp;
645 int err;
646 struct ipath_swqe *swq = NULL;
647 struct ipath_ibdev *dev;
648 size_t sz;
649 struct ib_qp *ret;
650
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700651 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
652 init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
653 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
654 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800655 ret = ERR_PTR(-ENOMEM);
656 goto bail;
657 }
658
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700659 if (init_attr->cap.max_send_sge +
660 init_attr->cap.max_recv_sge +
661 init_attr->cap.max_send_wr +
662 init_attr->cap.max_recv_wr == 0) {
663 ret = ERR_PTR(-EINVAL);
664 goto bail;
665 }
666
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800667 switch (init_attr->qp_type) {
668 case IB_QPT_UC:
669 case IB_QPT_RC:
670 sz = sizeof(struct ipath_sge) *
671 init_attr->cap.max_send_sge +
672 sizeof(struct ipath_swqe);
673 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
674 if (swq == NULL) {
675 ret = ERR_PTR(-ENOMEM);
676 goto bail;
677 }
678 /* FALLTHROUGH */
679 case IB_QPT_UD:
680 case IB_QPT_SMI:
681 case IB_QPT_GSI:
682 qp = kmalloc(sizeof(*qp), GFP_KERNEL);
683 if (!qp) {
Bryan O'Sullivan60460df2006-07-01 04:35:55 -0700684 vfree(swq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800685 ret = ERR_PTR(-ENOMEM);
686 goto bail;
687 }
688 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
689 sz = sizeof(struct ipath_sge) *
690 init_attr->cap.max_recv_sge +
691 sizeof(struct ipath_rwqe);
692 qp->r_rq.wq = vmalloc(qp->r_rq.size * sz);
693 if (!qp->r_rq.wq) {
694 kfree(qp);
Bryan O'Sullivan60460df2006-07-01 04:35:55 -0700695 vfree(swq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800696 ret = ERR_PTR(-ENOMEM);
697 goto bail;
698 }
699
700 /*
701 * ib_create_qp() will initialize qp->ibqp
702 * except for qp->ibqp.qp_num.
703 */
704 spin_lock_init(&qp->s_lock);
705 spin_lock_init(&qp->r_rq.lock);
706 atomic_set(&qp->refcount, 0);
707 init_waitqueue_head(&qp->wait);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700708 tasklet_init(&qp->s_task, ipath_do_ruc_send,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800709 (unsigned long)qp);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700710 INIT_LIST_HEAD(&qp->piowait);
711 INIT_LIST_HEAD(&qp->timerwait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800712 qp->state = IB_QPS_RESET;
713 qp->s_wq = swq;
714 qp->s_size = init_attr->cap.max_send_wr + 1;
715 qp->s_max_sge = init_attr->cap.max_send_sge;
716 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
717 qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ?
718 1 << IPATH_S_SIGNAL_REQ_WR : 0;
719 dev = to_idev(ibpd->device);
720 err = ipath_alloc_qpn(&dev->qp_table, qp,
721 init_attr->qp_type);
722 if (err) {
723 vfree(swq);
724 vfree(qp->r_rq.wq);
725 kfree(qp);
726 ret = ERR_PTR(err);
727 goto bail;
728 }
729 ipath_reset_qp(qp);
730
731 /* Tell the core driver that the kernel SMA is present. */
Bryan O'Sullivan09b74de2006-05-23 11:32:38 -0700732 if (init_attr->qp_type == IB_QPT_SMI)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800733 ipath_layer_set_verbs_flags(dev->dd,
734 IPATH_VERBS_KERNEL_SMA);
735 break;
736
737 default:
738 /* Don't support raw QPs */
739 ret = ERR_PTR(-ENOSYS);
740 goto bail;
741 }
742
743 init_attr->cap.max_inline_data = 0;
744
745 ret = &qp->ibqp;
746
747bail:
748 return ret;
749}
750
751/**
752 * ipath_destroy_qp - destroy a queue pair
753 * @ibqp: the queue pair to destroy
754 *
755 * Returns 0 on success.
756 *
757 * Note that this can be called while the QP is actively sending or
758 * receiving!
759 */
760int ipath_destroy_qp(struct ib_qp *ibqp)
761{
762 struct ipath_qp *qp = to_iqp(ibqp);
763 struct ipath_ibdev *dev = to_idev(ibqp->device);
764 unsigned long flags;
765
766 /* Tell the core driver that the kernel SMA is gone. */
767 if (qp->ibqp.qp_type == IB_QPT_SMI)
768 ipath_layer_set_verbs_flags(dev->dd, 0);
769
770 spin_lock_irqsave(&qp->r_rq.lock, flags);
771 spin_lock(&qp->s_lock);
772 qp->state = IB_QPS_ERR;
773 spin_unlock(&qp->s_lock);
774 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
775
776 /* Stop the sending tasklet. */
777 tasklet_kill(&qp->s_task);
778
779 /* Make sure the QP isn't on the timeout list. */
780 spin_lock_irqsave(&dev->pending_lock, flags);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700781 if (!list_empty(&qp->timerwait))
782 list_del_init(&qp->timerwait);
783 if (!list_empty(&qp->piowait))
784 list_del_init(&qp->piowait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800785 spin_unlock_irqrestore(&dev->pending_lock, flags);
786
787 /*
788 * Make sure that the QP is not in the QPN table so receive
789 * interrupts will discard packets for this QP. XXX Also remove QP
790 * from multicast table.
791 */
792 if (atomic_read(&qp->refcount) != 0)
793 ipath_free_qp(&dev->qp_table, qp);
794
795 vfree(qp->s_wq);
796 vfree(qp->r_rq.wq);
797 kfree(qp);
798 return 0;
799}
800
801/**
802 * ipath_init_qp_table - initialize the QP table for a device
803 * @idev: the device who's QP table we're initializing
804 * @size: the size of the QP table
805 *
806 * Returns 0 on success, otherwise returns an errno.
807 */
808int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
809{
810 int i;
811 int ret;
812
813 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
814 idev->qp_table.max = size;
815 idev->qp_table.nmaps = 1;
816 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
817 GFP_KERNEL);
818 if (idev->qp_table.table == NULL) {
819 ret = -ENOMEM;
820 goto bail;
821 }
822
823 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
824 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
825 idev->qp_table.map[i].page = NULL;
826 }
827
828 ret = 0;
829
830bail:
831 return ret;
832}
833
834/**
835 * ipath_sqerror_qp - put a QP's send queue into an error state
836 * @qp: QP who's send queue will be put into an error state
837 * @wc: the WC responsible for putting the QP in this state
838 *
839 * Flushes the send work queue.
840 * The QP s_lock should be held.
841 */
842
843void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
844{
845 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
846 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
847
848 _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n",
849 qp->ibqp.qp_num, qp->remote_qpn, wc->status);
850
851 spin_lock(&dev->pending_lock);
852 /* XXX What if its already removed by the timeout code? */
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700853 if (!list_empty(&qp->timerwait))
854 list_del_init(&qp->timerwait);
855 if (!list_empty(&qp->piowait))
856 list_del_init(&qp->piowait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800857 spin_unlock(&dev->pending_lock);
858
859 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
860 if (++qp->s_last >= qp->s_size)
861 qp->s_last = 0;
862
863 wc->status = IB_WC_WR_FLUSH_ERR;
864
865 while (qp->s_last != qp->s_head) {
866 wc->wr_id = wqe->wr.wr_id;
867 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
868 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
869 if (++qp->s_last >= qp->s_size)
870 qp->s_last = 0;
871 wqe = get_swqe_ptr(qp, qp->s_last);
872 }
873 qp->s_cur = qp->s_tail = qp->s_head;
874 qp->state = IB_QPS_SQE;
875}
876
877/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800878 * ipath_get_credit - flush the send work queue of a QP
879 * @qp: the qp who's send work queue to flush
880 * @aeth: the Acknowledge Extended Transport Header
881 *
882 * The QP s_lock should be held.
883 */
884void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
885{
886 u32 credit = (aeth >> IPS_AETH_CREDIT_SHIFT) & IPS_AETH_CREDIT_MASK;
887
888 /*
889 * If the credit is invalid, we can send
890 * as many packets as we like. Otherwise, we have to
891 * honor the credit field.
892 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700893 if (credit == IPS_AETH_CREDIT_INVAL)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800894 qp->s_lsn = (u32) -1;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700895 else if (qp->s_lsn != (u32) -1) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800896 /* Compute new LSN (i.e., MSN + credit) */
897 credit = (aeth + credit_table[credit]) & IPS_MSN_MASK;
898 if (ipath_cmp24(credit, qp->s_lsn) > 0)
899 qp->s_lsn = credit;
900 }
901
902 /* Restart sending if it was blocked due to lack of credits. */
903 if (qp->s_cur != qp->s_head &&
904 (qp->s_lsn == (u32) -1 ||
905 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
906 qp->s_lsn + 1) <= 0))
907 tasklet_hi_schedule(&qp->s_task);
908}