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