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