xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_txrx.h (revision 1cc3462159babb69c84c39cb1b4e262aef3ea325)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3 
4 #ifndef _FBNIC_TXRX_H_
5 #define _FBNIC_TXRX_H_
6 
7 #include <linux/netdevice.h>
8 #include <linux/skbuff.h>
9 #include <linux/types.h>
10 #include <linux/u64_stats_sync.h>
11 #include <net/xdp.h>
12 
13 struct fbnic_net;
14 
15 /* Guarantee we have space needed for storing the buffer
16  * To store the buffer we need:
17  *	1 descriptor per page
18  *	+ 1 descriptor for skb head
19  *	+ 2 descriptors for metadata and optional metadata
20  *	+ 7 descriptors to keep tail out of the same cacheline as head
21  * If we cannot guarantee that then we should return TX_BUSY
22  */
23 #define FBNIC_MAX_SKB_DESC	(MAX_SKB_FRAGS + 10)
24 #define FBNIC_TX_DESC_WAKEUP	(FBNIC_MAX_SKB_DESC * 2)
25 #define FBNIC_TX_DESC_MIN	roundup_pow_of_two(FBNIC_TX_DESC_WAKEUP)
26 
27 #define FBNIC_MAX_TXQS			128u
28 #define FBNIC_MAX_RXQS			128u
29 
30 #define FBNIC_TXQ_SIZE_DEFAULT		1024
31 #define FBNIC_HPQ_SIZE_DEFAULT		256
32 #define FBNIC_PPQ_SIZE_DEFAULT		256
33 #define FBNIC_RCQ_SIZE_DEFAULT		1024
34 #define FBNIC_TX_USECS_DEFAULT		35
35 #define FBNIC_RX_USECS_DEFAULT		30
36 #define FBNIC_RX_FRAMES_DEFAULT		0
37 
38 #define FBNIC_RX_TROOM \
39 	SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
40 #define FBNIC_RX_HROOM \
41 	(ALIGN(FBNIC_RX_TROOM + NET_SKB_PAD, 128) - FBNIC_RX_TROOM)
42 #define FBNIC_RX_PAD			0
43 #define FBNIC_RX_MAX_HDR		(1536 - FBNIC_RX_PAD)
44 #define FBNIC_RX_PAYLD_OFFSET		0
45 #define FBNIC_RX_PAYLD_PG_CL		0
46 
47 #define FBNIC_RING_F_DISABLED		BIT(0)
48 #define FBNIC_RING_F_CTX		BIT(1)
49 #define FBNIC_RING_F_STATS		BIT(2)	/* Ring's stats may be used */
50 
51 struct fbnic_pkt_buff {
52 	struct xdp_buff buff;
53 	ktime_t hwtstamp;
54 	u32 data_truesize;
55 	u16 data_len;
56 	u16 nr_frags;
57 };
58 
59 struct fbnic_queue_stats {
60 	u64 packets;
61 	u64 bytes;
62 	union {
63 		struct {
64 			u64 csum_partial;
65 			u64 lso;
66 			u64 ts_packets;
67 			u64 ts_lost;
68 			u64 stop;
69 			u64 wake;
70 		} twq;
71 		struct {
72 			u64 alloc_failed;
73 			u64 csum_complete;
74 			u64 csum_none;
75 		} rx;
76 	};
77 	u64 dropped;
78 	struct u64_stats_sync syncp;
79 };
80 
81 /* Pagecnt bias is long max to reserve the last bit to catch overflow
82  * cases where if we overcharge the bias it will flip over to be negative.
83  */
84 #define PAGECNT_BIAS_MAX	LONG_MAX
85 struct fbnic_rx_buf {
86 	struct page *page;
87 	long pagecnt_bias;
88 };
89 
90 struct fbnic_ring {
91 	/* Pointer to buffer specific info */
92 	union {
93 		struct fbnic_pkt_buff *pkt;	/* RCQ */
94 		struct fbnic_rx_buf *rx_buf;	/* BDQ */
95 		void **tx_buf;			/* TWQ */
96 		void *buffer;			/* Generic pointer */
97 	};
98 
99 	u32 __iomem *doorbell;		/* Pointer to CSR space for ring */
100 	__le64 *desc;			/* Descriptor ring memory */
101 	u16 size_mask;			/* Size of ring in descriptors - 1 */
102 	u8 q_idx;			/* Logical netdev ring index */
103 	u8 flags;			/* Ring flags (FBNIC_RING_F_*) */
104 
105 	u32 head, tail;			/* Head/Tail of ring */
106 
107 	struct fbnic_queue_stats stats;
108 
109 	/* Slow path fields follow */
110 	dma_addr_t dma;			/* Phys addr of descriptor memory */
111 	size_t size;			/* Size of descriptor ring in memory */
112 };
113 
114 struct fbnic_q_triad {
115 	struct fbnic_ring sub0, sub1, cmpl;
116 };
117 
118 struct fbnic_napi_vector {
119 	struct napi_struct napi;
120 	struct device *dev;		/* Device for DMA unmapping */
121 	struct page_pool *page_pool;
122 	struct fbnic_dev *fbd;
123 
124 	u16 v_idx;
125 	u8 txt_count;
126 	u8 rxt_count;
127 
128 	struct fbnic_q_triad qt[];
129 };
130 
131 #define FBNIC_MAX_TXQS			128u
132 #define FBNIC_MAX_RXQS			128u
133 
134 netdev_tx_t fbnic_xmit_frame(struct sk_buff *skb, struct net_device *dev);
135 netdev_features_t
136 fbnic_features_check(struct sk_buff *skb, struct net_device *dev,
137 		     netdev_features_t features);
138 
139 void fbnic_aggregate_ring_rx_counters(struct fbnic_net *fbn,
140 				      struct fbnic_ring *rxr);
141 void fbnic_aggregate_ring_tx_counters(struct fbnic_net *fbn,
142 				      struct fbnic_ring *txr);
143 
144 int fbnic_alloc_napi_vectors(struct fbnic_net *fbn);
145 void fbnic_free_napi_vectors(struct fbnic_net *fbn);
146 int fbnic_alloc_resources(struct fbnic_net *fbn);
147 void fbnic_free_resources(struct fbnic_net *fbn);
148 int fbnic_set_netif_queues(struct fbnic_net *fbn);
149 void fbnic_reset_netif_queues(struct fbnic_net *fbn);
150 irqreturn_t fbnic_msix_clean_rings(int irq, void *data);
151 void fbnic_napi_enable(struct fbnic_net *fbn);
152 void fbnic_napi_disable(struct fbnic_net *fbn);
153 void fbnic_enable(struct fbnic_net *fbn);
154 void fbnic_disable(struct fbnic_net *fbn);
155 void fbnic_flush(struct fbnic_net *fbn);
156 void fbnic_fill(struct fbnic_net *fbn);
157 
158 void fbnic_napi_depletion_check(struct net_device *netdev);
159 int fbnic_wait_all_queues_idle(struct fbnic_dev *fbd, bool may_fail);
160 
161 static inline int fbnic_napi_idx(const struct fbnic_napi_vector *nv)
162 {
163 	return nv->v_idx - FBNIC_NON_NAPI_VECTORS;
164 }
165 
166 #endif /* _FBNIC_TXRX_H_ */
167