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