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