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