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>
17 #define HNS_PHY_PAGE_MDIX 0
18 #define HNS_PHY_PAGE_LED 3
19 #define HNS_PHY_PAGE_COPPER 0
21 #define HNS_PHY_PAGE_REG 22 /* Page Selection Reg. */
22 #define HNS_PHY_CSC_REG 16 /* Copper Specific Control Register */
23 #define HNS_PHY_CSS_REG 17 /* Copper Specific Status Register */
24 #define HNS_LED_FC_REG 16 /* LED Function Control Reg. */
25 #define HNS_LED_PC_REG 17 /* LED Polarity Control Reg. */
27 #define HNS_LED_FORCE_ON 9
28 #define HNS_LED_FORCE_OFF 8
30 #define HNS_CHIP_VERSION 660
31 #define HNS_NET_STATS_CNT 26
33 #define PHY_MDIX_CTRL_S (5)
34 #define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
36 #define PHY_MDIX_STATUS_B (6)
37 #define PHY_SPEED_DUP_RESOLVE_B (11)
40 *hns_nic_get_link - get current link status
42 *retuen 0 - success , negative --fail
44 static u32 hns_nic_get_link(struct net_device *net_dev)
46 struct hns_nic_priv *priv = netdev_priv(net_dev);
47 u32 link_stat = priv->link;
48 struct hnae_handle *h;
50 assert(priv && priv->ae_handle);
54 if (!genphy_update_link(priv->phy))
55 link_stat = priv->phy->link;
60 if (h->dev && h->dev->ops && h->dev->ops->get_status)
61 link_stat = link_stat && h->dev->ops->get_status(h);
68 static void hns_get_mdix_mode(struct net_device *net_dev,
69 struct ethtool_cmd *cmd)
71 int mdix_ctrl, mdix, retval, is_resolved;
72 struct hns_nic_priv *priv = netdev_priv(net_dev);
73 struct phy_device *phy_dev = priv->phy;
75 if (!phy_dev || !phy_dev->bus) {
76 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
77 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
81 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
84 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSC_REG);
85 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
87 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSS_REG);
88 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
89 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
91 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
96 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI;
99 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_X;
102 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
105 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
110 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
112 cmd->eth_tp_mdix = ETH_TP_MDI_X;
114 cmd->eth_tp_mdix = ETH_TP_MDI;
118 *hns_nic_get_settings - implement ethtool get settings
119 *@net_dev: net_device
121 *retuen 0 - success , negative --fail
123 static int hns_nic_get_settings(struct net_device *net_dev,
124 struct ethtool_cmd *cmd)
126 struct hns_nic_priv *priv = netdev_priv(net_dev);
127 struct hnae_handle *h;
133 if (!priv || !priv->ae_handle)
137 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
140 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
142 netdev_err(net_dev, "%s get_info error!\n", __func__);
146 /* When there is no phy, autoneg is off. */
147 cmd->autoneg = false;
148 ethtool_cmd_speed_set(cmd, speed);
149 cmd->duplex = duplex;
152 (void)phy_ethtool_gset(priv->phy, cmd);
154 link_stat = hns_nic_get_link(net_dev);
156 ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
157 cmd->duplex = DUPLEX_UNKNOWN;
161 cmd->advertising |= ADVERTISED_Autoneg;
163 cmd->supported |= h->if_support;
164 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
165 cmd->supported |= SUPPORTED_TP;
166 cmd->advertising |= ADVERTISED_1000baseT_Full;
167 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
168 cmd->supported |= SUPPORTED_FIBRE;
169 cmd->advertising |= ADVERTISED_10000baseKR_Full;
172 if (h->port_type == HNAE_PORT_SERVICE) {
173 cmd->port = PORT_FIBRE;
174 cmd->supported |= SUPPORTED_Pause;
179 cmd->transceiver = XCVR_EXTERNAL;
180 cmd->mdio_support = (ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22);
181 hns_get_mdix_mode(net_dev, cmd);
187 *hns_nic_set_settings - implement ethtool set settings
188 *@net_dev: net_device
190 *retuen 0 - success , negative --fail
192 static int hns_nic_set_settings(struct net_device *net_dev,
193 struct ethtool_cmd *cmd)
195 struct hns_nic_priv *priv = netdev_priv(net_dev);
196 struct hnae_handle *h;
201 if (!netif_running(net_dev))
204 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
205 !priv->ae_handle->dev->ops)
209 link_stat = hns_nic_get_link(net_dev);
210 duplex = cmd->duplex;
211 speed = ethtool_cmd_speed(cmd);
212 autoneg = cmd->autoneg;
215 if (duplex != (u8)DUPLEX_UNKNOWN || speed != (u32)SPEED_UNKNOWN)
218 if (h->phy_if == PHY_INTERFACE_MODE_SGMII && h->phy_node) {
219 priv->phy->autoneg = autoneg;
220 return phy_start_aneg(priv->phy);
224 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
225 if (autoneg != AUTONEG_DISABLE)
228 if (speed != SPEED_10000 || duplex != DUPLEX_FULL)
230 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
231 if (!h->phy_node && autoneg != AUTONEG_DISABLE)
234 if (speed == SPEED_1000 && duplex == DUPLEX_HALF)
237 if (speed != SPEED_10 && speed != SPEED_100 &&
241 netdev_err(net_dev, "Not supported!");
246 return phy_ethtool_sset(priv->phy, cmd);
247 } else if (h->dev->ops->adjust_link && link_stat) {
248 h->dev->ops->adjust_link(h, speed, duplex);
251 netdev_err(net_dev, "Not supported!");
255 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
257 "Serdes Loopback test",
261 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
263 #define COPPER_CONTROL_REG 0
264 #define PHY_LOOP_BACK BIT(14)
267 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
272 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
273 HNS_PHY_PAGE_REG, 2);
274 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
277 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
280 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
282 /* If autoneg disabled,two soft-reset operations */
283 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
285 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
288 /* Default is 0x0400 */
289 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
292 /* Force 1000M Link, Default is 0x0200 */
293 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
295 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
298 /* Enable MAC loop-back */
299 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
301 val |= PHY_LOOP_BACK;
302 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
303 COPPER_CONTROL_REG, val);
305 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
307 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
309 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
311 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
314 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
316 val &= ~PHY_LOOP_BACK;
317 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
318 COPPER_CONTROL_REG, val);
323 static int __lb_setup(struct net_device *ndev,
327 struct hns_nic_priv *priv = netdev_priv(ndev);
328 struct phy_device *phy_dev = priv->phy;
329 struct hnae_handle *h = priv->ae_handle;
332 case MAC_INTERNALLOOP_PHY:
333 if ((phy_dev) && (!phy_dev->is_c45))
334 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
336 case MAC_INTERNALLOOP_MAC:
337 if ((h->dev->ops->set_loopback) &&
338 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
339 ret = h->dev->ops->set_loopback(h, loop, 0x1);
341 case MAC_INTERNALLOOP_SERDES:
342 if (h->dev->ops->set_loopback)
343 ret = h->dev->ops->set_loopback(h, loop, 0x1);
346 if ((phy_dev) && (!phy_dev->is_c45))
347 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
349 if (h->dev->ops->set_loopback) {
350 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
351 ret |= h->dev->ops->set_loopback(h,
352 MAC_INTERNALLOOP_MAC, 0x0);
354 ret |= h->dev->ops->set_loopback(h,
355 MAC_INTERNALLOOP_SERDES, 0x0);
366 static int __lb_up(struct net_device *ndev,
367 enum hnae_loop loop_mode)
369 struct hns_nic_priv *priv = netdev_priv(ndev);
370 struct hnae_handle *h = priv->ae_handle;
374 hns_nic_net_reset(ndev);
377 phy_disconnect(priv->phy);
380 ret = hns_nic_init_phy(ndev, h);
385 ret = __lb_setup(ndev, loop_mode);
391 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
396 phy_start(priv->phy);
398 /* link adjust duplex*/
399 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
405 h->dev->ops->adjust_link(h, speed, duplex);
410 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
413 struct net_device *ndev;
414 struct hnae_ring *ring;
415 struct netdev_queue *dev_queue;
416 struct sk_buff *new_skb;
417 unsigned int frame_size;
420 char buff[33]; /* 32B data and the last character '\0' */
422 if (!ring_data) { /* Just for doing create frame*/
423 frame_size = skb->len;
424 memset(skb->data, 0xFF, frame_size);
426 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
427 memset(&skb->data[frame_size / 2 + 10], 0xBE,
428 frame_size / 2 - 11);
429 memset(&skb->data[frame_size / 2 + 12], 0xAF,
430 frame_size / 2 - 13);
434 ring = ring_data->ring;
435 ndev = ring_data->napi.dev;
436 if (is_tx_ring(ring)) { /* for tx queue reset*/
437 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
438 netdev_tx_reset_queue(dev_queue);
442 frame_size = skb->len;
445 new_skb = skb_copy(skb, GFP_ATOMIC);
446 dev_kfree_skb_any(skb);
450 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
451 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
452 (*(skb->data + frame_size / 2 + 12) == 0xAF))
457 ndev->stats.rx_packets++;
458 ndev->stats.rx_bytes += skb->len;
460 ndev->stats.rx_frame_errors++;
461 for (i = 0; i < skb->len; i++) {
462 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
463 "%02x", *(skb->data + i));
464 if ((i % 16 == 15) || (i == skb->len - 1))
465 pr_info("%s\n", buff);
468 dev_kfree_skb_any(skb);
471 static int __lb_clean_rings(struct hns_nic_priv *priv,
472 int ringid0, int ringid1, int budget)
475 struct hns_nic_ring_data *ring_data;
476 struct net_device *ndev = priv->netdev;
477 unsigned long rx_packets = ndev->stats.rx_packets;
478 unsigned long rx_bytes = ndev->stats.rx_bytes;
479 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
481 for (i = ringid0; i <= ringid1; i++) {
482 ring_data = &priv->ring_data[i];
483 (void)ring_data->poll_one(ring_data,
484 budget, __lb_other_process);
486 ret = (int)(ndev->stats.rx_packets - rx_packets);
487 ndev->stats.rx_packets = rx_packets;
488 ndev->stats.rx_bytes = rx_bytes;
489 ndev->stats.rx_frame_errors = rx_frame_errors;
494 * nic_run_loopback_test - run loopback test
495 * @nic_dev: net device
496 * @loopback_type: loopback type
498 static int __lb_run_test(struct net_device *ndev,
499 enum hnae_loop loop_mode)
501 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
502 #define NIC_LB_TEST_RING_ID 0
503 #define NIC_LB_TEST_FRAME_SIZE 128
504 /* nic loopback test err */
505 #define NIC_LB_TEST_NO_MEM_ERR 1
506 #define NIC_LB_TEST_TX_CNT_ERR 2
507 #define NIC_LB_TEST_RX_CNT_ERR 3
508 #define NIC_LB_TEST_RX_PKG_ERR 4
509 struct hns_nic_priv *priv = netdev_priv(ndev);
510 struct hnae_handle *h = priv->ae_handle;
511 int i, j, lc, good_cnt, ret_val = 0;
513 netdev_tx_t tx_ret_val;
516 size = NIC_LB_TEST_FRAME_SIZE;
517 /* allocate test skb */
518 skb = alloc_skb(size, GFP_KERNEL);
520 return NIC_LB_TEST_NO_MEM_ERR;
522 /* place data into test skb */
523 (void)skb_put(skb, size);
524 __lb_other_process(NULL, skb);
525 skb->queue_mapping = NIC_LB_TEST_RING_ID;
528 for (j = 0; j < lc; j++) {
529 /* reset count of good packets */
531 /* place 64 packets on the transmit queue*/
532 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
535 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
537 &tx_ring_data(priv, skb->queue_mapping));
538 if (tx_ret_val == NETDEV_TX_OK)
543 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
544 ret_val = NIC_LB_TEST_TX_CNT_ERR;
545 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
546 hns_nic_test_strs[loop_mode], good_cnt,
547 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
551 /* allow 100 milliseconds for packets to go from Tx to Rx */
554 good_cnt = __lb_clean_rings(priv,
555 h->q_num, h->q_num * 2 - 1,
556 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
557 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
558 ret_val = NIC_LB_TEST_RX_CNT_ERR;
559 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
560 hns_nic_test_strs[loop_mode], good_cnt,
561 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
564 (void)__lb_clean_rings(priv,
565 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
566 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
569 /* free the original skb */
575 static int __lb_down(struct net_device *ndev)
577 struct hns_nic_priv *priv = netdev_priv(ndev);
578 struct hnae_handle *h = priv->ae_handle;
581 ret = __lb_setup(ndev, MAC_LOOP_NONE);
583 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
590 if (h->dev->ops->stop)
591 h->dev->ops->stop(h);
593 usleep_range(10000, 20000);
594 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
596 hns_nic_net_reset(ndev);
602 * hns_nic_self_test - self test
604 * @eth_test: test cmd
607 static void hns_nic_self_test(struct net_device *ndev,
608 struct ethtool_test *eth_test, u64 *data)
610 struct hns_nic_priv *priv = netdev_priv(ndev);
611 bool if_running = netif_running(ndev);
612 #define SELF_TEST_TPYE_NUM 3
613 int st_param[SELF_TEST_TPYE_NUM][2];
617 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
618 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
619 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
620 st_param[1][1] = 1; /*serdes must exist*/
621 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
622 st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
623 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
625 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
626 set_bit(NIC_STATE_TESTING, &priv->state);
629 (void)dev_close(ndev);
631 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
633 continue; /* NEXT testing */
635 data[test_index] = __lb_up(ndev,
636 (enum hnae_loop)st_param[i][0]);
637 if (!data[test_index]) {
638 data[test_index] = __lb_run_test(
639 ndev, (enum hnae_loop)st_param[i][0]);
640 (void)__lb_down(ndev);
643 if (data[test_index])
644 eth_test->flags |= ETH_TEST_FL_FAILED;
649 hns_nic_net_reset(priv->netdev);
651 clear_bit(NIC_STATE_TESTING, &priv->state);
654 (void)dev_open(ndev);
656 /* Online tests aren't run; pass by default */
658 (void)msleep_interruptible(4 * 1000);
662 * hns_nic_get_drvinfo - get net driver info
664 * @drvinfo: driver info
666 static void hns_nic_get_drvinfo(struct net_device *net_dev,
667 struct ethtool_drvinfo *drvinfo)
669 struct hns_nic_priv *priv = netdev_priv(net_dev);
673 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
674 sizeof(drvinfo->version));
675 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
677 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
678 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
680 strncpy(drvinfo->bus_info, priv->dev->bus->name,
681 sizeof(drvinfo->bus_info));
682 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
684 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
688 * hns_get_ringparam - get ring parameter
690 * @param: ethtool parameter
692 void hns_get_ringparam(struct net_device *net_dev,
693 struct ethtool_ringparam *param)
695 struct hns_nic_priv *priv = netdev_priv(net_dev);
696 struct hnae_ae_ops *ops;
697 struct hnae_queue *queue;
700 queue = priv->ae_handle->qs[0];
701 ops = priv->ae_handle->dev->ops;
703 if (ops->get_ring_bdnum_limit)
704 ops->get_ring_bdnum_limit(queue, &uplimit);
706 param->rx_max_pending = uplimit;
707 param->tx_max_pending = uplimit;
708 param->rx_pending = queue->rx_ring.desc_num;
709 param->tx_pending = queue->tx_ring.desc_num;
713 * hns_get_pauseparam - get pause parameter
715 * @param: pause parameter
717 static void hns_get_pauseparam(struct net_device *net_dev,
718 struct ethtool_pauseparam *param)
720 struct hns_nic_priv *priv = netdev_priv(net_dev);
721 struct hnae_ae_ops *ops;
723 ops = priv->ae_handle->dev->ops;
725 if (ops->get_pauseparam)
726 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
727 ¶m->rx_pause, ¶m->tx_pause);
731 * hns_set_pauseparam - set pause parameter
733 * @param: pause parameter
735 * Return 0 on success, negative on failure
737 static int hns_set_pauseparam(struct net_device *net_dev,
738 struct ethtool_pauseparam *param)
740 struct hns_nic_priv *priv = netdev_priv(net_dev);
741 struct hnae_handle *h;
742 struct hnae_ae_ops *ops;
744 assert(priv || priv->ae_handle);
749 if (!ops->set_pauseparam)
752 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
753 param->rx_pause, param->tx_pause);
757 * hns_get_coalesce - get coalesce info.
759 * @ec: coalesce info.
761 * Return 0 on success, negative on failure.
763 static int hns_get_coalesce(struct net_device *net_dev,
764 struct ethtool_coalesce *ec)
766 struct hns_nic_priv *priv = netdev_priv(net_dev);
767 struct hnae_ae_ops *ops;
769 ops = priv->ae_handle->dev->ops;
771 ec->use_adaptive_rx_coalesce = 1;
772 ec->use_adaptive_tx_coalesce = 1;
774 if ((!ops->get_coalesce_usecs) ||
775 (!ops->get_rx_max_coalesced_frames))
778 ops->get_coalesce_usecs(priv->ae_handle,
779 &ec->tx_coalesce_usecs,
780 &ec->rx_coalesce_usecs);
782 ops->get_rx_max_coalesced_frames(
784 &ec->tx_max_coalesced_frames,
785 &ec->rx_max_coalesced_frames);
791 * hns_set_coalesce - set coalesce info.
793 * @ec: coalesce info.
795 * Return 0 on success, negative on failure.
797 static int hns_set_coalesce(struct net_device *net_dev,
798 struct ethtool_coalesce *ec)
800 struct hns_nic_priv *priv = netdev_priv(net_dev);
801 struct hnae_ae_ops *ops;
804 assert(priv || priv->ae_handle);
806 ops = priv->ae_handle->dev->ops;
808 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
811 if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
814 if ((!ops->set_coalesce_usecs) ||
815 (!ops->set_coalesce_frames))
818 ops->set_coalesce_usecs(priv->ae_handle,
819 ec->rx_coalesce_usecs);
821 ret = ops->set_coalesce_frames(
823 ec->rx_max_coalesced_frames);
829 * hns_get_channels - get channel info.
833 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
835 struct hns_nic_priv *priv = netdev_priv(net_dev);
837 ch->max_rx = priv->ae_handle->q_num;
838 ch->max_tx = priv->ae_handle->q_num;
840 ch->rx_count = priv->ae_handle->q_num;
841 ch->tx_count = priv->ae_handle->q_num;
845 * get_ethtool_stats - get detail statistics.
847 * @stats: statistics info.
848 * @data: statistics data.
850 void hns_get_ethtool_stats(struct net_device *netdev,
851 struct ethtool_stats *stats, u64 *data)
854 struct hns_nic_priv *priv = netdev_priv(netdev);
855 struct hnae_handle *h = priv->ae_handle;
856 const struct rtnl_link_stats64 *net_stats;
857 struct rtnl_link_stats64 temp;
859 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
860 netdev_err(netdev, "get_stats or update_stats is null!\n");
864 h->dev->ops->update_stats(h, &netdev->stats);
866 net_stats = dev_get_stats(netdev, &temp);
868 /* get netdev statistics */
869 p[0] = net_stats->rx_packets;
870 p[1] = net_stats->tx_packets;
871 p[2] = net_stats->rx_bytes;
872 p[3] = net_stats->tx_bytes;
873 p[4] = net_stats->rx_errors;
874 p[5] = net_stats->tx_errors;
875 p[6] = net_stats->rx_dropped;
876 p[7] = net_stats->tx_dropped;
877 p[8] = net_stats->multicast;
878 p[9] = net_stats->collisions;
879 p[10] = net_stats->rx_over_errors;
880 p[11] = net_stats->rx_crc_errors;
881 p[12] = net_stats->rx_frame_errors;
882 p[13] = net_stats->rx_fifo_errors;
883 p[14] = net_stats->rx_missed_errors;
884 p[15] = net_stats->tx_aborted_errors;
885 p[16] = net_stats->tx_carrier_errors;
886 p[17] = net_stats->tx_fifo_errors;
887 p[18] = net_stats->tx_heartbeat_errors;
888 p[19] = net_stats->rx_length_errors;
889 p[20] = net_stats->tx_window_errors;
890 p[21] = net_stats->rx_compressed;
891 p[22] = net_stats->tx_compressed;
893 p[23] = netdev->rx_dropped.counter;
894 p[24] = netdev->tx_dropped.counter;
896 p[25] = priv->tx_timeout_count;
898 /* get driver statistics */
899 h->dev->ops->get_stats(h, &p[26]);
903 * get_strings: Return a set of strings that describe the requested objects
905 * @stats: string set ID.
906 * @data: objects data.
908 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
910 struct hns_nic_priv *priv = netdev_priv(netdev);
911 struct hnae_handle *h = priv->ae_handle;
912 char *buff = (char *)data;
914 if (!h->dev->ops->get_strings) {
915 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
919 if (stringset == ETH_SS_TEST) {
920 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
921 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
923 buff += ETH_GSTRING_LEN;
925 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
927 buff += ETH_GSTRING_LEN;
928 if ((priv->phy) && (!priv->phy->is_c45))
929 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
933 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
934 buff = buff + ETH_GSTRING_LEN;
935 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
936 buff = buff + ETH_GSTRING_LEN;
937 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
938 buff = buff + ETH_GSTRING_LEN;
939 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
940 buff = buff + ETH_GSTRING_LEN;
941 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
942 buff = buff + ETH_GSTRING_LEN;
943 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
944 buff = buff + ETH_GSTRING_LEN;
945 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
946 buff = buff + ETH_GSTRING_LEN;
947 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
948 buff = buff + ETH_GSTRING_LEN;
949 snprintf(buff, ETH_GSTRING_LEN, "multicast");
950 buff = buff + ETH_GSTRING_LEN;
951 snprintf(buff, ETH_GSTRING_LEN, "collisions");
952 buff = buff + ETH_GSTRING_LEN;
953 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
954 buff = buff + ETH_GSTRING_LEN;
955 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
956 buff = buff + ETH_GSTRING_LEN;
957 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
958 buff = buff + ETH_GSTRING_LEN;
959 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
960 buff = buff + ETH_GSTRING_LEN;
961 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
962 buff = buff + ETH_GSTRING_LEN;
963 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
964 buff = buff + ETH_GSTRING_LEN;
965 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
966 buff = buff + ETH_GSTRING_LEN;
967 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
968 buff = buff + ETH_GSTRING_LEN;
969 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
970 buff = buff + ETH_GSTRING_LEN;
971 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
972 buff = buff + ETH_GSTRING_LEN;
973 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
974 buff = buff + ETH_GSTRING_LEN;
975 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
976 buff = buff + ETH_GSTRING_LEN;
977 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
978 buff = buff + ETH_GSTRING_LEN;
979 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
980 buff = buff + ETH_GSTRING_LEN;
981 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
982 buff = buff + ETH_GSTRING_LEN;
984 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
985 buff = buff + ETH_GSTRING_LEN;
987 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
992 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
994 * @stringset: string set index, 0: self test string; 1: statistics string.
996 * Return string set count.
998 int hns_get_sset_count(struct net_device *netdev, int stringset)
1000 struct hns_nic_priv *priv = netdev_priv(netdev);
1001 struct hnae_handle *h = priv->ae_handle;
1002 struct hnae_ae_ops *ops = h->dev->ops;
1004 if (!ops->get_sset_count) {
1005 netdev_err(netdev, "get_sset_count is null!\n");
1008 if (stringset == ETH_SS_TEST) {
1009 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1011 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1014 if ((!priv->phy) || (priv->phy->is_c45))
1019 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1024 * hns_phy_led_set - set phy LED status.
1026 * @value: LED state.
1028 * Return 0 on success, negative on failure.
1030 int hns_phy_led_set(struct net_device *netdev, int value)
1033 struct hns_nic_priv *priv = netdev_priv(netdev);
1034 struct phy_device *phy_dev = priv->phy;
1036 if (!phy_dev->bus) {
1037 netdev_err(netdev, "phy_dev->bus is null!\n");
1040 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1041 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1042 retval = mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_LED_FC_REG,
1044 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1045 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1047 netdev_err(netdev, "mdiobus_write fail !\n");
1054 * nic_set_phys_id - set phy identify LED.
1056 * @state: LED state.
1058 * Return 0 on success, negative on failure.
1060 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1062 struct hns_nic_priv *priv = netdev_priv(netdev);
1063 struct hnae_handle *h = priv->ae_handle;
1064 struct phy_device *phy_dev = priv->phy;
1069 case ETHTOOL_ID_ACTIVE:
1070 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1076 priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
1080 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1082 HNS_PHY_PAGE_COPPER);
1087 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1091 case ETHTOOL_ID_OFF:
1092 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1096 case ETHTOOL_ID_INACTIVE:
1097 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1103 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1104 HNS_LED_FC_REG, priv->phy_led_val);
1108 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1110 HNS_PHY_PAGE_COPPER);
1119 case ETHTOOL_ID_ACTIVE:
1120 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1122 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1123 case ETHTOOL_ID_OFF:
1124 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1125 case ETHTOOL_ID_INACTIVE:
1126 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1135 * hns_get_regs - get net device register
1138 * @date: register data
1140 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1143 struct hns_nic_priv *priv = netdev_priv(net_dev);
1144 struct hnae_ae_ops *ops;
1146 assert(priv || priv->ae_handle);
1148 ops = priv->ae_handle->dev->ops;
1150 cmd->version = HNS_CHIP_VERSION;
1151 if (!ops->get_regs) {
1152 netdev_err(net_dev, "ops->get_regs is null!\n");
1155 ops->get_regs(priv->ae_handle, data);
1159 * nic_get_regs_len - get total register len.
1162 * Return total register len.
1164 static int hns_get_regs_len(struct net_device *net_dev)
1167 struct hns_nic_priv *priv = netdev_priv(net_dev);
1168 struct hnae_ae_ops *ops;
1170 assert(priv || priv->ae_handle);
1172 ops = priv->ae_handle->dev->ops;
1173 if (!ops->get_regs_len) {
1174 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1178 reg_num = ops->get_regs_len(priv->ae_handle);
1180 return reg_num * sizeof(u32);
1182 return reg_num; /* error code */
1186 * hns_nic_nway_reset - nway reset
1189 * Return 0 on success, negative on failure
1191 static int hns_nic_nway_reset(struct net_device *netdev)
1194 struct hns_nic_priv *priv = netdev_priv(netdev);
1195 struct phy_device *phy = priv->phy;
1197 if (netif_running(netdev)) {
1199 ret = genphy_restart_aneg(phy);
1205 static struct ethtool_ops hns_ethtool_ops = {
1206 .get_drvinfo = hns_nic_get_drvinfo,
1207 .get_link = hns_nic_get_link,
1208 .get_settings = hns_nic_get_settings,
1209 .set_settings = hns_nic_set_settings,
1210 .get_ringparam = hns_get_ringparam,
1211 .get_pauseparam = hns_get_pauseparam,
1212 .set_pauseparam = hns_set_pauseparam,
1213 .get_coalesce = hns_get_coalesce,
1214 .set_coalesce = hns_set_coalesce,
1215 .get_channels = hns_get_channels,
1216 .self_test = hns_nic_self_test,
1217 .get_strings = hns_get_strings,
1218 .get_sset_count = hns_get_sset_count,
1219 .get_ethtool_stats = hns_get_ethtool_stats,
1220 .set_phys_id = hns_set_phys_id,
1221 .get_regs_len = hns_get_regs_len,
1222 .get_regs = hns_get_regs,
1223 .nway_reset = hns_nic_nway_reset,
1226 void hns_ethtool_set_ops(struct net_device *ndev)
1228 ndev->ethtool_ops = &hns_ethtool_ops;