linux/drivers/net/ethernet/intel/i40evf/i40evf_main.c
<<
>>
Prefs
   1/*******************************************************************************
   2 *
   3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
   4 * Copyright(c) 2013 - 2016 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
  16 * with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 *
  18 * The full GNU General Public License is included in this distribution in
  19 * the file called "COPYING".
  20 *
  21 * Contact Information:
  22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24 *
  25 ******************************************************************************/
  26
  27#include "i40evf.h"
  28#include "i40e_prototype.h"
  29static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
  30static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
  31static int i40evf_close(struct net_device *netdev);
  32
  33char i40evf_driver_name[] = "i40evf";
  34static const char i40evf_driver_string[] =
  35        "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
  36
  37#define DRV_KERN "-k"
  38
  39#define DRV_VERSION_MAJOR 1
  40#define DRV_VERSION_MINOR 6
  41#define DRV_VERSION_BUILD 27
  42#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  43             __stringify(DRV_VERSION_MINOR) "." \
  44             __stringify(DRV_VERSION_BUILD) \
  45             DRV_KERN
  46const char i40evf_driver_version[] = DRV_VERSION;
  47static const char i40evf_copyright[] =
  48        "Copyright (c) 2013 - 2015 Intel Corporation.";
  49
  50/* i40evf_pci_tbl - PCI Device ID Table
  51 *
  52 * Wildcard entries (PCI_ANY_ID) should come last
  53 * Last entry must be all 0s
  54 *
  55 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  56 *   Class, Class Mask, private data (not used) }
  57 */
  58static const struct pci_device_id i40evf_pci_tbl[] = {
  59        {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
  60        {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
  61        {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
  62        /* required last entry */
  63        {0, }
  64};
  65
  66MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
  67
  68MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  69MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
  70MODULE_LICENSE("GPL");
  71MODULE_VERSION(DRV_VERSION);
  72
  73static struct workqueue_struct *i40evf_wq;
  74
  75/**
  76 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
  77 * @hw:   pointer to the HW structure
  78 * @mem:  ptr to mem struct to fill out
  79 * @size: size of memory requested
  80 * @alignment: what to align the allocation to
  81 **/
  82i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
  83                                      struct i40e_dma_mem *mem,
  84                                      u64 size, u32 alignment)
  85{
  86        struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  87
  88        if (!mem)
  89                return I40E_ERR_PARAM;
  90
  91        mem->size = ALIGN(size, alignment);
  92        mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
  93                                     (dma_addr_t *)&mem->pa, GFP_KERNEL);
  94        if (mem->va)
  95                return 0;
  96        else
  97                return I40E_ERR_NO_MEMORY;
  98}
  99
 100/**
 101 * i40evf_free_dma_mem_d - OS specific memory free for shared code
 102 * @hw:   pointer to the HW structure
 103 * @mem:  ptr to mem struct to free
 104 **/
 105i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
 106{
 107        struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
 108
 109        if (!mem || !mem->va)
 110                return I40E_ERR_PARAM;
 111        dma_free_coherent(&adapter->pdev->dev, mem->size,
 112                          mem->va, (dma_addr_t)mem->pa);
 113        return 0;
 114}
 115
 116/**
 117 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
 118 * @hw:   pointer to the HW structure
 119 * @mem:  ptr to mem struct to fill out
 120 * @size: size of memory requested
 121 **/
 122i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
 123                                       struct i40e_virt_mem *mem, u32 size)
 124{
 125        if (!mem)
 126                return I40E_ERR_PARAM;
 127
 128        mem->size = size;
 129        mem->va = kzalloc(size, GFP_KERNEL);
 130
 131        if (mem->va)
 132                return 0;
 133        else
 134                return I40E_ERR_NO_MEMORY;
 135}
 136
 137/**
 138 * i40evf_free_virt_mem_d - OS specific memory free for shared code
 139 * @hw:   pointer to the HW structure
 140 * @mem:  ptr to mem struct to free
 141 **/
 142i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
 143                                   struct i40e_virt_mem *mem)
 144{
 145        if (!mem)
 146                return I40E_ERR_PARAM;
 147
 148        /* it's ok to kfree a NULL pointer */
 149        kfree(mem->va);
 150
 151        return 0;
 152}
 153
 154/**
 155 * i40evf_debug_d - OS dependent version of debug printing
 156 * @hw:  pointer to the HW structure
 157 * @mask: debug level mask
 158 * @fmt_str: printf-type format description
 159 **/
 160void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
 161{
 162        char buf[512];
 163        va_list argptr;
 164
 165        if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
 166                return;
 167
 168        va_start(argptr, fmt_str);
 169        vsnprintf(buf, sizeof(buf), fmt_str, argptr);
 170        va_end(argptr);
 171
 172        /* the debug string is already formatted with a newline */
 173        pr_info("%s", buf);
 174}
 175
 176/**
 177 * i40evf_schedule_reset - Set the flags and schedule a reset event
 178 * @adapter: board private structure
 179 **/
 180void i40evf_schedule_reset(struct i40evf_adapter *adapter)
 181{
 182        if (!(adapter->flags &
 183              (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
 184                adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
 185                schedule_work(&adapter->reset_task);
 186        }
 187}
 188
 189/**
 190 * i40evf_tx_timeout - Respond to a Tx Hang
 191 * @netdev: network interface device structure
 192 **/
 193static void i40evf_tx_timeout(struct net_device *netdev)
 194{
 195        struct i40evf_adapter *adapter = netdev_priv(netdev);
 196
 197        adapter->tx_timeout_count++;
 198        i40evf_schedule_reset(adapter);
 199}
 200
 201/**
 202 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
 203 * @adapter: board private structure
 204 **/
 205static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
 206{
 207        struct i40e_hw *hw = &adapter->hw;
 208
 209        if (!adapter->msix_entries)
 210                return;
 211
 212        wr32(hw, I40E_VFINT_DYN_CTL01, 0);
 213
 214        /* read flush */
 215        rd32(hw, I40E_VFGEN_RSTAT);
 216
 217        synchronize_irq(adapter->msix_entries[0].vector);
 218}
 219
 220/**
 221 * i40evf_misc_irq_enable - Enable default interrupt generation settings
 222 * @adapter: board private structure
 223 **/
 224static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
 225{
 226        struct i40e_hw *hw = &adapter->hw;
 227
 228        wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
 229                                       I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
 230        wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
 231
 232        /* read flush */
 233        rd32(hw, I40E_VFGEN_RSTAT);
 234}
 235
 236/**
 237 * i40evf_irq_disable - Mask off interrupt generation on the NIC
 238 * @adapter: board private structure
 239 **/
 240static void i40evf_irq_disable(struct i40evf_adapter *adapter)
 241{
 242        int i;
 243        struct i40e_hw *hw = &adapter->hw;
 244
 245        if (!adapter->msix_entries)
 246                return;
 247
 248        for (i = 1; i < adapter->num_msix_vectors; i++) {
 249                wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
 250                synchronize_irq(adapter->msix_entries[i].vector);
 251        }
 252        /* read flush */
 253        rd32(hw, I40E_VFGEN_RSTAT);
 254}
 255
 256/**
 257 * i40evf_irq_enable_queues - Enable interrupt for specified queues
 258 * @adapter: board private structure
 259 * @mask: bitmap of queues to enable
 260 **/
 261void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
 262{
 263        struct i40e_hw *hw = &adapter->hw;
 264        int i;
 265
 266        for (i = 1; i < adapter->num_msix_vectors; i++) {
 267                if (mask & BIT(i - 1)) {
 268                        wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
 269                             I40E_VFINT_DYN_CTLN1_INTENA_MASK |
 270                             I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
 271                             I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
 272                }
 273        }
 274}
 275
 276/**
 277 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
 278 * @adapter: board private structure
 279 * @mask: bitmap of vectors to trigger
 280 **/
 281static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
 282{
 283        struct i40e_hw *hw = &adapter->hw;
 284        int i;
 285        u32 dyn_ctl;
 286
 287        if (mask & 1) {
 288                dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
 289                dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
 290                           I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
 291                           I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
 292                wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
 293        }
 294        for (i = 1; i < adapter->num_msix_vectors; i++) {
 295                if (mask & BIT(i)) {
 296                        dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
 297                        dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
 298                                   I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
 299                                   I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
 300                        wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
 301                }
 302        }
 303}
 304
 305/**
 306 * i40evf_irq_enable - Enable default interrupt generation settings
 307 * @adapter: board private structure
 308 * @flush: boolean value whether to run rd32()
 309 **/
 310void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
 311{
 312        struct i40e_hw *hw = &adapter->hw;
 313
 314        i40evf_misc_irq_enable(adapter);
 315        i40evf_irq_enable_queues(adapter, ~0);
 316
 317        if (flush)
 318                rd32(hw, I40E_VFGEN_RSTAT);
 319}
 320
 321/**
 322 * i40evf_msix_aq - Interrupt handler for vector 0
 323 * @irq: interrupt number
 324 * @data: pointer to netdev
 325 **/
 326static irqreturn_t i40evf_msix_aq(int irq, void *data)
 327{
 328        struct net_device *netdev = data;
 329        struct i40evf_adapter *adapter = netdev_priv(netdev);
 330        struct i40e_hw *hw = &adapter->hw;
 331        u32 val;
 332
 333        /* handle non-queue interrupts, these reads clear the registers */
 334        val = rd32(hw, I40E_VFINT_ICR01);
 335        val = rd32(hw, I40E_VFINT_ICR0_ENA1);
 336
 337        val = rd32(hw, I40E_VFINT_DYN_CTL01) |
 338              I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
 339        wr32(hw, I40E_VFINT_DYN_CTL01, val);
 340
 341        /* schedule work on the private workqueue */
 342        schedule_work(&adapter->adminq_task);
 343
 344        return IRQ_HANDLED;
 345}
 346
 347/**
 348 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
 349 * @irq: interrupt number
 350 * @data: pointer to a q_vector
 351 **/
 352static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
 353{
 354        struct i40e_q_vector *q_vector = data;
 355
 356        if (!q_vector->tx.ring && !q_vector->rx.ring)
 357                return IRQ_HANDLED;
 358
 359        napi_schedule_irqoff(&q_vector->napi);
 360
 361        return IRQ_HANDLED;
 362}
 363
 364/**
 365 * i40evf_map_vector_to_rxq - associate irqs with rx queues
 366 * @adapter: board private structure
 367 * @v_idx: interrupt number
 368 * @r_idx: queue number
 369 **/
 370static void
 371i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
 372{
 373        struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
 374        struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
 375        struct i40e_hw *hw = &adapter->hw;
 376
 377        rx_ring->q_vector = q_vector;
 378        rx_ring->next = q_vector->rx.ring;
 379        rx_ring->vsi = &adapter->vsi;
 380        q_vector->rx.ring = rx_ring;
 381        q_vector->rx.count++;
 382        q_vector->rx.latency_range = I40E_LOW_LATENCY;
 383        q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
 384        q_vector->ring_mask |= BIT(r_idx);
 385        q_vector->itr_countdown = ITR_COUNTDOWN_START;
 386        wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
 387}
 388
 389/**
 390 * i40evf_map_vector_to_txq - associate irqs with tx queues
 391 * @adapter: board private structure
 392 * @v_idx: interrupt number
 393 * @t_idx: queue number
 394 **/
 395static void
 396i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
 397{
 398        struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
 399        struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
 400        struct i40e_hw *hw = &adapter->hw;
 401
 402        tx_ring->q_vector = q_vector;
 403        tx_ring->next = q_vector->tx.ring;
 404        tx_ring->vsi = &adapter->vsi;
 405        q_vector->tx.ring = tx_ring;
 406        q_vector->tx.count++;
 407        q_vector->tx.latency_range = I40E_LOW_LATENCY;
 408        q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
 409        q_vector->itr_countdown = ITR_COUNTDOWN_START;
 410        q_vector->num_ringpairs++;
 411        wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
 412}
 413
 414/**
 415 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
 416 * @adapter: board private structure to initialize
 417 *
 418 * This function maps descriptor rings to the queue-specific vectors
 419 * we were allotted through the MSI-X enabling code.  Ideally, we'd have
 420 * one vector per ring/queue, but on a constrained vector budget, we
 421 * group the rings as "efficiently" as possible.  You would add new
 422 * mapping configurations in here.
 423 **/
 424static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
 425{
 426        int q_vectors;
 427        int v_start = 0;
 428        int rxr_idx = 0, txr_idx = 0;
 429        int rxr_remaining = adapter->num_active_queues;
 430        int txr_remaining = adapter->num_active_queues;
 431        int i, j;
 432        int rqpv, tqpv;
 433        int err = 0;
 434
 435        q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 436
 437        /* The ideal configuration...
 438         * We have enough vectors to map one per queue.
 439         */
 440        if (q_vectors >= (rxr_remaining * 2)) {
 441                for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
 442                        i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
 443
 444                for (; txr_idx < txr_remaining; v_start++, txr_idx++)
 445                        i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
 446                goto out;
 447        }
 448
 449        /* If we don't have enough vectors for a 1-to-1
 450         * mapping, we'll have to group them so there are
 451         * multiple queues per vector.
 452         * Re-adjusting *qpv takes care of the remainder.
 453         */
 454        for (i = v_start; i < q_vectors; i++) {
 455                rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
 456                for (j = 0; j < rqpv; j++) {
 457                        i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
 458                        rxr_idx++;
 459                        rxr_remaining--;
 460                }
 461        }
 462        for (i = v_start; i < q_vectors; i++) {
 463                tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
 464                for (j = 0; j < tqpv; j++) {
 465                        i40evf_map_vector_to_txq(adapter, i, txr_idx);
 466                        txr_idx++;
 467                        txr_remaining--;
 468                }
 469        }
 470
 471out:
 472        adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
 473
 474        return err;
 475}
 476
 477#ifdef CONFIG_NET_POLL_CONTROLLER
 478/**
 479 * i40evf_netpoll - A Polling 'interrupt' handler
 480 * @netdev: network interface device structure
 481 *
 482 * This is used by netconsole to send skbs without having to re-enable
 483 * interrupts.  It's not called while the normal interrupt routine is executing.
 484 **/
 485static void i40evf_netpoll(struct net_device *netdev)
 486{
 487        struct i40evf_adapter *adapter = netdev_priv(netdev);
 488        int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 489        int i;
 490
 491        /* if interface is down do nothing */
 492        if (test_bit(__I40E_DOWN, &adapter->vsi.state))
 493                return;
 494
 495        for (i = 0; i < q_vectors; i++)
 496                i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
 497}
 498
 499#endif
 500/**
 501 * i40evf_irq_affinity_notify - Callback for affinity changes
 502 * @notify: context as to what irq was changed
 503 * @mask: the new affinity mask
 504 *
 505 * This is a callback function used by the irq_set_affinity_notifier function
 506 * so that we may register to receive changes to the irq affinity masks.
 507 **/
 508static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
 509                                       const cpumask_t *mask)
 510{
 511        struct i40e_q_vector *q_vector =
 512                container_of(notify, struct i40e_q_vector, affinity_notify);
 513
 514        q_vector->affinity_mask = *mask;
 515}
 516
 517/**
 518 * i40evf_irq_affinity_release - Callback for affinity notifier release
 519 * @ref: internal core kernel usage
 520 *
 521 * This is a callback function used by the irq_set_affinity_notifier function
 522 * to inform the current notification subscriber that they will no longer
 523 * receive notifications.
 524 **/
 525static void i40evf_irq_affinity_release(struct kref *ref) {}
 526
 527/**
 528 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
 529 * @adapter: board private structure
 530 *
 531 * Allocates MSI-X vectors for tx and rx handling, and requests
 532 * interrupts from the kernel.
 533 **/
 534static int
 535i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
 536{
 537        int vector, err, q_vectors;
 538        int rx_int_idx = 0, tx_int_idx = 0;
 539        int irq_num;
 540
 541        i40evf_irq_disable(adapter);
 542        /* Decrement for Other and TCP Timer vectors */
 543        q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 544
 545        for (vector = 0; vector < q_vectors; vector++) {
 546                struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
 547                irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 548
 549                if (q_vector->tx.ring && q_vector->rx.ring) {
 550                        snprintf(q_vector->name, sizeof(q_vector->name) - 1,
 551                                 "i40evf-%s-%s-%d", basename,
 552                                 "TxRx", rx_int_idx++);
 553                        tx_int_idx++;
 554                } else if (q_vector->rx.ring) {
 555                        snprintf(q_vector->name, sizeof(q_vector->name) - 1,
 556                                 "i40evf-%s-%s-%d", basename,
 557                                 "rx", rx_int_idx++);
 558                } else if (q_vector->tx.ring) {
 559                        snprintf(q_vector->name, sizeof(q_vector->name) - 1,
 560                                 "i40evf-%s-%s-%d", basename,
 561                                 "tx", tx_int_idx++);
 562                } else {
 563                        /* skip this unused q_vector */
 564                        continue;
 565                }
 566                err = request_irq(irq_num,
 567                                  i40evf_msix_clean_rings,
 568                                  0,
 569                                  q_vector->name,
 570                                  q_vector);
 571                if (err) {
 572                        dev_info(&adapter->pdev->dev,
 573                                 "Request_irq failed, error: %d\n", err);
 574                        goto free_queue_irqs;
 575                }
 576                /* register for affinity change notifications */
 577                q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
 578                q_vector->affinity_notify.release =
 579                                                   i40evf_irq_affinity_release;
 580                irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
 581                /* assign the mask for this irq */
 582                irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
 583        }
 584
 585        return 0;
 586
 587free_queue_irqs:
 588        while (vector) {
 589                vector--;
 590                irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 591                irq_set_affinity_notifier(irq_num, NULL);
 592                irq_set_affinity_hint(irq_num, NULL);
 593                free_irq(irq_num, &adapter->q_vectors[vector]);
 594        }
 595        return err;
 596}
 597
 598/**
 599 * i40evf_request_misc_irq - Initialize MSI-X interrupts
 600 * @adapter: board private structure
 601 *
 602 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
 603 * vector is only for the admin queue, and stays active even when the netdev
 604 * is closed.
 605 **/
 606static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
 607{
 608        struct net_device *netdev = adapter->netdev;
 609        int err;
 610
 611        snprintf(adapter->misc_vector_name,
 612                 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
 613                 dev_name(&adapter->pdev->dev));
 614        err = request_irq(adapter->msix_entries[0].vector,
 615                          &i40evf_msix_aq, 0,
 616                          adapter->misc_vector_name, netdev);
 617        if (err) {
 618                dev_err(&adapter->pdev->dev,
 619                        "request_irq for %s failed: %d\n",
 620                        adapter->misc_vector_name, err);
 621                free_irq(adapter->msix_entries[0].vector, netdev);
 622        }
 623        return err;
 624}
 625
 626/**
 627 * i40evf_free_traffic_irqs - Free MSI-X interrupts
 628 * @adapter: board private structure
 629 *
 630 * Frees all MSI-X vectors other than 0.
 631 **/
 632static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
 633{
 634        int vector, irq_num, q_vectors;
 635
 636        if (!adapter->msix_entries)
 637                return;
 638
 639        q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 640
 641        for (vector = 0; vector < q_vectors; vector++) {
 642                irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 643                irq_set_affinity_notifier(irq_num, NULL);
 644                irq_set_affinity_hint(irq_num, NULL);
 645                free_irq(irq_num, &adapter->q_vectors[vector]);
 646        }
 647}
 648
 649/**
 650 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
 651 * @adapter: board private structure
 652 *
 653 * Frees MSI-X vector 0.
 654 **/
 655static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
 656{
 657        struct net_device *netdev = adapter->netdev;
 658
 659        if (!adapter->msix_entries)
 660                return;
 661
 662        free_irq(adapter->msix_entries[0].vector, netdev);
 663}
 664
 665/**
 666 * i40evf_configure_tx - Configure Transmit Unit after Reset
 667 * @adapter: board private structure
 668 *
 669 * Configure the Tx unit of the MAC after a reset.
 670 **/
 671static void i40evf_configure_tx(struct i40evf_adapter *adapter)
 672{
 673        struct i40e_hw *hw = &adapter->hw;
 674        int i;
 675
 676        for (i = 0; i < adapter->num_active_queues; i++)
 677                adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
 678}
 679
 680/**
 681 * i40evf_configure_rx - Configure Receive Unit after Reset
 682 * @adapter: board private structure
 683 *
 684 * Configure the Rx unit of the MAC after a reset.
 685 **/
 686static void i40evf_configure_rx(struct i40evf_adapter *adapter)
 687{
 688        struct i40e_hw *hw = &adapter->hw;
 689        int i;
 690
 691        for (i = 0; i < adapter->num_active_queues; i++) {
 692                adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
 693                adapter->rx_rings[i].rx_buf_len = I40EVF_RXBUFFER_2048;
 694        }
 695}
 696
 697/**
 698 * i40evf_find_vlan - Search filter list for specific vlan filter
 699 * @adapter: board private structure
 700 * @vlan: vlan tag
 701 *
 702 * Returns ptr to the filter object or NULL
 703 **/
 704static struct
 705i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
 706{
 707        struct i40evf_vlan_filter *f;
 708
 709        list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 710                if (vlan == f->vlan)
 711                        return f;
 712        }
 713        return NULL;
 714}
 715
 716/**
 717 * i40evf_add_vlan - Add a vlan filter to the list
 718 * @adapter: board private structure
 719 * @vlan: VLAN tag
 720 *
 721 * Returns ptr to the filter object or NULL when no memory available.
 722 **/
 723static struct
 724i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
 725{
 726        struct i40evf_vlan_filter *f = NULL;
 727        int count = 50;
 728
 729        while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
 730                                &adapter->crit_section)) {
 731                udelay(1);
 732                if (--count == 0)
 733                        goto out;
 734        }
 735
 736        f = i40evf_find_vlan(adapter, vlan);
 737        if (!f) {
 738                f = kzalloc(sizeof(*f), GFP_ATOMIC);
 739                if (!f)
 740                        goto clearout;
 741
 742                f->vlan = vlan;
 743
 744                INIT_LIST_HEAD(&f->list);
 745                list_add(&f->list, &adapter->vlan_filter_list);
 746                f->add = true;
 747                adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
 748        }
 749
 750clearout:
 751        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
 752out:
 753        return f;
 754}
 755
 756/**
 757 * i40evf_del_vlan - Remove a vlan filter from the list
 758 * @adapter: board private structure
 759 * @vlan: VLAN tag
 760 **/
 761static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
 762{
 763        struct i40evf_vlan_filter *f;
 764        int count = 50;
 765
 766        while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
 767                                &adapter->crit_section)) {
 768                udelay(1);
 769                if (--count == 0)
 770                        return;
 771        }
 772
 773        f = i40evf_find_vlan(adapter, vlan);
 774        if (f) {
 775                f->remove = true;
 776                adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
 777        }
 778        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
 779}
 780
 781/**
 782 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
 783 * @netdev: network device struct
 784 * @vid: VLAN tag
 785 **/
 786static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
 787                                  __always_unused __be16 proto, u16 vid)
 788{
 789        struct i40evf_adapter *adapter = netdev_priv(netdev);
 790
 791        if (!VLAN_ALLOWED(adapter))
 792                return -EIO;
 793        if (i40evf_add_vlan(adapter, vid) == NULL)
 794                return -ENOMEM;
 795        return 0;
 796}
 797
 798/**
 799 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
 800 * @netdev: network device struct
 801 * @vid: VLAN tag
 802 **/
 803static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
 804                                   __always_unused __be16 proto, u16 vid)
 805{
 806        struct i40evf_adapter *adapter = netdev_priv(netdev);
 807
 808        if (VLAN_ALLOWED(adapter)) {
 809                i40evf_del_vlan(adapter, vid);
 810                return 0;
 811        }
 812        return -EIO;
 813}
 814
 815/**
 816 * i40evf_find_filter - Search filter list for specific mac filter
 817 * @adapter: board private structure
 818 * @macaddr: the MAC address
 819 *
 820 * Returns ptr to the filter object or NULL
 821 **/
 822static struct
 823i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
 824                                      u8 *macaddr)
 825{
 826        struct i40evf_mac_filter *f;
 827
 828        if (!macaddr)
 829                return NULL;
 830
 831        list_for_each_entry(f, &adapter->mac_filter_list, list) {
 832                if (ether_addr_equal(macaddr, f->macaddr))
 833                        return f;
 834        }
 835        return NULL;
 836}
 837
 838/**
 839 * i40e_add_filter - Add a mac filter to the filter list
 840 * @adapter: board private structure
 841 * @macaddr: the MAC address
 842 *
 843 * Returns ptr to the filter object or NULL when no memory available.
 844 **/
 845static struct
 846i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
 847                                     u8 *macaddr)
 848{
 849        struct i40evf_mac_filter *f;
 850        int count = 50;
 851
 852        if (!macaddr)
 853                return NULL;
 854
 855        while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
 856                                &adapter->crit_section)) {
 857                udelay(1);
 858                if (--count == 0)
 859                        return NULL;
 860        }
 861
 862        f = i40evf_find_filter(adapter, macaddr);
 863        if (!f) {
 864                f = kzalloc(sizeof(*f), GFP_ATOMIC);
 865                if (!f) {
 866                        clear_bit(__I40EVF_IN_CRITICAL_TASK,
 867                                  &adapter->crit_section);
 868                        return NULL;
 869                }
 870
 871                ether_addr_copy(f->macaddr, macaddr);
 872
 873                list_add_tail(&f->list, &adapter->mac_filter_list);
 874                f->add = true;
 875                adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
 876        }
 877
 878        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
 879        return f;
 880}
 881
 882/**
 883 * i40evf_set_mac - NDO callback to set port mac address
 884 * @netdev: network interface device structure
 885 * @p: pointer to an address structure
 886 *
 887 * Returns 0 on success, negative on failure
 888 **/
 889static int i40evf_set_mac(struct net_device *netdev, void *p)
 890{
 891        struct i40evf_adapter *adapter = netdev_priv(netdev);
 892        struct i40e_hw *hw = &adapter->hw;
 893        struct i40evf_mac_filter *f;
 894        struct sockaddr *addr = p;
 895
 896        if (!is_valid_ether_addr(addr->sa_data))
 897                return -EADDRNOTAVAIL;
 898
 899        if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
 900                return 0;
 901
 902        if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
 903                return -EPERM;
 904
 905        f = i40evf_find_filter(adapter, hw->mac.addr);
 906        if (f) {
 907                f->remove = true;
 908                adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
 909        }
 910
 911        f = i40evf_add_filter(adapter, addr->sa_data);
 912        if (f) {
 913                ether_addr_copy(hw->mac.addr, addr->sa_data);
 914                ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
 915        }
 916
 917        return (f == NULL) ? -ENOMEM : 0;
 918}
 919
 920/**
 921 * i40evf_set_rx_mode - NDO callback to set the netdev filters
 922 * @netdev: network interface device structure
 923 **/
 924static void i40evf_set_rx_mode(struct net_device *netdev)
 925{
 926        struct i40evf_adapter *adapter = netdev_priv(netdev);
 927        struct i40evf_mac_filter *f, *ftmp;
 928        struct netdev_hw_addr *uca;
 929        struct netdev_hw_addr *mca;
 930        struct netdev_hw_addr *ha;
 931        int count = 50;
 932
 933        /* add addr if not already in the filter list */
 934        netdev_for_each_uc_addr(uca, netdev) {
 935                i40evf_add_filter(adapter, uca->addr);
 936        }
 937        netdev_for_each_mc_addr(mca, netdev) {
 938                i40evf_add_filter(adapter, mca->addr);
 939        }
 940
 941        while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
 942                                &adapter->crit_section)) {
 943                udelay(1);
 944                if (--count == 0) {
 945                        dev_err(&adapter->pdev->dev,
 946                                "Failed to get lock in %s\n", __func__);
 947                        return;
 948                }
 949        }
 950        /* remove filter if not in netdev list */
 951        list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
 952                netdev_for_each_mc_addr(mca, netdev)
 953                        if (ether_addr_equal(mca->addr, f->macaddr))
 954                                goto bottom_of_search_loop;
 955
 956                netdev_for_each_uc_addr(uca, netdev)
 957                        if (ether_addr_equal(uca->addr, f->macaddr))
 958                                goto bottom_of_search_loop;
 959
 960                for_each_dev_addr(netdev, ha)
 961                        if (ether_addr_equal(ha->addr, f->macaddr))
 962                                goto bottom_of_search_loop;
 963
 964                if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
 965                        goto bottom_of_search_loop;
 966
 967                /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
 968                f->remove = true;
 969                adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
 970
 971bottom_of_search_loop:
 972                continue;
 973        }
 974
 975        if (netdev->flags & IFF_PROMISC &&
 976            !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
 977                adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
 978        else if (!(netdev->flags & IFF_PROMISC) &&
 979                 adapter->flags & I40EVF_FLAG_PROMISC_ON)
 980                adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
 981
 982        if (netdev->flags & IFF_ALLMULTI &&
 983            !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
 984                adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
 985        else if (!(netdev->flags & IFF_ALLMULTI) &&
 986                 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
 987                adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
 988
 989        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
 990}
 991
 992/**
 993 * i40evf_napi_enable_all - enable NAPI on all queue vectors
 994 * @adapter: board private structure
 995 **/
 996static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
 997{
 998        int q_idx;
 999        struct i40e_q_vector *q_vector;
1000        int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1001
1002        for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1003                struct napi_struct *napi;
1004
1005                q_vector = &adapter->q_vectors[q_idx];
1006                napi = &q_vector->napi;
1007                napi_enable(napi);
1008        }
1009}
1010
1011/**
1012 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1013 * @adapter: board private structure
1014 **/
1015static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1016{
1017        int q_idx;
1018        struct i40e_q_vector *q_vector;
1019        int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1020
1021        for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1022                q_vector = &adapter->q_vectors[q_idx];
1023                napi_disable(&q_vector->napi);
1024        }
1025}
1026
1027/**
1028 * i40evf_configure - set up transmit and receive data structures
1029 * @adapter: board private structure
1030 **/
1031static void i40evf_configure(struct i40evf_adapter *adapter)
1032{
1033        struct net_device *netdev = adapter->netdev;
1034        int i;
1035
1036        i40evf_set_rx_mode(netdev);
1037
1038        i40evf_configure_tx(adapter);
1039        i40evf_configure_rx(adapter);
1040        adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1041
1042        for (i = 0; i < adapter->num_active_queues; i++) {
1043                struct i40e_ring *ring = &adapter->rx_rings[i];
1044
1045                i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
1046        }
1047}
1048
1049/**
1050 * i40evf_up_complete - Finish the last steps of bringing up a connection
1051 * @adapter: board private structure
1052 **/
1053static void i40evf_up_complete(struct i40evf_adapter *adapter)
1054{
1055        adapter->state = __I40EVF_RUNNING;
1056        clear_bit(__I40E_DOWN, &adapter->vsi.state);
1057
1058        i40evf_napi_enable_all(adapter);
1059
1060        adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1061        mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1062}
1063
1064/**
1065 * i40e_down - Shutdown the connection processing
1066 * @adapter: board private structure
1067 **/
1068void i40evf_down(struct i40evf_adapter *adapter)
1069{
1070        struct net_device *netdev = adapter->netdev;
1071        struct i40evf_mac_filter *f;
1072
1073        if (adapter->state <= __I40EVF_DOWN_PENDING)
1074                return;
1075
1076        while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1077                                &adapter->crit_section))
1078                usleep_range(500, 1000);
1079
1080        netif_carrier_off(netdev);
1081        netif_tx_disable(netdev);
1082        adapter->link_up = false;
1083        i40evf_napi_disable_all(adapter);
1084        i40evf_irq_disable(adapter);
1085
1086        /* remove all MAC filters */
1087        list_for_each_entry(f, &adapter->mac_filter_list, list) {
1088                f->remove = true;
1089        }
1090        /* remove all VLAN filters */
1091        list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1092                f->remove = true;
1093        }
1094        if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1095            adapter->state != __I40EVF_RESETTING) {
1096                /* cancel any current operation */
1097                adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1098                /* Schedule operations to close down the HW. Don't wait
1099                 * here for this to complete. The watchdog is still running
1100                 * and it will take care of this.
1101                 */
1102                adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1103                adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1104                adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1105        }
1106
1107        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1108}
1109
1110/**
1111 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1112 * @adapter: board private structure
1113 * @vectors: number of vectors to request
1114 *
1115 * Work with the OS to set up the MSIX vectors needed.
1116 *
1117 * Returns 0 on success, negative on failure
1118 **/
1119static int
1120i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1121{
1122        int err, vector_threshold;
1123
1124        /* We'll want at least 3 (vector_threshold):
1125         * 0) Other (Admin Queue and link, mostly)
1126         * 1) TxQ[0] Cleanup
1127         * 2) RxQ[0] Cleanup
1128         */
1129        vector_threshold = MIN_MSIX_COUNT;
1130
1131        /* The more we get, the more we will assign to Tx/Rx Cleanup
1132         * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1133         * Right now, we simply care about how many we'll get; we'll
1134         * set them up later while requesting irq's.
1135         */
1136        err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1137                                    vector_threshold, vectors);
1138        if (err < 0) {
1139                dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1140                kfree(adapter->msix_entries);
1141                adapter->msix_entries = NULL;
1142                return err;
1143        }
1144
1145        /* Adjust for only the vectors we'll use, which is minimum
1146         * of max_msix_q_vectors + NONQ_VECS, or the number of
1147         * vectors we were allocated.
1148         */
1149        adapter->num_msix_vectors = err;
1150        return 0;
1151}
1152
1153/**
1154 * i40evf_free_queues - Free memory for all rings
1155 * @adapter: board private structure to initialize
1156 *
1157 * Free all of the memory associated with queue pairs.
1158 **/
1159static void i40evf_free_queues(struct i40evf_adapter *adapter)
1160{
1161        if (!adapter->vsi_res)
1162                return;
1163        kfree(adapter->tx_rings);
1164        adapter->tx_rings = NULL;
1165        kfree(adapter->rx_rings);
1166        adapter->rx_rings = NULL;
1167}
1168
1169/**
1170 * i40evf_alloc_queues - Allocate memory for all rings
1171 * @adapter: board private structure to initialize
1172 *
1173 * We allocate one ring per queue at run-time since we don't know the
1174 * number of queues at compile-time.  The polling_netdev array is
1175 * intended for Multiqueue, but should work fine with a single queue.
1176 **/
1177static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1178{
1179        int i;
1180
1181        adapter->tx_rings = kcalloc(adapter->num_active_queues,
1182                                    sizeof(struct i40e_ring), GFP_KERNEL);
1183        if (!adapter->tx_rings)
1184                goto err_out;
1185        adapter->rx_rings = kcalloc(adapter->num_active_queues,
1186                                    sizeof(struct i40e_ring), GFP_KERNEL);
1187        if (!adapter->rx_rings)
1188                goto err_out;
1189
1190        for (i = 0; i < adapter->num_active_queues; i++) {
1191                struct i40e_ring *tx_ring;
1192                struct i40e_ring *rx_ring;
1193
1194                tx_ring = &adapter->tx_rings[i];
1195
1196                tx_ring->queue_index = i;
1197                tx_ring->netdev = adapter->netdev;
1198                tx_ring->dev = &adapter->pdev->dev;
1199                tx_ring->count = adapter->tx_desc_count;
1200                tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
1201                if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1202                        tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1203
1204                rx_ring = &adapter->rx_rings[i];
1205                rx_ring->queue_index = i;
1206                rx_ring->netdev = adapter->netdev;
1207                rx_ring->dev = &adapter->pdev->dev;
1208                rx_ring->count = adapter->rx_desc_count;
1209                rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
1210        }
1211
1212        return 0;
1213
1214err_out:
1215        i40evf_free_queues(adapter);
1216        return -ENOMEM;
1217}
1218
1219/**
1220 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1221 * @adapter: board private structure to initialize
1222 *
1223 * Attempt to configure the interrupts using the best available
1224 * capabilities of the hardware and the kernel.
1225 **/
1226static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1227{
1228        int vector, v_budget;
1229        int pairs = 0;
1230        int err = 0;
1231
1232        if (!adapter->vsi_res) {
1233                err = -EIO;
1234                goto out;
1235        }
1236        pairs = adapter->num_active_queues;
1237
1238        /* It's easy to be greedy for MSI-X vectors, but it really
1239         * doesn't do us much good if we have a lot more vectors
1240         * than CPU's.  So let's be conservative and only ask for
1241         * (roughly) twice the number of vectors as there are CPU's.
1242         */
1243        v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1244        v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
1245
1246        adapter->msix_entries = kcalloc(v_budget,
1247                                        sizeof(struct msix_entry), GFP_KERNEL);
1248        if (!adapter->msix_entries) {
1249                err = -ENOMEM;
1250                goto out;
1251        }
1252
1253        for (vector = 0; vector < v_budget; vector++)
1254                adapter->msix_entries[vector].entry = vector;
1255
1256        err = i40evf_acquire_msix_vectors(adapter, v_budget);
1257
1258out:
1259        netif_set_real_num_rx_queues(adapter->netdev, pairs);
1260        netif_set_real_num_tx_queues(adapter->netdev, pairs);
1261        return err;
1262}
1263
1264/**
1265 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1266 * @adapter: board private structure
1267 *
1268 * Return 0 on success, negative on failure
1269 **/
1270static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
1271{
1272        struct i40e_aqc_get_set_rss_key_data *rss_key =
1273                (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1274        struct i40e_hw *hw = &adapter->hw;
1275        int ret = 0;
1276
1277        if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1278                /* bail because we already have a command pending */
1279                dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1280                        adapter->current_op);
1281                return -EBUSY;
1282        }
1283
1284        ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1285        if (ret) {
1286                dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1287                        i40evf_stat_str(hw, ret),
1288                        i40evf_aq_str(hw, hw->aq.asq_last_status));
1289                return ret;
1290
1291        }
1292
1293        ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1294                                    adapter->rss_lut, adapter->rss_lut_size);
1295        if (ret) {
1296                dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1297                        i40evf_stat_str(hw, ret),
1298                        i40evf_aq_str(hw, hw->aq.asq_last_status));
1299        }
1300
1301        return ret;
1302
1303}
1304
1305/**
1306 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1307 * @adapter: board private structure
1308 *
1309 * Returns 0 on success, negative on failure
1310 **/
1311static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
1312{
1313        struct i40e_hw *hw = &adapter->hw;
1314        u32 *dw;
1315        u16 i;
1316
1317        dw = (u32 *)adapter->rss_key;
1318        for (i = 0; i <= adapter->rss_key_size / 4; i++)
1319                wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1320
1321        dw = (u32 *)adapter->rss_lut;
1322        for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1323                wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1324
1325        i40e_flush(hw);
1326
1327        return 0;
1328}
1329
1330/**
1331 * i40evf_config_rss - Configure RSS keys and lut
1332 * @adapter: board private structure
1333 *
1334 * Returns 0 on success, negative on failure
1335 **/
1336int i40evf_config_rss(struct i40evf_adapter *adapter)
1337{
1338
1339        if (RSS_PF(adapter)) {
1340                adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1341                                        I40EVF_FLAG_AQ_SET_RSS_KEY;
1342                return 0;
1343        } else if (RSS_AQ(adapter)) {
1344                return i40evf_config_rss_aq(adapter);
1345        } else {
1346                return i40evf_config_rss_reg(adapter);
1347        }
1348}
1349
1350/**
1351 * i40evf_fill_rss_lut - Fill the lut with default values
1352 * @adapter: board private structure
1353 **/
1354static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
1355{
1356        u16 i;
1357
1358        for (i = 0; i < adapter->rss_lut_size; i++)
1359                adapter->rss_lut[i] = i % adapter->num_active_queues;
1360}
1361
1362/**
1363 * i40evf_init_rss - Prepare for RSS
1364 * @adapter: board private structure
1365 *
1366 * Return 0 on success, negative on failure
1367 **/
1368static int i40evf_init_rss(struct i40evf_adapter *adapter)
1369{
1370        struct i40e_hw *hw = &adapter->hw;
1371        int ret;
1372
1373        if (!RSS_PF(adapter)) {
1374                /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1375                if (adapter->vf_res->vf_offload_flags &
1376                    I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1377                        adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1378                else
1379                        adapter->hena = I40E_DEFAULT_RSS_HENA;
1380
1381                wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1382                wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1383        }
1384
1385        i40evf_fill_rss_lut(adapter);
1386
1387        netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1388        ret = i40evf_config_rss(adapter);
1389
1390        return ret;
1391}
1392
1393/**
1394 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1395 * @adapter: board private structure to initialize
1396 *
1397 * We allocate one q_vector per queue interrupt.  If allocation fails we
1398 * return -ENOMEM.
1399 **/
1400static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1401{
1402        int q_idx = 0, num_q_vectors;
1403        struct i40e_q_vector *q_vector;
1404
1405        num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1406        adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1407                                     GFP_KERNEL);
1408        if (!adapter->q_vectors)
1409                return -ENOMEM;
1410
1411        for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1412                q_vector = &adapter->q_vectors[q_idx];
1413                q_vector->adapter = adapter;
1414                q_vector->vsi = &adapter->vsi;
1415                q_vector->v_idx = q_idx;
1416                netif_napi_add(adapter->netdev, &q_vector->napi,
1417                               i40evf_napi_poll, NAPI_POLL_WEIGHT);
1418        }
1419
1420        return 0;
1421}
1422
1423/**
1424 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1425 * @adapter: board private structure to initialize
1426 *
1427 * This function frees the memory allocated to the q_vectors.  In addition if
1428 * NAPI is enabled it will delete any references to the NAPI struct prior
1429 * to freeing the q_vector.
1430 **/
1431static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1432{
1433        int q_idx, num_q_vectors;
1434        int napi_vectors;
1435
1436        if (!adapter->q_vectors)
1437                return;
1438
1439        num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1440        napi_vectors = adapter->num_active_queues;
1441
1442        for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1443                struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1444                if (q_idx < napi_vectors)
1445                        netif_napi_del(&q_vector->napi);
1446        }
1447        kfree(adapter->q_vectors);
1448        adapter->q_vectors = NULL;
1449}
1450
1451/**
1452 * i40evf_reset_interrupt_capability - Reset MSIX setup
1453 * @adapter: board private structure
1454 *
1455 **/
1456void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1457{
1458        if (!adapter->msix_entries)
1459                return;
1460
1461        pci_disable_msix(adapter->pdev);
1462        kfree(adapter->msix_entries);
1463        adapter->msix_entries = NULL;
1464}
1465
1466/**
1467 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1468 * @adapter: board private structure to initialize
1469 *
1470 **/
1471int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1472{
1473        int err;
1474
1475        rtnl_lock();
1476        err = i40evf_set_interrupt_capability(adapter);
1477        rtnl_unlock();
1478        if (err) {
1479                dev_err(&adapter->pdev->dev,
1480                        "Unable to setup interrupt capabilities\n");
1481                goto err_set_interrupt;
1482        }
1483
1484        err = i40evf_alloc_q_vectors(adapter);
1485        if (err) {
1486                dev_err(&adapter->pdev->dev,
1487                        "Unable to allocate memory for queue vectors\n");
1488                goto err_alloc_q_vectors;
1489        }
1490
1491        err = i40evf_alloc_queues(adapter);
1492        if (err) {
1493                dev_err(&adapter->pdev->dev,
1494                        "Unable to allocate memory for queues\n");
1495                goto err_alloc_queues;
1496        }
1497
1498        dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1499                 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1500                 adapter->num_active_queues);
1501
1502        return 0;
1503err_alloc_queues:
1504        i40evf_free_q_vectors(adapter);
1505err_alloc_q_vectors:
1506        i40evf_reset_interrupt_capability(adapter);
1507err_set_interrupt:
1508        return err;
1509}
1510
1511/**
1512 * i40evf_free_rss - Free memory used by RSS structs
1513 * @adapter: board private structure
1514 **/
1515static void i40evf_free_rss(struct i40evf_adapter *adapter)
1516{
1517        kfree(adapter->rss_key);
1518        adapter->rss_key = NULL;
1519
1520        kfree(adapter->rss_lut);
1521        adapter->rss_lut = NULL;
1522}
1523
1524/**
1525 * i40evf_watchdog_timer - Periodic call-back timer
1526 * @data: pointer to adapter disguised as unsigned long
1527 **/
1528static void i40evf_watchdog_timer(unsigned long data)
1529{
1530        struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1531
1532        schedule_work(&adapter->watchdog_task);
1533        /* timer will be rescheduled in watchdog task */
1534}
1535
1536/**
1537 * i40evf_watchdog_task - Periodic call-back task
1538 * @work: pointer to work_struct
1539 **/
1540static void i40evf_watchdog_task(struct work_struct *work)
1541{
1542        struct i40evf_adapter *adapter = container_of(work,
1543                                                      struct i40evf_adapter,
1544                                                      watchdog_task);
1545        struct i40e_hw *hw = &adapter->hw;
1546        u32 reg_val;
1547
1548        if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1549                goto restart_watchdog;
1550
1551        if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1552                reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1553                          I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1554                if ((reg_val == I40E_VFR_VFACTIVE) ||
1555                    (reg_val == I40E_VFR_COMPLETED)) {
1556                        /* A chance for redemption! */
1557                        dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1558                        adapter->state = __I40EVF_STARTUP;
1559                        adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1560                        schedule_delayed_work(&adapter->init_task, 10);
1561                        clear_bit(__I40EVF_IN_CRITICAL_TASK,
1562                                  &adapter->crit_section);
1563                        /* Don't reschedule the watchdog, since we've restarted
1564                         * the init task. When init_task contacts the PF and
1565                         * gets everything set up again, it'll restart the
1566                         * watchdog for us. Down, boy. Sit. Stay. Woof.
1567                         */
1568                        return;
1569                }
1570                adapter->aq_required = 0;
1571                adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1572                goto watchdog_done;
1573        }
1574
1575        if ((adapter->state < __I40EVF_DOWN) ||
1576            (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1577                goto watchdog_done;
1578
1579        /* check for reset */
1580        reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1581        if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
1582                adapter->state = __I40EVF_RESETTING;
1583                adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1584                dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1585                schedule_work(&adapter->reset_task);
1586                adapter->aq_required = 0;
1587                adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1588                goto watchdog_done;
1589        }
1590
1591        /* Process admin queue tasks. After init, everything gets done
1592         * here so we don't race on the admin queue.
1593         */
1594        if (adapter->current_op) {
1595                if (!i40evf_asq_done(hw)) {
1596                        dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1597                        i40evf_send_api_ver(adapter);
1598                }
1599                goto watchdog_done;
1600        }
1601        if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1602                i40evf_send_vf_config_msg(adapter);
1603                goto watchdog_done;
1604        }
1605
1606        if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1607                i40evf_disable_queues(adapter);
1608                goto watchdog_done;
1609        }
1610
1611        if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1612                i40evf_map_queues(adapter);
1613                goto watchdog_done;
1614        }
1615
1616        if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1617                i40evf_add_ether_addrs(adapter);
1618                goto watchdog_done;
1619        }
1620
1621        if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1622                i40evf_add_vlans(adapter);
1623                goto watchdog_done;
1624        }
1625
1626        if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1627                i40evf_del_ether_addrs(adapter);
1628                goto watchdog_done;
1629        }
1630
1631        if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1632                i40evf_del_vlans(adapter);
1633                goto watchdog_done;
1634        }
1635
1636        if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1637                i40evf_configure_queues(adapter);
1638                goto watchdog_done;
1639        }
1640
1641        if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1642                i40evf_enable_queues(adapter);
1643                goto watchdog_done;
1644        }
1645
1646        if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1647                /* This message goes straight to the firmware, not the
1648                 * PF, so we don't have to set current_op as we will
1649                 * not get a response through the ARQ.
1650                 */
1651                i40evf_init_rss(adapter);
1652                adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1653                goto watchdog_done;
1654        }
1655        if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1656                i40evf_get_hena(adapter);
1657                goto watchdog_done;
1658        }
1659        if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1660                i40evf_set_hena(adapter);
1661                goto watchdog_done;
1662        }
1663        if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1664                i40evf_set_rss_key(adapter);
1665                goto watchdog_done;
1666        }
1667        if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1668                i40evf_set_rss_lut(adapter);
1669                goto watchdog_done;
1670        }
1671
1672        if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1673                i40evf_set_promiscuous(adapter, I40E_FLAG_VF_UNICAST_PROMISC |
1674                                       I40E_FLAG_VF_MULTICAST_PROMISC);
1675                goto watchdog_done;
1676        }
1677
1678        if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1679                i40evf_set_promiscuous(adapter, I40E_FLAG_VF_MULTICAST_PROMISC);
1680                goto watchdog_done;
1681        }
1682
1683        if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1684            (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1685                i40evf_set_promiscuous(adapter, 0);
1686                goto watchdog_done;
1687        }
1688
1689        if (adapter->state == __I40EVF_RUNNING)
1690                i40evf_request_stats(adapter);
1691watchdog_done:
1692        if (adapter->state == __I40EVF_RUNNING) {
1693                i40evf_irq_enable_queues(adapter, ~0);
1694                i40evf_fire_sw_int(adapter, 0xFF);
1695        } else {
1696                i40evf_fire_sw_int(adapter, 0x1);
1697        }
1698
1699        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1700restart_watchdog:
1701        if (adapter->state == __I40EVF_REMOVE)
1702                return;
1703        if (adapter->aq_required)
1704                mod_timer(&adapter->watchdog_timer,
1705                          jiffies + msecs_to_jiffies(20));
1706        else
1707                mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1708        schedule_work(&adapter->adminq_task);
1709}
1710
1711static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1712{
1713        struct i40evf_mac_filter *f, *ftmp;
1714        struct i40evf_vlan_filter *fv, *fvtmp;
1715
1716        adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1717
1718        if (netif_running(adapter->netdev)) {
1719                set_bit(__I40E_DOWN, &adapter->vsi.state);
1720                netif_carrier_off(adapter->netdev);
1721                netif_tx_disable(adapter->netdev);
1722                adapter->link_up = false;
1723                i40evf_napi_disable_all(adapter);
1724                i40evf_irq_disable(adapter);
1725                i40evf_free_traffic_irqs(adapter);
1726                i40evf_free_all_tx_resources(adapter);
1727                i40evf_free_all_rx_resources(adapter);
1728        }
1729
1730        /* Delete all of the filters, both MAC and VLAN. */
1731        list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1732                list_del(&f->list);
1733                kfree(f);
1734        }
1735
1736        list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1737                list_del(&fv->list);
1738                kfree(fv);
1739        }
1740
1741        i40evf_free_misc_irq(adapter);
1742        i40evf_reset_interrupt_capability(adapter);
1743        i40evf_free_queues(adapter);
1744        i40evf_free_q_vectors(adapter);
1745        kfree(adapter->vf_res);
1746        i40evf_shutdown_adminq(&adapter->hw);
1747        adapter->netdev->flags &= ~IFF_UP;
1748        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1749        adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1750        adapter->state = __I40EVF_DOWN;
1751        dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1752}
1753
1754#define I40EVF_RESET_WAIT_MS 10
1755#define I40EVF_RESET_WAIT_COUNT 500
1756/**
1757 * i40evf_reset_task - Call-back task to handle hardware reset
1758 * @work: pointer to work_struct
1759 *
1760 * During reset we need to shut down and reinitialize the admin queue
1761 * before we can use it to communicate with the PF again. We also clear
1762 * and reinit the rings because that context is lost as well.
1763 **/
1764static void i40evf_reset_task(struct work_struct *work)
1765{
1766        struct i40evf_adapter *adapter = container_of(work,
1767                                                      struct i40evf_adapter,
1768                                                      reset_task);
1769        struct net_device *netdev = adapter->netdev;
1770        struct i40e_hw *hw = &adapter->hw;
1771        struct i40evf_vlan_filter *vlf;
1772        struct i40evf_mac_filter *f;
1773        u32 reg_val;
1774        int i = 0, err;
1775
1776        while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1777                                &adapter->crit_section))
1778                usleep_range(500, 1000);
1779
1780        i40evf_misc_irq_disable(adapter);
1781        if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1782                adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1783                /* Restart the AQ here. If we have been reset but didn't
1784                 * detect it, or if the PF had to reinit, our AQ will be hosed.
1785                 */
1786                i40evf_shutdown_adminq(hw);
1787                i40evf_init_adminq(hw);
1788                i40evf_request_reset(adapter);
1789        }
1790        adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1791
1792        /* poll until we see the reset actually happen */
1793        for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1794                reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1795                          I40E_VF_ARQLEN1_ARQENABLE_MASK;
1796                if (!reg_val)
1797                        break;
1798                usleep_range(5000, 10000);
1799        }
1800        if (i == I40EVF_RESET_WAIT_COUNT) {
1801                dev_info(&adapter->pdev->dev, "Never saw reset\n");
1802                goto continue_reset; /* act like the reset happened */
1803        }
1804
1805        /* wait until the reset is complete and the PF is responding to us */
1806        for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1807                /* sleep first to make sure a minimum wait time is met */
1808                msleep(I40EVF_RESET_WAIT_MS);
1809
1810                reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1811                          I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1812                if (reg_val == I40E_VFR_VFACTIVE)
1813                        break;
1814        }
1815
1816        pci_set_master(adapter->pdev);
1817
1818        if (i == I40EVF_RESET_WAIT_COUNT) {
1819                dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1820                        reg_val);
1821                i40evf_disable_vf(adapter);
1822                return; /* Do not attempt to reinit. It's dead, Jim. */
1823        }
1824
1825continue_reset:
1826        if (netif_running(adapter->netdev)) {
1827                netif_carrier_off(netdev);
1828                netif_tx_stop_all_queues(netdev);
1829                adapter->link_up = false;
1830                i40evf_napi_disable_all(adapter);
1831        }
1832        i40evf_irq_disable(adapter);
1833
1834        adapter->state = __I40EVF_RESETTING;
1835        adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1836
1837        /* free the Tx/Rx rings and descriptors, might be better to just
1838         * re-use them sometime in the future
1839         */
1840        i40evf_free_all_rx_resources(adapter);
1841        i40evf_free_all_tx_resources(adapter);
1842
1843        /* kill and reinit the admin queue */
1844        i40evf_shutdown_adminq(hw);
1845        adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1846        err = i40evf_init_adminq(hw);
1847        if (err)
1848                dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1849                         err);
1850
1851        adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1852        adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1853
1854        /* re-add all MAC filters */
1855        list_for_each_entry(f, &adapter->mac_filter_list, list) {
1856                f->add = true;
1857        }
1858        /* re-add all VLAN filters */
1859        list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1860                vlf->add = true;
1861        }
1862        adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1863        adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1864        /* Open RDMA Client again */
1865        adapter->aq_required |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
1866        clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1867        i40evf_misc_irq_enable(adapter);
1868
1869        mod_timer(&adapter->watchdog_timer, jiffies + 2);
1870
1871        if (netif_running(adapter->netdev)) {
1872                /* allocate transmit descriptors */
1873                err = i40evf_setup_all_tx_resources(adapter);
1874                if (err)
1875                        goto reset_err;
1876
1877                /* allocate receive descriptors */
1878                err = i40evf_setup_all_rx_resources(adapter);
1879                if (err)
1880                        goto reset_err;
1881
1882                i40evf_configure(adapter);
1883
1884                i40evf_up_complete(adapter);
1885
1886                i40evf_irq_enable(adapter, true);
1887        } else {
1888                adapter->state = __I40EVF_DOWN;
1889        }
1890
1891        return;
1892reset_err:
1893        dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1894        i40evf_close(adapter->netdev);
1895}
1896
1897/**
1898 * i40evf_adminq_task - worker thread to clean the admin queue
1899 * @work: pointer to work_struct containing our data
1900 **/
1901static void i40evf_adminq_task(struct work_struct *work)
1902{
1903        struct i40evf_adapter *adapter =
1904                container_of(work, struct i40evf_adapter, adminq_task);
1905        struct i40e_hw *hw = &adapter->hw;
1906        struct i40e_arq_event_info event;
1907        struct i40e_virtchnl_msg *v_msg;
1908        i40e_status ret;
1909        u32 val, oldval;
1910        u16 pending;
1911
1912        if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1913                goto out;
1914
1915        event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1916        event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1917        if (!event.msg_buf)
1918                goto out;
1919
1920        v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1921        do {
1922                ret = i40evf_clean_arq_element(hw, &event, &pending);
1923                if (ret || !v_msg->v_opcode)
1924                        break; /* No event to process or error cleaning ARQ */
1925
1926                i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1927                                           v_msg->v_retval, event.msg_buf,
1928                                           event.msg_len);
1929                if (pending != 0)
1930                        memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1931        } while (pending);
1932
1933        if ((adapter->flags &
1934             (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1935            adapter->state == __I40EVF_RESETTING)
1936                goto freedom;
1937
1938        /* check for error indications */
1939        val = rd32(hw, hw->aq.arq.len);
1940        if (val == 0xdeadbeef) /* indicates device in reset */
1941                goto freedom;
1942        oldval = val;
1943        if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
1944                dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1945                val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
1946        }
1947        if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
1948                dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1949                val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
1950        }
1951        if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
1952                dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1953                val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
1954        }
1955        if (oldval != val)
1956                wr32(hw, hw->aq.arq.len, val);
1957
1958        val = rd32(hw, hw->aq.asq.len);
1959        oldval = val;
1960        if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
1961                dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1962                val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
1963        }
1964        if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
1965                dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1966                val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
1967        }
1968        if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
1969                dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1970                val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
1971        }
1972        if (oldval != val)
1973                wr32(hw, hw->aq.asq.len, val);
1974
1975freedom:
1976        kfree(event.msg_buf);
1977out:
1978        /* re-enable Admin queue interrupt cause */
1979        i40evf_misc_irq_enable(adapter);
1980}
1981
1982/**
1983 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1984 * @adapter: board private structure
1985 *
1986 * Free all transmit software resources
1987 **/
1988void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1989{
1990        int i;
1991
1992        if (!adapter->tx_rings)
1993                return;
1994
1995        for (i = 0; i < adapter->num_active_queues; i++)
1996                if (adapter->tx_rings[i].desc)
1997                        i40evf_free_tx_resources(&adapter->tx_rings[i]);
1998}
1999
2000/**
2001 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2002 * @adapter: board private structure
2003 *
2004 * If this function returns with an error, then it's possible one or
2005 * more of the rings is populated (while the rest are not).  It is the
2006 * callers duty to clean those orphaned rings.
2007 *
2008 * Return 0 on success, negative on failure
2009 **/
2010static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2011{
2012        int i, err = 0;
2013
2014        for (i = 0; i < adapter->num_active_queues; i++) {
2015                adapter->tx_rings[i].count = adapter->tx_desc_count;
2016                err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
2017                if (!err)
2018                        continue;
2019                dev_err(&adapter->pdev->dev,
2020                        "Allocation for Tx Queue %u failed\n", i);
2021                break;
2022        }
2023
2024        return err;
2025}
2026
2027/**
2028 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2029 * @adapter: board private structure
2030 *
2031 * If this function returns with an error, then it's possible one or
2032 * more of the rings is populated (while the rest are not).  It is the
2033 * callers duty to clean those orphaned rings.
2034 *
2035 * Return 0 on success, negative on failure
2036 **/
2037static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2038{
2039        int i, err = 0;
2040
2041        for (i = 0; i < adapter->num_active_queues; i++) {
2042                adapter->rx_rings[i].count = adapter->rx_desc_count;
2043                err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
2044                if (!err)
2045                        continue;
2046                dev_err(&adapter->pdev->dev,
2047                        "Allocation for Rx Queue %u failed\n", i);
2048                break;
2049        }
2050        return err;
2051}
2052
2053/**
2054 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2055 * @adapter: board private structure
2056 *
2057 * Free all receive software resources
2058 **/
2059void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
2060{
2061        int i;
2062
2063        if (!adapter->rx_rings)
2064                return;
2065
2066        for (i = 0; i < adapter->num_active_queues; i++)
2067                if (adapter->rx_rings[i].desc)
2068                        i40evf_free_rx_resources(&adapter->rx_rings[i]);
2069}
2070
2071/**
2072 * i40evf_open - Called when a network interface is made active
2073 * @netdev: network interface device structure
2074 *
2075 * Returns 0 on success, negative value on failure
2076 *
2077 * The open entry point is called when a network interface is made
2078 * active by the system (IFF_UP).  At this point all resources needed
2079 * for transmit and receive operations are allocated, the interrupt
2080 * handler is registered with the OS, the watchdog timer is started,
2081 * and the stack is notified that the interface is ready.
2082 **/
2083static int i40evf_open(struct net_device *netdev)
2084{
2085        struct i40evf_adapter *adapter = netdev_priv(netdev);
2086        int err;
2087
2088        if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2089                dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2090                return -EIO;
2091        }
2092
2093        if (adapter->state != __I40EVF_DOWN)
2094                return -EBUSY;
2095
2096        /* allocate transmit descriptors */
2097        err = i40evf_setup_all_tx_resources(adapter);
2098        if (err)
2099                goto err_setup_tx;
2100
2101        /* allocate receive descriptors */
2102        err = i40evf_setup_all_rx_resources(adapter);
2103        if (err)
2104                goto err_setup_rx;
2105
2106        /* clear any pending interrupts, may auto mask */
2107        err = i40evf_request_traffic_irqs(adapter, netdev->name);
2108        if (err)
2109                goto err_req_irq;
2110
2111        i40evf_add_filter(adapter, adapter->hw.mac.addr);
2112        i40evf_configure(adapter);
2113
2114        i40evf_up_complete(adapter);
2115
2116        i40evf_irq_enable(adapter, true);
2117
2118        return 0;
2119
2120err_req_irq:
2121        i40evf_down(adapter);
2122        i40evf_free_traffic_irqs(adapter);
2123err_setup_rx:
2124        i40evf_free_all_rx_resources(adapter);
2125err_setup_tx:
2126        i40evf_free_all_tx_resources(adapter);
2127
2128        return err;
2129}
2130
2131/**
2132 * i40evf_close - Disables a network interface
2133 * @netdev: network interface device structure
2134 *
2135 * Returns 0, this is not allowed to fail
2136 *
2137 * The close entry point is called when an interface is de-activated
2138 * by the OS.  The hardware is still under the drivers control, but
2139 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2140 * are freed, along with all transmit and receive resources.
2141 **/
2142static int i40evf_close(struct net_device *netdev)
2143{
2144        struct i40evf_adapter *adapter = netdev_priv(netdev);
2145
2146        if (adapter->state <= __I40EVF_DOWN_PENDING)
2147                return 0;
2148
2149
2150        set_bit(__I40E_DOWN, &adapter->vsi.state);
2151
2152        i40evf_down(adapter);
2153        adapter->state = __I40EVF_DOWN_PENDING;
2154        i40evf_free_traffic_irqs(adapter);
2155
2156        /* We explicitly don't free resources here because the hardware is
2157         * still active and can DMA into memory. Resources are cleared in
2158         * i40evf_virtchnl_completion() after we get confirmation from the PF
2159         * driver that the rings have been stopped.
2160         */
2161        return 0;
2162}
2163
2164/**
2165 * i40evf_get_stats - Get System Network Statistics
2166 * @netdev: network interface device structure
2167 *
2168 * Returns the address of the device statistics structure.
2169 * The statistics are actually updated from the timer callback.
2170 **/
2171static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2172{
2173        struct i40evf_adapter *adapter = netdev_priv(netdev);
2174
2175        /* only return the current stats */
2176        return &adapter->net_stats;
2177}
2178
2179/**
2180 * i40evf_change_mtu - Change the Maximum Transfer Unit
2181 * @netdev: network interface device structure
2182 * @new_mtu: new value for maximum frame size
2183 *
2184 * Returns 0 on success, negative on failure
2185 **/
2186static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2187{
2188        struct i40evf_adapter *adapter = netdev_priv(netdev);
2189
2190        netdev->mtu = new_mtu;
2191        adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2192        schedule_work(&adapter->reset_task);
2193
2194        return 0;
2195}
2196
2197/**
2198 * i40evf_features_check - Validate encapsulated packet conforms to limits
2199 * @skb: skb buff
2200 * @netdev: This physical port's netdev
2201 * @features: Offload features that the stack believes apply
2202 **/
2203static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2204                                               struct net_device *dev,
2205                                               netdev_features_t features)
2206{
2207        size_t len;
2208
2209        /* No point in doing any of this if neither checksum nor GSO are
2210         * being requested for this frame.  We can rule out both by just
2211         * checking for CHECKSUM_PARTIAL
2212         */
2213        if (skb->ip_summed != CHECKSUM_PARTIAL)
2214                return features;
2215
2216        /* We cannot support GSO if the MSS is going to be less than
2217         * 64 bytes.  If it is then we need to drop support for GSO.
2218         */
2219        if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2220                features &= ~NETIF_F_GSO_MASK;
2221
2222        /* MACLEN can support at most 63 words */
2223        len = skb_network_header(skb) - skb->data;
2224        if (len & ~(63 * 2))
2225                goto out_err;
2226
2227        /* IPLEN and EIPLEN can support at most 127 dwords */
2228        len = skb_transport_header(skb) - skb_network_header(skb);
2229        if (len & ~(127 * 4))
2230                goto out_err;
2231
2232        if (skb->encapsulation) {
2233                /* L4TUNLEN can support 127 words */
2234                len = skb_inner_network_header(skb) - skb_transport_header(skb);
2235                if (len & ~(127 * 2))
2236                        goto out_err;
2237
2238                /* IPLEN can support at most 127 dwords */
2239                len = skb_inner_transport_header(skb) -
2240                      skb_inner_network_header(skb);
2241                if (len & ~(127 * 4))
2242                        goto out_err;
2243        }
2244
2245        /* No need to validate L4LEN as TCP is the only protocol with a
2246         * a flexible value and we support all possible values supported
2247         * by TCP, which is at most 15 dwords
2248         */
2249
2250        return features;
2251out_err:
2252        return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2253}
2254
2255#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2256                              NETIF_F_HW_VLAN_CTAG_RX |\
2257                              NETIF_F_HW_VLAN_CTAG_FILTER)
2258
2259/**
2260 * i40evf_fix_features - fix up the netdev feature bits
2261 * @netdev: our net device
2262 * @features: desired feature bits
2263 *
2264 * Returns fixed-up features bits
2265 **/
2266static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2267                                             netdev_features_t features)
2268{
2269        struct i40evf_adapter *adapter = netdev_priv(netdev);
2270
2271        features &= ~I40EVF_VLAN_FEATURES;
2272        if (adapter->vf_res->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN)
2273                features |= I40EVF_VLAN_FEATURES;
2274        return features;
2275}
2276
2277static const struct net_device_ops i40evf_netdev_ops = {
2278        .ndo_open               = i40evf_open,
2279        .ndo_stop               = i40evf_close,
2280        .ndo_start_xmit         = i40evf_xmit_frame,
2281        .ndo_get_stats          = i40evf_get_stats,
2282        .ndo_set_rx_mode        = i40evf_set_rx_mode,
2283        .ndo_validate_addr      = eth_validate_addr,
2284        .ndo_set_mac_address    = i40evf_set_mac,
2285        .ndo_change_mtu         = i40evf_change_mtu,
2286        .ndo_tx_timeout         = i40evf_tx_timeout,
2287        .ndo_vlan_rx_add_vid    = i40evf_vlan_rx_add_vid,
2288        .ndo_vlan_rx_kill_vid   = i40evf_vlan_rx_kill_vid,
2289        .ndo_features_check     = i40evf_features_check,
2290        .ndo_fix_features       = i40evf_fix_features,
2291#ifdef CONFIG_NET_POLL_CONTROLLER
2292        .ndo_poll_controller    = i40evf_netpoll,
2293#endif
2294};
2295
2296/**
2297 * i40evf_check_reset_complete - check that VF reset is complete
2298 * @hw: pointer to hw struct
2299 *
2300 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2301 **/
2302static int i40evf_check_reset_complete(struct i40e_hw *hw)
2303{
2304        u32 rstat;
2305        int i;
2306
2307        for (i = 0; i < 100; i++) {
2308                rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2309                            I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2310                if ((rstat == I40E_VFR_VFACTIVE) ||
2311                    (rstat == I40E_VFR_COMPLETED))
2312                        return 0;
2313                usleep_range(10, 20);
2314        }
2315        return -EBUSY;
2316}
2317
2318/**
2319 * i40evf_process_config - Process the config information we got from the PF
2320 * @adapter: board private structure
2321 *
2322 * Verify that we have a valid config struct, and set up our netdev features
2323 * and our VSI struct.
2324 **/
2325int i40evf_process_config(struct i40evf_adapter *adapter)
2326{
2327        struct i40e_virtchnl_vf_resource *vfres = adapter->vf_res;
2328        struct net_device *netdev = adapter->netdev;
2329        struct i40e_vsi *vsi = &adapter->vsi;
2330        int i;
2331
2332        /* got VF config message back from PF, now we can parse it */
2333        for (i = 0; i < vfres->num_vsis; i++) {
2334                if (vfres->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2335                        adapter->vsi_res = &vfres->vsi_res[i];
2336        }
2337        if (!adapter->vsi_res) {
2338                dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2339                return -ENODEV;
2340        }
2341
2342        netdev->hw_enc_features |= NETIF_F_SG                   |
2343                                   NETIF_F_IP_CSUM              |
2344                                   NETIF_F_IPV6_CSUM            |
2345                                   NETIF_F_HIGHDMA              |
2346                                   NETIF_F_SOFT_FEATURES        |
2347                                   NETIF_F_TSO                  |
2348                                   NETIF_F_TSO_ECN              |
2349                                   NETIF_F_TSO6                 |
2350                                   NETIF_F_GSO_GRE              |
2351                                   NETIF_F_GSO_GRE_CSUM         |
2352                                   NETIF_F_GSO_IPXIP4           |
2353                                   NETIF_F_GSO_IPXIP6           |
2354                                   NETIF_F_GSO_UDP_TUNNEL       |
2355                                   NETIF_F_GSO_UDP_TUNNEL_CSUM  |
2356                                   NETIF_F_GSO_PARTIAL          |
2357                                   NETIF_F_SCTP_CRC             |
2358                                   NETIF_F_RXHASH               |
2359                                   NETIF_F_RXCSUM               |
2360                                   0;
2361
2362        if (!(adapter->flags & I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE))
2363                netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2364
2365        netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2366
2367        /* record features VLANs can make use of */
2368        netdev->vlan_features |= netdev->hw_enc_features |
2369                                 NETIF_F_TSO_MANGLEID;
2370
2371        /* Write features and hw_features separately to avoid polluting
2372         * with, or dropping, features that are set when we registgered.
2373         */
2374        netdev->hw_features |= netdev->hw_enc_features;
2375
2376        netdev->features |= netdev->hw_enc_features | I40EVF_VLAN_FEATURES;
2377        netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2378
2379        /* disable VLAN features if not supported */
2380        if (!(vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN))
2381                netdev->features ^= I40EVF_VLAN_FEATURES;
2382
2383        adapter->vsi.id = adapter->vsi_res->vsi_id;
2384
2385        adapter->vsi.back = adapter;
2386        adapter->vsi.base_vector = 1;
2387        adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2388        vsi->netdev = adapter->netdev;
2389        vsi->qs_handle = adapter->vsi_res->qset_handle;
2390        if (vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2391                adapter->rss_key_size = vfres->rss_key_size;
2392                adapter->rss_lut_size = vfres->rss_lut_size;
2393        } else {
2394                adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2395                adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2396        }
2397
2398        return 0;
2399}
2400
2401/**
2402 * i40evf_init_task - worker thread to perform delayed initialization
2403 * @work: pointer to work_struct containing our data
2404 *
2405 * This task completes the work that was begun in probe. Due to the nature
2406 * of VF-PF communications, we may need to wait tens of milliseconds to get
2407 * responses back from the PF. Rather than busy-wait in probe and bog down the
2408 * whole system, we'll do it in a task so we can sleep.
2409 * This task only runs during driver init. Once we've established
2410 * communications with the PF driver and set up our netdev, the watchdog
2411 * takes over.
2412 **/
2413static void i40evf_init_task(struct work_struct *work)
2414{
2415        struct i40evf_adapter *adapter = container_of(work,
2416                                                      struct i40evf_adapter,
2417                                                      init_task.work);
2418        struct net_device *netdev = adapter->netdev;
2419        struct i40e_hw *hw = &adapter->hw;
2420        struct pci_dev *pdev = adapter->pdev;
2421        int err, bufsz;
2422
2423        switch (adapter->state) {
2424        case __I40EVF_STARTUP:
2425                /* driver loaded, probe complete */
2426                adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2427                adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2428                err = i40e_set_mac_type(hw);
2429                if (err) {
2430                        dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2431                                err);
2432                        goto err;
2433                }
2434                err = i40evf_check_reset_complete(hw);
2435                if (err) {
2436                        dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2437                                 err);
2438                        goto err;
2439                }
2440                hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2441                hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2442                hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2443                hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2444
2445                err = i40evf_init_adminq(hw);
2446                if (err) {
2447                        dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2448                                err);
2449                        goto err;
2450                }
2451                err = i40evf_send_api_ver(adapter);
2452                if (err) {
2453                        dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2454                        i40evf_shutdown_adminq(hw);
2455                        goto err;
2456                }
2457                adapter->state = __I40EVF_INIT_VERSION_CHECK;
2458                goto restart;
2459        case __I40EVF_INIT_VERSION_CHECK:
2460                if (!i40evf_asq_done(hw)) {
2461                        dev_err(&pdev->dev, "Admin queue command never completed\n");
2462                        i40evf_shutdown_adminq(hw);
2463                        adapter->state = __I40EVF_STARTUP;
2464                        goto err;
2465                }
2466
2467                /* aq msg sent, awaiting reply */
2468                err = i40evf_verify_api_ver(adapter);
2469                if (err) {
2470                        if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2471                                err = i40evf_send_api_ver(adapter);
2472                        else
2473                                dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2474                                        adapter->pf_version.major,
2475                                        adapter->pf_version.minor,
2476                                        I40E_VIRTCHNL_VERSION_MAJOR,
2477                                        I40E_VIRTCHNL_VERSION_MINOR);
2478                        goto err;
2479                }
2480                err = i40evf_send_vf_config_msg(adapter);
2481                if (err) {
2482                        dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2483                                err);
2484                        goto err;
2485                }
2486                adapter->state = __I40EVF_INIT_GET_RESOURCES;
2487                goto restart;
2488        case __I40EVF_INIT_GET_RESOURCES:
2489                /* aq msg sent, awaiting reply */
2490                if (!adapter->vf_res) {
2491                        bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2492                                (I40E_MAX_VF_VSI *
2493                                 sizeof(struct i40e_virtchnl_vsi_resource));
2494                        adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2495                        if (!adapter->vf_res)
2496                                goto err;
2497                }
2498                err = i40evf_get_vf_config(adapter);
2499                if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2500                        err = i40evf_send_vf_config_msg(adapter);
2501                        goto err;
2502                } else if (err == I40E_ERR_PARAM) {
2503                        /* We only get ERR_PARAM if the device is in a very bad
2504                         * state or if we've been disabled for previous bad
2505                         * behavior. Either way, we're done now.
2506                         */
2507                        i40evf_shutdown_adminq(hw);
2508                        dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2509                        return;
2510                }
2511                if (err) {
2512                        dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2513                                err);
2514                        goto err_alloc;
2515                }
2516                adapter->state = __I40EVF_INIT_SW;
2517                break;
2518        default:
2519                goto err_alloc;
2520        }
2521
2522        if (hw->mac.type == I40E_MAC_X722_VF)
2523                adapter->flags |= I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE;
2524
2525        if (i40evf_process_config(adapter))
2526                goto err_alloc;
2527        adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
2528
2529        adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2530
2531        netdev->netdev_ops = &i40evf_netdev_ops;
2532        i40evf_set_ethtool_ops(netdev);
2533        netdev->watchdog_timeo = 5 * HZ;
2534
2535        /* MTU range: 68 - 9710 */
2536        netdev->min_mtu = ETH_MIN_MTU;
2537        netdev->max_mtu = I40E_MAX_RXBUFFER - (ETH_HLEN + ETH_FCS_LEN);
2538
2539        if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2540                dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2541                         adapter->hw.mac.addr);
2542                eth_hw_addr_random(netdev);
2543                ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2544        } else {
2545                adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2546                ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2547                ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2548        }
2549
2550        init_timer(&adapter->watchdog_timer);
2551        adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2552        adapter->watchdog_timer.data = (unsigned long)adapter;
2553        mod_timer(&adapter->watchdog_timer, jiffies + 1);
2554
2555        adapter->num_active_queues = min_t(int,
2556                                           adapter->vsi_res->num_queue_pairs,
2557                                           (int)(num_online_cpus()));
2558        adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2559        adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2560        err = i40evf_init_interrupt_scheme(adapter);
2561        if (err)
2562                goto err_sw_init;
2563        i40evf_map_rings_to_vectors(adapter);
2564        if (adapter->vf_res->vf_offload_flags &
2565            I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2566                adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2567
2568        err = i40evf_request_misc_irq(adapter);
2569        if (err)
2570                goto err_sw_init;
2571
2572        netif_carrier_off(netdev);
2573        adapter->link_up = false;
2574
2575        if (!adapter->netdev_registered) {
2576                err = register_netdev(netdev);
2577                if (err)
2578                        goto err_register;
2579        }
2580
2581        adapter->netdev_registered = true;
2582
2583        netif_tx_stop_all_queues(netdev);
2584
2585        dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2586        if (netdev->features & NETIF_F_GRO)
2587                dev_info(&pdev->dev, "GRO is enabled\n");
2588
2589        adapter->state = __I40EVF_DOWN;
2590        set_bit(__I40E_DOWN, &adapter->vsi.state);
2591        i40evf_misc_irq_enable(adapter);
2592
2593        adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2594        adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2595        if (!adapter->rss_key || !adapter->rss_lut)
2596                goto err_mem;
2597
2598        if (RSS_AQ(adapter)) {
2599                adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2600                mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2601        } else {
2602                i40evf_init_rss(adapter);
2603        }
2604        return;
2605restart:
2606        schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
2607        return;
2608err_mem:
2609        i40evf_free_rss(adapter);
2610err_register:
2611        i40evf_free_misc_irq(adapter);
2612err_sw_init:
2613        i40evf_reset_interrupt_capability(adapter);
2614err_alloc:
2615        kfree(adapter->vf_res);
2616        adapter->vf_res = NULL;
2617err:
2618        /* Things went into the weeds, so try again later */
2619        if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2620                dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
2621                adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2622                i40evf_shutdown_adminq(hw);
2623                adapter->state = __I40EVF_STARTUP;
2624                schedule_delayed_work(&adapter->init_task, HZ * 5);
2625                return;
2626        }
2627        schedule_delayed_work(&adapter->init_task, HZ);
2628}
2629
2630/**
2631 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2632 * @pdev: pci device structure
2633 **/
2634static void i40evf_shutdown(struct pci_dev *pdev)
2635{
2636        struct net_device *netdev = pci_get_drvdata(pdev);
2637        struct i40evf_adapter *adapter = netdev_priv(netdev);
2638
2639        netif_device_detach(netdev);
2640
2641        if (netif_running(netdev))
2642                i40evf_close(netdev);
2643
2644        /* Prevent the watchdog from running. */
2645        adapter->state = __I40EVF_REMOVE;
2646        adapter->aq_required = 0;
2647
2648#ifdef CONFIG_PM
2649        pci_save_state(pdev);
2650
2651#endif
2652        pci_disable_device(pdev);
2653}
2654
2655/**
2656 * i40evf_probe - Device Initialization Routine
2657 * @pdev: PCI device information struct
2658 * @ent: entry in i40evf_pci_tbl
2659 *
2660 * Returns 0 on success, negative on failure
2661 *
2662 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2663 * The OS initialization, configuring of the adapter private structure,
2664 * and a hardware reset occur.
2665 **/
2666static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2667{
2668        struct net_device *netdev;
2669        struct i40evf_adapter *adapter = NULL;
2670        struct i40e_hw *hw = NULL;
2671        int err;
2672
2673        err = pci_enable_device(pdev);
2674        if (err)
2675                return err;
2676
2677        err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2678        if (err) {
2679                err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2680                if (err) {
2681                        dev_err(&pdev->dev,
2682                                "DMA configuration failed: 0x%x\n", err);
2683                        goto err_dma;
2684                }
2685        }
2686
2687        err = pci_request_regions(pdev, i40evf_driver_name);
2688        if (err) {
2689                dev_err(&pdev->dev,
2690                        "pci_request_regions failed 0x%x\n", err);
2691                goto err_pci_reg;
2692        }
2693
2694        pci_enable_pcie_error_reporting(pdev);
2695
2696        pci_set_master(pdev);
2697
2698        netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
2699        if (!netdev) {
2700                err = -ENOMEM;
2701                goto err_alloc_etherdev;
2702        }
2703
2704        SET_NETDEV_DEV(netdev, &pdev->dev);
2705
2706        pci_set_drvdata(pdev, netdev);
2707        adapter = netdev_priv(netdev);
2708
2709        adapter->netdev = netdev;
2710        adapter->pdev = pdev;
2711
2712        hw = &adapter->hw;
2713        hw->back = adapter;
2714
2715        adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2716        adapter->state = __I40EVF_STARTUP;
2717
2718        /* Call save state here because it relies on the adapter struct. */
2719        pci_save_state(pdev);
2720
2721        hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2722                              pci_resource_len(pdev, 0));
2723        if (!hw->hw_addr) {
2724                err = -EIO;
2725                goto err_ioremap;
2726        }
2727        hw->vendor_id = pdev->vendor;
2728        hw->device_id = pdev->device;
2729        pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2730        hw->subsystem_vendor_id = pdev->subsystem_vendor;
2731        hw->subsystem_device_id = pdev->subsystem_device;
2732        hw->bus.device = PCI_SLOT(pdev->devfn);
2733        hw->bus.func = PCI_FUNC(pdev->devfn);
2734        hw->bus.bus_id = pdev->bus->number;
2735
2736        /* set up the locks for the AQ, do this only once in probe
2737         * and destroy them only once in remove
2738         */
2739        mutex_init(&hw->aq.asq_mutex);
2740        mutex_init(&hw->aq.arq_mutex);
2741
2742        INIT_LIST_HEAD(&adapter->mac_filter_list);
2743        INIT_LIST_HEAD(&adapter->vlan_filter_list);
2744
2745        INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2746        INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2747        INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2748        INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2749        schedule_delayed_work(&adapter->init_task,
2750                              msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
2751
2752        return 0;
2753
2754err_ioremap:
2755        free_netdev(netdev);
2756err_alloc_etherdev:
2757        pci_release_regions(pdev);
2758err_pci_reg:
2759err_dma:
2760        pci_disable_device(pdev);
2761        return err;
2762}
2763
2764#ifdef CONFIG_PM
2765/**
2766 * i40evf_suspend - Power management suspend routine
2767 * @pdev: PCI device information struct
2768 * @state: unused
2769 *
2770 * Called when the system (VM) is entering sleep/suspend.
2771 **/
2772static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2773{
2774        struct net_device *netdev = pci_get_drvdata(pdev);
2775        struct i40evf_adapter *adapter = netdev_priv(netdev);
2776        int retval = 0;
2777
2778        netif_device_detach(netdev);
2779
2780        if (netif_running(netdev)) {
2781                rtnl_lock();
2782                i40evf_down(adapter);
2783                rtnl_unlock();
2784        }
2785        i40evf_free_misc_irq(adapter);
2786        i40evf_reset_interrupt_capability(adapter);
2787
2788        retval = pci_save_state(pdev);
2789        if (retval)
2790                return retval;
2791
2792        pci_disable_device(pdev);
2793
2794        return 0;
2795}
2796
2797/**
2798 * i40evf_resume - Power management resume routine
2799 * @pdev: PCI device information struct
2800 *
2801 * Called when the system (VM) is resumed from sleep/suspend.
2802 **/
2803static int i40evf_resume(struct pci_dev *pdev)
2804{
2805        struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2806        struct net_device *netdev = adapter->netdev;
2807        u32 err;
2808
2809        pci_set_power_state(pdev, PCI_D0);
2810        pci_restore_state(pdev);
2811        /* pci_restore_state clears dev->state_saved so call
2812         * pci_save_state to restore it.
2813         */
2814        pci_save_state(pdev);
2815
2816        err = pci_enable_device_mem(pdev);
2817        if (err) {
2818                dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2819                return err;
2820        }
2821        pci_set_master(pdev);
2822
2823        rtnl_lock();
2824        err = i40evf_set_interrupt_capability(adapter);
2825        if (err) {
2826                rtnl_unlock();
2827                dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2828                return err;
2829        }
2830        err = i40evf_request_misc_irq(adapter);
2831        rtnl_unlock();
2832        if (err) {
2833                dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2834                return err;
2835        }
2836
2837        schedule_work(&adapter->reset_task);
2838
2839        netif_device_attach(netdev);
2840
2841        return err;
2842}
2843
2844#endif /* CONFIG_PM */
2845/**
2846 * i40evf_remove - Device Removal Routine
2847 * @pdev: PCI device information struct
2848 *
2849 * i40evf_remove is called by the PCI subsystem to alert the driver
2850 * that it should release a PCI device.  The could be caused by a
2851 * Hot-Plug event, or because the driver is going to be removed from
2852 * memory.
2853 **/
2854static void i40evf_remove(struct pci_dev *pdev)
2855{
2856        struct net_device *netdev = pci_get_drvdata(pdev);
2857        struct i40evf_adapter *adapter = netdev_priv(netdev);
2858        struct i40evf_mac_filter *f, *ftmp;
2859        struct i40e_hw *hw = &adapter->hw;
2860
2861        cancel_delayed_work_sync(&adapter->init_task);
2862        cancel_work_sync(&adapter->reset_task);
2863
2864        if (adapter->netdev_registered) {
2865                unregister_netdev(netdev);
2866                adapter->netdev_registered = false;
2867        }
2868
2869        /* Shut down all the garbage mashers on the detention level */
2870        adapter->state = __I40EVF_REMOVE;
2871        adapter->aq_required = 0;
2872        i40evf_request_reset(adapter);
2873        msleep(50);
2874        /* If the FW isn't responding, kick it once, but only once. */
2875        if (!i40evf_asq_done(hw)) {
2876                i40evf_request_reset(adapter);
2877                msleep(50);
2878        }
2879        i40evf_free_all_tx_resources(adapter);
2880        i40evf_free_all_rx_resources(adapter);
2881        i40evf_misc_irq_disable(adapter);
2882        i40evf_free_misc_irq(adapter);
2883        i40evf_reset_interrupt_capability(adapter);
2884        i40evf_free_q_vectors(adapter);
2885
2886        if (adapter->watchdog_timer.function)
2887                del_timer_sync(&adapter->watchdog_timer);
2888
2889        flush_scheduled_work();
2890
2891        i40evf_free_rss(adapter);
2892
2893        if (hw->aq.asq.count)
2894                i40evf_shutdown_adminq(hw);
2895
2896        /* destroy the locks only once, here */
2897        mutex_destroy(&hw->aq.arq_mutex);
2898        mutex_destroy(&hw->aq.asq_mutex);
2899
2900        iounmap(hw->hw_addr);
2901        pci_release_regions(pdev);
2902        i40evf_free_all_tx_resources(adapter);
2903        i40evf_free_all_rx_resources(adapter);
2904        i40evf_free_queues(adapter);
2905        kfree(adapter->vf_res);
2906        /* If we got removed before an up/down sequence, we've got a filter
2907         * hanging out there that we need to get rid of.
2908         */
2909        list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2910                list_del(&f->list);
2911                kfree(f);
2912        }
2913        list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2914                list_del(&f->list);
2915                kfree(f);
2916        }
2917
2918        free_netdev(netdev);
2919
2920        pci_disable_pcie_error_reporting(pdev);
2921
2922        pci_disable_device(pdev);
2923}
2924
2925static struct pci_driver i40evf_driver = {
2926        .name     = i40evf_driver_name,
2927        .id_table = i40evf_pci_tbl,
2928        .probe    = i40evf_probe,
2929        .remove   = i40evf_remove,
2930#ifdef CONFIG_PM
2931        .suspend  = i40evf_suspend,
2932        .resume   = i40evf_resume,
2933#endif
2934        .shutdown = i40evf_shutdown,
2935};
2936
2937/**
2938 * i40e_init_module - Driver Registration Routine
2939 *
2940 * i40e_init_module is the first routine called when the driver is
2941 * loaded. All it does is register with the PCI subsystem.
2942 **/
2943static int __init i40evf_init_module(void)
2944{
2945        int ret;
2946
2947        pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2948                i40evf_driver_version);
2949
2950        pr_info("%s\n", i40evf_copyright);
2951
2952        i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
2953                                    i40evf_driver_name);
2954        if (!i40evf_wq) {
2955                pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2956                return -ENOMEM;
2957        }
2958        ret = pci_register_driver(&i40evf_driver);
2959        return ret;
2960}
2961
2962module_init(i40evf_init_module);
2963
2964/**
2965 * i40e_exit_module - Driver Exit Cleanup Routine
2966 *
2967 * i40e_exit_module is called just before the driver is removed
2968 * from memory.
2969 **/
2970static void __exit i40evf_exit_module(void)
2971{
2972        pci_unregister_driver(&i40evf_driver);
2973        destroy_workqueue(i40evf_wq);
2974}
2975
2976module_exit(i40evf_exit_module);
2977
2978/* i40evf_main.c */
2979