162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */ 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci#ifndef _FUNETH_TXRX_H 462306a36Sopenharmony_ci#define _FUNETH_TXRX_H 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/netdevice.h> 762306a36Sopenharmony_ci#include <linux/u64_stats_sync.h> 862306a36Sopenharmony_ci#include <net/xdp.h> 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci/* Tx descriptor size */ 1162306a36Sopenharmony_ci#define FUNETH_SQE_SIZE 64U 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci/* Size of device headers per Tx packet */ 1462306a36Sopenharmony_ci#define FUNETH_FUNOS_HDR_SZ (sizeof(struct fun_eth_tx_req)) 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci/* Number of gather list entries per Tx descriptor */ 1762306a36Sopenharmony_ci#define FUNETH_GLE_PER_DESC (FUNETH_SQE_SIZE / sizeof(struct fun_dataop_gl)) 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* Max gather list size in bytes for an sk_buff. */ 2062306a36Sopenharmony_ci#define FUNETH_MAX_GL_SZ ((MAX_SKB_FRAGS + 1) * sizeof(struct fun_dataop_gl)) 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_TLS_DEVICE) 2362306a36Sopenharmony_ci# define FUNETH_TLS_SZ sizeof(struct fun_eth_tls) 2462306a36Sopenharmony_ci#else 2562306a36Sopenharmony_ci# define FUNETH_TLS_SZ 0 2662306a36Sopenharmony_ci#endif 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* Max number of Tx descriptors for an sk_buff using a gather list. */ 2962306a36Sopenharmony_ci#define FUNETH_MAX_GL_DESC \ 3062306a36Sopenharmony_ci DIV_ROUND_UP((FUNETH_FUNOS_HDR_SZ + FUNETH_MAX_GL_SZ + FUNETH_TLS_SZ), \ 3162306a36Sopenharmony_ci FUNETH_SQE_SIZE) 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/* Max number of Tx descriptors for any packet. */ 3462306a36Sopenharmony_ci#define FUNETH_MAX_PKT_DESC FUNETH_MAX_GL_DESC 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci/* Rx CQ descriptor size. */ 3762306a36Sopenharmony_ci#define FUNETH_CQE_SIZE 64U 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci/* Offset of cqe_info within a CQE. */ 4062306a36Sopenharmony_ci#define FUNETH_CQE_INFO_OFFSET (FUNETH_CQE_SIZE - sizeof(struct fun_cqe_info)) 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci/* Construct the IRQ portion of a CQ doorbell. The resulting value arms the 4362306a36Sopenharmony_ci * interrupt with the supplied time delay and packet count moderation settings. 4462306a36Sopenharmony_ci */ 4562306a36Sopenharmony_ci#define FUN_IRQ_CQ_DB(usec, pkts) \ 4662306a36Sopenharmony_ci (FUN_DB_IRQ_ARM_F | ((usec) << FUN_DB_INTCOAL_USEC_S) | \ 4762306a36Sopenharmony_ci ((pkts) << FUN_DB_INTCOAL_ENTRIES_S)) 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci/* As above for SQ doorbells. */ 5062306a36Sopenharmony_ci#define FUN_IRQ_SQ_DB(usec, pkts) \ 5162306a36Sopenharmony_ci (FUN_DB_IRQ_ARM_F | \ 5262306a36Sopenharmony_ci ((usec) << FUN_DB_INTCOAL_USEC_S) | \ 5362306a36Sopenharmony_ci ((pkts) << FUN_DB_INTCOAL_ENTRIES_S)) 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/* Per packet tailroom. Present only for 1-frag packets. */ 5662306a36Sopenharmony_ci#define FUN_RX_TAILROOM SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* Per packet headroom for XDP. Preferred over XDP_PACKET_HEADROOM to 5962306a36Sopenharmony_ci * accommodate two packets per buffer for 4K pages and 1500B MTUs. 6062306a36Sopenharmony_ci */ 6162306a36Sopenharmony_ci#define FUN_XDP_HEADROOM 192 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci/* Initialization state of a queue. */ 6462306a36Sopenharmony_cienum { 6562306a36Sopenharmony_ci FUN_QSTATE_DESTROYED, /* what queue? */ 6662306a36Sopenharmony_ci FUN_QSTATE_INIT_SW, /* exists in SW, not on the device */ 6762306a36Sopenharmony_ci FUN_QSTATE_INIT_FULL, /* exists both in SW and on device */ 6862306a36Sopenharmony_ci}; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/* Initialization state of an interrupt. */ 7162306a36Sopenharmony_cienum { 7262306a36Sopenharmony_ci FUN_IRQ_INIT, /* initialized and in the XArray but inactive */ 7362306a36Sopenharmony_ci FUN_IRQ_REQUESTED, /* request_irq() done */ 7462306a36Sopenharmony_ci FUN_IRQ_ENABLED, /* processing enabled */ 7562306a36Sopenharmony_ci FUN_IRQ_DISABLED, /* processing disabled */ 7662306a36Sopenharmony_ci}; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cistruct bpf_prog; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_cistruct funeth_txq_stats { /* per Tx queue SW counters */ 8162306a36Sopenharmony_ci u64 tx_pkts; /* # of Tx packets */ 8262306a36Sopenharmony_ci u64 tx_bytes; /* total bytes of Tx packets */ 8362306a36Sopenharmony_ci u64 tx_cso; /* # of packets with checksum offload */ 8462306a36Sopenharmony_ci u64 tx_tso; /* # of non-encapsulated TSO super-packets */ 8562306a36Sopenharmony_ci u64 tx_encap_tso; /* # of encapsulated TSO super-packets */ 8662306a36Sopenharmony_ci u64 tx_uso; /* # of non-encapsulated UDP LSO super-packets */ 8762306a36Sopenharmony_ci u64 tx_more; /* # of DBs elided due to xmit_more */ 8862306a36Sopenharmony_ci u64 tx_nstops; /* # of times the queue has stopped */ 8962306a36Sopenharmony_ci u64 tx_nrestarts; /* # of times the queue has restarted */ 9062306a36Sopenharmony_ci u64 tx_map_err; /* # of packets dropped due to DMA mapping errors */ 9162306a36Sopenharmony_ci u64 tx_xdp_full; /* # of XDP packets that could not be enqueued */ 9262306a36Sopenharmony_ci u64 tx_tls_pkts; /* # of Tx TLS packets offloaded to HW */ 9362306a36Sopenharmony_ci u64 tx_tls_bytes; /* Tx bytes of HW-handled TLS payload */ 9462306a36Sopenharmony_ci u64 tx_tls_fallback; /* attempted Tx TLS offloads punted to SW */ 9562306a36Sopenharmony_ci u64 tx_tls_drops; /* attempted Tx TLS offloads dropped */ 9662306a36Sopenharmony_ci}; 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_cistruct funeth_tx_info { /* per Tx descriptor state */ 9962306a36Sopenharmony_ci union { 10062306a36Sopenharmony_ci struct sk_buff *skb; /* associated packet (sk_buff path) */ 10162306a36Sopenharmony_ci struct xdp_frame *xdpf; /* associated XDP frame (XDP path) */ 10262306a36Sopenharmony_ci }; 10362306a36Sopenharmony_ci}; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_cistruct funeth_txq { 10662306a36Sopenharmony_ci /* RO cacheline of frequently accessed data */ 10762306a36Sopenharmony_ci u32 mask; /* queue depth - 1 */ 10862306a36Sopenharmony_ci u32 hw_qid; /* device ID of the queue */ 10962306a36Sopenharmony_ci void *desc; /* base address of descriptor ring */ 11062306a36Sopenharmony_ci struct funeth_tx_info *info; 11162306a36Sopenharmony_ci struct device *dma_dev; /* device for DMA mappings */ 11262306a36Sopenharmony_ci volatile __be64 *hw_wb; /* HW write-back location */ 11362306a36Sopenharmony_ci u32 __iomem *db; /* SQ doorbell register address */ 11462306a36Sopenharmony_ci struct netdev_queue *ndq; 11562306a36Sopenharmony_ci dma_addr_t dma_addr; /* DMA address of descriptor ring */ 11662306a36Sopenharmony_ci /* producer R/W cacheline */ 11762306a36Sopenharmony_ci u16 qidx; /* queue index within net_device */ 11862306a36Sopenharmony_ci u16 ethid; 11962306a36Sopenharmony_ci u32 prod_cnt; /* producer counter */ 12062306a36Sopenharmony_ci struct funeth_txq_stats stats; 12162306a36Sopenharmony_ci /* shared R/W cacheline, primarily accessed by consumer */ 12262306a36Sopenharmony_ci u32 irq_db_val; /* value written to IRQ doorbell */ 12362306a36Sopenharmony_ci u32 cons_cnt; /* consumer (cleanup) counter */ 12462306a36Sopenharmony_ci struct net_device *netdev; 12562306a36Sopenharmony_ci struct fun_irq *irq; 12662306a36Sopenharmony_ci int numa_node; 12762306a36Sopenharmony_ci u8 init_state; /* queue initialization state */ 12862306a36Sopenharmony_ci struct u64_stats_sync syncp; 12962306a36Sopenharmony_ci}; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_cistruct funeth_rxq_stats { /* per Rx queue SW counters */ 13262306a36Sopenharmony_ci u64 rx_pkts; /* # of received packets, including SW drops */ 13362306a36Sopenharmony_ci u64 rx_bytes; /* total size of received packets */ 13462306a36Sopenharmony_ci u64 rx_cso; /* # of packets with checksum offload */ 13562306a36Sopenharmony_ci u64 rx_bufs; /* total # of Rx buffers provided to device */ 13662306a36Sopenharmony_ci u64 gro_pkts; /* # of GRO superpackets */ 13762306a36Sopenharmony_ci u64 gro_merged; /* # of pkts merged into existing GRO superpackets */ 13862306a36Sopenharmony_ci u64 rx_page_alloc; /* # of page allocations for Rx buffers */ 13962306a36Sopenharmony_ci u64 rx_budget; /* NAPI iterations that exhausted their budget */ 14062306a36Sopenharmony_ci u64 rx_mem_drops; /* # of packets dropped due to memory shortage */ 14162306a36Sopenharmony_ci u64 rx_map_err; /* # of page DMA mapping errors */ 14262306a36Sopenharmony_ci u64 xdp_drops; /* XDP_DROPped packets */ 14362306a36Sopenharmony_ci u64 xdp_tx; /* successful XDP transmits */ 14462306a36Sopenharmony_ci u64 xdp_redir; /* successful XDP redirects */ 14562306a36Sopenharmony_ci u64 xdp_err; /* packets dropped due to XDP errors */ 14662306a36Sopenharmony_ci}; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_cistruct funeth_rxbuf { /* per Rx buffer state */ 14962306a36Sopenharmony_ci struct page *page; /* associated page */ 15062306a36Sopenharmony_ci dma_addr_t dma_addr; /* DMA address of page start */ 15162306a36Sopenharmony_ci int pg_refs; /* page refs held by driver */ 15262306a36Sopenharmony_ci int node; /* page node, or -1 if it is PF_MEMALLOC */ 15362306a36Sopenharmony_ci}; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_cistruct funeth_rx_cache { /* cache of DMA-mapped previously used buffers */ 15662306a36Sopenharmony_ci struct funeth_rxbuf *bufs; /* base of Rx buffer state ring */ 15762306a36Sopenharmony_ci unsigned int prod_cnt; /* producer counter */ 15862306a36Sopenharmony_ci unsigned int cons_cnt; /* consumer counter */ 15962306a36Sopenharmony_ci unsigned int mask; /* depth - 1 */ 16062306a36Sopenharmony_ci}; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci/* An Rx queue consists of a CQ and an SQ used to provide Rx buffers. */ 16362306a36Sopenharmony_cistruct funeth_rxq { 16462306a36Sopenharmony_ci struct net_device *netdev; 16562306a36Sopenharmony_ci struct napi_struct *napi; 16662306a36Sopenharmony_ci struct device *dma_dev; /* device for DMA mappings */ 16762306a36Sopenharmony_ci void *cqes; /* base of CQ descriptor ring */ 16862306a36Sopenharmony_ci const void *next_cqe_info; /* fun_cqe_info of next CQE */ 16962306a36Sopenharmony_ci u32 __iomem *cq_db; /* CQ doorbell register address */ 17062306a36Sopenharmony_ci unsigned int cq_head; /* CQ head index */ 17162306a36Sopenharmony_ci unsigned int cq_mask; /* CQ depth - 1 */ 17262306a36Sopenharmony_ci u16 phase; /* CQ phase tag */ 17362306a36Sopenharmony_ci u16 qidx; /* queue index within net_device */ 17462306a36Sopenharmony_ci unsigned int irq_db_val; /* IRQ info for CQ doorbell */ 17562306a36Sopenharmony_ci struct fun_eprq_rqbuf *rqes; /* base of RQ descriptor ring */ 17662306a36Sopenharmony_ci struct funeth_rxbuf *bufs; /* base of Rx buffer state ring */ 17762306a36Sopenharmony_ci struct funeth_rxbuf *cur_buf; /* currently active buffer */ 17862306a36Sopenharmony_ci u32 __iomem *rq_db; /* RQ doorbell register address */ 17962306a36Sopenharmony_ci unsigned int rq_cons; /* RQ consumer counter */ 18062306a36Sopenharmony_ci unsigned int rq_mask; /* RQ depth - 1 */ 18162306a36Sopenharmony_ci unsigned int buf_offset; /* offset of next pkt in head buffer */ 18262306a36Sopenharmony_ci u8 xdp_flush; /* XDP flush types needed at NAPI end */ 18362306a36Sopenharmony_ci u8 init_state; /* queue initialization state */ 18462306a36Sopenharmony_ci u16 headroom; /* per packet headroom */ 18562306a36Sopenharmony_ci unsigned int rq_cons_db; /* value of rq_cons at last RQ db */ 18662306a36Sopenharmony_ci unsigned int rq_db_thres; /* # of new buffers needed to write RQ db */ 18762306a36Sopenharmony_ci struct funeth_rxbuf spare_buf; /* spare for next buffer replacement */ 18862306a36Sopenharmony_ci struct funeth_rx_cache cache; /* used buffer cache */ 18962306a36Sopenharmony_ci struct bpf_prog *xdp_prog; /* optional XDP BPF program */ 19062306a36Sopenharmony_ci struct funeth_rxq_stats stats; 19162306a36Sopenharmony_ci dma_addr_t cq_dma_addr; /* DMA address of CQE ring */ 19262306a36Sopenharmony_ci dma_addr_t rq_dma_addr; /* DMA address of RQE ring */ 19362306a36Sopenharmony_ci u16 irq_cnt; 19462306a36Sopenharmony_ci u32 hw_cqid; /* device ID of the queue's CQ */ 19562306a36Sopenharmony_ci u32 hw_sqid; /* device ID of the queue's SQ */ 19662306a36Sopenharmony_ci int numa_node; 19762306a36Sopenharmony_ci struct u64_stats_sync syncp; 19862306a36Sopenharmony_ci struct xdp_rxq_info xdp_rxq; 19962306a36Sopenharmony_ci}; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci#define FUN_QSTAT_INC(q, counter) \ 20262306a36Sopenharmony_ci do { \ 20362306a36Sopenharmony_ci u64_stats_update_begin(&(q)->syncp); \ 20462306a36Sopenharmony_ci (q)->stats.counter++; \ 20562306a36Sopenharmony_ci u64_stats_update_end(&(q)->syncp); \ 20662306a36Sopenharmony_ci } while (0) 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci#define FUN_QSTAT_READ(q, seq, stats_copy) \ 20962306a36Sopenharmony_ci do { \ 21062306a36Sopenharmony_ci seq = u64_stats_fetch_begin(&(q)->syncp); \ 21162306a36Sopenharmony_ci stats_copy = (q)->stats; \ 21262306a36Sopenharmony_ci } while (u64_stats_fetch_retry(&(q)->syncp, (seq))) 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci#define FUN_INT_NAME_LEN (IFNAMSIZ + 16) 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_cistruct fun_irq { 21762306a36Sopenharmony_ci struct napi_struct napi; 21862306a36Sopenharmony_ci struct funeth_txq *txq; 21962306a36Sopenharmony_ci struct funeth_rxq *rxq; 22062306a36Sopenharmony_ci u8 state; 22162306a36Sopenharmony_ci u16 irq_idx; /* index of MSI-X interrupt */ 22262306a36Sopenharmony_ci int irq; /* Linux IRQ vector */ 22362306a36Sopenharmony_ci cpumask_t affinity_mask; /* IRQ affinity */ 22462306a36Sopenharmony_ci struct irq_affinity_notify aff_notify; 22562306a36Sopenharmony_ci char name[FUN_INT_NAME_LEN]; 22662306a36Sopenharmony_ci} ____cacheline_internodealigned_in_smp; 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci/* Return the start address of the idx-th Tx descriptor. */ 22962306a36Sopenharmony_cistatic inline void *fun_tx_desc_addr(const struct funeth_txq *q, 23062306a36Sopenharmony_ci unsigned int idx) 23162306a36Sopenharmony_ci{ 23262306a36Sopenharmony_ci return q->desc + idx * FUNETH_SQE_SIZE; 23362306a36Sopenharmony_ci} 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_cistatic inline void fun_txq_wr_db(const struct funeth_txq *q) 23662306a36Sopenharmony_ci{ 23762306a36Sopenharmony_ci unsigned int tail = q->prod_cnt & q->mask; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci writel(tail, q->db); 24062306a36Sopenharmony_ci} 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_cistatic inline int fun_irq_node(const struct fun_irq *p) 24362306a36Sopenharmony_ci{ 24462306a36Sopenharmony_ci return cpu_to_mem(cpumask_first(&p->affinity_mask)); 24562306a36Sopenharmony_ci} 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ciint fun_rxq_napi_poll(struct napi_struct *napi, int budget); 24862306a36Sopenharmony_ciint fun_txq_napi_poll(struct napi_struct *napi, int budget); 24962306a36Sopenharmony_cinetdev_tx_t fun_start_xmit(struct sk_buff *skb, struct net_device *netdev); 25062306a36Sopenharmony_cibool fun_xdp_tx(struct funeth_txq *q, struct xdp_frame *xdpf); 25162306a36Sopenharmony_ciint fun_xdp_xmit_frames(struct net_device *dev, int n, 25262306a36Sopenharmony_ci struct xdp_frame **frames, u32 flags); 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ciint funeth_txq_create(struct net_device *dev, unsigned int qidx, 25562306a36Sopenharmony_ci unsigned int ndesc, struct fun_irq *irq, int state, 25662306a36Sopenharmony_ci struct funeth_txq **qp); 25762306a36Sopenharmony_ciint fun_txq_create_dev(struct funeth_txq *q, struct fun_irq *irq); 25862306a36Sopenharmony_cistruct funeth_txq *funeth_txq_free(struct funeth_txq *q, int state); 25962306a36Sopenharmony_ciint funeth_rxq_create(struct net_device *dev, unsigned int qidx, 26062306a36Sopenharmony_ci unsigned int ncqe, unsigned int nrqe, struct fun_irq *irq, 26162306a36Sopenharmony_ci int state, struct funeth_rxq **qp); 26262306a36Sopenharmony_ciint fun_rxq_create_dev(struct funeth_rxq *q, struct fun_irq *irq); 26362306a36Sopenharmony_cistruct funeth_rxq *funeth_rxq_free(struct funeth_rxq *q, int state); 26462306a36Sopenharmony_ciint fun_rxq_set_bpf(struct funeth_rxq *q, struct bpf_prog *prog); 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci#endif /* _FUNETH_TXRX_H */ 267