]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c
net: hns3: add ethtool_ops.get_channels support for VF
[mirror_ubuntu-bionic-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, \
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
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
77static const struct hns3_link_mode_mapping hns3_lm_map[] = {
78 {HNS3_LM_FIBRE_BIT, ETHTOOL_LINK_MODE_FIBRE_BIT},
79 {HNS3_LM_AUTONEG_BIT, ETHTOOL_LINK_MODE_Autoneg_BIT},
80 {HNS3_LM_TP_BIT, ETHTOOL_LINK_MODE_TP_BIT},
81 {HNS3_LM_PAUSE_BIT, ETHTOOL_LINK_MODE_Pause_BIT},
82 {HNS3_LM_BACKPLANE_BIT, ETHTOOL_LINK_MODE_Backplane_BIT},
83 {HNS3_LM_10BASET_HALF_BIT, ETHTOOL_LINK_MODE_10baseT_Half_BIT},
84 {HNS3_LM_10BASET_FULL_BIT, ETHTOOL_LINK_MODE_10baseT_Full_BIT},
85 {HNS3_LM_100BASET_HALF_BIT, ETHTOOL_LINK_MODE_100baseT_Half_BIT},
86 {HNS3_LM_100BASET_FULL_BIT, ETHTOOL_LINK_MODE_100baseT_Full_BIT},
87 {HNS3_LM_1000BASET_FULL_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT},
88};
89
c39c4d98
YL
90static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop)
91{
92 struct hnae3_handle *h = hns3_get_handle(ndev);
93 int ret;
94
95 if (!h->ae_algo->ops->set_loopback ||
96 !h->ae_algo->ops->set_promisc_mode)
97 return -EOPNOTSUPP;
98
99 switch (loop) {
100 case HNAE3_MAC_INTER_LOOP_MAC:
101 ret = h->ae_algo->ops->set_loopback(h, loop, true);
102 break;
103 case HNAE3_MAC_LOOP_NONE:
104 ret = h->ae_algo->ops->set_loopback(h,
105 HNAE3_MAC_INTER_LOOP_MAC, false);
106 break;
107 default:
108 ret = -ENOTSUPP;
109 break;
110 }
111
112 if (ret)
113 return ret;
114
115 if (loop == HNAE3_MAC_LOOP_NONE)
116 h->ae_algo->ops->set_promisc_mode(h, ndev->flags & IFF_PROMISC);
117 else
118 h->ae_algo->ops->set_promisc_mode(h, 1);
119
120 return ret;
121}
122
123static int hns3_lp_up(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->start)
129 return -EOPNOTSUPP;
130
131 ret = h->ae_algo->ops->start(h);
132 if (ret) {
133 netdev_err(ndev,
134 "hns3_lb_up ae start return error: %d\n", ret);
135 return ret;
136 }
137
138 ret = hns3_lp_setup(ndev, loop_mode);
139 usleep_range(10000, 20000);
140
141 return ret;
142}
143
144static int hns3_lp_down(struct net_device *ndev)
145{
146 struct hnae3_handle *h = hns3_get_handle(ndev);
147 int ret;
148
149 if (!h->ae_algo->ops->stop)
150 return -EOPNOTSUPP;
151
152 ret = hns3_lp_setup(ndev, HNAE3_MAC_LOOP_NONE);
153 if (ret) {
154 netdev_err(ndev, "lb_setup return error: %d\n", ret);
155 return ret;
156 }
157
158 h->ae_algo->ops->stop(h);
159 usleep_range(10000, 20000);
160
161 return 0;
162}
163
164static void hns3_lp_setup_skb(struct sk_buff *skb)
165{
166 struct net_device *ndev = skb->dev;
167 unsigned char *packet;
168 struct ethhdr *ethh;
169 unsigned int i;
170
171 skb_reserve(skb, NET_IP_ALIGN);
172 ethh = skb_put(skb, sizeof(struct ethhdr));
173 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
174
175 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
176 eth_zero_addr(ethh->h_source);
177 ethh->h_proto = htons(ETH_P_ARP);
178 skb_reset_mac_header(skb);
179
180 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
181 packet[i] = (unsigned char)(i & 0xff);
182}
183
184static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
185 struct sk_buff *skb)
186{
187 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
188 unsigned char *packet = skb->data;
189 u32 i;
190
191 for (i = 0; i < skb->len; i++)
192 if (packet[i] != (unsigned char)(i & 0xff))
193 break;
194
195 /* The packet is correctly received */
196 if (i == skb->len)
197 tqp_vector->rx_group.total_packets++;
198 else
199 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
200 skb->data, skb->len, true);
201
202 dev_kfree_skb_any(skb);
203}
204
205static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
206{
207 struct hnae3_handle *h = priv->ae_handle;
208 struct hnae3_knic_private_info *kinfo;
209 u32 i, rcv_good_pkt_total = 0;
210
211 kinfo = &h->kinfo;
212 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
213 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
214 struct hns3_enet_ring_group *rx_group;
215 u64 pre_rx_pkt;
216
217 rx_group = &ring->tqp_vector->rx_group;
218 pre_rx_pkt = rx_group->total_packets;
219
220 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
221
222 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
223 rx_group->total_packets = pre_rx_pkt;
224 }
225 return rcv_good_pkt_total;
226}
227
228static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
229 u32 end_ringid, u32 budget)
230{
231 u32 i;
232
233 for (i = start_ringid; i <= end_ringid; i++) {
234 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
235
236 hns3_clean_tx_ring(ring, budget);
237 }
238}
239
240/**
241 * hns3_lp_run_test - run loopback test
242 * @ndev: net device
243 * @mode: loopback type
244 */
245static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
246{
247 struct hns3_nic_priv *priv = netdev_priv(ndev);
248 struct sk_buff *skb;
249 u32 i, good_cnt;
250 int ret_val = 0;
251
252 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
253 GFP_KERNEL);
254 if (!skb)
255 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
256
257 skb->dev = ndev;
258 hns3_lp_setup_skb(skb);
259 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
260
261 good_cnt = 0;
262 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
263 netdev_tx_t tx_ret;
264
265 skb_get(skb);
266 tx_ret = hns3_nic_net_xmit(skb, ndev);
267 if (tx_ret == NETDEV_TX_OK)
268 good_cnt++;
269 else
270 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
271 tx_ret);
272 }
273 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
274 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
275 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
276 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
277 goto out;
278 }
279
280 /* Allow 200 milliseconds for packets to go from Tx to Rx */
281 msleep(200);
282
283 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
284 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
285 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
286 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
287 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
288 }
289
290out:
291 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
292 HNS3_NIC_LB_TEST_RING_ID,
293 HNS3_NIC_LB_TEST_PKT_NUM);
294
295 kfree_skb(skb);
296 return ret_val;
297}
298
299/**
300 * hns3_nic_self_test - self test
301 * @ndev: net device
302 * @eth_test: test cmd
303 * @data: test result
304 */
305static void hns3_self_test(struct net_device *ndev,
306 struct ethtool_test *eth_test, u64 *data)
307{
308 struct hns3_nic_priv *priv = netdev_priv(ndev);
309 struct hnae3_handle *h = priv->ae_handle;
310 int st_param[HNS3_SELF_TEST_TPYE_NUM][2];
311 bool if_running = netif_running(ndev);
312 int test_index = 0;
313 u32 i;
314
315 /* Only do offline selftest, or pass by default */
316 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
317 return;
318
319 st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
320 st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
321 h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
322
323 if (if_running)
324 dev_close(ndev);
325
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);
337 hns3_lp_down(ndev);
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
348 if (if_running)
349 dev_open(ndev);
350}
351
496d03e9
S
352static void hns3_driv_to_eth_caps(u32 caps, struct ethtool_link_ksettings *cmd,
353 bool is_advertised)
354{
355 int i;
356
357 for (i = 0; i < ARRAY_SIZE(hns3_lm_map); i++) {
358 if (!(caps & hns3_lm_map[i].hns3_link_mode))
359 continue;
360
3e1a8f10 361 if (is_advertised)
496d03e9
S
362 __set_bit(hns3_lm_map[i].ethtool_link_mode,
363 cmd->link_modes.advertising);
3e1a8f10 364 else
496d03e9
S
365 __set_bit(hns3_lm_map[i].ethtool_link_mode,
366 cmd->link_modes.supported);
496d03e9
S
367 }
368}
369
370static int hns3_get_sset_count(struct net_device *netdev, int stringset)
371{
9780cb97 372 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
373 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
374
375 if (!ops->get_sset_count)
376 return -EOPNOTSUPP;
377
378 switch (stringset) {
379 case ETH_SS_STATS:
380 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
381 ops->get_sset_count(h, stringset));
382
383 case ETH_SS_TEST:
384 return ops->get_sset_count(h, stringset);
385 }
386
387 return 0;
388}
389
390static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
c36317be 391 u32 stat_count, u32 num_tqps, const char *prefix)
496d03e9 392{
c36317be 393#define MAX_PREFIX_SIZE (6 + 4)
496d03e9
S
394 u32 size_left;
395 u32 i, j;
396 u32 n1;
397
398 for (i = 0; i < num_tqps; i++) {
399 for (j = 0; j < stat_count; j++) {
400 data[ETH_GSTRING_LEN - 1] = '\0';
401
402 /* first, prepend the prefix string */
c36317be
JS
403 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
404 prefix, i);
496d03e9
S
405 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
406 size_left = (ETH_GSTRING_LEN - 1) - n1;
407
408 /* now, concatenate the stats string to it */
409 strncat(data, stats[j].stats_string, size_left);
410 data += ETH_GSTRING_LEN;
411 }
412 }
413
414 return data;
415}
416
417static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
418{
419 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
c36317be
JS
420 const char tx_prefix[] = "txq";
421 const char rx_prefix[] = "rxq";
496d03e9
S
422
423 /* get strings for Tx */
424 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
c36317be 425 kinfo->num_tqps, tx_prefix);
496d03e9
S
426
427 /* get strings for Rx */
428 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
c36317be 429 kinfo->num_tqps, rx_prefix);
496d03e9
S
430
431 return data;
432}
433
434static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
435{
9780cb97 436 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
437 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
438 char *buff = (char *)data;
439
440 if (!ops->get_strings)
441 return;
442
443 switch (stringset) {
444 case ETH_SS_STATS:
445 buff = hns3_get_strings_tqps(h, buff);
446 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
447 break;
448 case ETH_SS_TEST:
449 ops->get_strings(h, stringset, data);
450 break;
451 }
452}
453
454static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
455{
456 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
457 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
458 struct hns3_enet_ring *ring;
459 u8 *stat;
78cceef2 460 int i, j;
496d03e9
S
461
462 /* get stats for Tx */
463 for (i = 0; i < kinfo->num_tqps; i++) {
464 ring = nic_priv->ring_data[i].ring;
78cceef2
JS
465 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
466 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
496d03e9
S
467 *data++ = *(u64 *)stat;
468 }
469 }
470
471 /* get stats for Rx */
472 for (i = 0; i < kinfo->num_tqps; i++) {
473 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
78cceef2
JS
474 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
475 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
496d03e9
S
476 *data++ = *(u64 *)stat;
477 }
478 }
479
480 return data;
481}
482
483/* hns3_get_stats - get detail statistics.
484 * @netdev: net device
485 * @stats: statistics info.
486 * @data: statistics data.
487 */
1db9b1bf
YL
488static void hns3_get_stats(struct net_device *netdev,
489 struct ethtool_stats *stats, u64 *data)
496d03e9 490{
9780cb97 491 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
492 u64 *p = data;
493
494 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
495 netdev_err(netdev, "could not get any statistics\n");
496 return;
497 }
498
333adffc 499 h->ae_algo->ops->update_stats(h, &netdev->stats);
496d03e9
S
500
501 /* get per-queue stats */
502 p = hns3_get_stats_tqps(h, p);
503
504 /* get MAC & other misc hardware stats */
505 h->ae_algo->ops->get_stats(h, p);
506}
507
508static void hns3_get_drvinfo(struct net_device *netdev,
509 struct ethtool_drvinfo *drvinfo)
510{
511 struct hns3_nic_priv *priv = netdev_priv(netdev);
512 struct hnae3_handle *h = priv->ae_handle;
513
514 strncpy(drvinfo->version, hns3_driver_version,
515 sizeof(drvinfo->version));
516 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
517
518 strncpy(drvinfo->driver, h->pdev->driver->name,
519 sizeof(drvinfo->driver));
520 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
521
522 strncpy(drvinfo->bus_info, pci_name(h->pdev),
523 sizeof(drvinfo->bus_info));
524 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
525
526 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
527 priv->ae_handle->ae_algo->ops->get_fw_version(h));
528}
529
530static u32 hns3_get_link(struct net_device *netdev)
531{
9780cb97 532 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
533
534 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
535 return h->ae_algo->ops->get_status(h);
536 else
537 return 0;
538}
539
540static void hns3_get_ringparam(struct net_device *netdev,
541 struct ethtool_ringparam *param)
542{
543 struct hns3_nic_priv *priv = netdev_priv(netdev);
9780cb97
YL
544 struct hnae3_handle *h = priv->ae_handle;
545 int queue_num = h->kinfo.num_tqps;
496d03e9
S
546
547 param->tx_max_pending = HNS3_RING_MAX_PENDING;
548 param->rx_max_pending = HNS3_RING_MAX_PENDING;
549
550 param->tx_pending = priv->ring_data[0].ring->desc_num;
551 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
552}
553
554static void hns3_get_pauseparam(struct net_device *netdev,
555 struct ethtool_pauseparam *param)
556{
9780cb97 557 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
558
559 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
560 h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
561 &param->rx_pause, &param->tx_pause);
562}
563
09ea401e
PL
564static int hns3_set_pauseparam(struct net_device *netdev,
565 struct ethtool_pauseparam *param)
566{
567 struct hnae3_handle *h = hns3_get_handle(netdev);
568
569 if (h->ae_algo->ops->set_pauseparam)
570 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
571 param->rx_pause,
572 param->tx_pause);
573 return -EOPNOTSUPP;
574}
575
496d03e9
S
576static int hns3_get_link_ksettings(struct net_device *netdev,
577 struct ethtool_link_ksettings *cmd)
578{
9780cb97 579 struct hnae3_handle *h = hns3_get_handle(netdev);
a2cfbadb 580 u32 flowctrl_adv = 0;
496d03e9
S
581 u32 supported_caps;
582 u32 advertised_caps;
fd07a62d 583 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
496d03e9 584 u8 link_stat;
496d03e9
S
585
586 if (!h->ae_algo || !h->ae_algo->ops)
587 return -EOPNOTSUPP;
588
9db85f33 589 /* 1.auto_neg & speed & duplex from cmd */
16b5e501
FL
590 if (netdev->phydev)
591 phy_ethtool_ksettings_get(netdev->phydev, cmd);
592 else if (h->ae_algo->ops->get_ksettings_an_result)
593 h->ae_algo->ops->get_ksettings_an_result(h,
594 &cmd->base.autoneg,
595 &cmd->base.speed,
596 &cmd->base.duplex);
597 else
598 return -EOPNOTSUPP;
599
600 link_stat = hns3_get_link(netdev);
601 if (!link_stat) {
602 cmd->base.speed = SPEED_UNKNOWN;
603 cmd->base.duplex = DUPLEX_UNKNOWN;
496d03e9
S
604 }
605
606 /* 2.media_type get from bios parameter block */
9db85f33 607 if (h->ae_algo->ops->get_media_type) {
496d03e9
S
608 h->ae_algo->ops->get_media_type(h, &media_type);
609
9db85f33
S
610 switch (media_type) {
611 case HNAE3_MEDIA_TYPE_FIBER:
612 cmd->base.port = PORT_FIBRE;
613 supported_caps = HNS3_LM_FIBRE_BIT |
614 HNS3_LM_AUTONEG_BIT |
615 HNS3_LM_PAUSE_BIT |
616 HNS3_LM_1000BASET_FULL_BIT;
617
618 advertised_caps = supported_caps;
619 break;
620 case HNAE3_MEDIA_TYPE_COPPER:
621 cmd->base.port = PORT_TP;
622 supported_caps = HNS3_LM_TP_BIT |
623 HNS3_LM_AUTONEG_BIT |
624 HNS3_LM_PAUSE_BIT |
625 HNS3_LM_1000BASET_FULL_BIT |
626 HNS3_LM_100BASET_FULL_BIT |
627 HNS3_LM_100BASET_HALF_BIT |
628 HNS3_LM_10BASET_FULL_BIT |
629 HNS3_LM_10BASET_HALF_BIT;
630 advertised_caps = supported_caps;
631 break;
632 case HNAE3_MEDIA_TYPE_BACKPLANE:
633 cmd->base.port = PORT_NONE;
634 supported_caps = HNS3_LM_BACKPLANE_BIT |
635 HNS3_LM_PAUSE_BIT |
636 HNS3_LM_AUTONEG_BIT |
637 HNS3_LM_1000BASET_FULL_BIT |
638 HNS3_LM_100BASET_FULL_BIT |
639 HNS3_LM_100BASET_HALF_BIT |
640 HNS3_LM_10BASET_FULL_BIT |
641 HNS3_LM_10BASET_HALF_BIT;
642
643 advertised_caps = supported_caps;
644 break;
645 case HNAE3_MEDIA_TYPE_UNKNOWN:
646 default:
647 cmd->base.port = PORT_OTHER;
648 supported_caps = 0;
649 advertised_caps = 0;
650 break;
651 }
496d03e9 652
2b39cabb
FL
653 if (!cmd->base.autoneg)
654 advertised_caps &= ~HNS3_LM_AUTONEG_BIT;
655
a2cfbadb
PL
656 advertised_caps &= ~HNS3_LM_PAUSE_BIT;
657
9db85f33
S
658 /* now, map driver link modes to ethtool link modes */
659 hns3_driv_to_eth_caps(supported_caps, cmd, false);
660 hns3_driv_to_eth_caps(advertised_caps, cmd, true);
496d03e9
S
661 }
662
496d03e9
S
663 /* 3.mdix_ctrl&mdix get from phy reg */
664 if (h->ae_algo->ops->get_mdix_mode)
665 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
9db85f33 666 &cmd->base.eth_tp_mdix);
496d03e9
S
667 /* 4.mdio_support */
668 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
669
a2cfbadb
PL
670 /* 5.get flow control setttings */
671 if (h->ae_algo->ops->get_flowctrl_adv)
672 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
673
674 if (flowctrl_adv & ADVERTISED_Pause)
675 ethtool_link_ksettings_add_link_mode(cmd, advertising,
676 Pause);
677
678 if (flowctrl_adv & ADVERTISED_Asym_Pause)
679 ethtool_link_ksettings_add_link_mode(cmd, advertising,
680 Asym_Pause);
681
496d03e9
S
682 return 0;
683}
684
80cb5f3d
FL
685static int hns3_set_link_ksettings(struct net_device *netdev,
686 const struct ethtool_link_ksettings *cmd)
687{
688 /* Only support ksettings_set for netdev with phy attached for now */
689 if (netdev->phydev)
690 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
691
692 return -EOPNOTSUPP;
693}
694
496d03e9
S
695static u32 hns3_get_rss_key_size(struct net_device *netdev)
696{
9780cb97 697 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
698
699 if (!h->ae_algo || !h->ae_algo->ops ||
700 !h->ae_algo->ops->get_rss_key_size)
701 return -EOPNOTSUPP;
702
703 return h->ae_algo->ops->get_rss_key_size(h);
704}
705
706static u32 hns3_get_rss_indir_size(struct net_device *netdev)
707{
9780cb97 708 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
709
710 if (!h->ae_algo || !h->ae_algo->ops ||
711 !h->ae_algo->ops->get_rss_indir_size)
712 return -EOPNOTSUPP;
713
714 return h->ae_algo->ops->get_rss_indir_size(h);
715}
716
717static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
718 u8 *hfunc)
719{
9780cb97 720 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
721
722 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
723 return -EOPNOTSUPP;
724
725 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
726}
727
728static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
729 const u8 *key, const u8 hfunc)
730{
9780cb97 731 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9
S
732
733 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
734 return -EOPNOTSUPP;
735
736 /* currently we only support Toeplitz hash */
737 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
738 netdev_err(netdev,
739 "hash func not supported (only Toeplitz hash)\n");
740 return -EOPNOTSUPP;
741 }
742 if (!indir) {
743 netdev_err(netdev,
744 "set rss failed for indir is empty\n");
745 return -EOPNOTSUPP;
746 }
747
748 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
749}
750
751static int hns3_get_rxnfc(struct net_device *netdev,
752 struct ethtool_rxnfc *cmd,
753 u32 *rule_locs)
754{
9780cb97 755 struct hnae3_handle *h = hns3_get_handle(netdev);
496d03e9 756
7410343e 757 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
496d03e9
S
758 return -EOPNOTSUPP;
759
760 switch (cmd->cmd) {
761 case ETHTOOL_GRXRINGS:
0241ffbe 762 cmd->data = h->kinfo.rss_size;
496d03e9 763 break;
07d29954
L
764 case ETHTOOL_GRXFH:
765 return h->ae_algo->ops->get_rss_tuple(h, cmd);
496d03e9
S
766 default:
767 return -EOPNOTSUPP;
768 }
769
770 return 0;
771}
772
7822b083
WY
773static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
774 u32 new_desc_num)
5668abda
L
775{
776 struct hnae3_handle *h = priv->ae_handle;
777 int i;
778
779 h->kinfo.num_desc = new_desc_num;
780
781 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
782 priv->ring_data[i].ring->desc_num = new_desc_num;
783
784 return hns3_init_all_ring(priv);
785}
786
7822b083
WY
787static int hns3_set_ringparam(struct net_device *ndev,
788 struct ethtool_ringparam *param)
5668abda
L
789{
790 struct hns3_nic_priv *priv = netdev_priv(ndev);
791 struct hnae3_handle *h = priv->ae_handle;
792 bool if_running = netif_running(ndev);
793 u32 old_desc_num, new_desc_num;
794 int ret;
795
796 if (param->rx_mini_pending || param->rx_jumbo_pending)
797 return -EINVAL;
798
799 if (param->tx_pending != param->rx_pending) {
800 netdev_err(ndev,
801 "Descriptors of tx and rx must be equal");
802 return -EINVAL;
803 }
804
805 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
806 param->tx_pending < HNS3_RING_MIN_PENDING) {
807 netdev_err(ndev,
808 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
809 param->tx_pending, HNS3_RING_MIN_PENDING,
810 HNS3_RING_MAX_PENDING);
811 return -EINVAL;
812 }
813
814 new_desc_num = param->tx_pending;
815
816 /* Hardware requires that its descriptors must be multiple of eight */
817 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
818 old_desc_num = h->kinfo.num_desc;
819 if (old_desc_num == new_desc_num)
820 return 0;
821
822 netdev_info(ndev,
823 "Changing descriptor count from %d to %d.\n",
824 old_desc_num, new_desc_num);
825
826 if (if_running)
827 dev_close(ndev);
828
829 ret = hns3_uninit_all_ring(priv);
830 if (ret)
831 return ret;
832
833 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
834 if (ret) {
835 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
836 if (ret) {
837 netdev_err(ndev,
838 "Revert to old bd num fail, ret=%d.\n", ret);
839 return ret;
840 }
841 }
842
843 if (if_running)
844 ret = dev_open(ndev);
845
846 return ret;
847}
848
f7db940a
L
849static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
850{
851 struct hnae3_handle *h = hns3_get_handle(netdev);
852
853 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
854 return -EOPNOTSUPP;
855
856 switch (cmd->cmd) {
857 case ETHTOOL_SRXFH:
858 return h->ae_algo->ops->set_rss_tuple(h, cmd);
859 default:
860 return -EOPNOTSUPP;
861 }
862}
863
d63671d2
FL
864static int hns3_nway_reset(struct net_device *netdev)
865{
866 struct phy_device *phy = netdev->phydev;
867
868 if (!netif_running(netdev))
869 return 0;
870
871 /* Only support nway_reset for netdev with phy attached for now */
872 if (!phy)
873 return -EOPNOTSUPP;
874
875 if (phy->autoneg != AUTONEG_ENABLE)
876 return -EINVAL;
877
878 return genphy_restart_aneg(phy);
879}
880
a33df2b8 881static void hns3_get_channels(struct net_device *netdev,
882 struct ethtool_channels *ch)
4f645a90
PL
883{
884 struct hnae3_handle *h = hns3_get_handle(netdev);
885
886 if (h->ae_algo->ops->get_channels)
887 h->ae_algo->ops->get_channels(h, ch);
888}
889
a9c89a3f
SM
890static const struct ethtool_ops hns3vf_ethtool_ops = {
891 .get_drvinfo = hns3_get_drvinfo,
892 .get_ringparam = hns3_get_ringparam,
893 .set_ringparam = hns3_set_ringparam,
894 .get_strings = hns3_get_strings,
895 .get_ethtool_stats = hns3_get_stats,
896 .get_sset_count = hns3_get_sset_count,
897 .get_rxnfc = hns3_get_rxnfc,
898 .get_rxfh_key_size = hns3_get_rss_key_size,
899 .get_rxfh_indir_size = hns3_get_rss_indir_size,
900 .get_rxfh = hns3_get_rss,
901 .set_rxfh = hns3_set_rss,
902 .get_link_ksettings = hns3_get_link_ksettings,
d65818a7 903 .get_channels = hns3_get_channels,
a9c89a3f
SM
904};
905
496d03e9 906static const struct ethtool_ops hns3_ethtool_ops = {
c39c4d98 907 .self_test = hns3_self_test,
496d03e9
S
908 .get_drvinfo = hns3_get_drvinfo,
909 .get_link = hns3_get_link,
910 .get_ringparam = hns3_get_ringparam,
5668abda 911 .set_ringparam = hns3_set_ringparam,
496d03e9 912 .get_pauseparam = hns3_get_pauseparam,
09ea401e 913 .set_pauseparam = hns3_set_pauseparam,
496d03e9
S
914 .get_strings = hns3_get_strings,
915 .get_ethtool_stats = hns3_get_stats,
916 .get_sset_count = hns3_get_sset_count,
917 .get_rxnfc = hns3_get_rxnfc,
f7db940a 918 .set_rxnfc = hns3_set_rxnfc,
496d03e9
S
919 .get_rxfh_key_size = hns3_get_rss_key_size,
920 .get_rxfh_indir_size = hns3_get_rss_indir_size,
921 .get_rxfh = hns3_get_rss,
922 .set_rxfh = hns3_set_rss,
923 .get_link_ksettings = hns3_get_link_ksettings,
80cb5f3d 924 .set_link_ksettings = hns3_set_link_ksettings,
d63671d2 925 .nway_reset = hns3_nway_reset,
4f645a90 926 .get_channels = hns3_get_channels,
f1f779ce 927 .set_channels = hns3_set_channels,
496d03e9
S
928};
929
930void hns3_ethtool_set_ops(struct net_device *netdev)
931{
a9c89a3f
SM
932 struct hnae3_handle *h = hns3_get_handle(netdev);
933
934 if (h->flags & HNAE3_SUPPORT_VF)
935 netdev->ethtool_ops = &hns3vf_ethtool_ops;
936 else
937 netdev->ethtool_ops = &hns3_ethtool_ops;
496d03e9 938}