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;
49 assert(priv && priv->ae_handle);
53 if (!genphy_update_link(priv->phy))
54 link_stat = priv->phy->link;
59 if (h->dev && h->dev->ops && h->dev->ops->get_status)
60 link_stat = link_stat && h->dev->ops->get_status(h);
67 static void hns_get_mdix_mode(struct net_device *net_dev,
68 struct ethtool_cmd *cmd)
70 int mdix_ctrl, mdix, retval, is_resolved;
71 struct hns_nic_priv *priv = netdev_priv(net_dev);
72 struct phy_device *phy_dev = priv->phy;
74 if (!phy_dev || !phy_dev->bus) {
75 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
76 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
80 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
83 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSC_REG);
84 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
86 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSS_REG);
87 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
88 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
90 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
95 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI;
98 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_X;
101 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
104 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
109 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
111 cmd->eth_tp_mdix = ETH_TP_MDI_X;
113 cmd->eth_tp_mdix = ETH_TP_MDI;
117 *hns_nic_get_settings - implement ethtool get settings
118 *@net_dev: net_device
120 *retuen 0 - success , negative --fail
122 static int hns_nic_get_settings(struct net_device *net_dev,
123 struct ethtool_cmd *cmd)
125 struct hns_nic_priv *priv = netdev_priv(net_dev);
126 struct hnae_handle *h;
132 if (!priv || !priv->ae_handle)
136 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
139 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
141 netdev_err(net_dev, "%s get_info error!\n", __func__);
145 /* When there is no phy, autoneg is off. */
146 cmd->autoneg = false;
147 ethtool_cmd_speed_set(cmd, speed);
148 cmd->duplex = duplex;
151 (void)phy_ethtool_gset(priv->phy, cmd);
153 link_stat = hns_nic_get_link(net_dev);
155 ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
156 cmd->duplex = DUPLEX_UNKNOWN;
160 cmd->advertising |= ADVERTISED_Autoneg;
162 cmd->supported |= h->if_support;
163 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
164 cmd->supported |= SUPPORTED_TP;
165 cmd->advertising |= ADVERTISED_1000baseT_Full;
166 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
167 cmd->supported |= SUPPORTED_FIBRE;
168 cmd->advertising |= ADVERTISED_10000baseKR_Full;
171 if (h->port_type == HNAE_PORT_SERVICE) {
172 cmd->port = PORT_FIBRE;
173 cmd->supported |= SUPPORTED_Pause;
178 cmd->transceiver = XCVR_EXTERNAL;
179 cmd->mdio_support = (ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22);
180 hns_get_mdix_mode(net_dev, cmd);
186 *hns_nic_set_settings - implement ethtool set settings
187 *@net_dev: net_device
189 *retuen 0 - success , negative --fail
191 static int hns_nic_set_settings(struct net_device *net_dev,
192 struct ethtool_cmd *cmd)
194 struct hns_nic_priv *priv = netdev_priv(net_dev);
195 struct hnae_handle *h;
198 if (!netif_running(net_dev))
201 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
202 !priv->ae_handle->dev->ops)
206 speed = ethtool_cmd_speed(cmd);
208 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
209 if (cmd->autoneg == AUTONEG_ENABLE || speed != SPEED_10000 ||
210 cmd->duplex != DUPLEX_FULL)
212 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
213 if (!priv->phy && cmd->autoneg == AUTONEG_ENABLE)
216 if (speed == SPEED_1000 && cmd->duplex == DUPLEX_HALF)
219 return phy_ethtool_sset(priv->phy, cmd);
221 if ((speed != SPEED_10 && speed != SPEED_100 &&
222 speed != SPEED_1000) || (cmd->duplex != DUPLEX_HALF &&
223 cmd->duplex != DUPLEX_FULL))
226 netdev_err(net_dev, "Not supported!");
230 if (h->dev->ops->adjust_link) {
231 h->dev->ops->adjust_link(h, (int)speed, cmd->duplex);
235 netdev_err(net_dev, "Not supported!");
239 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
241 "Serdes Loopback test",
245 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
247 #define COPPER_CONTROL_REG 0
248 #define PHY_LOOP_BACK BIT(14)
251 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
256 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
257 HNS_PHY_PAGE_REG, 2);
258 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
261 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
264 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
266 /* If autoneg disabled,two soft-reset operations */
267 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
269 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
272 /* Default is 0x0400 */
273 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
276 /* Force 1000M Link, Default is 0x0200 */
277 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
279 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
282 /* Enable MAC loop-back */
283 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
285 val |= PHY_LOOP_BACK;
286 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
287 COPPER_CONTROL_REG, val);
289 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
291 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
293 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
295 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
298 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
300 val &= ~PHY_LOOP_BACK;
301 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
302 COPPER_CONTROL_REG, val);
307 static int __lb_setup(struct net_device *ndev,
311 struct hns_nic_priv *priv = netdev_priv(ndev);
312 struct phy_device *phy_dev = priv->phy;
313 struct hnae_handle *h = priv->ae_handle;
316 case MAC_INTERNALLOOP_PHY:
317 if ((phy_dev) && (!phy_dev->is_c45))
318 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
320 case MAC_INTERNALLOOP_MAC:
321 if ((h->dev->ops->set_loopback) &&
322 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
323 ret = h->dev->ops->set_loopback(h, loop, 0x1);
325 case MAC_INTERNALLOOP_SERDES:
326 if (h->dev->ops->set_loopback)
327 ret = h->dev->ops->set_loopback(h, loop, 0x1);
330 if ((phy_dev) && (!phy_dev->is_c45))
331 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
333 if (h->dev->ops->set_loopback) {
334 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
335 ret |= h->dev->ops->set_loopback(h,
336 MAC_INTERNALLOOP_MAC, 0x0);
338 ret |= h->dev->ops->set_loopback(h,
339 MAC_INTERNALLOOP_SERDES, 0x0);
350 static int __lb_up(struct net_device *ndev,
351 enum hnae_loop loop_mode)
353 struct hns_nic_priv *priv = netdev_priv(ndev);
354 struct hnae_handle *h = priv->ae_handle;
358 hns_nic_net_reset(ndev);
361 phy_disconnect(priv->phy);
364 ret = hns_nic_init_phy(ndev, h);
369 ret = __lb_setup(ndev, loop_mode);
375 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
380 phy_start(priv->phy);
382 /* link adjust duplex*/
383 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
389 h->dev->ops->adjust_link(h, speed, duplex);
394 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
397 struct net_device *ndev;
398 struct hnae_ring *ring;
399 struct netdev_queue *dev_queue;
400 struct sk_buff *new_skb;
401 unsigned int frame_size;
404 char buff[33]; /* 32B data and the last character '\0' */
406 if (!ring_data) { /* Just for doing create frame*/
407 frame_size = skb->len;
408 memset(skb->data, 0xFF, frame_size);
410 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
411 memset(&skb->data[frame_size / 2 + 10], 0xBE,
412 frame_size / 2 - 11);
413 memset(&skb->data[frame_size / 2 + 12], 0xAF,
414 frame_size / 2 - 13);
418 ring = ring_data->ring;
419 ndev = ring_data->napi.dev;
420 if (is_tx_ring(ring)) { /* for tx queue reset*/
421 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
422 netdev_tx_reset_queue(dev_queue);
426 frame_size = skb->len;
429 new_skb = skb_copy(skb, GFP_ATOMIC);
430 dev_kfree_skb_any(skb);
434 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
435 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
436 (*(skb->data + frame_size / 2 + 12) == 0xAF))
441 ndev->stats.rx_packets++;
442 ndev->stats.rx_bytes += skb->len;
444 ndev->stats.rx_frame_errors++;
445 for (i = 0; i < skb->len; i++) {
446 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
447 "%02x", *(skb->data + i));
448 if ((i % 16 == 15) || (i == skb->len - 1))
449 pr_info("%s\n", buff);
452 dev_kfree_skb_any(skb);
455 static int __lb_clean_rings(struct hns_nic_priv *priv,
456 int ringid0, int ringid1, int budget)
459 struct hns_nic_ring_data *ring_data;
460 struct net_device *ndev = priv->netdev;
461 unsigned long rx_packets = ndev->stats.rx_packets;
462 unsigned long rx_bytes = ndev->stats.rx_bytes;
463 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
465 for (i = ringid0; i <= ringid1; i++) {
466 ring_data = &priv->ring_data[i];
467 (void)ring_data->poll_one(ring_data,
468 budget, __lb_other_process);
470 ret = (int)(ndev->stats.rx_packets - rx_packets);
471 ndev->stats.rx_packets = rx_packets;
472 ndev->stats.rx_bytes = rx_bytes;
473 ndev->stats.rx_frame_errors = rx_frame_errors;
478 * nic_run_loopback_test - run loopback test
479 * @nic_dev: net device
480 * @loopback_type: loopback type
482 static int __lb_run_test(struct net_device *ndev,
483 enum hnae_loop loop_mode)
485 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
486 #define NIC_LB_TEST_RING_ID 0
487 #define NIC_LB_TEST_FRAME_SIZE 128
488 /* nic loopback test err */
489 #define NIC_LB_TEST_NO_MEM_ERR 1
490 #define NIC_LB_TEST_TX_CNT_ERR 2
491 #define NIC_LB_TEST_RX_CNT_ERR 3
492 #define NIC_LB_TEST_RX_PKG_ERR 4
493 struct hns_nic_priv *priv = netdev_priv(ndev);
494 struct hnae_handle *h = priv->ae_handle;
495 int i, j, lc, good_cnt, ret_val = 0;
497 netdev_tx_t tx_ret_val;
500 size = NIC_LB_TEST_FRAME_SIZE;
501 /* allocate test skb */
502 skb = alloc_skb(size, GFP_KERNEL);
504 return NIC_LB_TEST_NO_MEM_ERR;
506 /* place data into test skb */
507 (void)skb_put(skb, size);
508 __lb_other_process(NULL, skb);
509 skb->queue_mapping = NIC_LB_TEST_RING_ID;
512 for (j = 0; j < lc; j++) {
513 /* reset count of good packets */
515 /* place 64 packets on the transmit queue*/
516 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
519 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
521 &tx_ring_data(priv, skb->queue_mapping));
522 if (tx_ret_val == NETDEV_TX_OK)
527 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
528 ret_val = NIC_LB_TEST_TX_CNT_ERR;
529 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
530 hns_nic_test_strs[loop_mode], good_cnt,
531 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
535 /* allow 100 milliseconds for packets to go from Tx to Rx */
538 good_cnt = __lb_clean_rings(priv,
539 h->q_num, h->q_num * 2 - 1,
540 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
541 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
542 ret_val = NIC_LB_TEST_RX_CNT_ERR;
543 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
544 hns_nic_test_strs[loop_mode], good_cnt,
545 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
548 (void)__lb_clean_rings(priv,
549 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
550 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
553 /* free the original skb */
559 static int __lb_down(struct net_device *ndev)
561 struct hns_nic_priv *priv = netdev_priv(ndev);
562 struct hnae_handle *h = priv->ae_handle;
565 ret = __lb_setup(ndev, MAC_LOOP_NONE);
567 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
574 if (h->dev->ops->stop)
575 h->dev->ops->stop(h);
577 usleep_range(10000, 20000);
578 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
580 hns_nic_net_reset(ndev);
586 * hns_nic_self_test - self test
588 * @eth_test: test cmd
591 static void hns_nic_self_test(struct net_device *ndev,
592 struct ethtool_test *eth_test, u64 *data)
594 struct hns_nic_priv *priv = netdev_priv(ndev);
595 bool if_running = netif_running(ndev);
596 #define SELF_TEST_TPYE_NUM 3
597 int st_param[SELF_TEST_TPYE_NUM][2];
601 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
602 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
603 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
604 st_param[1][1] = 1; /*serdes must exist*/
605 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
606 st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
607 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
609 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
610 set_bit(NIC_STATE_TESTING, &priv->state);
613 (void)dev_close(ndev);
615 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
617 continue; /* NEXT testing */
619 data[test_index] = __lb_up(ndev,
620 (enum hnae_loop)st_param[i][0]);
621 if (!data[test_index]) {
622 data[test_index] = __lb_run_test(
623 ndev, (enum hnae_loop)st_param[i][0]);
624 (void)__lb_down(ndev);
627 if (data[test_index])
628 eth_test->flags |= ETH_TEST_FL_FAILED;
633 hns_nic_net_reset(priv->netdev);
635 clear_bit(NIC_STATE_TESTING, &priv->state);
638 (void)dev_open(ndev);
640 /* Online tests aren't run; pass by default */
642 (void)msleep_interruptible(4 * 1000);
646 * hns_nic_get_drvinfo - get net driver info
648 * @drvinfo: driver info
650 static void hns_nic_get_drvinfo(struct net_device *net_dev,
651 struct ethtool_drvinfo *drvinfo)
653 struct hns_nic_priv *priv = netdev_priv(net_dev);
657 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
658 sizeof(drvinfo->version));
659 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
661 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
662 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
664 strncpy(drvinfo->bus_info, priv->dev->bus->name,
665 sizeof(drvinfo->bus_info));
666 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
668 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
669 drvinfo->eedump_len = 0;
673 * hns_get_ringparam - get ring parameter
675 * @param: ethtool parameter
677 void hns_get_ringparam(struct net_device *net_dev,
678 struct ethtool_ringparam *param)
680 struct hns_nic_priv *priv = netdev_priv(net_dev);
681 struct hnae_ae_ops *ops;
682 struct hnae_queue *queue;
685 queue = priv->ae_handle->qs[0];
686 ops = priv->ae_handle->dev->ops;
688 if (ops->get_ring_bdnum_limit)
689 ops->get_ring_bdnum_limit(queue, &uplimit);
691 param->rx_max_pending = uplimit;
692 param->tx_max_pending = uplimit;
693 param->rx_pending = queue->rx_ring.desc_num;
694 param->tx_pending = queue->tx_ring.desc_num;
698 * hns_get_pauseparam - get pause parameter
700 * @param: pause parameter
702 static void hns_get_pauseparam(struct net_device *net_dev,
703 struct ethtool_pauseparam *param)
705 struct hns_nic_priv *priv = netdev_priv(net_dev);
706 struct hnae_ae_ops *ops;
708 ops = priv->ae_handle->dev->ops;
710 if (ops->get_pauseparam)
711 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
712 ¶m->rx_pause, ¶m->tx_pause);
716 * hns_set_pauseparam - set pause parameter
718 * @param: pause parameter
720 * Return 0 on success, negative on failure
722 static int hns_set_pauseparam(struct net_device *net_dev,
723 struct ethtool_pauseparam *param)
725 struct hns_nic_priv *priv = netdev_priv(net_dev);
726 struct hnae_handle *h;
727 struct hnae_ae_ops *ops;
729 assert(priv || priv->ae_handle);
734 if (!ops->set_pauseparam)
737 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
738 param->rx_pause, param->tx_pause);
742 * hns_get_coalesce - get coalesce info.
744 * @ec: coalesce info.
746 * Return 0 on success, negative on failure.
748 static int hns_get_coalesce(struct net_device *net_dev,
749 struct ethtool_coalesce *ec)
751 struct hns_nic_priv *priv = netdev_priv(net_dev);
752 struct hnae_ae_ops *ops;
754 ops = priv->ae_handle->dev->ops;
756 ec->use_adaptive_rx_coalesce = 1;
757 ec->use_adaptive_tx_coalesce = 1;
759 if ((!ops->get_coalesce_usecs) ||
760 (!ops->get_rx_max_coalesced_frames))
763 ops->get_coalesce_usecs(priv->ae_handle,
764 &ec->tx_coalesce_usecs,
765 &ec->rx_coalesce_usecs);
767 ops->get_rx_max_coalesced_frames(
769 &ec->tx_max_coalesced_frames,
770 &ec->rx_max_coalesced_frames);
776 * hns_set_coalesce - set coalesce info.
778 * @ec: coalesce info.
780 * Return 0 on success, negative on failure.
782 static int hns_set_coalesce(struct net_device *net_dev,
783 struct ethtool_coalesce *ec)
785 struct hns_nic_priv *priv = netdev_priv(net_dev);
786 struct hnae_ae_ops *ops;
789 assert(priv || priv->ae_handle);
791 ops = priv->ae_handle->dev->ops;
793 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
796 if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
799 if ((!ops->set_coalesce_usecs) ||
800 (!ops->set_coalesce_frames))
803 ops->set_coalesce_usecs(priv->ae_handle,
804 ec->rx_coalesce_usecs);
806 ret = ops->set_coalesce_frames(
808 ec->rx_max_coalesced_frames);
814 * hns_get_channels - get channel info.
818 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
820 struct hns_nic_priv *priv = netdev_priv(net_dev);
822 ch->max_rx = priv->ae_handle->q_num;
823 ch->max_tx = priv->ae_handle->q_num;
825 ch->rx_count = priv->ae_handle->q_num;
826 ch->tx_count = priv->ae_handle->q_num;
830 * get_ethtool_stats - get detail statistics.
832 * @stats: statistics info.
833 * @data: statistics data.
835 void hns_get_ethtool_stats(struct net_device *netdev,
836 struct ethtool_stats *stats, u64 *data)
839 struct hns_nic_priv *priv = netdev_priv(netdev);
840 struct hnae_handle *h = priv->ae_handle;
841 const struct rtnl_link_stats64 *net_stats;
842 struct rtnl_link_stats64 temp;
844 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
845 netdev_err(netdev, "get_stats or update_stats is null!\n");
849 h->dev->ops->update_stats(h, &netdev->stats);
851 net_stats = dev_get_stats(netdev, &temp);
853 /* get netdev statistics */
854 p[0] = net_stats->rx_packets;
855 p[1] = net_stats->tx_packets;
856 p[2] = net_stats->rx_bytes;
857 p[3] = net_stats->tx_bytes;
858 p[4] = net_stats->rx_errors;
859 p[5] = net_stats->tx_errors;
860 p[6] = net_stats->rx_dropped;
861 p[7] = net_stats->tx_dropped;
862 p[8] = net_stats->multicast;
863 p[9] = net_stats->collisions;
864 p[10] = net_stats->rx_over_errors;
865 p[11] = net_stats->rx_crc_errors;
866 p[12] = net_stats->rx_frame_errors;
867 p[13] = net_stats->rx_fifo_errors;
868 p[14] = net_stats->rx_missed_errors;
869 p[15] = net_stats->tx_aborted_errors;
870 p[16] = net_stats->tx_carrier_errors;
871 p[17] = net_stats->tx_fifo_errors;
872 p[18] = net_stats->tx_heartbeat_errors;
873 p[19] = net_stats->rx_length_errors;
874 p[20] = net_stats->tx_window_errors;
875 p[21] = net_stats->rx_compressed;
876 p[22] = net_stats->tx_compressed;
878 p[23] = netdev->rx_dropped.counter;
879 p[24] = netdev->tx_dropped.counter;
881 p[25] = priv->tx_timeout_count;
883 /* get driver statistics */
884 h->dev->ops->get_stats(h, &p[26]);
888 * get_strings: Return a set of strings that describe the requested objects
890 * @stats: string set ID.
891 * @data: objects data.
893 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
895 struct hns_nic_priv *priv = netdev_priv(netdev);
896 struct hnae_handle *h = priv->ae_handle;
897 char *buff = (char *)data;
899 if (!h->dev->ops->get_strings) {
900 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
904 if (stringset == ETH_SS_TEST) {
905 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
906 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
908 buff += ETH_GSTRING_LEN;
910 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
912 buff += ETH_GSTRING_LEN;
913 if ((priv->phy) && (!priv->phy->is_c45))
914 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
918 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
919 buff = buff + ETH_GSTRING_LEN;
920 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
921 buff = buff + ETH_GSTRING_LEN;
922 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
923 buff = buff + ETH_GSTRING_LEN;
924 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
925 buff = buff + ETH_GSTRING_LEN;
926 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
927 buff = buff + ETH_GSTRING_LEN;
928 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
929 buff = buff + ETH_GSTRING_LEN;
930 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
931 buff = buff + ETH_GSTRING_LEN;
932 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
933 buff = buff + ETH_GSTRING_LEN;
934 snprintf(buff, ETH_GSTRING_LEN, "multicast");
935 buff = buff + ETH_GSTRING_LEN;
936 snprintf(buff, ETH_GSTRING_LEN, "collisions");
937 buff = buff + ETH_GSTRING_LEN;
938 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
939 buff = buff + ETH_GSTRING_LEN;
940 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
941 buff = buff + ETH_GSTRING_LEN;
942 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
943 buff = buff + ETH_GSTRING_LEN;
944 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
945 buff = buff + ETH_GSTRING_LEN;
946 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
947 buff = buff + ETH_GSTRING_LEN;
948 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
949 buff = buff + ETH_GSTRING_LEN;
950 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
951 buff = buff + ETH_GSTRING_LEN;
952 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
953 buff = buff + ETH_GSTRING_LEN;
954 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
955 buff = buff + ETH_GSTRING_LEN;
956 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
957 buff = buff + ETH_GSTRING_LEN;
958 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
959 buff = buff + ETH_GSTRING_LEN;
960 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
961 buff = buff + ETH_GSTRING_LEN;
962 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
963 buff = buff + ETH_GSTRING_LEN;
964 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
965 buff = buff + ETH_GSTRING_LEN;
966 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
967 buff = buff + ETH_GSTRING_LEN;
969 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
970 buff = buff + ETH_GSTRING_LEN;
972 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
977 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
979 * @stringset: string set index, 0: self test string; 1: statistics string.
981 * Return string set count.
983 int hns_get_sset_count(struct net_device *netdev, int stringset)
985 struct hns_nic_priv *priv = netdev_priv(netdev);
986 struct hnae_handle *h = priv->ae_handle;
987 struct hnae_ae_ops *ops = h->dev->ops;
989 if (!ops->get_sset_count) {
990 netdev_err(netdev, "get_sset_count is null!\n");
993 if (stringset == ETH_SS_TEST) {
994 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
996 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
999 if ((!priv->phy) || (priv->phy->is_c45))
1004 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1009 * hns_phy_led_set - set phy LED status.
1011 * @value: LED state.
1013 * Return 0 on success, negative on failure.
1015 int hns_phy_led_set(struct net_device *netdev, int value)
1018 struct hns_nic_priv *priv = netdev_priv(netdev);
1019 struct phy_device *phy_dev = priv->phy;
1021 if (!phy_dev->bus) {
1022 netdev_err(netdev, "phy_dev->bus is null!\n");
1025 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1026 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1027 retval = mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_LED_FC_REG,
1029 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1030 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1032 netdev_err(netdev, "mdiobus_write fail !\n");
1039 * nic_set_phys_id - set phy identify LED.
1041 * @state: LED state.
1043 * Return 0 on success, negative on failure.
1045 int 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 = priv->phy;
1054 case ETHTOOL_ID_ACTIVE:
1055 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1061 priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
1065 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1067 HNS_PHY_PAGE_COPPER);
1072 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1076 case ETHTOOL_ID_OFF:
1077 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1081 case ETHTOOL_ID_INACTIVE:
1082 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1088 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1089 HNS_LED_FC_REG, priv->phy_led_val);
1093 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1095 HNS_PHY_PAGE_COPPER);
1104 case ETHTOOL_ID_ACTIVE:
1105 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1107 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1108 case ETHTOOL_ID_OFF:
1109 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1110 case ETHTOOL_ID_INACTIVE:
1111 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1120 * hns_get_regs - get net device register
1123 * @date: register data
1125 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1128 struct hns_nic_priv *priv = netdev_priv(net_dev);
1129 struct hnae_ae_ops *ops;
1131 assert(priv || priv->ae_handle);
1133 ops = priv->ae_handle->dev->ops;
1135 cmd->version = HNS_CHIP_VERSION;
1136 if (!ops->get_regs) {
1137 netdev_err(net_dev, "ops->get_regs is null!\n");
1140 ops->get_regs(priv->ae_handle, data);
1144 * nic_get_regs_len - get total register len.
1147 * Return total register len.
1149 static int hns_get_regs_len(struct net_device *net_dev)
1152 struct hns_nic_priv *priv = netdev_priv(net_dev);
1153 struct hnae_ae_ops *ops;
1155 assert(priv || priv->ae_handle);
1157 ops = priv->ae_handle->dev->ops;
1158 if (!ops->get_regs_len) {
1159 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1163 reg_num = ops->get_regs_len(priv->ae_handle);
1165 return reg_num * sizeof(u32);
1167 return reg_num; /* error code */
1171 * hns_nic_nway_reset - nway reset
1174 * Return 0 on success, negative on failure
1176 static int hns_nic_nway_reset(struct net_device *netdev)
1179 struct hns_nic_priv *priv = netdev_priv(netdev);
1180 struct phy_device *phy = priv->phy;
1182 if (netif_running(netdev)) {
1184 ret = genphy_restart_aneg(phy);
1191 hns_get_rss_key_size(struct net_device *netdev)
1193 struct hns_nic_priv *priv = netdev_priv(netdev);
1194 struct hnae_ae_ops *ops;
1197 if (AE_IS_VER1(priv->enet_ver)) {
1199 "RSS feature is not supported on this hardware\n");
1203 ops = priv->ae_handle->dev->ops;
1204 ret = ops->get_rss_key_size(priv->ae_handle);
1210 hns_get_rss_indir_size(struct net_device *netdev)
1212 struct hns_nic_priv *priv = netdev_priv(netdev);
1213 struct hnae_ae_ops *ops;
1216 if (AE_IS_VER1(priv->enet_ver)) {
1218 "RSS feature is not supported on this hardware\n");
1222 ops = priv->ae_handle->dev->ops;
1223 ret = ops->get_rss_indir_size(priv->ae_handle);
1229 hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1231 struct hns_nic_priv *priv = netdev_priv(netdev);
1232 struct hnae_ae_ops *ops;
1235 if (AE_IS_VER1(priv->enet_ver)) {
1237 "RSS feature is not supported on this hardware\n");
1241 ops = priv->ae_handle->dev->ops;
1246 ret = ops->get_rss(priv->ae_handle, indir, key, hfunc);
1252 hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1255 struct hns_nic_priv *priv = netdev_priv(netdev);
1256 struct hnae_ae_ops *ops;
1259 if (AE_IS_VER1(priv->enet_ver)) {
1261 "RSS feature is not supported on this hardware\n");
1265 ops = priv->ae_handle->dev->ops;
1267 /* currently hfunc can only be Toeplitz hash */
1269 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1274 ret = ops->set_rss(priv->ae_handle, indir, key, hfunc);
1279 static struct ethtool_ops hns_ethtool_ops = {
1280 .get_drvinfo = hns_nic_get_drvinfo,
1281 .get_link = hns_nic_get_link,
1282 .get_settings = hns_nic_get_settings,
1283 .set_settings = hns_nic_set_settings,
1284 .get_ringparam = hns_get_ringparam,
1285 .get_pauseparam = hns_get_pauseparam,
1286 .set_pauseparam = hns_set_pauseparam,
1287 .get_coalesce = hns_get_coalesce,
1288 .set_coalesce = hns_set_coalesce,
1289 .get_channels = hns_get_channels,
1290 .self_test = hns_nic_self_test,
1291 .get_strings = hns_get_strings,
1292 .get_sset_count = hns_get_sset_count,
1293 .get_ethtool_stats = hns_get_ethtool_stats,
1294 .set_phys_id = hns_set_phys_id,
1295 .get_regs_len = hns_get_regs_len,
1296 .get_regs = hns_get_regs,
1297 .nway_reset = hns_nic_nway_reset,
1298 .get_rxfh_key_size = hns_get_rss_key_size,
1299 .get_rxfh_indir_size = hns_get_rss_indir_size,
1300 .get_rxfh = hns_get_rss,
1301 .set_rxfh = hns_set_rss,
1304 void hns_ethtool_set_ops(struct net_device *ndev)
1306 ndev->ethtool_ops = &hns_ethtool_ops;