linux/drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2// Copyright (c) 2016-2017 Hisilicon Limited.
   3
   4#include <linux/etherdevice.h>
   5#include <linux/string.h>
   6#include <linux/phy.h>
   7#include <linux/sfp.h>
   8
   9#include "hns3_enet.h"
  10#include "hns3_ethtool.h"
  11
  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),   \
  17}
  18
  19static 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),
  41};
  42
  43#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
  44
  45static 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),
  62};
  63
  64#define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
  65
  66#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
  67
  68#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
  69
  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
  75
  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
  80
  81static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
  82{
  83        struct hnae3_handle *h = hns3_get_handle(ndev);
  84        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
  85        int ret;
  86
  87        if (!h->ae_algo->ops->set_loopback ||
  88            !h->ae_algo->ops->set_promisc_mode)
  89                return -EOPNOTSUPP;
  90
  91        switch (loop) {
  92        case HNAE3_LOOP_SERIAL_SERDES:
  93        case HNAE3_LOOP_PARALLEL_SERDES:
  94        case HNAE3_LOOP_APP:
  95        case HNAE3_LOOP_PHY:
  96                ret = h->ae_algo->ops->set_loopback(h, loop, en);
  97                break;
  98        default:
  99                ret = -ENOTSUPP;
 100                break;
 101        }
 102
 103        if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
 104                return ret;
 105
 106        if (en)
 107                h->ae_algo->ops->set_promisc_mode(h, true, true);
 108        else
 109                /* recover promisc mode before loopback test */
 110                hns3_request_update_promisc_mode(h);
 111
 112        return ret;
 113}
 114
 115static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
 116{
 117        struct hnae3_handle *h = hns3_get_handle(ndev);
 118        int ret;
 119
 120        ret = hns3_nic_reset_all_ring(h);
 121        if (ret)
 122                return ret;
 123
 124        ret = hns3_lp_setup(ndev, loop_mode, true);
 125        usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
 126
 127        return ret;
 128}
 129
 130static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
 131{
 132        int ret;
 133
 134        ret = hns3_lp_setup(ndev, loop_mode, false);
 135        if (ret) {
 136                netdev_err(ndev, "lb_setup return error: %d\n", ret);
 137                return ret;
 138        }
 139
 140        usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
 141
 142        return 0;
 143}
 144
 145static void hns3_lp_setup_skb(struct sk_buff *skb)
 146{
 147#define HNS3_NIC_LB_DST_MAC_ADDR        0x1f
 148
 149        struct net_device *ndev = skb->dev;
 150        struct hnae3_handle *handle;
 151        struct hnae3_ae_dev *ae_dev;
 152        unsigned char *packet;
 153        struct ethhdr *ethh;
 154        unsigned int i;
 155
 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);
 159
 160        memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
 161
 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.
 166         */
 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);
 174
 175        for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
 176                packet[i] = (unsigned char)(i & 0xff);
 177}
 178
 179static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
 180                                   struct sk_buff *skb)
 181{
 182        struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
 183        unsigned char *packet = skb->data;
 184        u32 len = skb_headlen(skb);
 185        u32 i;
 186
 187        len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
 188
 189        for (i = 0; i < len; i++)
 190                if (packet[i] != (unsigned char)(i & 0xff))
 191                        break;
 192
 193        /* The packet is correctly received */
 194        if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
 195                tqp_vector->rx_group.total_packets++;
 196        else
 197                print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
 198                               skb->data, len, true);
 199
 200        dev_kfree_skb_any(skb);
 201}
 202
 203static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
 204{
 205        struct hnae3_handle *h = priv->ae_handle;
 206        struct hnae3_knic_private_info *kinfo;
 207        u32 i, rcv_good_pkt_total = 0;
 208
 209        kinfo = &h->kinfo;
 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;
 213                u64 pre_rx_pkt;
 214
 215                rx_group = &ring->tqp_vector->rx_group;
 216                pre_rx_pkt = rx_group->total_packets;
 217
 218                preempt_disable();
 219                hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
 220                preempt_enable();
 221
 222                rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
 223                rx_group->total_packets = pre_rx_pkt;
 224        }
 225        return rcv_good_pkt_total;
 226}
 227
 228static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
 229                                  u32 end_ringid, u32 budget)
 230{
 231        u32 i;
 232
 233        for (i = start_ringid; i <= end_ringid; i++) {
 234                struct hns3_enet_ring *ring = &priv->ring[i];
 235
 236                hns3_clean_tx_ring(ring, 0);
 237        }
 238}
 239
 240/**
 241 * hns3_lp_run_test -  run loopback test
 242 * @ndev: net device
 243 * @mode: loopback type
 244 */
 245static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
 246{
 247        struct hns3_nic_priv *priv = netdev_priv(ndev);
 248        struct sk_buff *skb;
 249        u32 i, good_cnt;
 250        int ret_val = 0;
 251
 252        skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
 253                        GFP_KERNEL);
 254        if (!skb)
 255                return HNS3_NIC_LB_TEST_NO_MEM_ERR;
 256
 257        skb->dev = ndev;
 258        hns3_lp_setup_skb(skb);
 259        skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
 260
 261        good_cnt = 0;
 262        for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
 263                netdev_tx_t tx_ret;
 264
 265                skb_get(skb);
 266                tx_ret = hns3_nic_net_xmit(skb, ndev);
 267                if (tx_ret == NETDEV_TX_OK) {
 268                        good_cnt++;
 269                } else {
 270                        kfree_skb(skb);
 271                        netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
 272                                   tx_ret);
 273                }
 274        }
 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);
 279                goto out;
 280        }
 281
 282        /* Allow 200 milliseconds for packets to go from Tx to Rx */
 283        msleep(200);
 284
 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);
 290        }
 291
 292out:
 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);
 296
 297        kfree_skb(skb);
 298        return ret_val;
 299}
 300
 301static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
 302{
 303        st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
 304        st_param[HNAE3_LOOP_APP][1] =
 305                        h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
 306
 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;
 310
 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;
 315
 316        st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
 317        st_param[HNAE3_LOOP_PHY][1] =
 318                        h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
 319}
 320
 321static void hns3_selftest_prepare(struct net_device *ndev,
 322                                  bool if_running, int (*st_param)[2])
 323{
 324        struct hns3_nic_priv *priv = netdev_priv(ndev);
 325        struct hnae3_handle *h = priv->ae_handle;
 326
 327        if (netif_msg_ifdown(h))
 328                netdev_info(ndev, "self test start\n");
 329
 330        hns3_set_selftest_param(h, st_param);
 331
 332        if (if_running)
 333                ndev->netdev_ops->ndo_stop(ndev);
 334
 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);
 340#endif
 341
 342        /* Tell firmware to stop mac autoneg before loopback test start,
 343         * otherwise loopback test may be failed when the port is still
 344         * negotiating.
 345         */
 346        if (h->ae_algo->ops->halt_autoneg)
 347                h->ae_algo->ops->halt_autoneg(h, true);
 348
 349        set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
 350}
 351
 352static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
 353{
 354        struct hns3_nic_priv *priv = netdev_priv(ndev);
 355        struct hnae3_handle *h = priv->ae_handle;
 356
 357        clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
 358
 359        if (h->ae_algo->ops->halt_autoneg)
 360                h->ae_algo->ops->halt_autoneg(h, false);
 361
 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);
 366#endif
 367
 368        if (if_running)
 369                ndev->netdev_ops->ndo_open(ndev);
 370
 371        if (netif_msg_ifdown(h))
 372                netdev_info(ndev, "self test end\n");
 373}
 374
 375static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
 376                             struct ethtool_test *eth_test, u64 *data)
 377{
 378        int test_index = 0;
 379        u32 i;
 380
 381        for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
 382                enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
 383
 384                if (!st_param[i][1])
 385                        continue;
 386
 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);
 390
 391                hns3_lp_down(ndev, loop_type);
 392
 393                if (data[test_index])
 394                        eth_test->flags |= ETH_TEST_FL_FAILED;
 395
 396                test_index++;
 397        }
 398}
 399
 400/**
 401 * hns3_nic_self_test - self test
 402 * @ndev: net device
 403 * @eth_test: test cmd
 404 * @data: test result
 405 */
 406static void hns3_self_test(struct net_device *ndev,
 407                           struct ethtool_test *eth_test, u64 *data)
 408{
 409        int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
 410        bool if_running = netif_running(ndev);
 411
 412        if (hns3_nic_resetting(ndev)) {
 413                netdev_err(ndev, "dev resetting!");
 414                return;
 415        }
 416
 417        /* Only do offline selftest, or pass by default */
 418        if (eth_test->flags != ETH_TEST_FL_OFFLINE)
 419                return;
 420
 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);
 424}
 425
 426static void hns3_update_limit_promisc_mode(struct net_device *netdev,
 427                                           bool enable)
 428{
 429        struct hnae3_handle *handle = hns3_get_handle(netdev);
 430
 431        if (enable)
 432                set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
 433        else
 434                clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
 435
 436        hns3_request_update_promisc_mode(handle);
 437}
 438
 439static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
 440        { "limit_promisc",      hns3_update_limit_promisc_mode }
 441};
 442
 443static int hns3_get_sset_count(struct net_device *netdev, int stringset)
 444{
 445        struct hnae3_handle *h = hns3_get_handle(netdev);
 446        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 447
 448        if (!ops->get_sset_count)
 449                return -EOPNOTSUPP;
 450
 451        switch (stringset) {
 452        case ETH_SS_STATS:
 453                return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
 454                        ops->get_sset_count(h, stringset));
 455
 456        case ETH_SS_TEST:
 457                return ops->get_sset_count(h, stringset);
 458
 459        case ETH_SS_PRIV_FLAGS:
 460                return HNAE3_PFLAG_MAX;
 461
 462        default:
 463                return -EOPNOTSUPP;
 464        }
 465}
 466
 467static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
 468                u32 stat_count, u32 num_tqps, const char *prefix)
 469{
 470#define MAX_PREFIX_SIZE (6 + 4)
 471        u32 size_left;
 472        u32 i, j;
 473        u32 n1;
 474
 475        for (i = 0; i < num_tqps; i++) {
 476                for (j = 0; j < stat_count; j++) {
 477                        data[ETH_GSTRING_LEN - 1] = '\0';
 478
 479                        /* first, prepend the prefix string */
 480                        n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
 481                                       prefix, i);
 482                        size_left = (ETH_GSTRING_LEN - 1) - n1;
 483
 484                        /* now, concatenate the stats string to it */
 485                        strncat(data, stats[j].stats_string, size_left);
 486                        data += ETH_GSTRING_LEN;
 487                }
 488        }
 489
 490        return data;
 491}
 492
 493static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
 494{
 495        struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 496        const char tx_prefix[] = "txq";
 497        const char rx_prefix[] = "rxq";
 498
 499        /* get strings for Tx */
 500        data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
 501                                   kinfo->num_tqps, tx_prefix);
 502
 503        /* get strings for Rx */
 504        data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
 505                                   kinfo->num_tqps, rx_prefix);
 506
 507        return data;
 508}
 509
 510static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 511{
 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;
 515        int i;
 516
 517        if (!ops->get_strings)
 518                return;
 519
 520        switch (stringset) {
 521        case ETH_SS_STATS:
 522                buff = hns3_get_strings_tqps(h, buff);
 523                ops->get_strings(h, stringset, (u8 *)buff);
 524                break;
 525        case ETH_SS_TEST:
 526                ops->get_strings(h, stringset, data);
 527                break;
 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;
 533                }
 534                break;
 535        default:
 536                break;
 537        }
 538}
 539
 540static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
 541{
 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;
 545        u8 *stat;
 546        int i, j;
 547
 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;
 554                }
 555        }
 556
 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;
 563                }
 564        }
 565
 566        return data;
 567}
 568
 569/* hns3_get_stats - get detail statistics.
 570 * @netdev: net device
 571 * @stats: statistics info.
 572 * @data: statistics data.
 573 */
 574static void hns3_get_stats(struct net_device *netdev,
 575                           struct ethtool_stats *stats, u64 *data)
 576{
 577        struct hnae3_handle *h = hns3_get_handle(netdev);
 578        u64 *p = data;
 579
 580        if (hns3_nic_resetting(netdev)) {
 581                netdev_err(netdev, "dev resetting, could not get stats\n");
 582                return;
 583        }
 584
 585        if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
 586                netdev_err(netdev, "could not get any statistics\n");
 587                return;
 588        }
 589
 590        h->ae_algo->ops->update_stats(h, &netdev->stats);
 591
 592        /* get per-queue stats */
 593        p = hns3_get_stats_tqps(h, p);
 594
 595        /* get MAC & other misc hardware stats */
 596        h->ae_algo->ops->get_stats(h, p);
 597}
 598
 599static void hns3_get_drvinfo(struct net_device *netdev,
 600                             struct ethtool_drvinfo *drvinfo)
 601{
 602        struct hns3_nic_priv *priv = netdev_priv(netdev);
 603        struct hnae3_handle *h = priv->ae_handle;
 604        u32 fw_version;
 605
 606        if (!h->ae_algo->ops->get_fw_version) {
 607                netdev_err(netdev, "could not get fw version!\n");
 608                return;
 609        }
 610
 611        strncpy(drvinfo->driver, h->pdev->driver->name,
 612                sizeof(drvinfo->driver));
 613        drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
 614
 615        strncpy(drvinfo->bus_info, pci_name(h->pdev),
 616                sizeof(drvinfo->bus_info));
 617        drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
 618
 619        fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
 620
 621        snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 622                 "%lu.%lu.%lu.%lu",
 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));
 631}
 632
 633static u32 hns3_get_link(struct net_device *netdev)
 634{
 635        struct hnae3_handle *h = hns3_get_handle(netdev);
 636
 637        if (h->ae_algo->ops->get_status)
 638                return h->ae_algo->ops->get_status(h);
 639        else
 640                return 0;
 641}
 642
 643static void hns3_get_ringparam(struct net_device *netdev,
 644                               struct ethtool_ringparam *param)
 645{
 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;
 649
 650        if (hns3_nic_resetting(netdev)) {
 651                netdev_err(netdev, "dev resetting!");
 652                return;
 653        }
 654
 655        param->tx_max_pending = HNS3_RING_MAX_PENDING;
 656        param->rx_max_pending = HNS3_RING_MAX_PENDING;
 657
 658        param->tx_pending = priv->ring[0].desc_num;
 659        param->rx_pending = priv->ring[queue_num].desc_num;
 660}
 661
 662static void hns3_get_pauseparam(struct net_device *netdev,
 663                                struct ethtool_pauseparam *param)
 664{
 665        struct hnae3_handle *h = hns3_get_handle(netdev);
 666        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
 667
 668        if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
 669                return;
 670
 671        if (h->ae_algo->ops->get_pauseparam)
 672                h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
 673                        &param->rx_pause, &param->tx_pause);
 674}
 675
 676static int hns3_set_pauseparam(struct net_device *netdev,
 677                               struct ethtool_pauseparam *param)
 678{
 679        struct hnae3_handle *h = hns3_get_handle(netdev);
 680        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
 681
 682        if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
 683                return -EOPNOTSUPP;
 684
 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);
 688
 689        if (h->ae_algo->ops->set_pauseparam)
 690                return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
 691                                                       param->rx_pause,
 692                                                       param->tx_pause);
 693        return -EOPNOTSUPP;
 694}
 695
 696static void hns3_get_ksettings(struct hnae3_handle *h,
 697                               struct ethtool_link_ksettings *cmd)
 698{
 699        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 700
 701        /* 1.auto_neg & speed & duplex from cmd */
 702        if (ops->get_ksettings_an_result)
 703                ops->get_ksettings_an_result(h,
 704                                             &cmd->base.autoneg,
 705                                             &cmd->base.speed,
 706                                             &cmd->base.duplex);
 707
 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);
 713
 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);
 718}
 719
 720static int hns3_get_link_ksettings(struct net_device *netdev,
 721                                   struct ethtool_link_ksettings *cmd)
 722{
 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;
 726        u8 module_type;
 727        u8 media_type;
 728        u8 link_stat;
 729
 730        ops = h->ae_algo->ops;
 731        if (ops->get_media_type)
 732                ops->get_media_type(h, &media_type, &module_type);
 733        else
 734                return -EOPNOTSUPP;
 735
 736        switch (media_type) {
 737        case HNAE3_MEDIA_TYPE_NONE:
 738                cmd->base.port = PORT_NONE;
 739                hns3_get_ksettings(h, cmd);
 740                break;
 741        case HNAE3_MEDIA_TYPE_FIBER:
 742                if (module_type == HNAE3_MODULE_TYPE_CR)
 743                        cmd->base.port = PORT_DA;
 744                else
 745                        cmd->base.port = PORT_FIBRE;
 746
 747                hns3_get_ksettings(h, cmd);
 748                break;
 749        case HNAE3_MEDIA_TYPE_BACKPLANE:
 750                cmd->base.port = PORT_NONE;
 751                hns3_get_ksettings(h, cmd);
 752                break;
 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);
 760                else
 761                        phy_ethtool_ksettings_get(netdev->phydev, cmd);
 762                break;
 763        default:
 764
 765                netdev_warn(netdev, "Unknown media type");
 766                return 0;
 767        }
 768
 769        /* mdio_support */
 770        cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
 771
 772        link_stat = hns3_get_link(netdev);
 773        if (!link_stat) {
 774                cmd->base.speed = SPEED_UNKNOWN;
 775                cmd->base.duplex = DUPLEX_UNKNOWN;
 776        }
 777
 778        return 0;
 779}
 780
 781static int hns3_check_ksettings_param(const struct net_device *netdev,
 782                                      const struct ethtool_link_ksettings *cmd)
 783{
 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;
 788        u8 autoneg;
 789        u32 speed;
 790        u8 duplex;
 791        int ret;
 792
 793        /* hw doesn't support use specified speed and duplex to negotiate,
 794         * unnecessary to check them when autoneg on.
 795         */
 796        if (cmd->base.autoneg)
 797                return 0;
 798
 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)
 803                        return 0;
 804        }
 805
 806        if (ops->get_media_type)
 807                ops->get_media_type(handle, &media_type, &module_type);
 808
 809        if (cmd->base.duplex == DUPLEX_HALF &&
 810            media_type != HNAE3_MEDIA_TYPE_COPPER) {
 811                netdev_err(netdev,
 812                           "only copper port supports half duplex!");
 813                return -EINVAL;
 814        }
 815
 816        if (ops->check_port_speed) {
 817                ret = ops->check_port_speed(handle, cmd->base.speed);
 818                if (ret) {
 819                        netdev_err(netdev, "unsupported speed\n");
 820                        return ret;
 821                }
 822        }
 823
 824        return 0;
 825}
 826
 827static int hns3_set_link_ksettings(struct net_device *netdev,
 828                                   const struct ethtool_link_ksettings *cmd)
 829{
 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;
 833        int ret;
 834
 835        /* Chip don't support this mode. */
 836        if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
 837                return -EINVAL;
 838
 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);
 843
 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)
 848                        return -EINVAL;
 849
 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);
 854        }
 855
 856        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
 857                return -EOPNOTSUPP;
 858
 859        ret = hns3_check_ksettings_param(netdev, cmd);
 860        if (ret)
 861                return ret;
 862
 863        if (ops->set_autoneg) {
 864                ret = ops->set_autoneg(handle, cmd->base.autoneg);
 865                if (ret)
 866                        return ret;
 867        }
 868
 869        /* hw doesn't support use specified speed and duplex to negotiate,
 870         * ignore them when autoneg on.
 871         */
 872        if (cmd->base.autoneg) {
 873                netdev_info(netdev,
 874                            "autoneg is on, ignore the speed and duplex\n");
 875                return 0;
 876        }
 877
 878        if (ops->cfg_mac_speed_dup_h)
 879                ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
 880                                               cmd->base.duplex);
 881
 882        return ret;
 883}
 884
 885static u32 hns3_get_rss_key_size(struct net_device *netdev)
 886{
 887        struct hnae3_handle *h = hns3_get_handle(netdev);
 888
 889        if (!h->ae_algo->ops->get_rss_key_size)
 890                return 0;
 891
 892        return h->ae_algo->ops->get_rss_key_size(h);
 893}
 894
 895static u32 hns3_get_rss_indir_size(struct net_device *netdev)
 896{
 897        struct hnae3_handle *h = hns3_get_handle(netdev);
 898        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
 899
 900        return ae_dev->dev_specs.rss_ind_tbl_size;
 901}
 902
 903static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
 904                        u8 *hfunc)
 905{
 906        struct hnae3_handle *h = hns3_get_handle(netdev);
 907
 908        if (!h->ae_algo->ops->get_rss)
 909                return -EOPNOTSUPP;
 910
 911        return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
 912}
 913
 914static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
 915                        const u8 *key, const u8 hfunc)
 916{
 917        struct hnae3_handle *h = hns3_get_handle(netdev);
 918        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
 919
 920        if (!h->ae_algo->ops->set_rss)
 921                return -EOPNOTSUPP;
 922
 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");
 927                return -EOPNOTSUPP;
 928        }
 929
 930        if (!indir) {
 931                netdev_err(netdev,
 932                           "set rss failed for indir is empty\n");
 933                return -EOPNOTSUPP;
 934        }
 935
 936        return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
 937}
 938
 939static int hns3_get_rxnfc(struct net_device *netdev,
 940                          struct ethtool_rxnfc *cmd,
 941                          u32 *rule_locs)
 942{
 943        struct hnae3_handle *h = hns3_get_handle(netdev);
 944
 945        switch (cmd->cmd) {
 946        case ETHTOOL_GRXRINGS:
 947                cmd->data = h->kinfo.num_tqps;
 948                return 0;
 949        case ETHTOOL_GRXFH:
 950                if (h->ae_algo->ops->get_rss_tuple)
 951                        return h->ae_algo->ops->get_rss_tuple(h, cmd);
 952                return -EOPNOTSUPP;
 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);
 956                return -EOPNOTSUPP;
 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);
 960                return -EOPNOTSUPP;
 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,
 964                                                                 rule_locs);
 965                return -EOPNOTSUPP;
 966        default:
 967                return -EOPNOTSUPP;
 968        }
 969}
 970
 971static 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},
 975};
 976
 977static const struct hns3_reset_type_map hns3vf_reset_type[] = {
 978        {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
 979};
 980
 981static int hns3_set_reset(struct net_device *netdev, u32 *flags)
 982{
 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;
 988        u32 i, size;
 989
 990        if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
 991                return -EBUSY;
 992
 993        if (!ops->set_default_reset_request || !ops->reset_event)
 994                return -EOPNOTSUPP;
 995
 996        if (h->flags & HNAE3_SUPPORT_VF) {
 997                rst_type_map = hns3vf_reset_type;
 998                size = ARRAY_SIZE(hns3vf_reset_type);
 999        } else {
1000                rst_type_map = hns3_reset_type;
1001                size = ARRAY_SIZE(hns3_reset_type);
1002        }
1003
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;
1007                        break;
1008                }
1009        }
1010
1011        if (rst_type == HNAE3_NONE_RESET ||
1012            (rst_type == HNAE3_IMP_RESET &&
1013             ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1014                return -EOPNOTSUPP;
1015
1016        netdev_info(netdev, "Setting reset type %d\n", rst_type);
1017
1018        ops->set_default_reset_request(ae_dev, rst_type);
1019
1020        ops->reset_event(h->pdev, h);
1021
1022        return 0;
1023}
1024
1025static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1026                                        u32 tx_desc_num, u32 rx_desc_num)
1027{
1028        struct hnae3_handle *h = priv->ae_handle;
1029        int i;
1030
1031        h->kinfo.num_tx_desc = tx_desc_num;
1032        h->kinfo.num_rx_desc = rx_desc_num;
1033
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;
1037        }
1038}
1039
1040static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1041{
1042        struct hnae3_handle *handle = priv->ae_handle;
1043        struct hns3_enet_ring *tmp_rings;
1044        int i;
1045
1046        tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1047                            sizeof(struct hns3_enet_ring), GFP_KERNEL);
1048        if (!tmp_rings)
1049                return NULL;
1050
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;
1055        }
1056
1057        return tmp_rings;
1058}
1059
1060static int hns3_check_ringparam(struct net_device *ndev,
1061                                struct ethtool_ringparam *param)
1062{
1063        if (hns3_nic_resetting(ndev))
1064                return -EBUSY;
1065
1066        if (param->rx_mini_pending || param->rx_jumbo_pending)
1067                return -EINVAL;
1068
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);
1075                return -EINVAL;
1076        }
1077
1078        return 0;
1079}
1080
1081static int hns3_set_ringparam(struct net_device *ndev,
1082                              struct ethtool_ringparam *param)
1083{
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;
1091        int ret, i;
1092
1093        ret = hns3_check_ringparam(ndev, param);
1094        if (ret)
1095                return ret;
1096
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)
1104                return 0;
1105
1106        tmp_rings = hns3_backup_ringparam(priv);
1107        if (!tmp_rings) {
1108                netdev_err(ndev,
1109                           "backup ring param failed by allocating memory fail\n");
1110                return -ENOMEM;
1111        }
1112
1113        netdev_info(ndev,
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);
1117
1118        if (if_running)
1119                ndev->netdev_ops->ndo_stop(ndev);
1120
1121        hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1122        ret = hns3_init_all_ring(priv);
1123        if (ret) {
1124                netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1125                           ret);
1126
1127                hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1128                                            old_rx_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));
1132        } else {
1133                for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1134                        hns3_fini_ring(&tmp_rings[i]);
1135        }
1136
1137        kfree(tmp_rings);
1138
1139        if (if_running)
1140                ret = ndev->netdev_ops->ndo_open(ndev);
1141
1142        return ret;
1143}
1144
1145static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1146{
1147        struct hnae3_handle *h = hns3_get_handle(netdev);
1148
1149        switch (cmd->cmd) {
1150        case ETHTOOL_SRXFH:
1151                if (h->ae_algo->ops->set_rss_tuple)
1152                        return h->ae_algo->ops->set_rss_tuple(h, cmd);
1153                return -EOPNOTSUPP;
1154        case ETHTOOL_SRXCLSRLINS:
1155                if (h->ae_algo->ops->add_fd_entry)
1156                        return h->ae_algo->ops->add_fd_entry(h, cmd);
1157                return -EOPNOTSUPP;
1158        case ETHTOOL_SRXCLSRLDEL:
1159                if (h->ae_algo->ops->del_fd_entry)
1160                        return h->ae_algo->ops->del_fd_entry(h, cmd);
1161                return -EOPNOTSUPP;
1162        default:
1163                return -EOPNOTSUPP;
1164        }
1165}
1166
1167static int hns3_nway_reset(struct net_device *netdev)
1168{
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;
1172        int autoneg;
1173
1174        if (!netif_running(netdev))
1175                return 0;
1176
1177        if (hns3_nic_resetting(netdev)) {
1178                netdev_err(netdev, "dev resetting!");
1179                return -EBUSY;
1180        }
1181
1182        if (!ops->get_autoneg || !ops->restart_autoneg)
1183                return -EOPNOTSUPP;
1184
1185        autoneg = ops->get_autoneg(handle);
1186        if (autoneg != AUTONEG_ENABLE) {
1187                netdev_err(netdev,
1188                           "Autoneg is off, don't support to restart it\n");
1189                return -EINVAL;
1190        }
1191
1192        netif_dbg(handle, drv, netdev,
1193                  "nway reset (using %s)\n", phy ? "phy" : "mac");
1194
1195        if (phy)
1196                return genphy_restart_aneg(phy);
1197
1198        return ops->restart_autoneg(handle);
1199}
1200
1201static void hns3_get_channels(struct net_device *netdev,
1202                              struct ethtool_channels *ch)
1203{
1204        struct hnae3_handle *h = hns3_get_handle(netdev);
1205
1206        if (h->ae_algo->ops->get_channels)
1207                h->ae_algo->ops->get_channels(h, ch);
1208}
1209
1210static 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)
1214{
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;
1219
1220        if (hns3_nic_resetting(netdev))
1221                return -EBUSY;
1222
1223        cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1224        cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1225
1226        cmd->tx_coalesce_usecs = tx_coal->int_gl;
1227        cmd->rx_coalesce_usecs = rx_coal->int_gl;
1228
1229        cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1230        cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1231
1232        cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1233        cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1234
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);
1239
1240        return 0;
1241}
1242
1243static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1244                                       struct ethtool_coalesce *cmd)
1245{
1246        struct hnae3_handle *handle = hns3_get_handle(netdev);
1247        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1248        u32 rx_gl, tx_gl;
1249
1250        if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1251                netdev_err(netdev,
1252                           "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1253                           ae_dev->dev_specs.max_int_gl);
1254                return -EINVAL;
1255        }
1256
1257        if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1258                netdev_err(netdev,
1259                           "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1260                           ae_dev->dev_specs.max_int_gl);
1261                return -EINVAL;
1262        }
1263
1264        /* device version above V3(include V3), GL uses 1us unit,
1265         * so the round down is not needed.
1266         */
1267        if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1268                return 0;
1269
1270        rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1271        if (rx_gl != cmd->rx_coalesce_usecs) {
1272                netdev_info(netdev,
1273                            "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1274                            cmd->rx_coalesce_usecs, rx_gl);
1275        }
1276
1277        tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1278        if (tx_gl != cmd->tx_coalesce_usecs) {
1279                netdev_info(netdev,
1280                            "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1281                            cmd->tx_coalesce_usecs, tx_gl);
1282        }
1283
1284        return 0;
1285}
1286
1287static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1288                                       struct ethtool_coalesce *cmd)
1289{
1290        u32 rl;
1291
1292        if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1293                netdev_err(netdev,
1294                           "tx_usecs_high must be same as rx_usecs_high.\n");
1295                return -EINVAL;
1296        }
1297
1298        if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1299                netdev_err(netdev,
1300                           "Invalid usecs_high value, usecs_high range is 0-%d\n",
1301                           HNS3_INT_RL_MAX);
1302                return -EINVAL;
1303        }
1304
1305        rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1306        if (rl != cmd->rx_coalesce_usecs_high) {
1307                netdev_info(netdev,
1308                            "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1309                            cmd->rx_coalesce_usecs_high, rl);
1310        }
1311
1312        return 0;
1313}
1314
1315static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1316                                        struct ethtool_coalesce *cmd)
1317{
1318        struct hnae3_handle *handle = hns3_get_handle(netdev);
1319        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1320
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");
1324                return -EOPNOTSUPP;
1325        }
1326
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) {
1329                netdev_err(netdev,
1330                           "invalid coalesced_frames value, range is 0-%u\n",
1331                           ae_dev->dev_specs.int_ql_max);
1332                return -ERANGE;
1333        }
1334
1335        return 0;
1336}
1337
1338static int hns3_check_coalesce_para(struct net_device *netdev,
1339                                    struct ethtool_coalesce *cmd)
1340{
1341        int ret;
1342
1343        ret = hns3_check_gl_coalesce_para(netdev, cmd);
1344        if (ret) {
1345                netdev_err(netdev,
1346                           "Check gl coalesce param fail. ret = %d\n", ret);
1347                return ret;
1348        }
1349
1350        ret = hns3_check_rl_coalesce_para(netdev, cmd);
1351        if (ret) {
1352                netdev_err(netdev,
1353                           "Check rl coalesce param fail. ret = %d\n", ret);
1354                return ret;
1355        }
1356
1357        return hns3_check_ql_coalesce_param(netdev, cmd);
1358}
1359
1360static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1361                                        struct ethtool_coalesce *cmd,
1362                                        u32 queue)
1363{
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;
1368
1369        tx_vector = priv->ring[queue].tqp_vector;
1370        rx_vector = priv->ring[queue_num + queue].tqp_vector;
1371
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;
1376
1377        tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1378        rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1379
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;
1382
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);
1387
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);
1390
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);
1397}
1398
1399static 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)
1403{
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;
1411        int ret;
1412        int i;
1413
1414        if (hns3_nic_resetting(netdev))
1415                return -EBUSY;
1416
1417        ret = hns3_check_coalesce_para(netdev, cmd);
1418        if (ret)
1419                return ret;
1420
1421        h->kinfo.int_rl_setting =
1422                hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1423
1424        tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1425        rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1426
1427        tx_coal->int_gl = cmd->tx_coalesce_usecs;
1428        rx_coal->int_gl = cmd->rx_coalesce_usecs;
1429
1430        tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1431        rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1432
1433        for (i = 0; i < queue_num; i++)
1434                hns3_set_coalesce_per_queue(netdev, cmd, i);
1435
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);
1443
1444        return 0;
1445}
1446
1447static int hns3_get_regs_len(struct net_device *netdev)
1448{
1449        struct hnae3_handle *h = hns3_get_handle(netdev);
1450
1451        if (!h->ae_algo->ops->get_regs_len)
1452                return -EOPNOTSUPP;
1453
1454        return h->ae_algo->ops->get_regs_len(h);
1455}
1456
1457static void hns3_get_regs(struct net_device *netdev,
1458                          struct ethtool_regs *cmd, void *data)
1459{
1460        struct hnae3_handle *h = hns3_get_handle(netdev);
1461
1462        if (!h->ae_algo->ops->get_regs)
1463                return;
1464
1465        h->ae_algo->ops->get_regs(h, &cmd->version, data);
1466}
1467
1468static int hns3_set_phys_id(struct net_device *netdev,
1469                            enum ethtool_phys_id_state state)
1470{
1471        struct hnae3_handle *h = hns3_get_handle(netdev);
1472
1473        if (!h->ae_algo->ops->set_led_id)
1474                return -EOPNOTSUPP;
1475
1476        return h->ae_algo->ops->set_led_id(h, state);
1477}
1478
1479static u32 hns3_get_msglevel(struct net_device *netdev)
1480{
1481        struct hnae3_handle *h = hns3_get_handle(netdev);
1482
1483        return h->msg_enable;
1484}
1485
1486static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1487{
1488        struct hnae3_handle *h = hns3_get_handle(netdev);
1489
1490        h->msg_enable = msg_level;
1491}
1492
1493/* Translate local fec value into ethtool value. */
1494static unsigned int loc_to_eth_fec(u8 loc_fec)
1495{
1496        u32 eth_fec = 0;
1497
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;
1504
1505        /* if nothing is set, then FEC is off */
1506        if (!eth_fec)
1507                eth_fec = ETHTOOL_FEC_OFF;
1508
1509        return eth_fec;
1510}
1511
1512/* Translate ethtool fec value into local value. */
1513static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1514{
1515        u32 loc_fec = 0;
1516
1517        if (eth_fec & ETHTOOL_FEC_OFF)
1518                return loc_fec;
1519
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);
1526
1527        return loc_fec;
1528}
1529
1530static int hns3_get_fecparam(struct net_device *netdev,
1531                             struct ethtool_fecparam *fec)
1532{
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;
1536        u8 fec_ability;
1537        u8 fec_mode;
1538
1539        if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1540                return -EOPNOTSUPP;
1541
1542        if (!ops->get_fec)
1543                return -EOPNOTSUPP;
1544
1545        ops->get_fec(handle, &fec_ability, &fec_mode);
1546
1547        fec->fec = loc_to_eth_fec(fec_ability);
1548        fec->active_fec = loc_to_eth_fec(fec_mode);
1549
1550        return 0;
1551}
1552
1553static int hns3_set_fecparam(struct net_device *netdev,
1554                             struct ethtool_fecparam *fec)
1555{
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;
1559        u32 fec_mode;
1560
1561        if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1562                return -EOPNOTSUPP;
1563
1564        if (!ops->set_fec)
1565                return -EOPNOTSUPP;
1566        fec_mode = eth_to_loc_fec(fec->fec);
1567
1568        netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1569
1570        return ops->set_fec(handle, fec_mode);
1571}
1572
1573static int hns3_get_module_info(struct net_device *netdev,
1574                                struct ethtool_modinfo *modinfo)
1575{
1576#define HNS3_SFF_8636_V1_3 0x03
1577
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;
1582        int ret;
1583
1584        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1585            !ops->get_module_eeprom)
1586                return -EOPNOTSUPP;
1587
1588        memset(&sfp_type, 0, sizeof(sfp_type));
1589        ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1590                                     (u8 *)&sfp_type);
1591        if (ret)
1592                return ret;
1593
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;
1598                break;
1599        case SFF8024_ID_QSFP_8438:
1600                modinfo->type = ETH_MODULE_SFF_8436;
1601                modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1602                break;
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;
1607                } else {
1608                        modinfo->type = ETH_MODULE_SFF_8636;
1609                        modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1610                }
1611                break;
1612        case SFF8024_ID_QSFP28_8636:
1613                modinfo->type = ETH_MODULE_SFF_8636;
1614                modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1615                break;
1616        default:
1617                netdev_err(netdev, "Optical module unknown: %#x\n",
1618                           sfp_type.type);
1619                return -EINVAL;
1620        }
1621
1622        return 0;
1623}
1624
1625static int hns3_get_module_eeprom(struct net_device *netdev,
1626                                  struct ethtool_eeprom *ee, u8 *data)
1627{
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;
1631
1632        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1633            !ops->get_module_eeprom)
1634                return -EOPNOTSUPP;
1635
1636        if (!ee->len)
1637                return -EINVAL;
1638
1639        memset(data, 0, ee->len);
1640
1641        return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1642}
1643
1644static u32 hns3_get_priv_flags(struct net_device *netdev)
1645{
1646        struct hnae3_handle *handle = hns3_get_handle(netdev);
1647
1648        return handle->priv_flags;
1649}
1650
1651static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1652{
1653        u32 i;
1654
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);
1659                        return -EOPNOTSUPP;
1660                }
1661
1662        return 0;
1663}
1664
1665static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1666{
1667        struct hnae3_handle *handle = hns3_get_handle(netdev);
1668        u32 changed = pflags ^ handle->priv_flags;
1669        int ret;
1670        u32 i;
1671
1672        ret = hns3_check_priv_flags(handle, changed);
1673        if (ret)
1674                return ret;
1675
1676        for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1677                if (changed & BIT(i)) {
1678                        bool enable = !(handle->priv_flags & BIT(i));
1679
1680                        if (enable)
1681                                handle->priv_flags |= BIT(i);
1682                        else
1683                                handle->priv_flags &= ~BIT(i);
1684                        hns3_priv_flags[i].handler(netdev, enable);
1685                }
1686        }
1687
1688        return 0;
1689}
1690
1691static int hns3_get_tunable(struct net_device *netdev,
1692                            const struct ethtool_tunable *tuna,
1693                            void *data)
1694{
1695        struct hns3_nic_priv *priv = netdev_priv(netdev);
1696        int ret = 0;
1697
1698        switch (tuna->id) {
1699        case ETHTOOL_TX_COPYBREAK:
1700                /* all the tx rings have the same tx_copybreak */
1701                *(u32 *)data = priv->tx_copybreak;
1702                break;
1703        case ETHTOOL_RX_COPYBREAK:
1704                *(u32 *)data = priv->rx_copybreak;
1705                break;
1706        default:
1707                ret = -EOPNOTSUPP;
1708                break;
1709        }
1710
1711        return ret;
1712}
1713
1714static int hns3_set_tunable(struct net_device *netdev,
1715                            const struct ethtool_tunable *tuna,
1716                            const void *data)
1717{
1718        struct hns3_nic_priv *priv = netdev_priv(netdev);
1719        struct hnae3_handle *h = priv->ae_handle;
1720        int i, ret = 0;
1721
1722        switch (tuna->id) {
1723        case ETHTOOL_TX_COPYBREAK:
1724                priv->tx_copybreak = *(u32 *)data;
1725
1726                for (i = 0; i < h->kinfo.num_tqps; i++)
1727                        priv->ring[i].tx_copybreak = priv->tx_copybreak;
1728
1729                break;
1730        case ETHTOOL_RX_COPYBREAK:
1731                priv->rx_copybreak = *(u32 *)data;
1732
1733                for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1734                        priv->ring[i].rx_copybreak = priv->rx_copybreak;
1735
1736                break;
1737        default:
1738                ret = -EOPNOTSUPP;
1739                break;
1740        }
1741
1742        return ret;
1743}
1744
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)
1751
1752static int hns3_get_ts_info(struct net_device *netdev,
1753                            struct ethtool_ts_info *info)
1754{
1755        struct hnae3_handle *handle = hns3_get_handle(netdev);
1756
1757        if (handle->ae_algo->ops->get_ts_info)
1758                return handle->ae_algo->ops->get_ts_info(handle, info);
1759
1760        return ethtool_op_get_ts_info(netdev, info);
1761}
1762
1763static const struct hns3_ethtool_link_ext_state_mapping
1764hns3_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},
1769
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},
1776
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},
1783
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},
1790
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},
1794
1795        {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1796};
1797
1798static int hns3_get_link_ext_state(struct net_device *netdev,
1799                                   struct ethtool_link_ext_state_info *info)
1800{
1801        const struct hns3_ethtool_link_ext_state_mapping *map;
1802        struct hnae3_handle *h = hns3_get_handle(netdev);
1803        u32 status_code, i;
1804        int ret;
1805
1806        if (netif_carrier_ok(netdev))
1807                return -ENODATA;
1808
1809        if (!h->ae_algo->ops->get_link_diagnosis_info)
1810                return -EOPNOTSUPP;
1811
1812        ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1813        if (ret)
1814                return ret;
1815
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;
1821                        return 0;
1822                }
1823        }
1824
1825        return -ENODATA;
1826}
1827
1828static 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,
1857};
1858
1859static 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,
1900};
1901
1902void hns3_ethtool_set_ops(struct net_device *netdev)
1903{
1904        struct hnae3_handle *h = hns3_get_handle(netdev);
1905
1906        if (h->flags & HNAE3_SUPPORT_VF)
1907                netdev->ethtool_ops = &hns3vf_ethtool_ops;
1908        else
1909                netdev->ethtool_ops = &hns3_ethtool_ops;
1910}
1911