xref: /linux/drivers/net/dsa/netc/netc_switch.h (revision 4b99990cdf9560e8a071640baf19f312e6ae02f4)
1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /*
3  * Copyright 2025-2026 NXP
4  */
5 
6 #ifndef _NETC_SWITCH_H
7 #define _NETC_SWITCH_H
8 
9 #include <linux/dsa/tag_netc.h>
10 #include <linux/fsl/netc_global.h>
11 #include <linux/fsl/ntmp.h>
12 #include <linux/of_device.h>
13 #include <linux/of_net.h>
14 #include <linux/pci.h>
15 
16 #include "netc_switch_hw.h"
17 
18 #define NETC_REGS_BAR			0
19 #define NETC_REGS_SIZE			0x80000
20 #define NETC_MSIX_TBL_BAR		2
21 #define NETC_REGS_PORT_BASE		0x4000
22 /* register block size per port  */
23 #define NETC_REGS_PORT_SIZE		0x4000
24 #define PORT_IOBASE(p)			(NETC_REGS_PORT_SIZE * (p))
25 #define NETC_REGS_GLOBAL_BASE		0x70000
26 
27 #define NETC_SWITCH_REV_4_3		0x0403
28 
29 #define NETC_TC_NUM			8
30 #define NETC_CBDR_NUM			2
31 #define NETC_IPV_NUM			8
32 
33 #define NETC_MAX_FRAME_LEN		9600
34 
35 #define NETC_STANDALONE_PVID		0
36 #define NETC_VLAN_UNAWARE_PVID(br_id)	(4096 - (br_id))
37 
38 /* Threshold format: MANT (bits 11:4) * 2^EXP (bits 3:0)
39  * Unit: Memory words (average of 20 bytes each)
40  * NETC_BP_THRESH = 0x8c3, MANT = 0x8c, EXP = 3. Threshold: 1120 words
41  * NETC_FC_THRESH_ON = 0x733, MANT = 0x73, EXP = 3. Threshold: 920 words
42  * NETC_FC_THRESH_OFF = 0x263, MANT = 0x26, EXP = 3. Threshold: 304 words
43  */
44 #define NETC_BP_THRESH			0x8c3
45 #define NETC_FC_THRESH_ON		0x733
46 #define NETC_FC_THRESH_OFF		0x263
47 
48 /* PAUSE quanta: 0xFFFF = 65535 quanta (each quanta = 512 bit times) */
49 #define NETC_PAUSE_QUANTA		0xFFFF
50 /* PAUSE refresh threshold: send refresh when timer reaches this value */
51 #define NETC_PAUSE_THRESH		0x7FFF
52 
53 #define NETC_FDBT_AGEING_DELAY		(3 * HZ)
54 #define NETC_FDBT_AGEING_THRESH		100
55 
56 struct netc_switch;
57 
58 struct netc_switch_info {
59 	u32 num_ports;
60 	void (*phylink_get_caps)(int port, struct phylink_config *config);
61 };
62 
63 struct netc_port_caps {
64 	u32 half_duplex:1; /* indicates whether the port support half-duplex */
65 	u32 pmac:1;	  /* indicates whether the port has preemption MAC */
66 	u32 pseudo_link:1;
67 };
68 
69 enum netc_host_reason {
70 	/* Software defined host reasons */
71 	NETC_HR_HOST_FLOOD = 8,
72 };
73 
74 struct netc_port {
75 	void __iomem *iobase;
76 	struct netc_switch *switch_priv;
77 	struct netc_port_caps caps;
78 	struct dsa_port *dp;
79 	struct clk *ref_clk; /* RGMII/RMII reference clock */
80 	struct mii_bus *emdio;
81 	int ett_offset;
82 
83 	u16 enable:1;
84 	u16 uc:1;
85 	u16 mc:1;
86 	u16 pvid;
87 	struct ipft_entry_data *host_flood;
88 };
89 
90 struct netc_switch_regs {
91 	void __iomem *base;
92 	void __iomem *port;
93 	void __iomem *global;
94 };
95 
96 struct netc_fdb_entry {
97 	u32 entry_id;
98 	struct fdbt_cfge_data cfge;
99 	struct fdbt_keye_data keye;
100 	struct hlist_node node;
101 };
102 
103 struct netc_vlan_entry {
104 	u16 vid;
105 	u32 ect_gid;
106 	u32 untagged_port_bitmap;
107 	struct vft_cfge_data cfge;
108 	struct hlist_node node;
109 };
110 
111 struct netc_port_stat {
112 	int reg;
113 	char name[ETH_GSTRING_LEN] __nonstring;
114 };
115 
116 struct netc_switch {
117 	struct pci_dev *pdev;
118 	struct device *dev;
119 	struct dsa_switch *ds;
120 	u16 revision;
121 
122 	const struct netc_switch_info *info;
123 	struct netc_switch_regs regs;
124 	struct netc_port **ports;
125 	u32 port_bitmap; /* bitmap of available ports */
126 
127 	struct ntmp_user ntmp;
128 	struct hlist_head fdb_list;
129 	struct mutex fdbt_lock; /* FDB table lock */
130 	struct delayed_work fdbt_ageing_work;
131 	/* (fdbt_ageing_delay * NETC_FDBT_AGEING_THRESH) is ageing time */
132 	unsigned long fdbt_ageing_delay;
133 	atomic_t br_cnt;
134 	struct hlist_head vlan_list;
135 	struct mutex vft_lock; /* VLAN filter table lock */
136 
137 	/* Switch hardware capabilities */
138 	u32 htmcapr_num_words;
139 	u32 num_bp;
140 
141 	struct bpt_cfge_data *bpt_list;
142 };
143 
144 #define NETC_PRIV(ds)			((struct netc_switch *)((ds)->priv))
145 #define NETC_PORT(ds, port_id)		(NETC_PRIV(ds)->ports[(port_id)])
146 
147 /* Write/Read Switch base registers */
148 #define netc_base_rd(r, o)		netc_read((r)->base + (o))
149 #define netc_base_wr(r, o, v)		netc_write((r)->base + (o), v)
150 
151 /* Write/Read registers of Switch Port (including pseudo MAC port) */
152 #define netc_port_rd(p, o)		netc_read((p)->iobase + (o))
153 #define netc_port_rd64(p, o)		netc_read64((p)->iobase + (o))
154 #define netc_port_wr(p, o, v)		netc_write((p)->iobase + (o), v)
155 
156 /* Write/Read Switch global registers */
157 #define netc_glb_rd(r, o)		netc_read((r)->global + (o))
158 #define netc_glb_wr(r, o, v)		netc_write((r)->global + (o), v)
159 
160 static inline bool is_netc_pseudo_port(struct netc_port *np)
161 {
162 	return np->caps.pseudo_link;
163 }
164 
165 static inline void netc_add_fdb_entry(struct netc_switch *priv,
166 				      struct netc_fdb_entry *entry)
167 {
168 	hlist_add_head(&entry->node, &priv->fdb_list);
169 }
170 
171 static inline void netc_del_fdb_entry(struct netc_fdb_entry *entry)
172 {
173 	hlist_del(&entry->node);
174 	kfree(entry);
175 }
176 
177 static inline void netc_add_vlan_entry(struct netc_switch *priv,
178 				       struct netc_vlan_entry *entry)
179 {
180 	hlist_add_head(&entry->node, &priv->vlan_list);
181 }
182 
183 static inline void netc_del_vlan_entry(struct netc_vlan_entry *entry)
184 {
185 	hlist_del(&entry->node);
186 	kfree(entry);
187 }
188 
189 int netc_switch_platform_probe(struct netc_switch *priv);
190 
191 /* ethtool APIs */
192 void netc_port_get_pause_stats(struct dsa_switch *ds, int port,
193 			       struct ethtool_pause_stats *pause_stats);
194 void netc_port_get_rmon_stats(struct dsa_switch *ds, int port,
195 			      struct ethtool_rmon_stats *rmon_stats,
196 			      const struct ethtool_rmon_hist_range **ranges);
197 void netc_port_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
198 				  struct ethtool_eth_ctrl_stats *ctrl_stats);
199 void netc_port_get_eth_mac_stats(struct dsa_switch *ds, int port,
200 				 struct ethtool_eth_mac_stats *mac_stats);
201 int netc_port_get_sset_count(struct dsa_switch *ds, int port, int sset);
202 void netc_port_get_strings(struct dsa_switch *ds, int port,
203 			   u32 sset, u8 *data);
204 void netc_port_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data);
205 
206 #endif
207