blob: 1324b35ff1f855b460e1a62723cd8f95a1023838 [file] [log] [blame]
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. 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"
Ralph Campbell373d9912006-09-22 15:22:26 -070038#include "ipath_kernel.h"
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080039
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
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080047/*
48 * Convert the AETH credit code into the number of credits.
49 */
50static u32 credit_table[31] = {
51 0, /* 0 */
52 1, /* 1 */
53 2, /* 2 */
54 3, /* 3 */
55 4, /* 4 */
56 6, /* 5 */
57 8, /* 6 */
58 12, /* 7 */
59 16, /* 8 */
60 24, /* 9 */
61 32, /* A */
62 48, /* B */
63 64, /* C */
64 96, /* D */
65 128, /* E */
66 192, /* F */
67 256, /* 10 */
68 384, /* 11 */
69 512, /* 12 */
70 768, /* 13 */
71 1024, /* 14 */
72 1536, /* 15 */
73 2048, /* 16 */
74 3072, /* 17 */
75 4096, /* 18 */
76 6144, /* 19 */
77 8192, /* 1A */
78 12288, /* 1B */
79 16384, /* 1C */
80 24576, /* 1D */
81 32768 /* 1E */
82};
83
Bryan O'Sullivan662af582007-03-15 14:45:12 -070084
85static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map)
86{
87 unsigned long page = get_zeroed_page(GFP_KERNEL);
88 unsigned long flags;
89
90 /*
91 * Free the page if someone raced with us installing it.
92 */
93
94 spin_lock_irqsave(&qpt->lock, flags);
95 if (map->page)
96 free_page(page);
97 else
98 map->page = (void *)page;
99 spin_unlock_irqrestore(&qpt->lock, flags);
100}
101
102
103static int alloc_qpn(struct ipath_qp_table *qpt, enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800104{
105 u32 i, offset, max_scan, qpn;
106 struct qpn_map *map;
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700107 u32 ret = -1;
108
109 if (type == IB_QPT_SMI)
110 ret = 0;
111 else if (type == IB_QPT_GSI)
112 ret = 1;
113
114 if (ret != -1) {
115 map = &qpt->map[0];
116 if (unlikely(!map->page)) {
117 get_map_page(qpt, map);
118 if (unlikely(!map->page)) {
119 ret = -ENOMEM;
120 goto bail;
121 }
122 }
123 if (!test_and_set_bit(ret, map->page))
124 atomic_dec(&map->n_free);
125 else
126 ret = -EBUSY;
127 goto bail;
128 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800129
130 qpn = qpt->last + 1;
131 if (qpn >= QPN_MAX)
132 qpn = 2;
133 offset = qpn & BITS_PER_PAGE_MASK;
134 map = &qpt->map[qpn / BITS_PER_PAGE];
135 max_scan = qpt->nmaps - !offset;
136 for (i = 0;;) {
137 if (unlikely(!map->page)) {
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700138 get_map_page(qpt, map);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800139 if (unlikely(!map->page))
140 break;
141 }
142 if (likely(atomic_read(&map->n_free))) {
143 do {
144 if (!test_and_set_bit(offset, map->page)) {
145 atomic_dec(&map->n_free);
146 qpt->last = qpn;
147 ret = qpn;
148 goto bail;
149 }
150 offset = find_next_offset(map, offset);
151 qpn = mk_qpn(qpt, map, offset);
152 /*
153 * This test differs from alloc_pidmap().
154 * If find_next_offset() does find a zero
155 * bit, we don't need to check for QPN
156 * wrapping around past our starting QPN.
157 * We just need to be sure we don't loop
158 * forever.
159 */
160 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
161 }
162 /*
163 * In order to keep the number of pages allocated to a
164 * minimum, we scan the all existing pages before increasing
165 * the size of the bitmap table.
166 */
167 if (++i > max_scan) {
168 if (qpt->nmaps == QPNMAP_ENTRIES)
169 break;
170 map = &qpt->map[qpt->nmaps++];
171 offset = 0;
172 } else if (map < &qpt->map[qpt->nmaps]) {
173 ++map;
174 offset = 0;
175 } else {
176 map = &qpt->map[0];
177 offset = 2;
178 }
179 qpn = mk_qpn(qpt, map, offset);
180 }
181
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700182 ret = -ENOMEM;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800183
184bail:
185 return ret;
186}
187
188static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
189{
190 struct qpn_map *map;
191
192 map = qpt->map + qpn / BITS_PER_PAGE;
193 if (map->page)
194 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
195 atomic_inc(&map->n_free);
196}
197
198/**
199 * ipath_alloc_qpn - allocate a QP number
200 * @qpt: the QP table
201 * @qp: the QP
202 * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
203 *
204 * Allocate the next available QPN and put the QP into the hash table.
205 * The hash table holds a reference to the QP.
206 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700207static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
208 enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800209{
210 unsigned long flags;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800211 int ret;
212
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700213 ret = alloc_qpn(qpt, type);
214 if (ret < 0)
215 goto bail;
216 qp->ibqp.qp_num = ret;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800217
218 /* Add the QP to the hash table. */
219 spin_lock_irqsave(&qpt->lock, flags);
220
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700221 ret %= qpt->max;
222 qp->next = qpt->table[ret];
223 qpt->table[ret] = qp;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800224 atomic_inc(&qp->refcount);
225
226 spin_unlock_irqrestore(&qpt->lock, flags);
227 ret = 0;
228
229bail:
230 return ret;
231}
232
233/**
234 * ipath_free_qp - remove a QP from the QP table
235 * @qpt: the QP table
236 * @qp: the QP to remove
237 *
238 * Remove the QP from the table so it can't be found asynchronously by
239 * the receive interrupt routine.
240 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700241static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800242{
243 struct ipath_qp *q, **qpp;
244 unsigned long flags;
245 int fnd = 0;
246
247 spin_lock_irqsave(&qpt->lock, flags);
248
249 /* Remove QP from the hash table. */
250 qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
251 for (; (q = *qpp) != NULL; qpp = &q->next) {
252 if (q == qp) {
253 *qpp = qp->next;
254 qp->next = NULL;
255 atomic_dec(&qp->refcount);
256 fnd = 1;
257 break;
258 }
259 }
260
261 spin_unlock_irqrestore(&qpt->lock, flags);
262
263 if (!fnd)
264 return;
265
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700266 free_qpn(qpt, qp->ibqp.qp_num);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800267
268 wait_event(qp->wait, !atomic_read(&qp->refcount));
269}
270
271/**
272 * ipath_free_all_qps - remove all QPs from the table
273 * @qpt: the QP table to empty
274 */
275void ipath_free_all_qps(struct ipath_qp_table *qpt)
276{
277 unsigned long flags;
278 struct ipath_qp *qp, *nqp;
279 u32 n;
280
281 for (n = 0; n < qpt->max; n++) {
282 spin_lock_irqsave(&qpt->lock, flags);
283 qp = qpt->table[n];
284 qpt->table[n] = NULL;
285 spin_unlock_irqrestore(&qpt->lock, flags);
286
287 while (qp) {
288 nqp = qp->next;
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700289 free_qpn(qpt, qp->ibqp.qp_num);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800290 if (!atomic_dec_and_test(&qp->refcount) ||
291 !ipath_destroy_qp(&qp->ibqp))
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -0700292 ipath_dbg("QP memory leak!\n");
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800293 qp = nqp;
294 }
295 }
296
297 for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
298 if (qpt->map[n].page)
299 free_page((unsigned long)qpt->map[n].page);
300 }
301}
302
303/**
304 * ipath_lookup_qpn - return the QP with the given QPN
305 * @qpt: the QP table
306 * @qpn: the QP number to look up
307 *
308 * The caller is responsible for decrementing the QP reference count
309 * when done.
310 */
311struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
312{
313 unsigned long flags;
314 struct ipath_qp *qp;
315
316 spin_lock_irqsave(&qpt->lock, flags);
317
318 for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
319 if (qp->ibqp.qp_num == qpn) {
320 atomic_inc(&qp->refcount);
321 break;
322 }
323 }
324
325 spin_unlock_irqrestore(&qpt->lock, flags);
326 return qp;
327}
328
329/**
330 * ipath_reset_qp - initialize the QP state to the reset state
331 * @qp: the QP to reset
332 */
333static void ipath_reset_qp(struct ipath_qp *qp)
334{
335 qp->remote_qpn = 0;
336 qp->qkey = 0;
337 qp->qp_access_flags = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700338 qp->s_busy = 0;
Robert Walsh991bda22007-06-04 09:55:48 -0700339 qp->s_flags &= IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800340 qp->s_hdrwords = 0;
341 qp->s_psn = 0;
342 qp->r_psn = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700343 qp->r_msn = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800344 if (qp->ibqp.qp_type == IB_QPT_RC) {
345 qp->s_state = IB_OPCODE_RC_SEND_LAST;
346 qp->r_state = IB_OPCODE_RC_SEND_LAST;
347 } else {
348 qp->s_state = IB_OPCODE_UC_SEND_LAST;
349 qp->r_state = IB_OPCODE_UC_SEND_LAST;
350 }
351 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700352 qp->r_nak_state = 0;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700353 qp->r_wrid_valid = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800354 qp->s_rnr_timeout = 0;
355 qp->s_head = 0;
356 qp->s_tail = 0;
357 qp->s_cur = 0;
358 qp->s_last = 0;
359 qp->s_ssn = 1;
360 qp->s_lsn = 0;
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700361 qp->s_wait_credit = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700362 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
363 qp->r_head_ack_queue = 0;
364 qp->s_tail_ack_queue = 0;
365 qp->s_num_rd_atomic = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700366 if (qp->r_rq.wq) {
367 qp->r_rq.wq->head = 0;
368 qp->r_rq.wq->tail = 0;
369 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800370 qp->r_reuse_sge = 0;
371}
372
373/**
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700374 * ipath_error_qp - put a QP into an error state
375 * @qp: the QP to put into an error state
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700376 * @err: the receive completion error to signal if a RWQE is active
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700377 *
378 * Flushes both send and receive work queues.
Ralph Campbell0434d272007-03-15 14:44:53 -0700379 * The QP s_lock should be held and interrupts disabled.
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700380 */
381
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700382void ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700383{
384 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
385 struct ib_wc wc;
386
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -0700387 ipath_dbg("QP%d/%d in error state\n",
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -0700388 qp->ibqp.qp_num, qp->remote_qpn);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700389
390 spin_lock(&dev->pending_lock);
391 /* XXX What if its already removed by the timeout code? */
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700392 if (!list_empty(&qp->timerwait))
393 list_del_init(&qp->timerwait);
394 if (!list_empty(&qp->piowait))
395 list_del_init(&qp->piowait);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700396 spin_unlock(&dev->pending_lock);
397
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700398 wc.vendor_err = 0;
399 wc.byte_len = 0;
400 wc.imm_data = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200401 wc.qp = &qp->ibqp;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700402 wc.src_qp = 0;
403 wc.wc_flags = 0;
404 wc.pkey_index = 0;
405 wc.slid = 0;
406 wc.sl = 0;
407 wc.dlid_path_bits = 0;
408 wc.port_num = 0;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700409 if (qp->r_wrid_valid) {
410 qp->r_wrid_valid = 0;
Ralph Campbell0434d272007-03-15 14:44:53 -0700411 wc.wr_id = qp->r_wr_id;
412 wc.opcode = IB_WC_RECV;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700413 wc.status = err;
414 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
415 }
416 wc.status = IB_WC_WR_FLUSH_ERR;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700417
418 while (qp->s_last != qp->s_head) {
419 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
420
421 wc.wr_id = wqe->wr.wr_id;
422 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
423 if (++qp->s_last >= qp->s_size)
424 qp->s_last = 0;
425 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
426 }
427 qp->s_cur = qp->s_tail = qp->s_head;
428 qp->s_hdrwords = 0;
429 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
430
Ralph Campbell373d9912006-09-22 15:22:26 -0700431 if (qp->r_rq.wq) {
432 struct ipath_rwq *wq;
433 u32 head;
434 u32 tail;
435
436 spin_lock(&qp->r_rq.lock);
437
438 /* sanity check pointers before trusting them */
439 wq = qp->r_rq.wq;
440 head = wq->head;
441 if (head >= qp->r_rq.size)
442 head = 0;
443 tail = wq->tail;
444 if (tail >= qp->r_rq.size)
445 tail = 0;
446 wc.opcode = IB_WC_RECV;
447 while (tail != head) {
448 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
449 if (++tail >= qp->r_rq.size)
450 tail = 0;
451 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
452 }
453 wq->tail = tail;
454
455 spin_unlock(&qp->r_rq.lock);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700456 }
457}
458
459/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800460 * ipath_modify_qp - modify the attributes of a queue pair
461 * @ibqp: the queue pair who's attributes we're modifying
462 * @attr: the new attributes
463 * @attr_mask: the mask of attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700464 * @udata: user data for ipathverbs.so
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800465 *
466 * Returns 0 on success, otherwise returns an errno.
467 */
468int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700469 int attr_mask, struct ib_udata *udata)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800470{
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700471 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800472 struct ipath_qp *qp = to_iqp(ibqp);
473 enum ib_qp_state cur_state, new_state;
474 unsigned long flags;
475 int ret;
476
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700477 spin_lock_irqsave(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800478
479 cur_state = attr_mask & IB_QP_CUR_STATE ?
480 attr->cur_qp_state : qp->state;
481 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
482
483 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
484 attr_mask))
485 goto inval;
486
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700487 if (attr_mask & IB_QP_AV) {
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700488 if (attr->ah_attr.dlid == 0 ||
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700489 attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700490 goto inval;
491
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700492 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
493 (attr->ah_attr.grh.sgid_index > 1))
494 goto inval;
495 }
496
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700497 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700498 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700499 goto inval;
500
501 if (attr_mask & IB_QP_MIN_RNR_TIMER)
502 if (attr->min_rnr_timer > 31)
503 goto inval;
504
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700505 if (attr_mask & IB_QP_PORT)
506 if (attr->port_num == 0 ||
507 attr->port_num > ibqp->device->phys_port_cnt)
508 goto inval;
509
Robert Walshe7340f02007-06-18 14:24:35 -0700510 /*
511 * Note: the chips support a maximum MTU of 4096, but the driver
512 * hasn't implemented this feature yet, so don't allow Path MTU
513 * values greater than 2048.
514 */
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700515 if (attr_mask & IB_QP_PATH_MTU)
Robert Walshe7340f02007-06-18 14:24:35 -0700516 if (attr->path_mtu > IB_MTU_2048)
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700517 goto inval;
518
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700519 if (attr_mask & IB_QP_PATH_MIG_STATE)
Bryan O'Sullivanca4ce382006-08-25 11:24:42 -0700520 if (attr->path_mig_state != IB_MIG_MIGRATED &&
521 attr->path_mig_state != IB_MIG_REARM)
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700522 goto inval;
523
Ralph Campbell3859e392007-03-15 14:44:51 -0700524 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
525 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
526 goto inval;
527
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800528 switch (new_state) {
529 case IB_QPS_RESET:
530 ipath_reset_qp(qp);
531 break;
532
533 case IB_QPS_ERR:
Bryan O'Sullivan041eab92007-03-15 14:44:57 -0700534 ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800535 break;
536
537 default:
538 break;
539
540 }
541
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700542 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800543 qp->s_pkey_index = attr->pkey_index;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800544
545 if (attr_mask & IB_QP_DEST_QPN)
546 qp->remote_qpn = attr->dest_qp_num;
547
548 if (attr_mask & IB_QP_SQ_PSN) {
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700549 qp->s_psn = qp->s_next_psn = attr->sq_psn;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800550 qp->s_last_psn = qp->s_next_psn - 1;
551 }
552
553 if (attr_mask & IB_QP_RQ_PSN)
554 qp->r_psn = attr->rq_psn;
555
556 if (attr_mask & IB_QP_ACCESS_FLAGS)
557 qp->qp_access_flags = attr->qp_access_flags;
558
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700559 if (attr_mask & IB_QP_AV)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800560 qp->remote_ah_attr = attr->ah_attr;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800561
562 if (attr_mask & IB_QP_PATH_MTU)
563 qp->path_mtu = attr->path_mtu;
564
565 if (attr_mask & IB_QP_RETRY_CNT)
566 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
567
568 if (attr_mask & IB_QP_RNR_RETRY) {
569 qp->s_rnr_retry = attr->rnr_retry;
570 if (qp->s_rnr_retry > 7)
571 qp->s_rnr_retry = 7;
572 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
573 }
574
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700575 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700576 qp->r_min_rnr_timer = attr->min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800577
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700578 if (attr_mask & IB_QP_TIMEOUT)
579 qp->timeout = attr->timeout;
580
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800581 if (attr_mask & IB_QP_QKEY)
582 qp->qkey = attr->qkey;
583
Ralph Campbell3859e392007-03-15 14:44:51 -0700584 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
585 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
586
587 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
588 qp->s_max_rd_atomic = attr->max_rd_atomic;
589
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800590 qp->state = new_state;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700591 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800592
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800593 ret = 0;
594 goto bail;
595
596inval:
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700597 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800598 ret = -EINVAL;
599
600bail:
601 return ret;
602}
603
604int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
605 int attr_mask, struct ib_qp_init_attr *init_attr)
606{
607 struct ipath_qp *qp = to_iqp(ibqp);
608
609 attr->qp_state = qp->state;
610 attr->cur_qp_state = attr->qp_state;
611 attr->path_mtu = qp->path_mtu;
612 attr->path_mig_state = 0;
613 attr->qkey = qp->qkey;
614 attr->rq_psn = qp->r_psn;
615 attr->sq_psn = qp->s_next_psn;
616 attr->dest_qp_num = qp->remote_qpn;
617 attr->qp_access_flags = qp->qp_access_flags;
618 attr->cap.max_send_wr = qp->s_size - 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700619 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800620 attr->cap.max_send_sge = qp->s_max_sge;
621 attr->cap.max_recv_sge = qp->r_rq.max_sge;
622 attr->cap.max_inline_data = 0;
623 attr->ah_attr = qp->remote_ah_attr;
624 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
625 attr->pkey_index = qp->s_pkey_index;
626 attr->alt_pkey_index = 0;
627 attr->en_sqd_async_notify = 0;
628 attr->sq_draining = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700629 attr->max_rd_atomic = qp->s_max_rd_atomic;
630 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700631 attr->min_rnr_timer = qp->r_min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800632 attr->port_num = 1;
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700633 attr->timeout = qp->timeout;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800634 attr->retry_cnt = qp->s_retry_cnt;
635 attr->rnr_retry = qp->s_rnr_retry;
636 attr->alt_port_num = 0;
637 attr->alt_timeout = 0;
638
639 init_attr->event_handler = qp->ibqp.event_handler;
640 init_attr->qp_context = qp->ibqp.qp_context;
641 init_attr->send_cq = qp->ibqp.send_cq;
642 init_attr->recv_cq = qp->ibqp.recv_cq;
643 init_attr->srq = qp->ibqp.srq;
644 init_attr->cap = attr->cap;
Ralph Campbell3859e392007-03-15 14:44:51 -0700645 if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700646 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
647 else
648 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800649 init_attr->qp_type = qp->ibqp.qp_type;
650 init_attr->port_num = 1;
651 return 0;
652}
653
654/**
655 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
656 * @qp: the queue pair to compute the AETH for
657 *
658 * Returns the AETH.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800659 */
660__be32 ipath_compute_aeth(struct ipath_qp *qp)
661{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700662 u32 aeth = qp->r_msn & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800663
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700664 if (qp->ibqp.srq) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800665 /*
666 * Shared receive queues don't generate credits.
667 * Set the credit field to the invalid value.
668 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700669 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800670 } else {
671 u32 min, max, x;
672 u32 credits;
Ralph Campbell373d9912006-09-22 15:22:26 -0700673 struct ipath_rwq *wq = qp->r_rq.wq;
674 u32 head;
675 u32 tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800676
Ralph Campbell373d9912006-09-22 15:22:26 -0700677 /* sanity check pointers before trusting them */
678 head = wq->head;
679 if (head >= qp->r_rq.size)
680 head = 0;
681 tail = wq->tail;
682 if (tail >= qp->r_rq.size)
683 tail = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800684 /*
685 * Compute the number of credits available (RWQEs).
686 * XXX Not holding the r_rq.lock here so there is a small
687 * chance that the pair of reads are not atomic.
688 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700689 credits = head - tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800690 if ((int)credits < 0)
691 credits += qp->r_rq.size;
692 /*
693 * Binary search the credit table to find the code to
694 * use.
695 */
696 min = 0;
697 max = 31;
698 for (;;) {
699 x = (min + max) / 2;
700 if (credit_table[x] == credits)
701 break;
702 if (credit_table[x] > credits)
703 max = x;
704 else if (min == x)
705 break;
706 else
707 min = x;
708 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700709 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800710 }
711 return cpu_to_be32(aeth);
712}
713
714/**
715 * ipath_create_qp - create a queue pair for a device
716 * @ibpd: the protection domain who's device we create the queue pair for
717 * @init_attr: the attributes of the queue pair
718 * @udata: unused by InfiniPath
719 *
720 * Returns the queue pair on success, otherwise returns an errno.
721 *
722 * Called by the ib_create_qp() core verbs function.
723 */
724struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
725 struct ib_qp_init_attr *init_attr,
726 struct ib_udata *udata)
727{
728 struct ipath_qp *qp;
729 int err;
730 struct ipath_swqe *swq = NULL;
731 struct ipath_ibdev *dev;
732 size_t sz;
733 struct ib_qp *ret;
734
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700735 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
736 init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
737 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
738 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800739 ret = ERR_PTR(-ENOMEM);
740 goto bail;
741 }
742
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700743 if (init_attr->cap.max_send_sge +
744 init_attr->cap.max_recv_sge +
745 init_attr->cap.max_send_wr +
746 init_attr->cap.max_recv_wr == 0) {
747 ret = ERR_PTR(-EINVAL);
748 goto bail;
749 }
750
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800751 switch (init_attr->qp_type) {
752 case IB_QPT_UC:
753 case IB_QPT_RC:
754 sz = sizeof(struct ipath_sge) *
755 init_attr->cap.max_send_sge +
756 sizeof(struct ipath_swqe);
757 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
758 if (swq == NULL) {
759 ret = ERR_PTR(-ENOMEM);
760 goto bail;
761 }
762 /* FALLTHROUGH */
763 case IB_QPT_UD:
764 case IB_QPT_SMI:
765 case IB_QPT_GSI:
Ralph Campbell373d9912006-09-22 15:22:26 -0700766 sz = sizeof(*qp);
767 if (init_attr->srq) {
768 struct ipath_srq *srq = to_isrq(init_attr->srq);
769
770 sz += sizeof(*qp->r_sg_list) *
771 srq->rq.max_sge;
772 } else
773 sz += sizeof(*qp->r_sg_list) *
774 init_attr->cap.max_recv_sge;
775 qp = kmalloc(sz, GFP_KERNEL);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800776 if (!qp) {
777 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700778 goto bail_swq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800779 }
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700780 if (init_attr->srq) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700781 sz = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700782 qp->r_rq.size = 0;
783 qp->r_rq.max_sge = 0;
784 qp->r_rq.wq = NULL;
Ralph Campbell373d9912006-09-22 15:22:26 -0700785 init_attr->cap.max_recv_wr = 0;
786 init_attr->cap.max_recv_sge = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700787 } else {
788 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
789 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
Ralph Campbell373d9912006-09-22 15:22:26 -0700790 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700791 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700792 qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
793 qp->r_rq.size * sz);
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700794 if (!qp->r_rq.wq) {
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700795 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700796 goto bail_qp;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700797 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800798 }
799
800 /*
801 * ib_create_qp() will initialize qp->ibqp
802 * except for qp->ibqp.qp_num.
803 */
804 spin_lock_init(&qp->s_lock);
805 spin_lock_init(&qp->r_rq.lock);
806 atomic_set(&qp->refcount, 0);
807 init_waitqueue_head(&qp->wait);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700808 tasklet_init(&qp->s_task, ipath_do_ruc_send,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800809 (unsigned long)qp);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700810 INIT_LIST_HEAD(&qp->piowait);
811 INIT_LIST_HEAD(&qp->timerwait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800812 qp->state = IB_QPS_RESET;
813 qp->s_wq = swq;
814 qp->s_size = init_attr->cap.max_send_wr + 1;
815 qp->s_max_sge = init_attr->cap.max_send_sge;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700816 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
Ralph Campbell3859e392007-03-15 14:44:51 -0700817 qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700818 else
819 qp->s_flags = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800820 dev = to_idev(ibpd->device);
821 err = ipath_alloc_qpn(&dev->qp_table, qp,
822 init_attr->qp_type);
823 if (err) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800824 ret = ERR_PTR(err);
Ralph Campbell373d9912006-09-22 15:22:26 -0700825 goto bail_rwq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800826 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700827 qp->ip = NULL;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800828 ipath_reset_qp(qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800829 break;
830
831 default:
832 /* Don't support raw QPs */
833 ret = ERR_PTR(-ENOSYS);
834 goto bail;
835 }
836
837 init_attr->cap.max_inline_data = 0;
838
Ralph Campbell373d9912006-09-22 15:22:26 -0700839 /*
840 * Return the address of the RWQ as the offset to mmap.
841 * See ipath_mmap() for details.
842 */
843 if (udata && udata->outlen >= sizeof(__u64)) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700844 int err;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800845
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700846 if (!qp->r_rq.wq) {
847 __u64 offset = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700848
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700849 err = ib_copy_to_udata(udata, &offset,
850 sizeof(offset));
851 if (err) {
852 ret = ERR_PTR(err);
853 goto bail_rwq;
854 }
855 } else {
856 u32 s = sizeof(struct ipath_rwq) +
857 qp->r_rq.size * sz;
858
859 qp->ip =
860 ipath_create_mmap_info(dev, s,
861 ibpd->uobject->context,
862 qp->r_rq.wq);
863 if (!qp->ip) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700864 ret = ERR_PTR(-ENOMEM);
865 goto bail_rwq;
866 }
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700867
868 err = ib_copy_to_udata(udata, &(qp->ip->offset),
869 sizeof(qp->ip->offset));
870 if (err) {
871 ret = ERR_PTR(err);
872 goto bail_ip;
873 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700874 }
875 }
876
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700877 spin_lock(&dev->n_qps_lock);
878 if (dev->n_qps_allocated == ib_ipath_max_qps) {
879 spin_unlock(&dev->n_qps_lock);
880 ret = ERR_PTR(-ENOMEM);
881 goto bail_ip;
882 }
883
884 dev->n_qps_allocated++;
885 spin_unlock(&dev->n_qps_lock);
886
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700887 if (qp->ip) {
888 spin_lock_irq(&dev->pending_lock);
889 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
890 spin_unlock_irq(&dev->pending_lock);
891 }
892
Ralph Campbell373d9912006-09-22 15:22:26 -0700893 ret = &qp->ibqp;
894 goto bail;
895
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700896bail_ip:
897 kfree(qp->ip);
Ralph Campbell373d9912006-09-22 15:22:26 -0700898bail_rwq:
899 vfree(qp->r_rq.wq);
900bail_qp:
901 kfree(qp);
902bail_swq:
903 vfree(swq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800904bail:
905 return ret;
906}
907
908/**
909 * ipath_destroy_qp - destroy a queue pair
910 * @ibqp: the queue pair to destroy
911 *
912 * Returns 0 on success.
913 *
914 * Note that this can be called while the QP is actively sending or
915 * receiving!
916 */
917int ipath_destroy_qp(struct ib_qp *ibqp)
918{
919 struct ipath_qp *qp = to_iqp(ibqp);
920 struct ipath_ibdev *dev = to_idev(ibqp->device);
921 unsigned long flags;
922
Ralph Campbell373d9912006-09-22 15:22:26 -0700923 spin_lock_irqsave(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800924 qp->state = IB_QPS_ERR;
Ralph Campbell373d9912006-09-22 15:22:26 -0700925 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700926 spin_lock(&dev->n_qps_lock);
927 dev->n_qps_allocated--;
928 spin_unlock(&dev->n_qps_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800929
930 /* Stop the sending tasklet. */
931 tasklet_kill(&qp->s_task);
932
933 /* Make sure the QP isn't on the timeout list. */
934 spin_lock_irqsave(&dev->pending_lock, flags);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700935 if (!list_empty(&qp->timerwait))
936 list_del_init(&qp->timerwait);
937 if (!list_empty(&qp->piowait))
938 list_del_init(&qp->piowait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800939 spin_unlock_irqrestore(&dev->pending_lock, flags);
940
941 /*
942 * Make sure that the QP is not in the QPN table so receive
943 * interrupts will discard packets for this QP. XXX Also remove QP
944 * from multicast table.
945 */
946 if (atomic_read(&qp->refcount) != 0)
947 ipath_free_qp(&dev->qp_table, qp);
948
Ralph Campbell373d9912006-09-22 15:22:26 -0700949 if (qp->ip)
950 kref_put(&qp->ip->ref, ipath_release_mmap_info);
951 else
952 vfree(qp->r_rq.wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800953 vfree(qp->s_wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800954 kfree(qp);
955 return 0;
956}
957
958/**
959 * ipath_init_qp_table - initialize the QP table for a device
960 * @idev: the device who's QP table we're initializing
961 * @size: the size of the QP table
962 *
963 * Returns 0 on success, otherwise returns an errno.
964 */
965int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
966{
967 int i;
968 int ret;
969
970 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
971 idev->qp_table.max = size;
972 idev->qp_table.nmaps = 1;
973 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
974 GFP_KERNEL);
975 if (idev->qp_table.table == NULL) {
976 ret = -ENOMEM;
977 goto bail;
978 }
979
980 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
981 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
982 idev->qp_table.map[i].page = NULL;
983 }
984
985 ret = 0;
986
987bail:
988 return ret;
989}
990
991/**
992 * ipath_sqerror_qp - put a QP's send queue into an error state
993 * @qp: QP who's send queue will be put into an error state
994 * @wc: the WC responsible for putting the QP in this state
995 *
996 * Flushes the send work queue.
Ralph Campbell0434d272007-03-15 14:44:53 -0700997 * The QP s_lock should be held and interrupts disabled.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800998 */
999
1000void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
1001{
1002 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
1003 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
1004
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -07001005 ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001006 qp->ibqp.qp_num, qp->remote_qpn, wc->status);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001007
1008 spin_lock(&dev->pending_lock);
1009 /* XXX What if its already removed by the timeout code? */
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -07001010 if (!list_empty(&qp->timerwait))
1011 list_del_init(&qp->timerwait);
1012 if (!list_empty(&qp->piowait))
1013 list_del_init(&qp->piowait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001014 spin_unlock(&dev->pending_lock);
1015
1016 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1017 if (++qp->s_last >= qp->s_size)
1018 qp->s_last = 0;
1019
1020 wc->status = IB_WC_WR_FLUSH_ERR;
1021
1022 while (qp->s_last != qp->s_head) {
Ralph Campbell0434d272007-03-15 14:44:53 -07001023 wqe = get_swqe_ptr(qp, qp->s_last);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001024 wc->wr_id = wqe->wr.wr_id;
1025 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1026 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1027 if (++qp->s_last >= qp->s_size)
1028 qp->s_last = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001029 }
1030 qp->s_cur = qp->s_tail = qp->s_head;
1031 qp->state = IB_QPS_SQE;
1032}
1033
1034/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001035 * ipath_get_credit - flush the send work queue of a QP
1036 * @qp: the qp who's send work queue to flush
1037 * @aeth: the Acknowledge Extended Transport Header
1038 *
1039 * The QP s_lock should be held.
1040 */
1041void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1042{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001043 u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001044
1045 /*
1046 * If the credit is invalid, we can send
1047 * as many packets as we like. Otherwise, we have to
1048 * honor the credit field.
1049 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001050 if (credit == IPATH_AETH_CREDIT_INVAL)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001051 qp->s_lsn = (u32) -1;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001052 else if (qp->s_lsn != (u32) -1) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001053 /* Compute new LSN (i.e., MSN + credit) */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001054 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001055 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1056 qp->s_lsn = credit;
1057 }
1058
1059 /* Restart sending if it was blocked due to lack of credits. */
1060 if (qp->s_cur != qp->s_head &&
1061 (qp->s_lsn == (u32) -1 ||
1062 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1063 qp->s_lsn + 1) <= 0))
1064 tasklet_hi_schedule(&qp->s_task);
1065}