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