Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / net / ethernet / hisilicon / hns / hns_ethtool.c
blobab33487a5321dd1d40f76be59ae7c49f6cf70757
1 /*
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.
8 */
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include "hns_enet.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)
38 /**
39 *hns_nic_get_link - get current link status
40 *@net_dev: net_device
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 h = priv->ae_handle;
51 if (priv->phy) {
52 if (!genphy_read_status(priv->phy))
53 link_stat = priv->phy->link;
54 else
55 link_stat = 0;
58 if (h->dev && h->dev->ops && h->dev->ops->get_status)
59 link_stat = link_stat && h->dev->ops->get_status(h);
60 else
61 link_stat = 0;
63 return link_stat;
66 static void hns_get_mdix_mode(struct net_device *net_dev,
67 struct ethtool_cmd *cmd)
69 int mdix_ctrl, mdix, retval, is_resolved;
70 struct hns_nic_priv *priv = netdev_priv(net_dev);
71 struct phy_device *phy_dev = priv->phy;
73 if (!phy_dev || !phy_dev->mdio.bus) {
74 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
75 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
76 return;
79 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_MDIX);
81 retval = phy_read(phy_dev, HNS_PHY_CSC_REG);
82 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
84 retval = phy_read(phy_dev, HNS_PHY_CSS_REG);
85 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
86 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
88 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
90 switch (mdix_ctrl) {
91 case 0x0:
92 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI;
93 break;
94 case 0x1:
95 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_X;
96 break;
97 case 0x3:
98 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
99 break;
100 default:
101 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
102 break;
105 if (!is_resolved)
106 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
107 else if (mdix)
108 cmd->eth_tp_mdix = ETH_TP_MDI_X;
109 else
110 cmd->eth_tp_mdix = ETH_TP_MDI;
114 *hns_nic_get_settings - implement ethtool get settings
115 *@net_dev: net_device
116 *@cmd: ethtool_cmd
117 *retuen 0 - success , negative --fail
119 static int hns_nic_get_settings(struct net_device *net_dev,
120 struct ethtool_cmd *cmd)
122 struct hns_nic_priv *priv = netdev_priv(net_dev);
123 struct hnae_handle *h;
124 u32 link_stat;
125 int ret;
126 u8 duplex;
127 u16 speed;
129 if (!priv || !priv->ae_handle)
130 return -ESRCH;
132 h = priv->ae_handle;
133 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
134 return -ESRCH;
136 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
137 if (ret < 0) {
138 netdev_err(net_dev, "%s get_info error!\n", __func__);
139 return -EINVAL;
142 /* When there is no phy, autoneg is off. */
143 cmd->autoneg = false;
144 ethtool_cmd_speed_set(cmd, speed);
145 cmd->duplex = duplex;
147 if (priv->phy)
148 (void)phy_ethtool_gset(priv->phy, cmd);
150 link_stat = hns_nic_get_link(net_dev);
151 if (!link_stat) {
152 ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
153 cmd->duplex = DUPLEX_UNKNOWN;
156 if (cmd->autoneg)
157 cmd->advertising |= ADVERTISED_Autoneg;
159 cmd->supported |= h->if_support;
160 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
161 cmd->supported |= SUPPORTED_TP;
162 cmd->advertising |= ADVERTISED_1000baseT_Full;
163 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
164 cmd->supported |= SUPPORTED_FIBRE;
165 cmd->advertising |= ADVERTISED_10000baseKR_Full;
168 switch (h->media_type) {
169 case HNAE_MEDIA_TYPE_FIBER:
170 cmd->port = PORT_FIBRE;
171 break;
172 case HNAE_MEDIA_TYPE_COPPER:
173 cmd->port = PORT_TP;
174 break;
175 case HNAE_MEDIA_TYPE_UNKNOWN:
176 default:
177 break;
180 if (!(AE_IS_VER1(priv->enet_ver) && h->port_type == HNAE_PORT_DEBUG))
181 cmd->supported |= SUPPORTED_Pause;
183 cmd->transceiver = XCVR_EXTERNAL;
184 cmd->mdio_support = (ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22);
185 hns_get_mdix_mode(net_dev, cmd);
187 return 0;
191 *hns_nic_set_settings - implement ethtool set settings
192 *@net_dev: net_device
193 *@cmd: ethtool_cmd
194 *retuen 0 - success , negative --fail
196 static int hns_nic_set_settings(struct net_device *net_dev,
197 struct ethtool_cmd *cmd)
199 struct hns_nic_priv *priv = netdev_priv(net_dev);
200 struct hnae_handle *h;
201 u32 speed;
203 if (!netif_running(net_dev))
204 return -ESRCH;
206 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
207 !priv->ae_handle->dev->ops)
208 return -ENODEV;
210 h = priv->ae_handle;
211 speed = ethtool_cmd_speed(cmd);
213 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
214 if (cmd->autoneg == AUTONEG_ENABLE || speed != SPEED_10000 ||
215 cmd->duplex != DUPLEX_FULL)
216 return -EINVAL;
217 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
218 if (!priv->phy && cmd->autoneg == AUTONEG_ENABLE)
219 return -EINVAL;
221 if (speed == SPEED_1000 && cmd->duplex == DUPLEX_HALF)
222 return -EINVAL;
223 if (priv->phy)
224 return phy_ethtool_sset(priv->phy, cmd);
226 if ((speed != SPEED_10 && speed != SPEED_100 &&
227 speed != SPEED_1000) || (cmd->duplex != DUPLEX_HALF &&
228 cmd->duplex != DUPLEX_FULL))
229 return -EINVAL;
230 } else {
231 netdev_err(net_dev, "Not supported!");
232 return -ENOTSUPP;
235 if (h->dev->ops->adjust_link) {
236 h->dev->ops->adjust_link(h, (int)speed, cmd->duplex);
237 return 0;
240 netdev_err(net_dev, "Not supported!");
241 return -ENOTSUPP;
244 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
245 "Mac Loopback test",
246 "Serdes Loopback test",
247 "Phy Loopback test"
250 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
252 #define COPPER_CONTROL_REG 0
253 #define PHY_POWER_DOWN BIT(11)
254 #define PHY_LOOP_BACK BIT(14)
255 u16 val = 0;
257 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
258 return -ENOTSUPP;
260 if (en) {
261 /* speed : 1000M */
262 phy_write(phy_dev, HNS_PHY_PAGE_REG, 2);
263 phy_write(phy_dev, 21, 0x1046);
265 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
266 /* Force Master */
267 phy_write(phy_dev, 9, 0x1F00);
269 /* Soft-reset */
270 phy_write(phy_dev, 0, 0x9140);
271 /* If autoneg disabled,two soft-reset operations */
272 phy_write(phy_dev, 0, 0x9140);
274 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
276 /* Default is 0x0400 */
277 phy_write(phy_dev, 1, 0x418);
279 /* Force 1000M Link, Default is 0x0200 */
280 phy_write(phy_dev, 7, 0x20C);
281 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
283 /* Enable PHY loop-back */
284 val = phy_read(phy_dev, COPPER_CONTROL_REG);
285 val |= PHY_LOOP_BACK;
286 val &= ~PHY_POWER_DOWN;
287 phy_write(phy_dev, COPPER_CONTROL_REG, val);
288 } else {
289 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
290 phy_write(phy_dev, 1, 0x400);
291 phy_write(phy_dev, 7, 0x200);
292 phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
293 phy_write(phy_dev, 9, 0xF00);
295 val = phy_read(phy_dev, COPPER_CONTROL_REG);
296 val &= ~PHY_LOOP_BACK;
297 val |= PHY_POWER_DOWN;
298 phy_write(phy_dev, COPPER_CONTROL_REG, val);
300 return 0;
303 static int __lb_setup(struct net_device *ndev,
304 enum hnae_loop loop)
306 int ret = 0;
307 struct hns_nic_priv *priv = netdev_priv(ndev);
308 struct phy_device *phy_dev = priv->phy;
309 struct hnae_handle *h = priv->ae_handle;
311 switch (loop) {
312 case MAC_INTERNALLOOP_PHY:
313 if ((phy_dev) && (!phy_dev->is_c45)) {
314 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
315 ret |= h->dev->ops->set_loopback(h, loop, 0x1);
317 break;
318 case MAC_INTERNALLOOP_MAC:
319 if ((h->dev->ops->set_loopback) &&
320 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
321 ret = h->dev->ops->set_loopback(h, loop, 0x1);
322 break;
323 case MAC_INTERNALLOOP_SERDES:
324 if (h->dev->ops->set_loopback)
325 ret = h->dev->ops->set_loopback(h, loop, 0x1);
326 break;
327 case MAC_LOOP_NONE:
328 if ((phy_dev) && (!phy_dev->is_c45))
329 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
331 if (h->dev->ops->set_loopback) {
332 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
333 ret |= h->dev->ops->set_loopback(h,
334 MAC_INTERNALLOOP_MAC, 0x0);
336 ret |= h->dev->ops->set_loopback(h,
337 MAC_INTERNALLOOP_SERDES, 0x0);
339 break;
340 default:
341 ret = -EINVAL;
342 break;
345 return ret;
348 static int __lb_up(struct net_device *ndev,
349 enum hnae_loop loop_mode)
351 struct hns_nic_priv *priv = netdev_priv(ndev);
352 struct hnae_handle *h = priv->ae_handle;
353 int speed, duplex;
354 int ret;
356 hns_nic_net_reset(ndev);
358 ret = __lb_setup(ndev, loop_mode);
359 if (ret)
360 return ret;
362 msleep(200);
364 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
365 if (ret)
366 return ret;
368 /* link adjust duplex*/
369 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
370 speed = 1000;
371 else
372 speed = 10000;
373 duplex = 1;
375 h->dev->ops->adjust_link(h, speed, duplex);
377 return 0;
380 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
381 struct sk_buff *skb)
383 struct net_device *ndev;
384 struct hns_nic_priv *priv;
385 struct hnae_ring *ring;
386 struct netdev_queue *dev_queue;
387 struct sk_buff *new_skb;
388 unsigned int frame_size;
389 int check_ok;
390 u32 i;
391 char buff[33]; /* 32B data and the last character '\0' */
393 if (!ring_data) { /* Just for doing create frame*/
394 ndev = skb->dev;
395 priv = netdev_priv(ndev);
397 frame_size = skb->len;
398 memset(skb->data, 0xFF, frame_size);
399 if ((!AE_IS_VER1(priv->enet_ver)) &&
400 (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
401 memcpy(skb->data, ndev->dev_addr, 6);
402 skb->data[5] += 0x1f;
405 frame_size &= ~1ul;
406 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
407 memset(&skb->data[frame_size / 2 + 10], 0xBE,
408 frame_size / 2 - 11);
409 memset(&skb->data[frame_size / 2 + 12], 0xAF,
410 frame_size / 2 - 13);
411 return;
414 ring = ring_data->ring;
415 ndev = ring_data->napi.dev;
416 if (is_tx_ring(ring)) { /* for tx queue reset*/
417 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
418 netdev_tx_reset_queue(dev_queue);
419 return;
422 frame_size = skb->len;
423 frame_size &= ~1ul;
424 /* for mutl buffer*/
425 new_skb = skb_copy(skb, GFP_ATOMIC);
426 dev_kfree_skb_any(skb);
427 skb = new_skb;
429 check_ok = 0;
430 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
431 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
432 (*(skb->data + frame_size / 2 + 12) == 0xAF))
433 check_ok = 1;
436 if (check_ok) {
437 ndev->stats.rx_packets++;
438 ndev->stats.rx_bytes += skb->len;
439 } else {
440 ndev->stats.rx_frame_errors++;
441 for (i = 0; i < skb->len; i++) {
442 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
443 "%02x", *(skb->data + i));
444 if ((i % 16 == 15) || (i == skb->len - 1))
445 pr_info("%s\n", buff);
448 dev_kfree_skb_any(skb);
451 static int __lb_clean_rings(struct hns_nic_priv *priv,
452 int ringid0, int ringid1, int budget)
454 int i, ret;
455 struct hns_nic_ring_data *ring_data;
456 struct net_device *ndev = priv->netdev;
457 unsigned long rx_packets = ndev->stats.rx_packets;
458 unsigned long rx_bytes = ndev->stats.rx_bytes;
459 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
461 for (i = ringid0; i <= ringid1; i++) {
462 ring_data = &priv->ring_data[i];
463 (void)ring_data->poll_one(ring_data,
464 budget, __lb_other_process);
466 ret = (int)(ndev->stats.rx_packets - rx_packets);
467 ndev->stats.rx_packets = rx_packets;
468 ndev->stats.rx_bytes = rx_bytes;
469 ndev->stats.rx_frame_errors = rx_frame_errors;
470 return ret;
474 * nic_run_loopback_test - run loopback test
475 * @nic_dev: net device
476 * @loopback_type: loopback type
478 static int __lb_run_test(struct net_device *ndev,
479 enum hnae_loop loop_mode)
481 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
482 #define NIC_LB_TEST_RING_ID 0
483 #define NIC_LB_TEST_FRAME_SIZE 128
484 /* nic loopback test err */
485 #define NIC_LB_TEST_NO_MEM_ERR 1
486 #define NIC_LB_TEST_TX_CNT_ERR 2
487 #define NIC_LB_TEST_RX_CNT_ERR 3
488 #define NIC_LB_TEST_RX_PKG_ERR 4
489 struct hns_nic_priv *priv = netdev_priv(ndev);
490 struct hnae_handle *h = priv->ae_handle;
491 int i, j, lc, good_cnt, ret_val = 0;
492 unsigned int size;
493 netdev_tx_t tx_ret_val;
494 struct sk_buff *skb;
496 size = NIC_LB_TEST_FRAME_SIZE;
497 /* allocate test skb */
498 skb = alloc_skb(size, GFP_KERNEL);
499 if (!skb)
500 return NIC_LB_TEST_NO_MEM_ERR;
502 /* place data into test skb */
503 (void)skb_put(skb, size);
504 skb->dev = ndev;
505 __lb_other_process(NULL, skb);
506 skb->queue_mapping = NIC_LB_TEST_RING_ID;
508 lc = 1;
509 for (j = 0; j < lc; j++) {
510 /* reset count of good packets */
511 good_cnt = 0;
512 /* place 64 packets on the transmit queue*/
513 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
514 (void)skb_get(skb);
516 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
517 ndev, skb,
518 &tx_ring_data(priv, skb->queue_mapping));
519 if (tx_ret_val == NETDEV_TX_OK)
520 good_cnt++;
521 else
522 break;
524 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
525 ret_val = NIC_LB_TEST_TX_CNT_ERR;
526 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
527 hns_nic_test_strs[loop_mode], good_cnt,
528 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
529 break;
532 /* allow 100 milliseconds for packets to go from Tx to Rx */
533 msleep(100);
535 good_cnt = __lb_clean_rings(priv,
536 h->q_num, h->q_num * 2 - 1,
537 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
538 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
539 ret_val = NIC_LB_TEST_RX_CNT_ERR;
540 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
541 hns_nic_test_strs[loop_mode], good_cnt,
542 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
543 break;
545 (void)__lb_clean_rings(priv,
546 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
547 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
550 /* free the original skb */
551 kfree_skb(skb);
553 return ret_val;
556 static int __lb_down(struct net_device *ndev)
558 struct hns_nic_priv *priv = netdev_priv(ndev);
559 struct hnae_handle *h = priv->ae_handle;
560 int ret;
562 ret = __lb_setup(ndev, MAC_LOOP_NONE);
563 if (ret)
564 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
565 __func__,
566 ret);
568 if (h->dev->ops->stop)
569 h->dev->ops->stop(h);
571 usleep_range(10000, 20000);
572 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
574 hns_nic_net_reset(ndev);
576 return 0;
580 * hns_nic_self_test - self test
581 * @dev: net device
582 * @eth_test: test cmd
583 * @data: test result
585 static void hns_nic_self_test(struct net_device *ndev,
586 struct ethtool_test *eth_test, u64 *data)
588 struct hns_nic_priv *priv = netdev_priv(ndev);
589 bool if_running = netif_running(ndev);
590 #define SELF_TEST_TPYE_NUM 3
591 int st_param[SELF_TEST_TPYE_NUM][2];
592 int i;
593 int test_index = 0;
595 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
596 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
597 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
598 st_param[1][1] = 1; /*serdes must exist*/
599 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
600 st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
601 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
603 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
604 set_bit(NIC_STATE_TESTING, &priv->state);
606 if (if_running)
607 (void)dev_close(ndev);
609 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
610 if (!st_param[i][1])
611 continue; /* NEXT testing */
613 data[test_index] = __lb_up(ndev,
614 (enum hnae_loop)st_param[i][0]);
615 if (!data[test_index]) {
616 data[test_index] = __lb_run_test(
617 ndev, (enum hnae_loop)st_param[i][0]);
618 (void)__lb_down(ndev);
621 if (data[test_index])
622 eth_test->flags |= ETH_TEST_FL_FAILED;
624 test_index++;
627 hns_nic_net_reset(priv->netdev);
629 clear_bit(NIC_STATE_TESTING, &priv->state);
631 if (if_running)
632 (void)dev_open(ndev);
634 /* Online tests aren't run; pass by default */
636 (void)msleep_interruptible(4 * 1000);
640 * hns_nic_get_drvinfo - get net driver info
641 * @dev: net device
642 * @drvinfo: driver info
644 static void hns_nic_get_drvinfo(struct net_device *net_dev,
645 struct ethtool_drvinfo *drvinfo)
647 struct hns_nic_priv *priv = netdev_priv(net_dev);
649 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
650 sizeof(drvinfo->version));
651 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
653 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
654 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
656 strncpy(drvinfo->bus_info, priv->dev->bus->name,
657 sizeof(drvinfo->bus_info));
658 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
660 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
661 drvinfo->eedump_len = 0;
665 * hns_get_ringparam - get ring parameter
666 * @dev: net device
667 * @param: ethtool parameter
669 void hns_get_ringparam(struct net_device *net_dev,
670 struct ethtool_ringparam *param)
672 struct hns_nic_priv *priv = netdev_priv(net_dev);
673 struct hnae_ae_ops *ops;
674 struct hnae_queue *queue;
675 u32 uplimit = 0;
677 queue = priv->ae_handle->qs[0];
678 ops = priv->ae_handle->dev->ops;
680 if (ops->get_ring_bdnum_limit)
681 ops->get_ring_bdnum_limit(queue, &uplimit);
683 param->rx_max_pending = uplimit;
684 param->tx_max_pending = uplimit;
685 param->rx_pending = queue->rx_ring.desc_num;
686 param->tx_pending = queue->tx_ring.desc_num;
690 * hns_get_pauseparam - get pause parameter
691 * @dev: net device
692 * @param: pause parameter
694 static void hns_get_pauseparam(struct net_device *net_dev,
695 struct ethtool_pauseparam *param)
697 struct hns_nic_priv *priv = netdev_priv(net_dev);
698 struct hnae_ae_ops *ops;
700 ops = priv->ae_handle->dev->ops;
702 if (ops->get_pauseparam)
703 ops->get_pauseparam(priv->ae_handle, &param->autoneg,
704 &param->rx_pause, &param->tx_pause);
708 * hns_set_pauseparam - set pause parameter
709 * @dev: net device
710 * @param: pause parameter
712 * Return 0 on success, negative on failure
714 static int hns_set_pauseparam(struct net_device *net_dev,
715 struct ethtool_pauseparam *param)
717 struct hns_nic_priv *priv = netdev_priv(net_dev);
718 struct hnae_handle *h;
719 struct hnae_ae_ops *ops;
721 h = priv->ae_handle;
722 ops = h->dev->ops;
724 if (!ops->set_pauseparam)
725 return -ESRCH;
727 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
728 param->rx_pause, param->tx_pause);
732 * hns_get_coalesce - get coalesce info.
733 * @dev: net device
734 * @ec: coalesce info.
736 * Return 0 on success, negative on failure.
738 static int hns_get_coalesce(struct net_device *net_dev,
739 struct ethtool_coalesce *ec)
741 struct hns_nic_priv *priv = netdev_priv(net_dev);
742 struct hnae_ae_ops *ops;
744 ops = priv->ae_handle->dev->ops;
746 ec->use_adaptive_rx_coalesce = 1;
747 ec->use_adaptive_tx_coalesce = 1;
749 if ((!ops->get_coalesce_usecs) ||
750 (!ops->get_rx_max_coalesced_frames))
751 return -ESRCH;
753 ops->get_coalesce_usecs(priv->ae_handle,
754 &ec->tx_coalesce_usecs,
755 &ec->rx_coalesce_usecs);
757 ops->get_rx_max_coalesced_frames(
758 priv->ae_handle,
759 &ec->tx_max_coalesced_frames,
760 &ec->rx_max_coalesced_frames);
762 ops->get_coalesce_range(priv->ae_handle,
763 &ec->tx_max_coalesced_frames_low,
764 &ec->rx_max_coalesced_frames_low,
765 &ec->tx_max_coalesced_frames_high,
766 &ec->rx_max_coalesced_frames_high,
767 &ec->tx_coalesce_usecs_low,
768 &ec->rx_coalesce_usecs_low,
769 &ec->tx_coalesce_usecs_high,
770 &ec->rx_coalesce_usecs_high);
772 return 0;
776 * hns_set_coalesce - set coalesce info.
777 * @dev: net device
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;
787 int ret;
789 ops = priv->ae_handle->dev->ops;
791 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
792 return -EINVAL;
794 if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
795 return -EINVAL;
797 if ((!ops->set_coalesce_usecs) ||
798 (!ops->set_coalesce_frames))
799 return -ESRCH;
801 ret = ops->set_coalesce_usecs(priv->ae_handle,
802 ec->rx_coalesce_usecs);
803 if (ret)
804 return ret;
806 ret = ops->set_coalesce_frames(
807 priv->ae_handle,
808 ec->rx_max_coalesced_frames);
810 return ret;
814 * hns_get_channels - get channel info.
815 * @dev: net device
816 * @ch: 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.
831 * @dev: net device
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)
838 u64 *p = 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");
846 return;
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
889 * @dev: net device
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");
901 return;
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],
907 ETH_GSTRING_LEN);
908 buff += ETH_GSTRING_LEN;
910 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
911 ETH_GSTRING_LEN);
912 buff += ETH_GSTRING_LEN;
913 if ((priv->phy) && (!priv->phy->is_c45))
914 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
915 ETH_GSTRING_LEN);
917 } else {
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.
978 * @dev: net device
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");
991 return -EOPNOTSUPP;
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)
997 cnt--;
999 if ((!priv->phy) || (priv->phy->is_c45))
1000 cnt--;
1002 return cnt;
1003 } else {
1004 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1009 * hns_phy_led_set - set phy LED status.
1010 * @dev: net device
1011 * @value: LED state.
1013 * Return 0 on success, negative on failure.
1015 int hns_phy_led_set(struct net_device *netdev, int value)
1017 int retval;
1018 struct hns_nic_priv *priv = netdev_priv(netdev);
1019 struct phy_device *phy_dev = priv->phy;
1021 retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1022 retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
1023 retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1024 if (retval) {
1025 netdev_err(netdev, "mdiobus_write fail !\n");
1026 return retval;
1028 return 0;
1032 * nic_set_phys_id - set phy identify LED.
1033 * @dev: net device
1034 * @state: LED state.
1036 * Return 0 on success, negative on failure.
1038 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1040 struct hns_nic_priv *priv = netdev_priv(netdev);
1041 struct hnae_handle *h = priv->ae_handle;
1042 struct phy_device *phy_dev = priv->phy;
1043 int ret;
1045 if (phy_dev)
1046 switch (state) {
1047 case ETHTOOL_ID_ACTIVE:
1048 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1049 HNS_PHY_PAGE_LED);
1050 if (ret)
1051 return ret;
1053 priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
1055 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1056 HNS_PHY_PAGE_COPPER);
1057 if (ret)
1058 return ret;
1059 return 2;
1060 case ETHTOOL_ID_ON:
1061 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1062 if (ret)
1063 return ret;
1064 break;
1065 case ETHTOOL_ID_OFF:
1066 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1067 if (ret)
1068 return ret;
1069 break;
1070 case ETHTOOL_ID_INACTIVE:
1071 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1072 HNS_PHY_PAGE_LED);
1073 if (ret)
1074 return ret;
1076 ret = phy_write(phy_dev, HNS_LED_FC_REG,
1077 priv->phy_led_val);
1078 if (ret)
1079 return ret;
1081 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1082 HNS_PHY_PAGE_COPPER);
1083 if (ret)
1084 return ret;
1085 break;
1086 default:
1087 return -EINVAL;
1089 else
1090 switch (state) {
1091 case ETHTOOL_ID_ACTIVE:
1092 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1093 case ETHTOOL_ID_ON:
1094 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1095 case ETHTOOL_ID_OFF:
1096 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1097 case ETHTOOL_ID_INACTIVE:
1098 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1099 default:
1100 return -EINVAL;
1103 return 0;
1107 * hns_get_regs - get net device register
1108 * @dev: net device
1109 * @cmd: ethtool cmd
1110 * @date: register data
1112 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1113 void *data)
1115 struct hns_nic_priv *priv = netdev_priv(net_dev);
1116 struct hnae_ae_ops *ops;
1118 ops = priv->ae_handle->dev->ops;
1120 cmd->version = HNS_CHIP_VERSION;
1121 if (!ops->get_regs) {
1122 netdev_err(net_dev, "ops->get_regs is null!\n");
1123 return;
1125 ops->get_regs(priv->ae_handle, data);
1129 * nic_get_regs_len - get total register len.
1130 * @dev: net device
1132 * Return total register len.
1134 static int hns_get_regs_len(struct net_device *net_dev)
1136 u32 reg_num;
1137 struct hns_nic_priv *priv = netdev_priv(net_dev);
1138 struct hnae_ae_ops *ops;
1140 ops = priv->ae_handle->dev->ops;
1141 if (!ops->get_regs_len) {
1142 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1143 return -EOPNOTSUPP;
1146 reg_num = ops->get_regs_len(priv->ae_handle);
1147 if (reg_num > 0)
1148 return reg_num * sizeof(u32);
1149 else
1150 return reg_num; /* error code */
1154 * hns_nic_nway_reset - nway reset
1155 * @dev: net device
1157 * Return 0 on success, negative on failure
1159 static int hns_nic_nway_reset(struct net_device *netdev)
1161 int ret = 0;
1162 struct hns_nic_priv *priv = netdev_priv(netdev);
1163 struct phy_device *phy = priv->phy;
1165 if (netif_running(netdev)) {
1166 if (phy)
1167 ret = genphy_restart_aneg(phy);
1170 return ret;
1173 static u32
1174 hns_get_rss_key_size(struct net_device *netdev)
1176 struct hns_nic_priv *priv = netdev_priv(netdev);
1177 struct hnae_ae_ops *ops;
1179 if (AE_IS_VER1(priv->enet_ver)) {
1180 netdev_err(netdev,
1181 "RSS feature is not supported on this hardware\n");
1182 return 0;
1185 ops = priv->ae_handle->dev->ops;
1186 return ops->get_rss_key_size(priv->ae_handle);
1189 static u32
1190 hns_get_rss_indir_size(struct net_device *netdev)
1192 struct hns_nic_priv *priv = netdev_priv(netdev);
1193 struct hnae_ae_ops *ops;
1195 if (AE_IS_VER1(priv->enet_ver)) {
1196 netdev_err(netdev,
1197 "RSS feature is not supported on this hardware\n");
1198 return 0;
1201 ops = priv->ae_handle->dev->ops;
1202 return ops->get_rss_indir_size(priv->ae_handle);
1205 static int
1206 hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1208 struct hns_nic_priv *priv = netdev_priv(netdev);
1209 struct hnae_ae_ops *ops;
1211 if (AE_IS_VER1(priv->enet_ver)) {
1212 netdev_err(netdev,
1213 "RSS feature is not supported on this hardware\n");
1214 return -EOPNOTSUPP;
1217 ops = priv->ae_handle->dev->ops;
1219 if (!indir)
1220 return 0;
1222 return ops->get_rss(priv->ae_handle, indir, key, hfunc);
1225 static int
1226 hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1227 const u8 hfunc)
1229 struct hns_nic_priv *priv = netdev_priv(netdev);
1230 struct hnae_ae_ops *ops;
1232 if (AE_IS_VER1(priv->enet_ver)) {
1233 netdev_err(netdev,
1234 "RSS feature is not supported on this hardware\n");
1235 return -EOPNOTSUPP;
1238 ops = priv->ae_handle->dev->ops;
1240 /* currently hfunc can only be Toeplitz hash */
1241 if (key ||
1242 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1243 return -EOPNOTSUPP;
1244 if (!indir)
1245 return 0;
1247 return ops->set_rss(priv->ae_handle, indir, key, hfunc);
1250 static int hns_get_rxnfc(struct net_device *netdev,
1251 struct ethtool_rxnfc *cmd,
1252 u32 *rule_locs)
1254 struct hns_nic_priv *priv = netdev_priv(netdev);
1256 switch (cmd->cmd) {
1257 case ETHTOOL_GRXRINGS:
1258 cmd->data = priv->ae_handle->q_num;
1259 break;
1260 default:
1261 return -EOPNOTSUPP;
1264 return 0;
1267 static struct ethtool_ops hns_ethtool_ops = {
1268 .get_drvinfo = hns_nic_get_drvinfo,
1269 .get_link = hns_nic_get_link,
1270 .get_settings = hns_nic_get_settings,
1271 .set_settings = hns_nic_set_settings,
1272 .get_ringparam = hns_get_ringparam,
1273 .get_pauseparam = hns_get_pauseparam,
1274 .set_pauseparam = hns_set_pauseparam,
1275 .get_coalesce = hns_get_coalesce,
1276 .set_coalesce = hns_set_coalesce,
1277 .get_channels = hns_get_channels,
1278 .self_test = hns_nic_self_test,
1279 .get_strings = hns_get_strings,
1280 .get_sset_count = hns_get_sset_count,
1281 .get_ethtool_stats = hns_get_ethtool_stats,
1282 .set_phys_id = hns_set_phys_id,
1283 .get_regs_len = hns_get_regs_len,
1284 .get_regs = hns_get_regs,
1285 .nway_reset = hns_nic_nway_reset,
1286 .get_rxfh_key_size = hns_get_rss_key_size,
1287 .get_rxfh_indir_size = hns_get_rss_indir_size,
1288 .get_rxfh = hns_get_rss,
1289 .set_rxfh = hns_set_rss,
1290 .get_rxnfc = hns_get_rxnfc,
1293 void hns_ethtool_set_ops(struct net_device *ndev)
1295 ndev->ethtool_ops = &hns_ethtool_ops;