drivers/net: get rid of unnecessary initializations in .get_drvinfo()
[sfrench/cifs-2.6.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         int link_stat;
198         u32 speed;
199         u8 duplex, autoneg;
200
201         if (!netif_running(net_dev))
202                 return -ESRCH;
203
204         if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
205             !priv->ae_handle->dev->ops)
206                 return -ENODEV;
207
208         h = priv->ae_handle;
209         link_stat = hns_nic_get_link(net_dev);
210         duplex = cmd->duplex;
211         speed = ethtool_cmd_speed(cmd);
212         autoneg = cmd->autoneg;
213
214         if (!link_stat) {
215                 if (duplex != (u8)DUPLEX_UNKNOWN || speed != (u32)SPEED_UNKNOWN)
216                         return -EINVAL;
217
218                 if (h->phy_if == PHY_INTERFACE_MODE_SGMII && h->phy_node) {
219                         priv->phy->autoneg = autoneg;
220                         return phy_start_aneg(priv->phy);
221                 }
222         }
223
224         if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
225                 if (autoneg != AUTONEG_DISABLE)
226                         return -EINVAL;
227
228                 if (speed != SPEED_10000 || duplex != DUPLEX_FULL)
229                         return -EINVAL;
230         } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
231                 if (!h->phy_node && autoneg != AUTONEG_DISABLE)
232                         return -EINVAL;
233
234                 if (speed == SPEED_1000 && duplex == DUPLEX_HALF)
235                         return -EINVAL;
236
237                 if (speed != SPEED_10 && speed != SPEED_100 &&
238                     speed != SPEED_1000)
239                         return -EINVAL;
240         } else {
241                 netdev_err(net_dev, "Not supported!");
242                 return -ENOTSUPP;
243         }
244
245         if (priv->phy) {
246                 return phy_ethtool_sset(priv->phy, cmd);
247         } else if (h->dev->ops->adjust_link && link_stat) {
248                 h->dev->ops->adjust_link(h, speed, duplex);
249                 return 0;
250         }
251         netdev_err(net_dev, "Not supported!");
252         return -ENOTSUPP;
253 }
254
255 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
256         "Mac    Loopback test",
257         "Serdes Loopback test",
258         "Phy    Loopback test"
259 };
260
261 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
262 {
263 #define COPPER_CONTROL_REG 0
264 #define PHY_LOOP_BACK BIT(14)
265         u16 val = 0;
266
267         if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
268                 return -ENOTSUPP;
269
270         if (en) {
271                 /* speed : 1000M */
272                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
273                                     HNS_PHY_PAGE_REG, 2);
274                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
275                                     21, 0x1046);
276                 /* Force Master */
277                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
278                                     9, 0x1F00);
279                 /* Soft-reset */
280                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
281                                     0, 0x9140);
282                 /* If autoneg disabled,two soft-reset operations */
283                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
284                                     0, 0x9140);
285                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
286                                     22, 0xFA);
287
288                 /* Default is 0x0400 */
289                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
290                                     1, 0x418);
291
292                 /* Force 1000M Link, Default is 0x0200 */
293                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
294                                     7, 0x20C);
295                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
296                                     22, 0);
297
298                 /* Enable MAC loop-back */
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         } else {
305                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
306                                     22, 0xFA);
307                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
308                                     1, 0x400);
309                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
310                                     7, 0x200);
311                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
312                                     22, 0);
313
314                 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
315                                         COPPER_CONTROL_REG);
316                 val &= ~PHY_LOOP_BACK;
317                 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
318                                     COPPER_CONTROL_REG, val);
319         }
320         return 0;
321 }
322
323 static int __lb_setup(struct net_device *ndev,
324                       enum hnae_loop loop)
325 {
326         int ret = 0;
327         struct hns_nic_priv *priv = netdev_priv(ndev);
328         struct phy_device *phy_dev = priv->phy;
329         struct hnae_handle *h = priv->ae_handle;
330
331         switch (loop) {
332         case MAC_INTERNALLOOP_PHY:
333                 if ((phy_dev) && (!phy_dev->is_c45))
334                         ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
335                 break;
336         case MAC_INTERNALLOOP_MAC:
337                 if ((h->dev->ops->set_loopback) &&
338                     (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
339                         ret = h->dev->ops->set_loopback(h, loop, 0x1);
340                 break;
341         case MAC_INTERNALLOOP_SERDES:
342                 if (h->dev->ops->set_loopback)
343                         ret = h->dev->ops->set_loopback(h, loop, 0x1);
344                 break;
345         case MAC_LOOP_NONE:
346                 if ((phy_dev) && (!phy_dev->is_c45))
347                         ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
348
349                 if (h->dev->ops->set_loopback) {
350                         if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
351                                 ret |= h->dev->ops->set_loopback(h,
352                                         MAC_INTERNALLOOP_MAC, 0x0);
353
354                         ret |= h->dev->ops->set_loopback(h,
355                                 MAC_INTERNALLOOP_SERDES, 0x0);
356                 }
357                 break;
358         default:
359                 ret = -EINVAL;
360                 break;
361         }
362
363         return ret;
364 }
365
366 static int __lb_up(struct net_device *ndev,
367                    enum hnae_loop loop_mode)
368 {
369         struct hns_nic_priv *priv = netdev_priv(ndev);
370         struct hnae_handle *h = priv->ae_handle;
371         int speed, duplex;
372         int ret;
373
374         hns_nic_net_reset(ndev);
375
376         if (priv->phy) {
377                 phy_disconnect(priv->phy);
378                 msleep(100);
379
380                 ret = hns_nic_init_phy(ndev, h);
381                 if (ret)
382                         return ret;
383         }
384
385         ret = __lb_setup(ndev, loop_mode);
386         if (ret)
387                 return ret;
388
389         msleep(100);
390
391         ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
392         if (ret)
393                 return ret;
394
395         if (priv->phy)
396                 phy_start(priv->phy);
397
398         /* link adjust duplex*/
399         if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
400                 speed = 1000;
401         else
402                 speed = 10000;
403         duplex = 1;
404
405         h->dev->ops->adjust_link(h, speed, duplex);
406
407         return 0;
408 }
409
410 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
411                                struct sk_buff *skb)
412 {
413         struct net_device *ndev;
414         struct hnae_ring *ring;
415         struct netdev_queue *dev_queue;
416         struct sk_buff *new_skb;
417         unsigned int frame_size;
418         int check_ok;
419         u32 i;
420         char buff[33]; /* 32B data and the last character '\0' */
421
422         if (!ring_data) { /* Just for doing create frame*/
423                 frame_size = skb->len;
424                 memset(skb->data, 0xFF, frame_size);
425                 frame_size &= ~1ul;
426                 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
427                 memset(&skb->data[frame_size / 2 + 10], 0xBE,
428                        frame_size / 2 - 11);
429                 memset(&skb->data[frame_size / 2 + 12], 0xAF,
430                        frame_size / 2 - 13);
431                 return;
432         }
433
434         ring = ring_data->ring;
435         ndev = ring_data->napi.dev;
436         if (is_tx_ring(ring)) { /* for tx queue reset*/
437                 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
438                 netdev_tx_reset_queue(dev_queue);
439                 return;
440         }
441
442         frame_size = skb->len;
443         frame_size &= ~1ul;
444         /* for mutl buffer*/
445         new_skb = skb_copy(skb, GFP_ATOMIC);
446         dev_kfree_skb_any(skb);
447         skb = new_skb;
448
449         check_ok = 0;
450         if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
451                 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
452                     (*(skb->data + frame_size / 2 + 12) == 0xAF))
453                         check_ok = 1;
454         }
455
456         if (check_ok) {
457                 ndev->stats.rx_packets++;
458                 ndev->stats.rx_bytes += skb->len;
459         } else {
460                 ndev->stats.rx_frame_errors++;
461                 for (i = 0; i < skb->len; i++) {
462                         snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
463                                  "%02x", *(skb->data + i));
464                         if ((i % 16 == 15) || (i == skb->len - 1))
465                                 pr_info("%s\n", buff);
466                 }
467         }
468         dev_kfree_skb_any(skb);
469 }
470
471 static int __lb_clean_rings(struct hns_nic_priv *priv,
472                             int ringid0, int ringid1, int budget)
473 {
474         int i, ret;
475         struct hns_nic_ring_data *ring_data;
476         struct net_device *ndev = priv->netdev;
477         unsigned long rx_packets = ndev->stats.rx_packets;
478         unsigned long rx_bytes = ndev->stats.rx_bytes;
479         unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
480
481         for (i = ringid0; i <= ringid1; i++) {
482                 ring_data = &priv->ring_data[i];
483                 (void)ring_data->poll_one(ring_data,
484                                           budget, __lb_other_process);
485         }
486         ret = (int)(ndev->stats.rx_packets - rx_packets);
487         ndev->stats.rx_packets = rx_packets;
488         ndev->stats.rx_bytes = rx_bytes;
489         ndev->stats.rx_frame_errors = rx_frame_errors;
490         return ret;
491 }
492
493 /**
494  * nic_run_loopback_test -  run loopback test
495  * @nic_dev: net device
496  * @loopback_type: loopback type
497  */
498 static int __lb_run_test(struct net_device *ndev,
499                          enum hnae_loop loop_mode)
500 {
501 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
502 #define NIC_LB_TEST_RING_ID 0
503 #define NIC_LB_TEST_FRAME_SIZE 128
504 /* nic loopback test err  */
505 #define NIC_LB_TEST_NO_MEM_ERR 1
506 #define NIC_LB_TEST_TX_CNT_ERR 2
507 #define NIC_LB_TEST_RX_CNT_ERR 3
508 #define NIC_LB_TEST_RX_PKG_ERR 4
509         struct hns_nic_priv *priv = netdev_priv(ndev);
510         struct hnae_handle *h = priv->ae_handle;
511         int i, j, lc, good_cnt, ret_val = 0;
512         unsigned int size;
513         netdev_tx_t tx_ret_val;
514         struct sk_buff *skb;
515
516         size = NIC_LB_TEST_FRAME_SIZE;
517         /* allocate test skb */
518         skb = alloc_skb(size, GFP_KERNEL);
519         if (!skb)
520                 return NIC_LB_TEST_NO_MEM_ERR;
521
522         /* place data into test skb */
523         (void)skb_put(skb, size);
524         __lb_other_process(NULL, skb);
525         skb->queue_mapping = NIC_LB_TEST_RING_ID;
526
527         lc = 1;
528         for (j = 0; j < lc; j++) {
529                 /* reset count of good packets */
530                 good_cnt = 0;
531                 /* place 64 packets on the transmit queue*/
532                 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
533                         (void)skb_get(skb);
534
535                         tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
536                                 ndev, skb,
537                                 &tx_ring_data(priv, skb->queue_mapping));
538                         if (tx_ret_val == NETDEV_TX_OK)
539                                 good_cnt++;
540                         else
541                                 break;
542                 }
543                 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
544                         ret_val = NIC_LB_TEST_TX_CNT_ERR;
545                         dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
546                                 hns_nic_test_strs[loop_mode], good_cnt,
547                                 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
548                         break;
549                 }
550
551                 /* allow 100 milliseconds for packets to go from Tx to Rx */
552                 msleep(100);
553
554                 good_cnt = __lb_clean_rings(priv,
555                                             h->q_num, h->q_num * 2 - 1,
556                                             NIC_LB_TEST_PKT_NUM_PER_CYCLE);
557                 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
558                         ret_val = NIC_LB_TEST_RX_CNT_ERR;
559                         dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
560                                 hns_nic_test_strs[loop_mode], good_cnt,
561                                 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
562                         break;
563                 }
564                 (void)__lb_clean_rings(priv,
565                                        NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
566                                        NIC_LB_TEST_PKT_NUM_PER_CYCLE);
567         }
568
569         /* free the original skb */
570         kfree_skb(skb);
571
572         return ret_val;
573 }
574
575 static int __lb_down(struct net_device *ndev)
576 {
577         struct hns_nic_priv *priv = netdev_priv(ndev);
578         struct hnae_handle *h = priv->ae_handle;
579         int ret;
580
581         ret = __lb_setup(ndev, MAC_LOOP_NONE);
582         if (ret)
583                 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
584                            __func__,
585                            ret);
586
587         if (priv->phy)
588                 phy_stop(priv->phy);
589
590         if (h->dev->ops->stop)
591                 h->dev->ops->stop(h);
592
593         usleep_range(10000, 20000);
594         (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
595
596         hns_nic_net_reset(ndev);
597
598         return 0;
599 }
600
601 /**
602  * hns_nic_self_test - self test
603  * @dev: net device
604  * @eth_test: test cmd
605  * @data: test result
606  */
607 static void hns_nic_self_test(struct net_device *ndev,
608                               struct ethtool_test *eth_test, u64 *data)
609 {
610         struct hns_nic_priv *priv = netdev_priv(ndev);
611         bool if_running = netif_running(ndev);
612 #define SELF_TEST_TPYE_NUM 3
613         int st_param[SELF_TEST_TPYE_NUM][2];
614         int i;
615         int test_index = 0;
616
617         st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
618         st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
619         st_param[1][0] = MAC_INTERNALLOOP_SERDES;
620         st_param[1][1] = 1; /*serdes must exist*/
621         st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
622         st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
623                 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
624
625         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
626                 set_bit(NIC_STATE_TESTING, &priv->state);
627
628                 if (if_running)
629                         (void)dev_close(ndev);
630
631                 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
632                         if (!st_param[i][1])
633                                 continue;       /* NEXT testing */
634
635                         data[test_index] = __lb_up(ndev,
636                                 (enum hnae_loop)st_param[i][0]);
637                         if (!data[test_index]) {
638                                 data[test_index] = __lb_run_test(
639                                         ndev, (enum hnae_loop)st_param[i][0]);
640                                 (void)__lb_down(ndev);
641                         }
642
643                         if (data[test_index])
644                                 eth_test->flags |= ETH_TEST_FL_FAILED;
645
646                         test_index++;
647                 }
648
649                 hns_nic_net_reset(priv->netdev);
650
651                 clear_bit(NIC_STATE_TESTING, &priv->state);
652
653                 if (if_running)
654                         (void)dev_open(ndev);
655         }
656         /* Online tests aren't run; pass by default */
657
658         (void)msleep_interruptible(4 * 1000);
659 }
660
661 /**
662  * hns_nic_get_drvinfo - get net driver info
663  * @dev: net device
664  * @drvinfo: driver info
665  */
666 static void hns_nic_get_drvinfo(struct net_device *net_dev,
667                                 struct ethtool_drvinfo *drvinfo)
668 {
669         struct hns_nic_priv *priv = netdev_priv(net_dev);
670
671         assert(priv);
672
673         strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
674                 sizeof(drvinfo->version));
675         drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
676
677         strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
678         drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
679
680         strncpy(drvinfo->bus_info, priv->dev->bus->name,
681                 sizeof(drvinfo->bus_info));
682         drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
683
684         strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
685 }
686
687 /**
688  * hns_get_ringparam - get ring parameter
689  * @dev: net device
690  * @param: ethtool parameter
691  */
692 void hns_get_ringparam(struct net_device *net_dev,
693                        struct ethtool_ringparam *param)
694 {
695         struct hns_nic_priv *priv = netdev_priv(net_dev);
696         struct hnae_ae_ops *ops;
697         struct hnae_queue *queue;
698         u32 uplimit = 0;
699
700         queue = priv->ae_handle->qs[0];
701         ops = priv->ae_handle->dev->ops;
702
703         if (ops->get_ring_bdnum_limit)
704                 ops->get_ring_bdnum_limit(queue, &uplimit);
705
706         param->rx_max_pending = uplimit;
707         param->tx_max_pending = uplimit;
708         param->rx_pending = queue->rx_ring.desc_num;
709         param->tx_pending = queue->tx_ring.desc_num;
710 }
711
712 /**
713  * hns_get_pauseparam - get pause parameter
714  * @dev: net device
715  * @param: pause parameter
716  */
717 static void hns_get_pauseparam(struct net_device *net_dev,
718                                struct ethtool_pauseparam *param)
719 {
720         struct hns_nic_priv *priv = netdev_priv(net_dev);
721         struct hnae_ae_ops *ops;
722
723         ops = priv->ae_handle->dev->ops;
724
725         if (ops->get_pauseparam)
726                 ops->get_pauseparam(priv->ae_handle, &param->autoneg,
727                                             &param->rx_pause, &param->tx_pause);
728 }
729
730 /**
731  * hns_set_pauseparam - set pause parameter
732  * @dev: net device
733  * @param: pause parameter
734  *
735  * Return 0 on success, negative on failure
736  */
737 static int hns_set_pauseparam(struct net_device *net_dev,
738                               struct ethtool_pauseparam *param)
739 {
740         struct hns_nic_priv *priv = netdev_priv(net_dev);
741         struct hnae_handle *h;
742         struct hnae_ae_ops *ops;
743
744         assert(priv || priv->ae_handle);
745
746         h = priv->ae_handle;
747         ops = h->dev->ops;
748
749         if (!ops->set_pauseparam)
750                 return -ESRCH;
751
752         return ops->set_pauseparam(priv->ae_handle, param->autoneg,
753                                    param->rx_pause, param->tx_pause);
754 }
755
756 /**
757  * hns_get_coalesce - get coalesce info.
758  * @dev: net device
759  * @ec: coalesce info.
760  *
761  * Return 0 on success, negative on failure.
762  */
763 static int hns_get_coalesce(struct net_device *net_dev,
764                             struct ethtool_coalesce *ec)
765 {
766         struct hns_nic_priv *priv = netdev_priv(net_dev);
767         struct hnae_ae_ops *ops;
768
769         ops = priv->ae_handle->dev->ops;
770
771         ec->use_adaptive_rx_coalesce = 1;
772         ec->use_adaptive_tx_coalesce = 1;
773
774         if ((!ops->get_coalesce_usecs) ||
775             (!ops->get_rx_max_coalesced_frames))
776                 return -ESRCH;
777
778         ops->get_coalesce_usecs(priv->ae_handle,
779                                         &ec->tx_coalesce_usecs,
780                                         &ec->rx_coalesce_usecs);
781
782         ops->get_rx_max_coalesced_frames(
783                 priv->ae_handle,
784                 &ec->tx_max_coalesced_frames,
785                 &ec->rx_max_coalesced_frames);
786
787         return 0;
788 }
789
790 /**
791  * hns_set_coalesce - set coalesce info.
792  * @dev: net device
793  * @ec: coalesce info.
794  *
795  * Return 0 on success, negative on failure.
796  */
797 static int hns_set_coalesce(struct net_device *net_dev,
798                             struct ethtool_coalesce *ec)
799 {
800         struct hns_nic_priv *priv = netdev_priv(net_dev);
801         struct hnae_ae_ops *ops;
802         int ret;
803
804         assert(priv || priv->ae_handle);
805
806         ops = priv->ae_handle->dev->ops;
807
808         if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
809                 return -EINVAL;
810
811         if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
812                 return -EINVAL;
813
814         if ((!ops->set_coalesce_usecs) ||
815             (!ops->set_coalesce_frames))
816                 return -ESRCH;
817
818         ops->set_coalesce_usecs(priv->ae_handle,
819                                         ec->rx_coalesce_usecs);
820
821         ret = ops->set_coalesce_frames(
822                 priv->ae_handle,
823                 ec->rx_max_coalesced_frames);
824
825         return ret;
826 }
827
828 /**
829  * hns_get_channels - get channel info.
830  * @dev: net device
831  * @ch: channel info.
832  */
833 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
834 {
835         struct hns_nic_priv *priv = netdev_priv(net_dev);
836
837         ch->max_rx = priv->ae_handle->q_num;
838         ch->max_tx = priv->ae_handle->q_num;
839
840         ch->rx_count = priv->ae_handle->q_num;
841         ch->tx_count = priv->ae_handle->q_num;
842 }
843
844 /**
845  * get_ethtool_stats - get detail statistics.
846  * @dev: net device
847  * @stats: statistics info.
848  * @data: statistics data.
849  */
850 void hns_get_ethtool_stats(struct net_device *netdev,
851                            struct ethtool_stats *stats, u64 *data)
852 {
853         u64 *p = data;
854         struct hns_nic_priv *priv = netdev_priv(netdev);
855         struct hnae_handle *h = priv->ae_handle;
856         const struct rtnl_link_stats64 *net_stats;
857         struct rtnl_link_stats64 temp;
858
859         if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
860                 netdev_err(netdev, "get_stats or update_stats is null!\n");
861                 return;
862         }
863
864         h->dev->ops->update_stats(h, &netdev->stats);
865
866         net_stats = dev_get_stats(netdev, &temp);
867
868         /* get netdev statistics */
869         p[0] = net_stats->rx_packets;
870         p[1] = net_stats->tx_packets;
871         p[2] = net_stats->rx_bytes;
872         p[3] = net_stats->tx_bytes;
873         p[4] = net_stats->rx_errors;
874         p[5] = net_stats->tx_errors;
875         p[6] = net_stats->rx_dropped;
876         p[7] = net_stats->tx_dropped;
877         p[8] = net_stats->multicast;
878         p[9] = net_stats->collisions;
879         p[10] = net_stats->rx_over_errors;
880         p[11] = net_stats->rx_crc_errors;
881         p[12] = net_stats->rx_frame_errors;
882         p[13] = net_stats->rx_fifo_errors;
883         p[14] = net_stats->rx_missed_errors;
884         p[15] = net_stats->tx_aborted_errors;
885         p[16] = net_stats->tx_carrier_errors;
886         p[17] = net_stats->tx_fifo_errors;
887         p[18] = net_stats->tx_heartbeat_errors;
888         p[19] = net_stats->rx_length_errors;
889         p[20] = net_stats->tx_window_errors;
890         p[21] = net_stats->rx_compressed;
891         p[22] = net_stats->tx_compressed;
892
893         p[23] = netdev->rx_dropped.counter;
894         p[24] = netdev->tx_dropped.counter;
895
896         p[25] = priv->tx_timeout_count;
897
898         /* get driver statistics */
899         h->dev->ops->get_stats(h, &p[26]);
900 }
901
902 /**
903  * get_strings: Return a set of strings that describe the requested objects
904  * @dev: net device
905  * @stats: string set ID.
906  * @data: objects data.
907  */
908 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
909 {
910         struct hns_nic_priv *priv = netdev_priv(netdev);
911         struct hnae_handle *h = priv->ae_handle;
912         char *buff = (char *)data;
913
914         if (!h->dev->ops->get_strings) {
915                 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
916                 return;
917         }
918
919         if (stringset == ETH_SS_TEST) {
920                 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
921                         memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
922                                ETH_GSTRING_LEN);
923                         buff += ETH_GSTRING_LEN;
924                 }
925                 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
926                        ETH_GSTRING_LEN);
927                 buff += ETH_GSTRING_LEN;
928                 if ((priv->phy) && (!priv->phy->is_c45))
929                         memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
930                                ETH_GSTRING_LEN);
931
932         } else {
933                 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
934                 buff = buff + ETH_GSTRING_LEN;
935                 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
936                 buff = buff + ETH_GSTRING_LEN;
937                 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
938                 buff = buff + ETH_GSTRING_LEN;
939                 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
940                 buff = buff + ETH_GSTRING_LEN;
941                 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
942                 buff = buff + ETH_GSTRING_LEN;
943                 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
944                 buff = buff + ETH_GSTRING_LEN;
945                 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
946                 buff = buff + ETH_GSTRING_LEN;
947                 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
948                 buff = buff + ETH_GSTRING_LEN;
949                 snprintf(buff, ETH_GSTRING_LEN, "multicast");
950                 buff = buff + ETH_GSTRING_LEN;
951                 snprintf(buff, ETH_GSTRING_LEN, "collisions");
952                 buff = buff + ETH_GSTRING_LEN;
953                 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
954                 buff = buff + ETH_GSTRING_LEN;
955                 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
956                 buff = buff + ETH_GSTRING_LEN;
957                 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
958                 buff = buff + ETH_GSTRING_LEN;
959                 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
960                 buff = buff + ETH_GSTRING_LEN;
961                 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
962                 buff = buff + ETH_GSTRING_LEN;
963                 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
964                 buff = buff + ETH_GSTRING_LEN;
965                 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
966                 buff = buff + ETH_GSTRING_LEN;
967                 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
968                 buff = buff + ETH_GSTRING_LEN;
969                 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
970                 buff = buff + ETH_GSTRING_LEN;
971                 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
972                 buff = buff + ETH_GSTRING_LEN;
973                 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
974                 buff = buff + ETH_GSTRING_LEN;
975                 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
976                 buff = buff + ETH_GSTRING_LEN;
977                 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
978                 buff = buff + ETH_GSTRING_LEN;
979                 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
980                 buff = buff + ETH_GSTRING_LEN;
981                 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
982                 buff = buff + ETH_GSTRING_LEN;
983
984                 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
985                 buff = buff + ETH_GSTRING_LEN;
986
987                 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
988         }
989 }
990
991 /**
992  * nic_get_sset_count - get string set count witch returned by nic_get_strings.
993  * @dev: net device
994  * @stringset: string set index, 0: self test string; 1: statistics string.
995  *
996  * Return string set count.
997  */
998 int hns_get_sset_count(struct net_device *netdev, int stringset)
999 {
1000         struct hns_nic_priv *priv = netdev_priv(netdev);
1001         struct hnae_handle *h = priv->ae_handle;
1002         struct hnae_ae_ops *ops = h->dev->ops;
1003
1004         if (!ops->get_sset_count) {
1005                 netdev_err(netdev, "get_sset_count is null!\n");
1006                 return -EOPNOTSUPP;
1007         }
1008         if (stringset == ETH_SS_TEST) {
1009                 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
1010
1011                 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
1012                         cnt--;
1013
1014                 if ((!priv->phy) || (priv->phy->is_c45))
1015                         cnt--;
1016
1017                 return cnt;
1018         } else {
1019                 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1020         }
1021 }
1022
1023 /**
1024  * hns_phy_led_set - set phy LED status.
1025  * @dev: net device
1026  * @value: LED state.
1027  *
1028  * Return 0 on success, negative on failure.
1029  */
1030 int hns_phy_led_set(struct net_device *netdev, int value)
1031 {
1032         int retval;
1033         struct hns_nic_priv *priv = netdev_priv(netdev);
1034         struct phy_device *phy_dev = priv->phy;
1035
1036         if (!phy_dev->bus) {
1037                 netdev_err(netdev, "phy_dev->bus is null!\n");
1038                 return -EINVAL;
1039         }
1040         retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1041                                HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1042         retval = mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_LED_FC_REG,
1043                                value);
1044         retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1045                                HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1046         if (retval) {
1047                 netdev_err(netdev, "mdiobus_write fail !\n");
1048                 return retval;
1049         }
1050         return 0;
1051 }
1052
1053 /**
1054  * nic_set_phys_id - set phy identify LED.
1055  * @dev: net device
1056  * @state: LED state.
1057  *
1058  * Return 0 on success, negative on failure.
1059  */
1060 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1061 {
1062         struct hns_nic_priv *priv = netdev_priv(netdev);
1063         struct hnae_handle *h = priv->ae_handle;
1064         struct phy_device *phy_dev = priv->phy;
1065         int ret;
1066
1067         if (phy_dev)
1068                 switch (state) {
1069                 case ETHTOOL_ID_ACTIVE:
1070                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1071                                             HNS_PHY_PAGE_REG,
1072                                             HNS_PHY_PAGE_LED);
1073                         if (ret)
1074                                 return ret;
1075
1076                         priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
1077                                                               phy_dev->addr,
1078                                                               HNS_LED_FC_REG);
1079
1080                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1081                                             HNS_PHY_PAGE_REG,
1082                                             HNS_PHY_PAGE_COPPER);
1083                         if (ret)
1084                                 return ret;
1085                         return 2;
1086                 case ETHTOOL_ID_ON:
1087                         ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1088                         if (ret)
1089                                 return ret;
1090                         break;
1091                 case ETHTOOL_ID_OFF:
1092                         ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1093                         if (ret)
1094                                 return ret;
1095                         break;
1096                 case ETHTOOL_ID_INACTIVE:
1097                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1098                                             HNS_PHY_PAGE_REG,
1099                                             HNS_PHY_PAGE_LED);
1100                         if (ret)
1101                                 return ret;
1102
1103                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1104                                             HNS_LED_FC_REG, priv->phy_led_val);
1105                         if (ret)
1106                                 return ret;
1107
1108                         ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1109                                             HNS_PHY_PAGE_REG,
1110                                             HNS_PHY_PAGE_COPPER);
1111                         if (ret)
1112                                 return ret;
1113                         break;
1114                 default:
1115                         return -EINVAL;
1116                 }
1117         else
1118                 switch (state) {
1119                 case ETHTOOL_ID_ACTIVE:
1120                         return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1121                 case ETHTOOL_ID_ON:
1122                         return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1123                 case ETHTOOL_ID_OFF:
1124                         return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1125                 case ETHTOOL_ID_INACTIVE:
1126                         return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1127                 default:
1128                         return -EINVAL;
1129                 }
1130
1131         return 0;
1132 }
1133
1134 /**
1135  * hns_get_regs - get net device register
1136  * @dev: net device
1137  * @cmd: ethtool cmd
1138  * @date: register data
1139  */
1140 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1141                   void *data)
1142 {
1143         struct hns_nic_priv *priv = netdev_priv(net_dev);
1144         struct hnae_ae_ops *ops;
1145
1146         assert(priv || priv->ae_handle);
1147
1148         ops = priv->ae_handle->dev->ops;
1149
1150         cmd->version = HNS_CHIP_VERSION;
1151         if (!ops->get_regs) {
1152                 netdev_err(net_dev, "ops->get_regs is null!\n");
1153                 return;
1154         }
1155         ops->get_regs(priv->ae_handle, data);
1156 }
1157
1158 /**
1159  * nic_get_regs_len - get total register len.
1160  * @dev: net device
1161  *
1162  * Return total register len.
1163  */
1164 static int hns_get_regs_len(struct net_device *net_dev)
1165 {
1166         u32 reg_num;
1167         struct hns_nic_priv *priv = netdev_priv(net_dev);
1168         struct hnae_ae_ops *ops;
1169
1170         assert(priv || priv->ae_handle);
1171
1172         ops = priv->ae_handle->dev->ops;
1173         if (!ops->get_regs_len) {
1174                 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1175                 return -EOPNOTSUPP;
1176         }
1177
1178         reg_num = ops->get_regs_len(priv->ae_handle);
1179         if (reg_num > 0)
1180                 return reg_num * sizeof(u32);
1181         else
1182                 return reg_num; /* error code */
1183 }
1184
1185 /**
1186  * hns_nic_nway_reset - nway reset
1187  * @dev: net device
1188  *
1189  * Return 0 on success, negative on failure
1190  */
1191 static int hns_nic_nway_reset(struct net_device *netdev)
1192 {
1193         int ret = 0;
1194         struct hns_nic_priv *priv = netdev_priv(netdev);
1195         struct phy_device *phy = priv->phy;
1196
1197         if (netif_running(netdev)) {
1198                 if (phy)
1199                         ret = genphy_restart_aneg(phy);
1200         }
1201
1202         return ret;
1203 }
1204
1205 static struct ethtool_ops hns_ethtool_ops = {
1206         .get_drvinfo = hns_nic_get_drvinfo,
1207         .get_link  = hns_nic_get_link,
1208         .get_settings  = hns_nic_get_settings,
1209         .set_settings  = hns_nic_set_settings,
1210         .get_ringparam = hns_get_ringparam,
1211         .get_pauseparam = hns_get_pauseparam,
1212         .set_pauseparam = hns_set_pauseparam,
1213         .get_coalesce = hns_get_coalesce,
1214         .set_coalesce = hns_set_coalesce,
1215         .get_channels = hns_get_channels,
1216         .self_test = hns_nic_self_test,
1217         .get_strings = hns_get_strings,
1218         .get_sset_count = hns_get_sset_count,
1219         .get_ethtool_stats = hns_get_ethtool_stats,
1220         .set_phys_id = hns_set_phys_id,
1221         .get_regs_len = hns_get_regs_len,
1222         .get_regs = hns_get_regs,
1223         .nway_reset = hns_nic_nway_reset,
1224 };
1225
1226 void hns_ethtool_set_ops(struct net_device *ndev)
1227 {
1228         ndev->ethtool_ops = &hns_ethtool_ops;
1229 }