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