GNU Linux-libre 4.4.294-gnu1
[releases.git] / drivers / net / ethernet / hisilicon / hns / hns_ethtool.c
1 /*
2  * Copyright (c) 2014-2015 Hisilicon Limited.
3  *
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  */
9
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14
15 #include "hns_enet.h"
16
17 #define HNS_PHY_PAGE_MDIX       0
18 #define HNS_PHY_PAGE_LED        3
19 #define HNS_PHY_PAGE_COPPER     0
20
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. */
26
27 #define HNS_LED_FORCE_ON        9
28 #define HNS_LED_FORCE_OFF       8
29
30 #define HNS_CHIP_VERSION 660
31 #define HNS_NET_STATS_CNT 26
32
33 #define PHY_MDIX_CTRL_S         (5)
34 #define PHY_MDIX_CTRL_M         (3 << PHY_MDIX_CTRL_S)
35
36 #define PHY_MDIX_STATUS_B       (6)
37 #define PHY_SPEED_DUP_RESOLVE_B (11)
38
39 /**
40  *hns_nic_get_link - get current link status
41  *@net_dev: net_device
42  *retuen 0 - success , negative --fail
43  */
44 static u32 hns_nic_get_link(struct net_device *net_dev)
45 {
46         struct hns_nic_priv *priv = netdev_priv(net_dev);
47         u32 link_stat = priv->link;
48         struct hnae_handle *h;
49
50         assert(priv && priv->ae_handle);
51         h = priv->ae_handle;
52
53         if (priv->phy) {
54                 if (!genphy_update_link(priv->phy))
55                         link_stat = priv->phy->link;
56                 else
57                         link_stat = 0;
58         }
59
60         if (h->dev && h->dev->ops && h->dev->ops->get_status)
61                 link_stat = link_stat && h->dev->ops->get_status(h);
62         else
63                 link_stat = 0;
64
65         return link_stat;
66 }
67
68 static void hns_get_mdix_mode(struct net_device *net_dev,
69                               struct ethtool_cmd *cmd)
70 {
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;
74
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;
78                 return;
79         }
80
81         (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
82                             HNS_PHY_PAGE_MDIX);
83
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);
86
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);
90
91         (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
92                             HNS_PHY_PAGE_COPPER);
93
94         switch (mdix_ctrl) {
95         case 0x0:
96                 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI;
97                 break;
98         case 0x1:
99                 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_X;
100                 break;
101         case 0x3:
102                 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
103                 break;
104         default:
105                 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
106                 break;
107         }
108
109         if (!is_resolved)
110                 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
111         else if (mdix)
112                 cmd->eth_tp_mdix = ETH_TP_MDI_X;
113         else
114                 cmd->eth_tp_mdix = ETH_TP_MDI;
115 }
116
117 /**
118  *hns_nic_get_settings - implement ethtool get settings
119  *@net_dev: net_device
120  *@cmd: ethtool_cmd
121  *retuen 0 - success , negative --fail
122  */
123 static int hns_nic_get_settings(struct net_device *net_dev,
124                                 struct ethtool_cmd *cmd)
125 {
126         struct hns_nic_priv *priv = netdev_priv(net_dev);
127         struct hnae_handle *h;
128         u32 link_stat;
129         int ret;
130         u8 duplex;
131         u16 speed;
132
133         if (!priv || !priv->ae_handle)
134                 return -ESRCH;
135
136         h = priv->ae_handle;
137         if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
138                 return -ESRCH;
139
140         ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
141         if (ret < 0) {
142                 netdev_err(net_dev, "%s get_info error!\n", __func__);
143                 return -EINVAL;
144         }
145
146         /* When there is no phy, autoneg is off. */
147         cmd->autoneg = false;
148         ethtool_cmd_speed_set(cmd, speed);
149         cmd->duplex = duplex;
150
151         if (priv->phy)
152                 (void)phy_ethtool_gset(priv->phy, cmd);
153
154         link_stat = hns_nic_get_link(net_dev);
155         if (!link_stat) {
156                 ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
157                 cmd->duplex = DUPLEX_UNKNOWN;
158         }
159
160         if (cmd->autoneg)
161                 cmd->advertising |= ADVERTISED_Autoneg;
162
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;
170         }
171
172         if (h->port_type == HNAE_PORT_SERVICE) {
173                 cmd->port = PORT_FIBRE;
174                 cmd->supported |= SUPPORTED_Pause;
175         } else {
176                 cmd->port = PORT_TP;
177         }
178
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);
182
183         return 0;
184 }
185
186 /**
187  *hns_nic_set_settings - implement ethtool set settings
188  *@net_dev: net_device
189  *@cmd: ethtool_cmd
190  *retuen 0 - success , negative --fail
191  */
192 static int hns_nic_set_settings(struct net_device *net_dev,
193                                 struct ethtool_cmd *cmd)
194 {
195         struct hns_nic_priv *priv = netdev_priv(net_dev);
196         struct hnae_handle *h;
197         u32 speed;
198
199         if (!netif_running(net_dev))
200                 return -ESRCH;
201
202         if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
203             !priv->ae_handle->dev->ops)
204                 return -ENODEV;
205
206         h = priv->ae_handle;
207         speed = ethtool_cmd_speed(cmd);
208
209         if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
210                 if (cmd->autoneg == AUTONEG_ENABLE || speed != SPEED_10000 ||
211                     cmd->duplex != DUPLEX_FULL)
212                         return -EINVAL;
213         } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
214                 if (!priv->phy && cmd->autoneg == AUTONEG_ENABLE)
215                         return -EINVAL;
216
217                 if (speed == SPEED_1000 && cmd->duplex == DUPLEX_HALF)
218                         return -EINVAL;
219                 if (priv->phy)
220                         return phy_ethtool_sset(priv->phy, cmd);
221
222                 if ((speed != SPEED_10 && speed != SPEED_100 &&
223                      speed != SPEED_1000) || (cmd->duplex != DUPLEX_HALF &&
224                      cmd->duplex != DUPLEX_FULL))
225                         return -EINVAL;
226         } else {
227                 netdev_err(net_dev, "Not supported!");
228                 return -ENOTSUPP;
229         }
230
231         if (h->dev->ops->adjust_link) {
232                 h->dev->ops->adjust_link(h, (int)speed, cmd->duplex);
233                 return 0;
234         }
235
236         netdev_err(net_dev, "Not supported!");
237         return -ENOTSUPP;
238 }
239
240 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
241         "Mac    Loopback test",
242         "Serdes Loopback test",
243         "Phy    Loopback test"
244 };
245
246 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
247 {
248 #define COPPER_CONTROL_REG 0
249 #define PHY_LOOP_BACK BIT(14)
250         u16 val = 0;
251
252         if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
253                 return -ENOTSUPP;
254
255         if (en) {
256                 /* speed : 1000M */
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,
260                                     21, 0x1046);
261                 /* Force Master */
262                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
263                                     9, 0x1F00);
264                 /* Soft-reset */
265                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
266                                     0, 0x9140);
267                 /* If autoneg disabled,two soft-reset operations */
268                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
269                                     0, 0x9140);
270                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
271                                     22, 0xFA);
272
273                 /* Default is 0x0400 */
274                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
275                                     1, 0x418);
276
277                 /* Force 1000M Link, Default is 0x0200 */
278                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
279                                     7, 0x20C);
280                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
281                                     22, 0);
282
283                 /* Enable MAC loop-back */
284                 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
285                                         COPPER_CONTROL_REG);
286                 val |= PHY_LOOP_BACK;
287                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
288                                     COPPER_CONTROL_REG, val);
289         } else {
290                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
291                                     22, 0xFA);
292                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
293                                     1, 0x400);
294                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
295                                     7, 0x200);
296                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
297                                     22, 0);
298
299                 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
300                                         COPPER_CONTROL_REG);
301                 val &= ~PHY_LOOP_BACK;
302                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
303                                     COPPER_CONTROL_REG, val);
304         }
305         return 0;
306 }
307
308 static int __lb_setup(struct net_device *ndev,
309                       enum hnae_loop loop)
310 {
311         int ret = 0;
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;
315
316         switch (loop) {
317         case MAC_INTERNALLOOP_PHY:
318                 if ((phy_dev) && (!phy_dev->is_c45))
319                         ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
320                 break;
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);
325                 break;
326         case MAC_INTERNALLOOP_SERDES:
327                 if (h->dev->ops->set_loopback)
328                         ret = h->dev->ops->set_loopback(h, loop, 0x1);
329                 break;
330         case MAC_LOOP_NONE:
331                 if ((phy_dev) && (!phy_dev->is_c45))
332                         ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
333
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);
338
339                         ret |= h->dev->ops->set_loopback(h,
340                                 MAC_INTERNALLOOP_SERDES, 0x0);
341                 }
342                 break;
343         default:
344                 ret = -EINVAL;
345                 break;
346         }
347
348         return ret;
349 }
350
351 static int __lb_up(struct net_device *ndev,
352                    enum hnae_loop loop_mode)
353 {
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;
357         int speed, duplex;
358         int ret;
359
360         hns_nic_net_reset(ndev);
361
362         if (priv->phy) {
363                 phy_disconnect(priv->phy);
364                 msleep(100);
365
366                 ret = hns_nic_init_phy(ndev, h);
367                 if (ret)
368                         return ret;
369         }
370
371         ret = __lb_setup(ndev, loop_mode);
372         if (ret)
373                 return ret;
374
375         msleep(100);
376
377         ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
378         if (ret)
379                 return ret;
380
381         if (priv->phy)
382                 phy_start(priv->phy);
383
384         /* link adjust duplex*/
385         if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
386                 speed = 1000;
387         else
388                 speed = 10000;
389         duplex = 1;
390
391         h->dev->ops->adjust_link(h, speed, duplex);
392
393         /* wait adjust link done and phy ready */
394         msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
395
396         return 0;
397 }
398
399 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
400                                struct sk_buff *skb)
401 {
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;
407         int check_ok;
408         u32 i;
409         char buff[33]; /* 32B data and the last character '\0' */
410
411         if (!ring_data) { /* Just for doing create frame*/
412                 frame_size = skb->len;
413                 memset(skb->data, 0xFF, frame_size);
414                 frame_size &= ~1ul;
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);
420                 return;
421         }
422
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);
428                 return;
429         }
430
431         frame_size = skb->len;
432         frame_size &= ~1ul;
433         /* for mutl buffer*/
434         new_skb = skb_copy(skb, GFP_ATOMIC);
435         dev_kfree_skb_any(skb);
436         if (!new_skb) {
437                 netdev_err(ndev, "skb alloc failed\n");
438                 return;
439         }
440         skb = new_skb;
441
442         check_ok = 0;
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))
446                         check_ok = 1;
447         }
448
449         if (check_ok) {
450                 ndev->stats.rx_packets++;
451                 ndev->stats.rx_bytes += skb->len;
452         } else {
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);
459                 }
460         }
461         dev_kfree_skb_any(skb);
462 }
463
464 static int __lb_clean_rings(struct hns_nic_priv *priv,
465                             int ringid0, int ringid1, int budget)
466 {
467         int i, ret;
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;
473
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);
478         }
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;
483         return ret;
484 }
485
486 /**
487  * nic_run_loopback_test -  run loopback test
488  * @nic_dev: net device
489  * @loopback_type: loopback type
490  */
491 static int __lb_run_test(struct net_device *ndev,
492                          enum hnae_loop loop_mode)
493 {
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;
505         unsigned int size;
506         netdev_tx_t tx_ret_val;
507         struct sk_buff *skb;
508
509         size = NIC_LB_TEST_FRAME_SIZE;
510         /* allocate test skb */
511         skb = alloc_skb(size, GFP_KERNEL);
512         if (!skb)
513                 return NIC_LB_TEST_NO_MEM_ERR;
514
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;
519
520         lc = 1;
521         for (j = 0; j < lc; j++) {
522                 /* reset count of good packets */
523                 good_cnt = 0;
524                 /* place 64 packets on the transmit queue*/
525                 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
526                         (void)skb_get(skb);
527
528                         tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
529                                 ndev, skb,
530                                 &tx_ring_data(priv, skb->queue_mapping));
531                         if (tx_ret_val == NETDEV_TX_OK)
532                                 good_cnt++;
533                         else
534                                 break;
535                 }
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);
541                         break;
542                 }
543
544                 /* allow 100 milliseconds for packets to go from Tx to Rx */
545                 msleep(100);
546
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);
555                         break;
556                 }
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);
560         }
561
562         /* free the original skb */
563         kfree_skb(skb);
564
565         return ret_val;
566 }
567
568 static int __lb_down(struct net_device *ndev)
569 {
570         struct hns_nic_priv *priv = netdev_priv(ndev);
571         struct hnae_handle *h = priv->ae_handle;
572         int ret;
573
574         ret = __lb_setup(ndev, MAC_LOOP_NONE);
575         if (ret)
576                 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
577                            __func__,
578                            ret);
579
580         if (priv->phy)
581                 phy_stop(priv->phy);
582
583         if (h->dev->ops->stop)
584                 h->dev->ops->stop(h);
585
586         usleep_range(10000, 20000);
587         (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
588
589         hns_nic_net_reset(ndev);
590
591         return 0;
592 }
593
594 /**
595  * hns_nic_self_test - self test
596  * @dev: net device
597  * @eth_test: test cmd
598  * @data: test result
599  */
600 static void hns_nic_self_test(struct net_device *ndev,
601                               struct ethtool_test *eth_test, u64 *data)
602 {
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];
607         int i;
608         int test_index = 0;
609
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));
617
618         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
619                 set_bit(NIC_STATE_TESTING, &priv->state);
620
621                 if (if_running)
622                         (void)dev_close(ndev);
623
624                 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
625                         if (!st_param[i][1])
626                                 continue;       /* NEXT testing */
627
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);
634                         }
635
636                         if (data[test_index])
637                                 eth_test->flags |= ETH_TEST_FL_FAILED;
638
639                         test_index++;
640                 }
641
642                 hns_nic_net_reset(priv->netdev);
643
644                 clear_bit(NIC_STATE_TESTING, &priv->state);
645
646                 if (if_running)
647                         (void)dev_open(ndev);
648         }
649         /* Online tests aren't run; pass by default */
650
651         (void)msleep_interruptible(4 * 1000);
652 }
653
654 /**
655  * hns_nic_get_drvinfo - get net driver info
656  * @dev: net device
657  * @drvinfo: driver info
658  */
659 static void hns_nic_get_drvinfo(struct net_device *net_dev,
660                                 struct ethtool_drvinfo *drvinfo)
661 {
662         struct hns_nic_priv *priv = netdev_priv(net_dev);
663
664         assert(priv);
665
666         strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
667                 sizeof(drvinfo->version));
668         drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
669
670         strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
671         drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
672
673         strncpy(drvinfo->bus_info, priv->dev->bus->name,
674                 sizeof(drvinfo->bus_info));
675         drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
676
677         strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
678 }
679
680 /**
681  * hns_get_ringparam - get ring parameter
682  * @dev: net device
683  * @param: ethtool parameter
684  */
685 void hns_get_ringparam(struct net_device *net_dev,
686                        struct ethtool_ringparam *param)
687 {
688         struct hns_nic_priv *priv = netdev_priv(net_dev);
689         struct hnae_ae_ops *ops;
690         struct hnae_queue *queue;
691         u32 uplimit = 0;
692
693         queue = priv->ae_handle->qs[0];
694         ops = priv->ae_handle->dev->ops;
695
696         if (ops->get_ring_bdnum_limit)
697                 ops->get_ring_bdnum_limit(queue, &uplimit);
698
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;
703 }
704
705 /**
706  * hns_get_pauseparam - get pause parameter
707  * @dev: net device
708  * @param: pause parameter
709  */
710 static void hns_get_pauseparam(struct net_device *net_dev,
711                                struct ethtool_pauseparam *param)
712 {
713         struct hns_nic_priv *priv = netdev_priv(net_dev);
714         struct hnae_ae_ops *ops;
715
716         ops = priv->ae_handle->dev->ops;
717
718         if (ops->get_pauseparam)
719                 ops->get_pauseparam(priv->ae_handle, &param->autoneg,
720                                             &param->rx_pause, &param->tx_pause);
721 }
722
723 /**
724  * hns_set_pauseparam - set pause parameter
725  * @dev: net device
726  * @param: pause parameter
727  *
728  * Return 0 on success, negative on failure
729  */
730 static int hns_set_pauseparam(struct net_device *net_dev,
731                               struct ethtool_pauseparam *param)
732 {
733         struct hns_nic_priv *priv = netdev_priv(net_dev);
734         struct hnae_handle *h;
735         struct hnae_ae_ops *ops;
736
737         assert(priv || priv->ae_handle);
738
739         h = priv->ae_handle;
740         ops = h->dev->ops;
741
742         if (!ops->set_pauseparam)
743                 return -ESRCH;
744
745         return ops->set_pauseparam(priv->ae_handle, param->autoneg,
746                                    param->rx_pause, param->tx_pause);
747 }
748
749 /**
750  * hns_get_coalesce - get coalesce info.
751  * @dev: net device
752  * @ec: coalesce info.
753  *
754  * Return 0 on success, negative on failure.
755  */
756 static int hns_get_coalesce(struct net_device *net_dev,
757                             struct ethtool_coalesce *ec)
758 {
759         struct hns_nic_priv *priv = netdev_priv(net_dev);
760         struct hnae_ae_ops *ops;
761
762         ops = priv->ae_handle->dev->ops;
763
764         ec->use_adaptive_rx_coalesce = 1;
765         ec->use_adaptive_tx_coalesce = 1;
766
767         if ((!ops->get_coalesce_usecs) ||
768             (!ops->get_rx_max_coalesced_frames))
769                 return -ESRCH;
770
771         ops->get_coalesce_usecs(priv->ae_handle,
772                                         &ec->tx_coalesce_usecs,
773                                         &ec->rx_coalesce_usecs);
774
775         ops->get_rx_max_coalesced_frames(
776                 priv->ae_handle,
777                 &ec->tx_max_coalesced_frames,
778                 &ec->rx_max_coalesced_frames);
779
780         return 0;
781 }
782
783 /**
784  * hns_set_coalesce - set coalesce info.
785  * @dev: net device
786  * @ec: coalesce info.
787  *
788  * Return 0 on success, negative on failure.
789  */
790 static int hns_set_coalesce(struct net_device *net_dev,
791                             struct ethtool_coalesce *ec)
792 {
793         struct hns_nic_priv *priv = netdev_priv(net_dev);
794         struct hnae_ae_ops *ops;
795         int ret;
796
797         assert(priv || priv->ae_handle);
798
799         ops = priv->ae_handle->dev->ops;
800
801         if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
802                 return -EINVAL;
803
804         if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
805                 return -EINVAL;
806
807         if ((!ops->set_coalesce_usecs) ||
808             (!ops->set_coalesce_frames))
809                 return -ESRCH;
810
811         ops->set_coalesce_usecs(priv->ae_handle,
812                                         ec->rx_coalesce_usecs);
813
814         ret = ops->set_coalesce_frames(
815                 priv->ae_handle,
816                 ec->rx_max_coalesced_frames);
817
818         return ret;
819 }
820
821 /**
822  * hns_get_channels - get channel info.
823  * @dev: net device
824  * @ch: channel info.
825  */
826 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
827 {
828         struct hns_nic_priv *priv = netdev_priv(net_dev);
829
830         ch->max_rx = priv->ae_handle->q_num;
831         ch->max_tx = priv->ae_handle->q_num;
832
833         ch->rx_count = priv->ae_handle->q_num;
834         ch->tx_count = priv->ae_handle->q_num;
835 }
836
837 /**
838  * get_ethtool_stats - get detail statistics.
839  * @dev: net device
840  * @stats: statistics info.
841  * @data: statistics data.
842  */
843 void hns_get_ethtool_stats(struct net_device *netdev,
844                            struct ethtool_stats *stats, u64 *data)
845 {
846         u64 *p = 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;
851
852         if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
853                 netdev_err(netdev, "get_stats or update_stats is null!\n");
854                 return;
855         }
856
857         h->dev->ops->update_stats(h, &netdev->stats);
858
859         net_stats = dev_get_stats(netdev, &temp);
860
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;
885
886         p[23] = netdev->rx_dropped.counter;
887         p[24] = netdev->tx_dropped.counter;
888
889         p[25] = priv->tx_timeout_count;
890
891         /* get driver statistics */
892         h->dev->ops->get_stats(h, &p[26]);
893 }
894
895 /**
896  * get_strings: Return a set of strings that describe the requested objects
897  * @dev: net device
898  * @stats: string set ID.
899  * @data: objects data.
900  */
901 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
902 {
903         struct hns_nic_priv *priv = netdev_priv(netdev);
904         struct hnae_handle *h = priv->ae_handle;
905         char *buff = (char *)data;
906
907         if (!h->dev->ops->get_strings) {
908                 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
909                 return;
910         }
911
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],
915                                ETH_GSTRING_LEN);
916                         buff += ETH_GSTRING_LEN;
917                 }
918                 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
919                        ETH_GSTRING_LEN);
920                 buff += ETH_GSTRING_LEN;
921                 if ((priv->phy) && (!priv->phy->is_c45))
922                         memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
923                                ETH_GSTRING_LEN);
924
925         } else {
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;
976
977                 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
978                 buff = buff + ETH_GSTRING_LEN;
979
980                 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
981         }
982 }
983
984 /**
985  * nic_get_sset_count - get string set count witch returned by nic_get_strings.
986  * @dev: net device
987  * @stringset: string set index, 0: self test string; 1: statistics string.
988  *
989  * Return string set count.
990  */
991 int hns_get_sset_count(struct net_device *netdev, int stringset)
992 {
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;
996
997         if (!ops->get_sset_count) {
998                 netdev_err(netdev, "get_sset_count is null!\n");
999                 return -EOPNOTSUPP;
1000         }
1001         if (stringset == ETH_SS_TEST) {
1002                 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1003
1004                 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1005                         cnt--;
1006
1007                 if ((!priv->phy) || (priv->phy->is_c45))
1008                         cnt--;
1009
1010                 return cnt;
1011         } else if (stringset == ETH_SS_STATS) {
1012                 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1013         } else {
1014                 return -EOPNOTSUPP;
1015         }
1016 }
1017
1018 /**
1019  * hns_phy_led_set - set phy LED status.
1020  * @dev: net device
1021  * @value: LED state.
1022  *
1023  * Return 0 on success, negative on failure.
1024  */
1025 int hns_phy_led_set(struct net_device *netdev, int value)
1026 {
1027         int retval;
1028         struct hns_nic_priv *priv = netdev_priv(netdev);
1029         struct phy_device *phy_dev = priv->phy;
1030
1031         if (!phy_dev->bus) {
1032                 netdev_err(netdev, "phy_dev->bus is null!\n");
1033                 return -EINVAL;
1034         }
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,
1038                                value);
1039         retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1040                                HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1041         if (retval) {
1042                 netdev_err(netdev, "mdiobus_write fail !\n");
1043                 return retval;
1044         }
1045         return 0;
1046 }
1047
1048 /**
1049  * nic_set_phys_id - set phy identify LED.
1050  * @dev: net device
1051  * @state: LED state.
1052  *
1053  * Return 0 on success, negative on failure.
1054  */
1055 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1056 {
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;
1060         int ret;
1061
1062         if (phy_dev)
1063                 switch (state) {
1064                 case ETHTOOL_ID_ACTIVE:
1065                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1066                                             HNS_PHY_PAGE_REG,
1067                                             HNS_PHY_PAGE_LED);
1068                         if (ret)
1069                                 return ret;
1070
1071                         priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
1072                                                               phy_dev->addr,
1073                                                               HNS_LED_FC_REG);
1074
1075                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1076                                             HNS_PHY_PAGE_REG,
1077                                             HNS_PHY_PAGE_COPPER);
1078                         if (ret)
1079                                 return ret;
1080                         return 2;
1081                 case ETHTOOL_ID_ON:
1082                         ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1083                         if (ret)
1084                                 return ret;
1085                         break;
1086                 case ETHTOOL_ID_OFF:
1087                         ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1088                         if (ret)
1089                                 return ret;
1090                         break;
1091                 case ETHTOOL_ID_INACTIVE:
1092                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1093                                             HNS_PHY_PAGE_REG,
1094                                             HNS_PHY_PAGE_LED);
1095                         if (ret)
1096                                 return ret;
1097
1098                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1099                                             HNS_LED_FC_REG, priv->phy_led_val);
1100                         if (ret)
1101                                 return ret;
1102
1103                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1104                                             HNS_PHY_PAGE_REG,
1105                                             HNS_PHY_PAGE_COPPER);
1106                         if (ret)
1107                                 return ret;
1108                         break;
1109                 default:
1110                         return -EINVAL;
1111                 }
1112         else
1113                 switch (state) {
1114                 case ETHTOOL_ID_ACTIVE:
1115                         return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1116                 case ETHTOOL_ID_ON:
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);
1122                 default:
1123                         return -EINVAL;
1124                 }
1125
1126         return 0;
1127 }
1128
1129 /**
1130  * hns_get_regs - get net device register
1131  * @dev: net device
1132  * @cmd: ethtool cmd
1133  * @date: register data
1134  */
1135 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1136                   void *data)
1137 {
1138         struct hns_nic_priv *priv = netdev_priv(net_dev);
1139         struct hnae_ae_ops *ops;
1140
1141         assert(priv || priv->ae_handle);
1142
1143         ops = priv->ae_handle->dev->ops;
1144
1145         cmd->version = HNS_CHIP_VERSION;
1146         if (!ops->get_regs) {
1147                 netdev_err(net_dev, "ops->get_regs is null!\n");
1148                 return;
1149         }
1150         ops->get_regs(priv->ae_handle, data);
1151 }
1152
1153 /**
1154  * nic_get_regs_len - get total register len.
1155  * @dev: net device
1156  *
1157  * Return total register len.
1158  */
1159 static int hns_get_regs_len(struct net_device *net_dev)
1160 {
1161         u32 reg_num;
1162         struct hns_nic_priv *priv = netdev_priv(net_dev);
1163         struct hnae_ae_ops *ops;
1164
1165         assert(priv || priv->ae_handle);
1166
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");
1170                 return -EOPNOTSUPP;
1171         }
1172
1173         reg_num = ops->get_regs_len(priv->ae_handle);
1174         if (reg_num > 0)
1175                 return reg_num * sizeof(u32);
1176         else
1177                 return reg_num; /* error code */
1178 }
1179
1180 /**
1181  * hns_nic_nway_reset - nway reset
1182  * @dev: net device
1183  *
1184  * Return 0 on success, negative on failure
1185  */
1186 static int hns_nic_nway_reset(struct net_device *netdev)
1187 {
1188         int ret = 0;
1189         struct hns_nic_priv *priv = netdev_priv(netdev);
1190         struct phy_device *phy = priv->phy;
1191
1192         if (netif_running(netdev)) {
1193                 if (phy)
1194                         ret = genphy_restart_aneg(phy);
1195         }
1196
1197         return ret;
1198 }
1199
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,
1219 };
1220
1221 void hns_ethtool_set_ops(struct net_device *ndev)
1222 {
1223         ndev->ethtool_ops = &hns_ethtool_ops;
1224 }