11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22b133ad6SJeff Kirsher /*
32b133ad6SJeff Kirsher * Copyright(c) 2009 - 2009 Atheros Corporation. All rights reserved.
42b133ad6SJeff Kirsher *
52b133ad6SJeff Kirsher * Derived from Intel e1000 driver
62b133ad6SJeff Kirsher * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
72b133ad6SJeff Kirsher */
82b133ad6SJeff Kirsher
92b133ad6SJeff Kirsher #include <linux/netdevice.h>
102b133ad6SJeff Kirsher #include <linux/ethtool.h>
112b133ad6SJeff Kirsher #include <linux/slab.h>
122b133ad6SJeff Kirsher
132b133ad6SJeff Kirsher #include "atl1c.h"
142b133ad6SJeff Kirsher
atl1c_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)1558046c70SPhilippe Reynes static int atl1c_get_link_ksettings(struct net_device *netdev,
1658046c70SPhilippe Reynes struct ethtool_link_ksettings *cmd)
172b133ad6SJeff Kirsher {
182b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
192b133ad6SJeff Kirsher struct atl1c_hw *hw = &adapter->hw;
2058046c70SPhilippe Reynes u32 supported, advertising;
212b133ad6SJeff Kirsher
2258046c70SPhilippe Reynes supported = (SUPPORTED_10baseT_Half |
232b133ad6SJeff Kirsher SUPPORTED_10baseT_Full |
242b133ad6SJeff Kirsher SUPPORTED_100baseT_Half |
252b133ad6SJeff Kirsher SUPPORTED_100baseT_Full |
262b133ad6SJeff Kirsher SUPPORTED_Autoneg |
272b133ad6SJeff Kirsher SUPPORTED_TP);
282b133ad6SJeff Kirsher if (hw->link_cap_flags & ATL1C_LINK_CAP_1000M)
2958046c70SPhilippe Reynes supported |= SUPPORTED_1000baseT_Full;
302b133ad6SJeff Kirsher
3158046c70SPhilippe Reynes advertising = ADVERTISED_TP;
322b133ad6SJeff Kirsher
3358046c70SPhilippe Reynes advertising |= hw->autoneg_advertised;
342b133ad6SJeff Kirsher
3558046c70SPhilippe Reynes cmd->base.port = PORT_TP;
3658046c70SPhilippe Reynes cmd->base.phy_address = 0;
372b133ad6SJeff Kirsher
382b133ad6SJeff Kirsher if (adapter->link_speed != SPEED_0) {
3958046c70SPhilippe Reynes cmd->base.speed = adapter->link_speed;
402b133ad6SJeff Kirsher if (adapter->link_duplex == FULL_DUPLEX)
4158046c70SPhilippe Reynes cmd->base.duplex = DUPLEX_FULL;
422b133ad6SJeff Kirsher else
4358046c70SPhilippe Reynes cmd->base.duplex = DUPLEX_HALF;
442b133ad6SJeff Kirsher } else {
4558046c70SPhilippe Reynes cmd->base.speed = SPEED_UNKNOWN;
4658046c70SPhilippe Reynes cmd->base.duplex = DUPLEX_UNKNOWN;
472b133ad6SJeff Kirsher }
482b133ad6SJeff Kirsher
4958046c70SPhilippe Reynes cmd->base.autoneg = AUTONEG_ENABLE;
5058046c70SPhilippe Reynes
5158046c70SPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
5258046c70SPhilippe Reynes supported);
5358046c70SPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
5458046c70SPhilippe Reynes advertising);
5558046c70SPhilippe Reynes
562b133ad6SJeff Kirsher return 0;
572b133ad6SJeff Kirsher }
582b133ad6SJeff Kirsher
atl1c_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * cmd)5958046c70SPhilippe Reynes static int atl1c_set_link_ksettings(struct net_device *netdev,
6058046c70SPhilippe Reynes const struct ethtool_link_ksettings *cmd)
612b133ad6SJeff Kirsher {
622b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
632b133ad6SJeff Kirsher struct atl1c_hw *hw = &adapter->hw;
642b133ad6SJeff Kirsher u16 autoneg_advertised;
652b133ad6SJeff Kirsher
662b133ad6SJeff Kirsher while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
672b133ad6SJeff Kirsher msleep(1);
682b133ad6SJeff Kirsher
6958046c70SPhilippe Reynes if (cmd->base.autoneg == AUTONEG_ENABLE) {
702b133ad6SJeff Kirsher autoneg_advertised = ADVERTISED_Autoneg;
712b133ad6SJeff Kirsher } else {
7258046c70SPhilippe Reynes u32 speed = cmd->base.speed;
732b133ad6SJeff Kirsher if (speed == SPEED_1000) {
7458046c70SPhilippe Reynes if (cmd->base.duplex != DUPLEX_FULL) {
752b133ad6SJeff Kirsher if (netif_msg_link(adapter))
762b133ad6SJeff Kirsher dev_warn(&adapter->pdev->dev,
772b133ad6SJeff Kirsher "1000M half is invalid\n");
782b133ad6SJeff Kirsher clear_bit(__AT_RESETTING, &adapter->flags);
792b133ad6SJeff Kirsher return -EINVAL;
802b133ad6SJeff Kirsher }
812b133ad6SJeff Kirsher autoneg_advertised = ADVERTISED_1000baseT_Full;
822b133ad6SJeff Kirsher } else if (speed == SPEED_100) {
8358046c70SPhilippe Reynes if (cmd->base.duplex == DUPLEX_FULL)
842b133ad6SJeff Kirsher autoneg_advertised = ADVERTISED_100baseT_Full;
852b133ad6SJeff Kirsher else
862b133ad6SJeff Kirsher autoneg_advertised = ADVERTISED_100baseT_Half;
872b133ad6SJeff Kirsher } else {
8858046c70SPhilippe Reynes if (cmd->base.duplex == DUPLEX_FULL)
892b133ad6SJeff Kirsher autoneg_advertised = ADVERTISED_10baseT_Full;
902b133ad6SJeff Kirsher else
912b133ad6SJeff Kirsher autoneg_advertised = ADVERTISED_10baseT_Half;
922b133ad6SJeff Kirsher }
932b133ad6SJeff Kirsher }
942b133ad6SJeff Kirsher
952b133ad6SJeff Kirsher if (hw->autoneg_advertised != autoneg_advertised) {
962b133ad6SJeff Kirsher hw->autoneg_advertised = autoneg_advertised;
972b133ad6SJeff Kirsher if (atl1c_restart_autoneg(hw) != 0) {
982b133ad6SJeff Kirsher if (netif_msg_link(adapter))
992b133ad6SJeff Kirsher dev_warn(&adapter->pdev->dev,
1002b133ad6SJeff Kirsher "ethtool speed/duplex setting failed\n");
1012b133ad6SJeff Kirsher clear_bit(__AT_RESETTING, &adapter->flags);
1022b133ad6SJeff Kirsher return -EINVAL;
1032b133ad6SJeff Kirsher }
1042b133ad6SJeff Kirsher }
1052b133ad6SJeff Kirsher clear_bit(__AT_RESETTING, &adapter->flags);
1062b133ad6SJeff Kirsher return 0;
1072b133ad6SJeff Kirsher }
1082b133ad6SJeff Kirsher
atl1c_get_msglevel(struct net_device * netdev)1092b133ad6SJeff Kirsher static u32 atl1c_get_msglevel(struct net_device *netdev)
1102b133ad6SJeff Kirsher {
1112b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
1122b133ad6SJeff Kirsher return adapter->msg_enable;
1132b133ad6SJeff Kirsher }
1142b133ad6SJeff Kirsher
atl1c_set_msglevel(struct net_device * netdev,u32 data)1152b133ad6SJeff Kirsher static void atl1c_set_msglevel(struct net_device *netdev, u32 data)
1162b133ad6SJeff Kirsher {
1172b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
1182b133ad6SJeff Kirsher adapter->msg_enable = data;
1192b133ad6SJeff Kirsher }
1202b133ad6SJeff Kirsher
atl1c_get_regs_len(struct net_device * netdev)1212b133ad6SJeff Kirsher static int atl1c_get_regs_len(struct net_device *netdev)
1222b133ad6SJeff Kirsher {
1232b133ad6SJeff Kirsher return AT_REGS_LEN;
1242b133ad6SJeff Kirsher }
1252b133ad6SJeff Kirsher
atl1c_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * p)1262b133ad6SJeff Kirsher static void atl1c_get_regs(struct net_device *netdev,
1272b133ad6SJeff Kirsher struct ethtool_regs *regs, void *p)
1282b133ad6SJeff Kirsher {
1292b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
1302b133ad6SJeff Kirsher struct atl1c_hw *hw = &adapter->hw;
1312b133ad6SJeff Kirsher u32 *regs_buff = p;
1322b133ad6SJeff Kirsher u16 phy_data;
1332b133ad6SJeff Kirsher
1342b133ad6SJeff Kirsher memset(p, 0, AT_REGS_LEN);
1352b133ad6SJeff Kirsher
136864ad85fSHuang, Xiong regs->version = 1;
1372b133ad6SJeff Kirsher AT_READ_REG(hw, REG_PM_CTRL, p++);
1382b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MAC_HALF_DUPLX_CTRL, p++);
1392b133ad6SJeff Kirsher AT_READ_REG(hw, REG_TWSI_CTRL, p++);
1402b133ad6SJeff Kirsher AT_READ_REG(hw, REG_PCIE_DEV_MISC_CTRL, p++);
1412b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MASTER_CTRL, p++);
1422b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MANUAL_TIMER_INIT, p++);
1432b133ad6SJeff Kirsher AT_READ_REG(hw, REG_IRQ_MODRT_TIMER_INIT, p++);
1442b133ad6SJeff Kirsher AT_READ_REG(hw, REG_GPHY_CTRL, p++);
1452b133ad6SJeff Kirsher AT_READ_REG(hw, REG_LINK_CTRL, p++);
1462b133ad6SJeff Kirsher AT_READ_REG(hw, REG_IDLE_STATUS, p++);
1472b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MDIO_CTRL, p++);
1487737fd96SHuang, Xiong AT_READ_REG(hw, REG_SERDES, p++);
1492b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MAC_CTRL, p++);
1502b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MAC_IPG_IFG, p++);
1512b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MAC_STA_ADDR, p++);
1522b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MAC_STA_ADDR+4, p++);
1532b133ad6SJeff Kirsher AT_READ_REG(hw, REG_RX_HASH_TABLE, p++);
1542b133ad6SJeff Kirsher AT_READ_REG(hw, REG_RX_HASH_TABLE+4, p++);
1552b133ad6SJeff Kirsher AT_READ_REG(hw, REG_RXQ_CTRL, p++);
1562b133ad6SJeff Kirsher AT_READ_REG(hw, REG_TXQ_CTRL, p++);
1572b133ad6SJeff Kirsher AT_READ_REG(hw, REG_MTU, p++);
1582b133ad6SJeff Kirsher AT_READ_REG(hw, REG_WOL_CTRL, p++);
1592b133ad6SJeff Kirsher
1602b133ad6SJeff Kirsher atl1c_read_phy_reg(hw, MII_BMCR, &phy_data);
161864ad85fSHuang, Xiong regs_buff[AT_REGS_LEN/sizeof(u32) - 2] = (u32) phy_data;
1622b133ad6SJeff Kirsher atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
163864ad85fSHuang, Xiong regs_buff[AT_REGS_LEN/sizeof(u32) - 1] = (u32) phy_data;
1642b133ad6SJeff Kirsher }
1652b133ad6SJeff Kirsher
atl1c_get_eeprom_len(struct net_device * netdev)1662b133ad6SJeff Kirsher static int atl1c_get_eeprom_len(struct net_device *netdev)
1672b133ad6SJeff Kirsher {
1682b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
1692b133ad6SJeff Kirsher
1702b133ad6SJeff Kirsher if (atl1c_check_eeprom_exist(&adapter->hw))
1712b133ad6SJeff Kirsher return AT_EEPROM_LEN;
1722b133ad6SJeff Kirsher else
1732b133ad6SJeff Kirsher return 0;
1742b133ad6SJeff Kirsher }
1752b133ad6SJeff Kirsher
atl1c_get_eeprom(struct net_device * netdev,struct ethtool_eeprom * eeprom,u8 * bytes)1762b133ad6SJeff Kirsher static int atl1c_get_eeprom(struct net_device *netdev,
1772b133ad6SJeff Kirsher struct ethtool_eeprom *eeprom, u8 *bytes)
1782b133ad6SJeff Kirsher {
1792b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
1802b133ad6SJeff Kirsher struct atl1c_hw *hw = &adapter->hw;
1812b133ad6SJeff Kirsher u32 *eeprom_buff;
1822b133ad6SJeff Kirsher int first_dword, last_dword;
1832b133ad6SJeff Kirsher int ret_val = 0;
1842b133ad6SJeff Kirsher int i;
1852b133ad6SJeff Kirsher
1862b133ad6SJeff Kirsher if (eeprom->len == 0)
1872b133ad6SJeff Kirsher return -EINVAL;
1882b133ad6SJeff Kirsher
1892b133ad6SJeff Kirsher if (!atl1c_check_eeprom_exist(hw)) /* not exist */
1902b133ad6SJeff Kirsher return -EINVAL;
1912b133ad6SJeff Kirsher
1922b133ad6SJeff Kirsher eeprom->magic = adapter->pdev->vendor |
1932b133ad6SJeff Kirsher (adapter->pdev->device << 16);
1942b133ad6SJeff Kirsher
1952b133ad6SJeff Kirsher first_dword = eeprom->offset >> 2;
1962b133ad6SJeff Kirsher last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
1972b133ad6SJeff Kirsher
1986da2ec56SKees Cook eeprom_buff = kmalloc_array(last_dword - first_dword + 1, sizeof(u32),
1996da2ec56SKees Cook GFP_KERNEL);
2002b133ad6SJeff Kirsher if (eeprom_buff == NULL)
2012b133ad6SJeff Kirsher return -ENOMEM;
2022b133ad6SJeff Kirsher
2032b133ad6SJeff Kirsher for (i = first_dword; i < last_dword; i++) {
2042b133ad6SJeff Kirsher if (!atl1c_read_eeprom(hw, i * 4, &(eeprom_buff[i-first_dword]))) {
2052b133ad6SJeff Kirsher kfree(eeprom_buff);
2062b133ad6SJeff Kirsher return -EIO;
2072b133ad6SJeff Kirsher }
2082b133ad6SJeff Kirsher }
2092b133ad6SJeff Kirsher
2102b133ad6SJeff Kirsher memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 3),
2112b133ad6SJeff Kirsher eeprom->len);
2122b133ad6SJeff Kirsher kfree(eeprom_buff);
2132b133ad6SJeff Kirsher
2142b133ad6SJeff Kirsher return ret_val;
2152b133ad6SJeff Kirsher return 0;
2162b133ad6SJeff Kirsher }
2172b133ad6SJeff Kirsher
atl1c_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)2182b133ad6SJeff Kirsher static void atl1c_get_drvinfo(struct net_device *netdev,
2192b133ad6SJeff Kirsher struct ethtool_drvinfo *drvinfo)
2202b133ad6SJeff Kirsher {
2212b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
2222b133ad6SJeff Kirsher
223*f029c781SWolfram Sang strscpy(drvinfo->driver, atl1c_driver_name, sizeof(drvinfo->driver));
224*f029c781SWolfram Sang strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
2252b133ad6SJeff Kirsher sizeof(drvinfo->bus_info));
2262b133ad6SJeff Kirsher }
2272b133ad6SJeff Kirsher
atl1c_get_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)2282b133ad6SJeff Kirsher static void atl1c_get_wol(struct net_device *netdev,
2292b133ad6SJeff Kirsher struct ethtool_wolinfo *wol)
2302b133ad6SJeff Kirsher {
2312b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
2322b133ad6SJeff Kirsher
2332b133ad6SJeff Kirsher wol->supported = WAKE_MAGIC | WAKE_PHY;
2342b133ad6SJeff Kirsher wol->wolopts = 0;
2352b133ad6SJeff Kirsher
2362b133ad6SJeff Kirsher if (adapter->wol & AT_WUFC_EX)
2372b133ad6SJeff Kirsher wol->wolopts |= WAKE_UCAST;
2382b133ad6SJeff Kirsher if (adapter->wol & AT_WUFC_MC)
2392b133ad6SJeff Kirsher wol->wolopts |= WAKE_MCAST;
2402b133ad6SJeff Kirsher if (adapter->wol & AT_WUFC_BC)
2412b133ad6SJeff Kirsher wol->wolopts |= WAKE_BCAST;
2422b133ad6SJeff Kirsher if (adapter->wol & AT_WUFC_MAG)
2432b133ad6SJeff Kirsher wol->wolopts |= WAKE_MAGIC;
2442b133ad6SJeff Kirsher if (adapter->wol & AT_WUFC_LNKC)
2452b133ad6SJeff Kirsher wol->wolopts |= WAKE_PHY;
2462b133ad6SJeff Kirsher }
2472b133ad6SJeff Kirsher
atl1c_set_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)2482b133ad6SJeff Kirsher static int atl1c_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2492b133ad6SJeff Kirsher {
2502b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
2512b133ad6SJeff Kirsher
2522b133ad6SJeff Kirsher if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE |
2532b133ad6SJeff Kirsher WAKE_UCAST | WAKE_BCAST | WAKE_MCAST))
2542b133ad6SJeff Kirsher return -EOPNOTSUPP;
2552b133ad6SJeff Kirsher /* these settings will always override what we currently have */
2562b133ad6SJeff Kirsher adapter->wol = 0;
2572b133ad6SJeff Kirsher
2582b133ad6SJeff Kirsher if (wol->wolopts & WAKE_MAGIC)
2592b133ad6SJeff Kirsher adapter->wol |= AT_WUFC_MAG;
2602b133ad6SJeff Kirsher if (wol->wolopts & WAKE_PHY)
2612b133ad6SJeff Kirsher adapter->wol |= AT_WUFC_LNKC;
2622b133ad6SJeff Kirsher
2632b133ad6SJeff Kirsher device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
2642b133ad6SJeff Kirsher
2652b133ad6SJeff Kirsher return 0;
2662b133ad6SJeff Kirsher }
2672b133ad6SJeff Kirsher
atl1c_nway_reset(struct net_device * netdev)2682b133ad6SJeff Kirsher static int atl1c_nway_reset(struct net_device *netdev)
2692b133ad6SJeff Kirsher {
2702b133ad6SJeff Kirsher struct atl1c_adapter *adapter = netdev_priv(netdev);
2712b133ad6SJeff Kirsher if (netif_running(netdev))
2722b133ad6SJeff Kirsher atl1c_reinit_locked(adapter);
2732b133ad6SJeff Kirsher return 0;
2742b133ad6SJeff Kirsher }
2752b133ad6SJeff Kirsher
2762b133ad6SJeff Kirsher static const struct ethtool_ops atl1c_ethtool_ops = {
2772b133ad6SJeff Kirsher .get_drvinfo = atl1c_get_drvinfo,
2782b133ad6SJeff Kirsher .get_regs_len = atl1c_get_regs_len,
2792b133ad6SJeff Kirsher .get_regs = atl1c_get_regs,
2802b133ad6SJeff Kirsher .get_wol = atl1c_get_wol,
2812b133ad6SJeff Kirsher .set_wol = atl1c_set_wol,
2822b133ad6SJeff Kirsher .get_msglevel = atl1c_get_msglevel,
2832b133ad6SJeff Kirsher .set_msglevel = atl1c_set_msglevel,
2842b133ad6SJeff Kirsher .nway_reset = atl1c_nway_reset,
2852b133ad6SJeff Kirsher .get_link = ethtool_op_get_link,
2862b133ad6SJeff Kirsher .get_eeprom_len = atl1c_get_eeprom_len,
2872b133ad6SJeff Kirsher .get_eeprom = atl1c_get_eeprom,
28858046c70SPhilippe Reynes .get_link_ksettings = atl1c_get_link_ksettings,
28958046c70SPhilippe Reynes .set_link_ksettings = atl1c_set_link_ksettings,
2902b133ad6SJeff Kirsher };
2912b133ad6SJeff Kirsher
atl1c_set_ethtool_ops(struct net_device * netdev)2922b133ad6SJeff Kirsher void atl1c_set_ethtool_ops(struct net_device *netdev)
2932b133ad6SJeff Kirsher {
2947ad24ea4SWilfried Klaebe netdev->ethtool_ops = &atl1c_ethtool_ops;
2952b133ad6SJeff Kirsher }
296