2 * Copyright (c) 2006, 2007 QLogic Corporation. 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
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
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 + \
44 #define find_next_offset(map, off) find_next_zero_bit((map)->page, \
48 * Convert the AETH credit code into the number of credits.
50 static u32 credit_table[31] = {
85 static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map)
87 unsigned long page = get_zeroed_page(GFP_KERNEL);
91 * Free the page if someone raced with us installing it.
94 spin_lock_irqsave(&qpt->lock, flags);
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;
109 if (type == IB_QPT_SMI)
111 else if (type == IB_QPT_GSI)
116 if (unlikely(!map->page)) {
117 get_map_page(qpt, map);
118 if (unlikely(!map->page)) {
123 if (!test_and_set_bit(ret, map->page))
124 atomic_dec(&map->n_free);
133 offset = qpn & BITS_PER_PAGE_MASK;
134 map = &qpt->map[qpn / BITS_PER_PAGE];
135 max_scan = qpt->nmaps - !offset;
137 if (unlikely(!map->page)) {
138 get_map_page(qpt, map);
139 if (unlikely(!map->page))
142 if (likely(atomic_read(&map->n_free))) {
144 if (!test_and_set_bit(offset, map->page)) {
145 atomic_dec(&map->n_free);
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
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)
170 map = &qpt->map[qpt->nmaps++];
172 } else if (map < &qpt->map[qpt->nmaps]) {
179 qpn = mk_qpn(qpt, map, offset);
188 static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
192 map = qpt->map + qpn / BITS_PER_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
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)
213 ret = alloc_qpn(qpt, type);
216 qp->ibqp.qp_num = ret;
218 /* Add the QP to the hash table. */
219 spin_lock_irqsave(&qpt->lock, flags);
222 qp->next = qpt->table[ret];
223 qpt->table[ret] = qp;
224 atomic_inc(&qp->refcount);
226 spin_unlock_irqrestore(&qpt->lock, flags);
234 * ipath_free_qp - remove a QP from 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;
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) {
255 atomic_dec(&qp->refcount);
261 spin_unlock_irqrestore(&qpt->lock, flags);
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)
278 struct ipath_qp *qp, *nqp;
281 for (n = 0; n < qpt->max; n++) {
282 spin_lock_irqsave(&qpt->lock, flags);
284 qpt->table[n] = NULL;
285 spin_unlock_irqrestore(&qpt->lock, flags);
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");
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
306 * @qpn: the QP number to look up
308 * The caller is responsible for decrementing the QP reference count
311 struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
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);
325 spin_unlock_irqrestore(&qpt->lock, flags);
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)
337 qp->qp_access_flags = 0;
339 qp->s_flags &= IPATH_S_SIGNAL_REQ_WR;
345 if (qp->ibqp.qp_type == IB_QPT_RC) {
346 qp->s_state = IB_OPCODE_RC_SEND_LAST;
347 qp->r_state = IB_OPCODE_RC_SEND_LAST;
349 qp->s_state = IB_OPCODE_UC_SEND_LAST;
350 qp->r_state = IB_OPCODE_UC_SEND_LAST;
352 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
354 qp->r_wrid_valid = 0;
355 qp->s_rnr_timeout = 0;
362 qp->s_wait_credit = 0;
363 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
364 qp->r_head_ack_queue = 0;
365 qp->s_tail_ack_queue = 0;
366 qp->s_num_rd_atomic = 0;
368 qp->r_rq.wq->head = 0;
369 qp->r_rq.wq->tail = 0;
375 * ipath_error_qp - put a QP into an error state
376 * @qp: the QP to put into an error state
377 * @err: the receive completion error to signal if a RWQE is active
379 * Flushes both send and receive work queues.
380 * Returns true if last WQE event should be generated.
381 * The QP s_lock should be held and interrupts disabled.
384 int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
386 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
390 ipath_dbg("QP%d/%d in error state (%d)\n",
391 qp->ibqp.qp_num, qp->remote_qpn, err);
393 spin_lock(&dev->pending_lock);
394 /* XXX What if its already removed by the timeout code? */
395 if (!list_empty(&qp->timerwait))
396 list_del_init(&qp->timerwait);
397 if (!list_empty(&qp->piowait))
398 list_del_init(&qp->piowait);
399 spin_unlock(&dev->pending_lock);
410 wc.dlid_path_bits = 0;
412 if (qp->r_wrid_valid) {
413 qp->r_wrid_valid = 0;
414 wc.wr_id = qp->r_wr_id;
415 wc.opcode = IB_WC_RECV;
417 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
419 wc.status = IB_WC_WR_FLUSH_ERR;
421 while (qp->s_last != qp->s_head) {
422 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
424 wc.wr_id = wqe->wr.wr_id;
425 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
426 if (++qp->s_last >= qp->s_size)
428 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
430 qp->s_cur = qp->s_tail = qp->s_head;
432 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
435 struct ipath_rwq *wq;
439 spin_lock(&qp->r_rq.lock);
441 /* sanity check pointers before trusting them */
444 if (head >= qp->r_rq.size)
447 if (tail >= qp->r_rq.size)
449 wc.opcode = IB_WC_RECV;
450 while (tail != head) {
451 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
452 if (++tail >= qp->r_rq.size)
454 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
458 spin_unlock(&qp->r_rq.lock);
459 } else if (qp->ibqp.event_handler)
466 * ipath_modify_qp - modify the attributes of a queue pair
467 * @ibqp: the queue pair who's attributes we're modifying
468 * @attr: the new attributes
469 * @attr_mask: the mask of attributes to modify
470 * @udata: user data for ipathverbs.so
472 * Returns 0 on success, otherwise returns an errno.
474 int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
475 int attr_mask, struct ib_udata *udata)
477 struct ipath_ibdev *dev = to_idev(ibqp->device);
478 struct ipath_qp *qp = to_iqp(ibqp);
479 enum ib_qp_state cur_state, new_state;
484 spin_lock_irqsave(&qp->s_lock, flags);
486 cur_state = attr_mask & IB_QP_CUR_STATE ?
487 attr->cur_qp_state : qp->state;
488 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
490 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
494 if (attr_mask & IB_QP_AV) {
495 if (attr->ah_attr.dlid == 0 ||
496 attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
499 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
500 (attr->ah_attr.grh.sgid_index > 1))
504 if (attr_mask & IB_QP_PKEY_INDEX)
505 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
508 if (attr_mask & IB_QP_MIN_RNR_TIMER)
509 if (attr->min_rnr_timer > 31)
512 if (attr_mask & IB_QP_PORT)
513 if (attr->port_num == 0 ||
514 attr->port_num > ibqp->device->phys_port_cnt)
518 * Note: the chips support a maximum MTU of 4096, but the driver
519 * hasn't implemented this feature yet, so don't allow Path MTU
520 * values greater than 2048.
522 if (attr_mask & IB_QP_PATH_MTU)
523 if (attr->path_mtu > IB_MTU_2048)
526 if (attr_mask & IB_QP_PATH_MIG_STATE)
527 if (attr->path_mig_state != IB_MIG_MIGRATED &&
528 attr->path_mig_state != IB_MIG_REARM)
531 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
532 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
541 lastwqe = ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
549 if (attr_mask & IB_QP_PKEY_INDEX)
550 qp->s_pkey_index = attr->pkey_index;
552 if (attr_mask & IB_QP_DEST_QPN)
553 qp->remote_qpn = attr->dest_qp_num;
555 if (attr_mask & IB_QP_SQ_PSN) {
556 qp->s_psn = qp->s_next_psn = attr->sq_psn;
557 qp->s_last_psn = qp->s_next_psn - 1;
560 if (attr_mask & IB_QP_RQ_PSN)
561 qp->r_psn = attr->rq_psn;
563 if (attr_mask & IB_QP_ACCESS_FLAGS)
564 qp->qp_access_flags = attr->qp_access_flags;
566 if (attr_mask & IB_QP_AV)
567 qp->remote_ah_attr = attr->ah_attr;
569 if (attr_mask & IB_QP_PATH_MTU)
570 qp->path_mtu = attr->path_mtu;
572 if (attr_mask & IB_QP_RETRY_CNT)
573 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
575 if (attr_mask & IB_QP_RNR_RETRY) {
576 qp->s_rnr_retry = attr->rnr_retry;
577 if (qp->s_rnr_retry > 7)
579 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
582 if (attr_mask & IB_QP_MIN_RNR_TIMER)
583 qp->r_min_rnr_timer = attr->min_rnr_timer;
585 if (attr_mask & IB_QP_TIMEOUT)
586 qp->timeout = attr->timeout;
588 if (attr_mask & IB_QP_QKEY)
589 qp->qkey = attr->qkey;
591 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
592 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
594 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
595 qp->s_max_rd_atomic = attr->max_rd_atomic;
597 qp->state = new_state;
598 spin_unlock_irqrestore(&qp->s_lock, flags);
603 ev.device = qp->ibqp.device;
604 ev.element.qp = &qp->ibqp;
605 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
606 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
612 spin_unlock_irqrestore(&qp->s_lock, flags);
619 int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
620 int attr_mask, struct ib_qp_init_attr *init_attr)
622 struct ipath_qp *qp = to_iqp(ibqp);
624 attr->qp_state = qp->state;
625 attr->cur_qp_state = attr->qp_state;
626 attr->path_mtu = qp->path_mtu;
627 attr->path_mig_state = 0;
628 attr->qkey = qp->qkey;
629 attr->rq_psn = qp->r_psn;
630 attr->sq_psn = qp->s_next_psn;
631 attr->dest_qp_num = qp->remote_qpn;
632 attr->qp_access_flags = qp->qp_access_flags;
633 attr->cap.max_send_wr = qp->s_size - 1;
634 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
635 attr->cap.max_send_sge = qp->s_max_sge;
636 attr->cap.max_recv_sge = qp->r_rq.max_sge;
637 attr->cap.max_inline_data = 0;
638 attr->ah_attr = qp->remote_ah_attr;
639 memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
640 attr->pkey_index = qp->s_pkey_index;
641 attr->alt_pkey_index = 0;
642 attr->en_sqd_async_notify = 0;
643 attr->sq_draining = 0;
644 attr->max_rd_atomic = qp->s_max_rd_atomic;
645 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
646 attr->min_rnr_timer = qp->r_min_rnr_timer;
648 attr->timeout = qp->timeout;
649 attr->retry_cnt = qp->s_retry_cnt;
650 attr->rnr_retry = qp->s_rnr_retry;
651 attr->alt_port_num = 0;
652 attr->alt_timeout = 0;
654 init_attr->event_handler = qp->ibqp.event_handler;
655 init_attr->qp_context = qp->ibqp.qp_context;
656 init_attr->send_cq = qp->ibqp.send_cq;
657 init_attr->recv_cq = qp->ibqp.recv_cq;
658 init_attr->srq = qp->ibqp.srq;
659 init_attr->cap = attr->cap;
660 if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
661 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
663 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
664 init_attr->qp_type = qp->ibqp.qp_type;
665 init_attr->port_num = 1;
670 * ipath_compute_aeth - compute the AETH (syndrome + MSN)
671 * @qp: the queue pair to compute the AETH for
675 __be32 ipath_compute_aeth(struct ipath_qp *qp)
677 u32 aeth = qp->r_msn & IPATH_MSN_MASK;
681 * Shared receive queues don't generate credits.
682 * Set the credit field to the invalid value.
684 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
688 struct ipath_rwq *wq = qp->r_rq.wq;
692 /* sanity check pointers before trusting them */
694 if (head >= qp->r_rq.size)
697 if (tail >= qp->r_rq.size)
700 * Compute the number of credits available (RWQEs).
701 * XXX Not holding the r_rq.lock here so there is a small
702 * chance that the pair of reads are not atomic.
704 credits = head - tail;
705 if ((int)credits < 0)
706 credits += qp->r_rq.size;
708 * Binary search the credit table to find the code to
715 if (credit_table[x] == credits)
717 if (credit_table[x] > credits)
724 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
726 return cpu_to_be32(aeth);
730 * ipath_create_qp - create a queue pair for a device
731 * @ibpd: the protection domain who's device we create the queue pair for
732 * @init_attr: the attributes of the queue pair
733 * @udata: unused by InfiniPath
735 * Returns the queue pair on success, otherwise returns an errno.
737 * Called by the ib_create_qp() core verbs function.
739 struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
740 struct ib_qp_init_attr *init_attr,
741 struct ib_udata *udata)
745 struct ipath_swqe *swq = NULL;
746 struct ipath_ibdev *dev;
750 if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
751 init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
752 init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
753 init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
754 ret = ERR_PTR(-ENOMEM);
758 if (init_attr->cap.max_send_sge +
759 init_attr->cap.max_recv_sge +
760 init_attr->cap.max_send_wr +
761 init_attr->cap.max_recv_wr == 0) {
762 ret = ERR_PTR(-EINVAL);
766 switch (init_attr->qp_type) {
772 sz = sizeof(struct ipath_sge) *
773 init_attr->cap.max_send_sge +
774 sizeof(struct ipath_swqe);
775 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
777 ret = ERR_PTR(-ENOMEM);
781 if (init_attr->srq) {
782 struct ipath_srq *srq = to_isrq(init_attr->srq);
784 sz += sizeof(*qp->r_sg_list) *
787 sz += sizeof(*qp->r_sg_list) *
788 init_attr->cap.max_recv_sge;
789 qp = kmalloc(sz, GFP_KERNEL);
791 ret = ERR_PTR(-ENOMEM);
794 if (init_attr->srq) {
797 qp->r_rq.max_sge = 0;
799 init_attr->cap.max_recv_wr = 0;
800 init_attr->cap.max_recv_sge = 0;
802 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
803 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
804 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
805 sizeof(struct ipath_rwqe);
806 qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
809 ret = ERR_PTR(-ENOMEM);
815 * ib_create_qp() will initialize qp->ibqp
816 * except for qp->ibqp.qp_num.
818 spin_lock_init(&qp->s_lock);
819 spin_lock_init(&qp->r_rq.lock);
820 atomic_set(&qp->refcount, 0);
821 init_waitqueue_head(&qp->wait);
822 tasklet_init(&qp->s_task, ipath_do_send, (unsigned long)qp);
823 INIT_LIST_HEAD(&qp->piowait);
824 INIT_LIST_HEAD(&qp->timerwait);
825 qp->state = IB_QPS_RESET;
827 qp->s_size = init_attr->cap.max_send_wr + 1;
828 qp->s_max_sge = init_attr->cap.max_send_sge;
829 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
830 qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
833 dev = to_idev(ibpd->device);
834 err = ipath_alloc_qpn(&dev->qp_table, qp,
846 /* Don't support raw QPs */
847 ret = ERR_PTR(-ENOSYS);
851 init_attr->cap.max_inline_data = 0;
854 * Return the address of the RWQ as the offset to mmap.
855 * See ipath_mmap() for details.
857 if (udata && udata->outlen >= sizeof(__u64)) {
861 err = ib_copy_to_udata(udata, &offset,
868 u32 s = sizeof(struct ipath_rwq) +
872 ipath_create_mmap_info(dev, s,
873 ibpd->uobject->context,
876 ret = ERR_PTR(-ENOMEM);
880 err = ib_copy_to_udata(udata, &(qp->ip->offset),
881 sizeof(qp->ip->offset));
889 spin_lock(&dev->n_qps_lock);
890 if (dev->n_qps_allocated == ib_ipath_max_qps) {
891 spin_unlock(&dev->n_qps_lock);
892 ret = ERR_PTR(-ENOMEM);
896 dev->n_qps_allocated++;
897 spin_unlock(&dev->n_qps_lock);
900 spin_lock_irq(&dev->pending_lock);
901 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
902 spin_unlock_irq(&dev->pending_lock);
910 kref_put(&qp->ip->ref, ipath_release_mmap_info);
913 ipath_free_qp(&dev->qp_table, qp);
923 * ipath_destroy_qp - destroy a queue pair
924 * @ibqp: the queue pair to destroy
926 * Returns 0 on success.
928 * Note that this can be called while the QP is actively sending or
931 int ipath_destroy_qp(struct ib_qp *ibqp)
933 struct ipath_qp *qp = to_iqp(ibqp);
934 struct ipath_ibdev *dev = to_idev(ibqp->device);
937 spin_lock_irqsave(&qp->s_lock, flags);
938 qp->state = IB_QPS_ERR;
939 spin_unlock_irqrestore(&qp->s_lock, flags);
940 spin_lock(&dev->n_qps_lock);
941 dev->n_qps_allocated--;
942 spin_unlock(&dev->n_qps_lock);
944 /* Stop the sending tasklet. */
945 tasklet_kill(&qp->s_task);
947 /* Make sure the QP isn't on the timeout list. */
948 spin_lock_irqsave(&dev->pending_lock, flags);
949 if (!list_empty(&qp->timerwait))
950 list_del_init(&qp->timerwait);
951 if (!list_empty(&qp->piowait))
952 list_del_init(&qp->piowait);
953 spin_unlock_irqrestore(&dev->pending_lock, flags);
956 * Make sure that the QP is not in the QPN table so receive
957 * interrupts will discard packets for this QP. XXX Also remove QP
958 * from multicast table.
960 if (atomic_read(&qp->refcount) != 0)
961 ipath_free_qp(&dev->qp_table, qp);
964 kref_put(&qp->ip->ref, ipath_release_mmap_info);
973 * ipath_init_qp_table - initialize the QP table for a device
974 * @idev: the device who's QP table we're initializing
975 * @size: the size of the QP table
977 * Returns 0 on success, otherwise returns an errno.
979 int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
984 idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
985 idev->qp_table.max = size;
986 idev->qp_table.nmaps = 1;
987 idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
989 if (idev->qp_table.table == NULL) {
994 for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
995 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
996 idev->qp_table.map[i].page = NULL;
1006 * ipath_sqerror_qp - put a QP's send queue into an error state
1007 * @qp: QP who's send queue will be put into an error state
1008 * @wc: the WC responsible for putting the QP in this state
1010 * Flushes the send work queue.
1011 * The QP s_lock should be held and interrupts disabled.
1014 void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
1016 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
1017 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
1019 ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
1020 qp->ibqp.qp_num, qp->remote_qpn, wc->status);
1022 spin_lock(&dev->pending_lock);
1023 /* XXX What if its already removed by the timeout code? */
1024 if (!list_empty(&qp->timerwait))
1025 list_del_init(&qp->timerwait);
1026 if (!list_empty(&qp->piowait))
1027 list_del_init(&qp->piowait);
1028 spin_unlock(&dev->pending_lock);
1030 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1031 if (++qp->s_last >= qp->s_size)
1034 wc->status = IB_WC_WR_FLUSH_ERR;
1036 while (qp->s_last != qp->s_head) {
1037 wqe = get_swqe_ptr(qp, qp->s_last);
1038 wc->wr_id = wqe->wr.wr_id;
1039 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1040 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1041 if (++qp->s_last >= qp->s_size)
1044 qp->s_cur = qp->s_tail = qp->s_head;
1045 qp->state = IB_QPS_SQE;
1049 * 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
1053 * The QP s_lock should be held.
1055 void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1057 u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
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.
1064 if (credit == IPATH_AETH_CREDIT_INVAL)
1065 qp->s_lsn = (u32) -1;
1066 else if (qp->s_lsn != (u32) -1) {
1067 /* Compute new LSN (i.e., MSN + credit) */
1068 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
1069 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1073 /* Restart sending if it was blocked due to lack of credits. */
1074 if (qp->s_cur != qp->s_head &&
1075 (qp->s_lsn == (u32) -1 ||
1076 ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1077 qp->s_lsn + 1) <= 0))
1078 tasklet_hi_schedule(&qp->s_task);