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