Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 // Copyright (c) 2016-2017 Hisilicon Limited.
0003 
0004 #include <linux/etherdevice.h>
0005 #include <linux/string.h>
0006 #include <linux/phy.h>
0007 #include <linux/sfp.h>
0008 
0009 #include "hns3_enet.h"
0010 #include "hns3_ethtool.h"
0011 
0012 /* tqp related stats */
0013 #define HNS3_TQP_STAT(_string, _member) {           \
0014     .stats_string = _string,                \
0015     .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
0016             offsetof(struct ring_stats, _member),   \
0017 }
0018 
0019 static const struct hns3_stats hns3_txq_stats[] = {
0020     /* Tx per-queue statistics */
0021     HNS3_TQP_STAT("dropped", sw_err_cnt),
0022     HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
0023     HNS3_TQP_STAT("packets", tx_pkts),
0024     HNS3_TQP_STAT("bytes", tx_bytes),
0025     HNS3_TQP_STAT("more", tx_more),
0026     HNS3_TQP_STAT("push", tx_push),
0027     HNS3_TQP_STAT("mem_doorbell", tx_mem_doorbell),
0028     HNS3_TQP_STAT("wake", restart_queue),
0029     HNS3_TQP_STAT("busy", tx_busy),
0030     HNS3_TQP_STAT("copy", tx_copy),
0031     HNS3_TQP_STAT("vlan_err", tx_vlan_err),
0032     HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
0033     HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
0034     HNS3_TQP_STAT("tso_err", tx_tso_err),
0035     HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
0036     HNS3_TQP_STAT("hw_limitation", hw_limitation),
0037     HNS3_TQP_STAT("bounce", tx_bounce),
0038     HNS3_TQP_STAT("spare_full", tx_spare_full),
0039     HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
0040     HNS3_TQP_STAT("sgl", tx_sgl),
0041     HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
0042     HNS3_TQP_STAT("map_sg_err", map_sg_err),
0043 };
0044 
0045 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
0046 
0047 static const struct hns3_stats hns3_rxq_stats[] = {
0048     /* Rx per-queue statistics */
0049     HNS3_TQP_STAT("dropped", sw_err_cnt),
0050     HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
0051     HNS3_TQP_STAT("packets", rx_pkts),
0052     HNS3_TQP_STAT("bytes", rx_bytes),
0053     HNS3_TQP_STAT("errors", rx_err_cnt),
0054     HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
0055     HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
0056     HNS3_TQP_STAT("err_bd_num", err_bd_num),
0057     HNS3_TQP_STAT("l2_err", l2_err),
0058     HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
0059     HNS3_TQP_STAT("csum_complete", csum_complete),
0060     HNS3_TQP_STAT("multicast", rx_multicast),
0061     HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
0062     HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
0063     HNS3_TQP_STAT("frag_alloc", frag_alloc),
0064 };
0065 
0066 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
0067 
0068 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
0069 
0070 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
0071 
0072 #define HNS3_SELF_TEST_TYPE_NUM         4
0073 #define HNS3_NIC_LB_TEST_PKT_NUM    1
0074 #define HNS3_NIC_LB_TEST_RING_ID    0
0075 #define HNS3_NIC_LB_TEST_PACKET_SIZE    128
0076 #define HNS3_NIC_LB_SETUP_USEC      10000
0077 
0078 /* Nic loopback test err  */
0079 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
0080 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
0081 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
0082 
0083 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
0084 {
0085     struct hnae3_handle *h = hns3_get_handle(ndev);
0086     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0087     int ret;
0088 
0089     if (!h->ae_algo->ops->set_loopback ||
0090         !h->ae_algo->ops->set_promisc_mode)
0091         return -EOPNOTSUPP;
0092 
0093     switch (loop) {
0094     case HNAE3_LOOP_SERIAL_SERDES:
0095     case HNAE3_LOOP_PARALLEL_SERDES:
0096     case HNAE3_LOOP_APP:
0097     case HNAE3_LOOP_PHY:
0098         ret = h->ae_algo->ops->set_loopback(h, loop, en);
0099         break;
0100     default:
0101         ret = -ENOTSUPP;
0102         break;
0103     }
0104 
0105     if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
0106         return ret;
0107 
0108     if (en)
0109         h->ae_algo->ops->set_promisc_mode(h, true, true);
0110     else
0111         /* recover promisc mode before loopback test */
0112         hns3_request_update_promisc_mode(h);
0113 
0114     return ret;
0115 }
0116 
0117 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
0118 {
0119     struct hnae3_handle *h = hns3_get_handle(ndev);
0120     int ret;
0121 
0122     ret = hns3_nic_reset_all_ring(h);
0123     if (ret)
0124         return ret;
0125 
0126     ret = hns3_lp_setup(ndev, loop_mode, true);
0127     usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
0128 
0129     return ret;
0130 }
0131 
0132 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
0133 {
0134     int ret;
0135 
0136     ret = hns3_lp_setup(ndev, loop_mode, false);
0137     if (ret) {
0138         netdev_err(ndev, "lb_setup return error: %d\n", ret);
0139         return ret;
0140     }
0141 
0142     usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
0143 
0144     return 0;
0145 }
0146 
0147 static void hns3_lp_setup_skb(struct sk_buff *skb)
0148 {
0149 #define HNS3_NIC_LB_DST_MAC_ADDR    0x1f
0150 
0151     struct net_device *ndev = skb->dev;
0152     struct hnae3_handle *handle;
0153     struct hnae3_ae_dev *ae_dev;
0154     unsigned char *packet;
0155     struct ethhdr *ethh;
0156     unsigned int i;
0157 
0158     skb_reserve(skb, NET_IP_ALIGN);
0159     ethh = skb_put(skb, sizeof(struct ethhdr));
0160     packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
0161 
0162     memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
0163 
0164     /* The dst mac addr of loopback packet is the same as the host'
0165      * mac addr, the SSU component may loop back the packet to host
0166      * before the packet reaches mac or serdes, which will defect
0167      * the purpose of mac or serdes selftest.
0168      */
0169     handle = hns3_get_handle(ndev);
0170     ae_dev = pci_get_drvdata(handle->pdev);
0171     if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
0172         ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
0173     eth_zero_addr(ethh->h_source);
0174     ethh->h_proto = htons(ETH_P_ARP);
0175     skb_reset_mac_header(skb);
0176 
0177     for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
0178         packet[i] = (unsigned char)(i & 0xff);
0179 }
0180 
0181 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
0182                    struct sk_buff *skb)
0183 {
0184     struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
0185     unsigned char *packet = skb->data;
0186     u32 len = skb_headlen(skb);
0187     u32 i;
0188 
0189     len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
0190 
0191     for (i = 0; i < len; i++)
0192         if (packet[i] != (unsigned char)(i & 0xff))
0193             break;
0194 
0195     /* The packet is correctly received */
0196     if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
0197         tqp_vector->rx_group.total_packets++;
0198     else
0199         print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
0200                    skb->data, len, true);
0201 
0202     dev_kfree_skb_any(skb);
0203 }
0204 
0205 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
0206 {
0207     struct hnae3_handle *h = priv->ae_handle;
0208     struct hnae3_knic_private_info *kinfo;
0209     u32 i, rcv_good_pkt_total = 0;
0210 
0211     kinfo = &h->kinfo;
0212     for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
0213         struct hns3_enet_ring *ring = &priv->ring[i];
0214         struct hns3_enet_ring_group *rx_group;
0215         u64 pre_rx_pkt;
0216 
0217         rx_group = &ring->tqp_vector->rx_group;
0218         pre_rx_pkt = rx_group->total_packets;
0219 
0220         preempt_disable();
0221         hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
0222         preempt_enable();
0223 
0224         rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
0225         rx_group->total_packets = pre_rx_pkt;
0226     }
0227     return rcv_good_pkt_total;
0228 }
0229 
0230 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
0231                   u32 end_ringid, u32 budget)
0232 {
0233     u32 i;
0234 
0235     for (i = start_ringid; i <= end_ringid; i++) {
0236         struct hns3_enet_ring *ring = &priv->ring[i];
0237 
0238         hns3_clean_tx_ring(ring, 0);
0239     }
0240 }
0241 
0242 /**
0243  * hns3_lp_run_test - run loopback test
0244  * @ndev: net device
0245  * @mode: loopback type
0246  *
0247  * Return: %0 for success or a NIC loopback test error code on failure
0248  */
0249 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
0250 {
0251     struct hns3_nic_priv *priv = netdev_priv(ndev);
0252     struct sk_buff *skb;
0253     u32 i, good_cnt;
0254     int ret_val = 0;
0255 
0256     skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
0257             GFP_KERNEL);
0258     if (!skb)
0259         return HNS3_NIC_LB_TEST_NO_MEM_ERR;
0260 
0261     skb->dev = ndev;
0262     hns3_lp_setup_skb(skb);
0263     skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
0264 
0265     good_cnt = 0;
0266     for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
0267         netdev_tx_t tx_ret;
0268 
0269         skb_get(skb);
0270         tx_ret = hns3_nic_net_xmit(skb, ndev);
0271         if (tx_ret == NETDEV_TX_OK) {
0272             good_cnt++;
0273         } else {
0274             kfree_skb(skb);
0275             netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
0276                    tx_ret);
0277         }
0278     }
0279     if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
0280         ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
0281         netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
0282                mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
0283         goto out;
0284     }
0285 
0286     /* Allow 200 milliseconds for packets to go from Tx to Rx */
0287     msleep(200);
0288 
0289     good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
0290     if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
0291         ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
0292         netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
0293                mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
0294     }
0295 
0296 out:
0297     hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
0298                   HNS3_NIC_LB_TEST_RING_ID,
0299                   HNS3_NIC_LB_TEST_PKT_NUM);
0300 
0301     kfree_skb(skb);
0302     return ret_val;
0303 }
0304 
0305 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
0306 {
0307     st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
0308     st_param[HNAE3_LOOP_APP][1] =
0309             h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
0310 
0311     st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
0312     st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
0313             h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
0314 
0315     st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
0316             HNAE3_LOOP_PARALLEL_SERDES;
0317     st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
0318             h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
0319 
0320     st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
0321     st_param[HNAE3_LOOP_PHY][1] =
0322             h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
0323 }
0324 
0325 static void hns3_selftest_prepare(struct net_device *ndev,
0326                   bool if_running, int (*st_param)[2])
0327 {
0328     struct hns3_nic_priv *priv = netdev_priv(ndev);
0329     struct hnae3_handle *h = priv->ae_handle;
0330 
0331     if (netif_msg_ifdown(h))
0332         netdev_info(ndev, "self test start\n");
0333 
0334     hns3_set_selftest_param(h, st_param);
0335 
0336     if (if_running)
0337         ndev->netdev_ops->ndo_stop(ndev);
0338 
0339 #if IS_ENABLED(CONFIG_VLAN_8021Q)
0340     /* Disable the vlan filter for selftest does not support it */
0341     if (h->ae_algo->ops->enable_vlan_filter &&
0342         ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
0343         h->ae_algo->ops->enable_vlan_filter(h, false);
0344 #endif
0345 
0346     /* Tell firmware to stop mac autoneg before loopback test start,
0347      * otherwise loopback test may be failed when the port is still
0348      * negotiating.
0349      */
0350     if (h->ae_algo->ops->halt_autoneg)
0351         h->ae_algo->ops->halt_autoneg(h, true);
0352 
0353     set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
0354 }
0355 
0356 static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
0357 {
0358     struct hns3_nic_priv *priv = netdev_priv(ndev);
0359     struct hnae3_handle *h = priv->ae_handle;
0360 
0361     clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
0362 
0363     if (h->ae_algo->ops->halt_autoneg)
0364         h->ae_algo->ops->halt_autoneg(h, false);
0365 
0366 #if IS_ENABLED(CONFIG_VLAN_8021Q)
0367     if (h->ae_algo->ops->enable_vlan_filter &&
0368         ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
0369         h->ae_algo->ops->enable_vlan_filter(h, true);
0370 #endif
0371 
0372     if (if_running)
0373         ndev->netdev_ops->ndo_open(ndev);
0374 
0375     if (netif_msg_ifdown(h))
0376         netdev_info(ndev, "self test end\n");
0377 }
0378 
0379 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
0380                  struct ethtool_test *eth_test, u64 *data)
0381 {
0382     int test_index = 0;
0383     u32 i;
0384 
0385     for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
0386         enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
0387 
0388         if (!st_param[i][1])
0389             continue;
0390 
0391         data[test_index] = hns3_lp_up(ndev, loop_type);
0392         if (!data[test_index])
0393             data[test_index] = hns3_lp_run_test(ndev, loop_type);
0394 
0395         hns3_lp_down(ndev, loop_type);
0396 
0397         if (data[test_index])
0398             eth_test->flags |= ETH_TEST_FL_FAILED;
0399 
0400         test_index++;
0401     }
0402 }
0403 
0404 /**
0405  * hns3_self_test - self test
0406  * @ndev: net device
0407  * @eth_test: test cmd
0408  * @data: test result
0409  */
0410 static void hns3_self_test(struct net_device *ndev,
0411                struct ethtool_test *eth_test, u64 *data)
0412 {
0413     int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
0414     bool if_running = netif_running(ndev);
0415 
0416     if (hns3_nic_resetting(ndev)) {
0417         netdev_err(ndev, "dev resetting!");
0418         return;
0419     }
0420 
0421     /* Only do offline selftest, or pass by default */
0422     if (eth_test->flags != ETH_TEST_FL_OFFLINE)
0423         return;
0424 
0425     hns3_selftest_prepare(ndev, if_running, st_param);
0426     hns3_do_selftest(ndev, st_param, eth_test, data);
0427     hns3_selftest_restore(ndev, if_running);
0428 }
0429 
0430 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
0431                        bool enable)
0432 {
0433     struct hnae3_handle *handle = hns3_get_handle(netdev);
0434 
0435     if (enable)
0436         set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
0437     else
0438         clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
0439 
0440     hns3_request_update_promisc_mode(handle);
0441 }
0442 
0443 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
0444     { "limit_promisc",  hns3_update_limit_promisc_mode }
0445 };
0446 
0447 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
0448 {
0449     struct hnae3_handle *h = hns3_get_handle(netdev);
0450     const struct hnae3_ae_ops *ops = h->ae_algo->ops;
0451 
0452     if (!ops->get_sset_count)
0453         return -EOPNOTSUPP;
0454 
0455     switch (stringset) {
0456     case ETH_SS_STATS:
0457         return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
0458             ops->get_sset_count(h, stringset));
0459 
0460     case ETH_SS_TEST:
0461         return ops->get_sset_count(h, stringset);
0462 
0463     case ETH_SS_PRIV_FLAGS:
0464         return HNAE3_PFLAG_MAX;
0465 
0466     default:
0467         return -EOPNOTSUPP;
0468     }
0469 }
0470 
0471 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
0472         u32 stat_count, u32 num_tqps, const char *prefix)
0473 {
0474 #define MAX_PREFIX_SIZE (6 + 4)
0475     u32 size_left;
0476     u32 i, j;
0477     u32 n1;
0478 
0479     for (i = 0; i < num_tqps; i++) {
0480         for (j = 0; j < stat_count; j++) {
0481             data[ETH_GSTRING_LEN - 1] = '\0';
0482 
0483             /* first, prepend the prefix string */
0484             n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
0485                        prefix, i);
0486             size_left = (ETH_GSTRING_LEN - 1) - n1;
0487 
0488             /* now, concatenate the stats string to it */
0489             strncat(data, stats[j].stats_string, size_left);
0490             data += ETH_GSTRING_LEN;
0491         }
0492     }
0493 
0494     return data;
0495 }
0496 
0497 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
0498 {
0499     struct hnae3_knic_private_info *kinfo = &handle->kinfo;
0500     const char tx_prefix[] = "txq";
0501     const char rx_prefix[] = "rxq";
0502 
0503     /* get strings for Tx */
0504     data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
0505                    kinfo->num_tqps, tx_prefix);
0506 
0507     /* get strings for Rx */
0508     data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
0509                    kinfo->num_tqps, rx_prefix);
0510 
0511     return data;
0512 }
0513 
0514 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
0515 {
0516     struct hnae3_handle *h = hns3_get_handle(netdev);
0517     const struct hnae3_ae_ops *ops = h->ae_algo->ops;
0518     char *buff = (char *)data;
0519     int i;
0520 
0521     if (!ops->get_strings)
0522         return;
0523 
0524     switch (stringset) {
0525     case ETH_SS_STATS:
0526         buff = hns3_get_strings_tqps(h, buff);
0527         ops->get_strings(h, stringset, (u8 *)buff);
0528         break;
0529     case ETH_SS_TEST:
0530         ops->get_strings(h, stringset, data);
0531         break;
0532     case ETH_SS_PRIV_FLAGS:
0533         for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
0534             snprintf(buff, ETH_GSTRING_LEN, "%s",
0535                  hns3_priv_flags[i].name);
0536             buff += ETH_GSTRING_LEN;
0537         }
0538         break;
0539     default:
0540         break;
0541     }
0542 }
0543 
0544 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
0545 {
0546     struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
0547     struct hnae3_knic_private_info *kinfo = &handle->kinfo;
0548     struct hns3_enet_ring *ring;
0549     u8 *stat;
0550     int i, j;
0551 
0552     /* get stats for Tx */
0553     for (i = 0; i < kinfo->num_tqps; i++) {
0554         ring = &nic_priv->ring[i];
0555         for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
0556             stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
0557             *data++ = *(u64 *)stat;
0558         }
0559     }
0560 
0561     /* get stats for Rx */
0562     for (i = 0; i < kinfo->num_tqps; i++) {
0563         ring = &nic_priv->ring[i + kinfo->num_tqps];
0564         for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
0565             stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
0566             *data++ = *(u64 *)stat;
0567         }
0568     }
0569 
0570     return data;
0571 }
0572 
0573 /* hns3_get_stats - get detail statistics.
0574  * @netdev: net device
0575  * @stats: statistics info.
0576  * @data: statistics data.
0577  */
0578 static void hns3_get_stats(struct net_device *netdev,
0579                struct ethtool_stats *stats, u64 *data)
0580 {
0581     struct hnae3_handle *h = hns3_get_handle(netdev);
0582     u64 *p = data;
0583 
0584     if (hns3_nic_resetting(netdev)) {
0585         netdev_err(netdev, "dev resetting, could not get stats\n");
0586         return;
0587     }
0588 
0589     if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
0590         netdev_err(netdev, "could not get any statistics\n");
0591         return;
0592     }
0593 
0594     h->ae_algo->ops->update_stats(h, &netdev->stats);
0595 
0596     /* get per-queue stats */
0597     p = hns3_get_stats_tqps(h, p);
0598 
0599     /* get MAC & other misc hardware stats */
0600     h->ae_algo->ops->get_stats(h, p);
0601 }
0602 
0603 static void hns3_get_drvinfo(struct net_device *netdev,
0604                  struct ethtool_drvinfo *drvinfo)
0605 {
0606     struct hns3_nic_priv *priv = netdev_priv(netdev);
0607     struct hnae3_handle *h = priv->ae_handle;
0608     u32 fw_version;
0609 
0610     if (!h->ae_algo->ops->get_fw_version) {
0611         netdev_err(netdev, "could not get fw version!\n");
0612         return;
0613     }
0614 
0615     strncpy(drvinfo->driver, dev_driver_string(&h->pdev->dev),
0616         sizeof(drvinfo->driver));
0617     drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
0618 
0619     strncpy(drvinfo->bus_info, pci_name(h->pdev),
0620         sizeof(drvinfo->bus_info));
0621     drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
0622 
0623     fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
0624 
0625     snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
0626          "%lu.%lu.%lu.%lu",
0627          hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
0628                  HNAE3_FW_VERSION_BYTE3_SHIFT),
0629          hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
0630                  HNAE3_FW_VERSION_BYTE2_SHIFT),
0631          hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
0632                  HNAE3_FW_VERSION_BYTE1_SHIFT),
0633          hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
0634                  HNAE3_FW_VERSION_BYTE0_SHIFT));
0635 }
0636 
0637 static u32 hns3_get_link(struct net_device *netdev)
0638 {
0639     struct hnae3_handle *h = hns3_get_handle(netdev);
0640 
0641     if (h->ae_algo->ops->get_status)
0642         return h->ae_algo->ops->get_status(h);
0643     else
0644         return 0;
0645 }
0646 
0647 static void hns3_get_ringparam(struct net_device *netdev,
0648                    struct ethtool_ringparam *param,
0649                    struct kernel_ethtool_ringparam *kernel_param,
0650                    struct netlink_ext_ack *extack)
0651 {
0652     struct hns3_nic_priv *priv = netdev_priv(netdev);
0653     struct hnae3_handle *h = priv->ae_handle;
0654     int rx_queue_index = h->kinfo.num_tqps;
0655 
0656     if (hns3_nic_resetting(netdev) || !priv->ring) {
0657         netdev_err(netdev, "failed to get ringparam value, due to dev resetting or uninited\n");
0658         return;
0659     }
0660 
0661     param->tx_max_pending = HNS3_RING_MAX_PENDING;
0662     param->rx_max_pending = HNS3_RING_MAX_PENDING;
0663 
0664     param->tx_pending = priv->ring[0].desc_num;
0665     param->rx_pending = priv->ring[rx_queue_index].desc_num;
0666     kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size;
0667     kernel_param->tx_push = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE,
0668                      &priv->state);
0669 }
0670 
0671 static void hns3_get_pauseparam(struct net_device *netdev,
0672                 struct ethtool_pauseparam *param)
0673 {
0674     struct hnae3_handle *h = hns3_get_handle(netdev);
0675     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0676 
0677     if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
0678         return;
0679 
0680     if (h->ae_algo->ops->get_pauseparam)
0681         h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
0682             &param->rx_pause, &param->tx_pause);
0683 }
0684 
0685 static int hns3_set_pauseparam(struct net_device *netdev,
0686                    struct ethtool_pauseparam *param)
0687 {
0688     struct hnae3_handle *h = hns3_get_handle(netdev);
0689     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0690 
0691     if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
0692         return -EOPNOTSUPP;
0693 
0694     netif_dbg(h, drv, netdev,
0695           "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
0696           param->autoneg, param->rx_pause, param->tx_pause);
0697 
0698     if (h->ae_algo->ops->set_pauseparam)
0699         return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
0700                                param->rx_pause,
0701                                param->tx_pause);
0702     return -EOPNOTSUPP;
0703 }
0704 
0705 static void hns3_get_ksettings(struct hnae3_handle *h,
0706                    struct ethtool_link_ksettings *cmd)
0707 {
0708     const struct hnae3_ae_ops *ops = h->ae_algo->ops;
0709 
0710     /* 1.auto_neg & speed & duplex from cmd */
0711     if (ops->get_ksettings_an_result)
0712         ops->get_ksettings_an_result(h,
0713                          &cmd->base.autoneg,
0714                          &cmd->base.speed,
0715                          &cmd->base.duplex);
0716 
0717     /* 2.get link mode */
0718     if (ops->get_link_mode)
0719         ops->get_link_mode(h,
0720                    cmd->link_modes.supported,
0721                    cmd->link_modes.advertising);
0722 
0723     /* 3.mdix_ctrl&mdix get from phy reg */
0724     if (ops->get_mdix_mode)
0725         ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
0726                    &cmd->base.eth_tp_mdix);
0727 }
0728 
0729 static int hns3_get_link_ksettings(struct net_device *netdev,
0730                    struct ethtool_link_ksettings *cmd)
0731 {
0732     struct hnae3_handle *h = hns3_get_handle(netdev);
0733     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0734     const struct hnae3_ae_ops *ops;
0735     u8 module_type;
0736     u8 media_type;
0737     u8 link_stat;
0738 
0739     ops = h->ae_algo->ops;
0740     if (ops->get_media_type)
0741         ops->get_media_type(h, &media_type, &module_type);
0742     else
0743         return -EOPNOTSUPP;
0744 
0745     switch (media_type) {
0746     case HNAE3_MEDIA_TYPE_NONE:
0747         cmd->base.port = PORT_NONE;
0748         hns3_get_ksettings(h, cmd);
0749         break;
0750     case HNAE3_MEDIA_TYPE_FIBER:
0751         if (module_type == HNAE3_MODULE_TYPE_CR)
0752             cmd->base.port = PORT_DA;
0753         else
0754             cmd->base.port = PORT_FIBRE;
0755 
0756         hns3_get_ksettings(h, cmd);
0757         break;
0758     case HNAE3_MEDIA_TYPE_BACKPLANE:
0759         cmd->base.port = PORT_NONE;
0760         hns3_get_ksettings(h, cmd);
0761         break;
0762     case HNAE3_MEDIA_TYPE_COPPER:
0763         cmd->base.port = PORT_TP;
0764         if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
0765             ops->get_phy_link_ksettings)
0766             ops->get_phy_link_ksettings(h, cmd);
0767         else if (!netdev->phydev)
0768             hns3_get_ksettings(h, cmd);
0769         else
0770             phy_ethtool_ksettings_get(netdev->phydev, cmd);
0771         break;
0772     default:
0773 
0774         netdev_warn(netdev, "Unknown media type");
0775         return 0;
0776     }
0777 
0778     /* mdio_support */
0779     cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
0780 
0781     link_stat = hns3_get_link(netdev);
0782     if (!link_stat) {
0783         cmd->base.speed = SPEED_UNKNOWN;
0784         cmd->base.duplex = DUPLEX_UNKNOWN;
0785     }
0786 
0787     return 0;
0788 }
0789 
0790 static int hns3_check_ksettings_param(const struct net_device *netdev,
0791                       const struct ethtool_link_ksettings *cmd)
0792 {
0793     struct hnae3_handle *handle = hns3_get_handle(netdev);
0794     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
0795     u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
0796     u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
0797     u8 autoneg;
0798     u32 speed;
0799     u8 duplex;
0800     int ret;
0801 
0802     /* hw doesn't support use specified speed and duplex to negotiate,
0803      * unnecessary to check them when autoneg on.
0804      */
0805     if (cmd->base.autoneg)
0806         return 0;
0807 
0808     if (ops->get_ksettings_an_result) {
0809         ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
0810         if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
0811             cmd->base.duplex == duplex)
0812             return 0;
0813     }
0814 
0815     if (ops->get_media_type)
0816         ops->get_media_type(handle, &media_type, &module_type);
0817 
0818     if (cmd->base.duplex == DUPLEX_HALF &&
0819         media_type != HNAE3_MEDIA_TYPE_COPPER) {
0820         netdev_err(netdev,
0821                "only copper port supports half duplex!");
0822         return -EINVAL;
0823     }
0824 
0825     if (ops->check_port_speed) {
0826         ret = ops->check_port_speed(handle, cmd->base.speed);
0827         if (ret) {
0828             netdev_err(netdev, "unsupported speed\n");
0829             return ret;
0830         }
0831     }
0832 
0833     return 0;
0834 }
0835 
0836 static int hns3_set_link_ksettings(struct net_device *netdev,
0837                    const struct ethtool_link_ksettings *cmd)
0838 {
0839     struct hnae3_handle *handle = hns3_get_handle(netdev);
0840     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
0841     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
0842     int ret;
0843 
0844     /* Chip don't support this mode. */
0845     if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
0846         return -EINVAL;
0847 
0848     netif_dbg(handle, drv, netdev,
0849           "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
0850           netdev->phydev ? "phy" : "mac",
0851           cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
0852 
0853     /* Only support ksettings_set for netdev with phy attached for now */
0854     if (netdev->phydev) {
0855         if (cmd->base.speed == SPEED_1000 &&
0856             cmd->base.autoneg == AUTONEG_DISABLE)
0857             return -EINVAL;
0858 
0859         return phy_ethtool_ksettings_set(netdev->phydev, cmd);
0860     } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
0861            ops->set_phy_link_ksettings) {
0862         return ops->set_phy_link_ksettings(handle, cmd);
0863     }
0864 
0865     if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
0866         return -EOPNOTSUPP;
0867 
0868     ret = hns3_check_ksettings_param(netdev, cmd);
0869     if (ret)
0870         return ret;
0871 
0872     if (ops->set_autoneg) {
0873         ret = ops->set_autoneg(handle, cmd->base.autoneg);
0874         if (ret)
0875             return ret;
0876     }
0877 
0878     /* hw doesn't support use specified speed and duplex to negotiate,
0879      * ignore them when autoneg on.
0880      */
0881     if (cmd->base.autoneg) {
0882         netdev_info(netdev,
0883                 "autoneg is on, ignore the speed and duplex\n");
0884         return 0;
0885     }
0886 
0887     if (ops->cfg_mac_speed_dup_h)
0888         ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
0889                            cmd->base.duplex);
0890 
0891     return ret;
0892 }
0893 
0894 static u32 hns3_get_rss_key_size(struct net_device *netdev)
0895 {
0896     struct hnae3_handle *h = hns3_get_handle(netdev);
0897 
0898     if (!h->ae_algo->ops->get_rss_key_size)
0899         return 0;
0900 
0901     return h->ae_algo->ops->get_rss_key_size(h);
0902 }
0903 
0904 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
0905 {
0906     struct hnae3_handle *h = hns3_get_handle(netdev);
0907     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0908 
0909     return ae_dev->dev_specs.rss_ind_tbl_size;
0910 }
0911 
0912 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
0913             u8 *hfunc)
0914 {
0915     struct hnae3_handle *h = hns3_get_handle(netdev);
0916 
0917     if (!h->ae_algo->ops->get_rss)
0918         return -EOPNOTSUPP;
0919 
0920     return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
0921 }
0922 
0923 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
0924             const u8 *key, const u8 hfunc)
0925 {
0926     struct hnae3_handle *h = hns3_get_handle(netdev);
0927     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0928 
0929     if (!h->ae_algo->ops->set_rss)
0930         return -EOPNOTSUPP;
0931 
0932     if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
0933          hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
0934          hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
0935         netdev_err(netdev, "hash func not supported\n");
0936         return -EOPNOTSUPP;
0937     }
0938 
0939     if (!indir) {
0940         netdev_err(netdev,
0941                "set rss failed for indir is empty\n");
0942         return -EOPNOTSUPP;
0943     }
0944 
0945     return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
0946 }
0947 
0948 static int hns3_get_rxnfc(struct net_device *netdev,
0949               struct ethtool_rxnfc *cmd,
0950               u32 *rule_locs)
0951 {
0952     struct hnae3_handle *h = hns3_get_handle(netdev);
0953 
0954     switch (cmd->cmd) {
0955     case ETHTOOL_GRXRINGS:
0956         cmd->data = h->kinfo.num_tqps;
0957         return 0;
0958     case ETHTOOL_GRXFH:
0959         if (h->ae_algo->ops->get_rss_tuple)
0960             return h->ae_algo->ops->get_rss_tuple(h, cmd);
0961         return -EOPNOTSUPP;
0962     case ETHTOOL_GRXCLSRLCNT:
0963         if (h->ae_algo->ops->get_fd_rule_cnt)
0964             return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
0965         return -EOPNOTSUPP;
0966     case ETHTOOL_GRXCLSRULE:
0967         if (h->ae_algo->ops->get_fd_rule_info)
0968             return h->ae_algo->ops->get_fd_rule_info(h, cmd);
0969         return -EOPNOTSUPP;
0970     case ETHTOOL_GRXCLSRLALL:
0971         if (h->ae_algo->ops->get_fd_all_rules)
0972             return h->ae_algo->ops->get_fd_all_rules(h, cmd,
0973                                  rule_locs);
0974         return -EOPNOTSUPP;
0975     default:
0976         return -EOPNOTSUPP;
0977     }
0978 }
0979 
0980 static const struct hns3_reset_type_map hns3_reset_type[] = {
0981     {ETH_RESET_MGMT, HNAE3_IMP_RESET},
0982     {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
0983     {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
0984 };
0985 
0986 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
0987     {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
0988 };
0989 
0990 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
0991 {
0992     enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
0993     struct hnae3_handle *h = hns3_get_handle(netdev);
0994     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
0995     const struct hnae3_ae_ops *ops = h->ae_algo->ops;
0996     const struct hns3_reset_type_map *rst_type_map;
0997     enum ethtool_reset_flags rst_flags;
0998     u32 i, size;
0999 
1000     if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
1001         return -EBUSY;
1002 
1003     if (!ops->set_default_reset_request || !ops->reset_event)
1004         return -EOPNOTSUPP;
1005 
1006     if (h->flags & HNAE3_SUPPORT_VF) {
1007         rst_type_map = hns3vf_reset_type;
1008         size = ARRAY_SIZE(hns3vf_reset_type);
1009     } else {
1010         rst_type_map = hns3_reset_type;
1011         size = ARRAY_SIZE(hns3_reset_type);
1012     }
1013 
1014     for (i = 0; i < size; i++) {
1015         if (rst_type_map[i].rst_flags == *flags) {
1016             rst_type = rst_type_map[i].rst_type;
1017             rst_flags = rst_type_map[i].rst_flags;
1018             break;
1019         }
1020     }
1021 
1022     if (rst_type == HNAE3_NONE_RESET ||
1023         (rst_type == HNAE3_IMP_RESET &&
1024          ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1025         return -EOPNOTSUPP;
1026 
1027     netdev_info(netdev, "Setting reset type %d\n", rst_type);
1028 
1029     ops->set_default_reset_request(ae_dev, rst_type);
1030 
1031     ops->reset_event(h->pdev, h);
1032 
1033     *flags &= ~rst_flags;
1034 
1035     return 0;
1036 }
1037 
1038 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1039                     u32 tx_desc_num, u32 rx_desc_num)
1040 {
1041     struct hnae3_handle *h = priv->ae_handle;
1042     int i;
1043 
1044     h->kinfo.num_tx_desc = tx_desc_num;
1045     h->kinfo.num_rx_desc = rx_desc_num;
1046 
1047     for (i = 0; i < h->kinfo.num_tqps; i++) {
1048         priv->ring[i].desc_num = tx_desc_num;
1049         priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1050     }
1051 }
1052 
1053 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1054 {
1055     struct hnae3_handle *handle = priv->ae_handle;
1056     struct hns3_enet_ring *tmp_rings;
1057     int i;
1058 
1059     tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1060                 sizeof(struct hns3_enet_ring), GFP_KERNEL);
1061     if (!tmp_rings)
1062         return NULL;
1063 
1064     for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1065         memcpy(&tmp_rings[i], &priv->ring[i],
1066                sizeof(struct hns3_enet_ring));
1067         tmp_rings[i].skb = NULL;
1068     }
1069 
1070     return tmp_rings;
1071 }
1072 
1073 static int hns3_check_ringparam(struct net_device *ndev,
1074                 struct ethtool_ringparam *param,
1075                 struct kernel_ethtool_ringparam *kernel_param)
1076 {
1077 #define RX_BUF_LEN_2K 2048
1078 #define RX_BUF_LEN_4K 4096
1079 
1080     struct hns3_nic_priv *priv = netdev_priv(ndev);
1081 
1082     if (hns3_nic_resetting(ndev) || !priv->ring) {
1083         netdev_err(ndev, "failed to set ringparam value, due to dev resetting or uninited\n");
1084         return -EBUSY;
1085     }
1086 
1087 
1088     if (param->rx_mini_pending || param->rx_jumbo_pending)
1089         return -EINVAL;
1090 
1091     if (kernel_param->rx_buf_len != RX_BUF_LEN_2K &&
1092         kernel_param->rx_buf_len != RX_BUF_LEN_4K) {
1093         netdev_err(ndev, "Rx buf len only support 2048 and 4096\n");
1094         return -EINVAL;
1095     }
1096 
1097     if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1098         param->tx_pending < HNS3_RING_MIN_PENDING ||
1099         param->rx_pending > HNS3_RING_MAX_PENDING ||
1100         param->rx_pending < HNS3_RING_MIN_PENDING) {
1101         netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1102                HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1103         return -EINVAL;
1104     }
1105 
1106     return 0;
1107 }
1108 
1109 static bool
1110 hns3_is_ringparam_changed(struct net_device *ndev,
1111               struct ethtool_ringparam *param,
1112               struct kernel_ethtool_ringparam *kernel_param,
1113               struct hns3_ring_param *old_ringparam,
1114               struct hns3_ring_param *new_ringparam)
1115 {
1116     struct hns3_nic_priv *priv = netdev_priv(ndev);
1117     struct hnae3_handle *h = priv->ae_handle;
1118     u16 queue_num = h->kinfo.num_tqps;
1119 
1120     new_ringparam->tx_desc_num = ALIGN(param->tx_pending,
1121                        HNS3_RING_BD_MULTIPLE);
1122     new_ringparam->rx_desc_num = ALIGN(param->rx_pending,
1123                        HNS3_RING_BD_MULTIPLE);
1124     old_ringparam->tx_desc_num = priv->ring[0].desc_num;
1125     old_ringparam->rx_desc_num = priv->ring[queue_num].desc_num;
1126     old_ringparam->rx_buf_len = priv->ring[queue_num].buf_size;
1127     new_ringparam->rx_buf_len = kernel_param->rx_buf_len;
1128 
1129     if (old_ringparam->tx_desc_num == new_ringparam->tx_desc_num &&
1130         old_ringparam->rx_desc_num == new_ringparam->rx_desc_num &&
1131         old_ringparam->rx_buf_len == new_ringparam->rx_buf_len) {
1132         netdev_info(ndev, "descriptor number and rx buffer length not changed\n");
1133         return false;
1134     }
1135 
1136     return true;
1137 }
1138 
1139 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len)
1140 {
1141     struct hns3_nic_priv *priv = netdev_priv(ndev);
1142     struct hnae3_handle *h = priv->ae_handle;
1143     int i;
1144 
1145     h->kinfo.rx_buf_len = rx_buf_len;
1146 
1147     for (i = 0; i < h->kinfo.num_tqps; i++) {
1148         h->kinfo.tqp[i]->buf_size = rx_buf_len;
1149         priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len;
1150     }
1151 
1152     return 0;
1153 }
1154 
1155 static int hns3_set_tx_push(struct net_device *netdev, u32 tx_push)
1156 {
1157     struct hns3_nic_priv *priv = netdev_priv(netdev);
1158     struct hnae3_handle *h = hns3_get_handle(netdev);
1159     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
1160     u32 old_state = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1161 
1162     if (!test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps) && tx_push)
1163         return -EOPNOTSUPP;
1164 
1165     if (tx_push == old_state)
1166         return 0;
1167 
1168     netdev_dbg(netdev, "Changing tx push from %s to %s\n",
1169            old_state ? "on" : "off", tx_push ? "on" : "off");
1170 
1171     if (tx_push)
1172         set_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1173     else
1174         clear_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1175 
1176     return 0;
1177 }
1178 
1179 static int hns3_set_ringparam(struct net_device *ndev,
1180                   struct ethtool_ringparam *param,
1181                   struct kernel_ethtool_ringparam *kernel_param,
1182                   struct netlink_ext_ack *extack)
1183 {
1184     struct hns3_ring_param old_ringparam, new_ringparam;
1185     struct hns3_nic_priv *priv = netdev_priv(ndev);
1186     struct hnae3_handle *h = priv->ae_handle;
1187     struct hns3_enet_ring *tmp_rings;
1188     bool if_running = netif_running(ndev);
1189     int ret, i;
1190 
1191     ret = hns3_check_ringparam(ndev, param, kernel_param);
1192     if (ret)
1193         return ret;
1194 
1195     ret = hns3_set_tx_push(ndev, kernel_param->tx_push);
1196     if (ret)
1197         return ret;
1198 
1199     if (!hns3_is_ringparam_changed(ndev, param, kernel_param,
1200                        &old_ringparam, &new_ringparam))
1201         return 0;
1202 
1203     tmp_rings = hns3_backup_ringparam(priv);
1204     if (!tmp_rings) {
1205         netdev_err(ndev, "backup ring param failed by allocating memory fail\n");
1206         return -ENOMEM;
1207     }
1208 
1209     netdev_info(ndev,
1210             "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %u to %u\n",
1211             old_ringparam.tx_desc_num, old_ringparam.rx_desc_num,
1212             new_ringparam.tx_desc_num, new_ringparam.rx_desc_num,
1213             old_ringparam.rx_buf_len, new_ringparam.rx_buf_len);
1214 
1215     if (if_running)
1216         ndev->netdev_ops->ndo_stop(ndev);
1217 
1218     hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
1219                     new_ringparam.rx_desc_num);
1220     hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len);
1221     ret = hns3_init_all_ring(priv);
1222     if (ret) {
1223         netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n",
1224                ret);
1225 
1226         hns3_change_rx_buf_len(ndev, old_ringparam.rx_buf_len);
1227         hns3_change_all_ring_bd_num(priv, old_ringparam.tx_desc_num,
1228                         old_ringparam.rx_desc_num);
1229         for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1230             memcpy(&priv->ring[i], &tmp_rings[i],
1231                    sizeof(struct hns3_enet_ring));
1232     } else {
1233         for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1234             hns3_fini_ring(&tmp_rings[i]);
1235     }
1236 
1237     kfree(tmp_rings);
1238 
1239     if (if_running)
1240         ret = ndev->netdev_ops->ndo_open(ndev);
1241 
1242     return ret;
1243 }
1244 
1245 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1246 {
1247     struct hnae3_handle *h = hns3_get_handle(netdev);
1248 
1249     switch (cmd->cmd) {
1250     case ETHTOOL_SRXFH:
1251         if (h->ae_algo->ops->set_rss_tuple)
1252             return h->ae_algo->ops->set_rss_tuple(h, cmd);
1253         return -EOPNOTSUPP;
1254     case ETHTOOL_SRXCLSRLINS:
1255         if (h->ae_algo->ops->add_fd_entry)
1256             return h->ae_algo->ops->add_fd_entry(h, cmd);
1257         return -EOPNOTSUPP;
1258     case ETHTOOL_SRXCLSRLDEL:
1259         if (h->ae_algo->ops->del_fd_entry)
1260             return h->ae_algo->ops->del_fd_entry(h, cmd);
1261         return -EOPNOTSUPP;
1262     default:
1263         return -EOPNOTSUPP;
1264     }
1265 }
1266 
1267 static int hns3_nway_reset(struct net_device *netdev)
1268 {
1269     struct hnae3_handle *handle = hns3_get_handle(netdev);
1270     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1271     struct phy_device *phy = netdev->phydev;
1272     int autoneg;
1273 
1274     if (!netif_running(netdev))
1275         return 0;
1276 
1277     if (hns3_nic_resetting(netdev)) {
1278         netdev_err(netdev, "dev resetting!");
1279         return -EBUSY;
1280     }
1281 
1282     if (!ops->get_autoneg || !ops->restart_autoneg)
1283         return -EOPNOTSUPP;
1284 
1285     autoneg = ops->get_autoneg(handle);
1286     if (autoneg != AUTONEG_ENABLE) {
1287         netdev_err(netdev,
1288                "Autoneg is off, don't support to restart it\n");
1289         return -EINVAL;
1290     }
1291 
1292     netif_dbg(handle, drv, netdev,
1293           "nway reset (using %s)\n", phy ? "phy" : "mac");
1294 
1295     if (phy)
1296         return genphy_restart_aneg(phy);
1297 
1298     return ops->restart_autoneg(handle);
1299 }
1300 
1301 static void hns3_get_channels(struct net_device *netdev,
1302                   struct ethtool_channels *ch)
1303 {
1304     struct hnae3_handle *h = hns3_get_handle(netdev);
1305 
1306     if (h->ae_algo->ops->get_channels)
1307         h->ae_algo->ops->get_channels(h, ch);
1308 }
1309 
1310 static int hns3_get_coalesce(struct net_device *netdev,
1311                  struct ethtool_coalesce *cmd,
1312                  struct kernel_ethtool_coalesce *kernel_coal,
1313                  struct netlink_ext_ack *extack)
1314 {
1315     struct hns3_nic_priv *priv = netdev_priv(netdev);
1316     struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1317     struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1318     struct hnae3_handle *h = priv->ae_handle;
1319 
1320     if (hns3_nic_resetting(netdev))
1321         return -EBUSY;
1322 
1323     cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1324     cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1325 
1326     cmd->tx_coalesce_usecs = tx_coal->int_gl;
1327     cmd->rx_coalesce_usecs = rx_coal->int_gl;
1328 
1329     cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1330     cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1331 
1332     cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1333     cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1334 
1335     kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1336                     DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1337     kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1338                     DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1339 
1340     return 0;
1341 }
1342 
1343 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1344                        struct ethtool_coalesce *cmd)
1345 {
1346     struct hnae3_handle *handle = hns3_get_handle(netdev);
1347     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1348     u32 rx_gl, tx_gl;
1349 
1350     if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1351         netdev_err(netdev,
1352                "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1353                ae_dev->dev_specs.max_int_gl);
1354         return -EINVAL;
1355     }
1356 
1357     if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1358         netdev_err(netdev,
1359                "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1360                ae_dev->dev_specs.max_int_gl);
1361         return -EINVAL;
1362     }
1363 
1364     /* device version above V3(include V3), GL uses 1us unit,
1365      * so the round down is not needed.
1366      */
1367     if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1368         return 0;
1369 
1370     rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1371     if (rx_gl != cmd->rx_coalesce_usecs) {
1372         netdev_info(netdev,
1373                 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1374                 cmd->rx_coalesce_usecs, rx_gl);
1375     }
1376 
1377     tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1378     if (tx_gl != cmd->tx_coalesce_usecs) {
1379         netdev_info(netdev,
1380                 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1381                 cmd->tx_coalesce_usecs, tx_gl);
1382     }
1383 
1384     return 0;
1385 }
1386 
1387 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1388                        struct ethtool_coalesce *cmd)
1389 {
1390     u32 rl;
1391 
1392     if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1393         netdev_err(netdev,
1394                "tx_usecs_high must be same as rx_usecs_high.\n");
1395         return -EINVAL;
1396     }
1397 
1398     if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1399         netdev_err(netdev,
1400                "Invalid usecs_high value, usecs_high range is 0-%d\n",
1401                HNS3_INT_RL_MAX);
1402         return -EINVAL;
1403     }
1404 
1405     rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1406     if (rl != cmd->rx_coalesce_usecs_high) {
1407         netdev_info(netdev,
1408                 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1409                 cmd->rx_coalesce_usecs_high, rl);
1410     }
1411 
1412     return 0;
1413 }
1414 
1415 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1416                     struct ethtool_coalesce *cmd)
1417 {
1418     struct hnae3_handle *handle = hns3_get_handle(netdev);
1419     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1420 
1421     if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1422         !ae_dev->dev_specs.int_ql_max) {
1423         netdev_err(netdev, "coalesced frames is not supported\n");
1424         return -EOPNOTSUPP;
1425     }
1426 
1427     if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1428         cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1429         netdev_err(netdev,
1430                "invalid coalesced_frames value, range is 0-%u\n",
1431                ae_dev->dev_specs.int_ql_max);
1432         return -ERANGE;
1433     }
1434 
1435     return 0;
1436 }
1437 
1438 static int
1439 hns3_check_cqe_coalesce_param(struct net_device *netdev,
1440                   struct kernel_ethtool_coalesce *kernel_coal)
1441 {
1442     struct hnae3_handle *handle = hns3_get_handle(netdev);
1443     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1444 
1445     if ((kernel_coal->use_cqe_mode_tx || kernel_coal->use_cqe_mode_rx) &&
1446         !hnae3_ae_dev_cq_supported(ae_dev)) {
1447         netdev_err(netdev, "coalesced cqe mode is not supported\n");
1448         return -EOPNOTSUPP;
1449     }
1450 
1451     return 0;
1452 }
1453 
1454 static int
1455 hns3_check_coalesce_para(struct net_device *netdev,
1456              struct ethtool_coalesce *cmd,
1457              struct kernel_ethtool_coalesce *kernel_coal)
1458 {
1459     int ret;
1460 
1461     ret = hns3_check_cqe_coalesce_param(netdev, kernel_coal);
1462     if (ret)
1463         return ret;
1464 
1465     ret = hns3_check_gl_coalesce_para(netdev, cmd);
1466     if (ret) {
1467         netdev_err(netdev,
1468                "Check gl coalesce param fail. ret = %d\n", ret);
1469         return ret;
1470     }
1471 
1472     ret = hns3_check_rl_coalesce_para(netdev, cmd);
1473     if (ret) {
1474         netdev_err(netdev,
1475                "Check rl coalesce param fail. ret = %d\n", ret);
1476         return ret;
1477     }
1478 
1479     return hns3_check_ql_coalesce_param(netdev, cmd);
1480 }
1481 
1482 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1483                     struct ethtool_coalesce *cmd,
1484                     u32 queue)
1485 {
1486     struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1487     struct hns3_nic_priv *priv = netdev_priv(netdev);
1488     struct hnae3_handle *h = priv->ae_handle;
1489     int queue_num = h->kinfo.num_tqps;
1490 
1491     tx_vector = priv->ring[queue].tqp_vector;
1492     rx_vector = priv->ring[queue_num + queue].tqp_vector;
1493 
1494     tx_vector->tx_group.coal.adapt_enable =
1495                 cmd->use_adaptive_tx_coalesce;
1496     rx_vector->rx_group.coal.adapt_enable =
1497                 cmd->use_adaptive_rx_coalesce;
1498 
1499     tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1500     rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1501 
1502     tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1503     rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1504 
1505     hns3_set_vector_coalesce_tx_gl(tx_vector,
1506                        tx_vector->tx_group.coal.int_gl);
1507     hns3_set_vector_coalesce_rx_gl(rx_vector,
1508                        rx_vector->rx_group.coal.int_gl);
1509 
1510     hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1511     hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1512 
1513     if (tx_vector->tx_group.coal.ql_enable)
1514         hns3_set_vector_coalesce_tx_ql(tx_vector,
1515                            tx_vector->tx_group.coal.int_ql);
1516     if (rx_vector->rx_group.coal.ql_enable)
1517         hns3_set_vector_coalesce_rx_ql(rx_vector,
1518                            rx_vector->rx_group.coal.int_ql);
1519 }
1520 
1521 static int hns3_set_coalesce(struct net_device *netdev,
1522                  struct ethtool_coalesce *cmd,
1523                  struct kernel_ethtool_coalesce *kernel_coal,
1524                  struct netlink_ext_ack *extack)
1525 {
1526     struct hnae3_handle *h = hns3_get_handle(netdev);
1527     struct hns3_nic_priv *priv = netdev_priv(netdev);
1528     struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1529     struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1530     u16 queue_num = h->kinfo.num_tqps;
1531     enum dim_cq_period_mode tx_mode;
1532     enum dim_cq_period_mode rx_mode;
1533     int ret;
1534     int i;
1535 
1536     if (hns3_nic_resetting(netdev))
1537         return -EBUSY;
1538 
1539     ret = hns3_check_coalesce_para(netdev, cmd, kernel_coal);
1540     if (ret)
1541         return ret;
1542 
1543     h->kinfo.int_rl_setting =
1544         hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1545 
1546     tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1547     rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1548 
1549     tx_coal->int_gl = cmd->tx_coalesce_usecs;
1550     rx_coal->int_gl = cmd->rx_coalesce_usecs;
1551 
1552     tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1553     rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1554 
1555     for (i = 0; i < queue_num; i++)
1556         hns3_set_coalesce_per_queue(netdev, cmd, i);
1557 
1558     tx_mode = kernel_coal->use_cqe_mode_tx ?
1559           DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1560           DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1561     rx_mode = kernel_coal->use_cqe_mode_rx ?
1562           DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1563           DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1564     hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1565 
1566     return 0;
1567 }
1568 
1569 static int hns3_get_regs_len(struct net_device *netdev)
1570 {
1571     struct hnae3_handle *h = hns3_get_handle(netdev);
1572 
1573     if (!h->ae_algo->ops->get_regs_len)
1574         return -EOPNOTSUPP;
1575 
1576     return h->ae_algo->ops->get_regs_len(h);
1577 }
1578 
1579 static void hns3_get_regs(struct net_device *netdev,
1580               struct ethtool_regs *cmd, void *data)
1581 {
1582     struct hnae3_handle *h = hns3_get_handle(netdev);
1583 
1584     if (!h->ae_algo->ops->get_regs)
1585         return;
1586 
1587     h->ae_algo->ops->get_regs(h, &cmd->version, data);
1588 }
1589 
1590 static int hns3_set_phys_id(struct net_device *netdev,
1591                 enum ethtool_phys_id_state state)
1592 {
1593     struct hnae3_handle *h = hns3_get_handle(netdev);
1594 
1595     if (!h->ae_algo->ops->set_led_id)
1596         return -EOPNOTSUPP;
1597 
1598     return h->ae_algo->ops->set_led_id(h, state);
1599 }
1600 
1601 static u32 hns3_get_msglevel(struct net_device *netdev)
1602 {
1603     struct hnae3_handle *h = hns3_get_handle(netdev);
1604 
1605     return h->msg_enable;
1606 }
1607 
1608 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1609 {
1610     struct hnae3_handle *h = hns3_get_handle(netdev);
1611 
1612     h->msg_enable = msg_level;
1613 }
1614 
1615 /* Translate local fec value into ethtool value. */
1616 static unsigned int loc_to_eth_fec(u8 loc_fec)
1617 {
1618     u32 eth_fec = 0;
1619 
1620     if (loc_fec & BIT(HNAE3_FEC_AUTO))
1621         eth_fec |= ETHTOOL_FEC_AUTO;
1622     if (loc_fec & BIT(HNAE3_FEC_RS))
1623         eth_fec |= ETHTOOL_FEC_RS;
1624     if (loc_fec & BIT(HNAE3_FEC_BASER))
1625         eth_fec |= ETHTOOL_FEC_BASER;
1626 
1627     /* if nothing is set, then FEC is off */
1628     if (!eth_fec)
1629         eth_fec = ETHTOOL_FEC_OFF;
1630 
1631     return eth_fec;
1632 }
1633 
1634 /* Translate ethtool fec value into local value. */
1635 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1636 {
1637     u32 loc_fec = 0;
1638 
1639     if (eth_fec & ETHTOOL_FEC_OFF)
1640         return loc_fec;
1641 
1642     if (eth_fec & ETHTOOL_FEC_AUTO)
1643         loc_fec |= BIT(HNAE3_FEC_AUTO);
1644     if (eth_fec & ETHTOOL_FEC_RS)
1645         loc_fec |= BIT(HNAE3_FEC_RS);
1646     if (eth_fec & ETHTOOL_FEC_BASER)
1647         loc_fec |= BIT(HNAE3_FEC_BASER);
1648 
1649     return loc_fec;
1650 }
1651 
1652 static int hns3_get_fecparam(struct net_device *netdev,
1653                  struct ethtool_fecparam *fec)
1654 {
1655     struct hnae3_handle *handle = hns3_get_handle(netdev);
1656     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1657     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1658     u8 fec_ability;
1659     u8 fec_mode;
1660 
1661     if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1662         return -EOPNOTSUPP;
1663 
1664     if (!ops->get_fec)
1665         return -EOPNOTSUPP;
1666 
1667     ops->get_fec(handle, &fec_ability, &fec_mode);
1668 
1669     fec->fec = loc_to_eth_fec(fec_ability);
1670     fec->active_fec = loc_to_eth_fec(fec_mode);
1671 
1672     return 0;
1673 }
1674 
1675 static int hns3_set_fecparam(struct net_device *netdev,
1676                  struct ethtool_fecparam *fec)
1677 {
1678     struct hnae3_handle *handle = hns3_get_handle(netdev);
1679     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1680     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1681     u32 fec_mode;
1682 
1683     if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1684         return -EOPNOTSUPP;
1685 
1686     if (!ops->set_fec)
1687         return -EOPNOTSUPP;
1688     fec_mode = eth_to_loc_fec(fec->fec);
1689 
1690     netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1691 
1692     return ops->set_fec(handle, fec_mode);
1693 }
1694 
1695 static int hns3_get_module_info(struct net_device *netdev,
1696                 struct ethtool_modinfo *modinfo)
1697 {
1698 #define HNS3_SFF_8636_V1_3 0x03
1699 
1700     struct hnae3_handle *handle = hns3_get_handle(netdev);
1701     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1702     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1703     struct hns3_sfp_type sfp_type;
1704     int ret;
1705 
1706     if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1707         !ops->get_module_eeprom)
1708         return -EOPNOTSUPP;
1709 
1710     memset(&sfp_type, 0, sizeof(sfp_type));
1711     ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1712                      (u8 *)&sfp_type);
1713     if (ret)
1714         return ret;
1715 
1716     switch (sfp_type.type) {
1717     case SFF8024_ID_SFP:
1718         modinfo->type = ETH_MODULE_SFF_8472;
1719         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1720         break;
1721     case SFF8024_ID_QSFP_8438:
1722         modinfo->type = ETH_MODULE_SFF_8436;
1723         modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1724         break;
1725     case SFF8024_ID_QSFP_8436_8636:
1726         if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1727             modinfo->type = ETH_MODULE_SFF_8436;
1728             modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1729         } else {
1730             modinfo->type = ETH_MODULE_SFF_8636;
1731             modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1732         }
1733         break;
1734     case SFF8024_ID_QSFP28_8636:
1735         modinfo->type = ETH_MODULE_SFF_8636;
1736         modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1737         break;
1738     default:
1739         netdev_err(netdev, "Optical module unknown: %#x\n",
1740                sfp_type.type);
1741         return -EINVAL;
1742     }
1743 
1744     return 0;
1745 }
1746 
1747 static int hns3_get_module_eeprom(struct net_device *netdev,
1748                   struct ethtool_eeprom *ee, u8 *data)
1749 {
1750     struct hnae3_handle *handle = hns3_get_handle(netdev);
1751     struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1752     const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1753 
1754     if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1755         !ops->get_module_eeprom)
1756         return -EOPNOTSUPP;
1757 
1758     if (!ee->len)
1759         return -EINVAL;
1760 
1761     memset(data, 0, ee->len);
1762 
1763     return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1764 }
1765 
1766 static u32 hns3_get_priv_flags(struct net_device *netdev)
1767 {
1768     struct hnae3_handle *handle = hns3_get_handle(netdev);
1769 
1770     return handle->priv_flags;
1771 }
1772 
1773 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1774 {
1775     u32 i;
1776 
1777     for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1778         if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1779             netdev_err(h->netdev, "%s is unsupported\n",
1780                    hns3_priv_flags[i].name);
1781             return -EOPNOTSUPP;
1782         }
1783 
1784     return 0;
1785 }
1786 
1787 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1788 {
1789     struct hnae3_handle *handle = hns3_get_handle(netdev);
1790     u32 changed = pflags ^ handle->priv_flags;
1791     int ret;
1792     u32 i;
1793 
1794     ret = hns3_check_priv_flags(handle, changed);
1795     if (ret)
1796         return ret;
1797 
1798     for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1799         if (changed & BIT(i)) {
1800             bool enable = !(handle->priv_flags & BIT(i));
1801 
1802             if (enable)
1803                 handle->priv_flags |= BIT(i);
1804             else
1805                 handle->priv_flags &= ~BIT(i);
1806             hns3_priv_flags[i].handler(netdev, enable);
1807         }
1808     }
1809 
1810     return 0;
1811 }
1812 
1813 static int hns3_get_tunable(struct net_device *netdev,
1814                 const struct ethtool_tunable *tuna,
1815                 void *data)
1816 {
1817     struct hns3_nic_priv *priv = netdev_priv(netdev);
1818     struct hnae3_handle *h = priv->ae_handle;
1819     int ret = 0;
1820 
1821     switch (tuna->id) {
1822     case ETHTOOL_TX_COPYBREAK:
1823         /* all the tx rings have the same tx_copybreak */
1824         *(u32 *)data = priv->tx_copybreak;
1825         break;
1826     case ETHTOOL_RX_COPYBREAK:
1827         *(u32 *)data = priv->rx_copybreak;
1828         break;
1829     case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1830         *(u32 *)data = h->kinfo.tx_spare_buf_size;
1831         break;
1832     default:
1833         ret = -EOPNOTSUPP;
1834         break;
1835     }
1836 
1837     return ret;
1838 }
1839 
1840 static int hns3_set_tx_spare_buf_size(struct net_device *netdev,
1841                       u32 data)
1842 {
1843     struct hns3_nic_priv *priv = netdev_priv(netdev);
1844     struct hnae3_handle *h = priv->ae_handle;
1845     int ret;
1846 
1847     h->kinfo.tx_spare_buf_size = data;
1848 
1849     ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
1850     if (ret)
1851         return ret;
1852 
1853     ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1854     if (ret)
1855         return ret;
1856 
1857     ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT);
1858     if (ret)
1859         return ret;
1860 
1861     ret = hns3_reset_notify(h, HNAE3_UP_CLIENT);
1862     if (ret)
1863         hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1864 
1865     return ret;
1866 }
1867 
1868 static int hns3_set_tunable(struct net_device *netdev,
1869                 const struct ethtool_tunable *tuna,
1870                 const void *data)
1871 {
1872     struct hns3_nic_priv *priv = netdev_priv(netdev);
1873     u32 old_tx_spare_buf_size, new_tx_spare_buf_size;
1874     struct hnae3_handle *h = priv->ae_handle;
1875     int i, ret = 0;
1876 
1877     if (hns3_nic_resetting(netdev) || !priv->ring) {
1878         netdev_err(netdev, "failed to set tunable value, dev resetting!");
1879         return -EBUSY;
1880     }
1881 
1882     switch (tuna->id) {
1883     case ETHTOOL_TX_COPYBREAK:
1884         priv->tx_copybreak = *(u32 *)data;
1885 
1886         for (i = 0; i < h->kinfo.num_tqps; i++)
1887             priv->ring[i].tx_copybreak = priv->tx_copybreak;
1888 
1889         break;
1890     case ETHTOOL_RX_COPYBREAK:
1891         priv->rx_copybreak = *(u32 *)data;
1892 
1893         for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1894             priv->ring[i].rx_copybreak = priv->rx_copybreak;
1895 
1896         break;
1897     case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1898         old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size;
1899         new_tx_spare_buf_size = *(u32 *)data;
1900         netdev_info(netdev, "request to set tx spare buf size from %u to %u\n",
1901                 old_tx_spare_buf_size, new_tx_spare_buf_size);
1902         ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size);
1903         if (ret ||
1904             (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) {
1905             int ret1;
1906 
1907             netdev_warn(netdev, "change tx spare buf size fail, revert to old value\n");
1908             ret1 = hns3_set_tx_spare_buf_size(netdev,
1909                               old_tx_spare_buf_size);
1910             if (ret1) {
1911                 netdev_err(netdev, "revert to old tx spare buf size fail\n");
1912                 return ret1;
1913             }
1914 
1915             return ret;
1916         }
1917 
1918         if (!priv->ring->tx_spare)
1919             netdev_info(netdev, "the active tx spare buf size is 0, disable tx spare buffer\n");
1920         else
1921             netdev_info(netdev, "the active tx spare buf size is %u, due to page order\n",
1922                     priv->ring->tx_spare->len);
1923 
1924         break;
1925     default:
1926         ret = -EOPNOTSUPP;
1927         break;
1928     }
1929 
1930     return ret;
1931 }
1932 
1933 #define HNS3_ETHTOOL_COALESCE   (ETHTOOL_COALESCE_USECS |       \
1934                  ETHTOOL_COALESCE_USE_ADAPTIVE |    \
1935                  ETHTOOL_COALESCE_RX_USECS_HIGH |   \
1936                  ETHTOOL_COALESCE_TX_USECS_HIGH |   \
1937                  ETHTOOL_COALESCE_MAX_FRAMES |      \
1938                  ETHTOOL_COALESCE_USE_CQE)
1939 
1940 #define HNS3_ETHTOOL_RING   (ETHTOOL_RING_USE_RX_BUF_LEN |      \
1941                  ETHTOOL_RING_USE_TX_PUSH)
1942 
1943 static int hns3_get_ts_info(struct net_device *netdev,
1944                 struct ethtool_ts_info *info)
1945 {
1946     struct hnae3_handle *handle = hns3_get_handle(netdev);
1947 
1948     if (handle->ae_algo->ops->get_ts_info)
1949         return handle->ae_algo->ops->get_ts_info(handle, info);
1950 
1951     return ethtool_op_get_ts_info(netdev, info);
1952 }
1953 
1954 static const struct hns3_ethtool_link_ext_state_mapping
1955 hns3_link_ext_state_map[] = {
1956     {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1957         ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1958     {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1959         ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1960 
1961     {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1962         ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1963     {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1964         ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1965     {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1966         ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1967 
1968     {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1969         ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1970     {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1971         ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1972     {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1973         ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1974 
1975     {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1976         ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1977     {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1978         ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1979     {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1980         ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1981 
1982     {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1983     {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1984         ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1985 
1986     {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1987 };
1988 
1989 static int hns3_get_link_ext_state(struct net_device *netdev,
1990                    struct ethtool_link_ext_state_info *info)
1991 {
1992     const struct hns3_ethtool_link_ext_state_mapping *map;
1993     struct hnae3_handle *h = hns3_get_handle(netdev);
1994     u32 status_code, i;
1995     int ret;
1996 
1997     if (netif_carrier_ok(netdev))
1998         return -ENODATA;
1999 
2000     if (!h->ae_algo->ops->get_link_diagnosis_info)
2001         return -EOPNOTSUPP;
2002 
2003     ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
2004     if (ret)
2005         return ret;
2006 
2007     for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
2008         map = &hns3_link_ext_state_map[i];
2009         if (map->status_code == status_code) {
2010             info->link_ext_state = map->link_ext_state;
2011             info->__link_ext_substate = map->link_ext_substate;
2012             return 0;
2013         }
2014     }
2015 
2016     return -ENODATA;
2017 }
2018 
2019 static const struct ethtool_ops hns3vf_ethtool_ops = {
2020     .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2021     .supported_ring_params = HNS3_ETHTOOL_RING,
2022     .get_drvinfo = hns3_get_drvinfo,
2023     .get_ringparam = hns3_get_ringparam,
2024     .set_ringparam = hns3_set_ringparam,
2025     .get_strings = hns3_get_strings,
2026     .get_ethtool_stats = hns3_get_stats,
2027     .get_sset_count = hns3_get_sset_count,
2028     .get_rxnfc = hns3_get_rxnfc,
2029     .set_rxnfc = hns3_set_rxnfc,
2030     .get_rxfh_key_size = hns3_get_rss_key_size,
2031     .get_rxfh_indir_size = hns3_get_rss_indir_size,
2032     .get_rxfh = hns3_get_rss,
2033     .set_rxfh = hns3_set_rss,
2034     .get_link_ksettings = hns3_get_link_ksettings,
2035     .get_channels = hns3_get_channels,
2036     .set_channels = hns3_set_channels,
2037     .get_coalesce = hns3_get_coalesce,
2038     .set_coalesce = hns3_set_coalesce,
2039     .get_regs_len = hns3_get_regs_len,
2040     .get_regs = hns3_get_regs,
2041     .get_link = hns3_get_link,
2042     .get_msglevel = hns3_get_msglevel,
2043     .set_msglevel = hns3_set_msglevel,
2044     .get_priv_flags = hns3_get_priv_flags,
2045     .set_priv_flags = hns3_set_priv_flags,
2046     .get_tunable = hns3_get_tunable,
2047     .set_tunable = hns3_set_tunable,
2048     .reset = hns3_set_reset,
2049 };
2050 
2051 static const struct ethtool_ops hns3_ethtool_ops = {
2052     .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2053     .supported_ring_params = HNS3_ETHTOOL_RING,
2054     .self_test = hns3_self_test,
2055     .get_drvinfo = hns3_get_drvinfo,
2056     .get_link = hns3_get_link,
2057     .get_ringparam = hns3_get_ringparam,
2058     .set_ringparam = hns3_set_ringparam,
2059     .get_pauseparam = hns3_get_pauseparam,
2060     .set_pauseparam = hns3_set_pauseparam,
2061     .get_strings = hns3_get_strings,
2062     .get_ethtool_stats = hns3_get_stats,
2063     .get_sset_count = hns3_get_sset_count,
2064     .get_rxnfc = hns3_get_rxnfc,
2065     .set_rxnfc = hns3_set_rxnfc,
2066     .get_rxfh_key_size = hns3_get_rss_key_size,
2067     .get_rxfh_indir_size = hns3_get_rss_indir_size,
2068     .get_rxfh = hns3_get_rss,
2069     .set_rxfh = hns3_set_rss,
2070     .get_link_ksettings = hns3_get_link_ksettings,
2071     .set_link_ksettings = hns3_set_link_ksettings,
2072     .nway_reset = hns3_nway_reset,
2073     .get_channels = hns3_get_channels,
2074     .set_channels = hns3_set_channels,
2075     .get_coalesce = hns3_get_coalesce,
2076     .set_coalesce = hns3_set_coalesce,
2077     .get_regs_len = hns3_get_regs_len,
2078     .get_regs = hns3_get_regs,
2079     .set_phys_id = hns3_set_phys_id,
2080     .get_msglevel = hns3_get_msglevel,
2081     .set_msglevel = hns3_set_msglevel,
2082     .get_fecparam = hns3_get_fecparam,
2083     .set_fecparam = hns3_set_fecparam,
2084     .get_module_info = hns3_get_module_info,
2085     .get_module_eeprom = hns3_get_module_eeprom,
2086     .get_priv_flags = hns3_get_priv_flags,
2087     .set_priv_flags = hns3_set_priv_flags,
2088     .get_ts_info = hns3_get_ts_info,
2089     .get_tunable = hns3_get_tunable,
2090     .set_tunable = hns3_set_tunable,
2091     .reset = hns3_set_reset,
2092     .get_link_ext_state = hns3_get_link_ext_state,
2093 };
2094 
2095 void hns3_ethtool_set_ops(struct net_device *netdev)
2096 {
2097     struct hnae3_handle *h = hns3_get_handle(netdev);
2098 
2099     if (h->flags & HNAE3_SUPPORT_VF)
2100         netdev->ethtool_ops = &hns3vf_ethtool_ops;
2101     else
2102         netdev->ethtool_ops = &hns3_ethtool_ops;
2103 }