net: hns3: optimize the tx clean process
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / hisilicon / hns3 / hns3_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
6 #include <linux/phy.h>
7 #include <linux/sfp.h>
8
9 #include "hns3_enet.h"
10
11 struct hns3_stats {
12         char stats_string[ETH_GSTRING_LEN];
13         int stats_offset;
14 };
15
16 struct hns3_sfp_type {
17         u8 type;
18         u8 ext_type;
19 };
20
21 /* tqp related stats */
22 #define HNS3_TQP_STAT(_string, _member) {                       \
23         .stats_string = _string,                                \
24         .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
25                         offsetof(struct ring_stats, _member),   \
26 }
27
28 static const struct hns3_stats hns3_txq_stats[] = {
29         /* Tx per-queue statistics */
30         HNS3_TQP_STAT("dropped", sw_err_cnt),
31         HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
32         HNS3_TQP_STAT("packets", tx_pkts),
33         HNS3_TQP_STAT("bytes", tx_bytes),
34         HNS3_TQP_STAT("more", tx_more),
35         HNS3_TQP_STAT("wake", restart_queue),
36         HNS3_TQP_STAT("busy", tx_busy),
37         HNS3_TQP_STAT("copy", tx_copy),
38         HNS3_TQP_STAT("vlan_err", tx_vlan_err),
39         HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
40         HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
41         HNS3_TQP_STAT("tso_err", tx_tso_err),
42 };
43
44 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
45
46 static const struct hns3_stats hns3_rxq_stats[] = {
47         /* Rx per-queue statistics */
48         HNS3_TQP_STAT("dropped", sw_err_cnt),
49         HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
50         HNS3_TQP_STAT("packets", rx_pkts),
51         HNS3_TQP_STAT("bytes", rx_bytes),
52         HNS3_TQP_STAT("errors", rx_err_cnt),
53         HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
54         HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
55         HNS3_TQP_STAT("err_bd_num", err_bd_num),
56         HNS3_TQP_STAT("l2_err", l2_err),
57         HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
58         HNS3_TQP_STAT("multicast", rx_multicast),
59         HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
60 };
61
62 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
63
64 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
65
66 #define HNS3_SELF_TEST_TYPE_NUM         4
67 #define HNS3_NIC_LB_TEST_PKT_NUM        1
68 #define HNS3_NIC_LB_TEST_RING_ID        0
69 #define HNS3_NIC_LB_TEST_PACKET_SIZE    128
70 #define HNS3_NIC_LB_SETUP_USEC          10000
71
72 /* Nic loopback test err  */
73 #define HNS3_NIC_LB_TEST_NO_MEM_ERR     1
74 #define HNS3_NIC_LB_TEST_TX_CNT_ERR     2
75 #define HNS3_NIC_LB_TEST_RX_CNT_ERR     3
76
77 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
78 {
79         struct hnae3_handle *h = hns3_get_handle(ndev);
80         bool vlan_filter_enable;
81         int ret;
82
83         if (!h->ae_algo->ops->set_loopback ||
84             !h->ae_algo->ops->set_promisc_mode)
85                 return -EOPNOTSUPP;
86
87         switch (loop) {
88         case HNAE3_LOOP_SERIAL_SERDES:
89         case HNAE3_LOOP_PARALLEL_SERDES:
90         case HNAE3_LOOP_APP:
91         case HNAE3_LOOP_PHY:
92                 ret = h->ae_algo->ops->set_loopback(h, loop, en);
93                 break;
94         default:
95                 ret = -ENOTSUPP;
96                 break;
97         }
98
99         if (ret || h->pdev->revision >= 0x21)
100                 return ret;
101
102         if (en) {
103                 h->ae_algo->ops->set_promisc_mode(h, true, true);
104         } else {
105                 /* recover promisc mode before loopback test */
106                 hns3_request_update_promisc_mode(h);
107                 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
108                 hns3_enable_vlan_filter(ndev, vlan_filter_enable);
109         }
110
111         return ret;
112 }
113
114 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
115 {
116         struct hnae3_handle *h = hns3_get_handle(ndev);
117         int ret;
118
119         ret = hns3_nic_reset_all_ring(h);
120         if (ret)
121                 return ret;
122
123         ret = hns3_lp_setup(ndev, loop_mode, true);
124         usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
125
126         return ret;
127 }
128
129 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
130 {
131         int ret;
132
133         ret = hns3_lp_setup(ndev, loop_mode, false);
134         if (ret) {
135                 netdev_err(ndev, "lb_setup return error: %d\n", ret);
136                 return ret;
137         }
138
139         usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
140
141         return 0;
142 }
143
144 static void hns3_lp_setup_skb(struct sk_buff *skb)
145 {
146 #define HNS3_NIC_LB_DST_MAC_ADDR        0x1f
147
148         struct net_device *ndev = skb->dev;
149         struct hnae3_handle *handle;
150         unsigned char *packet;
151         struct ethhdr *ethh;
152         unsigned int i;
153
154         skb_reserve(skb, NET_IP_ALIGN);
155         ethh = skb_put(skb, sizeof(struct ethhdr));
156         packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
157
158         memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
159
160         /* The dst mac addr of loopback packet is the same as the host'
161          * mac addr, the SSU component may loop back the packet to host
162          * before the packet reaches mac or serdes, which will defect
163          * the purpose of mac or serdes selftest.
164          */
165         handle = hns3_get_handle(ndev);
166         if (handle->pdev->revision == 0x20)
167                 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
168         eth_zero_addr(ethh->h_source);
169         ethh->h_proto = htons(ETH_P_ARP);
170         skb_reset_mac_header(skb);
171
172         for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
173                 packet[i] = (unsigned char)(i & 0xff);
174 }
175
176 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
177                                    struct sk_buff *skb)
178 {
179         struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
180         unsigned char *packet = skb->data;
181         u32 len = skb_headlen(skb);
182         u32 i;
183
184         len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
185
186         for (i = 0; i < len; i++)
187                 if (packet[i] != (unsigned char)(i & 0xff))
188                         break;
189
190         /* The packet is correctly received */
191         if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
192                 tqp_vector->rx_group.total_packets++;
193         else
194                 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
195                                skb->data, len, true);
196
197         dev_kfree_skb_any(skb);
198 }
199
200 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
201 {
202         struct hnae3_handle *h = priv->ae_handle;
203         struct hnae3_knic_private_info *kinfo;
204         u32 i, rcv_good_pkt_total = 0;
205
206         kinfo = &h->kinfo;
207         for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
208                 struct hns3_enet_ring *ring = &priv->ring[i];
209                 struct hns3_enet_ring_group *rx_group;
210                 u64 pre_rx_pkt;
211
212                 rx_group = &ring->tqp_vector->rx_group;
213                 pre_rx_pkt = rx_group->total_packets;
214
215                 preempt_disable();
216                 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
217                 preempt_enable();
218
219                 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
220                 rx_group->total_packets = pre_rx_pkt;
221         }
222         return rcv_good_pkt_total;
223 }
224
225 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
226                                   u32 end_ringid, u32 budget)
227 {
228         u32 i;
229
230         for (i = start_ringid; i <= end_ringid; i++) {
231                 struct hns3_enet_ring *ring = &priv->ring[i];
232
233                 hns3_clean_tx_ring(ring);
234         }
235 }
236
237 /**
238  * hns3_lp_run_test -  run loopback test
239  * @ndev: net device
240  * @mode: loopback type
241  */
242 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
243 {
244         struct hns3_nic_priv *priv = netdev_priv(ndev);
245         struct sk_buff *skb;
246         u32 i, good_cnt;
247         int ret_val = 0;
248
249         skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
250                         GFP_KERNEL);
251         if (!skb)
252                 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
253
254         skb->dev = ndev;
255         hns3_lp_setup_skb(skb);
256         skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
257
258         good_cnt = 0;
259         for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
260                 netdev_tx_t tx_ret;
261
262                 skb_get(skb);
263                 tx_ret = hns3_nic_net_xmit(skb, ndev);
264                 if (tx_ret == NETDEV_TX_OK) {
265                         good_cnt++;
266                 } else {
267                         kfree_skb(skb);
268                         netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
269                                    tx_ret);
270                 }
271         }
272         if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
273                 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
274                 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
275                            mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
276                 goto out;
277         }
278
279         /* Allow 200 milliseconds for packets to go from Tx to Rx */
280         msleep(200);
281
282         good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
283         if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
284                 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
285                 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
286                            mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
287         }
288
289 out:
290         hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
291                               HNS3_NIC_LB_TEST_RING_ID,
292                               HNS3_NIC_LB_TEST_PKT_NUM);
293
294         kfree_skb(skb);
295         return ret_val;
296 }
297
298 /**
299  * hns3_nic_self_test - self test
300  * @ndev: net device
301  * @eth_test: test cmd
302  * @data: test result
303  */
304 static void hns3_self_test(struct net_device *ndev,
305                            struct ethtool_test *eth_test, u64 *data)
306 {
307         struct hns3_nic_priv *priv = netdev_priv(ndev);
308         struct hnae3_handle *h = priv->ae_handle;
309         int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
310         bool if_running = netif_running(ndev);
311 #if IS_ENABLED(CONFIG_VLAN_8021Q)
312         bool dis_vlan_filter;
313 #endif
314         int test_index = 0;
315         u32 i;
316
317         if (hns3_nic_resetting(ndev)) {
318                 netdev_err(ndev, "dev resetting!");
319                 return;
320         }
321
322         /* Only do offline selftest, or pass by default */
323         if (eth_test->flags != ETH_TEST_FL_OFFLINE)
324                 return;
325
326         netif_dbg(h, drv, ndev, "self test start");
327
328         st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
329         st_param[HNAE3_LOOP_APP][1] =
330                         h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
331
332         st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
333         st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
334                         h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
335
336         st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
337                         HNAE3_LOOP_PARALLEL_SERDES;
338         st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
339                         h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
340
341         st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
342         st_param[HNAE3_LOOP_PHY][1] =
343                         h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
344
345         if (if_running)
346                 ndev->netdev_ops->ndo_stop(ndev);
347
348 #if IS_ENABLED(CONFIG_VLAN_8021Q)
349         /* Disable the vlan filter for selftest does not support it */
350         dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
351                                 h->ae_algo->ops->enable_vlan_filter;
352         if (dis_vlan_filter)
353                 h->ae_algo->ops->enable_vlan_filter(h, false);
354 #endif
355
356         /* Tell firmware to stop mac autoneg before loopback test start,
357          * otherwise loopback test may be failed when the port is still
358          * negotiating.
359          */
360         if (h->ae_algo->ops->halt_autoneg)
361                 h->ae_algo->ops->halt_autoneg(h, true);
362
363         set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
364
365         for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
366                 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
367
368                 if (!st_param[i][1])
369                         continue;
370
371                 data[test_index] = hns3_lp_up(ndev, loop_type);
372                 if (!data[test_index])
373                         data[test_index] = hns3_lp_run_test(ndev, loop_type);
374
375                 hns3_lp_down(ndev, loop_type);
376
377                 if (data[test_index])
378                         eth_test->flags |= ETH_TEST_FL_FAILED;
379
380                 test_index++;
381         }
382
383         clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
384
385         if (h->ae_algo->ops->halt_autoneg)
386                 h->ae_algo->ops->halt_autoneg(h, false);
387
388 #if IS_ENABLED(CONFIG_VLAN_8021Q)
389         if (dis_vlan_filter)
390                 h->ae_algo->ops->enable_vlan_filter(h, true);
391 #endif
392
393         if (if_running)
394                 ndev->netdev_ops->ndo_open(ndev);
395
396         netif_dbg(h, drv, ndev, "self test end\n");
397 }
398
399 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
400 {
401         struct hnae3_handle *h = hns3_get_handle(netdev);
402         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
403
404         if (!ops->get_sset_count)
405                 return -EOPNOTSUPP;
406
407         switch (stringset) {
408         case ETH_SS_STATS:
409                 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
410                         ops->get_sset_count(h, stringset));
411
412         case ETH_SS_TEST:
413                 return ops->get_sset_count(h, stringset);
414
415         default:
416                 return -EOPNOTSUPP;
417         }
418 }
419
420 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
421                 u32 stat_count, u32 num_tqps, const char *prefix)
422 {
423 #define MAX_PREFIX_SIZE (6 + 4)
424         u32 size_left;
425         u32 i, j;
426         u32 n1;
427
428         for (i = 0; i < num_tqps; i++) {
429                 for (j = 0; j < stat_count; j++) {
430                         data[ETH_GSTRING_LEN - 1] = '\0';
431
432                         /* first, prepend the prefix string */
433                         n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%d_",
434                                        prefix, i);
435                         size_left = (ETH_GSTRING_LEN - 1) - n1;
436
437                         /* now, concatenate the stats string to it */
438                         strncat(data, stats[j].stats_string, size_left);
439                         data += ETH_GSTRING_LEN;
440                 }
441         }
442
443         return data;
444 }
445
446 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
447 {
448         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
449         const char tx_prefix[] = "txq";
450         const char rx_prefix[] = "rxq";
451
452         /* get strings for Tx */
453         data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
454                                    kinfo->num_tqps, tx_prefix);
455
456         /* get strings for Rx */
457         data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
458                                    kinfo->num_tqps, rx_prefix);
459
460         return data;
461 }
462
463 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
464 {
465         struct hnae3_handle *h = hns3_get_handle(netdev);
466         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
467         char *buff = (char *)data;
468
469         if (!ops->get_strings)
470                 return;
471
472         switch (stringset) {
473         case ETH_SS_STATS:
474                 buff = hns3_get_strings_tqps(h, buff);
475                 ops->get_strings(h, stringset, (u8 *)buff);
476                 break;
477         case ETH_SS_TEST:
478                 ops->get_strings(h, stringset, data);
479                 break;
480         default:
481                 break;
482         }
483 }
484
485 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
486 {
487         struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
488         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
489         struct hns3_enet_ring *ring;
490         u8 *stat;
491         int i, j;
492
493         /* get stats for Tx */
494         for (i = 0; i < kinfo->num_tqps; i++) {
495                 ring = &nic_priv->ring[i];
496                 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
497                         stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
498                         *data++ = *(u64 *)stat;
499                 }
500         }
501
502         /* get stats for Rx */
503         for (i = 0; i < kinfo->num_tqps; i++) {
504                 ring = &nic_priv->ring[i + kinfo->num_tqps];
505                 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
506                         stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
507                         *data++ = *(u64 *)stat;
508                 }
509         }
510
511         return data;
512 }
513
514 /* hns3_get_stats - get detail statistics.
515  * @netdev: net device
516  * @stats: statistics info.
517  * @data: statistics data.
518  */
519 static void hns3_get_stats(struct net_device *netdev,
520                            struct ethtool_stats *stats, u64 *data)
521 {
522         struct hnae3_handle *h = hns3_get_handle(netdev);
523         u64 *p = data;
524
525         if (hns3_nic_resetting(netdev)) {
526                 netdev_err(netdev, "dev resetting, could not get stats\n");
527                 return;
528         }
529
530         if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
531                 netdev_err(netdev, "could not get any statistics\n");
532                 return;
533         }
534
535         h->ae_algo->ops->update_stats(h, &netdev->stats);
536
537         /* get per-queue stats */
538         p = hns3_get_stats_tqps(h, p);
539
540         /* get MAC & other misc hardware stats */
541         h->ae_algo->ops->get_stats(h, p);
542 }
543
544 static void hns3_get_drvinfo(struct net_device *netdev,
545                              struct ethtool_drvinfo *drvinfo)
546 {
547         struct hns3_nic_priv *priv = netdev_priv(netdev);
548         struct hnae3_handle *h = priv->ae_handle;
549         u32 fw_version;
550
551         if (!h->ae_algo->ops->get_fw_version) {
552                 netdev_err(netdev, "could not get fw version!\n");
553                 return;
554         }
555
556         strncpy(drvinfo->driver, h->pdev->driver->name,
557                 sizeof(drvinfo->driver));
558         drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
559
560         strncpy(drvinfo->bus_info, pci_name(h->pdev),
561                 sizeof(drvinfo->bus_info));
562         drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
563
564         fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
565
566         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
567                  "%lu.%lu.%lu.%lu",
568                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
569                                  HNAE3_FW_VERSION_BYTE3_SHIFT),
570                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
571                                  HNAE3_FW_VERSION_BYTE2_SHIFT),
572                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
573                                  HNAE3_FW_VERSION_BYTE1_SHIFT),
574                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
575                                  HNAE3_FW_VERSION_BYTE0_SHIFT));
576 }
577
578 static u32 hns3_get_link(struct net_device *netdev)
579 {
580         struct hnae3_handle *h = hns3_get_handle(netdev);
581
582         if (h->ae_algo->ops->get_status)
583                 return h->ae_algo->ops->get_status(h);
584         else
585                 return 0;
586 }
587
588 static void hns3_get_ringparam(struct net_device *netdev,
589                                struct ethtool_ringparam *param)
590 {
591         struct hns3_nic_priv *priv = netdev_priv(netdev);
592         struct hnae3_handle *h = priv->ae_handle;
593         int queue_num = h->kinfo.num_tqps;
594
595         if (hns3_nic_resetting(netdev)) {
596                 netdev_err(netdev, "dev resetting!");
597                 return;
598         }
599
600         param->tx_max_pending = HNS3_RING_MAX_PENDING;
601         param->rx_max_pending = HNS3_RING_MAX_PENDING;
602
603         param->tx_pending = priv->ring[0].desc_num;
604         param->rx_pending = priv->ring[queue_num].desc_num;
605 }
606
607 static void hns3_get_pauseparam(struct net_device *netdev,
608                                 struct ethtool_pauseparam *param)
609 {
610         struct hnae3_handle *h = hns3_get_handle(netdev);
611
612         if (h->ae_algo->ops->get_pauseparam)
613                 h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
614                         &param->rx_pause, &param->tx_pause);
615 }
616
617 static int hns3_set_pauseparam(struct net_device *netdev,
618                                struct ethtool_pauseparam *param)
619 {
620         struct hnae3_handle *h = hns3_get_handle(netdev);
621
622         netif_dbg(h, drv, netdev,
623                   "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
624                   param->autoneg, param->rx_pause, param->tx_pause);
625
626         if (h->ae_algo->ops->set_pauseparam)
627                 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
628                                                        param->rx_pause,
629                                                        param->tx_pause);
630         return -EOPNOTSUPP;
631 }
632
633 static void hns3_get_ksettings(struct hnae3_handle *h,
634                                struct ethtool_link_ksettings *cmd)
635 {
636         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
637
638         /* 1.auto_neg & speed & duplex from cmd */
639         if (ops->get_ksettings_an_result)
640                 ops->get_ksettings_an_result(h,
641                                              &cmd->base.autoneg,
642                                              &cmd->base.speed,
643                                              &cmd->base.duplex);
644
645         /* 2.get link mode */
646         if (ops->get_link_mode)
647                 ops->get_link_mode(h,
648                                    cmd->link_modes.supported,
649                                    cmd->link_modes.advertising);
650
651         /* 3.mdix_ctrl&mdix get from phy reg */
652         if (ops->get_mdix_mode)
653                 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
654                                    &cmd->base.eth_tp_mdix);
655 }
656
657 static int hns3_get_link_ksettings(struct net_device *netdev,
658                                    struct ethtool_link_ksettings *cmd)
659 {
660         struct hnae3_handle *h = hns3_get_handle(netdev);
661         const struct hnae3_ae_ops *ops;
662         u8 module_type;
663         u8 media_type;
664         u8 link_stat;
665
666         ops = h->ae_algo->ops;
667         if (ops->get_media_type)
668                 ops->get_media_type(h, &media_type, &module_type);
669         else
670                 return -EOPNOTSUPP;
671
672         switch (media_type) {
673         case HNAE3_MEDIA_TYPE_NONE:
674                 cmd->base.port = PORT_NONE;
675                 hns3_get_ksettings(h, cmd);
676                 break;
677         case HNAE3_MEDIA_TYPE_FIBER:
678                 if (module_type == HNAE3_MODULE_TYPE_CR)
679                         cmd->base.port = PORT_DA;
680                 else
681                         cmd->base.port = PORT_FIBRE;
682
683                 hns3_get_ksettings(h, cmd);
684                 break;
685         case HNAE3_MEDIA_TYPE_BACKPLANE:
686                 cmd->base.port = PORT_NONE;
687                 hns3_get_ksettings(h, cmd);
688                 break;
689         case HNAE3_MEDIA_TYPE_COPPER:
690                 cmd->base.port = PORT_TP;
691                 if (!netdev->phydev)
692                         hns3_get_ksettings(h, cmd);
693                 else
694                         phy_ethtool_ksettings_get(netdev->phydev, cmd);
695                 break;
696         default:
697
698                 netdev_warn(netdev, "Unknown media type");
699                 return 0;
700         }
701
702         /* mdio_support */
703         cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
704
705         link_stat = hns3_get_link(netdev);
706         if (!link_stat) {
707                 cmd->base.speed = SPEED_UNKNOWN;
708                 cmd->base.duplex = DUPLEX_UNKNOWN;
709         }
710
711         return 0;
712 }
713
714 static int hns3_check_ksettings_param(const struct net_device *netdev,
715                                       const struct ethtool_link_ksettings *cmd)
716 {
717         struct hnae3_handle *handle = hns3_get_handle(netdev);
718         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
719         u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
720         u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
721         u8 autoneg;
722         u32 speed;
723         u8 duplex;
724         int ret;
725
726         /* hw doesn't support use specified speed and duplex to negotiate,
727          * unnecessary to check them when autoneg on.
728          */
729         if (cmd->base.autoneg)
730                 return 0;
731
732         if (ops->get_ksettings_an_result) {
733                 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
734                 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
735                     cmd->base.duplex == duplex)
736                         return 0;
737         }
738
739         if (ops->get_media_type)
740                 ops->get_media_type(handle, &media_type, &module_type);
741
742         if (cmd->base.duplex == DUPLEX_HALF &&
743             media_type != HNAE3_MEDIA_TYPE_COPPER) {
744                 netdev_err(netdev,
745                            "only copper port supports half duplex!");
746                 return -EINVAL;
747         }
748
749         if (ops->check_port_speed) {
750                 ret = ops->check_port_speed(handle, cmd->base.speed);
751                 if (ret) {
752                         netdev_err(netdev, "unsupported speed\n");
753                         return ret;
754                 }
755         }
756
757         return 0;
758 }
759
760 static int hns3_set_link_ksettings(struct net_device *netdev,
761                                    const struct ethtool_link_ksettings *cmd)
762 {
763         struct hnae3_handle *handle = hns3_get_handle(netdev);
764         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
765         int ret;
766
767         /* Chip don't support this mode. */
768         if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
769                 return -EINVAL;
770
771         netif_dbg(handle, drv, netdev,
772                   "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
773                   netdev->phydev ? "phy" : "mac",
774                   cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
775
776         /* Only support ksettings_set for netdev with phy attached for now */
777         if (netdev->phydev) {
778                 if (cmd->base.speed == SPEED_1000 &&
779                     cmd->base.autoneg == AUTONEG_DISABLE)
780                         return -EINVAL;
781
782                 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
783         }
784
785         if (handle->pdev->revision == 0x20)
786                 return -EOPNOTSUPP;
787
788         ret = hns3_check_ksettings_param(netdev, cmd);
789         if (ret)
790                 return ret;
791
792         if (ops->set_autoneg) {
793                 ret = ops->set_autoneg(handle, cmd->base.autoneg);
794                 if (ret)
795                         return ret;
796         }
797
798         /* hw doesn't support use specified speed and duplex to negotiate,
799          * ignore them when autoneg on.
800          */
801         if (cmd->base.autoneg) {
802                 netdev_info(netdev,
803                             "autoneg is on, ignore the speed and duplex\n");
804                 return 0;
805         }
806
807         if (ops->cfg_mac_speed_dup_h)
808                 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
809                                                cmd->base.duplex);
810
811         return ret;
812 }
813
814 static u32 hns3_get_rss_key_size(struct net_device *netdev)
815 {
816         struct hnae3_handle *h = hns3_get_handle(netdev);
817
818         if (!h->ae_algo->ops->get_rss_key_size)
819                 return 0;
820
821         return h->ae_algo->ops->get_rss_key_size(h);
822 }
823
824 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
825 {
826         struct hnae3_handle *h = hns3_get_handle(netdev);
827
828         if (!h->ae_algo->ops->get_rss_indir_size)
829                 return 0;
830
831         return h->ae_algo->ops->get_rss_indir_size(h);
832 }
833
834 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
835                         u8 *hfunc)
836 {
837         struct hnae3_handle *h = hns3_get_handle(netdev);
838
839         if (!h->ae_algo->ops->get_rss)
840                 return -EOPNOTSUPP;
841
842         return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
843 }
844
845 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
846                         const u8 *key, const u8 hfunc)
847 {
848         struct hnae3_handle *h = hns3_get_handle(netdev);
849
850         if (!h->ae_algo->ops->set_rss)
851                 return -EOPNOTSUPP;
852
853         if ((h->pdev->revision == 0x20 &&
854              hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
855              hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
856                 netdev_err(netdev, "hash func not supported\n");
857                 return -EOPNOTSUPP;
858         }
859
860         if (!indir) {
861                 netdev_err(netdev,
862                            "set rss failed for indir is empty\n");
863                 return -EOPNOTSUPP;
864         }
865
866         return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
867 }
868
869 static int hns3_get_rxnfc(struct net_device *netdev,
870                           struct ethtool_rxnfc *cmd,
871                           u32 *rule_locs)
872 {
873         struct hnae3_handle *h = hns3_get_handle(netdev);
874
875         switch (cmd->cmd) {
876         case ETHTOOL_GRXRINGS:
877                 cmd->data = h->kinfo.num_tqps;
878                 return 0;
879         case ETHTOOL_GRXFH:
880                 if (h->ae_algo->ops->get_rss_tuple)
881                         return h->ae_algo->ops->get_rss_tuple(h, cmd);
882                 return -EOPNOTSUPP;
883         case ETHTOOL_GRXCLSRLCNT:
884                 if (h->ae_algo->ops->get_fd_rule_cnt)
885                         return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
886                 return -EOPNOTSUPP;
887         case ETHTOOL_GRXCLSRULE:
888                 if (h->ae_algo->ops->get_fd_rule_info)
889                         return h->ae_algo->ops->get_fd_rule_info(h, cmd);
890                 return -EOPNOTSUPP;
891         case ETHTOOL_GRXCLSRLALL:
892                 if (h->ae_algo->ops->get_fd_all_rules)
893                         return h->ae_algo->ops->get_fd_all_rules(h, cmd,
894                                                                  rule_locs);
895                 return -EOPNOTSUPP;
896         default:
897                 return -EOPNOTSUPP;
898         }
899 }
900
901 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
902                                         u32 tx_desc_num, u32 rx_desc_num)
903 {
904         struct hnae3_handle *h = priv->ae_handle;
905         int i;
906
907         h->kinfo.num_tx_desc = tx_desc_num;
908         h->kinfo.num_rx_desc = rx_desc_num;
909
910         for (i = 0; i < h->kinfo.num_tqps; i++) {
911                 priv->ring[i].desc_num = tx_desc_num;
912                 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
913         }
914 }
915
916 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
917 {
918         struct hnae3_handle *handle = priv->ae_handle;
919         struct hns3_enet_ring *tmp_rings;
920         int i;
921
922         tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
923                             sizeof(struct hns3_enet_ring), GFP_KERNEL);
924         if (!tmp_rings)
925                 return NULL;
926
927         for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
928                 memcpy(&tmp_rings[i], &priv->ring[i],
929                        sizeof(struct hns3_enet_ring));
930                 tmp_rings[i].skb = NULL;
931         }
932
933         return tmp_rings;
934 }
935
936 static int hns3_check_ringparam(struct net_device *ndev,
937                                 struct ethtool_ringparam *param)
938 {
939         if (hns3_nic_resetting(ndev))
940                 return -EBUSY;
941
942         if (param->rx_mini_pending || param->rx_jumbo_pending)
943                 return -EINVAL;
944
945         if (param->tx_pending > HNS3_RING_MAX_PENDING ||
946             param->tx_pending < HNS3_RING_MIN_PENDING ||
947             param->rx_pending > HNS3_RING_MAX_PENDING ||
948             param->rx_pending < HNS3_RING_MIN_PENDING) {
949                 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
950                            HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
951                 return -EINVAL;
952         }
953
954         return 0;
955 }
956
957 static int hns3_set_ringparam(struct net_device *ndev,
958                               struct ethtool_ringparam *param)
959 {
960         struct hns3_nic_priv *priv = netdev_priv(ndev);
961         struct hnae3_handle *h = priv->ae_handle;
962         struct hns3_enet_ring *tmp_rings;
963         bool if_running = netif_running(ndev);
964         u32 old_tx_desc_num, new_tx_desc_num;
965         u32 old_rx_desc_num, new_rx_desc_num;
966         u16 queue_num = h->kinfo.num_tqps;
967         int ret, i;
968
969         ret = hns3_check_ringparam(ndev, param);
970         if (ret)
971                 return ret;
972
973         /* Hardware requires that its descriptors must be multiple of eight */
974         new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
975         new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
976         old_tx_desc_num = priv->ring[0].desc_num;
977         old_rx_desc_num = priv->ring[queue_num].desc_num;
978         if (old_tx_desc_num == new_tx_desc_num &&
979             old_rx_desc_num == new_rx_desc_num)
980                 return 0;
981
982         tmp_rings = hns3_backup_ringparam(priv);
983         if (!tmp_rings) {
984                 netdev_err(ndev,
985                            "backup ring param failed by allocating memory fail\n");
986                 return -ENOMEM;
987         }
988
989         netdev_info(ndev,
990                     "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
991                     old_tx_desc_num, old_rx_desc_num,
992                     new_tx_desc_num, new_rx_desc_num);
993
994         if (if_running)
995                 ndev->netdev_ops->ndo_stop(ndev);
996
997         hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
998         ret = hns3_init_all_ring(priv);
999         if (ret) {
1000                 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1001                            ret);
1002
1003                 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1004                                             old_rx_desc_num);
1005                 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1006                         memcpy(&priv->ring[i], &tmp_rings[i],
1007                                sizeof(struct hns3_enet_ring));
1008         } else {
1009                 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1010                         hns3_fini_ring(&tmp_rings[i]);
1011         }
1012
1013         kfree(tmp_rings);
1014
1015         if (if_running)
1016                 ret = ndev->netdev_ops->ndo_open(ndev);
1017
1018         return ret;
1019 }
1020
1021 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1022 {
1023         struct hnae3_handle *h = hns3_get_handle(netdev);
1024
1025         switch (cmd->cmd) {
1026         case ETHTOOL_SRXFH:
1027                 if (h->ae_algo->ops->set_rss_tuple)
1028                         return h->ae_algo->ops->set_rss_tuple(h, cmd);
1029                 return -EOPNOTSUPP;
1030         case ETHTOOL_SRXCLSRLINS:
1031                 if (h->ae_algo->ops->add_fd_entry)
1032                         return h->ae_algo->ops->add_fd_entry(h, cmd);
1033                 return -EOPNOTSUPP;
1034         case ETHTOOL_SRXCLSRLDEL:
1035                 if (h->ae_algo->ops->del_fd_entry)
1036                         return h->ae_algo->ops->del_fd_entry(h, cmd);
1037                 return -EOPNOTSUPP;
1038         default:
1039                 return -EOPNOTSUPP;
1040         }
1041 }
1042
1043 static int hns3_nway_reset(struct net_device *netdev)
1044 {
1045         struct hnae3_handle *handle = hns3_get_handle(netdev);
1046         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1047         struct phy_device *phy = netdev->phydev;
1048         int autoneg;
1049
1050         if (!netif_running(netdev))
1051                 return 0;
1052
1053         if (hns3_nic_resetting(netdev)) {
1054                 netdev_err(netdev, "dev resetting!");
1055                 return -EBUSY;
1056         }
1057
1058         if (!ops->get_autoneg || !ops->restart_autoneg)
1059                 return -EOPNOTSUPP;
1060
1061         autoneg = ops->get_autoneg(handle);
1062         if (autoneg != AUTONEG_ENABLE) {
1063                 netdev_err(netdev,
1064                            "Autoneg is off, don't support to restart it\n");
1065                 return -EINVAL;
1066         }
1067
1068         netif_dbg(handle, drv, netdev,
1069                   "nway reset (using %s)\n", phy ? "phy" : "mac");
1070
1071         if (phy)
1072                 return genphy_restart_aneg(phy);
1073
1074         if (handle->pdev->revision == 0x20)
1075                 return -EOPNOTSUPP;
1076
1077         return ops->restart_autoneg(handle);
1078 }
1079
1080 static void hns3_get_channels(struct net_device *netdev,
1081                               struct ethtool_channels *ch)
1082 {
1083         struct hnae3_handle *h = hns3_get_handle(netdev);
1084
1085         if (h->ae_algo->ops->get_channels)
1086                 h->ae_algo->ops->get_channels(h, ch);
1087 }
1088
1089 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1090                                        struct ethtool_coalesce *cmd)
1091 {
1092         struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1093         struct hns3_nic_priv *priv = netdev_priv(netdev);
1094         struct hnae3_handle *h = priv->ae_handle;
1095         u16 queue_num = h->kinfo.num_tqps;
1096
1097         if (hns3_nic_resetting(netdev))
1098                 return -EBUSY;
1099
1100         if (queue >= queue_num) {
1101                 netdev_err(netdev,
1102                            "Invalid queue value %u! Queue max id=%u\n",
1103                            queue, queue_num - 1);
1104                 return -EINVAL;
1105         }
1106
1107         tx_vector = priv->ring[queue].tqp_vector;
1108         rx_vector = priv->ring[queue_num + queue].tqp_vector;
1109
1110         cmd->use_adaptive_tx_coalesce =
1111                         tx_vector->tx_group.coal.gl_adapt_enable;
1112         cmd->use_adaptive_rx_coalesce =
1113                         rx_vector->rx_group.coal.gl_adapt_enable;
1114
1115         cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1116         cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1117
1118         cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1119         cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1120
1121         return 0;
1122 }
1123
1124 static int hns3_get_coalesce(struct net_device *netdev,
1125                              struct ethtool_coalesce *cmd)
1126 {
1127         return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1128 }
1129
1130 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1131                                        struct ethtool_coalesce *cmd)
1132 {
1133         u32 rx_gl, tx_gl;
1134
1135         if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1136                 netdev_err(netdev,
1137                            "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1138                            HNS3_INT_GL_MAX);
1139                 return -EINVAL;
1140         }
1141
1142         if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1143                 netdev_err(netdev,
1144                            "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1145                            HNS3_INT_GL_MAX);
1146                 return -EINVAL;
1147         }
1148
1149         rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1150         if (rx_gl != cmd->rx_coalesce_usecs) {
1151                 netdev_info(netdev,
1152                             "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1153                             cmd->rx_coalesce_usecs, rx_gl);
1154         }
1155
1156         tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1157         if (tx_gl != cmd->tx_coalesce_usecs) {
1158                 netdev_info(netdev,
1159                             "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1160                             cmd->tx_coalesce_usecs, tx_gl);
1161         }
1162
1163         return 0;
1164 }
1165
1166 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1167                                        struct ethtool_coalesce *cmd)
1168 {
1169         u32 rl;
1170
1171         if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1172                 netdev_err(netdev,
1173                            "tx_usecs_high must be same as rx_usecs_high.\n");
1174                 return -EINVAL;
1175         }
1176
1177         if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1178                 netdev_err(netdev,
1179                            "Invalid usecs_high value, usecs_high range is 0-%d\n",
1180                            HNS3_INT_RL_MAX);
1181                 return -EINVAL;
1182         }
1183
1184         rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1185         if (rl != cmd->rx_coalesce_usecs_high) {
1186                 netdev_info(netdev,
1187                             "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1188                             cmd->rx_coalesce_usecs_high, rl);
1189         }
1190
1191         return 0;
1192 }
1193
1194 static int hns3_check_coalesce_para(struct net_device *netdev,
1195                                     struct ethtool_coalesce *cmd)
1196 {
1197         int ret;
1198
1199         ret = hns3_check_gl_coalesce_para(netdev, cmd);
1200         if (ret) {
1201                 netdev_err(netdev,
1202                            "Check gl coalesce param fail. ret = %d\n", ret);
1203                 return ret;
1204         }
1205
1206         ret = hns3_check_rl_coalesce_para(netdev, cmd);
1207         if (ret) {
1208                 netdev_err(netdev,
1209                            "Check rl coalesce param fail. ret = %d\n", ret);
1210                 return ret;
1211         }
1212
1213         if (cmd->use_adaptive_tx_coalesce == 1 ||
1214             cmd->use_adaptive_rx_coalesce == 1) {
1215                 netdev_info(netdev,
1216                             "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1217                             cmd->use_adaptive_tx_coalesce,
1218                             cmd->use_adaptive_rx_coalesce);
1219         }
1220
1221         return 0;
1222 }
1223
1224 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1225                                         struct ethtool_coalesce *cmd,
1226                                         u32 queue)
1227 {
1228         struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1229         struct hns3_nic_priv *priv = netdev_priv(netdev);
1230         struct hnae3_handle *h = priv->ae_handle;
1231         int queue_num = h->kinfo.num_tqps;
1232
1233         tx_vector = priv->ring[queue].tqp_vector;
1234         rx_vector = priv->ring[queue_num + queue].tqp_vector;
1235
1236         tx_vector->tx_group.coal.gl_adapt_enable =
1237                                 cmd->use_adaptive_tx_coalesce;
1238         rx_vector->rx_group.coal.gl_adapt_enable =
1239                                 cmd->use_adaptive_rx_coalesce;
1240
1241         tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1242         rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1243
1244         hns3_set_vector_coalesce_tx_gl(tx_vector,
1245                                        tx_vector->tx_group.coal.int_gl);
1246         hns3_set_vector_coalesce_rx_gl(rx_vector,
1247                                        rx_vector->rx_group.coal.int_gl);
1248
1249         hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1250         hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1251 }
1252
1253 static int hns3_set_coalesce(struct net_device *netdev,
1254                              struct ethtool_coalesce *cmd)
1255 {
1256         struct hnae3_handle *h = hns3_get_handle(netdev);
1257         u16 queue_num = h->kinfo.num_tqps;
1258         int ret;
1259         int i;
1260
1261         if (hns3_nic_resetting(netdev))
1262                 return -EBUSY;
1263
1264         ret = hns3_check_coalesce_para(netdev, cmd);
1265         if (ret)
1266                 return ret;
1267
1268         h->kinfo.int_rl_setting =
1269                 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1270
1271         for (i = 0; i < queue_num; i++)
1272                 hns3_set_coalesce_per_queue(netdev, cmd, i);
1273
1274         return 0;
1275 }
1276
1277 static int hns3_get_regs_len(struct net_device *netdev)
1278 {
1279         struct hnae3_handle *h = hns3_get_handle(netdev);
1280
1281         if (!h->ae_algo->ops->get_regs_len)
1282                 return -EOPNOTSUPP;
1283
1284         return h->ae_algo->ops->get_regs_len(h);
1285 }
1286
1287 static void hns3_get_regs(struct net_device *netdev,
1288                           struct ethtool_regs *cmd, void *data)
1289 {
1290         struct hnae3_handle *h = hns3_get_handle(netdev);
1291
1292         if (!h->ae_algo->ops->get_regs)
1293                 return;
1294
1295         h->ae_algo->ops->get_regs(h, &cmd->version, data);
1296 }
1297
1298 static int hns3_set_phys_id(struct net_device *netdev,
1299                             enum ethtool_phys_id_state state)
1300 {
1301         struct hnae3_handle *h = hns3_get_handle(netdev);
1302
1303         if (!h->ae_algo->ops->set_led_id)
1304                 return -EOPNOTSUPP;
1305
1306         return h->ae_algo->ops->set_led_id(h, state);
1307 }
1308
1309 static u32 hns3_get_msglevel(struct net_device *netdev)
1310 {
1311         struct hnae3_handle *h = hns3_get_handle(netdev);
1312
1313         return h->msg_enable;
1314 }
1315
1316 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1317 {
1318         struct hnae3_handle *h = hns3_get_handle(netdev);
1319
1320         h->msg_enable = msg_level;
1321 }
1322
1323 /* Translate local fec value into ethtool value. */
1324 static unsigned int loc_to_eth_fec(u8 loc_fec)
1325 {
1326         u32 eth_fec = 0;
1327
1328         if (loc_fec & BIT(HNAE3_FEC_AUTO))
1329                 eth_fec |= ETHTOOL_FEC_AUTO;
1330         if (loc_fec & BIT(HNAE3_FEC_RS))
1331                 eth_fec |= ETHTOOL_FEC_RS;
1332         if (loc_fec & BIT(HNAE3_FEC_BASER))
1333                 eth_fec |= ETHTOOL_FEC_BASER;
1334
1335         /* if nothing is set, then FEC is off */
1336         if (!eth_fec)
1337                 eth_fec = ETHTOOL_FEC_OFF;
1338
1339         return eth_fec;
1340 }
1341
1342 /* Translate ethtool fec value into local value. */
1343 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1344 {
1345         u32 loc_fec = 0;
1346
1347         if (eth_fec & ETHTOOL_FEC_OFF)
1348                 return loc_fec;
1349
1350         if (eth_fec & ETHTOOL_FEC_AUTO)
1351                 loc_fec |= BIT(HNAE3_FEC_AUTO);
1352         if (eth_fec & ETHTOOL_FEC_RS)
1353                 loc_fec |= BIT(HNAE3_FEC_RS);
1354         if (eth_fec & ETHTOOL_FEC_BASER)
1355                 loc_fec |= BIT(HNAE3_FEC_BASER);
1356
1357         return loc_fec;
1358 }
1359
1360 static int hns3_get_fecparam(struct net_device *netdev,
1361                              struct ethtool_fecparam *fec)
1362 {
1363         struct hnae3_handle *handle = hns3_get_handle(netdev);
1364         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1365         u8 fec_ability;
1366         u8 fec_mode;
1367
1368         if (handle->pdev->revision == 0x20)
1369                 return -EOPNOTSUPP;
1370
1371         if (!ops->get_fec)
1372                 return -EOPNOTSUPP;
1373
1374         ops->get_fec(handle, &fec_ability, &fec_mode);
1375
1376         fec->fec = loc_to_eth_fec(fec_ability);
1377         fec->active_fec = loc_to_eth_fec(fec_mode);
1378
1379         return 0;
1380 }
1381
1382 static int hns3_set_fecparam(struct net_device *netdev,
1383                              struct ethtool_fecparam *fec)
1384 {
1385         struct hnae3_handle *handle = hns3_get_handle(netdev);
1386         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1387         u32 fec_mode;
1388
1389         if (handle->pdev->revision == 0x20)
1390                 return -EOPNOTSUPP;
1391
1392         if (!ops->set_fec)
1393                 return -EOPNOTSUPP;
1394         fec_mode = eth_to_loc_fec(fec->fec);
1395
1396         netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1397
1398         return ops->set_fec(handle, fec_mode);
1399 }
1400
1401 static int hns3_get_module_info(struct net_device *netdev,
1402                                 struct ethtool_modinfo *modinfo)
1403 {
1404 #define HNS3_SFF_8636_V1_3 0x03
1405
1406         struct hnae3_handle *handle = hns3_get_handle(netdev);
1407         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1408         struct hns3_sfp_type sfp_type;
1409         int ret;
1410
1411         if (handle->pdev->revision == 0x20 || !ops->get_module_eeprom)
1412                 return -EOPNOTSUPP;
1413
1414         memset(&sfp_type, 0, sizeof(sfp_type));
1415         ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1416                                      (u8 *)&sfp_type);
1417         if (ret)
1418                 return ret;
1419
1420         switch (sfp_type.type) {
1421         case SFF8024_ID_SFP:
1422                 modinfo->type = ETH_MODULE_SFF_8472;
1423                 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1424                 break;
1425         case SFF8024_ID_QSFP_8438:
1426                 modinfo->type = ETH_MODULE_SFF_8436;
1427                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1428                 break;
1429         case SFF8024_ID_QSFP_8436_8636:
1430                 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1431                         modinfo->type = ETH_MODULE_SFF_8436;
1432                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1433                 } else {
1434                         modinfo->type = ETH_MODULE_SFF_8636;
1435                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1436                 }
1437                 break;
1438         case SFF8024_ID_QSFP28_8636:
1439                 modinfo->type = ETH_MODULE_SFF_8636;
1440                 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1441                 break;
1442         default:
1443                 netdev_err(netdev, "Optical module unknown: %#x\n",
1444                            sfp_type.type);
1445                 return -EINVAL;
1446         }
1447
1448         return 0;
1449 }
1450
1451 static int hns3_get_module_eeprom(struct net_device *netdev,
1452                                   struct ethtool_eeprom *ee, u8 *data)
1453 {
1454         struct hnae3_handle *handle = hns3_get_handle(netdev);
1455         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1456
1457         if (handle->pdev->revision == 0x20 || !ops->get_module_eeprom)
1458                 return -EOPNOTSUPP;
1459
1460         if (!ee->len)
1461                 return -EINVAL;
1462
1463         memset(data, 0, ee->len);
1464
1465         return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1466 }
1467
1468 #define HNS3_ETHTOOL_COALESCE   (ETHTOOL_COALESCE_USECS |               \
1469                                  ETHTOOL_COALESCE_USE_ADAPTIVE |        \
1470                                  ETHTOOL_COALESCE_RX_USECS_HIGH |       \
1471                                  ETHTOOL_COALESCE_TX_USECS_HIGH)
1472
1473 static const struct ethtool_ops hns3vf_ethtool_ops = {
1474         .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1475         .get_drvinfo = hns3_get_drvinfo,
1476         .get_ringparam = hns3_get_ringparam,
1477         .set_ringparam = hns3_set_ringparam,
1478         .get_strings = hns3_get_strings,
1479         .get_ethtool_stats = hns3_get_stats,
1480         .get_sset_count = hns3_get_sset_count,
1481         .get_rxnfc = hns3_get_rxnfc,
1482         .set_rxnfc = hns3_set_rxnfc,
1483         .get_rxfh_key_size = hns3_get_rss_key_size,
1484         .get_rxfh_indir_size = hns3_get_rss_indir_size,
1485         .get_rxfh = hns3_get_rss,
1486         .set_rxfh = hns3_set_rss,
1487         .get_link_ksettings = hns3_get_link_ksettings,
1488         .get_channels = hns3_get_channels,
1489         .set_channels = hns3_set_channels,
1490         .get_coalesce = hns3_get_coalesce,
1491         .set_coalesce = hns3_set_coalesce,
1492         .get_regs_len = hns3_get_regs_len,
1493         .get_regs = hns3_get_regs,
1494         .get_link = hns3_get_link,
1495         .get_msglevel = hns3_get_msglevel,
1496         .set_msglevel = hns3_set_msglevel,
1497 };
1498
1499 static const struct ethtool_ops hns3_ethtool_ops = {
1500         .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1501         .self_test = hns3_self_test,
1502         .get_drvinfo = hns3_get_drvinfo,
1503         .get_link = hns3_get_link,
1504         .get_ringparam = hns3_get_ringparam,
1505         .set_ringparam = hns3_set_ringparam,
1506         .get_pauseparam = hns3_get_pauseparam,
1507         .set_pauseparam = hns3_set_pauseparam,
1508         .get_strings = hns3_get_strings,
1509         .get_ethtool_stats = hns3_get_stats,
1510         .get_sset_count = hns3_get_sset_count,
1511         .get_rxnfc = hns3_get_rxnfc,
1512         .set_rxnfc = hns3_set_rxnfc,
1513         .get_rxfh_key_size = hns3_get_rss_key_size,
1514         .get_rxfh_indir_size = hns3_get_rss_indir_size,
1515         .get_rxfh = hns3_get_rss,
1516         .set_rxfh = hns3_set_rss,
1517         .get_link_ksettings = hns3_get_link_ksettings,
1518         .set_link_ksettings = hns3_set_link_ksettings,
1519         .nway_reset = hns3_nway_reset,
1520         .get_channels = hns3_get_channels,
1521         .set_channels = hns3_set_channels,
1522         .get_coalesce = hns3_get_coalesce,
1523         .set_coalesce = hns3_set_coalesce,
1524         .get_regs_len = hns3_get_regs_len,
1525         .get_regs = hns3_get_regs,
1526         .set_phys_id = hns3_set_phys_id,
1527         .get_msglevel = hns3_get_msglevel,
1528         .set_msglevel = hns3_set_msglevel,
1529         .get_fecparam = hns3_get_fecparam,
1530         .set_fecparam = hns3_set_fecparam,
1531         .get_module_info = hns3_get_module_info,
1532         .get_module_eeprom = hns3_get_module_eeprom,
1533 };
1534
1535 void hns3_ethtool_set_ops(struct net_device *netdev)
1536 {
1537         struct hnae3_handle *h = hns3_get_handle(netdev);
1538
1539         if (h->flags & HNAE3_SUPPORT_VF)
1540                 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1541         else
1542                 netdev->ethtool_ops = &hns3_ethtool_ops;
1543 }