2 * Copyright (c) 2013 Johannes Berg <johannes@sipsolutions.net>
4 * This file is free software: you may copy, redistribute and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation, either version 2 of the License, or (at your
7 * option) any later version.
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * This file incorporates work covered by the following copyright and
20 * Copyright (c) 2012 Qualcomm Atheros, Inc.
22 * Permission to use, copy, modify, and/or distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies.
26 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
27 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
29 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
31 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
32 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 #include <linux/pci.h>
37 #include <linux/tcp.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/ethtool.h>
41 #include <linux/mdio.h>
42 #include <linux/interrupt.h>
43 #include <asm/byteorder.h>
49 static u32 alx_get_supported_speeds(struct alx_hw *hw)
51 u32 supported = SUPPORTED_10baseT_Half |
52 SUPPORTED_10baseT_Full |
53 SUPPORTED_100baseT_Half |
54 SUPPORTED_100baseT_Full;
57 supported |= SUPPORTED_1000baseT_Full;
59 BUILD_BUG_ON(SUPPORTED_10baseT_Half != ADVERTISED_10baseT_Half);
60 BUILD_BUG_ON(SUPPORTED_10baseT_Full != ADVERTISED_10baseT_Full);
61 BUILD_BUG_ON(SUPPORTED_100baseT_Half != ADVERTISED_100baseT_Half);
62 BUILD_BUG_ON(SUPPORTED_100baseT_Full != ADVERTISED_100baseT_Full);
63 BUILD_BUG_ON(SUPPORTED_1000baseT_Full != ADVERTISED_1000baseT_Full);
68 static int alx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
70 struct alx_priv *alx = netdev_priv(netdev);
71 struct alx_hw *hw = &alx->hw;
73 ecmd->supported = SUPPORTED_Autoneg |
78 ecmd->supported |= SUPPORTED_1000baseT_Full;
79 ecmd->supported |= alx_get_supported_speeds(hw);
81 ecmd->advertising = ADVERTISED_TP;
82 if (hw->adv_cfg & ADVERTISED_Autoneg)
83 ecmd->advertising |= hw->adv_cfg;
86 ecmd->phy_address = 0;
88 if (hw->adv_cfg & ADVERTISED_Autoneg)
89 ecmd->autoneg = AUTONEG_ENABLE;
91 ecmd->autoneg = AUTONEG_DISABLE;
92 ecmd->transceiver = XCVR_INTERNAL;
94 if (hw->flowctrl & ALX_FC_ANEG && hw->adv_cfg & ADVERTISED_Autoneg) {
95 if (hw->flowctrl & ALX_FC_RX) {
96 ecmd->advertising |= ADVERTISED_Pause;
98 if (!(hw->flowctrl & ALX_FC_TX))
99 ecmd->advertising |= ADVERTISED_Asym_Pause;
100 } else if (hw->flowctrl & ALX_FC_TX) {
101 ecmd->advertising |= ADVERTISED_Asym_Pause;
105 ethtool_cmd_speed_set(ecmd, hw->link_speed);
106 ecmd->duplex = hw->duplex;
111 static int alx_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
113 struct alx_priv *alx = netdev_priv(netdev);
114 struct alx_hw *hw = &alx->hw;
119 if (ecmd->autoneg == AUTONEG_ENABLE) {
120 if (ecmd->advertising & ~alx_get_supported_speeds(hw))
122 adv_cfg = ecmd->advertising | ADVERTISED_Autoneg;
124 adv_cfg = alx_speed_to_ethadv(ethtool_cmd_speed(ecmd),
127 if (!adv_cfg || adv_cfg == ADVERTISED_1000baseT_Full)
131 hw->adv_cfg = adv_cfg;
132 return alx_setup_speed_duplex(hw, adv_cfg, hw->flowctrl);
135 static void alx_get_pauseparam(struct net_device *netdev,
136 struct ethtool_pauseparam *pause)
138 struct alx_priv *alx = netdev_priv(netdev);
139 struct alx_hw *hw = &alx->hw;
141 pause->autoneg = !!(hw->flowctrl & ALX_FC_ANEG &&
142 hw->adv_cfg & ADVERTISED_Autoneg);
143 pause->tx_pause = !!(hw->flowctrl & ALX_FC_TX);
144 pause->rx_pause = !!(hw->flowctrl & ALX_FC_RX);
148 static int alx_set_pauseparam(struct net_device *netdev,
149 struct ethtool_pauseparam *pause)
151 struct alx_priv *alx = netdev_priv(netdev);
152 struct alx_hw *hw = &alx->hw;
154 bool reconfig_phy = false;
166 /* restart auto-neg for auto-mode */
167 if (hw->adv_cfg & ADVERTISED_Autoneg) {
168 if (!((fc ^ hw->flowctrl) & ALX_FC_ANEG))
170 if (fc & hw->flowctrl & ALX_FC_ANEG &&
171 (fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
176 err = alx_setup_speed_duplex(hw, hw->adv_cfg, fc);
181 /* flow control on mac */
182 if ((fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
183 alx_cfg_mac_flowcontrol(hw, fc);
190 static u32 alx_get_msglevel(struct net_device *netdev)
192 struct alx_priv *alx = netdev_priv(netdev);
194 return alx->msg_enable;
197 static void alx_set_msglevel(struct net_device *netdev, u32 data)
199 struct alx_priv *alx = netdev_priv(netdev);
201 alx->msg_enable = data;
204 const struct ethtool_ops alx_ethtool_ops = {
205 .get_settings = alx_get_settings,
206 .set_settings = alx_set_settings,
207 .get_pauseparam = alx_get_pauseparam,
208 .set_pauseparam = alx_set_pauseparam,
209 .get_msglevel = alx_get_msglevel,
210 .set_msglevel = alx_set_msglevel,
211 .get_link = ethtool_op_get_link,