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