xref: /linux/drivers/net/ethernet/freescale/enetc/enetc.h (revision c6c223fd06edbc34c7b04170e375abfb2a13cbf8)
1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /* Copyright 2017-2019 NXP */
3 
4 #include <linux/timer.h>
5 #include <linux/pci.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/skbuff.h>
10 #include <linux/ethtool.h>
11 #include <linux/fsl/ntmp.h>
12 #include <linux/if_vlan.h>
13 #include <linux/phylink.h>
14 #include <linux/dim.h>
15 #include <net/xdp.h>
16 
17 #include "enetc_hw.h"
18 #include "enetc4_hw.h"
19 
20 #define ENETC_MAC_MAXFRM_SIZE	9600
21 #define ENETC_MAX_MTU		(ENETC_MAC_MAXFRM_SIZE - \
22 				(ETH_FCS_LEN + ETH_HLEN + VLAN_HLEN))
23 
24 #define ENETC_CBD_DATA_MEM_ALIGN 64
25 
26 #define ENETC_MADDR_HASH_TBL_SZ	64
27 
28 enum enetc_mac_addr_type {UC, MC, MADDR_TYPE};
29 
30 struct enetc_mac_filter {
31 	union {
32 		char mac_addr[ETH_ALEN];
33 		DECLARE_BITMAP(mac_hash_table, ENETC_MADDR_HASH_TBL_SZ);
34 	};
35 	int mac_addr_cnt;
36 };
37 
38 struct enetc_tx_swbd {
39 	union {
40 		struct sk_buff *skb;
41 		struct xdp_frame *xdp_frame;
42 	};
43 	dma_addr_t dma;
44 	struct page *page;	/* valid only if is_xdp_tx */
45 	u16 page_offset;	/* valid only if is_xdp_tx */
46 	u16 len;
47 	enum dma_data_direction dir;
48 	u8 is_dma_page:1;
49 	u8 check_wb:1;
50 	u8 do_twostep_tstamp:1;
51 	u8 is_eof:1;
52 	u8 is_xdp_tx:1;
53 	u8 is_xdp_redirect:1;
54 	u8 qbv_en:1;
55 };
56 
57 struct enetc_skb_cb {
58 	u8 flag;
59 	bool udp;
60 	u16 correction_off;
61 	u16 origin_tstamp_off;
62 };
63 
64 #define ENETC_SKB_CB(skb) ((struct enetc_skb_cb *)((skb)->cb))
65 
66 struct enetc_lso_t {
67 	bool	ipv6;
68 	bool	tcp;
69 	u8	l3_hdr_len;
70 	u8	hdr_len; /* LSO header length */
71 	u8	l3_start;
72 	u16	lso_seg_size;
73 	int	total_len; /* total data length, not include LSO header */
74 };
75 
76 #define ENETC_LSO_MAX_DATA_LEN		SZ_256K
77 
78 #define ENETC_RX_MAXFRM_SIZE	ENETC_MAC_MAXFRM_SIZE
79 #define ENETC_RXB_TRUESIZE	(PAGE_SIZE >> 1)
80 #define ENETC_RXB_PAD		NET_SKB_PAD /* add extra space if needed */
81 #define ENETC_RXB_DMA_SIZE	\
82 	min(SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - ENETC_RXB_PAD, 0xffff)
83 #define ENETC_RXB_DMA_SIZE_XDP	\
84 	min(SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - XDP_PACKET_HEADROOM, 0xffff)
85 
86 struct enetc_rx_swbd {
87 	dma_addr_t dma;
88 	struct page *page;
89 	u16 page_offset;
90 	enum dma_data_direction dir;
91 	u16 len;
92 };
93 
94 /* ENETC overhead: optional extension BD + 1 BD gap */
95 #define ENETC_TXBDS_NEEDED(val)	((val) + 2)
96 /* For LS1028A, max # of chained Tx BDs is 15, including head and
97  * extension BD.
98  */
99 #define ENETC_MAX_SKB_FRAGS	13
100 /* For ENETC v4 and later versions, max # of chained Tx BDs is 63,
101  * including head and extension BD, but the range of MAX_SKB_FRAGS
102  * is 17 ~ 45, so set ENETC4_MAX_SKB_FRAGS to MAX_SKB_FRAGS.
103  */
104 #define ENETC4_MAX_SKB_FRAGS		MAX_SKB_FRAGS
105 #define ENETC_TXBDS_MAX_NEEDED(x)	ENETC_TXBDS_NEEDED((x) + 1)
106 
107 struct enetc_ring_stats {
108 	unsigned long packets;
109 	unsigned long bytes;
110 	unsigned long rx_alloc_errs;
111 	unsigned long xdp_drops;
112 	unsigned long xdp_tx;
113 	unsigned long xdp_tx_drops;
114 	unsigned long xdp_redirect;
115 	unsigned long xdp_redirect_failures;
116 	unsigned long recycles;
117 	unsigned long recycle_failures;
118 	unsigned long win_drop;
119 };
120 
121 struct enetc_xdp_data {
122 	struct xdp_rxq_info rxq;
123 	struct bpf_prog *prog;
124 	int xdp_tx_in_flight;
125 };
126 
127 #define ENETC_RX_RING_DEFAULT_SIZE	2048
128 #define ENETC_TX_RING_DEFAULT_SIZE	2048
129 #define ENETC_DEFAULT_TX_WORK		(ENETC_TX_RING_DEFAULT_SIZE / 2)
130 
131 struct enetc_bdr_resource {
132 	/* Input arguments saved for teardown */
133 	struct device *dev; /* for DMA mapping */
134 	size_t bd_count;
135 	size_t bd_size;
136 
137 	/* Resource proper */
138 	void *bd_base; /* points to Rx or Tx BD ring */
139 	dma_addr_t bd_dma_base;
140 	union {
141 		struct enetc_tx_swbd *tx_swbd;
142 		struct enetc_rx_swbd *rx_swbd;
143 	};
144 	char *tso_headers;
145 	dma_addr_t tso_headers_dma;
146 };
147 
148 struct enetc_bdr {
149 	struct device *dev; /* for DMA mapping */
150 	struct net_device *ndev;
151 	void *bd_base; /* points to Rx or Tx BD ring */
152 	union {
153 		void __iomem *tpir;
154 		void __iomem *rcir;
155 	};
156 	u16 index;
157 	u16 prio;
158 	int bd_count; /* # of BDs */
159 	int next_to_use;
160 	int next_to_clean;
161 	union {
162 		struct enetc_tx_swbd *tx_swbd;
163 		struct enetc_rx_swbd *rx_swbd;
164 	};
165 	union {
166 		void __iomem *tcir; /* Tx */
167 		int next_to_alloc; /* Rx */
168 	};
169 	void __iomem *idr; /* Interrupt Detect Register pointer */
170 
171 	int buffer_offset;
172 	struct enetc_xdp_data xdp;
173 
174 	struct enetc_ring_stats stats;
175 
176 	dma_addr_t bd_dma_base;
177 	u8 tsd_enable; /* Time specific departure */
178 	bool ext_en; /* enable h/w descriptor extensions */
179 
180 	/* DMA buffer for TSO headers */
181 	char *tso_headers;
182 	dma_addr_t tso_headers_dma;
183 } ____cacheline_aligned_in_smp;
184 
185 static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i)
186 {
187 	if (unlikely(++*i == bdr->bd_count))
188 		*i = 0;
189 }
190 
191 static inline int enetc_bd_unused(struct enetc_bdr *bdr)
192 {
193 	if (bdr->next_to_clean > bdr->next_to_use)
194 		return bdr->next_to_clean - bdr->next_to_use - 1;
195 
196 	return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1;
197 }
198 
199 static inline int enetc_swbd_unused(struct enetc_bdr *bdr)
200 {
201 	if (bdr->next_to_clean > bdr->next_to_alloc)
202 		return bdr->next_to_clean - bdr->next_to_alloc - 1;
203 
204 	return bdr->bd_count + bdr->next_to_clean - bdr->next_to_alloc - 1;
205 }
206 
207 /* Control BD ring */
208 #define ENETC_CBDR_DEFAULT_SIZE	64
209 struct enetc_cbdr {
210 	void *bd_base; /* points to Rx or Tx BD ring */
211 	void __iomem *pir;
212 	void __iomem *cir;
213 	void __iomem *mr; /* mode register */
214 
215 	int bd_count; /* # of BDs */
216 	int next_to_use;
217 	int next_to_clean;
218 
219 	dma_addr_t bd_dma_base;
220 	struct device *dma_dev;
221 };
222 
223 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
224 
225 static inline union enetc_rx_bd *enetc_rxbd(struct enetc_bdr *rx_ring, int i)
226 {
227 	int hw_idx = i;
228 
229 	if (rx_ring->ext_en)
230 		hw_idx = 2 * i;
231 
232 	return &(((union enetc_rx_bd *)rx_ring->bd_base)[hw_idx]);
233 }
234 
235 static inline void enetc_rxbd_next(struct enetc_bdr *rx_ring,
236 				   union enetc_rx_bd **old_rxbd, int *old_index)
237 {
238 	union enetc_rx_bd *new_rxbd = *old_rxbd;
239 	int new_index = *old_index;
240 
241 	new_rxbd++;
242 
243 	if (rx_ring->ext_en)
244 		new_rxbd++;
245 
246 	if (unlikely(++new_index == rx_ring->bd_count)) {
247 		new_rxbd = rx_ring->bd_base;
248 		new_index = 0;
249 	}
250 
251 	*old_rxbd = new_rxbd;
252 	*old_index = new_index;
253 }
254 
255 static inline union enetc_rx_bd *enetc_rxbd_ext(union enetc_rx_bd *rxbd)
256 {
257 	return ++rxbd;
258 }
259 
260 struct enetc_msg_swbd {
261 	void *vaddr;
262 	dma_addr_t dma;
263 	int size;
264 };
265 
266 #define ENETC_REV1	0x1
267 #define ENETC_REV4	0x4
268 
269 enum enetc_errata {
270 	ENETC_ERR_VLAN_ISOL	= BIT(0),
271 	ENETC_ERR_UCMCSWP	= BIT(1),
272 };
273 
274 #define ENETC_SI_F_PSFP BIT(0)
275 #define ENETC_SI_F_QBV  BIT(1)
276 #define ENETC_SI_F_QBU  BIT(2)
277 #define ENETC_SI_F_LSO	BIT(3)
278 #define ENETC_SI_F_PPM	BIT(4) /* pseudo MAC */
279 
280 struct enetc_drvdata {
281 	u32 pmac_offset; /* Only valid for PSI which supports 802.1Qbu */
282 	u8 tx_csum:1;
283 	u8 max_frags;
284 	u64 sysclk_freq;
285 	const struct ethtool_ops *eth_ops;
286 };
287 
288 struct enetc_platform_info {
289 	u16 revision;
290 	u16 dev_id;
291 	const struct enetc_drvdata *data;
292 };
293 
294 struct enetc_si;
295 
296 /*
297  * This structure defines the some common hooks for ENETC PSI and VSI.
298  * In addition, since VSI only uses the struct enetc_si as its private
299  * driver data, so this structure also define some hooks specifically
300  * for VSI. For VSI-specific hooks, the format is ‘vf_*()’.
301  */
302 struct enetc_si_ops {
303 	int (*get_rss_table)(struct enetc_si *si, u32 *table, int count);
304 	int (*set_rss_table)(struct enetc_si *si, const u32 *table, int count);
305 };
306 
307 /* PCI IEP device data */
308 struct enetc_si {
309 	struct pci_dev *pdev;
310 	struct enetc_hw hw;
311 	enum enetc_errata errata;
312 
313 	struct net_device *ndev; /* back ref. */
314 
315 	union {
316 		struct enetc_cbdr cbd_ring; /* Only ENETC 1.0 */
317 		struct ntmp_user ntmp_user; /* ENETC 4.1 and later */
318 	};
319 
320 	int num_rx_rings; /* how many rings are available in the SI */
321 	int num_tx_rings;
322 	int num_fs_entries;
323 	int num_rss; /* number of RSS buckets */
324 	unsigned short pad;
325 	u16 revision;
326 	int hw_features;
327 	const struct enetc_drvdata *drvdata;
328 	const struct enetc_si_ops *ops;
329 
330 	struct workqueue_struct *workqueue;
331 	struct work_struct rx_mode_task;
332 	struct dentry *debugfs_root;
333 };
334 
335 #define ENETC_SI_ALIGN	32
336 
337 static inline bool is_enetc_rev1(struct enetc_si *si)
338 {
339 	return si->pdev->revision == ENETC_REV1;
340 }
341 
342 static inline void *enetc_si_priv(const struct enetc_si *si)
343 {
344 	return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN);
345 }
346 
347 static inline bool enetc_si_is_pf(struct enetc_si *si)
348 {
349 	return !!(si->hw.port);
350 }
351 
352 static inline int enetc_pf_to_port(struct pci_dev *pf_pdev)
353 {
354 	switch (pf_pdev->devfn) {
355 	case 0:
356 		return 0;
357 	case 1:
358 		return 1;
359 	case 2:
360 		return 2;
361 	case 6:
362 		return 3;
363 	default:
364 		return -1;
365 	}
366 }
367 
368 static inline bool enetc_is_pseudo_mac(struct enetc_si *si)
369 {
370 	return si->hw_features & ENETC_SI_F_PPM;
371 }
372 
373 #define ENETC_MAX_NUM_TXQS	8
374 #define ENETC_INT_NAME_MAX	(IFNAMSIZ + 8)
375 
376 struct enetc_int_vector {
377 	void __iomem *rbier;
378 	void __iomem *tbier_base;
379 	void __iomem *ricr1;
380 	unsigned long tx_rings_map;
381 	int count_tx_rings;
382 	u32 rx_ictt;
383 	u16 comp_cnt;
384 	bool rx_dim_en, rx_napi_work;
385 	struct napi_struct napi ____cacheline_aligned_in_smp;
386 	struct dim rx_dim ____cacheline_aligned_in_smp;
387 	char name[ENETC_INT_NAME_MAX];
388 
389 	struct enetc_bdr rx_ring;
390 	struct enetc_bdr tx_ring[] __counted_by(count_tx_rings);
391 } ____cacheline_aligned_in_smp;
392 
393 struct enetc_cls_rule {
394 	struct ethtool_rx_flow_spec fs;
395 	int used;
396 };
397 
398 #define ENETC_MAX_BDR_INT	6 /* fixed to max # of available cpus */
399 struct psfp_cap {
400 	u32 max_streamid;
401 	u32 max_psfp_filter;
402 	u32 max_psfp_gate;
403 	u32 max_psfp_gatelist;
404 	u32 max_psfp_meter;
405 };
406 
407 #define ENETC_F_TX_TSTAMP_MASK	0xff
408 enum enetc_active_offloads {
409 	/* 8 bits reserved for TX timestamp types (hwtstamp_tx_types) */
410 	ENETC_F_TX_TSTAMP		= BIT(0),
411 	ENETC_F_TX_ONESTEP_SYNC_TSTAMP	= BIT(1),
412 
413 	ENETC_F_RX_TSTAMP		= BIT(8),
414 	ENETC_F_QBV			= BIT(9),
415 	ENETC_F_QCI			= BIT(10),
416 	ENETC_F_QBU			= BIT(11),
417 	ENETC_F_TXCSUM			= BIT(12),
418 	ENETC_F_LSO			= BIT(13),
419 };
420 
421 enum enetc_flags_bit {
422 	ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS = 0,
423 	ENETC_TX_DOWN,
424 };
425 
426 /* interrupt coalescing modes */
427 enum enetc_ic_mode {
428 	/* one interrupt per frame */
429 	ENETC_IC_NONE = 0,
430 	/* activated when int coalescing time is set to a non-0 value */
431 	ENETC_IC_RX_MANUAL = BIT(0),
432 	ENETC_IC_TX_MANUAL = BIT(1),
433 	/* use dynamic interrupt moderation */
434 	ENETC_IC_RX_ADAPTIVE = BIT(2),
435 };
436 
437 #define ENETC_RXIC_PKTTHR	min_t(u32, 256, ENETC_RX_RING_DEFAULT_SIZE / 2)
438 #define ENETC_TXIC_PKTTHR	min_t(u32, 128, ENETC_TX_RING_DEFAULT_SIZE / 2)
439 
440 struct enetc_ndev_priv {
441 	struct net_device *ndev;
442 	struct device *dev; /* dma-mapping device */
443 	struct enetc_si *si;
444 
445 	int bdr_int_num; /* number of Rx/Tx ring interrupts */
446 	struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT];
447 	u16 num_rx_rings, num_tx_rings;
448 	u16 rx_bd_count, tx_bd_count;
449 
450 	u16 msg_enable;
451 
452 	u8 preemptible_tcs;
453 	u8 max_frags; /* The maximum number of BDs for fragments */
454 
455 	enum enetc_active_offloads active_offloads;
456 
457 	u32 speed; /* store speed for compare update pspeed */
458 
459 	struct enetc_bdr **xdp_tx_ring;
460 	struct enetc_bdr *tx_ring[16];
461 	struct enetc_bdr *rx_ring[16];
462 	const struct enetc_bdr_resource *tx_res;
463 	const struct enetc_bdr_resource *rx_res;
464 
465 	struct enetc_cls_rule *cls_rules;
466 
467 	struct psfp_cap psfp_cap;
468 
469 	/* Minimum number of TX queues required by the network stack */
470 	unsigned int min_num_stack_tx_queues;
471 
472 	struct phylink *phylink;
473 	int ic_mode;
474 	u32 tx_ictt;
475 
476 	struct bpf_prog *xdp_prog;
477 
478 	unsigned long flags;
479 
480 	struct work_struct	tx_onestep_tstamp;
481 	struct sk_buff_head	tx_skbs;
482 
483 	/* Serialize access to MAC Merge state between ethtool requests
484 	 * and link state updates
485 	 */
486 	struct mutex		mm_lock;
487 
488 	struct clk *ref_clk; /* RGMII/RMII reference clock */
489 	u64 sysclk_freq; /* NETC system clock frequency */
490 };
491 
492 /* Messaging */
493 
494 /* VF-PF set primary MAC address message format */
495 struct enetc_msg_cmd_set_primary_mac {
496 	struct enetc_msg_cmd_header header;
497 	struct sockaddr mac;
498 };
499 
500 #define ENETC_CBD(R, i)	(&(((struct enetc_cbd *)((R).bd_base))[i]))
501 
502 #define ENETC_CBDR_TIMEOUT	1000 /* usecs */
503 
504 /* SI common */
505 u32 enetc_port_mac_rd(struct enetc_si *si, u32 reg);
506 void enetc_port_mac_wr(struct enetc_si *si, u32 reg, u32 val);
507 int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv);
508 void enetc_pci_remove(struct pci_dev *pdev);
509 int enetc_alloc_msix(struct enetc_ndev_priv *priv);
510 void enetc_free_msix(struct enetc_ndev_priv *priv);
511 void enetc_get_si_caps(struct enetc_si *si);
512 void enetc_init_si_rings_params(struct enetc_ndev_priv *priv);
513 int enetc_alloc_si_resources(struct enetc_ndev_priv *priv);
514 void enetc_free_si_resources(struct enetc_ndev_priv *priv);
515 int enetc_configure_si(struct enetc_ndev_priv *priv);
516 int enetc_get_driver_data(struct enetc_si *si);
517 void enetc_add_mac_addr_ht_filter(struct enetc_mac_filter *filter,
518 				  const unsigned char *addr);
519 void enetc_reset_mac_addr_filter(struct enetc_mac_filter *filter);
520 
521 int enetc_open(struct net_device *ndev);
522 int enetc_close(struct net_device *ndev);
523 void enetc_start(struct net_device *ndev);
524 void enetc_stop(struct net_device *ndev);
525 netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev);
526 struct net_device_stats *enetc_get_stats(struct net_device *ndev);
527 void enetc_set_features(struct net_device *ndev, netdev_features_t features);
528 int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd);
529 int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data);
530 void enetc_reset_tc_mqprio(struct net_device *ndev);
531 int enetc_setup_bpf(struct net_device *ndev, struct netdev_bpf *bpf);
532 int enetc_xdp_xmit(struct net_device *ndev, int num_frames,
533 		   struct xdp_frame **frames, u32 flags);
534 
535 int enetc_hwtstamp_get(struct net_device *ndev,
536 		       struct kernel_hwtstamp_config *config);
537 int enetc_hwtstamp_set(struct net_device *ndev,
538 		       struct kernel_hwtstamp_config *config,
539 		       struct netlink_ext_ack *extack);
540 
541 /* ethtool */
542 extern const struct ethtool_ops enetc_pf_ethtool_ops;
543 extern const struct ethtool_ops enetc4_pf_ethtool_ops;
544 extern const struct ethtool_ops enetc_vf_ethtool_ops;
545 extern const struct ethtool_ops enetc4_ppm_ethtool_ops;
546 
547 void enetc_set_ethtool_ops(struct net_device *ndev);
548 void enetc_mm_link_state_update(struct enetc_ndev_priv *priv, bool link);
549 void enetc_mm_commit_preemptible_tcs(struct enetc_ndev_priv *priv);
550 
551 /* control buffer descriptor ring (CBDR) */
552 int enetc_setup_cbdr(struct device *dev, struct enetc_hw *hw, int bd_count,
553 		     struct enetc_cbdr *cbdr);
554 void enetc_teardown_cbdr(struct enetc_cbdr *cbdr);
555 int enetc4_setup_cbdr(struct enetc_si *si);
556 void enetc4_teardown_cbdr(struct enetc_si *si);
557 int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
558 			    char *mac_addr, int si_map);
559 int enetc_clear_mac_flt_entry(struct enetc_si *si, int index);
560 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
561 		       int index);
562 void enetc_set_rss_key(struct enetc_si *si, const u8 *bytes);
563 int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
564 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
565 int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);
566 int enetc4_get_rss_table(struct enetc_si *si, u32 *table, int count);
567 int enetc4_set_rss_table(struct enetc_si *si, const u32 *table, int count);
568 
569 static inline void *enetc_cbd_alloc_data_mem(struct enetc_si *si,
570 					     struct enetc_cbd *cbd,
571 					     int size, dma_addr_t *dma,
572 					     void **data_align)
573 {
574 	struct enetc_cbdr *ring = &si->cbd_ring;
575 	dma_addr_t dma_align;
576 	void *data;
577 
578 	data = dma_alloc_coherent(ring->dma_dev,
579 				  size + ENETC_CBD_DATA_MEM_ALIGN,
580 				  dma, GFP_KERNEL);
581 	if (!data) {
582 		dev_err(ring->dma_dev, "CBD alloc data memory failed!\n");
583 		return NULL;
584 	}
585 
586 	dma_align = ALIGN(*dma, ENETC_CBD_DATA_MEM_ALIGN);
587 	*data_align = PTR_ALIGN(data, ENETC_CBD_DATA_MEM_ALIGN);
588 
589 	cbd->addr[0] = cpu_to_le32(lower_32_bits(dma_align));
590 	cbd->addr[1] = cpu_to_le32(upper_32_bits(dma_align));
591 	cbd->length = cpu_to_le16(size);
592 
593 	return data;
594 }
595 
596 static inline void enetc_cbd_free_data_mem(struct enetc_si *si, int size,
597 					   void *data, dma_addr_t *dma)
598 {
599 	struct enetc_cbdr *ring = &si->cbd_ring;
600 
601 	dma_free_coherent(ring->dma_dev, size + ENETC_CBD_DATA_MEM_ALIGN,
602 			  data, *dma);
603 }
604 
605 void enetc_reset_ptcmsdur(struct enetc_hw *hw);
606 void enetc_set_ptcmsdur(struct enetc_hw *hw, u32 *queue_max_sdu);
607 
608 static inline bool enetc_ptp_clock_is_enabled(struct enetc_si *si)
609 {
610 	if (is_enetc_rev1(si))
611 		return IS_ENABLED(CONFIG_FSL_ENETC_PTP_CLOCK);
612 
613 	return IS_ENABLED(CONFIG_PTP_NETC_V4_TIMER);
614 }
615 
616 #ifdef CONFIG_FSL_ENETC_QOS
617 int enetc_qos_query_caps(struct net_device *ndev, void *type_data);
618 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
619 void enetc_sched_speed_set(struct enetc_ndev_priv *priv, int speed);
620 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data);
621 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data);
622 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
623 			    void *cb_priv);
624 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data);
625 int enetc_psfp_init(struct enetc_ndev_priv *priv);
626 int enetc_psfp_clean(struct enetc_ndev_priv *priv);
627 int enetc_set_psfp(struct net_device *ndev, bool en);
628 
629 static inline void enetc_get_max_cap(struct enetc_ndev_priv *priv)
630 {
631 	struct enetc_hw *hw = &priv->si->hw;
632 	u32 reg;
633 
634 	reg = enetc_port_rd(hw, ENETC_PSIDCAPR);
635 	priv->psfp_cap.max_streamid = reg & ENETC_PSIDCAPR_MSK;
636 	/* Port stream filter capability */
637 	reg = enetc_port_rd(hw, ENETC_PSFCAPR);
638 	priv->psfp_cap.max_psfp_filter = reg & ENETC_PSFCAPR_MSK;
639 	/* Port stream gate capability */
640 	reg = enetc_port_rd(hw, ENETC_PSGCAPR);
641 	priv->psfp_cap.max_psfp_gate = (reg & ENETC_PSGCAPR_SGIT_MSK);
642 	priv->psfp_cap.max_psfp_gatelist = (reg & ENETC_PSGCAPR_GCL_MSK) >> 16;
643 	/* Port flow meter capability */
644 	reg = enetc_port_rd(hw, ENETC_PFMCAPR);
645 	priv->psfp_cap.max_psfp_meter = reg & ENETC_PFMCAPR_MSK;
646 }
647 
648 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
649 {
650 	struct enetc_hw *hw = &priv->si->hw;
651 	int err;
652 
653 	enetc_get_max_cap(priv);
654 
655 	err = enetc_psfp_init(priv);
656 	if (err)
657 		return err;
658 
659 	enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) |
660 		 ENETC_PPSFPMR_PSFPEN | ENETC_PPSFPMR_VS |
661 		 ENETC_PPSFPMR_PVC | ENETC_PPSFPMR_PVZC);
662 
663 	return 0;
664 }
665 
666 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
667 {
668 	struct enetc_hw *hw = &priv->si->hw;
669 	int err;
670 
671 	err = enetc_psfp_clean(priv);
672 	if (err)
673 		return err;
674 
675 	enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) &
676 		 ~ENETC_PPSFPMR_PSFPEN & ~ENETC_PPSFPMR_VS &
677 		 ~ENETC_PPSFPMR_PVC & ~ENETC_PPSFPMR_PVZC);
678 
679 	memset(&priv->psfp_cap, 0, sizeof(struct psfp_cap));
680 
681 	return 0;
682 }
683 
684 #else
685 #define enetc_qos_query_caps(ndev, type_data) -EOPNOTSUPP
686 #define enetc_setup_tc_taprio(ndev, type_data) -EOPNOTSUPP
687 #define enetc_sched_speed_set(priv, speed) (void)0
688 #define enetc_setup_tc_cbs(ndev, type_data) -EOPNOTSUPP
689 #define enetc_setup_tc_txtime(ndev, type_data) -EOPNOTSUPP
690 #define enetc_setup_tc_psfp(ndev, type_data) -EOPNOTSUPP
691 #define enetc_setup_tc_block_cb NULL
692 
693 #define enetc_get_max_cap(p)		\
694 	memset(&((p)->psfp_cap), 0, sizeof(struct psfp_cap))
695 
696 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
697 {
698 	return 0;
699 }
700 
701 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
702 {
703 	return 0;
704 }
705 
706 static inline int enetc_set_psfp(struct net_device *ndev, bool en)
707 {
708 	return 0;
709 }
710 #endif
711