2 * Copyright (c) 2012, 2013 Intel Corporation. All rights reserved.
3 * Copyright (c) 2006 - 2012 QLogic Corporation. * All rights reserved.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/err.h>
36 #include <linux/vmalloc.h>
37 #include <linux/jhash.h>
38 #include <rdma/rdma_vt.h>
39 #ifdef CONFIG_DEBUG_FS
40 #include <linux/seq_file.h>
45 #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
46 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
48 static inline unsigned mk_qpn(struct qib_qpn_table *qpt,
49 struct qpn_map *map, unsigned off)
51 return (map - qpt->map) * BITS_PER_PAGE + off;
54 static inline unsigned find_next_offset(struct qib_qpn_table *qpt,
55 struct qpn_map *map, unsigned off,
60 if (((off & qpt->mask) >> 1) >= n)
61 off = (off | qpt->mask) + 2;
63 off = find_next_zero_bit(map->page, BITS_PER_PAGE, off);
68 * Convert the AETH credit code into the number of credits.
70 static u32 credit_table[31] = {
104 static void get_map_page(struct qib_qpn_table *qpt, struct qpn_map *map,
107 unsigned long page = get_zeroed_page(gfp);
110 * Free the page if someone raced with us installing it.
113 spin_lock(&qpt->lock);
117 map->page = (void *)page;
118 spin_unlock(&qpt->lock);
122 * Allocate the next available QPN or
123 * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
125 static int alloc_qpn(struct qib_devdata *dd, struct qib_qpn_table *qpt,
126 enum ib_qp_type type, u8 port, gfp_t gfp)
128 u32 i, offset, max_scan, qpn;
132 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
135 ret = type == IB_QPT_GSI;
136 n = 1 << (ret + 2 * (port - 1));
137 spin_lock(&qpt->lock);
142 spin_unlock(&qpt->lock);
149 if (qpt->mask && ((qpn & qpt->mask) >> 1) >= dd->n_krcv_queues)
150 qpn = (qpn | qpt->mask) + 2;
151 offset = qpn & BITS_PER_PAGE_MASK;
152 map = &qpt->map[qpn / BITS_PER_PAGE];
153 max_scan = qpt->nmaps - !offset;
155 if (unlikely(!map->page)) {
156 get_map_page(qpt, map, gfp);
157 if (unlikely(!map->page))
161 if (!test_and_set_bit(offset, map->page)) {
166 offset = find_next_offset(qpt, map, offset,
168 qpn = mk_qpn(qpt, map, offset);
170 * This test differs from alloc_pidmap().
171 * If find_next_offset() does find a zero
172 * bit, we don't need to check for QPN
173 * wrapping around past our starting QPN.
174 * We just need to be sure we don't loop
177 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
179 * In order to keep the number of pages allocated to a
180 * minimum, we scan the all existing pages before increasing
181 * the size of the bitmap table.
183 if (++i > max_scan) {
184 if (qpt->nmaps == QPNMAP_ENTRIES)
186 map = &qpt->map[qpt->nmaps++];
188 } else if (map < &qpt->map[qpt->nmaps]) {
195 qpn = mk_qpn(qpt, map, offset);
204 static void free_qpn(struct qib_qpn_table *qpt, u32 qpn)
208 map = qpt->map + qpn / BITS_PER_PAGE;
210 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
213 static inline unsigned qpn_hash(struct qib_ibdev *dev, u32 qpn)
215 return jhash_1word(qpn, dev->qp_rnd) &
216 (dev->qp_table_size - 1);
221 * Put the QP into the hash table.
222 * The hash table holds a reference to the QP.
224 static void insert_qp(struct qib_ibdev *dev, struct qib_qp *qp)
226 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
228 unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
230 atomic_inc(&qp->refcount);
231 spin_lock_irqsave(&dev->qpt_lock, flags);
233 if (qp->ibqp.qp_num == 0)
234 rcu_assign_pointer(ibp->qp0, qp);
235 else if (qp->ibqp.qp_num == 1)
236 rcu_assign_pointer(ibp->qp1, qp);
238 qp->next = dev->qp_table[n];
239 rcu_assign_pointer(dev->qp_table[n], qp);
242 spin_unlock_irqrestore(&dev->qpt_lock, flags);
246 * Remove the QP from the table so it can't be found asynchronously by
247 * the receive interrupt routine.
249 static void remove_qp(struct qib_ibdev *dev, struct qib_qp *qp)
251 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
252 unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
256 spin_lock_irqsave(&dev->qpt_lock, flags);
258 if (rcu_dereference_protected(ibp->qp0,
259 lockdep_is_held(&dev->qpt_lock)) == qp) {
260 RCU_INIT_POINTER(ibp->qp0, NULL);
261 } else if (rcu_dereference_protected(ibp->qp1,
262 lockdep_is_held(&dev->qpt_lock)) == qp) {
263 RCU_INIT_POINTER(ibp->qp1, NULL);
266 struct qib_qp __rcu **qpp;
269 qpp = &dev->qp_table[n];
270 for (; (q = rcu_dereference_protected(*qpp,
271 lockdep_is_held(&dev->qpt_lock))) != NULL;
274 RCU_INIT_POINTER(*qpp,
275 rcu_dereference_protected(qp->next,
276 lockdep_is_held(&dev->qpt_lock)));
282 spin_unlock_irqrestore(&dev->qpt_lock, flags);
285 atomic_dec(&qp->refcount);
290 * qib_free_all_qps - check for QPs still in use
291 * @qpt: the QP table to empty
293 * There should not be any QPs still in use.
294 * Free memory for table.
296 unsigned qib_free_all_qps(struct qib_devdata *dd)
298 struct qib_ibdev *dev = &dd->verbs_dev;
301 unsigned n, qp_inuse = 0;
303 for (n = 0; n < dd->num_pports; n++) {
304 struct qib_ibport *ibp = &dd->pport[n].ibport_data;
306 if (!qib_mcast_tree_empty(ibp))
309 if (rcu_dereference(ibp->qp0))
311 if (rcu_dereference(ibp->qp1))
316 spin_lock_irqsave(&dev->qpt_lock, flags);
317 for (n = 0; n < dev->qp_table_size; n++) {
318 qp = rcu_dereference_protected(dev->qp_table[n],
319 lockdep_is_held(&dev->qpt_lock));
320 RCU_INIT_POINTER(dev->qp_table[n], NULL);
322 for (; qp; qp = rcu_dereference_protected(qp->next,
323 lockdep_is_held(&dev->qpt_lock)))
326 spin_unlock_irqrestore(&dev->qpt_lock, flags);
333 * qib_lookup_qpn - return the QP with the given QPN
335 * @qpn: the QP number to look up
337 * The caller is responsible for decrementing the QP reference count
340 struct qib_qp *qib_lookup_qpn(struct qib_ibport *ibp, u32 qpn)
342 struct qib_qp *qp = NULL;
345 if (unlikely(qpn <= 1)) {
347 qp = rcu_dereference(ibp->qp0);
349 qp = rcu_dereference(ibp->qp1);
351 atomic_inc(&qp->refcount);
353 struct qib_ibdev *dev = &ppd_from_ibp(ibp)->dd->verbs_dev;
354 unsigned n = qpn_hash(dev, qpn);
356 for (qp = rcu_dereference(dev->qp_table[n]); qp;
357 qp = rcu_dereference(qp->next))
358 if (qp->ibqp.qp_num == qpn) {
359 atomic_inc(&qp->refcount);
368 * qib_reset_qp - initialize the QP state to the reset state
369 * @qp: the QP to reset
372 static void qib_reset_qp(struct qib_qp *qp, enum ib_qp_type type)
374 struct qib_qp_priv *priv = qp->priv;
377 qp->qp_access_flags = 0;
378 atomic_set(&priv->s_dma_busy, 0);
379 qp->s_flags &= QIB_S_SIGNAL_REQ_WR;
385 qp->s_sending_psn = 0;
386 qp->s_sending_hpsn = 0;
390 if (type == IB_QPT_RC) {
391 qp->s_state = IB_OPCODE_RC_SEND_LAST;
392 qp->r_state = IB_OPCODE_RC_SEND_LAST;
394 qp->s_state = IB_OPCODE_UC_SEND_LAST;
395 qp->r_state = IB_OPCODE_UC_SEND_LAST;
397 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
408 qp->s_mig_state = IB_MIG_MIGRATED;
409 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
410 qp->r_head_ack_queue = 0;
411 qp->s_tail_ack_queue = 0;
412 qp->s_num_rd_atomic = 0;
414 qp->r_rq.wq->head = 0;
415 qp->r_rq.wq->tail = 0;
417 qp->r_sge.num_sge = 0;
420 static void clear_mr_refs(struct qib_qp *qp, int clr_sends)
424 if (test_and_clear_bit(QIB_R_REWIND_SGE, &qp->r_aflags))
425 qib_put_ss(&qp->s_rdma_read_sge);
427 qib_put_ss(&qp->r_sge);
430 while (qp->s_last != qp->s_head) {
431 struct qib_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
434 for (i = 0; i < wqe->wr.num_sge; i++) {
435 struct qib_sge *sge = &wqe->sg_list[i];
439 if (qp->ibqp.qp_type == IB_QPT_UD ||
440 qp->ibqp.qp_type == IB_QPT_SMI ||
441 qp->ibqp.qp_type == IB_QPT_GSI)
442 atomic_dec(&to_iah(wqe->ud_wr.ah)->refcount);
443 if (++qp->s_last >= qp->s_size)
447 qib_put_mr(qp->s_rdma_mr);
448 qp->s_rdma_mr = NULL;
452 if (qp->ibqp.qp_type != IB_QPT_RC)
455 for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
456 struct qib_ack_entry *e = &qp->s_ack_queue[n];
458 if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
460 qib_put_mr(e->rdma_sge.mr);
461 e->rdma_sge.mr = NULL;
467 * qib_error_qp - put a QP into the error state
468 * @qp: the QP to put into the error state
469 * @err: the receive completion error to signal if a RWQE is active
471 * Flushes both send and receive work queues.
472 * Returns true if last WQE event should be generated.
473 * The QP r_lock and s_lock should be held and interrupts disabled.
474 * If we are already in error state, just return.
476 int qib_error_qp(struct qib_qp *qp, enum ib_wc_status err)
478 struct qib_qp_priv *priv = qp->priv;
479 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
483 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
486 qp->state = IB_QPS_ERR;
488 if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
489 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
490 del_timer(&qp->s_timer);
493 if (qp->s_flags & QIB_S_ANY_WAIT_SEND)
494 qp->s_flags &= ~QIB_S_ANY_WAIT_SEND;
496 spin_lock(&dev->pending_lock);
497 if (!list_empty(&priv->iowait) && !(qp->s_flags & QIB_S_BUSY)) {
498 qp->s_flags &= ~QIB_S_ANY_WAIT_IO;
499 list_del_init(&priv->iowait);
501 spin_unlock(&dev->pending_lock);
503 if (!(qp->s_flags & QIB_S_BUSY)) {
506 qib_put_mr(qp->s_rdma_mr);
507 qp->s_rdma_mr = NULL;
510 qib_put_txreq(priv->s_tx);
515 /* Schedule the sending tasklet to drain the send work queue. */
516 if (qp->s_last != qp->s_head)
517 qib_schedule_send(qp);
519 clear_mr_refs(qp, 0);
521 memset(&wc, 0, sizeof(wc));
523 wc.opcode = IB_WC_RECV;
525 if (test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags)) {
526 wc.wr_id = qp->r_wr_id;
528 qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
530 wc.status = IB_WC_WR_FLUSH_ERR;
537 spin_lock(&qp->r_rq.lock);
539 /* sanity check pointers before trusting them */
542 if (head >= qp->r_rq.size)
545 if (tail >= qp->r_rq.size)
547 while (tail != head) {
548 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
549 if (++tail >= qp->r_rq.size)
551 qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
555 spin_unlock(&qp->r_rq.lock);
556 } else if (qp->ibqp.event_handler)
564 * qib_modify_qp - modify the attributes of a queue pair
565 * @ibqp: the queue pair who's attributes we're modifying
566 * @attr: the new attributes
567 * @attr_mask: the mask of attributes to modify
568 * @udata: user data for libibverbs.so
570 * Returns 0 on success, otherwise returns an errno.
572 int qib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
573 int attr_mask, struct ib_udata *udata)
575 struct qib_ibdev *dev = to_idev(ibqp->device);
576 struct qib_qp *qp = to_iqp(ibqp);
577 struct qib_qp_priv *priv = qp->priv;
578 enum ib_qp_state cur_state, new_state;
583 u32 pmtu = 0; /* for gcc warning only */
585 spin_lock_irq(&qp->r_lock);
586 spin_lock(&qp->s_lock);
588 cur_state = attr_mask & IB_QP_CUR_STATE ?
589 attr->cur_qp_state : qp->state;
590 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
592 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
593 attr_mask, IB_LINK_LAYER_UNSPECIFIED))
596 if (attr_mask & IB_QP_AV) {
597 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
599 if (qib_check_ah(qp->ibqp.device, &attr->ah_attr))
603 if (attr_mask & IB_QP_ALT_PATH) {
604 if (attr->alt_ah_attr.dlid >=
605 be16_to_cpu(IB_MULTICAST_LID_BASE))
607 if (qib_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
609 if (attr->alt_pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
613 if (attr_mask & IB_QP_PKEY_INDEX)
614 if (attr->pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
617 if (attr_mask & IB_QP_MIN_RNR_TIMER)
618 if (attr->min_rnr_timer > 31)
621 if (attr_mask & IB_QP_PORT)
622 if (qp->ibqp.qp_type == IB_QPT_SMI ||
623 qp->ibqp.qp_type == IB_QPT_GSI ||
624 attr->port_num == 0 ||
625 attr->port_num > ibqp->device->phys_port_cnt)
628 if (attr_mask & IB_QP_DEST_QPN)
629 if (attr->dest_qp_num > QIB_QPN_MASK)
632 if (attr_mask & IB_QP_RETRY_CNT)
633 if (attr->retry_cnt > 7)
636 if (attr_mask & IB_QP_RNR_RETRY)
637 if (attr->rnr_retry > 7)
641 * Don't allow invalid path_mtu values. OK to set greater
642 * than the active mtu (or even the max_cap, if we have tuned
643 * that to a small mtu. We'll set qp->path_mtu
644 * to the lesser of requested attribute mtu and active,
645 * for packetizing messages.
646 * Note that the QP port has to be set in INIT and MTU in RTR.
648 if (attr_mask & IB_QP_PATH_MTU) {
649 struct qib_devdata *dd = dd_from_dev(dev);
650 int mtu, pidx = qp->port_num - 1;
652 mtu = ib_mtu_enum_to_int(attr->path_mtu);
655 if (mtu > dd->pport[pidx].ibmtu) {
656 switch (dd->pport[pidx].ibmtu) {
676 pmtu = attr->path_mtu;
679 if (attr_mask & IB_QP_PATH_MIG_STATE) {
680 if (attr->path_mig_state == IB_MIG_REARM) {
681 if (qp->s_mig_state == IB_MIG_ARMED)
683 if (new_state != IB_QPS_RTS)
685 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
686 if (qp->s_mig_state == IB_MIG_REARM)
688 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
690 if (qp->s_mig_state == IB_MIG_ARMED)
696 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
697 if (attr->max_dest_rd_atomic > QIB_MAX_RDMA_ATOMIC)
702 if (qp->state != IB_QPS_RESET) {
703 qp->state = IB_QPS_RESET;
704 spin_lock(&dev->pending_lock);
705 if (!list_empty(&priv->iowait))
706 list_del_init(&priv->iowait);
707 spin_unlock(&dev->pending_lock);
708 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_ANY_WAIT);
709 spin_unlock(&qp->s_lock);
710 spin_unlock_irq(&qp->r_lock);
711 /* Stop the sending work queue and retry timer */
712 cancel_work_sync(&priv->s_work);
713 del_timer_sync(&qp->s_timer);
714 wait_event(priv->wait_dma,
715 !atomic_read(&priv->s_dma_busy));
717 qib_put_txreq(priv->s_tx);
721 wait_event(qp->wait, !atomic_read(&qp->refcount));
722 spin_lock_irq(&qp->r_lock);
723 spin_lock(&qp->s_lock);
724 clear_mr_refs(qp, 1);
725 qib_reset_qp(qp, ibqp->qp_type);
730 /* Allow event to retrigger if QP set to RTR more than once */
731 qp->r_flags &= ~QIB_R_COMM_EST;
732 qp->state = new_state;
736 qp->s_draining = qp->s_last != qp->s_cur;
737 qp->state = new_state;
741 if (qp->ibqp.qp_type == IB_QPT_RC)
743 qp->state = new_state;
747 lastwqe = qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
751 qp->state = new_state;
755 if (attr_mask & IB_QP_PKEY_INDEX)
756 qp->s_pkey_index = attr->pkey_index;
758 if (attr_mask & IB_QP_PORT)
759 qp->port_num = attr->port_num;
761 if (attr_mask & IB_QP_DEST_QPN)
762 qp->remote_qpn = attr->dest_qp_num;
764 if (attr_mask & IB_QP_SQ_PSN) {
765 qp->s_next_psn = attr->sq_psn & QIB_PSN_MASK;
766 qp->s_psn = qp->s_next_psn;
767 qp->s_sending_psn = qp->s_next_psn;
768 qp->s_last_psn = qp->s_next_psn - 1;
769 qp->s_sending_hpsn = qp->s_last_psn;
772 if (attr_mask & IB_QP_RQ_PSN)
773 qp->r_psn = attr->rq_psn & QIB_PSN_MASK;
775 if (attr_mask & IB_QP_ACCESS_FLAGS)
776 qp->qp_access_flags = attr->qp_access_flags;
778 if (attr_mask & IB_QP_AV) {
779 qp->remote_ah_attr = attr->ah_attr;
780 qp->s_srate = attr->ah_attr.static_rate;
783 if (attr_mask & IB_QP_ALT_PATH) {
784 qp->alt_ah_attr = attr->alt_ah_attr;
785 qp->s_alt_pkey_index = attr->alt_pkey_index;
788 if (attr_mask & IB_QP_PATH_MIG_STATE) {
789 qp->s_mig_state = attr->path_mig_state;
791 qp->remote_ah_attr = qp->alt_ah_attr;
792 qp->port_num = qp->alt_ah_attr.port_num;
793 qp->s_pkey_index = qp->s_alt_pkey_index;
797 if (attr_mask & IB_QP_PATH_MTU) {
799 qp->pmtu = ib_mtu_enum_to_int(pmtu);
802 if (attr_mask & IB_QP_RETRY_CNT) {
803 qp->s_retry_cnt = attr->retry_cnt;
804 qp->s_retry = attr->retry_cnt;
807 if (attr_mask & IB_QP_RNR_RETRY) {
808 qp->s_rnr_retry_cnt = attr->rnr_retry;
809 qp->s_rnr_retry = attr->rnr_retry;
812 if (attr_mask & IB_QP_MIN_RNR_TIMER)
813 qp->r_min_rnr_timer = attr->min_rnr_timer;
815 if (attr_mask & IB_QP_TIMEOUT) {
816 qp->timeout = attr->timeout;
817 qp->timeout_jiffies =
818 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
822 if (attr_mask & IB_QP_QKEY)
823 qp->qkey = attr->qkey;
825 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
826 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
828 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
829 qp->s_max_rd_atomic = attr->max_rd_atomic;
831 spin_unlock(&qp->s_lock);
832 spin_unlock_irq(&qp->r_lock);
834 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
838 ev.device = qp->ibqp.device;
839 ev.element.qp = &qp->ibqp;
840 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
841 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
844 ev.device = qp->ibqp.device;
845 ev.element.qp = &qp->ibqp;
846 ev.event = IB_EVENT_PATH_MIG;
847 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
853 spin_unlock(&qp->s_lock);
854 spin_unlock_irq(&qp->r_lock);
861 int qib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
862 int attr_mask, struct ib_qp_init_attr *init_attr)
864 struct qib_qp *qp = to_iqp(ibqp);
866 attr->qp_state = qp->state;
867 attr->cur_qp_state = attr->qp_state;
868 attr->path_mtu = qp->path_mtu;
869 attr->path_mig_state = qp->s_mig_state;
870 attr->qkey = qp->qkey;
871 attr->rq_psn = qp->r_psn & QIB_PSN_MASK;
872 attr->sq_psn = qp->s_next_psn & QIB_PSN_MASK;
873 attr->dest_qp_num = qp->remote_qpn;
874 attr->qp_access_flags = qp->qp_access_flags;
875 attr->cap.max_send_wr = qp->s_size - 1;
876 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
877 attr->cap.max_send_sge = qp->s_max_sge;
878 attr->cap.max_recv_sge = qp->r_rq.max_sge;
879 attr->cap.max_inline_data = 0;
880 attr->ah_attr = qp->remote_ah_attr;
881 attr->alt_ah_attr = qp->alt_ah_attr;
882 attr->pkey_index = qp->s_pkey_index;
883 attr->alt_pkey_index = qp->s_alt_pkey_index;
884 attr->en_sqd_async_notify = 0;
885 attr->sq_draining = qp->s_draining;
886 attr->max_rd_atomic = qp->s_max_rd_atomic;
887 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
888 attr->min_rnr_timer = qp->r_min_rnr_timer;
889 attr->port_num = qp->port_num;
890 attr->timeout = qp->timeout;
891 attr->retry_cnt = qp->s_retry_cnt;
892 attr->rnr_retry = qp->s_rnr_retry_cnt;
893 attr->alt_port_num = qp->alt_ah_attr.port_num;
894 attr->alt_timeout = qp->alt_timeout;
896 init_attr->event_handler = qp->ibqp.event_handler;
897 init_attr->qp_context = qp->ibqp.qp_context;
898 init_attr->send_cq = qp->ibqp.send_cq;
899 init_attr->recv_cq = qp->ibqp.recv_cq;
900 init_attr->srq = qp->ibqp.srq;
901 init_attr->cap = attr->cap;
902 if (qp->s_flags & QIB_S_SIGNAL_REQ_WR)
903 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
905 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
906 init_attr->qp_type = qp->ibqp.qp_type;
907 init_attr->port_num = qp->port_num;
912 * qib_compute_aeth - compute the AETH (syndrome + MSN)
913 * @qp: the queue pair to compute the AETH for
917 __be32 qib_compute_aeth(struct qib_qp *qp)
919 u32 aeth = qp->r_msn & QIB_MSN_MASK;
923 * Shared receive queues don't generate credits.
924 * Set the credit field to the invalid value.
926 aeth |= QIB_AETH_CREDIT_INVAL << QIB_AETH_CREDIT_SHIFT;
930 struct qib_rwq *wq = qp->r_rq.wq;
934 /* sanity check pointers before trusting them */
936 if (head >= qp->r_rq.size)
939 if (tail >= qp->r_rq.size)
942 * Compute the number of credits available (RWQEs).
943 * XXX Not holding the r_rq.lock here so there is a small
944 * chance that the pair of reads are not atomic.
946 credits = head - tail;
947 if ((int)credits < 0)
948 credits += qp->r_rq.size;
950 * Binary search the credit table to find the code to
957 if (credit_table[x] == credits)
959 if (credit_table[x] > credits)
966 aeth |= x << QIB_AETH_CREDIT_SHIFT;
968 return cpu_to_be32(aeth);
972 * qib_create_qp - create a queue pair for a device
973 * @ibpd: the protection domain who's device we create the queue pair for
974 * @init_attr: the attributes of the queue pair
975 * @udata: user data for libibverbs.so
977 * Returns the queue pair on success, otherwise returns an errno.
979 * Called by the ib_create_qp() core verbs function.
981 struct ib_qp *qib_create_qp(struct ib_pd *ibpd,
982 struct ib_qp_init_attr *init_attr,
983 struct ib_udata *udata)
987 struct qib_swqe *swq = NULL;
988 struct qib_ibdev *dev;
989 struct qib_devdata *dd;
994 struct qib_qp_priv *priv;
996 if (init_attr->cap.max_send_sge > ib_qib_max_sges ||
997 init_attr->cap.max_send_wr > ib_qib_max_qp_wrs ||
998 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
999 return ERR_PTR(-EINVAL);
1001 /* GFP_NOIO is applicable in RC QPs only */
1002 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
1003 init_attr->qp_type != IB_QPT_RC)
1004 return ERR_PTR(-EINVAL);
1006 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
1007 GFP_NOIO : GFP_KERNEL;
1009 /* Check receive queue parameters if no SRQ is specified. */
1010 if (!init_attr->srq) {
1011 if (init_attr->cap.max_recv_sge > ib_qib_max_sges ||
1012 init_attr->cap.max_recv_wr > ib_qib_max_qp_wrs) {
1013 ret = ERR_PTR(-EINVAL);
1016 if (init_attr->cap.max_send_sge +
1017 init_attr->cap.max_send_wr +
1018 init_attr->cap.max_recv_sge +
1019 init_attr->cap.max_recv_wr == 0) {
1020 ret = ERR_PTR(-EINVAL);
1025 switch (init_attr->qp_type) {
1028 if (init_attr->port_num == 0 ||
1029 init_attr->port_num > ibpd->device->phys_port_cnt) {
1030 ret = ERR_PTR(-EINVAL);
1036 sz = sizeof(struct qib_sge) *
1037 init_attr->cap.max_send_sge +
1038 sizeof(struct qib_swqe);
1039 swq = __vmalloc((init_attr->cap.max_send_wr + 1) * sz,
1042 ret = ERR_PTR(-ENOMEM);
1047 if (init_attr->srq) {
1048 struct qib_srq *srq = to_isrq(init_attr->srq);
1050 if (srq->rq.max_sge > 1)
1051 sg_list_sz = sizeof(*qp->r_sg_list) *
1052 (srq->rq.max_sge - 1);
1053 } else if (init_attr->cap.max_recv_sge > 1)
1054 sg_list_sz = sizeof(*qp->r_sg_list) *
1055 (init_attr->cap.max_recv_sge - 1);
1056 qp = kzalloc(sz + sg_list_sz, gfp);
1058 ret = ERR_PTR(-ENOMEM);
1061 RCU_INIT_POINTER(qp->next, NULL);
1062 priv = kzalloc(sizeof(*priv), gfp);
1064 ret = ERR_PTR(-ENOMEM);
1068 priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), gfp);
1070 ret = ERR_PTR(-ENOMEM);
1074 qp->timeout_jiffies =
1075 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1080 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
1081 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
1082 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
1083 sizeof(struct qib_rwqe);
1084 if (gfp != GFP_NOIO)
1085 qp->r_rq.wq = vmalloc_user(
1086 sizeof(struct qib_rwq) +
1087 qp->r_rq.size * sz);
1089 qp->r_rq.wq = __vmalloc(
1090 sizeof(struct qib_rwq) +
1095 ret = ERR_PTR(-ENOMEM);
1101 * ib_create_qp() will initialize qp->ibqp
1102 * except for qp->ibqp.qp_num.
1104 spin_lock_init(&qp->r_lock);
1105 spin_lock_init(&qp->s_lock);
1106 spin_lock_init(&qp->r_rq.lock);
1107 atomic_set(&qp->refcount, 0);
1108 init_waitqueue_head(&qp->wait);
1109 init_waitqueue_head(&priv->wait_dma);
1110 init_timer(&qp->s_timer);
1111 qp->s_timer.data = (unsigned long)qp;
1112 INIT_WORK(&priv->s_work, qib_do_send);
1113 INIT_LIST_HEAD(&priv->iowait);
1114 INIT_LIST_HEAD(&qp->rspwait);
1115 qp->state = IB_QPS_RESET;
1117 qp->s_size = init_attr->cap.max_send_wr + 1;
1118 qp->s_max_sge = init_attr->cap.max_send_sge;
1119 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
1120 qp->s_flags = QIB_S_SIGNAL_REQ_WR;
1121 dev = to_idev(ibpd->device);
1122 dd = dd_from_dev(dev);
1123 err = alloc_qpn(dd, &dev->qpn_table, init_attr->qp_type,
1124 init_attr->port_num, gfp);
1130 qp->ibqp.qp_num = err;
1131 qp->port_num = init_attr->port_num;
1132 qib_reset_qp(qp, init_attr->qp_type);
1136 /* Don't support raw QPs */
1137 ret = ERR_PTR(-ENOSYS);
1141 init_attr->cap.max_inline_data = 0;
1144 * Return the address of the RWQ as the offset to mmap.
1145 * See qib_mmap() for details.
1147 if (udata && udata->outlen >= sizeof(__u64)) {
1151 err = ib_copy_to_udata(udata, &offset,
1158 u32 s = sizeof(struct qib_rwq) + qp->r_rq.size * sz;
1160 qp->ip = qib_create_mmap_info(dev, s,
1161 ibpd->uobject->context,
1164 ret = ERR_PTR(-ENOMEM);
1168 err = ib_copy_to_udata(udata, &(qp->ip->offset),
1169 sizeof(qp->ip->offset));
1177 spin_lock(&dev->n_qps_lock);
1178 if (dev->n_qps_allocated == ib_qib_max_qps) {
1179 spin_unlock(&dev->n_qps_lock);
1180 ret = ERR_PTR(-ENOMEM);
1184 dev->n_qps_allocated++;
1185 spin_unlock(&dev->n_qps_lock);
1188 spin_lock_irq(&dev->pending_lock);
1189 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
1190 spin_unlock_irq(&dev->pending_lock);
1198 kref_put(&qp->ip->ref, qib_release_mmap_info);
1201 free_qpn(&dev->qpn_table, qp->ibqp.qp_num);
1214 * qib_destroy_qp - destroy a queue pair
1215 * @ibqp: the queue pair to destroy
1217 * Returns 0 on success.
1219 * Note that this can be called while the QP is actively sending or
1222 int qib_destroy_qp(struct ib_qp *ibqp)
1224 struct qib_qp *qp = to_iqp(ibqp);
1225 struct qib_ibdev *dev = to_idev(ibqp->device);
1226 struct qib_qp_priv *priv = qp->priv;
1228 /* Make sure HW and driver activity is stopped. */
1229 spin_lock_irq(&qp->s_lock);
1230 if (qp->state != IB_QPS_RESET) {
1231 qp->state = IB_QPS_RESET;
1232 spin_lock(&dev->pending_lock);
1233 if (!list_empty(&priv->iowait))
1234 list_del_init(&priv->iowait);
1235 spin_unlock(&dev->pending_lock);
1236 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_ANY_WAIT);
1237 spin_unlock_irq(&qp->s_lock);
1238 cancel_work_sync(&priv->s_work);
1239 del_timer_sync(&qp->s_timer);
1240 wait_event(priv->wait_dma, !atomic_read(&priv->s_dma_busy));
1242 qib_put_txreq(priv->s_tx);
1246 wait_event(qp->wait, !atomic_read(&qp->refcount));
1247 clear_mr_refs(qp, 1);
1249 spin_unlock_irq(&qp->s_lock);
1251 /* all user's cleaned up, mark it available */
1252 free_qpn(&dev->qpn_table, qp->ibqp.qp_num);
1253 spin_lock(&dev->n_qps_lock);
1254 dev->n_qps_allocated--;
1255 spin_unlock(&dev->n_qps_lock);
1258 kref_put(&qp->ip->ref, qib_release_mmap_info);
1269 * qib_init_qpn_table - initialize the QP number table for a device
1270 * @qpt: the QPN table
1272 void qib_init_qpn_table(struct qib_devdata *dd, struct qib_qpn_table *qpt)
1274 spin_lock_init(&qpt->lock);
1275 qpt->last = 1; /* start with QPN 2 */
1277 qpt->mask = dd->qpn_mask;
1281 * qib_free_qpn_table - free the QP number table for a device
1282 * @qpt: the QPN table
1284 void qib_free_qpn_table(struct qib_qpn_table *qpt)
1288 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
1289 if (qpt->map[i].page)
1290 free_page((unsigned long) qpt->map[i].page);
1294 * qib_get_credit - flush the send work queue of a QP
1295 * @qp: the qp who's send work queue to flush
1296 * @aeth: the Acknowledge Extended Transport Header
1298 * The QP s_lock should be held.
1300 void qib_get_credit(struct qib_qp *qp, u32 aeth)
1302 u32 credit = (aeth >> QIB_AETH_CREDIT_SHIFT) & QIB_AETH_CREDIT_MASK;
1305 * If the credit is invalid, we can send
1306 * as many packets as we like. Otherwise, we have to
1307 * honor the credit field.
1309 if (credit == QIB_AETH_CREDIT_INVAL) {
1310 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT)) {
1311 qp->s_flags |= QIB_S_UNLIMITED_CREDIT;
1312 if (qp->s_flags & QIB_S_WAIT_SSN_CREDIT) {
1313 qp->s_flags &= ~QIB_S_WAIT_SSN_CREDIT;
1314 qib_schedule_send(qp);
1317 } else if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT)) {
1318 /* Compute new LSN (i.e., MSN + credit) */
1319 credit = (aeth + credit_table[credit]) & QIB_MSN_MASK;
1320 if (qib_cmp24(credit, qp->s_lsn) > 0) {
1322 if (qp->s_flags & QIB_S_WAIT_SSN_CREDIT) {
1323 qp->s_flags &= ~QIB_S_WAIT_SSN_CREDIT;
1324 qib_schedule_send(qp);
1330 #ifdef CONFIG_DEBUG_FS
1332 struct qib_qp_iter {
1333 struct qib_ibdev *dev;
1338 struct qib_qp_iter *qib_qp_iter_init(struct qib_ibdev *dev)
1340 struct qib_qp_iter *iter;
1342 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1347 if (qib_qp_iter_next(iter)) {
1355 int qib_qp_iter_next(struct qib_qp_iter *iter)
1357 struct qib_ibdev *dev = iter->dev;
1360 struct qib_qp *pqp = iter->qp;
1363 for (; n < dev->qp_table_size; n++) {
1365 qp = rcu_dereference(pqp->next);
1367 qp = rcu_dereference(dev->qp_table[n]);
1378 static const char * const qp_type_str[] = {
1379 "SMI", "GSI", "RC", "UC", "UD",
1382 void qib_qp_iter_print(struct seq_file *s, struct qib_qp_iter *iter)
1384 struct qib_swqe *wqe;
1385 struct qib_qp *qp = iter->qp;
1386 struct qib_qp_priv *priv = qp->priv;
1388 wqe = get_swqe_ptr(qp, qp->s_last);
1390 "N %d QP%u %s %u %u %u f=%x %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u) QP%u LID %x\n",
1393 qp_type_str[qp->ibqp.qp_type],
1398 atomic_read(&priv->s_dma_busy),
1399 !list_empty(&priv->iowait),
1404 qp->s_psn, qp->s_next_psn,
1405 qp->s_sending_psn, qp->s_sending_hpsn,
1406 qp->s_last, qp->s_acked, qp->s_cur,
1407 qp->s_tail, qp->s_head, qp->s_size,
1409 qp->remote_ah_attr.dlid);