blob: 0857a9c3cd3d3ca4e731091b5b9ef129cf5024f1 [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040035#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080037#include <linux/vmalloc.h>
38
39#include "ipath_verbs.h"
Ralph Campbell373d9912006-09-22 15:22:26 -070040#include "ipath_kernel.h"
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080041
42#define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
43#define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
44#define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \
45 (off))
46#define find_next_offset(map, off) find_next_zero_bit((map)->page, \
47 BITS_PER_PAGE, off)
48
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080049/*
50 * Convert the AETH credit code into the number of credits.
51 */
52static u32 credit_table[31] = {
53 0, /* 0 */
54 1, /* 1 */
55 2, /* 2 */
56 3, /* 3 */
57 4, /* 4 */
58 6, /* 5 */
59 8, /* 6 */
60 12, /* 7 */
61 16, /* 8 */
62 24, /* 9 */
63 32, /* A */
64 48, /* B */
65 64, /* C */
66 96, /* D */
67 128, /* E */
68 192, /* F */
69 256, /* 10 */
70 384, /* 11 */
71 512, /* 12 */
72 768, /* 13 */
73 1024, /* 14 */
74 1536, /* 15 */
75 2048, /* 16 */
76 3072, /* 17 */
77 4096, /* 18 */
78 6144, /* 19 */
79 8192, /* 1A */
80 12288, /* 1B */
81 16384, /* 1C */
82 24576, /* 1D */
83 32768 /* 1E */
84};
85
Bryan O'Sullivan662af582007-03-15 14:45:12 -070086
87static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map)
88{
89 unsigned long page = get_zeroed_page(GFP_KERNEL);
90 unsigned long flags;
91
92 /*
93 * Free the page if someone raced with us installing it.
94 */
95
96 spin_lock_irqsave(&qpt->lock, flags);
97 if (map->page)
98 free_page(page);
99 else
100 map->page = (void *)page;
101 spin_unlock_irqrestore(&qpt->lock, flags);
102}
103
104
105static int alloc_qpn(struct ipath_qp_table *qpt, enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800106{
107 u32 i, offset, max_scan, qpn;
108 struct qpn_map *map;
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700109 u32 ret = -1;
110
111 if (type == IB_QPT_SMI)
112 ret = 0;
113 else if (type == IB_QPT_GSI)
114 ret = 1;
115
116 if (ret != -1) {
117 map = &qpt->map[0];
118 if (unlikely(!map->page)) {
119 get_map_page(qpt, map);
120 if (unlikely(!map->page)) {
121 ret = -ENOMEM;
122 goto bail;
123 }
124 }
125 if (!test_and_set_bit(ret, map->page))
126 atomic_dec(&map->n_free);
127 else
128 ret = -EBUSY;
129 goto bail;
130 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800131
132 qpn = qpt->last + 1;
133 if (qpn >= QPN_MAX)
134 qpn = 2;
135 offset = qpn & BITS_PER_PAGE_MASK;
136 map = &qpt->map[qpn / BITS_PER_PAGE];
137 max_scan = qpt->nmaps - !offset;
138 for (i = 0;;) {
139 if (unlikely(!map->page)) {
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700140 get_map_page(qpt, map);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800141 if (unlikely(!map->page))
142 break;
143 }
144 if (likely(atomic_read(&map->n_free))) {
145 do {
146 if (!test_and_set_bit(offset, map->page)) {
147 atomic_dec(&map->n_free);
148 qpt->last = qpn;
149 ret = qpn;
150 goto bail;
151 }
152 offset = find_next_offset(map, offset);
153 qpn = mk_qpn(qpt, map, offset);
154 /*
155 * This test differs from alloc_pidmap().
156 * If find_next_offset() does find a zero
157 * bit, we don't need to check for QPN
158 * wrapping around past our starting QPN.
159 * We just need to be sure we don't loop
160 * forever.
161 */
162 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
163 }
164 /*
165 * In order to keep the number of pages allocated to a
166 * minimum, we scan the all existing pages before increasing
167 * the size of the bitmap table.
168 */
169 if (++i > max_scan) {
170 if (qpt->nmaps == QPNMAP_ENTRIES)
171 break;
172 map = &qpt->map[qpt->nmaps++];
173 offset = 0;
174 } else if (map < &qpt->map[qpt->nmaps]) {
175 ++map;
176 offset = 0;
177 } else {
178 map = &qpt->map[0];
179 offset = 2;
180 }
181 qpn = mk_qpn(qpt, map, offset);
182 }
183
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700184 ret = -ENOMEM;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800185
186bail:
187 return ret;
188}
189
190static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
191{
192 struct qpn_map *map;
193
194 map = qpt->map + qpn / BITS_PER_PAGE;
195 if (map->page)
196 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
197 atomic_inc(&map->n_free);
198}
199
200/**
201 * ipath_alloc_qpn - allocate a QP number
202 * @qpt: the QP table
203 * @qp: the QP
204 * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
205 *
206 * Allocate the next available QPN and put the QP into the hash table.
207 * The hash table holds a reference to the QP.
208 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700209static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
210 enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800211{
212 unsigned long flags;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800213 int ret;
214
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700215 ret = alloc_qpn(qpt, type);
216 if (ret < 0)
217 goto bail;
218 qp->ibqp.qp_num = ret;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800219
220 /* Add the QP to the hash table. */
221 spin_lock_irqsave(&qpt->lock, flags);
222
Bryan O'Sullivan662af582007-03-15 14:45:12 -0700223 ret %= qpt->max;
224 qp->next = qpt->table[ret];
225 qpt->table[ret] = qp;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800226 atomic_inc(&qp->refcount);
227
228 spin_unlock_irqrestore(&qpt->lock, flags);
229 ret = 0;
230
231bail:
232 return ret;
233}
234
235/**
236 * ipath_free_qp - remove a QP from the QP table
237 * @qpt: the QP table
238 * @qp: the QP to remove
239 *
240 * Remove the QP from the table so it can't be found asynchronously by
241 * the receive interrupt routine.
242 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700243static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800244{
245 struct ipath_qp *q, **qpp;
246 unsigned long flags;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800247
248 spin_lock_irqsave(&qpt->lock, flags);
249
250 /* Remove QP from the hash table. */
251 qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
252 for (; (q = *qpp) != NULL; qpp = &q->next) {
253 if (q == qp) {
254 *qpp = qp->next;
255 qp->next = NULL;
256 atomic_dec(&qp->refcount);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800257 break;
258 }
259 }
260
261 spin_unlock_irqrestore(&qpt->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800262}
263
264/**
Ralph Campbelle509be82008-05-13 11:41:29 -0700265 * ipath_free_all_qps - check for QPs still in use
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800266 * @qpt: the QP table to empty
Ralph Campbelle509be82008-05-13 11:41:29 -0700267 *
268 * There should not be any QPs still in use.
269 * Free memory for table.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800270 */
Ralph Campbelle509be82008-05-13 11:41:29 -0700271unsigned ipath_free_all_qps(struct ipath_qp_table *qpt)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800272{
273 unsigned long flags;
Ralph Campbelle509be82008-05-13 11:41:29 -0700274 struct ipath_qp *qp;
275 u32 n, qp_inuse = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800276
Ralph Campbelle509be82008-05-13 11:41:29 -0700277 spin_lock_irqsave(&qpt->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800278 for (n = 0; n < qpt->max; n++) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800279 qp = qpt->table[n];
280 qpt->table[n] = NULL;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800281
Ralph Campbelle509be82008-05-13 11:41:29 -0700282 for (; qp; qp = qp->next)
283 qp_inuse++;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800284 }
Ralph Campbelle509be82008-05-13 11:41:29 -0700285 spin_unlock_irqrestore(&qpt->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800286
Ralph Campbelle509be82008-05-13 11:41:29 -0700287 for (n = 0; n < ARRAY_SIZE(qpt->map); n++)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800288 if (qpt->map[n].page)
Ralph Campbelle509be82008-05-13 11:41:29 -0700289 free_page((unsigned long) qpt->map[n].page);
290 return qp_inuse;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800291}
292
293/**
294 * ipath_lookup_qpn - return the QP with the given QPN
295 * @qpt: the QP table
296 * @qpn: the QP number to look up
297 *
298 * The caller is responsible for decrementing the QP reference count
299 * when done.
300 */
301struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
302{
303 unsigned long flags;
304 struct ipath_qp *qp;
305
306 spin_lock_irqsave(&qpt->lock, flags);
307
308 for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
309 if (qp->ibqp.qp_num == qpn) {
310 atomic_inc(&qp->refcount);
311 break;
312 }
313 }
314
315 spin_unlock_irqrestore(&qpt->lock, flags);
316 return qp;
317}
318
319/**
320 * ipath_reset_qp - initialize the QP state to the reset state
321 * @qp: the QP to reset
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800322 * @type: the QP type
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800323 */
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800324static void ipath_reset_qp(struct ipath_qp *qp, enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800325{
326 qp->remote_qpn = 0;
327 qp->qkey = 0;
328 qp->qp_access_flags = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700329 atomic_set(&qp->s_dma_busy, 0);
Robert Walsh991bda22007-06-04 09:55:48 -0700330 qp->s_flags &= IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800331 qp->s_hdrwords = 0;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700332 qp->s_wqe = NULL;
Dave Olson124b4dc2008-04-16 21:09:32 -0700333 qp->s_pkt_delay = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700334 qp->s_draining = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800335 qp->s_psn = 0;
336 qp->r_psn = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700337 qp->r_msn = 0;
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800338 if (type == IB_QPT_RC) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800339 qp->s_state = IB_OPCODE_RC_SEND_LAST;
340 qp->r_state = IB_OPCODE_RC_SEND_LAST;
341 } else {
342 qp->s_state = IB_OPCODE_UC_SEND_LAST;
343 qp->r_state = IB_OPCODE_UC_SEND_LAST;
344 }
345 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700346 qp->r_nak_state = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700347 qp->r_aflags = 0;
348 qp->r_flags = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800349 qp->s_rnr_timeout = 0;
350 qp->s_head = 0;
351 qp->s_tail = 0;
352 qp->s_cur = 0;
353 qp->s_last = 0;
354 qp->s_ssn = 1;
355 qp->s_lsn = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700356 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
357 qp->r_head_ack_queue = 0;
358 qp->s_tail_ack_queue = 0;
359 qp->s_num_rd_atomic = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700360 if (qp->r_rq.wq) {
361 qp->r_rq.wq->head = 0;
362 qp->r_rq.wq->tail = 0;
363 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800364}
365
366/**
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700367 * ipath_error_qp - put a QP into the error state
368 * @qp: the QP to put into the error state
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700369 * @err: the receive completion error to signal if a RWQE is active
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700370 *
371 * Flushes both send and receive work queues.
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700372 * Returns true if last WQE event should be generated.
Ralph Campbell0434d272007-03-15 14:44:53 -0700373 * The QP s_lock should be held and interrupts disabled.
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700374 * If we are already in error state, just return.
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700375 */
376
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700377int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700378{
379 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
380 struct ib_wc wc;
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700381 int ret = 0;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700382
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700383 if (qp->state == IB_QPS_ERR)
384 goto bail;
385
386 qp->state = IB_QPS_ERR;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700387
388 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700389 if (!list_empty(&qp->timerwait))
390 list_del_init(&qp->timerwait);
391 if (!list_empty(&qp->piowait))
392 list_del_init(&qp->piowait);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700393 spin_unlock(&dev->pending_lock);
394
Ralph Campbelle509be82008-05-13 11:41:29 -0700395 /* Schedule the sending tasklet to drain the send work queue. */
396 if (qp->s_last != qp->s_head)
397 ipath_schedule_send(qp);
398
399 memset(&wc, 0, sizeof(wc));
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200400 wc.qp = &qp->ibqp;
Ralph Campbelle509be82008-05-13 11:41:29 -0700401 wc.opcode = IB_WC_RECV;
402
403 if (test_and_clear_bit(IPATH_R_WRID_VALID, &qp->r_aflags)) {
Ralph Campbell0434d272007-03-15 14:44:53 -0700404 wc.wr_id = qp->r_wr_id;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700405 wc.status = err;
Patrick Marchand Latifi2a049e52008-01-31 00:24:37 -0800406 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700407 }
408 wc.status = IB_WC_WR_FLUSH_ERR;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700409
Ralph Campbell373d9912006-09-22 15:22:26 -0700410 if (qp->r_rq.wq) {
411 struct ipath_rwq *wq;
412 u32 head;
413 u32 tail;
414
415 spin_lock(&qp->r_rq.lock);
416
417 /* sanity check pointers before trusting them */
418 wq = qp->r_rq.wq;
419 head = wq->head;
420 if (head >= qp->r_rq.size)
421 head = 0;
422 tail = wq->tail;
423 if (tail >= qp->r_rq.size)
424 tail = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700425 while (tail != head) {
426 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
427 if (++tail >= qp->r_rq.size)
428 tail = 0;
429 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
430 }
431 wq->tail = tail;
432
433 spin_unlock(&qp->r_rq.lock);
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700434 } else if (qp->ibqp.event_handler)
435 ret = 1;
436
Ralph Campbell53dc1ca2008-05-13 11:40:25 -0700437bail:
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700438 return ret;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700439}
440
441/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800442 * ipath_modify_qp - modify the attributes of a queue pair
443 * @ibqp: the queue pair who's attributes we're modifying
444 * @attr: the new attributes
445 * @attr_mask: the mask of attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700446 * @udata: user data for ipathverbs.so
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800447 *
448 * Returns 0 on success, otherwise returns an errno.
449 */
450int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700451 int attr_mask, struct ib_udata *udata)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800452{
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700453 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800454 struct ipath_qp *qp = to_iqp(ibqp);
455 enum ib_qp_state cur_state, new_state;
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700456 int lastwqe = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800457 int ret;
458
Ralph Campbelle509be82008-05-13 11:41:29 -0700459 spin_lock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800460
461 cur_state = attr_mask & IB_QP_CUR_STATE ?
462 attr->cur_qp_state : qp->state;
463 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
464
465 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
466 attr_mask))
467 goto inval;
468
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700469 if (attr_mask & IB_QP_AV) {
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700470 if (attr->ah_attr.dlid == 0 ||
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700471 attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700472 goto inval;
473
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700474 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
475 (attr->ah_attr.grh.sgid_index > 1))
476 goto inval;
477 }
478
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700479 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700480 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700481 goto inval;
482
483 if (attr_mask & IB_QP_MIN_RNR_TIMER)
484 if (attr->min_rnr_timer > 31)
485 goto inval;
486
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700487 if (attr_mask & IB_QP_PORT)
488 if (attr->port_num == 0 ||
489 attr->port_num > ibqp->device->phys_port_cnt)
490 goto inval;
491
Robert Walshe7340f02007-06-18 14:24:35 -0700492 /*
Dave Olson826d8012008-04-16 21:01:12 -0700493 * don't allow invalid Path MTU values or greater than 2048
494 * unless we are configured for a 4KB MTU
Robert Walshe7340f02007-06-18 14:24:35 -0700495 */
Dave Olson826d8012008-04-16 21:01:12 -0700496 if ((attr_mask & IB_QP_PATH_MTU) &&
497 (ib_mtu_enum_to_int(attr->path_mtu) == -1 ||
498 (attr->path_mtu > IB_MTU_2048 && !ipath_mtu4096)))
499 goto inval;
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700500
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700501 if (attr_mask & IB_QP_PATH_MIG_STATE)
Bryan O'Sullivanca4ce382006-08-25 11:24:42 -0700502 if (attr->path_mig_state != IB_MIG_MIGRATED &&
503 attr->path_mig_state != IB_MIG_REARM)
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700504 goto inval;
505
Ralph Campbell3859e392007-03-15 14:44:51 -0700506 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
507 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
508 goto inval;
509
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800510 switch (new_state) {
511 case IB_QPS_RESET:
Ralph Campbelle509be82008-05-13 11:41:29 -0700512 if (qp->state != IB_QPS_RESET) {
513 qp->state = IB_QPS_RESET;
514 spin_lock(&dev->pending_lock);
515 if (!list_empty(&qp->timerwait))
516 list_del_init(&qp->timerwait);
517 if (!list_empty(&qp->piowait))
518 list_del_init(&qp->piowait);
519 spin_unlock(&dev->pending_lock);
520 qp->s_flags &= ~IPATH_S_ANY_WAIT;
521 spin_unlock_irq(&qp->s_lock);
522 /* Stop the sending tasklet */
523 tasklet_kill(&qp->s_task);
524 wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
525 spin_lock_irq(&qp->s_lock);
526 }
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800527 ipath_reset_qp(qp, ibqp->qp_type);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800528 break;
529
Ralph Campbelle509be82008-05-13 11:41:29 -0700530 case IB_QPS_SQD:
531 qp->s_draining = qp->s_last != qp->s_cur;
532 qp->state = new_state;
533 break;
534
535 case IB_QPS_SQE:
536 if (qp->ibqp.qp_type == IB_QPT_RC)
537 goto inval;
538 qp->state = new_state;
539 break;
540
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800541 case IB_QPS_ERR:
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700542 lastwqe = ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800543 break;
544
545 default:
Ralph Campbelle509be82008-05-13 11:41:29 -0700546 qp->state = new_state;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800547 break;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800548 }
549
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700550 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800551 qp->s_pkey_index = attr->pkey_index;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800552
553 if (attr_mask & IB_QP_DEST_QPN)
554 qp->remote_qpn = attr->dest_qp_num;
555
556 if (attr_mask & IB_QP_SQ_PSN) {
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700557 qp->s_psn = qp->s_next_psn = attr->sq_psn;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800558 qp->s_last_psn = qp->s_next_psn - 1;
559 }
560
561 if (attr_mask & IB_QP_RQ_PSN)
562 qp->r_psn = attr->rq_psn;
563
564 if (attr_mask & IB_QP_ACCESS_FLAGS)
565 qp->qp_access_flags = attr->qp_access_flags;
566
Dave Olson124b4dc2008-04-16 21:09:32 -0700567 if (attr_mask & IB_QP_AV) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800568 qp->remote_ah_attr = attr->ah_attr;
Dave Olson124b4dc2008-04-16 21:09:32 -0700569 qp->s_dmult = ipath_ib_rate_to_mult(attr->ah_attr.static_rate);
570 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800571
572 if (attr_mask & IB_QP_PATH_MTU)
573 qp->path_mtu = attr->path_mtu;
574
575 if (attr_mask & IB_QP_RETRY_CNT)
576 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
577
578 if (attr_mask & IB_QP_RNR_RETRY) {
579 qp->s_rnr_retry = attr->rnr_retry;
580 if (qp->s_rnr_retry > 7)
581 qp->s_rnr_retry = 7;
582 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
583 }
584
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700585 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700586 qp->r_min_rnr_timer = attr->min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800587
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700588 if (attr_mask & IB_QP_TIMEOUT)
589 qp->timeout = attr->timeout;
590
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800591 if (attr_mask & IB_QP_QKEY)
592 qp->qkey = attr->qkey;
593
Ralph Campbell3859e392007-03-15 14:44:51 -0700594 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
595 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
596
597 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
598 qp->s_max_rd_atomic = attr->max_rd_atomic;
599
Ralph Campbelle509be82008-05-13 11:41:29 -0700600 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800601
Ralph Campbelld42b01b2007-08-25 16:45:03 -0700602 if (lastwqe) {
603 struct ib_event ev;
604
605 ev.device = qp->ibqp.device;
606 ev.element.qp = &qp->ibqp;
607 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
608 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
609 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800610 ret = 0;
611 goto bail;
612
613inval:
Ralph Campbelle509be82008-05-13 11:41:29 -0700614 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800615 ret = -EINVAL;
616
617bail:
618 return ret;
619}
620
621int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
622 int attr_mask, struct ib_qp_init_attr *init_attr)
623{
624 struct ipath_qp *qp = to_iqp(ibqp);
625
626 attr->qp_state = qp->state;
627 attr->cur_qp_state = attr->qp_state;
628 attr->path_mtu = qp->path_mtu;
629 attr->path_mig_state = 0;
630 attr->qkey = qp->qkey;
631 attr->rq_psn = qp->r_psn;
632 attr->sq_psn = qp->s_next_psn;
633 attr->dest_qp_num = qp->remote_qpn;
634 attr->qp_access_flags = qp->qp_access_flags;
635 attr->cap.max_send_wr = qp->s_size - 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700636 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800637 attr->cap.max_send_sge = qp->s_max_sge;
638 attr->cap.max_recv_sge = qp->r_rq.max_sge;
639 attr->cap.max_inline_data = 0;
640 attr->ah_attr = qp->remote_ah_attr;
641 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
642 attr->pkey_index = qp->s_pkey_index;
643 attr->alt_pkey_index = 0;
644 attr->en_sqd_async_notify = 0;
Ralph Campbelle509be82008-05-13 11:41:29 -0700645 attr->sq_draining = qp->s_draining;
Ralph Campbell3859e392007-03-15 14:44:51 -0700646 attr->max_rd_atomic = qp->s_max_rd_atomic;
647 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700648 attr->min_rnr_timer = qp->r_min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800649 attr->port_num = 1;
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700650 attr->timeout = qp->timeout;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800651 attr->retry_cnt = qp->s_retry_cnt;
Patrick Marchand Latifi87d5aed2008-01-07 23:43:04 -0800652 attr->rnr_retry = qp->s_rnr_retry_cnt;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800653 attr->alt_port_num = 0;
654 attr->alt_timeout = 0;
655
656 init_attr->event_handler = qp->ibqp.event_handler;
657 init_attr->qp_context = qp->ibqp.qp_context;
658 init_attr->send_cq = qp->ibqp.send_cq;
659 init_attr->recv_cq = qp->ibqp.recv_cq;
660 init_attr->srq = qp->ibqp.srq;
661 init_attr->cap = attr->cap;
Ralph Campbell3859e392007-03-15 14:44:51 -0700662 if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700663 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
664 else
665 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800666 init_attr->qp_type = qp->ibqp.qp_type;
667 init_attr->port_num = 1;
668 return 0;
669}
670
671/**
672 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
673 * @qp: the queue pair to compute the AETH for
674 *
675 * Returns the AETH.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800676 */
677__be32 ipath_compute_aeth(struct ipath_qp *qp)
678{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700679 u32 aeth = qp->r_msn & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800680
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700681 if (qp->ibqp.srq) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800682 /*
683 * Shared receive queues don't generate credits.
684 * Set the credit field to the invalid value.
685 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700686 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800687 } else {
688 u32 min, max, x;
689 u32 credits;
Ralph Campbell373d9912006-09-22 15:22:26 -0700690 struct ipath_rwq *wq = qp->r_rq.wq;
691 u32 head;
692 u32 tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800693
Ralph Campbell373d9912006-09-22 15:22:26 -0700694 /* sanity check pointers before trusting them */
695 head = wq->head;
696 if (head >= qp->r_rq.size)
697 head = 0;
698 tail = wq->tail;
699 if (tail >= qp->r_rq.size)
700 tail = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800701 /*
702 * Compute the number of credits available (RWQEs).
703 * XXX Not holding the r_rq.lock here so there is a small
704 * chance that the pair of reads are not atomic.
705 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700706 credits = head - tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800707 if ((int)credits < 0)
708 credits += qp->r_rq.size;
709 /*
710 * Binary search the credit table to find the code to
711 * use.
712 */
713 min = 0;
714 max = 31;
715 for (;;) {
716 x = (min + max) / 2;
717 if (credit_table[x] == credits)
718 break;
719 if (credit_table[x] > credits)
720 max = x;
721 else if (min == x)
722 break;
723 else
724 min = x;
725 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700726 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800727 }
728 return cpu_to_be32(aeth);
729}
730
731/**
732 * ipath_create_qp - create a queue pair for a device
733 * @ibpd: the protection domain who's device we create the queue pair for
734 * @init_attr: the attributes of the queue pair
735 * @udata: unused by InfiniPath
736 *
737 * Returns the queue pair on success, otherwise returns an errno.
738 *
739 * Called by the ib_create_qp() core verbs function.
740 */
741struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
742 struct ib_qp_init_attr *init_attr,
743 struct ib_udata *udata)
744{
745 struct ipath_qp *qp;
746 int err;
747 struct ipath_swqe *swq = NULL;
748 struct ipath_ibdev *dev;
749 size_t sz;
Ralph Campbell7c37d742008-12-01 20:59:08 -0800750 size_t sg_list_sz;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800751 struct ib_qp *ret;
752
Eli Cohenb846f252008-04-16 21:09:27 -0700753 if (init_attr->create_flags) {
754 ret = ERR_PTR(-EINVAL);
755 goto bail;
756 }
757
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700758 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
Ralph Campbell10a8c3c2008-04-16 21:09:25 -0700759 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs) {
760 ret = ERR_PTR(-EINVAL);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800761 goto bail;
762 }
763
Ralph Campbell10a8c3c2008-04-16 21:09:25 -0700764 /* Check receive queue parameters if no SRQ is specified. */
765 if (!init_attr->srq) {
766 if (init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
767 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
768 ret = ERR_PTR(-EINVAL);
769 goto bail;
770 }
771 if (init_attr->cap.max_send_sge +
772 init_attr->cap.max_send_wr +
773 init_attr->cap.max_recv_sge +
774 init_attr->cap.max_recv_wr == 0) {
775 ret = ERR_PTR(-EINVAL);
776 goto bail;
777 }
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700778 }
779
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800780 switch (init_attr->qp_type) {
781 case IB_QPT_UC:
782 case IB_QPT_RC:
Ralph Campbell4ee97182007-07-25 11:08:28 -0700783 case IB_QPT_UD:
784 case IB_QPT_SMI:
785 case IB_QPT_GSI:
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800786 sz = sizeof(struct ipath_sge) *
787 init_attr->cap.max_send_sge +
788 sizeof(struct ipath_swqe);
789 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
790 if (swq == NULL) {
791 ret = ERR_PTR(-ENOMEM);
792 goto bail;
793 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700794 sz = sizeof(*qp);
Ralph Campbell7c37d742008-12-01 20:59:08 -0800795 sg_list_sz = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700796 if (init_attr->srq) {
797 struct ipath_srq *srq = to_isrq(init_attr->srq);
798
Ralph Campbell7c37d742008-12-01 20:59:08 -0800799 if (srq->rq.max_sge > 1)
800 sg_list_sz = sizeof(*qp->r_sg_list) *
801 (srq->rq.max_sge - 1);
802 } else if (init_attr->cap.max_recv_sge > 1)
803 sg_list_sz = sizeof(*qp->r_sg_list) *
804 (init_attr->cap.max_recv_sge - 1);
805 qp = kmalloc(sz + sg_list_sz, GFP_KERNEL);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800806 if (!qp) {
807 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700808 goto bail_swq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800809 }
Ralph Campbell7c37d742008-12-01 20:59:08 -0800810 if (sg_list_sz && (init_attr->qp_type == IB_QPT_UD ||
811 init_attr->qp_type == IB_QPT_SMI ||
812 init_attr->qp_type == IB_QPT_GSI)) {
813 qp->r_ud_sg_list = kmalloc(sg_list_sz, GFP_KERNEL);
814 if (!qp->r_ud_sg_list) {
815 ret = ERR_PTR(-ENOMEM);
816 goto bail_qp;
817 }
818 } else
819 qp->r_ud_sg_list = NULL;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700820 if (init_attr->srq) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700821 sz = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700822 qp->r_rq.size = 0;
823 qp->r_rq.max_sge = 0;
824 qp->r_rq.wq = NULL;
Ralph Campbell373d9912006-09-22 15:22:26 -0700825 init_attr->cap.max_recv_wr = 0;
826 init_attr->cap.max_recv_sge = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700827 } else {
828 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
829 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
Ralph Campbell373d9912006-09-22 15:22:26 -0700830 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700831 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700832 qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
833 qp->r_rq.size * sz);
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700834 if (!qp->r_rq.wq) {
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700835 ret = ERR_PTR(-ENOMEM);
Ralph Campbell7c37d742008-12-01 20:59:08 -0800836 goto bail_sg_list;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700837 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800838 }
839
840 /*
841 * ib_create_qp() will initialize qp->ibqp
842 * except for qp->ibqp.qp_num.
843 */
844 spin_lock_init(&qp->s_lock);
845 spin_lock_init(&qp->r_rq.lock);
846 atomic_set(&qp->refcount, 0);
847 init_waitqueue_head(&qp->wait);
Ralph Campbelle509be82008-05-13 11:41:29 -0700848 init_waitqueue_head(&qp->wait_dma);
Ralph Campbell4ee97182007-07-25 11:08:28 -0700849 tasklet_init(&qp->s_task, ipath_do_send, (unsigned long)qp);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700850 INIT_LIST_HEAD(&qp->piowait);
851 INIT_LIST_HEAD(&qp->timerwait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800852 qp->state = IB_QPS_RESET;
853 qp->s_wq = swq;
854 qp->s_size = init_attr->cap.max_send_wr + 1;
855 qp->s_max_sge = init_attr->cap.max_send_sge;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700856 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
Ralph Campbell3859e392007-03-15 14:44:51 -0700857 qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700858 else
859 qp->s_flags = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800860 dev = to_idev(ibpd->device);
861 err = ipath_alloc_qpn(&dev->qp_table, qp,
862 init_attr->qp_type);
863 if (err) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800864 ret = ERR_PTR(err);
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800865 vfree(qp->r_rq.wq);
Ralph Campbell7c37d742008-12-01 20:59:08 -0800866 goto bail_sg_list;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800867 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700868 qp->ip = NULL;
Dave Olson124b4dc2008-04-16 21:09:32 -0700869 qp->s_tx = NULL;
Patrick Marchand Latifi4cd50602008-01-18 20:10:48 -0800870 ipath_reset_qp(qp, init_attr->qp_type);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800871 break;
872
873 default:
874 /* Don't support raw QPs */
875 ret = ERR_PTR(-ENOSYS);
876 goto bail;
877 }
878
879 init_attr->cap.max_inline_data = 0;
880
Ralph Campbell373d9912006-09-22 15:22:26 -0700881 /*
882 * Return the address of the RWQ as the offset to mmap.
883 * See ipath_mmap() for details.
884 */
885 if (udata && udata->outlen >= sizeof(__u64)) {
Robert Walsh6b66b2da2007-04-27 21:07:23 -0700886 if (!qp->r_rq.wq) {
887 __u64 offset = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700888
Robert Walsh6b66b2da2007-04-27 21:07:23 -0700889 err = ib_copy_to_udata(udata, &offset,
890 sizeof(offset));
891 if (err) {
892 ret = ERR_PTR(err);
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800893 goto bail_ip;
Robert Walsh6b66b2da2007-04-27 21:07:23 -0700894 }
895 } else {
896 u32 s = sizeof(struct ipath_rwq) +
897 qp->r_rq.size * sz;
898
899 qp->ip =
900 ipath_create_mmap_info(dev, s,
901 ibpd->uobject->context,
902 qp->r_rq.wq);
903 if (!qp->ip) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700904 ret = ERR_PTR(-ENOMEM);
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800905 goto bail_ip;
Ralph Campbell373d9912006-09-22 15:22:26 -0700906 }
Robert Walsh6b66b2da2007-04-27 21:07:23 -0700907
908 err = ib_copy_to_udata(udata, &(qp->ip->offset),
909 sizeof(qp->ip->offset));
910 if (err) {
911 ret = ERR_PTR(err);
912 goto bail_ip;
913 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700914 }
915 }
916
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700917 spin_lock(&dev->n_qps_lock);
918 if (dev->n_qps_allocated == ib_ipath_max_qps) {
919 spin_unlock(&dev->n_qps_lock);
920 ret = ERR_PTR(-ENOMEM);
921 goto bail_ip;
922 }
923
924 dev->n_qps_allocated++;
925 spin_unlock(&dev->n_qps_lock);
926
Robert Walsh6b66b2da2007-04-27 21:07:23 -0700927 if (qp->ip) {
928 spin_lock_irq(&dev->pending_lock);
929 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
930 spin_unlock_irq(&dev->pending_lock);
931 }
932
Ralph Campbell373d9912006-09-22 15:22:26 -0700933 ret = &qp->ibqp;
934 goto bail;
935
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700936bail_ip:
Ralph Campbell8a278e6d52007-11-14 12:09:05 -0800937 if (qp->ip)
938 kref_put(&qp->ip->ref, ipath_release_mmap_info);
939 else
940 vfree(qp->r_rq.wq);
941 ipath_free_qp(&dev->qp_table, qp);
Ralph Campbelle509be82008-05-13 11:41:29 -0700942 free_qpn(&dev->qp_table, qp->ibqp.qp_num);
Ralph Campbell7c37d742008-12-01 20:59:08 -0800943bail_sg_list:
944 kfree(qp->r_ud_sg_list);
Ralph Campbell373d9912006-09-22 15:22:26 -0700945bail_qp:
946 kfree(qp);
947bail_swq:
948 vfree(swq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800949bail:
950 return ret;
951}
952
953/**
954 * ipath_destroy_qp - destroy a queue pair
955 * @ibqp: the queue pair to destroy
956 *
957 * Returns 0 on success.
958 *
959 * Note that this can be called while the QP is actively sending or
960 * receiving!
961 */
962int ipath_destroy_qp(struct ib_qp *ibqp)
963{
964 struct ipath_qp *qp = to_iqp(ibqp);
965 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800966
Ralph Campbelle509be82008-05-13 11:41:29 -0700967 /* Make sure HW and driver activity is stopped. */
968 spin_lock_irq(&qp->s_lock);
969 if (qp->state != IB_QPS_RESET) {
970 qp->state = IB_QPS_RESET;
971 spin_lock(&dev->pending_lock);
972 if (!list_empty(&qp->timerwait))
973 list_del_init(&qp->timerwait);
974 if (!list_empty(&qp->piowait))
975 list_del_init(&qp->piowait);
976 spin_unlock(&dev->pending_lock);
977 qp->s_flags &= ~IPATH_S_ANY_WAIT;
978 spin_unlock_irq(&qp->s_lock);
979 /* Stop the sending tasklet */
980 tasklet_kill(&qp->s_task);
981 wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
982 } else
983 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800984
Ralph Campbelle509be82008-05-13 11:41:29 -0700985 ipath_free_qp(&dev->qp_table, qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800986
Dave Olson124b4dc2008-04-16 21:09:32 -0700987 if (qp->s_tx) {
988 atomic_dec(&qp->refcount);
989 if (qp->s_tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEBUF)
990 kfree(qp->s_tx->txreq.map_addr);
Ralph Campbelle509be82008-05-13 11:41:29 -0700991 spin_lock_irq(&dev->pending_lock);
992 list_add(&qp->s_tx->txreq.list, &dev->txreq_free);
993 spin_unlock_irq(&dev->pending_lock);
994 qp->s_tx = NULL;
Dave Olson124b4dc2008-04-16 21:09:32 -0700995 }
996
Ralph Campbelle509be82008-05-13 11:41:29 -0700997 wait_event(qp->wait, !atomic_read(&qp->refcount));
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800998
Ralph Campbelle509be82008-05-13 11:41:29 -0700999 /* all user's cleaned up, mark it available */
1000 free_qpn(&dev->qp_table, qp->ibqp.qp_num);
1001 spin_lock(&dev->n_qps_lock);
1002 dev->n_qps_allocated--;
1003 spin_unlock(&dev->n_qps_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001004
Ralph Campbell373d9912006-09-22 15:22:26 -07001005 if (qp->ip)
1006 kref_put(&qp->ip->ref, ipath_release_mmap_info);
1007 else
1008 vfree(qp->r_rq.wq);
Ralph Campbell7c37d742008-12-01 20:59:08 -08001009 kfree(qp->r_ud_sg_list);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001010 vfree(qp->s_wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001011 kfree(qp);
1012 return 0;
1013}
1014
1015/**
1016 * ipath_init_qp_table - initialize the QP table for a device
1017 * @idev: the device who's QP table we're initializing
1018 * @size: the size of the QP table
1019 *
1020 * Returns 0 on success, otherwise returns an errno.
1021 */
1022int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
1023{
1024 int i;
1025 int ret;
1026
1027 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
1028 idev->qp_table.max = size;
1029 idev->qp_table.nmaps = 1;
1030 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
1031 GFP_KERNEL);
1032 if (idev->qp_table.table == NULL) {
1033 ret = -ENOMEM;
1034 goto bail;
1035 }
1036
1037 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
1038 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
1039 idev->qp_table.map[i].page = NULL;
1040 }
1041
1042 ret = 0;
1043
1044bail:
1045 return ret;
1046}
1047
1048/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001049 * ipath_get_credit - flush the send work queue of a QP
1050 * @qp: the qp who's send work queue to flush
1051 * @aeth: the Acknowledge Extended Transport Header
1052 *
1053 * The QP s_lock should be held.
1054 */
1055void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1056{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001057 u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001058
1059 /*
1060 * If the credit is invalid, we can send
1061 * as many packets as we like. Otherwise, we have to
1062 * honor the credit field.
1063 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001064 if (credit == IPATH_AETH_CREDIT_INVAL)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001065 qp->s_lsn = (u32) -1;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001066 else if (qp->s_lsn != (u32) -1) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001067 /* Compute new LSN (i.e., MSN + credit) */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001068 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001069 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1070 qp->s_lsn = credit;
1071 }
1072
1073 /* Restart sending if it was blocked due to lack of credits. */
Ralph Campbelle509be82008-05-13 11:41:29 -07001074 if ((qp->s_flags & IPATH_S_WAIT_SSN_CREDIT) &&
1075 qp->s_cur != qp->s_head &&
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001076 (qp->s_lsn == (u32) -1 ||
1077 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1078 qp->s_lsn + 1) <= 0))
Ralph Campbelle509be82008-05-13 11:41:29 -07001079 ipath_schedule_send(qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001080}