Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * drivers/net/ethernet/ibm/emac/core.h
0004  *
0005  * Driver for PowerPC 4xx on-chip ethernet controller.
0006  *
0007  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
0008  *                <benh@kernel.crashing.org>
0009  *
0010  * Based on the arch/ppc version of the driver:
0011  *
0012  * Copyright (c) 2004, 2005 Zultys Technologies.
0013  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
0014  *
0015  * Based on original work by
0016  *      Armin Kuster <akuster@mvista.com>
0017  *  Johnnie Peters <jpeters@mvista.com>
0018  *      Copyright 2000, 2001 MontaVista Softare Inc.
0019  */
0020 #ifndef __IBM_NEWEMAC_CORE_H
0021 #define __IBM_NEWEMAC_CORE_H
0022 
0023 #include <linux/module.h>
0024 #include <linux/list.h>
0025 #include <linux/kernel.h>
0026 #include <linux/interrupt.h>
0027 #include <linux/netdevice.h>
0028 #include <linux/dma-mapping.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/of_platform.h>
0031 #include <linux/slab.h>
0032 
0033 #include <asm/io.h>
0034 #include <asm/dcr.h>
0035 
0036 #include "emac.h"
0037 #include "phy.h"
0038 #include "zmii.h"
0039 #include "rgmii.h"
0040 #include "mal.h"
0041 #include "tah.h"
0042 #include "debug.h"
0043 
0044 #define NUM_TX_BUFF         CONFIG_IBM_EMAC_TXB
0045 #define NUM_RX_BUFF         CONFIG_IBM_EMAC_RXB
0046 
0047 /* Simple sanity check */
0048 #if NUM_TX_BUFF > 256 || NUM_RX_BUFF > 256
0049 #error Invalid number of buffer descriptors (greater than 256)
0050 #endif
0051 
0052 #define EMAC_MIN_MTU            46
0053 
0054 /* Maximum L2 header length (VLAN tagged, no FCS) */
0055 #define EMAC_MTU_OVERHEAD       (6 * 2 + 2 + 4)
0056 
0057 /* RX BD size for the given MTU */
0058 static inline int emac_rx_size(int mtu)
0059 {
0060     if (mtu > ETH_DATA_LEN)
0061         return MAL_MAX_RX_SIZE;
0062     else
0063         return mal_rx_size(ETH_DATA_LEN + EMAC_MTU_OVERHEAD);
0064 }
0065 
0066 /* Size of RX skb for the given MTU */
0067 static inline int emac_rx_skb_size(int mtu)
0068 {
0069     int size = max(mtu + EMAC_MTU_OVERHEAD, emac_rx_size(mtu));
0070 
0071     return SKB_DATA_ALIGN(size + NET_IP_ALIGN) + NET_SKB_PAD;
0072 }
0073 
0074 /* RX DMA sync size */
0075 static inline int emac_rx_sync_size(int mtu)
0076 {
0077     return SKB_DATA_ALIGN(emac_rx_size(mtu) + NET_IP_ALIGN);
0078 }
0079 
0080 /* Driver statistcs is split into two parts to make it more cache friendly:
0081  *   - normal statistics (packet count, etc)
0082  *   - error statistics
0083  *
0084  * When statistics is requested by ethtool, these parts are concatenated,
0085  * normal one goes first.
0086  *
0087  * Please, keep these structures in sync with emac_stats_keys.
0088  */
0089 
0090 /* Normal TX/RX Statistics */
0091 struct emac_stats {
0092     u64 rx_packets;
0093     u64 rx_bytes;
0094     u64 tx_packets;
0095     u64 tx_bytes;
0096     u64 rx_packets_csum;
0097     u64 tx_packets_csum;
0098 };
0099 
0100 /* Error statistics */
0101 struct emac_error_stats {
0102     u64 tx_undo;
0103 
0104     /* Software RX Errors */
0105     u64 rx_dropped_stack;
0106     u64 rx_dropped_oom;
0107     u64 rx_dropped_error;
0108     u64 rx_dropped_resize;
0109     u64 rx_dropped_mtu;
0110     u64 rx_stopped;
0111     /* BD reported RX errors */
0112     u64 rx_bd_errors;
0113     u64 rx_bd_overrun;
0114     u64 rx_bd_bad_packet;
0115     u64 rx_bd_runt_packet;
0116     u64 rx_bd_short_event;
0117     u64 rx_bd_alignment_error;
0118     u64 rx_bd_bad_fcs;
0119     u64 rx_bd_packet_too_long;
0120     u64 rx_bd_out_of_range;
0121     u64 rx_bd_in_range;
0122     /* EMAC IRQ reported RX errors */
0123     u64 rx_parity;
0124     u64 rx_fifo_overrun;
0125     u64 rx_overrun;
0126     u64 rx_bad_packet;
0127     u64 rx_runt_packet;
0128     u64 rx_short_event;
0129     u64 rx_alignment_error;
0130     u64 rx_bad_fcs;
0131     u64 rx_packet_too_long;
0132     u64 rx_out_of_range;
0133     u64 rx_in_range;
0134 
0135     /* Software TX Errors */
0136     u64 tx_dropped;
0137     /* BD reported TX errors */
0138     u64 tx_bd_errors;
0139     u64 tx_bd_bad_fcs;
0140     u64 tx_bd_carrier_loss;
0141     u64 tx_bd_excessive_deferral;
0142     u64 tx_bd_excessive_collisions;
0143     u64 tx_bd_late_collision;
0144     u64 tx_bd_multple_collisions;
0145     u64 tx_bd_single_collision;
0146     u64 tx_bd_underrun;
0147     u64 tx_bd_sqe;
0148     /* EMAC IRQ reported TX errors */
0149     u64 tx_parity;
0150     u64 tx_underrun;
0151     u64 tx_sqe;
0152     u64 tx_errors;
0153 };
0154 
0155 #define EMAC_ETHTOOL_STATS_COUNT    ((sizeof(struct emac_stats) + \
0156                       sizeof(struct emac_error_stats)) \
0157                      / sizeof(u64))
0158 
0159 struct emac_instance {
0160     struct net_device       *ndev;
0161     struct emac_regs        __iomem *emacp;
0162     struct platform_device      *ofdev;
0163     struct device_node      **blist; /* bootlist entry */
0164 
0165     /* MAL linkage */
0166     u32             mal_ph;
0167     struct platform_device      *mal_dev;
0168     u32             mal_rx_chan;
0169     u32             mal_tx_chan;
0170     struct mal_instance     *mal;
0171     struct mal_commac       commac;
0172 
0173     /* PHY infos */
0174     phy_interface_t         phy_mode;
0175     u32             phy_map;
0176     u32             phy_address;
0177     u32             phy_feat_exc;
0178     struct mii_phy          phy;
0179     struct mutex            link_lock;
0180     struct delayed_work     link_work;
0181     int             link_polling;
0182 
0183     /* GPCS PHY infos */
0184     u32             gpcs_address;
0185 
0186     /* Shared MDIO if any */
0187     u32             mdio_ph;
0188     struct platform_device      *mdio_dev;
0189     struct emac_instance        *mdio_instance;
0190     struct mutex            mdio_lock;
0191 
0192     /* Device-tree based phy configuration */
0193     struct mii_bus          *mii_bus;
0194     struct phy_device       *phy_dev;
0195 
0196     /* ZMII infos if any */
0197     u32             zmii_ph;
0198     u32             zmii_port;
0199     struct platform_device      *zmii_dev;
0200 
0201     /* RGMII infos if any */
0202     u32             rgmii_ph;
0203     u32             rgmii_port;
0204     struct platform_device      *rgmii_dev;
0205 
0206     /* TAH infos if any */
0207     u32             tah_ph;
0208     u32             tah_port;
0209     struct platform_device      *tah_dev;
0210 
0211     /* IRQs */
0212     int             wol_irq;
0213     int             emac_irq;
0214 
0215     /* OPB bus frequency in Mhz */
0216     u32             opb_bus_freq;
0217 
0218     /* Cell index within an ASIC (for clk mgmnt) */
0219     u32             cell_index;
0220 
0221     /* Max supported MTU */
0222     u32             max_mtu;
0223 
0224     /* Feature bits (from probe table) */
0225     unsigned int            features;
0226 
0227     /* Tx and Rx fifo sizes & other infos in bytes */
0228     u32             tx_fifo_size;
0229     u32             tx_fifo_size_gige;
0230     u32             rx_fifo_size;
0231     u32             rx_fifo_size_gige;
0232     u32             fifo_entry_size;
0233     u32             mal_burst_size; /* move to MAL ? */
0234 
0235     /* IAHT and GAHT filter parameterization */
0236     u32             xaht_slots_shift;
0237     u32             xaht_width_shift;
0238 
0239     /* Descriptor management
0240      */
0241     struct mal_descriptor       *tx_desc;
0242     int             tx_cnt;
0243     int             tx_slot;
0244     int             ack_slot;
0245 
0246     struct mal_descriptor       *rx_desc;
0247     int             rx_slot;
0248     struct sk_buff          *rx_sg_skb; /* 1 */
0249     int                 rx_skb_size;
0250     int             rx_sync_size;
0251 
0252     struct sk_buff          *tx_skb[NUM_TX_BUFF];
0253     struct sk_buff          *rx_skb[NUM_RX_BUFF];
0254 
0255     /* Stats
0256      */
0257     struct emac_error_stats     estats;
0258     struct emac_stats       stats;
0259 
0260     /* Misc
0261      */
0262     int             reset_failed;
0263     int             stop_timeout;   /* in us */
0264     int             no_mcast;
0265     int             mcast_pending;
0266     int             opened;
0267     struct work_struct      reset_work;
0268     spinlock_t          lock;
0269 };
0270 
0271 /*
0272  * Features of various EMAC implementations
0273  */
0274 
0275 /*
0276  * No flow control on 40x according to the original driver
0277  */
0278 #define EMAC_FTR_NO_FLOW_CONTROL_40x    0x00000001
0279 /*
0280  * Cell is an EMAC4
0281  */
0282 #define EMAC_FTR_EMAC4          0x00000002
0283 /*
0284  * For the 440SPe, AMCC inexplicably changed the polarity of
0285  * the "operation complete" bit in the MII control register.
0286  */
0287 #define EMAC_FTR_STACR_OC_INVERT    0x00000004
0288 /*
0289  * Set if we have a TAH.
0290  */
0291 #define EMAC_FTR_HAS_TAH        0x00000008
0292 /*
0293  * Set if we have a ZMII.
0294  */
0295 #define EMAC_FTR_HAS_ZMII       0x00000010
0296 /*
0297  * Set if we have a RGMII.
0298  */
0299 #define EMAC_FTR_HAS_RGMII      0x00000020
0300 /*
0301  * Set if we have new type STACR with STAOPC
0302  */
0303 #define EMAC_FTR_HAS_NEW_STACR      0x00000040
0304 /*
0305  * Set if we need phy clock workaround for 440gx
0306  */
0307 #define EMAC_FTR_440GX_PHY_CLK_FIX  0x00000080
0308 /*
0309  * Set if we need phy clock workaround for 440ep or 440gr
0310  */
0311 #define EMAC_FTR_440EP_PHY_CLK_FIX  0x00000100
0312 /*
0313  * The 405EX and 460EX contain the EMAC4SYNC core
0314  */
0315 #define EMAC_FTR_EMAC4SYNC      0x00000200
0316 /*
0317  * Set if we need phy clock workaround for 460ex or 460gt
0318  */
0319 #define EMAC_FTR_460EX_PHY_CLK_FIX  0x00000400
0320 /*
0321  * APM821xx requires Jumbo frame size set explicitly
0322  */
0323 #define EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE  0x00000800
0324 /*
0325  * APM821xx does not support Half Duplex mode
0326  */
0327 #define EMAC_FTR_APM821XX_NO_HALF_DUPLEX    0x00001000
0328 
0329 /* Right now, we don't quite handle the always/possible masks on the
0330  * most optimal way as we don't have a way to say something like
0331  * always EMAC4. Patches welcome.
0332  */
0333 enum {
0334     EMAC_FTRS_ALWAYS    = 0,
0335 
0336     EMAC_FTRS_POSSIBLE  =
0337 #ifdef CONFIG_IBM_EMAC_EMAC4
0338         EMAC_FTR_EMAC4  | EMAC_FTR_EMAC4SYNC    |
0339         EMAC_FTR_HAS_NEW_STACR  |
0340         EMAC_FTR_STACR_OC_INVERT | EMAC_FTR_440GX_PHY_CLK_FIX |
0341 #endif
0342 #ifdef CONFIG_IBM_EMAC_TAH
0343         EMAC_FTR_HAS_TAH    |
0344 #endif
0345 #ifdef CONFIG_IBM_EMAC_ZMII
0346         EMAC_FTR_HAS_ZMII   |
0347 #endif
0348 #ifdef CONFIG_IBM_EMAC_RGMII
0349         EMAC_FTR_HAS_RGMII  |
0350 #endif
0351 #ifdef CONFIG_IBM_EMAC_NO_FLOW_CTRL
0352         EMAC_FTR_NO_FLOW_CONTROL_40x |
0353 #endif
0354     EMAC_FTR_460EX_PHY_CLK_FIX |
0355     EMAC_FTR_440EP_PHY_CLK_FIX |
0356     EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE |
0357     EMAC_FTR_APM821XX_NO_HALF_DUPLEX,
0358 };
0359 
0360 static inline int emac_has_feature(struct emac_instance *dev,
0361                    unsigned long feature)
0362 {
0363     return (EMAC_FTRS_ALWAYS & feature) ||
0364            (EMAC_FTRS_POSSIBLE & dev->features & feature);
0365 }
0366 
0367 /*
0368  * Various instances of the EMAC core have varying 1) number of
0369  * address match slots, 2) width of the registers for handling address
0370  * match slots, 3) number of registers for handling address match
0371  * slots and 4) base offset for those registers.
0372  *
0373  * These macros and inlines handle these differences based on
0374  * parameters supplied by the device structure which are, in turn,
0375  * initialized based on the "compatible" entry in the device tree.
0376  */
0377 
0378 #define EMAC4_XAHT_SLOTS_SHIFT      6
0379 #define EMAC4_XAHT_WIDTH_SHIFT      4
0380 
0381 #define EMAC4SYNC_XAHT_SLOTS_SHIFT  8
0382 #define EMAC4SYNC_XAHT_WIDTH_SHIFT  5
0383 
0384 /* The largest span between slots and widths above is 3 */
0385 #define EMAC_XAHT_MAX_REGS      (1 << 3)
0386 
0387 #define EMAC_XAHT_SLOTS(dev)            (1 << (dev)->xaht_slots_shift)
0388 #define EMAC_XAHT_WIDTH(dev)            (1 << (dev)->xaht_width_shift)
0389 #define EMAC_XAHT_REGS(dev)             (1 << ((dev)->xaht_slots_shift - \
0390                            (dev)->xaht_width_shift))
0391 
0392 #define EMAC_XAHT_CRC_TO_SLOT(dev, crc)         \
0393     ((EMAC_XAHT_SLOTS(dev) - 1) -           \
0394      ((crc) >> ((sizeof (u32) * BITS_PER_BYTE) -    \
0395             (dev)->xaht_slots_shift)))
0396 
0397 #define EMAC_XAHT_SLOT_TO_REG(dev, slot)        \
0398     ((slot) >> (dev)->xaht_width_shift)
0399 
0400 #define EMAC_XAHT_SLOT_TO_MASK(dev, slot)       \
0401     ((u32)(1 << (EMAC_XAHT_WIDTH(dev) - 1)) >>  \
0402      ((slot) & (u32)(EMAC_XAHT_WIDTH(dev) - 1)))
0403 
0404 static inline u32 *emac_xaht_base(struct emac_instance *dev)
0405 {
0406     struct emac_regs __iomem *p = dev->emacp;
0407     int offset;
0408 
0409     /* The first IAHT entry always is the base of the block of
0410      * IAHT and GAHT registers.
0411      */
0412     if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC))
0413         offset = offsetof(struct emac_regs, u1.emac4sync.iaht1);
0414     else
0415         offset = offsetof(struct emac_regs, u0.emac4.iaht1);
0416 
0417     return (u32 *)((ptrdiff_t)p + offset);
0418 }
0419 
0420 static inline u32 *emac_gaht_base(struct emac_instance *dev)
0421 {
0422     /* GAHT registers always come after an identical number of
0423      * IAHT registers.
0424      */
0425     return emac_xaht_base(dev) + EMAC_XAHT_REGS(dev);
0426 }
0427 
0428 static inline u32 *emac_iaht_base(struct emac_instance *dev)
0429 {
0430     /* IAHT registers always come before an identical number of
0431      * GAHT registers.
0432      */
0433     return emac_xaht_base(dev);
0434 }
0435 
0436 /* Ethtool get_regs complex data.
0437  * We want to get not just EMAC registers, but also MAL, ZMII, RGMII, TAH
0438  * when available.
0439  *
0440  * Returned BLOB consists of the ibm_emac_ethtool_regs_hdr,
0441  * MAL registers, EMAC registers and optional ZMII, RGMII, TAH registers.
0442  * Each register component is preceded with emac_ethtool_regs_subhdr.
0443  * Order of the optional headers follows their relative bit posititions
0444  * in emac_ethtool_regs_hdr.components
0445  */
0446 #define EMAC_ETHTOOL_REGS_ZMII      0x00000001
0447 #define EMAC_ETHTOOL_REGS_RGMII     0x00000002
0448 #define EMAC_ETHTOOL_REGS_TAH       0x00000004
0449 
0450 struct emac_ethtool_regs_hdr {
0451     u32 components;
0452 };
0453 
0454 struct emac_ethtool_regs_subhdr {
0455     u32 version;
0456     u32 index;
0457 };
0458 
0459 #define EMAC_ETHTOOL_REGS_VER       3
0460 #define EMAC4_ETHTOOL_REGS_VER      4
0461 #define EMAC4SYNC_ETHTOOL_REGS_VER  5
0462 
0463 #endif /* __IBM_NEWEMAC_CORE_H */