allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / infiniband / hw / ipath / ipath_qp.c
blobbfef08ecd342561b0d1272766564f43a700b1b2a
1 /*
2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
34 #include <linux/err.h>
35 #include <linux/vmalloc.h>
37 #include "ipath_verbs.h"
38 #include "ipath_kernel.h"
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)
48 * Convert the AETH credit code into the number of credits.
50 static 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 */
85 static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map)
87 unsigned long page = get_zeroed_page(GFP_KERNEL);
88 unsigned long flags;
91 * Free the page if someone raced with us installing it.
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);
103 static int alloc_qpn(struct ipath_qp_table *qpt, enum ib_qp_type type)
105 u32 i, offset, max_scan, qpn;
106 struct qpn_map *map;
107 u32 ret = -1;
109 if (type == IB_QPT_SMI)
110 ret = 0;
111 else if (type == IB_QPT_GSI)
112 ret = 1;
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;
123 if (!test_and_set_bit(ret, map->page))
124 atomic_dec(&map->n_free);
125 else
126 ret = -EBUSY;
127 goto bail;
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)) {
138 get_map_page(qpt, map);
139 if (unlikely(!map->page))
140 break;
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;
150 offset = find_next_offset(map, offset);
151 qpn = mk_qpn(qpt, map, offset);
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.
160 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
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.
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;
179 qpn = mk_qpn(qpt, map, offset);
182 ret = -ENOMEM;
184 bail:
185 return ret;
188 static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
190 struct qpn_map *map;
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);
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)
204 * Allocate the next available QPN and put the QP into the hash table.
205 * The hash table holds a reference to the QP.
207 static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
208 enum ib_qp_type type)
210 unsigned long flags;
211 int ret;
213 ret = alloc_qpn(qpt, type);
214 if (ret < 0)
215 goto bail;
216 qp->ibqp.qp_num = ret;
218 /* Add the QP to the hash table. */
219 spin_lock_irqsave(&qpt->lock, flags);
221 ret %= qpt->max;
222 qp->next = qpt->table[ret];
223 qpt->table[ret] = qp;
224 atomic_inc(&qp->refcount);
226 spin_unlock_irqrestore(&qpt->lock, flags);
227 ret = 0;
229 bail:
230 return ret;
234 * ipath_free_qp - remove a QP from the QP table
235 * @qpt: the QP table
236 * @qp: the QP to remove
238 * Remove the QP from the table so it can't be found asynchronously by
239 * the receive interrupt routine.
241 static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
243 struct ipath_qp *q, **qpp;
244 unsigned long flags;
245 int fnd = 0;
247 spin_lock_irqsave(&qpt->lock, flags);
249 /* Remove QP from the hash table. */
250 qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
251 for (; (q = *qpp) != NULL; qpp = &q->next) {
252 if (q == qp) {
253 *qpp = qp->next;
254 qp->next = NULL;
255 atomic_dec(&qp->refcount);
256 fnd = 1;
257 break;
261 spin_unlock_irqrestore(&qpt->lock, flags);
263 if (!fnd)
264 return;
266 free_qpn(qpt, qp->ibqp.qp_num);
268 wait_event(qp->wait, !atomic_read(&qp->refcount));
272 * ipath_free_all_qps - remove all QPs from the table
273 * @qpt: the QP table to empty
275 void ipath_free_all_qps(struct ipath_qp_table *qpt)
277 unsigned long flags;
278 struct ipath_qp *qp, *nqp;
279 u32 n;
281 for (n = 0; n < qpt->max; n++) {
282 spin_lock_irqsave(&qpt->lock, flags);
283 qp = qpt->table[n];
284 qpt->table[n] = NULL;
285 spin_unlock_irqrestore(&qpt->lock, flags);
287 while (qp) {
288 nqp = qp->next;
289 free_qpn(qpt, qp->ibqp.qp_num);
290 if (!atomic_dec_and_test(&qp->refcount) ||
291 !ipath_destroy_qp(&qp->ibqp))
292 ipath_dbg("QP memory leak!\n");
293 qp = nqp;
297 for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
298 if (qpt->map[n].page)
299 free_page((unsigned long)qpt->map[n].page);
304 * ipath_lookup_qpn - return the QP with the given QPN
305 * @qpt: the QP table
306 * @qpn: the QP number to look up
308 * The caller is responsible for decrementing the QP reference count
309 * when done.
311 struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
313 unsigned long flags;
314 struct ipath_qp *qp;
316 spin_lock_irqsave(&qpt->lock, flags);
318 for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
319 if (qp->ibqp.qp_num == qpn) {
320 atomic_inc(&qp->refcount);
321 break;
325 spin_unlock_irqrestore(&qpt->lock, flags);
326 return qp;
330 * ipath_reset_qp - initialize the QP state to the reset state
331 * @qp: the QP to reset
333 static void ipath_reset_qp(struct ipath_qp *qp)
335 qp->remote_qpn = 0;
336 qp->qkey = 0;
337 qp->qp_access_flags = 0;
338 qp->s_busy = 0;
339 qp->s_flags &= ~IPATH_S_SIGNAL_REQ_WR;
340 qp->s_hdrwords = 0;
341 qp->s_psn = 0;
342 qp->r_psn = 0;
343 qp->r_msn = 0;
344 if (qp->ibqp.qp_type == IB_QPT_RC) {
345 qp->s_state = IB_OPCODE_RC_SEND_LAST;
346 qp->r_state = IB_OPCODE_RC_SEND_LAST;
347 } else {
348 qp->s_state = IB_OPCODE_UC_SEND_LAST;
349 qp->r_state = IB_OPCODE_UC_SEND_LAST;
351 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
352 qp->r_nak_state = 0;
353 qp->r_wrid_valid = 0;
354 qp->s_rnr_timeout = 0;
355 qp->s_head = 0;
356 qp->s_tail = 0;
357 qp->s_cur = 0;
358 qp->s_last = 0;
359 qp->s_ssn = 1;
360 qp->s_lsn = 0;
361 qp->s_wait_credit = 0;
362 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
363 qp->r_head_ack_queue = 0;
364 qp->s_tail_ack_queue = 0;
365 qp->s_num_rd_atomic = 0;
366 if (qp->r_rq.wq) {
367 qp->r_rq.wq->head = 0;
368 qp->r_rq.wq->tail = 0;
370 qp->r_reuse_sge = 0;
374 * ipath_error_qp - put a QP into an error state
375 * @qp: the QP to put into an error state
376 * @err: the receive completion error to signal if a RWQE is active
378 * Flushes both send and receive work queues.
379 * The QP s_lock should be held and interrupts disabled.
382 void ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
384 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
385 struct ib_wc wc;
387 ipath_dbg("QP%d/%d in error state\n",
388 qp->ibqp.qp_num, qp->remote_qpn);
390 spin_lock(&dev->pending_lock);
391 /* XXX What if its already removed by the timeout code? */
392 if (!list_empty(&qp->timerwait))
393 list_del_init(&qp->timerwait);
394 if (!list_empty(&qp->piowait))
395 list_del_init(&qp->piowait);
396 spin_unlock(&dev->pending_lock);
398 wc.vendor_err = 0;
399 wc.byte_len = 0;
400 wc.imm_data = 0;
401 wc.qp = &qp->ibqp;
402 wc.src_qp = 0;
403 wc.wc_flags = 0;
404 wc.pkey_index = 0;
405 wc.slid = 0;
406 wc.sl = 0;
407 wc.dlid_path_bits = 0;
408 wc.port_num = 0;
409 if (qp->r_wrid_valid) {
410 qp->r_wrid_valid = 0;
411 wc.wr_id = qp->r_wr_id;
412 wc.opcode = IB_WC_RECV;
413 wc.status = err;
414 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
416 wc.status = IB_WC_WR_FLUSH_ERR;
418 while (qp->s_last != qp->s_head) {
419 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
421 wc.wr_id = wqe->wr.wr_id;
422 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
423 if (++qp->s_last >= qp->s_size)
424 qp->s_last = 0;
425 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
427 qp->s_cur = qp->s_tail = qp->s_head;
428 qp->s_hdrwords = 0;
429 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
431 if (qp->r_rq.wq) {
432 struct ipath_rwq *wq;
433 u32 head;
434 u32 tail;
436 spin_lock(&qp->r_rq.lock);
438 /* sanity check pointers before trusting them */
439 wq = qp->r_rq.wq;
440 head = wq->head;
441 if (head >= qp->r_rq.size)
442 head = 0;
443 tail = wq->tail;
444 if (tail >= qp->r_rq.size)
445 tail = 0;
446 wc.opcode = IB_WC_RECV;
447 while (tail != head) {
448 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
449 if (++tail >= qp->r_rq.size)
450 tail = 0;
451 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
453 wq->tail = tail;
455 spin_unlock(&qp->r_rq.lock);
460 * ipath_modify_qp - modify the attributes of a queue pair
461 * @ibqp: the queue pair who's attributes we're modifying
462 * @attr: the new attributes
463 * @attr_mask: the mask of attributes to modify
464 * @udata: user data for ipathverbs.so
466 * Returns 0 on success, otherwise returns an errno.
468 int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
469 int attr_mask, struct ib_udata *udata)
471 struct ipath_ibdev *dev = to_idev(ibqp->device);
472 struct ipath_qp *qp = to_iqp(ibqp);
473 enum ib_qp_state cur_state, new_state;
474 unsigned long flags;
475 int ret;
477 spin_lock_irqsave(&qp->s_lock, flags);
479 cur_state = attr_mask & IB_QP_CUR_STATE ?
480 attr->cur_qp_state : qp->state;
481 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
483 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
484 attr_mask))
485 goto inval;
487 if (attr_mask & IB_QP_AV) {
488 if (attr->ah_attr.dlid == 0 ||
489 attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
490 goto inval;
492 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
493 (attr->ah_attr.grh.sgid_index > 1))
494 goto inval;
497 if (attr_mask & IB_QP_PKEY_INDEX)
498 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
499 goto inval;
501 if (attr_mask & IB_QP_MIN_RNR_TIMER)
502 if (attr->min_rnr_timer > 31)
503 goto inval;
505 if (attr_mask & IB_QP_PORT)
506 if (attr->port_num == 0 ||
507 attr->port_num > ibqp->device->phys_port_cnt)
508 goto inval;
510 if (attr_mask & IB_QP_PATH_MTU)
511 if (attr->path_mtu > IB_MTU_4096)
512 goto inval;
514 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
515 if (attr->max_dest_rd_atomic > 1)
516 goto inval;
518 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
519 if (attr->max_rd_atomic > 1)
520 goto inval;
522 if (attr_mask & IB_QP_PATH_MIG_STATE)
523 if (attr->path_mig_state != IB_MIG_MIGRATED &&
524 attr->path_mig_state != IB_MIG_REARM)
525 goto inval;
527 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
528 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
529 goto inval;
531 switch (new_state) {
532 case IB_QPS_RESET:
533 ipath_reset_qp(qp);
534 break;
536 case IB_QPS_ERR:
537 ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
538 break;
540 default:
541 break;
545 if (attr_mask & IB_QP_PKEY_INDEX)
546 qp->s_pkey_index = attr->pkey_index;
548 if (attr_mask & IB_QP_DEST_QPN)
549 qp->remote_qpn = attr->dest_qp_num;
551 if (attr_mask & IB_QP_SQ_PSN) {
552 qp->s_psn = qp->s_next_psn = attr->sq_psn;
553 qp->s_last_psn = qp->s_next_psn - 1;
556 if (attr_mask & IB_QP_RQ_PSN)
557 qp->r_psn = attr->rq_psn;
559 if (attr_mask & IB_QP_ACCESS_FLAGS)
560 qp->qp_access_flags = attr->qp_access_flags;
562 if (attr_mask & IB_QP_AV)
563 qp->remote_ah_attr = attr->ah_attr;
565 if (attr_mask & IB_QP_PATH_MTU)
566 qp->path_mtu = attr->path_mtu;
568 if (attr_mask & IB_QP_RETRY_CNT)
569 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
571 if (attr_mask & IB_QP_RNR_RETRY) {
572 qp->s_rnr_retry = attr->rnr_retry;
573 if (qp->s_rnr_retry > 7)
574 qp->s_rnr_retry = 7;
575 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
578 if (attr_mask & IB_QP_MIN_RNR_TIMER)
579 qp->r_min_rnr_timer = attr->min_rnr_timer;
581 if (attr_mask & IB_QP_TIMEOUT)
582 qp->timeout = attr->timeout;
584 if (attr_mask & IB_QP_QKEY)
585 qp->qkey = attr->qkey;
587 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
588 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
590 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
591 qp->s_max_rd_atomic = attr->max_rd_atomic;
593 qp->state = new_state;
594 spin_unlock_irqrestore(&qp->s_lock, flags);
596 ret = 0;
597 goto bail;
599 inval:
600 spin_unlock_irqrestore(&qp->s_lock, flags);
601 ret = -EINVAL;
603 bail:
604 return ret;
607 int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
608 int attr_mask, struct ib_qp_init_attr *init_attr)
610 struct ipath_qp *qp = to_iqp(ibqp);
612 attr->qp_state = qp->state;
613 attr->cur_qp_state = attr->qp_state;
614 attr->path_mtu = qp->path_mtu;
615 attr->path_mig_state = 0;
616 attr->qkey = qp->qkey;
617 attr->rq_psn = qp->r_psn;
618 attr->sq_psn = qp->s_next_psn;
619 attr->dest_qp_num = qp->remote_qpn;
620 attr->qp_access_flags = qp->qp_access_flags;
621 attr->cap.max_send_wr = qp->s_size - 1;
622 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
623 attr->cap.max_send_sge = qp->s_max_sge;
624 attr->cap.max_recv_sge = qp->r_rq.max_sge;
625 attr->cap.max_inline_data = 0;
626 attr->ah_attr = qp->remote_ah_attr;
627 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
628 attr->pkey_index = qp->s_pkey_index;
629 attr->alt_pkey_index = 0;
630 attr->en_sqd_async_notify = 0;
631 attr->sq_draining = 0;
632 attr->max_rd_atomic = qp->s_max_rd_atomic;
633 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
634 attr->min_rnr_timer = qp->r_min_rnr_timer;
635 attr->port_num = 1;
636 attr->timeout = qp->timeout;
637 attr->retry_cnt = qp->s_retry_cnt;
638 attr->rnr_retry = qp->s_rnr_retry;
639 attr->alt_port_num = 0;
640 attr->alt_timeout = 0;
642 init_attr->event_handler = qp->ibqp.event_handler;
643 init_attr->qp_context = qp->ibqp.qp_context;
644 init_attr->send_cq = qp->ibqp.send_cq;
645 init_attr->recv_cq = qp->ibqp.recv_cq;
646 init_attr->srq = qp->ibqp.srq;
647 init_attr->cap = attr->cap;
648 if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
649 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
650 else
651 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
652 init_attr->qp_type = qp->ibqp.qp_type;
653 init_attr->port_num = 1;
654 return 0;
658 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
659 * @qp: the queue pair to compute the AETH for
661 * Returns the AETH.
663 __be32 ipath_compute_aeth(struct ipath_qp *qp)
665 u32 aeth = qp->r_msn & IPATH_MSN_MASK;
667 if (qp->ibqp.srq) {
669 * Shared receive queues don't generate credits.
670 * Set the credit field to the invalid value.
672 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
673 } else {
674 u32 min, max, x;
675 u32 credits;
676 struct ipath_rwq *wq = qp->r_rq.wq;
677 u32 head;
678 u32 tail;
680 /* sanity check pointers before trusting them */
681 head = wq->head;
682 if (head >= qp->r_rq.size)
683 head = 0;
684 tail = wq->tail;
685 if (tail >= qp->r_rq.size)
686 tail = 0;
688 * Compute the number of credits available (RWQEs).
689 * XXX Not holding the r_rq.lock here so there is a small
690 * chance that the pair of reads are not atomic.
692 credits = head - tail;
693 if ((int)credits < 0)
694 credits += qp->r_rq.size;
696 * Binary search the credit table to find the code to
697 * use.
699 min = 0;
700 max = 31;
701 for (;;) {
702 x = (min + max) / 2;
703 if (credit_table[x] == credits)
704 break;
705 if (credit_table[x] > credits)
706 max = x;
707 else if (min == x)
708 break;
709 else
710 min = x;
712 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
714 return cpu_to_be32(aeth);
718 * ipath_create_qp - create a queue pair for a device
719 * @ibpd: the protection domain who's device we create the queue pair for
720 * @init_attr: the attributes of the queue pair
721 * @udata: unused by InfiniPath
723 * Returns the queue pair on success, otherwise returns an errno.
725 * Called by the ib_create_qp() core verbs function.
727 struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
728 struct ib_qp_init_attr *init_attr,
729 struct ib_udata *udata)
731 struct ipath_qp *qp;
732 int err;
733 struct ipath_swqe *swq = NULL;
734 struct ipath_ibdev *dev;
735 size_t sz;
736 struct ib_qp *ret;
738 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
739 init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
740 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
741 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
742 ret = ERR_PTR(-ENOMEM);
743 goto bail;
746 if (init_attr->cap.max_send_sge +
747 init_attr->cap.max_recv_sge +
748 init_attr->cap.max_send_wr +
749 init_attr->cap.max_recv_wr == 0) {
750 ret = ERR_PTR(-EINVAL);
751 goto bail;
754 switch (init_attr->qp_type) {
755 case IB_QPT_UC:
756 case IB_QPT_RC:
757 sz = sizeof(struct ipath_sge) *
758 init_attr->cap.max_send_sge +
759 sizeof(struct ipath_swqe);
760 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
761 if (swq == NULL) {
762 ret = ERR_PTR(-ENOMEM);
763 goto bail;
765 /* FALLTHROUGH */
766 case IB_QPT_UD:
767 case IB_QPT_SMI:
768 case IB_QPT_GSI:
769 sz = sizeof(*qp);
770 if (init_attr->srq) {
771 struct ipath_srq *srq = to_isrq(init_attr->srq);
773 sz += sizeof(*qp->r_sg_list) *
774 srq->rq.max_sge;
775 } else
776 sz += sizeof(*qp->r_sg_list) *
777 init_attr->cap.max_recv_sge;
778 qp = kmalloc(sz, GFP_KERNEL);
779 if (!qp) {
780 ret = ERR_PTR(-ENOMEM);
781 goto bail_swq;
783 if (init_attr->srq) {
784 sz = 0;
785 qp->r_rq.size = 0;
786 qp->r_rq.max_sge = 0;
787 qp->r_rq.wq = NULL;
788 init_attr->cap.max_recv_wr = 0;
789 init_attr->cap.max_recv_sge = 0;
790 } else {
791 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
792 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
793 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
794 sizeof(struct ipath_rwqe);
795 qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
796 qp->r_rq.size * sz);
797 if (!qp->r_rq.wq) {
798 ret = ERR_PTR(-ENOMEM);
799 goto bail_qp;
804 * ib_create_qp() will initialize qp->ibqp
805 * except for qp->ibqp.qp_num.
807 spin_lock_init(&qp->s_lock);
808 spin_lock_init(&qp->r_rq.lock);
809 atomic_set(&qp->refcount, 0);
810 init_waitqueue_head(&qp->wait);
811 tasklet_init(&qp->s_task, ipath_do_ruc_send,
812 (unsigned long)qp);
813 INIT_LIST_HEAD(&qp->piowait);
814 INIT_LIST_HEAD(&qp->timerwait);
815 qp->state = IB_QPS_RESET;
816 qp->s_wq = swq;
817 qp->s_size = init_attr->cap.max_send_wr + 1;
818 qp->s_max_sge = init_attr->cap.max_send_sge;
819 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
820 qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
821 else
822 qp->s_flags = 0;
823 dev = to_idev(ibpd->device);
824 err = ipath_alloc_qpn(&dev->qp_table, qp,
825 init_attr->qp_type);
826 if (err) {
827 ret = ERR_PTR(err);
828 goto bail_rwq;
830 qp->ip = NULL;
831 ipath_reset_qp(qp);
832 break;
834 default:
835 /* Don't support raw QPs */
836 ret = ERR_PTR(-ENOSYS);
837 goto bail;
840 init_attr->cap.max_inline_data = 0;
843 * Return the address of the RWQ as the offset to mmap.
844 * See ipath_mmap() for details.
846 if (udata && udata->outlen >= sizeof(__u64)) {
847 int err;
849 if (!qp->r_rq.wq) {
850 __u64 offset = 0;
852 err = ib_copy_to_udata(udata, &offset,
853 sizeof(offset));
854 if (err) {
855 ret = ERR_PTR(err);
856 goto bail_rwq;
858 } else {
859 u32 s = sizeof(struct ipath_rwq) +
860 qp->r_rq.size * sz;
862 qp->ip =
863 ipath_create_mmap_info(dev, s,
864 ibpd->uobject->context,
865 qp->r_rq.wq);
866 if (!qp->ip) {
867 ret = ERR_PTR(-ENOMEM);
868 goto bail_rwq;
871 err = ib_copy_to_udata(udata, &(qp->ip->offset),
872 sizeof(qp->ip->offset));
873 if (err) {
874 ret = ERR_PTR(err);
875 goto bail_ip;
880 spin_lock(&dev->n_qps_lock);
881 if (dev->n_qps_allocated == ib_ipath_max_qps) {
882 spin_unlock(&dev->n_qps_lock);
883 ret = ERR_PTR(-ENOMEM);
884 goto bail_ip;
887 dev->n_qps_allocated++;
888 spin_unlock(&dev->n_qps_lock);
890 if (qp->ip) {
891 spin_lock_irq(&dev->pending_lock);
892 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
893 spin_unlock_irq(&dev->pending_lock);
896 ret = &qp->ibqp;
897 goto bail;
899 bail_ip:
900 kfree(qp->ip);
901 bail_rwq:
902 vfree(qp->r_rq.wq);
903 bail_qp:
904 kfree(qp);
905 bail_swq:
906 vfree(swq);
907 bail:
908 return ret;
912 * ipath_destroy_qp - destroy a queue pair
913 * @ibqp: the queue pair to destroy
915 * Returns 0 on success.
917 * Note that this can be called while the QP is actively sending or
918 * receiving!
920 int ipath_destroy_qp(struct ib_qp *ibqp)
922 struct ipath_qp *qp = to_iqp(ibqp);
923 struct ipath_ibdev *dev = to_idev(ibqp->device);
924 unsigned long flags;
926 spin_lock_irqsave(&qp->s_lock, flags);
927 qp->state = IB_QPS_ERR;
928 spin_unlock_irqrestore(&qp->s_lock, flags);
929 spin_lock(&dev->n_qps_lock);
930 dev->n_qps_allocated--;
931 spin_unlock(&dev->n_qps_lock);
933 /* Stop the sending tasklet. */
934 tasklet_kill(&qp->s_task);
936 /* Make sure the QP isn't on the timeout list. */
937 spin_lock_irqsave(&dev->pending_lock, flags);
938 if (!list_empty(&qp->timerwait))
939 list_del_init(&qp->timerwait);
940 if (!list_empty(&qp->piowait))
941 list_del_init(&qp->piowait);
942 spin_unlock_irqrestore(&dev->pending_lock, flags);
945 * Make sure that the QP is not in the QPN table so receive
946 * interrupts will discard packets for this QP. XXX Also remove QP
947 * from multicast table.
949 if (atomic_read(&qp->refcount) != 0)
950 ipath_free_qp(&dev->qp_table, qp);
952 if (qp->ip)
953 kref_put(&qp->ip->ref, ipath_release_mmap_info);
954 else
955 vfree(qp->r_rq.wq);
956 vfree(qp->s_wq);
957 kfree(qp);
958 return 0;
962 * ipath_init_qp_table - initialize the QP table for a device
963 * @idev: the device who's QP table we're initializing
964 * @size: the size of the QP table
966 * Returns 0 on success, otherwise returns an errno.
968 int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
970 int i;
971 int ret;
973 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
974 idev->qp_table.max = size;
975 idev->qp_table.nmaps = 1;
976 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
977 GFP_KERNEL);
978 if (idev->qp_table.table == NULL) {
979 ret = -ENOMEM;
980 goto bail;
983 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
984 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
985 idev->qp_table.map[i].page = NULL;
988 ret = 0;
990 bail:
991 return ret;
995 * ipath_sqerror_qp - put a QP's send queue into an error state
996 * @qp: QP who's send queue will be put into an error state
997 * @wc: the WC responsible for putting the QP in this state
999 * Flushes the send work queue.
1000 * The QP s_lock should be held and interrupts disabled.
1003 void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
1005 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
1006 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
1008 ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
1009 qp->ibqp.qp_num, qp->remote_qpn, wc->status);
1011 spin_lock(&dev->pending_lock);
1012 /* XXX What if its already removed by the timeout code? */
1013 if (!list_empty(&qp->timerwait))
1014 list_del_init(&qp->timerwait);
1015 if (!list_empty(&qp->piowait))
1016 list_del_init(&qp->piowait);
1017 spin_unlock(&dev->pending_lock);
1019 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1020 if (++qp->s_last >= qp->s_size)
1021 qp->s_last = 0;
1023 wc->status = IB_WC_WR_FLUSH_ERR;
1025 while (qp->s_last != qp->s_head) {
1026 wqe = get_swqe_ptr(qp, qp->s_last);
1027 wc->wr_id = wqe->wr.wr_id;
1028 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1029 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1030 if (++qp->s_last >= qp->s_size)
1031 qp->s_last = 0;
1033 qp->s_cur = qp->s_tail = qp->s_head;
1034 qp->state = IB_QPS_SQE;
1038 * ipath_get_credit - flush the send work queue of a QP
1039 * @qp: the qp who's send work queue to flush
1040 * @aeth: the Acknowledge Extended Transport Header
1042 * The QP s_lock should be held.
1044 void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1046 u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
1049 * If the credit is invalid, we can send
1050 * as many packets as we like. Otherwise, we have to
1051 * honor the credit field.
1053 if (credit == IPATH_AETH_CREDIT_INVAL)
1054 qp->s_lsn = (u32) -1;
1055 else if (qp->s_lsn != (u32) -1) {
1056 /* Compute new LSN (i.e., MSN + credit) */
1057 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
1058 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1059 qp->s_lsn = credit;
1062 /* Restart sending if it was blocked due to lack of credits. */
1063 if (qp->s_cur != qp->s_head &&
1064 (qp->s_lsn == (u32) -1 ||
1065 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1066 qp->s_lsn + 1) <= 0))
1067 tasklet_hi_schedule(&qp->s_task);