linux/drivers/net/ethernet/hisilicon/hns3/hns3pf/hns3_ethtool.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016~2017 Hisilicon Limited.
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 */
   9
  10#include <linux/etherdevice.h>
  11#include <linux/string.h>
  12
  13#include "hns3_enet.h"
  14
  15struct hns3_stats {
  16        char stats_string[ETH_GSTRING_LEN];
  17        int stats_size;
  18        int stats_offset;
  19};
  20
  21/* tqp related stats */
  22#define HNS3_TQP_STAT(_string, _member) {                       \
  23        .stats_string = _string,                                \
  24        .stats_size = FIELD_SIZEOF(struct ring_stats, _member), \
  25        .stats_offset = offsetof(struct hns3_enet_ring, stats), \
  26}                                                               \
  27
  28static const struct hns3_stats hns3_txq_stats[] = {
  29        /* Tx per-queue statistics */
  30        HNS3_TQP_STAT("tx_io_err_cnt", io_err_cnt),
  31        HNS3_TQP_STAT("tx_sw_err_cnt", sw_err_cnt),
  32        HNS3_TQP_STAT("tx_seg_pkt_cnt", seg_pkt_cnt),
  33        HNS3_TQP_STAT("tx_pkts", tx_pkts),
  34        HNS3_TQP_STAT("tx_bytes", tx_bytes),
  35        HNS3_TQP_STAT("tx_err_cnt", tx_err_cnt),
  36        HNS3_TQP_STAT("tx_restart_queue", restart_queue),
  37        HNS3_TQP_STAT("tx_busy", tx_busy),
  38};
  39
  40#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
  41
  42static const struct hns3_stats hns3_rxq_stats[] = {
  43        /* Rx per-queue statistics */
  44        HNS3_TQP_STAT("rx_io_err_cnt", io_err_cnt),
  45        HNS3_TQP_STAT("rx_sw_err_cnt", sw_err_cnt),
  46        HNS3_TQP_STAT("rx_seg_pkt_cnt", seg_pkt_cnt),
  47        HNS3_TQP_STAT("rx_pkts", rx_pkts),
  48        HNS3_TQP_STAT("rx_bytes", rx_bytes),
  49        HNS3_TQP_STAT("rx_err_cnt", rx_err_cnt),
  50        HNS3_TQP_STAT("rx_reuse_pg_cnt", reuse_pg_cnt),
  51        HNS3_TQP_STAT("rx_err_pkt_len", err_pkt_len),
  52        HNS3_TQP_STAT("rx_non_vld_descs", non_vld_descs),
  53        HNS3_TQP_STAT("rx_err_bd_num", err_bd_num),
  54        HNS3_TQP_STAT("rx_l2_err", l2_err),
  55        HNS3_TQP_STAT("rx_l3l4_csum_err", l3l4_csum_err),
  56};
  57
  58#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
  59
  60#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
  61
  62struct hns3_link_mode_mapping {
  63        u32 hns3_link_mode;
  64        u32 ethtool_link_mode;
  65};
  66
  67static const struct hns3_link_mode_mapping hns3_lm_map[] = {
  68        {HNS3_LM_FIBRE_BIT, ETHTOOL_LINK_MODE_FIBRE_BIT},
  69        {HNS3_LM_AUTONEG_BIT, ETHTOOL_LINK_MODE_Autoneg_BIT},
  70        {HNS3_LM_TP_BIT, ETHTOOL_LINK_MODE_TP_BIT},
  71        {HNS3_LM_PAUSE_BIT, ETHTOOL_LINK_MODE_Pause_BIT},
  72        {HNS3_LM_BACKPLANE_BIT, ETHTOOL_LINK_MODE_Backplane_BIT},
  73        {HNS3_LM_10BASET_HALF_BIT, ETHTOOL_LINK_MODE_10baseT_Half_BIT},
  74        {HNS3_LM_10BASET_FULL_BIT, ETHTOOL_LINK_MODE_10baseT_Full_BIT},
  75        {HNS3_LM_100BASET_HALF_BIT, ETHTOOL_LINK_MODE_100baseT_Half_BIT},
  76        {HNS3_LM_100BASET_FULL_BIT, ETHTOOL_LINK_MODE_100baseT_Full_BIT},
  77        {HNS3_LM_1000BASET_FULL_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT},
  78};
  79
  80static void hns3_driv_to_eth_caps(u32 caps, struct ethtool_link_ksettings *cmd,
  81                                  bool is_advertised)
  82{
  83        int i;
  84
  85        for (i = 0; i < ARRAY_SIZE(hns3_lm_map); i++) {
  86                if (!(caps & hns3_lm_map[i].hns3_link_mode))
  87                        continue;
  88
  89                if (is_advertised) {
  90                        ethtool_link_ksettings_zero_link_mode(cmd,
  91                                                              advertising);
  92                        __set_bit(hns3_lm_map[i].ethtool_link_mode,
  93                                  cmd->link_modes.advertising);
  94                } else {
  95                        ethtool_link_ksettings_zero_link_mode(cmd,
  96                                                              supported);
  97                        __set_bit(hns3_lm_map[i].ethtool_link_mode,
  98                                  cmd->link_modes.supported);
  99                }
 100        }
 101}
 102
 103static int hns3_get_sset_count(struct net_device *netdev, int stringset)
 104{
 105        struct hns3_nic_priv *priv = netdev_priv(netdev);
 106        struct hnae3_handle *h = priv->ae_handle;
 107        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 108
 109        if (!ops->get_sset_count)
 110                return -EOPNOTSUPP;
 111
 112        switch (stringset) {
 113        case ETH_SS_STATS:
 114                return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
 115                        ops->get_sset_count(h, stringset));
 116
 117        case ETH_SS_TEST:
 118                return ops->get_sset_count(h, stringset);
 119        }
 120
 121        return 0;
 122}
 123
 124static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
 125                                 u32 stat_count, u32 num_tqps)
 126{
 127#define MAX_PREFIX_SIZE (8 + 4)
 128        u32 size_left;
 129        u32 i, j;
 130        u32 n1;
 131
 132        for (i = 0; i < num_tqps; i++) {
 133                for (j = 0; j < stat_count; j++) {
 134                        data[ETH_GSTRING_LEN - 1] = '\0';
 135
 136                        /* first, prepend the prefix string */
 137                        n1 = snprintf(data, MAX_PREFIX_SIZE, "rcb_q%d_", i);
 138                        n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
 139                        size_left = (ETH_GSTRING_LEN - 1) - n1;
 140
 141                        /* now, concatenate the stats string to it */
 142                        strncat(data, stats[j].stats_string, size_left);
 143                        data += ETH_GSTRING_LEN;
 144                }
 145        }
 146
 147        return data;
 148}
 149
 150static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
 151{
 152        struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 153
 154        /* get strings for Tx */
 155        data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
 156                                   kinfo->num_tqps);
 157
 158        /* get strings for Rx */
 159        data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
 160                                   kinfo->num_tqps);
 161
 162        return data;
 163}
 164
 165static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 166{
 167        struct hns3_nic_priv *priv = netdev_priv(netdev);
 168        struct hnae3_handle *h = priv->ae_handle;
 169        const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 170        char *buff = (char *)data;
 171
 172        if (!ops->get_strings)
 173                return;
 174
 175        switch (stringset) {
 176        case ETH_SS_STATS:
 177                buff = hns3_get_strings_tqps(h, buff);
 178                h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
 179                break;
 180        case ETH_SS_TEST:
 181                ops->get_strings(h, stringset, data);
 182                break;
 183        }
 184}
 185
 186static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
 187{
 188        struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
 189        struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 190        struct hns3_enet_ring *ring;
 191        u8 *stat;
 192        u32 i;
 193
 194        /* get stats for Tx */
 195        for (i = 0; i < kinfo->num_tqps; i++) {
 196                ring = nic_priv->ring_data[i].ring;
 197                for (i = 0; i < HNS3_TXQ_STATS_COUNT; i++) {
 198                        stat = (u8 *)ring + hns3_txq_stats[i].stats_offset;
 199                        *data++ = *(u64 *)stat;
 200                }
 201        }
 202
 203        /* get stats for Rx */
 204        for (i = 0; i < kinfo->num_tqps; i++) {
 205                ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
 206                for (i = 0; i < HNS3_RXQ_STATS_COUNT; i++) {
 207                        stat = (u8 *)ring + hns3_rxq_stats[i].stats_offset;
 208                        *data++ = *(u64 *)stat;
 209                }
 210        }
 211
 212        return data;
 213}
 214
 215/* hns3_get_stats - get detail statistics.
 216 * @netdev: net device
 217 * @stats: statistics info.
 218 * @data: statistics data.
 219 */
 220void hns3_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
 221                    u64 *data)
 222{
 223        struct hns3_nic_priv *priv = netdev_priv(netdev);
 224        struct hnae3_handle *h = priv->ae_handle;
 225        u64 *p = data;
 226
 227        if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
 228                netdev_err(netdev, "could not get any statistics\n");
 229                return;
 230        }
 231
 232        h->ae_algo->ops->update_stats(h, &netdev->stats);
 233
 234        /* get per-queue stats */
 235        p = hns3_get_stats_tqps(h, p);
 236
 237        /* get MAC & other misc hardware stats */
 238        h->ae_algo->ops->get_stats(h, p);
 239}
 240
 241static void hns3_get_drvinfo(struct net_device *netdev,
 242                             struct ethtool_drvinfo *drvinfo)
 243{
 244        struct hns3_nic_priv *priv = netdev_priv(netdev);
 245        struct hnae3_handle *h = priv->ae_handle;
 246
 247        strncpy(drvinfo->version, hns3_driver_version,
 248                sizeof(drvinfo->version));
 249        drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
 250
 251        strncpy(drvinfo->driver, h->pdev->driver->name,
 252                sizeof(drvinfo->driver));
 253        drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
 254
 255        strncpy(drvinfo->bus_info, pci_name(h->pdev),
 256                sizeof(drvinfo->bus_info));
 257        drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
 258
 259        snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
 260                 priv->ae_handle->ae_algo->ops->get_fw_version(h));
 261}
 262
 263static u32 hns3_get_link(struct net_device *netdev)
 264{
 265        struct hns3_nic_priv *priv = netdev_priv(netdev);
 266        struct hnae3_handle *h;
 267
 268        h = priv->ae_handle;
 269
 270        if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
 271                return h->ae_algo->ops->get_status(h);
 272        else
 273                return 0;
 274}
 275
 276static void hns3_get_ringparam(struct net_device *netdev,
 277                               struct ethtool_ringparam *param)
 278{
 279        struct hns3_nic_priv *priv = netdev_priv(netdev);
 280        int queue_num = priv->ae_handle->kinfo.num_tqps;
 281
 282        param->tx_max_pending = HNS3_RING_MAX_PENDING;
 283        param->rx_max_pending = HNS3_RING_MAX_PENDING;
 284
 285        param->tx_pending = priv->ring_data[0].ring->desc_num;
 286        param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
 287}
 288
 289static void hns3_get_pauseparam(struct net_device *netdev,
 290                                struct ethtool_pauseparam *param)
 291{
 292        struct hns3_nic_priv *priv = netdev_priv(netdev);
 293        struct hnae3_handle *h = priv->ae_handle;
 294
 295        if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
 296                h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
 297                        &param->rx_pause, &param->tx_pause);
 298}
 299
 300static int hns3_get_link_ksettings(struct net_device *netdev,
 301                                   struct ethtool_link_ksettings *cmd)
 302{
 303        struct hns3_nic_priv *priv = netdev_priv(netdev);
 304        struct hnae3_handle *h = priv->ae_handle;
 305        u32 supported_caps;
 306        u32 advertised_caps;
 307        u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
 308        u8 link_stat;
 309        u8 auto_neg;
 310        u8 duplex;
 311        u32 speed;
 312
 313        if (!h->ae_algo || !h->ae_algo->ops)
 314                return -EOPNOTSUPP;
 315
 316        /* 1.auto_neg & speed & duplex from cmd */
 317        if (h->ae_algo->ops->get_ksettings_an_result) {
 318                h->ae_algo->ops->get_ksettings_an_result(h, &auto_neg,
 319                                                         &speed, &duplex);
 320                cmd->base.autoneg = auto_neg;
 321                cmd->base.speed = speed;
 322                cmd->base.duplex = duplex;
 323
 324                link_stat = hns3_get_link(netdev);
 325                if (!link_stat) {
 326                        cmd->base.speed = (u32)SPEED_UNKNOWN;
 327                        cmd->base.duplex = DUPLEX_UNKNOWN;
 328                }
 329        }
 330
 331        /* 2.media_type get from bios parameter block */
 332        if (h->ae_algo->ops->get_media_type) {
 333                h->ae_algo->ops->get_media_type(h, &media_type);
 334
 335                switch (media_type) {
 336                case HNAE3_MEDIA_TYPE_FIBER:
 337                        cmd->base.port = PORT_FIBRE;
 338                        supported_caps = HNS3_LM_FIBRE_BIT |
 339                                         HNS3_LM_AUTONEG_BIT |
 340                                         HNS3_LM_PAUSE_BIT |
 341                                         HNS3_LM_1000BASET_FULL_BIT;
 342
 343                        advertised_caps = supported_caps;
 344                        break;
 345                case HNAE3_MEDIA_TYPE_COPPER:
 346                        cmd->base.port = PORT_TP;
 347                        supported_caps = HNS3_LM_TP_BIT |
 348                                         HNS3_LM_AUTONEG_BIT |
 349                                         HNS3_LM_PAUSE_BIT |
 350                                         HNS3_LM_1000BASET_FULL_BIT |
 351                                         HNS3_LM_100BASET_FULL_BIT |
 352                                         HNS3_LM_100BASET_HALF_BIT |
 353                                         HNS3_LM_10BASET_FULL_BIT |
 354                                         HNS3_LM_10BASET_HALF_BIT;
 355                        advertised_caps = supported_caps;
 356                        break;
 357                case HNAE3_MEDIA_TYPE_BACKPLANE:
 358                        cmd->base.port = PORT_NONE;
 359                        supported_caps = HNS3_LM_BACKPLANE_BIT |
 360                                         HNS3_LM_PAUSE_BIT |
 361                                         HNS3_LM_AUTONEG_BIT |
 362                                         HNS3_LM_1000BASET_FULL_BIT |
 363                                         HNS3_LM_100BASET_FULL_BIT |
 364                                         HNS3_LM_100BASET_HALF_BIT |
 365                                         HNS3_LM_10BASET_FULL_BIT |
 366                                         HNS3_LM_10BASET_HALF_BIT;
 367
 368                        advertised_caps = supported_caps;
 369                        break;
 370                case HNAE3_MEDIA_TYPE_UNKNOWN:
 371                default:
 372                        cmd->base.port = PORT_OTHER;
 373                        supported_caps = 0;
 374                        advertised_caps = 0;
 375                        break;
 376                }
 377
 378                /* now, map driver link modes to ethtool link modes */
 379                hns3_driv_to_eth_caps(supported_caps, cmd, false);
 380                hns3_driv_to_eth_caps(advertised_caps, cmd, true);
 381        }
 382
 383        /* 3.mdix_ctrl&mdix get from phy reg */
 384        if (h->ae_algo->ops->get_mdix_mode)
 385                h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
 386                                               &cmd->base.eth_tp_mdix);
 387        /* 4.mdio_support */
 388        cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
 389
 390        return 0;
 391}
 392
 393static u32 hns3_get_rss_key_size(struct net_device *netdev)
 394{
 395        struct hns3_nic_priv *priv = netdev_priv(netdev);
 396        struct hnae3_handle *h = priv->ae_handle;
 397
 398        if (!h->ae_algo || !h->ae_algo->ops ||
 399            !h->ae_algo->ops->get_rss_key_size)
 400                return -EOPNOTSUPP;
 401
 402        return h->ae_algo->ops->get_rss_key_size(h);
 403}
 404
 405static u32 hns3_get_rss_indir_size(struct net_device *netdev)
 406{
 407        struct hns3_nic_priv *priv = netdev_priv(netdev);
 408        struct hnae3_handle *h = priv->ae_handle;
 409
 410        if (!h->ae_algo || !h->ae_algo->ops ||
 411            !h->ae_algo->ops->get_rss_indir_size)
 412                return -EOPNOTSUPP;
 413
 414        return h->ae_algo->ops->get_rss_indir_size(h);
 415}
 416
 417static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
 418                        u8 *hfunc)
 419{
 420        struct hns3_nic_priv *priv = netdev_priv(netdev);
 421        struct hnae3_handle *h = priv->ae_handle;
 422
 423        if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
 424                return -EOPNOTSUPP;
 425
 426        return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
 427}
 428
 429static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
 430                        const u8 *key, const u8 hfunc)
 431{
 432        struct hns3_nic_priv *priv = netdev_priv(netdev);
 433        struct hnae3_handle *h = priv->ae_handle;
 434
 435        if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
 436                return -EOPNOTSUPP;
 437
 438        /* currently we only support Toeplitz hash */
 439        if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
 440                netdev_err(netdev,
 441                           "hash func not supported (only Toeplitz hash)\n");
 442                return -EOPNOTSUPP;
 443        }
 444        if (!indir) {
 445                netdev_err(netdev,
 446                           "set rss failed for indir is empty\n");
 447                return -EOPNOTSUPP;
 448        }
 449
 450        return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
 451}
 452
 453static int hns3_get_rxnfc(struct net_device *netdev,
 454                          struct ethtool_rxnfc *cmd,
 455                          u32 *rule_locs)
 456{
 457        struct hns3_nic_priv *priv = netdev_priv(netdev);
 458        struct hnae3_handle *h = priv->ae_handle;
 459
 460        if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_tc_size)
 461                return -EOPNOTSUPP;
 462
 463        switch (cmd->cmd) {
 464        case ETHTOOL_GRXRINGS:
 465                cmd->data = h->ae_algo->ops->get_tc_size(h);
 466                break;
 467        default:
 468                return -EOPNOTSUPP;
 469        }
 470
 471        return 0;
 472}
 473
 474static const struct ethtool_ops hns3_ethtool_ops = {
 475        .get_drvinfo = hns3_get_drvinfo,
 476        .get_link = hns3_get_link,
 477        .get_ringparam = hns3_get_ringparam,
 478        .get_pauseparam = hns3_get_pauseparam,
 479        .get_strings = hns3_get_strings,
 480        .get_ethtool_stats = hns3_get_stats,
 481        .get_sset_count = hns3_get_sset_count,
 482        .get_rxnfc = hns3_get_rxnfc,
 483        .get_rxfh_key_size = hns3_get_rss_key_size,
 484        .get_rxfh_indir_size = hns3_get_rss_indir_size,
 485        .get_rxfh = hns3_get_rss,
 486        .set_rxfh = hns3_set_rss,
 487        .get_link_ksettings = hns3_get_link_ksettings,
 488};
 489
 490void hns3_ethtool_set_ops(struct net_device *netdev)
 491{
 492        netdev->ethtool_ops = &hns3_ethtool_ops;
 493}
 494