Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 
0003 #ifndef __LAN966X_MAIN_H__
0004 #define __LAN966X_MAIN_H__
0005 
0006 #include <linux/etherdevice.h>
0007 #include <linux/if_vlan.h>
0008 #include <linux/jiffies.h>
0009 #include <linux/phy.h>
0010 #include <linux/phylink.h>
0011 #include <linux/ptp_clock_kernel.h>
0012 #include <net/switchdev.h>
0013 
0014 #include "lan966x_regs.h"
0015 #include "lan966x_ifh.h"
0016 
0017 #define TABLE_UPDATE_SLEEP_US       10
0018 #define TABLE_UPDATE_TIMEOUT_US     100000
0019 
0020 #define READL_SLEEP_US          10
0021 #define READL_TIMEOUT_US        100000000
0022 
0023 #define LAN966X_BUFFER_CELL_SZ      64
0024 #define LAN966X_BUFFER_MEMORY       (160 * 1024)
0025 #define LAN966X_BUFFER_MIN_SZ       60
0026 
0027 #define PGID_AGGR           64
0028 #define PGID_SRC            80
0029 #define PGID_ENTRIES            89
0030 
0031 #define UNAWARE_PVID            0
0032 #define HOST_PVID           4095
0033 
0034 /* Reserved amount for (SRC, PRIO) at index 8*SRC + PRIO */
0035 #define QSYS_Q_RSRV         95
0036 
0037 #define NUM_PHYS_PORTS          8
0038 #define CPU_PORT            8
0039 
0040 /* Reserved PGIDs */
0041 #define PGID_CPU            (PGID_AGGR - 6)
0042 #define PGID_UC             (PGID_AGGR - 5)
0043 #define PGID_BC             (PGID_AGGR - 4)
0044 #define PGID_MC             (PGID_AGGR - 3)
0045 #define PGID_MCIPV4         (PGID_AGGR - 2)
0046 #define PGID_MCIPV6         (PGID_AGGR - 1)
0047 
0048 /* Non-reserved PGIDs, used for general purpose */
0049 #define PGID_GP_START           (CPU_PORT + 1)
0050 #define PGID_GP_END         PGID_CPU
0051 
0052 #define LAN966X_SPEED_NONE      0
0053 #define LAN966X_SPEED_2500      1
0054 #define LAN966X_SPEED_1000      1
0055 #define LAN966X_SPEED_100       2
0056 #define LAN966X_SPEED_10        3
0057 
0058 #define LAN966X_PHC_COUNT       3
0059 #define LAN966X_PHC_PORT        0
0060 #define LAN966X_PHC_PINS_NUM        7
0061 
0062 #define IFH_REW_OP_NOOP         0x0
0063 #define IFH_REW_OP_ONE_STEP_PTP     0x3
0064 #define IFH_REW_OP_TWO_STEP_PTP     0x4
0065 
0066 #define FDMA_RX_DCB_MAX_DBS     1
0067 #define FDMA_TX_DCB_MAX_DBS     1
0068 #define FDMA_DCB_INFO_DATAL(x)      ((x) & GENMASK(15, 0))
0069 
0070 #define FDMA_DCB_STATUS_BLOCKL(x)   ((x) & GENMASK(15, 0))
0071 #define FDMA_DCB_STATUS_SOF     BIT(16)
0072 #define FDMA_DCB_STATUS_EOF     BIT(17)
0073 #define FDMA_DCB_STATUS_INTR        BIT(18)
0074 #define FDMA_DCB_STATUS_DONE        BIT(19)
0075 #define FDMA_DCB_STATUS_BLOCKO(x)   (((x) << 20) & GENMASK(31, 20))
0076 #define FDMA_DCB_INVALID_DATA       0x1
0077 
0078 #define FDMA_XTR_CHANNEL        6
0079 #define FDMA_INJ_CHANNEL        0
0080 #define FDMA_DCB_MAX            512
0081 
0082 /* MAC table entry types.
0083  * ENTRYTYPE_NORMAL is subject to aging.
0084  * ENTRYTYPE_LOCKED is not subject to aging.
0085  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
0086  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
0087  */
0088 enum macaccess_entry_type {
0089     ENTRYTYPE_NORMAL = 0,
0090     ENTRYTYPE_LOCKED,
0091     ENTRYTYPE_MACV4,
0092     ENTRYTYPE_MACV6,
0093 };
0094 
0095 struct lan966x_port;
0096 
0097 struct lan966x_db {
0098     u64 dataptr;
0099     u64 status;
0100 };
0101 
0102 struct lan966x_rx_dcb {
0103     u64 nextptr;
0104     u64 info;
0105     struct lan966x_db db[FDMA_RX_DCB_MAX_DBS];
0106 };
0107 
0108 struct lan966x_tx_dcb {
0109     u64 nextptr;
0110     u64 info;
0111     struct lan966x_db db[FDMA_TX_DCB_MAX_DBS];
0112 };
0113 
0114 struct lan966x_rx {
0115     struct lan966x *lan966x;
0116 
0117     /* Pointer to the array of hardware dcbs. */
0118     struct lan966x_rx_dcb *dcbs;
0119 
0120     /* Pointer to the last address in the dcbs. */
0121     struct lan966x_rx_dcb *last_entry;
0122 
0123     /* For each DB, there is a page */
0124     struct page *page[FDMA_DCB_MAX][FDMA_RX_DCB_MAX_DBS];
0125 
0126     /* Represents the db_index, it can have a value between 0 and
0127      * FDMA_RX_DCB_MAX_DBS, once it reaches the value of FDMA_RX_DCB_MAX_DBS
0128      * it means that the DCB can be reused.
0129      */
0130     int db_index;
0131 
0132     /* Represents the index in the dcbs. It has a value between 0 and
0133      * FDMA_DCB_MAX
0134      */
0135     int dcb_index;
0136 
0137     /* Represents the dma address to the dcbs array */
0138     dma_addr_t dma;
0139 
0140     /* Represents the page order that is used to allocate the pages for the
0141      * RX buffers. This value is calculated based on max MTU of the devices.
0142      */
0143     u8 page_order;
0144 
0145     u8 channel_id;
0146 };
0147 
0148 struct lan966x_tx_dcb_buf {
0149     struct net_device *dev;
0150     struct sk_buff *skb;
0151     dma_addr_t dma_addr;
0152     bool used;
0153     bool ptp;
0154 };
0155 
0156 struct lan966x_tx {
0157     struct lan966x *lan966x;
0158 
0159     /* Pointer to the dcb list */
0160     struct lan966x_tx_dcb *dcbs;
0161     u16 last_in_use;
0162 
0163     /* Represents the DMA address to the first entry of the dcb entries. */
0164     dma_addr_t dma;
0165 
0166     /* Array of dcbs that are given to the HW */
0167     struct lan966x_tx_dcb_buf *dcbs_buf;
0168 
0169     u8 channel_id;
0170 
0171     bool activated;
0172 };
0173 
0174 struct lan966x_stat_layout {
0175     u32 offset;
0176     char name[ETH_GSTRING_LEN];
0177 };
0178 
0179 struct lan966x_phc {
0180     struct ptp_clock *clock;
0181     struct ptp_clock_info info;
0182     struct ptp_pin_desc pins[LAN966X_PHC_PINS_NUM];
0183     struct hwtstamp_config hwtstamp_config;
0184     struct lan966x *lan966x;
0185     u8 index;
0186 };
0187 
0188 struct lan966x_skb_cb {
0189     u8 rew_op;
0190     u16 ts_id;
0191     unsigned long jiffies;
0192 };
0193 
0194 #define LAN966X_PTP_TIMEOUT     msecs_to_jiffies(10)
0195 #define LAN966X_SKB_CB(skb) \
0196     ((struct lan966x_skb_cb *)((skb)->cb))
0197 
0198 struct lan966x {
0199     struct device *dev;
0200 
0201     u8 num_phys_ports;
0202     struct lan966x_port **ports;
0203 
0204     void __iomem *regs[NUM_TARGETS];
0205 
0206     int shared_queue_sz;
0207 
0208     u8 base_mac[ETH_ALEN];
0209 
0210     spinlock_t tx_lock; /* lock for frame transmition */
0211 
0212     struct net_device *bridge;
0213     u16 bridge_mask;
0214     u16 bridge_fwd_mask;
0215 
0216     struct list_head mac_entries;
0217     spinlock_t mac_lock; /* lock for mac_entries list */
0218 
0219     u16 vlan_mask[VLAN_N_VID];
0220     DECLARE_BITMAP(cpu_vlan_mask, VLAN_N_VID);
0221 
0222     /* stats */
0223     const struct lan966x_stat_layout *stats_layout;
0224     u32 num_stats;
0225 
0226     /* workqueue for reading stats */
0227     struct mutex stats_lock;
0228     u64 *stats;
0229     struct delayed_work stats_work;
0230     struct workqueue_struct *stats_queue;
0231 
0232     /* interrupts */
0233     int xtr_irq;
0234     int ana_irq;
0235     int ptp_irq;
0236     int fdma_irq;
0237     int ptp_ext_irq;
0238 
0239     /* worqueue for fdb */
0240     struct workqueue_struct *fdb_work;
0241     struct list_head fdb_entries;
0242 
0243     /* mdb */
0244     struct list_head mdb_entries;
0245     struct list_head pgid_entries;
0246 
0247     /* ptp */
0248     bool ptp;
0249     struct lan966x_phc phc[LAN966X_PHC_COUNT];
0250     spinlock_t ptp_clock_lock; /* lock for phc */
0251     spinlock_t ptp_ts_id_lock; /* lock for ts_id */
0252     struct mutex ptp_lock; /* lock for ptp interface state */
0253     u16 ptp_skbs;
0254 
0255     /* fdma */
0256     bool fdma;
0257     struct net_device *fdma_ndev;
0258     struct lan966x_rx rx;
0259     struct lan966x_tx tx;
0260     struct napi_struct napi;
0261 };
0262 
0263 struct lan966x_port_config {
0264     phy_interface_t portmode;
0265     const unsigned long *advertising;
0266     int speed;
0267     int duplex;
0268     u32 pause;
0269     bool inband;
0270     bool autoneg;
0271 };
0272 
0273 struct lan966x_port {
0274     struct net_device *dev;
0275     struct lan966x *lan966x;
0276 
0277     u8 chip_port;
0278     u16 pvid;
0279     u16 vid;
0280     bool vlan_aware;
0281 
0282     bool learn_ena;
0283     bool mcast_ena;
0284 
0285     struct phylink_config phylink_config;
0286     struct phylink_pcs phylink_pcs;
0287     struct lan966x_port_config config;
0288     struct phylink *phylink;
0289     struct phy *serdes;
0290     struct fwnode_handle *fwnode;
0291 
0292     u8 ptp_cmd;
0293     u16 ts_id;
0294     struct sk_buff_head tx_skbs;
0295 };
0296 
0297 extern const struct phylink_mac_ops lan966x_phylink_mac_ops;
0298 extern const struct phylink_pcs_ops lan966x_phylink_pcs_ops;
0299 extern const struct ethtool_ops lan966x_ethtool_ops;
0300 
0301 bool lan966x_netdevice_check(const struct net_device *dev);
0302 
0303 void lan966x_register_notifier_blocks(void);
0304 void lan966x_unregister_notifier_blocks(void);
0305 
0306 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb);
0307 
0308 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port);
0309 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp);
0310 
0311 void lan966x_stats_get(struct net_device *dev,
0312                struct rtnl_link_stats64 *stats);
0313 int lan966x_stats_init(struct lan966x *lan966x);
0314 
0315 void lan966x_port_config_down(struct lan966x_port *port);
0316 void lan966x_port_config_up(struct lan966x_port *port);
0317 void lan966x_port_status_get(struct lan966x_port *port,
0318                  struct phylink_link_state *state);
0319 int lan966x_port_pcs_set(struct lan966x_port *port,
0320              struct lan966x_port_config *config);
0321 void lan966x_port_init(struct lan966x_port *port);
0322 
0323 int lan966x_mac_ip_learn(struct lan966x *lan966x,
0324              bool cpu_copy,
0325              const unsigned char mac[ETH_ALEN],
0326              unsigned int vid,
0327              enum macaccess_entry_type type);
0328 int lan966x_mac_learn(struct lan966x *lan966x, int port,
0329               const unsigned char mac[ETH_ALEN],
0330               unsigned int vid,
0331               enum macaccess_entry_type type);
0332 int lan966x_mac_forget(struct lan966x *lan966x,
0333                const unsigned char mac[ETH_ALEN],
0334                unsigned int vid,
0335                enum macaccess_entry_type type);
0336 int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid);
0337 int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid);
0338 void lan966x_mac_init(struct lan966x *lan966x);
0339 void lan966x_mac_set_ageing(struct lan966x *lan966x,
0340                 u32 ageing);
0341 int lan966x_mac_del_entry(struct lan966x *lan966x,
0342               const unsigned char *addr,
0343               u16 vid);
0344 int lan966x_mac_add_entry(struct lan966x *lan966x,
0345               struct lan966x_port *port,
0346               const unsigned char *addr,
0347               u16 vid);
0348 void lan966x_mac_purge_entries(struct lan966x *lan966x);
0349 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x);
0350 
0351 void lan966x_vlan_init(struct lan966x *lan966x);
0352 void lan966x_vlan_port_apply(struct lan966x_port *port);
0353 bool lan966x_vlan_cpu_member_cpu_vlan_mask(struct lan966x *lan966x, u16 vid);
0354 void lan966x_vlan_port_set_vlan_aware(struct lan966x_port *port,
0355                       bool vlan_aware);
0356 int lan966x_vlan_port_set_vid(struct lan966x_port *port,
0357                   u16 vid,
0358                   bool pvid,
0359                   bool untagged);
0360 void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
0361                 u16 vid,
0362                 bool pvid,
0363                 bool untagged);
0364 void lan966x_vlan_port_del_vlan(struct lan966x_port *port, u16 vid);
0365 void lan966x_vlan_cpu_add_vlan(struct lan966x *lan966x, u16 vid);
0366 void lan966x_vlan_cpu_del_vlan(struct lan966x *lan966x, u16 vid);
0367 
0368 void lan966x_fdb_write_entries(struct lan966x *lan966x, u16 vid);
0369 void lan966x_fdb_erase_entries(struct lan966x *lan966x, u16 vid);
0370 int lan966x_fdb_init(struct lan966x *lan966x);
0371 void lan966x_fdb_deinit(struct lan966x *lan966x);
0372 int lan966x_handle_fdb(struct net_device *dev,
0373                struct net_device *orig_dev,
0374                unsigned long event, const void *ctx,
0375                const struct switchdev_notifier_fdb_info *fdb_info);
0376 
0377 void lan966x_mdb_init(struct lan966x *lan966x);
0378 void lan966x_mdb_deinit(struct lan966x *lan966x);
0379 int lan966x_handle_port_mdb_add(struct lan966x_port *port,
0380                 const struct switchdev_obj *obj);
0381 int lan966x_handle_port_mdb_del(struct lan966x_port *port,
0382                 const struct switchdev_obj *obj);
0383 void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid);
0384 void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid);
0385 void lan966x_mdb_clear_entries(struct lan966x *lan966x);
0386 void lan966x_mdb_restore_entries(struct lan966x *lan966x);
0387 
0388 int lan966x_ptp_init(struct lan966x *lan966x);
0389 void lan966x_ptp_deinit(struct lan966x *lan966x);
0390 int lan966x_ptp_hwtstamp_set(struct lan966x_port *port, struct ifreq *ifr);
0391 int lan966x_ptp_hwtstamp_get(struct lan966x_port *port, struct ifreq *ifr);
0392 void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb,
0393               u64 timestamp);
0394 int lan966x_ptp_txtstamp_request(struct lan966x_port *port,
0395                  struct sk_buff *skb);
0396 void lan966x_ptp_txtstamp_release(struct lan966x_port *port,
0397                   struct sk_buff *skb);
0398 irqreturn_t lan966x_ptp_irq_handler(int irq, void *args);
0399 irqreturn_t lan966x_ptp_ext_irq_handler(int irq, void *args);
0400 
0401 int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev);
0402 int lan966x_fdma_change_mtu(struct lan966x *lan966x);
0403 void lan966x_fdma_netdev_init(struct lan966x *lan966x, struct net_device *dev);
0404 void lan966x_fdma_netdev_deinit(struct lan966x *lan966x, struct net_device *dev);
0405 int lan966x_fdma_init(struct lan966x *lan966x);
0406 void lan966x_fdma_deinit(struct lan966x *lan966x);
0407 irqreturn_t lan966x_fdma_irq_handler(int irq, void *args);
0408 
0409 static inline void __iomem *lan_addr(void __iomem *base[],
0410                      int id, int tinst, int tcnt,
0411                      int gbase, int ginst,
0412                      int gcnt, int gwidth,
0413                      int raddr, int rinst,
0414                      int rcnt, int rwidth)
0415 {
0416     WARN_ON((tinst) >= tcnt);
0417     WARN_ON((ginst) >= gcnt);
0418     WARN_ON((rinst) >= rcnt);
0419     return base[id + (tinst)] +
0420         gbase + ((ginst) * gwidth) +
0421         raddr + ((rinst) * rwidth);
0422 }
0423 
0424 static inline u32 lan_rd(struct lan966x *lan966x, int id, int tinst, int tcnt,
0425              int gbase, int ginst, int gcnt, int gwidth,
0426              int raddr, int rinst, int rcnt, int rwidth)
0427 {
0428     return readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
0429                   gcnt, gwidth, raddr, rinst, rcnt, rwidth));
0430 }
0431 
0432 static inline void lan_wr(u32 val, struct lan966x *lan966x,
0433               int id, int tinst, int tcnt,
0434               int gbase, int ginst, int gcnt, int gwidth,
0435               int raddr, int rinst, int rcnt, int rwidth)
0436 {
0437     writel(val, lan_addr(lan966x->regs, id, tinst, tcnt,
0438                  gbase, ginst, gcnt, gwidth,
0439                  raddr, rinst, rcnt, rwidth));
0440 }
0441 
0442 static inline void lan_rmw(u32 val, u32 mask, struct lan966x *lan966x,
0443                int id, int tinst, int tcnt,
0444                int gbase, int ginst, int gcnt, int gwidth,
0445                int raddr, int rinst, int rcnt, int rwidth)
0446 {
0447     u32 nval;
0448 
0449     nval = readl(lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
0450                   gcnt, gwidth, raddr, rinst, rcnt, rwidth));
0451     nval = (nval & ~mask) | (val & mask);
0452     writel(nval, lan_addr(lan966x->regs, id, tinst, tcnt, gbase, ginst,
0453                   gcnt, gwidth, raddr, rinst, rcnt, rwidth));
0454 }
0455 
0456 #endif /* __LAN966X_MAIN_H__ */