xref: /linux/drivers/net/ethernet/stmicro/stmmac/stmmac.h (revision 49bda4826843be0ef97a162009a29ea3a63f3935)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*******************************************************************************
3   Copyright (C) 2007-2009  STMicroelectronics Ltd
4 
5 
6   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
7 *******************************************************************************/
8 
9 #ifndef __STMMAC_H__
10 #define __STMMAC_H__
11 
12 #define STMMAC_RESOURCE_NAME   "stmmaceth"
13 
14 #include <linux/clk.h>
15 #include <linux/hrtimer.h>
16 #include <linux/if_vlan.h>
17 #include <linux/stmmac.h>
18 #include <linux/phylink.h>
19 #include <linux/pci.h>
20 #include "common.h"
21 #include <linux/ptp_clock_kernel.h>
22 #include <linux/net_tstamp.h>
23 #include <linux/reset.h>
24 #include <net/page_pool/types.h>
25 #include <net/xdp.h>
26 #include <uapi/linux/bpf.h>
27 
28 struct stmmac_pcs;
29 
30 struct stmmac_resources {
31 	void __iomem *addr;
32 	u8 mac[ETH_ALEN];
33 	int wol_irq;
34 	int irq;
35 	int sfty_irq;
36 	int sfty_ce_irq;
37 	int sfty_ue_irq;
38 	int rx_irq[MTL_MAX_RX_QUEUES];
39 	int tx_irq[MTL_MAX_TX_QUEUES];
40 };
41 
42 enum stmmac_txbuf_type {
43 	STMMAC_TXBUF_T_SKB,
44 	STMMAC_TXBUF_T_XDP_TX,
45 	STMMAC_TXBUF_T_XDP_NDO,
46 	STMMAC_TXBUF_T_XSK_TX,
47 };
48 
49 struct stmmac_tx_info {
50 	struct xsk_tx_metadata_compl xsk_meta;
51 	dma_addr_t buf;
52 	unsigned len;
53 	enum stmmac_txbuf_type buf_type;
54 	bool map_as_page;
55 	bool last_segment;
56 	bool is_jumbo;
57 };
58 
59 #define STMMAC_TBS_AVAIL	BIT(0)
60 #define STMMAC_TBS_EN		BIT(1)
61 
62 /* Frequently used values are kept adjacent for cache effect */
63 struct stmmac_tx_queue {
64 	u32 tx_count_frames;
65 	int tbs;
66 	struct hrtimer txtimer;
67 	u32 queue_index;
68 	struct stmmac_priv *priv_data;
69 	struct dma_extended_desc *dma_etx ____cacheline_aligned_in_smp;
70 	struct dma_edesc *dma_entx;
71 	struct dma_desc *dma_tx;
72 	union {
73 		struct sk_buff **tx_skbuff;
74 		struct xdp_frame **xdpf;
75 	};
76 	struct stmmac_tx_info *tx_skbuff_dma;
77 	struct xsk_buff_pool *xsk_pool;
78 	u32 xsk_frames_done;
79 	unsigned int cur_tx;
80 	unsigned int dirty_tx;
81 	dma_addr_t dma_tx_phy;
82 	u32 mss;
83 };
84 
85 struct stmmac_rx_buffer {
86 	union {
87 		struct {
88 			struct page *page;
89 			dma_addr_t addr;
90 			__u32 page_offset;
91 		};
92 		struct xdp_buff *xdp;
93 	};
94 	struct page *sec_page;
95 	dma_addr_t sec_addr;
96 };
97 
98 struct stmmac_xdp_buff {
99 	struct xdp_buff xdp;
100 	struct stmmac_priv *priv;
101 	struct dma_desc *desc;
102 	struct dma_desc *ndesc;
103 };
104 
105 struct stmmac_metadata_request {
106 	struct stmmac_priv *priv;
107 	struct dma_desc *tx_desc;
108 	bool *set_ic;
109 	struct dma_edesc *edesc;
110 	int tbs;
111 };
112 
113 struct stmmac_xsk_tx_complete {
114 	struct stmmac_priv *priv;
115 	struct dma_desc *desc;
116 };
117 
118 struct stmmac_rx_queue {
119 	u32 rx_count_frames;
120 	u32 queue_index;
121 	struct xdp_rxq_info xdp_rxq;
122 	struct xsk_buff_pool *xsk_pool;
123 	struct page_pool *page_pool;
124 	struct stmmac_rx_buffer *buf_pool;
125 	struct stmmac_priv *priv_data;
126 	struct dma_extended_desc *dma_erx;
127 	struct dma_desc *dma_rx ____cacheline_aligned_in_smp;
128 	unsigned int cur_rx;
129 	unsigned int dirty_rx;
130 	unsigned int buf_alloc_num;
131 	unsigned int napi_skb_frag_size;
132 	dma_addr_t dma_rx_phy;
133 	unsigned int state_saved;
134 	struct {
135 		struct sk_buff *skb;
136 		unsigned int len;
137 		unsigned int error;
138 	} state;
139 };
140 
141 struct stmmac_channel {
142 	struct napi_struct rx_napi ____cacheline_aligned_in_smp;
143 	struct napi_struct tx_napi ____cacheline_aligned_in_smp;
144 	struct napi_struct rxtx_napi ____cacheline_aligned_in_smp;
145 	struct stmmac_priv *priv_data;
146 	spinlock_t lock;
147 	u32 index;
148 };
149 
150 struct stmmac_fpe_cfg {
151 	struct ethtool_mmsv mmsv;
152 	const struct stmmac_fpe_reg *reg;
153 	u32 fpe_csr;	/* MAC_FPE_CTRL_STS reg cache */
154 };
155 
156 struct stmmac_tc_entry {
157 	bool in_use;
158 	bool in_hw;
159 	bool is_last;
160 	bool is_frag;
161 	void *frag_ptr;
162 	unsigned int table_pos;
163 	u32 handle;
164 	u32 prio;
165 	struct {
166 		u32 match_data;
167 		u32 match_en;
168 		u8 af:1;
169 		u8 rf:1;
170 		u8 im:1;
171 		u8 nc:1;
172 		u8 res1:4;
173 		u8 frame_offset;
174 		u8 ok_index;
175 		u8 dma_ch_no;
176 		u32 res2;
177 	} __packed val;
178 };
179 
180 #define STMMAC_PPS_MAX		4
181 struct stmmac_pps_cfg {
182 	struct timespec64 start;
183 	struct timespec64 period;
184 };
185 
186 struct stmmac_rss {
187 	int enable;
188 	u8 key[STMMAC_RSS_HASH_KEY_SIZE];
189 	u32 table[STMMAC_RSS_MAX_TABLE_SIZE];
190 };
191 
192 #define STMMAC_FLOW_ACTION_DROP		BIT(0)
193 struct stmmac_flow_entry {
194 	unsigned long cookie;
195 	unsigned long action;
196 	u8 ip_proto;
197 	int in_use;
198 	int idx;
199 	int is_l4;
200 };
201 
202 /* Rx Frame Steering */
203 enum stmmac_rfs_type {
204 	STMMAC_RFS_T_VLAN,
205 	STMMAC_RFS_T_LLDP,
206 	STMMAC_RFS_T_1588,
207 	STMMAC_RFS_T_MAX,
208 };
209 
210 struct stmmac_rfs_entry {
211 	unsigned long cookie;
212 	u16 etype;
213 	int in_use;
214 	int type;
215 	int tc;
216 };
217 
218 struct stmmac_dma_conf {
219 	unsigned int dma_buf_sz;
220 
221 	/* RX Queue */
222 	struct stmmac_rx_queue rx_queue[MTL_MAX_RX_QUEUES];
223 	unsigned int dma_rx_size;
224 
225 	/* TX Queue */
226 	struct stmmac_tx_queue tx_queue[MTL_MAX_TX_QUEUES];
227 	unsigned int dma_tx_size;
228 };
229 
230 #define EST_GCL         1024
231 struct stmmac_est {
232 	int enable;
233 	u32 btr_reserve[2];
234 	u32 btr_offset[2];
235 	u32 btr[2];
236 	u32 ctr[2];
237 	u32 ter;
238 	u32 gcl_unaligned[EST_GCL];
239 	u32 gcl[EST_GCL];
240 	u32 gcl_size;
241 	u32 max_sdu[MTL_MAX_TX_QUEUES];
242 };
243 
244 struct stmmac_msi {
245 	int sfty_ce_irq;
246 	int sfty_ue_irq;
247 	int rx_irq[MTL_MAX_RX_QUEUES];
248 	int tx_irq[MTL_MAX_TX_QUEUES];
249 
250 	/*irq name */
251 	char int_name_mac[IFNAMSIZ + 9];
252 	char int_name_wol[IFNAMSIZ + 9];
253 	char int_name_lpi[IFNAMSIZ + 9];
254 	char int_name_sfty[IFNAMSIZ + 10];
255 	char int_name_sfty_ce[IFNAMSIZ + 10];
256 	char int_name_sfty_ue[IFNAMSIZ + 10];
257 	char int_name_rx_irq[MTL_MAX_RX_QUEUES][IFNAMSIZ + 14];
258 	char int_name_tx_irq[MTL_MAX_TX_QUEUES][IFNAMSIZ + 18];
259 };
260 
261 struct stmmac_priv {
262 	/* Frequently used values are kept adjacent for cache effect */
263 	u32 tx_coal_frames[MTL_MAX_TX_QUEUES];
264 	u32 tx_coal_timer[MTL_MAX_TX_QUEUES];
265 	u32 rx_coal_frames[MTL_MAX_RX_QUEUES];
266 
267 	int hwts_tx_en;
268 	bool tx_path_in_lpi_mode;
269 	bool sph_active;
270 	bool sph_capable;
271 	u32 sarc_type;
272 	u32 rx_riwt[MTL_MAX_RX_QUEUES];
273 	int hwts_rx_en;
274 	bool tsfupdt_coarse;
275 
276 	void __iomem *ioaddr;
277 	struct net_device *dev;
278 	struct device *device;
279 	struct mac_device_info *hw;
280 	int (*hwif_quirks)(struct stmmac_priv *priv);
281 	struct mutex lock;
282 
283 	struct stmmac_dma_conf dma_conf;
284 
285 	/* Generic channel for NAPI */
286 	struct stmmac_channel channel[STMMAC_CH_MAX];
287 
288 	unsigned int pause_time;
289 	struct mii_bus *mii;
290 
291 	struct stmmac_pcs *integrated_pcs;
292 
293 	struct phylink_config phylink_config;
294 	struct phylink *phylink;
295 
296 	struct stmmac_extra_stats xstats ____cacheline_aligned_in_smp;
297 	struct stmmac_safety_stats sstats;
298 	struct plat_stmmacenet_data *plat;
299 	/* Protect est parameters */
300 	struct mutex est_lock;
301 	struct stmmac_est *est;
302 	struct dma_features dma_cap;
303 	struct stmmac_counters mmc;
304 	int hw_cap_support;
305 	int synopsys_id;
306 	u32 msg_enable;
307 	/* Our MAC Wake-on-Lan options */
308 	int wolopts;
309 	int wol_irq;
310 	u32 gmii_address_bus_config;
311 	struct timer_list eee_ctrl_timer;
312 	u32 tx_lpi_timer;
313 	bool tx_lpi_clk_stop;
314 	bool eee_enabled;
315 	bool eee_active;
316 	bool eee_sw_timer_en;
317 	bool legacy_serdes_is_powered;
318 	/* descriptor format:
319 	 *  when clear: struct dma_desc or for tx TBS struct dma_edesc
320 	 *  when set, struct dma_extended_desc
321 	 */
322 	bool extend_desc;
323 	/* chain_mode: requested descriptor mode */
324 	bool chain_mode;
325 	/* descriptor_mode: actual descriptor mode,
326 	 * see STMMAC_CHAIN_MODE or STMMAC_RING_MODE
327 	 */
328 	u8 descriptor_mode;
329 	struct kernel_hwtstamp_config tstamp_config;
330 	struct ptp_clock *ptp_clock;
331 	struct ptp_clock_info ptp_clock_ops;
332 	unsigned int default_addend;
333 	u32 sub_second_inc;
334 	u32 systime_flags;
335 	u32 adv_ts;
336 	int use_riwt;
337 	int irq_wake;
338 	rwlock_t ptp_lock;
339 	/* Protects auxiliary snapshot registers from concurrent access. */
340 	struct mutex aux_ts_lock;
341 	wait_queue_head_t tstamp_busy_wait;
342 
343 	void __iomem *mmcaddr;
344 	void __iomem *ptpaddr;
345 	void __iomem *estaddr;
346 	unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
347 	unsigned int num_double_vlans;
348 	int sfty_irq;
349 	struct stmmac_msi *msi;
350 
351 #ifdef CONFIG_DEBUG_FS
352 	struct dentry *dbgfs_dir;
353 #endif
354 
355 	unsigned long state;
356 	struct workqueue_struct *wq;
357 	struct work_struct service_task;
358 
359 	/* Frame Preemption feature (FPE) */
360 	struct stmmac_fpe_cfg fpe_cfg;
361 
362 	/* TC Handling */
363 	unsigned int tc_entries_max;
364 	unsigned int tc_off_max;
365 	struct stmmac_tc_entry *tc_entries;
366 	unsigned int flow_entries_max;
367 	struct stmmac_flow_entry *flow_entries;
368 	unsigned int rfs_entries_max[STMMAC_RFS_T_MAX];
369 	unsigned int rfs_entries_cnt[STMMAC_RFS_T_MAX];
370 	unsigned int rfs_entries_total;
371 	struct stmmac_rfs_entry *rfs_entries;
372 
373 	/* Pulse Per Second output */
374 	struct stmmac_pps_cfg pps[STMMAC_PPS_MAX];
375 
376 	/* Receive Side Scaling */
377 	struct stmmac_rss rss;
378 
379 	/* XDP BPF Program */
380 	unsigned long *af_xdp_zc_qps;
381 	struct bpf_prog *xdp_prog;
382 
383 	struct devlink *devlink;
384 };
385 
386 enum stmmac_state {
387 	STMMAC_DOWN,
388 	STMMAC_RESET_REQUESTED,
389 	STMMAC_RESETING,
390 	STMMAC_SERVICE_SCHED,
391 };
392 
393 extern const struct dev_pm_ops stmmac_simple_pm_ops;
394 
395 int stmmac_mdio_unregister(struct net_device *ndev);
396 int stmmac_mdio_register(struct net_device *ndev);
397 int stmmac_mdio_reset(struct mii_bus *mii);
398 void stmmac_mdio_lock(struct stmmac_priv *priv);
399 void stmmac_mdio_unlock(struct stmmac_priv *priv);
400 int stmmac_pcs_setup(struct net_device *ndev);
401 void stmmac_pcs_clean(struct net_device *ndev);
402 void stmmac_set_ethtool_ops(struct net_device *netdev);
403 
404 void stmmac_ptp_register(struct stmmac_priv *priv);
405 void stmmac_ptp_unregister(struct stmmac_priv *priv);
406 int stmmac_xdp_open(struct net_device *dev);
407 void stmmac_xdp_release(struct net_device *dev);
408 int stmmac_get_phy_intf_sel(phy_interface_t interface);
409 int stmmac_resume(struct device *dev);
410 int stmmac_suspend(struct device *dev);
411 void stmmac_dvr_remove(struct device *dev);
412 int stmmac_dvr_probe(struct device *device,
413 		     struct plat_stmmacenet_data *plat_dat,
414 		     struct stmmac_resources *res);
415 int stmmac_reinit_queues(struct net_device *dev, u8 rx_cnt, u8 tx_cnt);
416 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size);
417 int stmmac_set_clk_tx_rate(void *bsp_priv, struct clk *clk_tx_i,
418 			   phy_interface_t interface, int speed);
419 
420 struct plat_stmmacenet_data *stmmac_plat_dat_alloc(struct device *dev);
421 
stmmac_xdp_is_enabled(struct stmmac_priv * priv)422 static inline bool stmmac_xdp_is_enabled(struct stmmac_priv *priv)
423 {
424 	return !!priv->xdp_prog;
425 }
426 
427 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue);
428 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue);
429 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue);
430 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue);
431 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags);
432 struct timespec64 stmmac_calc_tas_basetime(ktime_t old_base_time,
433 					   ktime_t current_time,
434 					   u64 cycle_time);
435 
436 #if IS_ENABLED(CONFIG_STMMAC_SELFTESTS)
437 void stmmac_selftest_run(struct net_device *dev,
438 			 struct ethtool_test *etest, u64 *buf);
439 void stmmac_selftest_get_strings(struct stmmac_priv *priv, u8 *data);
440 int stmmac_selftest_get_count(struct stmmac_priv *priv);
441 #else
stmmac_selftest_run(struct net_device * dev,struct ethtool_test * etest,u64 * buf)442 static inline void stmmac_selftest_run(struct net_device *dev,
443 				       struct ethtool_test *etest, u64 *buf)
444 {
445 	/* Not enabled */
446 }
stmmac_selftest_get_strings(struct stmmac_priv * priv,u8 * data)447 static inline void stmmac_selftest_get_strings(struct stmmac_priv *priv,
448 					       u8 *data)
449 {
450 	/* Not enabled */
451 }
stmmac_selftest_get_count(struct stmmac_priv * priv)452 static inline int stmmac_selftest_get_count(struct stmmac_priv *priv)
453 {
454 	return -EOPNOTSUPP;
455 }
456 #endif /* CONFIG_STMMAC_SELFTESTS */
457 
458 #endif /* __STMMAC_H__ */
459