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 /* The order of these strings must match the order of the fields in
53 static const char alx_gstrings_stats
[][ETH_GSTRING_LEN
] = {
64 "rx_64B_or_less_packets",
65 "rx_65B_to_127B_packets",
66 "rx_128B_to_255B_packets",
67 "rx_256B_to_511B_packets",
68 "rx_512B_to_1023B_packets",
69 "rx_1024B_to_1518B_packets",
70 "rx_1519B_to_mtu_packets",
71 "rx_oversize_packets",
72 "rx_rxf_ov_drop_packets",
73 "rx_rrd_ov_drop_packets",
82 "tx_exc_defer_packets",
86 "tx_64B_or_less_packets",
87 "tx_65B_to_127B_packets",
88 "tx_128B_to_255B_packets",
89 "tx_256B_to_511B_packets",
90 "tx_512B_to_1023B_packets",
91 "tx_1024B_to_1518B_packets",
92 "tx_1519B_to_mtu_packets",
93 "tx_single_collision",
94 "tx_multiple_collisions",
106 #define ALX_NUM_STATS ARRAY_SIZE(alx_gstrings_stats)
109 static u32
alx_get_supported_speeds(struct alx_hw
*hw
)
111 u32 supported
= SUPPORTED_10baseT_Half
|
112 SUPPORTED_10baseT_Full
|
113 SUPPORTED_100baseT_Half
|
114 SUPPORTED_100baseT_Full
;
117 supported
|= SUPPORTED_1000baseT_Full
;
119 BUILD_BUG_ON(SUPPORTED_10baseT_Half
!= ADVERTISED_10baseT_Half
);
120 BUILD_BUG_ON(SUPPORTED_10baseT_Full
!= ADVERTISED_10baseT_Full
);
121 BUILD_BUG_ON(SUPPORTED_100baseT_Half
!= ADVERTISED_100baseT_Half
);
122 BUILD_BUG_ON(SUPPORTED_100baseT_Full
!= ADVERTISED_100baseT_Full
);
123 BUILD_BUG_ON(SUPPORTED_1000baseT_Full
!= ADVERTISED_1000baseT_Full
);
128 static int alx_get_link_ksettings(struct net_device
*netdev
,
129 struct ethtool_link_ksettings
*cmd
)
131 struct alx_priv
*alx
= netdev_priv(netdev
);
132 struct alx_hw
*hw
= &alx
->hw
;
133 u32 supported
, advertising
;
135 supported
= SUPPORTED_Autoneg
|
138 SUPPORTED_Asym_Pause
;
140 supported
|= SUPPORTED_1000baseT_Full
;
141 supported
|= alx_get_supported_speeds(hw
);
143 advertising
= ADVERTISED_TP
;
144 if (hw
->adv_cfg
& ADVERTISED_Autoneg
)
145 advertising
|= hw
->adv_cfg
;
147 cmd
->base
.port
= PORT_TP
;
148 cmd
->base
.phy_address
= 0;
150 if (hw
->adv_cfg
& ADVERTISED_Autoneg
)
151 cmd
->base
.autoneg
= AUTONEG_ENABLE
;
153 cmd
->base
.autoneg
= AUTONEG_DISABLE
;
155 if (hw
->flowctrl
& ALX_FC_ANEG
&& hw
->adv_cfg
& ADVERTISED_Autoneg
) {
156 if (hw
->flowctrl
& ALX_FC_RX
) {
157 advertising
|= ADVERTISED_Pause
;
159 if (!(hw
->flowctrl
& ALX_FC_TX
))
160 advertising
|= ADVERTISED_Asym_Pause
;
161 } else if (hw
->flowctrl
& ALX_FC_TX
) {
162 advertising
|= ADVERTISED_Asym_Pause
;
166 cmd
->base
.speed
= hw
->link_speed
;
167 cmd
->base
.duplex
= hw
->duplex
;
169 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.supported
,
171 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.advertising
,
177 static int alx_set_link_ksettings(struct net_device
*netdev
,
178 const struct ethtool_link_ksettings
*cmd
)
180 struct alx_priv
*alx
= netdev_priv(netdev
);
181 struct alx_hw
*hw
= &alx
->hw
;
187 ethtool_convert_link_mode_to_legacy_u32(&advertising
,
188 cmd
->link_modes
.advertising
);
190 if (cmd
->base
.autoneg
== AUTONEG_ENABLE
) {
191 if (advertising
& ~alx_get_supported_speeds(hw
))
193 adv_cfg
= advertising
| ADVERTISED_Autoneg
;
195 adv_cfg
= alx_speed_to_ethadv(cmd
->base
.speed
,
198 if (!adv_cfg
|| adv_cfg
== ADVERTISED_1000baseT_Full
)
202 hw
->adv_cfg
= adv_cfg
;
203 return alx_setup_speed_duplex(hw
, adv_cfg
, hw
->flowctrl
);
206 static void alx_get_pauseparam(struct net_device
*netdev
,
207 struct ethtool_pauseparam
*pause
)
209 struct alx_priv
*alx
= netdev_priv(netdev
);
210 struct alx_hw
*hw
= &alx
->hw
;
212 pause
->autoneg
= !!(hw
->flowctrl
& ALX_FC_ANEG
&&
213 hw
->adv_cfg
& ADVERTISED_Autoneg
);
214 pause
->tx_pause
= !!(hw
->flowctrl
& ALX_FC_TX
);
215 pause
->rx_pause
= !!(hw
->flowctrl
& ALX_FC_RX
);
219 static int alx_set_pauseparam(struct net_device
*netdev
,
220 struct ethtool_pauseparam
*pause
)
222 struct alx_priv
*alx
= netdev_priv(netdev
);
223 struct alx_hw
*hw
= &alx
->hw
;
225 bool reconfig_phy
= false;
237 /* restart auto-neg for auto-mode */
238 if (hw
->adv_cfg
& ADVERTISED_Autoneg
) {
239 if (!((fc
^ hw
->flowctrl
) & ALX_FC_ANEG
))
241 if (fc
& hw
->flowctrl
& ALX_FC_ANEG
&&
242 (fc
^ hw
->flowctrl
) & (ALX_FC_RX
| ALX_FC_TX
))
247 err
= alx_setup_speed_duplex(hw
, hw
->adv_cfg
, fc
);
252 /* flow control on mac */
253 if ((fc
^ hw
->flowctrl
) & (ALX_FC_RX
| ALX_FC_TX
))
254 alx_cfg_mac_flowcontrol(hw
, fc
);
261 static u32
alx_get_msglevel(struct net_device
*netdev
)
263 struct alx_priv
*alx
= netdev_priv(netdev
);
265 return alx
->msg_enable
;
268 static void alx_set_msglevel(struct net_device
*netdev
, u32 data
)
270 struct alx_priv
*alx
= netdev_priv(netdev
);
272 alx
->msg_enable
= data
;
275 static void alx_get_ethtool_stats(struct net_device
*netdev
,
276 struct ethtool_stats
*estats
, u64
*data
)
278 struct alx_priv
*alx
= netdev_priv(netdev
);
279 struct alx_hw
*hw
= &alx
->hw
;
281 spin_lock(&alx
->stats_lock
);
283 alx_update_hw_stats(hw
);
284 BUILD_BUG_ON(sizeof(hw
->stats
) - offsetof(struct alx_hw_stats
, rx_ok
) <
285 ALX_NUM_STATS
* sizeof(u64
));
286 memcpy(data
, &hw
->stats
.rx_ok
, ALX_NUM_STATS
* sizeof(u64
));
288 spin_unlock(&alx
->stats_lock
);
291 static void alx_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*buf
)
295 memcpy(buf
, &alx_gstrings_stats
, sizeof(alx_gstrings_stats
));
303 static int alx_get_sset_count(struct net_device
*netdev
, int sset
)
307 return ALX_NUM_STATS
;
313 const struct ethtool_ops alx_ethtool_ops
= {
314 .get_pauseparam
= alx_get_pauseparam
,
315 .set_pauseparam
= alx_set_pauseparam
,
316 .get_msglevel
= alx_get_msglevel
,
317 .set_msglevel
= alx_set_msglevel
,
318 .get_link
= ethtool_op_get_link
,
319 .get_strings
= alx_get_strings
,
320 .get_sset_count
= alx_get_sset_count
,
321 .get_ethtool_stats
= alx_get_ethtool_stats
,
322 .get_link_ksettings
= alx_get_link_ksettings
,
323 .set_link_ksettings
= alx_set_link_ksettings
,