ethtool: extend coalesce setting uAPI with CQE mode
[linux-2.6-microblaze.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 #include "hns3_ethtool.h"
11
12 /* tqp related stats */
13 #define HNS3_TQP_STAT(_string, _member) {                       \
14         .stats_string = _string,                                \
15         .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
16                         offsetof(struct ring_stats, _member),   \
17 }
18
19 static const struct hns3_stats hns3_txq_stats[] = {
20         /* Tx per-queue statistics */
21         HNS3_TQP_STAT("dropped", sw_err_cnt),
22         HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
23         HNS3_TQP_STAT("packets", tx_pkts),
24         HNS3_TQP_STAT("bytes", tx_bytes),
25         HNS3_TQP_STAT("more", tx_more),
26         HNS3_TQP_STAT("wake", restart_queue),
27         HNS3_TQP_STAT("busy", tx_busy),
28         HNS3_TQP_STAT("copy", tx_copy),
29         HNS3_TQP_STAT("vlan_err", tx_vlan_err),
30         HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
31         HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
32         HNS3_TQP_STAT("tso_err", tx_tso_err),
33         HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
34         HNS3_TQP_STAT("hw_limitation", hw_limitation),
35         HNS3_TQP_STAT("bounce", tx_bounce),
36         HNS3_TQP_STAT("spare_full", tx_spare_full),
37         HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
38         HNS3_TQP_STAT("sgl", tx_sgl),
39         HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
40         HNS3_TQP_STAT("map_sg_err", map_sg_err),
41 };
42
43 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
44
45 static const struct hns3_stats hns3_rxq_stats[] = {
46         /* Rx per-queue statistics */
47         HNS3_TQP_STAT("dropped", sw_err_cnt),
48         HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
49         HNS3_TQP_STAT("packets", rx_pkts),
50         HNS3_TQP_STAT("bytes", rx_bytes),
51         HNS3_TQP_STAT("errors", rx_err_cnt),
52         HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
53         HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
54         HNS3_TQP_STAT("err_bd_num", err_bd_num),
55         HNS3_TQP_STAT("l2_err", l2_err),
56         HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
57         HNS3_TQP_STAT("csum_complete", csum_complete),
58         HNS3_TQP_STAT("multicast", rx_multicast),
59         HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
60         HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
61         HNS3_TQP_STAT("frag_alloc", frag_alloc),
62 };
63
64 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
65
66 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
67
68 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
69
70 #define HNS3_SELF_TEST_TYPE_NUM         4
71 #define HNS3_NIC_LB_TEST_PKT_NUM        1
72 #define HNS3_NIC_LB_TEST_RING_ID        0
73 #define HNS3_NIC_LB_TEST_PACKET_SIZE    128
74 #define HNS3_NIC_LB_SETUP_USEC          10000
75
76 /* Nic loopback test err  */
77 #define HNS3_NIC_LB_TEST_NO_MEM_ERR     1
78 #define HNS3_NIC_LB_TEST_TX_CNT_ERR     2
79 #define HNS3_NIC_LB_TEST_RX_CNT_ERR     3
80
81 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
82 {
83         struct hnae3_handle *h = hns3_get_handle(ndev);
84         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
85         int ret;
86
87         if (!h->ae_algo->ops->set_loopback ||
88             !h->ae_algo->ops->set_promisc_mode)
89                 return -EOPNOTSUPP;
90
91         switch (loop) {
92         case HNAE3_LOOP_SERIAL_SERDES:
93         case HNAE3_LOOP_PARALLEL_SERDES:
94         case HNAE3_LOOP_APP:
95         case HNAE3_LOOP_PHY:
96                 ret = h->ae_algo->ops->set_loopback(h, loop, en);
97                 break;
98         default:
99                 ret = -ENOTSUPP;
100                 break;
101         }
102
103         if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
104                 return ret;
105
106         if (en)
107                 h->ae_algo->ops->set_promisc_mode(h, true, true);
108         else
109                 /* recover promisc mode before loopback test */
110                 hns3_request_update_promisc_mode(h);
111
112         return ret;
113 }
114
115 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
116 {
117         struct hnae3_handle *h = hns3_get_handle(ndev);
118         int ret;
119
120         ret = hns3_nic_reset_all_ring(h);
121         if (ret)
122                 return ret;
123
124         ret = hns3_lp_setup(ndev, loop_mode, true);
125         usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
126
127         return ret;
128 }
129
130 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
131 {
132         int ret;
133
134         ret = hns3_lp_setup(ndev, loop_mode, false);
135         if (ret) {
136                 netdev_err(ndev, "lb_setup return error: %d\n", ret);
137                 return ret;
138         }
139
140         usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
141
142         return 0;
143 }
144
145 static void hns3_lp_setup_skb(struct sk_buff *skb)
146 {
147 #define HNS3_NIC_LB_DST_MAC_ADDR        0x1f
148
149         struct net_device *ndev = skb->dev;
150         struct hnae3_handle *handle;
151         struct hnae3_ae_dev *ae_dev;
152         unsigned char *packet;
153         struct ethhdr *ethh;
154         unsigned int i;
155
156         skb_reserve(skb, NET_IP_ALIGN);
157         ethh = skb_put(skb, sizeof(struct ethhdr));
158         packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
159
160         memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
161
162         /* The dst mac addr of loopback packet is the same as the host'
163          * mac addr, the SSU component may loop back the packet to host
164          * before the packet reaches mac or serdes, which will defect
165          * the purpose of mac or serdes selftest.
166          */
167         handle = hns3_get_handle(ndev);
168         ae_dev = pci_get_drvdata(handle->pdev);
169         if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
170                 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
171         eth_zero_addr(ethh->h_source);
172         ethh->h_proto = htons(ETH_P_ARP);
173         skb_reset_mac_header(skb);
174
175         for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
176                 packet[i] = (unsigned char)(i & 0xff);
177 }
178
179 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
180                                    struct sk_buff *skb)
181 {
182         struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
183         unsigned char *packet = skb->data;
184         u32 len = skb_headlen(skb);
185         u32 i;
186
187         len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
188
189         for (i = 0; i < len; i++)
190                 if (packet[i] != (unsigned char)(i & 0xff))
191                         break;
192
193         /* The packet is correctly received */
194         if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
195                 tqp_vector->rx_group.total_packets++;
196         else
197                 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
198                                skb->data, len, true);
199
200         dev_kfree_skb_any(skb);
201 }
202
203 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
204 {
205         struct hnae3_handle *h = priv->ae_handle;
206         struct hnae3_knic_private_info *kinfo;
207         u32 i, rcv_good_pkt_total = 0;
208
209         kinfo = &h->kinfo;
210         for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
211                 struct hns3_enet_ring *ring = &priv->ring[i];
212                 struct hns3_enet_ring_group *rx_group;
213                 u64 pre_rx_pkt;
214
215                 rx_group = &ring->tqp_vector->rx_group;
216                 pre_rx_pkt = rx_group->total_packets;
217
218                 preempt_disable();
219                 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
220                 preempt_enable();
221
222                 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
223                 rx_group->total_packets = pre_rx_pkt;
224         }
225         return rcv_good_pkt_total;
226 }
227
228 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
229                                   u32 end_ringid, u32 budget)
230 {
231         u32 i;
232
233         for (i = start_ringid; i <= end_ringid; i++) {
234                 struct hns3_enet_ring *ring = &priv->ring[i];
235
236                 hns3_clean_tx_ring(ring, 0);
237         }
238 }
239
240 /**
241  * hns3_lp_run_test -  run loopback test
242  * @ndev: net device
243  * @mode: loopback type
244  */
245 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
246 {
247         struct hns3_nic_priv *priv = netdev_priv(ndev);
248         struct sk_buff *skb;
249         u32 i, good_cnt;
250         int ret_val = 0;
251
252         skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
253                         GFP_KERNEL);
254         if (!skb)
255                 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
256
257         skb->dev = ndev;
258         hns3_lp_setup_skb(skb);
259         skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
260
261         good_cnt = 0;
262         for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
263                 netdev_tx_t tx_ret;
264
265                 skb_get(skb);
266                 tx_ret = hns3_nic_net_xmit(skb, ndev);
267                 if (tx_ret == NETDEV_TX_OK) {
268                         good_cnt++;
269                 } else {
270                         kfree_skb(skb);
271                         netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
272                                    tx_ret);
273                 }
274         }
275         if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
276                 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
277                 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
278                            mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
279                 goto out;
280         }
281
282         /* Allow 200 milliseconds for packets to go from Tx to Rx */
283         msleep(200);
284
285         good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
286         if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
287                 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
288                 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
289                            mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
290         }
291
292 out:
293         hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
294                               HNS3_NIC_LB_TEST_RING_ID,
295                               HNS3_NIC_LB_TEST_PKT_NUM);
296
297         kfree_skb(skb);
298         return ret_val;
299 }
300
301 /**
302  * hns3_self_test - self test
303  * @ndev: net device
304  * @eth_test: test cmd
305  * @data: test result
306  */
307 static void hns3_self_test(struct net_device *ndev,
308                            struct ethtool_test *eth_test, u64 *data)
309 {
310         struct hns3_nic_priv *priv = netdev_priv(ndev);
311         struct hnae3_handle *h = priv->ae_handle;
312         int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
313         bool if_running = netif_running(ndev);
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         if (h->ae_algo->ops->enable_vlan_filter)
351                 h->ae_algo->ops->enable_vlan_filter(h, false);
352 #endif
353
354         /* Tell firmware to stop mac autoneg before loopback test start,
355          * otherwise loopback test may be failed when the port is still
356          * negotiating.
357          */
358         if (h->ae_algo->ops->halt_autoneg)
359                 h->ae_algo->ops->halt_autoneg(h, true);
360
361         set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
362
363         for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
364                 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
365
366                 if (!st_param[i][1])
367                         continue;
368
369                 data[test_index] = hns3_lp_up(ndev, loop_type);
370                 if (!data[test_index])
371                         data[test_index] = hns3_lp_run_test(ndev, loop_type);
372
373                 hns3_lp_down(ndev, loop_type);
374
375                 if (data[test_index])
376                         eth_test->flags |= ETH_TEST_FL_FAILED;
377
378                 test_index++;
379         }
380
381         clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
382
383         if (h->ae_algo->ops->halt_autoneg)
384                 h->ae_algo->ops->halt_autoneg(h, false);
385
386 #if IS_ENABLED(CONFIG_VLAN_8021Q)
387         if (h->ae_algo->ops->enable_vlan_filter)
388                 h->ae_algo->ops->enable_vlan_filter(h, true);
389 #endif
390
391         if (if_running)
392                 ndev->netdev_ops->ndo_open(ndev);
393
394         netif_dbg(h, drv, ndev, "self test end\n");
395 }
396
397 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
398                                            bool enable)
399 {
400         struct hnae3_handle *handle = hns3_get_handle(netdev);
401
402         if (enable)
403                 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
404         else
405                 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
406
407         hns3_request_update_promisc_mode(handle);
408 }
409
410 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
411         { "limit_promisc",      hns3_update_limit_promisc_mode }
412 };
413
414 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
415 {
416         struct hnae3_handle *h = hns3_get_handle(netdev);
417         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
418
419         if (!ops->get_sset_count)
420                 return -EOPNOTSUPP;
421
422         switch (stringset) {
423         case ETH_SS_STATS:
424                 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
425                         ops->get_sset_count(h, stringset));
426
427         case ETH_SS_TEST:
428                 return ops->get_sset_count(h, stringset);
429
430         case ETH_SS_PRIV_FLAGS:
431                 return HNAE3_PFLAG_MAX;
432
433         default:
434                 return -EOPNOTSUPP;
435         }
436 }
437
438 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
439                 u32 stat_count, u32 num_tqps, const char *prefix)
440 {
441 #define MAX_PREFIX_SIZE (6 + 4)
442         u32 size_left;
443         u32 i, j;
444         u32 n1;
445
446         for (i = 0; i < num_tqps; i++) {
447                 for (j = 0; j < stat_count; j++) {
448                         data[ETH_GSTRING_LEN - 1] = '\0';
449
450                         /* first, prepend the prefix string */
451                         n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
452                                        prefix, i);
453                         size_left = (ETH_GSTRING_LEN - 1) - n1;
454
455                         /* now, concatenate the stats string to it */
456                         strncat(data, stats[j].stats_string, size_left);
457                         data += ETH_GSTRING_LEN;
458                 }
459         }
460
461         return data;
462 }
463
464 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
465 {
466         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
467         const char tx_prefix[] = "txq";
468         const char rx_prefix[] = "rxq";
469
470         /* get strings for Tx */
471         data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
472                                    kinfo->num_tqps, tx_prefix);
473
474         /* get strings for Rx */
475         data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
476                                    kinfo->num_tqps, rx_prefix);
477
478         return data;
479 }
480
481 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
482 {
483         struct hnae3_handle *h = hns3_get_handle(netdev);
484         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
485         char *buff = (char *)data;
486         int i;
487
488         if (!ops->get_strings)
489                 return;
490
491         switch (stringset) {
492         case ETH_SS_STATS:
493                 buff = hns3_get_strings_tqps(h, buff);
494                 ops->get_strings(h, stringset, (u8 *)buff);
495                 break;
496         case ETH_SS_TEST:
497                 ops->get_strings(h, stringset, data);
498                 break;
499         case ETH_SS_PRIV_FLAGS:
500                 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
501                         snprintf(buff, ETH_GSTRING_LEN, "%s",
502                                  hns3_priv_flags[i].name);
503                         buff += ETH_GSTRING_LEN;
504                 }
505                 break;
506         default:
507                 break;
508         }
509 }
510
511 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
512 {
513         struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
514         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
515         struct hns3_enet_ring *ring;
516         u8 *stat;
517         int i, j;
518
519         /* get stats for Tx */
520         for (i = 0; i < kinfo->num_tqps; i++) {
521                 ring = &nic_priv->ring[i];
522                 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
523                         stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
524                         *data++ = *(u64 *)stat;
525                 }
526         }
527
528         /* get stats for Rx */
529         for (i = 0; i < kinfo->num_tqps; i++) {
530                 ring = &nic_priv->ring[i + kinfo->num_tqps];
531                 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
532                         stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
533                         *data++ = *(u64 *)stat;
534                 }
535         }
536
537         return data;
538 }
539
540 /* hns3_get_stats - get detail statistics.
541  * @netdev: net device
542  * @stats: statistics info.
543  * @data: statistics data.
544  */
545 static void hns3_get_stats(struct net_device *netdev,
546                            struct ethtool_stats *stats, u64 *data)
547 {
548         struct hnae3_handle *h = hns3_get_handle(netdev);
549         u64 *p = data;
550
551         if (hns3_nic_resetting(netdev)) {
552                 netdev_err(netdev, "dev resetting, could not get stats\n");
553                 return;
554         }
555
556         if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
557                 netdev_err(netdev, "could not get any statistics\n");
558                 return;
559         }
560
561         h->ae_algo->ops->update_stats(h, &netdev->stats);
562
563         /* get per-queue stats */
564         p = hns3_get_stats_tqps(h, p);
565
566         /* get MAC & other misc hardware stats */
567         h->ae_algo->ops->get_stats(h, p);
568 }
569
570 static void hns3_get_drvinfo(struct net_device *netdev,
571                              struct ethtool_drvinfo *drvinfo)
572 {
573         struct hns3_nic_priv *priv = netdev_priv(netdev);
574         struct hnae3_handle *h = priv->ae_handle;
575         u32 fw_version;
576
577         if (!h->ae_algo->ops->get_fw_version) {
578                 netdev_err(netdev, "could not get fw version!\n");
579                 return;
580         }
581
582         strncpy(drvinfo->driver, h->pdev->driver->name,
583                 sizeof(drvinfo->driver));
584         drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
585
586         strncpy(drvinfo->bus_info, pci_name(h->pdev),
587                 sizeof(drvinfo->bus_info));
588         drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
589
590         fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
591
592         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
593                  "%lu.%lu.%lu.%lu",
594                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
595                                  HNAE3_FW_VERSION_BYTE3_SHIFT),
596                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
597                                  HNAE3_FW_VERSION_BYTE2_SHIFT),
598                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
599                                  HNAE3_FW_VERSION_BYTE1_SHIFT),
600                  hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
601                                  HNAE3_FW_VERSION_BYTE0_SHIFT));
602 }
603
604 static u32 hns3_get_link(struct net_device *netdev)
605 {
606         struct hnae3_handle *h = hns3_get_handle(netdev);
607
608         if (h->ae_algo->ops->get_status)
609                 return h->ae_algo->ops->get_status(h);
610         else
611                 return 0;
612 }
613
614 static void hns3_get_ringparam(struct net_device *netdev,
615                                struct ethtool_ringparam *param)
616 {
617         struct hns3_nic_priv *priv = netdev_priv(netdev);
618         struct hnae3_handle *h = priv->ae_handle;
619         int queue_num = h->kinfo.num_tqps;
620
621         if (hns3_nic_resetting(netdev)) {
622                 netdev_err(netdev, "dev resetting!");
623                 return;
624         }
625
626         param->tx_max_pending = HNS3_RING_MAX_PENDING;
627         param->rx_max_pending = HNS3_RING_MAX_PENDING;
628
629         param->tx_pending = priv->ring[0].desc_num;
630         param->rx_pending = priv->ring[queue_num].desc_num;
631 }
632
633 static void hns3_get_pauseparam(struct net_device *netdev,
634                                 struct ethtool_pauseparam *param)
635 {
636         struct hnae3_handle *h = hns3_get_handle(netdev);
637         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
638
639         if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
640                 return;
641
642         if (h->ae_algo->ops->get_pauseparam)
643                 h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
644                         &param->rx_pause, &param->tx_pause);
645 }
646
647 static int hns3_set_pauseparam(struct net_device *netdev,
648                                struct ethtool_pauseparam *param)
649 {
650         struct hnae3_handle *h = hns3_get_handle(netdev);
651         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
652
653         if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
654                 return -EOPNOTSUPP;
655
656         netif_dbg(h, drv, netdev,
657                   "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
658                   param->autoneg, param->rx_pause, param->tx_pause);
659
660         if (h->ae_algo->ops->set_pauseparam)
661                 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
662                                                        param->rx_pause,
663                                                        param->tx_pause);
664         return -EOPNOTSUPP;
665 }
666
667 static void hns3_get_ksettings(struct hnae3_handle *h,
668                                struct ethtool_link_ksettings *cmd)
669 {
670         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
671
672         /* 1.auto_neg & speed & duplex from cmd */
673         if (ops->get_ksettings_an_result)
674                 ops->get_ksettings_an_result(h,
675                                              &cmd->base.autoneg,
676                                              &cmd->base.speed,
677                                              &cmd->base.duplex);
678
679         /* 2.get link mode */
680         if (ops->get_link_mode)
681                 ops->get_link_mode(h,
682                                    cmd->link_modes.supported,
683                                    cmd->link_modes.advertising);
684
685         /* 3.mdix_ctrl&mdix get from phy reg */
686         if (ops->get_mdix_mode)
687                 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
688                                    &cmd->base.eth_tp_mdix);
689 }
690
691 static int hns3_get_link_ksettings(struct net_device *netdev,
692                                    struct ethtool_link_ksettings *cmd)
693 {
694         struct hnae3_handle *h = hns3_get_handle(netdev);
695         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
696         const struct hnae3_ae_ops *ops;
697         u8 module_type;
698         u8 media_type;
699         u8 link_stat;
700
701         ops = h->ae_algo->ops;
702         if (ops->get_media_type)
703                 ops->get_media_type(h, &media_type, &module_type);
704         else
705                 return -EOPNOTSUPP;
706
707         switch (media_type) {
708         case HNAE3_MEDIA_TYPE_NONE:
709                 cmd->base.port = PORT_NONE;
710                 hns3_get_ksettings(h, cmd);
711                 break;
712         case HNAE3_MEDIA_TYPE_FIBER:
713                 if (module_type == HNAE3_MODULE_TYPE_CR)
714                         cmd->base.port = PORT_DA;
715                 else
716                         cmd->base.port = PORT_FIBRE;
717
718                 hns3_get_ksettings(h, cmd);
719                 break;
720         case HNAE3_MEDIA_TYPE_BACKPLANE:
721                 cmd->base.port = PORT_NONE;
722                 hns3_get_ksettings(h, cmd);
723                 break;
724         case HNAE3_MEDIA_TYPE_COPPER:
725                 cmd->base.port = PORT_TP;
726                 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
727                     ops->get_phy_link_ksettings)
728                         ops->get_phy_link_ksettings(h, cmd);
729                 else if (!netdev->phydev)
730                         hns3_get_ksettings(h, cmd);
731                 else
732                         phy_ethtool_ksettings_get(netdev->phydev, cmd);
733                 break;
734         default:
735
736                 netdev_warn(netdev, "Unknown media type");
737                 return 0;
738         }
739
740         /* mdio_support */
741         cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
742
743         link_stat = hns3_get_link(netdev);
744         if (!link_stat) {
745                 cmd->base.speed = SPEED_UNKNOWN;
746                 cmd->base.duplex = DUPLEX_UNKNOWN;
747         }
748
749         return 0;
750 }
751
752 static int hns3_check_ksettings_param(const struct net_device *netdev,
753                                       const struct ethtool_link_ksettings *cmd)
754 {
755         struct hnae3_handle *handle = hns3_get_handle(netdev);
756         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
757         u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
758         u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
759         u8 autoneg;
760         u32 speed;
761         u8 duplex;
762         int ret;
763
764         /* hw doesn't support use specified speed and duplex to negotiate,
765          * unnecessary to check them when autoneg on.
766          */
767         if (cmd->base.autoneg)
768                 return 0;
769
770         if (ops->get_ksettings_an_result) {
771                 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
772                 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
773                     cmd->base.duplex == duplex)
774                         return 0;
775         }
776
777         if (ops->get_media_type)
778                 ops->get_media_type(handle, &media_type, &module_type);
779
780         if (cmd->base.duplex == DUPLEX_HALF &&
781             media_type != HNAE3_MEDIA_TYPE_COPPER) {
782                 netdev_err(netdev,
783                            "only copper port supports half duplex!");
784                 return -EINVAL;
785         }
786
787         if (ops->check_port_speed) {
788                 ret = ops->check_port_speed(handle, cmd->base.speed);
789                 if (ret) {
790                         netdev_err(netdev, "unsupported speed\n");
791                         return ret;
792                 }
793         }
794
795         return 0;
796 }
797
798 static int hns3_set_link_ksettings(struct net_device *netdev,
799                                    const struct ethtool_link_ksettings *cmd)
800 {
801         struct hnae3_handle *handle = hns3_get_handle(netdev);
802         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
803         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
804         int ret;
805
806         /* Chip don't support this mode. */
807         if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
808                 return -EINVAL;
809
810         netif_dbg(handle, drv, netdev,
811                   "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
812                   netdev->phydev ? "phy" : "mac",
813                   cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
814
815         /* Only support ksettings_set for netdev with phy attached for now */
816         if (netdev->phydev) {
817                 if (cmd->base.speed == SPEED_1000 &&
818                     cmd->base.autoneg == AUTONEG_DISABLE)
819                         return -EINVAL;
820
821                 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
822         } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
823                    ops->set_phy_link_ksettings) {
824                 return ops->set_phy_link_ksettings(handle, cmd);
825         }
826
827         if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
828                 return -EOPNOTSUPP;
829
830         ret = hns3_check_ksettings_param(netdev, cmd);
831         if (ret)
832                 return ret;
833
834         if (ops->set_autoneg) {
835                 ret = ops->set_autoneg(handle, cmd->base.autoneg);
836                 if (ret)
837                         return ret;
838         }
839
840         /* hw doesn't support use specified speed and duplex to negotiate,
841          * ignore them when autoneg on.
842          */
843         if (cmd->base.autoneg) {
844                 netdev_info(netdev,
845                             "autoneg is on, ignore the speed and duplex\n");
846                 return 0;
847         }
848
849         if (ops->cfg_mac_speed_dup_h)
850                 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
851                                                cmd->base.duplex);
852
853         return ret;
854 }
855
856 static u32 hns3_get_rss_key_size(struct net_device *netdev)
857 {
858         struct hnae3_handle *h = hns3_get_handle(netdev);
859
860         if (!h->ae_algo->ops->get_rss_key_size)
861                 return 0;
862
863         return h->ae_algo->ops->get_rss_key_size(h);
864 }
865
866 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
867 {
868         struct hnae3_handle *h = hns3_get_handle(netdev);
869         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
870
871         return ae_dev->dev_specs.rss_ind_tbl_size;
872 }
873
874 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
875                         u8 *hfunc)
876 {
877         struct hnae3_handle *h = hns3_get_handle(netdev);
878
879         if (!h->ae_algo->ops->get_rss)
880                 return -EOPNOTSUPP;
881
882         return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
883 }
884
885 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
886                         const u8 *key, const u8 hfunc)
887 {
888         struct hnae3_handle *h = hns3_get_handle(netdev);
889         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
890
891         if (!h->ae_algo->ops->set_rss)
892                 return -EOPNOTSUPP;
893
894         if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
895              hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
896              hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
897                 netdev_err(netdev, "hash func not supported\n");
898                 return -EOPNOTSUPP;
899         }
900
901         if (!indir) {
902                 netdev_err(netdev,
903                            "set rss failed for indir is empty\n");
904                 return -EOPNOTSUPP;
905         }
906
907         return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
908 }
909
910 static int hns3_get_rxnfc(struct net_device *netdev,
911                           struct ethtool_rxnfc *cmd,
912                           u32 *rule_locs)
913 {
914         struct hnae3_handle *h = hns3_get_handle(netdev);
915
916         switch (cmd->cmd) {
917         case ETHTOOL_GRXRINGS:
918                 cmd->data = h->kinfo.num_tqps;
919                 return 0;
920         case ETHTOOL_GRXFH:
921                 if (h->ae_algo->ops->get_rss_tuple)
922                         return h->ae_algo->ops->get_rss_tuple(h, cmd);
923                 return -EOPNOTSUPP;
924         case ETHTOOL_GRXCLSRLCNT:
925                 if (h->ae_algo->ops->get_fd_rule_cnt)
926                         return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
927                 return -EOPNOTSUPP;
928         case ETHTOOL_GRXCLSRULE:
929                 if (h->ae_algo->ops->get_fd_rule_info)
930                         return h->ae_algo->ops->get_fd_rule_info(h, cmd);
931                 return -EOPNOTSUPP;
932         case ETHTOOL_GRXCLSRLALL:
933                 if (h->ae_algo->ops->get_fd_all_rules)
934                         return h->ae_algo->ops->get_fd_all_rules(h, cmd,
935                                                                  rule_locs);
936                 return -EOPNOTSUPP;
937         default:
938                 return -EOPNOTSUPP;
939         }
940 }
941
942 static const struct hns3_reset_type_map hns3_reset_type[] = {
943         {ETH_RESET_MGMT, HNAE3_IMP_RESET},
944         {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
945         {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
946 };
947
948 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
949         {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
950 };
951
952 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
953 {
954         enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
955         struct hnae3_handle *h = hns3_get_handle(netdev);
956         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
957         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
958         const struct hns3_reset_type_map *rst_type_map;
959         u32 i, size;
960
961         if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
962                 return -EBUSY;
963
964         if (!ops->set_default_reset_request || !ops->reset_event)
965                 return -EOPNOTSUPP;
966
967         if (h->flags & HNAE3_SUPPORT_VF) {
968                 rst_type_map = hns3vf_reset_type;
969                 size = ARRAY_SIZE(hns3vf_reset_type);
970         } else {
971                 rst_type_map = hns3_reset_type;
972                 size = ARRAY_SIZE(hns3_reset_type);
973         }
974
975         for (i = 0; i < size; i++) {
976                 if (rst_type_map[i].rst_flags == *flags) {
977                         rst_type = rst_type_map[i].rst_type;
978                         break;
979                 }
980         }
981
982         if (rst_type == HNAE3_NONE_RESET ||
983             (rst_type == HNAE3_IMP_RESET &&
984              ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
985                 return -EOPNOTSUPP;
986
987         netdev_info(netdev, "Setting reset type %d\n", rst_type);
988
989         ops->set_default_reset_request(ae_dev, rst_type);
990
991         ops->reset_event(h->pdev, h);
992
993         return 0;
994 }
995
996 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
997                                         u32 tx_desc_num, u32 rx_desc_num)
998 {
999         struct hnae3_handle *h = priv->ae_handle;
1000         int i;
1001
1002         h->kinfo.num_tx_desc = tx_desc_num;
1003         h->kinfo.num_rx_desc = rx_desc_num;
1004
1005         for (i = 0; i < h->kinfo.num_tqps; i++) {
1006                 priv->ring[i].desc_num = tx_desc_num;
1007                 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1008         }
1009 }
1010
1011 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1012 {
1013         struct hnae3_handle *handle = priv->ae_handle;
1014         struct hns3_enet_ring *tmp_rings;
1015         int i;
1016
1017         tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1018                             sizeof(struct hns3_enet_ring), GFP_KERNEL);
1019         if (!tmp_rings)
1020                 return NULL;
1021
1022         for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1023                 memcpy(&tmp_rings[i], &priv->ring[i],
1024                        sizeof(struct hns3_enet_ring));
1025                 tmp_rings[i].skb = NULL;
1026         }
1027
1028         return tmp_rings;
1029 }
1030
1031 static int hns3_check_ringparam(struct net_device *ndev,
1032                                 struct ethtool_ringparam *param)
1033 {
1034         if (hns3_nic_resetting(ndev))
1035                 return -EBUSY;
1036
1037         if (param->rx_mini_pending || param->rx_jumbo_pending)
1038                 return -EINVAL;
1039
1040         if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1041             param->tx_pending < HNS3_RING_MIN_PENDING ||
1042             param->rx_pending > HNS3_RING_MAX_PENDING ||
1043             param->rx_pending < HNS3_RING_MIN_PENDING) {
1044                 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1045                            HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1046                 return -EINVAL;
1047         }
1048
1049         return 0;
1050 }
1051
1052 static int hns3_set_ringparam(struct net_device *ndev,
1053                               struct ethtool_ringparam *param)
1054 {
1055         struct hns3_nic_priv *priv = netdev_priv(ndev);
1056         struct hnae3_handle *h = priv->ae_handle;
1057         struct hns3_enet_ring *tmp_rings;
1058         bool if_running = netif_running(ndev);
1059         u32 old_tx_desc_num, new_tx_desc_num;
1060         u32 old_rx_desc_num, new_rx_desc_num;
1061         u16 queue_num = h->kinfo.num_tqps;
1062         int ret, i;
1063
1064         ret = hns3_check_ringparam(ndev, param);
1065         if (ret)
1066                 return ret;
1067
1068         /* Hardware requires that its descriptors must be multiple of eight */
1069         new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1070         new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1071         old_tx_desc_num = priv->ring[0].desc_num;
1072         old_rx_desc_num = priv->ring[queue_num].desc_num;
1073         if (old_tx_desc_num == new_tx_desc_num &&
1074             old_rx_desc_num == new_rx_desc_num)
1075                 return 0;
1076
1077         tmp_rings = hns3_backup_ringparam(priv);
1078         if (!tmp_rings) {
1079                 netdev_err(ndev,
1080                            "backup ring param failed by allocating memory fail\n");
1081                 return -ENOMEM;
1082         }
1083
1084         netdev_info(ndev,
1085                     "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1086                     old_tx_desc_num, old_rx_desc_num,
1087                     new_tx_desc_num, new_rx_desc_num);
1088
1089         if (if_running)
1090                 ndev->netdev_ops->ndo_stop(ndev);
1091
1092         hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1093         ret = hns3_init_all_ring(priv);
1094         if (ret) {
1095                 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1096                            ret);
1097
1098                 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1099                                             old_rx_desc_num);
1100                 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1101                         memcpy(&priv->ring[i], &tmp_rings[i],
1102                                sizeof(struct hns3_enet_ring));
1103         } else {
1104                 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1105                         hns3_fini_ring(&tmp_rings[i]);
1106         }
1107
1108         kfree(tmp_rings);
1109
1110         if (if_running)
1111                 ret = ndev->netdev_ops->ndo_open(ndev);
1112
1113         return ret;
1114 }
1115
1116 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1117 {
1118         struct hnae3_handle *h = hns3_get_handle(netdev);
1119
1120         switch (cmd->cmd) {
1121         case ETHTOOL_SRXFH:
1122                 if (h->ae_algo->ops->set_rss_tuple)
1123                         return h->ae_algo->ops->set_rss_tuple(h, cmd);
1124                 return -EOPNOTSUPP;
1125         case ETHTOOL_SRXCLSRLINS:
1126                 if (h->ae_algo->ops->add_fd_entry)
1127                         return h->ae_algo->ops->add_fd_entry(h, cmd);
1128                 return -EOPNOTSUPP;
1129         case ETHTOOL_SRXCLSRLDEL:
1130                 if (h->ae_algo->ops->del_fd_entry)
1131                         return h->ae_algo->ops->del_fd_entry(h, cmd);
1132                 return -EOPNOTSUPP;
1133         default:
1134                 return -EOPNOTSUPP;
1135         }
1136 }
1137
1138 static int hns3_nway_reset(struct net_device *netdev)
1139 {
1140         struct hnae3_handle *handle = hns3_get_handle(netdev);
1141         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1142         struct phy_device *phy = netdev->phydev;
1143         int autoneg;
1144
1145         if (!netif_running(netdev))
1146                 return 0;
1147
1148         if (hns3_nic_resetting(netdev)) {
1149                 netdev_err(netdev, "dev resetting!");
1150                 return -EBUSY;
1151         }
1152
1153         if (!ops->get_autoneg || !ops->restart_autoneg)
1154                 return -EOPNOTSUPP;
1155
1156         autoneg = ops->get_autoneg(handle);
1157         if (autoneg != AUTONEG_ENABLE) {
1158                 netdev_err(netdev,
1159                            "Autoneg is off, don't support to restart it\n");
1160                 return -EINVAL;
1161         }
1162
1163         netif_dbg(handle, drv, netdev,
1164                   "nway reset (using %s)\n", phy ? "phy" : "mac");
1165
1166         if (phy)
1167                 return genphy_restart_aneg(phy);
1168
1169         return ops->restart_autoneg(handle);
1170 }
1171
1172 static void hns3_get_channels(struct net_device *netdev,
1173                               struct ethtool_channels *ch)
1174 {
1175         struct hnae3_handle *h = hns3_get_handle(netdev);
1176
1177         if (h->ae_algo->ops->get_channels)
1178                 h->ae_algo->ops->get_channels(h, ch);
1179 }
1180
1181 static int hns3_get_coalesce(struct net_device *netdev,
1182                              struct ethtool_coalesce *cmd,
1183                              struct kernel_ethtool_coalesce *kernel_coal,
1184                              struct netlink_ext_ack *extack)
1185 {
1186         struct hns3_nic_priv *priv = netdev_priv(netdev);
1187         struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1188         struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1189         struct hnae3_handle *h = priv->ae_handle;
1190
1191         if (hns3_nic_resetting(netdev))
1192                 return -EBUSY;
1193
1194         cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1195         cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1196
1197         cmd->tx_coalesce_usecs = tx_coal->int_gl;
1198         cmd->rx_coalesce_usecs = rx_coal->int_gl;
1199
1200         cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1201         cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1202
1203         cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1204         cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1205
1206         return 0;
1207 }
1208
1209 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1210                                        struct ethtool_coalesce *cmd)
1211 {
1212         struct hnae3_handle *handle = hns3_get_handle(netdev);
1213         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1214         u32 rx_gl, tx_gl;
1215
1216         if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1217                 netdev_err(netdev,
1218                            "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1219                            ae_dev->dev_specs.max_int_gl);
1220                 return -EINVAL;
1221         }
1222
1223         if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1224                 netdev_err(netdev,
1225                            "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1226                            ae_dev->dev_specs.max_int_gl);
1227                 return -EINVAL;
1228         }
1229
1230         /* device version above V3(include V3), GL uses 1us unit,
1231          * so the round down is not needed.
1232          */
1233         if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1234                 return 0;
1235
1236         rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1237         if (rx_gl != cmd->rx_coalesce_usecs) {
1238                 netdev_info(netdev,
1239                             "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1240                             cmd->rx_coalesce_usecs, rx_gl);
1241         }
1242
1243         tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1244         if (tx_gl != cmd->tx_coalesce_usecs) {
1245                 netdev_info(netdev,
1246                             "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1247                             cmd->tx_coalesce_usecs, tx_gl);
1248         }
1249
1250         return 0;
1251 }
1252
1253 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1254                                        struct ethtool_coalesce *cmd)
1255 {
1256         u32 rl;
1257
1258         if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1259                 netdev_err(netdev,
1260                            "tx_usecs_high must be same as rx_usecs_high.\n");
1261                 return -EINVAL;
1262         }
1263
1264         if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1265                 netdev_err(netdev,
1266                            "Invalid usecs_high value, usecs_high range is 0-%d\n",
1267                            HNS3_INT_RL_MAX);
1268                 return -EINVAL;
1269         }
1270
1271         rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1272         if (rl != cmd->rx_coalesce_usecs_high) {
1273                 netdev_info(netdev,
1274                             "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1275                             cmd->rx_coalesce_usecs_high, rl);
1276         }
1277
1278         return 0;
1279 }
1280
1281 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1282                                         struct ethtool_coalesce *cmd)
1283 {
1284         struct hnae3_handle *handle = hns3_get_handle(netdev);
1285         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1286
1287         if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1288             !ae_dev->dev_specs.int_ql_max) {
1289                 netdev_err(netdev, "coalesced frames is not supported\n");
1290                 return -EOPNOTSUPP;
1291         }
1292
1293         if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1294             cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1295                 netdev_err(netdev,
1296                            "invalid coalesced_frames value, range is 0-%u\n",
1297                            ae_dev->dev_specs.int_ql_max);
1298                 return -ERANGE;
1299         }
1300
1301         return 0;
1302 }
1303
1304 static int hns3_check_coalesce_para(struct net_device *netdev,
1305                                     struct ethtool_coalesce *cmd)
1306 {
1307         int ret;
1308
1309         ret = hns3_check_gl_coalesce_para(netdev, cmd);
1310         if (ret) {
1311                 netdev_err(netdev,
1312                            "Check gl coalesce param fail. ret = %d\n", ret);
1313                 return ret;
1314         }
1315
1316         ret = hns3_check_rl_coalesce_para(netdev, cmd);
1317         if (ret) {
1318                 netdev_err(netdev,
1319                            "Check rl coalesce param fail. ret = %d\n", ret);
1320                 return ret;
1321         }
1322
1323         return hns3_check_ql_coalesce_param(netdev, cmd);
1324 }
1325
1326 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1327                                         struct ethtool_coalesce *cmd,
1328                                         u32 queue)
1329 {
1330         struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1331         struct hns3_nic_priv *priv = netdev_priv(netdev);
1332         struct hnae3_handle *h = priv->ae_handle;
1333         int queue_num = h->kinfo.num_tqps;
1334
1335         tx_vector = priv->ring[queue].tqp_vector;
1336         rx_vector = priv->ring[queue_num + queue].tqp_vector;
1337
1338         tx_vector->tx_group.coal.adapt_enable =
1339                                 cmd->use_adaptive_tx_coalesce;
1340         rx_vector->rx_group.coal.adapt_enable =
1341                                 cmd->use_adaptive_rx_coalesce;
1342
1343         tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1344         rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1345
1346         tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1347         rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1348
1349         hns3_set_vector_coalesce_tx_gl(tx_vector,
1350                                        tx_vector->tx_group.coal.int_gl);
1351         hns3_set_vector_coalesce_rx_gl(rx_vector,
1352                                        rx_vector->rx_group.coal.int_gl);
1353
1354         hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1355         hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1356
1357         if (tx_vector->tx_group.coal.ql_enable)
1358                 hns3_set_vector_coalesce_tx_ql(tx_vector,
1359                                                tx_vector->tx_group.coal.int_ql);
1360         if (rx_vector->rx_group.coal.ql_enable)
1361                 hns3_set_vector_coalesce_rx_ql(rx_vector,
1362                                                rx_vector->rx_group.coal.int_ql);
1363 }
1364
1365 static int hns3_set_coalesce(struct net_device *netdev,
1366                              struct ethtool_coalesce *cmd,
1367                              struct kernel_ethtool_coalesce *kernel_coal,
1368                              struct netlink_ext_ack *extack)
1369 {
1370         struct hnae3_handle *h = hns3_get_handle(netdev);
1371         struct hns3_nic_priv *priv = netdev_priv(netdev);
1372         struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1373         struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1374         u16 queue_num = h->kinfo.num_tqps;
1375         int ret;
1376         int i;
1377
1378         if (hns3_nic_resetting(netdev))
1379                 return -EBUSY;
1380
1381         ret = hns3_check_coalesce_para(netdev, cmd);
1382         if (ret)
1383                 return ret;
1384
1385         h->kinfo.int_rl_setting =
1386                 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1387
1388         tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1389         rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1390
1391         tx_coal->int_gl = cmd->tx_coalesce_usecs;
1392         rx_coal->int_gl = cmd->rx_coalesce_usecs;
1393
1394         tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1395         rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1396
1397         for (i = 0; i < queue_num; i++)
1398                 hns3_set_coalesce_per_queue(netdev, cmd, i);
1399
1400         return 0;
1401 }
1402
1403 static int hns3_get_regs_len(struct net_device *netdev)
1404 {
1405         struct hnae3_handle *h = hns3_get_handle(netdev);
1406
1407         if (!h->ae_algo->ops->get_regs_len)
1408                 return -EOPNOTSUPP;
1409
1410         return h->ae_algo->ops->get_regs_len(h);
1411 }
1412
1413 static void hns3_get_regs(struct net_device *netdev,
1414                           struct ethtool_regs *cmd, void *data)
1415 {
1416         struct hnae3_handle *h = hns3_get_handle(netdev);
1417
1418         if (!h->ae_algo->ops->get_regs)
1419                 return;
1420
1421         h->ae_algo->ops->get_regs(h, &cmd->version, data);
1422 }
1423
1424 static int hns3_set_phys_id(struct net_device *netdev,
1425                             enum ethtool_phys_id_state state)
1426 {
1427         struct hnae3_handle *h = hns3_get_handle(netdev);
1428
1429         if (!h->ae_algo->ops->set_led_id)
1430                 return -EOPNOTSUPP;
1431
1432         return h->ae_algo->ops->set_led_id(h, state);
1433 }
1434
1435 static u32 hns3_get_msglevel(struct net_device *netdev)
1436 {
1437         struct hnae3_handle *h = hns3_get_handle(netdev);
1438
1439         return h->msg_enable;
1440 }
1441
1442 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1443 {
1444         struct hnae3_handle *h = hns3_get_handle(netdev);
1445
1446         h->msg_enable = msg_level;
1447 }
1448
1449 /* Translate local fec value into ethtool value. */
1450 static unsigned int loc_to_eth_fec(u8 loc_fec)
1451 {
1452         u32 eth_fec = 0;
1453
1454         if (loc_fec & BIT(HNAE3_FEC_AUTO))
1455                 eth_fec |= ETHTOOL_FEC_AUTO;
1456         if (loc_fec & BIT(HNAE3_FEC_RS))
1457                 eth_fec |= ETHTOOL_FEC_RS;
1458         if (loc_fec & BIT(HNAE3_FEC_BASER))
1459                 eth_fec |= ETHTOOL_FEC_BASER;
1460
1461         /* if nothing is set, then FEC is off */
1462         if (!eth_fec)
1463                 eth_fec = ETHTOOL_FEC_OFF;
1464
1465         return eth_fec;
1466 }
1467
1468 /* Translate ethtool fec value into local value. */
1469 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1470 {
1471         u32 loc_fec = 0;
1472
1473         if (eth_fec & ETHTOOL_FEC_OFF)
1474                 return loc_fec;
1475
1476         if (eth_fec & ETHTOOL_FEC_AUTO)
1477                 loc_fec |= BIT(HNAE3_FEC_AUTO);
1478         if (eth_fec & ETHTOOL_FEC_RS)
1479                 loc_fec |= BIT(HNAE3_FEC_RS);
1480         if (eth_fec & ETHTOOL_FEC_BASER)
1481                 loc_fec |= BIT(HNAE3_FEC_BASER);
1482
1483         return loc_fec;
1484 }
1485
1486 static int hns3_get_fecparam(struct net_device *netdev,
1487                              struct ethtool_fecparam *fec)
1488 {
1489         struct hnae3_handle *handle = hns3_get_handle(netdev);
1490         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1491         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1492         u8 fec_ability;
1493         u8 fec_mode;
1494
1495         if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1496                 return -EOPNOTSUPP;
1497
1498         if (!ops->get_fec)
1499                 return -EOPNOTSUPP;
1500
1501         ops->get_fec(handle, &fec_ability, &fec_mode);
1502
1503         fec->fec = loc_to_eth_fec(fec_ability);
1504         fec->active_fec = loc_to_eth_fec(fec_mode);
1505
1506         return 0;
1507 }
1508
1509 static int hns3_set_fecparam(struct net_device *netdev,
1510                              struct ethtool_fecparam *fec)
1511 {
1512         struct hnae3_handle *handle = hns3_get_handle(netdev);
1513         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1514         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1515         u32 fec_mode;
1516
1517         if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1518                 return -EOPNOTSUPP;
1519
1520         if (!ops->set_fec)
1521                 return -EOPNOTSUPP;
1522         fec_mode = eth_to_loc_fec(fec->fec);
1523
1524         netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1525
1526         return ops->set_fec(handle, fec_mode);
1527 }
1528
1529 static int hns3_get_module_info(struct net_device *netdev,
1530                                 struct ethtool_modinfo *modinfo)
1531 {
1532 #define HNS3_SFF_8636_V1_3 0x03
1533
1534         struct hnae3_handle *handle = hns3_get_handle(netdev);
1535         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1536         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1537         struct hns3_sfp_type sfp_type;
1538         int ret;
1539
1540         if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1541             !ops->get_module_eeprom)
1542                 return -EOPNOTSUPP;
1543
1544         memset(&sfp_type, 0, sizeof(sfp_type));
1545         ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1546                                      (u8 *)&sfp_type);
1547         if (ret)
1548                 return ret;
1549
1550         switch (sfp_type.type) {
1551         case SFF8024_ID_SFP:
1552                 modinfo->type = ETH_MODULE_SFF_8472;
1553                 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1554                 break;
1555         case SFF8024_ID_QSFP_8438:
1556                 modinfo->type = ETH_MODULE_SFF_8436;
1557                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1558                 break;
1559         case SFF8024_ID_QSFP_8436_8636:
1560                 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1561                         modinfo->type = ETH_MODULE_SFF_8436;
1562                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1563                 } else {
1564                         modinfo->type = ETH_MODULE_SFF_8636;
1565                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1566                 }
1567                 break;
1568         case SFF8024_ID_QSFP28_8636:
1569                 modinfo->type = ETH_MODULE_SFF_8636;
1570                 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1571                 break;
1572         default:
1573                 netdev_err(netdev, "Optical module unknown: %#x\n",
1574                            sfp_type.type);
1575                 return -EINVAL;
1576         }
1577
1578         return 0;
1579 }
1580
1581 static int hns3_get_module_eeprom(struct net_device *netdev,
1582                                   struct ethtool_eeprom *ee, u8 *data)
1583 {
1584         struct hnae3_handle *handle = hns3_get_handle(netdev);
1585         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1586         const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1587
1588         if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1589             !ops->get_module_eeprom)
1590                 return -EOPNOTSUPP;
1591
1592         if (!ee->len)
1593                 return -EINVAL;
1594
1595         memset(data, 0, ee->len);
1596
1597         return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1598 }
1599
1600 static u32 hns3_get_priv_flags(struct net_device *netdev)
1601 {
1602         struct hnae3_handle *handle = hns3_get_handle(netdev);
1603
1604         return handle->priv_flags;
1605 }
1606
1607 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1608 {
1609         u32 i;
1610
1611         for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1612                 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1613                         netdev_err(h->netdev, "%s is unsupported\n",
1614                                    hns3_priv_flags[i].name);
1615                         return -EOPNOTSUPP;
1616                 }
1617
1618         return 0;
1619 }
1620
1621 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1622 {
1623         struct hnae3_handle *handle = hns3_get_handle(netdev);
1624         u32 changed = pflags ^ handle->priv_flags;
1625         int ret;
1626         u32 i;
1627
1628         ret = hns3_check_priv_flags(handle, changed);
1629         if (ret)
1630                 return ret;
1631
1632         for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1633                 if (changed & BIT(i)) {
1634                         bool enable = !(handle->priv_flags & BIT(i));
1635
1636                         if (enable)
1637                                 handle->priv_flags |= BIT(i);
1638                         else
1639                                 handle->priv_flags &= ~BIT(i);
1640                         hns3_priv_flags[i].handler(netdev, enable);
1641                 }
1642         }
1643
1644         return 0;
1645 }
1646
1647 static int hns3_get_tunable(struct net_device *netdev,
1648                             const struct ethtool_tunable *tuna,
1649                             void *data)
1650 {
1651         struct hns3_nic_priv *priv = netdev_priv(netdev);
1652         int ret = 0;
1653
1654         switch (tuna->id) {
1655         case ETHTOOL_TX_COPYBREAK:
1656                 /* all the tx rings have the same tx_copybreak */
1657                 *(u32 *)data = priv->tx_copybreak;
1658                 break;
1659         case ETHTOOL_RX_COPYBREAK:
1660                 *(u32 *)data = priv->rx_copybreak;
1661                 break;
1662         default:
1663                 ret = -EOPNOTSUPP;
1664                 break;
1665         }
1666
1667         return ret;
1668 }
1669
1670 static int hns3_set_tunable(struct net_device *netdev,
1671                             const struct ethtool_tunable *tuna,
1672                             const void *data)
1673 {
1674         struct hns3_nic_priv *priv = netdev_priv(netdev);
1675         struct hnae3_handle *h = priv->ae_handle;
1676         int i, ret = 0;
1677
1678         switch (tuna->id) {
1679         case ETHTOOL_TX_COPYBREAK:
1680                 priv->tx_copybreak = *(u32 *)data;
1681
1682                 for (i = 0; i < h->kinfo.num_tqps; i++)
1683                         priv->ring[i].tx_copybreak = priv->tx_copybreak;
1684
1685                 break;
1686         case ETHTOOL_RX_COPYBREAK:
1687                 priv->rx_copybreak = *(u32 *)data;
1688
1689                 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1690                         priv->ring[i].rx_copybreak = priv->rx_copybreak;
1691
1692                 break;
1693         default:
1694                 ret = -EOPNOTSUPP;
1695                 break;
1696         }
1697
1698         return ret;
1699 }
1700
1701 #define HNS3_ETHTOOL_COALESCE   (ETHTOOL_COALESCE_USECS |               \
1702                                  ETHTOOL_COALESCE_USE_ADAPTIVE |        \
1703                                  ETHTOOL_COALESCE_RX_USECS_HIGH |       \
1704                                  ETHTOOL_COALESCE_TX_USECS_HIGH |       \
1705                                  ETHTOOL_COALESCE_MAX_FRAMES)
1706
1707 static int hns3_get_ts_info(struct net_device *netdev,
1708                             struct ethtool_ts_info *info)
1709 {
1710         struct hnae3_handle *handle = hns3_get_handle(netdev);
1711
1712         if (handle->ae_algo->ops->get_ts_info)
1713                 return handle->ae_algo->ops->get_ts_info(handle, info);
1714
1715         return ethtool_op_get_ts_info(netdev, info);
1716 }
1717
1718 static const struct hns3_ethtool_link_ext_state_mapping
1719 hns3_link_ext_state_map[] = {
1720         {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1721                 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1722         {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1723                 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1724
1725         {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1726                 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1727         {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1728                 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1729         {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1730                 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1731
1732         {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1733                 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1734         {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1735                 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1736         {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1737                 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1738
1739         {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1740                 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1741         {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1742                 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1743         {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1744                 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1745
1746         {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1747         {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1748                 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1749
1750         {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1751 };
1752
1753 static int hns3_get_link_ext_state(struct net_device *netdev,
1754                                    struct ethtool_link_ext_state_info *info)
1755 {
1756         const struct hns3_ethtool_link_ext_state_mapping *map;
1757         struct hnae3_handle *h = hns3_get_handle(netdev);
1758         u32 status_code, i;
1759         int ret;
1760
1761         if (netif_carrier_ok(netdev))
1762                 return -ENODATA;
1763
1764         if (!h->ae_algo->ops->get_link_diagnosis_info)
1765                 return -EOPNOTSUPP;
1766
1767         ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1768         if (ret)
1769                 return ret;
1770
1771         for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
1772                 map = &hns3_link_ext_state_map[i];
1773                 if (map->status_code == status_code) {
1774                         info->link_ext_state = map->link_ext_state;
1775                         info->__link_ext_substate = map->link_ext_substate;
1776                         return 0;
1777                 }
1778         }
1779
1780         return -ENODATA;
1781 }
1782
1783 static const struct ethtool_ops hns3vf_ethtool_ops = {
1784         .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1785         .get_drvinfo = hns3_get_drvinfo,
1786         .get_ringparam = hns3_get_ringparam,
1787         .set_ringparam = hns3_set_ringparam,
1788         .get_strings = hns3_get_strings,
1789         .get_ethtool_stats = hns3_get_stats,
1790         .get_sset_count = hns3_get_sset_count,
1791         .get_rxnfc = hns3_get_rxnfc,
1792         .set_rxnfc = hns3_set_rxnfc,
1793         .get_rxfh_key_size = hns3_get_rss_key_size,
1794         .get_rxfh_indir_size = hns3_get_rss_indir_size,
1795         .get_rxfh = hns3_get_rss,
1796         .set_rxfh = hns3_set_rss,
1797         .get_link_ksettings = hns3_get_link_ksettings,
1798         .get_channels = hns3_get_channels,
1799         .set_channels = hns3_set_channels,
1800         .get_coalesce = hns3_get_coalesce,
1801         .set_coalesce = hns3_set_coalesce,
1802         .get_regs_len = hns3_get_regs_len,
1803         .get_regs = hns3_get_regs,
1804         .get_link = hns3_get_link,
1805         .get_msglevel = hns3_get_msglevel,
1806         .set_msglevel = hns3_set_msglevel,
1807         .get_priv_flags = hns3_get_priv_flags,
1808         .set_priv_flags = hns3_set_priv_flags,
1809         .get_tunable = hns3_get_tunable,
1810         .set_tunable = hns3_set_tunable,
1811         .reset = hns3_set_reset,
1812 };
1813
1814 static const struct ethtool_ops hns3_ethtool_ops = {
1815         .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1816         .self_test = hns3_self_test,
1817         .get_drvinfo = hns3_get_drvinfo,
1818         .get_link = hns3_get_link,
1819         .get_ringparam = hns3_get_ringparam,
1820         .set_ringparam = hns3_set_ringparam,
1821         .get_pauseparam = hns3_get_pauseparam,
1822         .set_pauseparam = hns3_set_pauseparam,
1823         .get_strings = hns3_get_strings,
1824         .get_ethtool_stats = hns3_get_stats,
1825         .get_sset_count = hns3_get_sset_count,
1826         .get_rxnfc = hns3_get_rxnfc,
1827         .set_rxnfc = hns3_set_rxnfc,
1828         .get_rxfh_key_size = hns3_get_rss_key_size,
1829         .get_rxfh_indir_size = hns3_get_rss_indir_size,
1830         .get_rxfh = hns3_get_rss,
1831         .set_rxfh = hns3_set_rss,
1832         .get_link_ksettings = hns3_get_link_ksettings,
1833         .set_link_ksettings = hns3_set_link_ksettings,
1834         .nway_reset = hns3_nway_reset,
1835         .get_channels = hns3_get_channels,
1836         .set_channels = hns3_set_channels,
1837         .get_coalesce = hns3_get_coalesce,
1838         .set_coalesce = hns3_set_coalesce,
1839         .get_regs_len = hns3_get_regs_len,
1840         .get_regs = hns3_get_regs,
1841         .set_phys_id = hns3_set_phys_id,
1842         .get_msglevel = hns3_get_msglevel,
1843         .set_msglevel = hns3_set_msglevel,
1844         .get_fecparam = hns3_get_fecparam,
1845         .set_fecparam = hns3_set_fecparam,
1846         .get_module_info = hns3_get_module_info,
1847         .get_module_eeprom = hns3_get_module_eeprom,
1848         .get_priv_flags = hns3_get_priv_flags,
1849         .set_priv_flags = hns3_set_priv_flags,
1850         .get_ts_info = hns3_get_ts_info,
1851         .get_tunable = hns3_get_tunable,
1852         .set_tunable = hns3_set_tunable,
1853         .reset = hns3_set_reset,
1854         .get_link_ext_state = hns3_get_link_ext_state,
1855 };
1856
1857 void hns3_ethtool_set_ops(struct net_device *netdev)
1858 {
1859         struct hnae3_handle *h = hns3_get_handle(netdev);
1860
1861         if (h->flags & HNAE3_SUPPORT_VF)
1862                 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1863         else
1864                 netdev->ethtool_ops = &hns3_ethtool_ops;
1865 }