1 /* SPDX-License-Identifier: GPL-2.0+ */
3 #ifndef __LAN966X_MAIN_H__
4 #define __LAN966X_MAIN_H__
6 #include <linux/etherdevice.h>
7 #include <linux/if_vlan.h>
8 #include <linux/jiffies.h>
10 #include <linux/phylink.h>
11 #include <linux/ptp_clock_kernel.h>
12 #include <net/switchdev.h>
14 #include "lan966x_regs.h"
15 #include "lan966x_ifh.h"
17 #define TABLE_UPDATE_SLEEP_US 10
18 #define TABLE_UPDATE_TIMEOUT_US 100000
20 #define READL_SLEEP_US 10
21 #define READL_TIMEOUT_US 100000000
23 #define LAN966X_BUFFER_CELL_SZ 64
24 #define LAN966X_BUFFER_MEMORY (160 * 1024)
25 #define LAN966X_BUFFER_MIN_SZ 60
29 #define PGID_ENTRIES 89
31 #define UNAWARE_PVID 0
32 #define HOST_PVID 4095
34 /* Reserved amount for (SRC, PRIO) at index 8*SRC + PRIO */
35 #define QSYS_Q_RSRV 95
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)
47 /* Non-reserved PGIDs, used for general purpose */
48 #define PGID_GP_START (CPU_PORT + 1)
49 #define PGID_GP_END PGID_CPU
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
57 #define LAN966X_PHC_COUNT 3
58 #define LAN966X_PHC_PORT 0
59 #define LAN966X_PHC_PINS_NUM 7
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
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))
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
77 #define FDMA_XTR_CHANNEL 6
78 #define FDMA_INJ_CHANNEL 0
79 #define FDMA_DCB_MAX 512
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.
87 enum macaccess_entry_type {
101 struct lan966x_rx_dcb {
104 struct lan966x_db db[FDMA_RX_DCB_MAX_DBS];
107 struct lan966x_tx_dcb {
110 struct lan966x_db db[FDMA_TX_DCB_MAX_DBS];
114 struct lan966x *lan966x;
116 /* Pointer to the array of hardware dcbs. */
117 struct lan966x_rx_dcb *dcbs;
119 /* Pointer to the last address in the dcbs. */
120 struct lan966x_rx_dcb *last_entry;
122 /* For each DB, there is a page */
123 struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
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.
131 /* Represents the index in the dcbs. It has a value between 0 and
136 /* Represents the dma address to the dcbs array */
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.
147 struct lan966x_tx_dcb_buf {
148 struct net_device *dev;
156 struct lan966x *lan966x;
158 /* Pointer to the dcb list */
159 struct lan966x_tx_dcb *dcbs;
162 /* Represents the DMA address to the first entry of the dcb entries. */
165 /* Array of dcbs that are given to the HW */
166 struct lan966x_tx_dcb_buf *dcbs_buf;
173 struct lan966x_stat_layout {
175 char name[ETH_GSTRING_LEN];
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;
187 struct lan966x_skb_cb {
190 unsigned long jiffies;
193 #define LAN966X_PTP_TIMEOUT msecs_to_jiffies(10)
194 #define LAN966X_SKB_CB(skb) \
195 ((struct lan966x_skb_cb *)((skb)->cb))
201 struct lan966x_port **ports;
203 void __iomem *regs[NUM_TARGETS];
207 u8 base_mac[ETH_ALEN];
209 spinlock_t tx_lock; /* lock for frame transmition */
211 struct net_device *bridge;
215 struct list_head mac_entries;
216 spinlock_t mac_lock; /* lock for mac_entries list */
218 u16 vlan_mask[VLAN_N_VID];
219 DECLARE_BITMAP(cpu_vlan_mask, VLAN_N_VID);
222 const struct lan966x_stat_layout *stats_layout;
225 /* workqueue for reading stats */
226 struct mutex stats_lock;
228 struct delayed_work stats_work;
229 struct workqueue_struct *stats_queue;
238 /* worqueue for fdb */
239 struct workqueue_struct *fdb_work;
240 struct list_head fdb_entries;
243 struct list_head mdb_entries;
244 struct list_head pgid_entries;
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 */
256 struct net_device *fdma_ndev;
257 struct lan966x_rx rx;
258 struct lan966x_tx tx;
259 struct napi_struct napi;
262 struct lan966x_port_config {
263 phy_interface_t portmode;
264 const unsigned long *advertising;
272 struct lan966x_port {
273 struct net_device *dev;
274 struct lan966x *lan966x;
284 struct phylink_config phylink_config;
285 struct phylink_pcs phylink_pcs;
286 struct lan966x_port_config config;
287 struct phylink *phylink;
289 struct fwnode_handle *fwnode;
293 struct sk_buff_head tx_skbs;
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;
300 bool lan966x_netdevice_check(const struct net_device *dev);
302 void lan966x_register_notifier_blocks(void);
303 void lan966x_unregister_notifier_blocks(void);
305 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb);
307 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port);
308 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp);
310 void lan966x_stats_get(struct net_device *dev,
311 struct rtnl_link_stats64 *stats);
312 int lan966x_stats_init(struct lan966x *lan966x);
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);
322 int lan966x_mac_ip_learn(struct lan966x *lan966x,
324 const unsigned char mac[ETH_ALEN],
326 enum macaccess_entry_type type);
327 int lan966x_mac_learn(struct lan966x *lan966x, int port,
328 const unsigned char mac[ETH_ALEN],
330 enum macaccess_entry_type type);
331 int lan966x_mac_forget(struct lan966x *lan966x,
332 const unsigned char mac[ETH_ALEN],
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,
340 int lan966x_mac_del_entry(struct lan966x *lan966x,
341 const unsigned char *addr,
343 int lan966x_mac_add_entry(struct lan966x *lan966x,
344 struct lan966x_port *port,
345 const unsigned char *addr,
347 void lan966x_mac_purge_entries(struct lan966x *lan966x);
348 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x);
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,
355 int lan966x_vlan_port_set_vid(struct lan966x_port *port,
359 void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
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);
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);
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);
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,
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);
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);
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)
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);
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)
427 return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
428 gcnt, gwidth, raddr, rinst, rcnt, rwidth));
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)
436 writel(val, lan_addr(lan966x->regs, id, tinst, tcnt,
437 gbase, ginst, gcnt, gwidth,
438 raddr, rinst, rcnt, rwidth));
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)
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));
455 #endif /* __LAN966X_MAIN_H__ */