1
2
3
4
5
6#include <linux/etherdevice.h>
7#include <linux/interrupt.h>
8#include <linux/module.h>
9#include <linux/platform_device.h>
10#include "hns_enet.h"
11
12#define HNS_PHY_PAGE_MDIX 0
13#define HNS_PHY_PAGE_LED 3
14#define HNS_PHY_PAGE_COPPER 0
15
16#define HNS_PHY_PAGE_REG 22
17#define HNS_PHY_CSC_REG 16
18#define HNS_PHY_CSS_REG 17
19#define HNS_LED_FC_REG 16
20
21#define HNS_LED_FORCE_ON 9
22#define HNS_LED_FORCE_OFF 8
23
24#define HNS_CHIP_VERSION 660
25#define HNS_NET_STATS_CNT 26
26
27#define PHY_MDIX_CTRL_S (5)
28#define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
29
30#define PHY_MDIX_STATUS_B (6)
31#define PHY_SPEED_DUP_RESOLVE_B (11)
32
33
34
35
36
37
38static u32 hns_nic_get_link(struct net_device *net_dev)
39{
40 struct hns_nic_priv *priv = netdev_priv(net_dev);
41 u32 link_stat = priv->link;
42 struct hnae_handle *h;
43
44 h = priv->ae_handle;
45
46 if (net_dev->phydev) {
47 if (!genphy_read_status(net_dev->phydev))
48 link_stat = net_dev->phydev->link;
49 else
50 link_stat = 0;
51 }
52
53 if (h->dev && h->dev->ops && h->dev->ops->get_status)
54 link_stat = link_stat && h->dev->ops->get_status(h);
55 else
56 link_stat = 0;
57
58 return link_stat;
59}
60
61static void hns_get_mdix_mode(struct net_device *net_dev,
62 struct ethtool_link_ksettings *cmd)
63{
64 int mdix_ctrl, mdix, retval, is_resolved;
65 struct phy_device *phy_dev = net_dev->phydev;
66
67 if (!phy_dev || !phy_dev->mdio.bus) {
68 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
69 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
70 return;
71 }
72
73 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_MDIX);
74
75 retval = phy_read(phy_dev, HNS_PHY_CSC_REG);
76 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
77
78 retval = phy_read(phy_dev, HNS_PHY_CSS_REG);
79 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
80 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
81
82 phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
83
84 switch (mdix_ctrl) {
85 case 0x0:
86 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI;
87 break;
88 case 0x1:
89 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_X;
90 break;
91 case 0x3:
92 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
93 break;
94 default:
95 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
96 break;
97 }
98
99 if (!is_resolved)
100 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
101 else if (mdix)
102 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
103 else
104 cmd->base.eth_tp_mdix = ETH_TP_MDI;
105}
106
107
108
109
110
111
112
113static int hns_nic_get_link_ksettings(struct net_device *net_dev,
114 struct ethtool_link_ksettings *cmd)
115{
116 struct hns_nic_priv *priv = netdev_priv(net_dev);
117 struct hnae_handle *h;
118 u32 link_stat;
119 int ret;
120 u8 duplex;
121 u16 speed;
122 u32 supported, advertising;
123
124 if (!priv || !priv->ae_handle)
125 return -ESRCH;
126
127 h = priv->ae_handle;
128 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
129 return -ESRCH;
130
131 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
132 if (ret < 0) {
133 netdev_err(net_dev, "%s get_info error!\n", __func__);
134 return -EINVAL;
135 }
136
137 ethtool_convert_link_mode_to_legacy_u32(&supported,
138 cmd->link_modes.supported);
139 ethtool_convert_link_mode_to_legacy_u32(&advertising,
140 cmd->link_modes.advertising);
141
142
143 cmd->base.autoneg = false;
144 cmd->base.speed = speed;
145 cmd->base.duplex = duplex;
146
147 if (net_dev->phydev)
148 phy_ethtool_ksettings_get(net_dev->phydev, cmd);
149
150 link_stat = hns_nic_get_link(net_dev);
151 if (!link_stat) {
152 cmd->base.speed = (u32)SPEED_UNKNOWN;
153 cmd->base.duplex = DUPLEX_UNKNOWN;
154 }
155
156 if (cmd->base.autoneg)
157 advertising |= ADVERTISED_Autoneg;
158
159 supported |= h->if_support;
160 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
161 supported |= SUPPORTED_TP;
162 advertising |= ADVERTISED_1000baseT_Full;
163 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
164 supported |= SUPPORTED_FIBRE;
165 advertising |= ADVERTISED_10000baseKR_Full;
166 }
167
168 switch (h->media_type) {
169 case HNAE_MEDIA_TYPE_FIBER:
170 cmd->base.port = PORT_FIBRE;
171 break;
172 case HNAE_MEDIA_TYPE_COPPER:
173 cmd->base.port = PORT_TP;
174 break;
175 case HNAE_MEDIA_TYPE_UNKNOWN:
176 default:
177 break;
178 }
179
180 if (!(AE_IS_VER1(priv->enet_ver) && h->port_type == HNAE_PORT_DEBUG))
181 supported |= SUPPORTED_Pause;
182
183 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
184 supported);
185 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
186 advertising);
187
188 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22;
189 hns_get_mdix_mode(net_dev, cmd);
190
191 return 0;
192}
193
194
195
196
197
198
199
200static int hns_nic_set_link_ksettings(struct net_device *net_dev,
201 const struct ethtool_link_ksettings *cmd)
202{
203 struct hns_nic_priv *priv = netdev_priv(net_dev);
204 struct hnae_handle *h;
205 u32 speed;
206
207 if (!netif_running(net_dev))
208 return -ESRCH;
209
210 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
211 !priv->ae_handle->dev->ops)
212 return -ENODEV;
213
214 h = priv->ae_handle;
215 speed = cmd->base.speed;
216
217 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
218 if (cmd->base.autoneg == AUTONEG_ENABLE ||
219 speed != SPEED_10000 ||
220 cmd->base.duplex != DUPLEX_FULL)
221 return -EINVAL;
222 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
223 if (!net_dev->phydev && cmd->base.autoneg == AUTONEG_ENABLE)
224 return -EINVAL;
225
226 if (speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
227 return -EINVAL;
228 if (net_dev->phydev)
229 return phy_ethtool_ksettings_set(net_dev->phydev, cmd);
230
231 if ((speed != SPEED_10 && speed != SPEED_100 &&
232 speed != SPEED_1000) || (cmd->base.duplex != DUPLEX_HALF &&
233 cmd->base.duplex != DUPLEX_FULL))
234 return -EINVAL;
235 } else {
236 netdev_err(net_dev, "Not supported!");
237 return -ENOTSUPP;
238 }
239
240 if (h->dev->ops->adjust_link) {
241 netif_carrier_off(net_dev);
242 h->dev->ops->adjust_link(h, (int)speed, cmd->base.duplex);
243 netif_carrier_on(net_dev);
244 return 0;
245 }
246
247 netdev_err(net_dev, "Not supported!");
248 return -ENOTSUPP;
249}
250
251static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
252 "Mac Loopback test",
253 "Serdes Loopback test",
254 "Phy Loopback test"
255};
256
257static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
258{
259 int err;
260
261 if (en) {
262
263
264
265 err = phy_resume(phy_dev);
266 if (err)
267 goto out;
268
269 err = phy_loopback(phy_dev, true);
270 } else {
271 err = phy_loopback(phy_dev, false);
272 if (err)
273 goto out;
274
275 err = phy_suspend(phy_dev);
276 }
277
278out:
279 return err;
280}
281
282static int __lb_setup(struct net_device *ndev,
283 enum hnae_loop loop)
284{
285 int ret = 0;
286 struct hns_nic_priv *priv = netdev_priv(ndev);
287 struct phy_device *phy_dev = ndev->phydev;
288 struct hnae_handle *h = priv->ae_handle;
289
290 switch (loop) {
291 case MAC_INTERNALLOOP_PHY:
292 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
293 if (!ret)
294 ret = h->dev->ops->set_loopback(h, loop, 0x1);
295 break;
296 case MAC_INTERNALLOOP_MAC:
297 if ((h->dev->ops->set_loopback) &&
298 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
299 ret = h->dev->ops->set_loopback(h, loop, 0x1);
300 break;
301 case MAC_INTERNALLOOP_SERDES:
302 if (h->dev->ops->set_loopback)
303 ret = h->dev->ops->set_loopback(h, loop, 0x1);
304 break;
305 case MAC_LOOP_PHY_NONE:
306 ret = hns_nic_config_phy_loopback(phy_dev, 0x0);
307 fallthrough;
308 case MAC_LOOP_NONE:
309 if (!ret && h->dev->ops->set_loopback) {
310 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
311 ret = h->dev->ops->set_loopback(h,
312 MAC_INTERNALLOOP_MAC, 0x0);
313
314 if (!ret)
315 ret = h->dev->ops->set_loopback(h,
316 MAC_INTERNALLOOP_SERDES, 0x0);
317 }
318 break;
319 default:
320 ret = -EINVAL;
321 break;
322 }
323
324 if (!ret) {
325 if (loop == MAC_LOOP_NONE)
326 h->dev->ops->set_promisc_mode(
327 h, ndev->flags & IFF_PROMISC);
328 else
329 h->dev->ops->set_promisc_mode(h, 1);
330 }
331 return ret;
332}
333
334static int __lb_up(struct net_device *ndev,
335 enum hnae_loop loop_mode)
336{
337#define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
338 struct hns_nic_priv *priv = netdev_priv(ndev);
339 struct hnae_handle *h = priv->ae_handle;
340 int speed, duplex;
341 int ret;
342
343 hns_nic_net_reset(ndev);
344
345 ret = __lb_setup(ndev, loop_mode);
346 if (ret)
347 return ret;
348
349 msleep(200);
350
351 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
352 if (ret)
353 return ret;
354
355
356 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
357 speed = 1000;
358 else
359 speed = 10000;
360 duplex = 1;
361
362 h->dev->ops->adjust_link(h, speed, duplex);
363
364
365 msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
366
367 return 0;
368}
369
370static void __lb_other_process(struct hns_nic_ring_data *ring_data,
371 struct sk_buff *skb)
372{
373 struct net_device *ndev;
374 struct hns_nic_priv *priv;
375 struct hnae_ring *ring;
376 struct netdev_queue *dev_queue;
377 struct sk_buff *new_skb;
378 unsigned int frame_size;
379 int check_ok;
380 u32 i;
381 char buff[33];
382
383 if (!ring_data) {
384 ndev = skb->dev;
385 priv = netdev_priv(ndev);
386
387 frame_size = skb->len;
388 memset(skb->data, 0xFF, frame_size);
389 if ((!AE_IS_VER1(priv->enet_ver)) &&
390 (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
391 memcpy(skb->data, ndev->dev_addr, 6);
392 skb->data[5] += 0x1f;
393 }
394
395 frame_size &= ~1ul;
396 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
397 memset(&skb->data[frame_size / 2 + 10], 0xBE,
398 frame_size / 2 - 11);
399 memset(&skb->data[frame_size / 2 + 12], 0xAF,
400 frame_size / 2 - 13);
401 return;
402 }
403
404 ring = ring_data->ring;
405 ndev = ring_data->napi.dev;
406 if (is_tx_ring(ring)) {
407 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
408 netdev_tx_reset_queue(dev_queue);
409 return;
410 }
411
412 frame_size = skb->len;
413 frame_size &= ~1ul;
414
415 new_skb = skb_copy(skb, GFP_ATOMIC);
416 dev_kfree_skb_any(skb);
417 if (!new_skb) {
418 netdev_err(ndev, "skb alloc failed\n");
419 return;
420 }
421 skb = new_skb;
422
423 check_ok = 0;
424 if (*(skb->data + 10) == 0xFF) {
425 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
426 (*(skb->data + frame_size / 2 + 12) == 0xAF))
427 check_ok = 1;
428 }
429
430 if (check_ok) {
431 ndev->stats.rx_packets++;
432 ndev->stats.rx_bytes += skb->len;
433 } else {
434 ndev->stats.rx_frame_errors++;
435 for (i = 0; i < skb->len; i++) {
436 snprintf(buff + i % 16 * 2, 3,
437 "%02x", *(skb->data + i));
438 if ((i % 16 == 15) || (i == skb->len - 1))
439 pr_info("%s\n", buff);
440 }
441 }
442 dev_kfree_skb_any(skb);
443}
444
445static int __lb_clean_rings(struct hns_nic_priv *priv,
446 int ringid0, int ringid1, int budget)
447{
448 int i, ret;
449 struct hns_nic_ring_data *ring_data;
450 struct net_device *ndev = priv->netdev;
451 unsigned long rx_packets = ndev->stats.rx_packets;
452 unsigned long rx_bytes = ndev->stats.rx_bytes;
453 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
454
455 for (i = ringid0; i <= ringid1; i++) {
456 ring_data = &priv->ring_data[i];
457 (void)ring_data->poll_one(ring_data,
458 budget, __lb_other_process);
459 }
460 ret = (int)(ndev->stats.rx_packets - rx_packets);
461 ndev->stats.rx_packets = rx_packets;
462 ndev->stats.rx_bytes = rx_bytes;
463 ndev->stats.rx_frame_errors = rx_frame_errors;
464 return ret;
465}
466
467
468
469
470
471
472static int __lb_run_test(struct net_device *ndev,
473 enum hnae_loop loop_mode)
474{
475#define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
476#define NIC_LB_TEST_RING_ID 0
477#define NIC_LB_TEST_FRAME_SIZE 128
478
479#define NIC_LB_TEST_NO_MEM_ERR 1
480#define NIC_LB_TEST_TX_CNT_ERR 2
481#define NIC_LB_TEST_RX_CNT_ERR 3
482
483 struct hns_nic_priv *priv = netdev_priv(ndev);
484 struct hnae_handle *h = priv->ae_handle;
485 int i, j, lc, good_cnt, ret_val = 0;
486 unsigned int size;
487 netdev_tx_t tx_ret_val;
488 struct sk_buff *skb;
489
490 size = NIC_LB_TEST_FRAME_SIZE;
491
492 skb = alloc_skb(size, GFP_KERNEL);
493 if (!skb)
494 return NIC_LB_TEST_NO_MEM_ERR;
495
496
497 (void)skb_put(skb, size);
498 skb->dev = ndev;
499 __lb_other_process(NULL, skb);
500 skb->queue_mapping = NIC_LB_TEST_RING_ID;
501
502 lc = 1;
503 for (j = 0; j < lc; j++) {
504
505 good_cnt = 0;
506
507 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
508 (void)skb_get(skb);
509
510 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
511 ndev, skb,
512 &tx_ring_data(priv, skb->queue_mapping));
513 if (tx_ret_val == NETDEV_TX_OK)
514 good_cnt++;
515 else
516 break;
517 }
518 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
519 ret_val = NIC_LB_TEST_TX_CNT_ERR;
520 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
521 hns_nic_test_strs[loop_mode], good_cnt,
522 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
523 break;
524 }
525
526
527 msleep(100);
528
529 good_cnt = __lb_clean_rings(priv,
530 h->q_num, h->q_num * 2 - 1,
531 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
532 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
533 ret_val = NIC_LB_TEST_RX_CNT_ERR;
534 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
535 hns_nic_test_strs[loop_mode], good_cnt,
536 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
537 break;
538 }
539 (void)__lb_clean_rings(priv,
540 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
541 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
542 }
543
544
545 kfree_skb(skb);
546
547 return ret_val;
548}
549
550static int __lb_down(struct net_device *ndev, enum hnae_loop loop)
551{
552 struct hns_nic_priv *priv = netdev_priv(ndev);
553 struct hnae_handle *h = priv->ae_handle;
554 int ret;
555
556 if (loop == MAC_INTERNALLOOP_PHY)
557 ret = __lb_setup(ndev, MAC_LOOP_PHY_NONE);
558 else
559 ret = __lb_setup(ndev, MAC_LOOP_NONE);
560 if (ret)
561 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
562 __func__,
563 ret);
564
565 if (h->dev->ops->stop)
566 h->dev->ops->stop(h);
567
568 usleep_range(10000, 20000);
569 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
570
571 hns_nic_net_reset(ndev);
572
573 return 0;
574}
575
576
577
578
579
580
581
582static void hns_nic_self_test(struct net_device *ndev,
583 struct ethtool_test *eth_test, u64 *data)
584{
585 struct hns_nic_priv *priv = netdev_priv(ndev);
586 bool if_running = netif_running(ndev);
587#define SELF_TEST_TPYE_NUM 3
588 int st_param[SELF_TEST_TPYE_NUM][2];
589 int i;
590 int test_index = 0;
591
592 st_param[0][0] = MAC_INTERNALLOOP_MAC;
593 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
594 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
595 st_param[1][1] = 1;
596 st_param[2][0] = MAC_INTERNALLOOP_PHY;
597 st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
598 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
599
600 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
601 set_bit(NIC_STATE_TESTING, &priv->state);
602
603 if (if_running)
604 dev_close(ndev);
605
606 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
607 if (!st_param[i][1])
608 continue;
609
610 data[test_index] = __lb_up(ndev,
611 (enum hnae_loop)st_param[i][0]);
612 if (!data[test_index]) {
613 data[test_index] = __lb_run_test(
614 ndev, (enum hnae_loop)st_param[i][0]);
615 (void)__lb_down(ndev,
616 (enum hnae_loop)st_param[i][0]);
617 }
618
619 if (data[test_index])
620 eth_test->flags |= ETH_TEST_FL_FAILED;
621
622 test_index++;
623 }
624
625 hns_nic_net_reset(priv->netdev);
626
627 clear_bit(NIC_STATE_TESTING, &priv->state);
628
629 if (if_running)
630 (void)dev_open(ndev, NULL);
631 }
632
633
634 (void)msleep_interruptible(4 * 1000);
635}
636
637
638
639
640
641
642static void hns_nic_get_drvinfo(struct net_device *net_dev,
643 struct ethtool_drvinfo *drvinfo)
644{
645 struct hns_nic_priv *priv = netdev_priv(net_dev);
646
647 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
648 sizeof(drvinfo->version));
649 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
650
651 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
652 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
653
654 strncpy(drvinfo->bus_info, priv->dev->bus->name,
655 sizeof(drvinfo->bus_info));
656 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
657
658 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
659 drvinfo->eedump_len = 0;
660}
661
662
663
664
665
666
667static void hns_get_ringparam(struct net_device *net_dev,
668 struct ethtool_ringparam *param)
669{
670 struct hns_nic_priv *priv = netdev_priv(net_dev);
671 struct hnae_ae_ops *ops;
672 struct hnae_queue *queue;
673 u32 uplimit = 0;
674
675 queue = priv->ae_handle->qs[0];
676 ops = priv->ae_handle->dev->ops;
677
678 if (ops->get_ring_bdnum_limit)
679 ops->get_ring_bdnum_limit(queue, &uplimit);
680
681 param->rx_max_pending = uplimit;
682 param->tx_max_pending = uplimit;
683 param->rx_pending = queue->rx_ring.desc_num;
684 param->tx_pending = queue->tx_ring.desc_num;
685}
686
687
688
689
690
691
692static void hns_get_pauseparam(struct net_device *net_dev,
693 struct ethtool_pauseparam *param)
694{
695 struct hns_nic_priv *priv = netdev_priv(net_dev);
696 struct hnae_ae_ops *ops;
697
698 ops = priv->ae_handle->dev->ops;
699
700 if (ops->get_pauseparam)
701 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
702 ¶m->rx_pause, ¶m->tx_pause);
703}
704
705
706
707
708
709
710
711
712static int hns_set_pauseparam(struct net_device *net_dev,
713 struct ethtool_pauseparam *param)
714{
715 struct hns_nic_priv *priv = netdev_priv(net_dev);
716 struct hnae_handle *h;
717 struct hnae_ae_ops *ops;
718
719 h = priv->ae_handle;
720 ops = h->dev->ops;
721
722 if (!ops->set_pauseparam)
723 return -ESRCH;
724
725 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
726 param->rx_pause, param->tx_pause);
727}
728
729
730
731
732
733
734
735
736static int hns_get_coalesce(struct net_device *net_dev,
737 struct ethtool_coalesce *ec)
738{
739 struct hns_nic_priv *priv = netdev_priv(net_dev);
740 struct hnae_ae_ops *ops;
741
742 ops = priv->ae_handle->dev->ops;
743
744 ec->use_adaptive_rx_coalesce = priv->ae_handle->coal_adapt_en;
745 ec->use_adaptive_tx_coalesce = priv->ae_handle->coal_adapt_en;
746
747 if ((!ops->get_coalesce_usecs) ||
748 (!ops->get_max_coalesced_frames))
749 return -ESRCH;
750
751 ops->get_coalesce_usecs(priv->ae_handle,
752 &ec->tx_coalesce_usecs,
753 &ec->rx_coalesce_usecs);
754
755 ops->get_max_coalesced_frames(
756 priv->ae_handle,
757 &ec->tx_max_coalesced_frames,
758 &ec->rx_max_coalesced_frames);
759
760 ops->get_coalesce_range(priv->ae_handle,
761 &ec->tx_max_coalesced_frames_low,
762 &ec->rx_max_coalesced_frames_low,
763 &ec->tx_max_coalesced_frames_high,
764 &ec->rx_max_coalesced_frames_high,
765 &ec->tx_coalesce_usecs_low,
766 &ec->rx_coalesce_usecs_low,
767 &ec->tx_coalesce_usecs_high,
768 &ec->rx_coalesce_usecs_high);
769
770 return 0;
771}
772
773
774
775
776
777
778
779
780static int hns_set_coalesce(struct net_device *net_dev,
781 struct ethtool_coalesce *ec)
782{
783 struct hns_nic_priv *priv = netdev_priv(net_dev);
784 struct hnae_ae_ops *ops;
785 int rc1, rc2;
786
787 ops = priv->ae_handle->dev->ops;
788
789 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
790 return -EINVAL;
791
792 if ((!ops->set_coalesce_usecs) ||
793 (!ops->set_coalesce_frames))
794 return -ESRCH;
795
796 if (ec->use_adaptive_rx_coalesce != priv->ae_handle->coal_adapt_en)
797 priv->ae_handle->coal_adapt_en = ec->use_adaptive_rx_coalesce;
798
799 rc1 = ops->set_coalesce_usecs(priv->ae_handle,
800 ec->rx_coalesce_usecs);
801
802 rc2 = ops->set_coalesce_frames(priv->ae_handle,
803 ec->tx_max_coalesced_frames,
804 ec->rx_max_coalesced_frames);
805
806 if (rc1 || rc2)
807 return -EINVAL;
808
809 return 0;
810}
811
812
813
814
815
816
817static void
818hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
819{
820 struct hns_nic_priv *priv = netdev_priv(net_dev);
821
822 ch->max_rx = priv->ae_handle->q_num;
823 ch->max_tx = priv->ae_handle->q_num;
824
825 ch->rx_count = priv->ae_handle->q_num;
826 ch->tx_count = priv->ae_handle->q_num;
827}
828
829
830
831
832
833
834
835static void hns_get_ethtool_stats(struct net_device *netdev,
836 struct ethtool_stats *stats, u64 *data)
837{
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;
843
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;
847 }
848
849 h->dev->ops->update_stats(h, &netdev->stats);
850
851 net_stats = dev_get_stats(netdev, &temp);
852
853
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;
877
878 p[23] = netdev->rx_dropped.counter;
879 p[24] = netdev->tx_dropped.counter;
880
881 p[25] = priv->tx_timeout_count;
882
883
884 h->dev->ops->get_stats(h, &p[26]);
885}
886
887
888
889
890
891
892
893static void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
894{
895 struct hns_nic_priv *priv = netdev_priv(netdev);
896 struct hnae_handle *h = priv->ae_handle;
897 u8 *buff = data;
898
899 if (!h->dev->ops->get_strings) {
900 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
901 return;
902 }
903
904 if (stringset == ETH_SS_TEST) {
905 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
906 ethtool_sprintf(&buff,
907 hns_nic_test_strs[MAC_INTERNALLOOP_MAC]);
908 ethtool_sprintf(&buff,
909 hns_nic_test_strs[MAC_INTERNALLOOP_SERDES]);
910 if ((netdev->phydev) && (!netdev->phydev->is_c45))
911 ethtool_sprintf(&buff,
912 hns_nic_test_strs[MAC_INTERNALLOOP_PHY]);
913
914 } else {
915 ethtool_sprintf(&buff, "rx_packets");
916 ethtool_sprintf(&buff, "tx_packets");
917 ethtool_sprintf(&buff, "rx_bytes");
918 ethtool_sprintf(&buff, "tx_bytes");
919 ethtool_sprintf(&buff, "rx_errors");
920 ethtool_sprintf(&buff, "tx_errors");
921 ethtool_sprintf(&buff, "rx_dropped");
922 ethtool_sprintf(&buff, "tx_dropped");
923 ethtool_sprintf(&buff, "multicast");
924 ethtool_sprintf(&buff, "collisions");
925 ethtool_sprintf(&buff, "rx_over_errors");
926 ethtool_sprintf(&buff, "rx_crc_errors");
927 ethtool_sprintf(&buff, "rx_frame_errors");
928 ethtool_sprintf(&buff, "rx_fifo_errors");
929 ethtool_sprintf(&buff, "rx_missed_errors");
930 ethtool_sprintf(&buff, "tx_aborted_errors");
931 ethtool_sprintf(&buff, "tx_carrier_errors");
932 ethtool_sprintf(&buff, "tx_fifo_errors");
933 ethtool_sprintf(&buff, "tx_heartbeat_errors");
934 ethtool_sprintf(&buff, "rx_length_errors");
935 ethtool_sprintf(&buff, "tx_window_errors");
936 ethtool_sprintf(&buff, "rx_compressed");
937 ethtool_sprintf(&buff, "tx_compressed");
938 ethtool_sprintf(&buff, "netdev_rx_dropped");
939 ethtool_sprintf(&buff, "netdev_tx_dropped");
940
941 ethtool_sprintf(&buff, "netdev_tx_timeout");
942
943 h->dev->ops->get_strings(h, stringset, buff);
944 }
945}
946
947
948
949
950
951
952
953
954static int hns_get_sset_count(struct net_device *netdev, int stringset)
955{
956 struct hns_nic_priv *priv = netdev_priv(netdev);
957 struct hnae_handle *h = priv->ae_handle;
958 struct hnae_ae_ops *ops = h->dev->ops;
959
960 if (!ops->get_sset_count) {
961 netdev_err(netdev, "get_sset_count is null!\n");
962 return -EOPNOTSUPP;
963 }
964 if (stringset == ETH_SS_TEST) {
965 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
966
967 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
968 cnt--;
969
970 if ((!netdev->phydev) || (netdev->phydev->is_c45))
971 cnt--;
972
973 return cnt;
974 } else if (stringset == ETH_SS_STATS) {
975 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
976 } else {
977 return -EOPNOTSUPP;
978 }
979}
980
981
982
983
984
985
986
987
988static int hns_phy_led_set(struct net_device *netdev, int value)
989{
990 int retval;
991 struct phy_device *phy_dev = netdev->phydev;
992
993 retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
994 retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
995 retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
996 if (retval) {
997 netdev_err(netdev, "mdiobus_write fail !\n");
998 return retval;
999 }
1000 return 0;
1001}
1002
1003
1004
1005
1006
1007
1008
1009
1010static int
1011hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1012{
1013 struct hns_nic_priv *priv = netdev_priv(netdev);
1014 struct hnae_handle *h = priv->ae_handle;
1015 struct phy_device *phy_dev = netdev->phydev;
1016 int ret;
1017
1018 if (phy_dev)
1019 switch (state) {
1020 case ETHTOOL_ID_ACTIVE:
1021 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1022 HNS_PHY_PAGE_LED);
1023 if (ret)
1024 return ret;
1025
1026 priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
1027
1028 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1029 HNS_PHY_PAGE_COPPER);
1030 if (ret)
1031 return ret;
1032 return 2;
1033 case ETHTOOL_ID_ON:
1034 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1035 if (ret)
1036 return ret;
1037 break;
1038 case ETHTOOL_ID_OFF:
1039 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1040 if (ret)
1041 return ret;
1042 break;
1043 case ETHTOOL_ID_INACTIVE:
1044 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1045 HNS_PHY_PAGE_LED);
1046 if (ret)
1047 return ret;
1048
1049 ret = phy_write(phy_dev, HNS_LED_FC_REG,
1050 priv->phy_led_val);
1051 if (ret)
1052 return ret;
1053
1054 ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1055 HNS_PHY_PAGE_COPPER);
1056 if (ret)
1057 return ret;
1058 break;
1059 default:
1060 return -EINVAL;
1061 }
1062 else
1063 switch (state) {
1064 case ETHTOOL_ID_ACTIVE:
1065 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1066 case ETHTOOL_ID_ON:
1067 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1068 case ETHTOOL_ID_OFF:
1069 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1070 case ETHTOOL_ID_INACTIVE:
1071 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1072 default:
1073 return -EINVAL;
1074 }
1075
1076 return 0;
1077}
1078
1079
1080
1081
1082
1083
1084
1085static void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1086 void *data)
1087{
1088 struct hns_nic_priv *priv = netdev_priv(net_dev);
1089 struct hnae_ae_ops *ops;
1090
1091 ops = priv->ae_handle->dev->ops;
1092
1093 cmd->version = HNS_CHIP_VERSION;
1094 if (!ops->get_regs) {
1095 netdev_err(net_dev, "ops->get_regs is null!\n");
1096 return;
1097 }
1098 ops->get_regs(priv->ae_handle, data);
1099}
1100
1101
1102
1103
1104
1105
1106
1107static int hns_get_regs_len(struct net_device *net_dev)
1108{
1109 u32 reg_num;
1110 struct hns_nic_priv *priv = netdev_priv(net_dev);
1111 struct hnae_ae_ops *ops;
1112
1113 ops = priv->ae_handle->dev->ops;
1114 if (!ops->get_regs_len) {
1115 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1116 return -EOPNOTSUPP;
1117 }
1118
1119 reg_num = ops->get_regs_len(priv->ae_handle);
1120 if (reg_num > 0)
1121 return reg_num * sizeof(u32);
1122 else
1123 return reg_num;
1124}
1125
1126
1127
1128
1129
1130
1131
1132static int hns_nic_nway_reset(struct net_device *netdev)
1133{
1134 struct phy_device *phy = netdev->phydev;
1135
1136 if (!netif_running(netdev))
1137 return 0;
1138
1139 if (!phy)
1140 return -EOPNOTSUPP;
1141
1142 if (phy->autoneg != AUTONEG_ENABLE)
1143 return -EINVAL;
1144
1145 return genphy_restart_aneg(phy);
1146}
1147
1148static u32
1149hns_get_rss_key_size(struct net_device *netdev)
1150{
1151 struct hns_nic_priv *priv = netdev_priv(netdev);
1152 struct hnae_ae_ops *ops;
1153
1154 if (AE_IS_VER1(priv->enet_ver)) {
1155 netdev_err(netdev,
1156 "RSS feature is not supported on this hardware\n");
1157 return 0;
1158 }
1159
1160 ops = priv->ae_handle->dev->ops;
1161 return ops->get_rss_key_size(priv->ae_handle);
1162}
1163
1164static u32
1165hns_get_rss_indir_size(struct net_device *netdev)
1166{
1167 struct hns_nic_priv *priv = netdev_priv(netdev);
1168 struct hnae_ae_ops *ops;
1169
1170 if (AE_IS_VER1(priv->enet_ver)) {
1171 netdev_err(netdev,
1172 "RSS feature is not supported on this hardware\n");
1173 return 0;
1174 }
1175
1176 ops = priv->ae_handle->dev->ops;
1177 return ops->get_rss_indir_size(priv->ae_handle);
1178}
1179
1180static int
1181hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1182{
1183 struct hns_nic_priv *priv = netdev_priv(netdev);
1184 struct hnae_ae_ops *ops;
1185
1186 if (AE_IS_VER1(priv->enet_ver)) {
1187 netdev_err(netdev,
1188 "RSS feature is not supported on this hardware\n");
1189 return -EOPNOTSUPP;
1190 }
1191
1192 ops = priv->ae_handle->dev->ops;
1193
1194 if (!indir)
1195 return 0;
1196
1197 return ops->get_rss(priv->ae_handle, indir, key, hfunc);
1198}
1199
1200static int
1201hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1202 const u8 hfunc)
1203{
1204 struct hns_nic_priv *priv = netdev_priv(netdev);
1205 struct hnae_ae_ops *ops;
1206
1207 if (AE_IS_VER1(priv->enet_ver)) {
1208 netdev_err(netdev,
1209 "RSS feature is not supported on this hardware\n");
1210 return -EOPNOTSUPP;
1211 }
1212
1213 ops = priv->ae_handle->dev->ops;
1214
1215 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
1216 netdev_err(netdev, "Invalid hfunc!\n");
1217 return -EOPNOTSUPP;
1218 }
1219
1220 return ops->set_rss(priv->ae_handle, indir, key, hfunc);
1221}
1222
1223static int hns_get_rxnfc(struct net_device *netdev,
1224 struct ethtool_rxnfc *cmd,
1225 u32 *rule_locs)
1226{
1227 struct hns_nic_priv *priv = netdev_priv(netdev);
1228
1229 switch (cmd->cmd) {
1230 case ETHTOOL_GRXRINGS:
1231 cmd->data = priv->ae_handle->q_num;
1232 break;
1233 default:
1234 return -EOPNOTSUPP;
1235 }
1236
1237 return 0;
1238}
1239
1240static const struct ethtool_ops hns_ethtool_ops = {
1241 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1242 ETHTOOL_COALESCE_MAX_FRAMES |
1243 ETHTOOL_COALESCE_USE_ADAPTIVE |
1244 ETHTOOL_COALESCE_USECS_LOW_HIGH |
1245 ETHTOOL_COALESCE_MAX_FRAMES_LOW_HIGH,
1246 .get_drvinfo = hns_nic_get_drvinfo,
1247 .get_link = hns_nic_get_link,
1248 .get_ringparam = hns_get_ringparam,
1249 .get_pauseparam = hns_get_pauseparam,
1250 .set_pauseparam = hns_set_pauseparam,
1251 .get_coalesce = hns_get_coalesce,
1252 .set_coalesce = hns_set_coalesce,
1253 .get_channels = hns_get_channels,
1254 .self_test = hns_nic_self_test,
1255 .get_strings = hns_get_strings,
1256 .get_sset_count = hns_get_sset_count,
1257 .get_ethtool_stats = hns_get_ethtool_stats,
1258 .set_phys_id = hns_set_phys_id,
1259 .get_regs_len = hns_get_regs_len,
1260 .get_regs = hns_get_regs,
1261 .nway_reset = hns_nic_nway_reset,
1262 .get_rxfh_key_size = hns_get_rss_key_size,
1263 .get_rxfh_indir_size = hns_get_rss_indir_size,
1264 .get_rxfh = hns_get_rss,
1265 .set_rxfh = hns_set_rss,
1266 .get_rxnfc = hns_get_rxnfc,
1267 .get_link_ksettings = hns_nic_get_link_ksettings,
1268 .set_link_ksettings = hns_nic_set_link_ksettings,
1269};
1270
1271void hns_ethtool_set_ops(struct net_device *ndev)
1272{
1273 ndev->ethtool_ops = &hns_ethtool_ops;
1274}
1275