Linux Audio

Check our new training course

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