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 #define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
355 struct hns_nic_priv *priv = netdev_priv(ndev);
356 struct hnae_handle *h = priv->ae_handle;
360 hns_nic_net_reset(ndev);
363 phy_disconnect(priv->phy);
366 ret = hns_nic_init_phy(ndev, h);
371 ret = __lb_setup(ndev, loop_mode);
377 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
382 phy_start(priv->phy);
384 /* link adjust duplex*/
385 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
391 h->dev->ops->adjust_link(h, speed, duplex);
393 /* wait adjust link done and phy ready */
394 msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
399 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
402 struct net_device *ndev;
403 struct hnae_ring *ring;
404 struct netdev_queue *dev_queue;
405 struct sk_buff *new_skb;
406 unsigned int frame_size;
409 char buff[33]; /* 32B data and the last character '\0' */
411 if (!ring_data) { /* Just for doing create frame*/
412 frame_size = skb->len;
413 memset(skb->data, 0xFF, frame_size);
415 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
416 memset(&skb->data[frame_size / 2 + 10], 0xBE,
417 frame_size / 2 - 11);
418 memset(&skb->data[frame_size / 2 + 12], 0xAF,
419 frame_size / 2 - 13);
423 ring = ring_data->ring;
424 ndev = ring_data->napi.dev;
425 if (is_tx_ring(ring)) { /* for tx queue reset*/
426 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
427 netdev_tx_reset_queue(dev_queue);
431 frame_size = skb->len;
434 new_skb = skb_copy(skb, GFP_ATOMIC);
435 dev_kfree_skb_any(skb);
437 netdev_err(ndev, "skb alloc failed\n");
443 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
444 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
445 (*(skb->data + frame_size / 2 + 12) == 0xAF))
450 ndev->stats.rx_packets++;
451 ndev->stats.rx_bytes += skb->len;
453 ndev->stats.rx_frame_errors++;
454 for (i = 0; i < skb->len; i++) {
455 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
456 "%02x", *(skb->data + i));
457 if ((i % 16 == 15) || (i == skb->len - 1))
458 pr_info("%s\n", buff);
461 dev_kfree_skb_any(skb);
464 static int __lb_clean_rings(struct hns_nic_priv *priv,
465 int ringid0, int ringid1, int budget)
468 struct hns_nic_ring_data *ring_data;
469 struct net_device *ndev = priv->netdev;
470 unsigned long rx_packets = ndev->stats.rx_packets;
471 unsigned long rx_bytes = ndev->stats.rx_bytes;
472 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
474 for (i = ringid0; i <= ringid1; i++) {
475 ring_data = &priv->ring_data[i];
476 (void)ring_data->poll_one(ring_data,
477 budget, __lb_other_process);
479 ret = (int)(ndev->stats.rx_packets - rx_packets);
480 ndev->stats.rx_packets = rx_packets;
481 ndev->stats.rx_bytes = rx_bytes;
482 ndev->stats.rx_frame_errors = rx_frame_errors;
487 * nic_run_loopback_test - run loopback test
488 * @nic_dev: net device
489 * @loopback_type: loopback type
491 static int __lb_run_test(struct net_device *ndev,
492 enum hnae_loop loop_mode)
494 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
495 #define NIC_LB_TEST_RING_ID 0
496 #define NIC_LB_TEST_FRAME_SIZE 128
497 /* nic loopback test err */
498 #define NIC_LB_TEST_NO_MEM_ERR 1
499 #define NIC_LB_TEST_TX_CNT_ERR 2
500 #define NIC_LB_TEST_RX_CNT_ERR 3
501 #define NIC_LB_TEST_RX_PKG_ERR 4
502 struct hns_nic_priv *priv = netdev_priv(ndev);
503 struct hnae_handle *h = priv->ae_handle;
504 int i, j, lc, good_cnt, ret_val = 0;
506 netdev_tx_t tx_ret_val;
509 size = NIC_LB_TEST_FRAME_SIZE;
510 /* allocate test skb */
511 skb = alloc_skb(size, GFP_KERNEL);
513 return NIC_LB_TEST_NO_MEM_ERR;
515 /* place data into test skb */
516 (void)skb_put(skb, size);
517 __lb_other_process(NULL, skb);
518 skb->queue_mapping = NIC_LB_TEST_RING_ID;
521 for (j = 0; j < lc; j++) {
522 /* reset count of good packets */
524 /* place 64 packets on the transmit queue*/
525 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
528 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
530 &tx_ring_data(priv, skb->queue_mapping));
531 if (tx_ret_val == NETDEV_TX_OK)
536 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
537 ret_val = NIC_LB_TEST_TX_CNT_ERR;
538 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
539 hns_nic_test_strs[loop_mode], good_cnt,
540 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
544 /* allow 100 milliseconds for packets to go from Tx to Rx */
547 good_cnt = __lb_clean_rings(priv,
548 h->q_num, h->q_num * 2 - 1,
549 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
550 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
551 ret_val = NIC_LB_TEST_RX_CNT_ERR;
552 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
553 hns_nic_test_strs[loop_mode], good_cnt,
554 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
557 (void)__lb_clean_rings(priv,
558 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
559 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
562 /* free the original skb */
568 static int __lb_down(struct net_device *ndev)
570 struct hns_nic_priv *priv = netdev_priv(ndev);
571 struct hnae_handle *h = priv->ae_handle;
574 ret = __lb_setup(ndev, MAC_LOOP_NONE);
576 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
583 if (h->dev->ops->stop)
584 h->dev->ops->stop(h);
586 usleep_range(10000, 20000);
587 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
589 hns_nic_net_reset(ndev);
595 * hns_nic_self_test - self test
597 * @eth_test: test cmd
600 static void hns_nic_self_test(struct net_device *ndev,
601 struct ethtool_test *eth_test, u64 *data)
603 struct hns_nic_priv *priv = netdev_priv(ndev);
604 bool if_running = netif_running(ndev);
605 #define SELF_TEST_TPYE_NUM 3
606 int st_param[SELF_TEST_TPYE_NUM][2];
610 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
611 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
612 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
613 st_param[1][1] = 1; /*serdes must exist*/
614 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
615 st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
616 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
618 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
619 set_bit(NIC_STATE_TESTING, &priv->state);
622 (void)dev_close(ndev);
624 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
626 continue; /* NEXT testing */
628 data[test_index] = __lb_up(ndev,
629 (enum hnae_loop)st_param[i][0]);
630 if (!data[test_index]) {
631 data[test_index] = __lb_run_test(
632 ndev, (enum hnae_loop)st_param[i][0]);
633 (void)__lb_down(ndev);
636 if (data[test_index])
637 eth_test->flags |= ETH_TEST_FL_FAILED;
642 hns_nic_net_reset(priv->netdev);
644 clear_bit(NIC_STATE_TESTING, &priv->state);
647 (void)dev_open(ndev);
649 /* Online tests aren't run; pass by default */
651 (void)msleep_interruptible(4 * 1000);
655 * hns_nic_get_drvinfo - get net driver info
657 * @drvinfo: driver info
659 static void hns_nic_get_drvinfo(struct net_device *net_dev,
660 struct ethtool_drvinfo *drvinfo)
662 struct hns_nic_priv *priv = netdev_priv(net_dev);
666 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
667 sizeof(drvinfo->version));
668 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
670 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
671 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
673 strncpy(drvinfo->bus_info, priv->dev->bus->name,
674 sizeof(drvinfo->bus_info));
675 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
677 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
681 * hns_get_ringparam - get ring parameter
683 * @param: ethtool parameter
685 void hns_get_ringparam(struct net_device *net_dev,
686 struct ethtool_ringparam *param)
688 struct hns_nic_priv *priv = netdev_priv(net_dev);
689 struct hnae_ae_ops *ops;
690 struct hnae_queue *queue;
693 queue = priv->ae_handle->qs[0];
694 ops = priv->ae_handle->dev->ops;
696 if (ops->get_ring_bdnum_limit)
697 ops->get_ring_bdnum_limit(queue, &uplimit);
699 param->rx_max_pending = uplimit;
700 param->tx_max_pending = uplimit;
701 param->rx_pending = queue->rx_ring.desc_num;
702 param->tx_pending = queue->tx_ring.desc_num;
706 * hns_get_pauseparam - get pause parameter
708 * @param: pause parameter
710 static void hns_get_pauseparam(struct net_device *net_dev,
711 struct ethtool_pauseparam *param)
713 struct hns_nic_priv *priv = netdev_priv(net_dev);
714 struct hnae_ae_ops *ops;
716 ops = priv->ae_handle->dev->ops;
718 if (ops->get_pauseparam)
719 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
720 ¶m->rx_pause, ¶m->tx_pause);
724 * hns_set_pauseparam - set pause parameter
726 * @param: pause parameter
728 * Return 0 on success, negative on failure
730 static int hns_set_pauseparam(struct net_device *net_dev,
731 struct ethtool_pauseparam *param)
733 struct hns_nic_priv *priv = netdev_priv(net_dev);
734 struct hnae_handle *h;
735 struct hnae_ae_ops *ops;
737 assert(priv || priv->ae_handle);
742 if (!ops->set_pauseparam)
745 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
746 param->rx_pause, param->tx_pause);
750 * hns_get_coalesce - get coalesce info.
752 * @ec: coalesce info.
754 * Return 0 on success, negative on failure.
756 static int hns_get_coalesce(struct net_device *net_dev,
757 struct ethtool_coalesce *ec)
759 struct hns_nic_priv *priv = netdev_priv(net_dev);
760 struct hnae_ae_ops *ops;
762 ops = priv->ae_handle->dev->ops;
764 ec->use_adaptive_rx_coalesce = 1;
765 ec->use_adaptive_tx_coalesce = 1;
767 if ((!ops->get_coalesce_usecs) ||
768 (!ops->get_rx_max_coalesced_frames))
771 ops->get_coalesce_usecs(priv->ae_handle,
772 &ec->tx_coalesce_usecs,
773 &ec->rx_coalesce_usecs);
775 ops->get_rx_max_coalesced_frames(
777 &ec->tx_max_coalesced_frames,
778 &ec->rx_max_coalesced_frames);
784 * hns_set_coalesce - set coalesce info.
786 * @ec: coalesce info.
788 * Return 0 on success, negative on failure.
790 static int hns_set_coalesce(struct net_device *net_dev,
791 struct ethtool_coalesce *ec)
793 struct hns_nic_priv *priv = netdev_priv(net_dev);
794 struct hnae_ae_ops *ops;
797 assert(priv || priv->ae_handle);
799 ops = priv->ae_handle->dev->ops;
801 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
804 if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
807 if ((!ops->set_coalesce_usecs) ||
808 (!ops->set_coalesce_frames))
811 ops->set_coalesce_usecs(priv->ae_handle,
812 ec->rx_coalesce_usecs);
814 ret = ops->set_coalesce_frames(
816 ec->rx_max_coalesced_frames);
822 * hns_get_channels - get channel info.
826 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
828 struct hns_nic_priv *priv = netdev_priv(net_dev);
830 ch->max_rx = priv->ae_handle->q_num;
831 ch->max_tx = priv->ae_handle->q_num;
833 ch->rx_count = priv->ae_handle->q_num;
834 ch->tx_count = priv->ae_handle->q_num;
838 * get_ethtool_stats - get detail statistics.
840 * @stats: statistics info.
841 * @data: statistics data.
843 void hns_get_ethtool_stats(struct net_device *netdev,
844 struct ethtool_stats *stats, u64 *data)
847 struct hns_nic_priv *priv = netdev_priv(netdev);
848 struct hnae_handle *h = priv->ae_handle;
849 const struct rtnl_link_stats64 *net_stats;
850 struct rtnl_link_stats64 temp;
852 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
853 netdev_err(netdev, "get_stats or update_stats is null!\n");
857 h->dev->ops->update_stats(h, &netdev->stats);
859 net_stats = dev_get_stats(netdev, &temp);
861 /* get netdev statistics */
862 p[0] = net_stats->rx_packets;
863 p[1] = net_stats->tx_packets;
864 p[2] = net_stats->rx_bytes;
865 p[3] = net_stats->tx_bytes;
866 p[4] = net_stats->rx_errors;
867 p[5] = net_stats->tx_errors;
868 p[6] = net_stats->rx_dropped;
869 p[7] = net_stats->tx_dropped;
870 p[8] = net_stats->multicast;
871 p[9] = net_stats->collisions;
872 p[10] = net_stats->rx_over_errors;
873 p[11] = net_stats->rx_crc_errors;
874 p[12] = net_stats->rx_frame_errors;
875 p[13] = net_stats->rx_fifo_errors;
876 p[14] = net_stats->rx_missed_errors;
877 p[15] = net_stats->tx_aborted_errors;
878 p[16] = net_stats->tx_carrier_errors;
879 p[17] = net_stats->tx_fifo_errors;
880 p[18] = net_stats->tx_heartbeat_errors;
881 p[19] = net_stats->rx_length_errors;
882 p[20] = net_stats->tx_window_errors;
883 p[21] = net_stats->rx_compressed;
884 p[22] = net_stats->tx_compressed;
886 p[23] = netdev->rx_dropped.counter;
887 p[24] = netdev->tx_dropped.counter;
889 p[25] = priv->tx_timeout_count;
891 /* get driver statistics */
892 h->dev->ops->get_stats(h, &p[26]);
896 * get_strings: Return a set of strings that describe the requested objects
898 * @stats: string set ID.
899 * @data: objects data.
901 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
903 struct hns_nic_priv *priv = netdev_priv(netdev);
904 struct hnae_handle *h = priv->ae_handle;
905 char *buff = (char *)data;
907 if (!h->dev->ops->get_strings) {
908 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
912 if (stringset == ETH_SS_TEST) {
913 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
914 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
916 buff += ETH_GSTRING_LEN;
918 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
920 buff += ETH_GSTRING_LEN;
921 if ((priv->phy) && (!priv->phy->is_c45))
922 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
926 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
927 buff = buff + ETH_GSTRING_LEN;
928 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
929 buff = buff + ETH_GSTRING_LEN;
930 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
931 buff = buff + ETH_GSTRING_LEN;
932 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
933 buff = buff + ETH_GSTRING_LEN;
934 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
935 buff = buff + ETH_GSTRING_LEN;
936 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
937 buff = buff + ETH_GSTRING_LEN;
938 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
939 buff = buff + ETH_GSTRING_LEN;
940 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
941 buff = buff + ETH_GSTRING_LEN;
942 snprintf(buff, ETH_GSTRING_LEN, "multicast");
943 buff = buff + ETH_GSTRING_LEN;
944 snprintf(buff, ETH_GSTRING_LEN, "collisions");
945 buff = buff + ETH_GSTRING_LEN;
946 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
947 buff = buff + ETH_GSTRING_LEN;
948 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
949 buff = buff + ETH_GSTRING_LEN;
950 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
951 buff = buff + ETH_GSTRING_LEN;
952 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
953 buff = buff + ETH_GSTRING_LEN;
954 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
955 buff = buff + ETH_GSTRING_LEN;
956 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
957 buff = buff + ETH_GSTRING_LEN;
958 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
959 buff = buff + ETH_GSTRING_LEN;
960 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
961 buff = buff + ETH_GSTRING_LEN;
962 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
963 buff = buff + ETH_GSTRING_LEN;
964 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
965 buff = buff + ETH_GSTRING_LEN;
966 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
967 buff = buff + ETH_GSTRING_LEN;
968 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
969 buff = buff + ETH_GSTRING_LEN;
970 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
971 buff = buff + ETH_GSTRING_LEN;
972 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
973 buff = buff + ETH_GSTRING_LEN;
974 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
975 buff = buff + ETH_GSTRING_LEN;
977 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
978 buff = buff + ETH_GSTRING_LEN;
980 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
985 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
987 * @stringset: string set index, 0: self test string; 1: statistics string.
989 * Return string set count.
991 int hns_get_sset_count(struct net_device *netdev, int stringset)
993 struct hns_nic_priv *priv = netdev_priv(netdev);
994 struct hnae_handle *h = priv->ae_handle;
995 struct hnae_ae_ops *ops = h->dev->ops;
997 if (!ops->get_sset_count) {
998 netdev_err(netdev, "get_sset_count is null!\n");
1001 if (stringset == ETH_SS_TEST) {
1002 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1004 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1007 if ((!priv->phy) || (priv->phy->is_c45))
1011 } else if (stringset == ETH_SS_STATS) {
1012 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1019 * hns_phy_led_set - set phy LED status.
1021 * @value: LED state.
1023 * Return 0 on success, negative on failure.
1025 int hns_phy_led_set(struct net_device *netdev, int value)
1028 struct hns_nic_priv *priv = netdev_priv(netdev);
1029 struct phy_device *phy_dev = priv->phy;
1031 if (!phy_dev->bus) {
1032 netdev_err(netdev, "phy_dev->bus is null!\n");
1035 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1036 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1037 retval = mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_LED_FC_REG,
1039 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1040 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1042 netdev_err(netdev, "mdiobus_write fail !\n");
1049 * nic_set_phys_id - set phy identify LED.
1051 * @state: LED state.
1053 * Return 0 on success, negative on failure.
1055 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1057 struct hns_nic_priv *priv = netdev_priv(netdev);
1058 struct hnae_handle *h = priv->ae_handle;
1059 struct phy_device *phy_dev = priv->phy;
1064 case ETHTOOL_ID_ACTIVE:
1065 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1071 priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
1075 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1077 HNS_PHY_PAGE_COPPER);
1082 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1086 case ETHTOOL_ID_OFF:
1087 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1091 case ETHTOOL_ID_INACTIVE:
1092 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1098 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1099 HNS_LED_FC_REG, priv->phy_led_val);
1103 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1105 HNS_PHY_PAGE_COPPER);
1114 case ETHTOOL_ID_ACTIVE:
1115 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1117 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1118 case ETHTOOL_ID_OFF:
1119 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1120 case ETHTOOL_ID_INACTIVE:
1121 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1130 * hns_get_regs - get net device register
1133 * @date: register data
1135 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1138 struct hns_nic_priv *priv = netdev_priv(net_dev);
1139 struct hnae_ae_ops *ops;
1141 assert(priv || priv->ae_handle);
1143 ops = priv->ae_handle->dev->ops;
1145 cmd->version = HNS_CHIP_VERSION;
1146 if (!ops->get_regs) {
1147 netdev_err(net_dev, "ops->get_regs is null!\n");
1150 ops->get_regs(priv->ae_handle, data);
1154 * nic_get_regs_len - get total register len.
1157 * Return total register len.
1159 static int hns_get_regs_len(struct net_device *net_dev)
1162 struct hns_nic_priv *priv = netdev_priv(net_dev);
1163 struct hnae_ae_ops *ops;
1165 assert(priv || priv->ae_handle);
1167 ops = priv->ae_handle->dev->ops;
1168 if (!ops->get_regs_len) {
1169 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1173 reg_num = ops->get_regs_len(priv->ae_handle);
1175 return reg_num * sizeof(u32);
1177 return reg_num; /* error code */
1181 * hns_nic_nway_reset - nway reset
1184 * Return 0 on success, negative on failure
1186 static int hns_nic_nway_reset(struct net_device *netdev)
1189 struct hns_nic_priv *priv = netdev_priv(netdev);
1190 struct phy_device *phy = priv->phy;
1192 if (netif_running(netdev)) {
1194 ret = genphy_restart_aneg(phy);
1200 static struct ethtool_ops hns_ethtool_ops = {
1201 .get_drvinfo = hns_nic_get_drvinfo,
1202 .get_link = hns_nic_get_link,
1203 .get_settings = hns_nic_get_settings,
1204 .set_settings = hns_nic_set_settings,
1205 .get_ringparam = hns_get_ringparam,
1206 .get_pauseparam = hns_get_pauseparam,
1207 .set_pauseparam = hns_set_pauseparam,
1208 .get_coalesce = hns_get_coalesce,
1209 .set_coalesce = hns_set_coalesce,
1210 .get_channels = hns_get_channels,
1211 .self_test = hns_nic_self_test,
1212 .get_strings = hns_get_strings,
1213 .get_sset_count = hns_get_sset_count,
1214 .get_ethtool_stats = hns_get_ethtool_stats,
1215 .set_phys_id = hns_set_phys_id,
1216 .get_regs_len = hns_get_regs_len,
1217 .get_regs = hns_get_regs,
1218 .nway_reset = hns_nic_nway_reset,
1221 void hns_ethtool_set_ops(struct net_device *ndev)
1223 ndev->ethtool_ops = &hns_ethtool_ops;