blob: f38848c4f69dae2cfe96190a3d493844b02b43c7 [file] [log] [blame]
huangdaodeb5996f12015-09-17 14:51:50 +08001/*
2 * Copyright (c) 2014-2015 Hisilicon Limited.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
huangdaodeb5996f12015-09-17 14:51:50 +080014#include "hns_enet.h"
15
16#define HNS_PHY_PAGE_MDIX 0
17#define HNS_PHY_PAGE_LED 3
18#define HNS_PHY_PAGE_COPPER 0
19
20#define HNS_PHY_PAGE_REG 22 /* Page Selection Reg. */
21#define HNS_PHY_CSC_REG 16 /* Copper Specific Control Register */
22#define HNS_PHY_CSS_REG 17 /* Copper Specific Status Register */
23#define HNS_LED_FC_REG 16 /* LED Function Control Reg. */
24#define HNS_LED_PC_REG 17 /* LED Polarity Control Reg. */
25
26#define HNS_LED_FORCE_ON 9
27#define HNS_LED_FORCE_OFF 8
28
29#define HNS_CHIP_VERSION 660
30#define HNS_NET_STATS_CNT 26
31
32#define PHY_MDIX_CTRL_S (5)
33#define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
34
35#define PHY_MDIX_STATUS_B (6)
36#define PHY_SPEED_DUP_RESOLVE_B (11)
37
38/**
39 *hns_nic_get_link - get current link status
40 *@net_dev: net_device
41 *retuen 0 - success , negative --fail
42 */
43static u32 hns_nic_get_link(struct net_device *net_dev)
44{
45 struct hns_nic_priv *priv = netdev_priv(net_dev);
46 u32 link_stat = priv->link;
47 struct hnae_handle *h;
48
huangdaodeb5996f12015-09-17 14:51:50 +080049 h = priv->ae_handle;
50
Philippe Reynes262b38c2016-09-20 22:30:11 +020051 if (net_dev->phydev) {
52 if (!genphy_read_status(net_dev->phydev))
53 link_stat = net_dev->phydev->link;
huangdaodeb5996f12015-09-17 14:51:50 +080054 else
55 link_stat = 0;
56 }
57
58 if (h->dev && h->dev->ops && h->dev->ops->get_status)
59 link_stat = link_stat && h->dev->ops->get_status(h);
60 else
61 link_stat = 0;
62
63 return link_stat;
64}
65
66static void hns_get_mdix_mode(struct net_device *net_dev,
Philippe Reynesd270f762016-09-20 22:30:12 +020067 struct ethtool_link_ksettings *cmd)
huangdaodeb5996f12015-09-17 14:51:50 +080068{
69 int mdix_ctrl, mdix, retval, is_resolved;
Philippe Reynes262b38c2016-09-20 22:30:11 +020070 struct phy_device *phy_dev = net_dev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +080071
Andrew Lunne5a03bf2016-01-06 20:11:16 +010072 if (!phy_dev || !phy_dev->mdio.bus) {
Philippe Reynesd270f762016-09-20 22:30:12 +020073 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
74 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
huangdaodeb5996f12015-09-17 14:51:50 +080075 return;
76 }
77
Andrew Lunncd690e42016-01-06 20:11:08 +010078 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_MDIX);
huangdaodeb5996f12015-09-17 14:51:50 +080079
Andrew Lunncd690e42016-01-06 20:11:08 +010080 retval = phy_read(phy_dev, HNS_PHY_CSC_REG);
huangdaodeb5996f12015-09-17 14:51:50 +080081 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
82
Andrew Lunncd690e42016-01-06 20:11:08 +010083 retval = phy_read(phy_dev, HNS_PHY_CSS_REG);
huangdaodeb5996f12015-09-17 14:51:50 +080084 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
85 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
86
Andrew Lunncd690e42016-01-06 20:11:08 +010087 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +080088
89 switch (mdix_ctrl) {
90 case 0x0:
Philippe Reynesd270f762016-09-20 22:30:12 +020091 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI;
huangdaodeb5996f12015-09-17 14:51:50 +080092 break;
93 case 0x1:
Philippe Reynesd270f762016-09-20 22:30:12 +020094 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_X;
huangdaodeb5996f12015-09-17 14:51:50 +080095 break;
96 case 0x3:
Philippe Reynesd270f762016-09-20 22:30:12 +020097 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
huangdaodeb5996f12015-09-17 14:51:50 +080098 break;
99 default:
Philippe Reynesd270f762016-09-20 22:30:12 +0200100 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
huangdaodeb5996f12015-09-17 14:51:50 +0800101 break;
102 }
103
104 if (!is_resolved)
Philippe Reynesd270f762016-09-20 22:30:12 +0200105 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
huangdaodeb5996f12015-09-17 14:51:50 +0800106 else if (mdix)
Philippe Reynesd270f762016-09-20 22:30:12 +0200107 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
huangdaodeb5996f12015-09-17 14:51:50 +0800108 else
Philippe Reynesd270f762016-09-20 22:30:12 +0200109 cmd->base.eth_tp_mdix = ETH_TP_MDI;
huangdaodeb5996f12015-09-17 14:51:50 +0800110}
111
112/**
Philippe Reynesd270f762016-09-20 22:30:12 +0200113 *hns_nic_get_link_ksettings - implement ethtool get link ksettings
huangdaodeb5996f12015-09-17 14:51:50 +0800114 *@net_dev: net_device
Philippe Reynesd270f762016-09-20 22:30:12 +0200115 *@cmd: ethtool_link_ksettings
huangdaodeb5996f12015-09-17 14:51:50 +0800116 *retuen 0 - success , negative --fail
117 */
Philippe Reynesd270f762016-09-20 22:30:12 +0200118static int hns_nic_get_link_ksettings(struct net_device *net_dev,
119 struct ethtool_link_ksettings *cmd)
huangdaodeb5996f12015-09-17 14:51:50 +0800120{
121 struct hns_nic_priv *priv = netdev_priv(net_dev);
122 struct hnae_handle *h;
123 u32 link_stat;
124 int ret;
125 u8 duplex;
126 u16 speed;
Philippe Reynesd270f762016-09-20 22:30:12 +0200127 u32 supported, advertising;
huangdaodeb5996f12015-09-17 14:51:50 +0800128
129 if (!priv || !priv->ae_handle)
130 return -ESRCH;
131
132 h = priv->ae_handle;
133 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
134 return -ESRCH;
135
136 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
137 if (ret < 0) {
138 netdev_err(net_dev, "%s get_info error!\n", __func__);
139 return -EINVAL;
140 }
141
Philippe Reynesd270f762016-09-20 22:30:12 +0200142 ethtool_convert_link_mode_to_legacy_u32(&supported,
143 cmd->link_modes.supported);
144 ethtool_convert_link_mode_to_legacy_u32(&advertising,
145 cmd->link_modes.advertising);
146
huangdaodeb5996f12015-09-17 14:51:50 +0800147 /* When there is no phy, autoneg is off. */
Philippe Reynesd270f762016-09-20 22:30:12 +0200148 cmd->base.autoneg = false;
149 cmd->base.cmd = speed;
150 cmd->base.duplex = duplex;
huangdaodeb5996f12015-09-17 14:51:50 +0800151
Philippe Reynes262b38c2016-09-20 22:30:11 +0200152 if (net_dev->phydev)
Philippe Reynesd270f762016-09-20 22:30:12 +0200153 (void)phy_ethtool_ksettings_get(net_dev->phydev, cmd);
huangdaodeb5996f12015-09-17 14:51:50 +0800154
155 link_stat = hns_nic_get_link(net_dev);
156 if (!link_stat) {
Philippe Reynesd270f762016-09-20 22:30:12 +0200157 cmd->base.speed = (u32)SPEED_UNKNOWN;
158 cmd->base.duplex = DUPLEX_UNKNOWN;
huangdaodeb5996f12015-09-17 14:51:50 +0800159 }
160
Philippe Reynesd270f762016-09-20 22:30:12 +0200161 if (cmd->base.autoneg)
162 advertising |= ADVERTISED_Autoneg;
huangdaodeb5996f12015-09-17 14:51:50 +0800163
Philippe Reynesd270f762016-09-20 22:30:12 +0200164 supported |= h->if_support;
huangdaodeb5996f12015-09-17 14:51:50 +0800165 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
Philippe Reynesd270f762016-09-20 22:30:12 +0200166 supported |= SUPPORTED_TP;
167 advertising |= ADVERTISED_1000baseT_Full;
huangdaodeb5996f12015-09-17 14:51:50 +0800168 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
Philippe Reynesd270f762016-09-20 22:30:12 +0200169 supported |= SUPPORTED_FIBRE;
170 advertising |= ADVERTISED_10000baseKR_Full;
huangdaodeb5996f12015-09-17 14:51:50 +0800171 }
172
Kejian Yan5d2525f2016-07-01 17:34:12 +0800173 switch (h->media_type) {
174 case HNAE_MEDIA_TYPE_FIBER:
Philippe Reynesd270f762016-09-20 22:30:12 +0200175 cmd->base.port = PORT_FIBRE;
Kejian Yan5d2525f2016-07-01 17:34:12 +0800176 break;
177 case HNAE_MEDIA_TYPE_COPPER:
Philippe Reynesd270f762016-09-20 22:30:12 +0200178 cmd->base.port = PORT_TP;
Kejian Yan5d2525f2016-07-01 17:34:12 +0800179 break;
180 case HNAE_MEDIA_TYPE_UNKNOWN:
181 default:
182 break;
huangdaodeb5996f12015-09-17 14:51:50 +0800183 }
184
Kejian Yan5d2525f2016-07-01 17:34:12 +0800185 if (!(AE_IS_VER1(priv->enet_ver) && h->port_type == HNAE_PORT_DEBUG))
Philippe Reynesd270f762016-09-20 22:30:12 +0200186 supported |= SUPPORTED_Pause;
Kejian Yan5d2525f2016-07-01 17:34:12 +0800187
Philippe Reynesd270f762016-09-20 22:30:12 +0200188 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
189 supported);
190 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
191 advertising);
192
193 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22;
huangdaodeb5996f12015-09-17 14:51:50 +0800194 hns_get_mdix_mode(net_dev, cmd);
195
196 return 0;
197}
198
199/**
Philippe Reynesd270f762016-09-20 22:30:12 +0200200 *hns_nic_set_link_settings - implement ethtool set link ksettings
huangdaodeb5996f12015-09-17 14:51:50 +0800201 *@net_dev: net_device
Philippe Reynesd270f762016-09-20 22:30:12 +0200202 *@cmd: ethtool_link_ksettings
huangdaodeb5996f12015-09-17 14:51:50 +0800203 *retuen 0 - success , negative --fail
204 */
Philippe Reynesd270f762016-09-20 22:30:12 +0200205static int hns_nic_set_link_ksettings(struct net_device *net_dev,
206 const struct ethtool_link_ksettings *cmd)
huangdaodeb5996f12015-09-17 14:51:50 +0800207{
208 struct hns_nic_priv *priv = netdev_priv(net_dev);
209 struct hnae_handle *h;
huangdaodeb5996f12015-09-17 14:51:50 +0800210 u32 speed;
huangdaodeb5996f12015-09-17 14:51:50 +0800211
212 if (!netif_running(net_dev))
213 return -ESRCH;
214
215 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
216 !priv->ae_handle->dev->ops)
217 return -ENODEV;
218
219 h = priv->ae_handle;
Philippe Reynesd270f762016-09-20 22:30:12 +0200220 speed = cmd->base.speed;
huangdaodeb5996f12015-09-17 14:51:50 +0800221
222 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
Philippe Reynesd270f762016-09-20 22:30:12 +0200223 if (cmd->base.autoneg == AUTONEG_ENABLE ||
224 speed != SPEED_10000 ||
225 cmd->base.duplex != DUPLEX_FULL)
huangdaodeb5996f12015-09-17 14:51:50 +0800226 return -EINVAL;
227 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
Philippe Reynesd270f762016-09-20 22:30:12 +0200228 if (!net_dev->phydev && cmd->base.autoneg == AUTONEG_ENABLE)
huangdaodeb5996f12015-09-17 14:51:50 +0800229 return -EINVAL;
230
Philippe Reynesd270f762016-09-20 22:30:12 +0200231 if (speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
huangdaodeb5996f12015-09-17 14:51:50 +0800232 return -EINVAL;
Philippe Reynes262b38c2016-09-20 22:30:11 +0200233 if (net_dev->phydev)
Philippe Reynesd270f762016-09-20 22:30:12 +0200234 return phy_ethtool_ksettings_set(net_dev->phydev, cmd);
huangdaodeb5996f12015-09-17 14:51:50 +0800235
Chenny Xu20ddb1d2015-10-16 17:03:19 +0800236 if ((speed != SPEED_10 && speed != SPEED_100 &&
Philippe Reynesd270f762016-09-20 22:30:12 +0200237 speed != SPEED_1000) || (cmd->base.duplex != DUPLEX_HALF &&
238 cmd->base.duplex != DUPLEX_FULL))
huangdaodeb5996f12015-09-17 14:51:50 +0800239 return -EINVAL;
240 } else {
241 netdev_err(net_dev, "Not supported!");
242 return -ENOTSUPP;
243 }
244
Chenny Xu20ddb1d2015-10-16 17:03:19 +0800245 if (h->dev->ops->adjust_link) {
Peng Li5640e042018-08-27 09:59:30 +0800246 netif_carrier_off(net_dev);
Philippe Reynesd270f762016-09-20 22:30:12 +0200247 h->dev->ops->adjust_link(h, (int)speed, cmd->base.duplex);
Peng Li5640e042018-08-27 09:59:30 +0800248 netif_carrier_on(net_dev);
huangdaodeb5996f12015-09-17 14:51:50 +0800249 return 0;
250 }
Chenny Xu20ddb1d2015-10-16 17:03:19 +0800251
huangdaodeb5996f12015-09-17 14:51:50 +0800252 netdev_err(net_dev, "Not supported!");
253 return -ENOTSUPP;
254}
255
256static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
257 "Mac Loopback test",
258 "Serdes Loopback test",
259 "Phy Loopback test"
260};
261
262static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
263{
264#define COPPER_CONTROL_REG 0
Kejian Yancba80bd2016-06-21 11:56:28 +0800265#define PHY_POWER_DOWN BIT(11)
huangdaodeb5996f12015-09-17 14:51:50 +0800266#define PHY_LOOP_BACK BIT(14)
267 u16 val = 0;
268
269 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
270 return -ENOTSUPP;
271
272 if (en) {
273 /* speed : 1000M */
Andrew Lunncd690e42016-01-06 20:11:08 +0100274 phy_write(phy_dev, HNS_PHY_PAGE_REG, 2);
275 phy_write(phy_dev, 21, 0x1046);
Kejian Yancba80bd2016-06-21 11:56:28 +0800276
277 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
huangdaodeb5996f12015-09-17 14:51:50 +0800278 /* Force Master */
Andrew Lunncd690e42016-01-06 20:11:08 +0100279 phy_write(phy_dev, 9, 0x1F00);
Kejian Yancba80bd2016-06-21 11:56:28 +0800280
huangdaodeb5996f12015-09-17 14:51:50 +0800281 /* Soft-reset */
Andrew Lunncd690e42016-01-06 20:11:08 +0100282 phy_write(phy_dev, 0, 0x9140);
huangdaodeb5996f12015-09-17 14:51:50 +0800283 /* If autoneg disabled,two soft-reset operations */
Andrew Lunncd690e42016-01-06 20:11:08 +0100284 phy_write(phy_dev, 0, 0x9140);
Kejian Yancba80bd2016-06-21 11:56:28 +0800285
286 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
huangdaodeb5996f12015-09-17 14:51:50 +0800287
288 /* Default is 0x0400 */
Andrew Lunncd690e42016-01-06 20:11:08 +0100289 phy_write(phy_dev, 1, 0x418);
huangdaodeb5996f12015-09-17 14:51:50 +0800290
291 /* Force 1000M Link, Default is 0x0200 */
Andrew Lunncd690e42016-01-06 20:11:08 +0100292 phy_write(phy_dev, 7, 0x20C);
Kejian Yancba80bd2016-06-21 11:56:28 +0800293 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
huangdaodeb5996f12015-09-17 14:51:50 +0800294
Kejian Yancba80bd2016-06-21 11:56:28 +0800295 /* Enable PHY loop-back */
Andrew Lunncd690e42016-01-06 20:11:08 +0100296 val = phy_read(phy_dev, COPPER_CONTROL_REG);
huangdaodeb5996f12015-09-17 14:51:50 +0800297 val |= PHY_LOOP_BACK;
Kejian Yancba80bd2016-06-21 11:56:28 +0800298 val &= ~PHY_POWER_DOWN;
Andrew Lunncd690e42016-01-06 20:11:08 +0100299 phy_write(phy_dev, COPPER_CONTROL_REG, val);
huangdaodeb5996f12015-09-17 14:51:50 +0800300 } else {
Kejian Yancba80bd2016-06-21 11:56:28 +0800301 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
Andrew Lunncd690e42016-01-06 20:11:08 +0100302 phy_write(phy_dev, 1, 0x400);
303 phy_write(phy_dev, 7, 0x200);
Kejian Yancba80bd2016-06-21 11:56:28 +0800304 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
305 phy_write(phy_dev, 9, 0xF00);
huangdaodeb5996f12015-09-17 14:51:50 +0800306
Andrew Lunncd690e42016-01-06 20:11:08 +0100307 val = phy_read(phy_dev, COPPER_CONTROL_REG);
huangdaodeb5996f12015-09-17 14:51:50 +0800308 val &= ~PHY_LOOP_BACK;
Kejian Yancba80bd2016-06-21 11:56:28 +0800309 val |= PHY_POWER_DOWN;
Andrew Lunncd690e42016-01-06 20:11:08 +0100310 phy_write(phy_dev, COPPER_CONTROL_REG, val);
huangdaodeb5996f12015-09-17 14:51:50 +0800311 }
312 return 0;
313}
314
315static int __lb_setup(struct net_device *ndev,
316 enum hnae_loop loop)
317{
318 int ret = 0;
319 struct hns_nic_priv *priv = netdev_priv(ndev);
Philippe Reynes262b38c2016-09-20 22:30:11 +0200320 struct phy_device *phy_dev = ndev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +0800321 struct hnae_handle *h = priv->ae_handle;
322
323 switch (loop) {
324 case MAC_INTERNALLOOP_PHY:
yankejian68c222a2016-03-05 14:10:42 +0800325 if ((phy_dev) && (!phy_dev->is_c45)) {
huangdaodeb5996f12015-09-17 14:51:50 +0800326 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
yankejian68c222a2016-03-05 14:10:42 +0800327 ret |= h->dev->ops->set_loopback(h, loop, 0x1);
328 }
huangdaodeb5996f12015-09-17 14:51:50 +0800329 break;
330 case MAC_INTERNALLOOP_MAC:
331 if ((h->dev->ops->set_loopback) &&
332 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
333 ret = h->dev->ops->set_loopback(h, loop, 0x1);
334 break;
335 case MAC_INTERNALLOOP_SERDES:
336 if (h->dev->ops->set_loopback)
337 ret = h->dev->ops->set_loopback(h, loop, 0x1);
338 break;
339 case MAC_LOOP_NONE:
340 if ((phy_dev) && (!phy_dev->is_c45))
341 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
342
343 if (h->dev->ops->set_loopback) {
344 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
345 ret |= h->dev->ops->set_loopback(h,
346 MAC_INTERNALLOOP_MAC, 0x0);
347
348 ret |= h->dev->ops->set_loopback(h,
349 MAC_INTERNALLOOP_SERDES, 0x0);
350 }
351 break;
352 default:
353 ret = -EINVAL;
354 break;
355 }
356
Kejian Yanea870bf2016-09-29 18:09:13 +0100357 if (!ret) {
358 if (loop == MAC_LOOP_NONE)
359 h->dev->ops->set_promisc_mode(
360 h, ndev->flags & IFF_PROMISC);
361 else
362 h->dev->ops->set_promisc_mode(h, 1);
363 }
huangdaodeb5996f12015-09-17 14:51:50 +0800364 return ret;
365}
366
367static int __lb_up(struct net_device *ndev,
368 enum hnae_loop loop_mode)
369{
Yonglong Liu439ab882019-05-31 16:59:50 +0800370#define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
huangdaodeb5996f12015-09-17 14:51:50 +0800371 struct hns_nic_priv *priv = netdev_priv(ndev);
372 struct hnae_handle *h = priv->ae_handle;
373 int speed, duplex;
374 int ret;
375
376 hns_nic_net_reset(ndev);
377
huangdaodeb5996f12015-09-17 14:51:50 +0800378 ret = __lb_setup(ndev, loop_mode);
379 if (ret)
380 return ret;
381
Kejian Yancba80bd2016-06-21 11:56:28 +0800382 msleep(200);
huangdaodeb5996f12015-09-17 14:51:50 +0800383
384 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
385 if (ret)
386 return ret;
387
huangdaodeb5996f12015-09-17 14:51:50 +0800388 /* link adjust duplex*/
389 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
390 speed = 1000;
391 else
392 speed = 10000;
393 duplex = 1;
394
395 h->dev->ops->adjust_link(h, speed, duplex);
396
Yonglong Liu439ab882019-05-31 16:59:50 +0800397 /* wait adjust link done and phy ready */
398 msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
399
huangdaodeb5996f12015-09-17 14:51:50 +0800400 return 0;
401}
402
403static void __lb_other_process(struct hns_nic_ring_data *ring_data,
404 struct sk_buff *skb)
405{
406 struct net_device *ndev;
yankejian68c222a2016-03-05 14:10:42 +0800407 struct hns_nic_priv *priv;
huangdaodeb5996f12015-09-17 14:51:50 +0800408 struct hnae_ring *ring;
409 struct netdev_queue *dev_queue;
410 struct sk_buff *new_skb;
411 unsigned int frame_size;
412 int check_ok;
413 u32 i;
414 char buff[33]; /* 32B data and the last character '\0' */
415
416 if (!ring_data) { /* Just for doing create frame*/
yankejian68c222a2016-03-05 14:10:42 +0800417 ndev = skb->dev;
418 priv = netdev_priv(ndev);
419
huangdaodeb5996f12015-09-17 14:51:50 +0800420 frame_size = skb->len;
421 memset(skb->data, 0xFF, frame_size);
yankejian68c222a2016-03-05 14:10:42 +0800422 if ((!AE_IS_VER1(priv->enet_ver)) &&
423 (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
424 memcpy(skb->data, ndev->dev_addr, 6);
425 skb->data[5] += 0x1f;
426 }
427
huangdaodeb5996f12015-09-17 14:51:50 +0800428 frame_size &= ~1ul;
429 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
430 memset(&skb->data[frame_size / 2 + 10], 0xBE,
431 frame_size / 2 - 11);
432 memset(&skb->data[frame_size / 2 + 12], 0xAF,
433 frame_size / 2 - 13);
434 return;
435 }
436
437 ring = ring_data->ring;
438 ndev = ring_data->napi.dev;
439 if (is_tx_ring(ring)) { /* for tx queue reset*/
440 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
441 netdev_tx_reset_queue(dev_queue);
442 return;
443 }
444
445 frame_size = skb->len;
446 frame_size &= ~1ul;
447 /* for mutl buffer*/
448 new_skb = skb_copy(skb, GFP_ATOMIC);
449 dev_kfree_skb_any(skb);
450 skb = new_skb;
451
452 check_ok = 0;
453 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
454 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
455 (*(skb->data + frame_size / 2 + 12) == 0xAF))
456 check_ok = 1;
457 }
458
459 if (check_ok) {
460 ndev->stats.rx_packets++;
461 ndev->stats.rx_bytes += skb->len;
462 } else {
463 ndev->stats.rx_frame_errors++;
464 for (i = 0; i < skb->len; i++) {
465 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
466 "%02x", *(skb->data + i));
467 if ((i % 16 == 15) || (i == skb->len - 1))
468 pr_info("%s\n", buff);
469 }
470 }
471 dev_kfree_skb_any(skb);
472}
473
474static int __lb_clean_rings(struct hns_nic_priv *priv,
475 int ringid0, int ringid1, int budget)
476{
477 int i, ret;
478 struct hns_nic_ring_data *ring_data;
479 struct net_device *ndev = priv->netdev;
480 unsigned long rx_packets = ndev->stats.rx_packets;
481 unsigned long rx_bytes = ndev->stats.rx_bytes;
482 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
483
484 for (i = ringid0; i <= ringid1; i++) {
485 ring_data = &priv->ring_data[i];
486 (void)ring_data->poll_one(ring_data,
487 budget, __lb_other_process);
488 }
489 ret = (int)(ndev->stats.rx_packets - rx_packets);
490 ndev->stats.rx_packets = rx_packets;
491 ndev->stats.rx_bytes = rx_bytes;
492 ndev->stats.rx_frame_errors = rx_frame_errors;
493 return ret;
494}
495
496/**
497 * nic_run_loopback_test - run loopback test
498 * @nic_dev: net device
499 * @loopback_type: loopback type
500 */
501static int __lb_run_test(struct net_device *ndev,
502 enum hnae_loop loop_mode)
503{
504#define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
505#define NIC_LB_TEST_RING_ID 0
506#define NIC_LB_TEST_FRAME_SIZE 128
507/* nic loopback test err */
508#define NIC_LB_TEST_NO_MEM_ERR 1
509#define NIC_LB_TEST_TX_CNT_ERR 2
510#define NIC_LB_TEST_RX_CNT_ERR 3
511#define NIC_LB_TEST_RX_PKG_ERR 4
512 struct hns_nic_priv *priv = netdev_priv(ndev);
513 struct hnae_handle *h = priv->ae_handle;
514 int i, j, lc, good_cnt, ret_val = 0;
515 unsigned int size;
516 netdev_tx_t tx_ret_val;
517 struct sk_buff *skb;
518
519 size = NIC_LB_TEST_FRAME_SIZE;
520 /* allocate test skb */
521 skb = alloc_skb(size, GFP_KERNEL);
522 if (!skb)
523 return NIC_LB_TEST_NO_MEM_ERR;
524
525 /* place data into test skb */
526 (void)skb_put(skb, size);
yankejian68c222a2016-03-05 14:10:42 +0800527 skb->dev = ndev;
huangdaodeb5996f12015-09-17 14:51:50 +0800528 __lb_other_process(NULL, skb);
529 skb->queue_mapping = NIC_LB_TEST_RING_ID;
530
531 lc = 1;
532 for (j = 0; j < lc; j++) {
533 /* reset count of good packets */
534 good_cnt = 0;
535 /* place 64 packets on the transmit queue*/
536 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
537 (void)skb_get(skb);
538
539 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
540 ndev, skb,
541 &tx_ring_data(priv, skb->queue_mapping));
542 if (tx_ret_val == NETDEV_TX_OK)
543 good_cnt++;
544 else
545 break;
546 }
547 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
548 ret_val = NIC_LB_TEST_TX_CNT_ERR;
549 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
550 hns_nic_test_strs[loop_mode], good_cnt,
551 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
552 break;
553 }
554
555 /* allow 100 milliseconds for packets to go from Tx to Rx */
556 msleep(100);
557
558 good_cnt = __lb_clean_rings(priv,
559 h->q_num, h->q_num * 2 - 1,
560 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
561 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
562 ret_val = NIC_LB_TEST_RX_CNT_ERR;
563 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
564 hns_nic_test_strs[loop_mode], good_cnt,
565 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
566 break;
567 }
568 (void)__lb_clean_rings(priv,
569 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
570 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
571 }
572
573 /* free the original skb */
574 kfree_skb(skb);
575
576 return ret_val;
577}
578
579static int __lb_down(struct net_device *ndev)
580{
581 struct hns_nic_priv *priv = netdev_priv(ndev);
582 struct hnae_handle *h = priv->ae_handle;
583 int ret;
584
585 ret = __lb_setup(ndev, MAC_LOOP_NONE);
586 if (ret)
587 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
588 __func__,
589 ret);
590
huangdaodeb5996f12015-09-17 14:51:50 +0800591 if (h->dev->ops->stop)
592 h->dev->ops->stop(h);
593
594 usleep_range(10000, 20000);
595 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
596
597 hns_nic_net_reset(ndev);
598
599 return 0;
600}
601
602/**
603 * hns_nic_self_test - self test
604 * @dev: net device
605 * @eth_test: test cmd
606 * @data: test result
607 */
608static void hns_nic_self_test(struct net_device *ndev,
609 struct ethtool_test *eth_test, u64 *data)
610{
611 struct hns_nic_priv *priv = netdev_priv(ndev);
612 bool if_running = netif_running(ndev);
613#define SELF_TEST_TPYE_NUM 3
614 int st_param[SELF_TEST_TPYE_NUM][2];
615 int i;
616 int test_index = 0;
617
618 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
619 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
620 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
621 st_param[1][1] = 1; /*serdes must exist*/
622 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
Kejian Yan652d39b2016-06-03 10:55:16 +0800623 st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
huangdaodeb5996f12015-09-17 14:51:50 +0800624 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
625
626 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
627 set_bit(NIC_STATE_TESTING, &priv->state);
628
629 if (if_running)
630 (void)dev_close(ndev);
631
632 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
633 if (!st_param[i][1])
634 continue; /* NEXT testing */
635
636 data[test_index] = __lb_up(ndev,
637 (enum hnae_loop)st_param[i][0]);
638 if (!data[test_index]) {
639 data[test_index] = __lb_run_test(
640 ndev, (enum hnae_loop)st_param[i][0]);
641 (void)__lb_down(ndev);
642 }
643
644 if (data[test_index])
645 eth_test->flags |= ETH_TEST_FL_FAILED;
646
647 test_index++;
648 }
649
650 hns_nic_net_reset(priv->netdev);
651
652 clear_bit(NIC_STATE_TESTING, &priv->state);
653
654 if (if_running)
655 (void)dev_open(ndev);
656 }
657 /* Online tests aren't run; pass by default */
658
659 (void)msleep_interruptible(4 * 1000);
660}
661
662/**
663 * hns_nic_get_drvinfo - get net driver info
664 * @dev: net device
665 * @drvinfo: driver info
666 */
667static void hns_nic_get_drvinfo(struct net_device *net_dev,
668 struct ethtool_drvinfo *drvinfo)
669{
670 struct hns_nic_priv *priv = netdev_priv(net_dev);
671
huangdaodeb5996f12015-09-17 14:51:50 +0800672 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
673 sizeof(drvinfo->version));
674 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
675
676 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
677 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
678
679 strncpy(drvinfo->bus_info, priv->dev->bus->name,
680 sizeof(drvinfo->bus_info));
681 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
682
683 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
Salil13ac6952015-12-03 12:17:53 +0000684 drvinfo->eedump_len = 0;
huangdaodeb5996f12015-09-17 14:51:50 +0800685}
686
687/**
688 * hns_get_ringparam - get ring parameter
689 * @dev: net device
690 * @param: ethtool parameter
691 */
692void hns_get_ringparam(struct net_device *net_dev,
693 struct ethtool_ringparam *param)
694{
695 struct hns_nic_priv *priv = netdev_priv(net_dev);
696 struct hnae_ae_ops *ops;
697 struct hnae_queue *queue;
698 u32 uplimit = 0;
699
700 queue = priv->ae_handle->qs[0];
701 ops = priv->ae_handle->dev->ops;
702
703 if (ops->get_ring_bdnum_limit)
704 ops->get_ring_bdnum_limit(queue, &uplimit);
705
706 param->rx_max_pending = uplimit;
707 param->tx_max_pending = uplimit;
708 param->rx_pending = queue->rx_ring.desc_num;
709 param->tx_pending = queue->tx_ring.desc_num;
710}
711
712/**
713 * hns_get_pauseparam - get pause parameter
714 * @dev: net device
715 * @param: pause parameter
716 */
717static void hns_get_pauseparam(struct net_device *net_dev,
718 struct ethtool_pauseparam *param)
719{
720 struct hns_nic_priv *priv = netdev_priv(net_dev);
721 struct hnae_ae_ops *ops;
722
723 ops = priv->ae_handle->dev->ops;
724
725 if (ops->get_pauseparam)
726 ops->get_pauseparam(priv->ae_handle, &param->autoneg,
727 &param->rx_pause, &param->tx_pause);
728}
729
730/**
731 * hns_set_pauseparam - set pause parameter
732 * @dev: net device
733 * @param: pause parameter
734 *
735 * Return 0 on success, negative on failure
736 */
737static int hns_set_pauseparam(struct net_device *net_dev,
738 struct ethtool_pauseparam *param)
739{
740 struct hns_nic_priv *priv = netdev_priv(net_dev);
741 struct hnae_handle *h;
742 struct hnae_ae_ops *ops;
743
huangdaodeb5996f12015-09-17 14:51:50 +0800744 h = priv->ae_handle;
745 ops = h->dev->ops;
746
747 if (!ops->set_pauseparam)
748 return -ESRCH;
749
750 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
751 param->rx_pause, param->tx_pause);
752}
753
754/**
755 * hns_get_coalesce - get coalesce info.
756 * @dev: net device
757 * @ec: coalesce info.
758 *
759 * Return 0 on success, negative on failure.
760 */
761static int hns_get_coalesce(struct net_device *net_dev,
762 struct ethtool_coalesce *ec)
763{
764 struct hns_nic_priv *priv = netdev_priv(net_dev);
765 struct hnae_ae_ops *ops;
766
767 ops = priv->ae_handle->dev->ops;
768
769 ec->use_adaptive_rx_coalesce = 1;
770 ec->use_adaptive_tx_coalesce = 1;
771
772 if ((!ops->get_coalesce_usecs) ||
773 (!ops->get_rx_max_coalesced_frames))
774 return -ESRCH;
775
776 ops->get_coalesce_usecs(priv->ae_handle,
777 &ec->tx_coalesce_usecs,
778 &ec->rx_coalesce_usecs);
779
780 ops->get_rx_max_coalesced_frames(
781 priv->ae_handle,
782 &ec->tx_max_coalesced_frames,
783 &ec->rx_max_coalesced_frames);
784
Daode Huangad59a172016-06-21 11:56:33 +0800785 ops->get_coalesce_range(priv->ae_handle,
786 &ec->tx_max_coalesced_frames_low,
787 &ec->rx_max_coalesced_frames_low,
788 &ec->tx_max_coalesced_frames_high,
789 &ec->rx_max_coalesced_frames_high,
790 &ec->tx_coalesce_usecs_low,
791 &ec->rx_coalesce_usecs_low,
792 &ec->tx_coalesce_usecs_high,
793 &ec->rx_coalesce_usecs_high);
794
huangdaodeb5996f12015-09-17 14:51:50 +0800795 return 0;
796}
797
798/**
799 * hns_set_coalesce - set coalesce info.
800 * @dev: net device
801 * @ec: coalesce info.
802 *
803 * Return 0 on success, negative on failure.
804 */
805static int hns_set_coalesce(struct net_device *net_dev,
806 struct ethtool_coalesce *ec)
807{
808 struct hns_nic_priv *priv = netdev_priv(net_dev);
809 struct hnae_ae_ops *ops;
810 int ret;
811
huangdaodeb5996f12015-09-17 14:51:50 +0800812 ops = priv->ae_handle->dev->ops;
813
814 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
815 return -EINVAL;
816
817 if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
818 return -EINVAL;
819
820 if ((!ops->set_coalesce_usecs) ||
821 (!ops->set_coalesce_frames))
822 return -ESRCH;
823
Lisheng9832ce42016-03-28 18:40:57 +0800824 ret = ops->set_coalesce_usecs(priv->ae_handle,
825 ec->rx_coalesce_usecs);
826 if (ret)
827 return ret;
huangdaodeb5996f12015-09-17 14:51:50 +0800828
829 ret = ops->set_coalesce_frames(
830 priv->ae_handle,
831 ec->rx_max_coalesced_frames);
832
833 return ret;
834}
835
836/**
837 * hns_get_channels - get channel info.
838 * @dev: net device
839 * @ch: channel info.
840 */
841void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
842{
843 struct hns_nic_priv *priv = netdev_priv(net_dev);
844
845 ch->max_rx = priv->ae_handle->q_num;
846 ch->max_tx = priv->ae_handle->q_num;
847
848 ch->rx_count = priv->ae_handle->q_num;
849 ch->tx_count = priv->ae_handle->q_num;
850}
851
852/**
853 * get_ethtool_stats - get detail statistics.
854 * @dev: net device
855 * @stats: statistics info.
856 * @data: statistics data.
857 */
858void hns_get_ethtool_stats(struct net_device *netdev,
859 struct ethtool_stats *stats, u64 *data)
860{
861 u64 *p = data;
862 struct hns_nic_priv *priv = netdev_priv(netdev);
863 struct hnae_handle *h = priv->ae_handle;
864 const struct rtnl_link_stats64 *net_stats;
865 struct rtnl_link_stats64 temp;
866
867 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
868 netdev_err(netdev, "get_stats or update_stats is null!\n");
869 return;
870 }
871
872 h->dev->ops->update_stats(h, &netdev->stats);
873
874 net_stats = dev_get_stats(netdev, &temp);
875
876 /* get netdev statistics */
877 p[0] = net_stats->rx_packets;
878 p[1] = net_stats->tx_packets;
879 p[2] = net_stats->rx_bytes;
880 p[3] = net_stats->tx_bytes;
881 p[4] = net_stats->rx_errors;
882 p[5] = net_stats->tx_errors;
883 p[6] = net_stats->rx_dropped;
884 p[7] = net_stats->tx_dropped;
885 p[8] = net_stats->multicast;
886 p[9] = net_stats->collisions;
887 p[10] = net_stats->rx_over_errors;
888 p[11] = net_stats->rx_crc_errors;
889 p[12] = net_stats->rx_frame_errors;
890 p[13] = net_stats->rx_fifo_errors;
891 p[14] = net_stats->rx_missed_errors;
892 p[15] = net_stats->tx_aborted_errors;
893 p[16] = net_stats->tx_carrier_errors;
894 p[17] = net_stats->tx_fifo_errors;
895 p[18] = net_stats->tx_heartbeat_errors;
896 p[19] = net_stats->rx_length_errors;
897 p[20] = net_stats->tx_window_errors;
898 p[21] = net_stats->rx_compressed;
899 p[22] = net_stats->tx_compressed;
900
901 p[23] = netdev->rx_dropped.counter;
902 p[24] = netdev->tx_dropped.counter;
903
904 p[25] = priv->tx_timeout_count;
905
906 /* get driver statistics */
907 h->dev->ops->get_stats(h, &p[26]);
908}
909
910/**
911 * get_strings: Return a set of strings that describe the requested objects
912 * @dev: net device
913 * @stats: string set ID.
914 * @data: objects data.
915 */
916void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
917{
918 struct hns_nic_priv *priv = netdev_priv(netdev);
919 struct hnae_handle *h = priv->ae_handle;
920 char *buff = (char *)data;
921
922 if (!h->dev->ops->get_strings) {
923 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
924 return;
925 }
926
927 if (stringset == ETH_SS_TEST) {
928 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
929 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
930 ETH_GSTRING_LEN);
931 buff += ETH_GSTRING_LEN;
932 }
933 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
934 ETH_GSTRING_LEN);
935 buff += ETH_GSTRING_LEN;
Philippe Reynes262b38c2016-09-20 22:30:11 +0200936 if ((netdev->phydev) && (!netdev->phydev->is_c45))
huangdaodeb5996f12015-09-17 14:51:50 +0800937 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
938 ETH_GSTRING_LEN);
939
940 } else {
941 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
942 buff = buff + ETH_GSTRING_LEN;
943 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
944 buff = buff + ETH_GSTRING_LEN;
945 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
946 buff = buff + ETH_GSTRING_LEN;
947 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
948 buff = buff + ETH_GSTRING_LEN;
949 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
950 buff = buff + ETH_GSTRING_LEN;
951 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
952 buff = buff + ETH_GSTRING_LEN;
953 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
954 buff = buff + ETH_GSTRING_LEN;
955 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
956 buff = buff + ETH_GSTRING_LEN;
957 snprintf(buff, ETH_GSTRING_LEN, "multicast");
958 buff = buff + ETH_GSTRING_LEN;
959 snprintf(buff, ETH_GSTRING_LEN, "collisions");
960 buff = buff + ETH_GSTRING_LEN;
961 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
962 buff = buff + ETH_GSTRING_LEN;
963 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
964 buff = buff + ETH_GSTRING_LEN;
965 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
966 buff = buff + ETH_GSTRING_LEN;
967 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
968 buff = buff + ETH_GSTRING_LEN;
969 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
970 buff = buff + ETH_GSTRING_LEN;
971 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
972 buff = buff + ETH_GSTRING_LEN;
973 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
974 buff = buff + ETH_GSTRING_LEN;
975 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
976 buff = buff + ETH_GSTRING_LEN;
977 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
978 buff = buff + ETH_GSTRING_LEN;
979 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
980 buff = buff + ETH_GSTRING_LEN;
981 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
982 buff = buff + ETH_GSTRING_LEN;
983 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
984 buff = buff + ETH_GSTRING_LEN;
985 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
986 buff = buff + ETH_GSTRING_LEN;
987 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
988 buff = buff + ETH_GSTRING_LEN;
989 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
990 buff = buff + ETH_GSTRING_LEN;
991
992 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
993 buff = buff + ETH_GSTRING_LEN;
994
995 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
996 }
997}
998
999/**
1000 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
1001 * @dev: net device
1002 * @stringset: string set index, 0: self test string; 1: statistics string.
1003 *
1004 * Return string set count.
1005 */
1006int hns_get_sset_count(struct net_device *netdev, int stringset)
1007{
1008 struct hns_nic_priv *priv = netdev_priv(netdev);
1009 struct hnae_handle *h = priv->ae_handle;
1010 struct hnae_ae_ops *ops = h->dev->ops;
1011
1012 if (!ops->get_sset_count) {
1013 netdev_err(netdev, "get_sset_count is null!\n");
1014 return -EOPNOTSUPP;
1015 }
1016 if (stringset == ETH_SS_TEST) {
1017 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1018
1019 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1020 cnt--;
1021
Philippe Reynes262b38c2016-09-20 22:30:11 +02001022 if ((!netdev->phydev) || (netdev->phydev->is_c45))
huangdaodeb5996f12015-09-17 14:51:50 +08001023 cnt--;
1024
1025 return cnt;
Matthias Brugger83216cb2018-03-15 17:54:20 +01001026 } else if (stringset == ETH_SS_STATS) {
huangdaodeb5996f12015-09-17 14:51:50 +08001027 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
Matthias Brugger83216cb2018-03-15 17:54:20 +01001028 } else {
1029 return -EOPNOTSUPP;
huangdaodeb5996f12015-09-17 14:51:50 +08001030 }
1031}
1032
1033/**
1034 * hns_phy_led_set - set phy LED status.
1035 * @dev: net device
1036 * @value: LED state.
1037 *
1038 * Return 0 on success, negative on failure.
1039 */
1040int hns_phy_led_set(struct net_device *netdev, int value)
1041{
1042 int retval;
Philippe Reynes262b38c2016-09-20 22:30:11 +02001043 struct phy_device *phy_dev = netdev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +08001044
Andrew Lunncd690e42016-01-06 20:11:08 +01001045 retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
Qianqian Xie055a9412016-03-24 19:08:02 +08001046 retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
1047 retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +08001048 if (retval) {
1049 netdev_err(netdev, "mdiobus_write fail !\n");
1050 return retval;
1051 }
1052 return 0;
1053}
1054
1055/**
1056 * nic_set_phys_id - set phy identify LED.
1057 * @dev: net device
1058 * @state: LED state.
1059 *
1060 * Return 0 on success, negative on failure.
1061 */
1062int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1063{
1064 struct hns_nic_priv *priv = netdev_priv(netdev);
1065 struct hnae_handle *h = priv->ae_handle;
Philippe Reynes262b38c2016-09-20 22:30:11 +02001066 struct phy_device *phy_dev = netdev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +08001067 int ret;
1068
1069 if (phy_dev)
1070 switch (state) {
1071 case ETHTOOL_ID_ACTIVE:
Andrew Lunncd690e42016-01-06 20:11:08 +01001072 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1073 HNS_PHY_PAGE_LED);
huangdaodeb5996f12015-09-17 14:51:50 +08001074 if (ret)
1075 return ret;
1076
Andrew Lunncd690e42016-01-06 20:11:08 +01001077 priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
huangdaodeb5996f12015-09-17 14:51:50 +08001078
Andrew Lunncd690e42016-01-06 20:11:08 +01001079 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1080 HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +08001081 if (ret)
1082 return ret;
1083 return 2;
1084 case ETHTOOL_ID_ON:
1085 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1086 if (ret)
1087 return ret;
1088 break;
1089 case ETHTOOL_ID_OFF:
1090 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1091 if (ret)
1092 return ret;
1093 break;
1094 case ETHTOOL_ID_INACTIVE:
Andrew Lunncd690e42016-01-06 20:11:08 +01001095 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1096 HNS_PHY_PAGE_LED);
huangdaodeb5996f12015-09-17 14:51:50 +08001097 if (ret)
1098 return ret;
1099
Andrew Lunncd690e42016-01-06 20:11:08 +01001100 ret = phy_write(phy_dev, HNS_LED_FC_REG,
1101 priv->phy_led_val);
huangdaodeb5996f12015-09-17 14:51:50 +08001102 if (ret)
1103 return ret;
1104
Andrew Lunncd690e42016-01-06 20:11:08 +01001105 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1106 HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +08001107 if (ret)
1108 return ret;
1109 break;
1110 default:
1111 return -EINVAL;
1112 }
1113 else
1114 switch (state) {
1115 case ETHTOOL_ID_ACTIVE:
1116 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1117 case ETHTOOL_ID_ON:
1118 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1119 case ETHTOOL_ID_OFF:
1120 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1121 case ETHTOOL_ID_INACTIVE:
1122 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1123 default:
1124 return -EINVAL;
1125 }
1126
1127 return 0;
1128}
1129
1130/**
1131 * hns_get_regs - get net device register
1132 * @dev: net device
1133 * @cmd: ethtool cmd
1134 * @date: register data
1135 */
1136void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1137 void *data)
1138{
1139 struct hns_nic_priv *priv = netdev_priv(net_dev);
1140 struct hnae_ae_ops *ops;
1141
huangdaodeb5996f12015-09-17 14:51:50 +08001142 ops = priv->ae_handle->dev->ops;
1143
1144 cmd->version = HNS_CHIP_VERSION;
1145 if (!ops->get_regs) {
1146 netdev_err(net_dev, "ops->get_regs is null!\n");
1147 return;
1148 }
1149 ops->get_regs(priv->ae_handle, data);
1150}
1151
1152/**
1153 * nic_get_regs_len - get total register len.
1154 * @dev: net device
1155 *
1156 * Return total register len.
1157 */
1158static int hns_get_regs_len(struct net_device *net_dev)
1159{
1160 u32 reg_num;
1161 struct hns_nic_priv *priv = netdev_priv(net_dev);
1162 struct hnae_ae_ops *ops;
1163
huangdaodeb5996f12015-09-17 14:51:50 +08001164 ops = priv->ae_handle->dev->ops;
1165 if (!ops->get_regs_len) {
1166 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1167 return -EOPNOTSUPP;
1168 }
1169
1170 reg_num = ops->get_regs_len(priv->ae_handle);
1171 if (reg_num > 0)
1172 return reg_num * sizeof(u32);
1173 else
1174 return reg_num; /* error code */
1175}
1176
1177/**
1178 * hns_nic_nway_reset - nway reset
1179 * @dev: net device
1180 *
1181 * Return 0 on success, negative on failure
1182 */
1183static int hns_nic_nway_reset(struct net_device *netdev)
1184{
1185 int ret = 0;
Philippe Reynes262b38c2016-09-20 22:30:11 +02001186 struct phy_device *phy = netdev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +08001187
1188 if (netif_running(netdev)) {
1189 if (phy)
1190 ret = genphy_restart_aneg(phy);
1191 }
1192
1193 return ret;
1194}
1195
Salil6bc0ce72015-12-03 12:17:54 +00001196static u32
1197hns_get_rss_key_size(struct net_device *netdev)
1198{
1199 struct hns_nic_priv *priv = netdev_priv(netdev);
1200 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001201
1202 if (AE_IS_VER1(priv->enet_ver)) {
1203 netdev_err(netdev,
1204 "RSS feature is not supported on this hardware\n");
Kejian Yanbeecfe92016-03-22 16:06:27 +08001205 return 0;
Salil6bc0ce72015-12-03 12:17:54 +00001206 }
1207
1208 ops = priv->ae_handle->dev->ops;
Kejian Yanbeecfe92016-03-22 16:06:27 +08001209 return ops->get_rss_key_size(priv->ae_handle);
Salil6bc0ce72015-12-03 12:17:54 +00001210}
1211
1212static u32
1213hns_get_rss_indir_size(struct net_device *netdev)
1214{
1215 struct hns_nic_priv *priv = netdev_priv(netdev);
1216 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001217
1218 if (AE_IS_VER1(priv->enet_ver)) {
1219 netdev_err(netdev,
1220 "RSS feature is not supported on this hardware\n");
Kejian Yanbeecfe92016-03-22 16:06:27 +08001221 return 0;
Salil6bc0ce72015-12-03 12:17:54 +00001222 }
1223
1224 ops = priv->ae_handle->dev->ops;
Kejian Yanbeecfe92016-03-22 16:06:27 +08001225 return ops->get_rss_indir_size(priv->ae_handle);
Salil6bc0ce72015-12-03 12:17:54 +00001226}
1227
1228static int
1229hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1230{
1231 struct hns_nic_priv *priv = netdev_priv(netdev);
1232 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001233
1234 if (AE_IS_VER1(priv->enet_ver)) {
1235 netdev_err(netdev,
1236 "RSS feature is not supported on this hardware\n");
1237 return -EOPNOTSUPP;
1238 }
1239
1240 ops = priv->ae_handle->dev->ops;
1241
1242 if (!indir)
1243 return 0;
1244
Kejian Yanbeecfe92016-03-22 16:06:27 +08001245 return ops->get_rss(priv->ae_handle, indir, key, hfunc);
Salil6bc0ce72015-12-03 12:17:54 +00001246}
1247
1248static int
1249hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1250 const u8 hfunc)
1251{
1252 struct hns_nic_priv *priv = netdev_priv(netdev);
1253 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001254
1255 if (AE_IS_VER1(priv->enet_ver)) {
1256 netdev_err(netdev,
1257 "RSS feature is not supported on this hardware\n");
1258 return -EOPNOTSUPP;
1259 }
1260
1261 ops = priv->ae_handle->dev->ops;
1262
lipeng9c1e8b42017-04-01 12:03:39 +01001263 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
1264 netdev_err(netdev, "Invalid hfunc!\n");
Salil6bc0ce72015-12-03 12:17:54 +00001265 return -EOPNOTSUPP;
lipeng9c1e8b42017-04-01 12:03:39 +01001266 }
Salil6bc0ce72015-12-03 12:17:54 +00001267
Kejian Yanbeecfe92016-03-22 16:06:27 +08001268 return ops->set_rss(priv->ae_handle, indir, key, hfunc);
Salil6bc0ce72015-12-03 12:17:54 +00001269}
1270
Kejian Yan717dd8072016-03-22 16:06:28 +08001271static int hns_get_rxnfc(struct net_device *netdev,
1272 struct ethtool_rxnfc *cmd,
1273 u32 *rule_locs)
1274{
1275 struct hns_nic_priv *priv = netdev_priv(netdev);
1276
1277 switch (cmd->cmd) {
1278 case ETHTOOL_GRXRINGS:
1279 cmd->data = priv->ae_handle->q_num;
1280 break;
1281 default:
1282 return -EOPNOTSUPP;
1283 }
1284
1285 return 0;
1286}
1287
Julia Lawallbc6f0132016-08-31 09:30:46 +02001288static const struct ethtool_ops hns_ethtool_ops = {
huangdaodeb5996f12015-09-17 14:51:50 +08001289 .get_drvinfo = hns_nic_get_drvinfo,
1290 .get_link = hns_nic_get_link,
huangdaodeb5996f12015-09-17 14:51:50 +08001291 .get_ringparam = hns_get_ringparam,
1292 .get_pauseparam = hns_get_pauseparam,
1293 .set_pauseparam = hns_set_pauseparam,
1294 .get_coalesce = hns_get_coalesce,
1295 .set_coalesce = hns_set_coalesce,
1296 .get_channels = hns_get_channels,
1297 .self_test = hns_nic_self_test,
1298 .get_strings = hns_get_strings,
1299 .get_sset_count = hns_get_sset_count,
1300 .get_ethtool_stats = hns_get_ethtool_stats,
1301 .set_phys_id = hns_set_phys_id,
1302 .get_regs_len = hns_get_regs_len,
1303 .get_regs = hns_get_regs,
1304 .nway_reset = hns_nic_nway_reset,
Salil6bc0ce72015-12-03 12:17:54 +00001305 .get_rxfh_key_size = hns_get_rss_key_size,
1306 .get_rxfh_indir_size = hns_get_rss_indir_size,
1307 .get_rxfh = hns_get_rss,
1308 .set_rxfh = hns_set_rss,
Kejian Yan717dd8072016-03-22 16:06:28 +08001309 .get_rxnfc = hns_get_rxnfc,
Philippe Reynesd270f762016-09-20 22:30:12 +02001310 .get_link_ksettings = hns_nic_get_link_ksettings,
1311 .set_link_ksettings = hns_nic_set_link_ksettings,
huangdaodeb5996f12015-09-17 14:51:50 +08001312};
1313
1314void hns_ethtool_set_ops(struct net_device *ndev)
1315{
1316 ndev->ethtool_ops = &hns_ethtool_ops;
1317}