2 * Copyright (c) 2014-2015 Hisilicon Limited.
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.
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
16 #define HNS_PHY_PAGE_MDIX 0
17 #define HNS_PHY_PAGE_LED 3
18 #define HNS_PHY_PAGE_COPPER 0
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. */
26 #define HNS_LED_FORCE_ON 9
27 #define HNS_LED_FORCE_OFF 8
29 #define HNS_CHIP_VERSION 660
30 #define HNS_NET_STATS_CNT 26
32 #define PHY_MDIX_CTRL_S (5)
33 #define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
35 #define PHY_MDIX_STATUS_B (6)
36 #define PHY_SPEED_DUP_RESOLVE_B (11)
39 *hns_nic_get_link - get current link status
41 *retuen 0 - success , negative --fail
43 static u32 hns_nic_get_link(struct net_device *net_dev)
45 struct hns_nic_priv *priv = netdev_priv(net_dev);
46 u32 link_stat = priv->link;
47 struct hnae_handle *h;
51 if (net_dev->phydev) {
52 if (!genphy_read_status(net_dev->phydev))
53 link_stat = net_dev->phydev->link;
58 if (h->dev && h->dev->ops && h->dev->ops->get_status)
59 link_stat = link_stat && h->dev->ops->get_status(h);
66 static void hns_get_mdix_mode(struct net_device *net_dev,
67 struct ethtool_link_ksettings *cmd)
69 int mdix_ctrl, mdix, retval, is_resolved;
70 struct phy_device *phy_dev = net_dev->phydev;
72 if (!phy_dev || !phy_dev->mdio.bus) {
73 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
74 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
78 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_MDIX);
80 retval = phy_read(phy_dev, HNS_PHY_CSC_REG);
81 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
83 retval = phy_read(phy_dev, HNS_PHY_CSS_REG);
84 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
85 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
87 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
91 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI;
94 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_X;
97 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
100 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
105 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
107 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
109 cmd->base.eth_tp_mdix = ETH_TP_MDI;
113 *hns_nic_get_link_ksettings - implement ethtool get link ksettings
114 *@net_dev: net_device
115 *@cmd: ethtool_link_ksettings
116 *retuen 0 - success , negative --fail
118 static int hns_nic_get_link_ksettings(struct net_device *net_dev,
119 struct ethtool_link_ksettings *cmd)
121 struct hns_nic_priv *priv = netdev_priv(net_dev);
122 struct hnae_handle *h;
127 u32 supported, advertising;
129 if (!priv || !priv->ae_handle)
133 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
136 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
138 netdev_err(net_dev, "%s get_info error!\n", __func__);
142 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);
147 /* When there is no phy, autoneg is off. */
148 cmd->base.autoneg = false;
149 cmd->base.speed = speed;
150 cmd->base.duplex = duplex;
153 phy_ethtool_ksettings_get(net_dev->phydev, cmd);
155 link_stat = hns_nic_get_link(net_dev);
157 cmd->base.speed = (u32)SPEED_UNKNOWN;
158 cmd->base.duplex = DUPLEX_UNKNOWN;
161 if (cmd->base.autoneg)
162 advertising |= ADVERTISED_Autoneg;
164 supported |= h->if_support;
165 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
166 supported |= SUPPORTED_TP;
167 advertising |= ADVERTISED_1000baseT_Full;
168 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
169 supported |= SUPPORTED_FIBRE;
170 advertising |= ADVERTISED_10000baseKR_Full;
173 switch (h->media_type) {
174 case HNAE_MEDIA_TYPE_FIBER:
175 cmd->base.port = PORT_FIBRE;
177 case HNAE_MEDIA_TYPE_COPPER:
178 cmd->base.port = PORT_TP;
180 case HNAE_MEDIA_TYPE_UNKNOWN:
185 if (!(AE_IS_VER1(priv->enet_ver) && h->port_type == HNAE_PORT_DEBUG))
186 supported |= SUPPORTED_Pause;
188 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
190 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
193 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22;
194 hns_get_mdix_mode(net_dev, cmd);
200 *hns_nic_set_link_settings - implement ethtool set link ksettings
201 *@net_dev: net_device
202 *@cmd: ethtool_link_ksettings
203 *retuen 0 - success , negative --fail
205 static int hns_nic_set_link_ksettings(struct net_device *net_dev,
206 const struct ethtool_link_ksettings *cmd)
208 struct hns_nic_priv *priv = netdev_priv(net_dev);
209 struct hnae_handle *h;
212 if (!netif_running(net_dev))
215 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
216 !priv->ae_handle->dev->ops)
220 speed = cmd->base.speed;
222 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
223 if (cmd->base.autoneg == AUTONEG_ENABLE ||
224 speed != SPEED_10000 ||
225 cmd->base.duplex != DUPLEX_FULL)
227 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
228 if (!net_dev->phydev && cmd->base.autoneg == AUTONEG_ENABLE)
231 if (speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
234 return phy_ethtool_ksettings_set(net_dev->phydev, cmd);
236 if ((speed != SPEED_10 && speed != SPEED_100 &&
237 speed != SPEED_1000) || (cmd->base.duplex != DUPLEX_HALF &&
238 cmd->base.duplex != DUPLEX_FULL))
241 netdev_err(net_dev, "Not supported!");
245 if (h->dev->ops->adjust_link) {
246 netif_carrier_off(net_dev);
247 h->dev->ops->adjust_link(h, (int)speed, cmd->base.duplex);
248 netif_carrier_on(net_dev);
252 netdev_err(net_dev, "Not supported!");
256 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
258 "Serdes Loopback test",
262 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
267 /* Doing phy loopback in offline state, phy resuming is
268 * needed to power up the device.
270 err = phy_resume(phy_dev);
274 err = phy_loopback(phy_dev, true);
276 err = phy_loopback(phy_dev, false);
280 err = phy_suspend(phy_dev);
287 static int __lb_setup(struct net_device *ndev,
291 struct hns_nic_priv *priv = netdev_priv(ndev);
292 struct phy_device *phy_dev = ndev->phydev;
293 struct hnae_handle *h = priv->ae_handle;
296 case MAC_INTERNALLOOP_PHY:
297 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
299 ret = h->dev->ops->set_loopback(h, loop, 0x1);
301 case MAC_INTERNALLOOP_MAC:
302 if ((h->dev->ops->set_loopback) &&
303 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
304 ret = h->dev->ops->set_loopback(h, loop, 0x1);
306 case MAC_INTERNALLOOP_SERDES:
307 if (h->dev->ops->set_loopback)
308 ret = h->dev->ops->set_loopback(h, loop, 0x1);
310 case MAC_LOOP_PHY_NONE:
311 ret = hns_nic_config_phy_loopback(phy_dev, 0x0);
314 if (!ret && h->dev->ops->set_loopback) {
315 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
316 ret = h->dev->ops->set_loopback(h,
317 MAC_INTERNALLOOP_MAC, 0x0);
320 ret = h->dev->ops->set_loopback(h,
321 MAC_INTERNALLOOP_SERDES, 0x0);
330 if (loop == MAC_LOOP_NONE)
331 h->dev->ops->set_promisc_mode(
332 h, ndev->flags & IFF_PROMISC);
334 h->dev->ops->set_promisc_mode(h, 1);
339 static int __lb_up(struct net_device *ndev,
340 enum hnae_loop loop_mode)
342 #define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
343 struct hns_nic_priv *priv = netdev_priv(ndev);
344 struct hnae_handle *h = priv->ae_handle;
348 hns_nic_net_reset(ndev);
350 ret = __lb_setup(ndev, loop_mode);
356 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
360 /* link adjust duplex*/
361 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
367 h->dev->ops->adjust_link(h, speed, duplex);
369 /* wait adjust link done and phy ready */
370 msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
375 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
378 struct net_device *ndev;
379 struct hns_nic_priv *priv;
380 struct hnae_ring *ring;
381 struct netdev_queue *dev_queue;
382 struct sk_buff *new_skb;
383 unsigned int frame_size;
386 char buff[33]; /* 32B data and the last character '\0' */
388 if (!ring_data) { /* Just for doing create frame*/
390 priv = netdev_priv(ndev);
392 frame_size = skb->len;
393 memset(skb->data, 0xFF, frame_size);
394 if ((!AE_IS_VER1(priv->enet_ver)) &&
395 (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
396 memcpy(skb->data, ndev->dev_addr, 6);
397 skb->data[5] += 0x1f;
401 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
402 memset(&skb->data[frame_size / 2 + 10], 0xBE,
403 frame_size / 2 - 11);
404 memset(&skb->data[frame_size / 2 + 12], 0xAF,
405 frame_size / 2 - 13);
409 ring = ring_data->ring;
410 ndev = ring_data->napi.dev;
411 if (is_tx_ring(ring)) { /* for tx queue reset*/
412 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
413 netdev_tx_reset_queue(dev_queue);
417 frame_size = skb->len;
420 new_skb = skb_copy(skb, GFP_ATOMIC);
421 dev_kfree_skb_any(skb);
423 netdev_err(ndev, "skb alloc failed\n");
429 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
430 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
431 (*(skb->data + frame_size / 2 + 12) == 0xAF))
436 ndev->stats.rx_packets++;
437 ndev->stats.rx_bytes += skb->len;
439 ndev->stats.rx_frame_errors++;
440 for (i = 0; i < skb->len; i++) {
441 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
442 "%02x", *(skb->data + i));
443 if ((i % 16 == 15) || (i == skb->len - 1))
444 pr_info("%s\n", buff);
447 dev_kfree_skb_any(skb);
450 static int __lb_clean_rings(struct hns_nic_priv *priv,
451 int ringid0, int ringid1, int budget)
454 struct hns_nic_ring_data *ring_data;
455 struct net_device *ndev = priv->netdev;
456 unsigned long rx_packets = ndev->stats.rx_packets;
457 unsigned long rx_bytes = ndev->stats.rx_bytes;
458 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
460 for (i = ringid0; i <= ringid1; i++) {
461 ring_data = &priv->ring_data[i];
462 (void)ring_data->poll_one(ring_data,
463 budget, __lb_other_process);
465 ret = (int)(ndev->stats.rx_packets - rx_packets);
466 ndev->stats.rx_packets = rx_packets;
467 ndev->stats.rx_bytes = rx_bytes;
468 ndev->stats.rx_frame_errors = rx_frame_errors;
473 * nic_run_loopback_test - run loopback test
474 * @nic_dev: net device
475 * @loopback_type: loopback type
477 static int __lb_run_test(struct net_device *ndev,
478 enum hnae_loop loop_mode)
480 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
481 #define NIC_LB_TEST_RING_ID 0
482 #define NIC_LB_TEST_FRAME_SIZE 128
483 /* nic loopback test err */
484 #define NIC_LB_TEST_NO_MEM_ERR 1
485 #define NIC_LB_TEST_TX_CNT_ERR 2
486 #define NIC_LB_TEST_RX_CNT_ERR 3
487 #define NIC_LB_TEST_RX_PKG_ERR 4
488 struct hns_nic_priv *priv = netdev_priv(ndev);
489 struct hnae_handle *h = priv->ae_handle;
490 int i, j, lc, good_cnt, ret_val = 0;
492 netdev_tx_t tx_ret_val;
495 size = NIC_LB_TEST_FRAME_SIZE;
496 /* allocate test skb */
497 skb = alloc_skb(size, GFP_KERNEL);
499 return NIC_LB_TEST_NO_MEM_ERR;
501 /* place data into test skb */
502 (void)skb_put(skb, size);
504 __lb_other_process(NULL, skb);
505 skb->queue_mapping = NIC_LB_TEST_RING_ID;
508 for (j = 0; j < lc; j++) {
509 /* reset count of good packets */
511 /* place 64 packets on the transmit queue*/
512 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
515 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
517 &tx_ring_data(priv, skb->queue_mapping));
518 if (tx_ret_val == NETDEV_TX_OK)
523 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
524 ret_val = NIC_LB_TEST_TX_CNT_ERR;
525 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
526 hns_nic_test_strs[loop_mode], good_cnt,
527 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
531 /* allow 100 milliseconds for packets to go from Tx to Rx */
534 good_cnt = __lb_clean_rings(priv,
535 h->q_num, h->q_num * 2 - 1,
536 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
537 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
538 ret_val = NIC_LB_TEST_RX_CNT_ERR;
539 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
540 hns_nic_test_strs[loop_mode], good_cnt,
541 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
544 (void)__lb_clean_rings(priv,
545 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
546 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
549 /* free the original skb */
555 static int __lb_down(struct net_device *ndev, enum hnae_loop loop)
557 struct hns_nic_priv *priv = netdev_priv(ndev);
558 struct hnae_handle *h = priv->ae_handle;
561 if (loop == MAC_INTERNALLOOP_PHY)
562 ret = __lb_setup(ndev, MAC_LOOP_PHY_NONE);
564 ret = __lb_setup(ndev, MAC_LOOP_NONE);
566 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
570 if (h->dev->ops->stop)
571 h->dev->ops->stop(h);
573 usleep_range(10000, 20000);
574 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
576 hns_nic_net_reset(ndev);
582 * hns_nic_self_test - self test
584 * @eth_test: test cmd
587 static void hns_nic_self_test(struct net_device *ndev,
588 struct ethtool_test *eth_test, u64 *data)
590 struct hns_nic_priv *priv = netdev_priv(ndev);
591 bool if_running = netif_running(ndev);
592 #define SELF_TEST_TPYE_NUM 3
593 int st_param[SELF_TEST_TPYE_NUM][2];
597 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
598 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
599 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
600 st_param[1][1] = 1; /*serdes must exist*/
601 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
602 st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
603 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
605 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
606 set_bit(NIC_STATE_TESTING, &priv->state);
611 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
613 continue; /* NEXT testing */
615 data[test_index] = __lb_up(ndev,
616 (enum hnae_loop)st_param[i][0]);
617 if (!data[test_index]) {
618 data[test_index] = __lb_run_test(
619 ndev, (enum hnae_loop)st_param[i][0]);
620 (void)__lb_down(ndev,
621 (enum hnae_loop)st_param[i][0]);
624 if (data[test_index])
625 eth_test->flags |= ETH_TEST_FL_FAILED;
630 hns_nic_net_reset(priv->netdev);
632 clear_bit(NIC_STATE_TESTING, &priv->state);
635 (void)dev_open(ndev);
637 /* Online tests aren't run; pass by default */
639 (void)msleep_interruptible(4 * 1000);
643 * hns_nic_get_drvinfo - get net driver info
645 * @drvinfo: driver info
647 static void hns_nic_get_drvinfo(struct net_device *net_dev,
648 struct ethtool_drvinfo *drvinfo)
650 struct hns_nic_priv *priv = netdev_priv(net_dev);
652 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
653 sizeof(drvinfo->version));
654 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
656 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
657 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
659 strncpy(drvinfo->bus_info, priv->dev->bus->name,
660 sizeof(drvinfo->bus_info));
661 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
663 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
664 drvinfo->eedump_len = 0;
668 * hns_get_ringparam - get ring parameter
670 * @param: ethtool parameter
672 static void hns_get_ringparam(struct net_device *net_dev,
673 struct ethtool_ringparam *param)
675 struct hns_nic_priv *priv = netdev_priv(net_dev);
676 struct hnae_ae_ops *ops;
677 struct hnae_queue *queue;
680 queue = priv->ae_handle->qs[0];
681 ops = priv->ae_handle->dev->ops;
683 if (ops->get_ring_bdnum_limit)
684 ops->get_ring_bdnum_limit(queue, &uplimit);
686 param->rx_max_pending = uplimit;
687 param->tx_max_pending = uplimit;
688 param->rx_pending = queue->rx_ring.desc_num;
689 param->tx_pending = queue->tx_ring.desc_num;
693 * hns_get_pauseparam - get pause parameter
695 * @param: pause parameter
697 static void hns_get_pauseparam(struct net_device *net_dev,
698 struct ethtool_pauseparam *param)
700 struct hns_nic_priv *priv = netdev_priv(net_dev);
701 struct hnae_ae_ops *ops;
703 ops = priv->ae_handle->dev->ops;
705 if (ops->get_pauseparam)
706 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
707 ¶m->rx_pause, ¶m->tx_pause);
711 * hns_set_pauseparam - set pause parameter
713 * @param: pause parameter
715 * Return 0 on success, negative on failure
717 static int hns_set_pauseparam(struct net_device *net_dev,
718 struct ethtool_pauseparam *param)
720 struct hns_nic_priv *priv = netdev_priv(net_dev);
721 struct hnae_handle *h;
722 struct hnae_ae_ops *ops;
727 if (!ops->set_pauseparam)
730 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
731 param->rx_pause, param->tx_pause);
735 * hns_get_coalesce - get coalesce info.
737 * @ec: coalesce info.
739 * Return 0 on success, negative on failure.
741 static int hns_get_coalesce(struct net_device *net_dev,
742 struct ethtool_coalesce *ec)
744 struct hns_nic_priv *priv = netdev_priv(net_dev);
745 struct hnae_ae_ops *ops;
747 ops = priv->ae_handle->dev->ops;
749 ec->use_adaptive_rx_coalesce = priv->ae_handle->coal_adapt_en;
750 ec->use_adaptive_tx_coalesce = priv->ae_handle->coal_adapt_en;
752 if ((!ops->get_coalesce_usecs) ||
753 (!ops->get_max_coalesced_frames))
756 ops->get_coalesce_usecs(priv->ae_handle,
757 &ec->tx_coalesce_usecs,
758 &ec->rx_coalesce_usecs);
760 ops->get_max_coalesced_frames(
762 &ec->tx_max_coalesced_frames,
763 &ec->rx_max_coalesced_frames);
765 ops->get_coalesce_range(priv->ae_handle,
766 &ec->tx_max_coalesced_frames_low,
767 &ec->rx_max_coalesced_frames_low,
768 &ec->tx_max_coalesced_frames_high,
769 &ec->rx_max_coalesced_frames_high,
770 &ec->tx_coalesce_usecs_low,
771 &ec->rx_coalesce_usecs_low,
772 &ec->tx_coalesce_usecs_high,
773 &ec->rx_coalesce_usecs_high);
779 * hns_set_coalesce - set coalesce info.
781 * @ec: coalesce info.
783 * Return 0 on success, negative on failure.
785 static int hns_set_coalesce(struct net_device *net_dev,
786 struct ethtool_coalesce *ec)
788 struct hns_nic_priv *priv = netdev_priv(net_dev);
789 struct hnae_ae_ops *ops;
792 ops = priv->ae_handle->dev->ops;
794 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
797 if ((!ops->set_coalesce_usecs) ||
798 (!ops->set_coalesce_frames))
801 if (ec->use_adaptive_rx_coalesce != priv->ae_handle->coal_adapt_en)
802 priv->ae_handle->coal_adapt_en = ec->use_adaptive_rx_coalesce;
804 rc1 = ops->set_coalesce_usecs(priv->ae_handle,
805 ec->rx_coalesce_usecs);
807 rc2 = ops->set_coalesce_frames(priv->ae_handle,
808 ec->tx_max_coalesced_frames,
809 ec->rx_max_coalesced_frames);
818 * hns_get_channels - get channel info.
823 hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
825 struct hns_nic_priv *priv = netdev_priv(net_dev);
827 ch->max_rx = priv->ae_handle->q_num;
828 ch->max_tx = priv->ae_handle->q_num;
830 ch->rx_count = priv->ae_handle->q_num;
831 ch->tx_count = priv->ae_handle->q_num;
835 * get_ethtool_stats - get detail statistics.
837 * @stats: statistics info.
838 * @data: statistics data.
840 static void hns_get_ethtool_stats(struct net_device *netdev,
841 struct ethtool_stats *stats, u64 *data)
844 struct hns_nic_priv *priv = netdev_priv(netdev);
845 struct hnae_handle *h = priv->ae_handle;
846 const struct rtnl_link_stats64 *net_stats;
847 struct rtnl_link_stats64 temp;
849 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
850 netdev_err(netdev, "get_stats or update_stats is null!\n");
854 h->dev->ops->update_stats(h, &netdev->stats);
856 net_stats = dev_get_stats(netdev, &temp);
858 /* get netdev statistics */
859 p[0] = net_stats->rx_packets;
860 p[1] = net_stats->tx_packets;
861 p[2] = net_stats->rx_bytes;
862 p[3] = net_stats->tx_bytes;
863 p[4] = net_stats->rx_errors;
864 p[5] = net_stats->tx_errors;
865 p[6] = net_stats->rx_dropped;
866 p[7] = net_stats->tx_dropped;
867 p[8] = net_stats->multicast;
868 p[9] = net_stats->collisions;
869 p[10] = net_stats->rx_over_errors;
870 p[11] = net_stats->rx_crc_errors;
871 p[12] = net_stats->rx_frame_errors;
872 p[13] = net_stats->rx_fifo_errors;
873 p[14] = net_stats->rx_missed_errors;
874 p[15] = net_stats->tx_aborted_errors;
875 p[16] = net_stats->tx_carrier_errors;
876 p[17] = net_stats->tx_fifo_errors;
877 p[18] = net_stats->tx_heartbeat_errors;
878 p[19] = net_stats->rx_length_errors;
879 p[20] = net_stats->tx_window_errors;
880 p[21] = net_stats->rx_compressed;
881 p[22] = net_stats->tx_compressed;
883 p[23] = netdev->rx_dropped.counter;
884 p[24] = netdev->tx_dropped.counter;
886 p[25] = priv->tx_timeout_count;
888 /* get driver statistics */
889 h->dev->ops->get_stats(h, &p[26]);
893 * get_strings: Return a set of strings that describe the requested objects
895 * @stats: string set ID.
896 * @data: objects data.
898 static void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
900 struct hns_nic_priv *priv = netdev_priv(netdev);
901 struct hnae_handle *h = priv->ae_handle;
902 char *buff = (char *)data;
904 if (!h->dev->ops->get_strings) {
905 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
909 if (stringset == ETH_SS_TEST) {
910 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
911 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
913 buff += ETH_GSTRING_LEN;
915 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
917 buff += ETH_GSTRING_LEN;
918 if ((netdev->phydev) && (!netdev->phydev->is_c45))
919 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
923 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
924 buff = buff + ETH_GSTRING_LEN;
925 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
926 buff = buff + ETH_GSTRING_LEN;
927 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
928 buff = buff + ETH_GSTRING_LEN;
929 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
930 buff = buff + ETH_GSTRING_LEN;
931 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
932 buff = buff + ETH_GSTRING_LEN;
933 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
934 buff = buff + ETH_GSTRING_LEN;
935 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
936 buff = buff + ETH_GSTRING_LEN;
937 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
938 buff = buff + ETH_GSTRING_LEN;
939 snprintf(buff, ETH_GSTRING_LEN, "multicast");
940 buff = buff + ETH_GSTRING_LEN;
941 snprintf(buff, ETH_GSTRING_LEN, "collisions");
942 buff = buff + ETH_GSTRING_LEN;
943 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
944 buff = buff + ETH_GSTRING_LEN;
945 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
946 buff = buff + ETH_GSTRING_LEN;
947 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
948 buff = buff + ETH_GSTRING_LEN;
949 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
950 buff = buff + ETH_GSTRING_LEN;
951 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
952 buff = buff + ETH_GSTRING_LEN;
953 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
954 buff = buff + ETH_GSTRING_LEN;
955 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
956 buff = buff + ETH_GSTRING_LEN;
957 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
958 buff = buff + ETH_GSTRING_LEN;
959 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
960 buff = buff + ETH_GSTRING_LEN;
961 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
962 buff = buff + ETH_GSTRING_LEN;
963 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
964 buff = buff + ETH_GSTRING_LEN;
965 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
966 buff = buff + ETH_GSTRING_LEN;
967 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
968 buff = buff + ETH_GSTRING_LEN;
969 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
970 buff = buff + ETH_GSTRING_LEN;
971 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
972 buff = buff + ETH_GSTRING_LEN;
974 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
975 buff = buff + ETH_GSTRING_LEN;
977 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
982 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
984 * @stringset: string set index, 0: self test string; 1: statistics string.
986 * Return string set count.
988 static int hns_get_sset_count(struct net_device *netdev, int stringset)
990 struct hns_nic_priv *priv = netdev_priv(netdev);
991 struct hnae_handle *h = priv->ae_handle;
992 struct hnae_ae_ops *ops = h->dev->ops;
994 if (!ops->get_sset_count) {
995 netdev_err(netdev, "get_sset_count is null!\n");
998 if (stringset == ETH_SS_TEST) {
999 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1001 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1004 if ((!netdev->phydev) || (netdev->phydev->is_c45))
1008 } else if (stringset == ETH_SS_STATS) {
1009 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1016 * hns_phy_led_set - set phy LED status.
1018 * @value: LED state.
1020 * Return 0 on success, negative on failure.
1022 static int hns_phy_led_set(struct net_device *netdev, int value)
1025 struct phy_device *phy_dev = netdev->phydev;
1027 retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1028 retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
1029 retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1031 netdev_err(netdev, "mdiobus_write fail !\n");
1038 * nic_set_phys_id - set phy identify LED.
1040 * @state: LED state.
1042 * Return 0 on success, negative on failure.
1045 hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1047 struct hns_nic_priv *priv = netdev_priv(netdev);
1048 struct hnae_handle *h = priv->ae_handle;
1049 struct phy_device *phy_dev = netdev->phydev;
1054 case ETHTOOL_ID_ACTIVE:
1055 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1060 priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
1062 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1063 HNS_PHY_PAGE_COPPER);
1068 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1072 case ETHTOOL_ID_OFF:
1073 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1077 case ETHTOOL_ID_INACTIVE:
1078 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1083 ret = phy_write(phy_dev, HNS_LED_FC_REG,
1088 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1089 HNS_PHY_PAGE_COPPER);
1098 case ETHTOOL_ID_ACTIVE:
1099 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1101 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1102 case ETHTOOL_ID_OFF:
1103 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1104 case ETHTOOL_ID_INACTIVE:
1105 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1114 * hns_get_regs - get net device register
1117 * @date: register data
1119 static void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1122 struct hns_nic_priv *priv = netdev_priv(net_dev);
1123 struct hnae_ae_ops *ops;
1125 ops = priv->ae_handle->dev->ops;
1127 cmd->version = HNS_CHIP_VERSION;
1128 if (!ops->get_regs) {
1129 netdev_err(net_dev, "ops->get_regs is null!\n");
1132 ops->get_regs(priv->ae_handle, data);
1136 * nic_get_regs_len - get total register len.
1139 * Return total register len.
1141 static int hns_get_regs_len(struct net_device *net_dev)
1144 struct hns_nic_priv *priv = netdev_priv(net_dev);
1145 struct hnae_ae_ops *ops;
1147 ops = priv->ae_handle->dev->ops;
1148 if (!ops->get_regs_len) {
1149 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1153 reg_num = ops->get_regs_len(priv->ae_handle);
1155 return reg_num * sizeof(u32);
1157 return reg_num; /* error code */
1161 * hns_nic_nway_reset - nway reset
1164 * Return 0 on success, negative on failure
1166 static int hns_nic_nway_reset(struct net_device *netdev)
1168 struct phy_device *phy = netdev->phydev;
1170 if (!netif_running(netdev))
1176 if (phy->autoneg != AUTONEG_ENABLE)
1179 return genphy_restart_aneg(phy);
1183 hns_get_rss_key_size(struct net_device *netdev)
1185 struct hns_nic_priv *priv = netdev_priv(netdev);
1186 struct hnae_ae_ops *ops;
1188 if (AE_IS_VER1(priv->enet_ver)) {
1190 "RSS feature is not supported on this hardware\n");
1194 ops = priv->ae_handle->dev->ops;
1195 return ops->get_rss_key_size(priv->ae_handle);
1199 hns_get_rss_indir_size(struct net_device *netdev)
1201 struct hns_nic_priv *priv = netdev_priv(netdev);
1202 struct hnae_ae_ops *ops;
1204 if (AE_IS_VER1(priv->enet_ver)) {
1206 "RSS feature is not supported on this hardware\n");
1210 ops = priv->ae_handle->dev->ops;
1211 return ops->get_rss_indir_size(priv->ae_handle);
1215 hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1217 struct hns_nic_priv *priv = netdev_priv(netdev);
1218 struct hnae_ae_ops *ops;
1220 if (AE_IS_VER1(priv->enet_ver)) {
1222 "RSS feature is not supported on this hardware\n");
1226 ops = priv->ae_handle->dev->ops;
1231 return ops->get_rss(priv->ae_handle, indir, key, hfunc);
1235 hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1238 struct hns_nic_priv *priv = netdev_priv(netdev);
1239 struct hnae_ae_ops *ops;
1241 if (AE_IS_VER1(priv->enet_ver)) {
1243 "RSS feature is not supported on this hardware\n");
1247 ops = priv->ae_handle->dev->ops;
1249 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
1250 netdev_err(netdev, "Invalid hfunc!\n");
1254 return ops->set_rss(priv->ae_handle, indir, key, hfunc);
1257 static int hns_get_rxnfc(struct net_device *netdev,
1258 struct ethtool_rxnfc *cmd,
1261 struct hns_nic_priv *priv = netdev_priv(netdev);
1264 case ETHTOOL_GRXRINGS:
1265 cmd->data = priv->ae_handle->q_num;
1274 static const struct ethtool_ops hns_ethtool_ops = {
1275 .get_drvinfo = hns_nic_get_drvinfo,
1276 .get_link = hns_nic_get_link,
1277 .get_ringparam = hns_get_ringparam,
1278 .get_pauseparam = hns_get_pauseparam,
1279 .set_pauseparam = hns_set_pauseparam,
1280 .get_coalesce = hns_get_coalesce,
1281 .set_coalesce = hns_set_coalesce,
1282 .get_channels = hns_get_channels,
1283 .self_test = hns_nic_self_test,
1284 .get_strings = hns_get_strings,
1285 .get_sset_count = hns_get_sset_count,
1286 .get_ethtool_stats = hns_get_ethtool_stats,
1287 .set_phys_id = hns_set_phys_id,
1288 .get_regs_len = hns_get_regs_len,
1289 .get_regs = hns_get_regs,
1290 .nway_reset = hns_nic_nway_reset,
1291 .get_rxfh_key_size = hns_get_rss_key_size,
1292 .get_rxfh_indir_size = hns_get_rss_indir_size,
1293 .get_rxfh = hns_get_rss,
1294 .set_rxfh = hns_set_rss,
1295 .get_rxnfc = hns_get_rxnfc,
1296 .get_link_ksettings = hns_nic_get_link_ksettings,
1297 .set_link_ksettings = hns_nic_set_link_ksettings,
1300 void hns_ethtool_set_ops(struct net_device *ndev)
1302 ndev->ethtool_ops = &hns_ethtool_ops;