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 /* To receive the worst case packet we need:
28 * 1 descriptor for primary metadata
29 * + 1 descriptor for optional metadata
30 * + 1 descriptor for headers
31 * + 4 descriptors for payload
32 */
33 #define FBNIC_MAX_RX_PKT_DESC 7
34 #define FBNIC_RX_DESC_MIN roundup_pow_of_two(FBNIC_MAX_RX_PKT_DESC * 2)
35
36 #define FBNIC_MAX_TXQS 128u
37 #define FBNIC_MAX_RXQS 128u
38
39 /* These apply to TWQs, TCQ, RCQ */
40 #define FBNIC_QUEUE_SIZE_MIN 16u
41 #define FBNIC_QUEUE_SIZE_MAX SZ_64K
42
43 #define FBNIC_TXQ_SIZE_DEFAULT 1024
44 #define FBNIC_HPQ_SIZE_DEFAULT 256
45 #define FBNIC_PPQ_SIZE_DEFAULT 256
46 #define FBNIC_RCQ_SIZE_DEFAULT 1024
47 #define FBNIC_TX_USECS_DEFAULT 35
48 #define FBNIC_RX_USECS_DEFAULT 30
49 #define FBNIC_RX_FRAMES_DEFAULT 0
50
51 #define FBNIC_RX_TROOM \
52 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
53 #define FBNIC_RX_HROOM \
54 (ALIGN(FBNIC_RX_TROOM + NET_SKB_PAD, 128) - FBNIC_RX_TROOM)
55 #define FBNIC_RX_PAD 0
56 #define FBNIC_RX_MAX_HDR (1536 - FBNIC_RX_PAD)
57 #define FBNIC_RX_PAYLD_OFFSET 0
58 #define FBNIC_RX_PAYLD_PG_CL 0
59
60 #define FBNIC_RING_F_DISABLED BIT(0)
61 #define FBNIC_RING_F_CTX BIT(1)
62 #define FBNIC_RING_F_STATS BIT(2) /* Ring's stats may be used */
63
64 struct fbnic_pkt_buff {
65 struct xdp_buff buff;
66 ktime_t hwtstamp;
67 u32 data_truesize;
68 u16 data_len;
69 u16 nr_frags;
70 };
71
72 struct fbnic_queue_stats {
73 u64 packets;
74 u64 bytes;
75 union {
76 struct {
77 u64 csum_partial;
78 u64 lso;
79 u64 ts_packets;
80 u64 ts_lost;
81 u64 stop;
82 u64 wake;
83 } twq;
84 struct {
85 u64 alloc_failed;
86 u64 csum_complete;
87 u64 csum_none;
88 } rx;
89 };
90 u64 dropped;
91 struct u64_stats_sync syncp;
92 };
93
94 #define FBNIC_PAGECNT_BIAS_MAX PAGE_SIZE
95
96 struct fbnic_rx_buf {
97 struct page *page;
98 long pagecnt_bias;
99 };
100
101 struct fbnic_ring {
102 /* Pointer to buffer specific info */
103 union {
104 struct fbnic_pkt_buff *pkt; /* RCQ */
105 struct fbnic_rx_buf *rx_buf; /* BDQ */
106 void **tx_buf; /* TWQ */
107 void *buffer; /* Generic pointer */
108 };
109
110 u32 __iomem *doorbell; /* Pointer to CSR space for ring */
111 __le64 *desc; /* Descriptor ring memory */
112 u16 size_mask; /* Size of ring in descriptors - 1 */
113 u8 q_idx; /* Logical netdev ring index */
114 u8 flags; /* Ring flags (FBNIC_RING_F_*) */
115
116 u32 head, tail; /* Head/Tail of ring */
117
118 struct fbnic_queue_stats stats;
119
120 /* Slow path fields follow */
121 dma_addr_t dma; /* Phys addr of descriptor memory */
122 size_t size; /* Size of descriptor ring in memory */
123 };
124
125 struct fbnic_q_triad {
126 struct fbnic_ring sub0, sub1, cmpl;
127 };
128
129 struct fbnic_napi_vector {
130 struct napi_struct napi;
131 struct device *dev; /* Device for DMA unmapping */
132 struct page_pool *page_pool;
133 struct fbnic_dev *fbd;
134
135 u16 v_idx;
136 u8 txt_count;
137 u8 rxt_count;
138
139 struct fbnic_q_triad qt[];
140 };
141
142 netdev_tx_t fbnic_xmit_frame(struct sk_buff *skb, struct net_device *dev);
143 netdev_features_t
144 fbnic_features_check(struct sk_buff *skb, struct net_device *dev,
145 netdev_features_t features);
146
147 void fbnic_aggregate_ring_rx_counters(struct fbnic_net *fbn,
148 struct fbnic_ring *rxr);
149 void fbnic_aggregate_ring_tx_counters(struct fbnic_net *fbn,
150 struct fbnic_ring *txr);
151
152 int fbnic_alloc_napi_vectors(struct fbnic_net *fbn);
153 void fbnic_free_napi_vectors(struct fbnic_net *fbn);
154 int fbnic_alloc_resources(struct fbnic_net *fbn);
155 void fbnic_free_resources(struct fbnic_net *fbn);
156 int fbnic_set_netif_queues(struct fbnic_net *fbn);
157 void fbnic_reset_netif_queues(struct fbnic_net *fbn);
158 irqreturn_t fbnic_msix_clean_rings(int irq, void *data);
159 void fbnic_napi_enable(struct fbnic_net *fbn);
160 void fbnic_napi_disable(struct fbnic_net *fbn);
161 void fbnic_enable(struct fbnic_net *fbn);
162 void fbnic_disable(struct fbnic_net *fbn);
163 void fbnic_flush(struct fbnic_net *fbn);
164 void fbnic_fill(struct fbnic_net *fbn);
165
166 void fbnic_napi_depletion_check(struct net_device *netdev);
167 int fbnic_wait_all_queues_idle(struct fbnic_dev *fbd, bool may_fail);
168
fbnic_napi_idx(const struct fbnic_napi_vector * nv)169 static inline int fbnic_napi_idx(const struct fbnic_napi_vector *nv)
170 {
171 return nv->v_idx - FBNIC_NON_NAPI_VECTORS;
172 }
173
174 #endif /* _FBNIC_TXRX_H_ */
175