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 READL_SLEEP_US			10
21 #define READL_TIMEOUT_US		100000000
22 
23 #define LAN966X_BUFFER_CELL_SZ		64
24 #define LAN966X_BUFFER_MEMORY		(160 * 1024)
25 #define LAN966X_BUFFER_MIN_SZ		60
26 
27 #define PGID_AGGR			64
28 #define PGID_SRC			80
29 #define PGID_ENTRIES			89
30 
31 #define UNAWARE_PVID			0
32 #define HOST_PVID			4095
33 
34 /* Reserved amount for (SRC, PRIO) at index 8*SRC + PRIO */
35 #define QSYS_Q_RSRV			95
36 
37 #define NUM_PHYS_PORTS			8
38 #define CPU_PORT			8
39 
40 /* Reserved PGIDs */
41 #define PGID_CPU			(PGID_AGGR - 6)
42 #define PGID_UC				(PGID_AGGR - 5)
43 #define PGID_BC				(PGID_AGGR - 4)
44 #define PGID_MC				(PGID_AGGR - 3)
45 #define PGID_MCIPV4			(PGID_AGGR - 2)
46 #define PGID_MCIPV6			(PGID_AGGR - 1)
47 
48 /* Non-reserved PGIDs, used for general purpose */
49 #define PGID_GP_START			(CPU_PORT + 1)
50 #define PGID_GP_END			PGID_CPU
51 
52 #define LAN966X_SPEED_NONE		0
53 #define LAN966X_SPEED_2500		1
54 #define LAN966X_SPEED_1000		1
55 #define LAN966X_SPEED_100		2
56 #define LAN966X_SPEED_10		3
57 
58 #define LAN966X_PHC_COUNT		3
59 #define LAN966X_PHC_PORT		0
60 #define LAN966X_PHC_PINS_NUM		7
61 
62 #define IFH_REW_OP_NOOP			0x0
63 #define IFH_REW_OP_ONE_STEP_PTP		0x3
64 #define IFH_REW_OP_TWO_STEP_PTP		0x4
65 
66 #define FDMA_RX_DCB_MAX_DBS		1
67 #define FDMA_TX_DCB_MAX_DBS		1
68 #define FDMA_DCB_INFO_DATAL(x)		((x) & GENMASK(15, 0))
69 
70 #define FDMA_DCB_STATUS_BLOCKL(x)	((x) & GENMASK(15, 0))
71 #define FDMA_DCB_STATUS_SOF		BIT(16)
72 #define FDMA_DCB_STATUS_EOF		BIT(17)
73 #define FDMA_DCB_STATUS_INTR		BIT(18)
74 #define FDMA_DCB_STATUS_DONE		BIT(19)
75 #define FDMA_DCB_STATUS_BLOCKO(x)	(((x) << 20) & GENMASK(31, 20))
76 #define FDMA_DCB_INVALID_DATA		0x1
77 
78 #define FDMA_XTR_CHANNEL		6
79 #define FDMA_INJ_CHANNEL		0
80 #define FDMA_DCB_MAX			512
81 
82 /* MAC table entry types.
83  * ENTRYTYPE_NORMAL is subject to aging.
84  * ENTRYTYPE_LOCKED is not subject to aging.
85  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
86  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
87  */
88 enum macaccess_entry_type {
89 	ENTRYTYPE_NORMAL = 0,
90 	ENTRYTYPE_LOCKED,
91 	ENTRYTYPE_MACV4,
92 	ENTRYTYPE_MACV6,
93 };
94 
95 struct lan966x_port;
96 
97 struct lan966x_db {
98 	u64 dataptr;
99 	u64 status;
100 };
101 
102 struct lan966x_rx_dcb {
103 	u64 nextptr;
104 	u64 info;
105 	struct lan966x_db db[FDMA_RX_DCB_MAX_DBS];
106 };
107 
108 struct lan966x_tx_dcb {
109 	u64 nextptr;
110 	u64 info;
111 	struct lan966x_db db[FDMA_TX_DCB_MAX_DBS];
112 };
113 
114 struct lan966x_rx {
115 	struct lan966x *lan966x;
116 
117 	/* Pointer to the array of hardware dcbs. */
118 	struct lan966x_rx_dcb *dcbs;
119 
120 	/* Pointer to the last address in the dcbs. */
121 	struct lan966x_rx_dcb *last_entry;
122 
123 	/* For each DB, there is a page */
124 	struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
125 
126 	/* Represents the db_index, it can have a value between 0 and
127 	 * FDMA_RX_DCB_MAX_DBS, once it reaches the value of FDMA_RX_DCB_MAX_DBS
128 	 * it means that the DCB can be reused.
129 	 */
130 	int db_index;
131 
132 	/* Represents the index in the dcbs. It has a value between 0 and
133 	 * FDMA_DCB_MAX
134 	 */
135 	int dcb_index;
136 
137 	/* Represents the dma address to the dcbs array */
138 	dma_addr_t dma;
139 
140 	/* Represents the page order that is used to allocate the pages for the
141 	 * RX buffers. This value is calculated based on max MTU of the devices.
142 	 */
143 	u8 page_order;
144 
145 	u8 channel_id;
146 };
147 
148 struct lan966x_tx_dcb_buf {
149 	struct net_device *dev;
150 	struct sk_buff *skb;
151 	dma_addr_t dma_addr;
152 	bool used;
153 	bool ptp;
154 };
155 
156 struct lan966x_tx {
157 	struct lan966x *lan966x;
158 
159 	/* Pointer to the dcb list */
160 	struct lan966x_tx_dcb *dcbs;
161 	u16 last_in_use;
162 
163 	/* Represents the DMA address to the first entry of the dcb entries. */
164 	dma_addr_t dma;
165 
166 	/* Array of dcbs that are given to the HW */
167 	struct lan966x_tx_dcb_buf *dcbs_buf;
168 
169 	u8 channel_id;
170 
171 	bool activated;
172 };
173 
174 struct lan966x_stat_layout {
175 	u32 offset;
176 	char name[ETH_GSTRING_LEN];
177 };
178 
179 struct lan966x_phc {
180 	struct ptp_clock *clock;
181 	struct ptp_clock_info info;
182 	struct ptp_pin_desc pins[LAN966X_PHC_PINS_NUM];
183 	struct hwtstamp_config hwtstamp_config;
184 	struct lan966x *lan966x;
185 	u8 index;
186 };
187 
188 struct lan966x_skb_cb {
189 	u8 rew_op;
190 	u16 ts_id;
191 	unsigned long jiffies;
192 };
193 
194 #define LAN966X_PTP_TIMEOUT		msecs_to_jiffies(10)
195 #define LAN966X_SKB_CB(skb) \
196 	((struct lan966x_skb_cb *)((skb)->cb))
197 
198 struct lan966x {
199 	struct device *dev;
200 
201 	u8 num_phys_ports;
202 	struct lan966x_port **ports;
203 
204 	void __iomem *regs[NUM_TARGETS];
205 
206 	int shared_queue_sz;
207 
208 	u8 base_mac[ETH_ALEN];
209 
210 	spinlock_t tx_lock; /* lock for frame transmition */
211 
212 	struct net_device *bridge;
213 	u16 bridge_mask;
214 	u16 bridge_fwd_mask;
215 
216 	struct list_head mac_entries;
217 	spinlock_t mac_lock; /* lock for mac_entries list */
218 
219 	u16 vlan_mask[VLAN_N_VID];
220 	DECLARE_BITMAP(cpu_vlan_mask, VLAN_N_VID);
221 
222 	/* stats */
223 	const struct lan966x_stat_layout *stats_layout;
224 	u32 num_stats;
225 
226 	/* workqueue for reading stats */
227 	struct mutex stats_lock;
228 	u64 *stats;
229 	struct delayed_work stats_work;
230 	struct workqueue_struct *stats_queue;
231 
232 	/* interrupts */
233 	int xtr_irq;
234 	int ana_irq;
235 	int ptp_irq;
236 	int fdma_irq;
237 	int ptp_ext_irq;
238 
239 	/* worqueue for fdb */
240 	struct workqueue_struct *fdb_work;
241 	struct list_head fdb_entries;
242 
243 	/* mdb */
244 	struct list_head mdb_entries;
245 	struct list_head pgid_entries;
246 
247 	/* ptp */
248 	bool ptp;
249 	struct lan966x_phc phc[LAN966X_PHC_COUNT];
250 	spinlock_t ptp_clock_lock; /* lock for phc */
251 	spinlock_t ptp_ts_id_lock; /* lock for ts_id */
252 	struct mutex ptp_lock; /* lock for ptp interface state */
253 	u16 ptp_skbs;
254 
255 	/* fdma */
256 	bool fdma;
257 	struct net_device *fdma_ndev;
258 	struct lan966x_rx rx;
259 	struct lan966x_tx tx;
260 	struct napi_struct napi;
261 };
262 
263 struct lan966x_port_config {
264 	phy_interface_t portmode;
265 	const unsigned long *advertising;
266 	int speed;
267 	int duplex;
268 	u32 pause;
269 	bool inband;
270 	bool autoneg;
271 };
272 
273 struct lan966x_port {
274 	struct net_device *dev;
275 	struct lan966x *lan966x;
276 
277 	u8 chip_port;
278 	u16 pvid;
279 	u16 vid;
280 	bool vlan_aware;
281 
282 	bool learn_ena;
283 	bool mcast_ena;
284 
285 	struct phylink_config phylink_config;
286 	struct phylink_pcs phylink_pcs;
287 	struct lan966x_port_config config;
288 	struct phylink *phylink;
289 	struct phy *serdes;
290 	struct fwnode_handle *fwnode;
291 
292 	u8 ptp_cmd;
293 	u16 ts_id;
294 	struct sk_buff_head tx_skbs;
295 };
296 
297 extern const struct phylink_mac_ops lan966x_phylink_mac_ops;
298 extern const struct phylink_pcs_ops lan966x_phylink_pcs_ops;
299 extern const struct ethtool_ops lan966x_ethtool_ops;
300 
301 bool lan966x_netdevice_check(const struct net_device *dev);
302 
303 void lan966x_register_notifier_blocks(void);
304 void lan966x_unregister_notifier_blocks(void);
305 
306 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb);
307 
308 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port);
309 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp);
310 
311 void lan966x_stats_get(struct net_device *dev,
312 		       struct rtnl_link_stats64 *stats);
313 int lan966x_stats_init(struct lan966x *lan966x);
314 
315 void lan966x_port_config_down(struct lan966x_port *port);
316 void lan966x_port_config_up(struct lan966x_port *port);
317 void lan966x_port_status_get(struct lan966x_port *port,
318 			     struct phylink_link_state *state);
319 int lan966x_port_pcs_set(struct lan966x_port *port,
320 			 struct lan966x_port_config *config);
321 void lan966x_port_init(struct lan966x_port *port);
322 
323 int lan966x_mac_ip_learn(struct lan966x *lan966x,
324 			 bool cpu_copy,
325 			 const unsigned char mac[ETH_ALEN],
326 			 unsigned int vid,
327 			 enum macaccess_entry_type type);
328 int lan966x_mac_learn(struct lan966x *lan966x, int port,
329 		      const unsigned char mac[ETH_ALEN],
330 		      unsigned int vid,
331 		      enum macaccess_entry_type type);
332 int lan966x_mac_forget(struct lan966x *lan966x,
333 		       const unsigned char mac[ETH_ALEN],
334 		       unsigned int vid,
335 		       enum macaccess_entry_type type);
336 int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid);
337 int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid);
338 void lan966x_mac_init(struct lan966x *lan966x);
339 void lan966x_mac_set_ageing(struct lan966x *lan966x,
340 			    u32 ageing);
341 int lan966x_mac_del_entry(struct lan966x *lan966x,
342 			  const unsigned char *addr,
343 			  u16 vid);
344 int lan966x_mac_add_entry(struct lan966x *lan966x,
345 			  struct lan966x_port *port,
346 			  const unsigned char *addr,
347 			  u16 vid);
348 void lan966x_mac_purge_entries(struct lan966x *lan966x);
349 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x);
350 
351 void lan966x_vlan_init(struct lan966x *lan966x);
352 void lan966x_vlan_port_apply(struct lan966x_port *port);
353 bool lan966x_vlan_cpu_member_cpu_vlan_mask(struct lan966x *lan966x, u16 vid);
354 void lan966x_vlan_port_set_vlan_aware(struct lan966x_port *port,
355 				      bool vlan_aware);
356 int lan966x_vlan_port_set_vid(struct lan966x_port *port,
357 			      u16 vid,
358 			      bool pvid,
359 			      bool untagged);
360 void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
361 				u16 vid,
362 				bool pvid,
363 				bool untagged);
364 void lan966x_vlan_port_del_vlan(struct lan966x_port *port, u16 vid);
365 void lan966x_vlan_cpu_add_vlan(struct lan966x *lan966x, u16 vid);
366 void lan966x_vlan_cpu_del_vlan(struct lan966x *lan966x, u16 vid);
367 
368 void lan966x_fdb_write_entries(struct lan966x *lan966x, u16 vid);
369 void lan966x_fdb_erase_entries(struct lan966x *lan966x, u16 vid);
370 int lan966x_fdb_init(struct lan966x *lan966x);
371 void lan966x_fdb_deinit(struct lan966x *lan966x);
372 int lan966x_handle_fdb(struct net_device *dev,
373 		       struct net_device *orig_dev,
374 		       unsigned long event, const void *ctx,
375 		       const struct switchdev_notifier_fdb_info *fdb_info);
376 
377 void lan966x_mdb_init(struct lan966x *lan966x);
378 void lan966x_mdb_deinit(struct lan966x *lan966x);
379 int lan966x_handle_port_mdb_add(struct lan966x_port *port,
380 				const struct switchdev_obj *obj);
381 int lan966x_handle_port_mdb_del(struct lan966x_port *port,
382 				const struct switchdev_obj *obj);
383 void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid);
384 void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid);
385 void lan966x_mdb_clear_entries(struct lan966x *lan966x);
386 void lan966x_mdb_restore_entries(struct lan966x *lan966x);
387 
388 int lan966x_ptp_init(struct lan966x *lan966x);
389 void lan966x_ptp_deinit(struct lan966x *lan966x);
390 int lan966x_ptp_hwtstamp_set(struct lan966x_port *port, struct ifreq *ifr);
391 int lan966x_ptp_hwtstamp_get(struct lan966x_port *port, struct ifreq *ifr);
392 void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb,
393 			  u64 timestamp);
394 int lan966x_ptp_txtstamp_request(struct lan966x_port *port,
395 				 struct sk_buff *skb);
396 void lan966x_ptp_txtstamp_release(struct lan966x_port *port,
397 				  struct sk_buff *skb);
398 irqreturn_t lan966x_ptp_irq_handler(int irq, void *args);
399 irqreturn_t lan966x_ptp_ext_irq_handler(int irq, void *args);
400 
401 int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev);
402 int lan966x_fdma_change_mtu(struct lan966x *lan966x);
403 void lan966x_fdma_netdev_init(struct lan966x *lan966x, struct net_device *dev);
404 void lan966x_fdma_netdev_deinit(struct lan966x *lan966x, struct net_device *dev);
405 int lan966x_fdma_init(struct lan966x *lan966x);
406 void lan966x_fdma_deinit(struct lan966x *lan966x);
407 irqreturn_t lan966x_fdma_irq_handler(int irq, void *args);
408 
409 static inline void __iomem *lan_addr(void __iomem *base[],
410 				     int id, int tinst, int tcnt,
411 				     int gbase, int ginst,
412 				     int gcnt, int gwidth,
413 				     int raddr, int rinst,
414 				     int rcnt, int rwidth)
415 {
416 	WARN_ON((tinst) >= tcnt);
417 	WARN_ON((ginst) >= gcnt);
418 	WARN_ON((rinst) >= rcnt);
419 	return base[id + (tinst)] +
420 		gbase + ((ginst) * gwidth) +
421 		raddr + ((rinst) * rwidth);
422 }
423 
424 static inline u32 lan_rd(struct lan966x *lan966x, int id, int tinst, int tcnt,
425 			 int gbase, int ginst, int gcnt, int gwidth,
426 			 int raddr, int rinst, int rcnt, int rwidth)
427 {
428 	return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
429 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
430 }
431 
432 static inline void lan_wr(u32 val, struct lan966x *lan966x,
433 			  int id, int tinst, int tcnt,
434 			  int gbase, int ginst, int gcnt, int gwidth,
435 			  int raddr, int rinst, int rcnt, int rwidth)
436 {
437 	writel(val, lan_addr(lan966x->regs, id, tinst, tcnt,
438 			     gbase, ginst, gcnt, gwidth,
439 			     raddr, rinst, rcnt, rwidth));
440 }
441 
442 static inline void lan_rmw(u32 val, u32 mask, struct lan966x *lan966x,
443 			   int id, int tinst, int tcnt,
444 			   int gbase, int ginst, int gcnt, int gwidth,
445 			   int raddr, int rinst, int rcnt, int rwidth)
446 {
447 	u32 nval;
448 
449 	nval = readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
450 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
451 	nval = (nval & ~mask) | (val & mask);
452 	writel(nval, lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
453 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
454 }
455 
456 #endif /* __LAN966X_MAIN_H__ */
457