Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
0002 /*
0003  * Copyright(c) 2015 - 2020 Intel Corporation.
0004  */
0005 
0006 #include <linux/err.h>
0007 #include <linux/vmalloc.h>
0008 #include <linux/hash.h>
0009 #include <linux/module.h>
0010 #include <linux/seq_file.h>
0011 #include <rdma/rdma_vt.h>
0012 #include <rdma/rdmavt_qp.h>
0013 #include <rdma/ib_verbs.h>
0014 
0015 #include "hfi.h"
0016 #include "qp.h"
0017 #include "trace.h"
0018 #include "verbs_txreq.h"
0019 
0020 unsigned int hfi1_qp_table_size = 256;
0021 module_param_named(qp_table_size, hfi1_qp_table_size, uint, S_IRUGO);
0022 MODULE_PARM_DESC(qp_table_size, "QP table size");
0023 
0024 static void flush_tx_list(struct rvt_qp *qp);
0025 static int iowait_sleep(
0026     struct sdma_engine *sde,
0027     struct iowait_work *wait,
0028     struct sdma_txreq *stx,
0029     unsigned int seq,
0030     bool pkts_sent);
0031 static void iowait_wakeup(struct iowait *wait, int reason);
0032 static void iowait_sdma_drained(struct iowait *wait);
0033 static void qp_pio_drain(struct rvt_qp *qp);
0034 
0035 const struct rvt_operation_params hfi1_post_parms[RVT_OPERATION_MAX] = {
0036 [IB_WR_RDMA_WRITE] = {
0037     .length = sizeof(struct ib_rdma_wr),
0038     .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
0039 },
0040 
0041 [IB_WR_RDMA_READ] = {
0042     .length = sizeof(struct ib_rdma_wr),
0043     .qpt_support = BIT(IB_QPT_RC),
0044     .flags = RVT_OPERATION_ATOMIC,
0045 },
0046 
0047 [IB_WR_ATOMIC_CMP_AND_SWP] = {
0048     .length = sizeof(struct ib_atomic_wr),
0049     .qpt_support = BIT(IB_QPT_RC),
0050     .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
0051 },
0052 
0053 [IB_WR_ATOMIC_FETCH_AND_ADD] = {
0054     .length = sizeof(struct ib_atomic_wr),
0055     .qpt_support = BIT(IB_QPT_RC),
0056     .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
0057 },
0058 
0059 [IB_WR_RDMA_WRITE_WITH_IMM] = {
0060     .length = sizeof(struct ib_rdma_wr),
0061     .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
0062 },
0063 
0064 [IB_WR_SEND] = {
0065     .length = sizeof(struct ib_send_wr),
0066     .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
0067                BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
0068 },
0069 
0070 [IB_WR_SEND_WITH_IMM] = {
0071     .length = sizeof(struct ib_send_wr),
0072     .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
0073                BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
0074 },
0075 
0076 [IB_WR_REG_MR] = {
0077     .length = sizeof(struct ib_reg_wr),
0078     .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
0079     .flags = RVT_OPERATION_LOCAL,
0080 },
0081 
0082 [IB_WR_LOCAL_INV] = {
0083     .length = sizeof(struct ib_send_wr),
0084     .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
0085     .flags = RVT_OPERATION_LOCAL,
0086 },
0087 
0088 [IB_WR_SEND_WITH_INV] = {
0089     .length = sizeof(struct ib_send_wr),
0090     .qpt_support = BIT(IB_QPT_RC),
0091 },
0092 
0093 [IB_WR_OPFN] = {
0094     .length = sizeof(struct ib_atomic_wr),
0095     .qpt_support = BIT(IB_QPT_RC),
0096     .flags = RVT_OPERATION_USE_RESERVE,
0097 },
0098 
0099 [IB_WR_TID_RDMA_WRITE] = {
0100     .length = sizeof(struct ib_rdma_wr),
0101     .qpt_support = BIT(IB_QPT_RC),
0102     .flags = RVT_OPERATION_IGN_RNR_CNT,
0103 },
0104 
0105 };
0106 
0107 static void flush_list_head(struct list_head *l)
0108 {
0109     while (!list_empty(l)) {
0110         struct sdma_txreq *tx;
0111 
0112         tx = list_first_entry(
0113             l,
0114             struct sdma_txreq,
0115             list);
0116         list_del_init(&tx->list);
0117         hfi1_put_txreq(
0118             container_of(tx, struct verbs_txreq, txreq));
0119     }
0120 }
0121 
0122 static void flush_tx_list(struct rvt_qp *qp)
0123 {
0124     struct hfi1_qp_priv *priv = qp->priv;
0125 
0126     flush_list_head(&iowait_get_ib_work(&priv->s_iowait)->tx_head);
0127     flush_list_head(&iowait_get_tid_work(&priv->s_iowait)->tx_head);
0128 }
0129 
0130 static void flush_iowait(struct rvt_qp *qp)
0131 {
0132     struct hfi1_qp_priv *priv = qp->priv;
0133     unsigned long flags;
0134     seqlock_t *lock = priv->s_iowait.lock;
0135 
0136     if (!lock)
0137         return;
0138     write_seqlock_irqsave(lock, flags);
0139     if (!list_empty(&priv->s_iowait.list)) {
0140         list_del_init(&priv->s_iowait.list);
0141         priv->s_iowait.lock = NULL;
0142         rvt_put_qp(qp);
0143     }
0144     write_sequnlock_irqrestore(lock, flags);
0145 }
0146 
0147 /*
0148  * This function is what we would push to the core layer if we wanted to be a
0149  * "first class citizen".  Instead we hide this here and rely on Verbs ULPs
0150  * to blindly pass the MTU enum value from the PathRecord to us.
0151  */
0152 static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu)
0153 {
0154     /* Constraining 10KB packets to 8KB packets */
0155     if (mtu == (enum ib_mtu)OPA_MTU_10240)
0156         mtu = (enum ib_mtu)OPA_MTU_8192;
0157     return opa_mtu_enum_to_int((enum opa_mtu)mtu);
0158 }
0159 
0160 int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
0161              int attr_mask, struct ib_udata *udata)
0162 {
0163     struct ib_qp *ibqp = &qp->ibqp;
0164     struct hfi1_ibdev *dev = to_idev(ibqp->device);
0165     struct hfi1_devdata *dd = dd_from_dev(dev);
0166     u8 sc;
0167 
0168     if (attr_mask & IB_QP_AV) {
0169         sc = ah_to_sc(ibqp->device, &attr->ah_attr);
0170         if (sc == 0xf)
0171             return -EINVAL;
0172 
0173         if (!qp_to_sdma_engine(qp, sc) &&
0174             dd->flags & HFI1_HAS_SEND_DMA)
0175             return -EINVAL;
0176 
0177         if (!qp_to_send_context(qp, sc))
0178             return -EINVAL;
0179     }
0180 
0181     if (attr_mask & IB_QP_ALT_PATH) {
0182         sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr);
0183         if (sc == 0xf)
0184             return -EINVAL;
0185 
0186         if (!qp_to_sdma_engine(qp, sc) &&
0187             dd->flags & HFI1_HAS_SEND_DMA)
0188             return -EINVAL;
0189 
0190         if (!qp_to_send_context(qp, sc))
0191             return -EINVAL;
0192     }
0193 
0194     return 0;
0195 }
0196 
0197 /*
0198  * qp_set_16b - Set the hdr_type based on whether the slid or the
0199  * dlid in the connection is extended. Only applicable for RC and UC
0200  * QPs. UD QPs determine this on the fly from the ah in the wqe
0201  */
0202 static inline void qp_set_16b(struct rvt_qp *qp)
0203 {
0204     struct hfi1_pportdata *ppd;
0205     struct hfi1_ibport *ibp;
0206     struct hfi1_qp_priv *priv = qp->priv;
0207 
0208     /* Update ah_attr to account for extended LIDs */
0209     hfi1_update_ah_attr(qp->ibqp.device, &qp->remote_ah_attr);
0210 
0211     /* Create 32 bit LIDs */
0212     hfi1_make_opa_lid(&qp->remote_ah_attr);
0213 
0214     if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH))
0215         return;
0216 
0217     ibp = to_iport(qp->ibqp.device, qp->port_num);
0218     ppd = ppd_from_ibp(ibp);
0219     priv->hdr_type = hfi1_get_hdr_type(ppd->lid, &qp->remote_ah_attr);
0220 }
0221 
0222 void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
0223             int attr_mask, struct ib_udata *udata)
0224 {
0225     struct ib_qp *ibqp = &qp->ibqp;
0226     struct hfi1_qp_priv *priv = qp->priv;
0227 
0228     if (attr_mask & IB_QP_AV) {
0229         priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
0230         priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
0231         priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
0232         qp_set_16b(qp);
0233     }
0234 
0235     if (attr_mask & IB_QP_PATH_MIG_STATE &&
0236         attr->path_mig_state == IB_MIG_MIGRATED &&
0237         qp->s_mig_state == IB_MIG_ARMED) {
0238         qp->s_flags |= HFI1_S_AHG_CLEAR;
0239         priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
0240         priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
0241         priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
0242         qp_set_16b(qp);
0243     }
0244 
0245     opfn_qp_init(qp, attr, attr_mask);
0246 }
0247 
0248 /**
0249  * hfi1_setup_wqe - set up the wqe
0250  * @qp: The qp
0251  * @wqe: The built wqe
0252  * @call_send: Determine if the send should be posted or scheduled.
0253  *
0254  * Perform setup of the wqe.  This is called
0255  * prior to inserting the wqe into the ring but after
0256  * the wqe has been setup by RDMAVT. This function
0257  * allows the driver the opportunity to perform
0258  * validation and additional setup of the wqe.
0259  *
0260  * Returns 0 on success, -EINVAL on failure
0261  *
0262  */
0263 int hfi1_setup_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe, bool *call_send)
0264 {
0265     struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
0266     struct rvt_ah *ah;
0267     struct hfi1_pportdata *ppd;
0268     struct hfi1_devdata *dd;
0269 
0270     switch (qp->ibqp.qp_type) {
0271     case IB_QPT_RC:
0272         hfi1_setup_tid_rdma_wqe(qp, wqe);
0273         fallthrough;
0274     case IB_QPT_UC:
0275         if (wqe->length > 0x80000000U)
0276             return -EINVAL;
0277         if (wqe->length > qp->pmtu)
0278             *call_send = false;
0279         break;
0280     case IB_QPT_SMI:
0281         /*
0282          * SM packets should exclusively use VL15 and their SL is
0283          * ignored (IBTA v1.3, Section 3.5.8.2). Therefore, when ah
0284          * is created, SL is 0 in most cases and as a result some
0285          * fields (vl and pmtu) in ah may not be set correctly,
0286          * depending on the SL2SC and SC2VL tables at the time.
0287          */
0288         ppd = ppd_from_ibp(ibp);
0289         dd = dd_from_ppd(ppd);
0290         if (wqe->length > dd->vld[15].mtu)
0291             return -EINVAL;
0292         break;
0293     case IB_QPT_GSI:
0294     case IB_QPT_UD:
0295         ah = rvt_get_swqe_ah(wqe);
0296         if (wqe->length > (1 << ah->log_pmtu))
0297             return -EINVAL;
0298         if (ibp->sl_to_sc[rdma_ah_get_sl(&ah->attr)] == 0xf)
0299             return -EINVAL;
0300         break;
0301     default:
0302         break;
0303     }
0304 
0305     /*
0306      * System latency between send and schedule is large enough that
0307      * forcing call_send to true for piothreshold packets is necessary.
0308      */
0309     if (wqe->length <= piothreshold)
0310         *call_send = true;
0311     return 0;
0312 }
0313 
0314 /**
0315  * _hfi1_schedule_send - schedule progress
0316  * @qp: the QP
0317  *
0318  * This schedules qp progress w/o regard to the s_flags.
0319  *
0320  * It is only used in the post send, which doesn't hold
0321  * the s_lock.
0322  */
0323 bool _hfi1_schedule_send(struct rvt_qp *qp)
0324 {
0325     struct hfi1_qp_priv *priv = qp->priv;
0326     struct hfi1_ibport *ibp =
0327         to_iport(qp->ibqp.device, qp->port_num);
0328     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0329     struct hfi1_devdata *dd = ppd->dd;
0330 
0331     if (dd->flags & HFI1_SHUTDOWN)
0332         return true;
0333 
0334     return iowait_schedule(&priv->s_iowait, ppd->hfi1_wq,
0335                    priv->s_sde ?
0336                    priv->s_sde->cpu :
0337                    cpumask_first(cpumask_of_node(dd->node)));
0338 }
0339 
0340 static void qp_pio_drain(struct rvt_qp *qp)
0341 {
0342     struct hfi1_qp_priv *priv = qp->priv;
0343 
0344     if (!priv->s_sendcontext)
0345         return;
0346     while (iowait_pio_pending(&priv->s_iowait)) {
0347         write_seqlock_irq(&priv->s_sendcontext->waitlock);
0348         hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 1);
0349         write_sequnlock_irq(&priv->s_sendcontext->waitlock);
0350         iowait_pio_drain(&priv->s_iowait);
0351         write_seqlock_irq(&priv->s_sendcontext->waitlock);
0352         hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 0);
0353         write_sequnlock_irq(&priv->s_sendcontext->waitlock);
0354     }
0355 }
0356 
0357 /**
0358  * hfi1_schedule_send - schedule progress
0359  * @qp: the QP
0360  *
0361  * This schedules qp progress and caller should hold
0362  * the s_lock.
0363  * @return true if the first leg is scheduled;
0364  * false if the first leg is not scheduled.
0365  */
0366 bool hfi1_schedule_send(struct rvt_qp *qp)
0367 {
0368     lockdep_assert_held(&qp->s_lock);
0369     if (hfi1_send_ok(qp)) {
0370         _hfi1_schedule_send(qp);
0371         return true;
0372     }
0373     if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
0374         iowait_set_flag(&((struct hfi1_qp_priv *)qp->priv)->s_iowait,
0375                 IOWAIT_PENDING_IB);
0376     return false;
0377 }
0378 
0379 static void hfi1_qp_schedule(struct rvt_qp *qp)
0380 {
0381     struct hfi1_qp_priv *priv = qp->priv;
0382     bool ret;
0383 
0384     if (iowait_flag_set(&priv->s_iowait, IOWAIT_PENDING_IB)) {
0385         ret = hfi1_schedule_send(qp);
0386         if (ret)
0387             iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
0388     }
0389     if (iowait_flag_set(&priv->s_iowait, IOWAIT_PENDING_TID)) {
0390         ret = hfi1_schedule_tid_send(qp);
0391         if (ret)
0392             iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
0393     }
0394 }
0395 
0396 void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag)
0397 {
0398     unsigned long flags;
0399 
0400     spin_lock_irqsave(&qp->s_lock, flags);
0401     if (qp->s_flags & flag) {
0402         qp->s_flags &= ~flag;
0403         trace_hfi1_qpwakeup(qp, flag);
0404         hfi1_qp_schedule(qp);
0405     }
0406     spin_unlock_irqrestore(&qp->s_lock, flags);
0407     /* Notify hfi1_destroy_qp() if it is waiting. */
0408     rvt_put_qp(qp);
0409 }
0410 
0411 void hfi1_qp_unbusy(struct rvt_qp *qp, struct iowait_work *wait)
0412 {
0413     struct hfi1_qp_priv *priv = qp->priv;
0414 
0415     if (iowait_set_work_flag(wait) == IOWAIT_IB_SE) {
0416         qp->s_flags &= ~RVT_S_BUSY;
0417         /*
0418          * If we are sending a first-leg packet from the second leg,
0419          * we need to clear the busy flag from priv->s_flags to
0420          * avoid a race condition when the qp wakes up before
0421          * the call to hfi1_verbs_send() returns to the second
0422          * leg. In that case, the second leg will terminate without
0423          * being re-scheduled, resulting in failure to send TID RDMA
0424          * WRITE DATA and TID RDMA ACK packets.
0425          */
0426         if (priv->s_flags & HFI1_S_TID_BUSY_SET) {
0427             priv->s_flags &= ~(HFI1_S_TID_BUSY_SET |
0428                        RVT_S_BUSY);
0429             iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
0430         }
0431     } else {
0432         priv->s_flags &= ~RVT_S_BUSY;
0433     }
0434 }
0435 
0436 static int iowait_sleep(
0437     struct sdma_engine *sde,
0438     struct iowait_work *wait,
0439     struct sdma_txreq *stx,
0440     uint seq,
0441     bool pkts_sent)
0442 {
0443     struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq);
0444     struct rvt_qp *qp;
0445     struct hfi1_qp_priv *priv;
0446     unsigned long flags;
0447     int ret = 0;
0448 
0449     qp = tx->qp;
0450     priv = qp->priv;
0451 
0452     spin_lock_irqsave(&qp->s_lock, flags);
0453     if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
0454         /*
0455          * If we couldn't queue the DMA request, save the info
0456          * and try again later rather than destroying the
0457          * buffer and undoing the side effects of the copy.
0458          */
0459         /* Make a common routine? */
0460         list_add_tail(&stx->list, &wait->tx_head);
0461         write_seqlock(&sde->waitlock);
0462         if (sdma_progress(sde, seq, stx))
0463             goto eagain;
0464         if (list_empty(&priv->s_iowait.list)) {
0465             struct hfi1_ibport *ibp =
0466                 to_iport(qp->ibqp.device, qp->port_num);
0467 
0468             ibp->rvp.n_dmawait++;
0469             qp->s_flags |= RVT_S_WAIT_DMA_DESC;
0470             iowait_get_priority(&priv->s_iowait);
0471             iowait_queue(pkts_sent, &priv->s_iowait,
0472                      &sde->dmawait);
0473             priv->s_iowait.lock = &sde->waitlock;
0474             trace_hfi1_qpsleep(qp, RVT_S_WAIT_DMA_DESC);
0475             rvt_get_qp(qp);
0476         }
0477         write_sequnlock(&sde->waitlock);
0478         hfi1_qp_unbusy(qp, wait);
0479         spin_unlock_irqrestore(&qp->s_lock, flags);
0480         ret = -EBUSY;
0481     } else {
0482         spin_unlock_irqrestore(&qp->s_lock, flags);
0483         hfi1_put_txreq(tx);
0484     }
0485     return ret;
0486 eagain:
0487     write_sequnlock(&sde->waitlock);
0488     spin_unlock_irqrestore(&qp->s_lock, flags);
0489     list_del_init(&stx->list);
0490     return -EAGAIN;
0491 }
0492 
0493 static void iowait_wakeup(struct iowait *wait, int reason)
0494 {
0495     struct rvt_qp *qp = iowait_to_qp(wait);
0496 
0497     WARN_ON(reason != SDMA_AVAIL_REASON);
0498     hfi1_qp_wakeup(qp, RVT_S_WAIT_DMA_DESC);
0499 }
0500 
0501 static void iowait_sdma_drained(struct iowait *wait)
0502 {
0503     struct rvt_qp *qp = iowait_to_qp(wait);
0504     unsigned long flags;
0505 
0506     /*
0507      * This happens when the send engine notes
0508      * a QP in the error state and cannot
0509      * do the flush work until that QP's
0510      * sdma work has finished.
0511      */
0512     spin_lock_irqsave(&qp->s_lock, flags);
0513     if (qp->s_flags & RVT_S_WAIT_DMA) {
0514         qp->s_flags &= ~RVT_S_WAIT_DMA;
0515         hfi1_schedule_send(qp);
0516     }
0517     spin_unlock_irqrestore(&qp->s_lock, flags);
0518 }
0519 
0520 static void hfi1_init_priority(struct iowait *w)
0521 {
0522     struct rvt_qp *qp = iowait_to_qp(w);
0523     struct hfi1_qp_priv *priv = qp->priv;
0524 
0525     if (qp->s_flags & RVT_S_ACK_PENDING)
0526         w->priority++;
0527     if (priv->s_flags & RVT_S_ACK_PENDING)
0528         w->priority++;
0529 }
0530 
0531 /**
0532  * qp_to_sdma_engine - map a qp to a send engine
0533  * @qp: the QP
0534  * @sc5: the 5 bit sc
0535  *
0536  * Return:
0537  * A send engine for the qp or NULL for SMI type qp.
0538  */
0539 struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5)
0540 {
0541     struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
0542     struct sdma_engine *sde;
0543 
0544     if (!(dd->flags & HFI1_HAS_SEND_DMA))
0545         return NULL;
0546     switch (qp->ibqp.qp_type) {
0547     case IB_QPT_SMI:
0548         return NULL;
0549     default:
0550         break;
0551     }
0552     sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5);
0553     return sde;
0554 }
0555 
0556 /**
0557  * qp_to_send_context - map a qp to a send context
0558  * @qp: the QP
0559  * @sc5: the 5 bit sc
0560  *
0561  * Return:
0562  * A send context for the qp
0563  */
0564 struct send_context *qp_to_send_context(struct rvt_qp *qp, u8 sc5)
0565 {
0566     struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
0567 
0568     switch (qp->ibqp.qp_type) {
0569     case IB_QPT_SMI:
0570         /* SMA packets to VL15 */
0571         return dd->vld[15].sc;
0572     default:
0573         break;
0574     }
0575 
0576     return pio_select_send_context_sc(dd, qp->ibqp.qp_num >> dd->qos_shift,
0577                       sc5);
0578 }
0579 
0580 static const char * const qp_type_str[] = {
0581     "SMI", "GSI", "RC", "UC", "UD",
0582 };
0583 
0584 static int qp_idle(struct rvt_qp *qp)
0585 {
0586     return
0587         qp->s_last == qp->s_acked &&
0588         qp->s_acked == qp->s_cur &&
0589         qp->s_cur == qp->s_tail &&
0590         qp->s_tail == qp->s_head;
0591 }
0592 
0593 /**
0594  * qp_iter_print - print the qp information to seq_file
0595  * @s: the seq_file to emit the qp information on
0596  * @iter: the iterator for the qp hash list
0597  */
0598 void qp_iter_print(struct seq_file *s, struct rvt_qp_iter *iter)
0599 {
0600     struct rvt_swqe *wqe;
0601     struct rvt_qp *qp = iter->qp;
0602     struct hfi1_qp_priv *priv = qp->priv;
0603     struct sdma_engine *sde;
0604     struct send_context *send_context;
0605     struct rvt_ack_entry *e = NULL;
0606     struct rvt_srq *srq = qp->ibqp.srq ?
0607         ibsrq_to_rvtsrq(qp->ibqp.srq) : NULL;
0608 
0609     sde = qp_to_sdma_engine(qp, priv->s_sc);
0610     wqe = rvt_get_swqe_ptr(qp, qp->s_last);
0611     send_context = qp_to_send_context(qp, priv->s_sc);
0612     if (qp->s_ack_queue)
0613         e = &qp->s_ack_queue[qp->s_tail_ack_queue];
0614     seq_printf(s,
0615            "N %d %s QP %x R %u %s %u %u f=%x %u %u %u %u %u %u SPSN %x %x %x %x %x RPSN %x S(%u %u %u %u %u %u %u) R(%u %u %u) RQP %x LID %x SL %u MTU %u %u %u %u %u SDE %p,%u SC %p,%u SCQ %u %u PID %d OS %x %x E %x %x %x RNR %d %s %d\n",
0616            iter->n,
0617            qp_idle(qp) ? "I" : "B",
0618            qp->ibqp.qp_num,
0619            atomic_read(&qp->refcount),
0620            qp_type_str[qp->ibqp.qp_type],
0621            qp->state,
0622            wqe ? wqe->wr.opcode : 0,
0623            qp->s_flags,
0624            iowait_sdma_pending(&priv->s_iowait),
0625            iowait_pio_pending(&priv->s_iowait),
0626            !list_empty(&priv->s_iowait.list),
0627            qp->timeout,
0628            wqe ? wqe->ssn : 0,
0629            qp->s_lsn,
0630            qp->s_last_psn,
0631            qp->s_psn, qp->s_next_psn,
0632            qp->s_sending_psn, qp->s_sending_hpsn,
0633            qp->r_psn,
0634            qp->s_last, qp->s_acked, qp->s_cur,
0635            qp->s_tail, qp->s_head, qp->s_size,
0636            qp->s_avail,
0637            /* ack_queue ring pointers, size */
0638            qp->s_tail_ack_queue, qp->r_head_ack_queue,
0639            rvt_max_atomic(&to_idev(qp->ibqp.device)->rdi),
0640            /* remote QP info  */
0641            qp->remote_qpn,
0642            rdma_ah_get_dlid(&qp->remote_ah_attr),
0643            rdma_ah_get_sl(&qp->remote_ah_attr),
0644            qp->pmtu,
0645            qp->s_retry,
0646            qp->s_retry_cnt,
0647            qp->s_rnr_retry_cnt,
0648            qp->s_rnr_retry,
0649            sde,
0650            sde ? sde->this_idx : 0,
0651            send_context,
0652            send_context ? send_context->sw_index : 0,
0653            ib_cq_head(qp->ibqp.send_cq),
0654            ib_cq_tail(qp->ibqp.send_cq),
0655            qp->pid,
0656            qp->s_state,
0657            qp->s_ack_state,
0658            /* ack queue information */
0659            e ? e->opcode : 0,
0660            e ? e->psn : 0,
0661            e ? e->lpsn : 0,
0662            qp->r_min_rnr_timer,
0663            srq ? "SRQ" : "RQ",
0664            srq ? srq->rq.size : qp->r_rq.size
0665         );
0666 }
0667 
0668 void *qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp)
0669 {
0670     struct hfi1_qp_priv *priv;
0671 
0672     priv = kzalloc_node(sizeof(*priv), GFP_KERNEL, rdi->dparms.node);
0673     if (!priv)
0674         return ERR_PTR(-ENOMEM);
0675 
0676     priv->owner = qp;
0677 
0678     priv->s_ahg = kzalloc_node(sizeof(*priv->s_ahg), GFP_KERNEL,
0679                    rdi->dparms.node);
0680     if (!priv->s_ahg) {
0681         kfree(priv);
0682         return ERR_PTR(-ENOMEM);
0683     }
0684     iowait_init(
0685         &priv->s_iowait,
0686         1,
0687         _hfi1_do_send,
0688         _hfi1_do_tid_send,
0689         iowait_sleep,
0690         iowait_wakeup,
0691         iowait_sdma_drained,
0692         hfi1_init_priority);
0693     /* Init to a value to start the running average correctly */
0694     priv->s_running_pkt_size = piothreshold / 2;
0695     return priv;
0696 }
0697 
0698 void qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
0699 {
0700     struct hfi1_qp_priv *priv = qp->priv;
0701 
0702     hfi1_qp_priv_tid_free(rdi, qp);
0703     kfree(priv->s_ahg);
0704     kfree(priv);
0705 }
0706 
0707 unsigned free_all_qps(struct rvt_dev_info *rdi)
0708 {
0709     struct hfi1_ibdev *verbs_dev = container_of(rdi,
0710                             struct hfi1_ibdev,
0711                             rdi);
0712     struct hfi1_devdata *dd = container_of(verbs_dev,
0713                            struct hfi1_devdata,
0714                            verbs_dev);
0715     int n;
0716     unsigned qp_inuse = 0;
0717 
0718     for (n = 0; n < dd->num_pports; n++) {
0719         struct hfi1_ibport *ibp = &dd->pport[n].ibport_data;
0720 
0721         rcu_read_lock();
0722         if (rcu_dereference(ibp->rvp.qp[0]))
0723             qp_inuse++;
0724         if (rcu_dereference(ibp->rvp.qp[1]))
0725             qp_inuse++;
0726         rcu_read_unlock();
0727     }
0728 
0729     return qp_inuse;
0730 }
0731 
0732 void flush_qp_waiters(struct rvt_qp *qp)
0733 {
0734     lockdep_assert_held(&qp->s_lock);
0735     flush_iowait(qp);
0736     hfi1_tid_rdma_flush_wait(qp);
0737 }
0738 
0739 void stop_send_queue(struct rvt_qp *qp)
0740 {
0741     struct hfi1_qp_priv *priv = qp->priv;
0742 
0743     iowait_cancel_work(&priv->s_iowait);
0744     if (cancel_work_sync(&priv->tid_rdma.trigger_work))
0745         rvt_put_qp(qp);
0746 }
0747 
0748 void quiesce_qp(struct rvt_qp *qp)
0749 {
0750     struct hfi1_qp_priv *priv = qp->priv;
0751 
0752     hfi1_del_tid_reap_timer(qp);
0753     hfi1_del_tid_retry_timer(qp);
0754     iowait_sdma_drain(&priv->s_iowait);
0755     qp_pio_drain(qp);
0756     flush_tx_list(qp);
0757 }
0758 
0759 void notify_qp_reset(struct rvt_qp *qp)
0760 {
0761     hfi1_qp_kern_exp_rcv_clear_all(qp);
0762     qp->r_adefered = 0;
0763     clear_ahg(qp);
0764 
0765     /* Clear any OPFN state */
0766     if (qp->ibqp.qp_type == IB_QPT_RC)
0767         opfn_conn_error(qp);
0768 }
0769 
0770 /*
0771  * Switch to alternate path.
0772  * The QP s_lock should be held and interrupts disabled.
0773  */
0774 void hfi1_migrate_qp(struct rvt_qp *qp)
0775 {
0776     struct hfi1_qp_priv *priv = qp->priv;
0777     struct ib_event ev;
0778 
0779     qp->s_mig_state = IB_MIG_MIGRATED;
0780     qp->remote_ah_attr = qp->alt_ah_attr;
0781     qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
0782     qp->s_pkey_index = qp->s_alt_pkey_index;
0783     qp->s_flags |= HFI1_S_AHG_CLEAR;
0784     priv->s_sc = ah_to_sc(qp->ibqp.device, &qp->remote_ah_attr);
0785     priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
0786     qp_set_16b(qp);
0787 
0788     ev.device = qp->ibqp.device;
0789     ev.element.qp = &qp->ibqp;
0790     ev.event = IB_EVENT_PATH_MIG;
0791     qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
0792 }
0793 
0794 int mtu_to_path_mtu(u32 mtu)
0795 {
0796     return mtu_to_enum(mtu, OPA_MTU_8192);
0797 }
0798 
0799 u32 mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
0800 {
0801     u32 mtu;
0802     struct hfi1_ibdev *verbs_dev = container_of(rdi,
0803                             struct hfi1_ibdev,
0804                             rdi);
0805     struct hfi1_devdata *dd = container_of(verbs_dev,
0806                            struct hfi1_devdata,
0807                            verbs_dev);
0808     struct hfi1_ibport *ibp;
0809     u8 sc, vl;
0810 
0811     ibp = &dd->pport[qp->port_num - 1].ibport_data;
0812     sc = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
0813     vl = sc_to_vlt(dd, sc);
0814 
0815     mtu = verbs_mtu_enum_to_int(qp->ibqp.device, pmtu);
0816     if (vl < PER_VL_SEND_CONTEXTS)
0817         mtu = min_t(u32, mtu, dd->vld[vl].mtu);
0818     return mtu;
0819 }
0820 
0821 int get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
0822                struct ib_qp_attr *attr)
0823 {
0824     int mtu, pidx = qp->port_num - 1;
0825     struct hfi1_ibdev *verbs_dev = container_of(rdi,
0826                             struct hfi1_ibdev,
0827                             rdi);
0828     struct hfi1_devdata *dd = container_of(verbs_dev,
0829                            struct hfi1_devdata,
0830                            verbs_dev);
0831     mtu = verbs_mtu_enum_to_int(qp->ibqp.device, attr->path_mtu);
0832     if (mtu == -1)
0833         return -1; /* values less than 0 are error */
0834 
0835     if (mtu > dd->pport[pidx].ibmtu)
0836         return mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048);
0837     else
0838         return attr->path_mtu;
0839 }
0840 
0841 void notify_error_qp(struct rvt_qp *qp)
0842 {
0843     struct hfi1_qp_priv *priv = qp->priv;
0844     seqlock_t *lock = priv->s_iowait.lock;
0845 
0846     if (lock) {
0847         write_seqlock(lock);
0848         if (!list_empty(&priv->s_iowait.list) &&
0849             !(qp->s_flags & RVT_S_BUSY) &&
0850             !(priv->s_flags & RVT_S_BUSY)) {
0851             qp->s_flags &= ~HFI1_S_ANY_WAIT_IO;
0852             iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
0853             iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
0854             list_del_init(&priv->s_iowait.list);
0855             priv->s_iowait.lock = NULL;
0856             rvt_put_qp(qp);
0857         }
0858         write_sequnlock(lock);
0859     }
0860 
0861     if (!(qp->s_flags & RVT_S_BUSY) && !(priv->s_flags & RVT_S_BUSY)) {
0862         qp->s_hdrwords = 0;
0863         if (qp->s_rdma_mr) {
0864             rvt_put_mr(qp->s_rdma_mr);
0865             qp->s_rdma_mr = NULL;
0866         }
0867         flush_tx_list(qp);
0868     }
0869 }
0870 
0871 /**
0872  * hfi1_qp_iter_cb - callback for iterator
0873  * @qp: the qp
0874  * @v: the sl in low bits of v
0875  *
0876  * This is called from the iterator callback to work
0877  * on an individual qp.
0878  */
0879 static void hfi1_qp_iter_cb(struct rvt_qp *qp, u64 v)
0880 {
0881     int lastwqe;
0882     struct ib_event ev;
0883     struct hfi1_ibport *ibp =
0884         to_iport(qp->ibqp.device, qp->port_num);
0885     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0886     u8 sl = (u8)v;
0887 
0888     if (qp->port_num != ppd->port ||
0889         (qp->ibqp.qp_type != IB_QPT_UC &&
0890          qp->ibqp.qp_type != IB_QPT_RC) ||
0891         rdma_ah_get_sl(&qp->remote_ah_attr) != sl ||
0892         !(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))
0893         return;
0894 
0895     spin_lock_irq(&qp->r_lock);
0896     spin_lock(&qp->s_hlock);
0897     spin_lock(&qp->s_lock);
0898     lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
0899     spin_unlock(&qp->s_lock);
0900     spin_unlock(&qp->s_hlock);
0901     spin_unlock_irq(&qp->r_lock);
0902     if (lastwqe) {
0903         ev.device = qp->ibqp.device;
0904         ev.element.qp = &qp->ibqp;
0905         ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
0906         qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
0907     }
0908 }
0909 
0910 /**
0911  * hfi1_error_port_qps - put a port's RC/UC qps into error state
0912  * @ibp: the ibport.
0913  * @sl: the service level.
0914  *
0915  * This function places all RC/UC qps with a given service level into error
0916  * state. It is generally called to force upper lay apps to abandon stale qps
0917  * after an sl->sc mapping change.
0918  */
0919 void hfi1_error_port_qps(struct hfi1_ibport *ibp, u8 sl)
0920 {
0921     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0922     struct hfi1_ibdev *dev = &ppd->dd->verbs_dev;
0923 
0924     rvt_qp_iter(&dev->rdi, sl, hfi1_qp_iter_cb);
0925 }