funeth_txrx.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
  2. #ifndef _FUNETH_TXRX_H
  3. #define _FUNETH_TXRX_H
  4. #include <linux/netdevice.h>
  5. #include <linux/u64_stats_sync.h>
  6. #include <net/xdp.h>
  7. /* Tx descriptor size */
  8. #define FUNETH_SQE_SIZE 64U
  9. /* Size of device headers per Tx packet */
  10. #define FUNETH_FUNOS_HDR_SZ (sizeof(struct fun_eth_tx_req))
  11. /* Number of gather list entries per Tx descriptor */
  12. #define FUNETH_GLE_PER_DESC (FUNETH_SQE_SIZE / sizeof(struct fun_dataop_gl))
  13. /* Max gather list size in bytes for an sk_buff. */
  14. #define FUNETH_MAX_GL_SZ ((MAX_SKB_FRAGS + 1) * sizeof(struct fun_dataop_gl))
  15. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  16. # define FUNETH_TLS_SZ sizeof(struct fun_eth_tls)
  17. #else
  18. # define FUNETH_TLS_SZ 0
  19. #endif
  20. /* Max number of Tx descriptors for an sk_buff using a gather list. */
  21. #define FUNETH_MAX_GL_DESC \
  22. DIV_ROUND_UP((FUNETH_FUNOS_HDR_SZ + FUNETH_MAX_GL_SZ + FUNETH_TLS_SZ), \
  23. FUNETH_SQE_SIZE)
  24. /* Max number of Tx descriptors for any packet. */
  25. #define FUNETH_MAX_PKT_DESC FUNETH_MAX_GL_DESC
  26. /* Rx CQ descriptor size. */
  27. #define FUNETH_CQE_SIZE 64U
  28. /* Offset of cqe_info within a CQE. */
  29. #define FUNETH_CQE_INFO_OFFSET (FUNETH_CQE_SIZE - sizeof(struct fun_cqe_info))
  30. /* Construct the IRQ portion of a CQ doorbell. The resulting value arms the
  31. * interrupt with the supplied time delay and packet count moderation settings.
  32. */
  33. #define FUN_IRQ_CQ_DB(usec, pkts) \
  34. (FUN_DB_IRQ_ARM_F | ((usec) << FUN_DB_INTCOAL_USEC_S) | \
  35. ((pkts) << FUN_DB_INTCOAL_ENTRIES_S))
  36. /* As above for SQ doorbells. */
  37. #define FUN_IRQ_SQ_DB(usec, pkts) \
  38. (FUN_DB_IRQ_ARM_F | \
  39. ((usec) << FUN_DB_INTCOAL_USEC_S) | \
  40. ((pkts) << FUN_DB_INTCOAL_ENTRIES_S))
  41. /* Per packet tailroom. Present only for 1-frag packets. */
  42. #define FUN_RX_TAILROOM SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
  43. /* Per packet headroom for XDP. Preferred over XDP_PACKET_HEADROOM to
  44. * accommodate two packets per buffer for 4K pages and 1500B MTUs.
  45. */
  46. #define FUN_XDP_HEADROOM 192
  47. /* Initialization state of a queue. */
  48. enum {
  49. FUN_QSTATE_DESTROYED, /* what queue? */
  50. FUN_QSTATE_INIT_SW, /* exists in SW, not on the device */
  51. FUN_QSTATE_INIT_FULL, /* exists both in SW and on device */
  52. };
  53. /* Initialization state of an interrupt. */
  54. enum {
  55. FUN_IRQ_INIT, /* initialized and in the XArray but inactive */
  56. FUN_IRQ_REQUESTED, /* request_irq() done */
  57. FUN_IRQ_ENABLED, /* processing enabled */
  58. FUN_IRQ_DISABLED, /* processing disabled */
  59. };
  60. struct bpf_prog;
  61. struct funeth_txq_stats { /* per Tx queue SW counters */
  62. u64 tx_pkts; /* # of Tx packets */
  63. u64 tx_bytes; /* total bytes of Tx packets */
  64. u64 tx_cso; /* # of packets with checksum offload */
  65. u64 tx_tso; /* # of non-encapsulated TSO super-packets */
  66. u64 tx_encap_tso; /* # of encapsulated TSO super-packets */
  67. u64 tx_uso; /* # of non-encapsulated UDP LSO super-packets */
  68. u64 tx_more; /* # of DBs elided due to xmit_more */
  69. u64 tx_nstops; /* # of times the queue has stopped */
  70. u64 tx_nrestarts; /* # of times the queue has restarted */
  71. u64 tx_map_err; /* # of packets dropped due to DMA mapping errors */
  72. u64 tx_xdp_full; /* # of XDP packets that could not be enqueued */
  73. u64 tx_tls_pkts; /* # of Tx TLS packets offloaded to HW */
  74. u64 tx_tls_bytes; /* Tx bytes of HW-handled TLS payload */
  75. u64 tx_tls_fallback; /* attempted Tx TLS offloads punted to SW */
  76. u64 tx_tls_drops; /* attempted Tx TLS offloads dropped */
  77. };
  78. struct funeth_tx_info { /* per Tx descriptor state */
  79. union {
  80. struct sk_buff *skb; /* associated packet (sk_buff path) */
  81. struct xdp_frame *xdpf; /* associated XDP frame (XDP path) */
  82. };
  83. };
  84. struct funeth_txq {
  85. /* RO cacheline of frequently accessed data */
  86. u32 mask; /* queue depth - 1 */
  87. u32 hw_qid; /* device ID of the queue */
  88. void *desc; /* base address of descriptor ring */
  89. struct funeth_tx_info *info;
  90. struct device *dma_dev; /* device for DMA mappings */
  91. volatile __be64 *hw_wb; /* HW write-back location */
  92. u32 __iomem *db; /* SQ doorbell register address */
  93. struct netdev_queue *ndq;
  94. dma_addr_t dma_addr; /* DMA address of descriptor ring */
  95. /* producer R/W cacheline */
  96. u16 qidx; /* queue index within net_device */
  97. u16 ethid;
  98. u32 prod_cnt; /* producer counter */
  99. struct funeth_txq_stats stats;
  100. /* shared R/W cacheline, primarily accessed by consumer */
  101. u32 irq_db_val; /* value written to IRQ doorbell */
  102. u32 cons_cnt; /* consumer (cleanup) counter */
  103. struct net_device *netdev;
  104. struct fun_irq *irq;
  105. int numa_node;
  106. u8 init_state; /* queue initialization state */
  107. struct u64_stats_sync syncp;
  108. };
  109. struct funeth_rxq_stats { /* per Rx queue SW counters */
  110. u64 rx_pkts; /* # of received packets, including SW drops */
  111. u64 rx_bytes; /* total size of received packets */
  112. u64 rx_cso; /* # of packets with checksum offload */
  113. u64 rx_bufs; /* total # of Rx buffers provided to device */
  114. u64 gro_pkts; /* # of GRO superpackets */
  115. u64 gro_merged; /* # of pkts merged into existing GRO superpackets */
  116. u64 rx_page_alloc; /* # of page allocations for Rx buffers */
  117. u64 rx_budget; /* NAPI iterations that exhausted their budget */
  118. u64 rx_mem_drops; /* # of packets dropped due to memory shortage */
  119. u64 rx_map_err; /* # of page DMA mapping errors */
  120. u64 xdp_drops; /* XDP_DROPped packets */
  121. u64 xdp_tx; /* successful XDP transmits */
  122. u64 xdp_redir; /* successful XDP redirects */
  123. u64 xdp_err; /* packets dropped due to XDP errors */
  124. };
  125. struct funeth_rxbuf { /* per Rx buffer state */
  126. struct page *page; /* associated page */
  127. dma_addr_t dma_addr; /* DMA address of page start */
  128. int pg_refs; /* page refs held by driver */
  129. int node; /* page node, or -1 if it is PF_MEMALLOC */
  130. };
  131. struct funeth_rx_cache { /* cache of DMA-mapped previously used buffers */
  132. struct funeth_rxbuf *bufs; /* base of Rx buffer state ring */
  133. unsigned int prod_cnt; /* producer counter */
  134. unsigned int cons_cnt; /* consumer counter */
  135. unsigned int mask; /* depth - 1 */
  136. };
  137. /* An Rx queue consists of a CQ and an SQ used to provide Rx buffers. */
  138. struct funeth_rxq {
  139. struct net_device *netdev;
  140. struct napi_struct *napi;
  141. struct device *dma_dev; /* device for DMA mappings */
  142. void *cqes; /* base of CQ descriptor ring */
  143. const void *next_cqe_info; /* fun_cqe_info of next CQE */
  144. u32 __iomem *cq_db; /* CQ doorbell register address */
  145. unsigned int cq_head; /* CQ head index */
  146. unsigned int cq_mask; /* CQ depth - 1 */
  147. u16 phase; /* CQ phase tag */
  148. u16 qidx; /* queue index within net_device */
  149. unsigned int irq_db_val; /* IRQ info for CQ doorbell */
  150. struct fun_eprq_rqbuf *rqes; /* base of RQ descriptor ring */
  151. struct funeth_rxbuf *bufs; /* base of Rx buffer state ring */
  152. struct funeth_rxbuf *cur_buf; /* currently active buffer */
  153. u32 __iomem *rq_db; /* RQ doorbell register address */
  154. unsigned int rq_cons; /* RQ consumer counter */
  155. unsigned int rq_mask; /* RQ depth - 1 */
  156. unsigned int buf_offset; /* offset of next pkt in head buffer */
  157. u8 xdp_flush; /* XDP flush types needed at NAPI end */
  158. u8 init_state; /* queue initialization state */
  159. u16 headroom; /* per packet headroom */
  160. unsigned int rq_cons_db; /* value of rq_cons at last RQ db */
  161. unsigned int rq_db_thres; /* # of new buffers needed to write RQ db */
  162. struct funeth_rxbuf spare_buf; /* spare for next buffer replacement */
  163. struct funeth_rx_cache cache; /* used buffer cache */
  164. struct bpf_prog *xdp_prog; /* optional XDP BPF program */
  165. struct funeth_rxq_stats stats;
  166. dma_addr_t cq_dma_addr; /* DMA address of CQE ring */
  167. dma_addr_t rq_dma_addr; /* DMA address of RQE ring */
  168. u16 irq_cnt;
  169. u32 hw_cqid; /* device ID of the queue's CQ */
  170. u32 hw_sqid; /* device ID of the queue's SQ */
  171. int numa_node;
  172. struct u64_stats_sync syncp;
  173. struct xdp_rxq_info xdp_rxq;
  174. };
  175. #define FUN_QSTAT_INC(q, counter) \
  176. do { \
  177. u64_stats_update_begin(&(q)->syncp); \
  178. (q)->stats.counter++; \
  179. u64_stats_update_end(&(q)->syncp); \
  180. } while (0)
  181. #define FUN_QSTAT_READ(q, seq, stats_copy) \
  182. do { \
  183. seq = u64_stats_fetch_begin(&(q)->syncp); \
  184. stats_copy = (q)->stats; \
  185. } while (u64_stats_fetch_retry(&(q)->syncp, (seq)))
  186. #define FUN_INT_NAME_LEN (IFNAMSIZ + 16)
  187. struct fun_irq {
  188. struct napi_struct napi;
  189. struct funeth_txq *txq;
  190. struct funeth_rxq *rxq;
  191. u8 state;
  192. u16 irq_idx; /* index of MSI-X interrupt */
  193. int irq; /* Linux IRQ vector */
  194. cpumask_t affinity_mask; /* IRQ affinity */
  195. struct irq_affinity_notify aff_notify;
  196. char name[FUN_INT_NAME_LEN];
  197. } ____cacheline_internodealigned_in_smp;
  198. /* Return the start address of the idx-th Tx descriptor. */
  199. static inline void *fun_tx_desc_addr(const struct funeth_txq *q,
  200. unsigned int idx)
  201. {
  202. return q->desc + idx * FUNETH_SQE_SIZE;
  203. }
  204. static inline void fun_txq_wr_db(const struct funeth_txq *q)
  205. {
  206. unsigned int tail = q->prod_cnt & q->mask;
  207. writel(tail, q->db);
  208. }
  209. static inline int fun_irq_node(const struct fun_irq *p)
  210. {
  211. return cpu_to_mem(cpumask_first(&p->affinity_mask));
  212. }
  213. int fun_rxq_napi_poll(struct napi_struct *napi, int budget);
  214. int fun_txq_napi_poll(struct napi_struct *napi, int budget);
  215. netdev_tx_t fun_start_xmit(struct sk_buff *skb, struct net_device *netdev);
  216. bool fun_xdp_tx(struct funeth_txq *q, struct xdp_frame *xdpf);
  217. int fun_xdp_xmit_frames(struct net_device *dev, int n,
  218. struct xdp_frame **frames, u32 flags);
  219. int funeth_txq_create(struct net_device *dev, unsigned int qidx,
  220. unsigned int ndesc, struct fun_irq *irq, int state,
  221. struct funeth_txq **qp);
  222. int fun_txq_create_dev(struct funeth_txq *q, struct fun_irq *irq);
  223. struct funeth_txq *funeth_txq_free(struct funeth_txq *q, int state);
  224. int funeth_rxq_create(struct net_device *dev, unsigned int qidx,
  225. unsigned int ncqe, unsigned int nrqe, struct fun_irq *irq,
  226. int state, struct funeth_rxq **qp);
  227. int fun_rxq_create_dev(struct funeth_rxq *q, struct fun_irq *irq);
  228. struct funeth_rxq *funeth_rxq_free(struct funeth_rxq *q, int state);
  229. int fun_rxq_set_bpf(struct funeth_rxq *q, struct bpf_prog *prog);
  230. #endif /* _FUNETH_TXRX_H */