linux/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
<<
>>
Prefs
   1/*******************************************************************************
   2
   3  Intel 10 Gigabit PCI Express Linux driver
   4  Copyright(c) 1999 - 2012 Intel Corporation.
   5
   6  This program is free software; you can redistribute it and/or modify it
   7  under the terms and conditions of the GNU General Public License,
   8  version 2, as published by the Free Software Foundation.
   9
  10  This program is distributed in the hope it will be useful, but WITHOUT
  11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13  more details.
  14
  15  You should have received a copy of the GNU General Public License along with
  16  this program; if not, write to the Free Software Foundation, Inc.,
  17  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18
  19  The full GNU General Public License is included in this distribution in
  20  the file called "COPYING".
  21
  22  Contact Information:
  23  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25
  26*******************************************************************************/
  27
  28#include <linux/types.h>
  29#include <linux/module.h>
  30#include <linux/pci.h>
  31#include <linux/netdevice.h>
  32#include <linux/vmalloc.h>
  33#include <linux/string.h>
  34#include <linux/in.h>
  35#include <linux/interrupt.h>
  36#include <linux/ip.h>
  37#include <linux/tcp.h>
  38#include <linux/sctp.h>
  39#include <linux/pkt_sched.h>
  40#include <linux/ipv6.h>
  41#include <linux/slab.h>
  42#include <net/checksum.h>
  43#include <net/ip6_checksum.h>
  44#include <linux/ethtool.h>
  45#include <linux/if.h>
  46#include <linux/if_vlan.h>
  47#include <linux/prefetch.h>
  48#include <scsi/fc/fc_fcoe.h>
  49
  50#include "ixgbe.h"
  51#include "ixgbe_common.h"
  52#include "ixgbe_dcb_82599.h"
  53#include "ixgbe_sriov.h"
  54
  55char ixgbe_driver_name[] = "ixgbe";
  56static const char ixgbe_driver_string[] =
  57                              "Intel(R) 10 Gigabit PCI Express Network Driver";
  58char ixgbe_default_device_descr[] =
  59                              "Intel(R) 10 Gigabit Network Connection";
  60#define MAJ 3
  61#define MIN 6
  62#define BUILD 7
  63#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
  64        __stringify(BUILD) "-k"
  65const char ixgbe_driver_version[] = DRV_VERSION;
  66static const char ixgbe_copyright[] =
  67                                "Copyright (c) 1999-2012 Intel Corporation.";
  68
  69static const struct ixgbe_info *ixgbe_info_tbl[] = {
  70        [board_82598] = &ixgbe_82598_info,
  71        [board_82599] = &ixgbe_82599_info,
  72        [board_X540] = &ixgbe_X540_info,
  73};
  74
  75/* ixgbe_pci_tbl - PCI Device ID Table
  76 *
  77 * Wildcard entries (PCI_ANY_ID) should come last
  78 * Last entry must be all 0s
  79 *
  80 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  81 *   Class, Class Mask, private data (not used) }
  82 */
  83static DEFINE_PCI_DEVICE_TABLE(ixgbe_pci_tbl) = {
  84        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598), board_82598 },
  85        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT), board_82598 },
  86        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT), board_82598 },
  87        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT), board_82598 },
  88        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT2), board_82598 },
  89        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4), board_82598 },
  90        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT), board_82598 },
  91        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_DA_DUAL_PORT), board_82598 },
  92        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM), board_82598 },
  93        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_XF_LR), board_82598 },
  94        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_SFP_LOM), board_82598 },
  95        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_BX), board_82598 },
  96        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4), board_82599 },
  97        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_XAUI_LOM), board_82599 },
  98        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KR), board_82599 },
  99        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP), board_82599 },
 100        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_EM), board_82599 },
 101        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4_MEZZ), board_82599 },
 102        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_CX4), board_82599 },
 103        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_BACKPLANE_FCOE), board_82599 },
 104        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_FCOE), board_82599 },
 105        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_T3_LOM), board_82599 },
 106        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_COMBO_BACKPLANE), board_82599 },
 107        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540T), board_X540 },
 108        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_SF2), board_82599 },
 109        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_LS), board_82599 },
 110        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599EN_SFP), board_82599 },
 111        {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_SF_QP), board_82599 },
 112        /* required last entry */
 113        {0, }
 114};
 115MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
 116
 117#ifdef CONFIG_IXGBE_DCA
 118static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
 119                            void *p);
 120static struct notifier_block dca_notifier = {
 121        .notifier_call = ixgbe_notify_dca,
 122        .next          = NULL,
 123        .priority      = 0
 124};
 125#endif
 126
 127#ifdef CONFIG_PCI_IOV
 128static unsigned int max_vfs;
 129module_param(max_vfs, uint, 0);
 130MODULE_PARM_DESC(max_vfs,
 131                 "Maximum number of virtual functions to allocate per physical function");
 132#endif /* CONFIG_PCI_IOV */
 133
 134MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
 135MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
 136MODULE_LICENSE("GPL");
 137MODULE_VERSION(DRV_VERSION);
 138
 139#define DEFAULT_DEBUG_LEVEL_SHIFT 3
 140
 141static void ixgbe_service_event_schedule(struct ixgbe_adapter *adapter)
 142{
 143        if (!test_bit(__IXGBE_DOWN, &adapter->state) &&
 144            !test_and_set_bit(__IXGBE_SERVICE_SCHED, &adapter->state))
 145                schedule_work(&adapter->service_task);
 146}
 147
 148static void ixgbe_service_event_complete(struct ixgbe_adapter *adapter)
 149{
 150        BUG_ON(!test_bit(__IXGBE_SERVICE_SCHED, &adapter->state));
 151
 152        /* flush memory to make sure state is correct before next watchdog */
 153        smp_mb__before_clear_bit();
 154        clear_bit(__IXGBE_SERVICE_SCHED, &adapter->state);
 155}
 156
 157struct ixgbe_reg_info {
 158        u32 ofs;
 159        char *name;
 160};
 161
 162static const struct ixgbe_reg_info ixgbe_reg_info_tbl[] = {
 163
 164        /* General Registers */
 165        {IXGBE_CTRL, "CTRL"},
 166        {IXGBE_STATUS, "STATUS"},
 167        {IXGBE_CTRL_EXT, "CTRL_EXT"},
 168
 169        /* Interrupt Registers */
 170        {IXGBE_EICR, "EICR"},
 171
 172        /* RX Registers */
 173        {IXGBE_SRRCTL(0), "SRRCTL"},
 174        {IXGBE_DCA_RXCTRL(0), "DRXCTL"},
 175        {IXGBE_RDLEN(0), "RDLEN"},
 176        {IXGBE_RDH(0), "RDH"},
 177        {IXGBE_RDT(0), "RDT"},
 178        {IXGBE_RXDCTL(0), "RXDCTL"},
 179        {IXGBE_RDBAL(0), "RDBAL"},
 180        {IXGBE_RDBAH(0), "RDBAH"},
 181
 182        /* TX Registers */
 183        {IXGBE_TDBAL(0), "TDBAL"},
 184        {IXGBE_TDBAH(0), "TDBAH"},
 185        {IXGBE_TDLEN(0), "TDLEN"},
 186        {IXGBE_TDH(0), "TDH"},
 187        {IXGBE_TDT(0), "TDT"},
 188        {IXGBE_TXDCTL(0), "TXDCTL"},
 189
 190        /* List Terminator */
 191        {}
 192};
 193
 194
 195/*
 196 * ixgbe_regdump - register printout routine
 197 */
 198static void ixgbe_regdump(struct ixgbe_hw *hw, struct ixgbe_reg_info *reginfo)
 199{
 200        int i = 0, j = 0;
 201        char rname[16];
 202        u32 regs[64];
 203
 204        switch (reginfo->ofs) {
 205        case IXGBE_SRRCTL(0):
 206                for (i = 0; i < 64; i++)
 207                        regs[i] = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
 208                break;
 209        case IXGBE_DCA_RXCTRL(0):
 210                for (i = 0; i < 64; i++)
 211                        regs[i] = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
 212                break;
 213        case IXGBE_RDLEN(0):
 214                for (i = 0; i < 64; i++)
 215                        regs[i] = IXGBE_READ_REG(hw, IXGBE_RDLEN(i));
 216                break;
 217        case IXGBE_RDH(0):
 218                for (i = 0; i < 64; i++)
 219                        regs[i] = IXGBE_READ_REG(hw, IXGBE_RDH(i));
 220                break;
 221        case IXGBE_RDT(0):
 222                for (i = 0; i < 64; i++)
 223                        regs[i] = IXGBE_READ_REG(hw, IXGBE_RDT(i));
 224                break;
 225        case IXGBE_RXDCTL(0):
 226                for (i = 0; i < 64; i++)
 227                        regs[i] = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
 228                break;
 229        case IXGBE_RDBAL(0):
 230                for (i = 0; i < 64; i++)
 231                        regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAL(i));
 232                break;
 233        case IXGBE_RDBAH(0):
 234                for (i = 0; i < 64; i++)
 235                        regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAH(i));
 236                break;
 237        case IXGBE_TDBAL(0):
 238                for (i = 0; i < 64; i++)
 239                        regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAL(i));
 240                break;
 241        case IXGBE_TDBAH(0):
 242                for (i = 0; i < 64; i++)
 243                        regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAH(i));
 244                break;
 245        case IXGBE_TDLEN(0):
 246                for (i = 0; i < 64; i++)
 247                        regs[i] = IXGBE_READ_REG(hw, IXGBE_TDLEN(i));
 248                break;
 249        case IXGBE_TDH(0):
 250                for (i = 0; i < 64; i++)
 251                        regs[i] = IXGBE_READ_REG(hw, IXGBE_TDH(i));
 252                break;
 253        case IXGBE_TDT(0):
 254                for (i = 0; i < 64; i++)
 255                        regs[i] = IXGBE_READ_REG(hw, IXGBE_TDT(i));
 256                break;
 257        case IXGBE_TXDCTL(0):
 258                for (i = 0; i < 64; i++)
 259                        regs[i] = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
 260                break;
 261        default:
 262                pr_info("%-15s %08x\n", reginfo->name,
 263                        IXGBE_READ_REG(hw, reginfo->ofs));
 264                return;
 265        }
 266
 267        for (i = 0; i < 8; i++) {
 268                snprintf(rname, 16, "%s[%d-%d]", reginfo->name, i*8, i*8+7);
 269                pr_err("%-15s", rname);
 270                for (j = 0; j < 8; j++)
 271                        pr_cont(" %08x", regs[i*8+j]);
 272                pr_cont("\n");
 273        }
 274
 275}
 276
 277/*
 278 * ixgbe_dump - Print registers, tx-rings and rx-rings
 279 */
 280static void ixgbe_dump(struct ixgbe_adapter *adapter)
 281{
 282        struct net_device *netdev = adapter->netdev;
 283        struct ixgbe_hw *hw = &adapter->hw;
 284        struct ixgbe_reg_info *reginfo;
 285        int n = 0;
 286        struct ixgbe_ring *tx_ring;
 287        struct ixgbe_tx_buffer *tx_buffer_info;
 288        union ixgbe_adv_tx_desc *tx_desc;
 289        struct my_u0 { u64 a; u64 b; } *u0;
 290        struct ixgbe_ring *rx_ring;
 291        union ixgbe_adv_rx_desc *rx_desc;
 292        struct ixgbe_rx_buffer *rx_buffer_info;
 293        u32 staterr;
 294        int i = 0;
 295
 296        if (!netif_msg_hw(adapter))
 297                return;
 298
 299        /* Print netdevice Info */
 300        if (netdev) {
 301                dev_info(&adapter->pdev->dev, "Net device Info\n");
 302                pr_info("Device Name     state            "
 303                        "trans_start      last_rx\n");
 304                pr_info("%-15s %016lX %016lX %016lX\n",
 305                        netdev->name,
 306                        netdev->state,
 307                        netdev->trans_start,
 308                        netdev->last_rx);
 309        }
 310
 311        /* Print Registers */
 312        dev_info(&adapter->pdev->dev, "Register Dump\n");
 313        pr_info(" Register Name   Value\n");
 314        for (reginfo = (struct ixgbe_reg_info *)ixgbe_reg_info_tbl;
 315             reginfo->name; reginfo++) {
 316                ixgbe_regdump(hw, reginfo);
 317        }
 318
 319        /* Print TX Ring Summary */
 320        if (!netdev || !netif_running(netdev))
 321                goto exit;
 322
 323        dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
 324        pr_info("Queue [NTU] [NTC] [bi(ntc)->dma  ] leng ntw timestamp\n");
 325        for (n = 0; n < adapter->num_tx_queues; n++) {
 326                tx_ring = adapter->tx_ring[n];
 327                tx_buffer_info =
 328                        &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
 329                pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
 330                           n, tx_ring->next_to_use, tx_ring->next_to_clean,
 331                           (u64)tx_buffer_info->dma,
 332                           tx_buffer_info->length,
 333                           tx_buffer_info->next_to_watch,
 334                           (u64)tx_buffer_info->time_stamp);
 335        }
 336
 337        /* Print TX Rings */
 338        if (!netif_msg_tx_done(adapter))
 339                goto rx_ring_summary;
 340
 341        dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
 342
 343        /* Transmit Descriptor Formats
 344         *
 345         * Advanced Transmit Descriptor
 346         *   +--------------------------------------------------------------+
 347         * 0 |         Buffer Address [63:0]                                |
 348         *   +--------------------------------------------------------------+
 349         * 8 |  PAYLEN  | PORTS  | IDX | STA | DCMD  |DTYP |  RSV |  DTALEN |
 350         *   +--------------------------------------------------------------+
 351         *   63       46 45    40 39 36 35 32 31   24 23 20 19              0
 352         */
 353
 354        for (n = 0; n < adapter->num_tx_queues; n++) {
 355                tx_ring = adapter->tx_ring[n];
 356                pr_info("------------------------------------\n");
 357                pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
 358                pr_info("------------------------------------\n");
 359                pr_info("T [desc]     [address 63:0  ] "
 360                        "[PlPOIdStDDt Ln] [bi->dma       ] "
 361                        "leng  ntw timestamp        bi->skb\n");
 362
 363                for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
 364                        tx_desc = IXGBE_TX_DESC_ADV(tx_ring, i);
 365                        tx_buffer_info = &tx_ring->tx_buffer_info[i];
 366                        u0 = (struct my_u0 *)tx_desc;
 367                        pr_info("T [0x%03X]    %016llX %016llX %016llX"
 368                                " %04X  %p %016llX %p", i,
 369                                le64_to_cpu(u0->a),
 370                                le64_to_cpu(u0->b),
 371                                (u64)tx_buffer_info->dma,
 372                                tx_buffer_info->length,
 373                                tx_buffer_info->next_to_watch,
 374                                (u64)tx_buffer_info->time_stamp,
 375                                tx_buffer_info->skb);
 376                        if (i == tx_ring->next_to_use &&
 377                                i == tx_ring->next_to_clean)
 378                                pr_cont(" NTC/U\n");
 379                        else if (i == tx_ring->next_to_use)
 380                                pr_cont(" NTU\n");
 381                        else if (i == tx_ring->next_to_clean)
 382                                pr_cont(" NTC\n");
 383                        else
 384                                pr_cont("\n");
 385
 386                        if (netif_msg_pktdata(adapter) &&
 387                                tx_buffer_info->dma != 0)
 388                                print_hex_dump(KERN_INFO, "",
 389                                        DUMP_PREFIX_ADDRESS, 16, 1,
 390                                        phys_to_virt(tx_buffer_info->dma),
 391                                        tx_buffer_info->length, true);
 392                }
 393        }
 394
 395        /* Print RX Rings Summary */
 396rx_ring_summary:
 397        dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
 398        pr_info("Queue [NTU] [NTC]\n");
 399        for (n = 0; n < adapter->num_rx_queues; n++) {
 400                rx_ring = adapter->rx_ring[n];
 401                pr_info("%5d %5X %5X\n",
 402                        n, rx_ring->next_to_use, rx_ring->next_to_clean);
 403        }
 404
 405        /* Print RX Rings */
 406        if (!netif_msg_rx_status(adapter))
 407                goto exit;
 408
 409        dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
 410
 411        /* Advanced Receive Descriptor (Read) Format
 412         *    63                                           1        0
 413         *    +-----------------------------------------------------+
 414         *  0 |       Packet Buffer Address [63:1]           |A0/NSE|
 415         *    +----------------------------------------------+------+
 416         *  8 |       Header Buffer Address [63:1]           |  DD  |
 417         *    +-----------------------------------------------------+
 418         *
 419         *
 420         * Advanced Receive Descriptor (Write-Back) Format
 421         *
 422         *   63       48 47    32 31  30      21 20 16 15   4 3     0
 423         *   +------------------------------------------------------+
 424         * 0 | Packet     IP     |SPH| HDR_LEN   | RSV|Packet|  RSS |
 425         *   | Checksum   Ident  |   |           |    | Type | Type |
 426         *   +------------------------------------------------------+
 427         * 8 | VLAN Tag | Length | Extended Error | Extended Status |
 428         *   +------------------------------------------------------+
 429         *   63       48 47    32 31            20 19               0
 430         */
 431        for (n = 0; n < adapter->num_rx_queues; n++) {
 432                rx_ring = adapter->rx_ring[n];
 433                pr_info("------------------------------------\n");
 434                pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
 435                pr_info("------------------------------------\n");
 436                pr_info("R  [desc]      [ PktBuf     A0] "
 437                        "[  HeadBuf   DD] [bi->dma       ] [bi->skb] "
 438                        "<-- Adv Rx Read format\n");
 439                pr_info("RWB[desc]      [PcsmIpSHl PtRs] "
 440                        "[vl er S cks ln] ---------------- [bi->skb] "
 441                        "<-- Adv Rx Write-Back format\n");
 442
 443                for (i = 0; i < rx_ring->count; i++) {
 444                        rx_buffer_info = &rx_ring->rx_buffer_info[i];
 445                        rx_desc = IXGBE_RX_DESC_ADV(rx_ring, i);
 446                        u0 = (struct my_u0 *)rx_desc;
 447                        staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
 448                        if (staterr & IXGBE_RXD_STAT_DD) {
 449                                /* Descriptor Done */
 450                                pr_info("RWB[0x%03X]     %016llX "
 451                                        "%016llX ---------------- %p", i,
 452                                        le64_to_cpu(u0->a),
 453                                        le64_to_cpu(u0->b),
 454                                        rx_buffer_info->skb);
 455                        } else {
 456                                pr_info("R  [0x%03X]     %016llX "
 457                                        "%016llX %016llX %p", i,
 458                                        le64_to_cpu(u0->a),
 459                                        le64_to_cpu(u0->b),
 460                                        (u64)rx_buffer_info->dma,
 461                                        rx_buffer_info->skb);
 462
 463                                if (netif_msg_pktdata(adapter)) {
 464                                        print_hex_dump(KERN_INFO, "",
 465                                           DUMP_PREFIX_ADDRESS, 16, 1,
 466                                           phys_to_virt(rx_buffer_info->dma),
 467                                           rx_ring->rx_buf_len, true);
 468
 469                                        if (rx_ring->rx_buf_len
 470                                                < IXGBE_RXBUFFER_2K)
 471                                                print_hex_dump(KERN_INFO, "",
 472                                                  DUMP_PREFIX_ADDRESS, 16, 1,
 473                                                  phys_to_virt(
 474                                                    rx_buffer_info->page_dma +
 475                                                    rx_buffer_info->page_offset
 476                                                  ),
 477                                                  PAGE_SIZE/2, true);
 478                                }
 479                        }
 480
 481                        if (i == rx_ring->next_to_use)
 482                                pr_cont(" NTU\n");
 483                        else if (i == rx_ring->next_to_clean)
 484                                pr_cont(" NTC\n");
 485                        else
 486                                pr_cont("\n");
 487
 488                }
 489        }
 490
 491exit:
 492        return;
 493}
 494
 495static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
 496{
 497        u32 ctrl_ext;
 498
 499        /* Let firmware take over control of h/w */
 500        ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
 501        IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
 502                        ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
 503}
 504
 505static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
 506{
 507        u32 ctrl_ext;
 508
 509        /* Let firmware know the driver has taken over */
 510        ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
 511        IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
 512                        ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
 513}
 514
 515/*
 516 * ixgbe_set_ivar - set the IVAR registers, mapping interrupt causes to vectors
 517 * @adapter: pointer to adapter struct
 518 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
 519 * @queue: queue to map the corresponding interrupt to
 520 * @msix_vector: the vector to map to the corresponding queue
 521 *
 522 */
 523static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, s8 direction,
 524                           u8 queue, u8 msix_vector)
 525{
 526        u32 ivar, index;
 527        struct ixgbe_hw *hw = &adapter->hw;
 528        switch (hw->mac.type) {
 529        case ixgbe_mac_82598EB:
 530                msix_vector |= IXGBE_IVAR_ALLOC_VAL;
 531                if (direction == -1)
 532                        direction = 0;
 533                index = (((direction * 64) + queue) >> 2) & 0x1F;
 534                ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
 535                ivar &= ~(0xFF << (8 * (queue & 0x3)));
 536                ivar |= (msix_vector << (8 * (queue & 0x3)));
 537                IXGBE_WRITE_REG(hw, IXGBE_IVAR(index), ivar);
 538                break;
 539        case ixgbe_mac_82599EB:
 540        case ixgbe_mac_X540:
 541                if (direction == -1) {
 542                        /* other causes */
 543                        msix_vector |= IXGBE_IVAR_ALLOC_VAL;
 544                        index = ((queue & 1) * 8);
 545                        ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR_MISC);
 546                        ivar &= ~(0xFF << index);
 547                        ivar |= (msix_vector << index);
 548                        IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR_MISC, ivar);
 549                        break;
 550                } else {
 551                        /* tx or rx causes */
 552                        msix_vector |= IXGBE_IVAR_ALLOC_VAL;
 553                        index = ((16 * (queue & 1)) + (8 * direction));
 554                        ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(queue >> 1));
 555                        ivar &= ~(0xFF << index);
 556                        ivar |= (msix_vector << index);
 557                        IXGBE_WRITE_REG(hw, IXGBE_IVAR(queue >> 1), ivar);
 558                        break;
 559                }
 560        default:
 561                break;
 562        }
 563}
 564
 565static inline void ixgbe_irq_rearm_queues(struct ixgbe_adapter *adapter,
 566                                          u64 qmask)
 567{
 568        u32 mask;
 569
 570        switch (adapter->hw.mac.type) {
 571        case ixgbe_mac_82598EB:
 572                mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
 573                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
 574                break;
 575        case ixgbe_mac_82599EB:
 576        case ixgbe_mac_X540:
 577                mask = (qmask & 0xFFFFFFFF);
 578                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
 579                mask = (qmask >> 32);
 580                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
 581                break;
 582        default:
 583                break;
 584        }
 585}
 586
 587static inline void ixgbe_unmap_tx_resource(struct ixgbe_ring *ring,
 588                                           struct ixgbe_tx_buffer *tx_buffer)
 589{
 590        if (tx_buffer->dma) {
 591                if (tx_buffer->tx_flags & IXGBE_TX_FLAGS_MAPPED_AS_PAGE)
 592                        dma_unmap_page(ring->dev,
 593                                       tx_buffer->dma,
 594                                       tx_buffer->length,
 595                                       DMA_TO_DEVICE);
 596                else
 597                        dma_unmap_single(ring->dev,
 598                                         tx_buffer->dma,
 599                                         tx_buffer->length,
 600                                         DMA_TO_DEVICE);
 601        }
 602        tx_buffer->dma = 0;
 603}
 604
 605void ixgbe_unmap_and_free_tx_resource(struct ixgbe_ring *tx_ring,
 606                                      struct ixgbe_tx_buffer *tx_buffer_info)
 607{
 608        ixgbe_unmap_tx_resource(tx_ring, tx_buffer_info);
 609        if (tx_buffer_info->skb)
 610                dev_kfree_skb_any(tx_buffer_info->skb);
 611        tx_buffer_info->skb = NULL;
 612        /* tx_buffer_info must be completely set up in the transmit path */
 613}
 614
 615static void ixgbe_update_xoff_received(struct ixgbe_adapter *adapter)
 616{
 617        struct ixgbe_hw *hw = &adapter->hw;
 618        struct ixgbe_hw_stats *hwstats = &adapter->stats;
 619        u32 data = 0;
 620        u32 xoff[8] = {0};
 621        int i;
 622
 623        if ((hw->fc.current_mode == ixgbe_fc_full) ||
 624            (hw->fc.current_mode == ixgbe_fc_rx_pause)) {
 625                switch (hw->mac.type) {
 626                case ixgbe_mac_82598EB:
 627                        data = IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
 628                        break;
 629                default:
 630                        data = IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
 631                }
 632                hwstats->lxoffrxc += data;
 633
 634                /* refill credits (no tx hang) if we received xoff */
 635                if (!data)
 636                        return;
 637
 638                for (i = 0; i < adapter->num_tx_queues; i++)
 639                        clear_bit(__IXGBE_HANG_CHECK_ARMED,
 640                                  &adapter->tx_ring[i]->state);
 641                return;
 642        } else if (!(adapter->dcb_cfg.pfc_mode_enable))
 643                return;
 644
 645        /* update stats for each tc, only valid with PFC enabled */
 646        for (i = 0; i < MAX_TX_PACKET_BUFFERS; i++) {
 647                switch (hw->mac.type) {
 648                case ixgbe_mac_82598EB:
 649                        xoff[i] = IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
 650                        break;
 651                default:
 652                        xoff[i] = IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
 653                }
 654                hwstats->pxoffrxc[i] += xoff[i];
 655        }
 656
 657        /* disarm tx queues that have received xoff frames */
 658        for (i = 0; i < adapter->num_tx_queues; i++) {
 659                struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
 660                u8 tc = tx_ring->dcb_tc;
 661
 662                if (xoff[tc])
 663                        clear_bit(__IXGBE_HANG_CHECK_ARMED, &tx_ring->state);
 664        }
 665}
 666
 667static u64 ixgbe_get_tx_completed(struct ixgbe_ring *ring)
 668{
 669        return ring->tx_stats.completed;
 670}
 671
 672static u64 ixgbe_get_tx_pending(struct ixgbe_ring *ring)
 673{
 674        struct ixgbe_adapter *adapter = netdev_priv(ring->netdev);
 675        struct ixgbe_hw *hw = &adapter->hw;
 676
 677        u32 head = IXGBE_READ_REG(hw, IXGBE_TDH(ring->reg_idx));
 678        u32 tail = IXGBE_READ_REG(hw, IXGBE_TDT(ring->reg_idx));
 679
 680        if (head != tail)
 681                return (head < tail) ?
 682                        tail - head : (tail + ring->count - head);
 683
 684        return 0;
 685}
 686
 687static inline bool ixgbe_check_tx_hang(struct ixgbe_ring *tx_ring)
 688{
 689        u32 tx_done = ixgbe_get_tx_completed(tx_ring);
 690        u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
 691        u32 tx_pending = ixgbe_get_tx_pending(tx_ring);
 692        bool ret = false;
 693
 694        clear_check_for_tx_hang(tx_ring);
 695
 696        /*
 697         * Check for a hung queue, but be thorough. This verifies
 698         * that a transmit has been completed since the previous
 699         * check AND there is at least one packet pending. The
 700         * ARMED bit is set to indicate a potential hang. The
 701         * bit is cleared if a pause frame is received to remove
 702         * false hang detection due to PFC or 802.3x frames. By
 703         * requiring this to fail twice we avoid races with
 704         * pfc clearing the ARMED bit and conditions where we
 705         * run the check_tx_hang logic with a transmit completion
 706         * pending but without time to complete it yet.
 707         */
 708        if ((tx_done_old == tx_done) && tx_pending) {
 709                /* make sure it is true for two checks in a row */
 710                ret = test_and_set_bit(__IXGBE_HANG_CHECK_ARMED,
 711                                       &tx_ring->state);
 712        } else {
 713                /* update completed stats and continue */
 714                tx_ring->tx_stats.tx_done_old = tx_done;
 715                /* reset the countdown */
 716                clear_bit(__IXGBE_HANG_CHECK_ARMED, &tx_ring->state);
 717        }
 718
 719        return ret;
 720}
 721
 722/**
 723 * ixgbe_tx_timeout_reset - initiate reset due to Tx timeout
 724 * @adapter: driver private struct
 725 **/
 726static void ixgbe_tx_timeout_reset(struct ixgbe_adapter *adapter)
 727{
 728
 729        /* Do the reset outside of interrupt context */
 730        if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
 731                adapter->flags2 |= IXGBE_FLAG2_RESET_REQUESTED;
 732                ixgbe_service_event_schedule(adapter);
 733        }
 734}
 735
 736/**
 737 * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
 738 * @q_vector: structure containing interrupt and ring information
 739 * @tx_ring: tx ring to clean
 740 **/
 741static bool ixgbe_clean_tx_irq(struct ixgbe_q_vector *q_vector,
 742                               struct ixgbe_ring *tx_ring)
 743{
 744        struct ixgbe_adapter *adapter = q_vector->adapter;
 745        struct ixgbe_tx_buffer *tx_buffer;
 746        union ixgbe_adv_tx_desc *tx_desc;
 747        unsigned int total_bytes = 0, total_packets = 0;
 748        unsigned int budget = q_vector->tx.work_limit;
 749        u16 i = tx_ring->next_to_clean;
 750
 751        tx_buffer = &tx_ring->tx_buffer_info[i];
 752        tx_desc = IXGBE_TX_DESC_ADV(tx_ring, i);
 753
 754        for (; budget; budget--) {
 755                union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
 756
 757                /* if next_to_watch is not set then there is no work pending */
 758                if (!eop_desc)
 759                        break;
 760
 761                /* if DD is not set pending work has not been completed */
 762                if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
 763                        break;
 764
 765                /* count the packet as being completed */
 766                tx_ring->tx_stats.completed++;
 767
 768                /* clear next_to_watch to prevent false hangs */
 769                tx_buffer->next_to_watch = NULL;
 770
 771                /* prevent any other reads prior to eop_desc being verified */
 772                rmb();
 773
 774                do {
 775                        ixgbe_unmap_tx_resource(tx_ring, tx_buffer);
 776                        tx_desc->wb.status = 0;
 777                        if (likely(tx_desc == eop_desc)) {
 778                                eop_desc = NULL;
 779                                dev_kfree_skb_any(tx_buffer->skb);
 780                                tx_buffer->skb = NULL;
 781
 782                                total_bytes += tx_buffer->bytecount;
 783                                total_packets += tx_buffer->gso_segs;
 784                        }
 785
 786                        tx_buffer++;
 787                        tx_desc++;
 788                        i++;
 789                        if (unlikely(i == tx_ring->count)) {
 790                                i = 0;
 791
 792                                tx_buffer = tx_ring->tx_buffer_info;
 793                                tx_desc = IXGBE_TX_DESC_ADV(tx_ring, 0);
 794                        }
 795
 796                } while (eop_desc);
 797        }
 798
 799        tx_ring->next_to_clean = i;
 800        u64_stats_update_begin(&tx_ring->syncp);
 801        tx_ring->stats.bytes += total_bytes;
 802        tx_ring->stats.packets += total_packets;
 803        u64_stats_update_end(&tx_ring->syncp);
 804        q_vector->tx.total_bytes += total_bytes;
 805        q_vector->tx.total_packets += total_packets;
 806
 807        if (check_for_tx_hang(tx_ring) && ixgbe_check_tx_hang(tx_ring)) {
 808                /* schedule immediate reset if we believe we hung */
 809                struct ixgbe_hw *hw = &adapter->hw;
 810                tx_desc = IXGBE_TX_DESC_ADV(tx_ring, i);
 811                e_err(drv, "Detected Tx Unit Hang\n"
 812                        "  Tx Queue             <%d>\n"
 813                        "  TDH, TDT             <%x>, <%x>\n"
 814                        "  next_to_use          <%x>\n"
 815                        "  next_to_clean        <%x>\n"
 816                        "tx_buffer_info[next_to_clean]\n"
 817                        "  time_stamp           <%lx>\n"
 818                        "  jiffies              <%lx>\n",
 819                        tx_ring->queue_index,
 820                        IXGBE_READ_REG(hw, IXGBE_TDH(tx_ring->reg_idx)),
 821                        IXGBE_READ_REG(hw, IXGBE_TDT(tx_ring->reg_idx)),
 822                        tx_ring->next_to_use, i,
 823                        tx_ring->tx_buffer_info[i].time_stamp, jiffies);
 824
 825                netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
 826
 827                e_info(probe,
 828                       "tx hang %d detected on queue %d, resetting adapter\n",
 829                        adapter->tx_timeout_count + 1, tx_ring->queue_index);
 830
 831                /* schedule immediate reset if we believe we hung */
 832                ixgbe_tx_timeout_reset(adapter);
 833
 834                /* the adapter is about to reset, no point in enabling stuff */
 835                return true;
 836        }
 837
 838#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
 839        if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
 840                     (ixgbe_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
 841                /* Make sure that anybody stopping the queue after this
 842                 * sees the new next_to_clean.
 843                 */
 844                smp_mb();
 845                if (__netif_subqueue_stopped(tx_ring->netdev, tx_ring->queue_index) &&
 846                    !test_bit(__IXGBE_DOWN, &adapter->state)) {
 847                        netif_wake_subqueue(tx_ring->netdev, tx_ring->queue_index);
 848                        ++tx_ring->tx_stats.restart_queue;
 849                }
 850        }
 851
 852        return !!budget;
 853}
 854
 855#ifdef CONFIG_IXGBE_DCA
 856static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
 857                                struct ixgbe_ring *rx_ring,
 858                                int cpu)
 859{
 860        struct ixgbe_hw *hw = &adapter->hw;
 861        u32 rxctrl;
 862        u8 reg_idx = rx_ring->reg_idx;
 863
 864        rxctrl = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(reg_idx));
 865        switch (hw->mac.type) {
 866        case ixgbe_mac_82598EB:
 867                rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK;
 868                rxctrl |= dca3_get_tag(rx_ring->dev, cpu);
 869                break;
 870        case ixgbe_mac_82599EB:
 871        case ixgbe_mac_X540:
 872                rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK_82599;
 873                rxctrl |= (dca3_get_tag(rx_ring->dev, cpu) <<
 874                           IXGBE_DCA_RXCTRL_CPUID_SHIFT_82599);
 875                break;
 876        default:
 877                break;
 878        }
 879        rxctrl |= IXGBE_DCA_RXCTRL_DESC_DCA_EN;
 880        rxctrl |= IXGBE_DCA_RXCTRL_HEAD_DCA_EN;
 881        rxctrl &= ~(IXGBE_DCA_RXCTRL_DESC_RRO_EN);
 882        IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(reg_idx), rxctrl);
 883}
 884
 885static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
 886                                struct ixgbe_ring *tx_ring,
 887                                int cpu)
 888{
 889        struct ixgbe_hw *hw = &adapter->hw;
 890        u32 txctrl;
 891        u8 reg_idx = tx_ring->reg_idx;
 892
 893        switch (hw->mac.type) {
 894        case ixgbe_mac_82598EB:
 895                txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(reg_idx));
 896                txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK;
 897                txctrl |= dca3_get_tag(tx_ring->dev, cpu);
 898                txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
 899                IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(reg_idx), txctrl);
 900                break;
 901        case ixgbe_mac_82599EB:
 902        case ixgbe_mac_X540:
 903                txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(reg_idx));
 904                txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK_82599;
 905                txctrl |= (dca3_get_tag(tx_ring->dev, cpu) <<
 906                           IXGBE_DCA_TXCTRL_CPUID_SHIFT_82599);
 907                txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
 908                IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(reg_idx), txctrl);
 909                break;
 910        default:
 911                break;
 912        }
 913}
 914
 915static void ixgbe_update_dca(struct ixgbe_q_vector *q_vector)
 916{
 917        struct ixgbe_adapter *adapter = q_vector->adapter;
 918        struct ixgbe_ring *ring;
 919        int cpu = get_cpu();
 920
 921        if (q_vector->cpu == cpu)
 922                goto out_no_update;
 923
 924        for (ring = q_vector->tx.ring; ring != NULL; ring = ring->next)
 925                ixgbe_update_tx_dca(adapter, ring, cpu);
 926
 927        for (ring = q_vector->rx.ring; ring != NULL; ring = ring->next)
 928                ixgbe_update_rx_dca(adapter, ring, cpu);
 929
 930        q_vector->cpu = cpu;
 931out_no_update:
 932        put_cpu();
 933}
 934
 935static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
 936{
 937        int num_q_vectors;
 938        int i;
 939
 940        if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
 941                return;
 942
 943        /* always use CB2 mode, difference is masked in the CB driver */
 944        IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
 945
 946        if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
 947                num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
 948        else
 949                num_q_vectors = 1;
 950
 951        for (i = 0; i < num_q_vectors; i++) {
 952                adapter->q_vector[i]->cpu = -1;
 953                ixgbe_update_dca(adapter->q_vector[i]);
 954        }
 955}
 956
 957static int __ixgbe_notify_dca(struct device *dev, void *data)
 958{
 959        struct ixgbe_adapter *adapter = dev_get_drvdata(dev);
 960        unsigned long event = *(unsigned long *)data;
 961
 962        if (!(adapter->flags & IXGBE_FLAG_DCA_CAPABLE))
 963                return 0;
 964
 965        switch (event) {
 966        case DCA_PROVIDER_ADD:
 967                /* if we're already enabled, don't do it again */
 968                if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
 969                        break;
 970                if (dca_add_requester(dev) == 0) {
 971                        adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
 972                        ixgbe_setup_dca(adapter);
 973                        break;
 974                }
 975                /* Fall Through since DCA is disabled. */
 976        case DCA_PROVIDER_REMOVE:
 977                if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
 978                        dca_remove_requester(dev);
 979                        adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
 980                        IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
 981                }
 982                break;
 983        }
 984
 985        return 0;
 986}
 987#endif /* CONFIG_IXGBE_DCA */
 988
 989static inline void ixgbe_rx_hash(union ixgbe_adv_rx_desc *rx_desc,
 990                                 struct sk_buff *skb)
 991{
 992        skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
 993}
 994
 995/**
 996 * ixgbe_rx_is_fcoe - check the rx desc for incoming pkt type
 997 * @adapter: address of board private structure
 998 * @rx_desc: advanced rx descriptor
 999 *
1000 * Returns : true if it is FCoE pkt
1001 */
1002static inline bool ixgbe_rx_is_fcoe(struct ixgbe_adapter *adapter,
1003                                    union ixgbe_adv_rx_desc *rx_desc)
1004{
1005        __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1006
1007        return (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) &&
1008               ((pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_ETQF_MASK)) ==
1009                (cpu_to_le16(IXGBE_ETQF_FILTER_FCOE <<
1010                             IXGBE_RXDADV_PKTTYPE_ETQF_SHIFT)));
1011}
1012
1013/**
1014 * ixgbe_receive_skb - Send a completed packet up the stack
1015 * @adapter: board private structure
1016 * @skb: packet to send up
1017 * @status: hardware indication of status of receive
1018 * @rx_ring: rx descriptor ring (for a specific queue) to setup
1019 * @rx_desc: rx descriptor
1020 **/
1021static void ixgbe_receive_skb(struct ixgbe_q_vector *q_vector,
1022                              struct sk_buff *skb, u8 status,
1023                              struct ixgbe_ring *ring,
1024                              union ixgbe_adv_rx_desc *rx_desc)
1025{
1026        struct ixgbe_adapter *adapter = q_vector->adapter;
1027        struct napi_struct *napi = &q_vector->napi;
1028        bool is_vlan = (status & IXGBE_RXD_STAT_VP);
1029        u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
1030
1031        if (is_vlan && (tag & VLAN_VID_MASK))
1032                __vlan_hwaccel_put_tag(skb, tag);
1033
1034        if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
1035                napi_gro_receive(napi, skb);
1036        else
1037                netif_rx(skb);
1038}
1039
1040/**
1041 * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
1042 * @adapter: address of board private structure
1043 * @status_err: hardware indication of status of receive
1044 * @skb: skb currently being received and modified
1045 * @status_err: status error value of last descriptor in packet
1046 **/
1047static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
1048                                     union ixgbe_adv_rx_desc *rx_desc,
1049                                     struct sk_buff *skb,
1050                                     u32 status_err)
1051{
1052        skb->ip_summed = CHECKSUM_NONE;
1053
1054        /* Rx csum disabled */
1055        if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
1056                return;
1057
1058        /* if IP and error */
1059        if ((status_err & IXGBE_RXD_STAT_IPCS) &&
1060            (status_err & IXGBE_RXDADV_ERR_IPE)) {
1061                adapter->hw_csum_rx_error++;
1062                return;
1063        }
1064
1065        if (!(status_err & IXGBE_RXD_STAT_L4CS))
1066                return;
1067
1068        if (status_err & IXGBE_RXDADV_ERR_TCPE) {
1069                u16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1070
1071                /*
1072                 * 82599 errata, UDP frames with a 0 checksum can be marked as
1073                 * checksum errors.
1074                 */
1075                if ((pkt_info & IXGBE_RXDADV_PKTTYPE_UDP) &&
1076                    (adapter->hw.mac.type == ixgbe_mac_82599EB))
1077                        return;
1078
1079                adapter->hw_csum_rx_error++;
1080                return;
1081        }
1082
1083        /* It must be a TCP or UDP packet with a valid checksum */
1084        skb->ip_summed = CHECKSUM_UNNECESSARY;
1085}
1086
1087static inline void ixgbe_release_rx_desc(struct ixgbe_ring *rx_ring, u32 val)
1088{
1089        /*
1090         * Force memory writes to complete before letting h/w
1091         * know there are new descriptors to fetch.  (Only
1092         * applicable for weak-ordered memory model archs,
1093         * such as IA-64).
1094         */
1095        wmb();
1096        writel(val, rx_ring->tail);
1097}
1098
1099/**
1100 * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
1101 * @rx_ring: ring to place buffers on
1102 * @cleaned_count: number of buffers to replace
1103 **/
1104void ixgbe_alloc_rx_buffers(struct ixgbe_ring *rx_ring, u16 cleaned_count)
1105{
1106        union ixgbe_adv_rx_desc *rx_desc;
1107        struct ixgbe_rx_buffer *bi;
1108        struct sk_buff *skb;
1109        u16 i = rx_ring->next_to_use;
1110
1111        /* do nothing if no valid netdev defined */
1112        if (!rx_ring->netdev)
1113                return;
1114
1115        while (cleaned_count--) {
1116                rx_desc = IXGBE_RX_DESC_ADV(rx_ring, i);
1117                bi = &rx_ring->rx_buffer_info[i];
1118                skb = bi->skb;
1119
1120                if (!skb) {
1121                        skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
1122                                                        rx_ring->rx_buf_len);
1123                        if (!skb) {
1124                                rx_ring->rx_stats.alloc_rx_buff_failed++;
1125                                goto no_buffers;
1126                        }
1127                        /* initialize queue mapping */
1128                        skb_record_rx_queue(skb, rx_ring->queue_index);
1129                        bi->skb = skb;
1130                }
1131
1132                if (!bi->dma) {
1133                        bi->dma = dma_map_single(rx_ring->dev,
1134                                                 skb->data,
1135                                                 rx_ring->rx_buf_len,
1136                                                 DMA_FROM_DEVICE);
1137                        if (dma_mapping_error(rx_ring->dev, bi->dma)) {
1138                                rx_ring->rx_stats.alloc_rx_buff_failed++;
1139                                bi->dma = 0;
1140                                goto no_buffers;
1141                        }
1142                }
1143
1144                if (ring_is_ps_enabled(rx_ring)) {
1145                        if (!bi->page) {
1146                                bi->page = alloc_page(GFP_ATOMIC | __GFP_COLD);
1147                                if (!bi->page) {
1148                                        rx_ring->rx_stats.alloc_rx_page_failed++;
1149                                        goto no_buffers;
1150                                }
1151                        }
1152
1153                        if (!bi->page_dma) {
1154                                /* use a half page if we're re-using */
1155                                bi->page_offset ^= PAGE_SIZE / 2;
1156                                bi->page_dma = dma_map_page(rx_ring->dev,
1157                                                            bi->page,
1158                                                            bi->page_offset,
1159                                                            PAGE_SIZE / 2,
1160                                                            DMA_FROM_DEVICE);
1161                                if (dma_mapping_error(rx_ring->dev,
1162                                                      bi->page_dma)) {
1163                                        rx_ring->rx_stats.alloc_rx_page_failed++;
1164                                        bi->page_dma = 0;
1165                                        goto no_buffers;
1166                                }
1167                        }
1168
1169                        /* Refresh the desc even if buffer_addrs didn't change
1170                         * because each write-back erases this info. */
1171                        rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
1172                        rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
1173                } else {
1174                        rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
1175                        rx_desc->read.hdr_addr = 0;
1176                }
1177
1178                i++;
1179                if (i == rx_ring->count)
1180                        i = 0;
1181        }
1182
1183no_buffers:
1184        if (rx_ring->next_to_use != i) {
1185                rx_ring->next_to_use = i;
1186                ixgbe_release_rx_desc(rx_ring, i);
1187        }
1188}
1189
1190static inline u16 ixgbe_get_hlen(union ixgbe_adv_rx_desc *rx_desc)
1191{
1192        /* HW will not DMA in data larger than the given buffer, even if it
1193         * parses the (NFS, of course) header to be larger.  In that case, it
1194         * fills the header buffer and spills the rest into the page.
1195         */
1196        u16 hdr_info = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.hdr_info);
1197        u16 hlen = (hdr_info &  IXGBE_RXDADV_HDRBUFLEN_MASK) >>
1198                    IXGBE_RXDADV_HDRBUFLEN_SHIFT;
1199        if (hlen > IXGBE_RX_HDR_SIZE)
1200                hlen = IXGBE_RX_HDR_SIZE;
1201        return hlen;
1202}
1203
1204/**
1205 * ixgbe_transform_rsc_queue - change rsc queue into a full packet
1206 * @skb: pointer to the last skb in the rsc queue
1207 *
1208 * This function changes a queue full of hw rsc buffers into a completed
1209 * packet.  It uses the ->prev pointers to find the first packet and then
1210 * turns it into the frag list owner.
1211 **/
1212static inline struct sk_buff *ixgbe_transform_rsc_queue(struct sk_buff *skb)
1213{
1214        unsigned int frag_list_size = 0;
1215        unsigned int skb_cnt = 1;
1216
1217        while (skb->prev) {
1218                struct sk_buff *prev = skb->prev;
1219                frag_list_size += skb->len;
1220                skb->prev = NULL;
1221                skb = prev;
1222                skb_cnt++;
1223        }
1224
1225        skb_shinfo(skb)->frag_list = skb->next;
1226        skb->next = NULL;
1227        skb->len += frag_list_size;
1228        skb->data_len += frag_list_size;
1229        skb->truesize += frag_list_size;
1230        IXGBE_RSC_CB(skb)->skb_cnt = skb_cnt;
1231
1232        return skb;
1233}
1234
1235static inline bool ixgbe_get_rsc_state(union ixgbe_adv_rx_desc *rx_desc)
1236{
1237        return !!(le32_to_cpu(rx_desc->wb.lower.lo_dword.data) &
1238                IXGBE_RXDADV_RSCCNT_MASK);
1239}
1240
1241static bool ixgbe_clean_rx_irq(struct ixgbe_q_vector *q_vector,
1242                               struct ixgbe_ring *rx_ring,
1243                               int budget)
1244{
1245        struct ixgbe_adapter *adapter = q_vector->adapter;
1246        union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
1247        struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
1248        struct sk_buff *skb;
1249        unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1250        const int current_node = numa_node_id();
1251#ifdef IXGBE_FCOE
1252        int ddp_bytes = 0;
1253#endif /* IXGBE_FCOE */
1254        u32 staterr;
1255        u16 i;
1256        u16 cleaned_count = 0;
1257        bool pkt_is_rsc = false;
1258
1259        i = rx_ring->next_to_clean;
1260        rx_desc = IXGBE_RX_DESC_ADV(rx_ring, i);
1261        staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1262
1263        while (staterr & IXGBE_RXD_STAT_DD) {
1264                u32 upper_len = 0;
1265
1266                rmb(); /* read descriptor and rx_buffer_info after status DD */
1267
1268                rx_buffer_info = &rx_ring->rx_buffer_info[i];
1269
1270                skb = rx_buffer_info->skb;
1271                rx_buffer_info->skb = NULL;
1272                prefetch(skb->data);
1273
1274                if (ring_is_rsc_enabled(rx_ring))
1275                        pkt_is_rsc = ixgbe_get_rsc_state(rx_desc);
1276
1277                /* linear means we are building an skb from multiple pages */
1278                if (!skb_is_nonlinear(skb)) {
1279                        u16 hlen;
1280                        if (pkt_is_rsc &&
1281                            !(staterr & IXGBE_RXD_STAT_EOP) &&
1282                            !skb->prev) {
1283                                /*
1284                                 * When HWRSC is enabled, delay unmapping
1285                                 * of the first packet. It carries the
1286                                 * header information, HW may still
1287                                 * access the header after the writeback.
1288                                 * Only unmap it when EOP is reached
1289                                 */
1290                                IXGBE_RSC_CB(skb)->delay_unmap = true;
1291                                IXGBE_RSC_CB(skb)->dma = rx_buffer_info->dma;
1292                        } else {
1293                                dma_unmap_single(rx_ring->dev,
1294                                                 rx_buffer_info->dma,
1295                                                 rx_ring->rx_buf_len,
1296                                                 DMA_FROM_DEVICE);
1297                        }
1298                        rx_buffer_info->dma = 0;
1299
1300                        if (ring_is_ps_enabled(rx_ring)) {
1301                                hlen = ixgbe_get_hlen(rx_desc);
1302                                upper_len = le16_to_cpu(rx_desc->wb.upper.length);
1303                        } else {
1304                                hlen = le16_to_cpu(rx_desc->wb.upper.length);
1305                        }
1306
1307                        skb_put(skb, hlen);
1308                } else {
1309                        /* assume packet split since header is unmapped */
1310                        upper_len = le16_to_cpu(rx_desc->wb.upper.length);
1311                }
1312
1313                if (upper_len) {
1314                        dma_unmap_page(rx_ring->dev,
1315                                       rx_buffer_info->page_dma,
1316                                       PAGE_SIZE / 2,
1317                                       DMA_FROM_DEVICE);
1318                        rx_buffer_info->page_dma = 0;
1319                        skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1320                                           rx_buffer_info->page,
1321                                           rx_buffer_info->page_offset,
1322                                           upper_len);
1323
1324                        if ((page_count(rx_buffer_info->page) == 1) &&
1325                            (page_to_nid(rx_buffer_info->page) == current_node))
1326                                get_page(rx_buffer_info->page);
1327                        else
1328                                rx_buffer_info->page = NULL;
1329
1330                        skb->len += upper_len;
1331                        skb->data_len += upper_len;
1332                        skb->truesize += PAGE_SIZE / 2;
1333                }
1334
1335                i++;
1336                if (i == rx_ring->count)
1337                        i = 0;
1338
1339                next_rxd = IXGBE_RX_DESC_ADV(rx_ring, i);
1340                prefetch(next_rxd);
1341                cleaned_count++;
1342
1343                if (pkt_is_rsc) {
1344                        u32 nextp = (staterr & IXGBE_RXDADV_NEXTP_MASK) >>
1345                                     IXGBE_RXDADV_NEXTP_SHIFT;
1346                        next_buffer = &rx_ring->rx_buffer_info[nextp];
1347                } else {
1348                        next_buffer = &rx_ring->rx_buffer_info[i];
1349                }
1350
1351                if (!(staterr & IXGBE_RXD_STAT_EOP)) {
1352                        if (ring_is_ps_enabled(rx_ring)) {
1353                                rx_buffer_info->skb = next_buffer->skb;
1354                                rx_buffer_info->dma = next_buffer->dma;
1355                                next_buffer->skb = skb;
1356                                next_buffer->dma = 0;
1357                        } else {
1358                                skb->next = next_buffer->skb;
1359                                skb->next->prev = skb;
1360                        }
1361                        rx_ring->rx_stats.non_eop_descs++;
1362                        goto next_desc;
1363                }
1364
1365                if (skb->prev) {
1366                        skb = ixgbe_transform_rsc_queue(skb);
1367                        /* if we got here without RSC the packet is invalid */
1368                        if (!pkt_is_rsc) {
1369                                __pskb_trim(skb, 0);
1370                                rx_buffer_info->skb = skb;
1371                                goto next_desc;
1372                        }
1373                }
1374
1375                if (ring_is_rsc_enabled(rx_ring)) {
1376                        if (IXGBE_RSC_CB(skb)->delay_unmap) {
1377                                dma_unmap_single(rx_ring->dev,
1378                                                 IXGBE_RSC_CB(skb)->dma,
1379                                                 rx_ring->rx_buf_len,
1380                                                 DMA_FROM_DEVICE);
1381                                IXGBE_RSC_CB(skb)->dma = 0;
1382                                IXGBE_RSC_CB(skb)->delay_unmap = false;
1383                        }
1384                }
1385                if (pkt_is_rsc) {
1386                        if (ring_is_ps_enabled(rx_ring))
1387                                rx_ring->rx_stats.rsc_count +=
1388                                        skb_shinfo(skb)->nr_frags;
1389                        else
1390                                rx_ring->rx_stats.rsc_count +=
1391                                        IXGBE_RSC_CB(skb)->skb_cnt;
1392                        rx_ring->rx_stats.rsc_flush++;
1393                }
1394
1395                /* ERR_MASK will only have valid bits if EOP set */
1396                if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
1397                        dev_kfree_skb_any(skb);
1398                        goto next_desc;
1399                }
1400
1401                ixgbe_rx_checksum(adapter, rx_desc, skb, staterr);
1402                if (adapter->netdev->features & NETIF_F_RXHASH)
1403                        ixgbe_rx_hash(rx_desc, skb);
1404
1405                /* probably a little skewed due to removing CRC */
1406                total_rx_bytes += skb->len;
1407                total_rx_packets++;
1408
1409                skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1410#ifdef IXGBE_FCOE
1411                /* if ddp, not passing to ULD unless for FCP_RSP or error */
1412                if (ixgbe_rx_is_fcoe(adapter, rx_desc)) {
1413                        ddp_bytes = ixgbe_fcoe_ddp(adapter, rx_desc, skb,
1414                                                   staterr);
1415                        if (!ddp_bytes) {
1416                                dev_kfree_skb_any(skb);
1417                                goto next_desc;
1418                        }
1419                }
1420#endif /* IXGBE_FCOE */
1421                ixgbe_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
1422
1423                budget--;
1424next_desc:
1425                rx_desc->wb.upper.status_error = 0;
1426
1427                if (!budget)
1428                        break;
1429
1430                /* return some buffers to hardware, one at a time is too slow */
1431                if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
1432                        ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
1433                        cleaned_count = 0;
1434                }
1435
1436                /* use prefetched values */
1437                rx_desc = next_rxd;
1438                staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1439        }
1440
1441        rx_ring->next_to_clean = i;
1442        cleaned_count = ixgbe_desc_unused(rx_ring);
1443
1444        if (cleaned_count)
1445                ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
1446
1447#ifdef IXGBE_FCOE
1448        /* include DDPed FCoE data */
1449        if (ddp_bytes > 0) {
1450                unsigned int mss;
1451
1452                mss = rx_ring->netdev->mtu - sizeof(struct fcoe_hdr) -
1453                        sizeof(struct fc_frame_header) -
1454                        sizeof(struct fcoe_crc_eof);
1455                if (mss > 512)
1456                        mss &= ~511;
1457                total_rx_bytes += ddp_bytes;
1458                total_rx_packets += DIV_ROUND_UP(ddp_bytes, mss);
1459        }
1460#endif /* IXGBE_FCOE */
1461
1462        u64_stats_update_begin(&rx_ring->syncp);
1463        rx_ring->stats.packets += total_rx_packets;
1464        rx_ring->stats.bytes += total_rx_bytes;
1465        u64_stats_update_end(&rx_ring->syncp);
1466        q_vector->rx.total_packets += total_rx_packets;
1467        q_vector->rx.total_bytes += total_rx_bytes;
1468
1469        return !!budget;
1470}
1471
1472/**
1473 * ixgbe_configure_msix - Configure MSI-X hardware
1474 * @adapter: board private structure
1475 *
1476 * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
1477 * interrupts.
1478 **/
1479static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
1480{
1481        struct ixgbe_q_vector *q_vector;
1482        int q_vectors, v_idx;
1483        u32 mask;
1484
1485        q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1486
1487        /* Populate MSIX to EITR Select */
1488        if (adapter->num_vfs > 32) {
1489                u32 eitrsel = (1 << (adapter->num_vfs - 32)) - 1;
1490                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, eitrsel);
1491        }
1492
1493        /*
1494         * Populate the IVAR table and set the ITR values to the
1495         * corresponding register.
1496         */
1497        for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1498                struct ixgbe_ring *ring;
1499                q_vector = adapter->q_vector[v_idx];
1500
1501                for (ring = q_vector->rx.ring; ring != NULL; ring = ring->next)
1502                        ixgbe_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1503
1504                for (ring = q_vector->tx.ring; ring != NULL; ring = ring->next)
1505                        ixgbe_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1506
1507                if (q_vector->tx.ring && !q_vector->rx.ring) {
1508                        /* tx only vector */
1509                        if (adapter->tx_itr_setting == 1)
1510                                q_vector->itr = IXGBE_10K_ITR;
1511                        else
1512                                q_vector->itr = adapter->tx_itr_setting;
1513                } else {
1514                        /* rx or rx/tx vector */
1515                        if (adapter->rx_itr_setting == 1)
1516                                q_vector->itr = IXGBE_20K_ITR;
1517                        else
1518                                q_vector->itr = adapter->rx_itr_setting;
1519                }
1520
1521                ixgbe_write_eitr(q_vector);
1522        }
1523
1524        switch (adapter->hw.mac.type) {
1525        case ixgbe_mac_82598EB:
1526                ixgbe_set_ivar(adapter, -1, IXGBE_IVAR_OTHER_CAUSES_INDEX,
1527                               v_idx);
1528                break;
1529        case ixgbe_mac_82599EB:
1530        case ixgbe_mac_X540:
1531                ixgbe_set_ivar(adapter, -1, 1, v_idx);
1532                break;
1533        default:
1534                break;
1535        }
1536        IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
1537
1538        /* set up to autoclear timer, and the vectors */
1539        mask = IXGBE_EIMS_ENABLE_MASK;
1540        mask &= ~(IXGBE_EIMS_OTHER |
1541                  IXGBE_EIMS_MAILBOX |
1542                  IXGBE_EIMS_LSC);
1543
1544        IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
1545}
1546
1547enum latency_range {
1548        lowest_latency = 0,
1549        low_latency = 1,
1550        bulk_latency = 2,
1551        latency_invalid = 255
1552};
1553
1554/**
1555 * ixgbe_update_itr - update the dynamic ITR value based on statistics
1556 * @q_vector: structure containing interrupt and ring information
1557 * @ring_container: structure containing ring performance data
1558 *
1559 *      Stores a new ITR value based on packets and byte
1560 *      counts during the last interrupt.  The advantage of per interrupt
1561 *      computation is faster updates and more accurate ITR for the current
1562 *      traffic pattern.  Constants in this function were computed
1563 *      based on theoretical maximum wire speed and thresholds were set based
1564 *      on testing data as well as attempting to minimize response time
1565 *      while increasing bulk throughput.
1566 *      this functionality is controlled by the InterruptThrottleRate module
1567 *      parameter (see ixgbe_param.c)
1568 **/
1569static void ixgbe_update_itr(struct ixgbe_q_vector *q_vector,
1570                             struct ixgbe_ring_container *ring_container)
1571{
1572        u64 bytes_perint;
1573        struct ixgbe_adapter *adapter = q_vector->adapter;
1574        int bytes = ring_container->total_bytes;
1575        int packets = ring_container->total_packets;
1576        u32 timepassed_us;
1577        u8 itr_setting = ring_container->itr;
1578
1579        if (packets == 0)
1580                return;
1581
1582        /* simple throttlerate management
1583         *    0-20MB/s lowest (100000 ints/s)
1584         *   20-100MB/s low   (20000 ints/s)
1585         *  100-1249MB/s bulk (8000 ints/s)
1586         */
1587        /* what was last interrupt timeslice? */
1588        timepassed_us = q_vector->itr >> 2;
1589        bytes_perint = bytes / timepassed_us; /* bytes/usec */
1590
1591        switch (itr_setting) {
1592        case lowest_latency:
1593                if (bytes_perint > adapter->eitr_low)
1594                        itr_setting = low_latency;
1595                break;
1596        case low_latency:
1597                if (bytes_perint > adapter->eitr_high)
1598                        itr_setting = bulk_latency;
1599                else if (bytes_perint <= adapter->eitr_low)
1600                        itr_setting = lowest_latency;
1601                break;
1602        case bulk_latency:
1603                if (bytes_perint <= adapter->eitr_high)
1604                        itr_setting = low_latency;
1605                break;
1606        }
1607
1608        /* clear work counters since we have the values we need */
1609        ring_container->total_bytes = 0;
1610        ring_container->total_packets = 0;
1611
1612        /* write updated itr to ring container */
1613        ring_container->itr = itr_setting;
1614}
1615
1616/**
1617 * ixgbe_write_eitr - write EITR register in hardware specific way
1618 * @q_vector: structure containing interrupt and ring information
1619 *
1620 * This function is made to be called by ethtool and by the driver
1621 * when it needs to update EITR registers at runtime.  Hardware
1622 * specific quirks/differences are taken care of here.
1623 */
1624void ixgbe_write_eitr(struct ixgbe_q_vector *q_vector)
1625{
1626        struct ixgbe_adapter *adapter = q_vector->adapter;
1627        struct ixgbe_hw *hw = &adapter->hw;
1628        int v_idx = q_vector->v_idx;
1629        u32 itr_reg = q_vector->itr;
1630
1631        switch (adapter->hw.mac.type) {
1632        case ixgbe_mac_82598EB:
1633                /* must write high and low 16 bits to reset counter */
1634                itr_reg |= (itr_reg << 16);
1635                break;
1636        case ixgbe_mac_82599EB:
1637        case ixgbe_mac_X540:
1638                /*
1639                 * set the WDIS bit to not clear the timer bits and cause an
1640                 * immediate assertion of the interrupt
1641                 */
1642                itr_reg |= IXGBE_EITR_CNT_WDIS;
1643                break;
1644        default:
1645                break;
1646        }
1647        IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg);
1648}
1649
1650static void ixgbe_set_itr(struct ixgbe_q_vector *q_vector)
1651{
1652        u32 new_itr = q_vector->itr;
1653        u8 current_itr;
1654
1655        ixgbe_update_itr(q_vector, &q_vector->tx);
1656        ixgbe_update_itr(q_vector, &q_vector->rx);
1657
1658        current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1659
1660        switch (current_itr) {
1661        /* counts and packets in update_itr are dependent on these numbers */
1662        case lowest_latency:
1663                new_itr = IXGBE_100K_ITR;
1664                break;
1665        case low_latency:
1666                new_itr = IXGBE_20K_ITR;
1667                break;
1668        case bulk_latency:
1669                new_itr = IXGBE_8K_ITR;
1670                break;
1671        default:
1672                break;
1673        }
1674
1675        if (new_itr != q_vector->itr) {
1676                /* do an exponential smoothing */
1677                new_itr = (10 * new_itr * q_vector->itr) /
1678                          ((9 * new_itr) + q_vector->itr);
1679
1680                /* save the algorithm value here */
1681                q_vector->itr = new_itr & IXGBE_MAX_EITR;
1682
1683                ixgbe_write_eitr(q_vector);
1684        }
1685}
1686
1687/**
1688 * ixgbe_check_overtemp_subtask - check for over tempurature
1689 * @adapter: pointer to adapter
1690 **/
1691static void ixgbe_check_overtemp_subtask(struct ixgbe_adapter *adapter)
1692{
1693        struct ixgbe_hw *hw = &adapter->hw;
1694        u32 eicr = adapter->interrupt_event;
1695
1696        if (test_bit(__IXGBE_DOWN, &adapter->state))
1697                return;
1698
1699        if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) &&
1700            !(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_EVENT))
1701                return;
1702
1703        adapter->flags2 &= ~IXGBE_FLAG2_TEMP_SENSOR_EVENT;
1704
1705        switch (hw->device_id) {
1706        case IXGBE_DEV_ID_82599_T3_LOM:
1707                /*
1708                 * Since the warning interrupt is for both ports
1709                 * we don't have to check if:
1710                 *  - This interrupt wasn't for our port.
1711                 *  - We may have missed the interrupt so always have to
1712                 *    check if we  got a LSC
1713                 */
1714                if (!(eicr & IXGBE_EICR_GPI_SDP0) &&
1715                    !(eicr & IXGBE_EICR_LSC))
1716                        return;
1717
1718                if (!(eicr & IXGBE_EICR_LSC) && hw->mac.ops.check_link) {
1719                        u32 autoneg;
1720                        bool link_up = false;
1721
1722                        hw->mac.ops.check_link(hw, &autoneg, &link_up, false);
1723
1724                        if (link_up)
1725                                return;
1726                }
1727
1728                /* Check if this is not due to overtemp */
1729                if (hw->phy.ops.check_overtemp(hw) != IXGBE_ERR_OVERTEMP)
1730                        return;
1731
1732                break;
1733        default:
1734                if (!(eicr & IXGBE_EICR_GPI_SDP0))
1735                        return;
1736                break;
1737        }
1738        e_crit(drv,
1739               "Network adapter has been stopped because it has over heated. "
1740               "Restart the computer. If the problem persists, "
1741               "power off the system and replace the adapter\n");
1742
1743        adapter->interrupt_event = 0;
1744}
1745
1746static void ixgbe_check_fan_failure(struct ixgbe_adapter *adapter, u32 eicr)
1747{
1748        struct ixgbe_hw *hw = &adapter->hw;
1749
1750        if ((adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) &&
1751            (eicr & IXGBE_EICR_GPI_SDP1)) {
1752                e_crit(probe, "Fan has stopped, replace the adapter\n");
1753                /* write to clear the interrupt */
1754                IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
1755        }
1756}
1757
1758static void ixgbe_check_overtemp_event(struct ixgbe_adapter *adapter, u32 eicr)
1759{
1760        if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE))
1761                return;
1762
1763        switch (adapter->hw.mac.type) {
1764        case ixgbe_mac_82599EB:
1765                /*
1766                 * Need to check link state so complete overtemp check
1767                 * on service task
1768                 */
1769                if (((eicr & IXGBE_EICR_GPI_SDP0) || (eicr & IXGBE_EICR_LSC)) &&
1770                    (!test_bit(__IXGBE_DOWN, &adapter->state))) {
1771                        adapter->interrupt_event = eicr;
1772                        adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_EVENT;
1773                        ixgbe_service_event_schedule(adapter);
1774                        return;
1775                }
1776                return;
1777        case ixgbe_mac_X540:
1778                if (!(eicr & IXGBE_EICR_TS))
1779                        return;
1780                break;
1781        default:
1782                return;
1783        }
1784
1785        e_crit(drv,
1786               "Network adapter has been stopped because it has over heated. "
1787               "Restart the computer. If the problem persists, "
1788               "power off the system and replace the adapter\n");
1789}
1790
1791static void ixgbe_check_sfp_event(struct ixgbe_adapter *adapter, u32 eicr)
1792{
1793        struct ixgbe_hw *hw = &adapter->hw;
1794
1795        if (eicr & IXGBE_EICR_GPI_SDP2) {
1796                /* Clear the interrupt */
1797                IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP2);
1798                if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
1799                        adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
1800                        ixgbe_service_event_schedule(adapter);
1801                }
1802        }
1803
1804        if (eicr & IXGBE_EICR_GPI_SDP1) {
1805                /* Clear the interrupt */
1806                IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
1807                if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
1808                        adapter->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
1809                        ixgbe_service_event_schedule(adapter);
1810                }
1811        }
1812}
1813
1814static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
1815{
1816        struct ixgbe_hw *hw = &adapter->hw;
1817
1818        adapter->lsc_int++;
1819        adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1820        adapter->link_check_timeout = jiffies;
1821        if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
1822                IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
1823                IXGBE_WRITE_FLUSH(hw);
1824                ixgbe_service_event_schedule(adapter);
1825        }
1826}
1827
1828static inline void ixgbe_irq_enable_queues(struct ixgbe_adapter *adapter,
1829                                           u64 qmask)
1830{
1831        u32 mask;
1832        struct ixgbe_hw *hw = &adapter->hw;
1833
1834        switch (hw->mac.type) {
1835        case ixgbe_mac_82598EB:
1836                mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
1837                IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
1838                break;
1839        case ixgbe_mac_82599EB:
1840        case ixgbe_mac_X540:
1841                mask = (qmask & 0xFFFFFFFF);
1842                if (mask)
1843                        IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
1844                mask = (qmask >> 32);
1845                if (mask)
1846                        IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
1847                break;
1848        default:
1849                break;
1850        }
1851        /* skip the flush */
1852}
1853
1854static inline void ixgbe_irq_disable_queues(struct ixgbe_adapter *adapter,
1855                                            u64 qmask)
1856{
1857        u32 mask;
1858        struct ixgbe_hw *hw = &adapter->hw;
1859
1860        switch (hw->mac.type) {
1861        case ixgbe_mac_82598EB:
1862                mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
1863                IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
1864                break;
1865        case ixgbe_mac_82599EB:
1866        case ixgbe_mac_X540:
1867                mask = (qmask & 0xFFFFFFFF);
1868                if (mask)
1869                        IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
1870                mask = (qmask >> 32);
1871                if (mask)
1872                        IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
1873                break;
1874        default:
1875                break;
1876        }
1877        /* skip the flush */
1878}
1879
1880/**
1881 * ixgbe_irq_enable - Enable default interrupt generation settings
1882 * @adapter: board private structure
1883 **/
1884static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter, bool queues,
1885                                    bool flush)
1886{
1887        u32 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
1888
1889        /* don't reenable LSC while waiting for link */
1890        if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE)
1891                mask &= ~IXGBE_EIMS_LSC;
1892
1893        if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE)
1894                switch (adapter->hw.mac.type) {
1895                case ixgbe_mac_82599EB:
1896                        mask |= IXGBE_EIMS_GPI_SDP0;
1897                        break;
1898                case ixgbe_mac_X540:
1899                        mask |= IXGBE_EIMS_TS;
1900                        break;
1901                default:
1902                        break;
1903                }
1904        if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
1905                mask |= IXGBE_EIMS_GPI_SDP1;
1906        switch (adapter->hw.mac.type) {
1907        case ixgbe_mac_82599EB:
1908                mask |= IXGBE_EIMS_GPI_SDP1;
1909                mask |= IXGBE_EIMS_GPI_SDP2;
1910        case ixgbe_mac_X540:
1911                mask |= IXGBE_EIMS_ECC;
1912                mask |= IXGBE_EIMS_MAILBOX;
1913                break;
1914        default:
1915                break;
1916        }
1917        if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) &&
1918            !(adapter->flags2 & IXGBE_FLAG2_FDIR_REQUIRES_REINIT))
1919                mask |= IXGBE_EIMS_FLOW_DIR;
1920
1921        IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
1922        if (queues)
1923                ixgbe_irq_enable_queues(adapter, ~0);
1924        if (flush)
1925                IXGBE_WRITE_FLUSH(&adapter->hw);
1926}
1927
1928static irqreturn_t ixgbe_msix_other(int irq, void *data)
1929{
1930        struct ixgbe_adapter *adapter = data;
1931        struct ixgbe_hw *hw = &adapter->hw;
1932        u32 eicr;
1933
1934        /*
1935         * Workaround for Silicon errata.  Use clear-by-write instead
1936         * of clear-by-read.  Reading with EICS will return the
1937         * interrupt causes without clearing, which later be done
1938         * with the write to EICR.
1939         */
1940        eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
1941        IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
1942
1943        if (eicr & IXGBE_EICR_LSC)
1944                ixgbe_check_lsc(adapter);
1945
1946        if (eicr & IXGBE_EICR_MAILBOX)
1947                ixgbe_msg_task(adapter);
1948
1949        switch (hw->mac.type) {
1950        case ixgbe_mac_82599EB:
1951        case ixgbe_mac_X540:
1952                if (eicr & IXGBE_EICR_ECC)
1953                        e_info(link, "Received unrecoverable ECC Err, please "
1954                               "reboot\n");
1955                /* Handle Flow Director Full threshold interrupt */
1956                if (eicr & IXGBE_EICR_FLOW_DIR) {
1957                        int reinit_count = 0;
1958                        int i;
1959                        for (i = 0; i < adapter->num_tx_queues; i++) {
1960                                struct ixgbe_ring *ring = adapter->tx_ring[i];
1961                                if (test_and_clear_bit(__IXGBE_TX_FDIR_INIT_DONE,
1962                                                       &ring->state))
1963                                        reinit_count++;
1964                        }
1965                        if (reinit_count) {
1966                                /* no more flow director interrupts until after init */
1967                                IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_FLOW_DIR);
1968                                adapter->flags2 |= IXGBE_FLAG2_FDIR_REQUIRES_REINIT;
1969                                ixgbe_service_event_schedule(adapter);
1970                        }
1971                }
1972                ixgbe_check_sfp_event(adapter, eicr);
1973                ixgbe_check_overtemp_event(adapter, eicr);
1974                break;
1975        default:
1976                break;
1977        }
1978
1979        ixgbe_check_fan_failure(adapter, eicr);
1980
1981        /* re-enable the original interrupt state, no lsc, no queues */
1982        if (!test_bit(__IXGBE_DOWN, &adapter->state))
1983                ixgbe_irq_enable(adapter, false, false);
1984
1985        return IRQ_HANDLED;
1986}
1987
1988static irqreturn_t ixgbe_msix_clean_rings(int irq, void *data)
1989{
1990        struct ixgbe_q_vector *q_vector = data;
1991
1992        /* EIAM disabled interrupts (on this vector) for us */
1993
1994        if (q_vector->rx.ring || q_vector->tx.ring)
1995                napi_schedule(&q_vector->napi);
1996
1997        return IRQ_HANDLED;
1998}
1999
2000static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
2001                                     int r_idx)
2002{
2003        struct ixgbe_q_vector *q_vector = a->q_vector[v_idx];
2004        struct ixgbe_ring *rx_ring = a->rx_ring[r_idx];
2005
2006        rx_ring->q_vector = q_vector;
2007        rx_ring->next = q_vector->rx.ring;
2008        q_vector->rx.ring = rx_ring;
2009        q_vector->rx.count++;
2010}
2011
2012static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
2013                                     int t_idx)
2014{
2015        struct ixgbe_q_vector *q_vector = a->q_vector[v_idx];
2016        struct ixgbe_ring *tx_ring = a->tx_ring[t_idx];
2017
2018        tx_ring->q_vector = q_vector;
2019        tx_ring->next = q_vector->tx.ring;
2020        q_vector->tx.ring = tx_ring;
2021        q_vector->tx.count++;
2022        q_vector->tx.work_limit = a->tx_work_limit;
2023}
2024
2025/**
2026 * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
2027 * @adapter: board private structure to initialize
2028 *
2029 * This function maps descriptor rings to the queue-specific vectors
2030 * we were allotted through the MSI-X enabling code.  Ideally, we'd have
2031 * one vector per ring/queue, but on a constrained vector budget, we
2032 * group the rings as "efficiently" as possible.  You would add new
2033 * mapping configurations in here.
2034 **/
2035static void ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter)
2036{
2037        int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2038        int rxr_remaining = adapter->num_rx_queues, rxr_idx = 0;
2039        int txr_remaining = adapter->num_tx_queues, txr_idx = 0;
2040        int v_start = 0;
2041
2042        /* only one q_vector if MSI-X is disabled. */
2043        if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
2044                q_vectors = 1;
2045
2046        /*
2047         * If we don't have enough vectors for a 1-to-1 mapping, we'll have to
2048         * group them so there are multiple queues per vector.
2049         *
2050         * Re-adjusting *qpv takes care of the remainder.
2051         */
2052        for (; v_start < q_vectors && rxr_remaining; v_start++) {
2053                int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_start);
2054                for (; rqpv; rqpv--, rxr_idx++, rxr_remaining--)
2055                        map_vector_to_rxq(adapter, v_start, rxr_idx);
2056        }
2057
2058        /*
2059         * If there are not enough q_vectors for each ring to have it's own
2060         * vector then we must pair up Rx/Tx on a each vector
2061         */
2062        if ((v_start + txr_remaining) > q_vectors)
2063                v_start = 0;
2064
2065        for (; v_start < q_vectors && txr_remaining; v_start++) {
2066                int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_start);
2067                for (; tqpv; tqpv--, txr_idx++, txr_remaining--)
2068                        map_vector_to_txq(adapter, v_start, txr_idx);
2069        }
2070}
2071
2072/**
2073 * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
2074 * @adapter: board private structure
2075 *
2076 * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
2077 * interrupts from the kernel.
2078 **/
2079static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
2080{
2081        struct net_device *netdev = adapter->netdev;
2082        int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2083        int vector, err;
2084        int ri = 0, ti = 0;
2085
2086        for (vector = 0; vector < q_vectors; vector++) {
2087                struct ixgbe_q_vector *q_vector = adapter->q_vector[vector];
2088                struct msix_entry *entry = &adapter->msix_entries[vector];
2089
2090                if (q_vector->tx.ring && q_vector->rx.ring) {
2091                        snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2092                                 "%s-%s-%d", netdev->name, "TxRx", ri++);
2093                        ti++;
2094                } else if (q_vector->rx.ring) {
2095                        snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2096                                 "%s-%s-%d", netdev->name, "rx", ri++);
2097                } else if (q_vector->tx.ring) {
2098                        snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2099                                 "%s-%s-%d", netdev->name, "tx", ti++);
2100                } else {
2101                        /* skip this unused q_vector */
2102                        continue;
2103                }
2104                err = request_irq(entry->vector, &ixgbe_msix_clean_rings, 0,
2105                                  q_vector->name, q_vector);
2106                if (err) {
2107                        e_err(probe, "request_irq failed for MSIX interrupt "
2108                              "Error: %d\n", err);
2109                        goto free_queue_irqs;
2110                }
2111                /* If Flow Director is enabled, set interrupt affinity */
2112                if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
2113                        /* assign the mask for this irq */
2114                        irq_set_affinity_hint(entry->vector,
2115                                              q_vector->affinity_mask);
2116                }
2117        }
2118
2119        err = request_irq(adapter->msix_entries[vector].vector,
2120                          ixgbe_msix_other, 0, netdev->name, adapter);
2121        if (err) {
2122                e_err(probe, "request_irq for msix_lsc failed: %d\n", err);
2123                goto free_queue_irqs;
2124        }
2125
2126        return 0;
2127
2128free_queue_irqs:
2129        while (vector) {
2130                vector--;
2131                irq_set_affinity_hint(adapter->msix_entries[vector].vector,
2132                                      NULL);
2133                free_irq(adapter->msix_entries[vector].vector,
2134                         adapter->q_vector[vector]);
2135        }
2136        adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2137        pci_disable_msix(adapter->pdev);
2138        kfree(adapter->msix_entries);
2139        adapter->msix_entries = NULL;
2140        return err;
2141}
2142
2143/**
2144 * ixgbe_intr - legacy mode Interrupt Handler
2145 * @irq: interrupt number
2146 * @data: pointer to a network interface device structure
2147 **/
2148static irqreturn_t ixgbe_intr(int irq, void *data)
2149{
2150        struct ixgbe_adapter *adapter = data;
2151        struct ixgbe_hw *hw = &adapter->hw;
2152        struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
2153        u32 eicr;
2154
2155        /*
2156         * Workaround for silicon errata on 82598.  Mask the interrupts
2157         * before the read of EICR.
2158         */
2159        IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
2160
2161        /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
2162         * therefore no explicit interrupt disable is necessary */
2163        eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
2164        if (!eicr) {
2165                /*
2166                 * shared interrupt alert!
2167                 * make sure interrupts are enabled because the read will
2168                 * have disabled interrupts due to EIAM
2169                 * finish the workaround of silicon errata on 82598.  Unmask
2170                 * the interrupt that we masked before the EICR read.
2171                 */
2172                if (!test_bit(__IXGBE_DOWN, &adapter->state))
2173                        ixgbe_irq_enable(adapter, true, true);
2174                return IRQ_NONE;        /* Not our interrupt */
2175        }
2176
2177        if (eicr & IXGBE_EICR_LSC)
2178                ixgbe_check_lsc(adapter);
2179
2180        switch (hw->mac.type) {
2181        case ixgbe_mac_82599EB:
2182                ixgbe_check_sfp_event(adapter, eicr);
2183                /* Fall through */
2184        case ixgbe_mac_X540:
2185                if (eicr & IXGBE_EICR_ECC)
2186                        e_info(link, "Received unrecoverable ECC err, please "
2187                                     "reboot\n");
2188                ixgbe_check_overtemp_event(adapter, eicr);
2189                break;
2190        default:
2191                break;
2192        }
2193
2194        ixgbe_check_fan_failure(adapter, eicr);
2195
2196        if (napi_schedule_prep(&(q_vector->napi))) {
2197                /* would disable interrupts here but EIAM disabled it */
2198                __napi_schedule(&(q_vector->napi));
2199        }
2200
2201        /*
2202         * re-enable link(maybe) and non-queue interrupts, no flush.
2203         * ixgbe_poll will re-enable the queue interrupts
2204         */
2205
2206        if (!test_bit(__IXGBE_DOWN, &adapter->state))
2207                ixgbe_irq_enable(adapter, false, false);
2208
2209        return IRQ_HANDLED;
2210}
2211
2212static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
2213{
2214        int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2215        int i;
2216
2217        /* legacy and MSI only use one vector */
2218        if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
2219                q_vectors = 1;
2220
2221        for (i = 0; i < adapter->num_rx_queues; i++) {
2222                adapter->rx_ring[i]->q_vector = NULL;
2223                adapter->rx_ring[i]->next = NULL;
2224        }
2225        for (i = 0; i < adapter->num_tx_queues; i++) {
2226                adapter->tx_ring[i]->q_vector = NULL;
2227                adapter->tx_ring[i]->next = NULL;
2228        }
2229
2230        for (i = 0; i < q_vectors; i++) {
2231                struct ixgbe_q_vector *q_vector = adapter->q_vector[i];
2232                memset(&q_vector->rx, 0, sizeof(struct ixgbe_ring_container));
2233                memset(&q_vector->tx, 0, sizeof(struct ixgbe_ring_container));
2234        }
2235}
2236
2237/**
2238 * ixgbe_request_irq - initialize interrupts
2239 * @adapter: board private structure
2240 *
2241 * Attempts to configure interrupts using the best available
2242 * capabilities of the hardware and kernel.
2243 **/
2244static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
2245{
2246        struct net_device *netdev = adapter->netdev;
2247        int err;
2248
2249        /* map all of the rings to the q_vectors */
2250        ixgbe_map_rings_to_vectors(adapter);
2251
2252        if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2253                err = ixgbe_request_msix_irqs(adapter);
2254        else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED)
2255                err = request_irq(adapter->pdev->irq, ixgbe_intr, 0,
2256                                  netdev->name, adapter);
2257        else
2258                err = request_irq(adapter->pdev->irq, ixgbe_intr, IRQF_SHARED,
2259                                  netdev->name, adapter);
2260
2261        if (err) {
2262                e_err(probe, "request_irq failed, Error %d\n", err);
2263
2264                /* place q_vectors and rings back into a known good state */
2265                ixgbe_reset_q_vectors(adapter);
2266        }
2267
2268        return err;
2269}
2270
2271static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
2272{
2273        if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2274                int i, q_vectors;
2275
2276                q_vectors = adapter->num_msix_vectors;
2277                i = q_vectors - 1;
2278                free_irq(adapter->msix_entries[i].vector, adapter);
2279                i--;
2280
2281                for (; i >= 0; i--) {
2282                        /* free only the irqs that were actually requested */
2283                        if (!adapter->q_vector[i]->rx.ring &&
2284                            !adapter->q_vector[i]->tx.ring)
2285                                continue;
2286
2287                        /* clear the affinity_mask in the IRQ descriptor */
2288                        irq_set_affinity_hint(adapter->msix_entries[i].vector,
2289                                              NULL);
2290
2291                        free_irq(adapter->msix_entries[i].vector,
2292                                 adapter->q_vector[i]);
2293                }
2294        } else {
2295                free_irq(adapter->pdev->irq, adapter);
2296        }
2297
2298        /* clear q_vector state information */
2299        ixgbe_reset_q_vectors(adapter);
2300}
2301
2302/**
2303 * ixgbe_irq_disable - Mask off interrupt generation on the NIC
2304 * @adapter: board private structure
2305 **/
2306static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
2307{
2308        switch (adapter->hw.mac.type) {
2309        case ixgbe_mac_82598EB:
2310                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
2311                break;
2312        case ixgbe_mac_82599EB:
2313        case ixgbe_mac_X540:
2314                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
2315                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
2316                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
2317                break;
2318        default:
2319                break;
2320        }
2321        IXGBE_WRITE_FLUSH(&adapter->hw);
2322        if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2323                int i;
2324                for (i = 0; i < adapter->num_msix_vectors; i++)
2325                        synchronize_irq(adapter->msix_entries[i].vector);
2326        } else {
2327                synchronize_irq(adapter->pdev->irq);
2328        }
2329}
2330
2331/**
2332 * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
2333 *
2334 **/
2335static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
2336{
2337        struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
2338
2339        /* rx/tx vector */
2340        if (adapter->rx_itr_setting == 1)
2341                q_vector->itr = IXGBE_20K_ITR;
2342        else
2343                q_vector->itr = adapter->rx_itr_setting;
2344
2345        ixgbe_write_eitr(q_vector);
2346
2347        ixgbe_set_ivar(adapter, 0, 0, 0);
2348        ixgbe_set_ivar(adapter, 1, 0, 0);
2349
2350        e_info(hw, "Legacy interrupt IVAR setup done\n");
2351}
2352
2353/**
2354 * ixgbe_configure_tx_ring - Configure 8259x Tx ring after Reset
2355 * @adapter: board private structure
2356 * @ring: structure containing ring specific data
2357 *
2358 * Configure the Tx descriptor ring after a reset.
2359 **/
2360void ixgbe_configure_tx_ring(struct ixgbe_adapter *adapter,
2361                             struct ixgbe_ring *ring)
2362{
2363        struct ixgbe_hw *hw = &adapter->hw;
2364        u64 tdba = ring->dma;
2365        int wait_loop = 10;
2366        u32 txdctl = IXGBE_TXDCTL_ENABLE;
2367        u8 reg_idx = ring->reg_idx;
2368
2369        /* disable queue to avoid issues while updating state */
2370        IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), 0);
2371        IXGBE_WRITE_FLUSH(hw);
2372
2373        IXGBE_WRITE_REG(hw, IXGBE_TDBAL(reg_idx),
2374                        (tdba & DMA_BIT_MASK(32)));
2375        IXGBE_WRITE_REG(hw, IXGBE_TDBAH(reg_idx), (tdba >> 32));
2376        IXGBE_WRITE_REG(hw, IXGBE_TDLEN(reg_idx),
2377                        ring->count * sizeof(union ixgbe_adv_tx_desc));
2378        IXGBE_WRITE_REG(hw, IXGBE_TDH(reg_idx), 0);
2379        IXGBE_WRITE_REG(hw, IXGBE_TDT(reg_idx), 0);
2380        ring->tail = hw->hw_addr + IXGBE_TDT(reg_idx);
2381
2382        /*
2383         * set WTHRESH to encourage burst writeback, it should not be set
2384         * higher than 1 when ITR is 0 as it could cause false TX hangs
2385         *
2386         * In order to avoid issues WTHRESH + PTHRESH should always be equal
2387         * to or less than the number of on chip descriptors, which is
2388         * currently 40.
2389         */
2390        if (!adapter->tx_itr_setting || !adapter->rx_itr_setting)
2391                txdctl |= (1 << 16);    /* WTHRESH = 1 */
2392        else
2393                txdctl |= (8 << 16);    /* WTHRESH = 8 */
2394
2395        /* PTHRESH=32 is needed to avoid a Tx hang with DFP enabled. */
2396        txdctl |= (1 << 8) |    /* HTHRESH = 1 */
2397                   32;          /* PTHRESH = 32 */
2398
2399        /* reinitialize flowdirector state */
2400        if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) &&
2401            adapter->atr_sample_rate) {
2402                ring->atr_sample_rate = adapter->atr_sample_rate;
2403                ring->atr_count = 0;
2404                set_bit(__IXGBE_TX_FDIR_INIT_DONE, &ring->state);
2405        } else {
2406                ring->atr_sample_rate = 0;
2407        }
2408
2409        clear_bit(__IXGBE_HANG_CHECK_ARMED, &ring->state);
2410
2411        /* enable queue */
2412        IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), txdctl);
2413
2414        /* TXDCTL.EN will return 0 on 82598 if link is down, so skip it */
2415        if (hw->mac.type == ixgbe_mac_82598EB &&
2416            !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2417                return;
2418
2419        /* poll to verify queue is enabled */
2420        do {
2421                usleep_range(1000, 2000);
2422                txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(reg_idx));
2423        } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
2424        if (!wait_loop)
2425                e_err(drv, "Could not enable Tx Queue %d\n", reg_idx);
2426}
2427
2428static void ixgbe_setup_mtqc(struct ixgbe_adapter *adapter)
2429{
2430        struct ixgbe_hw *hw = &adapter->hw;
2431        u32 rttdcs;
2432        u32 reg;
2433        u8 tcs = netdev_get_num_tc(adapter->netdev);
2434
2435        if (hw->mac.type == ixgbe_mac_82598EB)
2436                return;
2437
2438        /* disable the arbiter while setting MTQC */
2439        rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
2440        rttdcs |= IXGBE_RTTDCS_ARBDIS;
2441        IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
2442
2443        /* set transmit pool layout */
2444        switch (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
2445        case (IXGBE_FLAG_SRIOV_ENABLED):
2446                IXGBE_WRITE_REG(hw, IXGBE_MTQC,
2447                                (IXGBE_MTQC_VT_ENA | IXGBE_MTQC_64VF));
2448                break;
2449        default:
2450                if (!tcs)
2451                        reg = IXGBE_MTQC_64Q_1PB;
2452                else if (tcs <= 4)
2453                        reg = IXGBE_MTQC_RT_ENA | IXGBE_MTQC_4TC_4TQ;
2454                else
2455                        reg = IXGBE_MTQC_RT_ENA | IXGBE_MTQC_8TC_8TQ;
2456
2457                IXGBE_WRITE_REG(hw, IXGBE_MTQC, reg);
2458
2459                /* Enable Security TX Buffer IFG for multiple pb */
2460                if (tcs) {
2461                        reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
2462                        reg |= IXGBE_SECTX_DCB;
2463                        IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
2464                }
2465                break;
2466        }
2467
2468        /* re-enable the arbiter */
2469        rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
2470        IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
2471}
2472
2473/**
2474 * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
2475 * @adapter: board private structure
2476 *
2477 * Configure the Tx unit of the MAC after a reset.
2478 **/
2479static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
2480{
2481        struct ixgbe_hw *hw = &adapter->hw;
2482        u32 dmatxctl;
2483        u32 i;
2484
2485        ixgbe_setup_mtqc(adapter);
2486
2487        if (hw->mac.type != ixgbe_mac_82598EB) {
2488                /* DMATXCTL.EN must be before Tx queues are enabled */
2489                dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
2490                dmatxctl |= IXGBE_DMATXCTL_TE;
2491                IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
2492        }
2493
2494        /* Setup the HW Tx Head and Tail descriptor pointers */
2495        for (i = 0; i < adapter->num_tx_queues; i++)
2496                ixgbe_configure_tx_ring(adapter, adapter->tx_ring[i]);
2497}
2498
2499#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
2500
2501static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter,
2502                                   struct ixgbe_ring *rx_ring)
2503{
2504        u32 srrctl;
2505        u8 reg_idx = rx_ring->reg_idx;
2506
2507        switch (adapter->hw.mac.type) {
2508        case ixgbe_mac_82598EB: {
2509                struct ixgbe_ring_feature *feature = adapter->ring_feature;
2510                const int mask = feature[RING_F_RSS].mask;
2511                reg_idx = reg_idx & mask;
2512        }
2513                break;
2514        case ixgbe_mac_82599EB:
2515        case ixgbe_mac_X540:
2516        default:
2517                break;
2518        }
2519
2520        srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(reg_idx));
2521
2522        srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
2523        srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
2524        if (adapter->num_vfs)
2525                srrctl |= IXGBE_SRRCTL_DROP_EN;
2526
2527        srrctl |= (IXGBE_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
2528                  IXGBE_SRRCTL_BSIZEHDR_MASK;
2529
2530        if (ring_is_ps_enabled(rx_ring)) {
2531#if (PAGE_SIZE / 2) > IXGBE_MAX_RXBUFFER
2532                srrctl |= IXGBE_MAX_RXBUFFER >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2533#else
2534                srrctl |= (PAGE_SIZE / 2) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2535#endif
2536                srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
2537        } else {
2538                srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
2539                          IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2540                srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
2541        }
2542
2543        IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(reg_idx), srrctl);
2544}
2545
2546static void ixgbe_setup_mrqc(struct ixgbe_adapter *adapter)
2547{
2548        struct ixgbe_hw *hw = &adapter->hw;
2549        static const u32 seed[10] = { 0xE291D73D, 0x1805EC6C, 0x2A94B30D,
2550                          0xA54F2BEC, 0xEA49AF7C, 0xE214AD3D, 0xB855AABE,
2551                          0x6A3E67EA, 0x14364D17, 0x3BED200D};
2552        u32 mrqc = 0, reta = 0;
2553        u32 rxcsum;
2554        int i, j;
2555        u8 tcs = netdev_get_num_tc(adapter->netdev);
2556        int maxq = adapter->ring_feature[RING_F_RSS].indices;
2557
2558        if (tcs)
2559                maxq = min(maxq, adapter->num_tx_queues / tcs);
2560
2561        /* Fill out hash function seeds */
2562        for (i = 0; i < 10; i++)
2563                IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), seed[i]);
2564
2565        /* Fill out redirection table */
2566        for (i = 0, j = 0; i < 128; i++, j++) {
2567                if (j == maxq)
2568                        j = 0;
2569                /* reta = 4-byte sliding window of
2570                 * 0x00..(indices-1)(indices-1)00..etc. */
2571                reta = (reta << 8) | (j * 0x11);
2572                if ((i & 3) == 3)
2573                        IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
2574        }
2575
2576        /* Disable indicating checksum in descriptor, enables RSS hash */
2577        rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
2578        rxcsum |= IXGBE_RXCSUM_PCSD;
2579        IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
2580
2581        if (adapter->hw.mac.type == ixgbe_mac_82598EB &&
2582            (adapter->flags & IXGBE_FLAG_RSS_ENABLED)) {
2583                mrqc = IXGBE_MRQC_RSSEN;
2584        } else {
2585                int mask = adapter->flags & (IXGBE_FLAG_RSS_ENABLED
2586                                             | IXGBE_FLAG_SRIOV_ENABLED);
2587
2588                switch (mask) {
2589                case (IXGBE_FLAG_RSS_ENABLED):
2590                        if (!tcs)
2591                                mrqc = IXGBE_MRQC_RSSEN;
2592                        else if (tcs <= 4)
2593                                mrqc = IXGBE_MRQC_RTRSS4TCEN;
2594                        else
2595                                mrqc = IXGBE_MRQC_RTRSS8TCEN;
2596                        break;
2597                case (IXGBE_FLAG_SRIOV_ENABLED):
2598                        mrqc = IXGBE_MRQC_VMDQEN;
2599                        break;
2600                default:
2601                        break;
2602                }
2603        }
2604
2605        /* Perform hash on these packet types */
2606        mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4
2607              | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
2608              | IXGBE_MRQC_RSS_FIELD_IPV6
2609              | IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
2610
2611        IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
2612}
2613
2614/**
2615 * ixgbe_configure_rscctl - enable RSC for the indicated ring
2616 * @adapter:    address of board private structure
2617 * @index:      index of ring to set
2618 **/
2619static void ixgbe_configure_rscctl(struct ixgbe_adapter *adapter,
2620                                   struct ixgbe_ring *ring)
2621{
2622        struct ixgbe_hw *hw = &adapter->hw;
2623        u32 rscctrl;
2624        int rx_buf_len;
2625        u8 reg_idx = ring->reg_idx;
2626
2627        if (!ring_is_rsc_enabled(ring))
2628                return;
2629
2630        rx_buf_len = ring->rx_buf_len;
2631        rscctrl = IXGBE_READ_REG(hw, IXGBE_RSCCTL(reg_idx));
2632        rscctrl |= IXGBE_RSCCTL_RSCEN;
2633        /*
2634         * we must limit the number of descriptors so that the
2635         * total size of max desc * buf_len is not greater
2636         * than 65536
2637         */
2638        if (ring_is_ps_enabled(ring)) {
2639#if (PAGE_SIZE < 8192)
2640                rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
2641#elif (PAGE_SIZE < 16384)
2642                rscctrl |= IXGBE_RSCCTL_MAXDESC_8;
2643#elif (PAGE_SIZE < 32768)
2644                rscctrl |= IXGBE_RSCCTL_MAXDESC_4;
2645#else
2646                rscctrl |= IXGBE_RSCCTL_MAXDESC_1;
2647#endif
2648        } else {
2649                if (rx_buf_len <= IXGBE_RXBUFFER_4K)
2650                        rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
2651                else if (rx_buf_len <= IXGBE_RXBUFFER_8K)
2652                        rscctrl |= IXGBE_RSCCTL_MAXDESC_8;
2653                else
2654                        rscctrl |= IXGBE_RSCCTL_MAXDESC_4;
2655        }
2656        IXGBE_WRITE_REG(hw, IXGBE_RSCCTL(reg_idx), rscctrl);
2657}
2658
2659/**
2660 *  ixgbe_set_uta - Set unicast filter table address
2661 *  @adapter: board private structure
2662 *
2663 *  The unicast table address is a register array of 32-bit registers.
2664 *  The table is meant to be used in a way similar to how the MTA is used
2665 *  however due to certain limitations in the hardware it is necessary to
2666 *  set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
2667 *  enable bit to allow vlan tag stripping when promiscuous mode is enabled
2668 **/
2669static void ixgbe_set_uta(struct ixgbe_adapter *adapter)
2670{
2671        struct ixgbe_hw *hw = &adapter->hw;
2672        int i;
2673
2674        /* The UTA table only exists on 82599 hardware and newer */
2675        if (hw->mac.type < ixgbe_mac_82599EB)
2676                return;
2677
2678        /* we only need to do this if VMDq is enabled */
2679        if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
2680                return;
2681
2682        for (i = 0; i < 128; i++)
2683                IXGBE_WRITE_REG(hw, IXGBE_UTA(i), ~0);
2684}
2685
2686#define IXGBE_MAX_RX_DESC_POLL 10
2687static void ixgbe_rx_desc_queue_enable(struct ixgbe_adapter *adapter,
2688                                       struct ixgbe_ring *ring)
2689{
2690        struct ixgbe_hw *hw = &adapter->hw;
2691        int wait_loop = IXGBE_MAX_RX_DESC_POLL;
2692        u32 rxdctl;
2693        u8 reg_idx = ring->reg_idx;
2694
2695        /* RXDCTL.EN will return 0 on 82598 if link is down, so skip it */
2696        if (hw->mac.type == ixgbe_mac_82598EB &&
2697            !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2698                return;
2699
2700        do {
2701                usleep_range(1000, 2000);
2702                rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2703        } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
2704
2705        if (!wait_loop) {
2706                e_err(drv, "RXDCTL.ENABLE on Rx queue %d not set within "
2707                      "the polling period\n", reg_idx);
2708        }
2709}
2710
2711void ixgbe_disable_rx_queue(struct ixgbe_adapter *adapter,
2712                            struct ixgbe_ring *ring)
2713{
2714        struct ixgbe_hw *hw = &adapter->hw;
2715        int wait_loop = IXGBE_MAX_RX_DESC_POLL;
2716        u32 rxdctl;
2717        u8 reg_idx = ring->reg_idx;
2718
2719        rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2720        rxdctl &= ~IXGBE_RXDCTL_ENABLE;
2721
2722        /* write value back with RXDCTL.ENABLE bit cleared */
2723        IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
2724
2725        if (hw->mac.type == ixgbe_mac_82598EB &&
2726            !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2727                return;
2728
2729        /* the hardware may take up to 100us to really disable the rx queue */
2730        do {
2731                udelay(10);
2732                rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2733        } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
2734
2735        if (!wait_loop) {
2736                e_err(drv, "RXDCTL.ENABLE on Rx queue %d not cleared within "
2737                      "the polling period\n", reg_idx);
2738        }
2739}
2740
2741void ixgbe_configure_rx_ring(struct ixgbe_adapter *adapter,
2742                             struct ixgbe_ring *ring)
2743{
2744        struct ixgbe_hw *hw = &adapter->hw;
2745        u64 rdba = ring->dma;
2746        u32 rxdctl;
2747        u8 reg_idx = ring->reg_idx;
2748
2749        /* disable queue to avoid issues while updating state */
2750        rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2751        ixgbe_disable_rx_queue(adapter, ring);
2752
2753        IXGBE_WRITE_REG(hw, IXGBE_RDBAL(reg_idx), (rdba & DMA_BIT_MASK(32)));
2754        IXGBE_WRITE_REG(hw, IXGBE_RDBAH(reg_idx), (rdba >> 32));
2755        IXGBE_WRITE_REG(hw, IXGBE_RDLEN(reg_idx),
2756                        ring->count * sizeof(union ixgbe_adv_rx_desc));
2757        IXGBE_WRITE_REG(hw, IXGBE_RDH(reg_idx), 0);
2758        IXGBE_WRITE_REG(hw, IXGBE_RDT(reg_idx), 0);
2759        ring->tail = hw->hw_addr + IXGBE_RDT(reg_idx);
2760
2761        ixgbe_configure_srrctl(adapter, ring);
2762        ixgbe_configure_rscctl(adapter, ring);
2763
2764        /* If operating in IOV mode set RLPML for X540 */
2765        if ((adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) &&
2766            hw->mac.type == ixgbe_mac_X540) {
2767                rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
2768                rxdctl |= ((ring->netdev->mtu + ETH_HLEN +
2769                            ETH_FCS_LEN + VLAN_HLEN) | IXGBE_RXDCTL_RLPML_EN);
2770        }
2771
2772        if (hw->mac.type == ixgbe_mac_82598EB) {
2773                /*
2774                 * enable cache line friendly hardware writes:
2775                 * PTHRESH=32 descriptors (half the internal cache),
2776                 * this also removes ugly rx_no_buffer_count increment
2777                 * HTHRESH=4 descriptors (to minimize latency on fetch)
2778                 * WTHRESH=8 burst writeback up to two cache lines
2779                 */
2780                rxdctl &= ~0x3FFFFF;
2781                rxdctl |=  0x080420;
2782        }
2783
2784        /* enable receive descriptor ring */
2785        rxdctl |= IXGBE_RXDCTL_ENABLE;
2786        IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
2787
2788        ixgbe_rx_desc_queue_enable(adapter, ring);
2789        ixgbe_alloc_rx_buffers(ring, ixgbe_desc_unused(ring));
2790}
2791
2792static void ixgbe_setup_psrtype(struct ixgbe_adapter *adapter)
2793{
2794        struct ixgbe_hw *hw = &adapter->hw;
2795        int p;
2796
2797        /* PSRTYPE must be initialized in non 82598 adapters */
2798        u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
2799                      IXGBE_PSRTYPE_UDPHDR |
2800                      IXGBE_PSRTYPE_IPV4HDR |
2801                      IXGBE_PSRTYPE_L2HDR |
2802                      IXGBE_PSRTYPE_IPV6HDR;
2803
2804        if (hw->mac.type == ixgbe_mac_82598EB)
2805                return;
2806
2807        if (adapter->flags & IXGBE_FLAG_RSS_ENABLED)
2808                psrtype |= (adapter->num_rx_queues_per_pool << 29);
2809
2810        for (p = 0; p < adapter->num_rx_pools; p++)
2811                IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(adapter->num_vfs + p),
2812                                psrtype);
2813}
2814
2815static void ixgbe_configure_virtualization(struct ixgbe_adapter *adapter)
2816{
2817        struct ixgbe_hw *hw = &adapter->hw;
2818        u32 gcr_ext;
2819        u32 vt_reg_bits;
2820        u32 reg_offset, vf_shift;
2821        u32 vmdctl;
2822        int i;
2823
2824        if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
2825                return;
2826
2827        vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
2828        vt_reg_bits = IXGBE_VMD_CTL_VMDQ_EN | IXGBE_VT_CTL_REPLEN;
2829        vt_reg_bits |= (adapter->num_vfs << IXGBE_VT_CTL_POOL_SHIFT);
2830        IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl | vt_reg_bits);
2831
2832        vf_shift = adapter->num_vfs % 32;
2833        reg_offset = (adapter->num_vfs >= 32) ? 1 : 0;
2834
2835        /* Enable only the PF's pool for Tx/Rx */
2836        IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), (1 << vf_shift));
2837        IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset ^ 1), 0);
2838        IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), (1 << vf_shift));
2839        IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset ^ 1), 0);
2840        IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
2841
2842        /* Map PF MAC address in RAR Entry 0 to first pool following VFs */
2843        hw->mac.ops.set_vmdq(hw, 0, adapter->num_vfs);
2844
2845        /*
2846         * Set up VF register offsets for selected VT Mode,
2847         * i.e. 32 or 64 VFs for SR-IOV
2848         */
2849        gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
2850        gcr_ext |= IXGBE_GCR_EXT_MSIX_EN;
2851        gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
2852        IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
2853
2854        /* enable Tx loopback for VF/PF communication */
2855        IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
2856        /* Enable MAC Anti-Spoofing */
2857        hw->mac.ops.set_mac_anti_spoofing(hw,
2858                                           (adapter->num_vfs != 0),
2859                                          adapter->num_vfs);
2860        /* For VFs that have spoof checking turned off */
2861        for (i = 0; i < adapter->num_vfs; i++) {
2862                if (!adapter->vfinfo[i].spoofchk_enabled)
2863                        ixgbe_ndo_set_vf_spoofchk(adapter->netdev, i, false);
2864        }
2865}
2866
2867static void ixgbe_set_rx_buffer_len(struct ixgbe_adapter *adapter)
2868{
2869        struct ixgbe_hw *hw = &adapter->hw;
2870        struct net_device *netdev = adapter->netdev;
2871        int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
2872        int rx_buf_len;
2873        struct ixgbe_ring *rx_ring;
2874        int i;
2875        u32 mhadd, hlreg0;
2876
2877        /* Decide whether to use packet split mode or not */
2878        /* On by default */
2879        adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
2880
2881        /* Do not use packet split if we're in SR-IOV Mode */
2882        if (adapter->num_vfs)
2883                adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
2884
2885        /* Disable packet split due to 82599 erratum #45 */
2886        if (hw->mac.type == ixgbe_mac_82599EB)
2887                adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
2888
2889#ifdef IXGBE_FCOE
2890        /* adjust max frame to be able to do baby jumbo for FCoE */
2891        if ((adapter->flags & IXGBE_FLAG_FCOE_ENABLED) &&
2892            (max_frame < IXGBE_FCOE_JUMBO_FRAME_SIZE))
2893                max_frame = IXGBE_FCOE_JUMBO_FRAME_SIZE;
2894
2895#endif /* IXGBE_FCOE */
2896        mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
2897        if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
2898                mhadd &= ~IXGBE_MHADD_MFS_MASK;
2899                mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
2900
2901                IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
2902        }
2903
2904        /* MHADD will allow an extra 4 bytes past for vlan tagged frames */
2905        max_frame += VLAN_HLEN;
2906
2907        /* Set the RX buffer length according to the mode */
2908        if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
2909                rx_buf_len = IXGBE_RX_HDR_SIZE;
2910        } else {
2911                if (!(adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED) &&
2912                    (netdev->mtu <= ETH_DATA_LEN))
2913                        rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
2914                /*
2915                 * Make best use of allocation by using all but 1K of a
2916                 * power of 2 allocation that will be used for skb->head.
2917                 */
2918                else if (max_frame <= IXGBE_RXBUFFER_3K)
2919                        rx_buf_len = IXGBE_RXBUFFER_3K;
2920                else if (max_frame <= IXGBE_RXBUFFER_7K)
2921                        rx_buf_len = IXGBE_RXBUFFER_7K;
2922                else if (max_frame <= IXGBE_RXBUFFER_15K)
2923                        rx_buf_len = IXGBE_RXBUFFER_15K;
2924                else
2925                        rx_buf_len = IXGBE_MAX_RXBUFFER;
2926        }
2927
2928        hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2929        /* set jumbo enable since MHADD.MFS is keeping size locked at max_frame */
2930        hlreg0 |= IXGBE_HLREG0_JUMBOEN;
2931        IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
2932
2933        /*
2934         * Setup the HW Rx Head and Tail Descriptor Pointers and
2935         * the Base and Length of the Rx Descriptor Ring
2936         */
2937        for (i = 0; i < adapter->num_rx_queues; i++) {
2938                rx_ring = adapter->rx_ring[i];
2939                rx_ring->rx_buf_len = rx_buf_len;
2940
2941                if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)
2942                        set_ring_ps_enabled(rx_ring);
2943                else
2944                        clear_ring_ps_enabled(rx_ring);
2945
2946                if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
2947                        set_ring_rsc_enabled(rx_ring);
2948                else
2949                        clear_ring_rsc_enabled(rx_ring);
2950
2951#ifdef IXGBE_FCOE
2952                if (netdev->features & NETIF_F_FCOE_MTU) {
2953                        struct ixgbe_ring_feature *f;
2954                        f = &adapter->ring_feature[RING_F_FCOE];
2955                        if ((i >= f->mask) && (i < f->mask + f->indices)) {
2956                                clear_ring_ps_enabled(rx_ring);
2957                                if (rx_buf_len < IXGBE_FCOE_JUMBO_FRAME_SIZE)
2958                                        rx_ring->rx_buf_len =
2959                                                IXGBE_FCOE_JUMBO_FRAME_SIZE;
2960                        } else if (!ring_is_rsc_enabled(rx_ring) &&
2961                                   !ring_is_ps_enabled(rx_ring)) {
2962                                rx_ring->rx_buf_len =
2963                                                IXGBE_FCOE_JUMBO_FRAME_SIZE;
2964                        }
2965                }
2966#endif /* IXGBE_FCOE */
2967        }
2968}
2969
2970static void ixgbe_setup_rdrxctl(struct ixgbe_adapter *adapter)
2971{
2972        struct ixgbe_hw *hw = &adapter->hw;
2973        u32 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
2974
2975        switch (hw->mac.type) {
2976        case ixgbe_mac_82598EB:
2977                /*
2978                 * For VMDq support of different descriptor types or
2979                 * buffer sizes through the use of multiple SRRCTL
2980                 * registers, RDRXCTL.MVMEN must be set to 1
2981                 *
2982                 * also, the manual doesn't mention it clearly but DCA hints
2983                 * will only use queue 0's tags unless this bit is set.  Side
2984                 * effects of setting this bit are only that SRRCTL must be
2985                 * fully programmed [0..15]
2986                 */
2987                rdrxctl |= IXGBE_RDRXCTL_MVMEN;
2988                break;
2989        case ixgbe_mac_82599EB:
2990        case ixgbe_mac_X540:
2991                /* Disable RSC for ACK packets */
2992                IXGBE_WRITE_REG(hw, IXGBE_RSCDBU,
2993                   (IXGBE_RSCDBU_RSCACKDIS | IXGBE_READ_REG(hw, IXGBE_RSCDBU)));
2994                rdrxctl &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
2995                /* hardware requires some bits to be set by default */
2996                rdrxctl |= (IXGBE_RDRXCTL_RSCACKC | IXGBE_RDRXCTL_FCOE_WRFIX);
2997                rdrxctl |= IXGBE_RDRXCTL_CRCSTRIP;
2998                break;
2999        default:
3000                /* We should do nothing since we don't know this hardware */
3001                return;
3002        }
3003
3004        IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
3005}
3006
3007/**
3008 * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
3009 * @adapter: board private structure
3010 *
3011 * Configure the Rx unit of the MAC after a reset.
3012 **/
3013static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
3014{
3015        struct ixgbe_hw *hw = &adapter->hw;
3016        int i;
3017        u32 rxctrl;
3018
3019        /* disable receives while setting up the descriptors */
3020        rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
3021        IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
3022
3023        ixgbe_setup_psrtype(adapter);
3024        ixgbe_setup_rdrxctl(adapter);
3025
3026        /* Program registers for the distribution of queues */
3027        ixgbe_setup_mrqc(adapter);
3028
3029        ixgbe_set_uta(adapter);
3030
3031        /* set_rx_buffer_len must be called before ring initialization */
3032        ixgbe_set_rx_buffer_len(adapter);
3033
3034        /*
3035         * Setup the HW Rx Head and Tail Descriptor Pointers and
3036         * the Base and Length of the Rx Descriptor Ring
3037         */
3038        for (i = 0; i < adapter->num_rx_queues; i++)
3039                ixgbe_configure_rx_ring(adapter, adapter->rx_ring[i]);
3040
3041        /* disable drop enable for 82598 parts */
3042        if (hw->mac.type == ixgbe_mac_82598EB)
3043                rxctrl |= IXGBE_RXCTRL_DMBYPS;
3044
3045        /* enable all receives */
3046        rxctrl |= IXGBE_RXCTRL_RXEN;
3047        hw->mac.ops.enable_rx_dma(hw, rxctrl);
3048}
3049
3050static int ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
3051{
3052        struct ixgbe_adapter *adapter = netdev_priv(netdev);
3053        struct ixgbe_hw *hw = &adapter->hw;
3054        int pool_ndx = adapter->num_vfs;
3055
3056        /* add VID to filter table */
3057        hw->mac.ops.set_vfta(&adapter->hw, vid, pool_ndx, true);
3058        set_bit(vid, adapter->active_vlans);
3059
3060        return 0;
3061}
3062
3063static int ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
3064{
3065        struct ixgbe_adapter *adapter = netdev_priv(netdev);
3066        struct ixgbe_hw *hw = &adapter->hw;
3067        int pool_ndx = adapter->num_vfs;
3068
3069        /* remove VID from filter table */
3070        hw->mac.ops.set_vfta(&adapter->hw, vid, pool_ndx, false);
3071        clear_bit(vid, adapter->active_vlans);
3072
3073        return 0;
3074}
3075
3076/**
3077 * ixgbe_vlan_filter_disable - helper to disable hw vlan filtering
3078 * @adapter: driver data
3079 */
3080static void ixgbe_vlan_filter_disable(struct ixgbe_adapter *adapter)
3081{
3082        struct ixgbe_hw *hw = &adapter->hw;
3083        u32 vlnctrl;
3084
3085        vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3086        vlnctrl &= ~(IXGBE_VLNCTRL_VFE | IXGBE_VLNCTRL_CFIEN);
3087        IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3088}
3089
3090/**
3091 * ixgbe_vlan_filter_enable - helper to enable hw vlan filtering
3092 * @adapter: driver data
3093 */
3094static void ixgbe_vlan_filter_enable(struct ixgbe_adapter *adapter)
3095{
3096        struct ixgbe_hw *hw = &adapter->hw;
3097        u32 vlnctrl;
3098
3099        vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3100        vlnctrl |= IXGBE_VLNCTRL_VFE;
3101        vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
3102        IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3103}
3104
3105/**
3106 * ixgbe_vlan_strip_disable - helper to disable hw vlan stripping
3107 * @adapter: driver data
3108 */
3109static void ixgbe_vlan_strip_disable(struct ixgbe_adapter *adapter)
3110{
3111        struct ixgbe_hw *hw = &adapter->hw;
3112        u32 vlnctrl;
3113        int i, j;
3114
3115        switch (hw->mac.type) {
3116        case ixgbe_mac_82598EB:
3117                vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3118                vlnctrl &= ~IXGBE_VLNCTRL_VME;
3119                IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3120                break;
3121        case ixgbe_mac_82599EB:
3122        case ixgbe_mac_X540:
3123                for (i = 0; i < adapter->num_rx_queues; i++) {
3124                        j = adapter->rx_ring[i]->reg_idx;
3125                        vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
3126                        vlnctrl &= ~IXGBE_RXDCTL_VME;
3127                        IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
3128                }
3129                break;
3130        default:
3131                break;
3132        }
3133}
3134
3135/**
3136 * ixgbe_vlan_strip_enable - helper to enable hw vlan stripping
3137 * @adapter: driver data
3138 */
3139static void ixgbe_vlan_strip_enable(struct ixgbe_adapter *adapter)
3140{
3141        struct ixgbe_hw *hw = &adapter->hw;
3142        u32 vlnctrl;
3143        int i, j;
3144
3145        switch (hw->mac.type) {
3146        case ixgbe_mac_82598EB:
3147                vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3148                vlnctrl |= IXGBE_VLNCTRL_VME;
3149                IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3150                break;
3151        case ixgbe_mac_82599EB:
3152        case ixgbe_mac_X540:
3153                for (i = 0; i < adapter->num_rx_queues; i++) {
3154                        j = adapter->rx_ring[i]->reg_idx;
3155                        vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
3156                        vlnctrl |= IXGBE_RXDCTL_VME;
3157                        IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
3158                }
3159                break;
3160        default:
3161                break;
3162        }
3163}
3164
3165static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
3166{
3167        u16 vid;
3168
3169        ixgbe_vlan_rx_add_vid(adapter->netdev, 0);
3170
3171        for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
3172                ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
3173}
3174
3175/**
3176 * ixgbe_write_uc_addr_list - write unicast addresses to RAR table
3177 * @netdev: network interface device structure
3178 *
3179 * Writes unicast address list to the RAR table.
3180 * Returns: -ENOMEM on failure/insufficient address space
3181 *                0 on no addresses written
3182 *                X on writing X addresses to the RAR table
3183 **/
3184static int ixgbe_write_uc_addr_list(struct net_device *netdev)
3185{
3186        struct ixgbe_adapter *adapter = netdev_priv(netdev);
3187        struct ixgbe_hw *hw = &adapter->hw;
3188        unsigned int vfn = adapter->num_vfs;
3189        unsigned int rar_entries = IXGBE_MAX_PF_MACVLANS;
3190        int count = 0;
3191
3192        /* return ENOMEM indicating insufficient memory for addresses */
3193        if (netdev_uc_count(netdev) > rar_entries)
3194                return -ENOMEM;
3195
3196        if (!netdev_uc_empty(netdev) && rar_entries) {
3197                struct netdev_hw_addr *ha;
3198                /* return error if we do not support writing to RAR table */
3199                if (!hw->mac.ops.set_rar)
3200                        return -ENOMEM;
3201
3202                netdev_for_each_uc_addr(ha, netdev) {
3203                        if (!rar_entries)
3204                                break;
3205                        hw->mac.ops.set_rar(hw, rar_entries--, ha->addr,
3206                                            vfn, IXGBE_RAH_AV);
3207                        count++;
3208                }
3209        }
3210        /* write the addresses in reverse order to avoid write combining */
3211        for (; rar_entries > 0 ; rar_entries--)
3212                hw->mac.ops.clear_rar(hw, rar_entries);
3213
3214        return count;
3215}
3216
3217/**
3218 * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
3219 * @netdev: network interface device structure
3220 *
3221 * The set_rx_method entry point is called whenever the unicast/multicast
3222 * address list or the network interface flags are updated.  This routine is
3223 * responsible for configuring the hardware for proper unicast, multicast and
3224 * promiscuous mode.
3225 **/
3226void ixgbe_set_rx_mode(struct net_device *netdev)
3227{
3228        struct ixgbe_adapter *adapter = netdev_priv(netdev);
3229        struct ixgbe_hw *hw = &adapter->hw;
3230        u32 fctrl, vmolr = IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE;
3231        int count;
3232
3233        /* Check for Promiscuous and All Multicast modes */
3234
3235        fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3236
3237        /* set all bits that we expect to always be set */
3238        fctrl |= IXGBE_FCTRL_BAM;
3239        fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
3240        fctrl |= IXGBE_FCTRL_PMCF;
3241
3242        /* clear the bits we are changing the status of */
3243        fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3244
3245        if (netdev->flags & IFF_PROMISC) {
3246                hw->addr_ctrl.user_set_promisc = true;
3247                fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3248                vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_MPE);
3249                /* don't hardware filter vlans in promisc mode */
3250                ixgbe_vlan_filter_disable(adapter);
3251        } else {
3252                if (netdev->flags & IFF_ALLMULTI) {
3253                        fctrl |= IXGBE_FCTRL_MPE;
3254                        vmolr |= IXGBE_VMOLR_MPE;
3255                } else {
3256                        /*
3257                         * Write addresses to the MTA, if the attempt fails
3258                         * then we should just turn on promiscuous mode so
3259                         * that we can at least receive multicast traffic
3260                         */
3261                        hw->mac.ops.update_mc_addr_list(hw, netdev);
3262                        vmolr |= IXGBE_VMOLR_ROMPE;
3263                }
3264                ixgbe_vlan_filter_enable(adapter);
3265                hw->addr_ctrl.user_set_promisc = false;
3266                /*
3267                 * Write addresses to available RAR registers, if there is not
3268                 * sufficient space to store all the addresses then enable
3269                 * unicast promiscuous mode
3270                 */
3271                count = ixgbe_write_uc_addr_list(netdev);
3272                if (count < 0) {
3273                        fctrl |= IXGBE_FCTRL_UPE;
3274                        vmolr |= IXGBE_VMOLR_ROPE;
3275                }
3276        }
3277
3278        if (adapter->num_vfs) {
3279                ixgbe_restore_vf_multicasts(adapter);
3280                vmolr |= IXGBE_READ_REG(hw, IXGBE_VMOLR(adapter->num_vfs)) &
3281                         ~(IXGBE_VMOLR_MPE | IXGBE_VMOLR_ROMPE |
3282                           IXGBE_VMOLR_ROPE);
3283                IXGBE_WRITE_REG(hw, IXGBE_VMOLR(adapter->num_vfs), vmolr);
3284        }
3285
3286        IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3287
3288        if (netdev->features & NETIF_F_HW_VLAN_RX)
3289                ixgbe_vlan_strip_enable(adapter);
3290        else
3291                ixgbe_vlan_strip_disable(adapter);
3292}
3293
3294static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
3295{
3296        int q_idx;
3297        struct ixgbe_q_vector *q_vector;
3298        int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3299
3300        /* legacy and MSI only use one vector */
3301        if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3302                q_vectors = 1;
3303
3304        for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3305                q_vector = adapter->q_vector[q_idx];
3306                napi_enable(&q_vector->napi);
3307        }
3308}
3309
3310static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
3311{
3312        int q_idx;
3313        struct ixgbe_q_vector *q_vector;
3314        int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3315
3316        /* legacy and MSI only use one vector */
3317        if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3318                q_vectors = 1;
3319
3320        for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3321                q_vector = adapter->q_vector[q_idx];
3322                napi_disable(&q_vector->napi);
3323        }
3324}
3325
3326#ifdef CONFIG_IXGBE_DCB
3327/*
3328 * ixgbe_configure_dcb - Configure DCB hardware
3329 * @adapter: ixgbe adapter struct
3330 *
3331 * This is called by the driver on open to configure the DCB hardware.
3332 * This is also called by the gennetlink interface when reconfiguring
3333 * the DCB state.
3334 */
3335static void ixgbe_configure_dcb(struct ixgbe_adapter *adapter)
3336{
3337        struct ixgbe_hw *hw = &adapter->hw;
3338        int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
3339
3340        if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED)) {
3341                if (hw->mac.type == ixgbe_mac_82598EB)
3342                        netif_set_gso_max_size(adapter->netdev, 65536);
3343                return;
3344        }
3345
3346        if (hw->mac.type == ixgbe_mac_82598EB)
3347                netif_set_gso_max_size(adapter->netdev, 32768);
3348
3349
3350        /* Enable VLAN tag insert/strip */
3351        adapter->netdev->features |= NETIF_F_HW_VLAN_RX;
3352
3353        hw->mac.ops.set_vfta(&adapter->hw, 0, 0, true);
3354
3355#ifdef IXGBE_FCOE
3356        if (adapter->netdev->features & NETIF_F_FCOE_MTU)
3357                max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
3358#endif
3359
3360        /* reconfigure the hardware */
3361        if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE) {
3362                ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
3363                                                DCB_TX_CONFIG);
3364                ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
3365                                                DCB_RX_CONFIG);
3366                ixgbe_dcb_hw_config(hw, &adapter->dcb_cfg);
3367        } else if (adapter->ixgbe_ieee_ets && adapter->ixgbe_ieee_pfc) {
3368                ixgbe_dcb_hw_ets(&adapter->hw,
3369                                 adapter->ixgbe_ieee_ets,
3370                                 max_frame);
3371                ixgbe_dcb_hw_pfc_config(&adapter->hw,
3372                                        adapter->ixgbe_ieee_pfc->pfc_en,
3373                                        adapter->ixgbe_ieee_ets->prio_tc);
3374        }
3375
3376        /* Enable RSS Hash per TC */
3377        if (hw->mac.type != ixgbe_mac_82598EB) {
3378                int i;
3379                u32 reg = 0;
3380
3381                for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
3382                        u8 msb = 0;
3383                        u8 cnt = adapter->netdev->tc_to_txq[i].count;
3384
3385                        while (cnt >>= 1)
3386                                msb++;
3387
3388                        reg |= msb << IXGBE_RQTC_SHIFT_TC(i);
3389                }
3390                IXGBE_WRITE_REG(hw, IXGBE_RQTC, reg);
3391        }
3392}
3393#endif
3394
3395/* Additional bittime to account for IXGBE framing */
3396#define IXGBE_ETH_FRAMING 20
3397
3398/*
3399 * ixgbe_hpbthresh - calculate high water mark for flow control
3400 *
3401 * @adapter: board private structure to calculate for
3402 * @pb - packet buffer to calculate
3403 */
3404static int ixgbe_hpbthresh(struct ixgbe_adapter *adapter, int pb)
3405{
3406        struct ixgbe_hw *hw = &adapter->hw;
3407        struct net_device *dev = adapter->netdev;
3408        int link, tc, kb, marker;
3409        u32 dv_id, rx_pba;
3410
3411        /* Calculate max LAN frame size */
3412        tc = link = dev->mtu + ETH_HLEN + ETH_FCS_LEN + IXGBE_ETH_FRAMING;
3413
3414#ifdef IXGBE_FCOE
3415        /* FCoE traffic class uses FCOE jumbo frames */
3416        if (dev->features & NETIF_F_FCOE_MTU) {
3417                int fcoe_pb = 0;
3418
3419#ifdef CONFIG_IXGBE_DCB
3420                fcoe_pb = netdev_get_prio_tc_map(dev, adapter->fcoe.up);
3421
3422#endif
3423                if (fcoe_pb == pb && tc < IXGBE_FCOE_JUMBO_FRAME_SIZE)
3424                        tc = IXGBE_FCOE_JUMBO_FRAME_SIZE;
3425        }
3426#endif
3427
3428        /* Calculate delay value for device */
3429        switch (hw->mac.type) {
3430        case ixgbe_mac_X540:
3431                dv_id = IXGBE_DV_X540(link, tc);
3432                break;
3433        default:
3434                dv_id = IXGBE_DV(link, tc);
3435                break;
3436        }
3437
3438        /* Loopback switch introduces additional latency */
3439        if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
3440                dv_id += IXGBE_B2BT(tc);
3441
3442        /* Delay value is calculated in bit times convert to KB */
3443        kb = IXGBE_BT2KB(dv_id);
3444        rx_pba = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(pb)) >> 10;
3445
3446        marker = rx_pba - kb;
3447
3448        /* It is possible that the packet buffer is not large enough
3449         * to provide required headroom. In this case throw an error
3450         * to user and a do the best we can.
3451         */
3452        if (marker < 0) {
3453                e_warn(drv, "Packet Buffer(%i) can not provide enough"
3454                            "headroom to support flow control."
3455                            "Decrease MTU or number of traffic classes\n", pb);
3456                marker = tc + 1;
3457        }
3458
3459        return marker;
3460}
3461
3462/*
3463 * ixgbe_lpbthresh - calculate low water mark for for flow control
3464 *
3465 * @adapter: board private structure to calculate for
3466 * @pb - packet buffer to calculate
3467 */
3468static int ixgbe_lpbthresh(struct ixgbe_adapter *adapter)
3469{
3470        struct ixgbe_hw *hw = &adapter->hw;
3471        struct net_device *dev = adapter->netdev;
3472        int tc;
3473        u32 dv_id;
3474
3475        /* Calculate max LAN frame size */
3476        tc = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
3477
3478        /* Calculate delay value for device */
3479        switch (hw->mac.type) {
3480        case ixgbe_mac_X540:
3481                dv_id = IXGBE_LOW_DV_X540(tc);
3482                break;
3483        default:
3484                dv_id = IXGBE_LOW_DV(tc);
3485                break;
3486        }
3487
3488        /* Delay value is calculated in bit times convert to KB */
3489        return IXGBE_BT2KB(dv_id);
3490}
3491
3492/*
3493 * ixgbe_pbthresh_setup - calculate and setup high low water marks
3494 */
3495static void ixgbe_pbthresh_setup(struct ixgbe_adapter *adapter)
3496{
3497        struct ixgbe_hw *hw = &adapter->hw;
3498        int num_tc = netdev_get_num_tc(adapter->netdev);
3499        int i;
3500
3501        if (!num_tc)
3502                num_tc = 1;
3503
3504        hw->fc.low_water = ixgbe_lpbthresh(adapter);
3505
3506        for (i = 0; i < num_tc; i++) {
3507                hw->fc.high_water[i] = ixgbe_hpbthresh(adapter, i);
3508
3509                /* Low water marks must not be larger than high water marks */
3510                if (hw->fc.low_water > hw->fc.high_water[i])
3511                        hw->fc.low_water = 0;
3512        }
3513}
3514
3515static void ixgbe_configure_pb(struct ixgbe_adapter *adapter)
3516{
3517        struct ixgbe_hw *hw = &adapter->hw;
3518        int hdrm;
3519        u8 tc = netdev_get_num_tc(adapter->netdev);
3520
3521        if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
3522            adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
3523                hdrm = 32 << adapter->fdir_pballoc;
3524        else
3525                hdrm = 0;
3526
3527        hw->mac.ops.set_rxpba(hw, tc, hdrm, PBA_STRATEGY_EQUAL);
3528        ixgbe_pbthresh_setup(adapter);
3529}
3530
3531static void ixgbe_fdir_filter_restore(struct ixgbe_adapter *adapter)
3532{
3533        struct ixgbe_hw *hw = &adapter->hw;
3534        struct hlist_node *node, *node2;
3535        struct ixgbe_fdir_filter *filter;
3536
3537        spin_lock(&adapter->fdir_perfect_lock);
3538
3539        if (!hlist_empty(&adapter->fdir_filter_list))
3540                ixgbe_fdir_set_input_mask_82599(hw, &adapter->fdir_mask);
3541
3542        hlist_for_each_entry_safe(filter, node, node2,
3543                                  &adapter->fdir_filter_list, fdir_node) {
3544                ixgbe_fdir_write_perfect_filter_82599(hw,
3545                                &filter->filter,
3546                                filter->sw_idx,
3547                                (filter->action == IXGBE_FDIR_DROP_QUEUE) ?
3548                                IXGBE_FDIR_DROP_QUEUE :
3549                                adapter->rx_ring[filter->action]->reg_idx);
3550        }
3551
3552        spin_unlock(&adapter->fdir_perfect_lock);
3553}
3554
3555static void ixgbe_configure(struct ixgbe_adapter *adapter)
3556{
3557        ixgbe_configure_pb(adapter);
3558#ifdef CONFIG_IXGBE_DCB
3559        ixgbe_configure_dcb(adapter);
3560#endif
3561
3562        ixgbe_set_rx_mode(adapter->netdev);
3563        ixgbe_restore_vlan(adapter);
3564
3565#ifdef IXGBE_FCOE
3566        if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
3567                ixgbe_configure_fcoe(adapter);
3568
3569#endif /* IXGBE_FCOE */
3570        if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
3571                ixgbe_init_fdir_signature_82599(&adapter->hw,
3572                                                adapter->fdir_pballoc);
3573        } else if (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE) {
3574                ixgbe_init_fdir_perfect_82599(&adapter->hw,
3575                                              adapter->fdir_pballoc);
3576                ixgbe_fdir_filter_restore(adapter);
3577        }
3578
3579        ixgbe_configure_virtualization(adapter);
3580
3581        ixgbe_configure_tx(adapter);
3582        ixgbe_configure_rx(adapter);
3583}
3584
3585static inline bool ixgbe_is_sfp(struct ixgbe_hw *hw)
3586{
3587        switch (hw->phy.type) {
3588        case ixgbe_phy_sfp_avago:
3589        case ixgbe_phy_sfp_ftl:
3590        case ixgbe_phy_sfp_intel:
3591        case ixgbe_phy_sfp_unknown:
3592        case ixgbe_phy_sfp_passive_tyco:
3593        case ixgbe_phy_sfp_passive_unknown:
3594        case ixgbe_phy_sfp_active_unknown:
3595        case ixgbe_phy_sfp_ftl_active:
3596                return true;
3597        case ixgbe_phy_nl:
3598                if (hw->mac.type == ixgbe_mac_82598EB)
3599                        return true;
3600        default:
3601                return false;
3602        }
3603}
3604
3605/**
3606 * ixgbe_sfp_link_config - set up SFP+ link
3607 * @adapter: pointer to private adapter struct
3608 **/
3609static void ixgbe_sfp_link_config(struct ixgbe_adapter *adapter)
3610{
3611        /*
3612         * We are assuming the worst case scenario here, and that
3613         * is that an SFP was inserted/removed after the reset
3614         * but before SFP detection was enabled.  As such the best
3615         * solution is to just start searching as soon as we start
3616         */
3617        if (adapter->hw.mac.type == ixgbe_mac_82598EB)
3618                adapter->flags2 |= IXGBE_FLAG2_SEARCH_FOR_SFP;
3619
3620        adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
3621}
3622
3623/**
3624 * ixgbe_non_sfp_link_config - set up non-SFP+ link
3625 * @hw: pointer to private hardware struct
3626 *
3627 * Returns 0 on success, negative on failure
3628 **/
3629static int ixgbe_non_sfp_link_config(struct ixgbe_hw *hw)
3630{
3631        u32 autoneg;
3632        bool negotiation, link_up = false;
3633        u32 ret = IXGBE_ERR_LINK_SETUP;
3634
3635        if (hw->mac.ops.check_link)
3636                ret = hw->mac.ops.check_link(hw, &autoneg, &link_up, false);
3637
3638        if (ret)
3639                goto link_cfg_out;
3640
3641        autoneg = hw->phy.autoneg_advertised;
3642        if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
3643                ret = hw->mac.ops.get_link_capabilities(hw, &autoneg,
3644                                                        &negotiation);
3645        if (ret)
3646                goto link_cfg_out;
3647
3648        if (hw->mac.ops.setup_link)
3649                ret = hw->mac.ops.setup_link(hw, autoneg, negotiation, link_up);
3650link_cfg_out:
3651        return ret;
3652}
3653
3654static void ixgbe_setup_gpie(struct ixgbe_adapter *adapter)
3655{
3656        struct ixgbe_hw *hw = &adapter->hw;
3657        u32 gpie = 0;
3658
3659        if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3660                gpie = IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_PBA_SUPPORT |
3661                       IXGBE_GPIE_OCD;
3662                gpie |= IXGBE_GPIE_EIAME;
3663                /*
3664                 * use EIAM to auto-mask when MSI-X interrupt is asserted
3665                 * this saves a register write for every interrupt
3666                 */
3667                switch (hw->mac.type) {
3668                case ixgbe_mac_82598EB:
3669                        IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3670                        break;
3671                case ixgbe_mac_82599EB:
3672                case ixgbe_mac_X540:
3673                default:
3674                        IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
3675                        IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
3676                        break;
3677                }
3678        } else {
3679                /* legacy interrupts, use EIAM to auto-mask when reading EICR,
3680                 * specifically only auto mask tx and rx interrupts */
3681                IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3682        }
3683
3684        /* XXX: to interrupt immediately for EICS writes, enable this */
3685        /* gpie |= IXGBE_GPIE_EIMEN; */
3686
3687        if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
3688                gpie &= ~IXGBE_GPIE_VTMODE_MASK;
3689                gpie |= IXGBE_GPIE_VTMODE_64;
3690        }
3691
3692        /* Enable Thermal over heat sensor interrupt */
3693        if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) {
3694                switch (adapter->hw.mac.type) {
3695                case ixgbe_mac_82599EB:
3696                        gpie |= IXGBE_SDP0_GPIEN;
3697                        break;
3698                case ixgbe_mac_X540:
3699                        gpie |= IXGBE_EIMS_TS;
3700                        break;
3701                default:
3702                        break;
3703                }
3704        }
3705
3706        /* Enable fan failure interrupt */
3707        if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
3708                gpie |= IXGBE_SDP1_GPIEN;
3709
3710        if (hw->mac.type == ixgbe_mac_82599EB) {
3711                gpie |= IXGBE_SDP1_GPIEN;
3712                gpie |= IXGBE_SDP2_GPIEN;
3713        }
3714
3715        IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
3716}
3717
3718static void ixgbe_up_complete(struct ixgbe_adapter *adapter)
3719{
3720        struct ixgbe_hw *hw = &adapter->hw;
3721        int err;
3722        u32 ctrl_ext;
3723
3724        ixgbe_get_hw_control(adapter);
3725        ixgbe_setup_gpie(adapter);
3726
3727        if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
3728                ixgbe_configure_msix(adapter);
3729        else
3730                ixgbe_configure_msi_and_legacy(adapter);
3731
3732        /* enable the optics for both mult-speed fiber and 82599 SFP+ fiber */
3733        if (hw->mac.ops.enable_tx_laser &&
3734            ((hw->phy.multispeed_fiber) ||
3735             ((hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber) &&
3736              (hw->mac.type == ixgbe_mac_82599EB))))
3737                hw->mac.ops.enable_tx_laser(hw);
3738
3739        clear_bit(__IXGBE_DOWN, &adapter->state);
3740        ixgbe_napi_enable_all(adapter);
3741
3742        if (ixgbe_is_sfp(hw)) {
3743                ixgbe_sfp_link_config(adapter);
3744        } else {
3745                err = ixgbe_non_sfp_link_config(hw);
3746                if (err)
3747                        e_err(probe, "link_config FAILED %d\n", err);
3748        }
3749
3750        /* clear any pending interrupts, may auto mask */
3751        IXGBE_READ_REG(hw, IXGBE_EICR);
3752        ixgbe_irq_enable(adapter, true, true);
3753
3754        /*
3755         * If this adapter has a fan, check to see if we had a failure
3756         * before we enabled the interrupt.
3757         */
3758        if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
3759                u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
3760                if (esdp & IXGBE_ESDP_SDP1)
3761                        e_crit(drv, "Fan has stopped, replace the adapter\n");
3762        }
3763
3764        /* enable transmits */
3765        netif_tx_start_all_queues(adapter->netdev);
3766
3767        /* bring the link up in the watchdog, this could race with our first
3768         * link up interrupt but shouldn't be a problem */
3769        adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
3770        adapter->link_check_timeout = jiffies;
3771        mod_timer(&adapter->service_timer, jiffies);
3772
3773        /* Set PF Reset Done bit so PF/VF Mail Ops can work */
3774        ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
3775        ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
3776        IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
3777}
3778
3779void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
3780{
3781        WARN_ON(in_interrupt());
3782        /* put off any impending NetWatchDogTimeout */
3783        adapter->netdev->trans_start = jiffies;
3784
3785        while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
3786                usleep_range(1000, 2000);
3787        ixgbe_down(adapter);
3788        /*
3789         * If SR-IOV enabled then wait a bit before bringing the adapter
3790         * back up to give the VFs time to respond to the reset.  The
3791         * two second wait is based upon the watchdog timer cycle in
3792         * the VF driver.
3793         */
3794        if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
3795                msleep(2000);
3796        ixgbe_up(adapter);
3797        clear_bit(__IXGBE_RESETTING, &adapter->state);
3798}
3799
3800void ixgbe_up(struct ixgbe_adapter *adapter)
3801{
3802        /* hardware has been reset, we need to reload some things */
3803        ixgbe_configure(adapter);
3804
3805        ixgbe_up_complete(adapter);
3806}
3807
3808void ixgbe_reset(struct ixgbe_adapter *adapter)
3809{
3810        struct ixgbe_hw *hw = &adapter->hw;
3811        int err;
3812
3813        /* lock SFP init bit to prevent race conditions with the watchdog */
3814        while (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
3815                usleep_range(1000, 2000);
3816
3817        /* clear all SFP and link config related flags while holding SFP_INIT */
3818        adapter->flags2 &= ~(IXGBE_FLAG2_SEARCH_FOR_SFP |
3819                             IXGBE_FLAG2_SFP_NEEDS_RESET);
3820        adapter->flags &= ~IXGBE_FLAG_NEED_LINK_CONFIG;
3821
3822        err = hw->mac.ops.init_hw(hw);
3823        switch (err) {
3824        case 0:
3825        case IXGBE_ERR_SFP_NOT_PRESENT:
3826        case IXGBE_ERR_SFP_NOT_SUPPORTED:
3827                break;
3828        case IXGBE_ERR_MASTER_REQUESTS_PENDING:
3829                e_dev_err("master disable timed out\n");
3830                break;
3831        case IXGBE_ERR_EEPROM_VERSION:
3832                /* We are running on a pre-production device, log a warning */
3833                e_dev_warn("This device is a pre-production adapter/LOM. "
3834                           "Please be aware there may be issues associated with "
3835                           "your hardware.  If you are experiencing problems "
3836                           "please contact your Intel or hardware "
3837                           "representative who provided you with this "
3838                           "hardware.\n");
3839                break;
3840        default:
3841                e_dev_err("Hardware Error: %d\n", err);
3842        }
3843
3844        clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
3845
3846        /* reprogram the RAR[0] in case user changed it. */
3847        hw->mac.ops.set_rar(hw, 0, hw->mac.addr, adapter->num_vfs,
3848                            IXGBE_RAH_AV);
3849}
3850
3851/**
3852 * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
3853 * @rx_ring: ring to free buffers from
3854 **/
3855static void ixgbe_clean_rx_ring(struct ixgbe_ring *rx_ring)
3856{
3857        struct device *dev = rx_ring->dev;
3858        unsigned long size;
3859        u16 i;
3860
3861        /* ring already cleared, nothing to do */
3862        if (!rx_ring->rx_buffer_info)
3863                return;
3864
3865        /* Free all the Rx ring sk_buffs */
3866        for (i = 0; i < rx_ring->count; i++) {
3867                struct ixgbe_rx_buffer *rx_buffer_info;
3868
3869                rx_buffer_info = &rx_ring->rx_buffer_info[i];
3870                if (rx_buffer_info->dma) {
3871                        dma_unmap_single(rx_ring->dev, rx_buffer_info->dma,
3872                                         rx_ring->rx_buf_len,
3873                                         DMA_FROM_DEVICE);
3874                        rx_buffer_info->dma = 0;
3875                }
3876                if (rx_buffer_info->skb) {
3877                        struct sk_buff *skb = rx_buffer_info->skb;
3878                        rx_buffer_info->skb = NULL;
3879                        do {
3880                                struct sk_buff *this = skb;
3881                                if (IXGBE_RSC_CB(this)->delay_unmap) {
3882                                        dma_unmap_single(dev,
3883                                                         IXGBE_RSC_CB(this)->dma,
3884                                                         rx_ring->rx_buf_len,
3885                                                         DMA_FROM_DEVICE);
3886                                        IXGBE_RSC_CB(this)->dma = 0;
3887                                        IXGBE_RSC_CB(skb)->delay_unmap = false;
3888                                }
3889                                skb = skb->prev;
3890                                dev_kfree_skb(this);
3891                        } while (skb);
3892                }
3893                if (!rx_buffer_info->page)
3894                        continue;
3895                if (rx_buffer_info->page_dma) {
3896                        dma_unmap_page(dev, rx_buffer_info->page_dma,
3897                                       PAGE_SIZE / 2, DMA_FROM_DEVICE);
3898                        rx_buffer_info->page_dma = 0;
3899                }
3900                put_page(rx_buffer_info->page);
3901                rx_buffer_info->page = NULL;
3902                rx_buffer_info->page_offset = 0;
3903        }
3904
3905        size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
3906        memset(rx_ring->rx_buffer_info, 0, size);
3907
3908        /* Zero out the descriptor ring */
3909        memset(rx_ring->desc, 0, rx_ring->size);
3910
3911        rx_ring->next_to_clean = 0;
3912        rx_ring->next_to_use = 0;
3913}
3914
3915/**
3916 * ixgbe_clean_tx_ring - Free Tx Buffers
3917 * @tx_ring: ring to be cleaned
3918 **/
3919static void ixgbe_clean_tx_ring(struct ixgbe_ring *tx_ring)
3920{
3921        struct ixgbe_tx_buffer *tx_buffer_info;
3922        unsigned long size;
3923        u16 i;
3924
3925        /* ring already cleared, nothing to do */
3926        if (!tx_ring->tx_buffer_info)
3927                return;
3928
3929        /* Free all the Tx ring sk_buffs */
3930        for (i = 0; i < tx_ring->count; i++) {
3931                tx_buffer_info = &tx_ring->tx_buffer_info[i];
3932                ixgbe_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
3933        }
3934
3935        size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
3936        memset(tx_ring->tx_buffer_info, 0, size);
3937
3938        /* Zero out the descriptor ring */
3939        memset(tx_ring->desc, 0, tx_ring->size);
3940
3941        tx_ring->next_to_use = 0;
3942        tx_ring->next_to_clean = 0;
3943}
3944
3945/**
3946 * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
3947 * @adapter: board private structure
3948 **/
3949static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
3950{
3951        int i;
3952
3953        for (i = 0; i < adapter->num_rx_queues; i++)
3954                ixgbe_clean_rx_ring(adapter->rx_ring[i]);
3955}
3956
3957/**
3958 * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
3959 * @adapter: board private structure
3960 **/
3961static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
3962{
3963        int i;
3964
3965        for (i = 0; i < adapter->num_tx_queues; i++)
3966                ixgbe_clean_tx_ring(adapter->tx_ring[i]);
3967}
3968
3969static void ixgbe_fdir_filter_exit(struct ixgbe_adapter *adapter)
3970{
3971        struct hlist_node *node, *node2;
3972        struct ixgbe_fdir_filter *filter;
3973
3974        spin_lock(&adapter->fdir_perfect_lock);
3975
3976        hlist_for_each_entry_safe(filter, node, node2,
3977                                  &adapter->fdir_filter_list, fdir_node) {
3978                hlist_del(&filter->fdir_node);
3979                kfree(filter);
3980        }
3981        adapter->fdir_filter_count = 0;
3982
3983        spin_unlock(&adapter->fdir_perfect_lock);
3984}
3985
3986void ixgbe_down(struct ixgbe_adapter *adapter)
3987{
3988        struct net_device *netdev = adapter->netdev;
3989        struct ixgbe_hw *hw = &adapter->hw;
3990        u32 rxctrl;
3991        int i;
3992
3993        /* signal that we are down to the interrupt handler */
3994        set_bit(__IXGBE_DOWN, &adapter->state);
3995
3996        /* disable receives */
3997        rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
3998        IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
3999
4000        /* disable all enabled rx queues */
4001        for (i = 0; i < adapter->num_rx_queues; i++)
4002                /* this call also flushes the previous write */
4003                ixgbe_disable_rx_queue(adapter, adapter->rx_ring[i]);
4004
4005        usleep_range(10000, 20000);
4006
4007        netif_tx_stop_all_queues(netdev);
4008
4009        /* call carrier off first to avoid false dev_watchdog timeouts */
4010        netif_carrier_off(netdev);
4011        netif_tx_disable(netdev);
4012
4013        ixgbe_irq_disable(adapter);
4014
4015        ixgbe_napi_disable_all(adapter);
4016
4017        adapter->flags2 &= ~(IXGBE_FLAG2_FDIR_REQUIRES_REINIT |
4018                             IXGBE_FLAG2_RESET_REQUESTED);
4019        adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
4020
4021        del_timer_sync(&adapter->service_timer);
4022
4023        if (adapter->num_vfs) {
4024                /* Clear EITR Select mapping */
4025                IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, 0);
4026
4027                /* Mark all the VFs as inactive */
4028                for (i = 0 ; i < adapter->num_vfs; i++)
4029                        adapter->vfinfo[i].clear_to_send = false;
4030
4031                /* ping all the active vfs to let them know we are going down */
4032                ixgbe_ping_all_vfs(adapter);
4033
4034                /* Disable all VFTE/VFRE TX/RX */
4035                ixgbe_disable_tx_rx(adapter);
4036        }
4037
4038        /* disable transmits in the hardware now that interrupts are off */
4039        for (i = 0; i < adapter->num_tx_queues; i++) {
4040                u8 reg_idx = adapter->tx_ring[i]->reg_idx;
4041                IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
4042        }
4043
4044        /* Disable the Tx DMA engine on 82599 and X540 */
4045        switch (hw->mac.type) {
4046        case ixgbe_mac_82599EB:
4047        case ixgbe_mac_X540:
4048                IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL,
4049                                (IXGBE_READ_REG(hw, IXGBE_DMATXCTL) &
4050                                 ~IXGBE_DMATXCTL_TE));
4051                break;
4052        default:
4053                break;
4054        }
4055
4056        if (!pci_channel_offline(adapter->pdev))
4057                ixgbe_reset(adapter);
4058
4059        /* power down the optics for multispeed fiber and 82599 SFP+ fiber */
4060        if (hw->mac.ops.disable_tx_laser &&
4061            ((hw->phy.multispeed_fiber) ||
4062             ((hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber) &&
4063              (hw->mac.type == ixgbe_mac_82599EB))))
4064                hw->mac.ops.disable_tx_laser(hw);
4065
4066        ixgbe_clean_all_tx_rings(adapter);
4067        ixgbe_clean_all_rx_rings(adapter);
4068
4069#ifdef CONFIG_IXGBE_DCA
4070        /* since we reset the hardware DCA settings were cleared */
4071        ixgbe_setup_dca(adapter);
4072#endif
4073}
4074
4075/**
4076 * ixgbe_poll - NAPI Rx polling callback
4077 * @napi: structure for representing this polling device
4078 * @budget: how many packets driver is allowed to clean
4079 *
4080 * This function is used for legacy and MSI, NAPI mode
4081 **/
4082static int ixgbe_poll(struct napi_struct *napi, int budget)
4083{
4084        struct ixgbe_q_vector *q_vector =
4085                                container_of(napi, struct ixgbe_q_vector, napi);
4086        struct ixgbe_adapter *adapter = q_vector->adapter;
4087        struct ixgbe_ring *ring;
4088        int per_ring_budget;
4089        bool clean_complete = true;
4090
4091#ifdef CONFIG_IXGBE_DCA
4092        if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
4093                ixgbe_update_dca(q_vector);
4094#endif
4095
4096        for (ring = q_vector->tx.ring; ring != NULL; ring = ring->next)
4097                clean_complete &= !!ixgbe_clean_tx_irq(q_vector, ring);
4098
4099        /* attempt to distribute budget to each queue fairly, but don't allow
4100         * the budget to go below 1 because we'll exit polling */
4101        if (q_vector->rx.count > 1)
4102                per_ring_budget = max(budget/q_vector->rx.count, 1);
4103        else
4104                per_ring_budget = budget;
4105
4106        for (ring = q_vector->rx.ring; ring != NULL; ring = ring->next)
4107                clean_complete &= ixgbe_clean_rx_irq(q_vector, ring,
4108                                                     per_ring_budget);
4109
4110        /* If all work not completed, return budget and keep polling */
4111        if (!clean_complete)
4112                return budget;
4113
4114        /* all work done, exit the polling mode */
4115        napi_complete(napi);
4116        if (adapter->rx_itr_setting & 1)
4117                ixgbe_set_itr(q_vector);
4118        if (!test_bit(__IXGBE_DOWN, &adapter->state))
4119                ixgbe_irq_enable_queues(adapter, ((u64)1 << q_vector->v_idx));
4120
4121        return 0;
4122}
4123
4124/**
4125 * ixgbe_tx_timeout - Respond to a Tx Hang
4126 * @netdev: network interface device structure
4127 **/
4128static void ixgbe_tx_timeout(struct net_device *netdev)
4129{
4130        struct ixgbe_adapter *adapter = netdev_priv(netdev);
4131
4132        /* Do the reset outside of interrupt context */
4133        ixgbe_tx_timeout_reset(adapter);
4134}
4135
4136/**
4137 * ixgbe_set_rss_queues: Allocate queues for RSS
4138 * @adapter: board private structure to initialize
4139 *
4140 * This is our "base" multiqueue mode.  RSS (Receive Side Scaling) will try
4141 * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
4142 *
4143 **/
4144static inline bool ixgbe_set_rss_queues(struct ixgbe_adapter *adapter)
4145{
4146        bool ret = false;
4147        struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_RSS];
4148
4149        if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
4150                f->mask = 0xF;
4151                adapter->num_rx_queues = f->indices;
4152                adapter->num_tx_queues = f->indices;
4153                ret = true;
4154        } else {
4155                ret = false;
4156        }
4157
4158        return ret;
4159}
4160
4161/**
4162 * ixgbe_set_fdir_queues: Allocate queues for Flow Director
4163 * @adapter: board private structure to initialize
4164 *
4165 * Flow Director is an advanced Rx filter, attempting to get Rx flows back
4166 * to the original CPU that initiated the Tx session.  This runs in addition
4167 * to RSS, so if a packet doesn't match an FDIR filter, we can still spread the
4168 * Rx load across CPUs using RSS.
4169 *
4170 **/
4171static inline bool ixgbe_set_fdir_queues(struct ixgbe_adapter *adapter)
4172{
4173        bool ret = false;
4174        struct ixgbe_ring_feature *f_fdir = &adapter->ring_feature[RING_F_FDIR];
4175
4176        f_fdir->indices = min((int)num_online_cpus(), f_fdir->indices);
4177        f_fdir->mask = 0;
4178
4179        /* Flow Director must have RSS enabled */
4180        if ((adapter->flags & IXGBE_FLAG_RSS_ENABLED) &&
4181            (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE)) {
4182                adapter->num_tx_queues = f_fdir->indices;
4183                adapter->num_rx_queues = f_fdir->indices;
4184                ret = true;
4185        } else {
4186                adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
4187        }
4188        return ret;
4189}
4190
4191#ifdef IXGBE_FCOE
4192/**
4193 * ixgbe_set_fcoe_queues: Allocate queues for Fiber Channel over Ethernet (FCoE)
4194 * @adapter: board private structure to initialize
4195 *
4196 * FCoE RX FCRETA can use up to 8 rx queues for up to 8 different exchanges.
4197 * The ring feature mask is not used as a mask for FCoE, as it can take any 8
4198 * rx queues out of the max number of rx queues, instead, it is used as the
4199 * index of the first rx queue used by FCoE.
4200 *
4201 **/
4202static inline bool ixgbe_set_fcoe_queues(struct ixgbe_adapter *adapter)
4203{
4204        struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
4205
4206        if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
4207                return false;
4208
4209        f->indices = min((int)num_online_cpus(), f->indices);
4210
4211        adapter->num_rx_queues = 1;
4212        adapter->num_tx_queues = 1;
4213
4214        if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
4215                e_info(probe, "FCoE enabled with RSS\n");
4216                if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE)
4217                        ixgbe_set_fdir_queues(adapter);
4218                else
4219                        ixgbe_set_rss_queues(adapter);
4220        }
4221
4222        /* adding FCoE rx rings to the end */
4223        f->mask = adapter->num_rx_queues;
4224        adapter->num_rx_queues += f->indices;
4225        adapter->num_tx_queues += f->indices;
4226
4227        return true;
4228}
4229#endif /* IXGBE_FCOE */
4230
4231/* Artificial max queue cap per traffic class in DCB mode */
4232#define DCB_QUEUE_CAP 8
4233
4234#ifdef CONFIG_IXGBE_DCB
4235static inline bool ixgbe_set_dcb_queues(struct ixgbe_adapter *adapter)
4236{
4237        int per_tc_q, q, i, offset = 0;
4238        struct net_device *dev = adapter->netdev;
4239        int tcs = netdev_get_num_tc(dev);
4240
4241        if (!tcs)
4242                return false;
4243
4244        /* Map queue offset and counts onto allocated tx queues */
4245        per_tc_q = min(dev->num_tx_queues / tcs, (unsigned int)DCB_QUEUE_CAP);
4246        q = min((int)num_online_cpus(), per_tc_q);
4247
4248        for (i = 0; i < tcs; i++) {
4249                netdev_set_tc_queue(dev, i, q, offset);
4250                offset += q;
4251        }
4252
4253        adapter->num_tx_queues = q * tcs;
4254        adapter->num_rx_queues = q * tcs;
4255
4256#ifdef IXGBE_FCOE
4257        /* FCoE enabled queues require special configuration indexed
4258         * by feature specific indices and mask. Here we map FCoE
4259         * indices onto the DCB queue pairs allowing FCoE to own
4260         * configuration later.
4261         */
4262        if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
4263                int tc;
4264                struct ixgbe_ring_feature *f =
4265                                        &adapter->ring_feature[RING_F_FCOE];
4266
4267                tc = netdev_get_prio_tc_map(dev, adapter->fcoe.up);
4268                f->indices = dev->tc_to_txq[tc].count;
4269                f->mask = dev->tc_to_txq[tc].offset;
4270        }
4271#endif
4272
4273        return true;
4274}
4275#endif
4276
4277/**
4278 * ixgbe_set_sriov_queues: Allocate queues for IOV use
4279 * @adapter: board private structure to initialize
4280 *
4281 * IOV doesn't actually use anything, so just NAK the
4282 * request for now and let the other queue routines
4283 * figure out what to do.
4284 */
4285static inline bool ixgbe_set_sriov_queues(struct ixgbe_adapter *adapter)
4286{
4287        return false;
4288}
4289
4290/*
4291 * ixgbe_set_num_queues: Allocate queues for device, feature dependent
4292 * @adapter: board private structure to initialize
4293 *
4294 * This is the top level queue allocation routine.  The order here is very
4295 * important, starting with the "most" number of features turned on at once,
4296 * and ending with the smallest set of features.  This way large combinations
4297 * can be allocated if they're turned on, and smaller combinations are the
4298 * fallthrough conditions.
4299 *
4300 **/
4301static int ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
4302{
4303        /* Start with base case */
4304        adapter->num_rx_queues = 1;
4305        adapter->num_tx_queues = 1;
4306        adapter->num_rx_pools = adapter->num_rx_queues;
4307        adapter->num_rx_queues_per_pool = 1;
4308
4309        if (ixgbe_set_sriov_queues(adapter))
4310                goto done;
4311
4312#ifdef CONFIG_IXGBE_DCB
4313        if (ixgbe_set_dcb_queues(adapter))
4314                goto done;
4315
4316#endif
4317#ifdef IXGBE_FCOE
4318        if (ixgbe_set_fcoe_queues(adapter))
4319                goto done;
4320
4321#endif /* IXGBE_FCOE */
4322        if (ixgbe_set_fdir_queues(adapter))
4323                goto done;
4324
4325        if (ixgbe_set_rss_queues(adapter))
4326                goto done;
4327
4328        /* fallback to base case */
4329        adapter->num_rx_queues = 1;
4330        adapter->num_tx_queues = 1;
4331
4332done:
4333        if ((adapter->netdev->reg_state == NETREG_UNREGISTERED) ||
4334            (adapter->netdev->reg_state == NETREG_UNREGISTERING))
4335                return 0;
4336
4337        /* Notify the stack of the (possibly) reduced queue counts. */
4338        netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
4339        return netif_set_real_num_rx_queues(adapter->netdev,
4340                                            adapter->num_rx_queues);
4341}
4342
4343static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
4344                                       int vectors)
4345{
4346        int err, vector_threshold;
4347
4348        /* We'll want at least 3 (vector_threshold):
4349         * 1) TxQ[0] Cleanup
4350         * 2) RxQ[0] Cleanup
4351         * 3) Other (Link Status Change, etc.)
4352         * 4) TCP Timer (optional)
4353         */
4354        vector_threshold = MIN_MSIX_COUNT;
4355
4356        /* The more we get, the more we will assign to Tx/Rx Cleanup
4357         * for the separate queues...where Rx Cleanup >= Tx Cleanup.
4358         * Right now, we simply care about how many we'll get; we'll
4359         * set them up later while requesting irq's.
4360         */
4361        while (vectors >= vector_threshold) {
4362                err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
4363                                      vectors);
4364                if (!err) /* Success in acquiring all requested vectors. */
4365                        break;
4366                else if (err < 0)
4367                        vectors = 0; /* Nasty failure, quit now */
4368                else /* err == number of vectors we should try again with */
4369                        vectors = err;
4370        }
4371
4372        if (vectors < vector_threshold) {
4373                /* Can't allocate enough MSI-X interrupts?  Oh well.
4374                 * This just means we'll go with either a single MSI
4375                 * vector or fall back to legacy interrupts.
4376                 */
4377                netif_printk(adapter, hw, KERN_DEBUG, adapter->netdev,
4378                             "Unable to allocate MSI-X interrupts\n");
4379                adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
4380                kfree(adapter->msix_entries);
4381                adapter->msix_entries = NULL;
4382        } else {
4383                adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
4384                /*
4385                 * Adjust for only the vectors we'll use, which is minimum
4386                 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
4387                 * vectors we were allocated.
4388                 */
4389                adapter->num_msix_vectors = min(vectors,
4390                                   adapter->max_msix_q_vectors + NON_Q_VECTORS);
4391        }
4392}
4393
4394/**
4395 * ixgbe_cache_ring_rss - Descriptor ring to register mapping for RSS
4396 * @adapter: board private structure to initialize
4397 *
4398 * Cache the descriptor ring offsets for RSS to the assigned rings.
4399 *
4400 **/
4401static inline bool ixgbe_cache_ring_rss(struct ixgbe_adapter *adapter)
4402{
4403        int i;
4404
4405        if (!(adapter->flags & IXGBE_FLAG_RSS_ENABLED))
4406                return false;
4407
4408        for (i = 0; i < adapter->num_rx_queues; i++)
4409                adapter->rx_ring[i]->reg_idx = i;
4410        for (i = 0; i < adapter->num_tx_queues; i++)
4411                adapter->tx_ring[i]->reg_idx = i;
4412
4413        return true;
4414}
4415
4416#ifdef CONFIG_IXGBE_DCB
4417
4418/* ixgbe_get_first_reg_idx - Return first register index associated with ring */
4419static void ixgbe_get_first_reg_idx(struct ixgbe_adapter *adapter, u8 tc,
4420                                    unsigned int *tx, unsigned int *rx)
4421{
4422        struct net_device *dev = adapter->netdev;
4423        struct ixgbe_hw *hw = &adapter->hw;
4424        u8 num_tcs = netdev_get_num_tc(dev);
4425
4426        *tx = 0;
4427        *rx = 0;
4428
4429        switch (hw->mac.type) {
4430        case ixgbe_mac_82598EB:
4431                *tx = tc << 2;
4432                *rx = tc << 3;
4433                break;
4434        case ixgbe_mac_82599EB:
4435        case ixgbe_mac_X540:
4436                if (num_tcs > 4) {
4437                        if (tc < 3) {
4438                                *tx = tc << 5;
4439                                *rx = tc << 4;
4440                        } else if (tc <  5) {
4441                                *tx = ((tc + 2) << 4);
4442                                *rx = tc << 4;
4443                        } else if (tc < num_tcs) {
4444                                *tx = ((tc + 8) << 3);
4445                                *rx = tc << 4;
4446                        }
4447                } else {
4448                        *rx =  tc << 5;
4449                        switch (tc) {
4450                        case 0:
4451                                *tx =  0;
4452                                break;
4453                        case 1:
4454                                *tx = 64;
4455                                break;
4456                        case 2:
4457                                *tx = 96;
4458                                break;
4459                        case 3:
4460                                *tx = 112;
4461                                break;
4462                        default:
4463                                break;
4464                        }
4465                }
4466                break;
4467        default:
4468                break;
4469        }
4470}
4471
4472/**
4473 * ixgbe_cache_ring_dcb - Descriptor ring to register mapping for DCB
4474 * @adapter: board private structure to initialize
4475 *
4476 * Cache the descriptor ring offsets for DCB to the assigned rings.
4477 *
4478 **/
4479static inline bool ixgbe_cache_ring_dcb(struct ixgbe_adapter *adapter)
4480{
4481        struct net_device *dev = adapter->netdev;
4482        int i, j, k;
4483        u8 num_tcs = netdev_get_num_tc(dev);
4484
4485        if (!num_tcs)
4486                return false;
4487
4488        for (i = 0, k = 0; i < num_tcs; i++) {
4489                unsigned int tx_s, rx_s;
4490                u16 count = dev->tc_to_txq[i].count;
4491
4492                ixgbe_get_first_reg_idx(adapter, i, &tx_s, &rx_s);
4493                for (j = 0; j < count; j++, k++) {
4494                        adapter->tx_ring[k]->reg_idx = tx_s + j;
4495                        adapter->rx_ring[k]->reg_idx = rx_s + j;
4496                        adapter->tx_ring[k]->dcb_tc = i;
4497                        adapter->rx_ring[k]->dcb_tc = i;
4498                }
4499        }
4500
4501        return true;
4502}
4503#endif
4504
4505/**
4506 * ixgbe_cache_ring_fdir - Descriptor ring to register mapping for Flow Director
4507 * @adapter: board private structure to initialize
4508 *
4509 * Cache the descriptor ring offsets for Flow Director to the assigned rings.
4510 *
4511 **/
4512static inline bool ixgbe_cache_ring_fdir(struct ixgbe_adapter *adapter)
4513{
4514        int i;
4515        bool ret = false;
4516
4517        if ((adapter->flags & IXGBE_FLAG_RSS_ENABLED) &&
4518            (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE)) {
4519                for (i = 0; i < adapter->num_rx_queues; i++)
4520                        adapter->rx_ring[i]->reg_idx = i;
4521                for (i = 0; i < adapter->num_tx_queues; i++)
4522                        adapter->tx_ring[i]->reg_idx = i;
4523                ret = true;
4524        }
4525
4526        return ret;
4527}
4528
4529#ifdef IXGBE_FCOE
4530/**
4531 * ixgbe_cache_ring_fcoe - Descriptor ring to register mapping for the FCoE
4532 * @adapter: board private structure to initialize
4533 *
4534 * Cache the descriptor ring offsets for FCoE mode to the assigned rings.
4535 *
4536 */
4537static inline bool ixgbe_cache_ring_fcoe(struct ixgbe_adapter *adapter)
4538{
4539        struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
4540        int i;
4541        u8 fcoe_rx_i = 0, fcoe_tx_i = 0;
4542
4543        if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
4544                return false;
4545
4546        if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
4547                if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE)
4548                        ixgbe_cache_ring_fdir(adapter);
4549                else
4550                        ixgbe_cache_ring_rss(adapter);
4551
4552                fcoe_rx_i = f->mask;
4553                fcoe_tx_i = f->mask;
4554        }
4555        for (i = 0; i < f->indices; i++, fcoe_rx_i++, fcoe_tx_i++) {
4556                adapter->rx_ring[f->mask + i]->reg_idx = fcoe_rx_i;
4557                adapter->tx_ring[f->mask + i]->reg_idx = fcoe_tx_i;
4558        }
4559        return true;
4560}
4561
4562#endif /* IXGBE_FCOE */
4563/**
4564 * ixgbe_cache_ring_sriov - Descriptor ring to register mapping for sriov
4565 * @adapter: board private structure to initialize
4566 *
4567 * SR-IOV doesn't use any descriptor rings but changes the default if
4568 * no other mapping is used.
4569 *
4570 */
4571static inline bool ixgbe_cache_ring_sriov(struct ixgbe_adapter *adapter)
4572{
4573        adapter->rx_ring[0]->reg_idx = adapter->num_vfs * 2;
4574        adapter->tx_ring[0]->reg_idx = adapter->num_vfs * 2;
4575        if (adapter->num_vfs)
4576                return true;
4577        else
4578                return false;
4579}
4580
4581/**
4582 * ixgbe_cache_ring_register - Descriptor ring to register mapping
4583 * @adapter: board private structure to initialize
4584 *
4585 * Once we know the feature-set enabled for the device, we'll cache
4586 * the register offset the descriptor ring is assigned to.
4587 *
4588 * Note, the order the various feature calls is important.  It must start with
4589 * the "most" features enabled at the same time, then trickle down to the
4590 * least amount of features turned on at once.
4591 **/
4592static void ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
4593{
4594        /* start with default case */
4595        adapter->rx_ring[0]->reg_idx = 0;
4596        adapter->tx_ring[0]->reg_idx = 0;
4597
4598        if (ixgbe_cache_ring_sriov(adapter))
4599                return;
4600
4601#ifdef CONFIG_IXGBE_DCB
4602        if (ixgbe_cache_ring_dcb(adapter))
4603                return;
4604#endif
4605
4606#ifdef IXGBE_FCOE
4607        if (ixgbe_cache_ring_fcoe(adapter))
4608                return;
4609#endif /* IXGBE_FCOE */
4610
4611        if (ixgbe_cache_ring_fdir(adapter))
4612                return;
4613
4614        if (ixgbe_cache_ring_rss(adapter))
4615                return;
4616}
4617
4618/**
4619 * ixgbe_alloc_queues - Allocate memory for all rings
4620 * @adapter: board private structure to initialize
4621 *
4622 * We allocate one ring per queue at run-time since we don't know the
4623 * number of queues at compile-time.  The polling_netdev array is
4624 * intended for Multiqueue, but should work fine with a single queue.
4625 **/
4626static int ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
4627{
4628        int rx = 0, tx = 0, nid = adapter->node;
4629
4630        if (nid < 0 || !node_online(nid))
4631                nid = first_online_node;
4632
4633        for (; tx < adapter->num_tx_queues; tx++) {
4634                struct ixgbe_ring *ring;
4635
4636                ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, nid);
4637                if (!ring)
4638                        ring = kzalloc(sizeof(*ring), GFP_KERNEL);
4639                if (!ring)
4640                        goto err_allocation;
4641                ring->count = adapter->tx_ring_count;
4642                ring->queue_index = tx;
4643                ring->numa_node = nid;
4644                ring->dev = &adapter->pdev->dev;
4645                ring->netdev = adapter->netdev;
4646
4647                adapter->tx_ring[tx] = ring;
4648        }
4649
4650        for (; rx < adapter->num_rx_queues; rx++) {
4651                struct ixgbe_ring *ring;
4652
4653                ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, nid);
4654                if (!ring)
4655                        ring = kzalloc(sizeof(*ring), GFP_KERNEL);
4656                if (!ring)
4657                        goto err_allocation;
4658                ring->count = adapter->rx_ring_count;
4659                ring->queue_index = rx;
4660                ring->numa_node = nid;
4661                ring->dev = &adapter->pdev->dev;
4662                ring->netdev = adapter->netdev;
4663
4664                adapter->rx_ring[rx] = ring;
4665        }
4666
4667        ixgbe_cache_ring_register(adapter);