1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
10 #include "hns3_ethtool.h"
12 /* tqp related stats */
13 #define HNS3_TQP_STAT(_string, _member) { \
14 .stats_string = _string, \
15 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
16 offsetof(struct ring_stats, _member), \
19 static const struct hns3_stats hns3_txq_stats[] = {
20 /* Tx per-queue statistics */
21 HNS3_TQP_STAT("dropped", sw_err_cnt),
22 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
23 HNS3_TQP_STAT("packets", tx_pkts),
24 HNS3_TQP_STAT("bytes", tx_bytes),
25 HNS3_TQP_STAT("more", tx_more),
26 HNS3_TQP_STAT("push", tx_push),
27 HNS3_TQP_STAT("mem_doorbell", tx_mem_doorbell),
28 HNS3_TQP_STAT("wake", restart_queue),
29 HNS3_TQP_STAT("busy", tx_busy),
30 HNS3_TQP_STAT("copy", tx_copy),
31 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
32 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
33 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
34 HNS3_TQP_STAT("tso_err", tx_tso_err),
35 HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
36 HNS3_TQP_STAT("hw_limitation", hw_limitation),
37 HNS3_TQP_STAT("bounce", tx_bounce),
38 HNS3_TQP_STAT("spare_full", tx_spare_full),
39 HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
40 HNS3_TQP_STAT("sgl", tx_sgl),
41 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
42 HNS3_TQP_STAT("map_sg_err", map_sg_err),
45 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
47 static const struct hns3_stats hns3_rxq_stats[] = {
48 /* Rx per-queue statistics */
49 HNS3_TQP_STAT("dropped", sw_err_cnt),
50 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
51 HNS3_TQP_STAT("packets", rx_pkts),
52 HNS3_TQP_STAT("bytes", rx_bytes),
53 HNS3_TQP_STAT("errors", rx_err_cnt),
54 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
55 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
56 HNS3_TQP_STAT("err_bd_num", err_bd_num),
57 HNS3_TQP_STAT("l2_err", l2_err),
58 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
59 HNS3_TQP_STAT("csum_complete", csum_complete),
60 HNS3_TQP_STAT("multicast", rx_multicast),
61 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
62 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
63 HNS3_TQP_STAT("frag_alloc", frag_alloc),
66 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
68 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
70 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
72 #define HNS3_NIC_LB_TEST_PKT_NUM 1
73 #define HNS3_NIC_LB_TEST_RING_ID 0
74 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
75 #define HNS3_NIC_LB_SETUP_USEC 10000
77 /* Nic loopback test err */
78 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
79 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
80 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
81 #define HNS3_NIC_LB_TEST_UNEXECUTED 4
83 static int hns3_get_sset_count(struct net_device *netdev, int stringset);
85 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
87 struct hnae3_handle *h = hns3_get_handle(ndev);
88 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
91 if (!h->ae_algo->ops->set_loopback ||
92 !h->ae_algo->ops->set_promisc_mode)
96 case HNAE3_LOOP_SERIAL_SERDES:
97 case HNAE3_LOOP_PARALLEL_SERDES:
100 case HNAE3_LOOP_EXTERNAL:
101 ret = h->ae_algo->ops->set_loopback(h, loop, en);
108 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
112 h->ae_algo->ops->set_promisc_mode(h, true, true);
114 /* recover promisc mode before loopback test */
115 hns3_request_update_promisc_mode(h);
120 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
122 struct hnae3_handle *h = hns3_get_handle(ndev);
125 ret = hns3_nic_reset_all_ring(h);
129 ret = hns3_lp_setup(ndev, loop_mode, true);
130 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
135 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
139 ret = hns3_lp_setup(ndev, loop_mode, false);
141 netdev_err(ndev, "lb_setup return error: %d\n", ret);
145 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
150 static void hns3_lp_setup_skb(struct sk_buff *skb)
152 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
154 struct net_device *ndev = skb->dev;
155 struct hnae3_handle *handle;
156 struct hnae3_ae_dev *ae_dev;
157 unsigned char *packet;
161 skb_reserve(skb, NET_IP_ALIGN);
162 ethh = skb_put(skb, sizeof(struct ethhdr));
163 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
165 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
167 /* The dst mac addr of loopback packet is the same as the host'
168 * mac addr, the SSU component may loop back the packet to host
169 * before the packet reaches mac or serdes, which will defect
170 * the purpose of mac or serdes selftest.
172 handle = hns3_get_handle(ndev);
173 ae_dev = pci_get_drvdata(handle->pdev);
174 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
175 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
176 eth_zero_addr(ethh->h_source);
177 ethh->h_proto = htons(ETH_P_ARP);
178 skb_reset_mac_header(skb);
180 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
181 packet[i] = (unsigned char)(i & 0xff);
184 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
187 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
188 unsigned char *packet = skb->data;
189 u32 len = skb_headlen(skb);
192 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
194 for (i = 0; i < len; i++)
195 if (packet[i] != (unsigned char)(i & 0xff))
198 /* The packet is correctly received */
199 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
200 tqp_vector->rx_group.total_packets++;
202 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
203 skb->data, len, true);
205 dev_kfree_skb_any(skb);
208 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
210 struct hnae3_handle *h = priv->ae_handle;
211 struct hnae3_knic_private_info *kinfo;
212 u32 i, rcv_good_pkt_total = 0;
215 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
216 struct hns3_enet_ring *ring = &priv->ring[i];
217 struct hns3_enet_ring_group *rx_group;
220 rx_group = &ring->tqp_vector->rx_group;
221 pre_rx_pkt = rx_group->total_packets;
224 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
227 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
228 rx_group->total_packets = pre_rx_pkt;
230 return rcv_good_pkt_total;
233 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
238 for (i = start_ringid; i <= end_ringid; i++) {
239 struct hns3_enet_ring *ring = &priv->ring[i];
241 hns3_clean_tx_ring(ring, 0);
246 * hns3_lp_run_test - run loopback test
248 * @mode: loopback type
250 * Return: %0 for success or a NIC loopback test error code on failure
252 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
254 struct hns3_nic_priv *priv = netdev_priv(ndev);
259 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
262 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
265 hns3_lp_setup_skb(skb);
266 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
269 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
273 tx_ret = hns3_nic_net_xmit(skb, ndev);
274 if (tx_ret == NETDEV_TX_OK) {
278 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
282 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
283 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
284 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
285 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
289 /* Allow 200 milliseconds for packets to go from Tx to Rx */
292 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
293 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
294 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
295 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
296 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
300 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
301 HNS3_NIC_LB_TEST_RING_ID);
307 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
309 st_param[HNAE3_LOOP_EXTERNAL][0] = HNAE3_LOOP_EXTERNAL;
310 st_param[HNAE3_LOOP_EXTERNAL][1] =
311 h->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK;
313 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
314 st_param[HNAE3_LOOP_APP][1] =
315 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
317 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
318 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
319 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
321 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
322 HNAE3_LOOP_PARALLEL_SERDES;
323 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
324 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
326 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
327 st_param[HNAE3_LOOP_PHY][1] =
328 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
331 static void hns3_selftest_prepare(struct net_device *ndev, bool if_running)
333 struct hns3_nic_priv *priv = netdev_priv(ndev);
334 struct hnae3_handle *h = priv->ae_handle;
337 ndev->netdev_ops->ndo_stop(ndev);
339 #if IS_ENABLED(CONFIG_VLAN_8021Q)
340 /* Disable the vlan filter for selftest does not support it */
341 if (h->ae_algo->ops->enable_vlan_filter &&
342 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
343 h->ae_algo->ops->enable_vlan_filter(h, false);
346 /* Tell firmware to stop mac autoneg before loopback test start,
347 * otherwise loopback test may be failed when the port is still
350 if (h->ae_algo->ops->halt_autoneg)
351 h->ae_algo->ops->halt_autoneg(h, true);
353 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
356 static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
358 struct hns3_nic_priv *priv = netdev_priv(ndev);
359 struct hnae3_handle *h = priv->ae_handle;
361 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
363 if (h->ae_algo->ops->halt_autoneg)
364 h->ae_algo->ops->halt_autoneg(h, false);
366 #if IS_ENABLED(CONFIG_VLAN_8021Q)
367 if (h->ae_algo->ops->enable_vlan_filter &&
368 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
369 h->ae_algo->ops->enable_vlan_filter(h, true);
373 ndev->netdev_ops->ndo_open(ndev);
376 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
377 struct ethtool_test *eth_test, u64 *data)
379 int test_index = HNAE3_LOOP_APP;
382 for (i = HNAE3_LOOP_APP; i < HNAE3_LOOP_NONE; i++) {
383 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
388 data[test_index] = hns3_lp_up(ndev, loop_type);
389 if (!data[test_index])
390 data[test_index] = hns3_lp_run_test(ndev, loop_type);
392 hns3_lp_down(ndev, loop_type);
394 if (data[test_index])
395 eth_test->flags |= ETH_TEST_FL_FAILED;
401 static void hns3_do_external_lb(struct net_device *ndev,
402 struct ethtool_test *eth_test, u64 *data)
404 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_up(ndev, HNAE3_LOOP_EXTERNAL);
405 if (!data[HNAE3_LOOP_EXTERNAL])
406 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_run_test(ndev, HNAE3_LOOP_EXTERNAL);
407 hns3_lp_down(ndev, HNAE3_LOOP_EXTERNAL);
409 if (data[HNAE3_LOOP_EXTERNAL])
410 eth_test->flags |= ETH_TEST_FL_FAILED;
412 eth_test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
416 * hns3_self_test - self test
418 * @eth_test: test cmd
421 static void hns3_self_test(struct net_device *ndev,
422 struct ethtool_test *eth_test, u64 *data)
424 int cnt = hns3_get_sset_count(ndev, ETH_SS_TEST);
425 struct hns3_nic_priv *priv = netdev_priv(ndev);
426 struct hnae3_handle *h = priv->ae_handle;
427 int st_param[HNAE3_LOOP_NONE][2];
428 bool if_running = netif_running(ndev);
431 /* initialize the loopback test result, avoid marking an unexcuted
432 * loopback test as PASS.
434 for (i = 0; i < cnt; i++)
435 data[i] = HNS3_NIC_LB_TEST_UNEXECUTED;
437 if (hns3_nic_resetting(ndev)) {
438 netdev_err(ndev, "dev resetting!");
442 if (!(eth_test->flags & ETH_TEST_FL_OFFLINE))
445 if (netif_msg_ifdown(h))
446 netdev_info(ndev, "self test start\n");
448 hns3_set_selftest_param(h, st_param);
450 /* external loopback test requires that the link is up and the duplex is
451 * full, do external test first to reduce the whole test time
453 if (eth_test->flags & ETH_TEST_FL_EXTERNAL_LB) {
454 hns3_external_lb_prepare(ndev, if_running);
455 hns3_do_external_lb(ndev, eth_test, data);
456 hns3_external_lb_restore(ndev, if_running);
459 hns3_selftest_prepare(ndev, if_running);
460 hns3_do_selftest(ndev, st_param, eth_test, data);
461 hns3_selftest_restore(ndev, if_running);
463 if (netif_msg_ifdown(h))
464 netdev_info(ndev, "self test end\n");
468 eth_test->flags |= ETH_TEST_FL_FAILED;
471 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
474 struct hnae3_handle *handle = hns3_get_handle(netdev);
477 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
479 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
481 hns3_request_update_promisc_mode(handle);
484 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
485 { "limit_promisc", hns3_update_limit_promisc_mode }
488 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
490 struct hnae3_handle *h = hns3_get_handle(netdev);
491 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
493 if (!ops->get_sset_count)
498 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
499 ops->get_sset_count(h, stringset));
502 return ops->get_sset_count(h, stringset);
504 case ETH_SS_PRIV_FLAGS:
505 return HNAE3_PFLAG_MAX;
512 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
513 u32 stat_count, u32 num_tqps, const char *prefix)
515 #define MAX_PREFIX_SIZE (6 + 4)
520 for (i = 0; i < num_tqps; i++) {
521 for (j = 0; j < stat_count; j++) {
522 data[ETH_GSTRING_LEN - 1] = '\0';
524 /* first, prepend the prefix string */
525 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
527 size_left = (ETH_GSTRING_LEN - 1) - n1;
529 /* now, concatenate the stats string to it */
530 strncat(data, stats[j].stats_string, size_left);
531 data += ETH_GSTRING_LEN;
538 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
540 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
541 const char tx_prefix[] = "txq";
542 const char rx_prefix[] = "rxq";
544 /* get strings for Tx */
545 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
546 kinfo->num_tqps, tx_prefix);
548 /* get strings for Rx */
549 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
550 kinfo->num_tqps, rx_prefix);
555 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
557 struct hnae3_handle *h = hns3_get_handle(netdev);
558 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
559 char *buff = (char *)data;
562 if (!ops->get_strings)
567 buff = hns3_get_strings_tqps(h, buff);
568 ops->get_strings(h, stringset, (u8 *)buff);
571 ops->get_strings(h, stringset, data);
573 case ETH_SS_PRIV_FLAGS:
574 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
575 snprintf(buff, ETH_GSTRING_LEN, "%s",
576 hns3_priv_flags[i].name);
577 buff += ETH_GSTRING_LEN;
585 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
587 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
588 struct hns3_nic_priv *nic_priv = handle->priv;
589 struct hns3_enet_ring *ring;
593 /* get stats for Tx */
594 for (i = 0; i < kinfo->num_tqps; i++) {
595 ring = &nic_priv->ring[i];
596 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
597 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
598 *data++ = *(u64 *)stat;
602 /* get stats for Rx */
603 for (i = 0; i < kinfo->num_tqps; i++) {
604 ring = &nic_priv->ring[i + kinfo->num_tqps];
605 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
606 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
607 *data++ = *(u64 *)stat;
614 /* hns3_get_stats - get detail statistics.
615 * @netdev: net device
616 * @stats: statistics info.
617 * @data: statistics data.
619 static void hns3_get_stats(struct net_device *netdev,
620 struct ethtool_stats *stats, u64 *data)
622 struct hnae3_handle *h = hns3_get_handle(netdev);
625 if (hns3_nic_resetting(netdev)) {
626 netdev_err(netdev, "dev resetting, could not get stats\n");
630 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
631 netdev_err(netdev, "could not get any statistics\n");
635 h->ae_algo->ops->update_stats(h);
637 /* get per-queue stats */
638 p = hns3_get_stats_tqps(h, p);
640 /* get MAC & other misc hardware stats */
641 h->ae_algo->ops->get_stats(h, p);
644 static void hns3_get_drvinfo(struct net_device *netdev,
645 struct ethtool_drvinfo *drvinfo)
647 struct hns3_nic_priv *priv = netdev_priv(netdev);
648 struct hnae3_handle *h = priv->ae_handle;
651 if (!h->ae_algo->ops->get_fw_version) {
652 netdev_err(netdev, "could not get fw version!\n");
656 strscpy(drvinfo->driver, dev_driver_string(&h->pdev->dev),
657 sizeof(drvinfo->driver));
659 strscpy(drvinfo->bus_info, pci_name(h->pdev),
660 sizeof(drvinfo->bus_info));
662 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
664 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
666 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
667 HNAE3_FW_VERSION_BYTE3_SHIFT),
668 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
669 HNAE3_FW_VERSION_BYTE2_SHIFT),
670 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
671 HNAE3_FW_VERSION_BYTE1_SHIFT),
672 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
673 HNAE3_FW_VERSION_BYTE0_SHIFT));
676 static u32 hns3_get_link(struct net_device *netdev)
678 struct hnae3_handle *h = hns3_get_handle(netdev);
680 if (h->ae_algo->ops->get_status)
681 return h->ae_algo->ops->get_status(h);
686 static void hns3_get_ringparam(struct net_device *netdev,
687 struct ethtool_ringparam *param,
688 struct kernel_ethtool_ringparam *kernel_param,
689 struct netlink_ext_ack *extack)
691 struct hns3_nic_priv *priv = netdev_priv(netdev);
692 struct hnae3_handle *h = priv->ae_handle;
693 int rx_queue_index = h->kinfo.num_tqps;
695 if (hns3_nic_resetting(netdev) || !priv->ring) {
696 netdev_err(netdev, "failed to get ringparam value, due to dev resetting or uninited\n");
700 param->tx_max_pending = HNS3_RING_MAX_PENDING;
701 param->rx_max_pending = HNS3_RING_MAX_PENDING;
703 param->tx_pending = priv->ring[0].desc_num;
704 param->rx_pending = priv->ring[rx_queue_index].desc_num;
705 kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size;
706 kernel_param->tx_push = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE,
710 static void hns3_get_pauseparam(struct net_device *netdev,
711 struct ethtool_pauseparam *param)
713 struct hnae3_handle *h = hns3_get_handle(netdev);
714 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
716 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
719 if (h->ae_algo->ops->get_pauseparam)
720 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
721 ¶m->rx_pause, ¶m->tx_pause);
724 static int hns3_set_pauseparam(struct net_device *netdev,
725 struct ethtool_pauseparam *param)
727 struct hnae3_handle *h = hns3_get_handle(netdev);
728 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
730 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
733 netif_dbg(h, drv, netdev,
734 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
735 param->autoneg, param->rx_pause, param->tx_pause);
737 if (h->ae_algo->ops->set_pauseparam)
738 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
744 static void hns3_get_ksettings(struct hnae3_handle *h,
745 struct ethtool_link_ksettings *cmd)
747 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
749 /* 1.auto_neg & speed & duplex from cmd */
750 if (ops->get_ksettings_an_result)
751 ops->get_ksettings_an_result(h,
757 /* 2.get link mode */
758 if (ops->get_link_mode)
759 ops->get_link_mode(h,
760 cmd->link_modes.supported,
761 cmd->link_modes.advertising);
763 /* 3.mdix_ctrl&mdix get from phy reg */
764 if (ops->get_mdix_mode)
765 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
766 &cmd->base.eth_tp_mdix);
769 static int hns3_get_link_ksettings(struct net_device *netdev,
770 struct ethtool_link_ksettings *cmd)
772 struct hnae3_handle *h = hns3_get_handle(netdev);
773 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
774 const struct hnae3_ae_ops *ops;
779 ops = h->ae_algo->ops;
780 if (ops->get_media_type)
781 ops->get_media_type(h, &media_type, &module_type);
785 switch (media_type) {
786 case HNAE3_MEDIA_TYPE_NONE:
787 cmd->base.port = PORT_NONE;
788 hns3_get_ksettings(h, cmd);
790 case HNAE3_MEDIA_TYPE_FIBER:
791 if (module_type == HNAE3_MODULE_TYPE_UNKNOWN)
792 cmd->base.port = PORT_OTHER;
793 else if (module_type == HNAE3_MODULE_TYPE_CR)
794 cmd->base.port = PORT_DA;
796 cmd->base.port = PORT_FIBRE;
798 hns3_get_ksettings(h, cmd);
800 case HNAE3_MEDIA_TYPE_BACKPLANE:
801 cmd->base.port = PORT_NONE;
802 hns3_get_ksettings(h, cmd);
804 case HNAE3_MEDIA_TYPE_COPPER:
805 cmd->base.port = PORT_TP;
806 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
807 ops->get_phy_link_ksettings)
808 ops->get_phy_link_ksettings(h, cmd);
809 else if (!netdev->phydev)
810 hns3_get_ksettings(h, cmd);
812 phy_ethtool_ksettings_get(netdev->phydev, cmd);
816 netdev_warn(netdev, "Unknown media type");
821 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
823 link_stat = hns3_get_link(netdev);
825 cmd->base.speed = SPEED_UNKNOWN;
826 cmd->base.duplex = DUPLEX_UNKNOWN;
832 static int hns3_check_ksettings_param(const struct net_device *netdev,
833 const struct ethtool_link_ksettings *cmd)
835 struct hnae3_handle *handle = hns3_get_handle(netdev);
836 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
837 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
838 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
845 /* hw doesn't support use specified speed and duplex to negotiate,
846 * unnecessary to check them when autoneg on.
848 if (cmd->base.autoneg)
851 if (ops->get_ksettings_an_result) {
852 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex, &lane_num);
853 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
854 cmd->base.duplex == duplex && cmd->lanes == lane_num)
858 if (ops->get_media_type)
859 ops->get_media_type(handle, &media_type, &module_type);
861 if (cmd->base.duplex == DUPLEX_HALF &&
862 media_type != HNAE3_MEDIA_TYPE_COPPER) {
864 "only copper port supports half duplex!");
868 if (ops->check_port_speed) {
869 ret = ops->check_port_speed(handle, cmd->base.speed);
871 netdev_err(netdev, "unsupported speed\n");
879 static int hns3_set_link_ksettings(struct net_device *netdev,
880 const struct ethtool_link_ksettings *cmd)
882 struct hnae3_handle *handle = hns3_get_handle(netdev);
883 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
884 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
887 /* Chip don't support this mode. */
888 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
891 if (cmd->lanes && !hnae3_ae_dev_lane_num_supported(ae_dev))
894 netif_dbg(handle, drv, netdev,
895 "set link(%s): autoneg=%u, speed=%u, duplex=%u, lanes=%u\n",
896 netdev->phydev ? "phy" : "mac",
897 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex,
900 /* Only support ksettings_set for netdev with phy attached for now */
901 if (netdev->phydev) {
902 if (cmd->base.speed == SPEED_1000 &&
903 cmd->base.autoneg == AUTONEG_DISABLE)
906 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
907 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
908 ops->set_phy_link_ksettings) {
909 return ops->set_phy_link_ksettings(handle, cmd);
912 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
915 ret = hns3_check_ksettings_param(netdev, cmd);
919 if (ops->set_autoneg) {
920 ret = ops->set_autoneg(handle, cmd->base.autoneg);
925 /* hw doesn't support use specified speed and duplex to negotiate,
926 * ignore them when autoneg on.
928 if (cmd->base.autoneg) {
930 "autoneg is on, ignore the speed and duplex\n");
934 if (ops->cfg_mac_speed_dup_h)
935 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
936 cmd->base.duplex, (u8)(cmd->lanes));
941 static u32 hns3_get_rss_key_size(struct net_device *netdev)
943 struct hnae3_handle *h = hns3_get_handle(netdev);
945 if (!h->ae_algo->ops->get_rss_key_size)
948 return h->ae_algo->ops->get_rss_key_size(h);
951 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
953 struct hnae3_handle *h = hns3_get_handle(netdev);
954 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
956 return ae_dev->dev_specs.rss_ind_tbl_size;
959 static int hns3_get_rss(struct net_device *netdev,
960 struct ethtool_rxfh_param *rxfh)
962 struct hnae3_handle *h = hns3_get_handle(netdev);
964 if (!h->ae_algo->ops->get_rss)
967 return h->ae_algo->ops->get_rss(h, rxfh->indir, rxfh->key,
971 static int hns3_set_rss(struct net_device *netdev,
972 struct ethtool_rxfh_param *rxfh,
973 struct netlink_ext_ack *extack)
975 struct hnae3_handle *h = hns3_get_handle(netdev);
976 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
978 if (!h->ae_algo->ops->set_rss)
981 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
982 rxfh->hfunc != ETH_RSS_HASH_TOP) ||
983 (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
984 rxfh->hfunc != ETH_RSS_HASH_TOP &&
985 rxfh->hfunc != ETH_RSS_HASH_XOR)) {
986 netdev_err(netdev, "hash func not supported\n");
992 "set rss failed for indir is empty\n");
996 return h->ae_algo->ops->set_rss(h, rxfh->indir, rxfh->key,
1000 static int hns3_get_rxnfc(struct net_device *netdev,
1001 struct ethtool_rxnfc *cmd,
1004 struct hnae3_handle *h = hns3_get_handle(netdev);
1007 case ETHTOOL_GRXRINGS:
1008 cmd->data = h->kinfo.num_tqps;
1011 if (h->ae_algo->ops->get_rss_tuple)
1012 return h->ae_algo->ops->get_rss_tuple(h, cmd);
1014 case ETHTOOL_GRXCLSRLCNT:
1015 if (h->ae_algo->ops->get_fd_rule_cnt)
1016 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
1018 case ETHTOOL_GRXCLSRULE:
1019 if (h->ae_algo->ops->get_fd_rule_info)
1020 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
1022 case ETHTOOL_GRXCLSRLALL:
1023 if (h->ae_algo->ops->get_fd_all_rules)
1024 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
1032 static const struct hns3_reset_type_map hns3_reset_type[] = {
1033 {ETH_RESET_MGMT, HNAE3_IMP_RESET},
1034 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
1035 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
1038 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
1039 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
1042 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
1044 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
1045 struct hnae3_handle *h = hns3_get_handle(netdev);
1046 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
1047 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
1048 const struct hns3_reset_type_map *rst_type_map;
1049 enum ethtool_reset_flags rst_flags;
1052 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
1055 if (!ops->set_default_reset_request || !ops->reset_event)
1058 if (h->flags & HNAE3_SUPPORT_VF) {
1059 rst_type_map = hns3vf_reset_type;
1060 size = ARRAY_SIZE(hns3vf_reset_type);
1062 rst_type_map = hns3_reset_type;
1063 size = ARRAY_SIZE(hns3_reset_type);
1066 for (i = 0; i < size; i++) {
1067 if (rst_type_map[i].rst_flags == *flags) {
1068 rst_type = rst_type_map[i].rst_type;
1069 rst_flags = rst_type_map[i].rst_flags;
1074 if (rst_type == HNAE3_NONE_RESET ||
1075 (rst_type == HNAE3_IMP_RESET &&
1076 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1079 netdev_info(netdev, "Setting reset type %d\n", rst_type);
1081 ops->set_default_reset_request(ae_dev, rst_type);
1083 ops->reset_event(h->pdev, h);
1085 *flags &= ~rst_flags;
1090 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1091 u32 tx_desc_num, u32 rx_desc_num)
1093 struct hnae3_handle *h = priv->ae_handle;
1096 h->kinfo.num_tx_desc = tx_desc_num;
1097 h->kinfo.num_rx_desc = rx_desc_num;
1099 for (i = 0; i < h->kinfo.num_tqps; i++) {
1100 priv->ring[i].desc_num = tx_desc_num;
1101 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1105 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1107 struct hnae3_handle *handle = priv->ae_handle;
1108 struct hns3_enet_ring *tmp_rings;
1111 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1112 sizeof(struct hns3_enet_ring), GFP_KERNEL);
1116 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1117 memcpy(&tmp_rings[i], &priv->ring[i],
1118 sizeof(struct hns3_enet_ring));
1119 tmp_rings[i].skb = NULL;
1125 static int hns3_check_ringparam(struct net_device *ndev,
1126 struct ethtool_ringparam *param,
1127 struct kernel_ethtool_ringparam *kernel_param)
1129 #define RX_BUF_LEN_2K 2048
1130 #define RX_BUF_LEN_4K 4096
1132 struct hns3_nic_priv *priv = netdev_priv(ndev);
1134 if (hns3_nic_resetting(ndev) || !priv->ring) {
1135 netdev_err(ndev, "failed to set ringparam value, due to dev resetting or uninited\n");
1140 if (param->rx_mini_pending || param->rx_jumbo_pending)
1143 if (kernel_param->rx_buf_len != RX_BUF_LEN_2K &&
1144 kernel_param->rx_buf_len != RX_BUF_LEN_4K) {
1145 netdev_err(ndev, "Rx buf len only support 2048 and 4096\n");
1149 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1150 param->tx_pending < HNS3_RING_MIN_PENDING ||
1151 param->rx_pending > HNS3_RING_MAX_PENDING ||
1152 param->rx_pending < HNS3_RING_MIN_PENDING) {
1153 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1154 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1162 hns3_is_ringparam_changed(struct net_device *ndev,
1163 struct ethtool_ringparam *param,
1164 struct kernel_ethtool_ringparam *kernel_param,
1165 struct hns3_ring_param *old_ringparam,
1166 struct hns3_ring_param *new_ringparam)
1168 struct hns3_nic_priv *priv = netdev_priv(ndev);
1169 struct hnae3_handle *h = priv->ae_handle;
1170 u16 queue_num = h->kinfo.num_tqps;
1172 new_ringparam->tx_desc_num = ALIGN(param->tx_pending,
1173 HNS3_RING_BD_MULTIPLE);
1174 new_ringparam->rx_desc_num = ALIGN(param->rx_pending,
1175 HNS3_RING_BD_MULTIPLE);
1176 old_ringparam->tx_desc_num = priv->ring[0].desc_num;
1177 old_ringparam->rx_desc_num = priv->ring[queue_num].desc_num;
1178 old_ringparam->rx_buf_len = priv->ring[queue_num].buf_size;
1179 new_ringparam->rx_buf_len = kernel_param->rx_buf_len;
1181 if (old_ringparam->tx_desc_num == new_ringparam->tx_desc_num &&
1182 old_ringparam->rx_desc_num == new_ringparam->rx_desc_num &&
1183 old_ringparam->rx_buf_len == new_ringparam->rx_buf_len) {
1184 netdev_info(ndev, "descriptor number and rx buffer length not changed\n");
1191 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len)
1193 struct hns3_nic_priv *priv = netdev_priv(ndev);
1194 struct hnae3_handle *h = priv->ae_handle;
1197 h->kinfo.rx_buf_len = rx_buf_len;
1199 for (i = 0; i < h->kinfo.num_tqps; i++) {
1200 h->kinfo.tqp[i]->buf_size = rx_buf_len;
1201 priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len;
1207 static int hns3_set_tx_push(struct net_device *netdev, u32 tx_push)
1209 struct hns3_nic_priv *priv = netdev_priv(netdev);
1210 struct hnae3_handle *h = hns3_get_handle(netdev);
1211 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
1212 u32 old_state = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1214 if (!test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps) && tx_push)
1217 if (tx_push == old_state)
1220 netdev_dbg(netdev, "Changing tx push from %s to %s\n",
1221 old_state ? "on" : "off", tx_push ? "on" : "off");
1224 set_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1226 clear_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1231 static int hns3_set_ringparam(struct net_device *ndev,
1232 struct ethtool_ringparam *param,
1233 struct kernel_ethtool_ringparam *kernel_param,
1234 struct netlink_ext_ack *extack)
1236 struct hns3_ring_param old_ringparam, new_ringparam;
1237 struct hns3_nic_priv *priv = netdev_priv(ndev);
1238 struct hnae3_handle *h = priv->ae_handle;
1239 struct hns3_enet_ring *tmp_rings;
1240 bool if_running = netif_running(ndev);
1243 ret = hns3_check_ringparam(ndev, param, kernel_param);
1247 ret = hns3_set_tx_push(ndev, kernel_param->tx_push);
1251 if (!hns3_is_ringparam_changed(ndev, param, kernel_param,
1252 &old_ringparam, &new_ringparam))
1255 tmp_rings = hns3_backup_ringparam(priv);
1257 netdev_err(ndev, "backup ring param failed by allocating memory fail\n");
1262 "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %u to %u\n",
1263 old_ringparam.tx_desc_num, old_ringparam.rx_desc_num,
1264 new_ringparam.tx_desc_num, new_ringparam.rx_desc_num,
1265 old_ringparam.rx_buf_len, new_ringparam.rx_buf_len);
1268 ndev->netdev_ops->ndo_stop(ndev);
1270 hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
1271 new_ringparam.rx_desc_num);
1272 hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len);
1273 ret = hns3_init_all_ring(priv);
1275 netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n",
1278 hns3_change_rx_buf_len(ndev, old_ringparam.rx_buf_len);
1279 hns3_change_all_ring_bd_num(priv, old_ringparam.tx_desc_num,
1280 old_ringparam.rx_desc_num);
1281 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1282 memcpy(&priv->ring[i], &tmp_rings[i],
1283 sizeof(struct hns3_enet_ring));
1285 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1286 hns3_fini_ring(&tmp_rings[i]);
1292 ret = ndev->netdev_ops->ndo_open(ndev);
1297 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1299 struct hnae3_handle *h = hns3_get_handle(netdev);
1303 if (h->ae_algo->ops->set_rss_tuple)
1304 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1306 case ETHTOOL_SRXCLSRLINS:
1307 if (h->ae_algo->ops->add_fd_entry)
1308 return h->ae_algo->ops->add_fd_entry(h, cmd);
1310 case ETHTOOL_SRXCLSRLDEL:
1311 if (h->ae_algo->ops->del_fd_entry)
1312 return h->ae_algo->ops->del_fd_entry(h, cmd);
1319 static int hns3_nway_reset(struct net_device *netdev)
1321 struct hnae3_handle *handle = hns3_get_handle(netdev);
1322 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1323 struct phy_device *phy = netdev->phydev;
1326 if (!netif_running(netdev))
1329 if (hns3_nic_resetting(netdev)) {
1330 netdev_err(netdev, "dev resetting!");
1334 if (!ops->get_autoneg || !ops->restart_autoneg)
1337 autoneg = ops->get_autoneg(handle);
1338 if (autoneg != AUTONEG_ENABLE) {
1340 "Autoneg is off, don't support to restart it\n");
1344 netif_dbg(handle, drv, netdev,
1345 "nway reset (using %s)\n", phy ? "phy" : "mac");
1348 return genphy_restart_aneg(phy);
1350 return ops->restart_autoneg(handle);
1353 static void hns3_get_channels(struct net_device *netdev,
1354 struct ethtool_channels *ch)
1356 struct hnae3_handle *h = hns3_get_handle(netdev);
1358 if (h->ae_algo->ops->get_channels)
1359 h->ae_algo->ops->get_channels(h, ch);
1362 static int hns3_get_coalesce(struct net_device *netdev,
1363 struct ethtool_coalesce *cmd,
1364 struct kernel_ethtool_coalesce *kernel_coal,
1365 struct netlink_ext_ack *extack)
1367 struct hns3_nic_priv *priv = netdev_priv(netdev);
1368 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1369 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1370 struct hnae3_handle *h = priv->ae_handle;
1372 if (hns3_nic_resetting(netdev))
1375 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1376 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1378 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1379 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1381 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1382 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1384 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1385 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1387 kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1388 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1389 kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1390 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1395 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1396 struct ethtool_coalesce *cmd)
1398 struct hnae3_handle *handle = hns3_get_handle(netdev);
1399 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1402 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1404 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1405 ae_dev->dev_specs.max_int_gl);
1409 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1411 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1412 ae_dev->dev_specs.max_int_gl);
1416 /* device version above V3(include V3), GL uses 1us unit,
1417 * so the round down is not needed.
1419 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1422 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1423 if (rx_gl != cmd->rx_coalesce_usecs) {
1425 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1426 cmd->rx_coalesce_usecs, rx_gl);
1429 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1430 if (tx_gl != cmd->tx_coalesce_usecs) {
1432 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1433 cmd->tx_coalesce_usecs, tx_gl);
1439 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1440 struct ethtool_coalesce *cmd)
1444 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1446 "tx_usecs_high must be same as rx_usecs_high.\n");
1450 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1452 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1457 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1458 if (rl != cmd->rx_coalesce_usecs_high) {
1460 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1461 cmd->rx_coalesce_usecs_high, rl);
1467 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1468 struct ethtool_coalesce *cmd)
1470 struct hnae3_handle *handle = hns3_get_handle(netdev);
1471 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1473 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1474 !ae_dev->dev_specs.int_ql_max) {
1475 netdev_err(netdev, "coalesced frames is not supported\n");
1479 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1480 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1482 "invalid coalesced_frames value, range is 0-%u\n",
1483 ae_dev->dev_specs.int_ql_max);
1491 hns3_check_cqe_coalesce_param(struct net_device *netdev,
1492 struct kernel_ethtool_coalesce *kernel_coal)
1494 struct hnae3_handle *handle = hns3_get_handle(netdev);
1495 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1497 if ((kernel_coal->use_cqe_mode_tx || kernel_coal->use_cqe_mode_rx) &&
1498 !hnae3_ae_dev_cq_supported(ae_dev)) {
1499 netdev_err(netdev, "coalesced cqe mode is not supported\n");
1507 hns3_check_coalesce_para(struct net_device *netdev,
1508 struct ethtool_coalesce *cmd,
1509 struct kernel_ethtool_coalesce *kernel_coal)
1513 ret = hns3_check_cqe_coalesce_param(netdev, kernel_coal);
1517 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1520 "Check gl coalesce param fail. ret = %d\n", ret);
1524 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1527 "Check rl coalesce param fail. ret = %d\n", ret);
1531 return hns3_check_ql_coalesce_param(netdev, cmd);
1534 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1535 struct ethtool_coalesce *cmd,
1538 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1539 struct hns3_nic_priv *priv = netdev_priv(netdev);
1540 struct hnae3_handle *h = priv->ae_handle;
1541 int queue_num = h->kinfo.num_tqps;
1543 tx_vector = priv->ring[queue].tqp_vector;
1544 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1546 tx_vector->tx_group.coal.adapt_enable =
1547 cmd->use_adaptive_tx_coalesce;
1548 rx_vector->rx_group.coal.adapt_enable =
1549 cmd->use_adaptive_rx_coalesce;
1551 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1552 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1554 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1555 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1557 hns3_set_vector_coalesce_tx_gl(tx_vector,
1558 tx_vector->tx_group.coal.int_gl);
1559 hns3_set_vector_coalesce_rx_gl(rx_vector,
1560 rx_vector->rx_group.coal.int_gl);
1562 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1563 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1565 if (tx_vector->tx_group.coal.ql_enable)
1566 hns3_set_vector_coalesce_tx_ql(tx_vector,
1567 tx_vector->tx_group.coal.int_ql);
1568 if (rx_vector->rx_group.coal.ql_enable)
1569 hns3_set_vector_coalesce_rx_ql(rx_vector,
1570 rx_vector->rx_group.coal.int_ql);
1573 static int hns3_set_coalesce(struct net_device *netdev,
1574 struct ethtool_coalesce *cmd,
1575 struct kernel_ethtool_coalesce *kernel_coal,
1576 struct netlink_ext_ack *extack)
1578 struct hnae3_handle *h = hns3_get_handle(netdev);
1579 struct hns3_nic_priv *priv = netdev_priv(netdev);
1580 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1581 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1582 u16 queue_num = h->kinfo.num_tqps;
1583 enum dim_cq_period_mode tx_mode;
1584 enum dim_cq_period_mode rx_mode;
1588 if (hns3_nic_resetting(netdev))
1591 ret = hns3_check_coalesce_para(netdev, cmd, kernel_coal);
1595 h->kinfo.int_rl_setting =
1596 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1598 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1599 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1601 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1602 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1604 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1605 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1607 for (i = 0; i < queue_num; i++)
1608 hns3_set_coalesce_per_queue(netdev, cmd, i);
1610 tx_mode = kernel_coal->use_cqe_mode_tx ?
1611 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1612 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1613 rx_mode = kernel_coal->use_cqe_mode_rx ?
1614 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1615 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1616 hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1621 static int hns3_get_regs_len(struct net_device *netdev)
1623 struct hnae3_handle *h = hns3_get_handle(netdev);
1625 if (!h->ae_algo->ops->get_regs_len)
1628 return h->ae_algo->ops->get_regs_len(h);
1631 static void hns3_get_regs(struct net_device *netdev,
1632 struct ethtool_regs *cmd, void *data)
1634 struct hnae3_handle *h = hns3_get_handle(netdev);
1636 if (!h->ae_algo->ops->get_regs)
1639 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1642 static int hns3_set_phys_id(struct net_device *netdev,
1643 enum ethtool_phys_id_state state)
1645 struct hnae3_handle *h = hns3_get_handle(netdev);
1647 if (!h->ae_algo->ops->set_led_id)
1650 return h->ae_algo->ops->set_led_id(h, state);
1653 static u32 hns3_get_msglevel(struct net_device *netdev)
1655 struct hnae3_handle *h = hns3_get_handle(netdev);
1657 return h->msg_enable;
1660 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1662 struct hnae3_handle *h = hns3_get_handle(netdev);
1664 h->msg_enable = msg_level;
1667 static void hns3_get_fec_stats(struct net_device *netdev,
1668 struct ethtool_fec_stats *fec_stats)
1670 struct hnae3_handle *handle = hns3_get_handle(netdev);
1671 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1672 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1674 if (!hnae3_ae_dev_fec_stats_supported(ae_dev) || !ops->get_fec_stats)
1677 ops->get_fec_stats(handle, fec_stats);
1680 /* Translate local fec value into ethtool value. */
1681 static unsigned int loc_to_eth_fec(u8 loc_fec)
1685 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1686 eth_fec |= ETHTOOL_FEC_AUTO;
1687 if (loc_fec & BIT(HNAE3_FEC_RS))
1688 eth_fec |= ETHTOOL_FEC_RS;
1689 if (loc_fec & BIT(HNAE3_FEC_LLRS))
1690 eth_fec |= ETHTOOL_FEC_LLRS;
1691 if (loc_fec & BIT(HNAE3_FEC_BASER))
1692 eth_fec |= ETHTOOL_FEC_BASER;
1693 if (loc_fec & BIT(HNAE3_FEC_NONE))
1694 eth_fec |= ETHTOOL_FEC_OFF;
1699 /* Translate ethtool fec value into local value. */
1700 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1704 if (eth_fec & ETHTOOL_FEC_OFF)
1705 loc_fec |= BIT(HNAE3_FEC_NONE);
1706 if (eth_fec & ETHTOOL_FEC_AUTO)
1707 loc_fec |= BIT(HNAE3_FEC_AUTO);
1708 if (eth_fec & ETHTOOL_FEC_RS)
1709 loc_fec |= BIT(HNAE3_FEC_RS);
1710 if (eth_fec & ETHTOOL_FEC_LLRS)
1711 loc_fec |= BIT(HNAE3_FEC_LLRS);
1712 if (eth_fec & ETHTOOL_FEC_BASER)
1713 loc_fec |= BIT(HNAE3_FEC_BASER);
1718 static int hns3_get_fecparam(struct net_device *netdev,
1719 struct ethtool_fecparam *fec)
1721 struct hnae3_handle *handle = hns3_get_handle(netdev);
1722 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1723 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1727 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1733 ops->get_fec(handle, &fec_ability, &fec_mode);
1735 fec->fec = loc_to_eth_fec(fec_ability);
1736 fec->active_fec = loc_to_eth_fec(fec_mode);
1737 if (!fec->active_fec)
1738 fec->active_fec = ETHTOOL_FEC_OFF;
1743 static int hns3_set_fecparam(struct net_device *netdev,
1744 struct ethtool_fecparam *fec)
1746 struct hnae3_handle *handle = hns3_get_handle(netdev);
1747 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1748 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1751 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1756 fec_mode = eth_to_loc_fec(fec->fec);
1758 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1760 return ops->set_fec(handle, fec_mode);
1763 static int hns3_get_module_info(struct net_device *netdev,
1764 struct ethtool_modinfo *modinfo)
1766 #define HNS3_SFF_8636_V1_3 0x03
1768 struct hnae3_handle *handle = hns3_get_handle(netdev);
1769 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1770 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1771 struct hns3_sfp_type sfp_type;
1774 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1775 !ops->get_module_eeprom)
1778 memset(&sfp_type, 0, sizeof(sfp_type));
1779 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1784 switch (sfp_type.type) {
1785 case SFF8024_ID_SFP:
1786 modinfo->type = ETH_MODULE_SFF_8472;
1787 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1789 case SFF8024_ID_QSFP_8438:
1790 modinfo->type = ETH_MODULE_SFF_8436;
1791 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1793 case SFF8024_ID_QSFP_8436_8636:
1794 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1795 modinfo->type = ETH_MODULE_SFF_8436;
1796 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1798 modinfo->type = ETH_MODULE_SFF_8636;
1799 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1802 case SFF8024_ID_QSFP28_8636:
1803 modinfo->type = ETH_MODULE_SFF_8636;
1804 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1807 netdev_err(netdev, "Optical module unknown: %#x\n",
1815 static int hns3_get_module_eeprom(struct net_device *netdev,
1816 struct ethtool_eeprom *ee, u8 *data)
1818 struct hnae3_handle *handle = hns3_get_handle(netdev);
1819 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1820 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1822 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1823 !ops->get_module_eeprom)
1829 memset(data, 0, ee->len);
1831 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1834 static u32 hns3_get_priv_flags(struct net_device *netdev)
1836 struct hnae3_handle *handle = hns3_get_handle(netdev);
1838 return handle->priv_flags;
1841 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1845 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1846 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1847 netdev_err(h->netdev, "%s is unsupported\n",
1848 hns3_priv_flags[i].name);
1855 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1857 struct hnae3_handle *handle = hns3_get_handle(netdev);
1858 u32 changed = pflags ^ handle->priv_flags;
1862 ret = hns3_check_priv_flags(handle, changed);
1866 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1867 if (changed & BIT(i)) {
1868 bool enable = !(handle->priv_flags & BIT(i));
1871 handle->priv_flags |= BIT(i);
1873 handle->priv_flags &= ~BIT(i);
1874 hns3_priv_flags[i].handler(netdev, enable);
1881 static int hns3_get_tunable(struct net_device *netdev,
1882 const struct ethtool_tunable *tuna,
1885 struct hns3_nic_priv *priv = netdev_priv(netdev);
1886 struct hnae3_handle *h = priv->ae_handle;
1890 case ETHTOOL_TX_COPYBREAK:
1891 /* all the tx rings have the same tx_copybreak */
1892 *(u32 *)data = priv->tx_copybreak;
1894 case ETHTOOL_RX_COPYBREAK:
1895 *(u32 *)data = priv->rx_copybreak;
1897 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1898 *(u32 *)data = h->kinfo.tx_spare_buf_size;
1908 static int hns3_set_tx_spare_buf_size(struct net_device *netdev,
1911 struct hns3_nic_priv *priv = netdev_priv(netdev);
1912 struct hnae3_handle *h = priv->ae_handle;
1915 h->kinfo.tx_spare_buf_size = data;
1917 ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
1921 ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1925 ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT);
1929 ret = hns3_reset_notify(h, HNAE3_UP_CLIENT);
1931 hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1936 static int hns3_set_tunable(struct net_device *netdev,
1937 const struct ethtool_tunable *tuna,
1940 struct hns3_nic_priv *priv = netdev_priv(netdev);
1941 u32 old_tx_spare_buf_size, new_tx_spare_buf_size;
1942 struct hnae3_handle *h = priv->ae_handle;
1945 if (hns3_nic_resetting(netdev) || !priv->ring) {
1946 netdev_err(netdev, "failed to set tunable value, dev resetting!");
1951 case ETHTOOL_TX_COPYBREAK:
1952 priv->tx_copybreak = *(u32 *)data;
1954 for (i = 0; i < h->kinfo.num_tqps; i++)
1955 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1958 case ETHTOOL_RX_COPYBREAK:
1959 priv->rx_copybreak = *(u32 *)data;
1961 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1962 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1965 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1966 old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size;
1967 new_tx_spare_buf_size = *(u32 *)data;
1968 netdev_info(netdev, "request to set tx spare buf size from %u to %u\n",
1969 old_tx_spare_buf_size, new_tx_spare_buf_size);
1970 ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size);
1972 (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) {
1975 netdev_warn(netdev, "change tx spare buf size fail, revert to old value\n");
1976 ret1 = hns3_set_tx_spare_buf_size(netdev,
1977 old_tx_spare_buf_size);
1979 netdev_err(netdev, "revert to old tx spare buf size fail\n");
1986 if (!priv->ring->tx_spare)
1987 netdev_info(netdev, "the active tx spare buf size is 0, disable tx spare buffer\n");
1989 netdev_info(netdev, "the active tx spare buf size is %u, due to page order\n",
1990 priv->ring->tx_spare->len);
2001 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
2002 ETHTOOL_COALESCE_USE_ADAPTIVE | \
2003 ETHTOOL_COALESCE_RX_USECS_HIGH | \
2004 ETHTOOL_COALESCE_TX_USECS_HIGH | \
2005 ETHTOOL_COALESCE_MAX_FRAMES | \
2006 ETHTOOL_COALESCE_USE_CQE)
2008 #define HNS3_ETHTOOL_RING (ETHTOOL_RING_USE_RX_BUF_LEN | \
2009 ETHTOOL_RING_USE_TX_PUSH)
2011 static int hns3_get_ts_info(struct net_device *netdev,
2012 struct ethtool_ts_info *info)
2014 struct hnae3_handle *handle = hns3_get_handle(netdev);
2016 if (handle->ae_algo->ops->get_ts_info)
2017 return handle->ae_algo->ops->get_ts_info(handle, info);
2019 return ethtool_op_get_ts_info(netdev, info);
2022 static const struct hns3_ethtool_link_ext_state_mapping
2023 hns3_link_ext_state_map[] = {
2024 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
2025 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
2026 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
2027 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
2029 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
2030 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
2031 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
2032 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
2033 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
2034 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
2036 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
2037 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
2038 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
2039 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
2040 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
2041 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
2043 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
2044 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
2045 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
2046 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
2047 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
2048 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
2050 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
2051 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
2052 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
2054 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
2057 static int hns3_get_link_ext_state(struct net_device *netdev,
2058 struct ethtool_link_ext_state_info *info)
2060 const struct hns3_ethtool_link_ext_state_mapping *map;
2061 struct hnae3_handle *h = hns3_get_handle(netdev);
2065 if (netif_carrier_ok(netdev))
2068 if (!h->ae_algo->ops->get_link_diagnosis_info)
2071 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
2075 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
2076 map = &hns3_link_ext_state_map[i];
2077 if (map->status_code == status_code) {
2078 info->link_ext_state = map->link_ext_state;
2079 info->__link_ext_substate = map->link_ext_substate;
2087 static void hns3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2089 struct hnae3_handle *handle = hns3_get_handle(netdev);
2090 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
2091 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
2093 if (!hnae3_ae_dev_wol_supported(ae_dev))
2096 ops->get_wol(handle, wol);
2099 static int hns3_set_wol(struct net_device *netdev,
2100 struct ethtool_wolinfo *wol)
2102 struct hnae3_handle *handle = hns3_get_handle(netdev);
2103 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
2104 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
2106 if (!hnae3_ae_dev_wol_supported(ae_dev))
2109 return ops->set_wol(handle, wol);
2112 static const struct ethtool_ops hns3vf_ethtool_ops = {
2113 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2114 .supported_ring_params = HNS3_ETHTOOL_RING,
2115 .get_drvinfo = hns3_get_drvinfo,
2116 .get_ringparam = hns3_get_ringparam,
2117 .set_ringparam = hns3_set_ringparam,
2118 .get_strings = hns3_get_strings,
2119 .get_ethtool_stats = hns3_get_stats,
2120 .get_sset_count = hns3_get_sset_count,
2121 .get_rxnfc = hns3_get_rxnfc,
2122 .set_rxnfc = hns3_set_rxnfc,
2123 .get_rxfh_key_size = hns3_get_rss_key_size,
2124 .get_rxfh_indir_size = hns3_get_rss_indir_size,
2125 .get_rxfh = hns3_get_rss,
2126 .set_rxfh = hns3_set_rss,
2127 .get_link_ksettings = hns3_get_link_ksettings,
2128 .get_channels = hns3_get_channels,
2129 .set_channels = hns3_set_channels,
2130 .get_coalesce = hns3_get_coalesce,
2131 .set_coalesce = hns3_set_coalesce,
2132 .get_regs_len = hns3_get_regs_len,
2133 .get_regs = hns3_get_regs,
2134 .get_link = hns3_get_link,
2135 .get_msglevel = hns3_get_msglevel,
2136 .set_msglevel = hns3_set_msglevel,
2137 .get_priv_flags = hns3_get_priv_flags,
2138 .set_priv_flags = hns3_set_priv_flags,
2139 .get_tunable = hns3_get_tunable,
2140 .set_tunable = hns3_set_tunable,
2141 .reset = hns3_set_reset,
2144 static const struct ethtool_ops hns3_ethtool_ops = {
2145 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2146 .supported_ring_params = HNS3_ETHTOOL_RING,
2147 .cap_link_lanes_supported = true,
2148 .self_test = hns3_self_test,
2149 .get_drvinfo = hns3_get_drvinfo,
2150 .get_link = hns3_get_link,
2151 .get_ringparam = hns3_get_ringparam,
2152 .set_ringparam = hns3_set_ringparam,
2153 .get_pauseparam = hns3_get_pauseparam,
2154 .set_pauseparam = hns3_set_pauseparam,
2155 .get_strings = hns3_get_strings,
2156 .get_ethtool_stats = hns3_get_stats,
2157 .get_sset_count = hns3_get_sset_count,
2158 .get_rxnfc = hns3_get_rxnfc,
2159 .set_rxnfc = hns3_set_rxnfc,
2160 .get_rxfh_key_size = hns3_get_rss_key_size,
2161 .get_rxfh_indir_size = hns3_get_rss_indir_size,
2162 .get_rxfh = hns3_get_rss,
2163 .set_rxfh = hns3_set_rss,
2164 .get_link_ksettings = hns3_get_link_ksettings,
2165 .set_link_ksettings = hns3_set_link_ksettings,
2166 .nway_reset = hns3_nway_reset,
2167 .get_channels = hns3_get_channels,
2168 .set_channels = hns3_set_channels,
2169 .get_coalesce = hns3_get_coalesce,
2170 .set_coalesce = hns3_set_coalesce,
2171 .get_regs_len = hns3_get_regs_len,
2172 .get_regs = hns3_get_regs,
2173 .set_phys_id = hns3_set_phys_id,
2174 .get_msglevel = hns3_get_msglevel,
2175 .set_msglevel = hns3_set_msglevel,
2176 .get_fecparam = hns3_get_fecparam,
2177 .set_fecparam = hns3_set_fecparam,
2178 .get_fec_stats = hns3_get_fec_stats,
2179 .get_module_info = hns3_get_module_info,
2180 .get_module_eeprom = hns3_get_module_eeprom,
2181 .get_priv_flags = hns3_get_priv_flags,
2182 .set_priv_flags = hns3_set_priv_flags,
2183 .get_ts_info = hns3_get_ts_info,
2184 .get_tunable = hns3_get_tunable,
2185 .set_tunable = hns3_set_tunable,
2186 .reset = hns3_set_reset,
2187 .get_link_ext_state = hns3_get_link_ext_state,
2188 .get_wol = hns3_get_wol,
2189 .set_wol = hns3_set_wol,
2192 void hns3_ethtool_set_ops(struct net_device *netdev)
2194 struct hnae3_handle *h = hns3_get_handle(netdev);
2196 if (h->flags & HNAE3_SUPPORT_VF)
2197 netdev->ethtool_ops = &hns3vf_ethtool_ops;
2199 netdev->ethtool_ops = &hns3_ethtool_ops;