Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
0003  *
0004  * This software is available to you under a choice of one of two
0005  * licenses.  You may choose to be licensed under the terms of the GNU
0006  * General Public License (GPL) Version 2, available from the file
0007  * COPYING in the main directory of this source tree, or the
0008  * OpenIB.org BSD license below:
0009  *
0010  *     Redistribution and use in source and binary forms, with or
0011  *     without modification, are permitted provided that the following
0012  *     conditions are met:
0013  *
0014  *      - Redistributions of source code must retain the above
0015  *        copyright notice, this list of conditions and the following
0016  *        disclaimer.
0017  *      - Redistributions in binary form must reproduce the above
0018  *        copyright notice, this list of conditions and the following
0019  *        disclaimer in the documentation and/or other materials
0020  *        provided with the distribution.
0021  *
0022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0023  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0024  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0025  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0026  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0027  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0028  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0029  * SOFTWARE.
0030  */
0031 #ifndef __T4_H__
0032 #define __T4_H__
0033 
0034 #include "t4_hw.h"
0035 #include "t4_regs.h"
0036 #include "t4_values.h"
0037 #include "t4_msg.h"
0038 #include "t4_tcb.h"
0039 #include "t4fw_ri_api.h"
0040 
0041 #define T4_MAX_NUM_PD 65536
0042 #define T4_MAX_MR_SIZE (~0ULL)
0043 #define T4_PAGESIZE_MASK 0xffff000  /* 4KB-128MB */
0044 #define T4_STAG_UNSET 0xffffffff
0045 #define T4_FW_MAJ 0
0046 #define PCIE_MA_SYNC_A 0x30b4
0047 
0048 struct t4_status_page {
0049     __be32 rsvd1;   /* flit 0 - hw owns */
0050     __be16 rsvd2;
0051     __be16 qid;
0052     __be16 cidx;
0053     __be16 pidx;
0054     u8 qp_err;  /* flit 1 - sw owns */
0055     u8 db_off;
0056     u8 pad[2];
0057     u16 host_wq_pidx;
0058     u16 host_cidx;
0059     u16 host_pidx;
0060     u16 pad2;
0061     u32 srqidx;
0062 };
0063 
0064 #define T4_RQT_ENTRY_SHIFT 6
0065 #define T4_RQT_ENTRY_SIZE  BIT(T4_RQT_ENTRY_SHIFT)
0066 #define T4_EQ_ENTRY_SIZE 64
0067 
0068 #define T4_SQ_NUM_SLOTS 5
0069 #define T4_SQ_NUM_BYTES (T4_EQ_ENTRY_SIZE * T4_SQ_NUM_SLOTS)
0070 #define T4_MAX_SEND_SGE ((T4_SQ_NUM_BYTES - sizeof(struct fw_ri_send_wr) - \
0071             sizeof(struct fw_ri_isgl)) / sizeof(struct fw_ri_sge))
0072 #define T4_MAX_SEND_INLINE ((T4_SQ_NUM_BYTES - sizeof(struct fw_ri_send_wr) - \
0073             sizeof(struct fw_ri_immd)))
0074 #define T4_MAX_WRITE_INLINE ((T4_SQ_NUM_BYTES - \
0075             sizeof(struct fw_ri_rdma_write_wr) - \
0076             sizeof(struct fw_ri_immd)))
0077 #define T4_MAX_WRITE_SGE ((T4_SQ_NUM_BYTES - \
0078             sizeof(struct fw_ri_rdma_write_wr) - \
0079             sizeof(struct fw_ri_isgl)) / sizeof(struct fw_ri_sge))
0080 #define T4_MAX_FR_IMMD ((T4_SQ_NUM_BYTES - sizeof(struct fw_ri_fr_nsmr_wr) - \
0081             sizeof(struct fw_ri_immd)) & ~31UL)
0082 #define T4_MAX_FR_IMMD_DEPTH (T4_MAX_FR_IMMD / sizeof(u64))
0083 #define T4_MAX_FR_DSGL 1024
0084 #define T4_MAX_FR_DSGL_DEPTH (T4_MAX_FR_DSGL / sizeof(u64))
0085 
0086 static inline int t4_max_fr_depth(int use_dsgl)
0087 {
0088     return use_dsgl ? T4_MAX_FR_DSGL_DEPTH : T4_MAX_FR_IMMD_DEPTH;
0089 }
0090 
0091 #define T4_RQ_NUM_SLOTS 2
0092 #define T4_RQ_NUM_BYTES (T4_EQ_ENTRY_SIZE * T4_RQ_NUM_SLOTS)
0093 #define T4_MAX_RECV_SGE 4
0094 
0095 #define T4_WRITE_CMPL_MAX_SGL 4
0096 #define T4_WRITE_CMPL_MAX_CQE 16
0097 
0098 union t4_wr {
0099     struct fw_ri_res_wr res;
0100     struct fw_ri_wr ri;
0101     struct fw_ri_rdma_write_wr write;
0102     struct fw_ri_send_wr send;
0103     struct fw_ri_rdma_read_wr read;
0104     struct fw_ri_bind_mw_wr bind;
0105     struct fw_ri_fr_nsmr_wr fr;
0106     struct fw_ri_fr_nsmr_tpte_wr fr_tpte;
0107     struct fw_ri_inv_lstag_wr inv;
0108     struct fw_ri_rdma_write_cmpl_wr write_cmpl;
0109     struct t4_status_page status;
0110     __be64 flits[T4_EQ_ENTRY_SIZE / sizeof(__be64) * T4_SQ_NUM_SLOTS];
0111 };
0112 
0113 union t4_recv_wr {
0114     struct fw_ri_recv_wr recv;
0115     struct t4_status_page status;
0116     __be64 flits[T4_EQ_ENTRY_SIZE / sizeof(__be64) * T4_RQ_NUM_SLOTS];
0117 };
0118 
0119 static inline void init_wr_hdr(union t4_wr *wqe, u16 wrid,
0120                    enum fw_wr_opcodes opcode, u8 flags, u8 len16)
0121 {
0122     wqe->send.opcode = (u8)opcode;
0123     wqe->send.flags = flags;
0124     wqe->send.wrid = wrid;
0125     wqe->send.r1[0] = 0;
0126     wqe->send.r1[1] = 0;
0127     wqe->send.r1[2] = 0;
0128     wqe->send.len16 = len16;
0129 }
0130 
0131 /* CQE/AE status codes */
0132 #define T4_ERR_SUCCESS                     0x0
0133 #define T4_ERR_STAG                        0x1  /* STAG invalid: either the */
0134                         /* STAG is offlimt, being 0, */
0135                         /* or STAG_key mismatch */
0136 #define T4_ERR_PDID                        0x2  /* PDID mismatch */
0137 #define T4_ERR_QPID                        0x3  /* QPID mismatch */
0138 #define T4_ERR_ACCESS                      0x4  /* Invalid access right */
0139 #define T4_ERR_WRAP                        0x5  /* Wrap error */
0140 #define T4_ERR_BOUND                       0x6  /* base and bounds voilation */
0141 #define T4_ERR_INVALIDATE_SHARED_MR        0x7  /* attempt to invalidate a  */
0142                         /* shared memory region */
0143 #define T4_ERR_INVALIDATE_MR_WITH_MW_BOUND 0x8  /* attempt to invalidate a  */
0144                         /* shared memory region */
0145 #define T4_ERR_ECC                         0x9  /* ECC error detected */
0146 #define T4_ERR_ECC_PSTAG                   0xA  /* ECC error detected when  */
0147                         /* reading PSTAG for a MW  */
0148                         /* Invalidate */
0149 #define T4_ERR_PBL_ADDR_BOUND              0xB  /* pbl addr out of bounds:  */
0150                         /* software error */
0151 #define T4_ERR_SWFLUSH             0xC  /* SW FLUSHED */
0152 #define T4_ERR_CRC                         0x10 /* CRC error */
0153 #define T4_ERR_MARKER                      0x11 /* Marker error */
0154 #define T4_ERR_PDU_LEN_ERR                 0x12 /* invalid PDU length */
0155 #define T4_ERR_OUT_OF_RQE                  0x13 /* out of RQE */
0156 #define T4_ERR_DDP_VERSION                 0x14 /* wrong DDP version */
0157 #define T4_ERR_RDMA_VERSION                0x15 /* wrong RDMA version */
0158 #define T4_ERR_OPCODE                      0x16 /* invalid rdma opcode */
0159 #define T4_ERR_DDP_QUEUE_NUM               0x17 /* invalid ddp queue number */
0160 #define T4_ERR_MSN                         0x18 /* MSN error */
0161 #define T4_ERR_TBIT                        0x19 /* tag bit not set correctly */
0162 #define T4_ERR_MO                          0x1A /* MO not 0 for TERMINATE  */
0163                         /* or READ_REQ */
0164 #define T4_ERR_MSN_GAP                     0x1B
0165 #define T4_ERR_MSN_RANGE                   0x1C
0166 #define T4_ERR_IRD_OVERFLOW                0x1D
0167 #define T4_ERR_RQE_ADDR_BOUND              0x1E /* RQE addr out of bounds:  */
0168                         /* software error */
0169 #define T4_ERR_INTERNAL_ERR                0x1F /* internal error (opcode  */
0170                         /* mismatch) */
0171 /*
0172  * CQE defs
0173  */
0174 struct t4_cqe {
0175     __be32 header;
0176     __be32 len;
0177     union {
0178         struct {
0179             __be32 stag;
0180             __be32 msn;
0181         } rcqe;
0182         struct {
0183             __be32 stag;
0184             u16 nada2;
0185             u16 cidx;
0186         } scqe;
0187         struct {
0188             __be32 wrid_hi;
0189             __be32 wrid_low;
0190         } gen;
0191         struct {
0192             __be32 stag;
0193             __be32 msn;
0194             __be32 reserved;
0195             __be32 abs_rqe_idx;
0196         } srcqe;
0197         struct {
0198             __be32 mo;
0199             __be32 msn;
0200             /*
0201              * Use union for immediate data to be consistent with
0202              * stack's 32 bit data and iWARP spec's 64 bit data.
0203              */
0204             union {
0205                 struct {
0206                     __be32 imm_data32;
0207                     u32 reserved;
0208                 } ib_imm_data;
0209                 __be64 imm_data64;
0210             } iw_imm_data;
0211         } imm_data_rcqe;
0212 
0213         u64 drain_cookie;
0214         __be64 flits[3];
0215     } u;
0216     __be64 reserved[3];
0217     __be64 bits_type_ts;
0218 };
0219 
0220 /* macros for flit 0 of the cqe */
0221 
0222 #define CQE_QPID_S        12
0223 #define CQE_QPID_M        0xFFFFF
0224 #define CQE_QPID_G(x)     ((((x) >> CQE_QPID_S)) & CQE_QPID_M)
0225 #define CQE_QPID_V(x)     ((x)<<CQE_QPID_S)
0226 
0227 #define CQE_SWCQE_S       11
0228 #define CQE_SWCQE_M       0x1
0229 #define CQE_SWCQE_G(x)    ((((x) >> CQE_SWCQE_S)) & CQE_SWCQE_M)
0230 #define CQE_SWCQE_V(x)    ((x)<<CQE_SWCQE_S)
0231 
0232 #define CQE_DRAIN_S       10
0233 #define CQE_DRAIN_M       0x1
0234 #define CQE_DRAIN_G(x)    ((((x) >> CQE_DRAIN_S)) & CQE_DRAIN_M)
0235 #define CQE_DRAIN_V(x)    ((x)<<CQE_DRAIN_S)
0236 
0237 #define CQE_STATUS_S      5
0238 #define CQE_STATUS_M      0x1F
0239 #define CQE_STATUS_G(x)   ((((x) >> CQE_STATUS_S)) & CQE_STATUS_M)
0240 #define CQE_STATUS_V(x)   ((x)<<CQE_STATUS_S)
0241 
0242 #define CQE_TYPE_S        4
0243 #define CQE_TYPE_M        0x1
0244 #define CQE_TYPE_G(x)     ((((x) >> CQE_TYPE_S)) & CQE_TYPE_M)
0245 #define CQE_TYPE_V(x)     ((x)<<CQE_TYPE_S)
0246 
0247 #define CQE_OPCODE_S      0
0248 #define CQE_OPCODE_M      0xF
0249 #define CQE_OPCODE_G(x)   ((((x) >> CQE_OPCODE_S)) & CQE_OPCODE_M)
0250 #define CQE_OPCODE_V(x)   ((x)<<CQE_OPCODE_S)
0251 
0252 #define SW_CQE(x)         (CQE_SWCQE_G(be32_to_cpu((x)->header)))
0253 #define DRAIN_CQE(x)      (CQE_DRAIN_G(be32_to_cpu((x)->header)))
0254 #define CQE_QPID(x)       (CQE_QPID_G(be32_to_cpu((x)->header)))
0255 #define CQE_TYPE(x)       (CQE_TYPE_G(be32_to_cpu((x)->header)))
0256 #define SQ_TYPE(x)    (CQE_TYPE((x)))
0257 #define RQ_TYPE(x)    (!CQE_TYPE((x)))
0258 #define CQE_STATUS(x)     (CQE_STATUS_G(be32_to_cpu((x)->header)))
0259 #define CQE_OPCODE(x)     (CQE_OPCODE_G(be32_to_cpu((x)->header)))
0260 
0261 #define CQE_SEND_OPCODE(x)( \
0262     (CQE_OPCODE_G(be32_to_cpu((x)->header)) == FW_RI_SEND) || \
0263     (CQE_OPCODE_G(be32_to_cpu((x)->header)) == FW_RI_SEND_WITH_SE) || \
0264     (CQE_OPCODE_G(be32_to_cpu((x)->header)) == FW_RI_SEND_WITH_INV) || \
0265     (CQE_OPCODE_G(be32_to_cpu((x)->header)) == FW_RI_SEND_WITH_SE_INV))
0266 
0267 #define CQE_LEN(x)        (be32_to_cpu((x)->len))
0268 
0269 /* used for RQ completion processing */
0270 #define CQE_WRID_STAG(x)  (be32_to_cpu((x)->u.rcqe.stag))
0271 #define CQE_WRID_MSN(x)   (be32_to_cpu((x)->u.rcqe.msn))
0272 #define CQE_ABS_RQE_IDX(x) (be32_to_cpu((x)->u.srcqe.abs_rqe_idx))
0273 #define CQE_IMM_DATA(x)( \
0274     (x)->u.imm_data_rcqe.iw_imm_data.ib_imm_data.imm_data32)
0275 
0276 /* used for SQ completion processing */
0277 #define CQE_WRID_SQ_IDX(x)  ((x)->u.scqe.cidx)
0278 #define CQE_WRID_FR_STAG(x)     (be32_to_cpu((x)->u.scqe.stag))
0279 
0280 /* generic accessor macros */
0281 #define CQE_WRID_HI(x)      (be32_to_cpu((x)->u.gen.wrid_hi))
0282 #define CQE_WRID_LOW(x)     (be32_to_cpu((x)->u.gen.wrid_low))
0283 #define CQE_DRAIN_COOKIE(x) ((x)->u.drain_cookie)
0284 
0285 /* macros for flit 3 of the cqe */
0286 #define CQE_GENBIT_S    63
0287 #define CQE_GENBIT_M    0x1
0288 #define CQE_GENBIT_G(x) (((x) >> CQE_GENBIT_S) & CQE_GENBIT_M)
0289 #define CQE_GENBIT_V(x) ((x)<<CQE_GENBIT_S)
0290 
0291 #define CQE_OVFBIT_S    62
0292 #define CQE_OVFBIT_M    0x1
0293 #define CQE_OVFBIT_G(x) ((((x) >> CQE_OVFBIT_S)) & CQE_OVFBIT_M)
0294 
0295 #define CQE_IQTYPE_S    60
0296 #define CQE_IQTYPE_M    0x3
0297 #define CQE_IQTYPE_G(x) ((((x) >> CQE_IQTYPE_S)) & CQE_IQTYPE_M)
0298 
0299 #define CQE_TS_M    0x0fffffffffffffffULL
0300 #define CQE_TS_G(x) ((x) & CQE_TS_M)
0301 
0302 #define CQE_OVFBIT(x)   ((unsigned)CQE_OVFBIT_G(be64_to_cpu((x)->bits_type_ts)))
0303 #define CQE_GENBIT(x)   ((unsigned)CQE_GENBIT_G(be64_to_cpu((x)->bits_type_ts)))
0304 #define CQE_TS(x)   (CQE_TS_G(be64_to_cpu((x)->bits_type_ts)))
0305 
0306 struct t4_swsqe {
0307     u64         wr_id;
0308     struct t4_cqe       cqe;
0309     int         read_len;
0310     int         opcode;
0311     int         complete;
0312     int         signaled;
0313     u16         idx;
0314     int                     flushed;
0315     ktime_t         host_time;
0316     u64                     sge_ts;
0317 };
0318 
0319 static inline pgprot_t t4_pgprot_wc(pgprot_t prot)
0320 {
0321 #if defined(__i386__) || defined(__x86_64__) || defined(CONFIG_PPC64)
0322     return pgprot_writecombine(prot);
0323 #else
0324     return pgprot_noncached(prot);
0325 #endif
0326 }
0327 
0328 enum {
0329     T4_SQ_ONCHIP = (1<<0),
0330 };
0331 
0332 struct t4_sq {
0333     union t4_wr *queue;
0334     dma_addr_t dma_addr;
0335     DEFINE_DMA_UNMAP_ADDR(mapping);
0336     unsigned long phys_addr;
0337     struct t4_swsqe *sw_sq;
0338     struct t4_swsqe *oldest_read;
0339     void __iomem *bar2_va;
0340     u64 bar2_pa;
0341     size_t memsize;
0342     u32 bar2_qid;
0343     u32 qid;
0344     u16 in_use;
0345     u16 size;
0346     u16 cidx;
0347     u16 pidx;
0348     u16 wq_pidx;
0349     u16 wq_pidx_inc;
0350     u16 flags;
0351     short flush_cidx;
0352 };
0353 
0354 struct t4_swrqe {
0355     u64 wr_id;
0356     ktime_t host_time;
0357     u64 sge_ts;
0358     int valid;
0359 };
0360 
0361 struct t4_rq {
0362     union  t4_recv_wr *queue;
0363     dma_addr_t dma_addr;
0364     DEFINE_DMA_UNMAP_ADDR(mapping);
0365     struct t4_swrqe *sw_rq;
0366     void __iomem *bar2_va;
0367     u64 bar2_pa;
0368     size_t memsize;
0369     u32 bar2_qid;
0370     u32 qid;
0371     u32 msn;
0372     u32 rqt_hwaddr;
0373     u16 rqt_size;
0374     u16 in_use;
0375     u16 size;
0376     u16 cidx;
0377     u16 pidx;
0378     u16 wq_pidx;
0379     u16 wq_pidx_inc;
0380 };
0381 
0382 struct t4_wq {
0383     struct t4_sq sq;
0384     struct t4_rq rq;
0385     void __iomem *db;
0386     struct c4iw_rdev *rdev;
0387     int flushed;
0388     u8 *qp_errp;
0389     u32 *srqidxp;
0390 };
0391 
0392 struct t4_srq_pending_wr {
0393     u64 wr_id;
0394     union t4_recv_wr wqe;
0395     u8 len16;
0396 };
0397 
0398 struct t4_srq {
0399     union t4_recv_wr *queue;
0400     dma_addr_t dma_addr;
0401     DEFINE_DMA_UNMAP_ADDR(mapping);
0402     struct t4_swrqe *sw_rq;
0403     void __iomem *bar2_va;
0404     u64 bar2_pa;
0405     size_t memsize;
0406     u32 bar2_qid;
0407     u32 qid;
0408     u32 msn;
0409     u32 rqt_hwaddr;
0410     u32 rqt_abs_idx;
0411     u16 rqt_size;
0412     u16 size;
0413     u16 cidx;
0414     u16 pidx;
0415     u16 wq_pidx;
0416     u16 wq_pidx_inc;
0417     u16 in_use;
0418     struct t4_srq_pending_wr *pending_wrs;
0419     u16 pending_cidx;
0420     u16 pending_pidx;
0421     u16 pending_in_use;
0422     u16 ooo_count;
0423 };
0424 
0425 static inline u32 t4_srq_avail(struct t4_srq *srq)
0426 {
0427     return srq->size - 1 - srq->in_use;
0428 }
0429 
0430 static inline void t4_srq_produce(struct t4_srq *srq, u8 len16)
0431 {
0432     srq->in_use++;
0433     if (++srq->pidx == srq->size)
0434         srq->pidx = 0;
0435     srq->wq_pidx += DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
0436     if (srq->wq_pidx >= srq->size * T4_RQ_NUM_SLOTS)
0437         srq->wq_pidx %= srq->size * T4_RQ_NUM_SLOTS;
0438     srq->queue[srq->size].status.host_pidx = srq->pidx;
0439 }
0440 
0441 static inline void t4_srq_produce_pending_wr(struct t4_srq *srq)
0442 {
0443     srq->pending_in_use++;
0444     srq->in_use++;
0445     if (++srq->pending_pidx == srq->size)
0446         srq->pending_pidx = 0;
0447 }
0448 
0449 static inline void t4_srq_consume_pending_wr(struct t4_srq *srq)
0450 {
0451     srq->pending_in_use--;
0452     srq->in_use--;
0453     if (++srq->pending_cidx == srq->size)
0454         srq->pending_cidx = 0;
0455 }
0456 
0457 static inline void t4_srq_produce_ooo(struct t4_srq *srq)
0458 {
0459     srq->in_use--;
0460     srq->ooo_count++;
0461 }
0462 
0463 static inline void t4_srq_consume_ooo(struct t4_srq *srq)
0464 {
0465     srq->cidx++;
0466     if (srq->cidx == srq->size)
0467         srq->cidx  = 0;
0468     srq->queue[srq->size].status.host_cidx = srq->cidx;
0469     srq->ooo_count--;
0470 }
0471 
0472 static inline void t4_srq_consume(struct t4_srq *srq)
0473 {
0474     srq->in_use--;
0475     if (++srq->cidx == srq->size)
0476         srq->cidx = 0;
0477     srq->queue[srq->size].status.host_cidx = srq->cidx;
0478 }
0479 
0480 static inline int t4_rqes_posted(struct t4_wq *wq)
0481 {
0482     return wq->rq.in_use;
0483 }
0484 
0485 static inline int t4_rq_empty(struct t4_wq *wq)
0486 {
0487     return wq->rq.in_use == 0;
0488 }
0489 
0490 static inline u32 t4_rq_avail(struct t4_wq *wq)
0491 {
0492     return wq->rq.size - 1 - wq->rq.in_use;
0493 }
0494 
0495 static inline void t4_rq_produce(struct t4_wq *wq, u8 len16)
0496 {
0497     wq->rq.in_use++;
0498     if (++wq->rq.pidx == wq->rq.size)
0499         wq->rq.pidx = 0;
0500     wq->rq.wq_pidx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
0501     if (wq->rq.wq_pidx >= wq->rq.size * T4_RQ_NUM_SLOTS)
0502         wq->rq.wq_pidx %= wq->rq.size * T4_RQ_NUM_SLOTS;
0503 }
0504 
0505 static inline void t4_rq_consume(struct t4_wq *wq)
0506 {
0507     wq->rq.in_use--;
0508     if (++wq->rq.cidx == wq->rq.size)
0509         wq->rq.cidx = 0;
0510 }
0511 
0512 static inline u16 t4_rq_host_wq_pidx(struct t4_wq *wq)
0513 {
0514     return wq->rq.queue[wq->rq.size].status.host_wq_pidx;
0515 }
0516 
0517 static inline u16 t4_rq_wq_size(struct t4_wq *wq)
0518 {
0519         return wq->rq.size * T4_RQ_NUM_SLOTS;
0520 }
0521 
0522 static inline int t4_sq_onchip(struct t4_sq *sq)
0523 {
0524     return sq->flags & T4_SQ_ONCHIP;
0525 }
0526 
0527 static inline int t4_sq_empty(struct t4_wq *wq)
0528 {
0529     return wq->sq.in_use == 0;
0530 }
0531 
0532 static inline u32 t4_sq_avail(struct t4_wq *wq)
0533 {
0534     return wq->sq.size - 1 - wq->sq.in_use;
0535 }
0536 
0537 static inline void t4_sq_produce(struct t4_wq *wq, u8 len16)
0538 {
0539     wq->sq.in_use++;
0540     if (++wq->sq.pidx == wq->sq.size)
0541         wq->sq.pidx = 0;
0542     wq->sq.wq_pidx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
0543     if (wq->sq.wq_pidx >= wq->sq.size * T4_SQ_NUM_SLOTS)
0544         wq->sq.wq_pidx %= wq->sq.size * T4_SQ_NUM_SLOTS;
0545 }
0546 
0547 static inline void t4_sq_consume(struct t4_wq *wq)
0548 {
0549     if (wq->sq.cidx == wq->sq.flush_cidx)
0550         wq->sq.flush_cidx = -1;
0551     wq->sq.in_use--;
0552     if (++wq->sq.cidx == wq->sq.size)
0553         wq->sq.cidx = 0;
0554 }
0555 
0556 static inline u16 t4_sq_host_wq_pidx(struct t4_wq *wq)
0557 {
0558     return wq->sq.queue[wq->sq.size].status.host_wq_pidx;
0559 }
0560 
0561 static inline u16 t4_sq_wq_size(struct t4_wq *wq)
0562 {
0563         return wq->sq.size * T4_SQ_NUM_SLOTS;
0564 }
0565 
0566 /* This function copies 64 byte coalesced work request to memory
0567  * mapped BAR2 space. For coalesced WRs, the SGE fetches data
0568  * from the FIFO instead of from Host.
0569  */
0570 static inline void pio_copy(u64 __iomem *dst, u64 *src)
0571 {
0572     int count = 8;
0573 
0574     while (count) {
0575         writeq(*src, dst);
0576         src++;
0577         dst++;
0578         count--;
0579     }
0580 }
0581 
0582 static inline void t4_ring_srq_db(struct t4_srq *srq, u16 inc, u8 len16,
0583                   union t4_recv_wr *wqe)
0584 {
0585     /* Flush host queue memory writes. */
0586     wmb();
0587     if (inc == 1 && srq->bar2_qid == 0 && wqe) {
0588         pr_debug("%s : WC srq->pidx = %d; len16=%d\n",
0589              __func__, srq->pidx, len16);
0590         pio_copy(srq->bar2_va + SGE_UDB_WCDOORBELL, (u64 *)wqe);
0591     } else {
0592         pr_debug("%s: DB srq->pidx = %d; len16=%d\n",
0593              __func__, srq->pidx, len16);
0594         writel(PIDX_T5_V(inc) | QID_V(srq->bar2_qid),
0595                srq->bar2_va + SGE_UDB_KDOORBELL);
0596     }
0597     /* Flush user doorbell area writes. */
0598     wmb();
0599 }
0600 
0601 static inline void t4_ring_sq_db(struct t4_wq *wq, u16 inc, union t4_wr *wqe)
0602 {
0603 
0604     /* Flush host queue memory writes. */
0605     wmb();
0606     if (wq->sq.bar2_va) {
0607         if (inc == 1 && wq->sq.bar2_qid == 0 && wqe) {
0608             pr_debug("WC wq->sq.pidx = %d\n", wq->sq.pidx);
0609             pio_copy((u64 __iomem *)
0610                  (wq->sq.bar2_va + SGE_UDB_WCDOORBELL),
0611                  (u64 *)wqe);
0612         } else {
0613             pr_debug("DB wq->sq.pidx = %d\n", wq->sq.pidx);
0614             writel(PIDX_T5_V(inc) | QID_V(wq->sq.bar2_qid),
0615                    wq->sq.bar2_va + SGE_UDB_KDOORBELL);
0616         }
0617 
0618         /* Flush user doorbell area writes. */
0619         wmb();
0620         return;
0621     }
0622     writel(QID_V(wq->sq.qid) | PIDX_V(inc), wq->db);
0623 }
0624 
0625 static inline void t4_ring_rq_db(struct t4_wq *wq, u16 inc,
0626                  union t4_recv_wr *wqe)
0627 {
0628 
0629     /* Flush host queue memory writes. */
0630     wmb();
0631     if (wq->rq.bar2_va) {
0632         if (inc == 1 && wq->rq.bar2_qid == 0 && wqe) {
0633             pr_debug("WC wq->rq.pidx = %d\n", wq->rq.pidx);
0634             pio_copy((u64 __iomem *)
0635                  (wq->rq.bar2_va + SGE_UDB_WCDOORBELL),
0636                  (void *)wqe);
0637         } else {
0638             pr_debug("DB wq->rq.pidx = %d\n", wq->rq.pidx);
0639             writel(PIDX_T5_V(inc) | QID_V(wq->rq.bar2_qid),
0640                    wq->rq.bar2_va + SGE_UDB_KDOORBELL);
0641         }
0642 
0643         /* Flush user doorbell area writes. */
0644         wmb();
0645         return;
0646     }
0647     writel(QID_V(wq->rq.qid) | PIDX_V(inc), wq->db);
0648 }
0649 
0650 static inline int t4_wq_in_error(struct t4_wq *wq)
0651 {
0652     return *wq->qp_errp;
0653 }
0654 
0655 static inline void t4_set_wq_in_error(struct t4_wq *wq, u32 srqidx)
0656 {
0657     if (srqidx)
0658         *wq->srqidxp = srqidx;
0659     *wq->qp_errp = 1;
0660 }
0661 
0662 static inline void t4_disable_wq_db(struct t4_wq *wq)
0663 {
0664     wq->rq.queue[wq->rq.size].status.db_off = 1;
0665 }
0666 
0667 static inline void t4_enable_wq_db(struct t4_wq *wq)
0668 {
0669     wq->rq.queue[wq->rq.size].status.db_off = 0;
0670 }
0671 
0672 enum t4_cq_flags {
0673     CQ_ARMED    = 1,
0674 };
0675 
0676 struct t4_cq {
0677     struct t4_cqe *queue;
0678     dma_addr_t dma_addr;
0679     DEFINE_DMA_UNMAP_ADDR(mapping);
0680     struct t4_cqe *sw_queue;
0681     void __iomem *gts;
0682     void __iomem *bar2_va;
0683     u64 bar2_pa;
0684     u32 bar2_qid;
0685     struct c4iw_rdev *rdev;
0686     size_t memsize;
0687     __be64 bits_type_ts;
0688     u32 cqid;
0689     u32 qid_mask;
0690     int vector;
0691     u16 size; /* including status page */
0692     u16 cidx;
0693     u16 sw_pidx;
0694     u16 sw_cidx;
0695     u16 sw_in_use;
0696     u16 cidx_inc;
0697     u8 gen;
0698     u8 error;
0699     u8 *qp_errp;
0700     unsigned long flags;
0701 };
0702 
0703 static inline void write_gts(struct t4_cq *cq, u32 val)
0704 {
0705     if (cq->bar2_va)
0706         writel(val | INGRESSQID_V(cq->bar2_qid),
0707                cq->bar2_va + SGE_UDB_GTS);
0708     else
0709         writel(val | INGRESSQID_V(cq->cqid), cq->gts);
0710 }
0711 
0712 static inline int t4_clear_cq_armed(struct t4_cq *cq)
0713 {
0714     return test_and_clear_bit(CQ_ARMED, &cq->flags);
0715 }
0716 
0717 static inline int t4_arm_cq(struct t4_cq *cq, int se)
0718 {
0719     u32 val;
0720 
0721     set_bit(CQ_ARMED, &cq->flags);
0722     while (cq->cidx_inc > CIDXINC_M) {
0723         val = SEINTARM_V(0) | CIDXINC_V(CIDXINC_M) | TIMERREG_V(7);
0724         write_gts(cq, val);
0725         cq->cidx_inc -= CIDXINC_M;
0726     }
0727     val = SEINTARM_V(se) | CIDXINC_V(cq->cidx_inc) | TIMERREG_V(6);
0728     write_gts(cq, val);
0729     cq->cidx_inc = 0;
0730     return 0;
0731 }
0732 
0733 static inline void t4_swcq_produce(struct t4_cq *cq)
0734 {
0735     cq->sw_in_use++;
0736     if (cq->sw_in_use == cq->size) {
0737         pr_warn("%s cxgb4 sw cq overflow cqid %u\n",
0738             __func__, cq->cqid);
0739         cq->error = 1;
0740         cq->sw_in_use--;
0741         return;
0742     }
0743     if (++cq->sw_pidx == cq->size)
0744         cq->sw_pidx = 0;
0745 }
0746 
0747 static inline void t4_swcq_consume(struct t4_cq *cq)
0748 {
0749     cq->sw_in_use--;
0750     if (++cq->sw_cidx == cq->size)
0751         cq->sw_cidx = 0;
0752 }
0753 
0754 static inline void t4_hwcq_consume(struct t4_cq *cq)
0755 {
0756     cq->bits_type_ts = cq->queue[cq->cidx].bits_type_ts;
0757     if (++cq->cidx_inc == (cq->size >> 4) || cq->cidx_inc == CIDXINC_M) {
0758         u32 val;
0759 
0760         val = SEINTARM_V(0) | CIDXINC_V(cq->cidx_inc) | TIMERREG_V(7);
0761         write_gts(cq, val);
0762         cq->cidx_inc = 0;
0763     }
0764     if (++cq->cidx == cq->size) {
0765         cq->cidx = 0;
0766         cq->gen ^= 1;
0767     }
0768 }
0769 
0770 static inline int t4_valid_cqe(struct t4_cq *cq, struct t4_cqe *cqe)
0771 {
0772     return (CQE_GENBIT(cqe) == cq->gen);
0773 }
0774 
0775 static inline int t4_cq_notempty(struct t4_cq *cq)
0776 {
0777     return cq->sw_in_use || t4_valid_cqe(cq, &cq->queue[cq->cidx]);
0778 }
0779 
0780 static inline int t4_next_hw_cqe(struct t4_cq *cq, struct t4_cqe **cqe)
0781 {
0782     int ret;
0783     u16 prev_cidx;
0784 
0785     if (cq->cidx == 0)
0786         prev_cidx = cq->size - 1;
0787     else
0788         prev_cidx = cq->cidx - 1;
0789 
0790     if (cq->queue[prev_cidx].bits_type_ts != cq->bits_type_ts) {
0791         ret = -EOVERFLOW;
0792         cq->error = 1;
0793         pr_err("cq overflow cqid %u\n", cq->cqid);
0794     } else if (t4_valid_cqe(cq, &cq->queue[cq->cidx])) {
0795 
0796         /* Ensure CQE is flushed to memory */
0797         rmb();
0798         *cqe = &cq->queue[cq->cidx];
0799         ret = 0;
0800     } else
0801         ret = -ENODATA;
0802     return ret;
0803 }
0804 
0805 static inline int t4_next_cqe(struct t4_cq *cq, struct t4_cqe **cqe)
0806 {
0807     int ret = 0;
0808 
0809     if (cq->error)
0810         ret = -ENODATA;
0811     else if (cq->sw_in_use)
0812         *cqe = &cq->sw_queue[cq->sw_cidx];
0813     else
0814         ret = t4_next_hw_cqe(cq, cqe);
0815     return ret;
0816 }
0817 
0818 static inline void t4_set_cq_in_error(struct t4_cq *cq)
0819 {
0820     *cq->qp_errp = 1;
0821 }
0822 #endif
0823 
0824 struct t4_dev_status_page {
0825     u8 db_off;
0826     u8 write_cmpl_supported;
0827     u16 pad2;
0828     u32 pad3;
0829     u64 qp_start;
0830     u64 qp_size;
0831     u64 cq_start;
0832     u64 cq_size;
0833 };