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
37 #define NUM_PHYS_PORTS 8
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)
48 /* Non-reserved PGIDs, used for general purpose */
49 #define PGID_GP_START (CPU_PORT + 1)
50 #define PGID_GP_END PGID_CPU
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
58 #define LAN966X_PHC_COUNT 3
59 #define LAN966X_PHC_PORT 0
60 #define LAN966X_PHC_PINS_NUM 7
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
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))
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
78 #define FDMA_XTR_CHANNEL 6
79 #define FDMA_INJ_CHANNEL 0
80 #define FDMA_DCB_MAX 512
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.
88 enum macaccess_entry_type {
102 struct lan966x_rx_dcb {
105 struct lan966x_db db[FDMA_RX_DCB_MAX_DBS];
108 struct lan966x_tx_dcb {
111 struct lan966x_db db[FDMA_TX_DCB_MAX_DBS];
115 struct lan966x *lan966x;
117 /* Pointer to the array of hardware dcbs. */
118 struct lan966x_rx_dcb *dcbs;
120 /* Pointer to the last address in the dcbs. */
121 struct lan966x_rx_dcb *last_entry;
123 /* For each DB, there is a page */
124 struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
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.
132 /* Represents the index in the dcbs. It has a value between 0 and
137 /* Represents the dma address to the dcbs array */
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.
148 struct lan966x_tx_dcb_buf {
149 struct net_device *dev;
157 struct lan966x *lan966x;
159 /* Pointer to the dcb list */
160 struct lan966x_tx_dcb *dcbs;
163 /* Represents the DMA address to the first entry of the dcb entries. */
166 /* Array of dcbs that are given to the HW */
167 struct lan966x_tx_dcb_buf *dcbs_buf;
174 struct lan966x_stat_layout {
176 char name[ETH_GSTRING_LEN];
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;
188 struct lan966x_skb_cb {
191 unsigned long jiffies;
194 #define LAN966X_PTP_TIMEOUT msecs_to_jiffies(10)
195 #define LAN966X_SKB_CB(skb) \
196 ((struct lan966x_skb_cb *)((skb)->cb))
202 struct lan966x_port **ports;
204 void __iomem *regs[NUM_TARGETS];
208 u8 base_mac[ETH_ALEN];
210 spinlock_t tx_lock; /* lock for frame transmition */
212 struct net_device *bridge;
216 struct list_head mac_entries;
217 spinlock_t mac_lock; /* lock for mac_entries list */
219 u16 vlan_mask[VLAN_N_VID];
220 DECLARE_BITMAP(cpu_vlan_mask, VLAN_N_VID);
223 const struct lan966x_stat_layout *stats_layout;
226 /* workqueue for reading stats */
227 struct mutex stats_lock;
229 struct delayed_work stats_work;
230 struct workqueue_struct *stats_queue;
239 /* worqueue for fdb */
240 struct workqueue_struct *fdb_work;
241 struct list_head fdb_entries;
244 struct list_head mdb_entries;
245 struct list_head pgid_entries;
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 */
257 struct net_device *fdma_ndev;
258 struct lan966x_rx rx;
259 struct lan966x_tx tx;
260 struct napi_struct napi;
263 struct lan966x_port_config {
264 phy_interface_t portmode;
265 const unsigned long *advertising;
273 struct lan966x_port {
274 struct net_device *dev;
275 struct lan966x *lan966x;
285 struct phylink_config phylink_config;
286 struct phylink_pcs phylink_pcs;
287 struct lan966x_port_config config;
288 struct phylink *phylink;
290 struct fwnode_handle *fwnode;
294 struct sk_buff_head tx_skbs;
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;
301 bool lan966x_netdevice_check(const struct net_device *dev);
303 void lan966x_register_notifier_blocks(void);
304 void lan966x_unregister_notifier_blocks(void);
306 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb);
308 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port);
309 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp);
311 void lan966x_stats_get(struct net_device *dev,
312 struct rtnl_link_stats64 *stats);
313 int lan966x_stats_init(struct lan966x *lan966x);
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);
323 int lan966x_mac_ip_learn(struct lan966x *lan966x,
325 const unsigned char mac[ETH_ALEN],
327 enum macaccess_entry_type type);
328 int lan966x_mac_learn(struct lan966x *lan966x, int port,
329 const unsigned char mac[ETH_ALEN],
331 enum macaccess_entry_type type);
332 int lan966x_mac_forget(struct lan966x *lan966x,
333 const unsigned char mac[ETH_ALEN],
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,
341 int lan966x_mac_del_entry(struct lan966x *lan966x,
342 const unsigned char *addr,
344 int lan966x_mac_add_entry(struct lan966x *lan966x,
345 struct lan966x_port *port,
346 const unsigned char *addr,
348 void lan966x_mac_purge_entries(struct lan966x *lan966x);
349 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x);
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,
356 int lan966x_vlan_port_set_vid(struct lan966x_port *port,
360 void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
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);
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);
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);
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,
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);
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);
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)
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);
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)
428 return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
429 gcnt, gwidth, raddr, rinst, rcnt, rwidth));
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)
437 writel(val, lan_addr(lan966x->regs, id, tinst, tcnt,
438 gbase, ginst, gcnt, gwidth,
439 raddr, rinst, rcnt, rwidth));
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)
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));
456 #endif /* __LAN966X_MAIN_H__ */