blob: 4715911101e4fff8363c0e748929302a213d05bc [file] [log] [blame]
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001/*
Ralph Campbelle7eacd32008-04-16 21:09:32 -07002 * Copyright (c) 2006, 2007, 2008 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;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800245
246 spin_lock_irqsave(&qpt->lock, flags);
247
248 /* Remove QP from the hash table. */
249 qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
250 for (; (q = *qpp) != NULL; qpp = &q->next) {
251 if (q == qp) {
252 *qpp = qp->next;
253 qp->next = NULL;
254 atomic_dec(&qp->refcount);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800255 break;
256 }
257 }
258
259 spin_unlock_irqrestore(&qpt->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800260}
261
262/**
Ralph Campbelle509be82008-05-13 11:41:29 -0700263 * ipath_free_all_qps - check for QPs still in use
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800264 * @qpt: the QP table to empty
Ralph Campbelle509be82008-05-13 11:41:29 -0700265 *
266 * There should not be any QPs still in use.
267 * Free memory for table.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800268 */
Ralph Campbelle509be82008-05-13 11:41:29 -0700269unsigned ipath_free_all_qps(struct ipath_qp_table *qpt)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800270{
271 unsigned long flags;
Ralph Campbelle509be82008-05-13 11:41:29 -0700272 struct ipath_qp *qp;
273 u32 n, qp_inuse = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800274
Ralph Campbelle509be82008-05-13 11:41:29 -0700275 spin_lock_irqsave(&qpt->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800276 for (n = 0; n < qpt->max; n++) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800277 qp = qpt->table[n];
278 qpt->table[n] = NULL;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800279
Ralph Campbelle509be82008-05-13 11:41:29 -0700280 for (; qp; qp = qp->next)
281 qp_inuse++;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800282 }
Ralph Campbelle509be82008-05-13 11:41:29 -0700283 spin_unlock_irqrestore(&qpt->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800284
Ralph Campbelle509be82008-05-13 11:41:29 -0700285 for (n = 0; n < ARRAY_SIZE(qpt->map); n++)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800286 if (qpt->map[n].page)
Ralph Campbelle509be82008-05-13 11:41:29 -0700287 free_page((unsigned long) qpt->map[n].page);
288 return qp_inuse;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800289}
290
291/**
292 * ipath_lookup_qpn - return the QP with the given QPN
293 * @qpt: the QP table
294 * @qpn: the QP number to look up
295 *
296 * The caller is responsible for decrementing the QP reference count
297 * when done.
298 */
299struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
300{
301 unsigned long flags;
302 struct ipath_qp *qp;
303
304 spin_lock_irqsave(&qpt->lock, flags);
305
306 for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
307 if (qp->ibqp.qp_num == qpn) {
308 atomic_inc(&qp->refcount);
309 break;
310 }
311 }
312
313 spin_unlock_irqrestore(&qpt->lock, flags);
314 return qp;
315}
316
317/**
318 * ipath_reset_qp - initialize the QP state to the reset state
319 * @qp: the QP to reset
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800320 * @type: the QP type
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800321 */
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800322static void ipath_reset_qp(struct ipath_qp *qp, enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800323{
324 qp->remote_qpn = 0;
325 qp->qkey = 0;
326 qp->qp_access_flags = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700327 atomic_set(&qp->s_dma_busy, 0);
Robert Walsh991bda22007-06-04 09:55:48 -0700328 qp->s_flags &= IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800329 qp->s_hdrwords = 0;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700330 qp->s_wqe = NULL;
Dave Olson124b4dc2008-04-16 21:09:32 -0700331 qp->s_pkt_delay = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700332 qp->s_draining = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800333 qp->s_psn = 0;
334 qp->r_psn = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700335 qp->r_msn = 0;
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800336 if (type == IB_QPT_RC) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800337 qp->s_state = IB_OPCODE_RC_SEND_LAST;
338 qp->r_state = IB_OPCODE_RC_SEND_LAST;
339 } else {
340 qp->s_state = IB_OPCODE_UC_SEND_LAST;
341 qp->r_state = IB_OPCODE_UC_SEND_LAST;
342 }
343 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700344 qp->r_nak_state = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700345 qp->r_aflags = 0;
346 qp->r_flags = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800347 qp->s_rnr_timeout = 0;
348 qp->s_head = 0;
349 qp->s_tail = 0;
350 qp->s_cur = 0;
351 qp->s_last = 0;
352 qp->s_ssn = 1;
353 qp->s_lsn = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700354 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
355 qp->r_head_ack_queue = 0;
356 qp->s_tail_ack_queue = 0;
357 qp->s_num_rd_atomic = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700358 if (qp->r_rq.wq) {
359 qp->r_rq.wq->head = 0;
360 qp->r_rq.wq->tail = 0;
361 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800362}
363
364/**
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700365 * ipath_error_qp - put a QP into the error state
366 * @qp: the QP to put into the error state
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700367 * @err: the receive completion error to signal if a RWQE is active
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700368 *
369 * Flushes both send and receive work queues.
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700370 * Returns true if last WQE event should be generated.
Ralph Campbell0434d272007-03-15 14:44:53 -0700371 * The QP s_lock should be held and interrupts disabled.
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700372 * If we are already in error state, just return.
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700373 */
374
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700375int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700376{
377 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
378 struct ib_wc wc;
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700379 int ret = 0;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700380
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700381 if (qp->state == IB_QPS_ERR)
382 goto bail;
383
384 qp->state = IB_QPS_ERR;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700385
386 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700387 if (!list_empty(&qp->timerwait))
388 list_del_init(&qp->timerwait);
389 if (!list_empty(&qp->piowait))
390 list_del_init(&qp->piowait);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700391 spin_unlock(&dev->pending_lock);
392
Ralph Campbelle509be82008-05-13 11:41:29 -0700393 /* Schedule the sending tasklet to drain the send work queue. */
394 if (qp->s_last != qp->s_head)
395 ipath_schedule_send(qp);
396
397 memset(&wc, 0, sizeof(wc));
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200398 wc.qp = &qp->ibqp;
Ralph Campbelle509be82008-05-13 11:41:29 -0700399 wc.opcode = IB_WC_RECV;
400
401 if (test_and_clear_bit(IPATH_R_WRID_VALID, &qp->r_aflags)) {
Ralph Campbell0434d272007-03-15 14:44:53 -0700402 wc.wr_id = qp->r_wr_id;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700403 wc.status = err;
Patrick Marchand Latifi2a049e52008-01-31 00:24:37 -0800404 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700405 }
406 wc.status = IB_WC_WR_FLUSH_ERR;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700407
Ralph Campbell373d9912006-09-22 15:22:26 -0700408 if (qp->r_rq.wq) {
409 struct ipath_rwq *wq;
410 u32 head;
411 u32 tail;
412
413 spin_lock(&qp->r_rq.lock);
414
415 /* sanity check pointers before trusting them */
416 wq = qp->r_rq.wq;
417 head = wq->head;
418 if (head >= qp->r_rq.size)
419 head = 0;
420 tail = wq->tail;
421 if (tail >= qp->r_rq.size)
422 tail = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700423 while (tail != head) {
424 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
425 if (++tail >= qp->r_rq.size)
426 tail = 0;
427 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
428 }
429 wq->tail = tail;
430
431 spin_unlock(&qp->r_rq.lock);
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700432 } else if (qp->ibqp.event_handler)
433 ret = 1;
434
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700435bail:
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700436 return ret;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700437}
438
439/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800440 * ipath_modify_qp - modify the attributes of a queue pair
441 * @ibqp: the queue pair who's attributes we're modifying
442 * @attr: the new attributes
443 * @attr_mask: the mask of attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700444 * @udata: user data for ipathverbs.so
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800445 *
446 * Returns 0 on success, otherwise returns an errno.
447 */
448int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700449 int attr_mask, struct ib_udata *udata)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800450{
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700451 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800452 struct ipath_qp *qp = to_iqp(ibqp);
453 enum ib_qp_state cur_state, new_state;
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700454 int lastwqe = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800455 int ret;
456
Ralph Campbelle509be82008-05-13 11:41:29 -0700457 spin_lock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800458
459 cur_state = attr_mask & IB_QP_CUR_STATE ?
460 attr->cur_qp_state : qp->state;
461 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
462
463 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
464 attr_mask))
465 goto inval;
466
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700467 if (attr_mask & IB_QP_AV) {
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700468 if (attr->ah_attr.dlid == 0 ||
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700469 attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700470 goto inval;
471
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700472 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
473 (attr->ah_attr.grh.sgid_index > 1))
474 goto inval;
475 }
476
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700477 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700478 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700479 goto inval;
480
481 if (attr_mask & IB_QP_MIN_RNR_TIMER)
482 if (attr->min_rnr_timer > 31)
483 goto inval;
484
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700485 if (attr_mask & IB_QP_PORT)
486 if (attr->port_num == 0 ||
487 attr->port_num > ibqp->device->phys_port_cnt)
488 goto inval;
489
Robert Walshe7340f02007-06-18 14:24:35 -0700490 /*
Dave Olson826d8012008-04-16 21:01:12 -0700491 * don't allow invalid Path MTU values or greater than 2048
492 * unless we are configured for a 4KB MTU
Robert Walshe7340f02007-06-18 14:24:35 -0700493 */
Dave Olson826d8012008-04-16 21:01:12 -0700494 if ((attr_mask & IB_QP_PATH_MTU) &&
495 (ib_mtu_enum_to_int(attr->path_mtu) == -1 ||
496 (attr->path_mtu > IB_MTU_2048 && !ipath_mtu4096)))
497 goto inval;
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700498
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700499 if (attr_mask & IB_QP_PATH_MIG_STATE)
Bryan O'Sullivanca4ce382006-08-25 11:24:42 -0700500 if (attr->path_mig_state != IB_MIG_MIGRATED &&
501 attr->path_mig_state != IB_MIG_REARM)
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700502 goto inval;
503
Ralph Campbell3859e392007-03-15 14:44:51 -0700504 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
505 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
506 goto inval;
507
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800508 switch (new_state) {
509 case IB_QPS_RESET:
Ralph Campbelle509be82008-05-13 11:41:29 -0700510 if (qp->state != IB_QPS_RESET) {
511 qp->state = IB_QPS_RESET;
512 spin_lock(&dev->pending_lock);
513 if (!list_empty(&qp->timerwait))
514 list_del_init(&qp->timerwait);
515 if (!list_empty(&qp->piowait))
516 list_del_init(&qp->piowait);
517 spin_unlock(&dev->pending_lock);
518 qp->s_flags &= ~IPATH_S_ANY_WAIT;
519 spin_unlock_irq(&qp->s_lock);
520 /* Stop the sending tasklet */
521 tasklet_kill(&qp->s_task);
522 wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
523 spin_lock_irq(&qp->s_lock);
524 }
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800525 ipath_reset_qp(qp, ibqp->qp_type);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800526 break;
527
Ralph Campbelle509be82008-05-13 11:41:29 -0700528 case IB_QPS_SQD:
529 qp->s_draining = qp->s_last != qp->s_cur;
530 qp->state = new_state;
531 break;
532
533 case IB_QPS_SQE:
534 if (qp->ibqp.qp_type == IB_QPT_RC)
535 goto inval;
536 qp->state = new_state;
537 break;
538
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800539 case IB_QPS_ERR:
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700540 lastwqe = ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800541 break;
542
543 default:
Ralph Campbelle509be82008-05-13 11:41:29 -0700544 qp->state = new_state;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800545 break;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800546 }
547
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700548 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800549 qp->s_pkey_index = attr->pkey_index;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800550
551 if (attr_mask & IB_QP_DEST_QPN)
552 qp->remote_qpn = attr->dest_qp_num;
553
554 if (attr_mask & IB_QP_SQ_PSN) {
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700555 qp->s_psn = qp->s_next_psn = attr->sq_psn;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800556 qp->s_last_psn = qp->s_next_psn - 1;
557 }
558
559 if (attr_mask & IB_QP_RQ_PSN)
560 qp->r_psn = attr->rq_psn;
561
562 if (attr_mask & IB_QP_ACCESS_FLAGS)
563 qp->qp_access_flags = attr->qp_access_flags;
564
Dave Olson124b4dc2008-04-16 21:09:32 -0700565 if (attr_mask & IB_QP_AV) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800566 qp->remote_ah_attr = attr->ah_attr;
Dave Olson124b4dc2008-04-16 21:09:32 -0700567 qp->s_dmult = ipath_ib_rate_to_mult(attr->ah_attr.static_rate);
568 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800569
570 if (attr_mask & IB_QP_PATH_MTU)
571 qp->path_mtu = attr->path_mtu;
572
573 if (attr_mask & IB_QP_RETRY_CNT)
574 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
575
576 if (attr_mask & IB_QP_RNR_RETRY) {
577 qp->s_rnr_retry = attr->rnr_retry;
578 if (qp->s_rnr_retry > 7)
579 qp->s_rnr_retry = 7;
580 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
581 }
582
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700583 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700584 qp->r_min_rnr_timer = attr->min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800585
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700586 if (attr_mask & IB_QP_TIMEOUT)
587 qp->timeout = attr->timeout;
588
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800589 if (attr_mask & IB_QP_QKEY)
590 qp->qkey = attr->qkey;
591
Ralph Campbell3859e392007-03-15 14:44:51 -0700592 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
593 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
594
595 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
596 qp->s_max_rd_atomic = attr->max_rd_atomic;
597
Ralph Campbelle509be82008-05-13 11:41:29 -0700598 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800599
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700600 if (lastwqe) {
601 struct ib_event ev;
602
603 ev.device = qp->ibqp.device;
604 ev.element.qp = &qp->ibqp;
605 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
606 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
607 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800608 ret = 0;
609 goto bail;
610
611inval:
Ralph Campbelle509be82008-05-13 11:41:29 -0700612 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800613 ret = -EINVAL;
614
615bail:
616 return ret;
617}
618
619int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
620 int attr_mask, struct ib_qp_init_attr *init_attr)
621{
622 struct ipath_qp *qp = to_iqp(ibqp);
623
624 attr->qp_state = qp->state;
625 attr->cur_qp_state = attr->qp_state;
626 attr->path_mtu = qp->path_mtu;
627 attr->path_mig_state = 0;
628 attr->qkey = qp->qkey;
629 attr->rq_psn = qp->r_psn;
630 attr->sq_psn = qp->s_next_psn;
631 attr->dest_qp_num = qp->remote_qpn;
632 attr->qp_access_flags = qp->qp_access_flags;
633 attr->cap.max_send_wr = qp->s_size - 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700634 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800635 attr->cap.max_send_sge = qp->s_max_sge;
636 attr->cap.max_recv_sge = qp->r_rq.max_sge;
637 attr->cap.max_inline_data = 0;
638 attr->ah_attr = qp->remote_ah_attr;
639 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
640 attr->pkey_index = qp->s_pkey_index;
641 attr->alt_pkey_index = 0;
642 attr->en_sqd_async_notify = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700643 attr->sq_draining = qp->s_draining;
Ralph Campbell3859e392007-03-15 14:44:51 -0700644 attr->max_rd_atomic = qp->s_max_rd_atomic;
645 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700646 attr->min_rnr_timer = qp->r_min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800647 attr->port_num = 1;
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700648 attr->timeout = qp->timeout;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800649 attr->retry_cnt = qp->s_retry_cnt;
Patrick Marchand Latifi87d5aed2008-01-07 23:43:04 -0800650 attr->rnr_retry = qp->s_rnr_retry_cnt;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800651 attr->alt_port_num = 0;
652 attr->alt_timeout = 0;
653
654 init_attr->event_handler = qp->ibqp.event_handler;
655 init_attr->qp_context = qp->ibqp.qp_context;
656 init_attr->send_cq = qp->ibqp.send_cq;
657 init_attr->recv_cq = qp->ibqp.recv_cq;
658 init_attr->srq = qp->ibqp.srq;
659 init_attr->cap = attr->cap;
Ralph Campbell3859e392007-03-15 14:44:51 -0700660 if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700661 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
662 else
663 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800664 init_attr->qp_type = qp->ibqp.qp_type;
665 init_attr->port_num = 1;
666 return 0;
667}
668
669/**
670 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
671 * @qp: the queue pair to compute the AETH for
672 *
673 * Returns the AETH.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800674 */
675__be32 ipath_compute_aeth(struct ipath_qp *qp)
676{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700677 u32 aeth = qp->r_msn & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800678
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700679 if (qp->ibqp.srq) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800680 /*
681 * Shared receive queues don't generate credits.
682 * Set the credit field to the invalid value.
683 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700684 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800685 } else {
686 u32 min, max, x;
687 u32 credits;
Ralph Campbell373d9912006-09-22 15:22:26 -0700688 struct ipath_rwq *wq = qp->r_rq.wq;
689 u32 head;
690 u32 tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800691
Ralph Campbell373d9912006-09-22 15:22:26 -0700692 /* sanity check pointers before trusting them */
693 head = wq->head;
694 if (head >= qp->r_rq.size)
695 head = 0;
696 tail = wq->tail;
697 if (tail >= qp->r_rq.size)
698 tail = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800699 /*
700 * Compute the number of credits available (RWQEs).
701 * XXX Not holding the r_rq.lock here so there is a small
702 * chance that the pair of reads are not atomic.
703 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700704 credits = head - tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800705 if ((int)credits < 0)
706 credits += qp->r_rq.size;
707 /*
708 * Binary search the credit table to find the code to
709 * use.
710 */
711 min = 0;
712 max = 31;
713 for (;;) {
714 x = (min + max) / 2;
715 if (credit_table[x] == credits)
716 break;
717 if (credit_table[x] > credits)
718 max = x;
719 else if (min == x)
720 break;
721 else
722 min = x;
723 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700724 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800725 }
726 return cpu_to_be32(aeth);
727}
728
729/**
730 * ipath_create_qp - create a queue pair for a device
731 * @ibpd: the protection domain who's device we create the queue pair for
732 * @init_attr: the attributes of the queue pair
733 * @udata: unused by InfiniPath
734 *
735 * Returns the queue pair on success, otherwise returns an errno.
736 *
737 * Called by the ib_create_qp() core verbs function.
738 */
739struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
740 struct ib_qp_init_attr *init_attr,
741 struct ib_udata *udata)
742{
743 struct ipath_qp *qp;
744 int err;
745 struct ipath_swqe *swq = NULL;
746 struct ipath_ibdev *dev;
747 size_t sz;
748 struct ib_qp *ret;
749
Eli Cohenb846f252008-04-16 21:09:27 -0700750 if (init_attr->create_flags) {
751 ret = ERR_PTR(-EINVAL);
752 goto bail;
753 }
754
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700755 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
Ralph Campbell10a8c3c2008-04-16 21:09:25 -0700756 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs) {
757 ret = ERR_PTR(-EINVAL);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800758 goto bail;
759 }
760
Ralph Campbell10a8c3c2008-04-16 21:09:25 -0700761 /* Check receive queue parameters if no SRQ is specified. */
762 if (!init_attr->srq) {
763 if (init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
764 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
765 ret = ERR_PTR(-EINVAL);
766 goto bail;
767 }
768 if (init_attr->cap.max_send_sge +
769 init_attr->cap.max_send_wr +
770 init_attr->cap.max_recv_sge +
771 init_attr->cap.max_recv_wr == 0) {
772 ret = ERR_PTR(-EINVAL);
773 goto bail;
774 }
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700775 }
776
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800777 switch (init_attr->qp_type) {
778 case IB_QPT_UC:
779 case IB_QPT_RC:
Ralph Campbell4ee97182007-07-25 11:08:28 -0700780 case IB_QPT_UD:
781 case IB_QPT_SMI:
782 case IB_QPT_GSI:
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800783 sz = sizeof(struct ipath_sge) *
784 init_attr->cap.max_send_sge +
785 sizeof(struct ipath_swqe);
786 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
787 if (swq == NULL) {
788 ret = ERR_PTR(-ENOMEM);
789 goto bail;
790 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700791 sz = sizeof(*qp);
792 if (init_attr->srq) {
793 struct ipath_srq *srq = to_isrq(init_attr->srq);
794
795 sz += sizeof(*qp->r_sg_list) *
796 srq->rq.max_sge;
797 } else
798 sz += sizeof(*qp->r_sg_list) *
799 init_attr->cap.max_recv_sge;
800 qp = kmalloc(sz, GFP_KERNEL);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800801 if (!qp) {
802 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700803 goto bail_swq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800804 }
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700805 if (init_attr->srq) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700806 sz = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700807 qp->r_rq.size = 0;
808 qp->r_rq.max_sge = 0;
809 qp->r_rq.wq = NULL;
Ralph Campbell373d9912006-09-22 15:22:26 -0700810 init_attr->cap.max_recv_wr = 0;
811 init_attr->cap.max_recv_sge = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700812 } else {
813 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
814 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
Ralph Campbell373d9912006-09-22 15:22:26 -0700815 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700816 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700817 qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
818 qp->r_rq.size * sz);
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700819 if (!qp->r_rq.wq) {
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700820 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700821 goto bail_qp;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700822 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800823 }
824
825 /*
826 * ib_create_qp() will initialize qp->ibqp
827 * except for qp->ibqp.qp_num.
828 */
829 spin_lock_init(&qp->s_lock);
830 spin_lock_init(&qp->r_rq.lock);
831 atomic_set(&qp->refcount, 0);
832 init_waitqueue_head(&qp->wait);
Ralph Campbelle509be82008-05-13 11:41:29 -0700833 init_waitqueue_head(&qp->wait_dma);
Ralph Campbell4ee97182007-07-25 11:08:28 -0700834 tasklet_init(&qp->s_task, ipath_do_send, (unsigned long)qp);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700835 INIT_LIST_HEAD(&qp->piowait);
836 INIT_LIST_HEAD(&qp->timerwait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800837 qp->state = IB_QPS_RESET;
838 qp->s_wq = swq;
839 qp->s_size = init_attr->cap.max_send_wr + 1;
840 qp->s_max_sge = init_attr->cap.max_send_sge;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700841 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
Ralph Campbell3859e392007-03-15 14:44:51 -0700842 qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700843 else
844 qp->s_flags = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800845 dev = to_idev(ibpd->device);
846 err = ipath_alloc_qpn(&dev->qp_table, qp,
847 init_attr->qp_type);
848 if (err) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800849 ret = ERR_PTR(err);
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800850 vfree(qp->r_rq.wq);
851 goto bail_qp;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800852 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700853 qp->ip = NULL;
Dave Olson124b4dc2008-04-16 21:09:32 -0700854 qp->s_tx = NULL;
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800855 ipath_reset_qp(qp, init_attr->qp_type);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800856 break;
857
858 default:
859 /* Don't support raw QPs */
860 ret = ERR_PTR(-ENOSYS);
861 goto bail;
862 }
863
864 init_attr->cap.max_inline_data = 0;
865
Ralph Campbell373d9912006-09-22 15:22:26 -0700866 /*
867 * Return the address of the RWQ as the offset to mmap.
868 * See ipath_mmap() for details.
869 */
870 if (udata && udata->outlen >= sizeof(__u64)) {
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700871 if (!qp->r_rq.wq) {
872 __u64 offset = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700873
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700874 err = ib_copy_to_udata(udata, &offset,
875 sizeof(offset));
876 if (err) {
877 ret = ERR_PTR(err);
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800878 goto bail_ip;
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700879 }
880 } else {
881 u32 s = sizeof(struct ipath_rwq) +
882 qp->r_rq.size * sz;
883
884 qp->ip =
885 ipath_create_mmap_info(dev, s,
886 ibpd->uobject->context,
887 qp->r_rq.wq);
888 if (!qp->ip) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700889 ret = ERR_PTR(-ENOMEM);
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800890 goto bail_ip;
Ralph Campbell373d9912006-09-22 15:22:26 -0700891 }
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700892
893 err = ib_copy_to_udata(udata, &(qp->ip->offset),
894 sizeof(qp->ip->offset));
895 if (err) {
896 ret = ERR_PTR(err);
897 goto bail_ip;
898 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700899 }
900 }
901
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700902 spin_lock(&dev->n_qps_lock);
903 if (dev->n_qps_allocated == ib_ipath_max_qps) {
904 spin_unlock(&dev->n_qps_lock);
905 ret = ERR_PTR(-ENOMEM);
906 goto bail_ip;
907 }
908
909 dev->n_qps_allocated++;
910 spin_unlock(&dev->n_qps_lock);
911
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700912 if (qp->ip) {
913 spin_lock_irq(&dev->pending_lock);
914 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
915 spin_unlock_irq(&dev->pending_lock);
916 }
917
Ralph Campbell373d9912006-09-22 15:22:26 -0700918 ret = &qp->ibqp;
919 goto bail;
920
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700921bail_ip:
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800922 if (qp->ip)
923 kref_put(&qp->ip->ref, ipath_release_mmap_info);
924 else
925 vfree(qp->r_rq.wq);
926 ipath_free_qp(&dev->qp_table, qp);
Ralph Campbelle509be82008-05-13 11:41:29 -0700927 free_qpn(&dev->qp_table, qp->ibqp.qp_num);
Ralph Campbell373d9912006-09-22 15:22:26 -0700928bail_qp:
929 kfree(qp);
930bail_swq:
931 vfree(swq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800932bail:
933 return ret;
934}
935
936/**
937 * ipath_destroy_qp - destroy a queue pair
938 * @ibqp: the queue pair to destroy
939 *
940 * Returns 0 on success.
941 *
942 * Note that this can be called while the QP is actively sending or
943 * receiving!
944 */
945int ipath_destroy_qp(struct ib_qp *ibqp)
946{
947 struct ipath_qp *qp = to_iqp(ibqp);
948 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800949
Ralph Campbelle509be82008-05-13 11:41:29 -0700950 /* Make sure HW and driver activity is stopped. */
951 spin_lock_irq(&qp->s_lock);
952 if (qp->state != IB_QPS_RESET) {
953 qp->state = IB_QPS_RESET;
954 spin_lock(&dev->pending_lock);
955 if (!list_empty(&qp->timerwait))
956 list_del_init(&qp->timerwait);
957 if (!list_empty(&qp->piowait))
958 list_del_init(&qp->piowait);
959 spin_unlock(&dev->pending_lock);
960 qp->s_flags &= ~IPATH_S_ANY_WAIT;
961 spin_unlock_irq(&qp->s_lock);
962 /* Stop the sending tasklet */
963 tasklet_kill(&qp->s_task);
964 wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
965 } else
966 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800967
Ralph Campbelle509be82008-05-13 11:41:29 -0700968 ipath_free_qp(&dev->qp_table, qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800969
Dave Olson124b4dc2008-04-16 21:09:32 -0700970 if (qp->s_tx) {
971 atomic_dec(&qp->refcount);
972 if (qp->s_tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEBUF)
973 kfree(qp->s_tx->txreq.map_addr);
Ralph Campbelle509be82008-05-13 11:41:29 -0700974 spin_lock_irq(&dev->pending_lock);
975 list_add(&qp->s_tx->txreq.list, &dev->txreq_free);
976 spin_unlock_irq(&dev->pending_lock);
977 qp->s_tx = NULL;
Dave Olson124b4dc2008-04-16 21:09:32 -0700978 }
979
Ralph Campbelle509be82008-05-13 11:41:29 -0700980 wait_event(qp->wait, !atomic_read(&qp->refcount));
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800981
Ralph Campbelle509be82008-05-13 11:41:29 -0700982 /* all user's cleaned up, mark it available */
983 free_qpn(&dev->qp_table, qp->ibqp.qp_num);
984 spin_lock(&dev->n_qps_lock);
985 dev->n_qps_allocated--;
986 spin_unlock(&dev->n_qps_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800987
Ralph Campbell373d9912006-09-22 15:22:26 -0700988 if (qp->ip)
989 kref_put(&qp->ip->ref, ipath_release_mmap_info);
990 else
991 vfree(qp->r_rq.wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800992 vfree(qp->s_wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800993 kfree(qp);
994 return 0;
995}
996
997/**
998 * ipath_init_qp_table - initialize the QP table for a device
999 * @idev: the device who's QP table we're initializing
1000 * @size: the size of the QP table
1001 *
1002 * Returns 0 on success, otherwise returns an errno.
1003 */
1004int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
1005{
1006 int i;
1007 int ret;
1008
1009 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
1010 idev->qp_table.max = size;
1011 idev->qp_table.nmaps = 1;
1012 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
1013 GFP_KERNEL);
1014 if (idev->qp_table.table == NULL) {
1015 ret = -ENOMEM;
1016 goto bail;
1017 }
1018
1019 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
1020 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
1021 idev->qp_table.map[i].page = NULL;
1022 }
1023
1024 ret = 0;
1025
1026bail:
1027 return ret;
1028}
1029
1030/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001031 * ipath_get_credit - flush the send work queue of a QP
1032 * @qp: the qp who's send work queue to flush
1033 * @aeth: the Acknowledge Extended Transport Header
1034 *
1035 * The QP s_lock should be held.
1036 */
1037void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1038{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001039 u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001040
1041 /*
1042 * If the credit is invalid, we can send
1043 * as many packets as we like. Otherwise, we have to
1044 * honor the credit field.
1045 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001046 if (credit == IPATH_AETH_CREDIT_INVAL)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001047 qp->s_lsn = (u32) -1;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001048 else if (qp->s_lsn != (u32) -1) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001049 /* Compute new LSN (i.e., MSN + credit) */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001050 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001051 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1052 qp->s_lsn = credit;
1053 }
1054
1055 /* Restart sending if it was blocked due to lack of credits. */
Ralph Campbelle509be82008-05-13 11:41:29 -07001056 if ((qp->s_flags & IPATH_S_WAIT_SSN_CREDIT) &&
1057 qp->s_cur != qp->s_head &&
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001058 (qp->s_lsn == (u32) -1 ||
1059 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1060 qp->s_lsn + 1) <= 0))
Ralph Campbelle509be82008-05-13 11:41:29 -07001061 ipath_schedule_send(qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001062}