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
  11struct hns3_stats {
  12        char stats_string[ETH_GSTRING_LEN];
  13        int stats_offset;
  14};
  15
  16struct hns3_sfp_type {
  17        u8 type;
  18        u8 ext_type;
  19};
  20
  21struct hns3_pflag_desc {
  22        char name[ETH_GSTRING_LEN];
  23        void (*handler)(struct net_device *netdev, bool enable);
  24};
  25
  26/* tqp related stats */
  27#define HNS3_TQP_STAT(_string, _member) {                       \
  28        .stats_string = _string,                                \
  29        .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
  30                        offsetof(struct ring_stats, _member),   \
  31}
  32
  33static const struct hns3_stats hns3_txq_stats[] = {
  34        /* Tx per-queue statistics */
  35        HNS3_TQP_STAT("dropped", sw_err_cnt),
  36        HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  37        HNS3_TQP_STAT("packets", tx_pkts),
  38        HNS3_TQP_STAT("bytes", tx_bytes),
  39        HNS3_TQP_STAT("more", tx_more),
  40        HNS3_TQP_STAT("wake", restart_queue),
  41        HNS3_TQP_STAT("busy", tx_busy),
  42        HNS3_TQP_STAT("copy", tx_copy),
  43        HNS3_TQP_STAT("vlan_err", tx_vlan_err),
  44        HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
  45        HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
  46        HNS3_TQP_STAT("tso_err", tx_tso_err),
  47};
  48
  49#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
  50
  51static const struct hns3_stats hns3_rxq_stats[] = {
  52        /* Rx per-queue statistics */
  53        HNS3_TQP_STAT("dropped", sw_err_cnt),
  54        HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  55        HNS3_TQP_STAT("packets", rx_pkts),
  56        HNS3_TQP_STAT("bytes", rx_bytes),
  57        HNS3_TQP_STAT("errors", rx_err_cnt),
  58        HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
  59        HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
  60        HNS3_TQP_STAT("err_bd_num", err_bd_num),
  61        HNS3_TQP_STAT("l2_err", l2_err),
  62        HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
  63        HNS3_TQP_STAT("csum_complete", csum_complete),
  64        HNS3_TQP_STAT("multicast", rx_multicast),
  65        HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
  66};
  67
  68#define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
  69
  70#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
  71
  72#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
  73
  74#define HNS3_SELF_TEST_TYPE_NUM         4
  75#define HNS3_NIC_LB_TEST_PKT_NUM        1
  76#define HNS3_NIC_LB_TEST_RING_ID        0
  77#define HNS3_NIC_LB_TEST_PACKET_SIZE    128
  78#define HNS3_NIC_LB_SETUP_USEC          10000
  79
  80/* Nic loopback test err  */
  81#define HNS3_NIC_LB_TEST_NO_MEM_ERR     1
  82#define HNS3_NIC_LB_TEST_TX_CNT_ERR     2
  83#define HNS3_NIC_LB_TEST_RX_CNT_ERR     3
  84
  85static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
  86{
  87        struct hnae3_handle *h = hns3_get_handle(ndev);
  88        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
  89        bool vlan_filter_enable;
  90        int ret;
  91
  92        if (!h->ae_algo->ops->set_loopback ||
  93            !h->ae_algo->ops->set_promisc_mode)
  94                return -EOPNOTSUPP;
  95
  96        switch (loop) {
  97        case HNAE3_LOOP_SERIAL_SERDES:
  98        case HNAE3_LOOP_PARALLEL_SERDES:
  99        case HNAE3_LOOP_APP:
 100        case HNAE3_LOOP_PHY:
 101                ret = h->ae_algo->ops->set_loopback(h, loop, en);
 102                break;
 103        default:
 104                ret = -ENOTSUPP;
 105                break;
 106        }
 107
 108        if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
 109                return ret;
 110
 111        if (en) {
 112                h->ae_algo->ops->set_promisc_mode(h, true, true);
 113        } else {
 114                /* recover promisc mode before loopback test */
 115                hns3_request_update_promisc_mode(h);
 116                vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
 117                hns3_enable_vlan_filter(ndev, vlan_filter_enable);
 118        }
 119
 120        return ret;
 121}
 122
 123static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
 124{
 125        struct hnae3_handle *h = hns3_get_handle(ndev);
 126        int ret;
 127
 128        ret = hns3_nic_reset_all_ring(h);
 129        if (ret)
 130                return ret;
 131
 132        ret = hns3_lp_setup(ndev, loop_mode, true);
 133        usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
 134
 135        return ret;
 136}
 137
 138static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
 139{
 140        int ret;
 141
 142        ret = hns3_lp_setup(ndev, loop_mode, false);
 143        if (ret) {
 144                netdev_err(ndev, "lb_setup return error: %d\n", ret);
 145                return ret;
 146        }
 147
 148        usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
 149
 150        return 0;
 151}
 152
 153static void hns3_lp_setup_skb(struct sk_buff *skb)
 154{
 155#define HNS3_NIC_LB_DST_MAC_ADDR        0x1f
 156
 157        struct net_device *ndev = skb->dev;
 158        struct hnae3_handle *handle;
 159        struct hnae3_ae_dev *ae_dev;
 160        unsigned char *packet;
 161        struct ethhdr *ethh;
 162        unsigned int i;
 163
 164        skb_reserve(skb, NET_IP_ALIGN);
 165        ethh = skb_put(skb, sizeof(struct ethhdr));
 166        packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
 167
 168        memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
 169
 170        /* The dst mac addr of loopback packet is the same as the host'
 171         * mac addr, the SSU component may loop back the packet to host
 172         * before the packet reaches mac or serdes, which will defect
 173         * the purpose of mac or serdes selftest.
 174         */
 175        handle = hns3_get_handle(ndev);
 176        ae_dev = pci_get_drvdata(handle->pdev);
 177        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
 178                ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
 179        eth_zero_addr(ethh->h_source);
 180        ethh->h_proto = htons(ETH_P_ARP);
 181        skb_reset_mac_header(skb);
 182
 183        for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
 184                packet[i] = (unsigned char)(i & 0xff);
 185}
 186
 187static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
 188                                   struct sk_buff *skb)
 189{
 190        struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
 191        unsigned char *packet = skb->data;
 192        u32 len = skb_headlen(skb);
 193        u32 i;
 194
 195        len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
 196
 197        for (i = 0; i < len; i++)
 198                if (packet[i] != (unsigned char)(i & 0xff))
 199                        break;
 200
 201        /* The packet is correctly received */
 202        if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
 203                tqp_vector->rx_group.total_packets++;
 204        else
 205                print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
 206                               skb->data, len, true);
 207
 208        dev_kfree_skb_any(skb);
 209}
 210
 211static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
 212{
 213        struct hnae3_handle *h = priv->ae_handle;
 214        struct hnae3_knic_private_info *kinfo;
 215        u32 i, rcv_good_pkt_total = 0;
 216
 217        kinfo = &h->kinfo;
 218        for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
 219                struct hns3_enet_ring *ring = &priv->ring[i];
 220                struct hns3_enet_ring_group *rx_group;
 221                u64 pre_rx_pkt;
 222
 223                rx_group = &ring->tqp_vector->rx_group;
 224                pre_rx_pkt = rx_group->total_packets;
 225
 226                preempt_disable();
 227                hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
 228                preempt_enable();
 229
 230                rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
 231                rx_group->total_packets = pre_rx_pkt;
 232        }
 233        return rcv_good_pkt_total;
 234}
 235
 236static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
 237                                  u32 end_ringid, u32 budget)
 238{
 239        u32 i;
 240
 241        for (i = start_ringid; i <= end_ringid; i++) {
 242                struct hns3_enet_ring *ring = &priv->ring[i];
 243
 244                hns3_clean_tx_ring(ring, 0);
 245        }
 246}
 247
 248/**
 249 * hns3_lp_run_test -  run loopback test
 250 * @ndev: net device
 251 * @mode: loopback type
 252 */
 253static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
 254{
 255        struct hns3_nic_priv *priv = netdev_priv(ndev);
 256        struct sk_buff *skb;
 257        u32 i, good_cnt;
 258        int ret_val = 0;
 259
 260        skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
 261                        GFP_KERNEL);
 262        if (!skb)
 263                return HNS3_NIC_LB_TEST_NO_MEM_ERR;
 264
 265        skb->dev = ndev;
 266        hns3_lp_setup_skb(skb);
 267        skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
 268
 269        good_cnt = 0;
 270        for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
 271                netdev_tx_t tx_ret;
 272
 273                skb_get(skb);
 274                tx_ret = hns3_nic_net_xmit(skb, ndev);
 275                if (tx_ret == NETDEV_TX_OK) {
 276                        good_cnt++;
 277                } else {
 278                        kfree_skb(skb);
 279                        netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
 280                                   tx_ret);
 281                }
 282        }
 283        if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
 284                ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
 285                netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
 286                           mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
 287                goto out;
 288        }
 289
 290        /* Allow 200 milliseconds for packets to go from Tx to Rx */
 291        msleep(200);
 292
 293        good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
 294        if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
 295                ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
 296                netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
 297                           mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
 298        }
 299
 300out:
 301        hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
 302                              HNS3_NIC_LB_TEST_RING_ID,
 303                              HNS3_NIC_LB_TEST_PKT_NUM);
 304
 305        kfree_skb(skb);
 306        return ret_val;
 307}
 308
 309/**
 310 * hns3_nic_self_test - self test
 311 * @ndev: net device
 312 * @eth_test: test cmd
 313 * @data: test result
 314 */
 315static void hns3_self_test(struct net_device *ndev,
 316                           struct ethtool_test *eth_test, u64 *data)
 317{
 318        struct hns3_nic_priv *priv = netdev_priv(ndev);
 319        struct hnae3_handle *h = priv->ae_handle;
 320        int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
 321        bool if_running = netif_running(ndev);
 322        int test_index = 0;
 323        u32 i;
 324
 325        if (hns3_nic_resetting(ndev)) {
 326                netdev_err(ndev, "dev resetting!");
 327                return;
 328        }
 329
 330        /* Only do offline selftest, or pass by default */
 331        if (eth_test->flags != ETH_TEST_FL_OFFLINE)
 332                return;
 333
 334        netif_dbg(h, drv, ndev, "self test start");
 335
 336        st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
 337        st_param[HNAE3_LOOP_APP][1] =
 338                        h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
 339
 340        st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
 341        st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
 342                        h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
 343
 344        st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
 345                        HNAE3_LOOP_PARALLEL_SERDES;
 346        st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
 347                        h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
 348
 349        st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
 350        st_param[HNAE3_LOOP_PHY][1] =
 351                        h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
 352
 353        if (if_running)
 354                ndev->netdev_ops->ndo_stop(ndev);
 355
 356#if IS_ENABLED(CONFIG_VLAN_8021Q)
 357        /* Disable the vlan filter for selftest does not support it */
 358        if (h->ae_algo->ops->enable_vlan_filter)
 359                h->ae_algo->ops->enable_vlan_filter(h, false);
 360#endif
 361
 362        /* Tell firmware to stop mac autoneg before loopback test start,
 363         * otherwise loopback test may be failed when the port is still
 364         * negotiating.
 365         */
 366        if (h->ae_algo->ops->halt_autoneg)
 367                h->ae_algo->ops->halt_autoneg(h, true);
 368
 369        set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
 370
 371        for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
 372                enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
 373
 374                if (!st_param[i][1])
 375                        continue;
 376
 377                data[test_index] = hns3_lp_up(ndev, loop_type);
 378                if (!data[test_index])
 379                        data[test_index] = hns3_lp_run_test(ndev, loop_type);
 380
 381                hns3_lp_down(ndev, loop_type);
 382
 383                if (data[test_index])
 384                        eth_test->flags |= ETH_TEST_FL_FAILED;
 385
 386                test_index++;
 387        }
 388
 389        clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
 390
 391        if (h->ae_algo->ops->halt_autoneg)
 392                h->ae_algo->ops->halt_autoneg(h, false);
 393
 394#if IS_ENABLED(CONFIG_VLAN_8021Q)
 395        if (h->ae_algo->ops->enable_vlan_filter)
 396                h->ae_algo->ops->enable_vlan_filter(h, true);
 397#endif
 398
 399        if (if_running)
 400                ndev->netdev_ops->ndo_open(ndev);
 401
 402        netif_dbg(h, drv, ndev, "self test end\n");
 403}
 404
 405static void hns3_update_limit_promisc_mode(struct net_device *netdev,
 406                                           bool enable)
 407{
 408        struct hnae3_handle *handle = hns3_get_handle(netdev);
 409
 410        if (enable)
 411                set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
 412        else
 413                clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
 414
 415        hns3_request_update_promisc_mode(handle);
 416}
 417
 418static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
 419        { "limit_promisc",      hns3_update_limit_promisc_mode }
 420};
 421
 422static int hns3_get_sset_count(struct net_device *netdev, int stringset)
 423{
 424        struct hnae3_handle *h = hns3_get_handle(netdev);
 425        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 426
 427        if (!ops->get_sset_count)
 428                return -EOPNOTSUPP;
 429
 430        switch (stringset) {
 431        case ETH_SS_STATS:
 432                return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
 433                        ops->get_sset_count(h, stringset));
 434
 435        case ETH_SS_TEST:
 436                return ops->get_sset_count(h, stringset);
 437
 438        case ETH_SS_PRIV_FLAGS:
 439                return HNAE3_PFLAG_MAX;
 440
 441        default:
 442                return -EOPNOTSUPP;
 443        }
 444}
 445
 446static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
 447                u32 stat_count, u32 num_tqps, const char *prefix)
 448{
 449#define MAX_PREFIX_SIZE (6 + 4)
 450        u32 size_left;
 451        u32 i, j;
 452        u32 n1;
 453
 454        for (i = 0; i < num_tqps; i++) {
 455                for (j = 0; j < stat_count; j++) {
 456                        data[ETH_GSTRING_LEN - 1] = '\0';
 457
 458                        /* first, prepend the prefix string */
 459                        n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
 460                                       prefix, i);
 461                        size_left = (ETH_GSTRING_LEN - 1) - n1;
 462
 463                        /* now, concatenate the stats string to it */
 464                        strncat(data, stats[j].stats_string, size_left);
 465                        data += ETH_GSTRING_LEN;
 466                }
 467        }
 468
 469        return data;
 470}
 471
 472static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
 473{
 474        struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 475        const char tx_prefix[] = "txq";
 476        const char rx_prefix[] = "rxq";
 477
 478        /* get strings for Tx */
 479        data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
 480                                   kinfo->num_tqps, tx_prefix);
 481
 482        /* get strings for Rx */
 483        data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
 484                                   kinfo->num_tqps, rx_prefix);
 485
 486        return data;
 487}
 488
 489static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 490{
 491        struct hnae3_handle *h = hns3_get_handle(netdev);
 492        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 493        char *buff = (char *)data;
 494        int i;
 495
 496        if (!ops->get_strings)
 497                return;
 498
 499        switch (stringset) {
 500        case ETH_SS_STATS:
 501                buff = hns3_get_strings_tqps(h, buff);
 502                ops->get_strings(h, stringset, (u8 *)buff);
 503                break;
 504        case ETH_SS_TEST:
 505                ops->get_strings(h, stringset, data);
 506                break;
 507        case ETH_SS_PRIV_FLAGS:
 508                for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
 509                        snprintf(buff, ETH_GSTRING_LEN, "%s",
 510                                 hns3_priv_flags[i].name);
 511                        buff += ETH_GSTRING_LEN;
 512                }
 513                break;
 514        default:
 515                break;
 516        }
 517}
 518
 519static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
 520{
 521        struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
 522        struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 523        struct hns3_enet_ring *ring;
 524        u8 *stat;
 525        int i, j;
 526
 527        /* get stats for Tx */
 528        for (i = 0; i < kinfo->num_tqps; i++) {
 529                ring = &nic_priv->ring[i];
 530                for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
 531                        stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
 532                        *data++ = *(u64 *)stat;
 533                }
 534        }
 535
 536        /* get stats for Rx */
 537        for (i = 0; i < kinfo->num_tqps; i++) {
 538                ring = &nic_priv->ring[i + kinfo->num_tqps];
 539                for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
 540                        stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
 541                        *data++ = *(u64 *)stat;
 542                }
 543        }
 544
 545        return data;
 546}
 547
 548/* hns3_get_stats - get detail statistics.
 549 * @netdev: net device
 550 * @stats: statistics info.
 551 * @data: statistics data.
 552 */
 553static void hns3_get_stats(struct net_device *netdev,
 554                           struct ethtool_stats *stats, u64 *data)
 555{
 556        struct hnae3_handle *h = hns3_get_handle(netdev);
 557        u64 *p = data;
 558
 559        if (hns3_nic_resetting(netdev)) {
 560                netdev_err(netdev, "dev resetting, could not get stats\n");
 561                return;
 562        }
 563
 564        if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
 565                netdev_err(netdev, "could not get any statistics\n");
 566                return;
 567        }
 568
 569        h->ae_algo->ops->update_stats(h, &netdev->stats);
 570
 571        /* get per-queue stats */
 572        p = hns3_get_stats_tqps(h, p);
 573
 574        /* get MAC & other misc hardware stats */
 575        h->ae_algo->ops->get_stats(h, p);
 576}
 577
 578static void hns3_get_drvinfo(struct net_device *netdev,
 579                             struct ethtool_drvinfo *drvinfo)
 580{
 581        struct hns3_nic_priv *priv = netdev_priv(netdev);
 582        struct hnae3_handle *h = priv->ae_handle;
 583        u32 fw_version;
 584
 585        if (!h->ae_algo->ops->get_fw_version) {
 586                netdev_err(netdev, "could not get fw version!\n");
 587                return;
 588        }
 589
 590        strncpy(drvinfo->driver, h->pdev->driver->name,
 591                sizeof(drvinfo->driver));
 592        drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
 593
 594        strncpy(drvinfo->bus_info, pci_name(h->pdev),
 595                sizeof(drvinfo->bus_info));
 596        drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
 597
 598        fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
 599
 600        snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 601                 "%lu.%lu.%lu.%lu",
 602                 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
 603                                 HNAE3_FW_VERSION_BYTE3_SHIFT),
 604                 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
 605                                 HNAE3_FW_VERSION_BYTE2_SHIFT),
 606                 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
 607                                 HNAE3_FW_VERSION_BYTE1_SHIFT),
 608                 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
 609                                 HNAE3_FW_VERSION_BYTE0_SHIFT));
 610}
 611
 612static u32 hns3_get_link(struct net_device *netdev)
 613{
 614        struct hnae3_handle *h = hns3_get_handle(netdev);
 615
 616        if (h->ae_algo->ops->get_status)
 617                return h->ae_algo->ops->get_status(h);
 618        else
 619                return 0;
 620}
 621
 622static void hns3_get_ringparam(struct net_device *netdev,
 623                               struct ethtool_ringparam *param)
 624{
 625        struct hns3_nic_priv *priv = netdev_priv(netdev);
 626        struct hnae3_handle *h = priv->ae_handle;
 627        int queue_num = h->kinfo.num_tqps;
 628
 629        if (hns3_nic_resetting(netdev)) {
 630                netdev_err(netdev, "dev resetting!");
 631                return;
 632        }
 633
 634        param->tx_max_pending = HNS3_RING_MAX_PENDING;
 635        param->rx_max_pending = HNS3_RING_MAX_PENDING;
 636
 637        param->tx_pending = priv->ring[0].desc_num;
 638        param->rx_pending = priv->ring[queue_num].desc_num;
 639}
 640
 641static void hns3_get_pauseparam(struct net_device *netdev,
 642                                struct ethtool_pauseparam *param)
 643{
 644        struct hnae3_handle *h = hns3_get_handle(netdev);
 645
 646        if (h->ae_algo->ops->get_pauseparam)
 647                h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
 648                        &param->rx_pause, &param->tx_pause);
 649}
 650
 651static int hns3_set_pauseparam(struct net_device *netdev,
 652                               struct ethtool_pauseparam *param)
 653{
 654        struct hnae3_handle *h = hns3_get_handle(netdev);
 655
 656        netif_dbg(h, drv, netdev,
 657                  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
 658                  param->autoneg, param->rx_pause, param->tx_pause);
 659
 660        if (h->ae_algo->ops->set_pauseparam)
 661                return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
 662                                                       param->rx_pause,
 663                                                       param->tx_pause);
 664        return -EOPNOTSUPP;
 665}
 666
 667static void hns3_get_ksettings(struct hnae3_handle *h,
 668                               struct ethtool_link_ksettings *cmd)
 669{
 670        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 671
 672        /* 1.auto_neg & speed & duplex from cmd */
 673        if (ops->get_ksettings_an_result)
 674                ops->get_ksettings_an_result(h,
 675                                             &cmd->base.autoneg,
 676                                             &cmd->base.speed,
 677                                             &cmd->base.duplex);
 678
 679        /* 2.get link mode */
 680        if (ops->get_link_mode)
 681                ops->get_link_mode(h,
 682                                   cmd->link_modes.supported,
 683                                   cmd->link_modes.advertising);
 684
 685        /* 3.mdix_ctrl&mdix get from phy reg */
 686        if (ops->get_mdix_mode)
 687                ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
 688                                   &cmd->base.eth_tp_mdix);
 689}
 690
 691static int hns3_get_link_ksettings(struct net_device *netdev,
 692                                   struct ethtool_link_ksettings *cmd)
 693{
 694        struct hnae3_handle *h = hns3_get_handle(netdev);
 695        const struct hnae3_ae_ops *ops;
 696        u8 module_type;
 697        u8 media_type;
 698        u8 link_stat;
 699
 700        ops = h->ae_algo->ops;
 701        if (ops->get_media_type)
 702                ops->get_media_type(h, &media_type, &module_type);
 703        else
 704                return -EOPNOTSUPP;
 705
 706        switch (media_type) {
 707        case HNAE3_MEDIA_TYPE_NONE:
 708                cmd->base.port = PORT_NONE;
 709                hns3_get_ksettings(h, cmd);
 710                break;
 711        case HNAE3_MEDIA_TYPE_FIBER:
 712                if (module_type == HNAE3_MODULE_TYPE_CR)
 713                        cmd->base.port = PORT_DA;
 714                else
 715                        cmd->base.port = PORT_FIBRE;
 716
 717                hns3_get_ksettings(h, cmd);
 718                break;
 719        case HNAE3_MEDIA_TYPE_BACKPLANE:
 720                cmd->base.port = PORT_NONE;
 721                hns3_get_ksettings(h, cmd);
 722                break;
 723        case HNAE3_MEDIA_TYPE_COPPER:
 724                cmd->base.port = PORT_TP;
 725                if (!netdev->phydev)
 726                        hns3_get_ksettings(h, cmd);
 727                else
 728                        phy_ethtool_ksettings_get(netdev->phydev, cmd);
 729                break;
 730        default:
 731
 732                netdev_warn(netdev, "Unknown media type");
 733                return 0;
 734        }
 735
 736        /* mdio_support */
 737        cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
 738
 739        link_stat = hns3_get_link(netdev);
 740        if (!link_stat) {
 741                cmd->base.speed = SPEED_UNKNOWN;
 742                cmd->base.duplex = DUPLEX_UNKNOWN;
 743        }
 744
 745        return 0;
 746}
 747
 748static int hns3_check_ksettings_param(const struct net_device *netdev,
 749                                      const struct ethtool_link_ksettings *cmd)
 750{
 751        struct hnae3_handle *handle = hns3_get_handle(netdev);
 752        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
 753        u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
 754        u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
 755        u8 autoneg;
 756        u32 speed;
 757        u8 duplex;
 758        int ret;
 759
 760        /* hw doesn't support use specified speed and duplex to negotiate,
 761         * unnecessary to check them when autoneg on.
 762         */
 763        if (cmd->base.autoneg)
 764                return 0;
 765
 766        if (ops->get_ksettings_an_result) {
 767                ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
 768                if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
 769                    cmd->base.duplex == duplex)
 770                        return 0;
 771        }
 772
 773        if (ops->get_media_type)
 774                ops->get_media_type(handle, &media_type, &module_type);
 775
 776        if (cmd->base.duplex == DUPLEX_HALF &&
 777            media_type != HNAE3_MEDIA_TYPE_COPPER) {
 778                netdev_err(netdev,
 779                           "only copper port supports half duplex!");
 780                return -EINVAL;
 781        }
 782
 783        if (ops->check_port_speed) {
 784                ret = ops->check_port_speed(handle, cmd->base.speed);
 785                if (ret) {
 786                        netdev_err(netdev, "unsupported speed\n");
 787                        return ret;
 788                }
 789        }
 790
 791        return 0;
 792}
 793
 794static int hns3_set_link_ksettings(struct net_device *netdev,
 795                                   const struct ethtool_link_ksettings *cmd)
 796{
 797        struct hnae3_handle *handle = hns3_get_handle(netdev);
 798        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
 799        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
 800        int ret;
 801
 802        /* Chip don't support this mode. */
 803        if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
 804                return -EINVAL;
 805
 806        netif_dbg(handle, drv, netdev,
 807                  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
 808                  netdev->phydev ? "phy" : "mac",
 809                  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
 810
 811        /* Only support ksettings_set for netdev with phy attached for now */
 812        if (netdev->phydev) {
 813                if (cmd->base.speed == SPEED_1000 &&
 814                    cmd->base.autoneg == AUTONEG_DISABLE)
 815                        return -EINVAL;
 816
 817                return phy_ethtool_ksettings_set(netdev->phydev, cmd);
 818        }
 819
 820        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
 821                return -EOPNOTSUPP;
 822
 823        ret = hns3_check_ksettings_param(netdev, cmd);
 824        if (ret)
 825                return ret;
 826
 827        if (ops->set_autoneg) {
 828                ret = ops->set_autoneg(handle, cmd->base.autoneg);
 829                if (ret)
 830                        return ret;
 831        }
 832
 833        /* hw doesn't support use specified speed and duplex to negotiate,
 834         * ignore them when autoneg on.
 835         */
 836        if (cmd->base.autoneg) {
 837                netdev_info(netdev,
 838                            "autoneg is on, ignore the speed and duplex\n");
 839                return 0;
 840        }
 841
 842        if (ops->cfg_mac_speed_dup_h)
 843                ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
 844                                               cmd->base.duplex);
 845
 846        return ret;
 847}
 848
 849static u32 hns3_get_rss_key_size(struct net_device *netdev)
 850{
 851        struct hnae3_handle *h = hns3_get_handle(netdev);
 852
 853        if (!h->ae_algo->ops->get_rss_key_size)
 854                return 0;
 855
 856        return h->ae_algo->ops->get_rss_key_size(h);
 857}
 858
 859static u32 hns3_get_rss_indir_size(struct net_device *netdev)
 860{
 861        struct hnae3_handle *h = hns3_get_handle(netdev);
 862        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
 863
 864        return ae_dev->dev_specs.rss_ind_tbl_size;
 865}
 866
 867static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
 868                        u8 *hfunc)
 869{
 870        struct hnae3_handle *h = hns3_get_handle(netdev);
 871
 872        if (!h->ae_algo->ops->get_rss)
 873                return -EOPNOTSUPP;
 874
 875        return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
 876}
 877
 878static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
 879                        const u8 *key, const u8 hfunc)
 880{
 881        struct hnae3_handle *h = hns3_get_handle(netdev);
 882        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
 883
 884        if (!h->ae_algo->ops->set_rss)
 885                return -EOPNOTSUPP;
 886
 887        if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
 888             hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
 889             hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
 890                netdev_err(netdev, "hash func not supported\n");
 891                return -EOPNOTSUPP;
 892        }
 893
 894        if (!indir) {
 895                netdev_err(netdev,
 896                           "set rss failed for indir is empty\n");
 897                return -EOPNOTSUPP;
 898        }
 899
 900        return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
 901}
 902
 903static int hns3_get_rxnfc(struct net_device *netdev,
 904                          struct ethtool_rxnfc *cmd,
 905                          u32 *rule_locs)
 906{
 907        struct hnae3_handle *h = hns3_get_handle(netdev);
 908
 909        switch (cmd->cmd) {
 910        case ETHTOOL_GRXRINGS:
 911                cmd->data = h->kinfo.num_tqps;
 912                return 0;
 913        case ETHTOOL_GRXFH:
 914                if (h->ae_algo->ops->get_rss_tuple)
 915                        return h->ae_algo->ops->get_rss_tuple(h, cmd);
 916                return -EOPNOTSUPP;
 917        case ETHTOOL_GRXCLSRLCNT:
 918                if (h->ae_algo->ops->get_fd_rule_cnt)
 919                        return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
 920                return -EOPNOTSUPP;
 921        case ETHTOOL_GRXCLSRULE:
 922                if (h->ae_algo->ops->get_fd_rule_info)
 923                        return h->ae_algo->ops->get_fd_rule_info(h, cmd);
 924                return -EOPNOTSUPP;
 925        case ETHTOOL_GRXCLSRLALL:
 926                if (h->ae_algo->ops->get_fd_all_rules)
 927                        return h->ae_algo->ops->get_fd_all_rules(h, cmd,
 928                                                                 rule_locs);
 929                return -EOPNOTSUPP;
 930        default:
 931                return -EOPNOTSUPP;
 932        }
 933}
 934
 935static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
 936                                        u32 tx_desc_num, u32 rx_desc_num)
 937{
 938        struct hnae3_handle *h = priv->ae_handle;
 939        int i;
 940
 941        h->kinfo.num_tx_desc = tx_desc_num;
 942        h->kinfo.num_rx_desc = rx_desc_num;
 943
 944        for (i = 0; i < h->kinfo.num_tqps; i++) {
 945                priv->ring[i].desc_num = tx_desc_num;
 946                priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
 947        }
 948}
 949
 950static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
 951{
 952        struct hnae3_handle *handle = priv->ae_handle;
 953        struct hns3_enet_ring *tmp_rings;
 954        int i;
 955
 956        tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
 957                            sizeof(struct hns3_enet_ring), GFP_KERNEL);
 958        if (!tmp_rings)
 959                return NULL;
 960
 961        for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
 962                memcpy(&tmp_rings[i], &priv->ring[i],
 963                       sizeof(struct hns3_enet_ring));
 964                tmp_rings[i].skb = NULL;
 965        }
 966
 967        return tmp_rings;
 968}
 969
 970static int hns3_check_ringparam(struct net_device *ndev,
 971                                struct ethtool_ringparam *param)
 972{
 973        if (hns3_nic_resetting(ndev))
 974                return -EBUSY;
 975
 976        if (param->rx_mini_pending || param->rx_jumbo_pending)
 977                return -EINVAL;
 978
 979        if (param->tx_pending > HNS3_RING_MAX_PENDING ||
 980            param->tx_pending < HNS3_RING_MIN_PENDING ||
 981            param->rx_pending > HNS3_RING_MAX_PENDING ||
 982            param->rx_pending < HNS3_RING_MIN_PENDING) {
 983                netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
 984                           HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
 985                return -EINVAL;
 986        }
 987
 988        return 0;
 989}
 990
 991static int hns3_set_ringparam(struct net_device *ndev,
 992                              struct ethtool_ringparam *param)
 993{
 994        struct hns3_nic_priv *priv = netdev_priv(ndev);
 995        struct hnae3_handle *h = priv->ae_handle;
 996        struct hns3_enet_ring *tmp_rings;
 997        bool if_running = netif_running(ndev);
 998        u32 old_tx_desc_num, new_tx_desc_num;
 999        u32 old_rx_desc_num, new_rx_desc_num;
1000        u16 queue_num = h->kinfo.num_tqps;
1001        int ret, i;
1002
1003        ret = hns3_check_ringparam(ndev, param);
1004        if (ret)
1005                return ret;
1006
1007        /* Hardware requires that its descriptors must be multiple of eight */
1008        new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1009        new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1010        old_tx_desc_num = priv->ring[0].desc_num;
1011        old_rx_desc_num = priv->ring[queue_num].desc_num;
1012        if (old_tx_desc_num == new_tx_desc_num &&
1013            old_rx_desc_num == new_rx_desc_num)
1014                return 0;
1015
1016        tmp_rings = hns3_backup_ringparam(priv);
1017        if (!tmp_rings) {
1018                netdev_err(ndev,
1019                           "backup ring param failed by allocating memory fail\n");
1020                return -ENOMEM;
1021        }
1022
1023        netdev_info(ndev,
1024                    "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1025                    old_tx_desc_num, old_rx_desc_num,
1026                    new_tx_desc_num, new_rx_desc_num);
1027
1028        if (if_running)
1029                ndev->netdev_ops->ndo_stop(ndev);
1030
1031        hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1032        ret = hns3_init_all_ring(priv);
1033        if (ret) {
1034                netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1035                           ret);
1036
1037                hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1038                                            old_rx_desc_num);
1039                for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1040                        memcpy(&priv->ring[i], &tmp_rings[i],
1041                               sizeof(struct hns3_enet_ring));
1042        } else {
1043                for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1044                        hns3_fini_ring(&tmp_rings[i]);
1045        }
1046
1047        kfree(tmp_rings);
1048
1049        if (if_running)
1050                ret = ndev->netdev_ops->ndo_open(ndev);
1051
1052        return ret;
1053}
1054
1055static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1056{
1057        struct hnae3_handle *h = hns3_get_handle(netdev);
1058
1059        switch (cmd->cmd) {
1060        case ETHTOOL_SRXFH:
1061                if (h->ae_algo->ops->set_rss_tuple)
1062                        return h->ae_algo->ops->set_rss_tuple(h, cmd);
1063                return -EOPNOTSUPP;
1064        case ETHTOOL_SRXCLSRLINS:
1065                if (h->ae_algo->ops->add_fd_entry)
1066                        return h->ae_algo->ops->add_fd_entry(h, cmd);
1067                return -EOPNOTSUPP;
1068        case ETHTOOL_SRXCLSRLDEL:
1069                if (h->ae_algo->ops->del_fd_entry)
1070                        return h->ae_algo->ops->del_fd_entry(h, cmd);
1071                return -EOPNOTSUPP;
1072        default:
1073                return -EOPNOTSUPP;
1074        }
1075}
1076
1077static int hns3_nway_reset(struct net_device *netdev)
1078{
1079        struct hnae3_handle *handle = hns3_get_handle(netdev);
1080        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1081        struct phy_device *phy = netdev->phydev;
1082        int autoneg;
1083
1084        if (!netif_running(netdev))
1085                return 0;
1086
1087        if (hns3_nic_resetting(netdev)) {
1088                netdev_err(netdev, "dev resetting!");
1089                return -EBUSY;
1090        }
1091
1092        if (!ops->get_autoneg || !ops->restart_autoneg)
1093                return -EOPNOTSUPP;
1094
1095        autoneg = ops->get_autoneg(handle);
1096        if (autoneg != AUTONEG_ENABLE) {
1097                netdev_err(netdev,
1098                           "Autoneg is off, don't support to restart it\n");
1099                return -EINVAL;
1100        }
1101
1102        netif_dbg(handle, drv, netdev,
1103                  "nway reset (using %s)\n", phy ? "phy" : "mac");
1104
1105        if (phy)
1106                return genphy_restart_aneg(phy);
1107
1108        return ops->restart_autoneg(handle);
1109}
1110
1111static void hns3_get_channels(struct net_device *netdev,
1112                              struct ethtool_channels *ch)
1113{
1114        struct hnae3_handle *h = hns3_get_handle(netdev);
1115
1116        if (h->ae_algo->ops->get_channels)
1117                h->ae_algo->ops->get_channels(h, ch);
1118}
1119
1120static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1121                                       struct ethtool_coalesce *cmd)
1122{
1123        struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1124        struct hns3_nic_priv *priv = netdev_priv(netdev);
1125        struct hnae3_handle *h = priv->ae_handle;
1126        u16 queue_num = h->kinfo.num_tqps;
1127
1128        if (hns3_nic_resetting(netdev))
1129                return -EBUSY;
1130
1131        if (queue >= queue_num) {
1132                netdev_err(netdev,
1133                           "Invalid queue value %u! Queue max id=%u\n",
1134                           queue, queue_num - 1);
1135                return -EINVAL;
1136        }
1137
1138        tx_vector = priv->ring[queue].tqp_vector;
1139        rx_vector = priv->ring[queue_num + queue].tqp_vector;
1140
1141        cmd->use_adaptive_tx_coalesce =
1142                        tx_vector->tx_group.coal.adapt_enable;
1143        cmd->use_adaptive_rx_coalesce =
1144                        rx_vector->rx_group.coal.adapt_enable;
1145
1146        cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1147        cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1148
1149        cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1150        cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1151
1152        cmd->tx_max_coalesced_frames = tx_vector->tx_group.coal.int_ql;
1153        cmd->rx_max_coalesced_frames = rx_vector->rx_group.coal.int_ql;
1154
1155        return 0;
1156}
1157
1158static int hns3_get_coalesce(struct net_device *netdev,
1159                             struct ethtool_coalesce *cmd)
1160{
1161        return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1162}
1163
1164static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1165                                       struct ethtool_coalesce *cmd)
1166{
1167        struct hnae3_handle *handle = hns3_get_handle(netdev);
1168        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1169        u32 rx_gl, tx_gl;
1170
1171        if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1172                netdev_err(netdev,
1173                           "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1174                           ae_dev->dev_specs.max_int_gl);
1175                return -EINVAL;
1176        }
1177
1178        if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1179                netdev_err(netdev,
1180                           "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1181                           ae_dev->dev_specs.max_int_gl);
1182                return -EINVAL;
1183        }
1184
1185        /* device version above V3(include V3), GL uses 1us unit,
1186         * so the round down is not needed.
1187         */
1188        if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1189                return 0;
1190
1191        rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1192        if (rx_gl != cmd->rx_coalesce_usecs) {
1193                netdev_info(netdev,
1194                            "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1195                            cmd->rx_coalesce_usecs, rx_gl);
1196        }
1197
1198        tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1199        if (tx_gl != cmd->tx_coalesce_usecs) {
1200                netdev_info(netdev,
1201                            "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1202                            cmd->tx_coalesce_usecs, tx_gl);
1203        }
1204
1205        return 0;
1206}
1207
1208static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1209                                       struct ethtool_coalesce *cmd)
1210{
1211        u32 rl;
1212
1213        if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1214                netdev_err(netdev,
1215                           "tx_usecs_high must be same as rx_usecs_high.\n");
1216                return -EINVAL;
1217        }
1218
1219        if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1220                netdev_err(netdev,
1221                           "Invalid usecs_high value, usecs_high range is 0-%d\n",
1222                           HNS3_INT_RL_MAX);
1223                return -EINVAL;
1224        }
1225
1226        rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1227        if (rl != cmd->rx_coalesce_usecs_high) {
1228                netdev_info(netdev,
1229                            "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1230                            cmd->rx_coalesce_usecs_high, rl);
1231        }
1232
1233        return 0;
1234}
1235
1236static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1237                                        struct ethtool_coalesce *cmd)
1238{
1239        struct hnae3_handle *handle = hns3_get_handle(netdev);
1240        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1241
1242        if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1243            !ae_dev->dev_specs.int_ql_max) {
1244                netdev_err(netdev, "coalesced frames is not supported\n");
1245                return -EOPNOTSUPP;
1246        }
1247
1248        if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1249            cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1250                netdev_err(netdev,
1251                           "invalid coalesced_frames value, range is 0-%u\n",
1252                           ae_dev->dev_specs.int_ql_max);
1253                return -ERANGE;
1254        }
1255
1256        return 0;
1257}
1258
1259static int hns3_check_coalesce_para(struct net_device *netdev,
1260                                    struct ethtool_coalesce *cmd)
1261{
1262        int ret;
1263
1264        ret = hns3_check_gl_coalesce_para(netdev, cmd);
1265        if (ret) {
1266                netdev_err(netdev,
1267                           "Check gl coalesce param fail. ret = %d\n", ret);
1268                return ret;
1269        }
1270
1271        ret = hns3_check_rl_coalesce_para(netdev, cmd);
1272        if (ret) {
1273                netdev_err(netdev,
1274                           "Check rl coalesce param fail. ret = %d\n", ret);
1275                return ret;
1276        }
1277
1278        ret = hns3_check_ql_coalesce_param(netdev, cmd);
1279        if (ret)
1280                return ret;
1281
1282        if (cmd->use_adaptive_tx_coalesce == 1 ||
1283            cmd->use_adaptive_rx_coalesce == 1) {
1284                netdev_info(netdev,
1285                            "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1286                            cmd->use_adaptive_tx_coalesce,
1287                            cmd->use_adaptive_rx_coalesce);
1288        }
1289
1290        return 0;
1291}
1292
1293static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1294                                        struct ethtool_coalesce *cmd,
1295                                        u32 queue)
1296{
1297        struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1298        struct hns3_nic_priv *priv = netdev_priv(netdev);
1299        struct hnae3_handle *h = priv->ae_handle;
1300        int queue_num = h->kinfo.num_tqps;
1301
1302        tx_vector = priv->ring[queue].tqp_vector;
1303        rx_vector = priv->ring[queue_num + queue].tqp_vector;
1304
1305        tx_vector->tx_group.coal.adapt_enable =
1306                                cmd->use_adaptive_tx_coalesce;
1307        rx_vector->rx_group.coal.adapt_enable =
1308                                cmd->use_adaptive_rx_coalesce;
1309
1310        tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1311        rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1312
1313        tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1314        rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1315
1316        hns3_set_vector_coalesce_tx_gl(tx_vector,
1317                                       tx_vector->tx_group.coal.int_gl);
1318        hns3_set_vector_coalesce_rx_gl(rx_vector,
1319                                       rx_vector->rx_group.coal.int_gl);
1320
1321        hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1322        hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1323
1324        if (tx_vector->tx_group.coal.ql_enable)
1325                hns3_set_vector_coalesce_tx_ql(tx_vector,
1326                                               tx_vector->tx_group.coal.int_ql);
1327        if (rx_vector->rx_group.coal.ql_enable)
1328                hns3_set_vector_coalesce_rx_ql(rx_vector,
1329                                               rx_vector->rx_group.coal.int_ql);
1330}
1331
1332static int hns3_set_coalesce(struct net_device *netdev,
1333                             struct ethtool_coalesce *cmd)
1334{
1335        struct hnae3_handle *h = hns3_get_handle(netdev);
1336        u16 queue_num = h->kinfo.num_tqps;
1337        int ret;
1338        int i;
1339
1340        if (hns3_nic_resetting(netdev))
1341                return -EBUSY;
1342
1343        ret = hns3_check_coalesce_para(netdev, cmd);
1344        if (ret)
1345                return ret;
1346
1347        h->kinfo.int_rl_setting =
1348                hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1349
1350        for (i = 0; i < queue_num; i++)
1351                hns3_set_coalesce_per_queue(netdev, cmd, i);
1352
1353        return 0;
1354}
1355
1356static int hns3_get_regs_len(struct net_device *netdev)
1357{
1358        struct hnae3_handle *h = hns3_get_handle(netdev);
1359
1360        if (!h->ae_algo->ops->get_regs_len)
1361                return -EOPNOTSUPP;
1362
1363        return h->ae_algo->ops->get_regs_len(h);
1364}
1365
1366static void hns3_get_regs(struct net_device *netdev,
1367                          struct ethtool_regs *cmd, void *data)
1368{
1369        struct hnae3_handle *h = hns3_get_handle(netdev);
1370
1371        if (!h->ae_algo->ops->get_regs)
1372                return;
1373
1374        h->ae_algo->ops->get_regs(h, &cmd->version, data);
1375}
1376
1377static int hns3_set_phys_id(struct net_device *netdev,
1378                            enum ethtool_phys_id_state state)
1379{
1380        struct hnae3_handle *h = hns3_get_handle(netdev);
1381
1382        if (!h->ae_algo->ops->set_led_id)
1383                return -EOPNOTSUPP;
1384
1385        return h->ae_algo->ops->set_led_id(h, state);
1386}
1387
1388static u32 hns3_get_msglevel(struct net_device *netdev)
1389{
1390        struct hnae3_handle *h = hns3_get_handle(netdev);
1391
1392        return h->msg_enable;
1393}
1394
1395static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1396{
1397        struct hnae3_handle *h = hns3_get_handle(netdev);
1398
1399        h->msg_enable = msg_level;
1400}
1401
1402/* Translate local fec value into ethtool value. */
1403static unsigned int loc_to_eth_fec(u8 loc_fec)
1404{
1405        u32 eth_fec = 0;
1406
1407        if (loc_fec & BIT(HNAE3_FEC_AUTO))
1408                eth_fec |= ETHTOOL_FEC_AUTO;
1409        if (loc_fec & BIT(HNAE3_FEC_RS))
1410                eth_fec |= ETHTOOL_FEC_RS;
1411        if (loc_fec & BIT(HNAE3_FEC_BASER))
1412                eth_fec |= ETHTOOL_FEC_BASER;
1413
1414        /* if nothing is set, then FEC is off */
1415        if (!eth_fec)
1416                eth_fec = ETHTOOL_FEC_OFF;
1417
1418        return eth_fec;
1419}
1420
1421/* Translate ethtool fec value into local value. */
1422static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1423{
1424        u32 loc_fec = 0;
1425
1426        if (eth_fec & ETHTOOL_FEC_OFF)
1427                return loc_fec;
1428
1429        if (eth_fec & ETHTOOL_FEC_AUTO)
1430                loc_fec |= BIT(HNAE3_FEC_AUTO);
1431        if (eth_fec & ETHTOOL_FEC_RS)
1432                loc_fec |= BIT(HNAE3_FEC_RS);
1433        if (eth_fec & ETHTOOL_FEC_BASER)
1434                loc_fec |= BIT(HNAE3_FEC_BASER);
1435
1436        return loc_fec;
1437}
1438
1439static int hns3_get_fecparam(struct net_device *netdev,
1440                             struct ethtool_fecparam *fec)
1441{
1442        struct hnae3_handle *handle = hns3_get_handle(netdev);
1443        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1444        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1445        u8 fec_ability;
1446        u8 fec_mode;
1447
1448        if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1449                return -EOPNOTSUPP;
1450
1451        if (!ops->get_fec)
1452                return -EOPNOTSUPP;
1453
1454        ops->get_fec(handle, &fec_ability, &fec_mode);
1455
1456        fec->fec = loc_to_eth_fec(fec_ability);
1457        fec->active_fec = loc_to_eth_fec(fec_mode);
1458
1459        return 0;
1460}
1461
1462static int hns3_set_fecparam(struct net_device *netdev,
1463                             struct ethtool_fecparam *fec)
1464{
1465        struct hnae3_handle *handle = hns3_get_handle(netdev);
1466        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1467        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1468        u32 fec_mode;
1469
1470        if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1471                return -EOPNOTSUPP;
1472
1473        if (!ops->set_fec)
1474                return -EOPNOTSUPP;
1475        fec_mode = eth_to_loc_fec(fec->fec);
1476
1477        netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1478
1479        return ops->set_fec(handle, fec_mode);
1480}
1481
1482static int hns3_get_module_info(struct net_device *netdev,
1483                                struct ethtool_modinfo *modinfo)
1484{
1485#define HNS3_SFF_8636_V1_3 0x03
1486
1487        struct hnae3_handle *handle = hns3_get_handle(netdev);
1488        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1489        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1490        struct hns3_sfp_type sfp_type;
1491        int ret;
1492
1493        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1494            !ops->get_module_eeprom)
1495                return -EOPNOTSUPP;
1496
1497        memset(&sfp_type, 0, sizeof(sfp_type));
1498        ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1499                                     (u8 *)&sfp_type);
1500        if (ret)
1501                return ret;
1502
1503        switch (sfp_type.type) {
1504        case SFF8024_ID_SFP:
1505                modinfo->type = ETH_MODULE_SFF_8472;
1506                modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1507                break;
1508        case SFF8024_ID_QSFP_8438:
1509                modinfo->type = ETH_MODULE_SFF_8436;
1510                modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1511                break;
1512        case SFF8024_ID_QSFP_8436_8636:
1513                if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1514                        modinfo->type = ETH_MODULE_SFF_8436;
1515                        modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1516                } else {
1517                        modinfo->type = ETH_MODULE_SFF_8636;
1518                        modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1519                }
1520                break;
1521        case SFF8024_ID_QSFP28_8636:
1522                modinfo->type = ETH_MODULE_SFF_8636;
1523                modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1524                break;
1525        default:
1526                netdev_err(netdev, "Optical module unknown: %#x\n",
1527                           sfp_type.type);
1528                return -EINVAL;
1529        }
1530
1531        return 0;
1532}
1533
1534static int hns3_get_module_eeprom(struct net_device *netdev,
1535                                  struct ethtool_eeprom *ee, u8 *data)
1536{
1537        struct hnae3_handle *handle = hns3_get_handle(netdev);
1538        struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1539        const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1540
1541        if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1542            !ops->get_module_eeprom)
1543                return -EOPNOTSUPP;
1544
1545        if (!ee->len)
1546                return -EINVAL;
1547
1548        memset(data, 0, ee->len);
1549
1550        return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1551}
1552
1553static u32 hns3_get_priv_flags(struct net_device *netdev)
1554{
1555        struct hnae3_handle *handle = hns3_get_handle(netdev);
1556
1557        return handle->priv_flags;
1558}
1559
1560static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1561{
1562        u32 i;
1563
1564        for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1565                if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1566                        netdev_err(h->netdev, "%s is unsupported\n",
1567                                   hns3_priv_flags[i].name);
1568                        return -EOPNOTSUPP;
1569                }
1570
1571        return 0;
1572}
1573
1574static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1575{
1576        struct hnae3_handle *handle = hns3_get_handle(netdev);
1577        u32 changed = pflags ^ handle->priv_flags;
1578        int ret;
1579        u32 i;
1580
1581        ret = hns3_check_priv_flags(handle, changed);
1582        if (ret)
1583                return ret;
1584
1585        for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1586                if (changed & BIT(i)) {
1587                        bool enable = !(handle->priv_flags & BIT(i));
1588
1589                        if (enable)
1590                                handle->priv_flags |= BIT(i);
1591                        else
1592                                handle->priv_flags &= ~BIT(i);
1593                        hns3_priv_flags[i].handler(netdev, enable);
1594                }
1595        }
1596
1597        return 0;
1598}
1599
1600#define HNS3_ETHTOOL_COALESCE   (ETHTOOL_COALESCE_USECS |               \
1601                                 ETHTOOL_COALESCE_USE_ADAPTIVE |        \
1602                                 ETHTOOL_COALESCE_RX_USECS_HIGH |       \
1603                                 ETHTOOL_COALESCE_TX_USECS_HIGH |       \
1604                                 ETHTOOL_COALESCE_MAX_FRAMES)
1605
1606static const struct ethtool_ops hns3vf_ethtool_ops = {
1607        .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1608        .get_drvinfo = hns3_get_drvinfo,
1609        .get_ringparam = hns3_get_ringparam,
1610        .set_ringparam = hns3_set_ringparam,
1611        .get_strings = hns3_get_strings,
1612        .get_ethtool_stats = hns3_get_stats,
1613        .get_sset_count = hns3_get_sset_count,
1614        .get_rxnfc = hns3_get_rxnfc,
1615        .set_rxnfc = hns3_set_rxnfc,
1616        .get_rxfh_key_size = hns3_get_rss_key_size,
1617        .get_rxfh_indir_size = hns3_get_rss_indir_size,
1618        .get_rxfh = hns3_get_rss,
1619        .set_rxfh = hns3_set_rss,
1620        .get_link_ksettings = hns3_get_link_ksettings,
1621        .get_channels = hns3_get_channels,
1622        .set_channels = hns3_set_channels,
1623        .get_coalesce = hns3_get_coalesce,
1624        .set_coalesce = hns3_set_coalesce,
1625        .get_regs_len = hns3_get_regs_len,
1626        .get_regs = hns3_get_regs,
1627        .get_link = hns3_get_link,
1628        .get_msglevel = hns3_get_msglevel,
1629        .set_msglevel = hns3_set_msglevel,
1630        .get_priv_flags = hns3_get_priv_flags,
1631        .set_priv_flags = hns3_set_priv_flags,
1632};
1633
1634static const struct ethtool_ops hns3_ethtool_ops = {
1635        .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1636        .self_test = hns3_self_test,
1637        .get_drvinfo = hns3_get_drvinfo,
1638        .get_link = hns3_get_link,
1639        .get_ringparam = hns3_get_ringparam,
1640        .set_ringparam = hns3_set_ringparam,
1641        .get_pauseparam = hns3_get_pauseparam,
1642        .set_pauseparam = hns3_set_pauseparam,
1643        .get_strings = hns3_get_strings,
1644        .get_ethtool_stats = hns3_get_stats,
1645        .get_sset_count = hns3_get_sset_count,
1646        .get_rxnfc = hns3_get_rxnfc,
1647        .set_rxnfc = hns3_set_rxnfc,
1648        .get_rxfh_key_size = hns3_get_rss_key_size,
1649        .get_rxfh_indir_size = hns3_get_rss_indir_size,
1650        .get_rxfh = hns3_get_rss,
1651        .set_rxfh = hns3_set_rss,
1652        .get_link_ksettings = hns3_get_link_ksettings,
1653        .set_link_ksettings = hns3_set_link_ksettings,
1654        .nway_reset = hns3_nway_reset,
1655        .get_channels = hns3_get_channels,
1656        .set_channels = hns3_set_channels,
1657        .get_coalesce = hns3_get_coalesce,
1658        .set_coalesce = hns3_set_coalesce,
1659        .get_regs_len = hns3_get_regs_len,
1660        .get_regs = hns3_get_regs,
1661        .set_phys_id = hns3_set_phys_id,
1662        .get_msglevel = hns3_get_msglevel,
1663        .set_msglevel = hns3_set_msglevel,
1664        .get_fecparam = hns3_get_fecparam,
1665        .set_fecparam = hns3_set_fecparam,
1666        .get_module_info = hns3_get_module_info,
1667        .get_module_eeprom = hns3_get_module_eeprom,
1668        .get_priv_flags = hns3_get_priv_flags,
1669        .set_priv_flags = hns3_set_priv_flags,
1670};
1671
1672void hns3_ethtool_set_ops(struct net_device *netdev)
1673{
1674        struct hnae3_handle *h = hns3_get_handle(netdev);
1675
1676        if (h->flags & HNAE3_SUPPORT_VF)
1677                netdev->ethtool_ops = &hns3vf_ethtool_ops;
1678        else
1679                netdev->ethtool_ops = &hns3_ethtool_ops;
1680}
1681