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("wake", restart_queue),
27 HNS3_TQP_STAT("busy", tx_busy),
28 HNS3_TQP_STAT("copy", tx_copy),
29 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
30 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
31 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
32 HNS3_TQP_STAT("tso_err", tx_tso_err),
33 HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
34 HNS3_TQP_STAT("hw_limitation", hw_limitation),
35 HNS3_TQP_STAT("bounce", tx_bounce),
36 HNS3_TQP_STAT("spare_full", tx_spare_full),
37 HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
38 HNS3_TQP_STAT("sgl", tx_sgl),
39 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
40 HNS3_TQP_STAT("map_sg_err", map_sg_err),
43 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
45 static const struct hns3_stats hns3_rxq_stats[] = {
46 /* Rx per-queue statistics */
47 HNS3_TQP_STAT("dropped", sw_err_cnt),
48 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
49 HNS3_TQP_STAT("packets", rx_pkts),
50 HNS3_TQP_STAT("bytes", rx_bytes),
51 HNS3_TQP_STAT("errors", rx_err_cnt),
52 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
53 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
54 HNS3_TQP_STAT("err_bd_num", err_bd_num),
55 HNS3_TQP_STAT("l2_err", l2_err),
56 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
57 HNS3_TQP_STAT("csum_complete", csum_complete),
58 HNS3_TQP_STAT("multicast", rx_multicast),
59 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
60 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
61 HNS3_TQP_STAT("frag_alloc", frag_alloc),
64 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
66 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
68 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
70 #define HNS3_SELF_TEST_TYPE_NUM 4
71 #define HNS3_NIC_LB_TEST_PKT_NUM 1
72 #define HNS3_NIC_LB_TEST_RING_ID 0
73 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
74 #define HNS3_NIC_LB_SETUP_USEC 10000
76 /* Nic loopback test err */
77 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
78 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
79 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
81 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
83 struct hnae3_handle *h = hns3_get_handle(ndev);
84 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
87 if (!h->ae_algo->ops->set_loopback ||
88 !h->ae_algo->ops->set_promisc_mode)
92 case HNAE3_LOOP_SERIAL_SERDES:
93 case HNAE3_LOOP_PARALLEL_SERDES:
96 ret = h->ae_algo->ops->set_loopback(h, loop, en);
103 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
107 h->ae_algo->ops->set_promisc_mode(h, true, true);
109 /* recover promisc mode before loopback test */
110 hns3_request_update_promisc_mode(h);
115 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
117 struct hnae3_handle *h = hns3_get_handle(ndev);
120 ret = hns3_nic_reset_all_ring(h);
124 ret = hns3_lp_setup(ndev, loop_mode, true);
125 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
130 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
134 ret = hns3_lp_setup(ndev, loop_mode, false);
136 netdev_err(ndev, "lb_setup return error: %d\n", ret);
140 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
145 static void hns3_lp_setup_skb(struct sk_buff *skb)
147 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
149 struct net_device *ndev = skb->dev;
150 struct hnae3_handle *handle;
151 struct hnae3_ae_dev *ae_dev;
152 unsigned char *packet;
156 skb_reserve(skb, NET_IP_ALIGN);
157 ethh = skb_put(skb, sizeof(struct ethhdr));
158 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
160 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
162 /* The dst mac addr of loopback packet is the same as the host'
163 * mac addr, the SSU component may loop back the packet to host
164 * before the packet reaches mac or serdes, which will defect
165 * the purpose of mac or serdes selftest.
167 handle = hns3_get_handle(ndev);
168 ae_dev = pci_get_drvdata(handle->pdev);
169 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
170 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
171 eth_zero_addr(ethh->h_source);
172 ethh->h_proto = htons(ETH_P_ARP);
173 skb_reset_mac_header(skb);
175 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
176 packet[i] = (unsigned char)(i & 0xff);
179 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
182 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
183 unsigned char *packet = skb->data;
184 u32 len = skb_headlen(skb);
187 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
189 for (i = 0; i < len; i++)
190 if (packet[i] != (unsigned char)(i & 0xff))
193 /* The packet is correctly received */
194 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
195 tqp_vector->rx_group.total_packets++;
197 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
198 skb->data, len, true);
200 dev_kfree_skb_any(skb);
203 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
205 struct hnae3_handle *h = priv->ae_handle;
206 struct hnae3_knic_private_info *kinfo;
207 u32 i, rcv_good_pkt_total = 0;
210 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
211 struct hns3_enet_ring *ring = &priv->ring[i];
212 struct hns3_enet_ring_group *rx_group;
215 rx_group = &ring->tqp_vector->rx_group;
216 pre_rx_pkt = rx_group->total_packets;
219 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
222 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
223 rx_group->total_packets = pre_rx_pkt;
225 return rcv_good_pkt_total;
228 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
229 u32 end_ringid, u32 budget)
233 for (i = start_ringid; i <= end_ringid; i++) {
234 struct hns3_enet_ring *ring = &priv->ring[i];
236 hns3_clean_tx_ring(ring, 0);
241 * hns3_lp_run_test - run loopback test
243 * @mode: loopback type
245 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
247 struct hns3_nic_priv *priv = netdev_priv(ndev);
252 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
255 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
258 hns3_lp_setup_skb(skb);
259 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
262 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
266 tx_ret = hns3_nic_net_xmit(skb, ndev);
267 if (tx_ret == NETDEV_TX_OK) {
271 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
275 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
276 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
277 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
278 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
282 /* Allow 200 milliseconds for packets to go from Tx to Rx */
285 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
286 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
287 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
288 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
289 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
293 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
294 HNS3_NIC_LB_TEST_RING_ID,
295 HNS3_NIC_LB_TEST_PKT_NUM);
301 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
303 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
304 st_param[HNAE3_LOOP_APP][1] =
305 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
307 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
308 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
309 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
311 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
312 HNAE3_LOOP_PARALLEL_SERDES;
313 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
314 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
316 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
317 st_param[HNAE3_LOOP_PHY][1] =
318 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
321 static void hns3_selftest_prepare(struct net_device *ndev,
322 bool if_running, int (*st_param)[2])
324 struct hns3_nic_priv *priv = netdev_priv(ndev);
325 struct hnae3_handle *h = priv->ae_handle;
327 if (netif_msg_ifdown(h))
328 netdev_info(ndev, "self test start\n");
330 hns3_set_selftest_param(h, st_param);
333 ndev->netdev_ops->ndo_stop(ndev);
335 #if IS_ENABLED(CONFIG_VLAN_8021Q)
336 /* Disable the vlan filter for selftest does not support it */
337 if (h->ae_algo->ops->enable_vlan_filter &&
338 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
339 h->ae_algo->ops->enable_vlan_filter(h, false);
342 /* Tell firmware to stop mac autoneg before loopback test start,
343 * otherwise loopback test may be failed when the port is still
346 if (h->ae_algo->ops->halt_autoneg)
347 h->ae_algo->ops->halt_autoneg(h, true);
349 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
352 static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
354 struct hns3_nic_priv *priv = netdev_priv(ndev);
355 struct hnae3_handle *h = priv->ae_handle;
357 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
359 if (h->ae_algo->ops->halt_autoneg)
360 h->ae_algo->ops->halt_autoneg(h, false);
362 #if IS_ENABLED(CONFIG_VLAN_8021Q)
363 if (h->ae_algo->ops->enable_vlan_filter &&
364 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
365 h->ae_algo->ops->enable_vlan_filter(h, true);
369 ndev->netdev_ops->ndo_open(ndev);
371 if (netif_msg_ifdown(h))
372 netdev_info(ndev, "self test end\n");
375 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
376 struct ethtool_test *eth_test, u64 *data)
381 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
382 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
387 data[test_index] = hns3_lp_up(ndev, loop_type);
388 if (!data[test_index])
389 data[test_index] = hns3_lp_run_test(ndev, loop_type);
391 hns3_lp_down(ndev, loop_type);
393 if (data[test_index])
394 eth_test->flags |= ETH_TEST_FL_FAILED;
401 * hns3_nic_self_test - self test
403 * @eth_test: test cmd
406 static void hns3_self_test(struct net_device *ndev,
407 struct ethtool_test *eth_test, u64 *data)
409 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
410 bool if_running = netif_running(ndev);
412 if (hns3_nic_resetting(ndev)) {
413 netdev_err(ndev, "dev resetting!");
417 /* Only do offline selftest, or pass by default */
418 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
421 hns3_selftest_prepare(ndev, if_running, st_param);
422 hns3_do_selftest(ndev, st_param, eth_test, data);
423 hns3_selftest_restore(ndev, if_running);
426 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
429 struct hnae3_handle *handle = hns3_get_handle(netdev);
432 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
434 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
436 hns3_request_update_promisc_mode(handle);
439 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
440 { "limit_promisc", hns3_update_limit_promisc_mode }
443 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
445 struct hnae3_handle *h = hns3_get_handle(netdev);
446 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
448 if (!ops->get_sset_count)
453 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
454 ops->get_sset_count(h, stringset));
457 return ops->get_sset_count(h, stringset);
459 case ETH_SS_PRIV_FLAGS:
460 return HNAE3_PFLAG_MAX;
467 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
468 u32 stat_count, u32 num_tqps, const char *prefix)
470 #define MAX_PREFIX_SIZE (6 + 4)
475 for (i = 0; i < num_tqps; i++) {
476 for (j = 0; j < stat_count; j++) {
477 data[ETH_GSTRING_LEN - 1] = '\0';
479 /* first, prepend the prefix string */
480 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
482 size_left = (ETH_GSTRING_LEN - 1) - n1;
484 /* now, concatenate the stats string to it */
485 strncat(data, stats[j].stats_string, size_left);
486 data += ETH_GSTRING_LEN;
493 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
495 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
496 const char tx_prefix[] = "txq";
497 const char rx_prefix[] = "rxq";
499 /* get strings for Tx */
500 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
501 kinfo->num_tqps, tx_prefix);
503 /* get strings for Rx */
504 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
505 kinfo->num_tqps, rx_prefix);
510 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
512 struct hnae3_handle *h = hns3_get_handle(netdev);
513 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
514 char *buff = (char *)data;
517 if (!ops->get_strings)
522 buff = hns3_get_strings_tqps(h, buff);
523 ops->get_strings(h, stringset, (u8 *)buff);
526 ops->get_strings(h, stringset, data);
528 case ETH_SS_PRIV_FLAGS:
529 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
530 snprintf(buff, ETH_GSTRING_LEN, "%s",
531 hns3_priv_flags[i].name);
532 buff += ETH_GSTRING_LEN;
540 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
542 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
543 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
544 struct hns3_enet_ring *ring;
548 /* get stats for Tx */
549 for (i = 0; i < kinfo->num_tqps; i++) {
550 ring = &nic_priv->ring[i];
551 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
552 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
553 *data++ = *(u64 *)stat;
557 /* get stats for Rx */
558 for (i = 0; i < kinfo->num_tqps; i++) {
559 ring = &nic_priv->ring[i + kinfo->num_tqps];
560 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
561 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
562 *data++ = *(u64 *)stat;
569 /* hns3_get_stats - get detail statistics.
570 * @netdev: net device
571 * @stats: statistics info.
572 * @data: statistics data.
574 static void hns3_get_stats(struct net_device *netdev,
575 struct ethtool_stats *stats, u64 *data)
577 struct hnae3_handle *h = hns3_get_handle(netdev);
580 if (hns3_nic_resetting(netdev)) {
581 netdev_err(netdev, "dev resetting, could not get stats\n");
585 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
586 netdev_err(netdev, "could not get any statistics\n");
590 h->ae_algo->ops->update_stats(h, &netdev->stats);
592 /* get per-queue stats */
593 p = hns3_get_stats_tqps(h, p);
595 /* get MAC & other misc hardware stats */
596 h->ae_algo->ops->get_stats(h, p);
599 static void hns3_get_drvinfo(struct net_device *netdev,
600 struct ethtool_drvinfo *drvinfo)
602 struct hns3_nic_priv *priv = netdev_priv(netdev);
603 struct hnae3_handle *h = priv->ae_handle;
606 if (!h->ae_algo->ops->get_fw_version) {
607 netdev_err(netdev, "could not get fw version!\n");
611 strncpy(drvinfo->driver, h->pdev->driver->name,
612 sizeof(drvinfo->driver));
613 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
615 strncpy(drvinfo->bus_info, pci_name(h->pdev),
616 sizeof(drvinfo->bus_info));
617 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
619 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
621 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
623 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
624 HNAE3_FW_VERSION_BYTE3_SHIFT),
625 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
626 HNAE3_FW_VERSION_BYTE2_SHIFT),
627 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
628 HNAE3_FW_VERSION_BYTE1_SHIFT),
629 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
630 HNAE3_FW_VERSION_BYTE0_SHIFT));
633 static u32 hns3_get_link(struct net_device *netdev)
635 struct hnae3_handle *h = hns3_get_handle(netdev);
637 if (h->ae_algo->ops->get_status)
638 return h->ae_algo->ops->get_status(h);
643 static void hns3_get_ringparam(struct net_device *netdev,
644 struct ethtool_ringparam *param)
646 struct hns3_nic_priv *priv = netdev_priv(netdev);
647 struct hnae3_handle *h = priv->ae_handle;
648 int queue_num = h->kinfo.num_tqps;
650 if (hns3_nic_resetting(netdev)) {
651 netdev_err(netdev, "dev resetting!");
655 param->tx_max_pending = HNS3_RING_MAX_PENDING;
656 param->rx_max_pending = HNS3_RING_MAX_PENDING;
658 param->tx_pending = priv->ring[0].desc_num;
659 param->rx_pending = priv->ring[queue_num].desc_num;
662 static void hns3_get_pauseparam(struct net_device *netdev,
663 struct ethtool_pauseparam *param)
665 struct hnae3_handle *h = hns3_get_handle(netdev);
666 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
668 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
671 if (h->ae_algo->ops->get_pauseparam)
672 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
673 ¶m->rx_pause, ¶m->tx_pause);
676 static int hns3_set_pauseparam(struct net_device *netdev,
677 struct ethtool_pauseparam *param)
679 struct hnae3_handle *h = hns3_get_handle(netdev);
680 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
682 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
685 netif_dbg(h, drv, netdev,
686 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
687 param->autoneg, param->rx_pause, param->tx_pause);
689 if (h->ae_algo->ops->set_pauseparam)
690 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
696 static void hns3_get_ksettings(struct hnae3_handle *h,
697 struct ethtool_link_ksettings *cmd)
699 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
701 /* 1.auto_neg & speed & duplex from cmd */
702 if (ops->get_ksettings_an_result)
703 ops->get_ksettings_an_result(h,
708 /* 2.get link mode */
709 if (ops->get_link_mode)
710 ops->get_link_mode(h,
711 cmd->link_modes.supported,
712 cmd->link_modes.advertising);
714 /* 3.mdix_ctrl&mdix get from phy reg */
715 if (ops->get_mdix_mode)
716 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
717 &cmd->base.eth_tp_mdix);
720 static int hns3_get_link_ksettings(struct net_device *netdev,
721 struct ethtool_link_ksettings *cmd)
723 struct hnae3_handle *h = hns3_get_handle(netdev);
724 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
725 const struct hnae3_ae_ops *ops;
730 ops = h->ae_algo->ops;
731 if (ops->get_media_type)
732 ops->get_media_type(h, &media_type, &module_type);
736 switch (media_type) {
737 case HNAE3_MEDIA_TYPE_NONE:
738 cmd->base.port = PORT_NONE;
739 hns3_get_ksettings(h, cmd);
741 case HNAE3_MEDIA_TYPE_FIBER:
742 if (module_type == HNAE3_MODULE_TYPE_CR)
743 cmd->base.port = PORT_DA;
745 cmd->base.port = PORT_FIBRE;
747 hns3_get_ksettings(h, cmd);
749 case HNAE3_MEDIA_TYPE_BACKPLANE:
750 cmd->base.port = PORT_NONE;
751 hns3_get_ksettings(h, cmd);
753 case HNAE3_MEDIA_TYPE_COPPER:
754 cmd->base.port = PORT_TP;
755 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
756 ops->get_phy_link_ksettings)
757 ops->get_phy_link_ksettings(h, cmd);
758 else if (!netdev->phydev)
759 hns3_get_ksettings(h, cmd);
761 phy_ethtool_ksettings_get(netdev->phydev, cmd);
765 netdev_warn(netdev, "Unknown media type");
770 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
772 link_stat = hns3_get_link(netdev);
774 cmd->base.speed = SPEED_UNKNOWN;
775 cmd->base.duplex = DUPLEX_UNKNOWN;
781 static int hns3_check_ksettings_param(const struct net_device *netdev,
782 const struct ethtool_link_ksettings *cmd)
784 struct hnae3_handle *handle = hns3_get_handle(netdev);
785 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
786 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
787 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
793 /* hw doesn't support use specified speed and duplex to negotiate,
794 * unnecessary to check them when autoneg on.
796 if (cmd->base.autoneg)
799 if (ops->get_ksettings_an_result) {
800 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
801 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
802 cmd->base.duplex == duplex)
806 if (ops->get_media_type)
807 ops->get_media_type(handle, &media_type, &module_type);
809 if (cmd->base.duplex == DUPLEX_HALF &&
810 media_type != HNAE3_MEDIA_TYPE_COPPER) {
812 "only copper port supports half duplex!");
816 if (ops->check_port_speed) {
817 ret = ops->check_port_speed(handle, cmd->base.speed);
819 netdev_err(netdev, "unsupported speed\n");
827 static int hns3_set_link_ksettings(struct net_device *netdev,
828 const struct ethtool_link_ksettings *cmd)
830 struct hnae3_handle *handle = hns3_get_handle(netdev);
831 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
832 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
835 /* Chip don't support this mode. */
836 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
839 netif_dbg(handle, drv, netdev,
840 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
841 netdev->phydev ? "phy" : "mac",
842 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
844 /* Only support ksettings_set for netdev with phy attached for now */
845 if (netdev->phydev) {
846 if (cmd->base.speed == SPEED_1000 &&
847 cmd->base.autoneg == AUTONEG_DISABLE)
850 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
851 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
852 ops->set_phy_link_ksettings) {
853 return ops->set_phy_link_ksettings(handle, cmd);
856 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
859 ret = hns3_check_ksettings_param(netdev, cmd);
863 if (ops->set_autoneg) {
864 ret = ops->set_autoneg(handle, cmd->base.autoneg);
869 /* hw doesn't support use specified speed and duplex to negotiate,
870 * ignore them when autoneg on.
872 if (cmd->base.autoneg) {
874 "autoneg is on, ignore the speed and duplex\n");
878 if (ops->cfg_mac_speed_dup_h)
879 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
885 static u32 hns3_get_rss_key_size(struct net_device *netdev)
887 struct hnae3_handle *h = hns3_get_handle(netdev);
889 if (!h->ae_algo->ops->get_rss_key_size)
892 return h->ae_algo->ops->get_rss_key_size(h);
895 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
897 struct hnae3_handle *h = hns3_get_handle(netdev);
898 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
900 return ae_dev->dev_specs.rss_ind_tbl_size;
903 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
906 struct hnae3_handle *h = hns3_get_handle(netdev);
908 if (!h->ae_algo->ops->get_rss)
911 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
914 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
915 const u8 *key, const u8 hfunc)
917 struct hnae3_handle *h = hns3_get_handle(netdev);
918 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
920 if (!h->ae_algo->ops->set_rss)
923 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
924 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
925 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
926 netdev_err(netdev, "hash func not supported\n");
932 "set rss failed for indir is empty\n");
936 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
939 static int hns3_get_rxnfc(struct net_device *netdev,
940 struct ethtool_rxnfc *cmd,
943 struct hnae3_handle *h = hns3_get_handle(netdev);
946 case ETHTOOL_GRXRINGS:
947 cmd->data = h->kinfo.num_tqps;
950 if (h->ae_algo->ops->get_rss_tuple)
951 return h->ae_algo->ops->get_rss_tuple(h, cmd);
953 case ETHTOOL_GRXCLSRLCNT:
954 if (h->ae_algo->ops->get_fd_rule_cnt)
955 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
957 case ETHTOOL_GRXCLSRULE:
958 if (h->ae_algo->ops->get_fd_rule_info)
959 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
961 case ETHTOOL_GRXCLSRLALL:
962 if (h->ae_algo->ops->get_fd_all_rules)
963 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
971 static const struct hns3_reset_type_map hns3_reset_type[] = {
972 {ETH_RESET_MGMT, HNAE3_IMP_RESET},
973 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
974 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
977 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
978 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
981 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
983 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
984 struct hnae3_handle *h = hns3_get_handle(netdev);
985 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
986 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
987 const struct hns3_reset_type_map *rst_type_map;
990 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
993 if (!ops->set_default_reset_request || !ops->reset_event)
996 if (h->flags & HNAE3_SUPPORT_VF) {
997 rst_type_map = hns3vf_reset_type;
998 size = ARRAY_SIZE(hns3vf_reset_type);
1000 rst_type_map = hns3_reset_type;
1001 size = ARRAY_SIZE(hns3_reset_type);
1004 for (i = 0; i < size; i++) {
1005 if (rst_type_map[i].rst_flags == *flags) {
1006 rst_type = rst_type_map[i].rst_type;
1011 if (rst_type == HNAE3_NONE_RESET ||
1012 (rst_type == HNAE3_IMP_RESET &&
1013 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1016 netdev_info(netdev, "Setting reset type %d\n", rst_type);
1018 ops->set_default_reset_request(ae_dev, rst_type);
1020 ops->reset_event(h->pdev, h);
1025 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1026 u32 tx_desc_num, u32 rx_desc_num)
1028 struct hnae3_handle *h = priv->ae_handle;
1031 h->kinfo.num_tx_desc = tx_desc_num;
1032 h->kinfo.num_rx_desc = rx_desc_num;
1034 for (i = 0; i < h->kinfo.num_tqps; i++) {
1035 priv->ring[i].desc_num = tx_desc_num;
1036 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1040 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1042 struct hnae3_handle *handle = priv->ae_handle;
1043 struct hns3_enet_ring *tmp_rings;
1046 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1047 sizeof(struct hns3_enet_ring), GFP_KERNEL);
1051 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1052 memcpy(&tmp_rings[i], &priv->ring[i],
1053 sizeof(struct hns3_enet_ring));
1054 tmp_rings[i].skb = NULL;
1060 static int hns3_check_ringparam(struct net_device *ndev,
1061 struct ethtool_ringparam *param)
1063 if (hns3_nic_resetting(ndev))
1066 if (param->rx_mini_pending || param->rx_jumbo_pending)
1069 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1070 param->tx_pending < HNS3_RING_MIN_PENDING ||
1071 param->rx_pending > HNS3_RING_MAX_PENDING ||
1072 param->rx_pending < HNS3_RING_MIN_PENDING) {
1073 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1074 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1081 static int hns3_set_ringparam(struct net_device *ndev,
1082 struct ethtool_ringparam *param)
1084 struct hns3_nic_priv *priv = netdev_priv(ndev);
1085 struct hnae3_handle *h = priv->ae_handle;
1086 struct hns3_enet_ring *tmp_rings;
1087 bool if_running = netif_running(ndev);
1088 u32 old_tx_desc_num, new_tx_desc_num;
1089 u32 old_rx_desc_num, new_rx_desc_num;
1090 u16 queue_num = h->kinfo.num_tqps;
1093 ret = hns3_check_ringparam(ndev, param);
1097 /* Hardware requires that its descriptors must be multiple of eight */
1098 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1099 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1100 old_tx_desc_num = priv->ring[0].desc_num;
1101 old_rx_desc_num = priv->ring[queue_num].desc_num;
1102 if (old_tx_desc_num == new_tx_desc_num &&
1103 old_rx_desc_num == new_rx_desc_num)
1106 tmp_rings = hns3_backup_ringparam(priv);
1109 "backup ring param failed by allocating memory fail\n");
1114 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1115 old_tx_desc_num, old_rx_desc_num,
1116 new_tx_desc_num, new_rx_desc_num);
1119 ndev->netdev_ops->ndo_stop(ndev);
1121 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1122 ret = hns3_init_all_ring(priv);
1124 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1127 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1129 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1130 memcpy(&priv->ring[i], &tmp_rings[i],
1131 sizeof(struct hns3_enet_ring));
1133 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1134 hns3_fini_ring(&tmp_rings[i]);
1140 ret = ndev->netdev_ops->ndo_open(ndev);
1145 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1147 struct hnae3_handle *h = hns3_get_handle(netdev);
1151 if (h->ae_algo->ops->set_rss_tuple)
1152 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1154 case ETHTOOL_SRXCLSRLINS:
1155 if (h->ae_algo->ops->add_fd_entry)
1156 return h->ae_algo->ops->add_fd_entry(h, cmd);
1158 case ETHTOOL_SRXCLSRLDEL:
1159 if (h->ae_algo->ops->del_fd_entry)
1160 return h->ae_algo->ops->del_fd_entry(h, cmd);
1167 static int hns3_nway_reset(struct net_device *netdev)
1169 struct hnae3_handle *handle = hns3_get_handle(netdev);
1170 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1171 struct phy_device *phy = netdev->phydev;
1174 if (!netif_running(netdev))
1177 if (hns3_nic_resetting(netdev)) {
1178 netdev_err(netdev, "dev resetting!");
1182 if (!ops->get_autoneg || !ops->restart_autoneg)
1185 autoneg = ops->get_autoneg(handle);
1186 if (autoneg != AUTONEG_ENABLE) {
1188 "Autoneg is off, don't support to restart it\n");
1192 netif_dbg(handle, drv, netdev,
1193 "nway reset (using %s)\n", phy ? "phy" : "mac");
1196 return genphy_restart_aneg(phy);
1198 return ops->restart_autoneg(handle);
1201 static void hns3_get_channels(struct net_device *netdev,
1202 struct ethtool_channels *ch)
1204 struct hnae3_handle *h = hns3_get_handle(netdev);
1206 if (h->ae_algo->ops->get_channels)
1207 h->ae_algo->ops->get_channels(h, ch);
1210 static int hns3_get_coalesce(struct net_device *netdev,
1211 struct ethtool_coalesce *cmd,
1212 struct kernel_ethtool_coalesce *kernel_coal,
1213 struct netlink_ext_ack *extack)
1215 struct hns3_nic_priv *priv = netdev_priv(netdev);
1216 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1217 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1218 struct hnae3_handle *h = priv->ae_handle;
1220 if (hns3_nic_resetting(netdev))
1223 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1224 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1226 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1227 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1229 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1230 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1232 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1233 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1235 kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1236 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1237 kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1238 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1243 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1244 struct ethtool_coalesce *cmd)
1246 struct hnae3_handle *handle = hns3_get_handle(netdev);
1247 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1250 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1252 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1253 ae_dev->dev_specs.max_int_gl);
1257 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1259 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1260 ae_dev->dev_specs.max_int_gl);
1264 /* device version above V3(include V3), GL uses 1us unit,
1265 * so the round down is not needed.
1267 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1270 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1271 if (rx_gl != cmd->rx_coalesce_usecs) {
1273 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1274 cmd->rx_coalesce_usecs, rx_gl);
1277 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1278 if (tx_gl != cmd->tx_coalesce_usecs) {
1280 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1281 cmd->tx_coalesce_usecs, tx_gl);
1287 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1288 struct ethtool_coalesce *cmd)
1292 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1294 "tx_usecs_high must be same as rx_usecs_high.\n");
1298 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1300 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1305 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1306 if (rl != cmd->rx_coalesce_usecs_high) {
1308 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1309 cmd->rx_coalesce_usecs_high, rl);
1315 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1316 struct ethtool_coalesce *cmd)
1318 struct hnae3_handle *handle = hns3_get_handle(netdev);
1319 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1321 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1322 !ae_dev->dev_specs.int_ql_max) {
1323 netdev_err(netdev, "coalesced frames is not supported\n");
1327 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1328 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1330 "invalid coalesced_frames value, range is 0-%u\n",
1331 ae_dev->dev_specs.int_ql_max);
1338 static int hns3_check_coalesce_para(struct net_device *netdev,
1339 struct ethtool_coalesce *cmd)
1343 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1346 "Check gl coalesce param fail. ret = %d\n", ret);
1350 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1353 "Check rl coalesce param fail. ret = %d\n", ret);
1357 return hns3_check_ql_coalesce_param(netdev, cmd);
1360 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1361 struct ethtool_coalesce *cmd,
1364 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1365 struct hns3_nic_priv *priv = netdev_priv(netdev);
1366 struct hnae3_handle *h = priv->ae_handle;
1367 int queue_num = h->kinfo.num_tqps;
1369 tx_vector = priv->ring[queue].tqp_vector;
1370 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1372 tx_vector->tx_group.coal.adapt_enable =
1373 cmd->use_adaptive_tx_coalesce;
1374 rx_vector->rx_group.coal.adapt_enable =
1375 cmd->use_adaptive_rx_coalesce;
1377 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1378 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1380 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1381 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1383 hns3_set_vector_coalesce_tx_gl(tx_vector,
1384 tx_vector->tx_group.coal.int_gl);
1385 hns3_set_vector_coalesce_rx_gl(rx_vector,
1386 rx_vector->rx_group.coal.int_gl);
1388 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1389 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1391 if (tx_vector->tx_group.coal.ql_enable)
1392 hns3_set_vector_coalesce_tx_ql(tx_vector,
1393 tx_vector->tx_group.coal.int_ql);
1394 if (rx_vector->rx_group.coal.ql_enable)
1395 hns3_set_vector_coalesce_rx_ql(rx_vector,
1396 rx_vector->rx_group.coal.int_ql);
1399 static int hns3_set_coalesce(struct net_device *netdev,
1400 struct ethtool_coalesce *cmd,
1401 struct kernel_ethtool_coalesce *kernel_coal,
1402 struct netlink_ext_ack *extack)
1404 struct hnae3_handle *h = hns3_get_handle(netdev);
1405 struct hns3_nic_priv *priv = netdev_priv(netdev);
1406 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1407 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1408 u16 queue_num = h->kinfo.num_tqps;
1409 enum dim_cq_period_mode tx_mode;
1410 enum dim_cq_period_mode rx_mode;
1414 if (hns3_nic_resetting(netdev))
1417 ret = hns3_check_coalesce_para(netdev, cmd);
1421 h->kinfo.int_rl_setting =
1422 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1424 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1425 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1427 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1428 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1430 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1431 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1433 for (i = 0; i < queue_num; i++)
1434 hns3_set_coalesce_per_queue(netdev, cmd, i);
1436 tx_mode = kernel_coal->use_cqe_mode_tx ?
1437 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1438 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1439 rx_mode = kernel_coal->use_cqe_mode_rx ?
1440 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1441 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1442 hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1447 static int hns3_get_regs_len(struct net_device *netdev)
1449 struct hnae3_handle *h = hns3_get_handle(netdev);
1451 if (!h->ae_algo->ops->get_regs_len)
1454 return h->ae_algo->ops->get_regs_len(h);
1457 static void hns3_get_regs(struct net_device *netdev,
1458 struct ethtool_regs *cmd, void *data)
1460 struct hnae3_handle *h = hns3_get_handle(netdev);
1462 if (!h->ae_algo->ops->get_regs)
1465 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1468 static int hns3_set_phys_id(struct net_device *netdev,
1469 enum ethtool_phys_id_state state)
1471 struct hnae3_handle *h = hns3_get_handle(netdev);
1473 if (!h->ae_algo->ops->set_led_id)
1476 return h->ae_algo->ops->set_led_id(h, state);
1479 static u32 hns3_get_msglevel(struct net_device *netdev)
1481 struct hnae3_handle *h = hns3_get_handle(netdev);
1483 return h->msg_enable;
1486 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1488 struct hnae3_handle *h = hns3_get_handle(netdev);
1490 h->msg_enable = msg_level;
1493 /* Translate local fec value into ethtool value. */
1494 static unsigned int loc_to_eth_fec(u8 loc_fec)
1498 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1499 eth_fec |= ETHTOOL_FEC_AUTO;
1500 if (loc_fec & BIT(HNAE3_FEC_RS))
1501 eth_fec |= ETHTOOL_FEC_RS;
1502 if (loc_fec & BIT(HNAE3_FEC_BASER))
1503 eth_fec |= ETHTOOL_FEC_BASER;
1505 /* if nothing is set, then FEC is off */
1507 eth_fec = ETHTOOL_FEC_OFF;
1512 /* Translate ethtool fec value into local value. */
1513 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1517 if (eth_fec & ETHTOOL_FEC_OFF)
1520 if (eth_fec & ETHTOOL_FEC_AUTO)
1521 loc_fec |= BIT(HNAE3_FEC_AUTO);
1522 if (eth_fec & ETHTOOL_FEC_RS)
1523 loc_fec |= BIT(HNAE3_FEC_RS);
1524 if (eth_fec & ETHTOOL_FEC_BASER)
1525 loc_fec |= BIT(HNAE3_FEC_BASER);
1530 static int hns3_get_fecparam(struct net_device *netdev,
1531 struct ethtool_fecparam *fec)
1533 struct hnae3_handle *handle = hns3_get_handle(netdev);
1534 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1535 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1539 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1545 ops->get_fec(handle, &fec_ability, &fec_mode);
1547 fec->fec = loc_to_eth_fec(fec_ability);
1548 fec->active_fec = loc_to_eth_fec(fec_mode);
1553 static int hns3_set_fecparam(struct net_device *netdev,
1554 struct ethtool_fecparam *fec)
1556 struct hnae3_handle *handle = hns3_get_handle(netdev);
1557 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1558 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1561 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1566 fec_mode = eth_to_loc_fec(fec->fec);
1568 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1570 return ops->set_fec(handle, fec_mode);
1573 static int hns3_get_module_info(struct net_device *netdev,
1574 struct ethtool_modinfo *modinfo)
1576 #define HNS3_SFF_8636_V1_3 0x03
1578 struct hnae3_handle *handle = hns3_get_handle(netdev);
1579 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1580 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1581 struct hns3_sfp_type sfp_type;
1584 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1585 !ops->get_module_eeprom)
1588 memset(&sfp_type, 0, sizeof(sfp_type));
1589 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1594 switch (sfp_type.type) {
1595 case SFF8024_ID_SFP:
1596 modinfo->type = ETH_MODULE_SFF_8472;
1597 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1599 case SFF8024_ID_QSFP_8438:
1600 modinfo->type = ETH_MODULE_SFF_8436;
1601 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1603 case SFF8024_ID_QSFP_8436_8636:
1604 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1605 modinfo->type = ETH_MODULE_SFF_8436;
1606 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1608 modinfo->type = ETH_MODULE_SFF_8636;
1609 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1612 case SFF8024_ID_QSFP28_8636:
1613 modinfo->type = ETH_MODULE_SFF_8636;
1614 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1617 netdev_err(netdev, "Optical module unknown: %#x\n",
1625 static int hns3_get_module_eeprom(struct net_device *netdev,
1626 struct ethtool_eeprom *ee, u8 *data)
1628 struct hnae3_handle *handle = hns3_get_handle(netdev);
1629 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1630 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1632 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1633 !ops->get_module_eeprom)
1639 memset(data, 0, ee->len);
1641 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1644 static u32 hns3_get_priv_flags(struct net_device *netdev)
1646 struct hnae3_handle *handle = hns3_get_handle(netdev);
1648 return handle->priv_flags;
1651 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1655 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1656 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1657 netdev_err(h->netdev, "%s is unsupported\n",
1658 hns3_priv_flags[i].name);
1665 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1667 struct hnae3_handle *handle = hns3_get_handle(netdev);
1668 u32 changed = pflags ^ handle->priv_flags;
1672 ret = hns3_check_priv_flags(handle, changed);
1676 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1677 if (changed & BIT(i)) {
1678 bool enable = !(handle->priv_flags & BIT(i));
1681 handle->priv_flags |= BIT(i);
1683 handle->priv_flags &= ~BIT(i);
1684 hns3_priv_flags[i].handler(netdev, enable);
1691 static int hns3_get_tunable(struct net_device *netdev,
1692 const struct ethtool_tunable *tuna,
1695 struct hns3_nic_priv *priv = netdev_priv(netdev);
1699 case ETHTOOL_TX_COPYBREAK:
1700 /* all the tx rings have the same tx_copybreak */
1701 *(u32 *)data = priv->tx_copybreak;
1703 case ETHTOOL_RX_COPYBREAK:
1704 *(u32 *)data = priv->rx_copybreak;
1714 static int hns3_set_tunable(struct net_device *netdev,
1715 const struct ethtool_tunable *tuna,
1718 struct hns3_nic_priv *priv = netdev_priv(netdev);
1719 struct hnae3_handle *h = priv->ae_handle;
1723 case ETHTOOL_TX_COPYBREAK:
1724 priv->tx_copybreak = *(u32 *)data;
1726 for (i = 0; i < h->kinfo.num_tqps; i++)
1727 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1730 case ETHTOOL_RX_COPYBREAK:
1731 priv->rx_copybreak = *(u32 *)data;
1733 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1734 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1745 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
1746 ETHTOOL_COALESCE_USE_ADAPTIVE | \
1747 ETHTOOL_COALESCE_RX_USECS_HIGH | \
1748 ETHTOOL_COALESCE_TX_USECS_HIGH | \
1749 ETHTOOL_COALESCE_MAX_FRAMES | \
1750 ETHTOOL_COALESCE_USE_CQE)
1752 static int hns3_get_ts_info(struct net_device *netdev,
1753 struct ethtool_ts_info *info)
1755 struct hnae3_handle *handle = hns3_get_handle(netdev);
1757 if (handle->ae_algo->ops->get_ts_info)
1758 return handle->ae_algo->ops->get_ts_info(handle, info);
1760 return ethtool_op_get_ts_info(netdev, info);
1763 static const struct hns3_ethtool_link_ext_state_mapping
1764 hns3_link_ext_state_map[] = {
1765 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1766 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1767 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1768 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1770 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1771 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1772 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1773 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1774 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1775 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1777 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1778 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1779 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1780 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1781 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1782 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1784 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1785 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1786 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1787 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1788 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1789 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1791 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1792 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1793 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1795 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1798 static int hns3_get_link_ext_state(struct net_device *netdev,
1799 struct ethtool_link_ext_state_info *info)
1801 const struct hns3_ethtool_link_ext_state_mapping *map;
1802 struct hnae3_handle *h = hns3_get_handle(netdev);
1806 if (netif_carrier_ok(netdev))
1809 if (!h->ae_algo->ops->get_link_diagnosis_info)
1812 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1816 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
1817 map = &hns3_link_ext_state_map[i];
1818 if (map->status_code == status_code) {
1819 info->link_ext_state = map->link_ext_state;
1820 info->__link_ext_substate = map->link_ext_substate;
1828 static const struct ethtool_ops hns3vf_ethtool_ops = {
1829 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1830 .get_drvinfo = hns3_get_drvinfo,
1831 .get_ringparam = hns3_get_ringparam,
1832 .set_ringparam = hns3_set_ringparam,
1833 .get_strings = hns3_get_strings,
1834 .get_ethtool_stats = hns3_get_stats,
1835 .get_sset_count = hns3_get_sset_count,
1836 .get_rxnfc = hns3_get_rxnfc,
1837 .set_rxnfc = hns3_set_rxnfc,
1838 .get_rxfh_key_size = hns3_get_rss_key_size,
1839 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1840 .get_rxfh = hns3_get_rss,
1841 .set_rxfh = hns3_set_rss,
1842 .get_link_ksettings = hns3_get_link_ksettings,
1843 .get_channels = hns3_get_channels,
1844 .set_channels = hns3_set_channels,
1845 .get_coalesce = hns3_get_coalesce,
1846 .set_coalesce = hns3_set_coalesce,
1847 .get_regs_len = hns3_get_regs_len,
1848 .get_regs = hns3_get_regs,
1849 .get_link = hns3_get_link,
1850 .get_msglevel = hns3_get_msglevel,
1851 .set_msglevel = hns3_set_msglevel,
1852 .get_priv_flags = hns3_get_priv_flags,
1853 .set_priv_flags = hns3_set_priv_flags,
1854 .get_tunable = hns3_get_tunable,
1855 .set_tunable = hns3_set_tunable,
1856 .reset = hns3_set_reset,
1859 static const struct ethtool_ops hns3_ethtool_ops = {
1860 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1861 .self_test = hns3_self_test,
1862 .get_drvinfo = hns3_get_drvinfo,
1863 .get_link = hns3_get_link,
1864 .get_ringparam = hns3_get_ringparam,
1865 .set_ringparam = hns3_set_ringparam,
1866 .get_pauseparam = hns3_get_pauseparam,
1867 .set_pauseparam = hns3_set_pauseparam,
1868 .get_strings = hns3_get_strings,
1869 .get_ethtool_stats = hns3_get_stats,
1870 .get_sset_count = hns3_get_sset_count,
1871 .get_rxnfc = hns3_get_rxnfc,
1872 .set_rxnfc = hns3_set_rxnfc,
1873 .get_rxfh_key_size = hns3_get_rss_key_size,
1874 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1875 .get_rxfh = hns3_get_rss,
1876 .set_rxfh = hns3_set_rss,
1877 .get_link_ksettings = hns3_get_link_ksettings,
1878 .set_link_ksettings = hns3_set_link_ksettings,
1879 .nway_reset = hns3_nway_reset,
1880 .get_channels = hns3_get_channels,
1881 .set_channels = hns3_set_channels,
1882 .get_coalesce = hns3_get_coalesce,
1883 .set_coalesce = hns3_set_coalesce,
1884 .get_regs_len = hns3_get_regs_len,
1885 .get_regs = hns3_get_regs,
1886 .set_phys_id = hns3_set_phys_id,
1887 .get_msglevel = hns3_get_msglevel,
1888 .set_msglevel = hns3_set_msglevel,
1889 .get_fecparam = hns3_get_fecparam,
1890 .set_fecparam = hns3_set_fecparam,
1891 .get_module_info = hns3_get_module_info,
1892 .get_module_eeprom = hns3_get_module_eeprom,
1893 .get_priv_flags = hns3_get_priv_flags,
1894 .set_priv_flags = hns3_set_priv_flags,
1895 .get_ts_info = hns3_get_ts_info,
1896 .get_tunable = hns3_get_tunable,
1897 .set_tunable = hns3_set_tunable,
1898 .reset = hns3_set_reset,
1899 .get_link_ext_state = hns3_get_link_ext_state,
1902 void hns3_ethtool_set_ops(struct net_device *netdev)
1904 struct hnae3_handle *h = hns3_get_handle(netdev);
1906 if (h->flags & HNAE3_SUPPORT_VF)
1907 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1909 netdev->ethtool_ops = &hns3_ethtool_ops;