xref: /linux/drivers/net/ethernet/microchip/sparx5/sparx5_main.h (revision a52629f85a3a97f76a12b9cb97717a36318a708e)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Microchip Sparx5 Switch driver
3  *
4  * Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
5  */
6 
7 #ifndef __SPARX5_MAIN_H__
8 #define __SPARX5_MAIN_H__
9 
10 #include <linux/types.h>
11 #include <linux/phy/phy.h>
12 #include <linux/netdevice.h>
13 #include <linux/phy.h>
14 #include <linux/if_vlan.h>
15 #include <linux/bitmap.h>
16 #include <linux/phylink.h>
17 #include <linux/net_tstamp.h>
18 #include <linux/ptp_clock_kernel.h>
19 #include <linux/hrtimer.h>
20 #include <linux/debugfs.h>
21 #include <net/flow_offload.h>
22 
23 #include <fdma_api.h>
24 
25 #include "sparx5_main_regs.h"
26 
27 /* Target chip type */
28 enum spx5_target_chiptype {
29 	SPX5_TARGET_CT_7546       = 0x7546,  /* SparX-5-64  Enterprise */
30 	SPX5_TARGET_CT_7549       = 0x7549,  /* SparX-5-90  Enterprise */
31 	SPX5_TARGET_CT_7552       = 0x7552,  /* SparX-5-128 Enterprise */
32 	SPX5_TARGET_CT_7556       = 0x7556,  /* SparX-5-160 Enterprise */
33 	SPX5_TARGET_CT_7558       = 0x7558,  /* SparX-5-200 Enterprise */
34 	SPX5_TARGET_CT_7546TSN    = 0x0546,  /* SparX-5-64i Industrial */
35 	SPX5_TARGET_CT_7549TSN    = 0x0549,  /* SparX-5-90i Industrial */
36 	SPX5_TARGET_CT_7552TSN    = 0x0552,  /* SparX-5-128i Industrial */
37 	SPX5_TARGET_CT_7556TSN    = 0x0556,  /* SparX-5-160i Industrial */
38 	SPX5_TARGET_CT_7558TSN    = 0x0558,  /* SparX-5-200i Industrial */
39 	SPX5_TARGET_CT_LAN9694    = 0x9694,  /* lan969x-40 */
40 	SPX5_TARGET_CT_LAN9691VAO = 0x9691,  /* lan969x-40-VAO */
41 	SPX5_TARGET_CT_LAN9694TSN = 0x9695,  /* lan969x-40-TSN */
42 	SPX5_TARGET_CT_LAN9694RED = 0x969A,  /* lan969x-40-RED */
43 	SPX5_TARGET_CT_LAN9696    = 0x9696,  /* lan969x-60 */
44 	SPX5_TARGET_CT_LAN9692VAO = 0x9692,  /* lan969x-65-VAO */
45 	SPX5_TARGET_CT_LAN9696TSN = 0x9697,  /* lan969x-60-TSN */
46 	SPX5_TARGET_CT_LAN9696RED = 0x969B,  /* lan969x-60-RED */
47 	SPX5_TARGET_CT_LAN9698    = 0x9698,  /* lan969x-100 */
48 	SPX5_TARGET_CT_LAN9693VAO = 0x9693,  /* lan969x-100-VAO */
49 	SPX5_TARGET_CT_LAN9698TSN = 0x9699,  /* lan969x-100-TSN */
50 	SPX5_TARGET_CT_LAN9698RED = 0x969C,  /* lan969x-100-RED */
51 };
52 
53 enum sparx5_port_max_tags {
54 	SPX5_PORT_MAX_TAGS_NONE,  /* No extra tags allowed */
55 	SPX5_PORT_MAX_TAGS_ONE,   /* Single tag allowed */
56 	SPX5_PORT_MAX_TAGS_TWO    /* Single and double tag allowed */
57 };
58 
59 enum sparx5_vlan_port_type {
60 	SPX5_VLAN_PORT_TYPE_UNAWARE, /* VLAN unaware port */
61 	SPX5_VLAN_PORT_TYPE_C,       /* C-port */
62 	SPX5_VLAN_PORT_TYPE_S,       /* S-port */
63 	SPX5_VLAN_PORT_TYPE_S_CUSTOM /* S-port using custom type */
64 };
65 
66 /* This is used in calendar configuration */
67 enum sparx5_cal_bw {
68 	SPX5_CAL_SPEED_NONE = 0,
69 	SPX5_CAL_SPEED_1G   = 1,
70 	SPX5_CAL_SPEED_2G5  = 2,
71 	SPX5_CAL_SPEED_5G   = 3,
72 	SPX5_CAL_SPEED_10G  = 4,
73 	SPX5_CAL_SPEED_25G  = 5,
74 	SPX5_CAL_SPEED_0G5  = 6,
75 	SPX5_CAL_SPEED_12G5 = 7
76 };
77 
78 enum sparx5_feature {
79 	SPX5_FEATURE_PSFP = BIT(0),
80 	SPX5_FEATURE_PTP  = BIT(1),
81 };
82 
83 #define SPX5_PORTS             65
84 #define SPX5_PORTS_ALL         70 /* Total number of ports */
85 
86 #define SPX5_PORT_CPU_0        0 /* CPU Port 0 */
87 #define SPX5_PORT_CPU_1        1 /* CPU Port 1 */
88 #define SPX5_PORT_VD0          2 /* VD0/Port used for IPMC */
89 #define SPX5_PORT_VD1          3 /* VD1/Port used for AFI/OAM */
90 #define SPX5_PORT_VD2          4 /* VD2/Port used for IPinIP*/
91 
92 #define PGID_UC_FLOOD          0
93 #define PGID_MC_FLOOD          1
94 #define PGID_IPV4_MC_DATA      2
95 #define PGID_IPV4_MC_CTRL      3
96 #define PGID_IPV6_MC_DATA      4
97 #define PGID_IPV6_MC_CTRL      5
98 #define PGID_BCAST             6
99 #define PGID_CPU               7
100 #define PGID_MCAST_START       8
101 
102 #define PGID_TABLE_SIZE	       3290
103 
104 #define IFH_LEN                9 /* 36 bytes */
105 #define NULL_VID               0
106 #define SPX5_MACT_PULL_DELAY   (2 * HZ)
107 #define SPX5_STATS_CHECK_DELAY (1 * HZ)
108 #define SPX5_PRIOS             8     /* Number of priority queues */
109 #define SPX5_BUFFER_CELL_SZ    184   /* Cell size  */
110 #define SPX5_BUFFER_MEMORY     4194280 /* 22795 words * 184 bytes */
111 #define SPX5_TAS_ENTRIES_PER_PORT 2 /* TAS lists per port */
112 
113 #define XTR_QUEUE     0
114 #define INJ_QUEUE     0
115 
116 #define FDMA_XTR_CHANNEL		6
117 #define FDMA_INJ_CHANNEL		0
118 #define FDMA_DCB_MAX			64
119 #define FDMA_RX_DCB_MAX_DBS		15
120 #define FDMA_TX_DCB_MAX_DBS		1
121 
122 #define SPARX5_PHC_COUNT		3
123 #define SPARX5_PHC_PORT			0
124 
125 #define IFH_REW_OP_NOOP			0x0
126 #define IFH_REW_OP_ONE_STEP_PTP		0x3
127 #define IFH_REW_OP_TWO_STEP_PTP		0x4
128 
129 #define IFH_PDU_TYPE_NONE		0x0
130 #define IFH_PDU_TYPE_PTP		0x5
131 #define IFH_PDU_TYPE_IPV4_UDP_PTP	0x6
132 #define IFH_PDU_TYPE_IPV6_UDP_PTP	0x7
133 
134 #define SPX5_DSM_CAL_LEN               64
135 #define SPX5_DSM_CAL_MAX_DEVS_PER_TAXI 13
136 #define SPX5_DSM_CAL_EMPTY             0xFFFF
137 
138 #define SPARX5_MAX_PTP_ID	512
139 
140 struct sparx5;
141 
142 struct sparx5_calendar_data {
143 	u32 schedule[SPX5_DSM_CAL_LEN];
144 	u32 avg_dist[SPX5_DSM_CAL_MAX_DEVS_PER_TAXI];
145 	u32 taxi_ports[SPX5_DSM_CAL_MAX_DEVS_PER_TAXI];
146 	u32 taxi_speeds[SPX5_DSM_CAL_MAX_DEVS_PER_TAXI];
147 	u32 dev_slots[SPX5_DSM_CAL_MAX_DEVS_PER_TAXI];
148 	u32 new_slots[SPX5_DSM_CAL_LEN];
149 	u32 temp_sched[SPX5_DSM_CAL_LEN];
150 	u32 indices[SPX5_DSM_CAL_LEN];
151 	u32 short_list[SPX5_DSM_CAL_LEN];
152 	u32 long_list[SPX5_DSM_CAL_LEN];
153 };
154 
155 /* Frame DMA receive state:
156  * For each DB, there is a SKB, and the skb data pointer is mapped in
157  * the DB. Once a frame is received the skb is given to the upper layers
158  * and a new skb is added to the dcb.
159  * When the db_index reached FDMA_RX_DCB_MAX_DBS the DB is reused.
160  */
161 struct sparx5_rx {
162 	struct fdma fdma;
163 	struct page_pool *page_pool;
164 	union {
165 		struct sk_buff *skb[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
166 		struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
167 	};
168 	dma_addr_t dma;
169 	struct napi_struct napi;
170 	struct net_device *ndev;
171 	u64 packets;
172 	u8 page_order;
173 };
174 
175 /* Used to store information about TX buffers. */
176 struct sparx5_tx_buf {
177 	struct net_device *dev;
178 	struct sk_buff *skb;
179 	dma_addr_t dma_addr;
180 	bool used;
181 	bool ptp;
182 };
183 
184 /* Frame DMA transmit state:
185  * DCBs are chained using the DCBs nextptr field.
186  */
187 struct sparx5_tx {
188 	struct fdma fdma;
189 	struct sparx5_tx_buf *dbs;
190 	u64 packets;
191 	u64 dropped;
192 };
193 
194 struct sparx5_port_config {
195 	phy_interface_t portmode;
196 	u32 bandwidth;
197 	int speed;
198 	int duplex;
199 	enum phy_media media;
200 	bool inband;
201 	bool power_down;
202 	bool autoneg;
203 	bool serdes_reset;
204 	u32 pause;
205 	u32 pause_adv;
206 	phy_interface_t phy_mode;
207 	u32 sd_sgpio;
208 };
209 
210 struct sparx5_port {
211 	struct net_device *ndev;
212 	struct sparx5 *sparx5;
213 	struct device_node *of_node;
214 	struct phy *serdes;
215 	struct sparx5_port_config conf;
216 	struct phylink_config phylink_config;
217 	struct phylink *phylink;
218 	struct phylink_pcs phylink_pcs;
219 	struct flow_stats mirror_stats;
220 	u16 portno;
221 	/* Ingress default VLAN (pvid) */
222 	u16 pvid;
223 	/* Egress default VLAN (vid) */
224 	u16 vid;
225 	bool signd_internal;
226 	bool signd_active_high;
227 	bool signd_enable;
228 	bool flow_control;
229 	enum sparx5_port_max_tags max_vlan_tags;
230 	enum sparx5_vlan_port_type vlan_type;
231 	u32 custom_etype;
232 	bool vlan_aware;
233 	struct hrtimer inj_timer;
234 	/* ptp */
235 	u8 ptp_cmd;
236 	u16 ts_id;
237 	struct sk_buff_head tx_skbs;
238 	bool is_mrouter;
239 	struct list_head tc_templates; /* list of TC templates on this port */
240 	/* TAS GCL entries owned by each of the port's two TAS lists. */
241 	struct {
242 		int gcl_base;
243 		int gcl_count;
244 	} tas_lists[SPX5_TAS_ENTRIES_PER_PORT];
245 };
246 
247 enum sparx5_core_clockfreq {
248 	SPX5_CORE_CLOCK_DEFAULT,  /* Defaults to the highest supported frequency */
249 	SPX5_CORE_CLOCK_250MHZ,   /* 250MHZ core clock frequency */
250 	SPX5_CORE_CLOCK_328MHZ,   /* 328MHZ core clock frequency */
251 	SPX5_CORE_CLOCK_500MHZ,   /* 500MHZ core clock frequency */
252 	SPX5_CORE_CLOCK_625MHZ,   /* 625MHZ core clock frequency */
253 };
254 
255 struct sparx5_phc {
256 	struct ptp_clock *clock;
257 	struct ptp_clock_info info;
258 	struct kernel_hwtstamp_config hwtstamp_config;
259 	struct sparx5 *sparx5;
260 	u8 index;
261 };
262 
263 struct sparx5_skb_cb {
264 	u8 rew_op;
265 	u8 pdu_type;
266 	u8 pdu_w16_offset;
267 	u16 ts_id;
268 	unsigned long jiffies;
269 };
270 
271 struct sparx5_mdb_entry {
272 	struct list_head list;
273 	DECLARE_BITMAP(port_mask, SPX5_PORTS);
274 	unsigned char addr[ETH_ALEN];
275 	bool cpu_copy;
276 	u16 vid;
277 	u16 pgid_idx;
278 };
279 
280 struct sparx5_mall_mirror_entry {
281 	u32 idx;
282 	struct sparx5_port *port;
283 };
284 
285 struct sparx5_mall_entry {
286 	struct list_head list;
287 	struct sparx5_port *port;
288 	unsigned long cookie;
289 	enum flow_action_id type;
290 	bool ingress;
291 	union {
292 		struct sparx5_mall_mirror_entry mirror;
293 	};
294 };
295 
296 #define SPARX5_PTP_TIMEOUT		msecs_to_jiffies(10)
297 #define SPARX5_SKB_CB(skb) \
298 	((struct sparx5_skb_cb *)((skb)->cb))
299 
300 struct sparx5_regs {
301 	const unsigned int *tsize;
302 	const unsigned int *gaddr;
303 	const unsigned int *gcnt;
304 	const unsigned int *gsize;
305 	const unsigned int *raddr;
306 	const unsigned int *rcnt;
307 	const unsigned int *fpos;
308 	const unsigned int *fsize;
309 };
310 
311 struct sparx5_consts {
312 	u32 n_ports;             /* Number of front ports */
313 	u32 n_ports_all;         /* Number of front ports + internal ports */
314 	u32 n_hsch_l1_elems;     /* Number of HSCH layer 1 elements */
315 	u32 n_hsch_queues;       /* Number of HSCH queues */
316 	u32 n_lb_groups;         /* Number of leacky bucket groupd */
317 	u32 n_pgids;             /* Number of PGID's */
318 	u32 n_sio_clks;          /* Number of serial IO clocks */
319 	u32 n_own_upsids;        /* Number of own UPSID's */
320 	u32 n_auto_cals;         /* Number of auto calendars */
321 	u32 n_filters;           /* Number of PSFP filters */
322 	u32 n_gates;             /* Number of PSFP gates */
323 	u32 n_sdlbs;             /* Number of service dual leaky buckets */
324 	u32 n_dsm_cal_taxis;     /* Number of DSM calendar taxis */
325 	u32 n_tas_gcl;           /* Number of TAS gate control list entries */
326 	u32 n_tas_lists;         /* Number of TAS lists */
327 	u32 buf_size;            /* Amount of QLIM watermark memory */
328 	u32 qres_max_prio_idx;   /* Maximum QRES prio index */
329 	u32 qres_max_colour_idx; /* Maximum QRES colour index */
330 	u32 tod_pin;             /* PTP TOD pin */
331 	const struct sparx5_vcap_inst *vcaps_cfg;
332 	const struct vcap_info *vcaps;
333 	const struct vcap_statistics *vcap_stats;
334 };
335 
336 struct sparx5_ops {
337 	bool (*is_port_2g5)(int portno);
338 	bool (*is_port_5g)(int portno);
339 	bool (*is_port_10g)(int portno);
340 	bool (*is_port_25g)(int portno);
341 	bool (*is_port_rgmii)(int portno);
342 	u32  (*get_port_dev_index)(struct sparx5 *sparx5, int port);
343 	u32  (*get_port_dev_bit)(struct sparx5 *sparx5, int port);
344 	u32  (*get_hsch_max_group_rate)(int grp);
345 	struct sparx5_sdlb_group *(*get_sdlb_group)(int idx);
346 	int (*set_port_mux)(struct sparx5 *sparx5, struct sparx5_port *port,
347 			    struct sparx5_port_config *conf);
348 
349 	irqreturn_t (*ptp_irq_handler)(int irq, void *args);
350 	int (*dsm_calendar_calc)(struct sparx5 *sparx5, u32 taxi,
351 				 struct sparx5_calendar_data *data);
352 	int (*port_config_rgmii)(struct sparx5_port *port,
353 				 struct sparx5_port_config *conf);
354 	int (*fdma_init)(struct sparx5 *sparx5);
355 	int (*fdma_deinit)(struct sparx5 *sparx5);
356 	int (*fdma_poll)(struct napi_struct *napi, int weight);
357 	int (*fdma_xmit)(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
358 			 struct net_device *dev);
359 };
360 
361 struct sparx5_main_io_resource {
362 	enum sparx5_target id;
363 	phys_addr_t offset;
364 	int range;
365 };
366 
367 struct sparx5_match_data {
368 	const struct sparx5_regs *regs;
369 	const struct sparx5_consts *consts;
370 	const struct sparx5_ops *ops;
371 	const struct sparx5_main_io_resource *iomap;
372 	int ioranges;
373 	int iomap_size;
374 };
375 
376 struct sparx5 {
377 	struct platform_device *pdev;
378 	struct device *dev;
379 	u32 chip_id;
380 	enum spx5_target_chiptype target_ct;
381 	u32 features;
382 	void __iomem *regs[NUM_TARGETS];
383 	int port_count;
384 	struct mutex lock; /* MAC reg lock */
385 	struct mutex tas_lock; /* Time Aware Shaper lock */
386 	unsigned long *tas_gcl_bitmap; /* Allocated TAS GCL entries */
387 	/* port structures are in net device */
388 	struct sparx5_port *ports[SPX5_PORTS];
389 	enum sparx5_core_clockfreq coreclock;
390 	/* Statistics */
391 	u32 num_stats;
392 	u32 num_ethtool_stats;
393 	const char * const *stats_layout;
394 	u64 *stats;
395 	/* Workqueue for reading stats */
396 	struct mutex queue_stats_lock;
397 	struct delayed_work stats_work;
398 	struct workqueue_struct *stats_queue;
399 	/* Notifiers */
400 	struct notifier_block netdevice_nb;
401 	struct notifier_block switchdev_nb;
402 	struct notifier_block switchdev_blocking_nb;
403 	/* Switch state */
404 	u8 base_mac[ETH_ALEN];
405 	u8 base_mac_assign_type;
406 	/* Associated bridge device (when bridged) */
407 	struct net_device *hw_bridge_dev;
408 	/* Bridged interfaces */
409 	DECLARE_BITMAP(bridge_mask, SPX5_PORTS);
410 	DECLARE_BITMAP(bridge_fwd_mask, SPX5_PORTS);
411 	DECLARE_BITMAP(bridge_lrn_mask, SPX5_PORTS);
412 	DECLARE_BITMAP(vlan_mask[VLAN_N_VID], SPX5_PORTS);
413 	/* SW MAC table */
414 	struct list_head mact_entries;
415 	/* mac table list (mact_entries) mutex */
416 	struct mutex mact_lock;
417 	/* SW MDB table */
418 	struct list_head mdb_entries;
419 	/* mdb list mutex */
420 	struct mutex mdb_lock;
421 	struct delayed_work mact_work;
422 	struct workqueue_struct *mact_queue;
423 	/* Board specifics */
424 	bool sd_sgpio_remapping;
425 	/* Register based inj/xtr */
426 	int xtr_irq;
427 	/* Frame DMA */
428 	int fdma_irq;
429 	spinlock_t tx_lock; /* lock for frame transmission */
430 	struct sparx5_rx rx;
431 	struct sparx5_tx tx;
432 	/* PTP */
433 	bool ptp;
434 	struct sparx5_phc phc[SPARX5_PHC_COUNT];
435 	spinlock_t ptp_clock_lock; /* lock for phc */
436 	spinlock_t ptp_ts_id_lock; /* lock for ts_id */
437 	struct mutex ptp_lock; /* lock for ptp interface state */
438 	u16 ptp_skbs;
439 	int ptp_irq;
440 	/* VCAP */
441 	struct vcap_control *vcap_ctrl;
442 	/* PGID allocation map */
443 	u8 pgid_map[PGID_TABLE_SIZE];
444 	struct list_head mall_entries;
445 	/* Common root for debugfs */
446 	struct dentry *debugfs_root;
447 	const struct sparx5_match_data *data;
448 };
449 
450 /* sparx5_main.c */
451 bool is_sparx5(struct sparx5 *sparx5);
452 bool sparx5_has_feature(struct sparx5 *sparx5, enum sparx5_feature feature);
453 
454 /* sparx5_switchdev.c */
455 int sparx5_register_notifier_blocks(struct sparx5 *sparx5);
456 void sparx5_unregister_notifier_blocks(struct sparx5 *sparx5);
457 
458 /* sparx5_packet.c */
459 struct frame_info {
460 	int src_port;
461 	u32 timestamp;
462 };
463 
464 void sparx5_xtr_flush(struct sparx5 *sparx5, u8 grp);
465 void sparx5_ifh_parse(struct sparx5 *sparx5, u32 *ifh, struct frame_info *info);
466 irqreturn_t sparx5_xtr_handler(int irq, void *_priv);
467 netdev_tx_t sparx5_port_xmit_impl(struct sk_buff *skb, struct net_device *dev);
468 int sparx5_manual_injection_mode(struct sparx5 *sparx5);
469 void sparx5_port_inj_timer_setup(struct sparx5_port *port);
470 
471 /* sparx5_fdma.c */
472 int sparx5_fdma_init(struct sparx5 *sparx5);
473 int sparx5_fdma_deinit(struct sparx5 *sparx5);
474 int sparx5_fdma_start(struct sparx5 *sparx5);
475 int sparx5_fdma_stop(struct sparx5 *sparx5);
476 int sparx5_fdma_napi_callback(struct napi_struct *napi, int weight);
477 int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
478 		     struct net_device *dev);
479 irqreturn_t sparx5_fdma_handler(int irq, void *args);
480 void sparx5_fdma_reload(struct sparx5 *sparx5, struct fdma *fdma);
481 void sparx5_fdma_injection_mode(struct sparx5 *sparx5);
482 
483 /* sparx5_mactable.c */
484 int sparx5_mact_learn(struct sparx5 *sparx5, int port,
485 		      const unsigned char mac[ETH_ALEN], u16 vid);
486 bool sparx5_mact_getnext(struct sparx5 *sparx5,
487 			 unsigned char mac[ETH_ALEN], u16 *vid, u32 *pcfg2);
488 int sparx5_mact_find(struct sparx5 *sparx5,
489 		     const unsigned char mac[ETH_ALEN], u16 vid, u32 *pcfg2);
490 int sparx5_mact_forget(struct sparx5 *sparx5,
491 		       const unsigned char mac[ETH_ALEN], u16 vid);
492 int sparx5_add_mact_entry(struct sparx5 *sparx5,
493 			  struct net_device *dev,
494 			  u16 portno,
495 			  const unsigned char *addr, u16 vid);
496 int sparx5_del_mact_entry(struct sparx5 *sparx5,
497 			  const unsigned char *addr,
498 			  u16 vid);
499 int sparx5_mc_sync(struct net_device *dev, const unsigned char *addr);
500 int sparx5_mc_unsync(struct net_device *dev, const unsigned char *addr);
501 void sparx5_set_ageing(struct sparx5 *sparx5, int msecs);
502 int sparx5_mact_init(struct sparx5 *sparx5);
503 void sparx5_mact_deinit(struct sparx5 *sparx5);
504 
505 /* sparx5_vlan.c */
506 void sparx5_pgid_update_mask(struct sparx5_port *port, int pgid, bool enable);
507 void sparx5_pgid_clear(struct sparx5 *spx5, int pgid);
508 void sparx5_pgid_read_mask(struct sparx5 *sparx5, int pgid, u32 portmask[3]);
509 void sparx5_update_fwd(struct sparx5 *sparx5);
510 void sparx5_vlan_init(struct sparx5 *sparx5);
511 void sparx5_vlan_port_setup(struct sparx5 *sparx5, int portno);
512 int sparx5_vlan_vid_add(struct sparx5_port *port, u16 vid, bool pvid,
513 			bool untagged);
514 int sparx5_vlan_vid_del(struct sparx5_port *port, u16 vid);
515 void sparx5_vlan_port_apply(struct sparx5 *sparx5, struct sparx5_port *port);
516 
517 /* sparx5_calendar.c */
518 int sparx5_calendar_init(struct sparx5 *sparx5);
519 int sparx5_dsm_calendar_calc(struct sparx5 *sparx5, u32 taxi,
520 			     struct sparx5_calendar_data *data);
521 u32 sparx5_cal_speed_to_value(enum sparx5_cal_bw speed);
522 enum sparx5_cal_bw sparx5_get_port_cal_speed(struct sparx5 *sparx5, u32 portno);
523 
524 
525 /* sparx5_ethtool.c */
526 void sparx5_get_stats64(struct net_device *ndev, struct rtnl_link_stats64 *stats);
527 int sparx5_stats_init(struct sparx5 *sparx5);
528 void sparx5_stats_deinit(struct sparx5 *sparx5);
529 
530 /* sparx5_dcb.c */
531 #ifdef CONFIG_SPARX5_DCB
532 int sparx5_dcb_init(struct sparx5 *sparx5);
533 #else
534 static inline int sparx5_dcb_init(struct sparx5 *sparx5)
535 {
536 	return 0;
537 }
538 #endif
539 
540 /* sparx5_netdev.c */
541 void sparx5_set_port_ifh_timestamp(struct sparx5 *sparx5, void *ifh_hdr,
542 				   u64 timestamp);
543 void sparx5_set_port_ifh_rew_op(void *ifh_hdr, u32 rew_op);
544 void sparx5_set_port_ifh_pdu_type(struct sparx5 *sparx5, void *ifh_hdr,
545 				  u32 pdu_type);
546 void sparx5_set_port_ifh_pdu_w16_offset(struct sparx5 *sparx5, void *ifh_hdr,
547 					u32 pdu_w16_offset);
548 void sparx5_set_port_ifh(struct sparx5 *sparx5, void *ifh_hdr, u16 portno);
549 bool sparx5_netdevice_check(const struct net_device *dev);
550 struct net_device *sparx5_create_netdev(struct sparx5 *sparx5, u32 portno);
551 int sparx5_register_netdevs(struct sparx5 *sparx5);
552 void sparx5_destroy_netdevs(struct sparx5 *sparx5);
553 void sparx5_unregister_netdevs(struct sparx5 *sparx5);
554 
555 /* sparx5_ptp.c */
556 int sparx5_ptp_init(struct sparx5 *sparx5);
557 void sparx5_ptp_deinit(struct sparx5 *sparx5);
558 int sparx5_ptp_hwtstamp_set(struct sparx5_port *port,
559 			    struct kernel_hwtstamp_config *cfg,
560 			    struct netlink_ext_ack *extack);
561 void sparx5_ptp_hwtstamp_get(struct sparx5_port *port,
562 			     struct kernel_hwtstamp_config *cfg);
563 void sparx5_ptp_rxtstamp(struct sparx5 *sparx5, struct sk_buff *skb,
564 			 u64 timestamp);
565 int sparx5_ptp_txtstamp_request(struct sparx5_port *port,
566 				struct sk_buff *skb);
567 void sparx5_ptp_txtstamp_release(struct sparx5_port *port,
568 				 struct sk_buff *skb);
569 irqreturn_t sparx5_ptp_irq_handler(int irq, void *args);
570 int sparx5_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts);
571 void sparx5_get_hwtimestamp(struct sparx5 *sparx5,
572 			    struct timespec64 *ts,
573 			    u32 nsec);
574 
575 /* sparx5_vcap_impl.c */
576 int sparx5_vcap_init(struct sparx5 *sparx5);
577 void sparx5_vcap_deinit(struct sparx5 *sparx5);
578 
579 /* sparx5_pgid.c */
580 enum sparx5_pgid_type {
581 	SPX5_PGID_FREE,
582 	SPX5_PGID_RESERVED,
583 	SPX5_PGID_MULTICAST,
584 };
585 
586 void sparx5_pgid_init(struct sparx5 *spx5);
587 int sparx5_pgid_alloc_mcast(struct sparx5 *spx5, u16 *idx);
588 int sparx5_pgid_free(struct sparx5 *spx5, u16 idx);
589 int sparx5_get_pgid(struct sparx5 *sparx5, int pgid);
590 
591 /* sparx5_pool.c */
592 struct sparx5_pool_entry {
593 	u16 ref_cnt;
594 	u32 idx; /* tc index */
595 };
596 
597 u32 sparx5_pool_idx_to_id(u32 idx);
598 int sparx5_pool_put(struct sparx5_pool_entry *pool, int size, u32 id);
599 int sparx5_pool_get(struct sparx5_pool_entry *pool, int size, u32 *id);
600 int sparx5_pool_get_with_idx(struct sparx5_pool_entry *pool, int size, u32 idx,
601 			     u32 *id);
602 
603 /* sparx5_port.c */
604 int sparx5_port_mux_set(struct sparx5 *sparx5, struct sparx5_port *port,
605 			struct sparx5_port_config *conf);
606 int sparx5_get_internal_port(struct sparx5 *sparx5, int port);
607 
608 /* sparx5_sdlb.c */
609 #define SPX5_SDLB_PUP_TOKEN_DISABLE 0x1FFF
610 #define SPX5_SDLB_PUP_TOKEN_MAX (SPX5_SDLB_PUP_TOKEN_DISABLE - 1)
611 #define SPX5_SDLB_GROUP_RATE_MAX 25000000000ULL
612 #define SPX5_SDLB_2CYCLES_TYPE2_THRES_OFFSET 13
613 #define SPX5_SDLB_CNT 4096
614 #define SPX5_SDLB_GROUP_CNT 10
615 #define SPX5_CLK_PER_100PS_DEFAULT 16
616 
617 struct sparx5_sdlb_group {
618 	u64 max_rate;
619 	u32 min_burst;
620 	u32 frame_size;
621 	u32 pup_interval;
622 	u32 nsets;
623 };
624 
625 extern struct sparx5_sdlb_group sdlb_groups[SPX5_SDLB_GROUP_CNT];
626 struct sparx5_sdlb_group *sparx5_get_sdlb_group(int idx);
627 int sparx5_sdlb_pup_token_get(struct sparx5 *sparx5, u32 pup_interval,
628 			      u64 rate);
629 
630 u64 sparx5_sdlb_clk_hz_get(struct sparx5 *sparx5);
631 int sparx5_sdlb_group_get_by_rate(struct sparx5 *sparx5, u32 rate, u32 burst);
632 int sparx5_sdlb_group_get_by_index(struct sparx5 *sparx5, u32 idx, u32 *group);
633 
634 int sparx5_sdlb_group_add(struct sparx5 *sparx5, u32 group, u32 idx);
635 int sparx5_sdlb_group_del(struct sparx5 *sparx5, u32 group, u32 idx);
636 
637 void sparx5_sdlb_group_init(struct sparx5 *sparx5, u64 max_rate, u32 min_burst,
638 			    u32 frame_size, u32 idx);
639 
640 /* sparx5_police.c */
641 enum {
642 	/* More policer types will be added later */
643 	SPX5_POL_SERVICE
644 };
645 
646 struct sparx5_policer {
647 	u32 type;
648 	u32 idx;
649 	u64 rate;
650 	u32 burst;
651 	u32 group;
652 	u8 event_mask;
653 };
654 
655 int sparx5_policer_conf_set(struct sparx5 *sparx5, struct sparx5_policer *pol);
656 
657 /* sparx5_psfp.c */
658 #define SPX5_PSFP_GCE_CNT 4
659 #define SPX5_PSFP_SG_CNT 1024
660 #define SPX5_PSFP_SG_MIN_CYCLE_TIME_NS (1 * NSEC_PER_USEC)
661 #define SPX5_PSFP_SG_MAX_CYCLE_TIME_NS ((1 * NSEC_PER_SEC) - 1)
662 #define SPX5_PSFP_SG_MAX_IPV (SPX5_PRIOS - 1)
663 #define SPX5_PSFP_SG_OPEN (SPX5_PSFP_SG_CNT - 1)
664 #define SPX5_PSFP_SG_CYCLE_TIME_DEFAULT 1000000
665 #define SPX5_PSFP_SF_MAX_SDU 16383
666 
667 struct sparx5_psfp_fm {
668 	struct sparx5_policer pol;
669 };
670 
671 struct sparx5_psfp_gce {
672 	bool gate_state;            /* StreamGateState */
673 	u32 interval;               /* TimeInterval */
674 	u32 ipv;                    /* InternalPriorityValue */
675 	u32 maxoctets;              /* IntervalOctetMax */
676 };
677 
678 struct sparx5_psfp_sg {
679 	bool gate_state;            /* PSFPAdminGateStates */
680 	bool gate_enabled;          /* PSFPGateEnabled */
681 	u32 ipv;                    /* PSFPAdminIPV */
682 	struct timespec64 basetime; /* PSFPAdminBaseTime */
683 	u32 cycletime;              /* PSFPAdminCycleTime */
684 	u32 cycletimeext;           /* PSFPAdminCycleTimeExtension */
685 	u32 num_entries;            /* PSFPAdminControlListLength */
686 	struct sparx5_psfp_gce gce[SPX5_PSFP_GCE_CNT];
687 };
688 
689 struct sparx5_psfp_sf {
690 	bool sblock_osize_ena;
691 	bool sblock_osize;
692 	u32 max_sdu;
693 	u32 sgid; /* Gate id */
694 	u32 fmid; /* Flow meter id */
695 };
696 
697 int sparx5_psfp_fm_add(struct sparx5 *sparx5, u32 uidx,
698 		       struct sparx5_psfp_fm *fm, u32 *id);
699 int sparx5_psfp_fm_del(struct sparx5 *sparx5, u32 id);
700 
701 int sparx5_psfp_sg_add(struct sparx5 *sparx5, u32 uidx,
702 		       struct sparx5_psfp_sg *sg, u32 *id);
703 int sparx5_psfp_sg_del(struct sparx5 *sparx5, u32 id);
704 
705 int sparx5_psfp_sf_add(struct sparx5 *sparx5, const struct sparx5_psfp_sf *sf,
706 		       u32 *id);
707 int sparx5_psfp_sf_del(struct sparx5 *sparx5, u32 id);
708 
709 u32 sparx5_psfp_isdx_get_sf(struct sparx5 *sparx5, u32 isdx);
710 u32 sparx5_psfp_isdx_get_fm(struct sparx5 *sparx5, u32 isdx);
711 u32 sparx5_psfp_sf_get_sg(struct sparx5 *sparx5, u32 sfid);
712 void sparx5_isdx_conf_set(struct sparx5 *sparx5, u32 isdx, u32 sfid, u32 fmid);
713 
714 void sparx5_psfp_init(struct sparx5 *sparx5);
715 
716 /* sparx5_qos.c */
717 void sparx5_new_base_time(struct sparx5 *sparx5, const u32 cycle_time,
718 			  const ktime_t org_base_time, ktime_t *new_base_time);
719 
720 /* sparx5_tas.c */
721 /* TAS list states, mirrors the HSCH_TAS_LIST_STATE_LIST_STATE HW field */
722 enum sparx5_tas_state {
723 	SPX5_TAS_STATE_ADMIN,
724 	SPX5_TAS_STATE_ADVANCING,
725 	SPX5_TAS_STATE_PENDING,
726 	SPX5_TAS_STATE_OPERATING,
727 	SPX5_TAS_STATE_TERMINATING,
728 	SPX5_NUM_TAS_STATE,
729 };
730 
731 int sparx5_tas_init(struct sparx5 *sparx5);
732 struct tc_taprio_qopt_offload;
733 int sparx5_tas_enable(struct sparx5_port *port,
734 		      struct tc_taprio_qopt_offload *qopt);
735 int sparx5_tas_disable(struct sparx5_port *port);
736 void sparx5_tas_speed(struct sparx5_port *port, int speed);
737 
738 /* sparx5_mirror.c */
739 int sparx5_mirror_add(struct sparx5_mall_entry *entry);
740 void sparx5_mirror_del(struct sparx5_mall_entry *entry);
741 void sparx5_mirror_stats(struct sparx5_mall_entry *entry,
742 			 struct flow_stats *fstats);
743 
744 /* Clock period in picoseconds */
745 static inline u32 sparx5_clk_period(enum sparx5_core_clockfreq cclock)
746 {
747 	switch (cclock) {
748 	case SPX5_CORE_CLOCK_250MHZ:
749 		return 4000;
750 	case SPX5_CORE_CLOCK_328MHZ:
751 		return 3048;
752 	case SPX5_CORE_CLOCK_500MHZ:
753 		return 2000;
754 	case SPX5_CORE_CLOCK_625MHZ:
755 	default:
756 		return 1600;
757 	}
758 }
759 
760 static inline bool sparx5_is_baser(phy_interface_t interface)
761 {
762 	return interface == PHY_INTERFACE_MODE_5GBASER ||
763 		   interface == PHY_INTERFACE_MODE_10GBASER ||
764 		   interface == PHY_INTERFACE_MODE_25GBASER;
765 }
766 
767 extern const struct phylink_mac_ops sparx5_phylink_mac_ops;
768 extern const struct phylink_pcs_ops sparx5_phylink_pcs_ops;
769 extern const struct ethtool_ops sparx5_ethtool_ops;
770 extern const struct dcbnl_rtnl_ops sparx5_dcbnl_ops;
771 
772 /* Calculate raw offset */
773 static inline __pure int spx5_offset(int id, int tinst, int tcnt,
774 				     int gbase, int ginst,
775 				     int gcnt, int gwidth,
776 				     int raddr, int rinst,
777 				     int rcnt, int rwidth)
778 {
779 	WARN_ON((tinst) >= tcnt);
780 	WARN_ON((ginst) >= gcnt);
781 	WARN_ON((rinst) >= rcnt);
782 	return gbase + ((ginst) * gwidth) +
783 		raddr + ((rinst) * rwidth);
784 }
785 
786 /* Read, Write and modify registers content.
787  * The register definition macros start at the id
788  */
789 static inline void __iomem *spx5_addr(void __iomem *base[],
790 				      int id, int tinst, int tcnt,
791 				      int gbase, int ginst,
792 				      int gcnt, int gwidth,
793 				      int raddr, int rinst,
794 				      int rcnt, int rwidth)
795 {
796 	WARN_ON((tinst) >= tcnt);
797 	WARN_ON((ginst) >= gcnt);
798 	WARN_ON((rinst) >= rcnt);
799 	return base[id + (tinst)] +
800 		gbase + ((ginst) * gwidth) +
801 		raddr + ((rinst) * rwidth);
802 }
803 
804 static inline void __iomem *spx5_inst_addr(void __iomem *base,
805 					   int gbase, int ginst,
806 					   int gcnt, int gwidth,
807 					   int raddr, int rinst,
808 					   int rcnt, int rwidth)
809 {
810 	WARN_ON((ginst) >= gcnt);
811 	WARN_ON((rinst) >= rcnt);
812 	return base +
813 		gbase + ((ginst) * gwidth) +
814 		raddr + ((rinst) * rwidth);
815 }
816 
817 static inline u32 spx5_rd(struct sparx5 *sparx5, int id, int tinst, int tcnt,
818 			  int gbase, int ginst, int gcnt, int gwidth,
819 			  int raddr, int rinst, int rcnt, int rwidth)
820 {
821 	return readl(spx5_addr(sparx5->regs, id, tinst, tcnt, gbase, ginst,
822 			       gcnt, gwidth, raddr, rinst, rcnt, rwidth));
823 }
824 
825 static inline u32 spx5_inst_rd(void __iomem *iomem, int id, int tinst, int tcnt,
826 			       int gbase, int ginst, int gcnt, int gwidth,
827 			       int raddr, int rinst, int rcnt, int rwidth)
828 {
829 	return readl(spx5_inst_addr(iomem, gbase, ginst,
830 				     gcnt, gwidth, raddr, rinst, rcnt, rwidth));
831 }
832 
833 static inline void spx5_wr(u32 val, struct sparx5 *sparx5,
834 			   int id, int tinst, int tcnt,
835 			   int gbase, int ginst, int gcnt, int gwidth,
836 			   int raddr, int rinst, int rcnt, int rwidth)
837 {
838 	writel(val, spx5_addr(sparx5->regs, id, tinst, tcnt,
839 			      gbase, ginst, gcnt, gwidth,
840 			      raddr, rinst, rcnt, rwidth));
841 }
842 
843 static inline void spx5_inst_wr(u32 val, void __iomem *iomem,
844 				int id, int tinst, int tcnt,
845 				int gbase, int ginst, int gcnt, int gwidth,
846 				int raddr, int rinst, int rcnt, int rwidth)
847 {
848 	writel(val, spx5_inst_addr(iomem,
849 				   gbase, ginst, gcnt, gwidth,
850 				   raddr, rinst, rcnt, rwidth));
851 }
852 
853 static inline void spx5_rmw(u32 val, u32 mask, struct sparx5 *sparx5,
854 			    int id, int tinst, int tcnt,
855 			    int gbase, int ginst, int gcnt, int gwidth,
856 			    int raddr, int rinst, int rcnt, int rwidth)
857 {
858 	u32 nval;
859 
860 	nval = readl(spx5_addr(sparx5->regs, id, tinst, tcnt, gbase, ginst,
861 			       gcnt, gwidth, raddr, rinst, rcnt, rwidth));
862 	nval = (nval & ~mask) | (val & mask);
863 	writel(nval, spx5_addr(sparx5->regs, id, tinst, tcnt, gbase, ginst,
864 			       gcnt, gwidth, raddr, rinst, rcnt, rwidth));
865 }
866 
867 static inline void spx5_inst_rmw(u32 val, u32 mask, void __iomem *iomem,
868 				 int id, int tinst, int tcnt,
869 				 int gbase, int ginst, int gcnt, int gwidth,
870 				 int raddr, int rinst, int rcnt, int rwidth)
871 {
872 	u32 nval;
873 
874 	nval = readl(spx5_inst_addr(iomem, gbase, ginst, gcnt, gwidth, raddr,
875 				    rinst, rcnt, rwidth));
876 	nval = (nval & ~mask) | (val & mask);
877 	writel(nval, spx5_inst_addr(iomem, gbase, ginst, gcnt, gwidth, raddr,
878 				    rinst, rcnt, rwidth));
879 }
880 
881 static inline void __iomem *spx5_inst_get(struct sparx5 *sparx5, int id, int tinst)
882 {
883 	return sparx5->regs[id + tinst];
884 }
885 
886 static inline void __iomem *spx5_reg_get(struct sparx5 *sparx5,
887 					 int id, int tinst, int tcnt,
888 					 int gbase, int ginst, int gcnt, int gwidth,
889 					 int raddr, int rinst, int rcnt, int rwidth)
890 {
891 	return spx5_addr(sparx5->regs, id, tinst, tcnt,
892 			 gbase, ginst, gcnt, gwidth,
893 			 raddr, rinst, rcnt, rwidth);
894 }
895 
896 #endif	/* __SPARX5_MAIN_H__ */
897