0001
0002
0003
0004
0005
0006 #include <linux/etherdevice.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/module.h>
0009 #include <linux/platform_device.h>
0010 #include "hns_enet.h"
0011
0012 #define HNS_PHY_PAGE_MDIX 0
0013 #define HNS_PHY_PAGE_LED 3
0014 #define HNS_PHY_PAGE_COPPER 0
0015
0016 #define HNS_PHY_PAGE_REG 22
0017 #define HNS_PHY_CSC_REG 16
0018 #define HNS_PHY_CSS_REG 17
0019 #define HNS_LED_FC_REG 16
0020
0021 #define HNS_LED_FORCE_ON 9
0022 #define HNS_LED_FORCE_OFF 8
0023
0024 #define HNS_CHIP_VERSION 660
0025 #define HNS_NET_STATS_CNT 26
0026
0027 #define PHY_MDIX_CTRL_S (5)
0028 #define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
0029
0030 #define PHY_MDIX_STATUS_B (6)
0031 #define PHY_SPEED_DUP_RESOLVE_B (11)
0032
0033
0034
0035
0036
0037
0038 static u32 hns_nic_get_link(struct net_device *net_dev)
0039 {
0040 struct hns_nic_priv *priv = netdev_priv(net_dev);
0041 u32 link_stat = priv->link;
0042 struct hnae_handle *h;
0043
0044 h = priv->ae_handle;
0045
0046 if (net_dev->phydev) {
0047 if (!genphy_read_status(net_dev->phydev))
0048 link_stat = net_dev->phydev->link;
0049 else
0050 link_stat = 0;
0051 }
0052
0053 if (h->dev && h->dev->ops && h->dev->ops->get_status)
0054 link_stat = link_stat && h->dev->ops->get_status(h);
0055 else
0056 link_stat = 0;
0057
0058 return link_stat;
0059 }
0060
0061 static void hns_get_mdix_mode(struct net_device *net_dev,
0062 struct ethtool_link_ksettings *cmd)
0063 {
0064 int mdix_ctrl, mdix, retval, is_resolved;
0065 struct phy_device *phy_dev = net_dev->phydev;
0066
0067 if (!phy_dev || !phy_dev->mdio.bus) {
0068 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
0069 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
0070 return;
0071 }
0072
0073 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_MDIX);
0074
0075 retval = phy_read(phy_dev, HNS_PHY_CSC_REG);
0076 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
0077
0078 retval = phy_read(phy_dev, HNS_PHY_CSS_REG);
0079 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
0080 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
0081
0082 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
0083
0084 switch (mdix_ctrl) {
0085 case 0x0:
0086 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI;
0087 break;
0088 case 0x1:
0089 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_X;
0090 break;
0091 case 0x3:
0092 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
0093 break;
0094 default:
0095 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
0096 break;
0097 }
0098
0099 if (!is_resolved)
0100 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
0101 else if (mdix)
0102 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
0103 else
0104 cmd->base.eth_tp_mdix = ETH_TP_MDI;
0105 }
0106
0107
0108
0109
0110
0111
0112
0113 static int hns_nic_get_link_ksettings(struct net_device *net_dev,
0114 struct ethtool_link_ksettings *cmd)
0115 {
0116 struct hns_nic_priv *priv = netdev_priv(net_dev);
0117 struct hnae_handle *h;
0118 u32 link_stat;
0119 int ret;
0120 u8 duplex;
0121 u16 speed;
0122 u32 supported, advertising;
0123
0124 if (!priv || !priv->ae_handle)
0125 return -ESRCH;
0126
0127 h = priv->ae_handle;
0128 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
0129 return -ESRCH;
0130
0131 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
0132 if (ret < 0) {
0133 netdev_err(net_dev, "%s get_info error!\n", __func__);
0134 return -EINVAL;
0135 }
0136
0137 ethtool_convert_link_mode_to_legacy_u32(&supported,
0138 cmd->link_modes.supported);
0139 ethtool_convert_link_mode_to_legacy_u32(&advertising,
0140 cmd->link_modes.advertising);
0141
0142
0143 cmd->base.autoneg = false;
0144 cmd->base.speed = speed;
0145 cmd->base.duplex = duplex;
0146
0147 if (net_dev->phydev)
0148 phy_ethtool_ksettings_get(net_dev->phydev, cmd);
0149
0150 link_stat = hns_nic_get_link(net_dev);
0151 if (!link_stat) {
0152 cmd->base.speed = (u32)SPEED_UNKNOWN;
0153 cmd->base.duplex = DUPLEX_UNKNOWN;
0154 }
0155
0156 if (cmd->base.autoneg)
0157 advertising |= ADVERTISED_Autoneg;
0158
0159 supported |= h->if_support;
0160 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
0161 supported |= SUPPORTED_TP;
0162 advertising |= ADVERTISED_1000baseT_Full;
0163 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
0164 supported |= SUPPORTED_FIBRE;
0165 advertising |= ADVERTISED_10000baseKR_Full;
0166 }
0167
0168 switch (h->media_type) {
0169 case HNAE_MEDIA_TYPE_FIBER:
0170 cmd->base.port = PORT_FIBRE;
0171 break;
0172 case HNAE_MEDIA_TYPE_COPPER:
0173 cmd->base.port = PORT_TP;
0174 break;
0175 case HNAE_MEDIA_TYPE_UNKNOWN:
0176 default:
0177 break;
0178 }
0179
0180 if (!(AE_IS_VER1(priv->enet_ver) && h->port_type == HNAE_PORT_DEBUG))
0181 supported |= SUPPORTED_Pause;
0182
0183 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
0184 supported);
0185 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
0186 advertising);
0187
0188 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22;
0189 hns_get_mdix_mode(net_dev, cmd);
0190
0191 return 0;
0192 }
0193
0194
0195
0196
0197
0198
0199
0200 static int hns_nic_set_link_ksettings(struct net_device *net_dev,
0201 const struct ethtool_link_ksettings *cmd)
0202 {
0203 struct hns_nic_priv *priv = netdev_priv(net_dev);
0204 struct hnae_handle *h;
0205 u32 speed;
0206
0207 if (!netif_running(net_dev))
0208 return -ESRCH;
0209
0210 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
0211 !priv->ae_handle->dev->ops)
0212 return -ENODEV;
0213
0214 h = priv->ae_handle;
0215 speed = cmd->base.speed;
0216
0217 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
0218 if (cmd->base.autoneg == AUTONEG_ENABLE ||
0219 speed != SPEED_10000 ||
0220 cmd->base.duplex != DUPLEX_FULL)
0221 return -EINVAL;
0222 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
0223 if (!net_dev->phydev && cmd->base.autoneg == AUTONEG_ENABLE)
0224 return -EINVAL;
0225
0226 if (speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
0227 return -EINVAL;
0228 if (net_dev->phydev)
0229 return phy_ethtool_ksettings_set(net_dev->phydev, cmd);
0230
0231 if ((speed != SPEED_10 && speed != SPEED_100 &&
0232 speed != SPEED_1000) || (cmd->base.duplex != DUPLEX_HALF &&
0233 cmd->base.duplex != DUPLEX_FULL))
0234 return -EINVAL;
0235 } else {
0236 netdev_err(net_dev, "Not supported!");
0237 return -ENOTSUPP;
0238 }
0239
0240 if (h->dev->ops->adjust_link) {
0241 netif_carrier_off(net_dev);
0242 h->dev->ops->adjust_link(h, (int)speed, cmd->base.duplex);
0243 netif_carrier_on(net_dev);
0244 return 0;
0245 }
0246
0247 netdev_err(net_dev, "Not supported!");
0248 return -ENOTSUPP;
0249 }
0250
0251 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
0252 "Mac Loopback test",
0253 "Serdes Loopback test",
0254 "Phy Loopback test"
0255 };
0256
0257 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
0258 {
0259 int err;
0260
0261 if (en) {
0262
0263
0264
0265 err = phy_resume(phy_dev);
0266 if (err)
0267 goto out;
0268
0269 err = phy_loopback(phy_dev, true);
0270 } else {
0271 err = phy_loopback(phy_dev, false);
0272 if (err)
0273 goto out;
0274
0275 err = phy_suspend(phy_dev);
0276 }
0277
0278 out:
0279 return err;
0280 }
0281
0282 static int __lb_setup(struct net_device *ndev,
0283 enum hnae_loop loop)
0284 {
0285 int ret = 0;
0286 struct hns_nic_priv *priv = netdev_priv(ndev);
0287 struct phy_device *phy_dev = ndev->phydev;
0288 struct hnae_handle *h = priv->ae_handle;
0289
0290 switch (loop) {
0291 case MAC_INTERNALLOOP_PHY:
0292 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
0293 if (!ret)
0294 ret = h->dev->ops->set_loopback(h, loop, 0x1);
0295 break;
0296 case MAC_INTERNALLOOP_MAC:
0297 if ((h->dev->ops->set_loopback) &&
0298 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
0299 ret = h->dev->ops->set_loopback(h, loop, 0x1);
0300 break;
0301 case MAC_INTERNALLOOP_SERDES:
0302 if (h->dev->ops->set_loopback)
0303 ret = h->dev->ops->set_loopback(h, loop, 0x1);
0304 break;
0305 case MAC_LOOP_PHY_NONE:
0306 ret = hns_nic_config_phy_loopback(phy_dev, 0x0);
0307 fallthrough;
0308 case MAC_LOOP_NONE:
0309 if (!ret && h->dev->ops->set_loopback) {
0310 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
0311 ret = h->dev->ops->set_loopback(h,
0312 MAC_INTERNALLOOP_MAC, 0x0);
0313
0314 if (!ret)
0315 ret = h->dev->ops->set_loopback(h,
0316 MAC_INTERNALLOOP_SERDES, 0x0);
0317 }
0318 break;
0319 default:
0320 ret = -EINVAL;
0321 break;
0322 }
0323
0324 if (!ret) {
0325 if (loop == MAC_LOOP_NONE)
0326 h->dev->ops->set_promisc_mode(
0327 h, ndev->flags & IFF_PROMISC);
0328 else
0329 h->dev->ops->set_promisc_mode(h, 1);
0330 }
0331 return ret;
0332 }
0333
0334 static int __lb_up(struct net_device *ndev,
0335 enum hnae_loop loop_mode)
0336 {
0337 #define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
0338 struct hns_nic_priv *priv = netdev_priv(ndev);
0339 struct hnae_handle *h = priv->ae_handle;
0340 int speed, duplex;
0341 int ret;
0342
0343 hns_nic_net_reset(ndev);
0344
0345 ret = __lb_setup(ndev, loop_mode);
0346 if (ret)
0347 return ret;
0348
0349 msleep(200);
0350
0351 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
0352 if (ret)
0353 return ret;
0354
0355
0356 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
0357 speed = 1000;
0358 else
0359 speed = 10000;
0360 duplex = 1;
0361
0362 h->dev->ops->adjust_link(h, speed, duplex);
0363
0364
0365 msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
0366
0367 return 0;
0368 }
0369
0370 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
0371 struct sk_buff *skb)
0372 {
0373 struct net_device *ndev;
0374 struct hns_nic_priv *priv;
0375 struct hnae_ring *ring;
0376 struct netdev_queue *dev_queue;
0377 struct sk_buff *new_skb;
0378 unsigned int frame_size;
0379 int check_ok;
0380 u32 i;
0381 char buff[33];
0382
0383 if (!ring_data) {
0384 ndev = skb->dev;
0385 priv = netdev_priv(ndev);
0386
0387 frame_size = skb->len;
0388 memset(skb->data, 0xFF, frame_size);
0389 if ((!AE_IS_VER1(priv->enet_ver)) &&
0390 (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
0391 memcpy(skb->data, ndev->dev_addr, 6);
0392 skb->data[5] += 0x1f;
0393 }
0394
0395 frame_size &= ~1ul;
0396 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
0397 memset(&skb->data[frame_size / 2 + 10], 0xBE,
0398 frame_size / 2 - 11);
0399 memset(&skb->data[frame_size / 2 + 12], 0xAF,
0400 frame_size / 2 - 13);
0401 return;
0402 }
0403
0404 ring = ring_data->ring;
0405 ndev = ring_data->napi.dev;
0406 if (is_tx_ring(ring)) {
0407 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
0408 netdev_tx_reset_queue(dev_queue);
0409 return;
0410 }
0411
0412 frame_size = skb->len;
0413 frame_size &= ~1ul;
0414
0415 new_skb = skb_copy(skb, GFP_ATOMIC);
0416 dev_kfree_skb_any(skb);
0417 if (!new_skb) {
0418 netdev_err(ndev, "skb alloc failed\n");
0419 return;
0420 }
0421 skb = new_skb;
0422
0423 check_ok = 0;
0424 if (*(skb->data + 10) == 0xFF) {
0425 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
0426 (*(skb->data + frame_size / 2 + 12) == 0xAF))
0427 check_ok = 1;
0428 }
0429
0430 if (check_ok) {
0431 ndev->stats.rx_packets++;
0432 ndev->stats.rx_bytes += skb->len;
0433 } else {
0434 ndev->stats.rx_frame_errors++;
0435 for (i = 0; i < skb->len; i++) {
0436 snprintf(buff + i % 16 * 2, 3,
0437 "%02x", *(skb->data + i));
0438 if ((i % 16 == 15) || (i == skb->len - 1))
0439 pr_info("%s\n", buff);
0440 }
0441 }
0442 dev_kfree_skb_any(skb);
0443 }
0444
0445 static int __lb_clean_rings(struct hns_nic_priv *priv,
0446 int ringid0, int ringid1, int budget)
0447 {
0448 int i, ret;
0449 struct hns_nic_ring_data *ring_data;
0450 struct net_device *ndev = priv->netdev;
0451 unsigned long rx_packets = ndev->stats.rx_packets;
0452 unsigned long rx_bytes = ndev->stats.rx_bytes;
0453 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
0454
0455 for (i = ringid0; i <= ringid1; i++) {
0456 ring_data = &priv->ring_data[i];
0457 (void)ring_data->poll_one(ring_data,
0458 budget, __lb_other_process);
0459 }
0460 ret = (int)(ndev->stats.rx_packets - rx_packets);
0461 ndev->stats.rx_packets = rx_packets;
0462 ndev->stats.rx_bytes = rx_bytes;
0463 ndev->stats.rx_frame_errors = rx_frame_errors;
0464 return ret;
0465 }
0466
0467
0468
0469
0470
0471
0472 static int __lb_run_test(struct net_device *ndev,
0473 enum hnae_loop loop_mode)
0474 {
0475 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
0476 #define NIC_LB_TEST_RING_ID 0
0477 #define NIC_LB_TEST_FRAME_SIZE 128
0478
0479 #define NIC_LB_TEST_NO_MEM_ERR 1
0480 #define NIC_LB_TEST_TX_CNT_ERR 2
0481 #define NIC_LB_TEST_RX_CNT_ERR 3
0482
0483 struct hns_nic_priv *priv = netdev_priv(ndev);
0484 struct hnae_handle *h = priv->ae_handle;
0485 int i, j, lc, good_cnt, ret_val = 0;
0486 unsigned int size;
0487 netdev_tx_t tx_ret_val;
0488 struct sk_buff *skb;
0489
0490 size = NIC_LB_TEST_FRAME_SIZE;
0491
0492 skb = alloc_skb(size, GFP_KERNEL);
0493 if (!skb)
0494 return NIC_LB_TEST_NO_MEM_ERR;
0495
0496
0497 (void)skb_put(skb, size);
0498 skb->dev = ndev;
0499 __lb_other_process(NULL, skb);
0500 skb->queue_mapping = NIC_LB_TEST_RING_ID;
0501
0502 lc = 1;
0503 for (j = 0; j < lc; j++) {
0504
0505 good_cnt = 0;
0506
0507 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
0508 (void)skb_get(skb);
0509
0510 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
0511 ndev, skb,
0512 &tx_ring_data(priv, skb->queue_mapping));
0513 if (tx_ret_val == NETDEV_TX_OK)
0514 good_cnt++;
0515 else
0516 break;
0517 }
0518 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
0519 ret_val = NIC_LB_TEST_TX_CNT_ERR;
0520 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
0521 hns_nic_test_strs[loop_mode], good_cnt,
0522 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
0523 break;
0524 }
0525
0526
0527 msleep(100);
0528
0529 good_cnt = __lb_clean_rings(priv,
0530 h->q_num, h->q_num * 2 - 1,
0531 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
0532 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
0533 ret_val = NIC_LB_TEST_RX_CNT_ERR;
0534 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
0535 hns_nic_test_strs[loop_mode], good_cnt,
0536 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
0537 break;
0538 }
0539 (void)__lb_clean_rings(priv,
0540 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
0541 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
0542 }
0543
0544
0545 kfree_skb(skb);
0546
0547 return ret_val;
0548 }
0549
0550 static int __lb_down(struct net_device *ndev, enum hnae_loop loop)
0551 {
0552 struct hns_nic_priv *priv = netdev_priv(ndev);
0553 struct hnae_handle *h = priv->ae_handle;
0554 int ret;
0555
0556 if (loop == MAC_INTERNALLOOP_PHY)
0557 ret = __lb_setup(ndev, MAC_LOOP_PHY_NONE);
0558 else
0559 ret = __lb_setup(ndev, MAC_LOOP_NONE);
0560 if (ret)
0561 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
0562 __func__,
0563 ret);
0564
0565 if (h->dev->ops->stop)
0566 h->dev->ops->stop(h);
0567
0568 usleep_range(10000, 20000);
0569 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
0570
0571 hns_nic_net_reset(ndev);
0572
0573 return 0;
0574 }
0575
0576
0577
0578
0579
0580
0581
0582 static void hns_nic_self_test(struct net_device *ndev,
0583 struct ethtool_test *eth_test, u64 *data)
0584 {
0585 struct hns_nic_priv *priv = netdev_priv(ndev);
0586 bool if_running = netif_running(ndev);
0587 #define SELF_TEST_TPYE_NUM 3
0588 int st_param[SELF_TEST_TPYE_NUM][2];
0589 int i;
0590 int test_index = 0;
0591
0592 st_param[0][0] = MAC_INTERNALLOOP_MAC;
0593 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
0594 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
0595 st_param[1][1] = 1;
0596 st_param[2][0] = MAC_INTERNALLOOP_PHY;
0597 st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
0598 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
0599
0600 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
0601 set_bit(NIC_STATE_TESTING, &priv->state);
0602
0603 if (if_running)
0604 dev_close(ndev);
0605
0606 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
0607 if (!st_param[i][1])
0608 continue;
0609
0610 data[test_index] = __lb_up(ndev,
0611 (enum hnae_loop)st_param[i][0]);
0612 if (!data[test_index]) {
0613 data[test_index] = __lb_run_test(
0614 ndev, (enum hnae_loop)st_param[i][0]);
0615 (void)__lb_down(ndev,
0616 (enum hnae_loop)st_param[i][0]);
0617 }
0618
0619 if (data[test_index])
0620 eth_test->flags |= ETH_TEST_FL_FAILED;
0621
0622 test_index++;
0623 }
0624
0625 hns_nic_net_reset(priv->netdev);
0626
0627 clear_bit(NIC_STATE_TESTING, &priv->state);
0628
0629 if (if_running)
0630 (void)dev_open(ndev, NULL);
0631 }
0632
0633
0634 (void)msleep_interruptible(4 * 1000);
0635 }
0636
0637
0638
0639
0640
0641
0642 static void hns_nic_get_drvinfo(struct net_device *net_dev,
0643 struct ethtool_drvinfo *drvinfo)
0644 {
0645 struct hns_nic_priv *priv = netdev_priv(net_dev);
0646
0647 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
0648 sizeof(drvinfo->version));
0649 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
0650
0651 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
0652 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
0653
0654 strncpy(drvinfo->bus_info, priv->dev->bus->name,
0655 sizeof(drvinfo->bus_info));
0656 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
0657
0658 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
0659 drvinfo->eedump_len = 0;
0660 }
0661
0662
0663
0664
0665
0666
0667
0668
0669 static void hns_get_ringparam(struct net_device *net_dev,
0670 struct ethtool_ringparam *param,
0671 struct kernel_ethtool_ringparam *kernel_param,
0672 struct netlink_ext_ack *extack)
0673 {
0674 struct hns_nic_priv *priv = netdev_priv(net_dev);
0675 struct hnae_ae_ops *ops;
0676 struct hnae_queue *queue;
0677 u32 uplimit = 0;
0678
0679 queue = priv->ae_handle->qs[0];
0680 ops = priv->ae_handle->dev->ops;
0681
0682 if (ops->get_ring_bdnum_limit)
0683 ops->get_ring_bdnum_limit(queue, &uplimit);
0684
0685 param->rx_max_pending = uplimit;
0686 param->tx_max_pending = uplimit;
0687 param->rx_pending = queue->rx_ring.desc_num;
0688 param->tx_pending = queue->tx_ring.desc_num;
0689 }
0690
0691
0692
0693
0694
0695
0696 static void hns_get_pauseparam(struct net_device *net_dev,
0697 struct ethtool_pauseparam *param)
0698 {
0699 struct hns_nic_priv *priv = netdev_priv(net_dev);
0700 struct hnae_ae_ops *ops;
0701
0702 ops = priv->ae_handle->dev->ops;
0703
0704 if (ops->get_pauseparam)
0705 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
0706 ¶m->rx_pause, ¶m->tx_pause);
0707 }
0708
0709
0710
0711
0712
0713
0714
0715
0716 static int hns_set_pauseparam(struct net_device *net_dev,
0717 struct ethtool_pauseparam *param)
0718 {
0719 struct hns_nic_priv *priv = netdev_priv(net_dev);
0720 struct hnae_handle *h;
0721 struct hnae_ae_ops *ops;
0722
0723 h = priv->ae_handle;
0724 ops = h->dev->ops;
0725
0726 if (!ops->set_pauseparam)
0727 return -ESRCH;
0728
0729 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
0730 param->rx_pause, param->tx_pause);
0731 }
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742 static int hns_get_coalesce(struct net_device *net_dev,
0743 struct ethtool_coalesce *ec,
0744 struct kernel_ethtool_coalesce *kernel_coal,
0745 struct netlink_ext_ack *extack)
0746 {
0747 struct hns_nic_priv *priv = netdev_priv(net_dev);
0748 struct hnae_ae_ops *ops;
0749
0750 ops = priv->ae_handle->dev->ops;
0751
0752 ec->use_adaptive_rx_coalesce = priv->ae_handle->coal_adapt_en;
0753 ec->use_adaptive_tx_coalesce = priv->ae_handle->coal_adapt_en;
0754
0755 if ((!ops->get_coalesce_usecs) ||
0756 (!ops->get_max_coalesced_frames))
0757 return -ESRCH;
0758
0759 ops->get_coalesce_usecs(priv->ae_handle,
0760 &ec->tx_coalesce_usecs,
0761 &ec->rx_coalesce_usecs);
0762
0763 ops->get_max_coalesced_frames(
0764 priv->ae_handle,
0765 &ec->tx_max_coalesced_frames,
0766 &ec->rx_max_coalesced_frames);
0767
0768 ops->get_coalesce_range(priv->ae_handle,
0769 &ec->tx_max_coalesced_frames_low,
0770 &ec->rx_max_coalesced_frames_low,
0771 &ec->tx_max_coalesced_frames_high,
0772 &ec->rx_max_coalesced_frames_high,
0773 &ec->tx_coalesce_usecs_low,
0774 &ec->rx_coalesce_usecs_low,
0775 &ec->tx_coalesce_usecs_high,
0776 &ec->rx_coalesce_usecs_high);
0777
0778 return 0;
0779 }
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790 static int hns_set_coalesce(struct net_device *net_dev,
0791 struct ethtool_coalesce *ec,
0792 struct kernel_ethtool_coalesce *kernel_coal,
0793 struct netlink_ext_ack *extack)
0794 {
0795 struct hns_nic_priv *priv = netdev_priv(net_dev);
0796 struct hnae_ae_ops *ops;
0797 int rc1, rc2;
0798
0799 ops = priv->ae_handle->dev->ops;
0800
0801 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
0802 return -EINVAL;
0803
0804 if ((!ops->set_coalesce_usecs) ||
0805 (!ops->set_coalesce_frames))
0806 return -ESRCH;
0807
0808 if (ec->use_adaptive_rx_coalesce != priv->ae_handle->coal_adapt_en)
0809 priv->ae_handle->coal_adapt_en = ec->use_adaptive_rx_coalesce;
0810
0811 rc1 = ops->set_coalesce_usecs(priv->ae_handle,
0812 ec->rx_coalesce_usecs);
0813
0814 rc2 = ops->set_coalesce_frames(priv->ae_handle,
0815 ec->tx_max_coalesced_frames,
0816 ec->rx_max_coalesced_frames);
0817
0818 if (rc1 || rc2)
0819 return -EINVAL;
0820
0821 return 0;
0822 }
0823
0824
0825
0826
0827
0828
0829 static void
0830 hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
0831 {
0832 struct hns_nic_priv *priv = netdev_priv(net_dev);
0833
0834 ch->max_rx = priv->ae_handle->q_num;
0835 ch->max_tx = priv->ae_handle->q_num;
0836
0837 ch->rx_count = priv->ae_handle->q_num;
0838 ch->tx_count = priv->ae_handle->q_num;
0839 }
0840
0841
0842
0843
0844
0845
0846
0847 static void hns_get_ethtool_stats(struct net_device *netdev,
0848 struct ethtool_stats *stats, u64 *data)
0849 {
0850 u64 *p = data;
0851 struct hns_nic_priv *priv = netdev_priv(netdev);
0852 struct hnae_handle *h = priv->ae_handle;
0853 const struct rtnl_link_stats64 *net_stats;
0854 struct rtnl_link_stats64 temp;
0855
0856 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
0857 netdev_err(netdev, "get_stats or update_stats is null!\n");
0858 return;
0859 }
0860
0861 h->dev->ops->update_stats(h, &netdev->stats);
0862
0863 net_stats = dev_get_stats(netdev, &temp);
0864
0865
0866 p[0] = net_stats->rx_packets;
0867 p[1] = net_stats->tx_packets;
0868 p[2] = net_stats->rx_bytes;
0869 p[3] = net_stats->tx_bytes;
0870 p[4] = net_stats->rx_errors;
0871 p[5] = net_stats->tx_errors;
0872 p[6] = net_stats->rx_dropped;
0873 p[7] = net_stats->tx_dropped;
0874 p[8] = net_stats->multicast;
0875 p[9] = net_stats->collisions;
0876 p[10] = net_stats->rx_over_errors;
0877 p[11] = net_stats->rx_crc_errors;
0878 p[12] = net_stats->rx_frame_errors;
0879 p[13] = net_stats->rx_fifo_errors;
0880 p[14] = net_stats->rx_missed_errors;
0881 p[15] = net_stats->tx_aborted_errors;
0882 p[16] = net_stats->tx_carrier_errors;
0883 p[17] = net_stats->tx_fifo_errors;
0884 p[18] = net_stats->tx_heartbeat_errors;
0885 p[19] = net_stats->rx_length_errors;
0886 p[20] = net_stats->tx_window_errors;
0887 p[21] = net_stats->rx_compressed;
0888 p[22] = net_stats->tx_compressed;
0889
0890 p[23] = 0;
0891 p[24] = 0;
0892
0893 p[25] = priv->tx_timeout_count;
0894
0895
0896 h->dev->ops->get_stats(h, &p[26]);
0897 }
0898
0899
0900
0901
0902
0903
0904
0905 static void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
0906 {
0907 struct hns_nic_priv *priv = netdev_priv(netdev);
0908 struct hnae_handle *h = priv->ae_handle;
0909 u8 *buff = data;
0910
0911 if (!h->dev->ops->get_strings) {
0912 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
0913 return;
0914 }
0915
0916 if (stringset == ETH_SS_TEST) {
0917 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
0918 ethtool_sprintf(&buff,
0919 hns_nic_test_strs[MAC_INTERNALLOOP_MAC]);
0920 ethtool_sprintf(&buff,
0921 hns_nic_test_strs[MAC_INTERNALLOOP_SERDES]);
0922 if ((netdev->phydev) && (!netdev->phydev->is_c45))
0923 ethtool_sprintf(&buff,
0924 hns_nic_test_strs[MAC_INTERNALLOOP_PHY]);
0925
0926 } else {
0927 ethtool_sprintf(&buff, "rx_packets");
0928 ethtool_sprintf(&buff, "tx_packets");
0929 ethtool_sprintf(&buff, "rx_bytes");
0930 ethtool_sprintf(&buff, "tx_bytes");
0931 ethtool_sprintf(&buff, "rx_errors");
0932 ethtool_sprintf(&buff, "tx_errors");
0933 ethtool_sprintf(&buff, "rx_dropped");
0934 ethtool_sprintf(&buff, "tx_dropped");
0935 ethtool_sprintf(&buff, "multicast");
0936 ethtool_sprintf(&buff, "collisions");
0937 ethtool_sprintf(&buff, "rx_over_errors");
0938 ethtool_sprintf(&buff, "rx_crc_errors");
0939 ethtool_sprintf(&buff, "rx_frame_errors");
0940 ethtool_sprintf(&buff, "rx_fifo_errors");
0941 ethtool_sprintf(&buff, "rx_missed_errors");
0942 ethtool_sprintf(&buff, "tx_aborted_errors");
0943 ethtool_sprintf(&buff, "tx_carrier_errors");
0944 ethtool_sprintf(&buff, "tx_fifo_errors");
0945 ethtool_sprintf(&buff, "tx_heartbeat_errors");
0946 ethtool_sprintf(&buff, "rx_length_errors");
0947 ethtool_sprintf(&buff, "tx_window_errors");
0948 ethtool_sprintf(&buff, "rx_compressed");
0949 ethtool_sprintf(&buff, "tx_compressed");
0950 ethtool_sprintf(&buff, "netdev_rx_dropped");
0951 ethtool_sprintf(&buff, "netdev_tx_dropped");
0952
0953 ethtool_sprintf(&buff, "netdev_tx_timeout");
0954
0955 h->dev->ops->get_strings(h, stringset, buff);
0956 }
0957 }
0958
0959
0960
0961
0962
0963
0964
0965
0966 static int hns_get_sset_count(struct net_device *netdev, int stringset)
0967 {
0968 struct hns_nic_priv *priv = netdev_priv(netdev);
0969 struct hnae_handle *h = priv->ae_handle;
0970 struct hnae_ae_ops *ops = h->dev->ops;
0971
0972 if (!ops->get_sset_count) {
0973 netdev_err(netdev, "get_sset_count is null!\n");
0974 return -EOPNOTSUPP;
0975 }
0976 if (stringset == ETH_SS_TEST) {
0977 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
0978
0979 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
0980 cnt--;
0981
0982 if ((!netdev->phydev) || (netdev->phydev->is_c45))
0983 cnt--;
0984
0985 return cnt;
0986 } else if (stringset == ETH_SS_STATS) {
0987 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
0988 } else {
0989 return -EOPNOTSUPP;
0990 }
0991 }
0992
0993
0994
0995
0996
0997
0998
0999
1000 static int hns_phy_led_set(struct net_device *netdev, int value)
1001 {
1002 int retval;
1003 struct phy_device *phy_dev = netdev->phydev;
1004
1005 retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1006 retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
1007 retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1008 if (retval) {
1009 netdev_err(netdev, "mdiobus_write fail !\n");
1010 return retval;
1011 }
1012 return 0;
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022 static int
1023 hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1024 {
1025 struct hns_nic_priv *priv = netdev_priv(netdev);
1026 struct hnae_handle *h = priv->ae_handle;
1027 struct phy_device *phy_dev = netdev->phydev;
1028 int ret;
1029
1030 if (phy_dev)
1031 switch (state) {
1032 case ETHTOOL_ID_ACTIVE:
1033 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1034 HNS_PHY_PAGE_LED);
1035 if (ret)
1036 return ret;
1037
1038 priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
1039
1040 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1041 HNS_PHY_PAGE_COPPER);
1042 if (ret)
1043 return ret;
1044 return 2;
1045 case ETHTOOL_ID_ON:
1046 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1047 if (ret)
1048 return ret;
1049 break;
1050 case ETHTOOL_ID_OFF:
1051 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1052 if (ret)
1053 return ret;
1054 break;
1055 case ETHTOOL_ID_INACTIVE:
1056 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1057 HNS_PHY_PAGE_LED);
1058 if (ret)
1059 return ret;
1060
1061 ret = phy_write(phy_dev, HNS_LED_FC_REG,
1062 priv->phy_led_val);
1063 if (ret)
1064 return ret;
1065
1066 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1067 HNS_PHY_PAGE_COPPER);
1068 if (ret)
1069 return ret;
1070 break;
1071 default:
1072 return -EINVAL;
1073 }
1074 else
1075 switch (state) {
1076 case ETHTOOL_ID_ACTIVE:
1077 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1078 case ETHTOOL_ID_ON:
1079 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1080 case ETHTOOL_ID_OFF:
1081 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1082 case ETHTOOL_ID_INACTIVE:
1083 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1084 default:
1085 return -EINVAL;
1086 }
1087
1088 return 0;
1089 }
1090
1091
1092
1093
1094
1095
1096
1097 static void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1098 void *data)
1099 {
1100 struct hns_nic_priv *priv = netdev_priv(net_dev);
1101 struct hnae_ae_ops *ops;
1102
1103 ops = priv->ae_handle->dev->ops;
1104
1105 cmd->version = HNS_CHIP_VERSION;
1106 if (!ops->get_regs) {
1107 netdev_err(net_dev, "ops->get_regs is null!\n");
1108 return;
1109 }
1110 ops->get_regs(priv->ae_handle, data);
1111 }
1112
1113
1114
1115
1116
1117
1118
1119 static int hns_get_regs_len(struct net_device *net_dev)
1120 {
1121 u32 reg_num;
1122 struct hns_nic_priv *priv = netdev_priv(net_dev);
1123 struct hnae_ae_ops *ops;
1124
1125 ops = priv->ae_handle->dev->ops;
1126 if (!ops->get_regs_len) {
1127 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1128 return -EOPNOTSUPP;
1129 }
1130
1131 reg_num = ops->get_regs_len(priv->ae_handle);
1132 if (reg_num > 0)
1133 return reg_num * sizeof(u32);
1134 else
1135 return reg_num;
1136 }
1137
1138
1139
1140
1141
1142
1143
1144 static int hns_nic_nway_reset(struct net_device *netdev)
1145 {
1146 struct phy_device *phy = netdev->phydev;
1147
1148 if (!netif_running(netdev))
1149 return 0;
1150
1151 if (!phy)
1152 return -EOPNOTSUPP;
1153
1154 if (phy->autoneg != AUTONEG_ENABLE)
1155 return -EINVAL;
1156
1157 return genphy_restart_aneg(phy);
1158 }
1159
1160 static u32
1161 hns_get_rss_key_size(struct net_device *netdev)
1162 {
1163 struct hns_nic_priv *priv = netdev_priv(netdev);
1164 struct hnae_ae_ops *ops;
1165
1166 if (AE_IS_VER1(priv->enet_ver)) {
1167 netdev_err(netdev,
1168 "RSS feature is not supported on this hardware\n");
1169 return 0;
1170 }
1171
1172 ops = priv->ae_handle->dev->ops;
1173 return ops->get_rss_key_size(priv->ae_handle);
1174 }
1175
1176 static u32
1177 hns_get_rss_indir_size(struct net_device *netdev)
1178 {
1179 struct hns_nic_priv *priv = netdev_priv(netdev);
1180 struct hnae_ae_ops *ops;
1181
1182 if (AE_IS_VER1(priv->enet_ver)) {
1183 netdev_err(netdev,
1184 "RSS feature is not supported on this hardware\n");
1185 return 0;
1186 }
1187
1188 ops = priv->ae_handle->dev->ops;
1189 return ops->get_rss_indir_size(priv->ae_handle);
1190 }
1191
1192 static int
1193 hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1194 {
1195 struct hns_nic_priv *priv = netdev_priv(netdev);
1196 struct hnae_ae_ops *ops;
1197
1198 if (AE_IS_VER1(priv->enet_ver)) {
1199 netdev_err(netdev,
1200 "RSS feature is not supported on this hardware\n");
1201 return -EOPNOTSUPP;
1202 }
1203
1204 ops = priv->ae_handle->dev->ops;
1205
1206 if (!indir)
1207 return 0;
1208
1209 return ops->get_rss(priv->ae_handle, indir, key, hfunc);
1210 }
1211
1212 static int
1213 hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1214 const u8 hfunc)
1215 {
1216 struct hns_nic_priv *priv = netdev_priv(netdev);
1217 struct hnae_ae_ops *ops;
1218
1219 if (AE_IS_VER1(priv->enet_ver)) {
1220 netdev_err(netdev,
1221 "RSS feature is not supported on this hardware\n");
1222 return -EOPNOTSUPP;
1223 }
1224
1225 ops = priv->ae_handle->dev->ops;
1226
1227 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
1228 netdev_err(netdev, "Invalid hfunc!\n");
1229 return -EOPNOTSUPP;
1230 }
1231
1232 return ops->set_rss(priv->ae_handle, indir, key, hfunc);
1233 }
1234
1235 static int hns_get_rxnfc(struct net_device *netdev,
1236 struct ethtool_rxnfc *cmd,
1237 u32 *rule_locs)
1238 {
1239 struct hns_nic_priv *priv = netdev_priv(netdev);
1240
1241 switch (cmd->cmd) {
1242 case ETHTOOL_GRXRINGS:
1243 cmd->data = priv->ae_handle->q_num;
1244 break;
1245 default:
1246 return -EOPNOTSUPP;
1247 }
1248
1249 return 0;
1250 }
1251
1252 static const struct ethtool_ops hns_ethtool_ops = {
1253 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1254 ETHTOOL_COALESCE_MAX_FRAMES |
1255 ETHTOOL_COALESCE_USE_ADAPTIVE |
1256 ETHTOOL_COALESCE_USECS_LOW_HIGH |
1257 ETHTOOL_COALESCE_MAX_FRAMES_LOW_HIGH,
1258 .get_drvinfo = hns_nic_get_drvinfo,
1259 .get_link = hns_nic_get_link,
1260 .get_ringparam = hns_get_ringparam,
1261 .get_pauseparam = hns_get_pauseparam,
1262 .set_pauseparam = hns_set_pauseparam,
1263 .get_coalesce = hns_get_coalesce,
1264 .set_coalesce = hns_set_coalesce,
1265 .get_channels = hns_get_channels,
1266 .self_test = hns_nic_self_test,
1267 .get_strings = hns_get_strings,
1268 .get_sset_count = hns_get_sset_count,
1269 .get_ethtool_stats = hns_get_ethtool_stats,
1270 .set_phys_id = hns_set_phys_id,
1271 .get_regs_len = hns_get_regs_len,
1272 .get_regs = hns_get_regs,
1273 .nway_reset = hns_nic_nway_reset,
1274 .get_rxfh_key_size = hns_get_rss_key_size,
1275 .get_rxfh_indir_size = hns_get_rss_indir_size,
1276 .get_rxfh = hns_get_rss,
1277 .set_rxfh = hns_set_rss,
1278 .get_rxnfc = hns_get_rxnfc,
1279 .get_link_ksettings = hns_nic_get_link_ksettings,
1280 .set_link_ksettings = hns_nic_set_link_ksettings,
1281 };
1282
1283 void hns_ethtool_set_ops(struct net_device *ndev)
1284 {
1285 ndev->ethtool_ops = &hns_ethtool_ops;
1286 }