linux/drivers/net/ethernet/stmicro/stmmac/stmmac_ethtool.c
<<
>>
Prefs
   1/*******************************************************************************
   2  STMMAC Ethtool support
   3
   4  Copyright (C) 2007-2009  STMicroelectronics Ltd
   5
   6  This program is free software; you can redistribute it and/or modify it
   7  under the terms and conditions of the GNU General Public License,
   8  version 2, as published by the Free Software Foundation.
   9
  10  This program is distributed in the hope it will be useful, but WITHOUT
  11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13  more details.
  14
  15  The full GNU General Public License is included in this distribution in
  16  the file called "COPYING".
  17
  18  Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  19*******************************************************************************/
  20
  21#include <linux/etherdevice.h>
  22#include <linux/ethtool.h>
  23#include <linux/interrupt.h>
  24#include <linux/mii.h>
  25#include <linux/phylink.h>
  26#include <linux/net_tstamp.h>
  27#include <asm/io.h>
  28
  29#include "stmmac.h"
  30#include "dwmac_dma.h"
  31#include "dwxgmac2.h"
  32
  33#define REG_SPACE_SIZE  0x1060
  34#define MAC100_ETHTOOL_NAME     "st_mac100"
  35#define GMAC_ETHTOOL_NAME       "st_gmac"
  36#define XGMAC_ETHTOOL_NAME      "st_xgmac"
  37
  38#define ETHTOOL_DMA_OFFSET      55
  39
  40struct stmmac_stats {
  41        char stat_string[ETH_GSTRING_LEN];
  42        int sizeof_stat;
  43        int stat_offset;
  44};
  45
  46#define STMMAC_STAT(m)  \
  47        { #m, sizeof_field(struct stmmac_extra_stats, m),       \
  48        offsetof(struct stmmac_priv, xstats.m)}
  49
  50static const struct stmmac_stats stmmac_gstrings_stats[] = {
  51        /* Transmit errors */
  52        STMMAC_STAT(tx_underflow),
  53        STMMAC_STAT(tx_carrier),
  54        STMMAC_STAT(tx_losscarrier),
  55        STMMAC_STAT(vlan_tag),
  56        STMMAC_STAT(tx_deferred),
  57        STMMAC_STAT(tx_vlan),
  58        STMMAC_STAT(tx_jabber),
  59        STMMAC_STAT(tx_frame_flushed),
  60        STMMAC_STAT(tx_payload_error),
  61        STMMAC_STAT(tx_ip_header_error),
  62        /* Receive errors */
  63        STMMAC_STAT(rx_desc),
  64        STMMAC_STAT(sa_filter_fail),
  65        STMMAC_STAT(overflow_error),
  66        STMMAC_STAT(ipc_csum_error),
  67        STMMAC_STAT(rx_collision),
  68        STMMAC_STAT(rx_crc_errors),
  69        STMMAC_STAT(dribbling_bit),
  70        STMMAC_STAT(rx_length),
  71        STMMAC_STAT(rx_mii),
  72        STMMAC_STAT(rx_multicast),
  73        STMMAC_STAT(rx_gmac_overflow),
  74        STMMAC_STAT(rx_watchdog),
  75        STMMAC_STAT(da_rx_filter_fail),
  76        STMMAC_STAT(sa_rx_filter_fail),
  77        STMMAC_STAT(rx_missed_cntr),
  78        STMMAC_STAT(rx_overflow_cntr),
  79        STMMAC_STAT(rx_vlan),
  80        STMMAC_STAT(rx_split_hdr_pkt_n),
  81        /* Tx/Rx IRQ error info */
  82        STMMAC_STAT(tx_undeflow_irq),
  83        STMMAC_STAT(tx_process_stopped_irq),
  84        STMMAC_STAT(tx_jabber_irq),
  85        STMMAC_STAT(rx_overflow_irq),
  86        STMMAC_STAT(rx_buf_unav_irq),
  87        STMMAC_STAT(rx_process_stopped_irq),
  88        STMMAC_STAT(rx_watchdog_irq),
  89        STMMAC_STAT(tx_early_irq),
  90        STMMAC_STAT(fatal_bus_error_irq),
  91        /* Tx/Rx IRQ Events */
  92        STMMAC_STAT(rx_early_irq),
  93        STMMAC_STAT(threshold),
  94        STMMAC_STAT(tx_pkt_n),
  95        STMMAC_STAT(rx_pkt_n),
  96        STMMAC_STAT(normal_irq_n),
  97        STMMAC_STAT(rx_normal_irq_n),
  98        STMMAC_STAT(napi_poll),
  99        STMMAC_STAT(tx_normal_irq_n),
 100        STMMAC_STAT(tx_clean),
 101        STMMAC_STAT(tx_set_ic_bit),
 102        STMMAC_STAT(irq_receive_pmt_irq_n),
 103        /* MMC info */
 104        STMMAC_STAT(mmc_tx_irq_n),
 105        STMMAC_STAT(mmc_rx_irq_n),
 106        STMMAC_STAT(mmc_rx_csum_offload_irq_n),
 107        /* EEE */
 108        STMMAC_STAT(irq_tx_path_in_lpi_mode_n),
 109        STMMAC_STAT(irq_tx_path_exit_lpi_mode_n),
 110        STMMAC_STAT(irq_rx_path_in_lpi_mode_n),
 111        STMMAC_STAT(irq_rx_path_exit_lpi_mode_n),
 112        STMMAC_STAT(phy_eee_wakeup_error_n),
 113        /* Extended RDES status */
 114        STMMAC_STAT(ip_hdr_err),
 115        STMMAC_STAT(ip_payload_err),
 116        STMMAC_STAT(ip_csum_bypassed),
 117        STMMAC_STAT(ipv4_pkt_rcvd),
 118        STMMAC_STAT(ipv6_pkt_rcvd),
 119        STMMAC_STAT(no_ptp_rx_msg_type_ext),
 120        STMMAC_STAT(ptp_rx_msg_type_sync),
 121        STMMAC_STAT(ptp_rx_msg_type_follow_up),
 122        STMMAC_STAT(ptp_rx_msg_type_delay_req),
 123        STMMAC_STAT(ptp_rx_msg_type_delay_resp),
 124        STMMAC_STAT(ptp_rx_msg_type_pdelay_req),
 125        STMMAC_STAT(ptp_rx_msg_type_pdelay_resp),
 126        STMMAC_STAT(ptp_rx_msg_type_pdelay_follow_up),
 127        STMMAC_STAT(ptp_rx_msg_type_announce),
 128        STMMAC_STAT(ptp_rx_msg_type_management),
 129        STMMAC_STAT(ptp_rx_msg_pkt_reserved_type),
 130        STMMAC_STAT(ptp_frame_type),
 131        STMMAC_STAT(ptp_ver),
 132        STMMAC_STAT(timestamp_dropped),
 133        STMMAC_STAT(av_pkt_rcvd),
 134        STMMAC_STAT(av_tagged_pkt_rcvd),
 135        STMMAC_STAT(vlan_tag_priority_val),
 136        STMMAC_STAT(l3_filter_match),
 137        STMMAC_STAT(l4_filter_match),
 138        STMMAC_STAT(l3_l4_filter_no_match),
 139        /* PCS */
 140        STMMAC_STAT(irq_pcs_ane_n),
 141        STMMAC_STAT(irq_pcs_link_n),
 142        STMMAC_STAT(irq_rgmii_n),
 143        /* DEBUG */
 144        STMMAC_STAT(mtl_tx_status_fifo_full),
 145        STMMAC_STAT(mtl_tx_fifo_not_empty),
 146        STMMAC_STAT(mmtl_fifo_ctrl),
 147        STMMAC_STAT(mtl_tx_fifo_read_ctrl_write),
 148        STMMAC_STAT(mtl_tx_fifo_read_ctrl_wait),
 149        STMMAC_STAT(mtl_tx_fifo_read_ctrl_read),
 150        STMMAC_STAT(mtl_tx_fifo_read_ctrl_idle),
 151        STMMAC_STAT(mac_tx_in_pause),
 152        STMMAC_STAT(mac_tx_frame_ctrl_xfer),
 153        STMMAC_STAT(mac_tx_frame_ctrl_idle),
 154        STMMAC_STAT(mac_tx_frame_ctrl_wait),
 155        STMMAC_STAT(mac_tx_frame_ctrl_pause),
 156        STMMAC_STAT(mac_gmii_tx_proto_engine),
 157        STMMAC_STAT(mtl_rx_fifo_fill_level_full),
 158        STMMAC_STAT(mtl_rx_fifo_fill_above_thresh),
 159        STMMAC_STAT(mtl_rx_fifo_fill_below_thresh),
 160        STMMAC_STAT(mtl_rx_fifo_fill_level_empty),
 161        STMMAC_STAT(mtl_rx_fifo_read_ctrl_flush),
 162        STMMAC_STAT(mtl_rx_fifo_read_ctrl_read_data),
 163        STMMAC_STAT(mtl_rx_fifo_read_ctrl_status),
 164        STMMAC_STAT(mtl_rx_fifo_read_ctrl_idle),
 165        STMMAC_STAT(mtl_rx_fifo_ctrl_active),
 166        STMMAC_STAT(mac_rx_frame_ctrl_fifo),
 167        STMMAC_STAT(mac_gmii_rx_proto_engine),
 168        /* TSO */
 169        STMMAC_STAT(tx_tso_frames),
 170        STMMAC_STAT(tx_tso_nfrags),
 171};
 172#define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)
 173
 174/* HW MAC Management counters (if supported) */
 175#define STMMAC_MMC_STAT(m)      \
 176        { #m, sizeof_field(struct stmmac_counters, m),  \
 177        offsetof(struct stmmac_priv, mmc.m)}
 178
 179static const struct stmmac_stats stmmac_mmc[] = {
 180        STMMAC_MMC_STAT(mmc_tx_octetcount_gb),
 181        STMMAC_MMC_STAT(mmc_tx_framecount_gb),
 182        STMMAC_MMC_STAT(mmc_tx_broadcastframe_g),
 183        STMMAC_MMC_STAT(mmc_tx_multicastframe_g),
 184        STMMAC_MMC_STAT(mmc_tx_64_octets_gb),
 185        STMMAC_MMC_STAT(mmc_tx_65_to_127_octets_gb),
 186        STMMAC_MMC_STAT(mmc_tx_128_to_255_octets_gb),
 187        STMMAC_MMC_STAT(mmc_tx_256_to_511_octets_gb),
 188        STMMAC_MMC_STAT(mmc_tx_512_to_1023_octets_gb),
 189        STMMAC_MMC_STAT(mmc_tx_1024_to_max_octets_gb),
 190        STMMAC_MMC_STAT(mmc_tx_unicast_gb),
 191        STMMAC_MMC_STAT(mmc_tx_multicast_gb),
 192        STMMAC_MMC_STAT(mmc_tx_broadcast_gb),
 193        STMMAC_MMC_STAT(mmc_tx_underflow_error),
 194        STMMAC_MMC_STAT(mmc_tx_singlecol_g),
 195        STMMAC_MMC_STAT(mmc_tx_multicol_g),
 196        STMMAC_MMC_STAT(mmc_tx_deferred),
 197        STMMAC_MMC_STAT(mmc_tx_latecol),
 198        STMMAC_MMC_STAT(mmc_tx_exesscol),
 199        STMMAC_MMC_STAT(mmc_tx_carrier_error),
 200        STMMAC_MMC_STAT(mmc_tx_octetcount_g),
 201        STMMAC_MMC_STAT(mmc_tx_framecount_g),
 202        STMMAC_MMC_STAT(mmc_tx_excessdef),
 203        STMMAC_MMC_STAT(mmc_tx_pause_frame),
 204        STMMAC_MMC_STAT(mmc_tx_vlan_frame_g),
 205        STMMAC_MMC_STAT(mmc_rx_framecount_gb),
 206        STMMAC_MMC_STAT(mmc_rx_octetcount_gb),
 207        STMMAC_MMC_STAT(mmc_rx_octetcount_g),
 208        STMMAC_MMC_STAT(mmc_rx_broadcastframe_g),
 209        STMMAC_MMC_STAT(mmc_rx_multicastframe_g),
 210        STMMAC_MMC_STAT(mmc_rx_crc_error),
 211        STMMAC_MMC_STAT(mmc_rx_align_error),
 212        STMMAC_MMC_STAT(mmc_rx_run_error),
 213        STMMAC_MMC_STAT(mmc_rx_jabber_error),
 214        STMMAC_MMC_STAT(mmc_rx_undersize_g),
 215        STMMAC_MMC_STAT(mmc_rx_oversize_g),
 216        STMMAC_MMC_STAT(mmc_rx_64_octets_gb),
 217        STMMAC_MMC_STAT(mmc_rx_65_to_127_octets_gb),
 218        STMMAC_MMC_STAT(mmc_rx_128_to_255_octets_gb),
 219        STMMAC_MMC_STAT(mmc_rx_256_to_511_octets_gb),
 220        STMMAC_MMC_STAT(mmc_rx_512_to_1023_octets_gb),
 221        STMMAC_MMC_STAT(mmc_rx_1024_to_max_octets_gb),
 222        STMMAC_MMC_STAT(mmc_rx_unicast_g),
 223        STMMAC_MMC_STAT(mmc_rx_length_error),
 224        STMMAC_MMC_STAT(mmc_rx_autofrangetype),
 225        STMMAC_MMC_STAT(mmc_rx_pause_frames),
 226        STMMAC_MMC_STAT(mmc_rx_fifo_overflow),
 227        STMMAC_MMC_STAT(mmc_rx_vlan_frames_gb),
 228        STMMAC_MMC_STAT(mmc_rx_watchdog_error),
 229        STMMAC_MMC_STAT(mmc_rx_ipc_intr_mask),
 230        STMMAC_MMC_STAT(mmc_rx_ipc_intr),
 231        STMMAC_MMC_STAT(mmc_rx_ipv4_gd),
 232        STMMAC_MMC_STAT(mmc_rx_ipv4_hderr),
 233        STMMAC_MMC_STAT(mmc_rx_ipv4_nopay),
 234        STMMAC_MMC_STAT(mmc_rx_ipv4_frag),
 235        STMMAC_MMC_STAT(mmc_rx_ipv4_udsbl),
 236        STMMAC_MMC_STAT(mmc_rx_ipv4_gd_octets),
 237        STMMAC_MMC_STAT(mmc_rx_ipv4_hderr_octets),
 238        STMMAC_MMC_STAT(mmc_rx_ipv4_nopay_octets),
 239        STMMAC_MMC_STAT(mmc_rx_ipv4_frag_octets),
 240        STMMAC_MMC_STAT(mmc_rx_ipv4_udsbl_octets),
 241        STMMAC_MMC_STAT(mmc_rx_ipv6_gd_octets),
 242        STMMAC_MMC_STAT(mmc_rx_ipv6_hderr_octets),
 243        STMMAC_MMC_STAT(mmc_rx_ipv6_nopay_octets),
 244        STMMAC_MMC_STAT(mmc_rx_ipv6_gd),
 245        STMMAC_MMC_STAT(mmc_rx_ipv6_hderr),
 246        STMMAC_MMC_STAT(mmc_rx_ipv6_nopay),
 247        STMMAC_MMC_STAT(mmc_rx_udp_gd),
 248        STMMAC_MMC_STAT(mmc_rx_udp_err),
 249        STMMAC_MMC_STAT(mmc_rx_tcp_gd),
 250        STMMAC_MMC_STAT(mmc_rx_tcp_err),
 251        STMMAC_MMC_STAT(mmc_rx_icmp_gd),
 252        STMMAC_MMC_STAT(mmc_rx_icmp_err),
 253        STMMAC_MMC_STAT(mmc_rx_udp_gd_octets),
 254        STMMAC_MMC_STAT(mmc_rx_udp_err_octets),
 255        STMMAC_MMC_STAT(mmc_rx_tcp_gd_octets),
 256        STMMAC_MMC_STAT(mmc_rx_tcp_err_octets),
 257        STMMAC_MMC_STAT(mmc_rx_icmp_gd_octets),
 258        STMMAC_MMC_STAT(mmc_rx_icmp_err_octets),
 259        STMMAC_MMC_STAT(mmc_tx_fpe_fragment_cntr),
 260        STMMAC_MMC_STAT(mmc_tx_hold_req_cntr),
 261        STMMAC_MMC_STAT(mmc_rx_packet_assembly_err_cntr),
 262        STMMAC_MMC_STAT(mmc_rx_packet_smd_err_cntr),
 263        STMMAC_MMC_STAT(mmc_rx_packet_assembly_ok_cntr),
 264        STMMAC_MMC_STAT(mmc_rx_fpe_fragment_cntr),
 265};
 266#define STMMAC_MMC_STATS_LEN ARRAY_SIZE(stmmac_mmc)
 267
 268static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
 269                                      struct ethtool_drvinfo *info)
 270{
 271        struct stmmac_priv *priv = netdev_priv(dev);
 272
 273        if (priv->plat->has_gmac || priv->plat->has_gmac4)
 274                strlcpy(info->driver, GMAC_ETHTOOL_NAME, sizeof(info->driver));
 275        else if (priv->plat->has_xgmac)
 276                strlcpy(info->driver, XGMAC_ETHTOOL_NAME, sizeof(info->driver));
 277        else
 278                strlcpy(info->driver, MAC100_ETHTOOL_NAME,
 279                        sizeof(info->driver));
 280
 281        strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
 282}
 283
 284static int stmmac_ethtool_get_link_ksettings(struct net_device *dev,
 285                                             struct ethtool_link_ksettings *cmd)
 286{
 287        struct stmmac_priv *priv = netdev_priv(dev);
 288
 289        if (priv->hw->pcs & STMMAC_PCS_RGMII ||
 290            priv->hw->pcs & STMMAC_PCS_SGMII) {
 291                struct rgmii_adv adv;
 292                u32 supported, advertising, lp_advertising;
 293
 294                if (!priv->xstats.pcs_link) {
 295                        cmd->base.speed = SPEED_UNKNOWN;
 296                        cmd->base.duplex = DUPLEX_UNKNOWN;
 297                        return 0;
 298                }
 299                cmd->base.duplex = priv->xstats.pcs_duplex;
 300
 301                cmd->base.speed = priv->xstats.pcs_speed;
 302
 303                /* Get and convert ADV/LP_ADV from the HW AN registers */
 304                if (stmmac_pcs_get_adv_lp(priv, priv->ioaddr, &adv))
 305                        return -EOPNOTSUPP;     /* should never happen indeed */
 306
 307                /* Encoding of PSE bits is defined in 802.3z, 37.2.1.4 */
 308
 309                ethtool_convert_link_mode_to_legacy_u32(
 310                        &supported, cmd->link_modes.supported);
 311                ethtool_convert_link_mode_to_legacy_u32(
 312                        &advertising, cmd->link_modes.advertising);
 313                ethtool_convert_link_mode_to_legacy_u32(
 314                        &lp_advertising, cmd->link_modes.lp_advertising);
 315
 316                if (adv.pause & STMMAC_PCS_PAUSE)
 317                        advertising |= ADVERTISED_Pause;
 318                if (adv.pause & STMMAC_PCS_ASYM_PAUSE)
 319                        advertising |= ADVERTISED_Asym_Pause;
 320                if (adv.lp_pause & STMMAC_PCS_PAUSE)
 321                        lp_advertising |= ADVERTISED_Pause;
 322                if (adv.lp_pause & STMMAC_PCS_ASYM_PAUSE)
 323                        lp_advertising |= ADVERTISED_Asym_Pause;
 324
 325                /* Reg49[3] always set because ANE is always supported */
 326                cmd->base.autoneg = ADVERTISED_Autoneg;
 327                supported |= SUPPORTED_Autoneg;
 328                advertising |= ADVERTISED_Autoneg;
 329                lp_advertising |= ADVERTISED_Autoneg;
 330
 331                if (adv.duplex) {
 332                        supported |= (SUPPORTED_1000baseT_Full |
 333                                      SUPPORTED_100baseT_Full |
 334                                      SUPPORTED_10baseT_Full);
 335                        advertising |= (ADVERTISED_1000baseT_Full |
 336                                        ADVERTISED_100baseT_Full |
 337                                        ADVERTISED_10baseT_Full);
 338                } else {
 339                        supported |= (SUPPORTED_1000baseT_Half |
 340                                      SUPPORTED_100baseT_Half |
 341                                      SUPPORTED_10baseT_Half);
 342                        advertising |= (ADVERTISED_1000baseT_Half |
 343                                        ADVERTISED_100baseT_Half |
 344                                        ADVERTISED_10baseT_Half);
 345                }
 346                if (adv.lp_duplex)
 347                        lp_advertising |= (ADVERTISED_1000baseT_Full |
 348                                           ADVERTISED_100baseT_Full |
 349                                           ADVERTISED_10baseT_Full);
 350                else
 351                        lp_advertising |= (ADVERTISED_1000baseT_Half |
 352                                           ADVERTISED_100baseT_Half |
 353                                           ADVERTISED_10baseT_Half);
 354                cmd->base.port = PORT_OTHER;
 355
 356                ethtool_convert_legacy_u32_to_link_mode(
 357                        cmd->link_modes.supported, supported);
 358                ethtool_convert_legacy_u32_to_link_mode(
 359                        cmd->link_modes.advertising, advertising);
 360                ethtool_convert_legacy_u32_to_link_mode(
 361                        cmd->link_modes.lp_advertising, lp_advertising);
 362
 363                return 0;
 364        }
 365
 366        return phylink_ethtool_ksettings_get(priv->phylink, cmd);
 367}
 368
 369static int
 370stmmac_ethtool_set_link_ksettings(struct net_device *dev,
 371                                  const struct ethtool_link_ksettings *cmd)
 372{
 373        struct stmmac_priv *priv = netdev_priv(dev);
 374
 375        if (priv->hw->pcs & STMMAC_PCS_RGMII ||
 376            priv->hw->pcs & STMMAC_PCS_SGMII) {
 377                u32 mask = ADVERTISED_Autoneg | ADVERTISED_Pause;
 378
 379                /* Only support ANE */
 380                if (cmd->base.autoneg != AUTONEG_ENABLE)
 381                        return -EINVAL;
 382
 383                mask &= (ADVERTISED_1000baseT_Half |
 384                        ADVERTISED_1000baseT_Full |
 385                        ADVERTISED_100baseT_Half |
 386                        ADVERTISED_100baseT_Full |
 387                        ADVERTISED_10baseT_Half |
 388                        ADVERTISED_10baseT_Full);
 389
 390                mutex_lock(&priv->lock);
 391                stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
 392                mutex_unlock(&priv->lock);
 393
 394                return 0;
 395        }
 396
 397        return phylink_ethtool_ksettings_set(priv->phylink, cmd);
 398}
 399
 400static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
 401{
 402        struct stmmac_priv *priv = netdev_priv(dev);
 403        return priv->msg_enable;
 404}
 405
 406static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
 407{
 408        struct stmmac_priv *priv = netdev_priv(dev);
 409        priv->msg_enable = level;
 410
 411}
 412
 413static int stmmac_check_if_running(struct net_device *dev)
 414{
 415        if (!netif_running(dev))
 416                return -EBUSY;
 417        return 0;
 418}
 419
 420static int stmmac_ethtool_get_regs_len(struct net_device *dev)
 421{
 422        struct stmmac_priv *priv = netdev_priv(dev);
 423
 424        if (priv->plat->has_xgmac)
 425                return XGMAC_REGSIZE * 4;
 426        return REG_SPACE_SIZE;
 427}
 428
 429static void stmmac_ethtool_gregs(struct net_device *dev,
 430                          struct ethtool_regs *regs, void *space)
 431{
 432        struct stmmac_priv *priv = netdev_priv(dev);
 433        u32 *reg_space = (u32 *) space;
 434
 435        stmmac_dump_mac_regs(priv, priv->hw, reg_space);
 436        stmmac_dump_dma_regs(priv, priv->ioaddr, reg_space);
 437
 438        if (!priv->plat->has_xgmac) {
 439                /* Copy DMA registers to where ethtool expects them */
 440                memcpy(&reg_space[ETHTOOL_DMA_OFFSET],
 441                       &reg_space[DMA_BUS_MODE / 4],
 442                       NUM_DWMAC1000_DMA_REGS * 4);
 443        }
 444}
 445
 446static int stmmac_nway_reset(struct net_device *dev)
 447{
 448        struct stmmac_priv *priv = netdev_priv(dev);
 449
 450        return phylink_ethtool_nway_reset(priv->phylink);
 451}
 452
 453static void stmmac_get_ringparam(struct net_device *netdev,
 454                                 struct ethtool_ringparam *ring)
 455{
 456        struct stmmac_priv *priv = netdev_priv(netdev);
 457
 458        ring->rx_max_pending = DMA_MAX_RX_SIZE;
 459        ring->tx_max_pending = DMA_MAX_TX_SIZE;
 460        ring->rx_pending = priv->dma_rx_size;
 461        ring->tx_pending = priv->dma_tx_size;
 462}
 463
 464static int stmmac_set_ringparam(struct net_device *netdev,
 465                                struct ethtool_ringparam *ring)
 466{
 467        if (ring->rx_mini_pending || ring->rx_jumbo_pending ||
 468            ring->rx_pending < DMA_MIN_RX_SIZE ||
 469            ring->rx_pending > DMA_MAX_RX_SIZE ||
 470            !is_power_of_2(ring->rx_pending) ||
 471            ring->tx_pending < DMA_MIN_TX_SIZE ||
 472            ring->tx_pending > DMA_MAX_TX_SIZE ||
 473            !is_power_of_2(ring->tx_pending))
 474                return -EINVAL;
 475
 476        return stmmac_reinit_ringparam(netdev, ring->rx_pending,
 477                                       ring->tx_pending);
 478}
 479
 480static void
 481stmmac_get_pauseparam(struct net_device *netdev,
 482                      struct ethtool_pauseparam *pause)
 483{
 484        struct stmmac_priv *priv = netdev_priv(netdev);
 485        struct rgmii_adv adv_lp;
 486
 487        if (priv->hw->pcs && !stmmac_pcs_get_adv_lp(priv, priv->ioaddr, &adv_lp)) {
 488                pause->autoneg = 1;
 489                if (!adv_lp.pause)
 490                        return;
 491        } else {
 492                phylink_ethtool_get_pauseparam(priv->phylink, pause);
 493        }
 494}
 495
 496static int
 497stmmac_set_pauseparam(struct net_device *netdev,
 498                      struct ethtool_pauseparam *pause)
 499{
 500        struct stmmac_priv *priv = netdev_priv(netdev);
 501        struct rgmii_adv adv_lp;
 502
 503        if (priv->hw->pcs && !stmmac_pcs_get_adv_lp(priv, priv->ioaddr, &adv_lp)) {
 504                pause->autoneg = 1;
 505                if (!adv_lp.pause)
 506                        return -EOPNOTSUPP;
 507                return 0;
 508        } else {
 509                return phylink_ethtool_set_pauseparam(priv->phylink, pause);
 510        }
 511}
 512
 513static void stmmac_get_ethtool_stats(struct net_device *dev,
 514                                 struct ethtool_stats *dummy, u64 *data)
 515{
 516        struct stmmac_priv *priv = netdev_priv(dev);
 517        u32 rx_queues_count = priv->plat->rx_queues_to_use;
 518        u32 tx_queues_count = priv->plat->tx_queues_to_use;
 519        unsigned long count;
 520        int i, j = 0, ret;
 521
 522        if (priv->dma_cap.asp) {
 523                for (i = 0; i < STMMAC_SAFETY_FEAT_SIZE; i++) {
 524                        if (!stmmac_safety_feat_dump(priv, &priv->sstats, i,
 525                                                &count, NULL))
 526                                data[j++] = count;
 527                }
 528        }
 529
 530        /* Update the DMA HW counters for dwmac10/100 */
 531        ret = stmmac_dma_diagnostic_fr(priv, &dev->stats, (void *) &priv->xstats,
 532                        priv->ioaddr);
 533        if (ret) {
 534                /* If supported, for new GMAC chips expose the MMC counters */
 535                if (priv->dma_cap.rmon) {
 536                        stmmac_mmc_read(priv, priv->mmcaddr, &priv->mmc);
 537
 538                        for (i = 0; i < STMMAC_MMC_STATS_LEN; i++) {
 539                                char *p;
 540                                p = (char *)priv + stmmac_mmc[i].stat_offset;
 541
 542                                data[j++] = (stmmac_mmc[i].sizeof_stat ==
 543                                             sizeof(u64)) ? (*(u64 *)p) :
 544                                             (*(u32 *)p);
 545                        }
 546                }
 547                if (priv->eee_enabled) {
 548                        int val = phylink_get_eee_err(priv->phylink);
 549                        if (val)
 550                                priv->xstats.phy_eee_wakeup_error_n = val;
 551                }
 552
 553                if (priv->synopsys_id >= DWMAC_CORE_3_50)
 554                        stmmac_mac_debug(priv, priv->ioaddr,
 555                                        (void *)&priv->xstats,
 556                                        rx_queues_count, tx_queues_count);
 557        }
 558        for (i = 0; i < STMMAC_STATS_LEN; i++) {
 559                char *p = (char *)priv + stmmac_gstrings_stats[i].stat_offset;
 560                data[j++] = (stmmac_gstrings_stats[i].sizeof_stat ==
 561                             sizeof(u64)) ? (*(u64 *)p) : (*(u32 *)p);
 562        }
 563}
 564
 565static int stmmac_get_sset_count(struct net_device *netdev, int sset)
 566{
 567        struct stmmac_priv *priv = netdev_priv(netdev);
 568        int i, len, safety_len = 0;
 569
 570        switch (sset) {
 571        case ETH_SS_STATS:
 572                len = STMMAC_STATS_LEN;
 573
 574                if (priv->dma_cap.rmon)
 575                        len += STMMAC_MMC_STATS_LEN;
 576                if (priv->dma_cap.asp) {
 577                        for (i = 0; i < STMMAC_SAFETY_FEAT_SIZE; i++) {
 578                                if (!stmmac_safety_feat_dump(priv,
 579                                                        &priv->sstats, i,
 580                                                        NULL, NULL))
 581                                        safety_len++;
 582                        }
 583
 584                        len += safety_len;
 585                }
 586
 587                return len;
 588        default:
 589                return -EOPNOTSUPP;
 590        }
 591}
 592
 593static void stmmac_get_strings(struct net_device *dev, u32 stringset, u8 *data)
 594{
 595        int i;
 596        u8 *p = data;
 597        struct stmmac_priv *priv = netdev_priv(dev);
 598
 599        switch (stringset) {
 600        case ETH_SS_STATS:
 601                if (priv->dma_cap.asp) {
 602                        for (i = 0; i < STMMAC_SAFETY_FEAT_SIZE; i++) {
 603                                const char *desc;
 604                                if (!stmmac_safety_feat_dump(priv,
 605                                                        &priv->sstats, i,
 606                                                        NULL, &desc)) {
 607                                        memcpy(p, desc, ETH_GSTRING_LEN);
 608                                        p += ETH_GSTRING_LEN;
 609                                }
 610                        }
 611                }
 612                if (priv->dma_cap.rmon)
 613                        for (i = 0; i < STMMAC_MMC_STATS_LEN; i++) {
 614                                memcpy(p, stmmac_mmc[i].stat_string,
 615                                       ETH_GSTRING_LEN);
 616                                p += ETH_GSTRING_LEN;
 617                        }
 618                for (i = 0; i < STMMAC_STATS_LEN; i++) {
 619                        memcpy(p, stmmac_gstrings_stats[i].stat_string,
 620                                ETH_GSTRING_LEN);
 621                        p += ETH_GSTRING_LEN;
 622                }
 623                break;
 624        default:
 625                WARN_ON(1);
 626                break;
 627        }
 628}
 629
 630/* Currently only support WOL through Magic packet. */
 631static void stmmac_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
 632{
 633        struct stmmac_priv *priv = netdev_priv(dev);
 634
 635        if (!priv->plat->pmt)
 636                return phylink_ethtool_get_wol(priv->phylink, wol);
 637
 638        mutex_lock(&priv->lock);
 639        if (device_can_wakeup(priv->device)) {
 640                wol->supported = WAKE_MAGIC | WAKE_UCAST;
 641                if (priv->hw_cap_support && !priv->dma_cap.pmt_magic_frame)
 642                        wol->supported &= ~WAKE_MAGIC;
 643                wol->wolopts = priv->wolopts;
 644        }
 645        mutex_unlock(&priv->lock);
 646}
 647
 648static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
 649{
 650        struct stmmac_priv *priv = netdev_priv(dev);
 651        u32 support = WAKE_MAGIC | WAKE_UCAST;
 652
 653        if (!device_can_wakeup(priv->device))
 654                return -EOPNOTSUPP;
 655
 656        if (!priv->plat->pmt) {
 657                int ret = phylink_ethtool_set_wol(priv->phylink, wol);
 658
 659                if (!ret)
 660                        device_set_wakeup_enable(priv->device, !!wol->wolopts);
 661                return ret;
 662        }
 663
 664        /* By default almost all GMAC devices support the WoL via
 665         * magic frame but we can disable it if the HW capability
 666         * register shows no support for pmt_magic_frame. */
 667        if ((priv->hw_cap_support) && (!priv->dma_cap.pmt_magic_frame))
 668                wol->wolopts &= ~WAKE_MAGIC;
 669
 670        if (wol->wolopts & ~support)
 671                return -EINVAL;
 672
 673        if (wol->wolopts) {
 674                pr_info("stmmac: wakeup enable\n");
 675                device_set_wakeup_enable(priv->device, 1);
 676                enable_irq_wake(priv->wol_irq);
 677        } else {
 678                device_set_wakeup_enable(priv->device, 0);
 679                disable_irq_wake(priv->wol_irq);
 680        }
 681
 682        mutex_lock(&priv->lock);
 683        priv->wolopts = wol->wolopts;
 684        mutex_unlock(&priv->lock);
 685
 686        return 0;
 687}
 688
 689static int stmmac_ethtool_op_get_eee(struct net_device *dev,
 690                                     struct ethtool_eee *edata)
 691{
 692        struct stmmac_priv *priv = netdev_priv(dev);
 693
 694        if (!priv->dma_cap.eee)
 695                return -EOPNOTSUPP;
 696
 697        edata->eee_enabled = priv->eee_enabled;
 698        edata->eee_active = priv->eee_active;
 699        edata->tx_lpi_timer = priv->tx_lpi_timer;
 700        edata->tx_lpi_enabled = priv->tx_lpi_enabled;
 701
 702        return phylink_ethtool_get_eee(priv->phylink, edata);
 703}
 704
 705static int stmmac_ethtool_op_set_eee(struct net_device *dev,
 706                                     struct ethtool_eee *edata)
 707{
 708        struct stmmac_priv *priv = netdev_priv(dev);
 709        int ret;
 710
 711        if (!priv->dma_cap.eee)
 712                return -EOPNOTSUPP;
 713
 714        if (priv->tx_lpi_enabled != edata->tx_lpi_enabled)
 715                netdev_warn(priv->dev,
 716                            "Setting EEE tx-lpi is not supported\n");
 717
 718        if (!edata->eee_enabled)
 719                stmmac_disable_eee_mode(priv);
 720
 721        ret = phylink_ethtool_set_eee(priv->phylink, edata);
 722        if (ret)
 723                return ret;
 724
 725        if (edata->eee_enabled &&
 726            priv->tx_lpi_timer != edata->tx_lpi_timer) {
 727                priv->tx_lpi_timer = edata->tx_lpi_timer;
 728                stmmac_eee_init(priv);
 729        }
 730
 731        return 0;
 732}
 733
 734static u32 stmmac_usec2riwt(u32 usec, struct stmmac_priv *priv)
 735{
 736        unsigned long clk = clk_get_rate(priv->plat->stmmac_clk);
 737
 738        if (!clk) {
 739                clk = priv->plat->clk_ref_rate;
 740                if (!clk)
 741                        return 0;
 742        }
 743
 744        return (usec * (clk / 1000000)) / 256;
 745}
 746
 747static u32 stmmac_riwt2usec(u32 riwt, struct stmmac_priv *priv)
 748{
 749        unsigned long clk = clk_get_rate(priv->plat->stmmac_clk);
 750
 751        if (!clk) {
 752                clk = priv->plat->clk_ref_rate;
 753                if (!clk)
 754                        return 0;
 755        }
 756
 757        return (riwt * 256) / (clk / 1000000);
 758}
 759
 760static int stmmac_get_coalesce(struct net_device *dev,
 761                               struct ethtool_coalesce *ec)
 762{
 763        struct stmmac_priv *priv = netdev_priv(dev);
 764
 765        ec->tx_coalesce_usecs = priv->tx_coal_timer;
 766        ec->tx_max_coalesced_frames = priv->tx_coal_frames;
 767
 768        if (priv->use_riwt) {
 769                ec->rx_max_coalesced_frames = priv->rx_coal_frames;
 770                ec->rx_coalesce_usecs = stmmac_riwt2usec(priv->rx_riwt, priv);
 771        }
 772
 773        return 0;
 774}
 775
 776static int stmmac_set_coalesce(struct net_device *dev,
 777                               struct ethtool_coalesce *ec)
 778{
 779        struct stmmac_priv *priv = netdev_priv(dev);
 780        u32 rx_cnt = priv->plat->rx_queues_to_use;
 781        unsigned int rx_riwt;
 782
 783        if (priv->use_riwt && (ec->rx_coalesce_usecs > 0)) {
 784                rx_riwt = stmmac_usec2riwt(ec->rx_coalesce_usecs, priv);
 785
 786                if ((rx_riwt > MAX_DMA_RIWT) || (rx_riwt < MIN_DMA_RIWT))
 787                        return -EINVAL;
 788
 789                priv->rx_riwt = rx_riwt;
 790                stmmac_rx_watchdog(priv, priv->ioaddr, priv->rx_riwt, rx_cnt);
 791        }
 792
 793        if ((ec->tx_coalesce_usecs == 0) &&
 794            (ec->tx_max_coalesced_frames == 0))
 795                return -EINVAL;
 796
 797        if ((ec->tx_coalesce_usecs > STMMAC_MAX_COAL_TX_TICK) ||
 798            (ec->tx_max_coalesced_frames > STMMAC_TX_MAX_FRAMES))
 799                return -EINVAL;
 800
 801        /* Only copy relevant parameters, ignore all others. */
 802        priv->tx_coal_frames = ec->tx_max_coalesced_frames;
 803        priv->tx_coal_timer = ec->tx_coalesce_usecs;
 804        priv->rx_coal_frames = ec->rx_max_coalesced_frames;
 805        return 0;
 806}
 807
 808static int stmmac_get_rxnfc(struct net_device *dev,
 809                            struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
 810{
 811        struct stmmac_priv *priv = netdev_priv(dev);
 812
 813        switch (rxnfc->cmd) {
 814        case ETHTOOL_GRXRINGS:
 815                rxnfc->data = priv->plat->rx_queues_to_use;
 816                break;
 817        default:
 818                return -EOPNOTSUPP;
 819        }
 820
 821        return 0;
 822}
 823
 824static u32 stmmac_get_rxfh_key_size(struct net_device *dev)
 825{
 826        struct stmmac_priv *priv = netdev_priv(dev);
 827
 828        return sizeof(priv->rss.key);
 829}
 830
 831static u32 stmmac_get_rxfh_indir_size(struct net_device *dev)
 832{
 833        struct stmmac_priv *priv = netdev_priv(dev);
 834
 835        return ARRAY_SIZE(priv->rss.table);
 836}
 837
 838static int stmmac_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
 839                           u8 *hfunc)
 840{
 841        struct stmmac_priv *priv = netdev_priv(dev);
 842        int i;
 843
 844        if (indir) {
 845                for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
 846                        indir[i] = priv->rss.table[i];
 847        }
 848
 849        if (key)
 850                memcpy(key, priv->rss.key, sizeof(priv->rss.key));
 851        if (hfunc)
 852                *hfunc = ETH_RSS_HASH_TOP;
 853
 854        return 0;
 855}
 856
 857static int stmmac_set_rxfh(struct net_device *dev, const u32 *indir,
 858                           const u8 *key, const u8 hfunc)
 859{
 860        struct stmmac_priv *priv = netdev_priv(dev);
 861        int i;
 862
 863        if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP))
 864                return -EOPNOTSUPP;
 865
 866        if (indir) {
 867                for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
 868                        priv->rss.table[i] = indir[i];
 869        }
 870
 871        if (key)
 872                memcpy(priv->rss.key, key, sizeof(priv->rss.key));
 873
 874        return stmmac_rss_configure(priv, priv->hw, &priv->rss,
 875                                    priv->plat->rx_queues_to_use);
 876}
 877
 878static void stmmac_get_channels(struct net_device *dev,
 879                                struct ethtool_channels *chan)
 880{
 881        struct stmmac_priv *priv = netdev_priv(dev);
 882
 883        chan->rx_count = priv->plat->rx_queues_to_use;
 884        chan->tx_count = priv->plat->tx_queues_to_use;
 885        chan->max_rx = priv->dma_cap.number_rx_queues;
 886        chan->max_tx = priv->dma_cap.number_tx_queues;
 887}
 888
 889static int stmmac_set_channels(struct net_device *dev,
 890                               struct ethtool_channels *chan)
 891{
 892        struct stmmac_priv *priv = netdev_priv(dev);
 893
 894        if (chan->rx_count > priv->dma_cap.number_rx_queues ||
 895            chan->tx_count > priv->dma_cap.number_tx_queues ||
 896            !chan->rx_count || !chan->tx_count)
 897                return -EINVAL;
 898
 899        return stmmac_reinit_queues(dev, chan->rx_count, chan->tx_count);
 900}
 901
 902static int stmmac_get_ts_info(struct net_device *dev,
 903                              struct ethtool_ts_info *info)
 904{
 905        struct stmmac_priv *priv = netdev_priv(dev);
 906
 907        if ((priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) {
 908
 909                info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
 910                                        SOF_TIMESTAMPING_TX_HARDWARE |
 911                                        SOF_TIMESTAMPING_RX_SOFTWARE |
 912                                        SOF_TIMESTAMPING_RX_HARDWARE |
 913                                        SOF_TIMESTAMPING_SOFTWARE |
 914                                        SOF_TIMESTAMPING_RAW_HARDWARE;
 915
 916                if (priv->ptp_clock)
 917                        info->phc_index = ptp_clock_index(priv->ptp_clock);
 918
 919                info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
 920
 921                info->rx_filters = ((1 << HWTSTAMP_FILTER_NONE) |
 922                                    (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
 923                                    (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
 924                                    (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
 925                                    (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
 926                                    (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
 927                                    (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
 928                                    (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
 929                                    (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
 930                                    (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
 931                                    (1 << HWTSTAMP_FILTER_ALL));
 932                return 0;
 933        } else
 934                return ethtool_op_get_ts_info(dev, info);
 935}
 936
 937static int stmmac_get_tunable(struct net_device *dev,
 938                              const struct ethtool_tunable *tuna, void *data)
 939{
 940        struct stmmac_priv *priv = netdev_priv(dev);
 941        int ret = 0;
 942
 943        switch (tuna->id) {
 944        case ETHTOOL_RX_COPYBREAK:
 945                *(u32 *)data = priv->rx_copybreak;
 946                break;
 947        default:
 948                ret = -EINVAL;
 949                break;
 950        }
 951
 952        return ret;
 953}
 954
 955static int stmmac_set_tunable(struct net_device *dev,
 956                              const struct ethtool_tunable *tuna,
 957                              const void *data)
 958{
 959        struct stmmac_priv *priv = netdev_priv(dev);
 960        int ret = 0;
 961
 962        switch (tuna->id) {
 963        case ETHTOOL_RX_COPYBREAK:
 964                priv->rx_copybreak = *(u32 *)data;
 965                break;
 966        default:
 967                ret = -EINVAL;
 968                break;
 969        }
 970
 971        return ret;
 972}
 973
 974static const struct ethtool_ops stmmac_ethtool_ops = {
 975        .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
 976                                     ETHTOOL_COALESCE_MAX_FRAMES,
 977        .begin = stmmac_check_if_running,
 978        .get_drvinfo = stmmac_ethtool_getdrvinfo,
 979        .get_msglevel = stmmac_ethtool_getmsglevel,
 980        .set_msglevel = stmmac_ethtool_setmsglevel,
 981        .get_regs = stmmac_ethtool_gregs,
 982        .get_regs_len = stmmac_ethtool_get_regs_len,
 983        .get_link = ethtool_op_get_link,
 984        .nway_reset = stmmac_nway_reset,
 985        .get_ringparam = stmmac_get_ringparam,
 986        .set_ringparam = stmmac_set_ringparam,
 987        .get_pauseparam = stmmac_get_pauseparam,
 988        .set_pauseparam = stmmac_set_pauseparam,
 989        .get_ethtool_stats = stmmac_get_ethtool_stats,
 990        .get_strings = stmmac_get_strings,
 991        .get_wol = stmmac_get_wol,
 992        .set_wol = stmmac_set_wol,
 993        .get_eee = stmmac_ethtool_op_get_eee,
 994        .set_eee = stmmac_ethtool_op_set_eee,
 995        .get_sset_count = stmmac_get_sset_count,
 996        .get_rxnfc = stmmac_get_rxnfc,
 997        .get_rxfh_key_size = stmmac_get_rxfh_key_size,
 998        .get_rxfh_indir_size = stmmac_get_rxfh_indir_size,
 999        .get_rxfh = stmmac_get_rxfh,
1000        .set_rxfh = stmmac_set_rxfh,
1001        .get_ts_info = stmmac_get_ts_info,
1002        .get_coalesce = stmmac_get_coalesce,
1003        .set_coalesce = stmmac_set_coalesce,
1004        .get_channels = stmmac_get_channels,
1005        .set_channels = stmmac_set_channels,
1006        .get_tunable = stmmac_get_tunable,
1007        .set_tunable = stmmac_set_tunable,
1008        .get_link_ksettings = stmmac_ethtool_get_link_ksettings,
1009        .set_link_ksettings = stmmac_ethtool_set_link_ksettings,
1010};
1011
1012void stmmac_set_ethtool_ops(struct net_device *netdev)
1013{
1014        netdev->ethtool_ops = &stmmac_ethtool_ops;
1015}
1016