1 /* SPDX-License-Identifier: GPL-2.0+ */
2 
3 #ifndef __LAN966X_MAIN_H__
4 #define __LAN966X_MAIN_H__
5 
6 #include <linux/etherdevice.h>
7 #include <linux/if_vlan.h>
8 #include <linux/jiffies.h>
9 #include <linux/phy.h>
10 #include <linux/phylink.h>
11 #include <net/switchdev.h>
12 
13 #include "lan966x_regs.h"
14 #include "lan966x_ifh.h"
15 
16 #define TABLE_UPDATE_SLEEP_US		10
17 #define TABLE_UPDATE_TIMEOUT_US		100000
18 
19 #define LAN966X_BUFFER_CELL_SZ		64
20 #define LAN966X_BUFFER_MEMORY		(160 * 1024)
21 #define LAN966X_BUFFER_MIN_SZ		60
22 
23 #define PGID_AGGR			64
24 #define PGID_SRC			80
25 #define PGID_ENTRIES			89
26 
27 #define UNAWARE_PVID			0
28 #define HOST_PVID			4095
29 
30 /* Reserved amount for (SRC, PRIO) at index 8*SRC + PRIO */
31 #define QSYS_Q_RSRV			95
32 
33 #define CPU_PORT			8
34 
35 /* Reserved PGIDs */
36 #define PGID_CPU			(PGID_AGGR - 6)
37 #define PGID_UC				(PGID_AGGR - 5)
38 #define PGID_BC				(PGID_AGGR - 4)
39 #define PGID_MC				(PGID_AGGR - 3)
40 #define PGID_MCIPV4			(PGID_AGGR - 2)
41 #define PGID_MCIPV6			(PGID_AGGR - 1)
42 
43 /* Non-reserved PGIDs, used for general purpose */
44 #define PGID_GP_START			(CPU_PORT + 1)
45 #define PGID_GP_END			PGID_CPU
46 
47 #define LAN966X_SPEED_NONE		0
48 #define LAN966X_SPEED_2500		1
49 #define LAN966X_SPEED_1000		1
50 #define LAN966X_SPEED_100		2
51 #define LAN966X_SPEED_10		3
52 
53 /* MAC table entry types.
54  * ENTRYTYPE_NORMAL is subject to aging.
55  * ENTRYTYPE_LOCKED is not subject to aging.
56  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
57  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
58  */
59 enum macaccess_entry_type {
60 	ENTRYTYPE_NORMAL = 0,
61 	ENTRYTYPE_LOCKED,
62 	ENTRYTYPE_MACV4,
63 	ENTRYTYPE_MACV6,
64 };
65 
66 struct lan966x_port;
67 
68 struct lan966x_stat_layout {
69 	u32 offset;
70 	char name[ETH_GSTRING_LEN];
71 };
72 
73 struct lan966x {
74 	struct device *dev;
75 
76 	u8 num_phys_ports;
77 	struct lan966x_port **ports;
78 
79 	void __iomem *regs[NUM_TARGETS];
80 
81 	int shared_queue_sz;
82 
83 	u8 base_mac[ETH_ALEN];
84 
85 	struct net_device *bridge;
86 	u16 bridge_mask;
87 	u16 bridge_fwd_mask;
88 
89 	struct list_head mac_entries;
90 	spinlock_t mac_lock; /* lock for mac_entries list */
91 
92 	u16 vlan_mask[VLAN_N_VID];
93 	DECLARE_BITMAP(cpu_vlan_mask, VLAN_N_VID);
94 
95 	/* stats */
96 	const struct lan966x_stat_layout *stats_layout;
97 	u32 num_stats;
98 
99 	/* workqueue for reading stats */
100 	struct mutex stats_lock;
101 	u64 *stats;
102 	struct delayed_work stats_work;
103 	struct workqueue_struct *stats_queue;
104 
105 	/* interrupts */
106 	int xtr_irq;
107 	int ana_irq;
108 
109 	/* worqueue for fdb */
110 	struct workqueue_struct *fdb_work;
111 	struct list_head fdb_entries;
112 
113 	/* mdb */
114 	struct list_head mdb_entries;
115 	struct list_head pgid_entries;
116 };
117 
118 struct lan966x_port_config {
119 	phy_interface_t portmode;
120 	const unsigned long *advertising;
121 	int speed;
122 	int duplex;
123 	u32 pause;
124 	bool inband;
125 	bool autoneg;
126 };
127 
128 struct lan966x_port {
129 	struct net_device *dev;
130 	struct lan966x *lan966x;
131 
132 	u8 chip_port;
133 	u16 pvid;
134 	u16 vid;
135 	bool vlan_aware;
136 
137 	bool learn_ena;
138 
139 	struct phylink_config phylink_config;
140 	struct phylink_pcs phylink_pcs;
141 	struct lan966x_port_config config;
142 	struct phylink *phylink;
143 	struct phy *serdes;
144 	struct fwnode_handle *fwnode;
145 };
146 
147 extern const struct phylink_mac_ops lan966x_phylink_mac_ops;
148 extern const struct phylink_pcs_ops lan966x_phylink_pcs_ops;
149 extern const struct ethtool_ops lan966x_ethtool_ops;
150 
151 bool lan966x_netdevice_check(const struct net_device *dev);
152 
153 void lan966x_register_notifier_blocks(void);
154 void lan966x_unregister_notifier_blocks(void);
155 
156 void lan966x_stats_get(struct net_device *dev,
157 		       struct rtnl_link_stats64 *stats);
158 int lan966x_stats_init(struct lan966x *lan966x);
159 
160 void lan966x_port_config_down(struct lan966x_port *port);
161 void lan966x_port_config_up(struct lan966x_port *port);
162 void lan966x_port_status_get(struct lan966x_port *port,
163 			     struct phylink_link_state *state);
164 int lan966x_port_pcs_set(struct lan966x_port *port,
165 			 struct lan966x_port_config *config);
166 void lan966x_port_init(struct lan966x_port *port);
167 
168 int lan966x_mac_ip_learn(struct lan966x *lan966x,
169 			 bool cpu_copy,
170 			 const unsigned char mac[ETH_ALEN],
171 			 unsigned int vid,
172 			 enum macaccess_entry_type type);
173 int lan966x_mac_learn(struct lan966x *lan966x, int port,
174 		      const unsigned char mac[ETH_ALEN],
175 		      unsigned int vid,
176 		      enum macaccess_entry_type type);
177 int lan966x_mac_forget(struct lan966x *lan966x,
178 		       const unsigned char mac[ETH_ALEN],
179 		       unsigned int vid,
180 		       enum macaccess_entry_type type);
181 int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid);
182 int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid);
183 void lan966x_mac_init(struct lan966x *lan966x);
184 void lan966x_mac_set_ageing(struct lan966x *lan966x,
185 			    u32 ageing);
186 int lan966x_mac_del_entry(struct lan966x *lan966x,
187 			  const unsigned char *addr,
188 			  u16 vid);
189 int lan966x_mac_add_entry(struct lan966x *lan966x,
190 			  struct lan966x_port *port,
191 			  const unsigned char *addr,
192 			  u16 vid);
193 void lan966x_mac_purge_entries(struct lan966x *lan966x);
194 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x);
195 
196 void lan966x_vlan_init(struct lan966x *lan966x);
197 void lan966x_vlan_port_apply(struct lan966x_port *port);
198 bool lan966x_vlan_cpu_member_cpu_vlan_mask(struct lan966x *lan966x, u16 vid);
199 void lan966x_vlan_port_set_vlan_aware(struct lan966x_port *port,
200 				      bool vlan_aware);
201 int lan966x_vlan_port_set_vid(struct lan966x_port *port,
202 			      u16 vid,
203 			      bool pvid,
204 			      bool untagged);
205 void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
206 				u16 vid,
207 				bool pvid,
208 				bool untagged);
209 void lan966x_vlan_port_del_vlan(struct lan966x_port *port, u16 vid);
210 void lan966x_vlan_cpu_add_vlan(struct lan966x *lan966x, u16 vid);
211 void lan966x_vlan_cpu_del_vlan(struct lan966x *lan966x, u16 vid);
212 
213 void lan966x_fdb_write_entries(struct lan966x *lan966x, u16 vid);
214 void lan966x_fdb_erase_entries(struct lan966x *lan966x, u16 vid);
215 int lan966x_fdb_init(struct lan966x *lan966x);
216 void lan966x_fdb_deinit(struct lan966x *lan966x);
217 int lan966x_handle_fdb(struct net_device *dev,
218 		       struct net_device *orig_dev,
219 		       unsigned long event, const void *ctx,
220 		       const struct switchdev_notifier_fdb_info *fdb_info);
221 
222 void lan966x_mdb_init(struct lan966x *lan966x);
223 void lan966x_mdb_deinit(struct lan966x *lan966x);
224 int lan966x_handle_port_mdb_add(struct lan966x_port *port,
225 				const struct switchdev_obj *obj);
226 int lan966x_handle_port_mdb_del(struct lan966x_port *port,
227 				const struct switchdev_obj *obj);
228 void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid);
229 void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid);
230 
231 static inline void __iomem *lan_addr(void __iomem *base[],
232 				     int id, int tinst, int tcnt,
233 				     int gbase, int ginst,
234 				     int gcnt, int gwidth,
235 				     int raddr, int rinst,
236 				     int rcnt, int rwidth)
237 {
238 	WARN_ON((tinst) >= tcnt);
239 	WARN_ON((ginst) >= gcnt);
240 	WARN_ON((rinst) >= rcnt);
241 	return base[id + (tinst)] +
242 		gbase + ((ginst) * gwidth) +
243 		raddr + ((rinst) * rwidth);
244 }
245 
246 static inline u32 lan_rd(struct lan966x *lan966x, int id, int tinst, int tcnt,
247 			 int gbase, int ginst, int gcnt, int gwidth,
248 			 int raddr, int rinst, int rcnt, int rwidth)
249 {
250 	return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
251 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
252 }
253 
254 static inline void lan_wr(u32 val, struct lan966x *lan966x,
255 			  int id, int tinst, int tcnt,
256 			  int gbase, int ginst, int gcnt, int gwidth,
257 			  int raddr, int rinst, int rcnt, int rwidth)
258 {
259 	writel(val, lan_addr(lan966x->regs, id, tinst, tcnt,
260 			     gbase, ginst, gcnt, gwidth,
261 			     raddr, rinst, rcnt, rwidth));
262 }
263 
264 static inline void lan_rmw(u32 val, u32 mask, struct lan966x *lan966x,
265 			   int id, int tinst, int tcnt,
266 			   int gbase, int ginst, int gcnt, int gwidth,
267 			   int raddr, int rinst, int rcnt, int rwidth)
268 {
269 	u32 nval;
270 
271 	nval = readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
272 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
273 	nval = (nval & ~mask) | (val & mask);
274 	writel(nval, lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
275 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
276 }
277 
278 #endif /* __LAN966X_MAIN_H__ */
279