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;
199 if (!netif_running(net_dev))
202 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
203 !priv->ae_handle->dev->ops)
207 speed = ethtool_cmd_speed(cmd);
209 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
210 if (cmd->autoneg == AUTONEG_ENABLE || speed != SPEED_10000 ||
211 cmd->duplex != DUPLEX_FULL)
213 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
214 if (!priv->phy && cmd->autoneg == AUTONEG_ENABLE)
217 if (speed == SPEED_1000 && cmd->duplex == DUPLEX_HALF)
220 return phy_ethtool_sset(priv->phy, cmd);
222 if ((speed != SPEED_10 && speed != SPEED_100 &&
223 speed != SPEED_1000) || (cmd->duplex != DUPLEX_HALF &&
224 cmd->duplex != DUPLEX_FULL))
227 netdev_err(net_dev, "Not supported!");
231 if (h->dev->ops->adjust_link) {
232 h->dev->ops->adjust_link(h, (int)speed, cmd->duplex);
236 netdev_err(net_dev, "Not supported!");
240 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
242 "Serdes Loopback test",
246 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
248 #define COPPER_CONTROL_REG 0
249 #define PHY_LOOP_BACK BIT(14)
252 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
257 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
258 HNS_PHY_PAGE_REG, 2);
259 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
262 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
265 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
267 /* If autoneg disabled,two soft-reset operations */
268 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
270 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
273 /* Default is 0x0400 */
274 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
277 /* Force 1000M Link, Default is 0x0200 */
278 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
280 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
283 /* Enable MAC loop-back */
284 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
286 val |= PHY_LOOP_BACK;
287 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
288 COPPER_CONTROL_REG, val);
290 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
292 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
294 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
296 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
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);
308 static int __lb_setup(struct net_device *ndev,
312 struct hns_nic_priv *priv = netdev_priv(ndev);
313 struct phy_device *phy_dev = priv->phy;
314 struct hnae_handle *h = priv->ae_handle;
317 case MAC_INTERNALLOOP_PHY:
318 if ((phy_dev) && (!phy_dev->is_c45))
319 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
321 case MAC_INTERNALLOOP_MAC:
322 if ((h->dev->ops->set_loopback) &&
323 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
324 ret = h->dev->ops->set_loopback(h, loop, 0x1);
326 case MAC_INTERNALLOOP_SERDES:
327 if (h->dev->ops->set_loopback)
328 ret = h->dev->ops->set_loopback(h, loop, 0x1);
331 if ((phy_dev) && (!phy_dev->is_c45))
332 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
334 if (h->dev->ops->set_loopback) {
335 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
336 ret |= h->dev->ops->set_loopback(h,
337 MAC_INTERNALLOOP_MAC, 0x0);
339 ret |= h->dev->ops->set_loopback(h,
340 MAC_INTERNALLOOP_SERDES, 0x0);
351 static int __lb_up(struct net_device *ndev,
352 enum hnae_loop loop_mode)
354 struct hns_nic_priv *priv = netdev_priv(ndev);
355 struct hnae_handle *h = priv->ae_handle;
359 hns_nic_net_reset(ndev);
362 phy_disconnect(priv->phy);
365 ret = hns_nic_init_phy(ndev, h);
370 ret = __lb_setup(ndev, loop_mode);
376 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
381 phy_start(priv->phy);
383 /* link adjust duplex*/
384 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
390 h->dev->ops->adjust_link(h, speed, duplex);
395 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
398 struct net_device *ndev;
399 struct hnae_ring *ring;
400 struct netdev_queue *dev_queue;
401 struct sk_buff *new_skb;
402 unsigned int frame_size;
405 char buff[33]; /* 32B data and the last character '\0' */
407 if (!ring_data) { /* Just for doing create frame*/
408 frame_size = skb->len;
409 memset(skb->data, 0xFF, frame_size);
411 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
412 memset(&skb->data[frame_size / 2 + 10], 0xBE,
413 frame_size / 2 - 11);
414 memset(&skb->data[frame_size / 2 + 12], 0xAF,
415 frame_size / 2 - 13);
419 ring = ring_data->ring;
420 ndev = ring_data->napi.dev;
421 if (is_tx_ring(ring)) { /* for tx queue reset*/
422 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
423 netdev_tx_reset_queue(dev_queue);
427 frame_size = skb->len;
430 new_skb = skb_copy(skb, GFP_ATOMIC);
431 dev_kfree_skb_any(skb);
435 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
436 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
437 (*(skb->data + frame_size / 2 + 12) == 0xAF))
442 ndev->stats.rx_packets++;
443 ndev->stats.rx_bytes += skb->len;
445 ndev->stats.rx_frame_errors++;
446 for (i = 0; i < skb->len; i++) {
447 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
448 "%02x", *(skb->data + i));
449 if ((i % 16 == 15) || (i == skb->len - 1))
450 pr_info("%s\n", buff);
453 dev_kfree_skb_any(skb);
456 static int __lb_clean_rings(struct hns_nic_priv *priv,
457 int ringid0, int ringid1, int budget)
460 struct hns_nic_ring_data *ring_data;
461 struct net_device *ndev = priv->netdev;
462 unsigned long rx_packets = ndev->stats.rx_packets;
463 unsigned long rx_bytes = ndev->stats.rx_bytes;
464 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
466 for (i = ringid0; i <= ringid1; i++) {
467 ring_data = &priv->ring_data[i];
468 (void)ring_data->poll_one(ring_data,
469 budget, __lb_other_process);
471 ret = (int)(ndev->stats.rx_packets - rx_packets);
472 ndev->stats.rx_packets = rx_packets;
473 ndev->stats.rx_bytes = rx_bytes;
474 ndev->stats.rx_frame_errors = rx_frame_errors;
479 * nic_run_loopback_test - run loopback test
480 * @nic_dev: net device
481 * @loopback_type: loopback type
483 static int __lb_run_test(struct net_device *ndev,
484 enum hnae_loop loop_mode)
486 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
487 #define NIC_LB_TEST_RING_ID 0
488 #define NIC_LB_TEST_FRAME_SIZE 128
489 /* nic loopback test err */
490 #define NIC_LB_TEST_NO_MEM_ERR 1
491 #define NIC_LB_TEST_TX_CNT_ERR 2
492 #define NIC_LB_TEST_RX_CNT_ERR 3
493 #define NIC_LB_TEST_RX_PKG_ERR 4
494 struct hns_nic_priv *priv = netdev_priv(ndev);
495 struct hnae_handle *h = priv->ae_handle;
496 int i, j, lc, good_cnt, ret_val = 0;
498 netdev_tx_t tx_ret_val;
501 size = NIC_LB_TEST_FRAME_SIZE;
502 /* allocate test skb */
503 skb = alloc_skb(size, GFP_KERNEL);
505 return NIC_LB_TEST_NO_MEM_ERR;
507 /* place data into test skb */
508 (void)skb_put(skb, size);
509 __lb_other_process(NULL, skb);
510 skb->queue_mapping = NIC_LB_TEST_RING_ID;
513 for (j = 0; j < lc; j++) {
514 /* reset count of good packets */
516 /* place 64 packets on the transmit queue*/
517 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
520 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
522 &tx_ring_data(priv, skb->queue_mapping));
523 if (tx_ret_val == NETDEV_TX_OK)
528 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
529 ret_val = NIC_LB_TEST_TX_CNT_ERR;
530 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
531 hns_nic_test_strs[loop_mode], good_cnt,
532 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
536 /* allow 100 milliseconds for packets to go from Tx to Rx */
539 good_cnt = __lb_clean_rings(priv,
540 h->q_num, h->q_num * 2 - 1,
541 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
542 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
543 ret_val = NIC_LB_TEST_RX_CNT_ERR;
544 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
545 hns_nic_test_strs[loop_mode], good_cnt,
546 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
549 (void)__lb_clean_rings(priv,
550 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
551 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
554 /* free the original skb */
560 static int __lb_down(struct net_device *ndev)
562 struct hns_nic_priv *priv = netdev_priv(ndev);
563 struct hnae_handle *h = priv->ae_handle;
566 ret = __lb_setup(ndev, MAC_LOOP_NONE);
568 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
575 if (h->dev->ops->stop)
576 h->dev->ops->stop(h);
578 usleep_range(10000, 20000);
579 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
581 hns_nic_net_reset(ndev);
587 * hns_nic_self_test - self test
589 * @eth_test: test cmd
592 static void hns_nic_self_test(struct net_device *ndev,
593 struct ethtool_test *eth_test, u64 *data)
595 struct hns_nic_priv *priv = netdev_priv(ndev);
596 bool if_running = netif_running(ndev);
597 #define SELF_TEST_TPYE_NUM 3
598 int st_param[SELF_TEST_TPYE_NUM][2];
602 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
603 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
604 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
605 st_param[1][1] = 1; /*serdes must exist*/
606 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
607 st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
608 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
610 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
611 set_bit(NIC_STATE_TESTING, &priv->state);
614 (void)dev_close(ndev);
616 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
618 continue; /* NEXT testing */
620 data[test_index] = __lb_up(ndev,
621 (enum hnae_loop)st_param[i][0]);
622 if (!data[test_index]) {
623 data[test_index] = __lb_run_test(
624 ndev, (enum hnae_loop)st_param[i][0]);
625 (void)__lb_down(ndev);
628 if (data[test_index])
629 eth_test->flags |= ETH_TEST_FL_FAILED;
634 hns_nic_net_reset(priv->netdev);
636 clear_bit(NIC_STATE_TESTING, &priv->state);
639 (void)dev_open(ndev);
641 /* Online tests aren't run; pass by default */
643 (void)msleep_interruptible(4 * 1000);
647 * hns_nic_get_drvinfo - get net driver info
649 * @drvinfo: driver info
651 static void hns_nic_get_drvinfo(struct net_device *net_dev,
652 struct ethtool_drvinfo *drvinfo)
654 struct hns_nic_priv *priv = netdev_priv(net_dev);
658 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
659 sizeof(drvinfo->version));
660 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
662 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
663 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
665 strncpy(drvinfo->bus_info, priv->dev->bus->name,
666 sizeof(drvinfo->bus_info));
667 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
669 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
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);
1190 static struct ethtool_ops hns_ethtool_ops = {
1191 .get_drvinfo = hns_nic_get_drvinfo,
1192 .get_link = hns_nic_get_link,
1193 .get_settings = hns_nic_get_settings,
1194 .set_settings = hns_nic_set_settings,
1195 .get_ringparam = hns_get_ringparam,
1196 .get_pauseparam = hns_get_pauseparam,
1197 .set_pauseparam = hns_set_pauseparam,
1198 .get_coalesce = hns_get_coalesce,
1199 .set_coalesce = hns_set_coalesce,
1200 .get_channels = hns_get_channels,
1201 .self_test = hns_nic_self_test,
1202 .get_strings = hns_get_strings,
1203 .get_sset_count = hns_get_sset_count,
1204 .get_ethtool_stats = hns_get_ethtool_stats,
1205 .set_phys_id = hns_set_phys_id,
1206 .get_regs_len = hns_get_regs_len,
1207 .get_regs = hns_get_regs,
1208 .nway_reset = hns_nic_nway_reset,
1211 void hns_ethtool_set_ops(struct net_device *ndev)
1213 ndev->ethtool_ops = &hns_ethtool_ops;