blob: e95795b3c84160c6dd567c3a725d66f7886fe6fe [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;
Daode Huang4b7cdec2017-04-01 12:03:35 +0100149 cmd->base.speed = speed;
Philippe Reynesd270f762016-09-20 22:30:12 +0200150 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) {
Philippe Reynesd270f762016-09-20 22:30:12 +0200246 h->dev->ops->adjust_link(h, (int)speed, cmd->base.duplex);
huangdaodeb5996f12015-09-17 14:51:50 +0800247 return 0;
248 }
Chenny Xu20ddb1d2015-10-16 17:03:19 +0800249
huangdaodeb5996f12015-09-17 14:51:50 +0800250 netdev_err(net_dev, "Not supported!");
251 return -ENOTSUPP;
252}
253
254static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
255 "Mac Loopback test",
256 "Serdes Loopback test",
257 "Phy Loopback test"
258};
259
260static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
261{
262#define COPPER_CONTROL_REG 0
Kejian Yancba80bd2016-06-21 11:56:28 +0800263#define PHY_POWER_DOWN BIT(11)
huangdaodeb5996f12015-09-17 14:51:50 +0800264#define PHY_LOOP_BACK BIT(14)
265 u16 val = 0;
266
267 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
268 return -ENOTSUPP;
269
270 if (en) {
271 /* speed : 1000M */
Andrew Lunncd690e42016-01-06 20:11:08 +0100272 phy_write(phy_dev, HNS_PHY_PAGE_REG, 2);
273 phy_write(phy_dev, 21, 0x1046);
Kejian Yancba80bd2016-06-21 11:56:28 +0800274
275 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
huangdaodeb5996f12015-09-17 14:51:50 +0800276 /* Force Master */
Andrew Lunncd690e42016-01-06 20:11:08 +0100277 phy_write(phy_dev, 9, 0x1F00);
Kejian Yancba80bd2016-06-21 11:56:28 +0800278
huangdaodeb5996f12015-09-17 14:51:50 +0800279 /* Soft-reset */
Andrew Lunncd690e42016-01-06 20:11:08 +0100280 phy_write(phy_dev, 0, 0x9140);
huangdaodeb5996f12015-09-17 14:51:50 +0800281 /* If autoneg disabled,two soft-reset operations */
Andrew Lunncd690e42016-01-06 20:11:08 +0100282 phy_write(phy_dev, 0, 0x9140);
Kejian Yancba80bd2016-06-21 11:56:28 +0800283
284 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
huangdaodeb5996f12015-09-17 14:51:50 +0800285
286 /* Default is 0x0400 */
Andrew Lunncd690e42016-01-06 20:11:08 +0100287 phy_write(phy_dev, 1, 0x418);
huangdaodeb5996f12015-09-17 14:51:50 +0800288
289 /* Force 1000M Link, Default is 0x0200 */
Andrew Lunncd690e42016-01-06 20:11:08 +0100290 phy_write(phy_dev, 7, 0x20C);
huangdaodeb5996f12015-09-17 14:51:50 +0800291
Lin Yun Sheng7fe5b912017-06-16 17:24:51 +0800292 /* Powerup Fiber */
293 phy_write(phy_dev, HNS_PHY_PAGE_REG, 1);
294 val = phy_read(phy_dev, COPPER_CONTROL_REG);
295 val &= ~PHY_POWER_DOWN;
296 phy_write(phy_dev, COPPER_CONTROL_REG, val);
297
298 /* Enable Phy Loopback */
299 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
Andrew Lunncd690e42016-01-06 20:11:08 +0100300 val = phy_read(phy_dev, COPPER_CONTROL_REG);
huangdaodeb5996f12015-09-17 14:51:50 +0800301 val |= PHY_LOOP_BACK;
Kejian Yancba80bd2016-06-21 11:56:28 +0800302 val &= ~PHY_POWER_DOWN;
Andrew Lunncd690e42016-01-06 20:11:08 +0100303 phy_write(phy_dev, COPPER_CONTROL_REG, val);
huangdaodeb5996f12015-09-17 14:51:50 +0800304 } else {
Kejian Yancba80bd2016-06-21 11:56:28 +0800305 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
Andrew Lunncd690e42016-01-06 20:11:08 +0100306 phy_write(phy_dev, 1, 0x400);
307 phy_write(phy_dev, 7, 0x200);
Lin Yun Sheng7fe5b912017-06-16 17:24:51 +0800308
309 phy_write(phy_dev, HNS_PHY_PAGE_REG, 1);
310 val = phy_read(phy_dev, COPPER_CONTROL_REG);
311 val |= PHY_POWER_DOWN;
312 phy_write(phy_dev, COPPER_CONTROL_REG, val);
313
Kejian Yancba80bd2016-06-21 11:56:28 +0800314 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
315 phy_write(phy_dev, 9, 0xF00);
huangdaodeb5996f12015-09-17 14:51:50 +0800316
Andrew Lunncd690e42016-01-06 20:11:08 +0100317 val = phy_read(phy_dev, COPPER_CONTROL_REG);
huangdaodeb5996f12015-09-17 14:51:50 +0800318 val &= ~PHY_LOOP_BACK;
Kejian Yancba80bd2016-06-21 11:56:28 +0800319 val |= PHY_POWER_DOWN;
Andrew Lunncd690e42016-01-06 20:11:08 +0100320 phy_write(phy_dev, COPPER_CONTROL_REG, val);
huangdaodeb5996f12015-09-17 14:51:50 +0800321 }
322 return 0;
323}
324
325static int __lb_setup(struct net_device *ndev,
326 enum hnae_loop loop)
327{
328 int ret = 0;
329 struct hns_nic_priv *priv = netdev_priv(ndev);
Philippe Reynes262b38c2016-09-20 22:30:11 +0200330 struct phy_device *phy_dev = ndev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +0800331 struct hnae_handle *h = priv->ae_handle;
332
333 switch (loop) {
334 case MAC_INTERNALLOOP_PHY:
yankejian68c222a2016-03-05 14:10:42 +0800335 if ((phy_dev) && (!phy_dev->is_c45)) {
huangdaodeb5996f12015-09-17 14:51:50 +0800336 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
yankejian68c222a2016-03-05 14:10:42 +0800337 ret |= h->dev->ops->set_loopback(h, loop, 0x1);
338 }
huangdaodeb5996f12015-09-17 14:51:50 +0800339 break;
340 case MAC_INTERNALLOOP_MAC:
341 if ((h->dev->ops->set_loopback) &&
342 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
343 ret = h->dev->ops->set_loopback(h, loop, 0x1);
344 break;
345 case MAC_INTERNALLOOP_SERDES:
346 if (h->dev->ops->set_loopback)
347 ret = h->dev->ops->set_loopback(h, loop, 0x1);
348 break;
349 case MAC_LOOP_NONE:
350 if ((phy_dev) && (!phy_dev->is_c45))
351 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
352
353 if (h->dev->ops->set_loopback) {
354 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
355 ret |= h->dev->ops->set_loopback(h,
356 MAC_INTERNALLOOP_MAC, 0x0);
357
358 ret |= h->dev->ops->set_loopback(h,
359 MAC_INTERNALLOOP_SERDES, 0x0);
360 }
361 break;
362 default:
363 ret = -EINVAL;
364 break;
365 }
366
Kejian Yanea870bf2016-09-29 18:09:13 +0100367 if (!ret) {
368 if (loop == MAC_LOOP_NONE)
369 h->dev->ops->set_promisc_mode(
370 h, ndev->flags & IFF_PROMISC);
371 else
372 h->dev->ops->set_promisc_mode(h, 1);
373 }
huangdaodeb5996f12015-09-17 14:51:50 +0800374 return ret;
375}
376
377static int __lb_up(struct net_device *ndev,
378 enum hnae_loop loop_mode)
379{
380 struct hns_nic_priv *priv = netdev_priv(ndev);
381 struct hnae_handle *h = priv->ae_handle;
382 int speed, duplex;
383 int ret;
384
385 hns_nic_net_reset(ndev);
386
huangdaodeb5996f12015-09-17 14:51:50 +0800387 ret = __lb_setup(ndev, loop_mode);
388 if (ret)
389 return ret;
390
Kejian Yancba80bd2016-06-21 11:56:28 +0800391 msleep(200);
huangdaodeb5996f12015-09-17 14:51:50 +0800392
393 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
394 if (ret)
395 return ret;
396
huangdaodeb5996f12015-09-17 14:51:50 +0800397 /* link adjust duplex*/
398 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
399 speed = 1000;
400 else
401 speed = 10000;
402 duplex = 1;
403
404 h->dev->ops->adjust_link(h, speed, duplex);
405
406 return 0;
407}
408
409static void __lb_other_process(struct hns_nic_ring_data *ring_data,
410 struct sk_buff *skb)
411{
412 struct net_device *ndev;
yankejian68c222a2016-03-05 14:10:42 +0800413 struct hns_nic_priv *priv;
huangdaodeb5996f12015-09-17 14:51:50 +0800414 struct hnae_ring *ring;
415 struct netdev_queue *dev_queue;
416 struct sk_buff *new_skb;
417 unsigned int frame_size;
418 int check_ok;
419 u32 i;
420 char buff[33]; /* 32B data and the last character '\0' */
421
422 if (!ring_data) { /* Just for doing create frame*/
yankejian68c222a2016-03-05 14:10:42 +0800423 ndev = skb->dev;
424 priv = netdev_priv(ndev);
425
huangdaodeb5996f12015-09-17 14:51:50 +0800426 frame_size = skb->len;
427 memset(skb->data, 0xFF, frame_size);
yankejian68c222a2016-03-05 14:10:42 +0800428 if ((!AE_IS_VER1(priv->enet_ver)) &&
429 (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
430 memcpy(skb->data, ndev->dev_addr, 6);
431 skb->data[5] += 0x1f;
432 }
433
huangdaodeb5996f12015-09-17 14:51:50 +0800434 frame_size &= ~1ul;
435 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
436 memset(&skb->data[frame_size / 2 + 10], 0xBE,
437 frame_size / 2 - 11);
438 memset(&skb->data[frame_size / 2 + 12], 0xAF,
439 frame_size / 2 - 13);
440 return;
441 }
442
443 ring = ring_data->ring;
444 ndev = ring_data->napi.dev;
445 if (is_tx_ring(ring)) { /* for tx queue reset*/
446 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
447 netdev_tx_reset_queue(dev_queue);
448 return;
449 }
450
451 frame_size = skb->len;
452 frame_size &= ~1ul;
453 /* for mutl buffer*/
454 new_skb = skb_copy(skb, GFP_ATOMIC);
455 dev_kfree_skb_any(skb);
456 skb = new_skb;
457
458 check_ok = 0;
459 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
460 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
461 (*(skb->data + frame_size / 2 + 12) == 0xAF))
462 check_ok = 1;
463 }
464
465 if (check_ok) {
466 ndev->stats.rx_packets++;
467 ndev->stats.rx_bytes += skb->len;
468 } else {
469 ndev->stats.rx_frame_errors++;
470 for (i = 0; i < skb->len; i++) {
471 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
472 "%02x", *(skb->data + i));
473 if ((i % 16 == 15) || (i == skb->len - 1))
474 pr_info("%s\n", buff);
475 }
476 }
477 dev_kfree_skb_any(skb);
478}
479
480static int __lb_clean_rings(struct hns_nic_priv *priv,
481 int ringid0, int ringid1, int budget)
482{
483 int i, ret;
484 struct hns_nic_ring_data *ring_data;
485 struct net_device *ndev = priv->netdev;
486 unsigned long rx_packets = ndev->stats.rx_packets;
487 unsigned long rx_bytes = ndev->stats.rx_bytes;
488 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
489
490 for (i = ringid0; i <= ringid1; i++) {
491 ring_data = &priv->ring_data[i];
492 (void)ring_data->poll_one(ring_data,
493 budget, __lb_other_process);
494 }
495 ret = (int)(ndev->stats.rx_packets - rx_packets);
496 ndev->stats.rx_packets = rx_packets;
497 ndev->stats.rx_bytes = rx_bytes;
498 ndev->stats.rx_frame_errors = rx_frame_errors;
499 return ret;
500}
501
502/**
503 * nic_run_loopback_test - run loopback test
504 * @nic_dev: net device
505 * @loopback_type: loopback type
506 */
507static int __lb_run_test(struct net_device *ndev,
508 enum hnae_loop loop_mode)
509{
510#define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
511#define NIC_LB_TEST_RING_ID 0
512#define NIC_LB_TEST_FRAME_SIZE 128
513/* nic loopback test err */
514#define NIC_LB_TEST_NO_MEM_ERR 1
515#define NIC_LB_TEST_TX_CNT_ERR 2
516#define NIC_LB_TEST_RX_CNT_ERR 3
517#define NIC_LB_TEST_RX_PKG_ERR 4
518 struct hns_nic_priv *priv = netdev_priv(ndev);
519 struct hnae_handle *h = priv->ae_handle;
520 int i, j, lc, good_cnt, ret_val = 0;
521 unsigned int size;
522 netdev_tx_t tx_ret_val;
523 struct sk_buff *skb;
524
525 size = NIC_LB_TEST_FRAME_SIZE;
526 /* allocate test skb */
527 skb = alloc_skb(size, GFP_KERNEL);
528 if (!skb)
529 return NIC_LB_TEST_NO_MEM_ERR;
530
531 /* place data into test skb */
532 (void)skb_put(skb, size);
yankejian68c222a2016-03-05 14:10:42 +0800533 skb->dev = ndev;
huangdaodeb5996f12015-09-17 14:51:50 +0800534 __lb_other_process(NULL, skb);
535 skb->queue_mapping = NIC_LB_TEST_RING_ID;
536
537 lc = 1;
538 for (j = 0; j < lc; j++) {
539 /* reset count of good packets */
540 good_cnt = 0;
541 /* place 64 packets on the transmit queue*/
542 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
543 (void)skb_get(skb);
544
545 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
546 ndev, skb,
547 &tx_ring_data(priv, skb->queue_mapping));
548 if (tx_ret_val == NETDEV_TX_OK)
549 good_cnt++;
550 else
551 break;
552 }
553 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
554 ret_val = NIC_LB_TEST_TX_CNT_ERR;
555 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
556 hns_nic_test_strs[loop_mode], good_cnt,
557 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
558 break;
559 }
560
561 /* allow 100 milliseconds for packets to go from Tx to Rx */
562 msleep(100);
563
564 good_cnt = __lb_clean_rings(priv,
565 h->q_num, h->q_num * 2 - 1,
566 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
567 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
568 ret_val = NIC_LB_TEST_RX_CNT_ERR;
569 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
570 hns_nic_test_strs[loop_mode], good_cnt,
571 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
572 break;
573 }
574 (void)__lb_clean_rings(priv,
575 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
576 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
577 }
578
579 /* free the original skb */
580 kfree_skb(skb);
581
582 return ret_val;
583}
584
585static int __lb_down(struct net_device *ndev)
586{
587 struct hns_nic_priv *priv = netdev_priv(ndev);
588 struct hnae_handle *h = priv->ae_handle;
589 int ret;
590
591 ret = __lb_setup(ndev, MAC_LOOP_NONE);
592 if (ret)
593 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
594 __func__,
595 ret);
596
huangdaodeb5996f12015-09-17 14:51:50 +0800597 if (h->dev->ops->stop)
598 h->dev->ops->stop(h);
599
600 usleep_range(10000, 20000);
601 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
602
603 hns_nic_net_reset(ndev);
604
605 return 0;
606}
607
608/**
609 * hns_nic_self_test - self test
610 * @dev: net device
611 * @eth_test: test cmd
612 * @data: test result
613 */
614static void hns_nic_self_test(struct net_device *ndev,
615 struct ethtool_test *eth_test, u64 *data)
616{
617 struct hns_nic_priv *priv = netdev_priv(ndev);
618 bool if_running = netif_running(ndev);
619#define SELF_TEST_TPYE_NUM 3
620 int st_param[SELF_TEST_TPYE_NUM][2];
621 int i;
622 int test_index = 0;
623
624 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
625 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
626 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
627 st_param[1][1] = 1; /*serdes must exist*/
628 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
Kejian Yan652d39b2016-06-03 10:55:16 +0800629 st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
huangdaodeb5996f12015-09-17 14:51:50 +0800630 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
631
632 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
633 set_bit(NIC_STATE_TESTING, &priv->state);
634
635 if (if_running)
636 (void)dev_close(ndev);
637
638 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
639 if (!st_param[i][1])
640 continue; /* NEXT testing */
641
642 data[test_index] = __lb_up(ndev,
643 (enum hnae_loop)st_param[i][0]);
644 if (!data[test_index]) {
645 data[test_index] = __lb_run_test(
646 ndev, (enum hnae_loop)st_param[i][0]);
647 (void)__lb_down(ndev);
648 }
649
650 if (data[test_index])
651 eth_test->flags |= ETH_TEST_FL_FAILED;
652
653 test_index++;
654 }
655
656 hns_nic_net_reset(priv->netdev);
657
658 clear_bit(NIC_STATE_TESTING, &priv->state);
659
660 if (if_running)
661 (void)dev_open(ndev);
662 }
663 /* Online tests aren't run; pass by default */
664
665 (void)msleep_interruptible(4 * 1000);
666}
667
668/**
669 * hns_nic_get_drvinfo - get net driver info
670 * @dev: net device
671 * @drvinfo: driver info
672 */
673static void hns_nic_get_drvinfo(struct net_device *net_dev,
674 struct ethtool_drvinfo *drvinfo)
675{
676 struct hns_nic_priv *priv = netdev_priv(net_dev);
677
huangdaodeb5996f12015-09-17 14:51:50 +0800678 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
679 sizeof(drvinfo->version));
680 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
681
682 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
683 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
684
685 strncpy(drvinfo->bus_info, priv->dev->bus->name,
686 sizeof(drvinfo->bus_info));
687 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
688
689 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
Salil13ac6952015-12-03 12:17:53 +0000690 drvinfo->eedump_len = 0;
huangdaodeb5996f12015-09-17 14:51:50 +0800691}
692
693/**
694 * hns_get_ringparam - get ring parameter
695 * @dev: net device
696 * @param: ethtool parameter
697 */
698void hns_get_ringparam(struct net_device *net_dev,
699 struct ethtool_ringparam *param)
700{
701 struct hns_nic_priv *priv = netdev_priv(net_dev);
702 struct hnae_ae_ops *ops;
703 struct hnae_queue *queue;
704 u32 uplimit = 0;
705
706 queue = priv->ae_handle->qs[0];
707 ops = priv->ae_handle->dev->ops;
708
709 if (ops->get_ring_bdnum_limit)
710 ops->get_ring_bdnum_limit(queue, &uplimit);
711
712 param->rx_max_pending = uplimit;
713 param->tx_max_pending = uplimit;
714 param->rx_pending = queue->rx_ring.desc_num;
715 param->tx_pending = queue->tx_ring.desc_num;
716}
717
718/**
719 * hns_get_pauseparam - get pause parameter
720 * @dev: net device
721 * @param: pause parameter
722 */
723static void hns_get_pauseparam(struct net_device *net_dev,
724 struct ethtool_pauseparam *param)
725{
726 struct hns_nic_priv *priv = netdev_priv(net_dev);
727 struct hnae_ae_ops *ops;
728
729 ops = priv->ae_handle->dev->ops;
730
731 if (ops->get_pauseparam)
732 ops->get_pauseparam(priv->ae_handle, &param->autoneg,
733 &param->rx_pause, &param->tx_pause);
734}
735
736/**
737 * hns_set_pauseparam - set pause parameter
738 * @dev: net device
739 * @param: pause parameter
740 *
741 * Return 0 on success, negative on failure
742 */
743static int hns_set_pauseparam(struct net_device *net_dev,
744 struct ethtool_pauseparam *param)
745{
746 struct hns_nic_priv *priv = netdev_priv(net_dev);
747 struct hnae_handle *h;
748 struct hnae_ae_ops *ops;
749
huangdaodeb5996f12015-09-17 14:51:50 +0800750 h = priv->ae_handle;
751 ops = h->dev->ops;
752
753 if (!ops->set_pauseparam)
754 return -ESRCH;
755
756 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
757 param->rx_pause, param->tx_pause);
758}
759
760/**
761 * hns_get_coalesce - get coalesce info.
762 * @dev: net device
763 * @ec: coalesce info.
764 *
765 * Return 0 on success, negative on failure.
766 */
767static int hns_get_coalesce(struct net_device *net_dev,
768 struct ethtool_coalesce *ec)
769{
770 struct hns_nic_priv *priv = netdev_priv(net_dev);
771 struct hnae_ae_ops *ops;
772
773 ops = priv->ae_handle->dev->ops;
774
775 ec->use_adaptive_rx_coalesce = 1;
776 ec->use_adaptive_tx_coalesce = 1;
777
778 if ((!ops->get_coalesce_usecs) ||
lipeng820c90c2017-04-01 12:03:47 +0100779 (!ops->get_max_coalesced_frames))
huangdaodeb5996f12015-09-17 14:51:50 +0800780 return -ESRCH;
781
782 ops->get_coalesce_usecs(priv->ae_handle,
783 &ec->tx_coalesce_usecs,
784 &ec->rx_coalesce_usecs);
785
lipeng820c90c2017-04-01 12:03:47 +0100786 ops->get_max_coalesced_frames(
huangdaodeb5996f12015-09-17 14:51:50 +0800787 priv->ae_handle,
788 &ec->tx_max_coalesced_frames,
789 &ec->rx_max_coalesced_frames);
790
Daode Huangad59a172016-06-21 11:56:33 +0800791 ops->get_coalesce_range(priv->ae_handle,
792 &ec->tx_max_coalesced_frames_low,
793 &ec->rx_max_coalesced_frames_low,
794 &ec->tx_max_coalesced_frames_high,
795 &ec->rx_max_coalesced_frames_high,
796 &ec->tx_coalesce_usecs_low,
797 &ec->rx_coalesce_usecs_low,
798 &ec->tx_coalesce_usecs_high,
799 &ec->rx_coalesce_usecs_high);
800
huangdaodeb5996f12015-09-17 14:51:50 +0800801 return 0;
802}
803
804/**
805 * hns_set_coalesce - set coalesce info.
806 * @dev: net device
807 * @ec: coalesce info.
808 *
809 * Return 0 on success, negative on failure.
810 */
811static int hns_set_coalesce(struct net_device *net_dev,
812 struct ethtool_coalesce *ec)
813{
814 struct hns_nic_priv *priv = netdev_priv(net_dev);
815 struct hnae_ae_ops *ops;
lipeng820c90c2017-04-01 12:03:47 +0100816 int rc1, rc2;
huangdaodeb5996f12015-09-17 14:51:50 +0800817
huangdaodeb5996f12015-09-17 14:51:50 +0800818 ops = priv->ae_handle->dev->ops;
819
820 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
821 return -EINVAL;
822
huangdaodeb5996f12015-09-17 14:51:50 +0800823 if ((!ops->set_coalesce_usecs) ||
824 (!ops->set_coalesce_frames))
825 return -ESRCH;
826
lipeng820c90c2017-04-01 12:03:47 +0100827 rc1 = ops->set_coalesce_usecs(priv->ae_handle,
Lisheng9832ce42016-03-28 18:40:57 +0800828 ec->rx_coalesce_usecs);
huangdaodeb5996f12015-09-17 14:51:50 +0800829
lipeng820c90c2017-04-01 12:03:47 +0100830 rc2 = ops->set_coalesce_frames(priv->ae_handle,
831 ec->tx_max_coalesced_frames,
832 ec->rx_max_coalesced_frames);
huangdaodeb5996f12015-09-17 14:51:50 +0800833
lipeng820c90c2017-04-01 12:03:47 +0100834 if (rc1 || rc2)
835 return -EINVAL;
836
837 return 0;
huangdaodeb5996f12015-09-17 14:51:50 +0800838}
839
840/**
841 * hns_get_channels - get channel info.
842 * @dev: net device
843 * @ch: channel info.
844 */
845void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
846{
847 struct hns_nic_priv *priv = netdev_priv(net_dev);
848
849 ch->max_rx = priv->ae_handle->q_num;
850 ch->max_tx = priv->ae_handle->q_num;
851
852 ch->rx_count = priv->ae_handle->q_num;
853 ch->tx_count = priv->ae_handle->q_num;
854}
855
856/**
857 * get_ethtool_stats - get detail statistics.
858 * @dev: net device
859 * @stats: statistics info.
860 * @data: statistics data.
861 */
862void hns_get_ethtool_stats(struct net_device *netdev,
863 struct ethtool_stats *stats, u64 *data)
864{
865 u64 *p = data;
866 struct hns_nic_priv *priv = netdev_priv(netdev);
867 struct hnae_handle *h = priv->ae_handle;
868 const struct rtnl_link_stats64 *net_stats;
869 struct rtnl_link_stats64 temp;
870
871 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
872 netdev_err(netdev, "get_stats or update_stats is null!\n");
873 return;
874 }
875
876 h->dev->ops->update_stats(h, &netdev->stats);
877
878 net_stats = dev_get_stats(netdev, &temp);
879
880 /* get netdev statistics */
881 p[0] = net_stats->rx_packets;
882 p[1] = net_stats->tx_packets;
883 p[2] = net_stats->rx_bytes;
884 p[3] = net_stats->tx_bytes;
885 p[4] = net_stats->rx_errors;
886 p[5] = net_stats->tx_errors;
887 p[6] = net_stats->rx_dropped;
888 p[7] = net_stats->tx_dropped;
889 p[8] = net_stats->multicast;
890 p[9] = net_stats->collisions;
891 p[10] = net_stats->rx_over_errors;
892 p[11] = net_stats->rx_crc_errors;
893 p[12] = net_stats->rx_frame_errors;
894 p[13] = net_stats->rx_fifo_errors;
895 p[14] = net_stats->rx_missed_errors;
896 p[15] = net_stats->tx_aborted_errors;
897 p[16] = net_stats->tx_carrier_errors;
898 p[17] = net_stats->tx_fifo_errors;
899 p[18] = net_stats->tx_heartbeat_errors;
900 p[19] = net_stats->rx_length_errors;
901 p[20] = net_stats->tx_window_errors;
902 p[21] = net_stats->rx_compressed;
903 p[22] = net_stats->tx_compressed;
904
905 p[23] = netdev->rx_dropped.counter;
906 p[24] = netdev->tx_dropped.counter;
907
908 p[25] = priv->tx_timeout_count;
909
910 /* get driver statistics */
911 h->dev->ops->get_stats(h, &p[26]);
912}
913
914/**
915 * get_strings: Return a set of strings that describe the requested objects
916 * @dev: net device
917 * @stats: string set ID.
918 * @data: objects data.
919 */
920void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
921{
922 struct hns_nic_priv *priv = netdev_priv(netdev);
923 struct hnae_handle *h = priv->ae_handle;
924 char *buff = (char *)data;
925
926 if (!h->dev->ops->get_strings) {
927 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
928 return;
929 }
930
931 if (stringset == ETH_SS_TEST) {
932 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
933 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
934 ETH_GSTRING_LEN);
935 buff += ETH_GSTRING_LEN;
936 }
937 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
938 ETH_GSTRING_LEN);
939 buff += ETH_GSTRING_LEN;
Philippe Reynes262b38c2016-09-20 22:30:11 +0200940 if ((netdev->phydev) && (!netdev->phydev->is_c45))
huangdaodeb5996f12015-09-17 14:51:50 +0800941 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
942 ETH_GSTRING_LEN);
943
944 } else {
945 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
946 buff = buff + ETH_GSTRING_LEN;
947 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
948 buff = buff + ETH_GSTRING_LEN;
949 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
950 buff = buff + ETH_GSTRING_LEN;
951 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
952 buff = buff + ETH_GSTRING_LEN;
953 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
954 buff = buff + ETH_GSTRING_LEN;
955 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
956 buff = buff + ETH_GSTRING_LEN;
957 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
958 buff = buff + ETH_GSTRING_LEN;
959 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
960 buff = buff + ETH_GSTRING_LEN;
961 snprintf(buff, ETH_GSTRING_LEN, "multicast");
962 buff = buff + ETH_GSTRING_LEN;
963 snprintf(buff, ETH_GSTRING_LEN, "collisions");
964 buff = buff + ETH_GSTRING_LEN;
965 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
966 buff = buff + ETH_GSTRING_LEN;
967 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
968 buff = buff + ETH_GSTRING_LEN;
969 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
970 buff = buff + ETH_GSTRING_LEN;
971 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
972 buff = buff + ETH_GSTRING_LEN;
973 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
974 buff = buff + ETH_GSTRING_LEN;
975 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
976 buff = buff + ETH_GSTRING_LEN;
977 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
978 buff = buff + ETH_GSTRING_LEN;
979 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
980 buff = buff + ETH_GSTRING_LEN;
981 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
982 buff = buff + ETH_GSTRING_LEN;
983 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
984 buff = buff + ETH_GSTRING_LEN;
985 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
986 buff = buff + ETH_GSTRING_LEN;
987 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
988 buff = buff + ETH_GSTRING_LEN;
989 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
990 buff = buff + ETH_GSTRING_LEN;
991 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
992 buff = buff + ETH_GSTRING_LEN;
993 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
994 buff = buff + ETH_GSTRING_LEN;
995
996 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
997 buff = buff + ETH_GSTRING_LEN;
998
999 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
1000 }
1001}
1002
1003/**
1004 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
1005 * @dev: net device
1006 * @stringset: string set index, 0: self test string; 1: statistics string.
1007 *
1008 * Return string set count.
1009 */
1010int hns_get_sset_count(struct net_device *netdev, int stringset)
1011{
1012 struct hns_nic_priv *priv = netdev_priv(netdev);
1013 struct hnae_handle *h = priv->ae_handle;
1014 struct hnae_ae_ops *ops = h->dev->ops;
1015
1016 if (!ops->get_sset_count) {
1017 netdev_err(netdev, "get_sset_count is null!\n");
1018 return -EOPNOTSUPP;
1019 }
1020 if (stringset == ETH_SS_TEST) {
1021 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1022
1023 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1024 cnt--;
1025
Philippe Reynes262b38c2016-09-20 22:30:11 +02001026 if ((!netdev->phydev) || (netdev->phydev->is_c45))
huangdaodeb5996f12015-09-17 14:51:50 +08001027 cnt--;
1028
1029 return cnt;
1030 } else {
1031 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1032 }
1033}
1034
1035/**
1036 * hns_phy_led_set - set phy LED status.
1037 * @dev: net device
1038 * @value: LED state.
1039 *
1040 * Return 0 on success, negative on failure.
1041 */
1042int hns_phy_led_set(struct net_device *netdev, int value)
1043{
1044 int retval;
Philippe Reynes262b38c2016-09-20 22:30:11 +02001045 struct phy_device *phy_dev = netdev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +08001046
Andrew Lunncd690e42016-01-06 20:11:08 +01001047 retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
Qianqian Xie055a9412016-03-24 19:08:02 +08001048 retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
1049 retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +08001050 if (retval) {
1051 netdev_err(netdev, "mdiobus_write fail !\n");
1052 return retval;
1053 }
1054 return 0;
1055}
1056
1057/**
1058 * nic_set_phys_id - set phy identify LED.
1059 * @dev: net device
1060 * @state: LED state.
1061 *
1062 * Return 0 on success, negative on failure.
1063 */
1064int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1065{
1066 struct hns_nic_priv *priv = netdev_priv(netdev);
1067 struct hnae_handle *h = priv->ae_handle;
Philippe Reynes262b38c2016-09-20 22:30:11 +02001068 struct phy_device *phy_dev = netdev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +08001069 int ret;
1070
1071 if (phy_dev)
1072 switch (state) {
1073 case ETHTOOL_ID_ACTIVE:
Andrew Lunncd690e42016-01-06 20:11:08 +01001074 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1075 HNS_PHY_PAGE_LED);
huangdaodeb5996f12015-09-17 14:51:50 +08001076 if (ret)
1077 return ret;
1078
Andrew Lunncd690e42016-01-06 20:11:08 +01001079 priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
huangdaodeb5996f12015-09-17 14:51:50 +08001080
Andrew Lunncd690e42016-01-06 20:11:08 +01001081 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1082 HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +08001083 if (ret)
1084 return ret;
1085 return 2;
1086 case ETHTOOL_ID_ON:
1087 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1088 if (ret)
1089 return ret;
1090 break;
1091 case ETHTOOL_ID_OFF:
1092 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1093 if (ret)
1094 return ret;
1095 break;
1096 case ETHTOOL_ID_INACTIVE:
Andrew Lunncd690e42016-01-06 20:11:08 +01001097 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1098 HNS_PHY_PAGE_LED);
huangdaodeb5996f12015-09-17 14:51:50 +08001099 if (ret)
1100 return ret;
1101
Andrew Lunncd690e42016-01-06 20:11:08 +01001102 ret = phy_write(phy_dev, HNS_LED_FC_REG,
1103 priv->phy_led_val);
huangdaodeb5996f12015-09-17 14:51:50 +08001104 if (ret)
1105 return ret;
1106
Andrew Lunncd690e42016-01-06 20:11:08 +01001107 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1108 HNS_PHY_PAGE_COPPER);
huangdaodeb5996f12015-09-17 14:51:50 +08001109 if (ret)
1110 return ret;
1111 break;
1112 default:
1113 return -EINVAL;
1114 }
1115 else
1116 switch (state) {
1117 case ETHTOOL_ID_ACTIVE:
1118 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1119 case ETHTOOL_ID_ON:
1120 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1121 case ETHTOOL_ID_OFF:
1122 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1123 case ETHTOOL_ID_INACTIVE:
1124 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1125 default:
1126 return -EINVAL;
1127 }
1128
1129 return 0;
1130}
1131
1132/**
1133 * hns_get_regs - get net device register
1134 * @dev: net device
1135 * @cmd: ethtool cmd
1136 * @date: register data
1137 */
1138void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1139 void *data)
1140{
1141 struct hns_nic_priv *priv = netdev_priv(net_dev);
1142 struct hnae_ae_ops *ops;
1143
huangdaodeb5996f12015-09-17 14:51:50 +08001144 ops = priv->ae_handle->dev->ops;
1145
1146 cmd->version = HNS_CHIP_VERSION;
1147 if (!ops->get_regs) {
1148 netdev_err(net_dev, "ops->get_regs is null!\n");
1149 return;
1150 }
1151 ops->get_regs(priv->ae_handle, data);
1152}
1153
1154/**
1155 * nic_get_regs_len - get total register len.
1156 * @dev: net device
1157 *
1158 * Return total register len.
1159 */
1160static int hns_get_regs_len(struct net_device *net_dev)
1161{
1162 u32 reg_num;
1163 struct hns_nic_priv *priv = netdev_priv(net_dev);
1164 struct hnae_ae_ops *ops;
1165
huangdaodeb5996f12015-09-17 14:51:50 +08001166 ops = priv->ae_handle->dev->ops;
1167 if (!ops->get_regs_len) {
1168 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1169 return -EOPNOTSUPP;
1170 }
1171
1172 reg_num = ops->get_regs_len(priv->ae_handle);
1173 if (reg_num > 0)
1174 return reg_num * sizeof(u32);
1175 else
1176 return reg_num; /* error code */
1177}
1178
1179/**
1180 * hns_nic_nway_reset - nway reset
1181 * @dev: net device
1182 *
1183 * Return 0 on success, negative on failure
1184 */
1185static int hns_nic_nway_reset(struct net_device *netdev)
1186{
1187 int ret = 0;
Philippe Reynes262b38c2016-09-20 22:30:11 +02001188 struct phy_device *phy = netdev->phydev;
huangdaodeb5996f12015-09-17 14:51:50 +08001189
1190 if (netif_running(netdev)) {
Daode Huangda2ef1e2016-11-09 18:13:49 +00001191 /* if autoneg is disabled, don't restart auto-negotiation */
1192 if (phy && phy->autoneg == AUTONEG_ENABLE)
huangdaodeb5996f12015-09-17 14:51:50 +08001193 ret = genphy_restart_aneg(phy);
1194 }
1195
1196 return ret;
1197}
1198
Salil6bc0ce72015-12-03 12:17:54 +00001199static u32
1200hns_get_rss_key_size(struct net_device *netdev)
1201{
1202 struct hns_nic_priv *priv = netdev_priv(netdev);
1203 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001204
1205 if (AE_IS_VER1(priv->enet_ver)) {
1206 netdev_err(netdev,
1207 "RSS feature is not supported on this hardware\n");
Kejian Yanbeecfe92016-03-22 16:06:27 +08001208 return 0;
Salil6bc0ce72015-12-03 12:17:54 +00001209 }
1210
1211 ops = priv->ae_handle->dev->ops;
Kejian Yanbeecfe92016-03-22 16:06:27 +08001212 return ops->get_rss_key_size(priv->ae_handle);
Salil6bc0ce72015-12-03 12:17:54 +00001213}
1214
1215static u32
1216hns_get_rss_indir_size(struct net_device *netdev)
1217{
1218 struct hns_nic_priv *priv = netdev_priv(netdev);
1219 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001220
1221 if (AE_IS_VER1(priv->enet_ver)) {
1222 netdev_err(netdev,
1223 "RSS feature is not supported on this hardware\n");
Kejian Yanbeecfe92016-03-22 16:06:27 +08001224 return 0;
Salil6bc0ce72015-12-03 12:17:54 +00001225 }
1226
1227 ops = priv->ae_handle->dev->ops;
Kejian Yanbeecfe92016-03-22 16:06:27 +08001228 return ops->get_rss_indir_size(priv->ae_handle);
Salil6bc0ce72015-12-03 12:17:54 +00001229}
1230
1231static int
1232hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1233{
1234 struct hns_nic_priv *priv = netdev_priv(netdev);
1235 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001236
1237 if (AE_IS_VER1(priv->enet_ver)) {
1238 netdev_err(netdev,
1239 "RSS feature is not supported on this hardware\n");
1240 return -EOPNOTSUPP;
1241 }
1242
1243 ops = priv->ae_handle->dev->ops;
1244
1245 if (!indir)
1246 return 0;
1247
Kejian Yanbeecfe92016-03-22 16:06:27 +08001248 return ops->get_rss(priv->ae_handle, indir, key, hfunc);
Salil6bc0ce72015-12-03 12:17:54 +00001249}
1250
1251static int
1252hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1253 const u8 hfunc)
1254{
1255 struct hns_nic_priv *priv = netdev_priv(netdev);
1256 struct hnae_ae_ops *ops;
Salil6bc0ce72015-12-03 12:17:54 +00001257
1258 if (AE_IS_VER1(priv->enet_ver)) {
1259 netdev_err(netdev,
1260 "RSS feature is not supported on this hardware\n");
1261 return -EOPNOTSUPP;
1262 }
1263
1264 ops = priv->ae_handle->dev->ops;
1265
lipeng64ec10d2017-04-01 12:03:39 +01001266 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
1267 netdev_err(netdev, "Invalid hfunc!\n");
Salil6bc0ce72015-12-03 12:17:54 +00001268 return -EOPNOTSUPP;
lipeng64ec10d2017-04-01 12:03:39 +01001269 }
Salil6bc0ce72015-12-03 12:17:54 +00001270
Kejian Yanbeecfe92016-03-22 16:06:27 +08001271 return ops->set_rss(priv->ae_handle, indir, key, hfunc);
Salil6bc0ce72015-12-03 12:17:54 +00001272}
1273
Kejian Yan717dd8072016-03-22 16:06:28 +08001274static int hns_get_rxnfc(struct net_device *netdev,
1275 struct ethtool_rxnfc *cmd,
1276 u32 *rule_locs)
1277{
1278 struct hns_nic_priv *priv = netdev_priv(netdev);
1279
1280 switch (cmd->cmd) {
1281 case ETHTOOL_GRXRINGS:
1282 cmd->data = priv->ae_handle->q_num;
1283 break;
1284 default:
1285 return -EOPNOTSUPP;
1286 }
1287
1288 return 0;
1289}
1290
Julia Lawallbc6f0132016-08-31 09:30:46 +02001291static const struct ethtool_ops hns_ethtool_ops = {
huangdaodeb5996f12015-09-17 14:51:50 +08001292 .get_drvinfo = hns_nic_get_drvinfo,
1293 .get_link = hns_nic_get_link,
huangdaodeb5996f12015-09-17 14:51:50 +08001294 .get_ringparam = hns_get_ringparam,
1295 .get_pauseparam = hns_get_pauseparam,
1296 .set_pauseparam = hns_set_pauseparam,
1297 .get_coalesce = hns_get_coalesce,
1298 .set_coalesce = hns_set_coalesce,
1299 .get_channels = hns_get_channels,
1300 .self_test = hns_nic_self_test,
1301 .get_strings = hns_get_strings,
1302 .get_sset_count = hns_get_sset_count,
1303 .get_ethtool_stats = hns_get_ethtool_stats,
1304 .set_phys_id = hns_set_phys_id,
1305 .get_regs_len = hns_get_regs_len,
1306 .get_regs = hns_get_regs,
1307 .nway_reset = hns_nic_nway_reset,
Salil6bc0ce72015-12-03 12:17:54 +00001308 .get_rxfh_key_size = hns_get_rss_key_size,
1309 .get_rxfh_indir_size = hns_get_rss_indir_size,
1310 .get_rxfh = hns_get_rss,
1311 .set_rxfh = hns_set_rss,
Kejian Yan717dd8072016-03-22 16:06:28 +08001312 .get_rxnfc = hns_get_rxnfc,
Philippe Reynesd270f762016-09-20 22:30:12 +02001313 .get_link_ksettings = hns_nic_get_link_ksettings,
1314 .set_link_ksettings = hns_nic_set_link_ksettings,
huangdaodeb5996f12015-09-17 14:51:50 +08001315};
1316
1317void hns_ethtool_set_ops(struct net_device *ndev)
1318{
1319 ndev->ethtool_ops = &hns_ethtool_ops;
1320}