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_SELF_TEST_TYPE_NUM 4
73 #define HNS3_NIC_LB_TEST_PKT_NUM 1
74 #define HNS3_NIC_LB_TEST_RING_ID 0
75 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
76 #define HNS3_NIC_LB_SETUP_USEC 10000
78 /* Nic loopback test err */
79 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
80 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
81 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
83 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
85 struct hnae3_handle *h = hns3_get_handle(ndev);
86 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
89 if (!h->ae_algo->ops->set_loopback ||
90 !h->ae_algo->ops->set_promisc_mode)
94 case HNAE3_LOOP_SERIAL_SERDES:
95 case HNAE3_LOOP_PARALLEL_SERDES:
98 ret = h->ae_algo->ops->set_loopback(h, loop, en);
105 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
109 h->ae_algo->ops->set_promisc_mode(h, true, true);
111 /* recover promisc mode before loopback test */
112 hns3_request_update_promisc_mode(h);
117 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
119 struct hnae3_handle *h = hns3_get_handle(ndev);
122 ret = hns3_nic_reset_all_ring(h);
126 ret = hns3_lp_setup(ndev, loop_mode, true);
127 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
132 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
136 ret = hns3_lp_setup(ndev, loop_mode, false);
138 netdev_err(ndev, "lb_setup return error: %d\n", ret);
142 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
147 static void hns3_lp_setup_skb(struct sk_buff *skb)
149 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
151 struct net_device *ndev = skb->dev;
152 struct hnae3_handle *handle;
153 struct hnae3_ae_dev *ae_dev;
154 unsigned char *packet;
158 skb_reserve(skb, NET_IP_ALIGN);
159 ethh = skb_put(skb, sizeof(struct ethhdr));
160 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
162 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
164 /* The dst mac addr of loopback packet is the same as the host'
165 * mac addr, the SSU component may loop back the packet to host
166 * before the packet reaches mac or serdes, which will defect
167 * the purpose of mac or serdes selftest.
169 handle = hns3_get_handle(ndev);
170 ae_dev = pci_get_drvdata(handle->pdev);
171 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
172 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
173 eth_zero_addr(ethh->h_source);
174 ethh->h_proto = htons(ETH_P_ARP);
175 skb_reset_mac_header(skb);
177 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
178 packet[i] = (unsigned char)(i & 0xff);
181 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
184 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
185 unsigned char *packet = skb->data;
186 u32 len = skb_headlen(skb);
189 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
191 for (i = 0; i < len; i++)
192 if (packet[i] != (unsigned char)(i & 0xff))
195 /* The packet is correctly received */
196 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
197 tqp_vector->rx_group.total_packets++;
199 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
200 skb->data, len, true);
202 dev_kfree_skb_any(skb);
205 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
207 struct hnae3_handle *h = priv->ae_handle;
208 struct hnae3_knic_private_info *kinfo;
209 u32 i, rcv_good_pkt_total = 0;
212 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
213 struct hns3_enet_ring *ring = &priv->ring[i];
214 struct hns3_enet_ring_group *rx_group;
217 rx_group = &ring->tqp_vector->rx_group;
218 pre_rx_pkt = rx_group->total_packets;
221 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
224 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
225 rx_group->total_packets = pre_rx_pkt;
227 return rcv_good_pkt_total;
230 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
231 u32 end_ringid, u32 budget)
235 for (i = start_ringid; i <= end_ringid; i++) {
236 struct hns3_enet_ring *ring = &priv->ring[i];
238 hns3_clean_tx_ring(ring, 0);
243 * hns3_lp_run_test - run loopback test
245 * @mode: loopback type
247 * Return: %0 for success or a NIC loopback test error code on failure
249 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
251 struct hns3_nic_priv *priv = netdev_priv(ndev);
256 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
259 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
262 hns3_lp_setup_skb(skb);
263 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
266 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
270 tx_ret = hns3_nic_net_xmit(skb, ndev);
271 if (tx_ret == NETDEV_TX_OK) {
275 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
279 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
280 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
281 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
282 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
286 /* Allow 200 milliseconds for packets to go from Tx to Rx */
289 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
290 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
291 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
292 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
293 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
297 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
298 HNS3_NIC_LB_TEST_RING_ID,
299 HNS3_NIC_LB_TEST_PKT_NUM);
305 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
307 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
308 st_param[HNAE3_LOOP_APP][1] =
309 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
311 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
312 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
313 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
315 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
316 HNAE3_LOOP_PARALLEL_SERDES;
317 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
318 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
320 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
321 st_param[HNAE3_LOOP_PHY][1] =
322 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
325 static void hns3_selftest_prepare(struct net_device *ndev,
326 bool if_running, int (*st_param)[2])
328 struct hns3_nic_priv *priv = netdev_priv(ndev);
329 struct hnae3_handle *h = priv->ae_handle;
331 if (netif_msg_ifdown(h))
332 netdev_info(ndev, "self test start\n");
334 hns3_set_selftest_param(h, st_param);
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);
375 if (netif_msg_ifdown(h))
376 netdev_info(ndev, "self test end\n");
379 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
380 struct ethtool_test *eth_test, u64 *data)
385 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
386 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
391 data[test_index] = hns3_lp_up(ndev, loop_type);
392 if (!data[test_index])
393 data[test_index] = hns3_lp_run_test(ndev, loop_type);
395 hns3_lp_down(ndev, loop_type);
397 if (data[test_index])
398 eth_test->flags |= ETH_TEST_FL_FAILED;
405 * hns3_self_test - self test
407 * @eth_test: test cmd
410 static void hns3_self_test(struct net_device *ndev,
411 struct ethtool_test *eth_test, u64 *data)
413 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
414 bool if_running = netif_running(ndev);
416 if (hns3_nic_resetting(ndev)) {
417 netdev_err(ndev, "dev resetting!");
421 /* Only do offline selftest, or pass by default */
422 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
425 hns3_selftest_prepare(ndev, if_running, st_param);
426 hns3_do_selftest(ndev, st_param, eth_test, data);
427 hns3_selftest_restore(ndev, if_running);
430 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
433 struct hnae3_handle *handle = hns3_get_handle(netdev);
436 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
438 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
440 hns3_request_update_promisc_mode(handle);
443 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
444 { "limit_promisc", hns3_update_limit_promisc_mode }
447 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
449 struct hnae3_handle *h = hns3_get_handle(netdev);
450 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
452 if (!ops->get_sset_count)
457 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
458 ops->get_sset_count(h, stringset));
461 return ops->get_sset_count(h, stringset);
463 case ETH_SS_PRIV_FLAGS:
464 return HNAE3_PFLAG_MAX;
471 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
472 u32 stat_count, u32 num_tqps, const char *prefix)
474 #define MAX_PREFIX_SIZE (6 + 4)
479 for (i = 0; i < num_tqps; i++) {
480 for (j = 0; j < stat_count; j++) {
481 data[ETH_GSTRING_LEN - 1] = '\0';
483 /* first, prepend the prefix string */
484 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
486 size_left = (ETH_GSTRING_LEN - 1) - n1;
488 /* now, concatenate the stats string to it */
489 strncat(data, stats[j].stats_string, size_left);
490 data += ETH_GSTRING_LEN;
497 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
499 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
500 const char tx_prefix[] = "txq";
501 const char rx_prefix[] = "rxq";
503 /* get strings for Tx */
504 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
505 kinfo->num_tqps, tx_prefix);
507 /* get strings for Rx */
508 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
509 kinfo->num_tqps, rx_prefix);
514 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
516 struct hnae3_handle *h = hns3_get_handle(netdev);
517 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
518 char *buff = (char *)data;
521 if (!ops->get_strings)
526 buff = hns3_get_strings_tqps(h, buff);
527 ops->get_strings(h, stringset, (u8 *)buff);
530 ops->get_strings(h, stringset, data);
532 case ETH_SS_PRIV_FLAGS:
533 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
534 snprintf(buff, ETH_GSTRING_LEN, "%s",
535 hns3_priv_flags[i].name);
536 buff += ETH_GSTRING_LEN;
544 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
546 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
547 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
548 struct hns3_enet_ring *ring;
552 /* get stats for Tx */
553 for (i = 0; i < kinfo->num_tqps; i++) {
554 ring = &nic_priv->ring[i];
555 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
556 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
557 *data++ = *(u64 *)stat;
561 /* get stats for Rx */
562 for (i = 0; i < kinfo->num_tqps; i++) {
563 ring = &nic_priv->ring[i + kinfo->num_tqps];
564 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
565 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
566 *data++ = *(u64 *)stat;
573 /* hns3_get_stats - get detail statistics.
574 * @netdev: net device
575 * @stats: statistics info.
576 * @data: statistics data.
578 static void hns3_get_stats(struct net_device *netdev,
579 struct ethtool_stats *stats, u64 *data)
581 struct hnae3_handle *h = hns3_get_handle(netdev);
584 if (hns3_nic_resetting(netdev)) {
585 netdev_err(netdev, "dev resetting, could not get stats\n");
589 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
590 netdev_err(netdev, "could not get any statistics\n");
594 h->ae_algo->ops->update_stats(h, &netdev->stats);
596 /* get per-queue stats */
597 p = hns3_get_stats_tqps(h, p);
599 /* get MAC & other misc hardware stats */
600 h->ae_algo->ops->get_stats(h, p);
603 static void hns3_get_drvinfo(struct net_device *netdev,
604 struct ethtool_drvinfo *drvinfo)
606 struct hns3_nic_priv *priv = netdev_priv(netdev);
607 struct hnae3_handle *h = priv->ae_handle;
610 if (!h->ae_algo->ops->get_fw_version) {
611 netdev_err(netdev, "could not get fw version!\n");
615 strncpy(drvinfo->driver, dev_driver_string(&h->pdev->dev),
616 sizeof(drvinfo->driver));
617 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
619 strncpy(drvinfo->bus_info, pci_name(h->pdev),
620 sizeof(drvinfo->bus_info));
621 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
623 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
625 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
627 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
628 HNAE3_FW_VERSION_BYTE3_SHIFT),
629 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
630 HNAE3_FW_VERSION_BYTE2_SHIFT),
631 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
632 HNAE3_FW_VERSION_BYTE1_SHIFT),
633 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
634 HNAE3_FW_VERSION_BYTE0_SHIFT));
637 static u32 hns3_get_link(struct net_device *netdev)
639 struct hnae3_handle *h = hns3_get_handle(netdev);
641 if (h->ae_algo->ops->get_status)
642 return h->ae_algo->ops->get_status(h);
647 static void hns3_get_ringparam(struct net_device *netdev,
648 struct ethtool_ringparam *param,
649 struct kernel_ethtool_ringparam *kernel_param,
650 struct netlink_ext_ack *extack)
652 struct hns3_nic_priv *priv = netdev_priv(netdev);
653 struct hnae3_handle *h = priv->ae_handle;
654 int rx_queue_index = h->kinfo.num_tqps;
656 if (hns3_nic_resetting(netdev)) {
657 netdev_err(netdev, "dev resetting!");
661 param->tx_max_pending = HNS3_RING_MAX_PENDING;
662 param->rx_max_pending = HNS3_RING_MAX_PENDING;
664 param->tx_pending = priv->ring[0].desc_num;
665 param->rx_pending = priv->ring[rx_queue_index].desc_num;
666 kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size;
669 static void hns3_get_pauseparam(struct net_device *netdev,
670 struct ethtool_pauseparam *param)
672 struct hnae3_handle *h = hns3_get_handle(netdev);
673 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
675 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
678 if (h->ae_algo->ops->get_pauseparam)
679 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
680 ¶m->rx_pause, ¶m->tx_pause);
683 static int hns3_set_pauseparam(struct net_device *netdev,
684 struct ethtool_pauseparam *param)
686 struct hnae3_handle *h = hns3_get_handle(netdev);
687 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
689 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
692 netif_dbg(h, drv, netdev,
693 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
694 param->autoneg, param->rx_pause, param->tx_pause);
696 if (h->ae_algo->ops->set_pauseparam)
697 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
703 static void hns3_get_ksettings(struct hnae3_handle *h,
704 struct ethtool_link_ksettings *cmd)
706 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
708 /* 1.auto_neg & speed & duplex from cmd */
709 if (ops->get_ksettings_an_result)
710 ops->get_ksettings_an_result(h,
715 /* 2.get link mode */
716 if (ops->get_link_mode)
717 ops->get_link_mode(h,
718 cmd->link_modes.supported,
719 cmd->link_modes.advertising);
721 /* 3.mdix_ctrl&mdix get from phy reg */
722 if (ops->get_mdix_mode)
723 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
724 &cmd->base.eth_tp_mdix);
727 static int hns3_get_link_ksettings(struct net_device *netdev,
728 struct ethtool_link_ksettings *cmd)
730 struct hnae3_handle *h = hns3_get_handle(netdev);
731 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
732 const struct hnae3_ae_ops *ops;
737 ops = h->ae_algo->ops;
738 if (ops->get_media_type)
739 ops->get_media_type(h, &media_type, &module_type);
743 switch (media_type) {
744 case HNAE3_MEDIA_TYPE_NONE:
745 cmd->base.port = PORT_NONE;
746 hns3_get_ksettings(h, cmd);
748 case HNAE3_MEDIA_TYPE_FIBER:
749 if (module_type == HNAE3_MODULE_TYPE_CR)
750 cmd->base.port = PORT_DA;
752 cmd->base.port = PORT_FIBRE;
754 hns3_get_ksettings(h, cmd);
756 case HNAE3_MEDIA_TYPE_BACKPLANE:
757 cmd->base.port = PORT_NONE;
758 hns3_get_ksettings(h, cmd);
760 case HNAE3_MEDIA_TYPE_COPPER:
761 cmd->base.port = PORT_TP;
762 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
763 ops->get_phy_link_ksettings)
764 ops->get_phy_link_ksettings(h, cmd);
765 else if (!netdev->phydev)
766 hns3_get_ksettings(h, cmd);
768 phy_ethtool_ksettings_get(netdev->phydev, cmd);
772 netdev_warn(netdev, "Unknown media type");
777 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
779 link_stat = hns3_get_link(netdev);
781 cmd->base.speed = SPEED_UNKNOWN;
782 cmd->base.duplex = DUPLEX_UNKNOWN;
788 static int hns3_check_ksettings_param(const struct net_device *netdev,
789 const struct ethtool_link_ksettings *cmd)
791 struct hnae3_handle *handle = hns3_get_handle(netdev);
792 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
793 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
794 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
800 /* hw doesn't support use specified speed and duplex to negotiate,
801 * unnecessary to check them when autoneg on.
803 if (cmd->base.autoneg)
806 if (ops->get_ksettings_an_result) {
807 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
808 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
809 cmd->base.duplex == duplex)
813 if (ops->get_media_type)
814 ops->get_media_type(handle, &media_type, &module_type);
816 if (cmd->base.duplex == DUPLEX_HALF &&
817 media_type != HNAE3_MEDIA_TYPE_COPPER) {
819 "only copper port supports half duplex!");
823 if (ops->check_port_speed) {
824 ret = ops->check_port_speed(handle, cmd->base.speed);
826 netdev_err(netdev, "unsupported speed\n");
834 static int hns3_set_link_ksettings(struct net_device *netdev,
835 const struct ethtool_link_ksettings *cmd)
837 struct hnae3_handle *handle = hns3_get_handle(netdev);
838 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
839 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
842 /* Chip don't support this mode. */
843 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
846 netif_dbg(handle, drv, netdev,
847 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
848 netdev->phydev ? "phy" : "mac",
849 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
851 /* Only support ksettings_set for netdev with phy attached for now */
852 if (netdev->phydev) {
853 if (cmd->base.speed == SPEED_1000 &&
854 cmd->base.autoneg == AUTONEG_DISABLE)
857 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
858 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
859 ops->set_phy_link_ksettings) {
860 return ops->set_phy_link_ksettings(handle, cmd);
863 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
866 ret = hns3_check_ksettings_param(netdev, cmd);
870 if (ops->set_autoneg) {
871 ret = ops->set_autoneg(handle, cmd->base.autoneg);
876 /* hw doesn't support use specified speed and duplex to negotiate,
877 * ignore them when autoneg on.
879 if (cmd->base.autoneg) {
881 "autoneg is on, ignore the speed and duplex\n");
885 if (ops->cfg_mac_speed_dup_h)
886 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
892 static u32 hns3_get_rss_key_size(struct net_device *netdev)
894 struct hnae3_handle *h = hns3_get_handle(netdev);
896 if (!h->ae_algo->ops->get_rss_key_size)
899 return h->ae_algo->ops->get_rss_key_size(h);
902 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
904 struct hnae3_handle *h = hns3_get_handle(netdev);
905 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
907 return ae_dev->dev_specs.rss_ind_tbl_size;
910 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
913 struct hnae3_handle *h = hns3_get_handle(netdev);
915 if (!h->ae_algo->ops->get_rss)
918 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
921 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
922 const u8 *key, const u8 hfunc)
924 struct hnae3_handle *h = hns3_get_handle(netdev);
925 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
927 if (!h->ae_algo->ops->set_rss)
930 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
931 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
932 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
933 netdev_err(netdev, "hash func not supported\n");
939 "set rss failed for indir is empty\n");
943 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
946 static int hns3_get_rxnfc(struct net_device *netdev,
947 struct ethtool_rxnfc *cmd,
950 struct hnae3_handle *h = hns3_get_handle(netdev);
953 case ETHTOOL_GRXRINGS:
954 cmd->data = h->kinfo.num_tqps;
957 if (h->ae_algo->ops->get_rss_tuple)
958 return h->ae_algo->ops->get_rss_tuple(h, cmd);
960 case ETHTOOL_GRXCLSRLCNT:
961 if (h->ae_algo->ops->get_fd_rule_cnt)
962 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
964 case ETHTOOL_GRXCLSRULE:
965 if (h->ae_algo->ops->get_fd_rule_info)
966 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
968 case ETHTOOL_GRXCLSRLALL:
969 if (h->ae_algo->ops->get_fd_all_rules)
970 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
978 static const struct hns3_reset_type_map hns3_reset_type[] = {
979 {ETH_RESET_MGMT, HNAE3_IMP_RESET},
980 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
981 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
984 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
985 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
988 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
990 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
991 struct hnae3_handle *h = hns3_get_handle(netdev);
992 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
993 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
994 const struct hns3_reset_type_map *rst_type_map;
995 enum ethtool_reset_flags rst_flags;
998 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
1001 if (!ops->set_default_reset_request || !ops->reset_event)
1004 if (h->flags & HNAE3_SUPPORT_VF) {
1005 rst_type_map = hns3vf_reset_type;
1006 size = ARRAY_SIZE(hns3vf_reset_type);
1008 rst_type_map = hns3_reset_type;
1009 size = ARRAY_SIZE(hns3_reset_type);
1012 for (i = 0; i < size; i++) {
1013 if (rst_type_map[i].rst_flags == *flags) {
1014 rst_type = rst_type_map[i].rst_type;
1015 rst_flags = rst_type_map[i].rst_flags;
1020 if (rst_type == HNAE3_NONE_RESET ||
1021 (rst_type == HNAE3_IMP_RESET &&
1022 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1025 netdev_info(netdev, "Setting reset type %d\n", rst_type);
1027 ops->set_default_reset_request(ae_dev, rst_type);
1029 ops->reset_event(h->pdev, h);
1031 *flags &= ~rst_flags;
1036 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1037 u32 tx_desc_num, u32 rx_desc_num)
1039 struct hnae3_handle *h = priv->ae_handle;
1042 h->kinfo.num_tx_desc = tx_desc_num;
1043 h->kinfo.num_rx_desc = rx_desc_num;
1045 for (i = 0; i < h->kinfo.num_tqps; i++) {
1046 priv->ring[i].desc_num = tx_desc_num;
1047 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1051 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1053 struct hnae3_handle *handle = priv->ae_handle;
1054 struct hns3_enet_ring *tmp_rings;
1057 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1058 sizeof(struct hns3_enet_ring), GFP_KERNEL);
1062 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1063 memcpy(&tmp_rings[i], &priv->ring[i],
1064 sizeof(struct hns3_enet_ring));
1065 tmp_rings[i].skb = NULL;
1071 static int hns3_check_ringparam(struct net_device *ndev,
1072 struct ethtool_ringparam *param,
1073 struct kernel_ethtool_ringparam *kernel_param)
1075 #define RX_BUF_LEN_2K 2048
1076 #define RX_BUF_LEN_4K 4096
1077 if (hns3_nic_resetting(ndev))
1080 if (param->rx_mini_pending || param->rx_jumbo_pending)
1083 if (kernel_param->rx_buf_len != RX_BUF_LEN_2K &&
1084 kernel_param->rx_buf_len != RX_BUF_LEN_4K) {
1085 netdev_err(ndev, "Rx buf len only support 2048 and 4096\n");
1089 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1090 param->tx_pending < HNS3_RING_MIN_PENDING ||
1091 param->rx_pending > HNS3_RING_MAX_PENDING ||
1092 param->rx_pending < HNS3_RING_MIN_PENDING) {
1093 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1094 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1101 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len)
1103 struct hns3_nic_priv *priv = netdev_priv(ndev);
1104 struct hnae3_handle *h = priv->ae_handle;
1107 h->kinfo.rx_buf_len = rx_buf_len;
1109 for (i = 0; i < h->kinfo.num_tqps; i++) {
1110 h->kinfo.tqp[i]->buf_size = rx_buf_len;
1111 priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len;
1117 static int hns3_set_ringparam(struct net_device *ndev,
1118 struct ethtool_ringparam *param,
1119 struct kernel_ethtool_ringparam *kernel_param,
1120 struct netlink_ext_ack *extack)
1122 struct hns3_nic_priv *priv = netdev_priv(ndev);
1123 struct hnae3_handle *h = priv->ae_handle;
1124 struct hns3_enet_ring *tmp_rings;
1125 bool if_running = netif_running(ndev);
1126 u32 old_tx_desc_num, new_tx_desc_num;
1127 u32 old_rx_desc_num, new_rx_desc_num;
1128 u16 queue_num = h->kinfo.num_tqps;
1132 ret = hns3_check_ringparam(ndev, param, kernel_param);
1136 /* Hardware requires that its descriptors must be multiple of eight */
1137 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1138 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1139 old_tx_desc_num = priv->ring[0].desc_num;
1140 old_rx_desc_num = priv->ring[queue_num].desc_num;
1141 old_rx_buf_len = priv->ring[queue_num].buf_size;
1142 if (old_tx_desc_num == new_tx_desc_num &&
1143 old_rx_desc_num == new_rx_desc_num &&
1144 kernel_param->rx_buf_len == old_rx_buf_len)
1147 tmp_rings = hns3_backup_ringparam(priv);
1150 "backup ring param failed by allocating memory fail\n");
1155 "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %d to %d\n",
1156 old_tx_desc_num, old_rx_desc_num,
1157 new_tx_desc_num, new_rx_desc_num,
1158 old_rx_buf_len, kernel_param->rx_buf_len);
1161 ndev->netdev_ops->ndo_stop(ndev);
1163 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1164 hns3_change_rx_buf_len(ndev, kernel_param->rx_buf_len);
1165 ret = hns3_init_all_ring(priv);
1167 netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n",
1170 hns3_change_rx_buf_len(ndev, old_rx_buf_len);
1171 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1173 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1174 memcpy(&priv->ring[i], &tmp_rings[i],
1175 sizeof(struct hns3_enet_ring));
1177 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1178 hns3_fini_ring(&tmp_rings[i]);
1184 ret = ndev->netdev_ops->ndo_open(ndev);
1189 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1191 struct hnae3_handle *h = hns3_get_handle(netdev);
1195 if (h->ae_algo->ops->set_rss_tuple)
1196 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1198 case ETHTOOL_SRXCLSRLINS:
1199 if (h->ae_algo->ops->add_fd_entry)
1200 return h->ae_algo->ops->add_fd_entry(h, cmd);
1202 case ETHTOOL_SRXCLSRLDEL:
1203 if (h->ae_algo->ops->del_fd_entry)
1204 return h->ae_algo->ops->del_fd_entry(h, cmd);
1211 static int hns3_nway_reset(struct net_device *netdev)
1213 struct hnae3_handle *handle = hns3_get_handle(netdev);
1214 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1215 struct phy_device *phy = netdev->phydev;
1218 if (!netif_running(netdev))
1221 if (hns3_nic_resetting(netdev)) {
1222 netdev_err(netdev, "dev resetting!");
1226 if (!ops->get_autoneg || !ops->restart_autoneg)
1229 autoneg = ops->get_autoneg(handle);
1230 if (autoneg != AUTONEG_ENABLE) {
1232 "Autoneg is off, don't support to restart it\n");
1236 netif_dbg(handle, drv, netdev,
1237 "nway reset (using %s)\n", phy ? "phy" : "mac");
1240 return genphy_restart_aneg(phy);
1242 return ops->restart_autoneg(handle);
1245 static void hns3_get_channels(struct net_device *netdev,
1246 struct ethtool_channels *ch)
1248 struct hnae3_handle *h = hns3_get_handle(netdev);
1250 if (h->ae_algo->ops->get_channels)
1251 h->ae_algo->ops->get_channels(h, ch);
1254 static int hns3_get_coalesce(struct net_device *netdev,
1255 struct ethtool_coalesce *cmd,
1256 struct kernel_ethtool_coalesce *kernel_coal,
1257 struct netlink_ext_ack *extack)
1259 struct hns3_nic_priv *priv = netdev_priv(netdev);
1260 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1261 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1262 struct hnae3_handle *h = priv->ae_handle;
1264 if (hns3_nic_resetting(netdev))
1267 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1268 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1270 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1271 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1273 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1274 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1276 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1277 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1279 kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1280 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1281 kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1282 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1287 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1288 struct ethtool_coalesce *cmd)
1290 struct hnae3_handle *handle = hns3_get_handle(netdev);
1291 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1294 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1296 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1297 ae_dev->dev_specs.max_int_gl);
1301 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1303 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1304 ae_dev->dev_specs.max_int_gl);
1308 /* device version above V3(include V3), GL uses 1us unit,
1309 * so the round down is not needed.
1311 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1314 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1315 if (rx_gl != cmd->rx_coalesce_usecs) {
1317 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1318 cmd->rx_coalesce_usecs, rx_gl);
1321 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1322 if (tx_gl != cmd->tx_coalesce_usecs) {
1324 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1325 cmd->tx_coalesce_usecs, tx_gl);
1331 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1332 struct ethtool_coalesce *cmd)
1336 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1338 "tx_usecs_high must be same as rx_usecs_high.\n");
1342 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1344 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1349 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1350 if (rl != cmd->rx_coalesce_usecs_high) {
1352 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1353 cmd->rx_coalesce_usecs_high, rl);
1359 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1360 struct ethtool_coalesce *cmd)
1362 struct hnae3_handle *handle = hns3_get_handle(netdev);
1363 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1365 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1366 !ae_dev->dev_specs.int_ql_max) {
1367 netdev_err(netdev, "coalesced frames is not supported\n");
1371 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1372 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1374 "invalid coalesced_frames value, range is 0-%u\n",
1375 ae_dev->dev_specs.int_ql_max);
1382 static int hns3_check_coalesce_para(struct net_device *netdev,
1383 struct ethtool_coalesce *cmd)
1387 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1390 "Check gl coalesce param fail. ret = %d\n", ret);
1394 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1397 "Check rl coalesce param fail. ret = %d\n", ret);
1401 return hns3_check_ql_coalesce_param(netdev, cmd);
1404 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1405 struct ethtool_coalesce *cmd,
1408 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1409 struct hns3_nic_priv *priv = netdev_priv(netdev);
1410 struct hnae3_handle *h = priv->ae_handle;
1411 int queue_num = h->kinfo.num_tqps;
1413 tx_vector = priv->ring[queue].tqp_vector;
1414 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1416 tx_vector->tx_group.coal.adapt_enable =
1417 cmd->use_adaptive_tx_coalesce;
1418 rx_vector->rx_group.coal.adapt_enable =
1419 cmd->use_adaptive_rx_coalesce;
1421 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1422 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1424 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1425 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1427 hns3_set_vector_coalesce_tx_gl(tx_vector,
1428 tx_vector->tx_group.coal.int_gl);
1429 hns3_set_vector_coalesce_rx_gl(rx_vector,
1430 rx_vector->rx_group.coal.int_gl);
1432 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1433 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1435 if (tx_vector->tx_group.coal.ql_enable)
1436 hns3_set_vector_coalesce_tx_ql(tx_vector,
1437 tx_vector->tx_group.coal.int_ql);
1438 if (rx_vector->rx_group.coal.ql_enable)
1439 hns3_set_vector_coalesce_rx_ql(rx_vector,
1440 rx_vector->rx_group.coal.int_ql);
1443 static int hns3_set_coalesce(struct net_device *netdev,
1444 struct ethtool_coalesce *cmd,
1445 struct kernel_ethtool_coalesce *kernel_coal,
1446 struct netlink_ext_ack *extack)
1448 struct hnae3_handle *h = hns3_get_handle(netdev);
1449 struct hns3_nic_priv *priv = netdev_priv(netdev);
1450 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1451 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1452 u16 queue_num = h->kinfo.num_tqps;
1453 enum dim_cq_period_mode tx_mode;
1454 enum dim_cq_period_mode rx_mode;
1458 if (hns3_nic_resetting(netdev))
1461 ret = hns3_check_coalesce_para(netdev, cmd);
1465 h->kinfo.int_rl_setting =
1466 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1468 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1469 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1471 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1472 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1474 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1475 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1477 for (i = 0; i < queue_num; i++)
1478 hns3_set_coalesce_per_queue(netdev, cmd, i);
1480 tx_mode = kernel_coal->use_cqe_mode_tx ?
1481 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1482 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1483 rx_mode = kernel_coal->use_cqe_mode_rx ?
1484 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1485 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1486 hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1491 static int hns3_get_regs_len(struct net_device *netdev)
1493 struct hnae3_handle *h = hns3_get_handle(netdev);
1495 if (!h->ae_algo->ops->get_regs_len)
1498 return h->ae_algo->ops->get_regs_len(h);
1501 static void hns3_get_regs(struct net_device *netdev,
1502 struct ethtool_regs *cmd, void *data)
1504 struct hnae3_handle *h = hns3_get_handle(netdev);
1506 if (!h->ae_algo->ops->get_regs)
1509 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1512 static int hns3_set_phys_id(struct net_device *netdev,
1513 enum ethtool_phys_id_state state)
1515 struct hnae3_handle *h = hns3_get_handle(netdev);
1517 if (!h->ae_algo->ops->set_led_id)
1520 return h->ae_algo->ops->set_led_id(h, state);
1523 static u32 hns3_get_msglevel(struct net_device *netdev)
1525 struct hnae3_handle *h = hns3_get_handle(netdev);
1527 return h->msg_enable;
1530 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1532 struct hnae3_handle *h = hns3_get_handle(netdev);
1534 h->msg_enable = msg_level;
1537 /* Translate local fec value into ethtool value. */
1538 static unsigned int loc_to_eth_fec(u8 loc_fec)
1542 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1543 eth_fec |= ETHTOOL_FEC_AUTO;
1544 if (loc_fec & BIT(HNAE3_FEC_RS))
1545 eth_fec |= ETHTOOL_FEC_RS;
1546 if (loc_fec & BIT(HNAE3_FEC_BASER))
1547 eth_fec |= ETHTOOL_FEC_BASER;
1549 /* if nothing is set, then FEC is off */
1551 eth_fec = ETHTOOL_FEC_OFF;
1556 /* Translate ethtool fec value into local value. */
1557 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1561 if (eth_fec & ETHTOOL_FEC_OFF)
1564 if (eth_fec & ETHTOOL_FEC_AUTO)
1565 loc_fec |= BIT(HNAE3_FEC_AUTO);
1566 if (eth_fec & ETHTOOL_FEC_RS)
1567 loc_fec |= BIT(HNAE3_FEC_RS);
1568 if (eth_fec & ETHTOOL_FEC_BASER)
1569 loc_fec |= BIT(HNAE3_FEC_BASER);
1574 static int hns3_get_fecparam(struct net_device *netdev,
1575 struct ethtool_fecparam *fec)
1577 struct hnae3_handle *handle = hns3_get_handle(netdev);
1578 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1579 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1583 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1589 ops->get_fec(handle, &fec_ability, &fec_mode);
1591 fec->fec = loc_to_eth_fec(fec_ability);
1592 fec->active_fec = loc_to_eth_fec(fec_mode);
1597 static int hns3_set_fecparam(struct net_device *netdev,
1598 struct ethtool_fecparam *fec)
1600 struct hnae3_handle *handle = hns3_get_handle(netdev);
1601 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1602 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1605 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1610 fec_mode = eth_to_loc_fec(fec->fec);
1612 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1614 return ops->set_fec(handle, fec_mode);
1617 static int hns3_get_module_info(struct net_device *netdev,
1618 struct ethtool_modinfo *modinfo)
1620 #define HNS3_SFF_8636_V1_3 0x03
1622 struct hnae3_handle *handle = hns3_get_handle(netdev);
1623 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1624 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1625 struct hns3_sfp_type sfp_type;
1628 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1629 !ops->get_module_eeprom)
1632 memset(&sfp_type, 0, sizeof(sfp_type));
1633 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1638 switch (sfp_type.type) {
1639 case SFF8024_ID_SFP:
1640 modinfo->type = ETH_MODULE_SFF_8472;
1641 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1643 case SFF8024_ID_QSFP_8438:
1644 modinfo->type = ETH_MODULE_SFF_8436;
1645 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1647 case SFF8024_ID_QSFP_8436_8636:
1648 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1649 modinfo->type = ETH_MODULE_SFF_8436;
1650 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1652 modinfo->type = ETH_MODULE_SFF_8636;
1653 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1656 case SFF8024_ID_QSFP28_8636:
1657 modinfo->type = ETH_MODULE_SFF_8636;
1658 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1661 netdev_err(netdev, "Optical module unknown: %#x\n",
1669 static int hns3_get_module_eeprom(struct net_device *netdev,
1670 struct ethtool_eeprom *ee, u8 *data)
1672 struct hnae3_handle *handle = hns3_get_handle(netdev);
1673 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1674 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1676 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1677 !ops->get_module_eeprom)
1683 memset(data, 0, ee->len);
1685 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1688 static u32 hns3_get_priv_flags(struct net_device *netdev)
1690 struct hnae3_handle *handle = hns3_get_handle(netdev);
1692 return handle->priv_flags;
1695 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1699 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1700 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1701 netdev_err(h->netdev, "%s is unsupported\n",
1702 hns3_priv_flags[i].name);
1709 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1711 struct hnae3_handle *handle = hns3_get_handle(netdev);
1712 u32 changed = pflags ^ handle->priv_flags;
1716 ret = hns3_check_priv_flags(handle, changed);
1720 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1721 if (changed & BIT(i)) {
1722 bool enable = !(handle->priv_flags & BIT(i));
1725 handle->priv_flags |= BIT(i);
1727 handle->priv_flags &= ~BIT(i);
1728 hns3_priv_flags[i].handler(netdev, enable);
1735 static int hns3_get_tunable(struct net_device *netdev,
1736 const struct ethtool_tunable *tuna,
1739 struct hns3_nic_priv *priv = netdev_priv(netdev);
1740 struct hnae3_handle *h = priv->ae_handle;
1744 case ETHTOOL_TX_COPYBREAK:
1745 /* all the tx rings have the same tx_copybreak */
1746 *(u32 *)data = priv->tx_copybreak;
1748 case ETHTOOL_RX_COPYBREAK:
1749 *(u32 *)data = priv->rx_copybreak;
1751 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1752 *(u32 *)data = h->kinfo.tx_spare_buf_size;
1762 static int hns3_set_tx_spare_buf_size(struct net_device *netdev,
1765 struct hns3_nic_priv *priv = netdev_priv(netdev);
1766 struct hnae3_handle *h = priv->ae_handle;
1769 if (hns3_nic_resetting(netdev))
1772 h->kinfo.tx_spare_buf_size = data;
1774 ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
1778 ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1782 ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT);
1786 ret = hns3_reset_notify(h, HNAE3_UP_CLIENT);
1788 hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1793 static int hns3_set_tunable(struct net_device *netdev,
1794 const struct ethtool_tunable *tuna,
1797 struct hns3_nic_priv *priv = netdev_priv(netdev);
1798 u32 old_tx_spare_buf_size, new_tx_spare_buf_size;
1799 struct hnae3_handle *h = priv->ae_handle;
1803 case ETHTOOL_TX_COPYBREAK:
1804 priv->tx_copybreak = *(u32 *)data;
1806 for (i = 0; i < h->kinfo.num_tqps; i++)
1807 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1810 case ETHTOOL_RX_COPYBREAK:
1811 priv->rx_copybreak = *(u32 *)data;
1813 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1814 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1817 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1818 old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size;
1819 new_tx_spare_buf_size = *(u32 *)data;
1820 ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size);
1822 (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) {
1826 "change tx spare buf size fail, revert to old value\n");
1827 ret1 = hns3_set_tx_spare_buf_size(netdev,
1828 old_tx_spare_buf_size);
1831 "revert to old tx spare buf size fail\n");
1846 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
1847 ETHTOOL_COALESCE_USE_ADAPTIVE | \
1848 ETHTOOL_COALESCE_RX_USECS_HIGH | \
1849 ETHTOOL_COALESCE_TX_USECS_HIGH | \
1850 ETHTOOL_COALESCE_MAX_FRAMES | \
1851 ETHTOOL_COALESCE_USE_CQE)
1853 #define HNS3_ETHTOOL_RING ETHTOOL_RING_USE_RX_BUF_LEN
1855 static int hns3_get_ts_info(struct net_device *netdev,
1856 struct ethtool_ts_info *info)
1858 struct hnae3_handle *handle = hns3_get_handle(netdev);
1860 if (handle->ae_algo->ops->get_ts_info)
1861 return handle->ae_algo->ops->get_ts_info(handle, info);
1863 return ethtool_op_get_ts_info(netdev, info);
1866 static const struct hns3_ethtool_link_ext_state_mapping
1867 hns3_link_ext_state_map[] = {
1868 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1869 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1870 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1871 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1873 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1874 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1875 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1876 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1877 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1878 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1880 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1881 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1882 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1883 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1884 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1885 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1887 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1888 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1889 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1890 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1891 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1892 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1894 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1895 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1896 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1898 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1901 static int hns3_get_link_ext_state(struct net_device *netdev,
1902 struct ethtool_link_ext_state_info *info)
1904 const struct hns3_ethtool_link_ext_state_mapping *map;
1905 struct hnae3_handle *h = hns3_get_handle(netdev);
1909 if (netif_carrier_ok(netdev))
1912 if (!h->ae_algo->ops->get_link_diagnosis_info)
1915 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1919 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
1920 map = &hns3_link_ext_state_map[i];
1921 if (map->status_code == status_code) {
1922 info->link_ext_state = map->link_ext_state;
1923 info->__link_ext_substate = map->link_ext_substate;
1931 static const struct ethtool_ops hns3vf_ethtool_ops = {
1932 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1933 .supported_ring_params = HNS3_ETHTOOL_RING,
1934 .get_drvinfo = hns3_get_drvinfo,
1935 .get_ringparam = hns3_get_ringparam,
1936 .set_ringparam = hns3_set_ringparam,
1937 .get_strings = hns3_get_strings,
1938 .get_ethtool_stats = hns3_get_stats,
1939 .get_sset_count = hns3_get_sset_count,
1940 .get_rxnfc = hns3_get_rxnfc,
1941 .set_rxnfc = hns3_set_rxnfc,
1942 .get_rxfh_key_size = hns3_get_rss_key_size,
1943 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1944 .get_rxfh = hns3_get_rss,
1945 .set_rxfh = hns3_set_rss,
1946 .get_link_ksettings = hns3_get_link_ksettings,
1947 .get_channels = hns3_get_channels,
1948 .set_channels = hns3_set_channels,
1949 .get_coalesce = hns3_get_coalesce,
1950 .set_coalesce = hns3_set_coalesce,
1951 .get_regs_len = hns3_get_regs_len,
1952 .get_regs = hns3_get_regs,
1953 .get_link = hns3_get_link,
1954 .get_msglevel = hns3_get_msglevel,
1955 .set_msglevel = hns3_set_msglevel,
1956 .get_priv_flags = hns3_get_priv_flags,
1957 .set_priv_flags = hns3_set_priv_flags,
1958 .get_tunable = hns3_get_tunable,
1959 .set_tunable = hns3_set_tunable,
1960 .reset = hns3_set_reset,
1963 static const struct ethtool_ops hns3_ethtool_ops = {
1964 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1965 .supported_ring_params = HNS3_ETHTOOL_RING,
1966 .self_test = hns3_self_test,
1967 .get_drvinfo = hns3_get_drvinfo,
1968 .get_link = hns3_get_link,
1969 .get_ringparam = hns3_get_ringparam,
1970 .set_ringparam = hns3_set_ringparam,
1971 .get_pauseparam = hns3_get_pauseparam,
1972 .set_pauseparam = hns3_set_pauseparam,
1973 .get_strings = hns3_get_strings,
1974 .get_ethtool_stats = hns3_get_stats,
1975 .get_sset_count = hns3_get_sset_count,
1976 .get_rxnfc = hns3_get_rxnfc,
1977 .set_rxnfc = hns3_set_rxnfc,
1978 .get_rxfh_key_size = hns3_get_rss_key_size,
1979 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1980 .get_rxfh = hns3_get_rss,
1981 .set_rxfh = hns3_set_rss,
1982 .get_link_ksettings = hns3_get_link_ksettings,
1983 .set_link_ksettings = hns3_set_link_ksettings,
1984 .nway_reset = hns3_nway_reset,
1985 .get_channels = hns3_get_channels,
1986 .set_channels = hns3_set_channels,
1987 .get_coalesce = hns3_get_coalesce,
1988 .set_coalesce = hns3_set_coalesce,
1989 .get_regs_len = hns3_get_regs_len,
1990 .get_regs = hns3_get_regs,
1991 .set_phys_id = hns3_set_phys_id,
1992 .get_msglevel = hns3_get_msglevel,
1993 .set_msglevel = hns3_set_msglevel,
1994 .get_fecparam = hns3_get_fecparam,
1995 .set_fecparam = hns3_set_fecparam,
1996 .get_module_info = hns3_get_module_info,
1997 .get_module_eeprom = hns3_get_module_eeprom,
1998 .get_priv_flags = hns3_get_priv_flags,
1999 .set_priv_flags = hns3_set_priv_flags,
2000 .get_ts_info = hns3_get_ts_info,
2001 .get_tunable = hns3_get_tunable,
2002 .set_tunable = hns3_set_tunable,
2003 .reset = hns3_set_reset,
2004 .get_link_ext_state = hns3_get_link_ext_state,
2007 void hns3_ethtool_set_ops(struct net_device *netdev)
2009 struct hnae3_handle *h = hns3_get_handle(netdev);
2011 if (h->flags & HNAE3_SUPPORT_VF)
2012 netdev->ethtool_ops = &hns3vf_ethtool_ops;
2014 netdev->ethtool_ops = &hns3_ethtool_ops;