Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.13.7.
   1// SPDX-License-Identifier: GPL-2.0
   2/*******************************************************************************
   3 *
   4 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
   5 * Copyright(c) 2013 - 2016 Intel Corporation.
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms and conditions of the GNU General Public License,
   9 * version 2, as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along
  17 * with this program.  If not, see <http://www.gnu.org/licenses/>.
  18 *
  19 * The full GNU General Public License is included in this distribution in
  20 * the file called "COPYING".
  21 *
  22 * Contact Information:
  23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25 *
  26 ******************************************************************************/
  27
  28#include "i40evf.h"
  29#include "i40e_prototype.h"
  30#include "i40evf_client.h"
  31/* All i40evf tracepoints are defined by the include below, which must
  32 * be included exactly once across the whole kernel with
  33 * CREATE_TRACE_POINTS defined
  34 */
  35#define CREATE_TRACE_POINTS
  36#include "i40e_trace.h"
  37
  38static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
  39static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
  40static int i40evf_close(struct net_device *netdev);
  41
  42char i40evf_driver_name[] = "i40evf";
  43static const char i40evf_driver_string[] =
  44	"Intel(R) 40-10 Gigabit Virtual Function Network Driver";
  45
  46#define DRV_KERN "-k"
  47
  48#define DRV_VERSION_MAJOR 3
  49#define DRV_VERSION_MINOR 2
  50#define DRV_VERSION_BUILD 2
  51#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  52	     __stringify(DRV_VERSION_MINOR) "." \
  53	     __stringify(DRV_VERSION_BUILD) \
  54	     DRV_KERN
  55const char i40evf_driver_version[] = DRV_VERSION;
  56static const char i40evf_copyright[] =
  57	"Copyright (c) 2013 - 2015 Intel Corporation.";
  58
  59/* i40evf_pci_tbl - PCI Device ID Table
  60 *
  61 * Wildcard entries (PCI_ANY_ID) should come last
  62 * Last entry must be all 0s
  63 *
  64 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  65 *   Class, Class Mask, private data (not used) }
  66 */
  67static const struct pci_device_id i40evf_pci_tbl[] = {
  68	{PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
  69	{PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
  70	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
  71	{PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
  72	/* required last entry */
  73	{0, }
  74};
  75
  76MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
  77
  78MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  79MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
  80MODULE_LICENSE("GPL");
  81MODULE_VERSION(DRV_VERSION);
  82
  83static struct workqueue_struct *i40evf_wq;
  84
  85/**
  86 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
  87 * @hw:   pointer to the HW structure
  88 * @mem:  ptr to mem struct to fill out
  89 * @size: size of memory requested
  90 * @alignment: what to align the allocation to
  91 **/
  92i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
  93				      struct i40e_dma_mem *mem,
  94				      u64 size, u32 alignment)
  95{
  96	struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  97
  98	if (!mem)
  99		return I40E_ERR_PARAM;
 100
 101	mem->size = ALIGN(size, alignment);
 102	mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
 103				     (dma_addr_t *)&mem->pa, GFP_KERNEL);
 104	if (mem->va)
 105		return 0;
 106	else
 107		return I40E_ERR_NO_MEMORY;
 108}
 109
 110/**
 111 * i40evf_free_dma_mem_d - OS specific memory free for shared code
 112 * @hw:   pointer to the HW structure
 113 * @mem:  ptr to mem struct to free
 114 **/
 115i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
 116{
 117	struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
 118
 119	if (!mem || !mem->va)
 120		return I40E_ERR_PARAM;
 121	dma_free_coherent(&adapter->pdev->dev, mem->size,
 122			  mem->va, (dma_addr_t)mem->pa);
 123	return 0;
 124}
 125
 126/**
 127 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
 128 * @hw:   pointer to the HW structure
 129 * @mem:  ptr to mem struct to fill out
 130 * @size: size of memory requested
 131 **/
 132i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
 133				       struct i40e_virt_mem *mem, u32 size)
 134{
 135	if (!mem)
 136		return I40E_ERR_PARAM;
 137
 138	mem->size = size;
 139	mem->va = kzalloc(size, GFP_KERNEL);
 140
 141	if (mem->va)
 142		return 0;
 143	else
 144		return I40E_ERR_NO_MEMORY;
 145}
 146
 147/**
 148 * i40evf_free_virt_mem_d - OS specific memory free for shared code
 149 * @hw:   pointer to the HW structure
 150 * @mem:  ptr to mem struct to free
 151 **/
 152i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
 153				   struct i40e_virt_mem *mem)
 154{
 155	if (!mem)
 156		return I40E_ERR_PARAM;
 157
 158	/* it's ok to kfree a NULL pointer */
 159	kfree(mem->va);
 160
 161	return 0;
 162}
 163
 164/**
 165 * i40evf_debug_d - OS dependent version of debug printing
 166 * @hw:  pointer to the HW structure
 167 * @mask: debug level mask
 168 * @fmt_str: printf-type format description
 169 **/
 170void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
 171{
 172	char buf[512];
 173	va_list argptr;
 174
 175	if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
 176		return;
 177
 178	va_start(argptr, fmt_str);
 179	vsnprintf(buf, sizeof(buf), fmt_str, argptr);
 180	va_end(argptr);
 181
 182	/* the debug string is already formatted with a newline */
 183	pr_info("%s", buf);
 184}
 185
 186/**
 187 * i40evf_schedule_reset - Set the flags and schedule a reset event
 188 * @adapter: board private structure
 189 **/
 190void i40evf_schedule_reset(struct i40evf_adapter *adapter)
 191{
 192	if (!(adapter->flags &
 193	      (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
 194		adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
 195		schedule_work(&adapter->reset_task);
 196	}
 197}
 198
 199/**
 200 * i40evf_tx_timeout - Respond to a Tx Hang
 201 * @netdev: network interface device structure
 202 **/
 203static void i40evf_tx_timeout(struct net_device *netdev)
 204{
 205	struct i40evf_adapter *adapter = netdev_priv(netdev);
 206
 207	adapter->tx_timeout_count++;
 208	i40evf_schedule_reset(adapter);
 209}
 210
 211/**
 212 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
 213 * @adapter: board private structure
 214 **/
 215static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
 216{
 217	struct i40e_hw *hw = &adapter->hw;
 218
 219	if (!adapter->msix_entries)
 220		return;
 221
 222	wr32(hw, I40E_VFINT_DYN_CTL01, 0);
 223
 224	/* read flush */
 225	rd32(hw, I40E_VFGEN_RSTAT);
 226
 227	synchronize_irq(adapter->msix_entries[0].vector);
 228}
 229
 230/**
 231 * i40evf_misc_irq_enable - Enable default interrupt generation settings
 232 * @adapter: board private structure
 233 **/
 234static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
 235{
 236	struct i40e_hw *hw = &adapter->hw;
 237
 238	wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
 239				       I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
 240	wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
 241
 242	/* read flush */
 243	rd32(hw, I40E_VFGEN_RSTAT);
 244}
 245
 246/**
 247 * i40evf_irq_disable - Mask off interrupt generation on the NIC
 248 * @adapter: board private structure
 249 **/
 250static void i40evf_irq_disable(struct i40evf_adapter *adapter)
 251{
 252	int i;
 253	struct i40e_hw *hw = &adapter->hw;
 254
 255	if (!adapter->msix_entries)
 256		return;
 257
 258	for (i = 1; i < adapter->num_msix_vectors; i++) {
 259		wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
 260		synchronize_irq(adapter->msix_entries[i].vector);
 261	}
 262	/* read flush */
 263	rd32(hw, I40E_VFGEN_RSTAT);
 264}
 265
 266/**
 267 * i40evf_irq_enable_queues - Enable interrupt for specified queues
 268 * @adapter: board private structure
 269 * @mask: bitmap of queues to enable
 270 **/
 271void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
 272{
 273	struct i40e_hw *hw = &adapter->hw;
 274	int i;
 275
 276	for (i = 1; i < adapter->num_msix_vectors; i++) {
 277		if (mask & BIT(i - 1)) {
 278			wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
 279			     I40E_VFINT_DYN_CTLN1_INTENA_MASK |
 280			     I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
 281		}
 282	}
 283}
 284
 285/**
 286 * i40evf_irq_enable - Enable default interrupt generation settings
 287 * @adapter: board private structure
 288 * @flush: boolean value whether to run rd32()
 289 **/
 290void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
 291{
 292	struct i40e_hw *hw = &adapter->hw;
 293
 294	i40evf_misc_irq_enable(adapter);
 295	i40evf_irq_enable_queues(adapter, ~0);
 296
 297	if (flush)
 298		rd32(hw, I40E_VFGEN_RSTAT);
 299}
 300
 301/**
 302 * i40evf_msix_aq - Interrupt handler for vector 0
 303 * @irq: interrupt number
 304 * @data: pointer to netdev
 305 **/
 306static irqreturn_t i40evf_msix_aq(int irq, void *data)
 307{
 308	struct net_device *netdev = data;
 309	struct i40evf_adapter *adapter = netdev_priv(netdev);
 310	struct i40e_hw *hw = &adapter->hw;
 311
 312	/* handle non-queue interrupts, these reads clear the registers */
 313	rd32(hw, I40E_VFINT_ICR01);
 314	rd32(hw, I40E_VFINT_ICR0_ENA1);
 315
 316	/* schedule work on the private workqueue */
 317	schedule_work(&adapter->adminq_task);
 318
 319	return IRQ_HANDLED;
 320}
 321
 322/**
 323 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
 324 * @irq: interrupt number
 325 * @data: pointer to a q_vector
 326 **/
 327static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
 328{
 329	struct i40e_q_vector *q_vector = data;
 330
 331	if (!q_vector->tx.ring && !q_vector->rx.ring)
 332		return IRQ_HANDLED;
 333
 334	napi_schedule_irqoff(&q_vector->napi);
 335
 336	return IRQ_HANDLED;
 337}
 338
 339/**
 340 * i40evf_map_vector_to_rxq - associate irqs with rx queues
 341 * @adapter: board private structure
 342 * @v_idx: interrupt number
 343 * @r_idx: queue number
 344 **/
 345static void
 346i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
 347{
 348	struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
 349	struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
 350	struct i40e_hw *hw = &adapter->hw;
 351
 352	rx_ring->q_vector = q_vector;
 353	rx_ring->next = q_vector->rx.ring;
 354	rx_ring->vsi = &adapter->vsi;
 355	q_vector->rx.ring = rx_ring;
 356	q_vector->rx.count++;
 357	q_vector->rx.next_update = jiffies + 1;
 358	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
 359	q_vector->ring_mask |= BIT(r_idx);
 360	wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, q_vector->reg_idx),
 361	     q_vector->rx.current_itr);
 362	q_vector->rx.current_itr = q_vector->rx.target_itr;
 363}
 364
 365/**
 366 * i40evf_map_vector_to_txq - associate irqs with tx queues
 367 * @adapter: board private structure
 368 * @v_idx: interrupt number
 369 * @t_idx: queue number
 370 **/
 371static void
 372i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
 373{
 374	struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
 375	struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
 376	struct i40e_hw *hw = &adapter->hw;
 377
 378	tx_ring->q_vector = q_vector;
 379	tx_ring->next = q_vector->tx.ring;
 380	tx_ring->vsi = &adapter->vsi;
 381	q_vector->tx.ring = tx_ring;
 382	q_vector->tx.count++;
 383	q_vector->tx.next_update = jiffies + 1;
 384	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
 385	q_vector->num_ringpairs++;
 386	wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, q_vector->reg_idx),
 387	     q_vector->tx.target_itr);
 388	q_vector->tx.current_itr = q_vector->tx.target_itr;
 389}
 390
 391/**
 392 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
 393 * @adapter: board private structure to initialize
 394 *
 395 * This function maps descriptor rings to the queue-specific vectors
 396 * we were allotted through the MSI-X enabling code.  Ideally, we'd have
 397 * one vector per ring/queue, but on a constrained vector budget, we
 398 * group the rings as "efficiently" as possible.  You would add new
 399 * mapping configurations in here.
 400 **/
 401static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
 402{
 403	int rings_remaining = adapter->num_active_queues;
 404	int ridx = 0, vidx = 0;
 405	int q_vectors;
 406
 407	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 408
 409	for (; ridx < rings_remaining; ridx++) {
 410		i40evf_map_vector_to_rxq(adapter, vidx, ridx);
 411		i40evf_map_vector_to_txq(adapter, vidx, ridx);
 412
 413		/* In the case where we have more queues than vectors, continue
 414		 * round-robin on vectors until all queues are mapped.
 415		 */
 416		if (++vidx >= q_vectors)
 417			vidx = 0;
 418	}
 419
 420	adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
 421}
 422
 423#ifdef CONFIG_NET_POLL_CONTROLLER
 424/**
 425 * i40evf_netpoll - A Polling 'interrupt' handler
 426 * @netdev: network interface device structure
 427 *
 428 * This is used by netconsole to send skbs without having to re-enable
 429 * interrupts.  It's not called while the normal interrupt routine is executing.
 430 **/
 431static void i40evf_netpoll(struct net_device *netdev)
 432{
 433	struct i40evf_adapter *adapter = netdev_priv(netdev);
 434	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 435	int i;
 436
 437	/* if interface is down do nothing */
 438	if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
 439		return;
 440
 441	for (i = 0; i < q_vectors; i++)
 442		i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
 443}
 444
 445#endif
 446/**
 447 * i40evf_irq_affinity_notify - Callback for affinity changes
 448 * @notify: context as to what irq was changed
 449 * @mask: the new affinity mask
 450 *
 451 * This is a callback function used by the irq_set_affinity_notifier function
 452 * so that we may register to receive changes to the irq affinity masks.
 453 **/
 454static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
 455				       const cpumask_t *mask)
 456{
 457	struct i40e_q_vector *q_vector =
 458		container_of(notify, struct i40e_q_vector, affinity_notify);
 459
 460	cpumask_copy(&q_vector->affinity_mask, mask);
 461}
 462
 463/**
 464 * i40evf_irq_affinity_release - Callback for affinity notifier release
 465 * @ref: internal core kernel usage
 466 *
 467 * This is a callback function used by the irq_set_affinity_notifier function
 468 * to inform the current notification subscriber that they will no longer
 469 * receive notifications.
 470 **/
 471static void i40evf_irq_affinity_release(struct kref *ref) {}
 472
 473/**
 474 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
 475 * @adapter: board private structure
 476 *
 477 * Allocates MSI-X vectors for tx and rx handling, and requests
 478 * interrupts from the kernel.
 479 **/
 480static int
 481i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
 482{
 483	unsigned int vector, q_vectors;
 484	unsigned int rx_int_idx = 0, tx_int_idx = 0;
 485	int irq_num, err;
 486	int cpu;
 487
 488	i40evf_irq_disable(adapter);
 489	/* Decrement for Other and TCP Timer vectors */
 490	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 491
 492	for (vector = 0; vector < q_vectors; vector++) {
 493		struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
 494		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 495
 496		if (q_vector->tx.ring && q_vector->rx.ring) {
 497			snprintf(q_vector->name, sizeof(q_vector->name),
 498				 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
 499			tx_int_idx++;
 500		} else if (q_vector->rx.ring) {
 501			snprintf(q_vector->name, sizeof(q_vector->name),
 502				 "i40evf-%s-rx-%d", basename, rx_int_idx++);
 503		} else if (q_vector->tx.ring) {
 504			snprintf(q_vector->name, sizeof(q_vector->name),
 505				 "i40evf-%s-tx-%d", basename, tx_int_idx++);
 506		} else {
 507			/* skip this unused q_vector */
 508			continue;
 509		}
 510		err = request_irq(irq_num,
 511				  i40evf_msix_clean_rings,
 512				  0,
 513				  q_vector->name,
 514				  q_vector);
 515		if (err) {
 516			dev_info(&adapter->pdev->dev,
 517				 "Request_irq failed, error: %d\n", err);
 518			goto free_queue_irqs;
 519		}
 520		/* register for affinity change notifications */
 521		q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
 522		q_vector->affinity_notify.release =
 523						   i40evf_irq_affinity_release;
 524		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
 525		/* Spread the IRQ affinity hints across online CPUs. Note that
 526		 * get_cpu_mask returns a mask with a permanent lifetime so
 527		 * it's safe to use as a hint for irq_set_affinity_hint.
 528		 */
 529		cpu = cpumask_local_spread(q_vector->v_idx, -1);
 530		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
 531	}
 532
 533	return 0;
 534
 535free_queue_irqs:
 536	while (vector) {
 537		vector--;
 538		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 539		irq_set_affinity_notifier(irq_num, NULL);
 540		irq_set_affinity_hint(irq_num, NULL);
 541		free_irq(irq_num, &adapter->q_vectors[vector]);
 542	}
 543	return err;
 544}
 545
 546/**
 547 * i40evf_request_misc_irq - Initialize MSI-X interrupts
 548 * @adapter: board private structure
 549 *
 550 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
 551 * vector is only for the admin queue, and stays active even when the netdev
 552 * is closed.
 553 **/
 554static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
 555{
 556	struct net_device *netdev = adapter->netdev;
 557	int err;
 558
 559	snprintf(adapter->misc_vector_name,
 560		 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
 561		 dev_name(&adapter->pdev->dev));
 562	err = request_irq(adapter->msix_entries[0].vector,
 563			  &i40evf_msix_aq, 0,
 564			  adapter->misc_vector_name, netdev);
 565	if (err) {
 566		dev_err(&adapter->pdev->dev,
 567			"request_irq for %s failed: %d\n",
 568			adapter->misc_vector_name, err);
 569		free_irq(adapter->msix_entries[0].vector, netdev);
 570	}
 571	return err;
 572}
 573
 574/**
 575 * i40evf_free_traffic_irqs - Free MSI-X interrupts
 576 * @adapter: board private structure
 577 *
 578 * Frees all MSI-X vectors other than 0.
 579 **/
 580static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
 581{
 582	int vector, irq_num, q_vectors;
 583
 584	if (!adapter->msix_entries)
 585		return;
 586
 587	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 588
 589	for (vector = 0; vector < q_vectors; vector++) {
 590		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 591		irq_set_affinity_notifier(irq_num, NULL);
 592		irq_set_affinity_hint(irq_num, NULL);
 593		free_irq(irq_num, &adapter->q_vectors[vector]);
 594	}
 595}
 596
 597/**
 598 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
 599 * @adapter: board private structure
 600 *
 601 * Frees MSI-X vector 0.
 602 **/
 603static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
 604{
 605	struct net_device *netdev = adapter->netdev;
 606
 607	if (!adapter->msix_entries)
 608		return;
 609
 610	free_irq(adapter->msix_entries[0].vector, netdev);
 611}
 612
 613/**
 614 * i40evf_configure_tx - Configure Transmit Unit after Reset
 615 * @adapter: board private structure
 616 *
 617 * Configure the Tx unit of the MAC after a reset.
 618 **/
 619static void i40evf_configure_tx(struct i40evf_adapter *adapter)
 620{
 621	struct i40e_hw *hw = &adapter->hw;
 622	int i;
 623
 624	for (i = 0; i < adapter->num_active_queues; i++)
 625		adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
 626}
 627
 628/**
 629 * i40evf_configure_rx - Configure Receive Unit after Reset
 630 * @adapter: board private structure
 631 *
 632 * Configure the Rx unit of the MAC after a reset.
 633 **/
 634static void i40evf_configure_rx(struct i40evf_adapter *adapter)
 635{
 636	unsigned int rx_buf_len = I40E_RXBUFFER_2048;
 637	struct i40e_hw *hw = &adapter->hw;
 638	int i;
 639
 640	/* Legacy Rx will always default to a 2048 buffer size. */
 641#if (PAGE_SIZE < 8192)
 642	if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
 643		struct net_device *netdev = adapter->netdev;
 644
 645		/* For jumbo frames on systems with 4K pages we have to use
 646		 * an order 1 page, so we might as well increase the size
 647		 * of our Rx buffer to make better use of the available space
 648		 */
 649		rx_buf_len = I40E_RXBUFFER_3072;
 650
 651		/* We use a 1536 buffer size for configurations with
 652		 * standard Ethernet mtu.  On x86 this gives us enough room
 653		 * for shared info and 192 bytes of padding.
 654		 */
 655		if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
 656		    (netdev->mtu <= ETH_DATA_LEN))
 657			rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
 658	}
 659#endif
 660
 661	for (i = 0; i < adapter->num_active_queues; i++) {
 662		adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
 663		adapter->rx_rings[i].rx_buf_len = rx_buf_len;
 664
 665		if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
 666			clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
 667		else
 668			set_ring_build_skb_enabled(&adapter->rx_rings[i]);
 669	}
 670}
 671
 672/**
 673 * i40evf_find_vlan - Search filter list for specific vlan filter
 674 * @adapter: board private structure
 675 * @vlan: vlan tag
 676 *
 677 * Returns ptr to the filter object or NULL. Must be called while holding the
 678 * mac_vlan_list_lock.
 679 **/
 680static struct
 681i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
 682{
 683	struct i40evf_vlan_filter *f;
 684
 685	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 686		if (vlan == f->vlan)
 687			return f;
 688	}
 689	return NULL;
 690}
 691
 692/**
 693 * i40evf_add_vlan - Add a vlan filter to the list
 694 * @adapter: board private structure
 695 * @vlan: VLAN tag
 696 *
 697 * Returns ptr to the filter object or NULL when no memory available.
 698 **/
 699static struct
 700i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
 701{
 702	struct i40evf_vlan_filter *f = NULL;
 703
 704	spin_lock_bh(&adapter->mac_vlan_list_lock);
 705
 706	f = i40evf_find_vlan(adapter, vlan);
 707	if (!f) {
 708		f = kzalloc(sizeof(*f), GFP_ATOMIC);
 709		if (!f)
 710			goto clearout;
 711
 712		f->vlan = vlan;
 713
 714		INIT_LIST_HEAD(&f->list);
 715		list_add(&f->list, &adapter->vlan_filter_list);
 716		f->add = true;
 717		adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
 718	}
 719
 720clearout:
 721	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 722	return f;
 723}
 724
 725/**
 726 * i40evf_del_vlan - Remove a vlan filter from the list
 727 * @adapter: board private structure
 728 * @vlan: VLAN tag
 729 **/
 730static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
 731{
 732	struct i40evf_vlan_filter *f;
 733
 734	spin_lock_bh(&adapter->mac_vlan_list_lock);
 735
 736	f = i40evf_find_vlan(adapter, vlan);
 737	if (f) {
 738		f->remove = true;
 739		adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
 740	}
 741
 742	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 743}
 744
 745/**
 746 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
 747 * @netdev: network device struct
 748 * @vid: VLAN tag
 749 **/
 750static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
 751				  __always_unused __be16 proto, u16 vid)
 752{
 753	struct i40evf_adapter *adapter = netdev_priv(netdev);
 754
 755	if (!VLAN_ALLOWED(adapter))
 756		return -EIO;
 757	if (i40evf_add_vlan(adapter, vid) == NULL)
 758		return -ENOMEM;
 759	return 0;
 760}
 761
 762/**
 763 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
 764 * @netdev: network device struct
 765 * @vid: VLAN tag
 766 **/
 767static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
 768				   __always_unused __be16 proto, u16 vid)
 769{
 770	struct i40evf_adapter *adapter = netdev_priv(netdev);
 771
 772	if (VLAN_ALLOWED(adapter)) {
 773		i40evf_del_vlan(adapter, vid);
 774		return 0;
 775	}
 776	return -EIO;
 777}
 778
 779/**
 780 * i40evf_find_filter - Search filter list for specific mac filter
 781 * @adapter: board private structure
 782 * @macaddr: the MAC address
 783 *
 784 * Returns ptr to the filter object or NULL. Must be called while holding the
 785 * mac_vlan_list_lock.
 786 **/
 787static struct
 788i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
 789				      const u8 *macaddr)
 790{
 791	struct i40evf_mac_filter *f;
 792
 793	if (!macaddr)
 794		return NULL;
 795
 796	list_for_each_entry(f, &adapter->mac_filter_list, list) {
 797		if (ether_addr_equal(macaddr, f->macaddr))
 798			return f;
 799	}
 800	return NULL;
 801}
 802
 803/**
 804 * i40e_add_filter - Add a mac filter to the filter list
 805 * @adapter: board private structure
 806 * @macaddr: the MAC address
 807 *
 808 * Returns ptr to the filter object or NULL when no memory available.
 809 **/
 810static struct
 811i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
 812				     const u8 *macaddr)
 813{
 814	struct i40evf_mac_filter *f;
 815
 816	if (!macaddr)
 817		return NULL;
 818
 819	f = i40evf_find_filter(adapter, macaddr);
 820	if (!f) {
 821		f = kzalloc(sizeof(*f), GFP_ATOMIC);
 822		if (!f)
 823			return f;
 824
 825		ether_addr_copy(f->macaddr, macaddr);
 826
 827		list_add_tail(&f->list, &adapter->mac_filter_list);
 828		f->add = true;
 829		adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
 830	} else {
 831		f->remove = false;
 832	}
 833
 834	return f;
 835}
 836
 837/**
 838 * i40evf_set_mac - NDO callback to set port mac address
 839 * @netdev: network interface device structure
 840 * @p: pointer to an address structure
 841 *
 842 * Returns 0 on success, negative on failure
 843 **/
 844static int i40evf_set_mac(struct net_device *netdev, void *p)
 845{
 846	struct i40evf_adapter *adapter = netdev_priv(netdev);
 847	struct i40e_hw *hw = &adapter->hw;
 848	struct i40evf_mac_filter *f;
 849	struct sockaddr *addr = p;
 850
 851	if (!is_valid_ether_addr(addr->sa_data))
 852		return -EADDRNOTAVAIL;
 853
 854	if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
 855		return 0;
 856
 857	if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
 858		return -EPERM;
 859
 860	spin_lock_bh(&adapter->mac_vlan_list_lock);
 861
 862	f = i40evf_find_filter(adapter, hw->mac.addr);
 863	if (f) {
 864		f->remove = true;
 865		adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
 866	}
 867
 868	f = i40evf_add_filter(adapter, addr->sa_data);
 869
 870	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 871
 872	if (f) {
 873		ether_addr_copy(hw->mac.addr, addr->sa_data);
 874		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
 875	}
 876
 877	return (f == NULL) ? -ENOMEM : 0;
 878}
 879
 880/**
 881 * i40evf_addr_sync - Callback for dev_(mc|uc)_sync to add address
 882 * @netdev: the netdevice
 883 * @addr: address to add
 884 *
 885 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
 886 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
 887 */
 888static int i40evf_addr_sync(struct net_device *netdev, const u8 *addr)
 889{
 890	struct i40evf_adapter *adapter = netdev_priv(netdev);
 891
 892	if (i40evf_add_filter(adapter, addr))
 893		return 0;
 894	else
 895		return -ENOMEM;
 896}
 897
 898/**
 899 * i40evf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
 900 * @netdev: the netdevice
 901 * @addr: address to add
 902 *
 903 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
 904 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
 905 */
 906static int i40evf_addr_unsync(struct net_device *netdev, const u8 *addr)
 907{
 908	struct i40evf_adapter *adapter = netdev_priv(netdev);
 909	struct i40evf_mac_filter *f;
 910
 911	/* Under some circumstances, we might receive a request to delete
 912	 * our own device address from our uc list. Because we store the
 913	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
 914	 * such requests and not delete our device address from this list.
 915	 */
 916	if (ether_addr_equal(addr, netdev->dev_addr))
 917		return 0;
 918
 919	f = i40evf_find_filter(adapter, addr);
 920	if (f) {
 921		f->remove = true;
 922		adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
 923	}
 924	return 0;
 925}
 926
 927/**
 928 * i40evf_set_rx_mode - NDO callback to set the netdev filters
 929 * @netdev: network interface device structure
 930 **/
 931static void i40evf_set_rx_mode(struct net_device *netdev)
 932{
 933	struct i40evf_adapter *adapter = netdev_priv(netdev);
 934
 935	spin_lock_bh(&adapter->mac_vlan_list_lock);
 936	__dev_uc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
 937	__dev_mc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
 938	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 939
 940	if (netdev->flags & IFF_PROMISC &&
 941	    !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
 942		adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
 943	else if (!(netdev->flags & IFF_PROMISC) &&
 944		 adapter->flags & I40EVF_FLAG_PROMISC_ON)
 945		adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
 946
 947	if (netdev->flags & IFF_ALLMULTI &&
 948	    !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
 949		adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
 950	else if (!(netdev->flags & IFF_ALLMULTI) &&
 951		 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
 952		adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
 953}
 954
 955/**
 956 * i40evf_napi_enable_all - enable NAPI on all queue vectors
 957 * @adapter: board private structure
 958 **/
 959static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
 960{
 961	int q_idx;
 962	struct i40e_q_vector *q_vector;
 963	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 964
 965	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
 966		struct napi_struct *napi;
 967
 968		q_vector = &adapter->q_vectors[q_idx];
 969		napi = &q_vector->napi;
 970		napi_enable(napi);
 971	}
 972}
 973
 974/**
 975 * i40evf_napi_disable_all - disable NAPI on all queue vectors
 976 * @adapter: board private structure
 977 **/
 978static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
 979{
 980	int q_idx;
 981	struct i40e_q_vector *q_vector;
 982	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 983
 984	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
 985		q_vector = &adapter->q_vectors[q_idx];
 986		napi_disable(&q_vector->napi);
 987	}
 988}
 989
 990/**
 991 * i40evf_configure - set up transmit and receive data structures
 992 * @adapter: board private structure
 993 **/
 994static void i40evf_configure(struct i40evf_adapter *adapter)
 995{
 996	struct net_device *netdev = adapter->netdev;
 997	int i;
 998
 999	i40evf_set_rx_mode(netdev);
1000
1001	i40evf_configure_tx(adapter);
1002	i40evf_configure_rx(adapter);
1003	adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1004
1005	for (i = 0; i < adapter->num_active_queues; i++) {
1006		struct i40e_ring *ring = &adapter->rx_rings[i];
1007
1008		i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
1009	}
1010}
1011
1012/**
1013 * i40evf_up_complete - Finish the last steps of bringing up a connection
1014 * @adapter: board private structure
1015 *
1016 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
1017 **/
1018static void i40evf_up_complete(struct i40evf_adapter *adapter)
1019{
1020	adapter->state = __I40EVF_RUNNING;
1021	clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1022
1023	i40evf_napi_enable_all(adapter);
1024
1025	adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1026	if (CLIENT_ENABLED(adapter))
1027		adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
1028	mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1029}
1030
1031/**
1032 * i40e_down - Shutdown the connection processing
1033 * @adapter: board private structure
1034 *
1035 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
1036 **/
1037void i40evf_down(struct i40evf_adapter *adapter)
1038{
1039	struct net_device *netdev = adapter->netdev;
1040	struct i40evf_vlan_filter *vlf;
1041	struct i40evf_mac_filter *f;
1042	struct i40evf_cloud_filter *cf;
1043
1044	if (adapter->state <= __I40EVF_DOWN_PENDING)
1045		return;
1046
1047	netif_carrier_off(netdev);
1048	netif_tx_disable(netdev);
1049	adapter->link_up = false;
1050	i40evf_napi_disable_all(adapter);
1051	i40evf_irq_disable(adapter);
1052
1053	spin_lock_bh(&adapter->mac_vlan_list_lock);
1054
1055	/* clear the sync flag on all filters */
1056	__dev_uc_unsync(adapter->netdev, NULL);
1057	__dev_mc_unsync(adapter->netdev, NULL);
1058
1059	/* remove all MAC filters */
1060	list_for_each_entry(f, &adapter->mac_filter_list, list) {
1061		f->remove = true;
1062	}
1063
1064	/* remove all VLAN filters */
1065	list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1066		vlf->remove = true;
1067	}
1068
1069	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1070
1071	/* remove all cloud filters */
1072	spin_lock_bh(&adapter->cloud_filter_list_lock);
1073	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1074		cf->del = true;
1075	}
1076	spin_unlock_bh(&adapter->cloud_filter_list_lock);
1077
1078	if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1079	    adapter->state != __I40EVF_RESETTING) {
1080		/* cancel any current operation */
1081		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1082		/* Schedule operations to close down the HW. Don't wait
1083		 * here for this to complete. The watchdog is still running
1084		 * and it will take care of this.
1085		 */
1086		adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1087		adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1088		adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
1089		adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1090	}
1091
1092	mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1093}
1094
1095/**
1096 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1097 * @adapter: board private structure
1098 * @vectors: number of vectors to request
1099 *
1100 * Work with the OS to set up the MSIX vectors needed.
1101 *
1102 * Returns 0 on success, negative on failure
1103 **/
1104static int
1105i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1106{
1107	int err, vector_threshold;
1108
1109	/* We'll want at least 3 (vector_threshold):
1110	 * 0) Other (Admin Queue and link, mostly)
1111	 * 1) TxQ[0] Cleanup
1112	 * 2) RxQ[0] Cleanup
1113	 */
1114	vector_threshold = MIN_MSIX_COUNT;
1115
1116	/* The more we get, the more we will assign to Tx/Rx Cleanup
1117	 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1118	 * Right now, we simply care about how many we'll get; we'll
1119	 * set them up later while requesting irq's.
1120	 */
1121	err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1122				    vector_threshold, vectors);
1123	if (err < 0) {
1124		dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1125		kfree(adapter->msix_entries);
1126		adapter->msix_entries = NULL;
1127		return err;
1128	}
1129
1130	/* Adjust for only the vectors we'll use, which is minimum
1131	 * of max_msix_q_vectors + NONQ_VECS, or the number of
1132	 * vectors we were allocated.
1133	 */
1134	adapter->num_msix_vectors = err;
1135	return 0;
1136}
1137
1138/**
1139 * i40evf_free_queues - Free memory for all rings
1140 * @adapter: board private structure to initialize
1141 *
1142 * Free all of the memory associated with queue pairs.
1143 **/
1144static void i40evf_free_queues(struct i40evf_adapter *adapter)
1145{
1146	if (!adapter->vsi_res)
1147		return;
1148	adapter->num_active_queues = 0;
1149	kfree(adapter->tx_rings);
1150	adapter->tx_rings = NULL;
1151	kfree(adapter->rx_rings);
1152	adapter->rx_rings = NULL;
1153}
1154
1155/**
1156 * i40evf_alloc_queues - Allocate memory for all rings
1157 * @adapter: board private structure to initialize
1158 *
1159 * We allocate one ring per queue at run-time since we don't know the
1160 * number of queues at compile-time.  The polling_netdev array is
1161 * intended for Multiqueue, but should work fine with a single queue.
1162 **/
1163static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1164{
1165	int i, num_active_queues;
1166
1167	/* If we're in reset reallocating queues we don't actually know yet for
1168	 * certain the PF gave us the number of queues we asked for but we'll
1169	 * assume it did.  Once basic reset is finished we'll confirm once we
1170	 * start negotiating config with PF.
1171	 */
1172	if (adapter->num_req_queues)
1173		num_active_queues = adapter->num_req_queues;
1174	else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1175		 adapter->num_tc)
1176		num_active_queues = adapter->ch_config.total_qps;
1177	else
1178		num_active_queues = min_t(int,
1179					  adapter->vsi_res->num_queue_pairs,
1180					  (int)(num_online_cpus()));
1181
1182
1183	adapter->tx_rings = kcalloc(num_active_queues,
1184				    sizeof(struct i40e_ring), GFP_KERNEL);
1185	if (!adapter->tx_rings)
1186		goto err_out;
1187	adapter->rx_rings = kcalloc(num_active_queues,
1188				    sizeof(struct i40e_ring), GFP_KERNEL);
1189	if (!adapter->rx_rings)
1190		goto err_out;
1191
1192	for (i = 0; i < num_active_queues; i++) {
1193		struct i40e_ring *tx_ring;
1194		struct i40e_ring *rx_ring;
1195
1196		tx_ring = &adapter->tx_rings[i];
1197
1198		tx_ring->queue_index = i;
1199		tx_ring->netdev = adapter->netdev;
1200		tx_ring->dev = &adapter->pdev->dev;
1201		tx_ring->count = adapter->tx_desc_count;
1202		tx_ring->itr_setting = I40E_ITR_TX_DEF;
1203		if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
1204			tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1205
1206		rx_ring = &adapter->rx_rings[i];
1207		rx_ring->queue_index = i;
1208		rx_ring->netdev = adapter->netdev;
1209		rx_ring->dev = &adapter->pdev->dev;
1210		rx_ring->count = adapter->rx_desc_count;
1211		rx_ring->itr_setting = I40E_ITR_RX_DEF;
1212	}
1213
1214	adapter->num_active_queues = num_active_queues;
1215
1216	return 0;
1217
1218err_out:
1219	i40evf_free_queues(adapter);
1220	return -ENOMEM;
1221}
1222
1223/**
1224 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1225 * @adapter: board private structure to initialize
1226 *
1227 * Attempt to configure the interrupts using the best available
1228 * capabilities of the hardware and the kernel.
1229 **/
1230static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1231{
1232	int vector, v_budget;
1233	int pairs = 0;
1234	int err = 0;
1235
1236	if (!adapter->vsi_res) {
1237		err = -EIO;
1238		goto out;
1239	}
1240	pairs = adapter->num_active_queues;
1241
1242	/* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1243	 * us much good if we have more vectors than CPUs. However, we already
1244	 * limit the total number of queues by the number of CPUs so we do not
1245	 * need any further limiting here.
1246	 */
1247	v_budget = min_t(int, pairs + NONQ_VECS,
1248			 (int)adapter->vf_res->max_vectors);
1249
1250	adapter->msix_entries = kcalloc(v_budget,
1251					sizeof(struct msix_entry), GFP_KERNEL);
1252	if (!adapter->msix_entries) {
1253		err = -ENOMEM;
1254		goto out;
1255	}
1256
1257	for (vector = 0; vector < v_budget; vector++)
1258		adapter->msix_entries[vector].entry = vector;
1259
1260	err = i40evf_acquire_msix_vectors(adapter, v_budget);
1261
1262out:
1263	netif_set_real_num_rx_queues(adapter->netdev, pairs);
1264	netif_set_real_num_tx_queues(adapter->netdev, pairs);
1265	return err;
1266}
1267
1268/**
1269 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1270 * @adapter: board private structure
1271 *
1272 * Return 0 on success, negative on failure
1273 **/
1274static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
1275{
1276	struct i40e_aqc_get_set_rss_key_data *rss_key =
1277		(struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1278	struct i40e_hw *hw = &adapter->hw;
1279	int ret = 0;
1280
1281	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1282		/* bail because we already have a command pending */
1283		dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1284			adapter->current_op);
1285		return -EBUSY;
1286	}
1287
1288	ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1289	if (ret) {
1290		dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1291			i40evf_stat_str(hw, ret),
1292			i40evf_aq_str(hw, hw->aq.asq_last_status));
1293		return ret;
1294
1295	}
1296
1297	ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1298				    adapter->rss_lut, adapter->rss_lut_size);
1299	if (ret) {
1300		dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1301			i40evf_stat_str(hw, ret),
1302			i40evf_aq_str(hw, hw->aq.asq_last_status));
1303	}
1304
1305	return ret;
1306
1307}
1308
1309/**
1310 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1311 * @adapter: board private structure
1312 *
1313 * Returns 0 on success, negative on failure
1314 **/
1315static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
1316{
1317	struct i40e_hw *hw = &adapter->hw;
1318	u32 *dw;
1319	u16 i;
1320
1321	dw = (u32 *)adapter->rss_key;
1322	for (i = 0; i <= adapter->rss_key_size / 4; i++)
1323		wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1324
1325	dw = (u32 *)adapter->rss_lut;
1326	for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1327		wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1328
1329	i40e_flush(hw);
1330
1331	return 0;
1332}
1333
1334/**
1335 * i40evf_config_rss - Configure RSS keys and lut
1336 * @adapter: board private structure
1337 *
1338 * Returns 0 on success, negative on failure
1339 **/
1340int i40evf_config_rss(struct i40evf_adapter *adapter)
1341{
1342
1343	if (RSS_PF(adapter)) {
1344		adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1345					I40EVF_FLAG_AQ_SET_RSS_KEY;
1346		return 0;
1347	} else if (RSS_AQ(adapter)) {
1348		return i40evf_config_rss_aq(adapter);
1349	} else {
1350		return i40evf_config_rss_reg(adapter);
1351	}
1352}
1353
1354/**
1355 * i40evf_fill_rss_lut - Fill the lut with default values
1356 * @adapter: board private structure
1357 **/
1358static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
1359{
1360	u16 i;
1361
1362	for (i = 0; i < adapter->rss_lut_size; i++)
1363		adapter->rss_lut[i] = i % adapter->num_active_queues;
1364}
1365
1366/**
1367 * i40evf_init_rss - Prepare for RSS
1368 * @adapter: board private structure
1369 *
1370 * Return 0 on success, negative on failure
1371 **/
1372static int i40evf_init_rss(struct i40evf_adapter *adapter)
1373{
1374	struct i40e_hw *hw = &adapter->hw;
1375	int ret;
1376
1377	if (!RSS_PF(adapter)) {
1378		/* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1379		if (adapter->vf_res->vf_cap_flags &
1380		    VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1381			adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1382		else
1383			adapter->hena = I40E_DEFAULT_RSS_HENA;
1384
1385		wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1386		wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1387	}
1388
1389	i40evf_fill_rss_lut(adapter);
1390
1391	netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1392	ret = i40evf_config_rss(adapter);
1393
1394	return ret;
1395}
1396
1397/**
1398 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1399 * @adapter: board private structure to initialize
1400 *
1401 * We allocate one q_vector per queue interrupt.  If allocation fails we
1402 * return -ENOMEM.
1403 **/
1404static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1405{
1406	int q_idx = 0, num_q_vectors;
1407	struct i40e_q_vector *q_vector;
1408
1409	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1410	adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1411				     GFP_KERNEL);
1412	if (!adapter->q_vectors)
1413		return -ENOMEM;
1414
1415	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1416		q_vector = &adapter->q_vectors[q_idx];
1417		q_vector->adapter = adapter;
1418		q_vector->vsi = &adapter->vsi;
1419		q_vector->v_idx = q_idx;
1420		q_vector->reg_idx = q_idx;
1421		cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1422		netif_napi_add(adapter->netdev, &q_vector->napi,
1423			       i40evf_napi_poll, NAPI_POLL_WEIGHT);
1424	}
1425
1426	return 0;
1427}
1428
1429/**
1430 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1431 * @adapter: board private structure to initialize
1432 *
1433 * This function frees the memory allocated to the q_vectors.  In addition if
1434 * NAPI is enabled it will delete any references to the NAPI struct prior
1435 * to freeing the q_vector.
1436 **/
1437static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1438{
1439	int q_idx, num_q_vectors;
1440	int napi_vectors;
1441
1442	if (!adapter->q_vectors)
1443		return;
1444
1445	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1446	napi_vectors = adapter->num_active_queues;
1447
1448	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1449		struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1450		if (q_idx < napi_vectors)
1451			netif_napi_del(&q_vector->napi);
1452	}
1453	kfree(adapter->q_vectors);
1454	adapter->q_vectors = NULL;
1455}
1456
1457/**
1458 * i40evf_reset_interrupt_capability - Reset MSIX setup
1459 * @adapter: board private structure
1460 *
1461 **/
1462void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1463{
1464	if (!adapter->msix_entries)
1465		return;
1466
1467	pci_disable_msix(adapter->pdev);
1468	kfree(adapter->msix_entries);
1469	adapter->msix_entries = NULL;
1470}
1471
1472/**
1473 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1474 * @adapter: board private structure to initialize
1475 *
1476 **/
1477int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1478{
1479	int err;
1480
1481	err = i40evf_alloc_queues(adapter);
1482	if (err) {
1483		dev_err(&adapter->pdev->dev,
1484			"Unable to allocate memory for queues\n");
1485		goto err_alloc_queues;
1486	}
1487
1488	rtnl_lock();
1489	err = i40evf_set_interrupt_capability(adapter);
1490	rtnl_unlock();
1491	if (err) {
1492		dev_err(&adapter->pdev->dev,
1493			"Unable to setup interrupt capabilities\n");
1494		goto err_set_interrupt;
1495	}
1496
1497	err = i40evf_alloc_q_vectors(adapter);
1498	if (err) {
1499		dev_err(&adapter->pdev->dev,
1500			"Unable to allocate memory for queue vectors\n");
1501		goto err_alloc_q_vectors;
1502	}
1503
1504	/* If we've made it so far while ADq flag being ON, then we haven't
1505	 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1506	 * resources have been allocated in the reset path.
1507	 * Now we can truly claim that ADq is enabled.
1508	 */
1509	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1510	    adapter->num_tc)
1511		dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1512			 adapter->num_tc);
1513
1514	dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1515		 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1516		 adapter->num_active_queues);
1517
1518	return 0;
1519err_alloc_q_vectors:
1520	i40evf_reset_interrupt_capability(adapter);
1521err_set_interrupt:
1522	i40evf_free_queues(adapter);
1523err_alloc_queues:
1524	return err;
1525}
1526
1527/**
1528 * i40evf_free_rss - Free memory used by RSS structs
1529 * @adapter: board private structure
1530 **/
1531static void i40evf_free_rss(struct i40evf_adapter *adapter)
1532{
1533	kfree(adapter->rss_key);
1534	adapter->rss_key = NULL;
1535
1536	kfree(adapter->rss_lut);
1537	adapter->rss_lut = NULL;
1538}
1539
1540/**
1541 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1542 * @adapter: board private structure
1543 *
1544 * Returns 0 on success, negative on failure
1545 **/
1546static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1547{
1548	struct net_device *netdev = adapter->netdev;
1549	int err;
1550
1551	if (netif_running(netdev))
1552		i40evf_free_traffic_irqs(adapter);
1553	i40evf_free_misc_irq(adapter);
1554	i40evf_reset_interrupt_capability(adapter);
1555	i40evf_free_q_vectors(adapter);
1556	i40evf_free_queues(adapter);
1557
1558	err =  i40evf_init_interrupt_scheme(adapter);
1559	if (err)
1560		goto err;
1561
1562	netif_tx_stop_all_queues(netdev);
1563
1564	err = i40evf_request_misc_irq(adapter);
1565	if (err)
1566		goto err;
1567
1568	set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1569
1570	i40evf_map_rings_to_vectors(adapter);
1571
1572	if (RSS_AQ(adapter))
1573		adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1574	else
1575		err = i40evf_init_rss(adapter);
1576err:
1577	return err;
1578}
1579
1580/**
1581 * i40evf_watchdog_timer - Periodic call-back timer
1582 * @data: pointer to adapter disguised as unsigned long
1583 **/
1584static void i40evf_watchdog_timer(struct timer_list *t)
1585{
1586	struct i40evf_adapter *adapter = from_timer(adapter, t,
1587						    watchdog_timer);
1588
1589	schedule_work(&adapter->watchdog_task);
1590	/* timer will be rescheduled in watchdog task */
1591}
1592
1593/**
1594 * i40evf_watchdog_task - Periodic call-back task
1595 * @work: pointer to work_struct
1596 **/
1597static void i40evf_watchdog_task(struct work_struct *work)
1598{
1599	struct i40evf_adapter *adapter = container_of(work,
1600						      struct i40evf_adapter,
1601						      watchdog_task);
1602	struct i40e_hw *hw = &adapter->hw;
1603	u32 reg_val;
1604
1605	if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1606		goto restart_watchdog;
1607
1608	if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1609		reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1610			  I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1611		if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1612		    (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1613			/* A chance for redemption! */
1614			dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1615			adapter->state = __I40EVF_STARTUP;
1616			adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1617			schedule_delayed_work(&adapter->init_task, 10);
1618			clear_bit(__I40EVF_IN_CRITICAL_TASK,
1619				  &adapter->crit_section);
1620			/* Don't reschedule the watchdog, since we've restarted
1621			 * the init task. When init_task contacts the PF and
1622			 * gets everything set up again, it'll restart the
1623			 * watchdog for us. Down, boy. Sit. Stay. Woof.
1624			 */
1625			return;
1626		}
1627		adapter->aq_required = 0;
1628		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1629		goto watchdog_done;
1630	}
1631
1632	if ((adapter->state < __I40EVF_DOWN) ||
1633	    (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1634		goto watchdog_done;
1635
1636	/* check for reset */
1637	reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1638	if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
1639		adapter->state = __I40EVF_RESETTING;
1640		adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1641		dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1642		schedule_work(&adapter->reset_task);
1643		adapter->aq_required = 0;
1644		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1645		goto watchdog_done;
1646	}
1647
1648	/* Process admin queue tasks. After init, everything gets done
1649	 * here so we don't race on the admin queue.
1650	 */
1651	if (adapter->current_op) {
1652		if (!i40evf_asq_done(hw)) {
1653			dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1654			i40evf_send_api_ver(adapter);
1655		}
1656		goto watchdog_done;
1657	}
1658	if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1659		i40evf_send_vf_config_msg(adapter);
1660		goto watchdog_done;
1661	}
1662
1663	if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1664		i40evf_disable_queues(adapter);
1665		goto watchdog_done;
1666	}
1667
1668	if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1669		i40evf_map_queues(adapter);
1670		goto watchdog_done;
1671	}
1672
1673	if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1674		i40evf_add_ether_addrs(adapter);
1675		goto watchdog_done;
1676	}
1677
1678	if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1679		i40evf_add_vlans(adapter);
1680		goto watchdog_done;
1681	}
1682
1683	if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1684		i40evf_del_ether_addrs(adapter);
1685		goto watchdog_done;
1686	}
1687
1688	if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1689		i40evf_del_vlans(adapter);
1690		goto watchdog_done;
1691	}
1692
1693	if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1694		i40evf_enable_vlan_stripping(adapter);
1695		goto watchdog_done;
1696	}
1697
1698	if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1699		i40evf_disable_vlan_stripping(adapter);
1700		goto watchdog_done;
1701	}
1702
1703	if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1704		i40evf_configure_queues(adapter);
1705		goto watchdog_done;
1706	}
1707
1708	if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1709		i40evf_enable_queues(adapter);
1710		goto watchdog_done;
1711	}
1712
1713	if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1714		/* This message goes straight to the firmware, not the
1715		 * PF, so we don't have to set current_op as we will
1716		 * not get a response through the ARQ.
1717		 */
1718		i40evf_init_rss(adapter);
1719		adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1720		goto watchdog_done;
1721	}
1722	if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1723		i40evf_get_hena(adapter);
1724		goto watchdog_done;
1725	}
1726	if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1727		i40evf_set_hena(adapter);
1728		goto watchdog_done;
1729	}
1730	if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1731		i40evf_set_rss_key(adapter);
1732		goto watchdog_done;
1733	}
1734	if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1735		i40evf_set_rss_lut(adapter);
1736		goto watchdog_done;
1737	}
1738
1739	if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1740		i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1741				       FLAG_VF_MULTICAST_PROMISC);
1742		goto watchdog_done;
1743	}
1744
1745	if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1746		i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1747		goto watchdog_done;
1748	}
1749
1750	if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1751	    (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1752		i40evf_set_promiscuous(adapter, 0);
1753		goto watchdog_done;
1754	}
1755
1756	if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_CHANNELS) {
1757		i40evf_enable_channels(adapter);
1758		goto watchdog_done;
1759	}
1760
1761	if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_CHANNELS) {
1762		i40evf_disable_channels(adapter);
1763		goto watchdog_done;
1764	}
1765
1766	if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1767		i40evf_add_cloud_filter(adapter);
1768		goto watchdog_done;
1769	}
1770
1771	if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1772		i40evf_del_cloud_filter(adapter);
1773		goto watchdog_done;
1774	}
1775
1776	schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1777
1778	if (adapter->state == __I40EVF_RUNNING)
1779		i40evf_request_stats(adapter);
1780watchdog_done:
1781	if (adapter->state == __I40EVF_RUNNING)
1782		i40evf_detect_recover_hung(&adapter->vsi);
1783	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1784restart_watchdog:
1785	if (adapter->state == __I40EVF_REMOVE)
1786		return;
1787	if (adapter->aq_required)
1788		mod_timer(&adapter->watchdog_timer,
1789			  jiffies + msecs_to_jiffies(20));
1790	else
1791		mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1792	schedule_work(&adapter->adminq_task);
1793}
1794
1795static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1796{
1797	struct i40evf_mac_filter *f, *ftmp;
1798	struct i40evf_vlan_filter *fv, *fvtmp;
1799	struct i40evf_cloud_filter *cf, *cftmp;
1800
1801	adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1802
1803	/* We don't use netif_running() because it may be true prior to
1804	 * ndo_open() returning, so we can't assume it means all our open
1805	 * tasks have finished, since we're not holding the rtnl_lock here.
1806	 */
1807	if (adapter->state == __I40EVF_RUNNING) {
1808		set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1809		netif_carrier_off(adapter->netdev);
1810		netif_tx_disable(adapter->netdev);
1811		adapter->link_up = false;
1812		i40evf_napi_disable_all(adapter);
1813		i40evf_irq_disable(adapter);
1814		i40evf_free_traffic_irqs(adapter);
1815		i40evf_free_all_tx_resources(adapter);
1816		i40evf_free_all_rx_resources(adapter);
1817	}
1818
1819	spin_lock_bh(&adapter->mac_vlan_list_lock);
1820
1821	/* Delete all of the filters */
1822	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1823		list_del(&f->list);
1824		kfree(f);
1825	}
1826
1827	list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1828		list_del(&fv->list);
1829		kfree(fv);
1830	}
1831
1832	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1833
1834	spin_lock_bh(&adapter->cloud_filter_list_lock);
1835	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1836		list_del(&cf->list);
1837		kfree(cf);
1838		adapter->num_cloud_filters--;
1839	}
1840	spin_unlock_bh(&adapter->cloud_filter_list_lock);
1841
1842	i40evf_free_misc_irq(adapter);
1843	i40evf_reset_interrupt_capability(adapter);
1844	i40evf_free_queues(adapter);
1845	i40evf_free_q_vectors(adapter);
1846	kfree(adapter->vf_res);
1847	i40evf_shutdown_adminq(&adapter->hw);
1848	adapter->netdev->flags &= ~IFF_UP;
1849	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1850	adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1851	adapter->state = __I40EVF_DOWN;
1852	wake_up(&adapter->down_waitqueue);
1853	dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1854}
1855
1856#define I40EVF_RESET_WAIT_MS 10
1857#define I40EVF_RESET_WAIT_COUNT 500
1858/**
1859 * i40evf_reset_task - Call-back task to handle hardware reset
1860 * @work: pointer to work_struct
1861 *
1862 * During reset we need to shut down and reinitialize the admin queue
1863 * before we can use it to communicate with the PF again. We also clear
1864 * and reinit the rings because that context is lost as well.
1865 **/
1866static void i40evf_reset_task(struct work_struct *work)
1867{
1868	struct i40evf_adapter *adapter = container_of(work,
1869						      struct i40evf_adapter,
1870						      reset_task);
1871	struct virtchnl_vf_resource *vfres = adapter->vf_res;
1872	struct net_device *netdev = adapter->netdev;
1873	struct i40e_hw *hw = &adapter->hw;
1874	struct i40evf_vlan_filter *vlf;
1875	struct i40evf_cloud_filter *cf;
1876	struct i40evf_mac_filter *f;
1877	u32 reg_val;
1878	int i = 0, err;
1879	bool running;
1880
1881	/* When device is being removed it doesn't make sense to run the reset
1882	 * task, just return in such a case.
1883	 */
1884	if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1885		return;
1886
1887	while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
1888				&adapter->crit_section))
1889		usleep_range(500, 1000);
1890	if (CLIENT_ENABLED(adapter)) {
1891		adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1892				    I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1893				    I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1894				    I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1895		cancel_delayed_work_sync(&adapter->client_task);
1896		i40evf_notify_client_close(&adapter->vsi, true);
1897	}
1898	i40evf_misc_irq_disable(adapter);
1899	if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1900		adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1901		/* Restart the AQ here. If we have been reset but didn't
1902		 * detect it, or if the PF had to reinit, our AQ will be hosed.
1903		 */
1904		i40evf_shutdown_adminq(hw);
1905		i40evf_init_adminq(hw);
1906		i40evf_request_reset(adapter);
1907	}
1908	adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1909
1910	/* poll until we see the reset actually happen */
1911	for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1912		reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1913			  I40E_VF_ARQLEN1_ARQENABLE_MASK;
1914		if (!reg_val)
1915			break;
1916		usleep_range(5000, 10000);
1917	}
1918	if (i == I40EVF_RESET_WAIT_COUNT) {
1919		dev_info(&adapter->pdev->dev, "Never saw reset\n");
1920		goto continue_reset; /* act like the reset happened */
1921	}
1922
1923	/* wait until the reset is complete and the PF is responding to us */
1924	for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1925		/* sleep first to make sure a minimum wait time is met */
1926		msleep(I40EVF_RESET_WAIT_MS);
1927
1928		reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1929			  I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1930		if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1931			break;
1932	}
1933
1934	pci_set_master(adapter->pdev);
1935
1936	if (i == I40EVF_RESET_WAIT_COUNT) {
1937		dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1938			reg_val);
1939		i40evf_disable_vf(adapter);
1940		clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1941		return; /* Do not attempt to reinit. It's dead, Jim. */
1942	}
1943
1944continue_reset:
1945	/* We don't use netif_running() because it may be true prior to
1946	 * ndo_open() returning, so we can't assume it means all our open
1947	 * tasks have finished, since we're not holding the rtnl_lock here.
1948	 */
1949	running = (adapter->state == __I40EVF_RUNNING);
1950
1951	if (running) {
1952		netif_carrier_off(netdev);
1953		netif_tx_stop_all_queues(netdev);
1954		adapter->link_up = false;
1955		i40evf_napi_disable_all(adapter);
1956	}
1957	i40evf_irq_disable(adapter);
1958
1959	adapter->state = __I40EVF_RESETTING;
1960	adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1961
1962	/* free the Tx/Rx rings and descriptors, might be better to just
1963	 * re-use them sometime in the future
1964	 */
1965	i40evf_free_all_rx_resources(adapter);
1966	i40evf_free_all_tx_resources(adapter);
1967
1968	adapter->flags |= I40EVF_FLAG_QUEUES_DISABLED;
1969	/* kill and reinit the admin queue */
1970	i40evf_shutdown_adminq(hw);
1971	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1972	err = i40evf_init_adminq(hw);
1973	if (err)
1974		dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1975			 err);
1976	adapter->aq_required = 0;
1977
1978	if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1979		err = i40evf_reinit_interrupt_scheme(adapter);
1980		if (err)
1981			goto reset_err;
1982	}
1983
1984	adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
1985	adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1986
1987	spin_lock_bh(&adapter->mac_vlan_list_lock);
1988
1989	/* re-add all MAC filters */
1990	list_for_each_entry(f, &adapter->mac_filter_list, list) {
1991		f->add = true;
1992	}
1993	/* re-add all VLAN filters */
1994	list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1995		vlf->add = true;
1996	}
1997
1998	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1999
2000	/* check if TCs are running and re-add all cloud filters */
2001	spin_lock_bh(&adapter->cloud_filter_list_lock);
2002	if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
2003	    adapter->num_tc) {
2004		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2005			cf->add = true;
2006		}
2007	}
2008	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2009
2010	adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2011	adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
2012	adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
2013	i40evf_misc_irq_enable(adapter);
2014
2015	mod_timer(&adapter->watchdog_timer, jiffies + 2);
2016
2017	/* We were running when the reset started, so we need to restore some
2018	 * state here.
2019	 */
2020	if (running) {
2021		/* allocate transmit descriptors */
2022		err = i40evf_setup_all_tx_resources(adapter);
2023		if (err)
2024			goto reset_err;
2025
2026		/* allocate receive descriptors */
2027		err = i40evf_setup_all_rx_resources(adapter);
2028		if (err)
2029			goto reset_err;
2030
2031		if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
2032			err = i40evf_request_traffic_irqs(adapter,
2033							  netdev->name);
2034			if (err)
2035				goto reset_err;
2036
2037			adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
2038		}
2039
2040		i40evf_configure(adapter);
2041
2042		i40evf_up_complete(adapter);
2043
2044		i40evf_irq_enable(adapter, true);
2045	} else {
2046		adapter->state = __I40EVF_DOWN;
2047		wake_up(&adapter->down_waitqueue);
2048	}
2049	clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2050	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2051
2052	return;
2053reset_err:
2054	clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2055	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2056	dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2057	i40evf_close(netdev);
2058}
2059
2060/**
2061 * i40evf_adminq_task - worker thread to clean the admin queue
2062 * @work: pointer to work_struct containing our data
2063 **/
2064static void i40evf_adminq_task(struct work_struct *work)
2065{
2066	struct i40evf_adapter *adapter =
2067		container_of(work, struct i40evf_adapter, adminq_task);
2068	struct i40e_hw *hw = &adapter->hw;
2069	struct i40e_arq_event_info event;
2070	enum virtchnl_ops v_op;
2071	i40e_status ret, v_ret;
2072	u32 val, oldval;
2073	u16 pending;
2074
2075	if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
2076		goto out;
2077
2078	event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2079	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2080	if (!event.msg_buf)
2081		goto out;
2082
2083	do {
2084		ret = i40evf_clean_arq_element(hw, &event, &pending);
2085		v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2086		v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2087
2088		if (ret || !v_op)
2089			break; /* No event to process or error cleaning ARQ */
2090
2091		i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2092					   event.msg_len);
2093		if (pending != 0)
2094			memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
2095	} while (pending);
2096
2097	if ((adapter->flags &
2098	     (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2099	    adapter->state == __I40EVF_RESETTING)
2100		goto freedom;
2101
2102	/* check for error indications */
2103	val = rd32(hw, hw->aq.arq.len);
2104	if (val == 0xdeadbeef) /* indicates device in reset */
2105		goto freedom;
2106	oldval = val;
2107	if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
2108		dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2109		val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
2110	}
2111	if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
2112		dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2113		val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
2114	}
2115	if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
2116		dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2117		val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
2118	}
2119	if (oldval != val)
2120		wr32(hw, hw->aq.arq.len, val);
2121
2122	val = rd32(hw, hw->aq.asq.len);
2123	oldval = val;
2124	if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
2125		dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2126		val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
2127	}
2128	if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
2129		dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2130		val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
2131	}
2132	if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
2133		dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2134		val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
2135	}
2136	if (oldval != val)
2137		wr32(hw, hw->aq.asq.len, val);
2138
2139freedom:
2140	kfree(event.msg_buf);
2141out:
2142	/* re-enable Admin queue interrupt cause */
2143	i40evf_misc_irq_enable(adapter);
2144}
2145
2146/**
2147 * i40evf_client_task - worker thread to perform client work
2148 * @work: pointer to work_struct containing our data
2149 *
2150 * This task handles client interactions. Because client calls can be
2151 * reentrant, we can't handle them in the watchdog.
2152 **/
2153static void i40evf_client_task(struct work_struct *work)
2154{
2155	struct i40evf_adapter *adapter =
2156		container_of(work, struct i40evf_adapter, client_task.work);
2157
2158	/* If we can't get the client bit, just give up. We'll be rescheduled
2159	 * later.
2160	 */
2161
2162	if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2163		return;
2164
2165	if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2166		i40evf_client_subtask(adapter);
2167		adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2168		goto out;
2169	}
2170	if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2171		i40evf_notify_client_l2_params(&adapter->vsi);
2172		adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2173		goto out;
2174	}
2175	if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2176		i40evf_notify_client_close(&adapter->vsi, false);
2177		adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2178		goto out;
2179	}
2180	if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2181		i40evf_notify_client_open(&adapter->vsi);
2182		adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2183	}
2184out:
2185	clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2186}
2187
2188/**
2189 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2190 * @adapter: board private structure
2191 *
2192 * Free all transmit software resources
2193 **/
2194void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
2195{
2196	int i;
2197
2198	if (!adapter->tx_rings)
2199		return;
2200
2201	for (i = 0; i < adapter->num_active_queues; i++)
2202		if (adapter->tx_rings[i].desc)
2203			i40evf_free_tx_resources(&adapter->tx_rings[i]);
2204}
2205
2206/**
2207 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2208 * @adapter: board private structure
2209 *
2210 * If this function returns with an error, then it's possible one or
2211 * more of the rings is populated (while the rest are not).  It is the
2212 * callers duty to clean those orphaned rings.
2213 *
2214 * Return 0 on success, negative on failure
2215 **/
2216static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2217{
2218	int i, err = 0;
2219
2220	for (i = 0; i < adapter->num_active_queues; i++) {
2221		adapter->tx_rings[i].count = adapter->tx_desc_count;
2222		err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
2223		if (!err)
2224			continue;
2225		dev_err(&adapter->pdev->dev,
2226			"Allocation for Tx Queue %u failed\n", i);
2227		break;
2228	}
2229
2230	return err;
2231}
2232
2233/**
2234 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2235 * @adapter: board private structure
2236 *
2237 * If this function returns with an error, then it's possible one or
2238 * more of the rings is populated (while the rest are not).  It is the
2239 * callers duty to clean those orphaned rings.
2240 *
2241 * Return 0 on success, negative on failure
2242 **/
2243static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2244{
2245	int i, err = 0;
2246
2247	for (i = 0; i < adapter->num_active_queues; i++) {
2248		adapter->rx_rings[i].count = adapter->rx_desc_count;
2249		err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
2250		if (!err)
2251			continue;
2252		dev_err(&adapter->pdev->dev,
2253			"Allocation for Rx Queue %u failed\n", i);
2254		break;
2255	}
2256	return err;
2257}
2258
2259/**
2260 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2261 * @adapter: board private structure
2262 *
2263 * Free all receive software resources
2264 **/
2265void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
2266{
2267	int i;
2268
2269	if (!adapter->rx_rings)
2270		return;
2271
2272	for (i = 0; i < adapter->num_active_queues; i++)
2273		if (adapter->rx_rings[i].desc)
2274			i40evf_free_rx_resources(&adapter->rx_rings[i]);
2275}
2276
2277/**
2278 * i40evf_validate_tx_bandwidth - validate the max Tx bandwidth
2279 * @adapter: board private structure
2280 * @max_tx_rate: max Tx bw for a tc
2281 **/
2282static int i40evf_validate_tx_bandwidth(struct i40evf_adapter *adapter,
2283					u64 max_tx_rate)
2284{
2285	int speed = 0, ret = 0;
2286
2287	switch (adapter->link_speed) {
2288	case I40E_LINK_SPEED_40GB:
2289		speed = 40000;
2290		break;
2291	case I40E_LINK_SPEED_25GB:
2292		speed = 25000;
2293		break;
2294	case I40E_LINK_SPEED_20GB:
2295		speed = 20000;
2296		break;
2297	case I40E_LINK_SPEED_10GB:
2298		speed = 10000;
2299		break;
2300	case I40E_LINK_SPEED_1GB:
2301		speed = 1000;
2302		break;
2303	case I40E_LINK_SPEED_100MB:
2304		speed = 100;
2305		break;
2306	default:
2307		break;
2308	}
2309
2310	if (max_tx_rate > speed) {
2311		dev_err(&adapter->pdev->dev,
2312			"Invalid tx rate specified\n");
2313		ret = -EINVAL;
2314	}
2315
2316	return ret;
2317}
2318
2319/**
2320 * i40evf_validate_channel_config - validate queue mapping info
2321 * @adapter: board private structure
2322 * @mqprio_qopt: queue parameters
2323 *
2324 * This function validates if the config provided by the user to
2325 * configure queue channels is valid or not. Returns 0 on a valid
2326 * config.
2327 **/
2328static int i40evf_validate_ch_config(struct i40evf_adapter *adapter,
2329				     struct tc_mqprio_qopt_offload *mqprio_qopt)
2330{
2331	u64 total_max_rate = 0;
2332	int i, num_qps = 0;
2333	u64 tx_rate = 0;
2334	int ret = 0;
2335
2336	if (mqprio_qopt->qopt.num_tc > I40EVF_MAX_TRAFFIC_CLASS ||
2337	    mqprio_qopt->qopt.num_tc < 1)
2338		return -EINVAL;
2339
2340	for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2341		if (!mqprio_qopt->qopt.count[i] ||
2342		    mqprio_qopt->qopt.offset[i] != num_qps)
2343			return -EINVAL;
2344		if (mqprio_qopt->min_rate[i]) {
2345			dev_err(&adapter->pdev->dev,
2346				"Invalid min tx rate (greater than 0) specified\n");
2347			return -EINVAL;
2348		}
2349		/*convert to Mbps */
2350		tx_rate = div_u64(mqprio_qopt->max_rate[i],
2351				  I40EVF_MBPS_DIVISOR);
2352		total_max_rate += tx_rate;
2353		num_qps += mqprio_qopt->qopt.count[i];
2354	}
2355	if (num_qps > MAX_QUEUES)
2356		return -EINVAL;
2357
2358	ret = i40evf_validate_tx_bandwidth(adapter, total_max_rate);
2359	return ret;
2360}
2361
2362/**
2363 * i40evf_del_all_cloud_filters - delete all cloud filters
2364 * on the traffic classes
2365 **/
2366static void i40evf_del_all_cloud_filters(struct i40evf_adapter *adapter)
2367{
2368	struct i40evf_cloud_filter *cf, *cftmp;
2369
2370	spin_lock_bh(&adapter->cloud_filter_list_lock);
2371	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2372				 list) {
2373		list_del(&cf->list);
2374		kfree(cf);
2375		adapter->num_cloud_filters--;
2376	}
2377	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2378}
2379
2380/**
2381 * __i40evf_setup_tc - configure multiple traffic classes
2382 * @netdev: network interface device structure
2383 * @type_date: tc offload data
2384 *
2385 * This function processes the config information provided by the
2386 * user to configure traffic classes/queue channels and packages the
2387 * information to request the PF to setup traffic classes.
2388 *
2389 * Returns 0 on success.
2390 **/
2391static int __i40evf_setup_tc(struct net_device *netdev, void *type_data)
2392{
2393	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2394	struct i40evf_adapter *adapter = netdev_priv(netdev);
2395	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2396	u8 num_tc = 0, total_qps = 0;
2397	int ret = 0, netdev_tc = 0;
2398	u64 max_tx_rate;
2399	u16 mode;
2400	int i;
2401
2402	num_tc = mqprio_qopt->qopt.num_tc;
2403	mode = mqprio_qopt->mode;
2404
2405	/* delete queue_channel */
2406	if (!mqprio_qopt->qopt.hw) {
2407		if (adapter->ch_config.state == __I40EVF_TC_RUNNING) {
2408			/* reset the tc configuration */
2409			netdev_reset_tc(netdev);
2410			adapter->num_tc = 0;
2411			netif_tx_stop_all_queues(netdev);
2412			netif_tx_disable(netdev);
2413			i40evf_del_all_cloud_filters(adapter);
2414			adapter->aq_required = I40EVF_FLAG_AQ_DISABLE_CHANNELS;
2415			goto exit;
2416		} else {
2417			return -EINVAL;
2418		}
2419	}
2420
2421	/* add queue channel */
2422	if (mode == TC_MQPRIO_MODE_CHANNEL) {
2423		if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2424			dev_err(&adapter->pdev->dev, "ADq not supported\n");
2425			return -EOPNOTSUPP;
2426		}
2427		if (adapter->ch_config.state != __I40EVF_TC_INVALID) {
2428			dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2429			return -EINVAL;
2430		}
2431
2432		ret = i40evf_validate_ch_config(adapter, mqprio_qopt);
2433		if (ret)
2434			return ret;
2435		/* Return if same TC config is requested */
2436		if (adapter->num_tc == num_tc)
2437			return 0;
2438		adapter->num_tc = num_tc;
2439
2440		for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2441			if (i < num_tc) {
2442				adapter->ch_config.ch_info[i].count =
2443					mqprio_qopt->qopt.count[i];
2444				adapter->ch_config.ch_info[i].offset =
2445					mqprio_qopt->qopt.offset[i];
2446				total_qps += mqprio_qopt->qopt.count[i];
2447				max_tx_rate = mqprio_qopt->max_rate[i];
2448				/* convert to Mbps */
2449				max_tx_rate = div_u64(max_tx_rate,
2450						      I40EVF_MBPS_DIVISOR);
2451				adapter->ch_config.ch_info[i].max_tx_rate =
2452					max_tx_rate;
2453			} else {
2454				adapter->ch_config.ch_info[i].count = 1;
2455				adapter->ch_config.ch_info[i].offset = 0;
2456			}
2457		}
2458		adapter->ch_config.total_qps = total_qps;
2459		netif_tx_stop_all_queues(netdev);
2460		netif_tx_disable(netdev);
2461		adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_CHANNELS;
2462		netdev_reset_tc(netdev);
2463		/* Report the tc mapping up the stack */
2464		netdev_set_num_tc(adapter->netdev, num_tc);
2465		for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2466			u16 qcount = mqprio_qopt->qopt.count[i];
2467			u16 qoffset = mqprio_qopt->qopt.offset[i];
2468
2469			if (i < num_tc)
2470				netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2471						    qoffset);
2472		}
2473	}
2474exit:
2475	return ret;
2476}
2477
2478/**
2479 * i40evf_parse_cls_flower - Parse tc flower filters provided by kernel
2480 * @adapter: board private structure
2481 * @cls_flower: pointer to struct tc_cls_flower_offload
2482 * @filter: pointer to cloud filter structure
2483 */
2484static int i40evf_parse_cls_flower(struct i40evf_adapter *adapter,
2485				   struct tc_cls_flower_offload *f,
2486				   struct i40evf_cloud_filter *filter)
2487{
2488	u16 n_proto_mask = 0;
2489	u16 n_proto_key = 0;
2490	u8 field_flags = 0;
2491	u16 addr_type = 0;
2492	u16 n_proto = 0;
2493	int i = 0;
2494	struct virtchnl_filter *vf = &filter->f;
2495
2496	if (f->dissector->used_keys &
2497	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2498	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
2499	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2500	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
2501	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2502	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2503	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
2504	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2505		dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2506			f->dissector->used_keys);
2507		return -EOPNOTSUPP;
2508	}
2509
2510	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2511		struct flow_dissector_key_keyid *mask =
2512			skb_flow_dissector_target(f->dissector,
2513						  FLOW_DISSECTOR_KEY_ENC_KEYID,
2514						  f->mask);
2515
2516		if (mask->keyid != 0)
2517			field_flags |= I40EVF_CLOUD_FIELD_TEN_ID;
2518	}
2519
2520	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
2521		struct flow_dissector_key_basic *key =
2522			skb_flow_dissector_target(f->dissector,
2523						  FLOW_DISSECTOR_KEY_BASIC,
2524						  f->key);
2525
2526		struct flow_dissector_key_basic *mask =
2527			skb_flow_dissector_target(f->dissector,
2528						  FLOW_DISSECTOR_KEY_BASIC,
2529						  f->mask);
2530		n_proto_key = ntohs(key->n_proto);
2531		n_proto_mask = ntohs(mask->n_proto);
2532
2533		if (n_proto_key == ETH_P_ALL) {
2534			n_proto_key = 0;
2535			n_proto_mask = 0;
2536		}
2537		n_proto = n_proto_key & n_proto_mask;
2538		if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2539			return -EINVAL;
2540		if (n_proto == ETH_P_IPV6) {
2541			/* specify flow type as TCP IPv6 */
2542			vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2543		}
2544
2545		if (key->ip_proto != IPPROTO_TCP) {
2546			dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2547			return -EINVAL;
2548		}
2549	}
2550
2551	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2552		struct flow_dissector_key_eth_addrs *key =
2553			skb_flow_dissector_target(f->dissector,
2554						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
2555						  f->key);
2556
2557		struct flow_dissector_key_eth_addrs *mask =
2558			skb_flow_dissector_target(f->dissector,
2559						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
2560						  f->mask);
2561		/* use is_broadcast and is_zero to check for all 0xf or 0 */
2562		if (!is_zero_ether_addr(mask->dst)) {
2563			if (is_broadcast_ether_addr(mask->dst)) {
2564				field_flags |= I40EVF_CLOUD_FIELD_OMAC;
2565			} else {
2566				dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2567					mask->dst);
2568				return I40E_ERR_CONFIG;
2569			}
2570		}
2571
2572		if (!is_zero_ether_addr(mask->src)) {
2573			if (is_broadcast_ether_addr(mask->src)) {
2574				field_flags |= I40EVF_CLOUD_FIELD_IMAC;
2575			} else {
2576				dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2577					mask->src);
2578				return I40E_ERR_CONFIG;
2579			}
2580		}
2581
2582		if (!is_zero_ether_addr(key->dst))
2583			if (is_valid_ether_addr(key->dst) ||
2584			    is_multicast_ether_addr(key->dst)) {
2585				/* set the mask if a valid dst_mac address */
2586				for (i = 0; i < ETH_ALEN; i++)
2587					vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2588				ether_addr_copy(vf->data.tcp_spec.dst_mac,
2589						key->dst);
2590			}
2591
2592		if (!is_zero_ether_addr(key->src))
2593			if (is_valid_ether_addr(key->src) ||
2594			    is_multicast_ether_addr(key->src)) {
2595				/* set the mask if a valid dst_mac address */
2596				for (i = 0; i < ETH_ALEN; i++)
2597					vf->mask.tcp_spec.src_mac[i] |= 0xff;
2598				ether_addr_copy(vf->data.tcp_spec.src_mac,
2599						key->src);
2600		}
2601	}
2602
2603	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
2604		struct flow_dissector_key_vlan *key =
2605			skb_flow_dissector_target(f->dissector,
2606						  FLOW_DISSECTOR_KEY_VLAN,
2607						  f->key);
2608		struct flow_dissector_key_vlan *mask =
2609			skb_flow_dissector_target(f->dissector,
2610						  FLOW_DISSECTOR_KEY_VLAN,
2611						  f->mask);
2612
2613		if (mask->vlan_id) {
2614			if (mask->vlan_id == VLAN_VID_MASK) {
2615				field_flags |= I40EVF_CLOUD_FIELD_IVLAN;
2616			} else {
2617				dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2618					mask->vlan_id);
2619				return I40E_ERR_CONFIG;
2620			}
2621		}
2622		vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2623		vf->data.tcp_spec.vlan_id = cpu_to_be16(key->vlan_id);
2624	}
2625
2626	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
2627		struct flow_dissector_key_control *key =
2628			skb_flow_dissector_target(f->dissector,
2629						  FLOW_DISSECTOR_KEY_CONTROL,
2630						  f->key);
2631
2632		addr_type = key->addr_type;
2633	}
2634
2635	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2636		struct flow_dissector_key_ipv4_addrs *key =
2637			skb_flow_dissector_target(f->dissector,
2638						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2639						  f->key);
2640		struct flow_dissector_key_ipv4_addrs *mask =
2641			skb_flow_dissector_target(f->dissector,
2642						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2643						  f->mask);
2644
2645		if (mask->dst) {
2646			if (mask->dst == cpu_to_be32(0xffffffff)) {
2647				field_flags |= I40EVF_CLOUD_FIELD_IIP;
2648			} else {
2649				dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2650					be32_to_cpu(mask->dst));
2651				return I40E_ERR_CONFIG;
2652			}
2653		}
2654
2655		if (mask->src) {
2656			if (mask->src == cpu_to_be32(0xffffffff)) {
2657				field_flags |= I40EVF_CLOUD_FIELD_IIP;
2658			} else {
2659				dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2660					be32_to_cpu(mask->dst));
2661				return I40E_ERR_CONFIG;
2662			}
2663		}
2664
2665		if (field_flags & I40EVF_CLOUD_FIELD_TEN_ID) {
2666			dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2667			return I40E_ERR_CONFIG;
2668		}
2669		if (key->dst) {
2670			vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2671			vf->data.tcp_spec.dst_ip[0] = key->dst;
2672		}
2673		if (key->src) {
2674			vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2675			vf->data.tcp_spec.src_ip[0] = key->src;
2676		}
2677	}
2678
2679	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2680		struct flow_dissector_key_ipv6_addrs *key =
2681			skb_flow_dissector_target(f->dissector,
2682						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2683						  f->key);
2684		struct flow_dissector_key_ipv6_addrs *mask =
2685			skb_flow_dissector_target(f->dissector,
2686						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2687						  f->mask);
2688
2689		/* validate mask, make sure it is not IPV6_ADDR_ANY */
2690		if (ipv6_addr_any(&mask->dst)) {
2691			dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2692				IPV6_ADDR_ANY);
2693			return I40E_ERR_CONFIG;
2694		}
2695
2696		/* src and dest IPv6 address should not be LOOPBACK
2697		 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2698		 */
2699		if (ipv6_addr_loopback(&key->dst) ||
2700		    ipv6_addr_loopback(&key->src)) {
2701			dev_err(&adapter->pdev->dev,
2702				"ipv6 addr should not be loopback\n");
2703			return I40E_ERR_CONFIG;
2704		}
2705		if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
2706			field_flags |= I40EVF_CLOUD_FIELD_IIP;
2707
2708		for (i = 0; i < 4; i++)
2709			vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2710		memcpy(&vf->data.tcp_spec.dst_ip, &key->dst.s6_addr32,
2711		       sizeof(vf->data.tcp_spec.dst_ip));
2712		for (i = 0; i < 4; i++)
2713			vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2714		memcpy(&vf->data.tcp_spec.src_ip, &key->src.s6_addr32,
2715		       sizeof(vf->data.tcp_spec.src_ip));
2716	}
2717	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
2718		struct flow_dissector_key_ports *key =
2719			skb_flow_dissector_target(f->dissector,
2720						  FLOW_DISSECTOR_KEY_PORTS,
2721						  f->key);
2722		struct flow_dissector_key_ports *mask =
2723			skb_flow_dissector_target(f->dissector,
2724						  FLOW_DISSECTOR_KEY_PORTS,
2725						  f->mask);
2726
2727		if (mask->src) {
2728			if (mask->src == cpu_to_be16(0xffff)) {
2729				field_flags |= I40EVF_CLOUD_FIELD_IIP;
2730			} else {
2731				dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2732					be16_to_cpu(mask->src));
2733				return I40E_ERR_CONFIG;
2734			}
2735		}
2736
2737		if (mask->dst) {
2738			if (mask->dst == cpu_to_be16(0xffff)) {
2739				field_flags |= I40EVF_CLOUD_FIELD_IIP;
2740			} else {
2741				dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2742					be16_to_cpu(mask->dst));
2743				return I40E_ERR_CONFIG;
2744			}
2745		}
2746		if (key->dst) {
2747			vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2748			vf->data.tcp_spec.dst_port = key->dst;
2749		}
2750
2751		if (key->src) {
2752			vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2753			vf->data.tcp_spec.src_port = key->src;
2754		}
2755	}
2756	vf->field_flags = field_flags;
2757
2758	return 0;
2759}
2760
2761/**
2762 * i40evf_handle_tclass - Forward to a traffic class on the device
2763 * @adapter: board private structure
2764 * @tc: traffic class index on the device
2765 * @filter: pointer to cloud filter structure
2766 */
2767static int i40evf_handle_tclass(struct i40evf_adapter *adapter, u32 tc,
2768				struct i40evf_cloud_filter *filter)
2769{
2770	if (tc == 0)
2771		return 0;
2772	if (tc < adapter->num_tc) {
2773		if (!filter->f.data.tcp_spec.dst_port) {
2774			dev_err(&adapter->pdev->dev,
2775				"Specify destination port to redirect to traffic class other than TC0\n");
2776			return -EINVAL;
2777		}
2778	}
2779	/* redirect to a traffic class on the same device */
2780	filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2781	filter->f.action_meta = tc;
2782	return 0;
2783}
2784
2785/**
2786 * i40evf_configure_clsflower - Add tc flower filters
2787 * @adapter: board private structure
2788 * @cls_flower: Pointer to struct tc_cls_flower_offload
2789 */
2790static int i40evf_configure_clsflower(struct i40evf_adapter *adapter,
2791				      struct tc_cls_flower_offload *cls_flower)
2792{
2793	int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2794	struct i40evf_cloud_filter *filter = NULL;
2795	int err = -EINVAL, count = 50;
2796
2797	if (tc < 0) {
2798		dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2799		return -EINVAL;
2800	}
2801
2802	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2803	if (!filter)
2804		return -ENOMEM;
2805
2806	while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2807				&adapter->crit_section)) {
2808		if (--count == 0)
2809			goto err;
2810		udelay(1);
2811	}
2812
2813	filter->cookie = cls_flower->cookie;
2814
2815	/* set the mask to all zeroes to begin with */
2816	memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
2817	/* start out with flow type and eth type IPv4 to begin with */
2818	filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
2819	err = i40evf_parse_cls_flower(adapter, cls_flower, filter);
2820	if (err < 0)
2821		goto err;
2822
2823	err = i40evf_handle_tclass(adapter, tc, filter);
2824	if (err < 0)
2825		goto err;
2826
2827	/* add filter to the list */
2828	spin_lock_bh(&adapter->cloud_filter_list_lock);
2829	list_add_tail(&filter->list, &adapter->cloud_filter_list);
2830	adapter->num_cloud_filters++;
2831	filter->add = true;
2832	adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
2833	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2834err:
2835	if (err)
2836		kfree(filter);
2837
2838	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2839	return err;
2840}
2841
2842/* i40evf_find_cf - Find the cloud filter in the list
2843 * @adapter: Board private structure
2844 * @cookie: filter specific cookie
2845 *
2846 * Returns ptr to the filter object or NULL. Must be called while holding the
2847 * cloud_filter_list_lock.
2848 */
2849static struct i40evf_cloud_filter *i40evf_find_cf(struct i40evf_adapter *adapter,
2850						  unsigned long *cookie)
2851{
2852	struct i40evf_cloud_filter *filter = NULL;
2853
2854	if (!cookie)
2855		return NULL;
2856
2857	list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
2858		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
2859			return filter;
2860	}
2861	return NULL;
2862}
2863
2864/**
2865 * i40evf_delete_clsflower - Remove tc flower filters
2866 * @adapter: board private structure
2867 * @cls_flower: Pointer to struct tc_cls_flower_offload
2868 */
2869static int i40evf_delete_clsflower(struct i40evf_adapter *adapter,
2870				   struct tc_cls_flower_offload *cls_flower)
2871{
2872	struct i40evf_cloud_filter *filter = NULL;
2873	int err = 0;
2874
2875	spin_lock_bh(&adapter->cloud_filter_list_lock);
2876	filter = i40evf_find_cf(adapter, &cls_flower->cookie);
2877	if (filter) {
2878		filter->del = true;
2879		adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
2880	} else {
2881		err = -EINVAL;
2882	}
2883	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2884
2885	return err;
2886}
2887
2888/**
2889 * i40evf_setup_tc_cls_flower - flower classifier offloads
2890 * @netdev: net device to configure
2891 * @type_data: offload data
2892 */
2893static int i40evf_setup_tc_cls_flower(struct i40evf_adapter *adapter,
2894				      struct tc_cls_flower_offload *cls_flower)
2895{
2896	if (cls_flower->common.chain_index)
2897		return -EOPNOTSUPP;
2898
2899	switch (cls_flower->command) {
2900	case TC_CLSFLOWER_REPLACE:
2901		return i40evf_configure_clsflower(adapter, cls_flower);
2902	case TC_CLSFLOWER_DESTROY:
2903		return i40evf_delete_clsflower(adapter, cls_flower);
2904	case TC_CLSFLOWER_STATS:
2905		return -EOPNOTSUPP;
2906	default:
2907		return -EINVAL;
2908	}
2909}
2910
2911/**
2912 * i40evf_setup_tc_block_cb - block callback for tc
2913 * @type: type of offload
2914 * @type_data: offload data
2915 * @cb_priv:
2916 *
2917 * This function is the block callback for traffic classes
2918 **/
2919static int i40evf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2920				    void *cb_priv)
2921{
2922	switch (type) {
2923	case TC_SETUP_CLSFLOWER:
2924		return i40evf_setup_tc_cls_flower(cb_priv, type_data);
2925	default:
2926		return -EOPNOTSUPP;
2927	}
2928}
2929
2930/**
2931 * i40evf_setup_tc_block - register callbacks for tc
2932 * @netdev: network interface device structure
2933 * @f: tc offload data
2934 *
2935 * This function registers block callbacks for tc
2936 * offloads
2937 **/
2938static int i40evf_setup_tc_block(struct net_device *dev,
2939				 struct tc_block_offload *f)
2940{
2941	struct i40evf_adapter *adapter = netdev_priv(dev);
2942
2943	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2944		return -EOPNOTSUPP;
2945
2946	switch (f->command) {
2947	case TC_BLOCK_BIND:
2948		return tcf_block_cb_register(f->block, i40evf_setup_tc_block_cb,
2949					     adapter, adapter);
2950	case TC_BLOCK_UNBIND:
2951		tcf_block_cb_unregister(f->block, i40evf_setup_tc_block_cb,
2952					adapter);
2953		return 0;
2954	default:
2955		return -EOPNOTSUPP;
2956	}
2957}
2958
2959/**
2960 * i40evf_setup_tc - configure multiple traffic classes
2961 * @netdev: network interface device structure
2962 * @type: type of offload
2963 * @type_date: tc offload data
2964 *
2965 * This function is the callback to ndo_setup_tc in the
2966 * netdev_ops.
2967 *
2968 * Returns 0 on success
2969 **/
2970static int i40evf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2971			   void *type_data)
2972{
2973	switch (type) {
2974	case TC_SETUP_QDISC_MQPRIO:
2975		return __i40evf_setup_tc(netdev, type_data);
2976	case TC_SETUP_BLOCK:
2977		return i40evf_setup_tc_block(netdev, type_data);
2978	default:
2979		return -EOPNOTSUPP;
2980	}
2981}
2982
2983/**
2984 * i40evf_open - Called when a network interface is made active
2985 * @netdev: network interface device structure
2986 *
2987 * Returns 0 on success, negative value on failure
2988 *
2989 * The open entry point is called when a network interface is made
2990 * active by the system (IFF_UP).  At this point all resources needed
2991 * for transmit and receive operations are allocated, the interrupt
2992 * handler is registered with the OS, the watchdog timer is started,
2993 * and the stack is notified that the interface is ready.
2994 **/
2995static int i40evf_open(struct net_device *netdev)
2996{
2997	struct i40evf_adapter *adapter = netdev_priv(netdev);
2998	int err;
2999
3000	if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
3001		dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3002		return -EIO;
3003	}
3004
3005	while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3006				&adapter->crit_section))
3007		usleep_range(500, 1000);
3008
3009	if (adapter->state != __I40EVF_DOWN) {
3010		err = -EBUSY;
3011		goto err_unlock;
3012	}
3013
3014	/* allocate transmit descriptors */
3015	err = i40evf_setup_all_tx_resources(adapter);
3016	if (err)
3017		goto err_setup_tx;
3018
3019	/* allocate receive descriptors */
3020	err = i40evf_setup_all_rx_resources(adapter);
3021	if (err)
3022		goto err_setup_rx;
3023
3024	/* clear any pending interrupts, may auto mask */
3025	err = i40evf_request_traffic_irqs(adapter, netdev->name);
3026	if (err)
3027		goto err_req_irq;
3028
3029	spin_lock_bh(&adapter->mac_vlan_list_lock);
3030
3031	i40evf_add_filter(adapter, adapter->hw.mac.addr);
3032
3033	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3034
3035	i40evf_configure(adapter);
3036
3037	i40evf_up_complete(adapter);
3038
3039	i40evf_irq_enable(adapter, true);
3040
3041	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3042
3043	return 0;
3044
3045err_req_irq:
3046	i40evf_down(adapter);
3047	i40evf_free_traffic_irqs(adapter);
3048err_setup_rx:
3049	i40evf_free_all_rx_resources(adapter);
3050err_setup_tx:
3051	i40evf_free_all_tx_resources(adapter);
3052err_unlock:
3053	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3054
3055	return err;
3056}
3057
3058/**
3059 * i40evf_close - Disables a network interface
3060 * @netdev: network interface device structure
3061 *
3062 * Returns 0, this is not allowed to fail
3063 *
3064 * The close entry point is called when an interface is de-activated
3065 * by the OS.  The hardware is still under the drivers control, but
3066 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3067 * are freed, along with all transmit and receive resources.
3068 **/
3069static int i40evf_close(struct net_device *netdev)
3070{
3071	struct i40evf_adapter *adapter = netdev_priv(netdev);
3072	int status;
3073
3074	if (adapter->state <= __I40EVF_DOWN_PENDING)
3075		return 0;
3076
3077	while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3078				&adapter->crit_section))
3079		usleep_range(500, 1000);
3080
3081	set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
3082	if (CLIENT_ENABLED(adapter))
3083		adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
3084
3085	i40evf_down(adapter);
3086	adapter->state = __I40EVF_DOWN_PENDING;
3087	i40evf_free_traffic_irqs(adapter);
3088
3089	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3090
3091	/* We explicitly don't free resources here because the hardware is
3092	 * still active and can DMA into memory. Resources are cleared in
3093	 * i40evf_virtchnl_completion() after we get confirmation from the PF
3094	 * driver that the rings have been stopped.
3095	 *
3096	 * Also, we wait for state to transition to __I40EVF_DOWN before
3097	 * returning. State change occurs in i40evf_virtchnl_completion() after
3098	 * VF resources are released (which occurs after PF driver processes and
3099	 * responds to admin queue commands).
3100	 */
3101
3102	status = wait_event_timeout(adapter->down_waitqueue,
3103				    adapter->state == __I40EVF_DOWN,
3104				    msecs_to_jiffies(200));
3105	if (!status)
3106		netdev_warn(netdev, "Device resources not yet released\n");
3107	return 0;
3108}
3109
3110/**
3111 * i40evf_change_mtu - Change the Maximum Transfer Unit
3112 * @netdev: network interface device structure
3113 * @new_mtu: new value for maximum frame size
3114 *
3115 * Returns 0 on success, negative on failure
3116 **/
3117static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
3118{
3119	struct i40evf_adapter *adapter = netdev_priv(netdev);
3120
3121	netdev->mtu = new_mtu;
3122	if (CLIENT_ENABLED(adapter)) {
3123		i40evf_notify_client_l2_params(&adapter->vsi);
3124		adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
3125	}
3126	adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
3127	schedule_work(&adapter->reset_task);
3128
3129	return 0;
3130}
3131
3132/**
3133 * i40e_set_features - set the netdev feature flags
3134 * @netdev: ptr to the netdev being adjusted
3135 * @features: the feature set that the stack is suggesting
3136 * Note: expects to be called while under rtnl_lock()
3137 **/
3138static int i40evf_set_features(struct net_device *netdev,
3139			       netdev_features_t features)
3140{
3141	struct i40evf_adapter *adapter = netdev_priv(netdev);
3142
3143	/* Don't allow changing VLAN_RX flag when VLAN is set for VF
3144	 * and return an error in this case
3145	 */
3146	if (VLAN_ALLOWED(adapter)) {
3147		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3148			adapter->aq_required |=
3149				I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3150		else
3151			adapter->aq_required |=
3152				I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3153	} else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3154		return -EINVAL;
3155	}
3156
3157	return 0;
3158}
3159
3160/**
3161 * i40evf_features_check - Validate encapsulated packet conforms to limits
3162 * @skb: skb buff
3163 * @netdev: This physical port's netdev
3164 * @features: Offload features that the stack believes apply
3165 **/
3166static netdev_features_t i40evf_features_check(struct sk_buff *skb,
3167					       struct net_device *dev,
3168					       netdev_features_t features)
3169{
3170	size_t len;
3171
3172	/* No point in doing any of this if neither checksum nor GSO are
3173	 * being requested for this frame.  We can rule out both by just
3174	 * checking for CHECKSUM_PARTIAL
3175	 */
3176	if (skb->ip_summed != CHECKSUM_PARTIAL)
3177		return features;
3178
3179	/* We cannot support GSO if the MSS is going to be less than
3180	 * 64 bytes.  If it is then we need to drop support for GSO.
3181	 */
3182	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3183		features &= ~NETIF_F_GSO_MASK;
3184
3185	/* MACLEN can support at most 63 words */
3186	len = skb_network_header(skb) - skb->data;
3187	if (len & ~(63 * 2))
3188		goto out_err;
3189
3190	/* IPLEN and EIPLEN can support at most 127 dwords */
3191	len = skb_transport_header(skb) - skb_network_header(skb);
3192	if (len & ~(127 * 4))
3193		goto out_err;
3194
3195	if (skb->encapsulation) {
3196		/* L4TUNLEN can support 127 words */
3197		len = skb_inner_network_header(skb) - skb_transport_header(skb);
3198		if (len & ~(127 * 2))
3199			goto out_err;
3200
3201		/* IPLEN can support at most 127 dwords */
3202		len = skb_inner_transport_header(skb) -
3203		      skb_inner_network_header(skb);
3204		if (len & ~(127 * 4))
3205			goto out_err;
3206	}
3207
3208	/* No need to validate L4LEN as TCP is the only protocol with a
3209	 * a flexible value and we support all possible values supported
3210	 * by TCP, which is at most 15 dwords
3211	 */
3212
3213	return features;
3214out_err:
3215	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3216}
3217
3218/**
3219 * i40evf_fix_features - fix up the netdev feature bits
3220 * @netdev: our net device
3221 * @features: desired feature bits
3222 *
3223 * Returns fixed-up features bits
3224 **/
3225static netdev_features_t i40evf_fix_features(struct net_device *netdev,
3226					     netdev_features_t features)
3227{
3228	struct i40evf_adapter *adapter = netdev_priv(netdev);
3229
3230	if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3231		features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3232			      NETIF_F_HW_VLAN_CTAG_RX |
3233			      NETIF_F_HW_VLAN_CTAG_FILTER);
3234
3235	return features;
3236}
3237
3238static const struct net_device_ops i40evf_netdev_ops = {
3239	.ndo_open		= i40evf_open,
3240	.ndo_stop		= i40evf_close,
3241	.ndo_start_xmit		= i40evf_xmit_frame,
3242	.ndo_set_rx_mode	= i40evf_set_rx_mode,
3243	.ndo_validate_addr	= eth_validate_addr,
3244	.ndo_set_mac_address	= i40evf_set_mac,
3245	.ndo_change_mtu		= i40evf_change_mtu,
3246	.ndo_tx_timeout		= i40evf_tx_timeout,
3247	.ndo_vlan_rx_add_vid	= i40evf_vlan_rx_add_vid,
3248	.ndo_vlan_rx_kill_vid	= i40evf_vlan_rx_kill_vid,
3249	.ndo_features_check	= i40evf_features_check,
3250	.ndo_fix_features	= i40evf_fix_features,
3251	.ndo_set_features	= i40evf_set_features,
3252#ifdef CONFIG_NET_POLL_CONTROLLER
3253	.ndo_poll_controller	= i40evf_netpoll,
3254#endif
3255	.ndo_setup_tc		= i40evf_setup_tc,
3256};
3257
3258/**
3259 * i40evf_check_reset_complete - check that VF reset is complete
3260 * @hw: pointer to hw struct
3261 *
3262 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3263 **/
3264static int i40evf_check_reset_complete(struct i40e_hw *hw)
3265{
3266	u32 rstat;
3267	int i;
3268
3269	for (i = 0; i < 100; i++) {
3270		rstat = rd32(hw, I40E_VFGEN_RSTAT) &
3271			    I40E_VFGEN_RSTAT_VFR_STATE_MASK;
3272		if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3273		    (rstat == VIRTCHNL_VFR_COMPLETED))
3274			return 0;
3275		usleep_range(10, 20);
3276	}
3277	return -EBUSY;
3278}
3279
3280/**
3281 * i40evf_process_config - Process the config information we got from the PF
3282 * @adapter: board private structure
3283 *
3284 * Verify that we have a valid config struct, and set up our netdev features
3285 * and our VSI struct.
3286 **/
3287int i40evf_process_config(struct i40evf_adapter *adapter)
3288{
3289	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3290	int i, num_req_queues = adapter->num_req_queues;
3291	struct net_device *netdev = adapter->netdev;
3292	struct i40e_vsi *vsi = &adapter->vsi;
3293	netdev_features_t hw_enc_features;
3294	netdev_features_t hw_features;
3295
3296	/* got VF config message back from PF, now we can parse it */
3297	for (i = 0; i < vfres->num_vsis; i++) {
3298		if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3299			adapter->vsi_res = &vfres->vsi_res[i];
3300	}
3301	if (!adapter->vsi_res) {
3302		dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3303		return -ENODEV;
3304	}
3305
3306	if (num_req_queues &&
3307	    num_req_queues != adapter->vsi_res->num_queue_pairs) {
3308		/* Problem.  The PF gave us fewer queues than what we had
3309		 * negotiated in our request.  Need a reset to see if we can't
3310		 * get back to a working state.
3311		 */
3312		dev_err(&adapter->pdev->dev,
3313			"Requested %d queues, but PF only gave us %d.\n",
3314			num_req_queues,
3315			adapter->vsi_res->num_queue_pairs);
3316		adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
3317		adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3318		i40evf_schedule_reset(adapter);
3319		return -ENODEV;
3320	}
3321	adapter->num_req_queues = 0;
3322
3323	hw_enc_features = NETIF_F_SG			|
3324			  NETIF_F_IP_CSUM		|
3325			  NETIF_F_IPV6_CSUM		|
3326			  NETIF_F_HIGHDMA		|
3327			  NETIF_F_SOFT_FEATURES	|
3328			  NETIF_F_TSO			|
3329			  NETIF_F_TSO_ECN		|
3330			  NETIF_F_TSO6			|
3331			  NETIF_F_SCTP_CRC		|
3332			  NETIF_F_RXHASH		|
3333			  NETIF_F_RXCSUM		|
3334			  0;
3335
3336	/* advertise to stack only if offloads for encapsulated packets is
3337	 * supported
3338	 */
3339	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3340		hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL	|
3341				   NETIF_F_GSO_GRE		|
3342				   NETIF_F_GSO_GRE_CSUM		|
3343				   NETIF_F_GSO_IPXIP4		|
3344				   NETIF_F_GSO_IPXIP6		|
3345				   NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3346				   NETIF_F_GSO_PARTIAL		|
3347				   0;
3348
3349		if (!(vfres->vf_cap_flags &
3350		      VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3351			netdev->gso_partial_features |=
3352				NETIF_F_GSO_UDP_TUNNEL_CSUM;
3353
3354		netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3355		netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3356		netdev->hw_enc_features |= hw_enc_features;
3357	}
3358	/* record features VLANs can make use of */
3359	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3360
3361	/* Write features and hw_features separately to avoid polluting
3362	 * with, or dropping, features that are set when we registered.
3363	 */
3364	hw_features = hw_enc_features;
3365
3366	/* Enable VLAN features if supported */
3367	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3368		hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3369				NETIF_F_HW_VLAN_CTAG_RX);
3370	/* Enable cloud filter if ADQ is supported */
3371	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3372		hw_features |= NETIF_F_HW_TC;
3373
3374	netdev->hw_features |= hw_features;
3375
3376	netdev->features |= hw_features;
3377
3378	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3379		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3380
3381	adapter->vsi.id = adapter->vsi_res->vsi_id;
3382
3383	adapter->vsi.back = adapter;
3384	adapter->vsi.base_vector = 1;
3385	adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
3386	vsi->netdev = adapter->netdev;
3387	vsi->qs_handle = adapter->vsi_res->qset_handle;
3388	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3389		adapter->rss_key_size = vfres->rss_key_size;
3390		adapter->rss_lut_size = vfres->rss_lut_size;
3391	} else {
3392		adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
3393		adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
3394	}
3395
3396	return 0;
3397}
3398
3399/**
3400 * i40evf_init_task - worker thread to perform delayed initialization
3401 * @work: pointer to work_struct containing our data
3402 *
3403 * This task completes the work that was begun in probe. Due to the nature
3404 * of VF-PF communications, we may need to wait tens of milliseconds to get
3405 * responses back from the PF. Rather than busy-wait in probe and bog down the
3406 * whole system, we'll do it in a task so we can sleep.
3407 * This task only runs during driver init. Once we've established
3408 * communications with the PF driver and set up our netdev, the watchdog
3409 * takes over.
3410 **/
3411static void i40evf_init_task(struct work_struct *work)
3412{
3413	struct i40evf_adapter *adapter = container_of(work,
3414						      struct i40evf_adapter,
3415						      init_task.work);
3416	struct net_device *netdev = adapter->netdev;
3417	struct i40e_hw *hw = &adapter->hw;
3418	struct pci_dev *pdev = adapter->pdev;
3419	int err, bufsz;
3420
3421	switch (adapter->state) {
3422	case __I40EVF_STARTUP:
3423		/* driver loaded, probe complete */
3424		adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
3425		adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
3426		err = i40e_set_mac_type(hw);
3427		if (err) {
3428			dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
3429				err);
3430			goto err;
3431		}
3432		err = i40evf_check_reset_complete(hw);
3433		if (err) {
3434			dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
3435				 err);
3436			goto err;
3437		}
3438		hw->aq.num_arq_entries = I40EVF_AQ_LEN;
3439		hw->aq.num_asq_entries = I40EVF_AQ_LEN;
3440		hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
3441		hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
3442
3443		err = i40evf_init_adminq(hw);
3444		if (err) {
3445			dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
3446				err);
3447			goto err;
3448		}
3449		err = i40evf_send_api_ver(adapter);
3450		if (err) {
3451			dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
3452			i40evf_shutdown_adminq(hw);
3453			goto err;
3454		}
3455		adapter->state = __I40EVF_INIT_VERSION_CHECK;
3456		goto restart;
3457	case __I40EVF_INIT_VERSION_CHECK:
3458		if (!i40evf_asq_done(hw)) {
3459			dev_err(&pdev->dev, "Admin queue command never completed\n");
3460			i40evf_shutdown_adminq(hw);
3461			adapter->state = __I40EVF_STARTUP;
3462			goto err;
3463		}
3464
3465		/* aq msg sent, awaiting reply */
3466		err = i40evf_verify_api_ver(adapter);
3467		if (err) {
3468			if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
3469				err = i40evf_send_api_ver(adapter);
3470			else
3471				dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
3472					adapter->pf_version.major,
3473					adapter->pf_version.minor,
3474					VIRTCHNL_VERSION_MAJOR,
3475					VIRTCHNL_VERSION_MINOR);
3476			goto err;
3477		}
3478		err = i40evf_send_vf_config_msg(adapter);
3479		if (err) {
3480			dev_err(&pdev->dev, "Unable to send config request (%d)\n",
3481				err);
3482			goto err;
3483		}
3484		adapter->state = __I40EVF_INIT_GET_RESOURCES;
3485		goto restart;
3486	case __I40EVF_INIT_GET_RESOURCES:
3487		/* aq msg sent, awaiting reply */
3488		if (!adapter->vf_res) {
3489			bufsz = sizeof(struct virtchnl_vf_resource) +
3490				(I40E_MAX_VF_VSI *
3491				 sizeof(struct virtchnl_vsi_resource));
3492			adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
3493			if (!adapter->vf_res)
3494				goto err;
3495		}
3496		err = i40evf_get_vf_config(adapter);
3497		if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
3498			err = i40evf_send_vf_config_msg(adapter);
3499			goto err;
3500		} else if (err == I40E_ERR_PARAM) {
3501			/* We only get ERR_PARAM if the device is in a very bad
3502			 * state or if we've been disabled for previous bad
3503			 * behavior. Either way, we're done now.
3504			 */
3505			i40evf_shutdown_adminq(hw);
3506			dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
3507			return;
3508		}
3509		if (err) {
3510			dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
3511				err);
3512			goto err_alloc;
3513		}
3514		adapter->state = __I40EVF_INIT_SW;
3515		break;
3516	default:
3517		goto err_alloc;
3518	}
3519
3520	if (i40evf_process_config(adapter))
3521		goto err_alloc;
3522	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3523
3524	adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
3525
3526	netdev->netdev_ops = &i40evf_netdev_ops;
3527	i40evf_set_ethtool_ops(netdev);
3528	netdev->watchdog_timeo = 5 * HZ;
3529
3530	/* MTU range: 68 - 9710 */
3531	netdev->min_mtu = ETH_MIN_MTU;
3532	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
3533
3534	if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
3535		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
3536			 adapter->hw.mac.addr);
3537		eth_hw_addr_random(netdev);
3538		ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
3539	} else {
3540		adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
3541		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
3542		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
3543	}
3544
3545	timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
3546	mod_timer(&adapter->watchdog_timer, jiffies + 1);
3547
3548	adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
3549	adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
3550	err = i40evf_init_interrupt_scheme(adapter);
3551	if (err)
3552		goto err_sw_init;
3553	i40evf_map_rings_to_vectors(adapter);
3554	if (adapter->vf_res->vf_cap_flags &
3555	    VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
3556		adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
3557
3558	err = i40evf_request_misc_irq(adapter);
3559	if (err)
3560		goto err_sw_init;
3561
3562	netif_carrier_off(netdev);
3563	adapter->link_up = false;
3564
3565	if (!adapter->netdev_registered) {
3566		err = register_netdev(netdev);
3567		if (err)
3568			goto err_register;
3569	}
3570
3571	adapter->netdev_registered = true;
3572
3573	netif_tx_stop_all_queues(netdev);
3574	if (CLIENT_ALLOWED(adapter)) {
3575		err = i40evf_lan_add_device(adapter);
3576		if (err)
3577			dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
3578				 err);
3579	}
3580
3581	dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
3582	if (netdev->features & NETIF_F_GRO)
3583		dev_info(&pdev->dev, "GRO is enabled\n");
3584
3585	adapter->state = __I40EVF_DOWN;
3586	set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
3587	i40evf_misc_irq_enable(adapter);
3588	wake_up(&adapter->down_waitqueue);
3589
3590	adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
3591	adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
3592	if (!adapter->rss_key || !adapter->rss_lut)
3593		goto err_mem;
3594
3595	if (RSS_AQ(adapter)) {
3596		adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
3597		mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
3598	} else {
3599		i40evf_init_rss(adapter);
3600	}
3601	return;
3602restart:
3603	schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
3604	return;
3605err_mem:
3606	i40evf_free_rss(adapter);
3607err_register:
3608	i40evf_free_misc_irq(adapter);
3609err_sw_init:
3610	i40evf_reset_interrupt_capability(adapter);
3611err_alloc:
3612	kfree(adapter->vf_res);
3613	adapter->vf_res = NULL;
3614err:
3615	/* Things went into the weeds, so try again later */
3616	if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
3617		dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
3618		adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
3619		i40evf_shutdown_adminq(hw);
3620		adapter->state = __I40EVF_STARTUP;
3621		schedule_delayed_work(&adapter->init_task, HZ * 5);
3622		return;
3623	}
3624	schedule_delayed_work(&adapter->init_task, HZ);
3625}
3626
3627/**
3628 * i40evf_shutdown - Shutdown the device in preparation for a reboot
3629 * @pdev: pci device structure
3630 **/
3631static void i40evf_shutdown(struct pci_dev *pdev)
3632{
3633	struct net_device *netdev = pci_get_drvdata(pdev);
3634	struct i40evf_adapter *adapter = netdev_priv(netdev);
3635
3636	netif_device_detach(netdev);
3637
3638	if (netif_running(netdev))
3639		i40evf_close(netdev);
3640
3641	/* Prevent the watchdog from running. */
3642	adapter->state = __I40EVF_REMOVE;
3643	adapter->aq_required = 0;
3644
3645#ifdef CONFIG_PM
3646	pci_save_state(pdev);
3647
3648#endif
3649	pci_disable_device(pdev);
3650}
3651
3652/**
3653 * i40evf_probe - Device Initialization Routine
3654 * @pdev: PCI device information struct
3655 * @ent: entry in i40evf_pci_tbl
3656 *
3657 * Returns 0 on success, negative on failure
3658 *
3659 * i40evf_probe initializes an adapter identified by a pci_dev structure.
3660 * The OS initialization, configuring of the adapter private structure,
3661 * and a hardware reset occur.
3662 **/
3663static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3664{
3665	struct net_device *netdev;
3666	struct i40evf_adapter *adapter = NULL;
3667	struct i40e_hw *hw = NULL;
3668	int err;
3669
3670	err = pci_enable_device(pdev);
3671	if (err)
3672		return err;
3673
3674	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3675	if (err) {
3676		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3677		if (err) {
3678			dev_err(&pdev->dev,
3679				"DMA configuration failed: 0x%x\n", err);
3680			goto err_dma;
3681		}
3682	}
3683
3684	err = pci_request_regions(pdev, i40evf_driver_name);
3685	if (err) {
3686		dev_err(&pdev->dev,
3687			"pci_request_regions failed 0x%x\n", err);
3688		goto err_pci_reg;
3689	}
3690
3691	pci_enable_pcie_error_reporting(pdev);
3692
3693	pci_set_master(pdev);
3694
3695	netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
3696	if (!netdev) {
3697		err = -ENOMEM;
3698		goto err_alloc_etherdev;
3699	}
3700
3701	SET_NETDEV_DEV(netdev, &pdev->dev);
3702
3703	pci_set_drvdata(pdev, netdev);
3704	adapter = netdev_priv(netdev);
3705
3706	adapter->netdev = netdev;
3707	adapter->pdev = pdev;
3708
3709	hw = &adapter->hw;
3710	hw->back = adapter;
3711
3712	adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3713	adapter->state = __I40EVF_STARTUP;
3714
3715	/* Call save state here because it relies on the adapter struct. */
3716	pci_save_state(pdev);
3717
3718	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3719			      pci_resource_len(pdev, 0));
3720	if (!hw->hw_addr) {
3721		err = -EIO;
3722		goto err_ioremap;
3723	}
3724	hw->vendor_id = pdev->vendor;
3725	hw->device_id = pdev->device;
3726	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3727	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3728	hw->subsystem_device_id = pdev->subsystem_device;
3729	hw->bus.device = PCI_SLOT(pdev->devfn);
3730	hw->bus.func = PCI_FUNC(pdev->devfn);
3731	hw->bus.bus_id = pdev->bus->number;
3732
3733	/* set up the locks for the AQ, do this only once in probe
3734	 * and destroy them only once in remove
3735	 */
3736	mutex_init(&hw->aq.asq_mutex);
3737	mutex_init(&hw->aq.arq_mutex);
3738
3739	spin_lock_init(&adapter->mac_vlan_list_lock);
3740	spin_lock_init(&adapter->cloud_filter_list_lock);
3741
3742	INIT_LIST_HEAD(&adapter->mac_filter_list);
3743	INIT_LIST_HEAD(&adapter->vlan_filter_list);
3744	INIT_LIST_HEAD(&adapter->cloud_filter_list);
3745
3746	INIT_WORK(&adapter->reset_task, i40evf_reset_task);
3747	INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
3748	INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
3749	INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
3750	INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
3751	schedule_delayed_work(&adapter->init_task,
3752			      msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3753
3754	/* Setup the wait queue for indicating transition to down status */
3755	init_waitqueue_head(&adapter->down_waitqueue);
3756
3757	return 0;
3758
3759err_ioremap:
3760	free_netdev(netdev);
3761err_alloc_etherdev:
3762	pci_release_regions(pdev);
3763err_pci_reg:
3764err_dma:
3765	pci_disable_device(pdev);
3766	return err;
3767}
3768
3769#ifdef CONFIG_PM
3770/**
3771 * i40evf_suspend - Power management suspend routine
3772 * @pdev: PCI device information struct
3773 * @state: unused
3774 *
3775 * Called when the system (VM) is entering sleep/suspend.
3776 **/
3777static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
3778{
3779	struct net_device *netdev = pci_get_drvdata(pdev);
3780	struct i40evf_adapter *adapter = netdev_priv(netdev);
3781	int retval = 0;
3782
3783	netif_device_detach(netdev);
3784
3785	while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3786				&adapter->crit_section))
3787		usleep_range(500, 1000);
3788
3789	if (netif_running(netdev)) {
3790		rtnl_lock();
3791		i40evf_down(adapter);
3792		rtnl_unlock();
3793	}
3794	i40evf_free_misc_irq(adapter);
3795	i40evf_reset_interrupt_capability(adapter);
3796
3797	clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3798
3799	retval = pci_save_state(pdev);
3800	if (retval)
3801		return retval;
3802
3803	pci_disable_device(pdev);
3804
3805	return 0;
3806}
3807
3808/**
3809 * i40evf_resume - Power management resume routine
3810 * @pdev: PCI device information struct
3811 *
3812 * Called when the system (VM) is resumed from sleep/suspend.
3813 **/
3814static int i40evf_resume(struct pci_dev *pdev)
3815{
3816	struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3817	struct net_device *netdev = adapter->netdev;
3818	u32 err;
3819
3820	pci_set_power_state(pdev, PCI_D0);
3821	pci_restore_state(pdev);
3822	/* pci_restore_state clears dev->state_saved so call
3823	 * pci_save_state to restore it.
3824	 */
3825	pci_save_state(pdev);
3826
3827	err = pci_enable_device_mem(pdev);
3828	if (err) {
3829		dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3830		return err;
3831	}
3832	pci_set_master(pdev);
3833
3834	rtnl_lock();
3835	err = i40evf_set_interrupt_capability(adapter);
3836	if (err) {
3837		rtnl_unlock();
3838		dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3839		return err;
3840	}
3841	err = i40evf_request_misc_irq(adapter);
3842	rtnl_unlock();
3843	if (err) {
3844		dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3845		return err;
3846	}
3847
3848	schedule_work(&adapter->reset_task);
3849
3850	netif_device_attach(netdev);
3851
3852	return err;
3853}
3854
3855#endif /* CONFIG_PM */
3856/**
3857 * i40evf_remove - Device Removal Routine
3858 * @pdev: PCI device information struct
3859 *
3860 * i40evf_remove is called by the PCI subsystem to alert the driver
3861 * that it should release a PCI device.  The could be caused by a
3862 * Hot-Plug event, or because the driver is going to be removed from
3863 * memory.
3864 **/
3865static void i40evf_remove(struct pci_dev *pdev)
3866{
3867	struct net_device *netdev = pci_get_drvdata(pdev);
3868	struct i40evf_adapter *adapter = netdev_priv(netdev);
3869	struct i40evf_vlan_filter *vlf, *vlftmp;
3870	struct i40evf_mac_filter *f, *ftmp;
3871	struct i40evf_cloud_filter *cf, *cftmp;
3872	struct i40e_hw *hw = &adapter->hw;
3873	int err;
3874	/* Indicate we are in remove and not to run reset_task */
3875	set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
3876	cancel_delayed_work_sync(&adapter->init_task);
3877	cancel_work_sync(&adapter->reset_task);
3878	cancel_delayed_work_sync(&adapter->client_task);
3879	if (adapter->netdev_registered) {
3880		unregister_netdev(netdev);
3881		adapter->netdev_registered = false;
3882	}
3883	if (CLIENT_ALLOWED(adapter)) {
3884		err = i40evf_lan_del_device(adapter);
3885		if (err)
3886			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3887				 err);
3888	}
3889
3890	/* Shut down all the garbage mashers on the detention level */
3891	adapter->state = __I40EVF_REMOVE;
3892	adapter->aq_required = 0;
3893	adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
3894	i40evf_request_reset(adapter);
3895	msleep(50);
3896	/* If the FW isn't responding, kick it once, but only once. */
3897	if (!i40evf_asq_done(hw)) {
3898		i40evf_request_reset(adapter);
3899		msleep(50);
3900	}
3901	i40evf_free_all_tx_resources(adapter);
3902	i40evf_free_all_rx_resources(adapter);
3903	i40evf_misc_irq_disable(adapter);
3904	i40evf_free_misc_irq(adapter);
3905	i40evf_reset_interrupt_capability(adapter);
3906	i40evf_free_q_vectors(adapter);
3907
3908	if (adapter->watchdog_timer.function)
3909		del_timer_sync(&adapter->watchdog_timer);
3910
3911	i40evf_free_rss(adapter);
3912
3913	if (hw->aq.asq.count)
3914		i40evf_shutdown_adminq(hw);
3915
3916	/* destroy the locks only once, here */
3917	mutex_destroy(&hw->aq.arq_mutex);
3918	mutex_destroy(&hw->aq.asq_mutex);
3919
3920	iounmap(hw->hw_addr);
3921	pci_release_regions(pdev);
3922	i40evf_free_all_tx_resources(adapter);
3923	i40evf_free_all_rx_resources(adapter);
3924	i40evf_free_queues(adapter);
3925	kfree(adapter->vf_res);
3926	spin_lock_bh(&adapter->mac_vlan_list_lock);
3927	/* If we got removed before an up/down sequence, we've got a filter
3928	 * hanging out there that we need to get rid of.
3929	 */
3930	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3931		list_del(&f->list);
3932		kfree(f);
3933	}
3934	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3935				 list) {
3936		list_del(&vlf->list);
3937		kfree(vlf);
3938	}
3939
3940	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3941
3942	spin_lock_bh(&adapter->cloud_filter_list_lock);
3943	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3944		list_del(&cf->list);
3945		kfree(cf);
3946	}
3947	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3948
3949	free_netdev(netdev);
3950
3951	pci_disable_pcie_error_reporting(pdev);
3952
3953	pci_disable_device(pdev);
3954}
3955
3956static struct pci_driver i40evf_driver = {
3957	.name     = i40evf_driver_name,
3958	.id_table = i40evf_pci_tbl,
3959	.probe    = i40evf_probe,
3960	.remove   = i40evf_remove,
3961#ifdef CONFIG_PM
3962	.suspend  = i40evf_suspend,
3963	.resume   = i40evf_resume,
3964#endif
3965	.shutdown = i40evf_shutdown,
3966};
3967
3968/**
3969 * i40e_init_module - Driver Registration Routine
3970 *
3971 * i40e_init_module is the first routine called when the driver is
3972 * loaded. All it does is register with the PCI subsystem.
3973 **/
3974static int __init i40evf_init_module(void)
3975{
3976	int ret;
3977
3978	pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
3979		i40evf_driver_version);
3980
3981	pr_info("%s\n", i40evf_copyright);
3982
3983	i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3984				    i40evf_driver_name);
3985	if (!i40evf_wq) {
3986		pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3987		return -ENOMEM;
3988	}
3989	ret = pci_register_driver(&i40evf_driver);
3990	return ret;
3991}
3992
3993module_init(i40evf_init_module);
3994
3995/**
3996 * i40e_exit_module - Driver Exit Cleanup Routine
3997 *
3998 * i40e_exit_module is called just before the driver is removed
3999 * from memory.
4000 **/
4001static void __exit i40evf_exit_module(void)
4002{
4003	pci_unregister_driver(&i40evf_driver);
4004	destroy_workqueue(i40evf_wq);
4005}
4006
4007module_exit(i40evf_exit_module);
4008
4009/* i40evf_main.c */