linux/drivers/net/ethernet/broadcom/bnxt/bnxt_ethtool.c
<<
>>
Prefs
   1/* Broadcom NetXtreme-C/E network driver.
   2 *
   3 * Copyright (c) 2014-2016 Broadcom Corporation
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation.
   8 */
   9
  10#include <linux/ctype.h>
  11#include <linux/stringify.h>
  12#include <linux/ethtool.h>
  13#include <linux/interrupt.h>
  14#include <linux/pci.h>
  15#include <linux/etherdevice.h>
  16#include <linux/crc32.h>
  17#include <linux/firmware.h>
  18#include "bnxt_hsi.h"
  19#include "bnxt.h"
  20#include "bnxt_ethtool.h"
  21#include "bnxt_nvm_defs.h"      /* NVRAM content constant and structure defs */
  22#include "bnxt_fw_hdr.h"        /* Firmware hdr constant and structure defs */
  23#define FLASH_NVRAM_TIMEOUT     ((HWRM_CMD_TIMEOUT) * 100)
  24
  25static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen);
  26
  27static u32 bnxt_get_msglevel(struct net_device *dev)
  28{
  29        struct bnxt *bp = netdev_priv(dev);
  30
  31        return bp->msg_enable;
  32}
  33
  34static void bnxt_set_msglevel(struct net_device *dev, u32 value)
  35{
  36        struct bnxt *bp = netdev_priv(dev);
  37
  38        bp->msg_enable = value;
  39}
  40
  41static int bnxt_get_coalesce(struct net_device *dev,
  42                             struct ethtool_coalesce *coal)
  43{
  44        struct bnxt *bp = netdev_priv(dev);
  45
  46        memset(coal, 0, sizeof(*coal));
  47
  48        coal->rx_coalesce_usecs = bp->rx_coal_ticks;
  49        /* 2 completion records per rx packet */
  50        coal->rx_max_coalesced_frames = bp->rx_coal_bufs / 2;
  51        coal->rx_coalesce_usecs_irq = bp->rx_coal_ticks_irq;
  52        coal->rx_max_coalesced_frames_irq = bp->rx_coal_bufs_irq / 2;
  53
  54        coal->tx_coalesce_usecs = bp->tx_coal_ticks;
  55        coal->tx_max_coalesced_frames = bp->tx_coal_bufs;
  56        coal->tx_coalesce_usecs_irq = bp->tx_coal_ticks_irq;
  57        coal->tx_max_coalesced_frames_irq = bp->tx_coal_bufs_irq;
  58
  59        coal->stats_block_coalesce_usecs = bp->stats_coal_ticks;
  60
  61        return 0;
  62}
  63
  64static int bnxt_set_coalesce(struct net_device *dev,
  65                             struct ethtool_coalesce *coal)
  66{
  67        struct bnxt *bp = netdev_priv(dev);
  68        bool update_stats = false;
  69        int rc = 0;
  70
  71        bp->rx_coal_ticks = coal->rx_coalesce_usecs;
  72        /* 2 completion records per rx packet */
  73        bp->rx_coal_bufs = coal->rx_max_coalesced_frames * 2;
  74        bp->rx_coal_ticks_irq = coal->rx_coalesce_usecs_irq;
  75        bp->rx_coal_bufs_irq = coal->rx_max_coalesced_frames_irq * 2;
  76
  77        bp->tx_coal_ticks = coal->tx_coalesce_usecs;
  78        bp->tx_coal_bufs = coal->tx_max_coalesced_frames;
  79        bp->tx_coal_ticks_irq = coal->tx_coalesce_usecs_irq;
  80        bp->tx_coal_bufs_irq = coal->tx_max_coalesced_frames_irq;
  81
  82        if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) {
  83                u32 stats_ticks = coal->stats_block_coalesce_usecs;
  84
  85                stats_ticks = clamp_t(u32, stats_ticks,
  86                                      BNXT_MIN_STATS_COAL_TICKS,
  87                                      BNXT_MAX_STATS_COAL_TICKS);
  88                stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS);
  89                bp->stats_coal_ticks = stats_ticks;
  90                update_stats = true;
  91        }
  92
  93        if (netif_running(dev)) {
  94                if (update_stats) {
  95                        rc = bnxt_close_nic(bp, true, false);
  96                        if (!rc)
  97                                rc = bnxt_open_nic(bp, true, false);
  98                } else {
  99                        rc = bnxt_hwrm_set_coal(bp);
 100                }
 101        }
 102
 103        return rc;
 104}
 105
 106#define BNXT_NUM_STATS  21
 107
 108#define BNXT_RX_STATS_OFFSET(counter)   \
 109        (offsetof(struct rx_port_stats, counter) / 8)
 110
 111#define BNXT_RX_STATS_ENTRY(counter)    \
 112        { BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
 113
 114#define BNXT_TX_STATS_OFFSET(counter)                   \
 115        ((offsetof(struct tx_port_stats, counter) +     \
 116          sizeof(struct rx_port_stats) + 512) / 8)
 117
 118#define BNXT_TX_STATS_ENTRY(counter)    \
 119        { BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
 120
 121static const struct {
 122        long offset;
 123        char string[ETH_GSTRING_LEN];
 124} bnxt_port_stats_arr[] = {
 125        BNXT_RX_STATS_ENTRY(rx_64b_frames),
 126        BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
 127        BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
 128        BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
 129        BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
 130        BNXT_RX_STATS_ENTRY(rx_1024b_1518_frames),
 131        BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
 132        BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
 133        BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
 134        BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
 135        BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
 136        BNXT_RX_STATS_ENTRY(rx_total_frames),
 137        BNXT_RX_STATS_ENTRY(rx_ucast_frames),
 138        BNXT_RX_STATS_ENTRY(rx_mcast_frames),
 139        BNXT_RX_STATS_ENTRY(rx_bcast_frames),
 140        BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
 141        BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
 142        BNXT_RX_STATS_ENTRY(rx_pause_frames),
 143        BNXT_RX_STATS_ENTRY(rx_pfc_frames),
 144        BNXT_RX_STATS_ENTRY(rx_align_err_frames),
 145        BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
 146        BNXT_RX_STATS_ENTRY(rx_jbr_frames),
 147        BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
 148        BNXT_RX_STATS_ENTRY(rx_tagged_frames),
 149        BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
 150        BNXT_RX_STATS_ENTRY(rx_good_frames),
 151        BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
 152        BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
 153        BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
 154        BNXT_RX_STATS_ENTRY(rx_bytes),
 155        BNXT_RX_STATS_ENTRY(rx_runt_bytes),
 156        BNXT_RX_STATS_ENTRY(rx_runt_frames),
 157
 158        BNXT_TX_STATS_ENTRY(tx_64b_frames),
 159        BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
 160        BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
 161        BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
 162        BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
 163        BNXT_TX_STATS_ENTRY(tx_1024b_1518_frames),
 164        BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
 165        BNXT_TX_STATS_ENTRY(tx_1519b_2047_frames),
 166        BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
 167        BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
 168        BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
 169        BNXT_TX_STATS_ENTRY(tx_good_frames),
 170        BNXT_TX_STATS_ENTRY(tx_total_frames),
 171        BNXT_TX_STATS_ENTRY(tx_ucast_frames),
 172        BNXT_TX_STATS_ENTRY(tx_mcast_frames),
 173        BNXT_TX_STATS_ENTRY(tx_bcast_frames),
 174        BNXT_TX_STATS_ENTRY(tx_pause_frames),
 175        BNXT_TX_STATS_ENTRY(tx_pfc_frames),
 176        BNXT_TX_STATS_ENTRY(tx_jabber_frames),
 177        BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
 178        BNXT_TX_STATS_ENTRY(tx_err),
 179        BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
 180        BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
 181        BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
 182        BNXT_TX_STATS_ENTRY(tx_total_collisions),
 183        BNXT_TX_STATS_ENTRY(tx_bytes),
 184};
 185
 186#define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
 187
 188static int bnxt_get_sset_count(struct net_device *dev, int sset)
 189{
 190        struct bnxt *bp = netdev_priv(dev);
 191
 192        switch (sset) {
 193        case ETH_SS_STATS: {
 194                int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
 195
 196                if (bp->flags & BNXT_FLAG_PORT_STATS)
 197                        num_stats += BNXT_NUM_PORT_STATS;
 198
 199                return num_stats;
 200        }
 201        default:
 202                return -EOPNOTSUPP;
 203        }
 204}
 205
 206static void bnxt_get_ethtool_stats(struct net_device *dev,
 207                                   struct ethtool_stats *stats, u64 *buf)
 208{
 209        u32 i, j = 0;
 210        struct bnxt *bp = netdev_priv(dev);
 211        u32 buf_size = sizeof(struct ctx_hw_stats) * bp->cp_nr_rings;
 212        u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
 213
 214        memset(buf, 0, buf_size);
 215
 216        if (!bp->bnapi)
 217                return;
 218
 219        for (i = 0; i < bp->cp_nr_rings; i++) {
 220                struct bnxt_napi *bnapi = bp->bnapi[i];
 221                struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
 222                __le64 *hw_stats = (__le64 *)cpr->hw_stats;
 223                int k;
 224
 225                for (k = 0; k < stat_fields; j++, k++)
 226                        buf[j] = le64_to_cpu(hw_stats[k]);
 227                buf[j++] = cpr->rx_l4_csum_errors;
 228        }
 229        if (bp->flags & BNXT_FLAG_PORT_STATS) {
 230                __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
 231
 232                for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
 233                        buf[j] = le64_to_cpu(*(port_stats +
 234                                               bnxt_port_stats_arr[i].offset));
 235                }
 236        }
 237}
 238
 239static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
 240{
 241        struct bnxt *bp = netdev_priv(dev);
 242        u32 i;
 243
 244        switch (stringset) {
 245        /* The number of strings must match BNXT_NUM_STATS defined above. */
 246        case ETH_SS_STATS:
 247                for (i = 0; i < bp->cp_nr_rings; i++) {
 248                        sprintf(buf, "[%d]: rx_ucast_packets", i);
 249                        buf += ETH_GSTRING_LEN;
 250                        sprintf(buf, "[%d]: rx_mcast_packets", i);
 251                        buf += ETH_GSTRING_LEN;
 252                        sprintf(buf, "[%d]: rx_bcast_packets", i);
 253                        buf += ETH_GSTRING_LEN;
 254                        sprintf(buf, "[%d]: rx_discards", i);
 255                        buf += ETH_GSTRING_LEN;
 256                        sprintf(buf, "[%d]: rx_drops", i);
 257                        buf += ETH_GSTRING_LEN;
 258                        sprintf(buf, "[%d]: rx_ucast_bytes", i);
 259                        buf += ETH_GSTRING_LEN;
 260                        sprintf(buf, "[%d]: rx_mcast_bytes", i);
 261                        buf += ETH_GSTRING_LEN;
 262                        sprintf(buf, "[%d]: rx_bcast_bytes", i);
 263                        buf += ETH_GSTRING_LEN;
 264                        sprintf(buf, "[%d]: tx_ucast_packets", i);
 265                        buf += ETH_GSTRING_LEN;
 266                        sprintf(buf, "[%d]: tx_mcast_packets", i);
 267                        buf += ETH_GSTRING_LEN;
 268                        sprintf(buf, "[%d]: tx_bcast_packets", i);
 269                        buf += ETH_GSTRING_LEN;
 270                        sprintf(buf, "[%d]: tx_discards", i);
 271                        buf += ETH_GSTRING_LEN;
 272                        sprintf(buf, "[%d]: tx_drops", i);
 273                        buf += ETH_GSTRING_LEN;
 274                        sprintf(buf, "[%d]: tx_ucast_bytes", i);
 275                        buf += ETH_GSTRING_LEN;
 276                        sprintf(buf, "[%d]: tx_mcast_bytes", i);
 277                        buf += ETH_GSTRING_LEN;
 278                        sprintf(buf, "[%d]: tx_bcast_bytes", i);
 279                        buf += ETH_GSTRING_LEN;
 280                        sprintf(buf, "[%d]: tpa_packets", i);
 281                        buf += ETH_GSTRING_LEN;
 282                        sprintf(buf, "[%d]: tpa_bytes", i);
 283                        buf += ETH_GSTRING_LEN;
 284                        sprintf(buf, "[%d]: tpa_events", i);
 285                        buf += ETH_GSTRING_LEN;
 286                        sprintf(buf, "[%d]: tpa_aborts", i);
 287                        buf += ETH_GSTRING_LEN;
 288                        sprintf(buf, "[%d]: rx_l4_csum_errors", i);
 289                        buf += ETH_GSTRING_LEN;
 290                }
 291                if (bp->flags & BNXT_FLAG_PORT_STATS) {
 292                        for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
 293                                strcpy(buf, bnxt_port_stats_arr[i].string);
 294                                buf += ETH_GSTRING_LEN;
 295                        }
 296                }
 297                break;
 298        default:
 299                netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
 300                           stringset);
 301                break;
 302        }
 303}
 304
 305static void bnxt_get_ringparam(struct net_device *dev,
 306                               struct ethtool_ringparam *ering)
 307{
 308        struct bnxt *bp = netdev_priv(dev);
 309
 310        ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
 311        ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
 312        ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
 313
 314        ering->rx_pending = bp->rx_ring_size;
 315        ering->rx_jumbo_pending = bp->rx_agg_ring_size;
 316        ering->tx_pending = bp->tx_ring_size;
 317}
 318
 319static int bnxt_set_ringparam(struct net_device *dev,
 320                              struct ethtool_ringparam *ering)
 321{
 322        struct bnxt *bp = netdev_priv(dev);
 323
 324        if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
 325            (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
 326            (ering->tx_pending <= MAX_SKB_FRAGS))
 327                return -EINVAL;
 328
 329        if (netif_running(dev))
 330                bnxt_close_nic(bp, false, false);
 331
 332        bp->rx_ring_size = ering->rx_pending;
 333        bp->tx_ring_size = ering->tx_pending;
 334        bnxt_set_ring_params(bp);
 335
 336        if (netif_running(dev))
 337                return bnxt_open_nic(bp, false, false);
 338
 339        return 0;
 340}
 341
 342static void bnxt_get_channels(struct net_device *dev,
 343                              struct ethtool_channels *channel)
 344{
 345        struct bnxt *bp = netdev_priv(dev);
 346        int max_rx_rings, max_tx_rings, tcs;
 347
 348        bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
 349        channel->max_combined = max_rx_rings;
 350
 351        if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) {
 352                max_rx_rings = 0;
 353                max_tx_rings = 0;
 354        }
 355
 356        tcs = netdev_get_num_tc(dev);
 357        if (tcs > 1)
 358                max_tx_rings /= tcs;
 359
 360        channel->max_rx = max_rx_rings;
 361        channel->max_tx = max_tx_rings;
 362        channel->max_other = 0;
 363        if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
 364                channel->combined_count = bp->rx_nr_rings;
 365                if (BNXT_CHIP_TYPE_NITRO_A0(bp))
 366                        channel->combined_count--;
 367        } else {
 368                if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) {
 369                        channel->rx_count = bp->rx_nr_rings;
 370                        channel->tx_count = bp->tx_nr_rings_per_tc;
 371                }
 372        }
 373}
 374
 375static int bnxt_set_channels(struct net_device *dev,
 376                             struct ethtool_channels *channel)
 377{
 378        struct bnxt *bp = netdev_priv(dev);
 379        int max_rx_rings, max_tx_rings, tcs;
 380        u32 rc = 0;
 381        bool sh = false;
 382
 383        if (channel->other_count)
 384                return -EINVAL;
 385
 386        if (!channel->combined_count &&
 387            (!channel->rx_count || !channel->tx_count))
 388                return -EINVAL;
 389
 390        if (channel->combined_count &&
 391            (channel->rx_count || channel->tx_count))
 392                return -EINVAL;
 393
 394        if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count ||
 395                                            channel->tx_count))
 396                return -EINVAL;
 397
 398        if (channel->combined_count)
 399                sh = true;
 400
 401        bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
 402
 403        tcs = netdev_get_num_tc(dev);
 404        if (tcs > 1)
 405                max_tx_rings /= tcs;
 406
 407        if (sh && (channel->combined_count > max_rx_rings ||
 408                   channel->combined_count > max_tx_rings))
 409                return -ENOMEM;
 410
 411        if (!sh && (channel->rx_count > max_rx_rings ||
 412                    channel->tx_count > max_tx_rings))
 413                return -ENOMEM;
 414
 415        if (netif_running(dev)) {
 416                if (BNXT_PF(bp)) {
 417                        /* TODO CHIMP_FW: Send message to all VF's
 418                         * before PF unload
 419                         */
 420                }
 421                rc = bnxt_close_nic(bp, true, false);
 422                if (rc) {
 423                        netdev_err(bp->dev, "Set channel failure rc :%x\n",
 424                                   rc);
 425                        return rc;
 426                }
 427        }
 428
 429        if (sh) {
 430                bp->flags |= BNXT_FLAG_SHARED_RINGS;
 431                bp->rx_nr_rings = channel->combined_count;
 432                bp->tx_nr_rings_per_tc = channel->combined_count;
 433        } else {
 434                bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
 435                bp->rx_nr_rings = channel->rx_count;
 436                bp->tx_nr_rings_per_tc = channel->tx_count;
 437        }
 438
 439        bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
 440        if (tcs > 1)
 441                bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
 442
 443        bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
 444                               bp->tx_nr_rings + bp->rx_nr_rings;
 445
 446        bp->num_stat_ctxs = bp->cp_nr_rings;
 447
 448        /* After changing number of rx channels, update NTUPLE feature. */
 449        netdev_update_features(dev);
 450        if (netif_running(dev)) {
 451                rc = bnxt_open_nic(bp, true, false);
 452                if ((!rc) && BNXT_PF(bp)) {
 453                        /* TODO CHIMP_FW: Send message to all VF's
 454                         * to renable
 455                         */
 456                }
 457        }
 458
 459        return rc;
 460}
 461
 462#ifdef CONFIG_RFS_ACCEL
 463static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
 464                            u32 *rule_locs)
 465{
 466        int i, j = 0;
 467
 468        cmd->data = bp->ntp_fltr_count;
 469        for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
 470                struct hlist_head *head;
 471                struct bnxt_ntuple_filter *fltr;
 472
 473                head = &bp->ntp_fltr_hash_tbl[i];
 474                rcu_read_lock();
 475                hlist_for_each_entry_rcu(fltr, head, hash) {
 476                        if (j == cmd->rule_cnt)
 477                                break;
 478                        rule_locs[j++] = fltr->sw_id;
 479                }
 480                rcu_read_unlock();
 481                if (j == cmd->rule_cnt)
 482                        break;
 483        }
 484        cmd->rule_cnt = j;
 485        return 0;
 486}
 487
 488static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
 489{
 490        struct ethtool_rx_flow_spec *fs =
 491                (struct ethtool_rx_flow_spec *)&cmd->fs;
 492        struct bnxt_ntuple_filter *fltr;
 493        struct flow_keys *fkeys;
 494        int i, rc = -EINVAL;
 495
 496        if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
 497                return rc;
 498
 499        for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
 500                struct hlist_head *head;
 501
 502                head = &bp->ntp_fltr_hash_tbl[i];
 503                rcu_read_lock();
 504                hlist_for_each_entry_rcu(fltr, head, hash) {
 505                        if (fltr->sw_id == fs->location)
 506                                goto fltr_found;
 507                }
 508                rcu_read_unlock();
 509        }
 510        return rc;
 511
 512fltr_found:
 513        fkeys = &fltr->fkeys;
 514        if (fkeys->basic.ip_proto == IPPROTO_TCP)
 515                fs->flow_type = TCP_V4_FLOW;
 516        else if (fkeys->basic.ip_proto == IPPROTO_UDP)
 517                fs->flow_type = UDP_V4_FLOW;
 518        else
 519                goto fltr_err;
 520
 521        fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
 522        fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
 523
 524        fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
 525        fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
 526
 527        fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
 528        fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
 529
 530        fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
 531        fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
 532
 533        fs->ring_cookie = fltr->rxq;
 534        rc = 0;
 535
 536fltr_err:
 537        rcu_read_unlock();
 538
 539        return rc;
 540}
 541
 542static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
 543                          u32 *rule_locs)
 544{
 545        struct bnxt *bp = netdev_priv(dev);
 546        int rc = 0;
 547
 548        switch (cmd->cmd) {
 549        case ETHTOOL_GRXRINGS:
 550                cmd->data = bp->rx_nr_rings;
 551                break;
 552
 553        case ETHTOOL_GRXCLSRLCNT:
 554                cmd->rule_cnt = bp->ntp_fltr_count;
 555                cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
 556                break;
 557
 558        case ETHTOOL_GRXCLSRLALL:
 559                rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
 560                break;
 561
 562        case ETHTOOL_GRXCLSRULE:
 563                rc = bnxt_grxclsrule(bp, cmd);
 564                break;
 565
 566        default:
 567                rc = -EOPNOTSUPP;
 568                break;
 569        }
 570
 571        return rc;
 572}
 573#endif
 574
 575static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
 576{
 577        return HW_HASH_INDEX_SIZE;
 578}
 579
 580static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
 581{
 582        return HW_HASH_KEY_SIZE;
 583}
 584
 585static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
 586                         u8 *hfunc)
 587{
 588        struct bnxt *bp = netdev_priv(dev);
 589        struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
 590        int i = 0;
 591
 592        if (hfunc)
 593                *hfunc = ETH_RSS_HASH_TOP;
 594
 595        if (indir)
 596                for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
 597                        indir[i] = le16_to_cpu(vnic->rss_table[i]);
 598
 599        if (key)
 600                memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
 601
 602        return 0;
 603}
 604
 605static void bnxt_get_drvinfo(struct net_device *dev,
 606                             struct ethtool_drvinfo *info)
 607{
 608        struct bnxt *bp = netdev_priv(dev);
 609        char *pkglog;
 610        char *pkgver = NULL;
 611
 612        pkglog = kmalloc(BNX_PKG_LOG_MAX_LENGTH, GFP_KERNEL);
 613        if (pkglog)
 614                pkgver = bnxt_get_pkgver(dev, pkglog, BNX_PKG_LOG_MAX_LENGTH);
 615        strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
 616        strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
 617        if (pkgver && *pkgver != 0 && isdigit(*pkgver))
 618                snprintf(info->fw_version, sizeof(info->fw_version) - 1,
 619                         "%s pkg %s", bp->fw_ver_str, pkgver);
 620        else
 621                strlcpy(info->fw_version, bp->fw_ver_str,
 622                        sizeof(info->fw_version));
 623        strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
 624        info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
 625        info->testinfo_len = BNXT_NUM_TESTS(bp);
 626        /* TODO CHIMP_FW: eeprom dump details */
 627        info->eedump_len = 0;
 628        /* TODO CHIMP FW: reg dump details */
 629        info->regdump_len = 0;
 630        kfree(pkglog);
 631}
 632
 633u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
 634{
 635        u32 speed_mask = 0;
 636
 637        /* TODO: support 25GB, 40GB, 50GB with different cable type */
 638        /* set the advertised speeds */
 639        if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
 640                speed_mask |= ADVERTISED_100baseT_Full;
 641        if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
 642                speed_mask |= ADVERTISED_1000baseT_Full;
 643        if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
 644                speed_mask |= ADVERTISED_2500baseX_Full;
 645        if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
 646                speed_mask |= ADVERTISED_10000baseT_Full;
 647        if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
 648                speed_mask |= ADVERTISED_40000baseCR4_Full;
 649
 650        if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
 651                speed_mask |= ADVERTISED_Pause;
 652        else if (fw_pause & BNXT_LINK_PAUSE_TX)
 653                speed_mask |= ADVERTISED_Asym_Pause;
 654        else if (fw_pause & BNXT_LINK_PAUSE_RX)
 655                speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
 656
 657        return speed_mask;
 658}
 659
 660#define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\
 661{                                                                       \
 662        if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB)                    \
 663                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 664                                                     100baseT_Full);    \
 665        if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB)                      \
 666                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 667                                                     1000baseT_Full);   \
 668        if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB)                     \
 669                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 670                                                     10000baseT_Full);  \
 671        if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB)                     \
 672                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 673                                                     25000baseCR_Full); \
 674        if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB)                     \
 675                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 676                                                     40000baseCR4_Full);\
 677        if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB)                     \
 678                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 679                                                     50000baseCR2_Full);\
 680        if ((fw_pause) & BNXT_LINK_PAUSE_RX) {                          \
 681                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 682                                                     Pause);            \
 683                if (!((fw_pause) & BNXT_LINK_PAUSE_TX))                 \
 684                        ethtool_link_ksettings_add_link_mode(           \
 685                                        lk_ksettings, name, Asym_Pause);\
 686        } else if ((fw_pause) & BNXT_LINK_PAUSE_TX) {                   \
 687                ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\
 688                                                     Asym_Pause);       \
 689        }                                                               \
 690}
 691
 692#define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name)          \
 693{                                                                       \
 694        if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 695                                                  100baseT_Full) ||     \
 696            ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 697                                                  100baseT_Half))       \
 698                (fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB;               \
 699        if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 700                                                  1000baseT_Full) ||    \
 701            ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 702                                                  1000baseT_Half))      \
 703                (fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB;                 \
 704        if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 705                                                  10000baseT_Full))     \
 706                (fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB;                \
 707        if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 708                                                  25000baseCR_Full))    \
 709                (fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB;                \
 710        if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 711                                                  40000baseCR4_Full))   \
 712                (fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB;                \
 713        if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name,   \
 714                                                  50000baseCR2_Full))   \
 715                (fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB;                \
 716}
 717
 718static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info,
 719                                struct ethtool_link_ksettings *lk_ksettings)
 720{
 721        u16 fw_speeds = link_info->auto_link_speeds;
 722        u8 fw_pause = 0;
 723
 724        if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
 725                fw_pause = link_info->auto_pause_setting;
 726
 727        BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising);
 728}
 729
 730static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info,
 731                                struct ethtool_link_ksettings *lk_ksettings)
 732{
 733        u16 fw_speeds = link_info->lp_auto_link_speeds;
 734        u8 fw_pause = 0;
 735
 736        if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
 737                fw_pause = link_info->lp_pause;
 738
 739        BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings,
 740                                lp_advertising);
 741}
 742
 743static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info,
 744                                struct ethtool_link_ksettings *lk_ksettings)
 745{
 746        u16 fw_speeds = link_info->support_speeds;
 747
 748        BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported);
 749
 750        ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause);
 751        ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
 752                                             Asym_Pause);
 753
 754        if (link_info->support_auto_speeds)
 755                ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
 756                                                     Autoneg);
 757}
 758
 759u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
 760{
 761        switch (fw_link_speed) {
 762        case BNXT_LINK_SPEED_100MB:
 763                return SPEED_100;
 764        case BNXT_LINK_SPEED_1GB:
 765                return SPEED_1000;
 766        case BNXT_LINK_SPEED_2_5GB:
 767                return SPEED_2500;
 768        case BNXT_LINK_SPEED_10GB:
 769                return SPEED_10000;
 770        case BNXT_LINK_SPEED_20GB:
 771                return SPEED_20000;
 772        case BNXT_LINK_SPEED_25GB:
 773                return SPEED_25000;
 774        case BNXT_LINK_SPEED_40GB:
 775                return SPEED_40000;
 776        case BNXT_LINK_SPEED_50GB:
 777                return SPEED_50000;
 778        default:
 779                return SPEED_UNKNOWN;
 780        }
 781}
 782
 783static int bnxt_get_link_ksettings(struct net_device *dev,
 784                                   struct ethtool_link_ksettings *lk_ksettings)
 785{
 786        struct bnxt *bp = netdev_priv(dev);
 787        struct bnxt_link_info *link_info = &bp->link_info;
 788        struct ethtool_link_settings *base = &lk_ksettings->base;
 789        u32 ethtool_speed;
 790
 791        ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported);
 792        bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings);
 793
 794        ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising);
 795        if (link_info->autoneg) {
 796                bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings);
 797                ethtool_link_ksettings_add_link_mode(lk_ksettings,
 798                                                     advertising, Autoneg);
 799                base->autoneg = AUTONEG_ENABLE;
 800                if (link_info->phy_link_status == BNXT_LINK_LINK)
 801                        bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings);
 802                ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
 803                if (!netif_carrier_ok(dev))
 804                        base->duplex = DUPLEX_UNKNOWN;
 805                else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
 806                        base->duplex = DUPLEX_FULL;
 807                else
 808                        base->duplex = DUPLEX_HALF;
 809        } else {
 810                base->autoneg = AUTONEG_DISABLE;
 811                ethtool_speed =
 812                        bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
 813                base->duplex = DUPLEX_HALF;
 814                if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
 815                        base->duplex = DUPLEX_FULL;
 816        }
 817        base->speed = ethtool_speed;
 818
 819        base->port = PORT_NONE;
 820        if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
 821                base->port = PORT_TP;
 822                ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
 823                                                     TP);
 824                ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
 825                                                     TP);
 826        } else {
 827                ethtool_link_ksettings_add_link_mode(lk_ksettings, supported,
 828                                                     FIBRE);
 829                ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising,
 830                                                     FIBRE);
 831
 832                if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
 833                        base->port = PORT_DA;
 834                else if (link_info->media_type ==
 835                         PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
 836                        base->port = PORT_FIBRE;
 837        }
 838        base->phy_address = link_info->phy_addr;
 839
 840        return 0;
 841}
 842
 843static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed)
 844{
 845        struct bnxt *bp = netdev_priv(dev);
 846        struct bnxt_link_info *link_info = &bp->link_info;
 847        u16 support_spds = link_info->support_speeds;
 848        u32 fw_speed = 0;
 849
 850        switch (ethtool_speed) {
 851        case SPEED_100:
 852                if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
 853                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
 854                break;
 855        case SPEED_1000:
 856                if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
 857                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
 858                break;
 859        case SPEED_2500:
 860                if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
 861                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
 862                break;
 863        case SPEED_10000:
 864                if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
 865                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
 866                break;
 867        case SPEED_20000:
 868                if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
 869                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
 870                break;
 871        case SPEED_25000:
 872                if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
 873                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
 874                break;
 875        case SPEED_40000:
 876                if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
 877                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
 878                break;
 879        case SPEED_50000:
 880                if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
 881                        fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
 882                break;
 883        default:
 884                netdev_err(dev, "unsupported speed!\n");
 885                break;
 886        }
 887        return fw_speed;
 888}
 889
 890u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
 891{
 892        u16 fw_speed_mask = 0;
 893
 894        /* only support autoneg at speed 100, 1000, and 10000 */
 895        if (advertising & (ADVERTISED_100baseT_Full |
 896                           ADVERTISED_100baseT_Half)) {
 897                fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
 898        }
 899        if (advertising & (ADVERTISED_1000baseT_Full |
 900                           ADVERTISED_1000baseT_Half)) {
 901                fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
 902        }
 903        if (advertising & ADVERTISED_10000baseT_Full)
 904                fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
 905
 906        if (advertising & ADVERTISED_40000baseCR4_Full)
 907                fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
 908
 909        return fw_speed_mask;
 910}
 911
 912static int bnxt_set_link_ksettings(struct net_device *dev,
 913                           const struct ethtool_link_ksettings *lk_ksettings)
 914{
 915        struct bnxt *bp = netdev_priv(dev);
 916        struct bnxt_link_info *link_info = &bp->link_info;
 917        const struct ethtool_link_settings *base = &lk_ksettings->base;
 918        u32 speed, fw_advertising = 0;
 919        bool set_pause = false;
 920        int rc = 0;
 921
 922        if (!BNXT_SINGLE_PF(bp))
 923                return -EOPNOTSUPP;
 924
 925        if (base->autoneg == AUTONEG_ENABLE) {
 926                BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings,
 927                                        advertising);
 928                link_info->autoneg |= BNXT_AUTONEG_SPEED;
 929                if (!fw_advertising)
 930                        link_info->advertising = link_info->support_auto_speeds;
 931                else
 932                        link_info->advertising = fw_advertising;
 933                /* any change to autoneg will cause link change, therefore the
 934                 * driver should put back the original pause setting in autoneg
 935                 */
 936                set_pause = true;
 937        } else {
 938                u16 fw_speed;
 939                u8 phy_type = link_info->phy_type;
 940
 941                if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET  ||
 942                    phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
 943                    link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
 944                        netdev_err(dev, "10GBase-T devices must autoneg\n");
 945                        rc = -EINVAL;
 946                        goto set_setting_exit;
 947                }
 948                if (base->duplex == DUPLEX_HALF) {
 949                        netdev_err(dev, "HALF DUPLEX is not supported!\n");
 950                        rc = -EINVAL;
 951                        goto set_setting_exit;
 952                }
 953                speed = base->speed;
 954                fw_speed = bnxt_get_fw_speed(dev, speed);
 955                if (!fw_speed) {
 956                        rc = -EINVAL;
 957                        goto set_setting_exit;
 958                }
 959                link_info->req_link_speed = fw_speed;
 960                link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
 961                link_info->autoneg = 0;
 962                link_info->advertising = 0;
 963        }
 964
 965        if (netif_running(dev))
 966                rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
 967
 968set_setting_exit:
 969        return rc;
 970}
 971
 972static void bnxt_get_pauseparam(struct net_device *dev,
 973                                struct ethtool_pauseparam *epause)
 974{
 975        struct bnxt *bp = netdev_priv(dev);
 976        struct bnxt_link_info *link_info = &bp->link_info;
 977
 978        if (BNXT_VF(bp))
 979                return;
 980        epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
 981        epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
 982        epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
 983}
 984
 985static int bnxt_set_pauseparam(struct net_device *dev,
 986                               struct ethtool_pauseparam *epause)
 987{
 988        int rc = 0;
 989        struct bnxt *bp = netdev_priv(dev);
 990        struct bnxt_link_info *link_info = &bp->link_info;
 991
 992        if (!BNXT_SINGLE_PF(bp))
 993                return -EOPNOTSUPP;
 994
 995        if (epause->autoneg) {
 996                if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
 997                        return -EINVAL;
 998
 999                link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
1000                if (bp->hwrm_spec_code >= 0x10201)
1001                        link_info->req_flow_ctrl =
1002                                PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
1003        } else {
1004                /* when transition from auto pause to force pause,
1005                 * force a link change
1006                 */
1007                if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
1008                        link_info->force_link_chng = true;
1009                link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
1010                link_info->req_flow_ctrl = 0;
1011        }
1012        if (epause->rx_pause)
1013                link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
1014
1015        if (epause->tx_pause)
1016                link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
1017
1018        if (netif_running(dev))
1019                rc = bnxt_hwrm_set_pause(bp);
1020        return rc;
1021}
1022
1023static u32 bnxt_get_link(struct net_device *dev)
1024{
1025        struct bnxt *bp = netdev_priv(dev);
1026
1027        /* TODO: handle MF, VF, driver close case */
1028        return bp->link_info.link_up;
1029}
1030
1031static int bnxt_flash_nvram(struct net_device *dev,
1032                            u16 dir_type,
1033                            u16 dir_ordinal,
1034                            u16 dir_ext,
1035                            u16 dir_attr,
1036                            const u8 *data,
1037                            size_t data_len)
1038{
1039        struct bnxt *bp = netdev_priv(dev);
1040        int rc;
1041        struct hwrm_nvm_write_input req = {0};
1042        dma_addr_t dma_handle;
1043        u8 *kmem;
1044
1045        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
1046
1047        req.dir_type = cpu_to_le16(dir_type);
1048        req.dir_ordinal = cpu_to_le16(dir_ordinal);
1049        req.dir_ext = cpu_to_le16(dir_ext);
1050        req.dir_attr = cpu_to_le16(dir_attr);
1051        req.dir_data_length = cpu_to_le32(data_len);
1052
1053        kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
1054                                  GFP_KERNEL);
1055        if (!kmem) {
1056                netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1057                           (unsigned)data_len);
1058                return -ENOMEM;
1059        }
1060        memcpy(kmem, data, data_len);
1061        req.host_src_addr = cpu_to_le64(dma_handle);
1062
1063        rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
1064        dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
1065
1066        return rc;
1067}
1068
1069static int bnxt_firmware_reset(struct net_device *dev,
1070                               u16 dir_type)
1071{
1072        struct bnxt *bp = netdev_priv(dev);
1073        struct hwrm_fw_reset_input req = {0};
1074
1075        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
1076
1077        /* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */
1078        /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
1079        /*       (e.g. when firmware isn't already running) */
1080        switch (dir_type) {
1081        case BNX_DIR_TYPE_CHIMP_PATCH:
1082        case BNX_DIR_TYPE_BOOTCODE:
1083        case BNX_DIR_TYPE_BOOTCODE_2:
1084                req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1085                /* Self-reset ChiMP upon next PCIe reset: */
1086                req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1087                break;
1088        case BNX_DIR_TYPE_APE_FW:
1089        case BNX_DIR_TYPE_APE_PATCH:
1090                req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
1091                /* Self-reset APE upon next PCIe reset: */
1092                req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1093                break;
1094        case BNX_DIR_TYPE_KONG_FW:
1095        case BNX_DIR_TYPE_KONG_PATCH:
1096                req.embedded_proc_type =
1097                        FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1098                break;
1099        case BNX_DIR_TYPE_BONO_FW:
1100        case BNX_DIR_TYPE_BONO_PATCH:
1101                req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1102                break;
1103        default:
1104                return -EINVAL;
1105        }
1106
1107        return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1108}
1109
1110static int bnxt_flash_firmware(struct net_device *dev,
1111                               u16 dir_type,
1112                               const u8 *fw_data,
1113                               size_t fw_size)
1114{
1115        int     rc = 0;
1116        u16     code_type;
1117        u32     stored_crc;
1118        u32     calculated_crc;
1119        struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1120
1121        switch (dir_type) {
1122        case BNX_DIR_TYPE_BOOTCODE:
1123        case BNX_DIR_TYPE_BOOTCODE_2:
1124                code_type = CODE_BOOT;
1125                break;
1126        case BNX_DIR_TYPE_CHIMP_PATCH:
1127                code_type = CODE_CHIMP_PATCH;
1128                break;
1129        case BNX_DIR_TYPE_APE_FW:
1130                code_type = CODE_MCTP_PASSTHRU;
1131                break;
1132        case BNX_DIR_TYPE_APE_PATCH:
1133                code_type = CODE_APE_PATCH;
1134                break;
1135        case BNX_DIR_TYPE_KONG_FW:
1136                code_type = CODE_KONG_FW;
1137                break;
1138        case BNX_DIR_TYPE_KONG_PATCH:
1139                code_type = CODE_KONG_PATCH;
1140                break;
1141        case BNX_DIR_TYPE_BONO_FW:
1142                code_type = CODE_BONO_FW;
1143                break;
1144        case BNX_DIR_TYPE_BONO_PATCH:
1145                code_type = CODE_BONO_PATCH;
1146                break;
1147        default:
1148                netdev_err(dev, "Unsupported directory entry type: %u\n",
1149                           dir_type);
1150                return -EINVAL;
1151        }
1152        if (fw_size < sizeof(struct bnxt_fw_header)) {
1153                netdev_err(dev, "Invalid firmware file size: %u\n",
1154                           (unsigned int)fw_size);
1155                return -EINVAL;
1156        }
1157        if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1158                netdev_err(dev, "Invalid firmware signature: %08X\n",
1159                           le32_to_cpu(header->signature));
1160                return -EINVAL;
1161        }
1162        if (header->code_type != code_type) {
1163                netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1164                           code_type, header->code_type);
1165                return -EINVAL;
1166        }
1167        if (header->device != DEVICE_CUMULUS_FAMILY) {
1168                netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1169                           DEVICE_CUMULUS_FAMILY, header->device);
1170                return -EINVAL;
1171        }
1172        /* Confirm the CRC32 checksum of the file: */
1173        stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1174                                             sizeof(stored_crc)));
1175        calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1176        if (calculated_crc != stored_crc) {
1177                netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1178                           (unsigned long)stored_crc,
1179                           (unsigned long)calculated_crc);
1180                return -EINVAL;
1181        }
1182        /* TODO: Validate digital signature (RSA-encrypted SHA-256 hash) here */
1183        rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1184                              0, 0, fw_data, fw_size);
1185        if (rc == 0)    /* Firmware update successful */
1186                rc = bnxt_firmware_reset(dev, dir_type);
1187
1188        return rc;
1189}
1190
1191static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1192{
1193        switch (dir_type) {
1194        case BNX_DIR_TYPE_CHIMP_PATCH:
1195        case BNX_DIR_TYPE_BOOTCODE:
1196        case BNX_DIR_TYPE_BOOTCODE_2:
1197        case BNX_DIR_TYPE_APE_FW:
1198        case BNX_DIR_TYPE_APE_PATCH:
1199        case BNX_DIR_TYPE_KONG_FW:
1200        case BNX_DIR_TYPE_KONG_PATCH:
1201        case BNX_DIR_TYPE_BONO_FW:
1202        case BNX_DIR_TYPE_BONO_PATCH:
1203                return true;
1204        }
1205
1206        return false;
1207}
1208
1209static bool bnxt_dir_type_is_unprotected_exec_format(u16 dir_type)
1210{
1211        switch (dir_type) {
1212        case BNX_DIR_TYPE_AVS:
1213        case BNX_DIR_TYPE_EXP_ROM_MBA:
1214        case BNX_DIR_TYPE_PCIE:
1215        case BNX_DIR_TYPE_TSCF_UCODE:
1216        case BNX_DIR_TYPE_EXT_PHY:
1217        case BNX_DIR_TYPE_CCM:
1218        case BNX_DIR_TYPE_ISCSI_BOOT:
1219        case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1220        case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1221                return true;
1222        }
1223
1224        return false;
1225}
1226
1227static bool bnxt_dir_type_is_executable(u16 dir_type)
1228{
1229        return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1230                bnxt_dir_type_is_unprotected_exec_format(dir_type);
1231}
1232
1233static int bnxt_flash_firmware_from_file(struct net_device *dev,
1234                                         u16 dir_type,
1235                                         const char *filename)
1236{
1237        const struct firmware  *fw;
1238        int                     rc;
1239
1240        if (dir_type != BNX_DIR_TYPE_UPDATE &&
1241            bnxt_dir_type_is_executable(dir_type) == false)
1242                return -EINVAL;
1243
1244        rc = request_firmware(&fw, filename, &dev->dev);
1245        if (rc != 0) {
1246                netdev_err(dev, "Error %d requesting firmware file: %s\n",
1247                           rc, filename);
1248                return rc;
1249        }
1250        if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1251                rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1252        else
1253                rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1254                                      0, 0, fw->data, fw->size);
1255        release_firmware(fw);
1256        return rc;
1257}
1258
1259static int bnxt_flash_package_from_file(struct net_device *dev,
1260                                        char *filename)
1261{
1262        netdev_err(dev, "packages are not yet supported\n");
1263        return -EINVAL;
1264}
1265
1266static int bnxt_flash_device(struct net_device *dev,
1267                             struct ethtool_flash *flash)
1268{
1269        if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1270                netdev_err(dev, "flashdev not supported from a virtual function\n");
1271                return -EINVAL;
1272        }
1273
1274        if (flash->region == ETHTOOL_FLASH_ALL_REGIONS)
1275                return bnxt_flash_package_from_file(dev, flash->data);
1276
1277        return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1278}
1279
1280static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1281{
1282        struct bnxt *bp = netdev_priv(dev);
1283        int rc;
1284        struct hwrm_nvm_get_dir_info_input req = {0};
1285        struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1286
1287        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1288
1289        mutex_lock(&bp->hwrm_cmd_lock);
1290        rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1291        if (!rc) {
1292                *entries = le32_to_cpu(output->entries);
1293                *length = le32_to_cpu(output->entry_length);
1294        }
1295        mutex_unlock(&bp->hwrm_cmd_lock);
1296        return rc;
1297}
1298
1299static int bnxt_get_eeprom_len(struct net_device *dev)
1300{
1301        /* The -1 return value allows the entire 32-bit range of offsets to be
1302         * passed via the ethtool command-line utility.
1303         */
1304        return -1;
1305}
1306
1307static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1308{
1309        struct bnxt *bp = netdev_priv(dev);
1310        int rc;
1311        u32 dir_entries;
1312        u32 entry_length;
1313        u8 *buf;
1314        size_t buflen;
1315        dma_addr_t dma_handle;
1316        struct hwrm_nvm_get_dir_entries_input req = {0};
1317
1318        rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1319        if (rc != 0)
1320                return rc;
1321
1322        /* Insert 2 bytes of directory info (count and size of entries) */
1323        if (len < 2)
1324                return -EINVAL;
1325
1326        *data++ = dir_entries;
1327        *data++ = entry_length;
1328        len -= 2;
1329        memset(data, 0xff, len);
1330
1331        buflen = dir_entries * entry_length;
1332        buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1333                                 GFP_KERNEL);
1334        if (!buf) {
1335                netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1336                           (unsigned)buflen);
1337                return -ENOMEM;
1338        }
1339        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1340        req.host_dest_addr = cpu_to_le64(dma_handle);
1341        rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1342        if (rc == 0)
1343                memcpy(data, buf, len > buflen ? buflen : len);
1344        dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1345        return rc;
1346}
1347
1348static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1349                               u32 length, u8 *data)
1350{
1351        struct bnxt *bp = netdev_priv(dev);
1352        int rc;
1353        u8 *buf;
1354        dma_addr_t dma_handle;
1355        struct hwrm_nvm_read_input req = {0};
1356
1357        buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1358                                 GFP_KERNEL);
1359        if (!buf) {
1360                netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1361                           (unsigned)length);
1362                return -ENOMEM;
1363        }
1364        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1365        req.host_dest_addr = cpu_to_le64(dma_handle);
1366        req.dir_idx = cpu_to_le16(index);
1367        req.offset = cpu_to_le32(offset);
1368        req.len = cpu_to_le32(length);
1369
1370        rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1371        if (rc == 0)
1372                memcpy(data, buf, length);
1373        dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1374        return rc;
1375}
1376
1377static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1378                                u16 ext, u16 *index, u32 *item_length,
1379                                u32 *data_length)
1380{
1381        struct bnxt *bp = netdev_priv(dev);
1382        int rc;
1383        struct hwrm_nvm_find_dir_entry_input req = {0};
1384        struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
1385
1386        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
1387        req.enables = 0;
1388        req.dir_idx = 0;
1389        req.dir_type = cpu_to_le16(type);
1390        req.dir_ordinal = cpu_to_le16(ordinal);
1391        req.dir_ext = cpu_to_le16(ext);
1392        req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
1393        rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1394        if (rc == 0) {
1395                if (index)
1396                        *index = le16_to_cpu(output->dir_idx);
1397                if (item_length)
1398                        *item_length = le32_to_cpu(output->dir_item_length);
1399                if (data_length)
1400                        *data_length = le32_to_cpu(output->dir_data_length);
1401        }
1402        return rc;
1403}
1404
1405static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
1406{
1407        char    *retval = NULL;
1408        char    *p;
1409        char    *value;
1410        int     field = 0;
1411
1412        if (datalen < 1)
1413                return NULL;
1414        /* null-terminate the log data (removing last '\n'): */
1415        data[datalen - 1] = 0;
1416        for (p = data; *p != 0; p++) {
1417                field = 0;
1418                retval = NULL;
1419                while (*p != 0 && *p != '\n') {
1420                        value = p;
1421                        while (*p != 0 && *p != '\t' && *p != '\n')
1422                                p++;
1423                        if (field == desired_field)
1424                                retval = value;
1425                        if (*p != '\t')
1426                                break;
1427                        *p = 0;
1428                        field++;
1429                        p++;
1430                }
1431                if (*p == 0)
1432                        break;
1433                *p = 0;
1434        }
1435        return retval;
1436}
1437
1438static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen)
1439{
1440        u16 index = 0;
1441        u32 datalen;
1442
1443        if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
1444                                 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1445                                 &index, NULL, &datalen) != 0)
1446                return NULL;
1447
1448        memset(buf, 0, buflen);
1449        if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0)
1450                return NULL;
1451
1452        return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf,
1453                datalen);
1454}
1455
1456static int bnxt_get_eeprom(struct net_device *dev,
1457                           struct ethtool_eeprom *eeprom,
1458                           u8 *data)
1459{
1460        u32 index;
1461        u32 offset;
1462
1463        if (eeprom->offset == 0) /* special offset value to get directory */
1464                return bnxt_get_nvram_directory(dev, eeprom->len, data);
1465
1466        index = eeprom->offset >> 24;
1467        offset = eeprom->offset & 0xffffff;
1468
1469        if (index == 0) {
1470                netdev_err(dev, "unsupported index value: %d\n", index);
1471                return -EINVAL;
1472        }
1473
1474        return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1475}
1476
1477static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1478{
1479        struct bnxt *bp = netdev_priv(dev);
1480        struct hwrm_nvm_erase_dir_entry_input req = {0};
1481
1482        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1483        req.dir_idx = cpu_to_le16(index);
1484        return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1485}
1486
1487static int bnxt_set_eeprom(struct net_device *dev,
1488                           struct ethtool_eeprom *eeprom,
1489                           u8 *data)
1490{
1491        struct bnxt *bp = netdev_priv(dev);
1492        u8 index, dir_op;
1493        u16 type, ext, ordinal, attr;
1494
1495        if (!BNXT_PF(bp)) {
1496                netdev_err(dev, "NVM write not supported from a virtual function\n");
1497                return -EINVAL;
1498        }
1499
1500        type = eeprom->magic >> 16;
1501
1502        if (type == 0xffff) { /* special value for directory operations */
1503                index = eeprom->magic & 0xff;
1504                dir_op = eeprom->magic >> 8;
1505                if (index == 0)
1506                        return -EINVAL;
1507                switch (dir_op) {
1508                case 0x0e: /* erase */
1509                        if (eeprom->offset != ~eeprom->magic)
1510                                return -EINVAL;
1511                        return bnxt_erase_nvram_directory(dev, index - 1);
1512                default:
1513                        return -EINVAL;
1514                }
1515        }
1516
1517        /* Create or re-write an NVM item: */
1518        if (bnxt_dir_type_is_executable(type) == true)
1519                return -EINVAL;
1520        ext = eeprom->magic & 0xffff;
1521        ordinal = eeprom->offset >> 16;
1522        attr = eeprom->offset & 0xffff;
1523
1524        return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1525                                eeprom->len);
1526}
1527
1528static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
1529{
1530        struct bnxt *bp = netdev_priv(dev);
1531        struct ethtool_eee *eee = &bp->eee;
1532        struct bnxt_link_info *link_info = &bp->link_info;
1533        u32 advertising =
1534                 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
1535        int rc = 0;
1536
1537        if (!BNXT_SINGLE_PF(bp))
1538                return -EOPNOTSUPP;
1539
1540        if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1541                return -EOPNOTSUPP;
1542
1543        if (!edata->eee_enabled)
1544                goto eee_ok;
1545
1546        if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
1547                netdev_warn(dev, "EEE requires autoneg\n");
1548                return -EINVAL;
1549        }
1550        if (edata->tx_lpi_enabled) {
1551                if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
1552                                       edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
1553                        netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
1554                                    bp->lpi_tmr_lo, bp->lpi_tmr_hi);
1555                        return -EINVAL;
1556                } else if (!bp->lpi_tmr_hi) {
1557                        edata->tx_lpi_timer = eee->tx_lpi_timer;
1558                }
1559        }
1560        if (!edata->advertised) {
1561                edata->advertised = advertising & eee->supported;
1562        } else if (edata->advertised & ~advertising) {
1563                netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
1564                            edata->advertised, advertising);
1565                return -EINVAL;
1566        }
1567
1568        eee->advertised = edata->advertised;
1569        eee->tx_lpi_enabled = edata->tx_lpi_enabled;
1570        eee->tx_lpi_timer = edata->tx_lpi_timer;
1571eee_ok:
1572        eee->eee_enabled = edata->eee_enabled;
1573
1574        if (netif_running(dev))
1575                rc = bnxt_hwrm_set_link_setting(bp, false, true);
1576
1577        return rc;
1578}
1579
1580static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
1581{
1582        struct bnxt *bp = netdev_priv(dev);
1583
1584        if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1585                return -EOPNOTSUPP;
1586
1587        *edata = bp->eee;
1588        if (!bp->eee.eee_enabled) {
1589                /* Preserve tx_lpi_timer so that the last value will be used
1590                 * by default when it is re-enabled.
1591                 */
1592                edata->advertised = 0;
1593                edata->tx_lpi_enabled = 0;
1594        }
1595
1596        if (!bp->eee.eee_active)
1597                edata->lp_advertised = 0;
1598
1599        return 0;
1600}
1601
1602static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr,
1603                                            u16 page_number, u16 start_addr,
1604                                            u16 data_length, u8 *buf)
1605{
1606        struct hwrm_port_phy_i2c_read_input req = {0};
1607        struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
1608        int rc, byte_offset = 0;
1609
1610        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
1611        req.i2c_slave_addr = i2c_addr;
1612        req.page_number = cpu_to_le16(page_number);
1613        req.port_id = cpu_to_le16(bp->pf.port_id);
1614        do {
1615                u16 xfer_size;
1616
1617                xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE);
1618                data_length -= xfer_size;
1619                req.page_offset = cpu_to_le16(start_addr + byte_offset);
1620                req.data_length = xfer_size;
1621                req.enables = cpu_to_le32(start_addr + byte_offset ?
1622                                 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0);
1623                mutex_lock(&bp->hwrm_cmd_lock);
1624                rc = _hwrm_send_message(bp, &req, sizeof(req),
1625                                        HWRM_CMD_TIMEOUT);
1626                if (!rc)
1627                        memcpy(buf + byte_offset, output->data, xfer_size);
1628                mutex_unlock(&bp->hwrm_cmd_lock);
1629                byte_offset += xfer_size;
1630        } while (!rc && data_length > 0);
1631
1632        return rc;
1633}
1634
1635static int bnxt_get_module_info(struct net_device *dev,
1636                                struct ethtool_modinfo *modinfo)
1637{
1638        struct bnxt *bp = netdev_priv(dev);
1639        struct hwrm_port_phy_i2c_read_input req = {0};
1640        struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr;
1641        int rc;
1642
1643        /* No point in going further if phy status indicates
1644         * module is not inserted or if it is powered down or
1645         * if it is of type 10GBase-T
1646         */
1647        if (bp->link_info.module_status >
1648                PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG)
1649                return -EOPNOTSUPP;
1650
1651        /* This feature is not supported in older firmware versions */
1652        if (bp->hwrm_spec_code < 0x10202)
1653                return -EOPNOTSUPP;
1654
1655        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1);
1656        req.i2c_slave_addr = I2C_DEV_ADDR_A0;
1657        req.page_number = 0;
1658        req.page_offset = cpu_to_le16(SFP_EEPROM_SFF_8472_COMP_ADDR);
1659        req.data_length = SFP_EEPROM_SFF_8472_COMP_SIZE;
1660        req.port_id = cpu_to_le16(bp->pf.port_id);
1661        mutex_lock(&bp->hwrm_cmd_lock);
1662        rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1663        if (!rc) {
1664                u32 module_id = le32_to_cpu(output->data[0]);
1665
1666                switch (module_id) {
1667                case SFF_MODULE_ID_SFP:
1668                        modinfo->type = ETH_MODULE_SFF_8472;
1669                        modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1670                        break;
1671                case SFF_MODULE_ID_QSFP:
1672                case SFF_MODULE_ID_QSFP_PLUS:
1673                        modinfo->type = ETH_MODULE_SFF_8436;
1674                        modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1675                        break;
1676                case SFF_MODULE_ID_QSFP28:
1677                        modinfo->type = ETH_MODULE_SFF_8636;
1678                        modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1679                        break;
1680                default:
1681                        rc = -EOPNOTSUPP;
1682                        break;
1683                }
1684        }
1685        mutex_unlock(&bp->hwrm_cmd_lock);
1686        return rc;
1687}
1688
1689static int bnxt_get_module_eeprom(struct net_device *dev,
1690                                  struct ethtool_eeprom *eeprom,
1691                                  u8 *data)
1692{
1693        struct bnxt *bp = netdev_priv(dev);
1694        u16  start = eeprom->offset, length = eeprom->len;
1695        int rc = 0;
1696
1697        memset(data, 0, eeprom->len);
1698
1699        /* Read A0 portion of the EEPROM */
1700        if (start < ETH_MODULE_SFF_8436_LEN) {
1701                if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN)
1702                        length = ETH_MODULE_SFF_8436_LEN - start;
1703                rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0,
1704                                                      start, length, data);
1705                if (rc)
1706                        return rc;
1707                start += length;
1708                data += length;
1709                length = eeprom->len - length;
1710        }
1711
1712        /* Read A2 portion of the EEPROM */
1713        if (length) {
1714                start -= ETH_MODULE_SFF_8436_LEN;
1715                bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1, start,
1716                                                 length, data);
1717        }
1718        return rc;
1719}
1720
1721const struct ethtool_ops bnxt_ethtool_ops = {
1722        .get_link_ksettings     = bnxt_get_link_ksettings,
1723        .set_link_ksettings     = bnxt_set_link_ksettings,
1724        .get_pauseparam         = bnxt_get_pauseparam,
1725        .set_pauseparam         = bnxt_set_pauseparam,
1726        .get_drvinfo            = bnxt_get_drvinfo,
1727        .get_coalesce           = bnxt_get_coalesce,
1728        .set_coalesce           = bnxt_set_coalesce,
1729        .get_msglevel           = bnxt_get_msglevel,
1730        .set_msglevel           = bnxt_set_msglevel,
1731        .get_sset_count         = bnxt_get_sset_count,
1732        .get_strings            = bnxt_get_strings,
1733        .get_ethtool_stats      = bnxt_get_ethtool_stats,
1734        .set_ringparam          = bnxt_set_ringparam,
1735        .get_ringparam          = bnxt_get_ringparam,
1736        .get_channels           = bnxt_get_channels,
1737        .set_channels           = bnxt_set_channels,
1738#ifdef CONFIG_RFS_ACCEL
1739        .get_rxnfc              = bnxt_get_rxnfc,
1740#endif
1741        .get_rxfh_indir_size    = bnxt_get_rxfh_indir_size,
1742        .get_rxfh_key_size      = bnxt_get_rxfh_key_size,
1743        .get_rxfh               = bnxt_get_rxfh,
1744        .flash_device           = bnxt_flash_device,
1745        .get_eeprom_len         = bnxt_get_eeprom_len,
1746        .get_eeprom             = bnxt_get_eeprom,
1747        .set_eeprom             = bnxt_set_eeprom,
1748        .get_link               = bnxt_get_link,
1749        .get_eee                = bnxt_get_eee,
1750        .set_eee                = bnxt_set_eee,
1751        .get_module_info        = bnxt_get_module_info,
1752        .get_module_eeprom      = bnxt_get_module_eeprom,
1753};
1754