blob: f671fd07325302977e83d08040bb31d08877ec30 [file] [log] [blame]
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/err.h>
35#include <linux/vmalloc.h>
36
37#include "ipath_verbs.h"
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
84static u32 alloc_qpn(struct ipath_qp_table *qpt)
85{
86 u32 i, offset, max_scan, qpn;
87 struct qpn_map *map;
88 u32 ret;
89
90 qpn = qpt->last + 1;
91 if (qpn >= QPN_MAX)
92 qpn = 2;
93 offset = qpn & BITS_PER_PAGE_MASK;
94 map = &qpt->map[qpn / BITS_PER_PAGE];
95 max_scan = qpt->nmaps - !offset;
96 for (i = 0;;) {
97 if (unlikely(!map->page)) {
98 unsigned long page = get_zeroed_page(GFP_KERNEL);
99 unsigned long flags;
100
101 /*
102 * Free the page if someone raced with us
103 * installing it:
104 */
105 spin_lock_irqsave(&qpt->lock, flags);
106 if (map->page)
107 free_page(page);
108 else
109 map->page = (void *)page;
110 spin_unlock_irqrestore(&qpt->lock, flags);
111 if (unlikely(!map->page))
112 break;
113 }
114 if (likely(atomic_read(&map->n_free))) {
115 do {
116 if (!test_and_set_bit(offset, map->page)) {
117 atomic_dec(&map->n_free);
118 qpt->last = qpn;
119 ret = qpn;
120 goto bail;
121 }
122 offset = find_next_offset(map, offset);
123 qpn = mk_qpn(qpt, map, offset);
124 /*
125 * This test differs from alloc_pidmap().
126 * If find_next_offset() does find a zero
127 * bit, we don't need to check for QPN
128 * wrapping around past our starting QPN.
129 * We just need to be sure we don't loop
130 * forever.
131 */
132 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
133 }
134 /*
135 * In order to keep the number of pages allocated to a
136 * minimum, we scan the all existing pages before increasing
137 * the size of the bitmap table.
138 */
139 if (++i > max_scan) {
140 if (qpt->nmaps == QPNMAP_ENTRIES)
141 break;
142 map = &qpt->map[qpt->nmaps++];
143 offset = 0;
144 } else if (map < &qpt->map[qpt->nmaps]) {
145 ++map;
146 offset = 0;
147 } else {
148 map = &qpt->map[0];
149 offset = 2;
150 }
151 qpn = mk_qpn(qpt, map, offset);
152 }
153
154 ret = 0;
155
156bail:
157 return ret;
158}
159
160static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
161{
162 struct qpn_map *map;
163
164 map = qpt->map + qpn / BITS_PER_PAGE;
165 if (map->page)
166 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
167 atomic_inc(&map->n_free);
168}
169
170/**
171 * ipath_alloc_qpn - allocate a QP number
172 * @qpt: the QP table
173 * @qp: the QP
174 * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
175 *
176 * Allocate the next available QPN and put the QP into the hash table.
177 * The hash table holds a reference to the QP.
178 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700179static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
180 enum ib_qp_type type)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800181{
182 unsigned long flags;
183 u32 qpn;
184 int ret;
185
186 if (type == IB_QPT_SMI)
187 qpn = 0;
188 else if (type == IB_QPT_GSI)
189 qpn = 1;
190 else {
191 /* Allocate the next available QPN */
192 qpn = alloc_qpn(qpt);
193 if (qpn == 0) {
194 ret = -ENOMEM;
195 goto bail;
196 }
197 }
198 qp->ibqp.qp_num = qpn;
199
200 /* Add the QP to the hash table. */
201 spin_lock_irqsave(&qpt->lock, flags);
202
203 qpn %= qpt->max;
204 qp->next = qpt->table[qpn];
205 qpt->table[qpn] = qp;
206 atomic_inc(&qp->refcount);
207
208 spin_unlock_irqrestore(&qpt->lock, flags);
209 ret = 0;
210
211bail:
212 return ret;
213}
214
215/**
216 * ipath_free_qp - remove a QP from the QP table
217 * @qpt: the QP table
218 * @qp: the QP to remove
219 *
220 * Remove the QP from the table so it can't be found asynchronously by
221 * the receive interrupt routine.
222 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700223static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800224{
225 struct ipath_qp *q, **qpp;
226 unsigned long flags;
227 int fnd = 0;
228
229 spin_lock_irqsave(&qpt->lock, flags);
230
231 /* Remove QP from the hash table. */
232 qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
233 for (; (q = *qpp) != NULL; qpp = &q->next) {
234 if (q == qp) {
235 *qpp = qp->next;
236 qp->next = NULL;
237 atomic_dec(&qp->refcount);
238 fnd = 1;
239 break;
240 }
241 }
242
243 spin_unlock_irqrestore(&qpt->lock, flags);
244
245 if (!fnd)
246 return;
247
248 /* If QPN is not reserved, mark QPN free in the bitmap. */
249 if (qp->ibqp.qp_num > 1)
250 free_qpn(qpt, qp->ibqp.qp_num);
251
252 wait_event(qp->wait, !atomic_read(&qp->refcount));
253}
254
255/**
256 * ipath_free_all_qps - remove all QPs from the table
257 * @qpt: the QP table to empty
258 */
259void ipath_free_all_qps(struct ipath_qp_table *qpt)
260{
261 unsigned long flags;
262 struct ipath_qp *qp, *nqp;
263 u32 n;
264
265 for (n = 0; n < qpt->max; n++) {
266 spin_lock_irqsave(&qpt->lock, flags);
267 qp = qpt->table[n];
268 qpt->table[n] = NULL;
269 spin_unlock_irqrestore(&qpt->lock, flags);
270
271 while (qp) {
272 nqp = qp->next;
273 if (qp->ibqp.qp_num > 1)
274 free_qpn(qpt, qp->ibqp.qp_num);
275 if (!atomic_dec_and_test(&qp->refcount) ||
276 !ipath_destroy_qp(&qp->ibqp))
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -0700277 ipath_dbg("QP memory leak!\n");
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800278 qp = nqp;
279 }
280 }
281
282 for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
283 if (qpt->map[n].page)
284 free_page((unsigned long)qpt->map[n].page);
285 }
286}
287
288/**
289 * ipath_lookup_qpn - return the QP with the given QPN
290 * @qpt: the QP table
291 * @qpn: the QP number to look up
292 *
293 * The caller is responsible for decrementing the QP reference count
294 * when done.
295 */
296struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
297{
298 unsigned long flags;
299 struct ipath_qp *qp;
300
301 spin_lock_irqsave(&qpt->lock, flags);
302
303 for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
304 if (qp->ibqp.qp_num == qpn) {
305 atomic_inc(&qp->refcount);
306 break;
307 }
308 }
309
310 spin_unlock_irqrestore(&qpt->lock, flags);
311 return qp;
312}
313
314/**
315 * ipath_reset_qp - initialize the QP state to the reset state
316 * @qp: the QP to reset
317 */
318static void ipath_reset_qp(struct ipath_qp *qp)
319{
320 qp->remote_qpn = 0;
321 qp->qkey = 0;
322 qp->qp_access_flags = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700323 qp->s_busy = 0;
324 qp->s_flags &= ~IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800325 qp->s_hdrwords = 0;
326 qp->s_psn = 0;
327 qp->r_psn = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700328 qp->r_msn = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800329 if (qp->ibqp.qp_type == IB_QPT_RC) {
330 qp->s_state = IB_OPCODE_RC_SEND_LAST;
331 qp->r_state = IB_OPCODE_RC_SEND_LAST;
332 } else {
333 qp->s_state = IB_OPCODE_UC_SEND_LAST;
334 qp->r_state = IB_OPCODE_UC_SEND_LAST;
335 }
336 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700337 qp->r_nak_state = 0;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700338 qp->r_wrid_valid = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800339 qp->s_rnr_timeout = 0;
340 qp->s_head = 0;
341 qp->s_tail = 0;
342 qp->s_cur = 0;
343 qp->s_last = 0;
344 qp->s_ssn = 1;
345 qp->s_lsn = 0;
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700346 qp->s_wait_credit = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700347 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
348 qp->r_head_ack_queue = 0;
349 qp->s_tail_ack_queue = 0;
350 qp->s_num_rd_atomic = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700351 if (qp->r_rq.wq) {
352 qp->r_rq.wq->head = 0;
353 qp->r_rq.wq->tail = 0;
354 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800355 qp->r_reuse_sge = 0;
356}
357
358/**
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700359 * ipath_error_qp - put a QP into an error state
360 * @qp: the QP to put into an error state
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700361 * @err: the receive completion error to signal if a RWQE is active
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700362 *
363 * Flushes both send and receive work queues.
Ralph Campbell0434d272007-03-15 14:44:53 -0700364 * The QP s_lock should be held and interrupts disabled.
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700365 */
366
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700367void ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700368{
369 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
370 struct ib_wc wc;
371
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -0700372 ipath_dbg("QP%d/%d in error state\n",
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -0700373 qp->ibqp.qp_num, qp->remote_qpn);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700374
375 spin_lock(&dev->pending_lock);
376 /* XXX What if its already removed by the timeout code? */
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700377 if (!list_empty(&qp->timerwait))
378 list_del_init(&qp->timerwait);
379 if (!list_empty(&qp->piowait))
380 list_del_init(&qp->piowait);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700381 spin_unlock(&dev->pending_lock);
382
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700383 wc.vendor_err = 0;
384 wc.byte_len = 0;
385 wc.imm_data = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200386 wc.qp = &qp->ibqp;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700387 wc.src_qp = 0;
388 wc.wc_flags = 0;
389 wc.pkey_index = 0;
390 wc.slid = 0;
391 wc.sl = 0;
392 wc.dlid_path_bits = 0;
393 wc.port_num = 0;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700394 if (qp->r_wrid_valid) {
395 qp->r_wrid_valid = 0;
Ralph Campbell0434d272007-03-15 14:44:53 -0700396 wc.wr_id = qp->r_wr_id;
397 wc.opcode = IB_WC_RECV;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -0700398 wc.status = err;
399 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
400 }
401 wc.status = IB_WC_WR_FLUSH_ERR;
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700402
403 while (qp->s_last != qp->s_head) {
404 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
405
406 wc.wr_id = wqe->wr.wr_id;
407 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
408 if (++qp->s_last >= qp->s_size)
409 qp->s_last = 0;
410 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
411 }
412 qp->s_cur = qp->s_tail = qp->s_head;
413 qp->s_hdrwords = 0;
414 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
415
Ralph Campbell373d9912006-09-22 15:22:26 -0700416 if (qp->r_rq.wq) {
417 struct ipath_rwq *wq;
418 u32 head;
419 u32 tail;
420
421 spin_lock(&qp->r_rq.lock);
422
423 /* sanity check pointers before trusting them */
424 wq = qp->r_rq.wq;
425 head = wq->head;
426 if (head >= qp->r_rq.size)
427 head = 0;
428 tail = wq->tail;
429 if (tail >= qp->r_rq.size)
430 tail = 0;
431 wc.opcode = IB_WC_RECV;
432 while (tail != head) {
433 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
434 if (++tail >= qp->r_rq.size)
435 tail = 0;
436 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
437 }
438 wq->tail = tail;
439
440 spin_unlock(&qp->r_rq.lock);
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700441 }
442}
443
444/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800445 * ipath_modify_qp - modify the attributes of a queue pair
446 * @ibqp: the queue pair who's attributes we're modifying
447 * @attr: the new attributes
448 * @attr_mask: the mask of attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700449 * @udata: user data for ipathverbs.so
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800450 *
451 * Returns 0 on success, otherwise returns an errno.
452 */
453int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700454 int attr_mask, struct ib_udata *udata)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800455{
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700456 struct ipath_ibdev *dev = to_idev(ibqp->device);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800457 struct ipath_qp *qp = to_iqp(ibqp);
458 enum ib_qp_state cur_state, new_state;
459 unsigned long flags;
460 int ret;
461
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700462 spin_lock_irqsave(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800463
464 cur_state = attr_mask & IB_QP_CUR_STATE ?
465 attr->cur_qp_state : qp->state;
466 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
467
468 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
469 attr_mask))
470 goto inval;
471
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700472 if (attr_mask & IB_QP_AV) {
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700473 if (attr->ah_attr.dlid == 0 ||
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700474 attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700475 goto inval;
476
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700477 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
478 (attr->ah_attr.grh.sgid_index > 1))
479 goto inval;
480 }
481
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700482 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700483 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700484 goto inval;
485
486 if (attr_mask & IB_QP_MIN_RNR_TIMER)
487 if (attr->min_rnr_timer > 31)
488 goto inval;
489
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700490 if (attr_mask & IB_QP_PORT)
491 if (attr->port_num == 0 ||
492 attr->port_num > ibqp->device->phys_port_cnt)
493 goto inval;
494
495 if (attr_mask & IB_QP_PATH_MTU)
496 if (attr->path_mtu > IB_MTU_4096)
497 goto inval;
498
499 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
500 if (attr->max_dest_rd_atomic > 1)
501 goto inval;
502
503 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
504 if (attr->max_rd_atomic > 1)
505 goto inval;
506
507 if (attr_mask & IB_QP_PATH_MIG_STATE)
Bryan O'Sullivanca4ce382006-08-25 11:24:42 -0700508 if (attr->path_mig_state != IB_MIG_MIGRATED &&
509 attr->path_mig_state != IB_MIG_REARM)
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700510 goto inval;
511
Ralph Campbell3859e392007-03-15 14:44:51 -0700512 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
513 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
514 goto inval;
515
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800516 switch (new_state) {
517 case IB_QPS_RESET:
518 ipath_reset_qp(qp);
519 break;
520
521 case IB_QPS_ERR:
Bryan O'Sullivan041eab92007-03-15 14:44:57 -0700522 ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800523 break;
524
525 default:
526 break;
527
528 }
529
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700530 if (attr_mask & IB_QP_PKEY_INDEX)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800531 qp->s_pkey_index = attr->pkey_index;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800532
533 if (attr_mask & IB_QP_DEST_QPN)
534 qp->remote_qpn = attr->dest_qp_num;
535
536 if (attr_mask & IB_QP_SQ_PSN) {
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700537 qp->s_psn = qp->s_next_psn = attr->sq_psn;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800538 qp->s_last_psn = qp->s_next_psn - 1;
539 }
540
541 if (attr_mask & IB_QP_RQ_PSN)
542 qp->r_psn = attr->rq_psn;
543
544 if (attr_mask & IB_QP_ACCESS_FLAGS)
545 qp->qp_access_flags = attr->qp_access_flags;
546
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700547 if (attr_mask & IB_QP_AV)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800548 qp->remote_ah_attr = attr->ah_attr;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800549
550 if (attr_mask & IB_QP_PATH_MTU)
551 qp->path_mtu = attr->path_mtu;
552
553 if (attr_mask & IB_QP_RETRY_CNT)
554 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
555
556 if (attr_mask & IB_QP_RNR_RETRY) {
557 qp->s_rnr_retry = attr->rnr_retry;
558 if (qp->s_rnr_retry > 7)
559 qp->s_rnr_retry = 7;
560 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
561 }
562
Bryan O'Sullivanb228b432006-05-23 11:32:30 -0700563 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700564 qp->r_min_rnr_timer = attr->min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800565
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700566 if (attr_mask & IB_QP_TIMEOUT)
567 qp->timeout = attr->timeout;
568
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800569 if (attr_mask & IB_QP_QKEY)
570 qp->qkey = attr->qkey;
571
Ralph Campbell3859e392007-03-15 14:44:51 -0700572 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
573 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
574
575 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
576 qp->s_max_rd_atomic = attr->max_rd_atomic;
577
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800578 qp->state = new_state;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700579 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800580
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800581 ret = 0;
582 goto bail;
583
584inval:
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700585 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800586 ret = -EINVAL;
587
588bail:
589 return ret;
590}
591
592int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
593 int attr_mask, struct ib_qp_init_attr *init_attr)
594{
595 struct ipath_qp *qp = to_iqp(ibqp);
596
597 attr->qp_state = qp->state;
598 attr->cur_qp_state = attr->qp_state;
599 attr->path_mtu = qp->path_mtu;
600 attr->path_mig_state = 0;
601 attr->qkey = qp->qkey;
602 attr->rq_psn = qp->r_psn;
603 attr->sq_psn = qp->s_next_psn;
604 attr->dest_qp_num = qp->remote_qpn;
605 attr->qp_access_flags = qp->qp_access_flags;
606 attr->cap.max_send_wr = qp->s_size - 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700607 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800608 attr->cap.max_send_sge = qp->s_max_sge;
609 attr->cap.max_recv_sge = qp->r_rq.max_sge;
610 attr->cap.max_inline_data = 0;
611 attr->ah_attr = qp->remote_ah_attr;
612 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
613 attr->pkey_index = qp->s_pkey_index;
614 attr->alt_pkey_index = 0;
615 attr->en_sqd_async_notify = 0;
616 attr->sq_draining = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700617 attr->max_rd_atomic = qp->s_max_rd_atomic;
618 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700619 attr->min_rnr_timer = qp->r_min_rnr_timer;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800620 attr->port_num = 1;
Bryan O'Sullivanfc8cf8c2006-08-25 11:24:41 -0700621 attr->timeout = qp->timeout;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800622 attr->retry_cnt = qp->s_retry_cnt;
623 attr->rnr_retry = qp->s_rnr_retry;
624 attr->alt_port_num = 0;
625 attr->alt_timeout = 0;
626
627 init_attr->event_handler = qp->ibqp.event_handler;
628 init_attr->qp_context = qp->ibqp.qp_context;
629 init_attr->send_cq = qp->ibqp.send_cq;
630 init_attr->recv_cq = qp->ibqp.recv_cq;
631 init_attr->srq = qp->ibqp.srq;
632 init_attr->cap = attr->cap;
Ralph Campbell3859e392007-03-15 14:44:51 -0700633 if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700634 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
635 else
636 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800637 init_attr->qp_type = qp->ibqp.qp_type;
638 init_attr->port_num = 1;
639 return 0;
640}
641
642/**
643 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
644 * @qp: the queue pair to compute the AETH for
645 *
646 * Returns the AETH.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800647 */
648__be32 ipath_compute_aeth(struct ipath_qp *qp)
649{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700650 u32 aeth = qp->r_msn & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800651
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700652 if (qp->ibqp.srq) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800653 /*
654 * Shared receive queues don't generate credits.
655 * Set the credit field to the invalid value.
656 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700657 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800658 } else {
659 u32 min, max, x;
660 u32 credits;
Ralph Campbell373d9912006-09-22 15:22:26 -0700661 struct ipath_rwq *wq = qp->r_rq.wq;
662 u32 head;
663 u32 tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800664
Ralph Campbell373d9912006-09-22 15:22:26 -0700665 /* sanity check pointers before trusting them */
666 head = wq->head;
667 if (head >= qp->r_rq.size)
668 head = 0;
669 tail = wq->tail;
670 if (tail >= qp->r_rq.size)
671 tail = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800672 /*
673 * Compute the number of credits available (RWQEs).
674 * XXX Not holding the r_rq.lock here so there is a small
675 * chance that the pair of reads are not atomic.
676 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700677 credits = head - tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800678 if ((int)credits < 0)
679 credits += qp->r_rq.size;
680 /*
681 * Binary search the credit table to find the code to
682 * use.
683 */
684 min = 0;
685 max = 31;
686 for (;;) {
687 x = (min + max) / 2;
688 if (credit_table[x] == credits)
689 break;
690 if (credit_table[x] > credits)
691 max = x;
692 else if (min == x)
693 break;
694 else
695 min = x;
696 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700697 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800698 }
699 return cpu_to_be32(aeth);
700}
701
702/**
703 * ipath_create_qp - create a queue pair for a device
704 * @ibpd: the protection domain who's device we create the queue pair for
705 * @init_attr: the attributes of the queue pair
706 * @udata: unused by InfiniPath
707 *
708 * Returns the queue pair on success, otherwise returns an errno.
709 *
710 * Called by the ib_create_qp() core verbs function.
711 */
712struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
713 struct ib_qp_init_attr *init_attr,
714 struct ib_udata *udata)
715{
716 struct ipath_qp *qp;
717 int err;
718 struct ipath_swqe *swq = NULL;
719 struct ipath_ibdev *dev;
720 size_t sz;
721 struct ib_qp *ret;
722
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700723 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
724 init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
725 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
726 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800727 ret = ERR_PTR(-ENOMEM);
728 goto bail;
729 }
730
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700731 if (init_attr->cap.max_send_sge +
732 init_attr->cap.max_recv_sge +
733 init_attr->cap.max_send_wr +
734 init_attr->cap.max_recv_wr == 0) {
735 ret = ERR_PTR(-EINVAL);
736 goto bail;
737 }
738
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800739 switch (init_attr->qp_type) {
740 case IB_QPT_UC:
741 case IB_QPT_RC:
742 sz = sizeof(struct ipath_sge) *
743 init_attr->cap.max_send_sge +
744 sizeof(struct ipath_swqe);
745 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
746 if (swq == NULL) {
747 ret = ERR_PTR(-ENOMEM);
748 goto bail;
749 }
750 /* FALLTHROUGH */
751 case IB_QPT_UD:
752 case IB_QPT_SMI:
753 case IB_QPT_GSI:
Ralph Campbell373d9912006-09-22 15:22:26 -0700754 sz = sizeof(*qp);
755 if (init_attr->srq) {
756 struct ipath_srq *srq = to_isrq(init_attr->srq);
757
758 sz += sizeof(*qp->r_sg_list) *
759 srq->rq.max_sge;
760 } else
761 sz += sizeof(*qp->r_sg_list) *
762 init_attr->cap.max_recv_sge;
763 qp = kmalloc(sz, GFP_KERNEL);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800764 if (!qp) {
765 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700766 goto bail_swq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800767 }
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700768 if (init_attr->srq) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700769 sz = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700770 qp->r_rq.size = 0;
771 qp->r_rq.max_sge = 0;
772 qp->r_rq.wq = NULL;
Ralph Campbell373d9912006-09-22 15:22:26 -0700773 init_attr->cap.max_recv_wr = 0;
774 init_attr->cap.max_recv_sge = 0;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700775 } else {
776 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
777 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
Ralph Campbell373d9912006-09-22 15:22:26 -0700778 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700779 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700780 qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
781 qp->r_rq.size * sz);
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700782 if (!qp->r_rq.wq) {
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700783 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700784 goto bail_qp;
Bryan O'Sullivan357b5522006-07-01 04:36:16 -0700785 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800786 }
787
788 /*
789 * ib_create_qp() will initialize qp->ibqp
790 * except for qp->ibqp.qp_num.
791 */
792 spin_lock_init(&qp->s_lock);
793 spin_lock_init(&qp->r_rq.lock);
794 atomic_set(&qp->refcount, 0);
795 init_waitqueue_head(&qp->wait);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700796 tasklet_init(&qp->s_task, ipath_do_ruc_send,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800797 (unsigned long)qp);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700798 INIT_LIST_HEAD(&qp->piowait);
799 INIT_LIST_HEAD(&qp->timerwait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800800 qp->state = IB_QPS_RESET;
801 qp->s_wq = swq;
802 qp->s_size = init_attr->cap.max_send_wr + 1;
803 qp->s_max_sge = init_attr->cap.max_send_sge;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700804 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
Ralph Campbell3859e392007-03-15 14:44:51 -0700805 qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
Bryan O'Sullivana78aa6f2006-08-25 11:24:44 -0700806 else
807 qp->s_flags = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800808 dev = to_idev(ibpd->device);
809 err = ipath_alloc_qpn(&dev->qp_table, qp,
810 init_attr->qp_type);
811 if (err) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800812 ret = ERR_PTR(err);
Ralph Campbell373d9912006-09-22 15:22:26 -0700813 goto bail_rwq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800814 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700815 qp->ip = NULL;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800816 ipath_reset_qp(qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800817 break;
818
819 default:
820 /* Don't support raw QPs */
821 ret = ERR_PTR(-ENOSYS);
822 goto bail;
823 }
824
825 init_attr->cap.max_inline_data = 0;
826
Ralph Campbell373d9912006-09-22 15:22:26 -0700827 /*
828 * Return the address of the RWQ as the offset to mmap.
829 * See ipath_mmap() for details.
830 */
831 if (udata && udata->outlen >= sizeof(__u64)) {
832 struct ipath_mmap_info *ip;
833 __u64 offset = (__u64) qp->r_rq.wq;
834 int err;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800835
Ralph Campbell373d9912006-09-22 15:22:26 -0700836 err = ib_copy_to_udata(udata, &offset, sizeof(offset));
837 if (err) {
838 ret = ERR_PTR(err);
839 goto bail_rwq;
840 }
841
842 if (qp->r_rq.wq) {
843 /* Allocate info for ipath_mmap(). */
844 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
845 if (!ip) {
846 ret = ERR_PTR(-ENOMEM);
847 goto bail_rwq;
848 }
849 qp->ip = ip;
850 ip->context = ibpd->uobject->context;
851 ip->obj = qp->r_rq.wq;
852 kref_init(&ip->ref);
853 ip->mmap_cnt = 0;
854 ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
855 qp->r_rq.size * sz);
856 spin_lock_irq(&dev->pending_lock);
857 ip->next = dev->pending_mmaps;
858 dev->pending_mmaps = ip;
859 spin_unlock_irq(&dev->pending_lock);
860 }
861 }
862
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700863 spin_lock(&dev->n_qps_lock);
864 if (dev->n_qps_allocated == ib_ipath_max_qps) {
865 spin_unlock(&dev->n_qps_lock);
866 ret = ERR_PTR(-ENOMEM);
867 goto bail_ip;
868 }
869
870 dev->n_qps_allocated++;
871 spin_unlock(&dev->n_qps_lock);
872
Ralph Campbell373d9912006-09-22 15:22:26 -0700873 ret = &qp->ibqp;
874 goto bail;
875
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700876bail_ip:
877 kfree(qp->ip);
Ralph Campbell373d9912006-09-22 15:22:26 -0700878bail_rwq:
879 vfree(qp->r_rq.wq);
880bail_qp:
881 kfree(qp);
882bail_swq:
883 vfree(swq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800884bail:
885 return ret;
886}
887
888/**
889 * ipath_destroy_qp - destroy a queue pair
890 * @ibqp: the queue pair to destroy
891 *
892 * Returns 0 on success.
893 *
894 * Note that this can be called while the QP is actively sending or
895 * receiving!
896 */
897int ipath_destroy_qp(struct ib_qp *ibqp)
898{
899 struct ipath_qp *qp = to_iqp(ibqp);
900 struct ipath_ibdev *dev = to_idev(ibqp->device);
901 unsigned long flags;
902
Ralph Campbell373d9912006-09-22 15:22:26 -0700903 spin_lock_irqsave(&qp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800904 qp->state = IB_QPS_ERR;
Ralph Campbell373d9912006-09-22 15:22:26 -0700905 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700906 spin_lock(&dev->n_qps_lock);
907 dev->n_qps_allocated--;
908 spin_unlock(&dev->n_qps_lock);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800909
910 /* Stop the sending tasklet. */
911 tasklet_kill(&qp->s_task);
912
913 /* Make sure the QP isn't on the timeout list. */
914 spin_lock_irqsave(&dev->pending_lock, flags);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700915 if (!list_empty(&qp->timerwait))
916 list_del_init(&qp->timerwait);
917 if (!list_empty(&qp->piowait))
918 list_del_init(&qp->piowait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800919 spin_unlock_irqrestore(&dev->pending_lock, flags);
920
921 /*
922 * Make sure that the QP is not in the QPN table so receive
923 * interrupts will discard packets for this QP. XXX Also remove QP
924 * from multicast table.
925 */
926 if (atomic_read(&qp->refcount) != 0)
927 ipath_free_qp(&dev->qp_table, qp);
928
Ralph Campbell373d9912006-09-22 15:22:26 -0700929 if (qp->ip)
930 kref_put(&qp->ip->ref, ipath_release_mmap_info);
931 else
932 vfree(qp->r_rq.wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800933 vfree(qp->s_wq);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800934 kfree(qp);
935 return 0;
936}
937
938/**
939 * ipath_init_qp_table - initialize the QP table for a device
940 * @idev: the device who's QP table we're initializing
941 * @size: the size of the QP table
942 *
943 * Returns 0 on success, otherwise returns an errno.
944 */
945int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
946{
947 int i;
948 int ret;
949
950 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
951 idev->qp_table.max = size;
952 idev->qp_table.nmaps = 1;
953 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
954 GFP_KERNEL);
955 if (idev->qp_table.table == NULL) {
956 ret = -ENOMEM;
957 goto bail;
958 }
959
960 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
961 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
962 idev->qp_table.map[i].page = NULL;
963 }
964
965 ret = 0;
966
967bail:
968 return ret;
969}
970
971/**
972 * ipath_sqerror_qp - put a QP's send queue into an error state
973 * @qp: QP who's send queue will be put into an error state
974 * @wc: the WC responsible for putting the QP in this state
975 *
976 * Flushes the send work queue.
Ralph Campbell0434d272007-03-15 14:44:53 -0700977 * The QP s_lock should be held and interrupts disabled.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800978 */
979
980void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
981{
982 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
983 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
984
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -0700985 ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -0700986 qp->ibqp.qp_num, qp->remote_qpn, wc->status);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800987
988 spin_lock(&dev->pending_lock);
989 /* XXX What if its already removed by the timeout code? */
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700990 if (!list_empty(&qp->timerwait))
991 list_del_init(&qp->timerwait);
992 if (!list_empty(&qp->piowait))
993 list_del_init(&qp->piowait);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800994 spin_unlock(&dev->pending_lock);
995
996 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
997 if (++qp->s_last >= qp->s_size)
998 qp->s_last = 0;
999
1000 wc->status = IB_WC_WR_FLUSH_ERR;
1001
1002 while (qp->s_last != qp->s_head) {
Ralph Campbell0434d272007-03-15 14:44:53 -07001003 wqe = get_swqe_ptr(qp, qp->s_last);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001004 wc->wr_id = wqe->wr.wr_id;
1005 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1006 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1007 if (++qp->s_last >= qp->s_size)
1008 qp->s_last = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001009 }
1010 qp->s_cur = qp->s_tail = qp->s_head;
1011 qp->state = IB_QPS_SQE;
1012}
1013
1014/**
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001015 * ipath_get_credit - flush the send work queue of a QP
1016 * @qp: the qp who's send work queue to flush
1017 * @aeth: the Acknowledge Extended Transport Header
1018 *
1019 * The QP s_lock should be held.
1020 */
1021void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1022{
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001023 u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001024
1025 /*
1026 * If the credit is invalid, we can send
1027 * as many packets as we like. Otherwise, we have to
1028 * honor the credit field.
1029 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001030 if (credit == IPATH_AETH_CREDIT_INVAL)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001031 qp->s_lsn = (u32) -1;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001032 else if (qp->s_lsn != (u32) -1) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001033 /* Compute new LSN (i.e., MSN + credit) */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001034 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001035 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1036 qp->s_lsn = credit;
1037 }
1038
1039 /* Restart sending if it was blocked due to lack of credits. */
1040 if (qp->s_cur != qp->s_head &&
1041 (qp->s_lsn == (u32) -1 ||
1042 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1043 qp->s_lsn + 1) <= 0))
1044 tasklet_hi_schedule(&qp->s_task);
1045}