OSDN Git Service

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