]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c
14797b99ae5bb5432a1a8852c76f632509321152
[linux.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
8 #include "hns3_enet.h"
9
10 struct hns3_stats {
11         char stats_string[ETH_GSTRING_LEN];
12         int stats_offset;
13 };
14
15 /* tqp related stats */
16 #define HNS3_TQP_STAT(_string, _member) {                       \
17         .stats_string = _string,                                \
18         .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
19                         offsetof(struct ring_stats, _member),   \
20 }
21
22 static const struct hns3_stats hns3_txq_stats[] = {
23         /* Tx per-queue statistics */
24         HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
25         HNS3_TQP_STAT("tx_dropped", sw_err_cnt),
26         HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
27         HNS3_TQP_STAT("packets", tx_pkts),
28         HNS3_TQP_STAT("bytes", tx_bytes),
29         HNS3_TQP_STAT("errors", tx_err_cnt),
30         HNS3_TQP_STAT("tx_wake", restart_queue),
31         HNS3_TQP_STAT("tx_busy", tx_busy),
32 };
33
34 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
35
36 static const struct hns3_stats hns3_rxq_stats[] = {
37         /* Rx per-queue statistics */
38         HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
39         HNS3_TQP_STAT("rx_dropped", sw_err_cnt),
40         HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
41         HNS3_TQP_STAT("packets", rx_pkts),
42         HNS3_TQP_STAT("bytes", rx_bytes),
43         HNS3_TQP_STAT("errors", rx_err_cnt),
44         HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
45         HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
46         HNS3_TQP_STAT("non_vld_descs", non_vld_descs),
47         HNS3_TQP_STAT("err_bd_num", err_bd_num),
48         HNS3_TQP_STAT("l2_err", l2_err),
49         HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
50 };
51
52 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
53
54 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
55
56 #define HNS3_SELF_TEST_TYPE_NUM         2
57 #define HNS3_NIC_LB_TEST_PKT_NUM        1
58 #define HNS3_NIC_LB_TEST_RING_ID        0
59 #define HNS3_NIC_LB_TEST_PACKET_SIZE    128
60
61 /* Nic loopback test err  */
62 #define HNS3_NIC_LB_TEST_NO_MEM_ERR     1
63 #define HNS3_NIC_LB_TEST_TX_CNT_ERR     2
64 #define HNS3_NIC_LB_TEST_RX_CNT_ERR     3
65
66 struct hns3_link_mode_mapping {
67         u32 hns3_link_mode;
68         u32 ethtool_link_mode;
69 };
70
71 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
72 {
73         struct hnae3_handle *h = hns3_get_handle(ndev);
74         int ret;
75
76         if (!h->ae_algo->ops->set_loopback ||
77             !h->ae_algo->ops->set_promisc_mode)
78                 return -EOPNOTSUPP;
79
80         switch (loop) {
81         case HNAE3_MAC_INTER_LOOP_SERDES:
82         case HNAE3_MAC_INTER_LOOP_MAC:
83                 ret = h->ae_algo->ops->set_loopback(h, loop, en);
84                 break;
85         default:
86                 ret = -ENOTSUPP;
87                 break;
88         }
89
90         if (ret)
91                 return ret;
92
93         h->ae_algo->ops->set_promisc_mode(h, en, en);
94
95         return ret;
96 }
97
98 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
99 {
100         struct hnae3_handle *h = hns3_get_handle(ndev);
101         int ret;
102
103         ret = hns3_nic_reset_all_ring(h);
104         if (ret)
105                 return ret;
106
107         ret = hns3_lp_setup(ndev, loop_mode, true);
108         usleep_range(10000, 20000);
109
110         return 0;
111 }
112
113 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
114 {
115         int ret;
116
117         ret = hns3_lp_setup(ndev, loop_mode, false);
118         if (ret) {
119                 netdev_err(ndev, "lb_setup return error: %d\n", ret);
120                 return ret;
121         }
122
123         usleep_range(10000, 20000);
124
125         return 0;
126 }
127
128 static void hns3_lp_setup_skb(struct sk_buff *skb)
129 {
130         struct net_device *ndev = skb->dev;
131         unsigned char *packet;
132         struct ethhdr *ethh;
133         unsigned int i;
134
135         skb_reserve(skb, NET_IP_ALIGN);
136         ethh = skb_put(skb, sizeof(struct ethhdr));
137         packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
138
139         memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
140         eth_zero_addr(ethh->h_source);
141         ethh->h_proto = htons(ETH_P_ARP);
142         skb_reset_mac_header(skb);
143
144         for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
145                 packet[i] = (unsigned char)(i & 0xff);
146 }
147
148 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
149                                    struct sk_buff *skb)
150 {
151         struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
152         unsigned char *packet = skb->data;
153         u32 i;
154
155         for (i = 0; i < skb->len; i++)
156                 if (packet[i] != (unsigned char)(i & 0xff))
157                         break;
158
159         /* The packet is correctly received */
160         if (i == skb->len)
161                 tqp_vector->rx_group.total_packets++;
162         else
163                 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
164                                skb->data, skb->len, true);
165
166         dev_kfree_skb_any(skb);
167 }
168
169 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
170 {
171         struct hnae3_handle *h = priv->ae_handle;
172         struct hnae3_knic_private_info *kinfo;
173         u32 i, rcv_good_pkt_total = 0;
174
175         kinfo = &h->kinfo;
176         for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
177                 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
178                 struct hns3_enet_ring_group *rx_group;
179                 u64 pre_rx_pkt;
180
181                 rx_group = &ring->tqp_vector->rx_group;
182                 pre_rx_pkt = rx_group->total_packets;
183
184                 preempt_disable();
185                 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
186                 preempt_enable();
187
188                 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
189                 rx_group->total_packets = pre_rx_pkt;
190         }
191         return rcv_good_pkt_total;
192 }
193
194 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
195                                   u32 end_ringid, u32 budget)
196 {
197         u32 i;
198
199         for (i = start_ringid; i <= end_ringid; i++) {
200                 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
201
202                 hns3_clean_tx_ring(ring, budget);
203         }
204 }
205
206 /**
207  * hns3_lp_run_test -  run loopback test
208  * @ndev: net device
209  * @mode: loopback type
210  */
211 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
212 {
213         struct hns3_nic_priv *priv = netdev_priv(ndev);
214         struct sk_buff *skb;
215         u32 i, good_cnt;
216         int ret_val = 0;
217
218         skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
219                         GFP_KERNEL);
220         if (!skb)
221                 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
222
223         skb->dev = ndev;
224         hns3_lp_setup_skb(skb);
225         skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
226
227         good_cnt = 0;
228         for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
229                 netdev_tx_t tx_ret;
230
231                 skb_get(skb);
232                 tx_ret = hns3_nic_net_xmit(skb, ndev);
233                 if (tx_ret == NETDEV_TX_OK)
234                         good_cnt++;
235                 else
236                         netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
237                                    tx_ret);
238         }
239         if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
240                 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
241                 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
242                            mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
243                 goto out;
244         }
245
246         /* Allow 200 milliseconds for packets to go from Tx to Rx */
247         msleep(200);
248
249         good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
250         if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
251                 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
252                 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
253                            mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
254         }
255
256 out:
257         hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
258                               HNS3_NIC_LB_TEST_RING_ID,
259                               HNS3_NIC_LB_TEST_PKT_NUM);
260
261         kfree_skb(skb);
262         return ret_val;
263 }
264
265 /**
266  * hns3_nic_self_test - self test
267  * @ndev: net device
268  * @eth_test: test cmd
269  * @data: test result
270  */
271 static void hns3_self_test(struct net_device *ndev,
272                            struct ethtool_test *eth_test, u64 *data)
273 {
274         struct hns3_nic_priv *priv = netdev_priv(ndev);
275         struct hnae3_handle *h = priv->ae_handle;
276         int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
277         bool if_running = netif_running(ndev);
278 #if IS_ENABLED(CONFIG_VLAN_8021Q)
279         bool dis_vlan_filter;
280 #endif
281         int test_index = 0;
282         u32 i;
283
284         /* Only do offline selftest, or pass by default */
285         if (eth_test->flags != ETH_TEST_FL_OFFLINE)
286                 return;
287
288         st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
289         st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
290                         h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
291
292         st_param[HNAE3_MAC_INTER_LOOP_SERDES][0] = HNAE3_MAC_INTER_LOOP_SERDES;
293         st_param[HNAE3_MAC_INTER_LOOP_SERDES][1] =
294                         h->flags & HNAE3_SUPPORT_SERDES_LOOPBACK;
295
296         if (if_running)
297                 dev_close(ndev);
298
299 #if IS_ENABLED(CONFIG_VLAN_8021Q)
300         /* Disable the vlan filter for selftest does not support it */
301         dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
302                                 h->ae_algo->ops->enable_vlan_filter;
303         if (dis_vlan_filter)
304                 h->ae_algo->ops->enable_vlan_filter(h, false);
305 #endif
306
307         set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
308
309         for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
310                 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
311
312                 if (!st_param[i][1])
313                         continue;
314
315                 data[test_index] = hns3_lp_up(ndev, loop_type);
316                 if (!data[test_index]) {
317                         data[test_index] = hns3_lp_run_test(ndev, loop_type);
318                         hns3_lp_down(ndev, loop_type);
319                 }
320
321                 if (data[test_index])
322                         eth_test->flags |= ETH_TEST_FL_FAILED;
323
324                 test_index++;
325         }
326
327         clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
328
329 #if IS_ENABLED(CONFIG_VLAN_8021Q)
330         if (dis_vlan_filter)
331                 h->ae_algo->ops->enable_vlan_filter(h, true);
332 #endif
333
334         if (if_running)
335                 dev_open(ndev);
336 }
337
338 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
339 {
340         struct hnae3_handle *h = hns3_get_handle(netdev);
341         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
342
343         if (!ops->get_sset_count)
344                 return -EOPNOTSUPP;
345
346         switch (stringset) {
347         case ETH_SS_STATS:
348                 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
349                         ops->get_sset_count(h, stringset));
350
351         case ETH_SS_TEST:
352                 return ops->get_sset_count(h, stringset);
353         }
354
355         return 0;
356 }
357
358 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
359                 u32 stat_count, u32 num_tqps, const char *prefix)
360 {
361 #define MAX_PREFIX_SIZE (6 + 4)
362         u32 size_left;
363         u32 i, j;
364         u32 n1;
365
366         for (i = 0; i < num_tqps; i++) {
367                 for (j = 0; j < stat_count; j++) {
368                         data[ETH_GSTRING_LEN - 1] = '\0';
369
370                         /* first, prepend the prefix string */
371                         n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
372                                       prefix, i);
373                         n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
374                         size_left = (ETH_GSTRING_LEN - 1) - n1;
375
376                         /* now, concatenate the stats string to it */
377                         strncat(data, stats[j].stats_string, size_left);
378                         data += ETH_GSTRING_LEN;
379                 }
380         }
381
382         return data;
383 }
384
385 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
386 {
387         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
388         const char tx_prefix[] = "txq";
389         const char rx_prefix[] = "rxq";
390
391         /* get strings for Tx */
392         data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
393                                    kinfo->num_tqps, tx_prefix);
394
395         /* get strings for Rx */
396         data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
397                                    kinfo->num_tqps, rx_prefix);
398
399         return data;
400 }
401
402 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
403 {
404         struct hnae3_handle *h = hns3_get_handle(netdev);
405         const struct hnae3_ae_ops *ops = h->ae_algo->ops;
406         char *buff = (char *)data;
407
408         if (!ops->get_strings)
409                 return;
410
411         switch (stringset) {
412         case ETH_SS_STATS:
413                 buff = hns3_get_strings_tqps(h, buff);
414                 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
415                 break;
416         case ETH_SS_TEST:
417                 ops->get_strings(h, stringset, data);
418                 break;
419         }
420 }
421
422 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
423 {
424         struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
425         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
426         struct hns3_enet_ring *ring;
427         u8 *stat;
428         int i, j;
429
430         /* get stats for Tx */
431         for (i = 0; i < kinfo->num_tqps; i++) {
432                 ring = nic_priv->ring_data[i].ring;
433                 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
434                         stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
435                         *data++ = *(u64 *)stat;
436                 }
437         }
438
439         /* get stats for Rx */
440         for (i = 0; i < kinfo->num_tqps; i++) {
441                 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
442                 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
443                         stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
444                         *data++ = *(u64 *)stat;
445                 }
446         }
447
448         return data;
449 }
450
451 /* hns3_get_stats - get detail statistics.
452  * @netdev: net device
453  * @stats: statistics info.
454  * @data: statistics data.
455  */
456 static void hns3_get_stats(struct net_device *netdev,
457                            struct ethtool_stats *stats, u64 *data)
458 {
459         struct hnae3_handle *h = hns3_get_handle(netdev);
460         u64 *p = data;
461
462         if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
463                 netdev_err(netdev, "could not get any statistics\n");
464                 return;
465         }
466
467         h->ae_algo->ops->update_stats(h, &netdev->stats);
468
469         /* get per-queue stats */
470         p = hns3_get_stats_tqps(h, p);
471
472         /* get MAC & other misc hardware stats */
473         h->ae_algo->ops->get_stats(h, p);
474 }
475
476 static void hns3_get_drvinfo(struct net_device *netdev,
477                              struct ethtool_drvinfo *drvinfo)
478 {
479         struct hns3_nic_priv *priv = netdev_priv(netdev);
480         struct hnae3_handle *h = priv->ae_handle;
481
482         strncpy(drvinfo->version, hns3_driver_version,
483                 sizeof(drvinfo->version));
484         drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
485
486         strncpy(drvinfo->driver, h->pdev->driver->name,
487                 sizeof(drvinfo->driver));
488         drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
489
490         strncpy(drvinfo->bus_info, pci_name(h->pdev),
491                 sizeof(drvinfo->bus_info));
492         drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
493
494         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
495                  priv->ae_handle->ae_algo->ops->get_fw_version(h));
496 }
497
498 static u32 hns3_get_link(struct net_device *netdev)
499 {
500         struct hnae3_handle *h = hns3_get_handle(netdev);
501
502         if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
503                 return h->ae_algo->ops->get_status(h);
504         else
505                 return 0;
506 }
507
508 static void hns3_get_ringparam(struct net_device *netdev,
509                                struct ethtool_ringparam *param)
510 {
511         struct hns3_nic_priv *priv = netdev_priv(netdev);
512         struct hnae3_handle *h = priv->ae_handle;
513         int queue_num = h->kinfo.num_tqps;
514
515         param->tx_max_pending = HNS3_RING_MAX_PENDING;
516         param->rx_max_pending = HNS3_RING_MAX_PENDING;
517
518         param->tx_pending = priv->ring_data[0].ring->desc_num;
519         param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
520 }
521
522 static void hns3_get_pauseparam(struct net_device *netdev,
523                                 struct ethtool_pauseparam *param)
524 {
525         struct hnae3_handle *h = hns3_get_handle(netdev);
526
527         if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
528                 h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
529                         &param->rx_pause, &param->tx_pause);
530 }
531
532 static int hns3_set_pauseparam(struct net_device *netdev,
533                                struct ethtool_pauseparam *param)
534 {
535         struct hnae3_handle *h = hns3_get_handle(netdev);
536
537         if (h->ae_algo->ops->set_pauseparam)
538                 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
539                                                        param->rx_pause,
540                                                        param->tx_pause);
541         return -EOPNOTSUPP;
542 }
543
544 static int hns3_get_link_ksettings(struct net_device *netdev,
545                                    struct ethtool_link_ksettings *cmd)
546 {
547         struct hnae3_handle *h = hns3_get_handle(netdev);
548         u32 flowctrl_adv = 0;
549         u8 link_stat;
550
551         if (!h->ae_algo || !h->ae_algo->ops)
552                 return -EOPNOTSUPP;
553
554         /* 1.auto_neg & speed & duplex from cmd */
555         if (netdev->phydev) {
556                 phy_ethtool_ksettings_get(netdev->phydev, cmd);
557
558                 return 0;
559         }
560
561         if (h->ae_algo->ops->get_ksettings_an_result)
562                 h->ae_algo->ops->get_ksettings_an_result(h,
563                                                          &cmd->base.autoneg,
564                                                          &cmd->base.speed,
565                                                          &cmd->base.duplex);
566         else
567                 return -EOPNOTSUPP;
568
569         link_stat = hns3_get_link(netdev);
570         if (!link_stat) {
571                 cmd->base.speed = SPEED_UNKNOWN;
572                 cmd->base.duplex = DUPLEX_UNKNOWN;
573         }
574
575         /* 2.get link mode and port type*/
576         if (h->ae_algo->ops->get_link_mode)
577                 h->ae_algo->ops->get_link_mode(h,
578                                                cmd->link_modes.supported,
579                                                cmd->link_modes.advertising);
580
581         cmd->base.port = PORT_NONE;
582         if (h->ae_algo->ops->get_port_type)
583                 h->ae_algo->ops->get_port_type(h,
584                                                &cmd->base.port);
585
586         /* 3.mdix_ctrl&mdix get from phy reg */
587         if (h->ae_algo->ops->get_mdix_mode)
588                 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
589                                                &cmd->base.eth_tp_mdix);
590         /* 4.mdio_support */
591         cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
592
593         /* 5.get flow control setttings */
594         if (h->ae_algo->ops->get_flowctrl_adv)
595                 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
596
597         if (flowctrl_adv & ADVERTISED_Pause)
598                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
599                                                      Pause);
600
601         if (flowctrl_adv & ADVERTISED_Asym_Pause)
602                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
603                                                      Asym_Pause);
604
605         return 0;
606 }
607
608 static int hns3_set_link_ksettings(struct net_device *netdev,
609                                    const struct ethtool_link_ksettings *cmd)
610 {
611         /* Only support ksettings_set for netdev with phy attached for now */
612         if (netdev->phydev)
613                 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
614
615         return -EOPNOTSUPP;
616 }
617
618 static u32 hns3_get_rss_key_size(struct net_device *netdev)
619 {
620         struct hnae3_handle *h = hns3_get_handle(netdev);
621
622         if (!h->ae_algo || !h->ae_algo->ops ||
623             !h->ae_algo->ops->get_rss_key_size)
624                 return 0;
625
626         return h->ae_algo->ops->get_rss_key_size(h);
627 }
628
629 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
630 {
631         struct hnae3_handle *h = hns3_get_handle(netdev);
632
633         if (!h->ae_algo || !h->ae_algo->ops ||
634             !h->ae_algo->ops->get_rss_indir_size)
635                 return 0;
636
637         return h->ae_algo->ops->get_rss_indir_size(h);
638 }
639
640 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
641                         u8 *hfunc)
642 {
643         struct hnae3_handle *h = hns3_get_handle(netdev);
644
645         if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
646                 return -EOPNOTSUPP;
647
648         return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
649 }
650
651 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
652                         const u8 *key, const u8 hfunc)
653 {
654         struct hnae3_handle *h = hns3_get_handle(netdev);
655
656         if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
657                 return -EOPNOTSUPP;
658
659         /* currently we only support Toeplitz hash */
660         if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
661                 netdev_err(netdev,
662                            "hash func not supported (only Toeplitz hash)\n");
663                 return -EOPNOTSUPP;
664         }
665         if (!indir) {
666                 netdev_err(netdev,
667                            "set rss failed for indir is empty\n");
668                 return -EOPNOTSUPP;
669         }
670
671         return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
672 }
673
674 static int hns3_get_rxnfc(struct net_device *netdev,
675                           struct ethtool_rxnfc *cmd,
676                           u32 *rule_locs)
677 {
678         struct hnae3_handle *h = hns3_get_handle(netdev);
679
680         if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
681                 return -EOPNOTSUPP;
682
683         switch (cmd->cmd) {
684         case ETHTOOL_GRXRINGS:
685                 cmd->data = h->kinfo.rss_size;
686                 break;
687         case ETHTOOL_GRXFH:
688                 return h->ae_algo->ops->get_rss_tuple(h, cmd);
689         default:
690                 return -EOPNOTSUPP;
691         }
692
693         return 0;
694 }
695
696 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
697                                        u32 new_desc_num)
698 {
699         struct hnae3_handle *h = priv->ae_handle;
700         int i;
701
702         h->kinfo.num_desc = new_desc_num;
703
704         for (i = 0; i < h->kinfo.num_tqps * 2; i++)
705                 priv->ring_data[i].ring->desc_num = new_desc_num;
706
707         return hns3_init_all_ring(priv);
708 }
709
710 static int hns3_set_ringparam(struct net_device *ndev,
711                               struct ethtool_ringparam *param)
712 {
713         struct hns3_nic_priv *priv = netdev_priv(ndev);
714         struct hnae3_handle *h = priv->ae_handle;
715         bool if_running = netif_running(ndev);
716         u32 old_desc_num, new_desc_num;
717         int ret;
718
719         if (param->rx_mini_pending || param->rx_jumbo_pending)
720                 return -EINVAL;
721
722         if (param->tx_pending != param->rx_pending) {
723                 netdev_err(ndev,
724                            "Descriptors of tx and rx must be equal");
725                 return -EINVAL;
726         }
727
728         if (param->tx_pending > HNS3_RING_MAX_PENDING ||
729             param->tx_pending < HNS3_RING_MIN_PENDING) {
730                 netdev_err(ndev,
731                            "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
732                            param->tx_pending, HNS3_RING_MIN_PENDING,
733                            HNS3_RING_MAX_PENDING);
734                 return -EINVAL;
735         }
736
737         new_desc_num = param->tx_pending;
738
739         /* Hardware requires that its descriptors must be multiple of eight */
740         new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
741         old_desc_num = h->kinfo.num_desc;
742         if (old_desc_num == new_desc_num)
743                 return 0;
744
745         netdev_info(ndev,
746                     "Changing descriptor count from %d to %d.\n",
747                     old_desc_num, new_desc_num);
748
749         if (if_running)
750                 dev_close(ndev);
751
752         ret = hns3_uninit_all_ring(priv);
753         if (ret)
754                 return ret;
755
756         ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
757         if (ret) {
758                 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
759                 if (ret) {
760                         netdev_err(ndev,
761                                    "Revert to old bd num fail, ret=%d.\n", ret);
762                         return ret;
763                 }
764         }
765
766         if (if_running)
767                 ret = dev_open(ndev);
768
769         return ret;
770 }
771
772 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
773 {
774         struct hnae3_handle *h = hns3_get_handle(netdev);
775
776         if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
777                 return -EOPNOTSUPP;
778
779         switch (cmd->cmd) {
780         case ETHTOOL_SRXFH:
781                 return h->ae_algo->ops->set_rss_tuple(h, cmd);
782         default:
783                 return -EOPNOTSUPP;
784         }
785 }
786
787 static int hns3_nway_reset(struct net_device *netdev)
788 {
789         struct phy_device *phy = netdev->phydev;
790
791         if (!netif_running(netdev))
792                 return 0;
793
794         /* Only support nway_reset for netdev with phy attached for now */
795         if (!phy)
796                 return -EOPNOTSUPP;
797
798         if (phy->autoneg != AUTONEG_ENABLE)
799                 return -EINVAL;
800
801         return genphy_restart_aneg(phy);
802 }
803
804 static void hns3_get_channels(struct net_device *netdev,
805                               struct ethtool_channels *ch)
806 {
807         struct hnae3_handle *h = hns3_get_handle(netdev);
808
809         if (h->ae_algo->ops->get_channels)
810                 h->ae_algo->ops->get_channels(h, ch);
811 }
812
813 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
814                                        struct ethtool_coalesce *cmd)
815 {
816         struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
817         struct hns3_nic_priv *priv = netdev_priv(netdev);
818         struct hnae3_handle *h = priv->ae_handle;
819         u16 queue_num = h->kinfo.num_tqps;
820
821         if (queue >= queue_num) {
822                 netdev_err(netdev,
823                            "Invalid queue value %d! Queue max id=%d\n",
824                            queue, queue_num - 1);
825                 return -EINVAL;
826         }
827
828         tx_vector = priv->ring_data[queue].ring->tqp_vector;
829         rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
830
831         cmd->use_adaptive_tx_coalesce =
832                         tx_vector->tx_group.coal.gl_adapt_enable;
833         cmd->use_adaptive_rx_coalesce =
834                         rx_vector->rx_group.coal.gl_adapt_enable;
835
836         cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
837         cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
838
839         cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
840         cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
841
842         return 0;
843 }
844
845 static int hns3_get_coalesce(struct net_device *netdev,
846                              struct ethtool_coalesce *cmd)
847 {
848         return hns3_get_coalesce_per_queue(netdev, 0, cmd);
849 }
850
851 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
852                                        struct ethtool_coalesce *cmd)
853 {
854         u32 rx_gl, tx_gl;
855
856         if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
857                 netdev_err(netdev,
858                            "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
859                            HNS3_INT_GL_MAX);
860                 return -EINVAL;
861         }
862
863         if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
864                 netdev_err(netdev,
865                            "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
866                            HNS3_INT_GL_MAX);
867                 return -EINVAL;
868         }
869
870         rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
871         if (rx_gl != cmd->rx_coalesce_usecs) {
872                 netdev_info(netdev,
873                             "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
874                             cmd->rx_coalesce_usecs, rx_gl);
875         }
876
877         tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
878         if (tx_gl != cmd->tx_coalesce_usecs) {
879                 netdev_info(netdev,
880                             "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
881                             cmd->tx_coalesce_usecs, tx_gl);
882         }
883
884         return 0;
885 }
886
887 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
888                                        struct ethtool_coalesce *cmd)
889 {
890         u32 rl;
891
892         if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
893                 netdev_err(netdev,
894                            "tx_usecs_high must be same as rx_usecs_high.\n");
895                 return -EINVAL;
896         }
897
898         if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
899                 netdev_err(netdev,
900                            "Invalid usecs_high value, usecs_high range is 0-%d\n",
901                            HNS3_INT_RL_MAX);
902                 return -EINVAL;
903         }
904
905         rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
906         if (rl != cmd->rx_coalesce_usecs_high) {
907                 netdev_info(netdev,
908                             "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
909                             cmd->rx_coalesce_usecs_high, rl);
910         }
911
912         return 0;
913 }
914
915 static int hns3_check_coalesce_para(struct net_device *netdev,
916                                     struct ethtool_coalesce *cmd)
917 {
918         int ret;
919
920         ret = hns3_check_gl_coalesce_para(netdev, cmd);
921         if (ret) {
922                 netdev_err(netdev,
923                            "Check gl coalesce param fail. ret = %d\n", ret);
924                 return ret;
925         }
926
927         ret = hns3_check_rl_coalesce_para(netdev, cmd);
928         if (ret) {
929                 netdev_err(netdev,
930                            "Check rl coalesce param fail. ret = %d\n", ret);
931                 return ret;
932         }
933
934         if (cmd->use_adaptive_tx_coalesce == 1 ||
935             cmd->use_adaptive_rx_coalesce == 1) {
936                 netdev_info(netdev,
937                             "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
938                             cmd->use_adaptive_tx_coalesce,
939                             cmd->use_adaptive_rx_coalesce);
940         }
941
942         return 0;
943 }
944
945 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
946                                         struct ethtool_coalesce *cmd,
947                                         u32 queue)
948 {
949         struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
950         struct hns3_nic_priv *priv = netdev_priv(netdev);
951         struct hnae3_handle *h = priv->ae_handle;
952         int queue_num = h->kinfo.num_tqps;
953
954         tx_vector = priv->ring_data[queue].ring->tqp_vector;
955         rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
956
957         tx_vector->tx_group.coal.gl_adapt_enable =
958                                 cmd->use_adaptive_tx_coalesce;
959         rx_vector->rx_group.coal.gl_adapt_enable =
960                                 cmd->use_adaptive_rx_coalesce;
961
962         tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
963         rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
964
965         hns3_set_vector_coalesce_tx_gl(tx_vector,
966                                        tx_vector->tx_group.coal.int_gl);
967         hns3_set_vector_coalesce_rx_gl(rx_vector,
968                                        rx_vector->rx_group.coal.int_gl);
969
970         hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
971         hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
972 }
973
974 static int hns3_set_coalesce(struct net_device *netdev,
975                              struct ethtool_coalesce *cmd)
976 {
977         struct hnae3_handle *h = hns3_get_handle(netdev);
978         u16 queue_num = h->kinfo.num_tqps;
979         int ret;
980         int i;
981
982         ret = hns3_check_coalesce_para(netdev, cmd);
983         if (ret)
984                 return ret;
985
986         h->kinfo.int_rl_setting =
987                 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
988
989         for (i = 0; i < queue_num; i++)
990                 hns3_set_coalesce_per_queue(netdev, cmd, i);
991
992         return 0;
993 }
994
995 static int hns3_get_regs_len(struct net_device *netdev)
996 {
997         struct hnae3_handle *h = hns3_get_handle(netdev);
998
999         if (!h->ae_algo->ops->get_regs_len)
1000                 return -EOPNOTSUPP;
1001
1002         return h->ae_algo->ops->get_regs_len(h);
1003 }
1004
1005 static void hns3_get_regs(struct net_device *netdev,
1006                           struct ethtool_regs *cmd, void *data)
1007 {
1008         struct hnae3_handle *h = hns3_get_handle(netdev);
1009
1010         if (!h->ae_algo->ops->get_regs)
1011                 return;
1012
1013         h->ae_algo->ops->get_regs(h, &cmd->version, data);
1014 }
1015
1016 static int hns3_set_phys_id(struct net_device *netdev,
1017                             enum ethtool_phys_id_state state)
1018 {
1019         struct hnae3_handle *h = hns3_get_handle(netdev);
1020
1021         if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1022                 return -EOPNOTSUPP;
1023
1024         return h->ae_algo->ops->set_led_id(h, state);
1025 }
1026
1027 static const struct ethtool_ops hns3vf_ethtool_ops = {
1028         .get_drvinfo = hns3_get_drvinfo,
1029         .get_ringparam = hns3_get_ringparam,
1030         .set_ringparam = hns3_set_ringparam,
1031         .get_strings = hns3_get_strings,
1032         .get_ethtool_stats = hns3_get_stats,
1033         .get_sset_count = hns3_get_sset_count,
1034         .get_rxnfc = hns3_get_rxnfc,
1035         .get_rxfh_key_size = hns3_get_rss_key_size,
1036         .get_rxfh_indir_size = hns3_get_rss_indir_size,
1037         .get_rxfh = hns3_get_rss,
1038         .set_rxfh = hns3_set_rss,
1039         .get_link_ksettings = hns3_get_link_ksettings,
1040         .get_channels = hns3_get_channels,
1041         .get_coalesce = hns3_get_coalesce,
1042         .set_coalesce = hns3_set_coalesce,
1043         .get_link = hns3_get_link,
1044 };
1045
1046 static const struct ethtool_ops hns3_ethtool_ops = {
1047         .self_test = hns3_self_test,
1048         .get_drvinfo = hns3_get_drvinfo,
1049         .get_link = hns3_get_link,
1050         .get_ringparam = hns3_get_ringparam,
1051         .set_ringparam = hns3_set_ringparam,
1052         .get_pauseparam = hns3_get_pauseparam,
1053         .set_pauseparam = hns3_set_pauseparam,
1054         .get_strings = hns3_get_strings,
1055         .get_ethtool_stats = hns3_get_stats,
1056         .get_sset_count = hns3_get_sset_count,
1057         .get_rxnfc = hns3_get_rxnfc,
1058         .set_rxnfc = hns3_set_rxnfc,
1059         .get_rxfh_key_size = hns3_get_rss_key_size,
1060         .get_rxfh_indir_size = hns3_get_rss_indir_size,
1061         .get_rxfh = hns3_get_rss,
1062         .set_rxfh = hns3_set_rss,
1063         .get_link_ksettings = hns3_get_link_ksettings,
1064         .set_link_ksettings = hns3_set_link_ksettings,
1065         .nway_reset = hns3_nway_reset,
1066         .get_channels = hns3_get_channels,
1067         .set_channels = hns3_set_channels,
1068         .get_coalesce = hns3_get_coalesce,
1069         .set_coalesce = hns3_set_coalesce,
1070         .get_regs_len = hns3_get_regs_len,
1071         .get_regs = hns3_get_regs,
1072         .set_phys_id = hns3_set_phys_id,
1073 };
1074
1075 void hns3_ethtool_set_ops(struct net_device *netdev)
1076 {
1077         struct hnae3_handle *h = hns3_get_handle(netdev);
1078
1079         if (h->flags & HNAE3_SUPPORT_VF)
1080                 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1081         else
1082                 netdev->ethtool_ops = &hns3_ethtool_ops;
1083 }