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 	struct net_device *bond;
297 	bool lag_tx_active;
298 	enum netdev_lag_hash hash_type;
299 };
300 
301 extern const struct phylink_mac_ops lan966x_phylink_mac_ops;
302 extern const struct phylink_pcs_ops lan966x_phylink_pcs_ops;
303 extern const struct ethtool_ops lan966x_ethtool_ops;
304 extern struct notifier_block lan966x_switchdev_nb __read_mostly;
305 extern struct notifier_block lan966x_switchdev_blocking_nb __read_mostly;
306 
307 bool lan966x_netdevice_check(const struct net_device *dev);
308 
309 void lan966x_register_notifier_blocks(void);
310 void lan966x_unregister_notifier_blocks(void);
311 
312 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb);
313 
314 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port);
315 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp);
316 
317 void lan966x_stats_get(struct net_device *dev,
318 		       struct rtnl_link_stats64 *stats);
319 int lan966x_stats_init(struct lan966x *lan966x);
320 
321 void lan966x_port_config_down(struct lan966x_port *port);
322 void lan966x_port_config_up(struct lan966x_port *port);
323 void lan966x_port_status_get(struct lan966x_port *port,
324 			     struct phylink_link_state *state);
325 int lan966x_port_pcs_set(struct lan966x_port *port,
326 			 struct lan966x_port_config *config);
327 void lan966x_port_init(struct lan966x_port *port);
328 
329 int lan966x_mac_ip_learn(struct lan966x *lan966x,
330 			 bool cpu_copy,
331 			 const unsigned char mac[ETH_ALEN],
332 			 unsigned int vid,
333 			 enum macaccess_entry_type type);
334 int lan966x_mac_learn(struct lan966x *lan966x, int port,
335 		      const unsigned char mac[ETH_ALEN],
336 		      unsigned int vid,
337 		      enum macaccess_entry_type type);
338 int lan966x_mac_forget(struct lan966x *lan966x,
339 		       const unsigned char mac[ETH_ALEN],
340 		       unsigned int vid,
341 		       enum macaccess_entry_type type);
342 int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid);
343 int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid);
344 void lan966x_mac_init(struct lan966x *lan966x);
345 void lan966x_mac_set_ageing(struct lan966x *lan966x,
346 			    u32 ageing);
347 int lan966x_mac_del_entry(struct lan966x *lan966x,
348 			  const unsigned char *addr,
349 			  u16 vid);
350 int lan966x_mac_add_entry(struct lan966x *lan966x,
351 			  struct lan966x_port *port,
352 			  const unsigned char *addr,
353 			  u16 vid);
354 void lan966x_mac_lag_replace_port_entry(struct lan966x *lan966x,
355 					struct lan966x_port *src,
356 					struct lan966x_port *dst);
357 void lan966x_mac_lag_remove_port_entry(struct lan966x *lan966x,
358 				       struct lan966x_port *src);
359 void lan966x_mac_purge_entries(struct lan966x *lan966x);
360 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x);
361 
362 void lan966x_vlan_init(struct lan966x *lan966x);
363 void lan966x_vlan_port_apply(struct lan966x_port *port);
364 bool lan966x_vlan_cpu_member_cpu_vlan_mask(struct lan966x *lan966x, u16 vid);
365 void lan966x_vlan_port_set_vlan_aware(struct lan966x_port *port,
366 				      bool vlan_aware);
367 int lan966x_vlan_port_set_vid(struct lan966x_port *port,
368 			      u16 vid,
369 			      bool pvid,
370 			      bool untagged);
371 void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
372 				u16 vid,
373 				bool pvid,
374 				bool untagged);
375 void lan966x_vlan_port_del_vlan(struct lan966x_port *port, u16 vid);
376 void lan966x_vlan_cpu_add_vlan(struct lan966x *lan966x, u16 vid);
377 void lan966x_vlan_cpu_del_vlan(struct lan966x *lan966x, u16 vid);
378 
379 void lan966x_fdb_write_entries(struct lan966x *lan966x, u16 vid);
380 void lan966x_fdb_erase_entries(struct lan966x *lan966x, u16 vid);
381 int lan966x_fdb_init(struct lan966x *lan966x);
382 void lan966x_fdb_deinit(struct lan966x *lan966x);
383 void lan966x_fdb_flush_workqueue(struct lan966x *lan966x);
384 int lan966x_handle_fdb(struct net_device *dev,
385 		       struct net_device *orig_dev,
386 		       unsigned long event, const void *ctx,
387 		       const struct switchdev_notifier_fdb_info *fdb_info);
388 
389 void lan966x_mdb_init(struct lan966x *lan966x);
390 void lan966x_mdb_deinit(struct lan966x *lan966x);
391 int lan966x_handle_port_mdb_add(struct lan966x_port *port,
392 				const struct switchdev_obj *obj);
393 int lan966x_handle_port_mdb_del(struct lan966x_port *port,
394 				const struct switchdev_obj *obj);
395 void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid);
396 void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid);
397 void lan966x_mdb_clear_entries(struct lan966x *lan966x);
398 void lan966x_mdb_restore_entries(struct lan966x *lan966x);
399 
400 int lan966x_ptp_init(struct lan966x *lan966x);
401 void lan966x_ptp_deinit(struct lan966x *lan966x);
402 int lan966x_ptp_hwtstamp_set(struct lan966x_port *port, struct ifreq *ifr);
403 int lan966x_ptp_hwtstamp_get(struct lan966x_port *port, struct ifreq *ifr);
404 void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb,
405 			  u64 timestamp);
406 int lan966x_ptp_txtstamp_request(struct lan966x_port *port,
407 				 struct sk_buff *skb);
408 void lan966x_ptp_txtstamp_release(struct lan966x_port *port,
409 				  struct sk_buff *skb);
410 irqreturn_t lan966x_ptp_irq_handler(int irq, void *args);
411 irqreturn_t lan966x_ptp_ext_irq_handler(int irq, void *args);
412 
413 int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev);
414 int lan966x_fdma_change_mtu(struct lan966x *lan966x);
415 void lan966x_fdma_netdev_init(struct lan966x *lan966x, struct net_device *dev);
416 void lan966x_fdma_netdev_deinit(struct lan966x *lan966x, struct net_device *dev);
417 int lan966x_fdma_init(struct lan966x *lan966x);
418 void lan966x_fdma_deinit(struct lan966x *lan966x);
419 irqreturn_t lan966x_fdma_irq_handler(int irq, void *args);
420 
421 int lan966x_lag_port_join(struct lan966x_port *port,
422 			  struct net_device *brport_dev,
423 			  struct net_device *bond,
424 			  struct netlink_ext_ack *extack);
425 void lan966x_lag_port_leave(struct lan966x_port *port, struct net_device *bond);
426 int lan966x_lag_port_prechangeupper(struct net_device *dev,
427 				    struct netdev_notifier_changeupper_info *info);
428 int lan966x_lag_port_changelowerstate(struct net_device *dev,
429 				      struct netdev_notifier_changelowerstate_info *info);
430 int lan966x_lag_netdev_prechangeupper(struct net_device *dev,
431 				      struct netdev_notifier_changeupper_info *info);
432 int lan966x_lag_netdev_changeupper(struct net_device *dev,
433 				   struct netdev_notifier_changeupper_info *info);
434 bool lan966x_lag_first_port(struct net_device *lag, struct net_device *dev);
435 u32 lan966x_lag_get_mask(struct lan966x *lan966x, struct net_device *bond);
436 
437 int lan966x_port_changeupper(struct net_device *dev,
438 			     struct net_device *brport_dev,
439 			     struct netdev_notifier_changeupper_info *info);
440 int lan966x_port_prechangeupper(struct net_device *dev,
441 				struct net_device *brport_dev,
442 				struct netdev_notifier_changeupper_info *info);
443 void lan966x_port_stp_state_set(struct lan966x_port *port, u8 state);
444 void lan966x_port_ageing_set(struct lan966x_port *port,
445 			     unsigned long ageing_clock_t);
446 void lan966x_update_fwd_mask(struct lan966x *lan966x);
447 
448 static inline void __iomem *lan_addr(void __iomem *base[],
449 				     int id, int tinst, int tcnt,
450 				     int gbase, int ginst,
451 				     int gcnt, int gwidth,
452 				     int raddr, int rinst,
453 				     int rcnt, int rwidth)
454 {
455 	WARN_ON((tinst) >= tcnt);
456 	WARN_ON((ginst) >= gcnt);
457 	WARN_ON((rinst) >= rcnt);
458 	return base[id + (tinst)] +
459 		gbase + ((ginst) * gwidth) +
460 		raddr + ((rinst) * rwidth);
461 }
462 
463 static inline u32 lan_rd(struct lan966x *lan966x, int id, int tinst, int tcnt,
464 			 int gbase, int ginst, int gcnt, int gwidth,
465 			 int raddr, int rinst, int rcnt, int rwidth)
466 {
467 	return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
468 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
469 }
470 
471 static inline void lan_wr(u32 val, struct lan966x *lan966x,
472 			  int id, int tinst, int tcnt,
473 			  int gbase, int ginst, int gcnt, int gwidth,
474 			  int raddr, int rinst, int rcnt, int rwidth)
475 {
476 	writel(val, lan_addr(lan966x->regs, id, tinst, tcnt,
477 			     gbase, ginst, gcnt, gwidth,
478 			     raddr, rinst, rcnt, rwidth));
479 }
480 
481 static inline void lan_rmw(u32 val, u32 mask, struct lan966x *lan966x,
482 			   int id, int tinst, int tcnt,
483 			   int gbase, int ginst, int gcnt, int gwidth,
484 			   int raddr, int rinst, int rcnt, int rwidth)
485 {
486 	u32 nval;
487 
488 	nval = readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
489 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
490 	nval = (nval & ~mask) | (val & mask);
491 	writel(nval, lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
492 			      gcnt, gwidth, raddr, rinst, rcnt, rwidth));
493 }
494 
495 #endif /* __LAN966X_MAIN_H__ */
496