Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
0002 /*
0003  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
0004  * Copyright 2016-2019 NXP
0005  *
0006  */
0007 
0008 #include <asm/cacheflush.h>
0009 #include <linux/io.h>
0010 #include <linux/slab.h>
0011 #include <linux/spinlock.h>
0012 #include <soc/fsl/dpaa2-global.h>
0013 
0014 #include "qbman-portal.h"
0015 
0016 /* All QBMan command and result structures use this "valid bit" encoding */
0017 #define QB_VALID_BIT ((u32)0x80)
0018 
0019 /* QBMan portal management command codes */
0020 #define QBMAN_MC_ACQUIRE       0x30
0021 #define QBMAN_WQCHAN_CONFIGURE 0x46
0022 
0023 /* CINH register offsets */
0024 #define QBMAN_CINH_SWP_EQCR_PI      0x800
0025 #define QBMAN_CINH_SWP_EQCR_CI      0x840
0026 #define QBMAN_CINH_SWP_EQAR    0x8c0
0027 #define QBMAN_CINH_SWP_CR_RT        0x900
0028 #define QBMAN_CINH_SWP_VDQCR_RT     0x940
0029 #define QBMAN_CINH_SWP_EQCR_AM_RT   0x980
0030 #define QBMAN_CINH_SWP_RCR_AM_RT    0x9c0
0031 #define QBMAN_CINH_SWP_DQPI    0xa00
0032 #define QBMAN_CINH_SWP_DQRR_ITR     0xa80
0033 #define QBMAN_CINH_SWP_DCAP    0xac0
0034 #define QBMAN_CINH_SWP_SDQCR   0xb00
0035 #define QBMAN_CINH_SWP_EQCR_AM_RT2  0xb40
0036 #define QBMAN_CINH_SWP_RCR_PI       0xc00
0037 #define QBMAN_CINH_SWP_RAR     0xcc0
0038 #define QBMAN_CINH_SWP_ISR     0xe00
0039 #define QBMAN_CINH_SWP_IER     0xe40
0040 #define QBMAN_CINH_SWP_ISDR    0xe80
0041 #define QBMAN_CINH_SWP_IIR     0xec0
0042 #define QBMAN_CINH_SWP_ITPR    0xf40
0043 
0044 /* CENA register offsets */
0045 #define QBMAN_CENA_SWP_EQCR(n) (0x000 + ((u32)(n) << 6))
0046 #define QBMAN_CENA_SWP_DQRR(n) (0x200 + ((u32)(n) << 6))
0047 #define QBMAN_CENA_SWP_RCR(n)  (0x400 + ((u32)(n) << 6))
0048 #define QBMAN_CENA_SWP_CR      0x600
0049 #define QBMAN_CENA_SWP_RR(vb)  (0x700 + ((u32)(vb) >> 1))
0050 #define QBMAN_CENA_SWP_VDQCR   0x780
0051 #define QBMAN_CENA_SWP_EQCR_CI 0x840
0052 #define QBMAN_CENA_SWP_EQCR_CI_MEMBACK 0x1840
0053 
0054 /* CENA register offsets in memory-backed mode */
0055 #define QBMAN_CENA_SWP_DQRR_MEM(n)  (0x800 + ((u32)(n) << 6))
0056 #define QBMAN_CENA_SWP_RCR_MEM(n)   (0x1400 + ((u32)(n) << 6))
0057 #define QBMAN_CENA_SWP_CR_MEM       0x1600
0058 #define QBMAN_CENA_SWP_RR_MEM       0x1680
0059 #define QBMAN_CENA_SWP_VDQCR_MEM    0x1780
0060 
0061 /* Reverse mapping of QBMAN_CENA_SWP_DQRR() */
0062 #define QBMAN_IDX_FROM_DQRR(p) (((unsigned long)(p) & 0x1ff) >> 6)
0063 
0064 /* Define token used to determine if response written to memory is valid */
0065 #define QMAN_DQ_TOKEN_VALID 1
0066 
0067 /* SDQCR attribute codes */
0068 #define QB_SDQCR_FC_SHIFT   29
0069 #define QB_SDQCR_FC_MASK    0x1
0070 #define QB_SDQCR_DCT_SHIFT  24
0071 #define QB_SDQCR_DCT_MASK   0x3
0072 #define QB_SDQCR_TOK_SHIFT  16
0073 #define QB_SDQCR_TOK_MASK   0xff
0074 #define QB_SDQCR_SRC_SHIFT  0
0075 #define QB_SDQCR_SRC_MASK   0xffff
0076 
0077 /* opaque token for static dequeues */
0078 #define QMAN_SDQCR_TOKEN    0xbb
0079 
0080 #define QBMAN_EQCR_DCA_IDXMASK          0x0f
0081 #define QBMAN_ENQUEUE_FLAG_DCA          (1ULL << 31)
0082 
0083 #define EQ_DESC_SIZE_WITHOUT_FD 29
0084 #define EQ_DESC_SIZE_FD_START 32
0085 
0086 enum qbman_sdqcr_dct {
0087     qbman_sdqcr_dct_null = 0,
0088     qbman_sdqcr_dct_prio_ics,
0089     qbman_sdqcr_dct_active_ics,
0090     qbman_sdqcr_dct_active
0091 };
0092 
0093 enum qbman_sdqcr_fc {
0094     qbman_sdqcr_fc_one = 0,
0095     qbman_sdqcr_fc_up_to_3 = 1
0096 };
0097 
0098 /* Internal Function declaration */
0099 static int qbman_swp_enqueue_direct(struct qbman_swp *s,
0100                     const struct qbman_eq_desc *d,
0101                     const struct dpaa2_fd *fd);
0102 static int qbman_swp_enqueue_mem_back(struct qbman_swp *s,
0103                       const struct qbman_eq_desc *d,
0104                       const struct dpaa2_fd *fd);
0105 static int qbman_swp_enqueue_multiple_direct(struct qbman_swp *s,
0106                          const struct qbman_eq_desc *d,
0107                          const struct dpaa2_fd *fd,
0108                          uint32_t *flags,
0109                          int num_frames);
0110 static int qbman_swp_enqueue_multiple_mem_back(struct qbman_swp *s,
0111                            const struct qbman_eq_desc *d,
0112                            const struct dpaa2_fd *fd,
0113                            uint32_t *flags,
0114                            int num_frames);
0115 static int
0116 qbman_swp_enqueue_multiple_desc_direct(struct qbman_swp *s,
0117                        const struct qbman_eq_desc *d,
0118                        const struct dpaa2_fd *fd,
0119                        int num_frames);
0120 static
0121 int qbman_swp_enqueue_multiple_desc_mem_back(struct qbman_swp *s,
0122                          const struct qbman_eq_desc *d,
0123                          const struct dpaa2_fd *fd,
0124                          int num_frames);
0125 static int qbman_swp_pull_direct(struct qbman_swp *s,
0126                  struct qbman_pull_desc *d);
0127 static int qbman_swp_pull_mem_back(struct qbman_swp *s,
0128                    struct qbman_pull_desc *d);
0129 
0130 const struct dpaa2_dq *qbman_swp_dqrr_next_direct(struct qbman_swp *s);
0131 const struct dpaa2_dq *qbman_swp_dqrr_next_mem_back(struct qbman_swp *s);
0132 
0133 static int qbman_swp_release_direct(struct qbman_swp *s,
0134                     const struct qbman_release_desc *d,
0135                     const u64 *buffers,
0136                     unsigned int num_buffers);
0137 static int qbman_swp_release_mem_back(struct qbman_swp *s,
0138                       const struct qbman_release_desc *d,
0139                       const u64 *buffers,
0140                       unsigned int num_buffers);
0141 
0142 /* Function pointers */
0143 int (*qbman_swp_enqueue_ptr)(struct qbman_swp *s,
0144                  const struct qbman_eq_desc *d,
0145                  const struct dpaa2_fd *fd)
0146     = qbman_swp_enqueue_direct;
0147 
0148 int (*qbman_swp_enqueue_multiple_ptr)(struct qbman_swp *s,
0149                       const struct qbman_eq_desc *d,
0150                       const struct dpaa2_fd *fd,
0151                       uint32_t *flags,
0152                          int num_frames)
0153     = qbman_swp_enqueue_multiple_direct;
0154 
0155 int
0156 (*qbman_swp_enqueue_multiple_desc_ptr)(struct qbman_swp *s,
0157                        const struct qbman_eq_desc *d,
0158                        const struct dpaa2_fd *fd,
0159                        int num_frames)
0160     = qbman_swp_enqueue_multiple_desc_direct;
0161 
0162 int (*qbman_swp_pull_ptr)(struct qbman_swp *s, struct qbman_pull_desc *d)
0163             = qbman_swp_pull_direct;
0164 
0165 const struct dpaa2_dq *(*qbman_swp_dqrr_next_ptr)(struct qbman_swp *s)
0166             = qbman_swp_dqrr_next_direct;
0167 
0168 int (*qbman_swp_release_ptr)(struct qbman_swp *s,
0169                  const struct qbman_release_desc *d,
0170                  const u64 *buffers,
0171                  unsigned int num_buffers)
0172             = qbman_swp_release_direct;
0173 
0174 /* Portal Access */
0175 
0176 static inline u32 qbman_read_register(struct qbman_swp *p, u32 offset)
0177 {
0178     return readl_relaxed(p->addr_cinh + offset);
0179 }
0180 
0181 static inline void qbman_write_register(struct qbman_swp *p, u32 offset,
0182                     u32 value)
0183 {
0184     writel_relaxed(value, p->addr_cinh + offset);
0185 }
0186 
0187 static inline void *qbman_get_cmd(struct qbman_swp *p, u32 offset)
0188 {
0189     return p->addr_cena + offset;
0190 }
0191 
0192 #define QBMAN_CINH_SWP_CFG   0xd00
0193 
0194 #define SWP_CFG_DQRR_MF_SHIFT 20
0195 #define SWP_CFG_EST_SHIFT     16
0196 #define SWP_CFG_CPBS_SHIFT    15
0197 #define SWP_CFG_WN_SHIFT      14
0198 #define SWP_CFG_RPM_SHIFT     12
0199 #define SWP_CFG_DCM_SHIFT     10
0200 #define SWP_CFG_EPM_SHIFT     8
0201 #define SWP_CFG_VPM_SHIFT     7
0202 #define SWP_CFG_CPM_SHIFT     6
0203 #define SWP_CFG_SD_SHIFT      5
0204 #define SWP_CFG_SP_SHIFT      4
0205 #define SWP_CFG_SE_SHIFT      3
0206 #define SWP_CFG_DP_SHIFT      2
0207 #define SWP_CFG_DE_SHIFT      1
0208 #define SWP_CFG_EP_SHIFT      0
0209 
0210 static inline u32 qbman_set_swp_cfg(u8 max_fill, u8 wn, u8 est, u8 rpm, u8 dcm,
0211                     u8 epm, int sd, int sp, int se,
0212                     int dp, int de, int ep)
0213 {
0214     return (max_fill << SWP_CFG_DQRR_MF_SHIFT |
0215         est << SWP_CFG_EST_SHIFT |
0216         wn << SWP_CFG_WN_SHIFT |
0217         rpm << SWP_CFG_RPM_SHIFT |
0218         dcm << SWP_CFG_DCM_SHIFT |
0219         epm << SWP_CFG_EPM_SHIFT |
0220         sd << SWP_CFG_SD_SHIFT |
0221         sp << SWP_CFG_SP_SHIFT |
0222         se << SWP_CFG_SE_SHIFT |
0223         dp << SWP_CFG_DP_SHIFT |
0224         de << SWP_CFG_DE_SHIFT |
0225         ep << SWP_CFG_EP_SHIFT);
0226 }
0227 
0228 #define QMAN_RT_MODE       0x00000100
0229 
0230 static inline u8 qm_cyc_diff(u8 ringsize, u8 first, u8 last)
0231 {
0232     /* 'first' is included, 'last' is excluded */
0233     if (first <= last)
0234         return last - first;
0235     else
0236         return (2 * ringsize) - (first - last);
0237 }
0238 
0239 /**
0240  * qbman_swp_init() - Create a functional object representing the given
0241  *                    QBMan portal descriptor.
0242  * @d: the given qbman swp descriptor
0243  *
0244  * Return qbman_swp portal for success, NULL if the object cannot
0245  * be created.
0246  */
0247 struct qbman_swp *qbman_swp_init(const struct qbman_swp_desc *d)
0248 {
0249     struct qbman_swp *p = kzalloc(sizeof(*p), GFP_KERNEL);
0250     u32 reg;
0251     u32 mask_size;
0252     u32 eqcr_pi;
0253 
0254     if (!p)
0255         return NULL;
0256 
0257     spin_lock_init(&p->access_spinlock);
0258 
0259     p->desc = d;
0260     p->mc.valid_bit = QB_VALID_BIT;
0261     p->sdq = 0;
0262     p->sdq |= qbman_sdqcr_dct_prio_ics << QB_SDQCR_DCT_SHIFT;
0263     p->sdq |= qbman_sdqcr_fc_up_to_3 << QB_SDQCR_FC_SHIFT;
0264     p->sdq |= QMAN_SDQCR_TOKEN << QB_SDQCR_TOK_SHIFT;
0265     if ((p->desc->qman_version & QMAN_REV_MASK) >= QMAN_REV_5000)
0266         p->mr.valid_bit = QB_VALID_BIT;
0267 
0268     atomic_set(&p->vdq.available, 1);
0269     p->vdq.valid_bit = QB_VALID_BIT;
0270     p->dqrr.next_idx = 0;
0271     p->dqrr.valid_bit = QB_VALID_BIT;
0272 
0273     if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_4100) {
0274         p->dqrr.dqrr_size = 4;
0275         p->dqrr.reset_bug = 1;
0276     } else {
0277         p->dqrr.dqrr_size = 8;
0278         p->dqrr.reset_bug = 0;
0279     }
0280 
0281     p->addr_cena = d->cena_bar;
0282     p->addr_cinh = d->cinh_bar;
0283 
0284     if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_5000) {
0285 
0286         reg = qbman_set_swp_cfg(p->dqrr.dqrr_size,
0287             1, /* Writes Non-cacheable */
0288             0, /* EQCR_CI stashing threshold */
0289             3, /* RPM: RCR in array mode */
0290             2, /* DCM: Discrete consumption ack */
0291             2, /* EPM: EQCR in ring mode */
0292             1, /* mem stashing drop enable enable */
0293             1, /* mem stashing priority enable */
0294             1, /* mem stashing enable */
0295             1, /* dequeue stashing priority enable */
0296             0, /* dequeue stashing enable enable */
0297             0); /* EQCR_CI stashing priority enable */
0298     } else {
0299         memset(p->addr_cena, 0, 64 * 1024);
0300         reg = qbman_set_swp_cfg(p->dqrr.dqrr_size,
0301             1, /* Writes Non-cacheable */
0302             1, /* EQCR_CI stashing threshold */
0303             3, /* RPM: RCR in array mode */
0304             2, /* DCM: Discrete consumption ack */
0305             0, /* EPM: EQCR in ring mode */
0306             1, /* mem stashing drop enable */
0307             1, /* mem stashing priority enable */
0308             1, /* mem stashing enable */
0309             1, /* dequeue stashing priority enable */
0310             0, /* dequeue stashing enable */
0311             0); /* EQCR_CI stashing priority enable */
0312         reg |= 1 << SWP_CFG_CPBS_SHIFT | /* memory-backed mode */
0313                1 << SWP_CFG_VPM_SHIFT |  /* VDQCR read triggered mode */
0314                1 << SWP_CFG_CPM_SHIFT;   /* CR read triggered mode */
0315     }
0316 
0317     qbman_write_register(p, QBMAN_CINH_SWP_CFG, reg);
0318     reg = qbman_read_register(p, QBMAN_CINH_SWP_CFG);
0319     if (!reg) {
0320         pr_err("qbman: the portal is not enabled!\n");
0321         kfree(p);
0322         return NULL;
0323     }
0324 
0325     if ((p->desc->qman_version & QMAN_REV_MASK) >= QMAN_REV_5000) {
0326         qbman_write_register(p, QBMAN_CINH_SWP_EQCR_PI, QMAN_RT_MODE);
0327         qbman_write_register(p, QBMAN_CINH_SWP_RCR_PI, QMAN_RT_MODE);
0328     }
0329     /*
0330      * SDQCR needs to be initialized to 0 when no channels are
0331      * being dequeued from or else the QMan HW will indicate an
0332      * error.  The values that were calculated above will be
0333      * applied when dequeues from a specific channel are enabled.
0334      */
0335     qbman_write_register(p, QBMAN_CINH_SWP_SDQCR, 0);
0336 
0337     p->eqcr.pi_ring_size = 8;
0338     if ((p->desc->qman_version & QMAN_REV_MASK) >= QMAN_REV_5000) {
0339         p->eqcr.pi_ring_size = 32;
0340         qbman_swp_enqueue_ptr =
0341             qbman_swp_enqueue_mem_back;
0342         qbman_swp_enqueue_multiple_ptr =
0343             qbman_swp_enqueue_multiple_mem_back;
0344         qbman_swp_enqueue_multiple_desc_ptr =
0345             qbman_swp_enqueue_multiple_desc_mem_back;
0346         qbman_swp_pull_ptr = qbman_swp_pull_mem_back;
0347         qbman_swp_dqrr_next_ptr = qbman_swp_dqrr_next_mem_back;
0348         qbman_swp_release_ptr = qbman_swp_release_mem_back;
0349     }
0350 
0351     for (mask_size = p->eqcr.pi_ring_size; mask_size > 0; mask_size >>= 1)
0352         p->eqcr.pi_ci_mask = (p->eqcr.pi_ci_mask << 1) + 1;
0353     eqcr_pi = qbman_read_register(p, QBMAN_CINH_SWP_EQCR_PI);
0354     p->eqcr.pi = eqcr_pi & p->eqcr.pi_ci_mask;
0355     p->eqcr.pi_vb = eqcr_pi & QB_VALID_BIT;
0356     p->eqcr.ci = qbman_read_register(p, QBMAN_CINH_SWP_EQCR_CI)
0357             & p->eqcr.pi_ci_mask;
0358     p->eqcr.available = p->eqcr.pi_ring_size;
0359 
0360     /* Initialize the software portal with a irq timeout period of 0us */
0361     qbman_swp_set_irq_coalescing(p, p->dqrr.dqrr_size - 1, 0);
0362 
0363     return p;
0364 }
0365 
0366 /**
0367  * qbman_swp_finish() - Create and destroy a functional object representing
0368  *                      the given QBMan portal descriptor.
0369  * @p: the qbman_swp object to be destroyed
0370  */
0371 void qbman_swp_finish(struct qbman_swp *p)
0372 {
0373     kfree(p);
0374 }
0375 
0376 /**
0377  * qbman_swp_interrupt_read_status()
0378  * @p: the given software portal
0379  *
0380  * Return the value in the SWP_ISR register.
0381  */
0382 u32 qbman_swp_interrupt_read_status(struct qbman_swp *p)
0383 {
0384     return qbman_read_register(p, QBMAN_CINH_SWP_ISR);
0385 }
0386 
0387 /**
0388  * qbman_swp_interrupt_clear_status()
0389  * @p: the given software portal
0390  * @mask: The mask to clear in SWP_ISR register
0391  */
0392 void qbman_swp_interrupt_clear_status(struct qbman_swp *p, u32 mask)
0393 {
0394     qbman_write_register(p, QBMAN_CINH_SWP_ISR, mask);
0395 }
0396 
0397 /**
0398  * qbman_swp_interrupt_get_trigger() - read interrupt enable register
0399  * @p: the given software portal
0400  *
0401  * Return the value in the SWP_IER register.
0402  */
0403 u32 qbman_swp_interrupt_get_trigger(struct qbman_swp *p)
0404 {
0405     return qbman_read_register(p, QBMAN_CINH_SWP_IER);
0406 }
0407 
0408 /**
0409  * qbman_swp_interrupt_set_trigger() - enable interrupts for a swp
0410  * @p: the given software portal
0411  * @mask: The mask of bits to enable in SWP_IER
0412  */
0413 void qbman_swp_interrupt_set_trigger(struct qbman_swp *p, u32 mask)
0414 {
0415     qbman_write_register(p, QBMAN_CINH_SWP_IER, mask);
0416 }
0417 
0418 /**
0419  * qbman_swp_interrupt_get_inhibit() - read interrupt mask register
0420  * @p: the given software portal object
0421  *
0422  * Return the value in the SWP_IIR register.
0423  */
0424 int qbman_swp_interrupt_get_inhibit(struct qbman_swp *p)
0425 {
0426     return qbman_read_register(p, QBMAN_CINH_SWP_IIR);
0427 }
0428 
0429 /**
0430  * qbman_swp_interrupt_set_inhibit() - write interrupt mask register
0431  * @p: the given software portal object
0432  * @inhibit: whether to inhibit the IRQs
0433  */
0434 void qbman_swp_interrupt_set_inhibit(struct qbman_swp *p, int inhibit)
0435 {
0436     qbman_write_register(p, QBMAN_CINH_SWP_IIR, inhibit ? 0xffffffff : 0);
0437 }
0438 
0439 /*
0440  * Different management commands all use this common base layer of code to issue
0441  * commands and poll for results.
0442  */
0443 
0444 /*
0445  * Returns a pointer to where the caller should fill in their management command
0446  * (caller should ignore the verb byte)
0447  */
0448 void *qbman_swp_mc_start(struct qbman_swp *p)
0449 {
0450     if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_5000)
0451         return qbman_get_cmd(p, QBMAN_CENA_SWP_CR);
0452     else
0453         return qbman_get_cmd(p, QBMAN_CENA_SWP_CR_MEM);
0454 }
0455 
0456 /*
0457  * Commits merges in the caller-supplied command verb (which should not include
0458  * the valid-bit) and submits the command to hardware
0459  */
0460 void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, u8 cmd_verb)
0461 {
0462     u8 *v = cmd;
0463 
0464     if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_5000) {
0465         dma_wmb();
0466         *v = cmd_verb | p->mc.valid_bit;
0467     } else {
0468         *v = cmd_verb | p->mc.valid_bit;
0469         dma_wmb();
0470         qbman_write_register(p, QBMAN_CINH_SWP_CR_RT, QMAN_RT_MODE);
0471     }
0472 }
0473 
0474 /*
0475  * Checks for a completed response (returns non-NULL if only if the response
0476  * is complete).
0477  */
0478 void *qbman_swp_mc_result(struct qbman_swp *p)
0479 {
0480     u32 *ret, verb;
0481 
0482     if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_5000) {
0483         ret = qbman_get_cmd(p, QBMAN_CENA_SWP_RR(p->mc.valid_bit));
0484         /* Remove the valid-bit - command completed if the rest
0485          * is non-zero.
0486          */
0487         verb = ret[0] & ~QB_VALID_BIT;
0488         if (!verb)
0489             return NULL;
0490         p->mc.valid_bit ^= QB_VALID_BIT;
0491     } else {
0492         ret = qbman_get_cmd(p, QBMAN_CENA_SWP_RR_MEM);
0493         /* Command completed if the valid bit is toggled */
0494         if (p->mr.valid_bit != (ret[0] & QB_VALID_BIT))
0495             return NULL;
0496         /* Command completed if the rest is non-zero */
0497         verb = ret[0] & ~QB_VALID_BIT;
0498         if (!verb)
0499             return NULL;
0500         p->mr.valid_bit ^= QB_VALID_BIT;
0501     }
0502 
0503     return ret;
0504 }
0505 
0506 #define QB_ENQUEUE_CMD_OPTIONS_SHIFT    0
0507 enum qb_enqueue_commands {
0508     enqueue_empty = 0,
0509     enqueue_response_always = 1,
0510     enqueue_rejects_to_fq = 2
0511 };
0512 
0513 #define QB_ENQUEUE_CMD_ORP_ENABLE_SHIFT      2
0514 #define QB_ENQUEUE_CMD_IRQ_ON_DISPATCH_SHIFT 3
0515 #define QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT     4
0516 #define QB_ENQUEUE_CMD_DCA_EN_SHIFT          7
0517 
0518 /*
0519  * qbman_eq_desc_clear() - Clear the contents of a descriptor to
0520  *                         default/starting state.
0521  */
0522 void qbman_eq_desc_clear(struct qbman_eq_desc *d)
0523 {
0524     memset(d, 0, sizeof(*d));
0525 }
0526 
0527 /**
0528  * qbman_eq_desc_set_no_orp() - Set enqueue descriptor without orp
0529  * @d:                the enqueue descriptor.
0530  * @respond_success:  1 = enqueue with response always; 0 = enqueue with
0531  *                    rejections returned on a FQ.
0532  */
0533 void qbman_eq_desc_set_no_orp(struct qbman_eq_desc *d, int respond_success)
0534 {
0535     d->verb &= ~(1 << QB_ENQUEUE_CMD_ORP_ENABLE_SHIFT);
0536     if (respond_success)
0537         d->verb |= enqueue_response_always;
0538     else
0539         d->verb |= enqueue_rejects_to_fq;
0540 }
0541 
0542 /*
0543  * Exactly one of the following descriptor "targets" should be set. (Calling any
0544  * one of these will replace the effect of any prior call to one of these.)
0545  *   -enqueue to a frame queue
0546  *   -enqueue to a queuing destination
0547  */
0548 
0549 /**
0550  * qbman_eq_desc_set_fq() - set the FQ for the enqueue command
0551  * @d:    the enqueue descriptor
0552  * @fqid: the id of the frame queue to be enqueued
0553  */
0554 void qbman_eq_desc_set_fq(struct qbman_eq_desc *d, u32 fqid)
0555 {
0556     d->verb &= ~(1 << QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT);
0557     d->tgtid = cpu_to_le32(fqid);
0558 }
0559 
0560 /**
0561  * qbman_eq_desc_set_qd() - Set Queuing Destination for the enqueue command
0562  * @d:       the enqueue descriptor
0563  * @qdid:    the id of the queuing destination to be enqueued
0564  * @qd_bin:  the queuing destination bin
0565  * @qd_prio: the queuing destination priority
0566  */
0567 void qbman_eq_desc_set_qd(struct qbman_eq_desc *d, u32 qdid,
0568               u32 qd_bin, u32 qd_prio)
0569 {
0570     d->verb |= 1 << QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT;
0571     d->tgtid = cpu_to_le32(qdid);
0572     d->qdbin = cpu_to_le16(qd_bin);
0573     d->qpri = qd_prio;
0574 }
0575 
0576 #define EQAR_IDX(eqar)     ((eqar) & 0x7)
0577 #define EQAR_VB(eqar)      ((eqar) & 0x80)
0578 #define EQAR_SUCCESS(eqar) ((eqar) & 0x100)
0579 
0580 #define QB_RT_BIT ((u32)0x100)
0581 /**
0582  * qbman_swp_enqueue_direct() - Issue an enqueue command
0583  * @s:  the software portal used for enqueue
0584  * @d:  the enqueue descriptor
0585  * @fd: the frame descriptor to be enqueued
0586  *
0587  * Please note that 'fd' should only be NULL if the "action" of the
0588  * descriptor is "orp_hole" or "orp_nesn".
0589  *
0590  * Return 0 for successful enqueue, -EBUSY if the EQCR is not ready.
0591  */
0592 static
0593 int qbman_swp_enqueue_direct(struct qbman_swp *s,
0594                  const struct qbman_eq_desc *d,
0595                  const struct dpaa2_fd *fd)
0596 {
0597     int flags = 0;
0598     int ret = qbman_swp_enqueue_multiple_direct(s, d, fd, &flags, 1);
0599 
0600     if (ret >= 0)
0601         ret = 0;
0602     else
0603         ret = -EBUSY;
0604     return  ret;
0605 }
0606 
0607 /**
0608  * qbman_swp_enqueue_mem_back() - Issue an enqueue command
0609  * @s:  the software portal used for enqueue
0610  * @d:  the enqueue descriptor
0611  * @fd: the frame descriptor to be enqueued
0612  *
0613  * Please note that 'fd' should only be NULL if the "action" of the
0614  * descriptor is "orp_hole" or "orp_nesn".
0615  *
0616  * Return 0 for successful enqueue, -EBUSY if the EQCR is not ready.
0617  */
0618 static
0619 int qbman_swp_enqueue_mem_back(struct qbman_swp *s,
0620                    const struct qbman_eq_desc *d,
0621                    const struct dpaa2_fd *fd)
0622 {
0623     int flags = 0;
0624     int ret = qbman_swp_enqueue_multiple_mem_back(s, d, fd, &flags, 1);
0625 
0626     if (ret >= 0)
0627         ret = 0;
0628     else
0629         ret = -EBUSY;
0630     return  ret;
0631 }
0632 
0633 /**
0634  * qbman_swp_enqueue_multiple_direct() - Issue a multi enqueue command
0635  * using one enqueue descriptor
0636  * @s:  the software portal used for enqueue
0637  * @d:  the enqueue descriptor
0638  * @fd: table pointer of frame descriptor table to be enqueued
0639  * @flags: table pointer of QBMAN_ENQUEUE_FLAG_DCA flags, not used if NULL
0640  * @num_frames: number of fd to be enqueued
0641  *
0642  * Return the number of fd enqueued, or a negative error number.
0643  */
0644 static
0645 int qbman_swp_enqueue_multiple_direct(struct qbman_swp *s,
0646                       const struct qbman_eq_desc *d,
0647                       const struct dpaa2_fd *fd,
0648                       uint32_t *flags,
0649                       int num_frames)
0650 {
0651     uint32_t *p = NULL;
0652     const uint32_t *cl = (uint32_t *)d;
0653     uint32_t eqcr_ci, eqcr_pi, half_mask, full_mask;
0654     int i, num_enqueued = 0;
0655 
0656     spin_lock(&s->access_spinlock);
0657     half_mask = (s->eqcr.pi_ci_mask>>1);
0658     full_mask = s->eqcr.pi_ci_mask;
0659 
0660     if (!s->eqcr.available) {
0661         eqcr_ci = s->eqcr.ci;
0662         p = s->addr_cena + QBMAN_CENA_SWP_EQCR_CI;
0663         s->eqcr.ci = qbman_read_register(s, QBMAN_CINH_SWP_EQCR_CI);
0664         s->eqcr.ci &= full_mask;
0665 
0666         s->eqcr.available = qm_cyc_diff(s->eqcr.pi_ring_size,
0667                     eqcr_ci, s->eqcr.ci);
0668         if (!s->eqcr.available) {
0669             spin_unlock(&s->access_spinlock);
0670             return 0;
0671         }
0672     }
0673 
0674     eqcr_pi = s->eqcr.pi;
0675     num_enqueued = (s->eqcr.available < num_frames) ?
0676             s->eqcr.available : num_frames;
0677     s->eqcr.available -= num_enqueued;
0678     /* Fill in the EQCR ring */
0679     for (i = 0; i < num_enqueued; i++) {
0680         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0681         /* Skip copying the verb */
0682         memcpy(&p[1], &cl[1], EQ_DESC_SIZE_WITHOUT_FD - 1);
0683         memcpy(&p[EQ_DESC_SIZE_FD_START/sizeof(uint32_t)],
0684                &fd[i], sizeof(*fd));
0685         eqcr_pi++;
0686     }
0687 
0688     dma_wmb();
0689 
0690     /* Set the verb byte, have to substitute in the valid-bit */
0691     eqcr_pi = s->eqcr.pi;
0692     for (i = 0; i < num_enqueued; i++) {
0693         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0694         p[0] = cl[0] | s->eqcr.pi_vb;
0695         if (flags && (flags[i] & QBMAN_ENQUEUE_FLAG_DCA)) {
0696             struct qbman_eq_desc *eq_desc = (struct qbman_eq_desc *)p;
0697 
0698             eq_desc->dca = (1 << QB_ENQUEUE_CMD_DCA_EN_SHIFT) |
0699                 ((flags[i]) & QBMAN_EQCR_DCA_IDXMASK);
0700         }
0701         eqcr_pi++;
0702         if (!(eqcr_pi & half_mask))
0703             s->eqcr.pi_vb ^= QB_VALID_BIT;
0704     }
0705 
0706     /* Flush all the cacheline without load/store in between */
0707     eqcr_pi = s->eqcr.pi;
0708     for (i = 0; i < num_enqueued; i++)
0709         eqcr_pi++;
0710     s->eqcr.pi = eqcr_pi & full_mask;
0711     spin_unlock(&s->access_spinlock);
0712 
0713     return num_enqueued;
0714 }
0715 
0716 /**
0717  * qbman_swp_enqueue_multiple_mem_back() - Issue a multi enqueue command
0718  * using one enqueue descriptor
0719  * @s:  the software portal used for enqueue
0720  * @d:  the enqueue descriptor
0721  * @fd: table pointer of frame descriptor table to be enqueued
0722  * @flags: table pointer of QBMAN_ENQUEUE_FLAG_DCA flags, not used if NULL
0723  * @num_frames: number of fd to be enqueued
0724  *
0725  * Return the number of fd enqueued, or a negative error number.
0726  */
0727 static
0728 int qbman_swp_enqueue_multiple_mem_back(struct qbman_swp *s,
0729                     const struct qbman_eq_desc *d,
0730                     const struct dpaa2_fd *fd,
0731                     uint32_t *flags,
0732                     int num_frames)
0733 {
0734     uint32_t *p = NULL;
0735     const uint32_t *cl = (uint32_t *)(d);
0736     uint32_t eqcr_ci, eqcr_pi, half_mask, full_mask;
0737     int i, num_enqueued = 0;
0738     unsigned long irq_flags;
0739 
0740     spin_lock_irqsave(&s->access_spinlock, irq_flags);
0741 
0742     half_mask = (s->eqcr.pi_ci_mask>>1);
0743     full_mask = s->eqcr.pi_ci_mask;
0744     if (!s->eqcr.available) {
0745         eqcr_ci = s->eqcr.ci;
0746         s->eqcr.ci = qbman_read_register(s, QBMAN_CINH_SWP_EQCR_CI);
0747         s->eqcr.ci &= full_mask;
0748         s->eqcr.available = qm_cyc_diff(s->eqcr.pi_ring_size,
0749                     eqcr_ci, s->eqcr.ci);
0750         if (!s->eqcr.available) {
0751             spin_unlock_irqrestore(&s->access_spinlock, irq_flags);
0752             return 0;
0753         }
0754     }
0755 
0756     eqcr_pi = s->eqcr.pi;
0757     num_enqueued = (s->eqcr.available < num_frames) ?
0758             s->eqcr.available : num_frames;
0759     s->eqcr.available -= num_enqueued;
0760     /* Fill in the EQCR ring */
0761     for (i = 0; i < num_enqueued; i++) {
0762         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0763         /* Skip copying the verb */
0764         memcpy(&p[1], &cl[1], EQ_DESC_SIZE_WITHOUT_FD - 1);
0765         memcpy(&p[EQ_DESC_SIZE_FD_START/sizeof(uint32_t)],
0766                &fd[i], sizeof(*fd));
0767         eqcr_pi++;
0768     }
0769 
0770     /* Set the verb byte, have to substitute in the valid-bit */
0771     eqcr_pi = s->eqcr.pi;
0772     for (i = 0; i < num_enqueued; i++) {
0773         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0774         p[0] = cl[0] | s->eqcr.pi_vb;
0775         if (flags && (flags[i] & QBMAN_ENQUEUE_FLAG_DCA)) {
0776             struct qbman_eq_desc *eq_desc = (struct qbman_eq_desc *)p;
0777 
0778             eq_desc->dca = (1 << QB_ENQUEUE_CMD_DCA_EN_SHIFT) |
0779                 ((flags[i]) & QBMAN_EQCR_DCA_IDXMASK);
0780         }
0781         eqcr_pi++;
0782         if (!(eqcr_pi & half_mask))
0783             s->eqcr.pi_vb ^= QB_VALID_BIT;
0784     }
0785     s->eqcr.pi = eqcr_pi & full_mask;
0786 
0787     dma_wmb();
0788     qbman_write_register(s, QBMAN_CINH_SWP_EQCR_PI,
0789                 (QB_RT_BIT)|(s->eqcr.pi)|s->eqcr.pi_vb);
0790     spin_unlock_irqrestore(&s->access_spinlock, irq_flags);
0791 
0792     return num_enqueued;
0793 }
0794 
0795 /**
0796  * qbman_swp_enqueue_multiple_desc_direct() - Issue a multi enqueue command
0797  * using multiple enqueue descriptor
0798  * @s:  the software portal used for enqueue
0799  * @d:  table of minimal enqueue descriptor
0800  * @fd: table pointer of frame descriptor table to be enqueued
0801  * @num_frames: number of fd to be enqueued
0802  *
0803  * Return the number of fd enqueued, or a negative error number.
0804  */
0805 static
0806 int qbman_swp_enqueue_multiple_desc_direct(struct qbman_swp *s,
0807                        const struct qbman_eq_desc *d,
0808                        const struct dpaa2_fd *fd,
0809                        int num_frames)
0810 {
0811     uint32_t *p;
0812     const uint32_t *cl;
0813     uint32_t eqcr_ci, eqcr_pi, half_mask, full_mask;
0814     int i, num_enqueued = 0;
0815 
0816     half_mask = (s->eqcr.pi_ci_mask>>1);
0817     full_mask = s->eqcr.pi_ci_mask;
0818     if (!s->eqcr.available) {
0819         eqcr_ci = s->eqcr.ci;
0820         p = s->addr_cena + QBMAN_CENA_SWP_EQCR_CI;
0821         s->eqcr.ci = qbman_read_register(s, QBMAN_CINH_SWP_EQCR_CI);
0822         s->eqcr.available = qm_cyc_diff(s->eqcr.pi_ring_size,
0823                     eqcr_ci, s->eqcr.ci);
0824         if (!s->eqcr.available)
0825             return 0;
0826     }
0827 
0828     eqcr_pi = s->eqcr.pi;
0829     num_enqueued = (s->eqcr.available < num_frames) ?
0830             s->eqcr.available : num_frames;
0831     s->eqcr.available -= num_enqueued;
0832     /* Fill in the EQCR ring */
0833     for (i = 0; i < num_enqueued; i++) {
0834         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0835         cl = (uint32_t *)(&d[i]);
0836         /* Skip copying the verb */
0837         memcpy(&p[1], &cl[1], EQ_DESC_SIZE_WITHOUT_FD - 1);
0838         memcpy(&p[EQ_DESC_SIZE_FD_START/sizeof(uint32_t)],
0839                &fd[i], sizeof(*fd));
0840         eqcr_pi++;
0841     }
0842 
0843     dma_wmb();
0844 
0845     /* Set the verb byte, have to substitute in the valid-bit */
0846     eqcr_pi = s->eqcr.pi;
0847     for (i = 0; i < num_enqueued; i++) {
0848         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0849         cl = (uint32_t *)(&d[i]);
0850         p[0] = cl[0] | s->eqcr.pi_vb;
0851         eqcr_pi++;
0852         if (!(eqcr_pi & half_mask))
0853             s->eqcr.pi_vb ^= QB_VALID_BIT;
0854     }
0855 
0856     /* Flush all the cacheline without load/store in between */
0857     eqcr_pi = s->eqcr.pi;
0858     for (i = 0; i < num_enqueued; i++)
0859         eqcr_pi++;
0860     s->eqcr.pi = eqcr_pi & full_mask;
0861 
0862     return num_enqueued;
0863 }
0864 
0865 /**
0866  * qbman_swp_enqueue_multiple_desc_mem_back() - Issue a multi enqueue command
0867  * using multiple enqueue descriptor
0868  * @s:  the software portal used for enqueue
0869  * @d:  table of minimal enqueue descriptor
0870  * @fd: table pointer of frame descriptor table to be enqueued
0871  * @num_frames: number of fd to be enqueued
0872  *
0873  * Return the number of fd enqueued, or a negative error number.
0874  */
0875 static
0876 int qbman_swp_enqueue_multiple_desc_mem_back(struct qbman_swp *s,
0877                        const struct qbman_eq_desc *d,
0878                        const struct dpaa2_fd *fd,
0879                        int num_frames)
0880 {
0881     uint32_t *p;
0882     const uint32_t *cl;
0883     uint32_t eqcr_ci, eqcr_pi, half_mask, full_mask;
0884     int i, num_enqueued = 0;
0885 
0886     half_mask = (s->eqcr.pi_ci_mask>>1);
0887     full_mask = s->eqcr.pi_ci_mask;
0888     if (!s->eqcr.available) {
0889         eqcr_ci = s->eqcr.ci;
0890         s->eqcr.ci = qbman_read_register(s, QBMAN_CINH_SWP_EQCR_CI);
0891         s->eqcr.ci &= full_mask;
0892         s->eqcr.available = qm_cyc_diff(s->eqcr.pi_ring_size,
0893                     eqcr_ci, s->eqcr.ci);
0894         if (!s->eqcr.available)
0895             return 0;
0896     }
0897 
0898     eqcr_pi = s->eqcr.pi;
0899     num_enqueued = (s->eqcr.available < num_frames) ?
0900             s->eqcr.available : num_frames;
0901     s->eqcr.available -= num_enqueued;
0902     /* Fill in the EQCR ring */
0903     for (i = 0; i < num_enqueued; i++) {
0904         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0905         cl = (uint32_t *)(&d[i]);
0906         /* Skip copying the verb */
0907         memcpy(&p[1], &cl[1], EQ_DESC_SIZE_WITHOUT_FD - 1);
0908         memcpy(&p[EQ_DESC_SIZE_FD_START/sizeof(uint32_t)],
0909                &fd[i], sizeof(*fd));
0910         eqcr_pi++;
0911     }
0912 
0913     /* Set the verb byte, have to substitute in the valid-bit */
0914     eqcr_pi = s->eqcr.pi;
0915     for (i = 0; i < num_enqueued; i++) {
0916         p = (s->addr_cena + QBMAN_CENA_SWP_EQCR(eqcr_pi & half_mask));
0917         cl = (uint32_t *)(&d[i]);
0918         p[0] = cl[0] | s->eqcr.pi_vb;
0919         eqcr_pi++;
0920         if (!(eqcr_pi & half_mask))
0921             s->eqcr.pi_vb ^= QB_VALID_BIT;
0922     }
0923 
0924     s->eqcr.pi = eqcr_pi & full_mask;
0925 
0926     dma_wmb();
0927     qbman_write_register(s, QBMAN_CINH_SWP_EQCR_PI,
0928                 (QB_RT_BIT)|(s->eqcr.pi)|s->eqcr.pi_vb);
0929 
0930     return num_enqueued;
0931 }
0932 
0933 /* Static (push) dequeue */
0934 
0935 /**
0936  * qbman_swp_push_get() - Get the push dequeue setup
0937  * @s:           the software portal object
0938  * @channel_idx: the channel index to query
0939  * @enabled:     returned boolean to show whether the push dequeue is enabled
0940  *               for the given channel
0941  */
0942 void qbman_swp_push_get(struct qbman_swp *s, u8 channel_idx, int *enabled)
0943 {
0944     u16 src = (s->sdq >> QB_SDQCR_SRC_SHIFT) & QB_SDQCR_SRC_MASK;
0945 
0946     WARN_ON(channel_idx > 15);
0947     *enabled = src | (1 << channel_idx);
0948 }
0949 
0950 /**
0951  * qbman_swp_push_set() - Enable or disable push dequeue
0952  * @s:           the software portal object
0953  * @channel_idx: the channel index (0 to 15)
0954  * @enable:      enable or disable push dequeue
0955  */
0956 void qbman_swp_push_set(struct qbman_swp *s, u8 channel_idx, int enable)
0957 {
0958     u16 dqsrc;
0959 
0960     WARN_ON(channel_idx > 15);
0961     if (enable)
0962         s->sdq |= 1 << channel_idx;
0963     else
0964         s->sdq &= ~(1 << channel_idx);
0965 
0966     /* Read make the complete src map.  If no channels are enabled
0967      * the SDQCR must be 0 or else QMan will assert errors
0968      */
0969     dqsrc = (s->sdq >> QB_SDQCR_SRC_SHIFT) & QB_SDQCR_SRC_MASK;
0970     if (dqsrc != 0)
0971         qbman_write_register(s, QBMAN_CINH_SWP_SDQCR, s->sdq);
0972     else
0973         qbman_write_register(s, QBMAN_CINH_SWP_SDQCR, 0);
0974 }
0975 
0976 #define QB_VDQCR_VERB_DCT_SHIFT    0
0977 #define QB_VDQCR_VERB_DT_SHIFT     2
0978 #define QB_VDQCR_VERB_RLS_SHIFT    4
0979 #define QB_VDQCR_VERB_WAE_SHIFT    5
0980 
0981 enum qb_pull_dt_e {
0982     qb_pull_dt_channel,
0983     qb_pull_dt_workqueue,
0984     qb_pull_dt_framequeue
0985 };
0986 
0987 /**
0988  * qbman_pull_desc_clear() - Clear the contents of a descriptor to
0989  *                           default/starting state
0990  * @d: the pull dequeue descriptor to be cleared
0991  */
0992 void qbman_pull_desc_clear(struct qbman_pull_desc *d)
0993 {
0994     memset(d, 0, sizeof(*d));
0995 }
0996 
0997 /**
0998  * qbman_pull_desc_set_storage()- Set the pull dequeue storage
0999  * @d:            the pull dequeue descriptor to be set
1000  * @storage:      the pointer of the memory to store the dequeue result
1001  * @storage_phys: the physical address of the storage memory
1002  * @stash:        to indicate whether write allocate is enabled
1003  *
1004  * If not called, or if called with 'storage' as NULL, the result pull dequeues
1005  * will produce results to DQRR. If 'storage' is non-NULL, then results are
1006  * produced to the given memory location (using the DMA address which
1007  * the caller provides in 'storage_phys'), and 'stash' controls whether or not
1008  * those writes to main-memory express a cache-warming attribute.
1009  */
1010 void qbman_pull_desc_set_storage(struct qbman_pull_desc *d,
1011                  struct dpaa2_dq *storage,
1012                  dma_addr_t storage_phys,
1013                  int stash)
1014 {
1015     /* save the virtual address */
1016     d->rsp_addr_virt = (u64)(uintptr_t)storage;
1017 
1018     if (!storage) {
1019         d->verb &= ~(1 << QB_VDQCR_VERB_RLS_SHIFT);
1020         return;
1021     }
1022     d->verb |= 1 << QB_VDQCR_VERB_RLS_SHIFT;
1023     if (stash)
1024         d->verb |= 1 << QB_VDQCR_VERB_WAE_SHIFT;
1025     else
1026         d->verb &= ~(1 << QB_VDQCR_VERB_WAE_SHIFT);
1027 
1028     d->rsp_addr = cpu_to_le64(storage_phys);
1029 }
1030 
1031 /**
1032  * qbman_pull_desc_set_numframes() - Set the number of frames to be dequeued
1033  * @d:         the pull dequeue descriptor to be set
1034  * @numframes: number of frames to be set, must be between 1 and 16, inclusive
1035  */
1036 void qbman_pull_desc_set_numframes(struct qbman_pull_desc *d, u8 numframes)
1037 {
1038     d->numf = numframes - 1;
1039 }
1040 
1041 /*
1042  * Exactly one of the following descriptor "actions" should be set. (Calling any
1043  * one of these will replace the effect of any prior call to one of these.)
1044  * - pull dequeue from the given frame queue (FQ)
1045  * - pull dequeue from any FQ in the given work queue (WQ)
1046  * - pull dequeue from any FQ in any WQ in the given channel
1047  */
1048 
1049 /**
1050  * qbman_pull_desc_set_fq() - Set fqid from which the dequeue command dequeues
1051  * @d:    the pull dequeue descriptor to be set
1052  * @fqid: the frame queue index of the given FQ
1053  */
1054 void qbman_pull_desc_set_fq(struct qbman_pull_desc *d, u32 fqid)
1055 {
1056     d->verb |= 1 << QB_VDQCR_VERB_DCT_SHIFT;
1057     d->verb |= qb_pull_dt_framequeue << QB_VDQCR_VERB_DT_SHIFT;
1058     d->dq_src = cpu_to_le32(fqid);
1059 }
1060 
1061 /**
1062  * qbman_pull_desc_set_wq() - Set wqid from which the dequeue command dequeues
1063  * @d:    the pull dequeue descriptor to be set
1064  * @wqid: composed of channel id and wqid within the channel
1065  * @dct:  the dequeue command type
1066  */
1067 void qbman_pull_desc_set_wq(struct qbman_pull_desc *d, u32 wqid,
1068                 enum qbman_pull_type_e dct)
1069 {
1070     d->verb |= dct << QB_VDQCR_VERB_DCT_SHIFT;
1071     d->verb |= qb_pull_dt_workqueue << QB_VDQCR_VERB_DT_SHIFT;
1072     d->dq_src = cpu_to_le32(wqid);
1073 }
1074 
1075 /**
1076  * qbman_pull_desc_set_channel() - Set channelid from which the dequeue command
1077  *                                 dequeues
1078  * @d:    the pull dequeue descriptor to be set
1079  * @chid: the channel id to be dequeued
1080  * @dct:  the dequeue command type
1081  */
1082 void qbman_pull_desc_set_channel(struct qbman_pull_desc *d, u32 chid,
1083                  enum qbman_pull_type_e dct)
1084 {
1085     d->verb |= dct << QB_VDQCR_VERB_DCT_SHIFT;
1086     d->verb |= qb_pull_dt_channel << QB_VDQCR_VERB_DT_SHIFT;
1087     d->dq_src = cpu_to_le32(chid);
1088 }
1089 
1090 /**
1091  * qbman_swp_pull_direct() - Issue the pull dequeue command
1092  * @s: the software portal object
1093  * @d: the software portal descriptor which has been configured with
1094  *     the set of qbman_pull_desc_set_*() calls
1095  *
1096  * Return 0 for success, and -EBUSY if the software portal is not ready
1097  * to do pull dequeue.
1098  */
1099 static
1100 int qbman_swp_pull_direct(struct qbman_swp *s, struct qbman_pull_desc *d)
1101 {
1102     struct qbman_pull_desc *p;
1103 
1104     if (!atomic_dec_and_test(&s->vdq.available)) {
1105         atomic_inc(&s->vdq.available);
1106         return -EBUSY;
1107     }
1108     s->vdq.storage = (void *)(uintptr_t)d->rsp_addr_virt;
1109     if ((s->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_5000)
1110         p = qbman_get_cmd(s, QBMAN_CENA_SWP_VDQCR);
1111     else
1112         p = qbman_get_cmd(s, QBMAN_CENA_SWP_VDQCR_MEM);
1113     p->numf = d->numf;
1114     p->tok = QMAN_DQ_TOKEN_VALID;
1115     p->dq_src = d->dq_src;
1116     p->rsp_addr = d->rsp_addr;
1117     p->rsp_addr_virt = d->rsp_addr_virt;
1118     dma_wmb();
1119     /* Set the verb byte, have to substitute in the valid-bit */
1120     p->verb = d->verb | s->vdq.valid_bit;
1121     s->vdq.valid_bit ^= QB_VALID_BIT;
1122 
1123     return 0;
1124 }
1125 
1126 /**
1127  * qbman_swp_pull_mem_back() - Issue the pull dequeue command
1128  * @s: the software portal object
1129  * @d: the software portal descriptor which has been configured with
1130  *     the set of qbman_pull_desc_set_*() calls
1131  *
1132  * Return 0 for success, and -EBUSY if the software portal is not ready
1133  * to do pull dequeue.
1134  */
1135 static
1136 int qbman_swp_pull_mem_back(struct qbman_swp *s, struct qbman_pull_desc *d)
1137 {
1138     struct qbman_pull_desc *p;
1139 
1140     if (!atomic_dec_and_test(&s->vdq.available)) {
1141         atomic_inc(&s->vdq.available);
1142         return -EBUSY;
1143     }
1144     s->vdq.storage = (void *)(uintptr_t)d->rsp_addr_virt;
1145     if ((s->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_5000)
1146         p = qbman_get_cmd(s, QBMAN_CENA_SWP_VDQCR);
1147     else
1148         p = qbman_get_cmd(s, QBMAN_CENA_SWP_VDQCR_MEM);
1149     p->numf = d->numf;
1150     p->tok = QMAN_DQ_TOKEN_VALID;
1151     p->dq_src = d->dq_src;
1152     p->rsp_addr = d->rsp_addr;
1153     p->rsp_addr_virt = d->rsp_addr_virt;
1154 
1155     /* Set the verb byte, have to substitute in the valid-bit */
1156     p->verb = d->verb | s->vdq.valid_bit;
1157     s->vdq.valid_bit ^= QB_VALID_BIT;
1158     dma_wmb();
1159     qbman_write_register(s, QBMAN_CINH_SWP_VDQCR_RT, QMAN_RT_MODE);
1160 
1161     return 0;
1162 }
1163 
1164 #define QMAN_DQRR_PI_MASK   0xf
1165 
1166 /**
1167  * qbman_swp_dqrr_next_direct() - Get an valid DQRR entry
1168  * @s: the software portal object
1169  *
1170  * Return NULL if there are no unconsumed DQRR entries. Return a DQRR entry
1171  * only once, so repeated calls can return a sequence of DQRR entries, without
1172  * requiring they be consumed immediately or in any particular order.
1173  */
1174 const struct dpaa2_dq *qbman_swp_dqrr_next_direct(struct qbman_swp *s)
1175 {
1176     u32 verb;
1177     u32 response_verb;
1178     u32 flags;
1179     struct dpaa2_dq *p;
1180 
1181     /* Before using valid-bit to detect if something is there, we have to
1182      * handle the case of the DQRR reset bug...
1183      */
1184     if (unlikely(s->dqrr.reset_bug)) {
1185         /*
1186          * We pick up new entries by cache-inhibited producer index,
1187          * which means that a non-coherent mapping would require us to
1188          * invalidate and read *only* once that PI has indicated that
1189          * there's an entry here. The first trip around the DQRR ring
1190          * will be much less efficient than all subsequent trips around
1191          * it...
1192          */
1193         u8 pi = qbman_read_register(s, QBMAN_CINH_SWP_DQPI) &
1194             QMAN_DQRR_PI_MASK;
1195 
1196         /* there are new entries if pi != next_idx */
1197         if (pi == s->dqrr.next_idx)
1198             return NULL;
1199 
1200         /*
1201          * if next_idx is/was the last ring index, and 'pi' is
1202          * different, we can disable the workaround as all the ring
1203          * entries have now been DMA'd to so valid-bit checking is
1204          * repaired. Note: this logic needs to be based on next_idx
1205          * (which increments one at a time), rather than on pi (which
1206          * can burst and wrap-around between our snapshots of it).
1207          */
1208         if (s->dqrr.next_idx == (s->dqrr.dqrr_size - 1)) {
1209             pr_debug("next_idx=%d, pi=%d, clear reset bug\n",
1210                  s->dqrr.next_idx, pi);
1211             s->dqrr.reset_bug = 0;
1212         }
1213         prefetch(qbman_get_cmd(s,
1214                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
1215     }
1216 
1217     p = qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
1218     verb = p->dq.verb;
1219 
1220     /*
1221      * If the valid-bit isn't of the expected polarity, nothing there. Note,
1222      * in the DQRR reset bug workaround, we shouldn't need to skip these
1223      * check, because we've already determined that a new entry is available
1224      * and we've invalidated the cacheline before reading it, so the
1225      * valid-bit behaviour is repaired and should tell us what we already
1226      * knew from reading PI.
1227      */
1228     if ((verb & QB_VALID_BIT) != s->dqrr.valid_bit) {
1229         prefetch(qbman_get_cmd(s,
1230                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
1231         return NULL;
1232     }
1233     /*
1234      * There's something there. Move "next_idx" attention to the next ring
1235      * entry (and prefetch it) before returning what we found.
1236      */
1237     s->dqrr.next_idx++;
1238     s->dqrr.next_idx &= s->dqrr.dqrr_size - 1; /* Wrap around */
1239     if (!s->dqrr.next_idx)
1240         s->dqrr.valid_bit ^= QB_VALID_BIT;
1241 
1242     /*
1243      * If this is the final response to a volatile dequeue command
1244      * indicate that the vdq is available
1245      */
1246     flags = p->dq.stat;
1247     response_verb = verb & QBMAN_RESULT_MASK;
1248     if ((response_verb == QBMAN_RESULT_DQ) &&
1249         (flags & DPAA2_DQ_STAT_VOLATILE) &&
1250         (flags & DPAA2_DQ_STAT_EXPIRED))
1251         atomic_inc(&s->vdq.available);
1252 
1253     prefetch(qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
1254 
1255     return p;
1256 }
1257 
1258 /**
1259  * qbman_swp_dqrr_next_mem_back() - Get an valid DQRR entry
1260  * @s: the software portal object
1261  *
1262  * Return NULL if there are no unconsumed DQRR entries. Return a DQRR entry
1263  * only once, so repeated calls can return a sequence of DQRR entries, without
1264  * requiring they be consumed immediately or in any particular order.
1265  */
1266 const struct dpaa2_dq *qbman_swp_dqrr_next_mem_back(struct qbman_swp *s)
1267 {
1268     u32 verb;
1269     u32 response_verb;
1270     u32 flags;
1271     struct dpaa2_dq *p;
1272 
1273     /* Before using valid-bit to detect if something is there, we have to
1274      * handle the case of the DQRR reset bug...
1275      */
1276     if (unlikely(s->dqrr.reset_bug)) {
1277         /*
1278          * We pick up new entries by cache-inhibited producer index,
1279          * which means that a non-coherent mapping would require us to
1280          * invalidate and read *only* once that PI has indicated that
1281          * there's an entry here. The first trip around the DQRR ring
1282          * will be much less efficient than all subsequent trips around
1283          * it...
1284          */
1285         u8 pi = qbman_read_register(s, QBMAN_CINH_SWP_DQPI) &
1286             QMAN_DQRR_PI_MASK;
1287 
1288         /* there are new entries if pi != next_idx */
1289         if (pi == s->dqrr.next_idx)
1290             return NULL;
1291 
1292         /*
1293          * if next_idx is/was the last ring index, and 'pi' is
1294          * different, we can disable the workaround as all the ring
1295          * entries have now been DMA'd to so valid-bit checking is
1296          * repaired. Note: this logic needs to be based on next_idx
1297          * (which increments one at a time), rather than on pi (which
1298          * can burst and wrap-around between our snapshots of it).
1299          */
1300         if (s->dqrr.next_idx == (s->dqrr.dqrr_size - 1)) {
1301             pr_debug("next_idx=%d, pi=%d, clear reset bug\n",
1302                  s->dqrr.next_idx, pi);
1303             s->dqrr.reset_bug = 0;
1304         }
1305         prefetch(qbman_get_cmd(s,
1306                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
1307     }
1308 
1309     p = qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR_MEM(s->dqrr.next_idx));
1310     verb = p->dq.verb;
1311 
1312     /*
1313      * If the valid-bit isn't of the expected polarity, nothing there. Note,
1314      * in the DQRR reset bug workaround, we shouldn't need to skip these
1315      * check, because we've already determined that a new entry is available
1316      * and we've invalidated the cacheline before reading it, so the
1317      * valid-bit behaviour is repaired and should tell us what we already
1318      * knew from reading PI.
1319      */
1320     if ((verb & QB_VALID_BIT) != s->dqrr.valid_bit) {
1321         prefetch(qbman_get_cmd(s,
1322                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
1323         return NULL;
1324     }
1325     /*
1326      * There's something there. Move "next_idx" attention to the next ring
1327      * entry (and prefetch it) before returning what we found.
1328      */
1329     s->dqrr.next_idx++;
1330     s->dqrr.next_idx &= s->dqrr.dqrr_size - 1; /* Wrap around */
1331     if (!s->dqrr.next_idx)
1332         s->dqrr.valid_bit ^= QB_VALID_BIT;
1333 
1334     /*
1335      * If this is the final response to a volatile dequeue command
1336      * indicate that the vdq is available
1337      */
1338     flags = p->dq.stat;
1339     response_verb = verb & QBMAN_RESULT_MASK;
1340     if ((response_verb == QBMAN_RESULT_DQ) &&
1341         (flags & DPAA2_DQ_STAT_VOLATILE) &&
1342         (flags & DPAA2_DQ_STAT_EXPIRED))
1343         atomic_inc(&s->vdq.available);
1344 
1345     prefetch(qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
1346 
1347     return p;
1348 }
1349 
1350 /**
1351  * qbman_swp_dqrr_consume() -  Consume DQRR entries previously returned from
1352  *                             qbman_swp_dqrr_next().
1353  * @s: the software portal object
1354  * @dq: the DQRR entry to be consumed
1355  */
1356 void qbman_swp_dqrr_consume(struct qbman_swp *s, const struct dpaa2_dq *dq)
1357 {
1358     qbman_write_register(s, QBMAN_CINH_SWP_DCAP, QBMAN_IDX_FROM_DQRR(dq));
1359 }
1360 
1361 /**
1362  * qbman_result_has_new_result() - Check and get the dequeue response from the
1363  *                                 dq storage memory set in pull dequeue command
1364  * @s: the software portal object
1365  * @dq: the dequeue result read from the memory
1366  *
1367  * Return 1 for getting a valid dequeue result, or 0 for not getting a valid
1368  * dequeue result.
1369  *
1370  * Only used for user-provided storage of dequeue results, not DQRR. For
1371  * efficiency purposes, the driver will perform any required endianness
1372  * conversion to ensure that the user's dequeue result storage is in host-endian
1373  * format. As such, once the user has called qbman_result_has_new_result() and
1374  * been returned a valid dequeue result, they should not call it again on
1375  * the same memory location (except of course if another dequeue command has
1376  * been executed to produce a new result to that location).
1377  */
1378 int qbman_result_has_new_result(struct qbman_swp *s, const struct dpaa2_dq *dq)
1379 {
1380     if (dq->dq.tok != QMAN_DQ_TOKEN_VALID)
1381         return 0;
1382 
1383     /*
1384      * Set token to be 0 so we will detect change back to 1
1385      * next time the looping is traversed. Const is cast away here
1386      * as we want users to treat the dequeue responses as read only.
1387      */
1388     ((struct dpaa2_dq *)dq)->dq.tok = 0;
1389 
1390     /*
1391      * Determine whether VDQCR is available based on whether the
1392      * current result is sitting in the first storage location of
1393      * the busy command.
1394      */
1395     if (s->vdq.storage == dq) {
1396         s->vdq.storage = NULL;
1397         atomic_inc(&s->vdq.available);
1398     }
1399 
1400     return 1;
1401 }
1402 
1403 /**
1404  * qbman_release_desc_clear() - Clear the contents of a descriptor to
1405  *                              default/starting state.
1406  * @d: the pull dequeue descriptor to be cleared
1407  */
1408 void qbman_release_desc_clear(struct qbman_release_desc *d)
1409 {
1410     memset(d, 0, sizeof(*d));
1411     d->verb = 1 << 5; /* Release Command Valid */
1412 }
1413 
1414 /**
1415  * qbman_release_desc_set_bpid() - Set the ID of the buffer pool to release to
1416  * @d:    the pull dequeue descriptor to be set
1417  * @bpid: the bpid value to be set
1418  */
1419 void qbman_release_desc_set_bpid(struct qbman_release_desc *d, u16 bpid)
1420 {
1421     d->bpid = cpu_to_le16(bpid);
1422 }
1423 
1424 /**
1425  * qbman_release_desc_set_rcdi() - Determines whether or not the portal's RCDI
1426  * interrupt source should be asserted after the release command is completed.
1427  * @d:      the pull dequeue descriptor to be set
1428  * @enable: enable (1) or disable (0) value
1429  */
1430 void qbman_release_desc_set_rcdi(struct qbman_release_desc *d, int enable)
1431 {
1432     if (enable)
1433         d->verb |= 1 << 6;
1434     else
1435         d->verb &= ~(1 << 6);
1436 }
1437 
1438 #define RAR_IDX(rar)     ((rar) & 0x7)
1439 #define RAR_VB(rar)      ((rar) & 0x80)
1440 #define RAR_SUCCESS(rar) ((rar) & 0x100)
1441 
1442 /**
1443  * qbman_swp_release_direct() - Issue a buffer release command
1444  * @s:           the software portal object
1445  * @d:           the release descriptor
1446  * @buffers:     a pointer pointing to the buffer address to be released
1447  * @num_buffers: number of buffers to be released,  must be less than 8
1448  *
1449  * Return 0 for success, -EBUSY if the release command ring is not ready.
1450  */
1451 int qbman_swp_release_direct(struct qbman_swp *s,
1452                  const struct qbman_release_desc *d,
1453                  const u64 *buffers, unsigned int num_buffers)
1454 {
1455     int i;
1456     struct qbman_release_desc *p;
1457     u32 rar;
1458 
1459     if (!num_buffers || (num_buffers > 7))
1460         return -EINVAL;
1461 
1462     rar = qbman_read_register(s, QBMAN_CINH_SWP_RAR);
1463     if (!RAR_SUCCESS(rar))
1464         return -EBUSY;
1465 
1466     /* Start the release command */
1467     p = qbman_get_cmd(s, QBMAN_CENA_SWP_RCR(RAR_IDX(rar)));
1468 
1469     /* Copy the caller's buffer pointers to the command */
1470     for (i = 0; i < num_buffers; i++)
1471         p->buf[i] = cpu_to_le64(buffers[i]);
1472     p->bpid = d->bpid;
1473 
1474     /*
1475      * Set the verb byte, have to substitute in the valid-bit
1476      * and the number of buffers.
1477      */
1478     dma_wmb();
1479     p->verb = d->verb | RAR_VB(rar) | num_buffers;
1480 
1481     return 0;
1482 }
1483 
1484 /**
1485  * qbman_swp_release_mem_back() - Issue a buffer release command
1486  * @s:           the software portal object
1487  * @d:           the release descriptor
1488  * @buffers:     a pointer pointing to the buffer address to be released
1489  * @num_buffers: number of buffers to be released,  must be less than 8
1490  *
1491  * Return 0 for success, -EBUSY if the release command ring is not ready.
1492  */
1493 int qbman_swp_release_mem_back(struct qbman_swp *s,
1494                    const struct qbman_release_desc *d,
1495                    const u64 *buffers, unsigned int num_buffers)
1496 {
1497     int i;
1498     struct qbman_release_desc *p;
1499     u32 rar;
1500 
1501     if (!num_buffers || (num_buffers > 7))
1502         return -EINVAL;
1503 
1504     rar = qbman_read_register(s, QBMAN_CINH_SWP_RAR);
1505     if (!RAR_SUCCESS(rar))
1506         return -EBUSY;
1507 
1508     /* Start the release command */
1509     p = qbman_get_cmd(s, QBMAN_CENA_SWP_RCR_MEM(RAR_IDX(rar)));
1510 
1511     /* Copy the caller's buffer pointers to the command */
1512     for (i = 0; i < num_buffers; i++)
1513         p->buf[i] = cpu_to_le64(buffers[i]);
1514     p->bpid = d->bpid;
1515 
1516     p->verb = d->verb | RAR_VB(rar) | num_buffers;
1517     dma_wmb();
1518     qbman_write_register(s, QBMAN_CINH_SWP_RCR_AM_RT +
1519                  RAR_IDX(rar)  * 4, QMAN_RT_MODE);
1520 
1521     return 0;
1522 }
1523 
1524 struct qbman_acquire_desc {
1525     u8 verb;
1526     u8 reserved;
1527     __le16 bpid;
1528     u8 num;
1529     u8 reserved2[59];
1530 };
1531 
1532 struct qbman_acquire_rslt {
1533     u8 verb;
1534     u8 rslt;
1535     __le16 reserved;
1536     u8 num;
1537     u8 reserved2[3];
1538     __le64 buf[7];
1539 };
1540 
1541 /**
1542  * qbman_swp_acquire() - Issue a buffer acquire command
1543  * @s:           the software portal object
1544  * @bpid:        the buffer pool index
1545  * @buffers:     a pointer pointing to the acquired buffer addresses
1546  * @num_buffers: number of buffers to be acquired, must be less than 8
1547  *
1548  * Return 0 for success, or negative error code if the acquire command
1549  * fails.
1550  */
1551 int qbman_swp_acquire(struct qbman_swp *s, u16 bpid, u64 *buffers,
1552               unsigned int num_buffers)
1553 {
1554     struct qbman_acquire_desc *p;
1555     struct qbman_acquire_rslt *r;
1556     int i;
1557 
1558     if (!num_buffers || (num_buffers > 7))
1559         return -EINVAL;
1560 
1561     /* Start the management command */
1562     p = qbman_swp_mc_start(s);
1563 
1564     if (!p)
1565         return -EBUSY;
1566 
1567     /* Encode the caller-provided attributes */
1568     p->bpid = cpu_to_le16(bpid);
1569     p->num = num_buffers;
1570 
1571     /* Complete the management command */
1572     r = qbman_swp_mc_complete(s, p, QBMAN_MC_ACQUIRE);
1573     if (unlikely(!r)) {
1574         pr_err("qbman: acquire from BPID %d failed, no response\n",
1575                bpid);
1576         return -EIO;
1577     }
1578 
1579     /* Decode the outcome */
1580     WARN_ON((r->verb & 0x7f) != QBMAN_MC_ACQUIRE);
1581 
1582     /* Determine success or failure */
1583     if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
1584         pr_err("qbman: acquire from BPID 0x%x failed, code=0x%02x\n",
1585                bpid, r->rslt);
1586         return -EIO;
1587     }
1588 
1589     WARN_ON(r->num > num_buffers);
1590 
1591     /* Copy the acquired buffers to the caller's array */
1592     for (i = 0; i < r->num; i++)
1593         buffers[i] = le64_to_cpu(r->buf[i]);
1594 
1595     return (int)r->num;
1596 }
1597 
1598 struct qbman_alt_fq_state_desc {
1599     u8 verb;
1600     u8 reserved[3];
1601     __le32 fqid;
1602     u8 reserved2[56];
1603 };
1604 
1605 struct qbman_alt_fq_state_rslt {
1606     u8 verb;
1607     u8 rslt;
1608     u8 reserved[62];
1609 };
1610 
1611 #define ALT_FQ_FQID_MASK 0x00FFFFFF
1612 
1613 int qbman_swp_alt_fq_state(struct qbman_swp *s, u32 fqid,
1614                u8 alt_fq_verb)
1615 {
1616     struct qbman_alt_fq_state_desc *p;
1617     struct qbman_alt_fq_state_rslt *r;
1618 
1619     /* Start the management command */
1620     p = qbman_swp_mc_start(s);
1621     if (!p)
1622         return -EBUSY;
1623 
1624     p->fqid = cpu_to_le32(fqid & ALT_FQ_FQID_MASK);
1625 
1626     /* Complete the management command */
1627     r = qbman_swp_mc_complete(s, p, alt_fq_verb);
1628     if (unlikely(!r)) {
1629         pr_err("qbman: mgmt cmd failed, no response (verb=0x%x)\n",
1630                alt_fq_verb);
1631         return -EIO;
1632     }
1633 
1634     /* Decode the outcome */
1635     WARN_ON((r->verb & QBMAN_RESULT_MASK) != alt_fq_verb);
1636 
1637     /* Determine success or failure */
1638     if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
1639         pr_err("qbman: ALT FQID %d failed: verb = 0x%08x code = 0x%02x\n",
1640                fqid, r->verb, r->rslt);
1641         return -EIO;
1642     }
1643 
1644     return 0;
1645 }
1646 
1647 struct qbman_cdan_ctrl_desc {
1648     u8 verb;
1649     u8 reserved;
1650     __le16 ch;
1651     u8 we;
1652     u8 ctrl;
1653     __le16 reserved2;
1654     __le64 cdan_ctx;
1655     u8 reserved3[48];
1656 
1657 };
1658 
1659 struct qbman_cdan_ctrl_rslt {
1660     u8 verb;
1661     u8 rslt;
1662     __le16 ch;
1663     u8 reserved[60];
1664 };
1665 
1666 int qbman_swp_CDAN_set(struct qbman_swp *s, u16 channelid,
1667                u8 we_mask, u8 cdan_en,
1668                u64 ctx)
1669 {
1670     struct qbman_cdan_ctrl_desc *p = NULL;
1671     struct qbman_cdan_ctrl_rslt *r = NULL;
1672 
1673     /* Start the management command */
1674     p = qbman_swp_mc_start(s);
1675     if (!p)
1676         return -EBUSY;
1677 
1678     /* Encode the caller-provided attributes */
1679     p->ch = cpu_to_le16(channelid);
1680     p->we = we_mask;
1681     if (cdan_en)
1682         p->ctrl = 1;
1683     else
1684         p->ctrl = 0;
1685     p->cdan_ctx = cpu_to_le64(ctx);
1686 
1687     /* Complete the management command */
1688     r = qbman_swp_mc_complete(s, p, QBMAN_WQCHAN_CONFIGURE);
1689     if (unlikely(!r)) {
1690         pr_err("qbman: wqchan config failed, no response\n");
1691         return -EIO;
1692     }
1693 
1694     WARN_ON((r->verb & 0x7f) != QBMAN_WQCHAN_CONFIGURE);
1695 
1696     /* Determine success or failure */
1697     if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
1698         pr_err("qbman: CDAN cQID %d failed: code = 0x%02x\n",
1699                channelid, r->rslt);
1700         return -EIO;
1701     }
1702 
1703     return 0;
1704 }
1705 
1706 #define QBMAN_RESPONSE_VERB_MASK    0x7f
1707 #define QBMAN_FQ_QUERY_NP       0x45
1708 #define QBMAN_BP_QUERY          0x32
1709 
1710 struct qbman_fq_query_desc {
1711     u8 verb;
1712     u8 reserved[3];
1713     __le32 fqid;
1714     u8 reserved2[56];
1715 };
1716 
1717 int qbman_fq_query_state(struct qbman_swp *s, u32 fqid,
1718              struct qbman_fq_query_np_rslt *r)
1719 {
1720     struct qbman_fq_query_desc *p;
1721     void *resp;
1722 
1723     p = (struct qbman_fq_query_desc *)qbman_swp_mc_start(s);
1724     if (!p)
1725         return -EBUSY;
1726 
1727     /* FQID is a 24 bit value */
1728     p->fqid = cpu_to_le32(fqid & 0x00FFFFFF);
1729     resp = qbman_swp_mc_complete(s, p, QBMAN_FQ_QUERY_NP);
1730     if (!resp) {
1731         pr_err("qbman: Query FQID %d NP fields failed, no response\n",
1732                fqid);
1733         return -EIO;
1734     }
1735     *r = *(struct qbman_fq_query_np_rslt *)resp;
1736     /* Decode the outcome */
1737     WARN_ON((r->verb & QBMAN_RESPONSE_VERB_MASK) != QBMAN_FQ_QUERY_NP);
1738 
1739     /* Determine success or failure */
1740     if (r->rslt != QBMAN_MC_RSLT_OK) {
1741         pr_err("Query NP fields of FQID 0x%x failed, code=0x%02x\n",
1742                p->fqid, r->rslt);
1743         return -EIO;
1744     }
1745 
1746     return 0;
1747 }
1748 
1749 u32 qbman_fq_state_frame_count(const struct qbman_fq_query_np_rslt *r)
1750 {
1751     return (le32_to_cpu(r->frm_cnt) & 0x00FFFFFF);
1752 }
1753 
1754 u32 qbman_fq_state_byte_count(const struct qbman_fq_query_np_rslt *r)
1755 {
1756     return le32_to_cpu(r->byte_cnt);
1757 }
1758 
1759 struct qbman_bp_query_desc {
1760     u8 verb;
1761     u8 reserved;
1762     __le16 bpid;
1763     u8 reserved2[60];
1764 };
1765 
1766 int qbman_bp_query(struct qbman_swp *s, u16 bpid,
1767            struct qbman_bp_query_rslt *r)
1768 {
1769     struct qbman_bp_query_desc *p;
1770     void *resp;
1771 
1772     p = (struct qbman_bp_query_desc *)qbman_swp_mc_start(s);
1773     if (!p)
1774         return -EBUSY;
1775 
1776     p->bpid = cpu_to_le16(bpid);
1777     resp = qbman_swp_mc_complete(s, p, QBMAN_BP_QUERY);
1778     if (!resp) {
1779         pr_err("qbman: Query BPID %d fields failed, no response\n",
1780                bpid);
1781         return -EIO;
1782     }
1783     *r = *(struct qbman_bp_query_rslt *)resp;
1784     /* Decode the outcome */
1785     WARN_ON((r->verb & QBMAN_RESPONSE_VERB_MASK) != QBMAN_BP_QUERY);
1786 
1787     /* Determine success or failure */
1788     if (r->rslt != QBMAN_MC_RSLT_OK) {
1789         pr_err("Query fields of BPID 0x%x failed, code=0x%02x\n",
1790                bpid, r->rslt);
1791         return -EIO;
1792     }
1793 
1794     return 0;
1795 }
1796 
1797 u32 qbman_bp_info_num_free_bufs(struct qbman_bp_query_rslt *a)
1798 {
1799     return le32_to_cpu(a->fill);
1800 }
1801 
1802 /**
1803  * qbman_swp_set_irq_coalescing() - Set new IRQ coalescing values
1804  * @p: the software portal object
1805  * @irq_threshold: interrupt threshold
1806  * @irq_holdoff: interrupt holdoff (timeout) period in us
1807  *
1808  * Return 0 for success, or negative error code on error.
1809  */
1810 int qbman_swp_set_irq_coalescing(struct qbman_swp *p, u32 irq_threshold,
1811                  u32 irq_holdoff)
1812 {
1813     u32 itp, max_holdoff;
1814 
1815     /* Convert irq_holdoff value from usecs to 256 QBMAN clock cycles
1816      * increments. This depends on the QBMAN internal frequency.
1817      */
1818     itp = (irq_holdoff * 1000) / p->desc->qman_256_cycles_per_ns;
1819     if (itp > 4096) {
1820         max_holdoff = (p->desc->qman_256_cycles_per_ns * 4096) / 1000;
1821         pr_err("irq_holdoff must be <= %uus\n", max_holdoff);
1822         return -EINVAL;
1823     }
1824 
1825     if (irq_threshold >= p->dqrr.dqrr_size) {
1826         pr_err("irq_threshold must be < %u\n", p->dqrr.dqrr_size - 1);
1827         return -EINVAL;
1828     }
1829 
1830     p->irq_threshold = irq_threshold;
1831     p->irq_holdoff = irq_holdoff;
1832 
1833     qbman_write_register(p, QBMAN_CINH_SWP_DQRR_ITR, irq_threshold);
1834     qbman_write_register(p, QBMAN_CINH_SWP_ITPR, itp);
1835 
1836     return 0;
1837 }
1838 
1839 /**
1840  * qbman_swp_get_irq_coalescing() - Get the current IRQ coalescing parameters
1841  * @p: the software portal object
1842  * @irq_threshold: interrupt threshold (an IRQ is generated when there are more
1843  * DQRR entries in the portal than the threshold)
1844  * @irq_holdoff: interrupt holdoff (timeout) period in us
1845  */
1846 void qbman_swp_get_irq_coalescing(struct qbman_swp *p, u32 *irq_threshold,
1847                   u32 *irq_holdoff)
1848 {
1849     if (irq_threshold)
1850         *irq_threshold = p->irq_threshold;
1851     if (irq_holdoff)
1852         *irq_holdoff = p->irq_holdoff;
1853 }