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