File Index
Files and directories
Sign in
FILE

iavf_main.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/intel/iavf/iavf_main.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2013 - 2018 Intel Corporation. */
   3
   4#include <linux/net/intel/libie/rx.h>
   5#include <net/netdev_lock.h>
   6
   7#include "iavf.h"
   8#include "iavf_ptp.h"
   9#include "iavf_prototype.h"
  10/* All iavf tracepoints are defined by the include below, which must
  11 * be included exactly once across the whole kernel with
  12 * CREATE_TRACE_POINTS defined
  13 */
  14#define CREATE_TRACE_POINTS
  15#include "iavf_trace.h"
  16
  17static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter);
  18static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter);
  19static int iavf_close(struct net_device *netdev);
  20static void iavf_init_get_resources(struct iavf_adapter *adapter);
  21static int iavf_check_reset_complete(struct iavf_hw *hw);
  22
  23char iavf_driver_name[] = "iavf";
  24static const char iavf_driver_string[] =
  25	"Intel(R) Ethernet Adaptive Virtual Function Network Driver";
  26
  27static const char iavf_copyright[] =
  28	"Copyright (c) 2013 - 2018 Intel Corporation.";
  29
  30/* iavf_pci_tbl - PCI Device ID Table
  31 *
  32 * Wildcard entries (PCI_ANY_ID) should come last
  33 * Last entry must be all 0s
  34 *
  35 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  36 *   Class, Class Mask, private data (not used) }
  37 */
  38static const struct pci_device_id iavf_pci_tbl[] = {
  39	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF), 0},
  40	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF_HV), 0},
  41	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_X722_VF), 0},
  42	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_ADAPTIVE_VF), 0},
  43	/* required last entry */
  44	{0, }
  45};
  46
  47MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
  48
  49MODULE_ALIAS("i40evf");
  50MODULE_DESCRIPTION("Intel(R) Ethernet Adaptive Virtual Function Network Driver");
  51MODULE_IMPORT_NS("LIBETH");
  52MODULE_IMPORT_NS("LIBIE");
  53MODULE_IMPORT_NS("LIBIE_ADMINQ");
  54MODULE_LICENSE("GPL v2");
  55
  56static const struct net_device_ops iavf_netdev_ops;
  57
  58int iavf_status_to_errno(enum iavf_status status)
  59{
  60	switch (status) {
  61	case IAVF_SUCCESS:
  62		return 0;
  63	case IAVF_ERR_PARAM:
  64	case IAVF_ERR_MAC_TYPE:
  65	case IAVF_ERR_INVALID_MAC_ADDR:
  66	case IAVF_ERR_INVALID_LINK_SETTINGS:
  67	case IAVF_ERR_INVALID_PD_ID:
  68	case IAVF_ERR_INVALID_QP_ID:
  69	case IAVF_ERR_INVALID_CQ_ID:
  70	case IAVF_ERR_INVALID_CEQ_ID:
  71	case IAVF_ERR_INVALID_AEQ_ID:
  72	case IAVF_ERR_INVALID_SIZE:
  73	case IAVF_ERR_INVALID_ARP_INDEX:
  74	case IAVF_ERR_INVALID_FPM_FUNC_ID:
  75	case IAVF_ERR_QP_INVALID_MSG_SIZE:
  76	case IAVF_ERR_INVALID_FRAG_COUNT:
  77	case IAVF_ERR_INVALID_ALIGNMENT:
  78	case IAVF_ERR_INVALID_PUSH_PAGE_INDEX:
  79	case IAVF_ERR_INVALID_IMM_DATA_SIZE:
  80	case IAVF_ERR_INVALID_VF_ID:
  81	case IAVF_ERR_INVALID_HMCFN_ID:
  82	case IAVF_ERR_INVALID_PBLE_INDEX:
  83	case IAVF_ERR_INVALID_SD_INDEX:
  84	case IAVF_ERR_INVALID_PAGE_DESC_INDEX:
  85	case IAVF_ERR_INVALID_SD_TYPE:
  86	case IAVF_ERR_INVALID_HMC_OBJ_INDEX:
  87	case IAVF_ERR_INVALID_HMC_OBJ_COUNT:
  88	case IAVF_ERR_INVALID_SRQ_ARM_LIMIT:
  89		return -EINVAL;
  90	case IAVF_ERR_NVM:
  91	case IAVF_ERR_NVM_CHECKSUM:
  92	case IAVF_ERR_PHY:
  93	case IAVF_ERR_CONFIG:
  94	case IAVF_ERR_UNKNOWN_PHY:
  95	case IAVF_ERR_LINK_SETUP:
  96	case IAVF_ERR_ADAPTER_STOPPED:
  97	case IAVF_ERR_PRIMARY_REQUESTS_PENDING:
  98	case IAVF_ERR_AUTONEG_NOT_COMPLETE:
  99	case IAVF_ERR_RESET_FAILED:
 100	case IAVF_ERR_BAD_PTR:
 101	case IAVF_ERR_SWFW_SYNC:
 102	case IAVF_ERR_QP_TOOMANY_WRS_POSTED:
 103	case IAVF_ERR_QUEUE_EMPTY:
 104	case IAVF_ERR_FLUSHED_QUEUE:
 105	case IAVF_ERR_OPCODE_MISMATCH:
 106	case IAVF_ERR_CQP_COMPL_ERROR:
 107	case IAVF_ERR_BACKING_PAGE_ERROR:
 108	case IAVF_ERR_NO_PBLCHUNKS_AVAILABLE:
 109	case IAVF_ERR_MEMCPY_FAILED:
 110	case IAVF_ERR_SRQ_ENABLED:
 111	case IAVF_ERR_ADMIN_QUEUE_ERROR:
 112	case IAVF_ERR_ADMIN_QUEUE_FULL:
 113	case IAVF_ERR_BAD_RDMA_CQE:
 114	case IAVF_ERR_NVM_BLANK_MODE:
 115	case IAVF_ERR_PE_DOORBELL_NOT_ENABLED:
 116	case IAVF_ERR_DIAG_TEST_FAILED:
 117	case IAVF_ERR_FIRMWARE_API_VERSION:
 118	case IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR:
 119		return -EIO;
 120	case IAVF_ERR_DEVICE_NOT_SUPPORTED:
 121		return -ENODEV;
 122	case IAVF_ERR_NO_AVAILABLE_VSI:
 123	case IAVF_ERR_RING_FULL:
 124		return -ENOSPC;
 125	case IAVF_ERR_NO_MEMORY:
 126		return -ENOMEM;
 127	case IAVF_ERR_TIMEOUT:
 128	case IAVF_ERR_ADMIN_QUEUE_TIMEOUT:
 129		return -ETIMEDOUT;
 130	case IAVF_ERR_NOT_IMPLEMENTED:
 131	case IAVF_NOT_SUPPORTED:
 132		return -EOPNOTSUPP;
 133	case IAVF_ERR_ADMIN_QUEUE_NO_WORK:
 134		return -EALREADY;
 135	case IAVF_ERR_NOT_READY:
 136		return -EBUSY;
 137	case IAVF_ERR_BUF_TOO_SHORT:
 138		return -EMSGSIZE;
 139	}
 140
 141	return -EIO;
 142}
 143
 144int virtchnl_status_to_errno(enum virtchnl_status_code v_status)
 145{
 146	switch (v_status) {
 147	case VIRTCHNL_STATUS_SUCCESS:
 148		return 0;
 149	case VIRTCHNL_STATUS_ERR_PARAM:
 150	case VIRTCHNL_STATUS_ERR_INVALID_VF_ID:
 151		return -EINVAL;
 152	case VIRTCHNL_STATUS_ERR_NO_MEMORY:
 153		return -ENOMEM;
 154	case VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH:
 155	case VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR:
 156	case VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR:
 157		return -EIO;
 158	case VIRTCHNL_STATUS_ERR_NOT_SUPPORTED:
 159		return -EOPNOTSUPP;
 160	}
 161
 162	return -EIO;
 163}
 164
 165/**
 166 * iavf_pdev_to_adapter - go from pci_dev to adapter
 167 * @pdev: pci_dev pointer
 168 */
 169static struct iavf_adapter *iavf_pdev_to_adapter(struct pci_dev *pdev)
 170{
 171	return netdev_priv(pci_get_drvdata(pdev));
 172}
 173
 174/**
 175 * iavf_is_reset_in_progress - Check if a reset is in progress
 176 * @adapter: board private structure
 177 */
 178static bool iavf_is_reset_in_progress(struct iavf_adapter *adapter)
 179{
 180	if (adapter->state == __IAVF_RESETTING ||
 181	    adapter->flags & (IAVF_FLAG_RESET_PENDING |
 182			      IAVF_FLAG_RESET_NEEDED))
 183		return true;
 184
 185	return false;
 186}
 187
 188/**
 189 * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
 190 * @hw:   pointer to the HW structure
 191 * @mem:  ptr to mem struct to fill out
 192 * @size: size of memory requested
 193 * @alignment: what to align the allocation to
 194 **/
 195enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
 196					 struct iavf_dma_mem *mem,
 197					 u64 size, u32 alignment)
 198{
 199	struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
 200
 201	if (!mem)
 202		return IAVF_ERR_PARAM;
 203
 204	mem->size = ALIGN(size, alignment);
 205	mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
 206				     (dma_addr_t *)&mem->pa, GFP_KERNEL);
 207	if (mem->va)
 208		return 0;
 209	else
 210		return IAVF_ERR_NO_MEMORY;
 211}
 212
 213/**
 214 * iavf_free_dma_mem - wrapper for DMA memory freeing
 215 * @hw:   pointer to the HW structure
 216 * @mem:  ptr to mem struct to free
 217 **/
 218enum iavf_status iavf_free_dma_mem(struct iavf_hw *hw, struct iavf_dma_mem *mem)
 219{
 220	struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
 221
 222	if (!mem || !mem->va)
 223		return IAVF_ERR_PARAM;
 224	dma_free_coherent(&adapter->pdev->dev, mem->size,
 225			  mem->va, (dma_addr_t)mem->pa);
 226	return 0;
 227}
 228
 229/**
 230 * iavf_allocate_virt_mem - virt memory alloc wrapper
 231 * @hw:   pointer to the HW structure
 232 * @mem:  ptr to mem struct to fill out
 233 * @size: size of memory requested
 234 **/
 235enum iavf_status iavf_allocate_virt_mem(struct iavf_hw *hw,
 236					struct iavf_virt_mem *mem, u32 size)
 237{
 238	if (!mem)
 239		return IAVF_ERR_PARAM;
 240
 241	mem->size = size;
 242	mem->va = kzalloc(size, GFP_KERNEL);
 243
 244	if (mem->va)
 245		return 0;
 246	else
 247		return IAVF_ERR_NO_MEMORY;
 248}
 249
 250/**
 251 * iavf_free_virt_mem - virt memory free wrapper
 252 * @hw:   pointer to the HW structure
 253 * @mem:  ptr to mem struct to free
 254 **/
 255void iavf_free_virt_mem(struct iavf_hw *hw, struct iavf_virt_mem *mem)
 256{
 257	kfree(mem->va);
 258}
 259
 260/**
 261 * iavf_schedule_reset - Set the flags and schedule a reset event
 262 * @adapter: board private structure
 263 * @flags: IAVF_FLAG_RESET_PENDING or IAVF_FLAG_RESET_NEEDED
 264 **/
 265void iavf_schedule_reset(struct iavf_adapter *adapter, u64 flags)
 266{
 267	if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section) &&
 268	    !(adapter->flags &
 269	    (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
 270		adapter->flags |= flags;
 271		queue_work(adapter->wq, &adapter->reset_task);
 272	}
 273}
 274
 275/**
 276 * iavf_schedule_aq_request - Set the flags and schedule aq request
 277 * @adapter: board private structure
 278 * @flags: requested aq flags
 279 **/
 280void iavf_schedule_aq_request(struct iavf_adapter *adapter, u64 flags)
 281{
 282	adapter->aq_required |= flags;
 283	mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
 284}
 285
 286/**
 287 * iavf_tx_timeout - Respond to a Tx Hang
 288 * @netdev: network interface device structure
 289 * @txqueue: queue number that is timing out
 290 **/
 291static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
 292{
 293	struct iavf_adapter *adapter = netdev_priv(netdev);
 294
 295	adapter->tx_timeout_count++;
 296	iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
 297}
 298
 299/**
 300 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
 301 * @adapter: board private structure
 302 **/
 303static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
 304{
 305	struct iavf_hw *hw = &adapter->hw;
 306
 307	if (!adapter->msix_entries)
 308		return;
 309
 310	wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
 311
 312	iavf_flush(hw);
 313
 314	synchronize_irq(adapter->msix_entries[0].vector);
 315}
 316
 317/**
 318 * iavf_misc_irq_enable - Enable default interrupt generation settings
 319 * @adapter: board private structure
 320 **/
 321static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
 322{
 323	struct iavf_hw *hw = &adapter->hw;
 324
 325	wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
 326				       IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
 327	wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
 328
 329	iavf_flush(hw);
 330}
 331
 332/**
 333 * iavf_irq_disable - Mask off interrupt generation on the NIC
 334 * @adapter: board private structure
 335 **/
 336static void iavf_irq_disable(struct iavf_adapter *adapter)
 337{
 338	int i;
 339	struct iavf_hw *hw = &adapter->hw;
 340
 341	if (!adapter->msix_entries)
 342		return;
 343
 344	for (i = 1; i < adapter->num_msix_vectors; i++) {
 345		wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
 346		synchronize_irq(adapter->msix_entries[i].vector);
 347	}
 348	iavf_flush(hw);
 349}
 350
 351/**
 352 * iavf_irq_enable_queues - Enable interrupt for all queues
 353 * @adapter: board private structure
 354 **/
 355static void iavf_irq_enable_queues(struct iavf_adapter *adapter)
 356{
 357	struct iavf_hw *hw = &adapter->hw;
 358	int i;
 359
 360	for (i = 1; i < adapter->num_msix_vectors; i++) {
 361		wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
 362		     IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
 363		     IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
 364	}
 365}
 366
 367/**
 368 * iavf_irq_enable - Enable default interrupt generation settings
 369 * @adapter: board private structure
 370 * @flush: boolean value whether to run rd32()
 371 **/
 372void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
 373{
 374	struct iavf_hw *hw = &adapter->hw;
 375
 376	iavf_misc_irq_enable(adapter);
 377	iavf_irq_enable_queues(adapter);
 378
 379	if (flush)
 380		iavf_flush(hw);
 381}
 382
 383/**
 384 * iavf_msix_aq - Interrupt handler for vector 0
 385 * @irq: interrupt number
 386 * @data: pointer to netdev
 387 **/
 388static irqreturn_t iavf_msix_aq(int irq, void *data)
 389{
 390	struct net_device *netdev = data;
 391	struct iavf_adapter *adapter = netdev_priv(netdev);
 392	struct iavf_hw *hw = &adapter->hw;
 393
 394	/* handle non-queue interrupts, these reads clear the registers */
 395	rd32(hw, IAVF_VFINT_ICR01);
 396	rd32(hw, IAVF_VFINT_ICR0_ENA1);
 397
 398	if (adapter->state != __IAVF_REMOVE)
 399		/* schedule work on the private workqueue */
 400		queue_work(adapter->wq, &adapter->adminq_task);
 401
 402	return IRQ_HANDLED;
 403}
 404
 405/**
 406 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
 407 * @irq: interrupt number
 408 * @data: pointer to a q_vector
 409 **/
 410static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
 411{
 412	struct iavf_q_vector *q_vector = data;
 413
 414	if (!q_vector->tx.ring && !q_vector->rx.ring)
 415		return IRQ_HANDLED;
 416
 417	napi_schedule_irqoff(&q_vector->napi);
 418
 419	return IRQ_HANDLED;
 420}
 421
 422/**
 423 * iavf_map_vector_to_rxq - associate irqs with rx queues
 424 * @adapter: board private structure
 425 * @v_idx: interrupt number
 426 * @r_idx: queue number
 427 **/
 428static void
 429iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
 430{
 431	struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
 432	struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
 433	struct iavf_hw *hw = &adapter->hw;
 434
 435	rx_ring->q_vector = q_vector;
 436	rx_ring->next = q_vector->rx.ring;
 437	rx_ring->vsi = &adapter->vsi;
 438	q_vector->rx.ring = rx_ring;
 439	q_vector->rx.count++;
 440	q_vector->rx.next_update = jiffies + 1;
 441	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
 442	q_vector->ring_mask |= BIT(r_idx);
 443	wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
 444	     q_vector->rx.current_itr >> 1);
 445	q_vector->rx.current_itr = q_vector->rx.target_itr;
 446}
 447
 448/**
 449 * iavf_map_vector_to_txq - associate irqs with tx queues
 450 * @adapter: board private structure
 451 * @v_idx: interrupt number
 452 * @t_idx: queue number
 453 **/
 454static void
 455iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
 456{
 457	struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
 458	struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
 459	struct iavf_hw *hw = &adapter->hw;
 460
 461	tx_ring->q_vector = q_vector;
 462	tx_ring->next = q_vector->tx.ring;
 463	tx_ring->vsi = &adapter->vsi;
 464	q_vector->tx.ring = tx_ring;
 465	q_vector->tx.count++;
 466	q_vector->tx.next_update = jiffies + 1;
 467	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
 468	q_vector->num_ringpairs++;
 469	wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
 470	     q_vector->tx.target_itr >> 1);
 471	q_vector->tx.current_itr = q_vector->tx.target_itr;
 472}
 473
 474/**
 475 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
 476 * @adapter: board private structure to initialize
 477 *
 478 * This function maps descriptor rings to the queue-specific vectors
 479 * we were allotted through the MSI-X enabling code.  Ideally, we'd have
 480 * one vector per ring/queue, but on a constrained vector budget, we
 481 * group the rings as "efficiently" as possible.  You would add new
 482 * mapping configurations in here.
 483 **/
 484static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
 485{
 486	int rings_remaining = adapter->num_active_queues;
 487	int ridx = 0, vidx = 0;
 488	int q_vectors;
 489
 490	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 491
 492	for (; ridx < rings_remaining; ridx++) {
 493		iavf_map_vector_to_rxq(adapter, vidx, ridx);
 494		iavf_map_vector_to_txq(adapter, vidx, ridx);
 495
 496		/* In the case where we have more queues than vectors, continue
 497		 * round-robin on vectors until all queues are mapped.
 498		 */
 499		if (++vidx >= q_vectors)
 500			vidx = 0;
 501	}
 502
 503	adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
 504}
 505
 506/**
 507 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
 508 * @adapter: board private structure
 509 * @basename: device basename
 510 *
 511 * Allocates MSI-X vectors for tx and rx handling, and requests
 512 * interrupts from the kernel.
 513 **/
 514static int
 515iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
 516{
 517	unsigned int vector, q_vectors;
 518	unsigned int rx_int_idx = 0, tx_int_idx = 0;
 519	int irq_num, err;
 520
 521	iavf_irq_disable(adapter);
 522	/* Decrement for Other and TCP Timer vectors */
 523	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 524
 525	for (vector = 0; vector < q_vectors; vector++) {
 526		struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
 527
 528		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 529
 530		if (q_vector->tx.ring && q_vector->rx.ring) {
 531			snprintf(q_vector->name, sizeof(q_vector->name),
 532				 "iavf-%s-TxRx-%u", basename, rx_int_idx++);
 533			tx_int_idx++;
 534		} else if (q_vector->rx.ring) {
 535			snprintf(q_vector->name, sizeof(q_vector->name),
 536				 "iavf-%s-rx-%u", basename, rx_int_idx++);
 537		} else if (q_vector->tx.ring) {
 538			snprintf(q_vector->name, sizeof(q_vector->name),
 539				 "iavf-%s-tx-%u", basename, tx_int_idx++);
 540		} else {
 541			/* skip this unused q_vector */
 542			continue;
 543		}
 544		err = request_irq(irq_num,
 545				  iavf_msix_clean_rings,
 546				  0,
 547				  q_vector->name,
 548				  q_vector);
 549		if (err) {
 550			dev_info(&adapter->pdev->dev,
 551				 "Request_irq failed, error: %d\n", err);
 552			goto free_queue_irqs;
 553		}
 554	}
 555
 556	return 0;
 557
 558free_queue_irqs:
 559	while (vector) {
 560		vector--;
 561		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 562		free_irq(irq_num, &adapter->q_vectors[vector]);
 563	}
 564	return err;
 565}
 566
 567/**
 568 * iavf_request_misc_irq - Initialize MSI-X interrupts
 569 * @adapter: board private structure
 570 *
 571 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
 572 * vector is only for the admin queue, and stays active even when the netdev
 573 * is closed.
 574 **/
 575static int iavf_request_misc_irq(struct iavf_adapter *adapter)
 576{
 577	struct net_device *netdev = adapter->netdev;
 578	int err;
 579
 580	snprintf(adapter->misc_vector_name,
 581		 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
 582		 dev_name(&adapter->pdev->dev));
 583	err = request_irq(adapter->msix_entries[0].vector,
 584			  &iavf_msix_aq, 0,
 585			  adapter->misc_vector_name, netdev);
 586	if (err) {
 587		dev_err(&adapter->pdev->dev,
 588			"request_irq for %s failed: %d\n",
 589			adapter->misc_vector_name, err);
 590		free_irq(adapter->msix_entries[0].vector, netdev);
 591	}
 592	return err;
 593}
 594
 595/**
 596 * iavf_free_traffic_irqs - Free MSI-X interrupts
 597 * @adapter: board private structure
 598 *
 599 * Frees all MSI-X vectors other than 0.
 600 **/
 601static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
 602{
 603	struct iavf_q_vector *q_vector;
 604	int vector, irq_num, q_vectors;
 605
 606	if (!adapter->msix_entries)
 607		return;
 608
 609	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 610
 611	for (vector = 0; vector < q_vectors; vector++) {
 612		q_vector = &adapter->q_vectors[vector];
 613		netif_napi_set_irq_locked(&q_vector->napi, -1);
 614		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
 615		free_irq(irq_num, q_vector);
 616	}
 617}
 618
 619/**
 620 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
 621 * @adapter: board private structure
 622 *
 623 * Frees MSI-X vector 0.
 624 **/
 625static void iavf_free_misc_irq(struct iavf_adapter *adapter)
 626{
 627	struct net_device *netdev = adapter->netdev;
 628
 629	if (!adapter->msix_entries)
 630		return;
 631
 632	free_irq(adapter->msix_entries[0].vector, netdev);
 633}
 634
 635/**
 636 * iavf_configure_tx - Configure Transmit Unit after Reset
 637 * @adapter: board private structure
 638 *
 639 * Configure the Tx unit of the MAC after a reset.
 640 **/
 641static void iavf_configure_tx(struct iavf_adapter *adapter)
 642{
 643	struct iavf_hw *hw = &adapter->hw;
 644	int i;
 645
 646	for (i = 0; i < adapter->num_active_queues; i++)
 647		adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
 648}
 649
 650/**
 651 * iavf_select_rx_desc_format - Select Rx descriptor format
 652 * @adapter: adapter private structure
 653 *
 654 * Select what Rx descriptor format based on availability and enabled
 655 * features.
 656 *
 657 * Return: the desired RXDID to select for a given Rx queue, as defined by
 658 *         enum virtchnl_rxdid_format.
 659 */
 660static u8 iavf_select_rx_desc_format(const struct iavf_adapter *adapter)
 661{
 662	u64 rxdids = adapter->supp_rxdids;
 663
 664	/* If we did not negotiate VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC, we must
 665	 * stick with the default value of the legacy 32 byte format.
 666	 */
 667	if (!IAVF_RXDID_ALLOWED(adapter))
 668		return VIRTCHNL_RXDID_1_32B_BASE;
 669
 670	/* Rx timestamping requires the use of flexible NIC descriptors */
 671	if (iavf_ptp_cap_supported(adapter, VIRTCHNL_1588_PTP_CAP_RX_TSTAMP)) {
 672		if (rxdids & BIT(VIRTCHNL_RXDID_2_FLEX_SQ_NIC))
 673			return VIRTCHNL_RXDID_2_FLEX_SQ_NIC;
 674
 675		pci_warn(adapter->pdev,
 676			 "Unable to negotiate flexible descriptor format\n");
 677	}
 678
 679	/* Warn if the PF does not list support for the default legacy
 680	 * descriptor format. This shouldn't happen, as this is the format
 681	 * used if VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC is not supported. It is
 682	 * likely caused by a bug in the PF implementation failing to indicate
 683	 * support for the format.
 684	 */
 685	if (!(rxdids & VIRTCHNL_RXDID_1_32B_BASE_M))
 686		netdev_warn(adapter->netdev, "PF does not list support for default Rx descriptor format\n");
 687
 688	return VIRTCHNL_RXDID_1_32B_BASE;
 689}
 690
 691/**
 692 * iavf_configure_rx - Configure Receive Unit after Reset
 693 * @adapter: board private structure
 694 *
 695 * Configure the Rx unit of the MAC after a reset.
 696 **/
 697static void iavf_configure_rx(struct iavf_adapter *adapter)
 698{
 699	struct iavf_hw *hw = &adapter->hw;
 700
 701	adapter->rxdid = iavf_select_rx_desc_format(adapter);
 702
 703	for (u32 i = 0; i < adapter->num_active_queues; i++) {
 704		adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
 705		adapter->rx_rings[i].rxdid = adapter->rxdid;
 706	}
 707}
 708
 709/**
 710 * iavf_find_vlan - Search filter list for specific vlan filter
 711 * @adapter: board private structure
 712 * @vlan: vlan tag
 713 *
 714 * Returns ptr to the filter object or NULL. Must be called while holding the
 715 * mac_vlan_list_lock.
 716 **/
 717static struct
 718iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter,
 719				 struct iavf_vlan vlan)
 720{
 721	struct iavf_vlan_filter *f;
 722
 723	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 724		if (f->vlan.vid == vlan.vid &&
 725		    f->vlan.tpid == vlan.tpid)
 726			return f;
 727	}
 728
 729	return NULL;
 730}
 731
 732/**
 733 * iavf_add_vlan - Add a vlan filter to the list
 734 * @adapter: board private structure
 735 * @vlan: VLAN tag
 736 *
 737 * Returns ptr to the filter object or NULL when no memory available.
 738 **/
 739static struct
 740iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter,
 741				struct iavf_vlan vlan)
 742{
 743	struct iavf_vlan_filter *f = NULL;
 744
 745	spin_lock_bh(&adapter->mac_vlan_list_lock);
 746
 747	f = iavf_find_vlan(adapter, vlan);
 748	if (!f) {
 749		f = kzalloc(sizeof(*f), GFP_ATOMIC);
 750		if (!f)
 751			goto clearout;
 752
 753		f->vlan = vlan;
 754
 755		list_add_tail(&f->list, &adapter->vlan_filter_list);
 756		f->state = IAVF_VLAN_ADD;
 757		adapter->num_vlan_filters++;
 758		iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_VLAN_FILTER);
 759	} else if (f->state == IAVF_VLAN_REMOVE) {
 760		/* Re-add the filter since we cannot tell whether the
 761		 * pending delete has already been processed by the PF.
 762		 * A duplicate add is harmless.
 763		 */
 764		f->state = IAVF_VLAN_ADD;
 765		iavf_schedule_aq_request(adapter,
 766					 IAVF_FLAG_AQ_ADD_VLAN_FILTER);
 767	}
 768
 769clearout:
 770	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 771	return f;
 772}
 773
 774/**
 775 * iavf_del_vlan - Remove a vlan filter from the list
 776 * @adapter: board private structure
 777 * @vlan: VLAN tag
 778 **/
 779static void iavf_del_vlan(struct iavf_adapter *adapter, struct iavf_vlan vlan)
 780{
 781	struct iavf_vlan_filter *f;
 782
 783	spin_lock_bh(&adapter->mac_vlan_list_lock);
 784
 785	f = iavf_find_vlan(adapter, vlan);
 786	if (f) {
 787		/* IAVF_ADD_VLAN means that VLAN wasn't even added yet.
 788		 * Remove it from the list.
 789		 */
 790		if (f->state == IAVF_VLAN_ADD) {
 791			list_del(&f->list);
 792			kfree(f);
 793			adapter->num_vlan_filters--;
 794		} else {
 795			f->state = IAVF_VLAN_REMOVE;
 796			iavf_schedule_aq_request(adapter,
 797						 IAVF_FLAG_AQ_DEL_VLAN_FILTER);
 798		}
 799	}
 800
 801	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 802}
 803
 804/**
 805 * iavf_restore_filters
 806 * @adapter: board private structure
 807 *
 808 * Restore existing non MAC filters when VF netdev comes back up
 809 **/
 810static void iavf_restore_filters(struct iavf_adapter *adapter)
 811{
 812	struct iavf_vlan_filter *f;
 813
 814	/* re-add all VLAN filters */
 815	spin_lock_bh(&adapter->mac_vlan_list_lock);
 816
 817	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 818		if (f->state == IAVF_VLAN_INACTIVE)
 819			f->state = IAVF_VLAN_ADD;
 820	}
 821
 822	spin_unlock_bh(&adapter->mac_vlan_list_lock);
 823	adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
 824}
 825
 826/**
 827 * iavf_get_num_vlans_added - get number of VLANs added
 828 * @adapter: board private structure
 829 */
 830u16 iavf_get_num_vlans_added(struct iavf_adapter *adapter)
 831{
 832	return adapter->num_vlan_filters;
 833}
 834
 835/**
 836 * iavf_get_max_vlans_allowed - get maximum VLANs allowed for this VF
 837 * @adapter: board private structure
 838 *
 839 * This depends on the negotiated VLAN capability. For VIRTCHNL_VF_OFFLOAD_VLAN,
 840 * do not impose a limit as that maintains current behavior and for
 841 * VIRTCHNL_VF_OFFLOAD_VLAN_V2, use the maximum allowed sent from the PF.
 842 **/
 843static u16 iavf_get_max_vlans_allowed(struct iavf_adapter *adapter)
 844{
 845	/* don't impose any limit for VIRTCHNL_VF_OFFLOAD_VLAN since there has
 846	 * never been a limit on the VF driver side
 847	 */
 848	if (VLAN_ALLOWED(adapter))
 849		return VLAN_N_VID;
 850	else if (VLAN_V2_ALLOWED(adapter))
 851		return adapter->vlan_v2_caps.filtering.max_filters;
 852
 853	return 0;
 854}
 855
 856/**
 857 * iavf_max_vlans_added - check if maximum VLANs allowed already exist
 858 * @adapter: board private structure
 859 **/
 860static bool iavf_max_vlans_added(struct iavf_adapter *adapter)
 861{
 862	if (iavf_get_num_vlans_added(adapter) <
 863	    iavf_get_max_vlans_allowed(adapter))
 864		return false;
 865
 866	return true;
 867}
 868
 869/**
 870 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
 871 * @netdev: network device struct
 872 * @proto: unused protocol data
 873 * @vid: VLAN tag
 874 **/
 875static int iavf_vlan_rx_add_vid(struct net_device *netdev,
 876				__always_unused __be16 proto, u16 vid)
 877{
 878	struct iavf_adapter *adapter = netdev_priv(netdev);
 879
 880	/* Do not track VLAN 0 filter, always added by the PF on VF init */
 881	if (!vid)
 882		return 0;
 883
 884	if (!VLAN_FILTERING_ALLOWED(adapter))
 885		return -EIO;
 886
 887	if (iavf_max_vlans_added(adapter)) {
 888		netdev_err(netdev, "Max allowed VLAN filters %u. Remove existing VLANs or disable filtering via Ethtool if supported.\n",
 889			   iavf_get_max_vlans_allowed(adapter));
 890		return -EIO;
 891	}
 892
 893	if (!iavf_add_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto))))
 894		return -ENOMEM;
 895
 896	return 0;
 897}
 898
 899/**
 900 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
 901 * @netdev: network device struct
 902 * @proto: unused protocol data
 903 * @vid: VLAN tag
 904 **/
 905static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
 906				 __always_unused __be16 proto, u16 vid)
 907{
 908	struct iavf_adapter *adapter = netdev_priv(netdev);
 909
 910	/* We do not track VLAN 0 filter */
 911	if (!vid)
 912		return 0;
 913
 914	iavf_del_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto)));
 915	return 0;
 916}
 917
 918/**
 919 * iavf_find_filter - Search filter list for specific mac filter
 920 * @adapter: board private structure
 921 * @macaddr: the MAC address
 922 *
 923 * Returns ptr to the filter object or NULL. Must be called while holding the
 924 * mac_vlan_list_lock.
 925 **/
 926static struct
 927iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
 928				  const u8 *macaddr)
 929{
 930	struct iavf_mac_filter *f;
 931
 932	if (!macaddr)
 933		return NULL;
 934
 935	list_for_each_entry(f, &adapter->mac_filter_list, list) {
 936		if (ether_addr_equal(macaddr, f->macaddr))
 937			return f;
 938	}
 939	return NULL;
 940}
 941
 942/**
 943 * iavf_add_filter - Add a mac filter to the filter list
 944 * @adapter: board private structure
 945 * @macaddr: the MAC address
 946 *
 947 * Returns ptr to the filter object or NULL when no memory available.
 948 **/
 949struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
 950					const u8 *macaddr)
 951{
 952	struct iavf_mac_filter *f;
 953
 954	if (!macaddr)
 955		return NULL;
 956
 957	f = iavf_find_filter(adapter, macaddr);
 958	if (!f) {
 959		f = kzalloc(sizeof(*f), GFP_ATOMIC);
 960		if (!f)
 961			return f;
 962
 963		ether_addr_copy(f->macaddr, macaddr);
 964
 965		list_add_tail(&f->list, &adapter->mac_filter_list);
 966		f->add = true;
 967		f->add_handled = false;
 968		f->is_new_mac = true;
 969		f->is_primary = ether_addr_equal(macaddr, adapter->hw.mac.addr);
 970		adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
 971	} else {
 972		f->remove = false;
 973	}
 974
 975	return f;
 976}
 977
 978/**
 979 * iavf_replace_primary_mac - Replace current primary address
 980 * @adapter: board private structure
 981 * @new_mac: new MAC address to be applied
 982 *
 983 * Replace current dev_addr and send request to PF for removal of previous
 984 * primary MAC address filter and addition of new primary MAC filter.
 985 * Return 0 for success, -ENOMEM for failure.
 986 *
 987 * Do not call this with mac_vlan_list_lock!
 988 **/
 989static int iavf_replace_primary_mac(struct iavf_adapter *adapter,
 990				    const u8 *new_mac)
 991{
 992	struct iavf_hw *hw = &adapter->hw;
 993	struct iavf_mac_filter *new_f;
 994	struct iavf_mac_filter *old_f;
 995
 996	spin_lock_bh(&adapter->mac_vlan_list_lock);
 997
 998	new_f = iavf_add_filter(adapter, new_mac);
 999	if (!new_f) {
1000		spin_unlock_bh(&adapter->mac_vlan_list_lock);
1001		return -ENOMEM;
1002	}
1003
1004	old_f = iavf_find_filter(adapter, hw->mac.addr);
1005	if (old_f) {
1006		old_f->is_primary = false;
1007		old_f->remove = true;
1008		adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1009	}
1010	/* Always send the request to add if changing primary MAC,
1011	 * even if filter is already present on the list
1012	 */
1013	new_f->is_primary = true;
1014	new_f->add = true;
1015	ether_addr_copy(hw->mac.addr, new_mac);
1016
1017	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1018
1019	/* schedule the watchdog task to immediately process the request */
1020	iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_MAC_FILTER);
1021	return 0;
1022}
1023
1024/**
1025 * iavf_is_mac_set_handled - wait for a response to set MAC from PF
1026 * @netdev: network interface device structure
1027 * @macaddr: MAC address to set
1028 *
1029 * Returns true on success, false on failure
1030 */
1031static bool iavf_is_mac_set_handled(struct net_device *netdev,
1032				    const u8 *macaddr)
1033{
1034	struct iavf_adapter *adapter = netdev_priv(netdev);
1035	struct iavf_mac_filter *f;
1036	bool ret = false;
1037
1038	spin_lock_bh(&adapter->mac_vlan_list_lock);
1039
1040	f = iavf_find_filter(adapter, macaddr);
1041
1042	if (!f || (!f->add && f->add_handled))
1043		ret = true;
1044
1045	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1046
1047	return ret;
1048}
1049
1050/**
1051 * iavf_set_mac - NDO callback to set port MAC address
1052 * @netdev: network interface device structure
1053 * @p: pointer to an address structure
1054 *
1055 * Returns 0 on success, negative on failure
1056 */
1057static int iavf_set_mac(struct net_device *netdev, void *p)
1058{
1059	struct iavf_adapter *adapter = netdev_priv(netdev);
1060	struct sockaddr *addr = p;
1061	int ret;
1062
1063	if (!is_valid_ether_addr(addr->sa_data))
1064		return -EADDRNOTAVAIL;
1065
1066	ret = iavf_replace_primary_mac(adapter, addr->sa_data);
1067
1068	if (ret)
1069		return ret;
1070
1071	ret = wait_event_interruptible_timeout(adapter->vc_waitqueue,
1072					       iavf_is_mac_set_handled(netdev, addr->sa_data),
1073					       msecs_to_jiffies(2500));
1074
1075	/* If ret < 0 then it means wait was interrupted.
1076	 * If ret == 0 then it means we got a timeout.
1077	 * else it means we got response for set MAC from PF,
1078	 * check if netdev MAC was updated to requested MAC,
1079	 * if yes then set MAC succeeded otherwise it failed return -EACCES
1080	 */
1081	if (ret < 0)
1082		return ret;
1083
1084	if (!ret)
1085		return -EAGAIN;
1086
1087	if (!ether_addr_equal(netdev->dev_addr, addr->sa_data))
1088		return -EACCES;
1089
1090	return 0;
1091}
1092
1093/**
1094 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
1095 * @netdev: the netdevice
1096 * @addr: address to add
1097 *
1098 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1099 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1100 */
1101static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
1102{
1103	struct iavf_adapter *adapter = netdev_priv(netdev);
1104
1105	if (iavf_add_filter(adapter, addr))
1106		return 0;
1107	else
1108		return -ENOMEM;
1109}
1110
1111/**
1112 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1113 * @netdev: the netdevice
1114 * @addr: address to add
1115 *
1116 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1117 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1118 */
1119static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
1120{
1121	struct iavf_adapter *adapter = netdev_priv(netdev);
1122	struct iavf_mac_filter *f;
1123
1124	/* Under some circumstances, we might receive a request to delete
1125	 * our own device address from our uc list. Because we store the
1126	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1127	 * such requests and not delete our device address from this list.
1128	 */
1129	if (ether_addr_equal(addr, netdev->dev_addr))
1130		return 0;
1131
1132	f = iavf_find_filter(adapter, addr);
1133	if (f) {
1134		f->remove = true;
1135		adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1136	}
1137	return 0;
1138}
1139
1140/**
1141 * iavf_promiscuous_mode_changed - check if promiscuous mode bits changed
1142 * @adapter: device specific adapter
1143 */
1144bool iavf_promiscuous_mode_changed(struct iavf_adapter *adapter)
1145{
1146	return (adapter->current_netdev_promisc_flags ^ adapter->netdev->flags) &
1147		(IFF_PROMISC | IFF_ALLMULTI);
1148}
1149
1150/**
1151 * iavf_set_rx_mode - NDO callback to set the netdev filters
1152 * @netdev: network interface device structure
1153 **/
1154static void iavf_set_rx_mode(struct net_device *netdev)
1155{
1156	struct iavf_adapter *adapter = netdev_priv(netdev);
1157
1158	spin_lock_bh(&adapter->mac_vlan_list_lock);
1159	__dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1160	__dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1161	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1162
1163	spin_lock_bh(&adapter->current_netdev_promisc_flags_lock);
1164	if (iavf_promiscuous_mode_changed(adapter))
1165		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE;
1166	spin_unlock_bh(&adapter->current_netdev_promisc_flags_lock);
1167}
1168
1169/**
1170 * iavf_napi_enable_all - enable NAPI on all queue vectors
1171 * @adapter: board private structure
1172 **/
1173static void iavf_napi_enable_all(struct iavf_adapter *adapter)
1174{
1175	int q_idx;
1176	struct iavf_q_vector *q_vector;
1177	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1178
1179	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1180		struct napi_struct *napi;
1181
1182		q_vector = &adapter->q_vectors[q_idx];
1183		napi = &q_vector->napi;
1184		napi_enable_locked(napi);
1185	}
1186}
1187
1188/**
1189 * iavf_napi_disable_all - disable NAPI on all queue vectors
1190 * @adapter: board private structure
1191 **/
1192static void iavf_napi_disable_all(struct iavf_adapter *adapter)
1193{
1194	int q_idx;
1195	struct iavf_q_vector *q_vector;
1196	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1197
1198	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1199		q_vector = &adapter->q_vectors[q_idx];
1200		napi_disable_locked(&q_vector->napi);
1201	}
1202}
1203
1204/**
1205 * iavf_configure - set up transmit and receive data structures
1206 * @adapter: board private structure
1207 **/
1208static void iavf_configure(struct iavf_adapter *adapter)
1209{
1210	struct net_device *netdev = adapter->netdev;
1211	int i;
1212
1213	iavf_set_rx_mode(netdev);
1214
1215	iavf_configure_tx(adapter);
1216	iavf_configure_rx(adapter);
1217	adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
1218
1219	for (i = 0; i < adapter->num_active_queues; i++) {
1220		struct iavf_ring *ring = &adapter->rx_rings[i];
1221
1222		iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
1223	}
1224}
1225
1226/**
1227 * iavf_up_complete - Finish the last steps of bringing up a connection
1228 * @adapter: board private structure
1229 */
1230static void iavf_up_complete(struct iavf_adapter *adapter)
1231{
1232	netdev_assert_locked(adapter->netdev);
1233
1234	iavf_change_state(adapter, __IAVF_RUNNING);
1235	clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1236
1237	iavf_napi_enable_all(adapter);
1238
1239	iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ENABLE_QUEUES);
1240}
1241
1242/**
1243 * iavf_clear_mac_vlan_filters - Remove mac and vlan filters not sent to PF
1244 * yet and mark other to be removed.
1245 * @adapter: board private structure
1246 **/
1247static void iavf_clear_mac_vlan_filters(struct iavf_adapter *adapter)
1248{
1249	struct iavf_vlan_filter *vlf, *vlftmp;
1250	struct iavf_mac_filter *f, *ftmp;
1251
1252	spin_lock_bh(&adapter->mac_vlan_list_lock);
1253	/* clear the sync flag on all filters */
1254	__dev_uc_unsync(adapter->netdev, NULL);
1255	__dev_mc_unsync(adapter->netdev, NULL);
1256
1257	/* remove all MAC filters */
1258	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1259				 list) {
1260		if (f->add) {
1261			list_del(&f->list);
1262			kfree(f);
1263		} else {
1264			f->remove = true;
1265		}
1266	}
1267
1268	/* disable all VLAN filters */
1269	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
1270				 list)
1271		vlf->state = IAVF_VLAN_DISABLE;
1272
1273	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1274}
1275
1276/**
1277 * iavf_clear_cloud_filters - Remove cloud filters not sent to PF yet and
1278 * mark other to be removed.
1279 * @adapter: board private structure
1280 **/
1281static void iavf_clear_cloud_filters(struct iavf_adapter *adapter)
1282{
1283	struct iavf_cloud_filter *cf, *cftmp;
1284
1285	/* remove all cloud filters */
1286	spin_lock_bh(&adapter->cloud_filter_list_lock);
1287	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1288				 list) {
1289		if (cf->add) {
1290			list_del(&cf->list);
1291			kfree(cf);
1292			adapter->num_cloud_filters--;
1293		} else {
1294			cf->del = true;
1295		}
1296	}
1297	spin_unlock_bh(&adapter->cloud_filter_list_lock);
1298}
1299
1300/**
1301 * iavf_clear_fdir_filters - Remove fdir filters not sent to PF yet and mark
1302 * other to be removed.
1303 * @adapter: board private structure
1304 **/
1305static void iavf_clear_fdir_filters(struct iavf_adapter *adapter)
1306{
1307	struct iavf_fdir_fltr *fdir;
1308
1309	/* remove all Flow Director filters */
1310	spin_lock_bh(&adapter->fdir_fltr_lock);
1311	list_for_each_entry(fdir, &adapter->fdir_list_head, list) {
1312		if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST) {
1313			/* Cancel a request, keep filter as inactive */
1314			fdir->state = IAVF_FDIR_FLTR_INACTIVE;
1315		} else if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING ||
1316			 fdir->state == IAVF_FDIR_FLTR_ACTIVE) {
1317			/* Disable filters which are active or have a pending
1318			 * request to PF to be added
1319			 */
1320			fdir->state = IAVF_FDIR_FLTR_DIS_REQUEST;
1321		}
1322	}
1323	spin_unlock_bh(&adapter->fdir_fltr_lock);
1324}
1325
1326/**
1327 * iavf_clear_adv_rss_conf - Remove adv rss conf not sent to PF yet and mark
1328 * other to be removed.
1329 * @adapter: board private structure
1330 **/
1331static void iavf_clear_adv_rss_conf(struct iavf_adapter *adapter)
1332{
1333	struct iavf_adv_rss *rss, *rsstmp;
1334
1335	/* remove all advance RSS configuration */
1336	spin_lock_bh(&adapter->adv_rss_lock);
1337	list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
1338				 list) {
1339		if (rss->state == IAVF_ADV_RSS_ADD_REQUEST) {
1340			list_del(&rss->list);
1341			kfree(rss);
1342		} else {
1343			rss->state = IAVF_ADV_RSS_DEL_REQUEST;
1344		}
1345	}
1346	spin_unlock_bh(&adapter->adv_rss_lock);
1347}
1348
1349/**
1350 * iavf_down - Shutdown the connection processing
1351 * @adapter: board private structure
1352 */
1353void iavf_down(struct iavf_adapter *adapter)
1354{
1355	struct net_device *netdev = adapter->netdev;
1356
1357	netdev_assert_locked(netdev);
1358
1359	if (adapter->state <= __IAVF_DOWN_PENDING)
1360		return;
1361
1362	netif_carrier_off(netdev);
1363	netif_tx_disable(netdev);
1364	adapter->link_up = false;
1365	iavf_napi_disable_all(adapter);
1366	iavf_irq_disable(adapter);
1367
1368	iavf_clear_mac_vlan_filters(adapter);
1369	iavf_clear_cloud_filters(adapter);
1370	iavf_clear_fdir_filters(adapter);
1371	iavf_clear_adv_rss_conf(adapter);
1372
1373	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
1374		return;
1375
1376	if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
1377		/* cancel any current operation */
1378		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1379		/* Schedule operations to close down the HW. Don't wait
1380		 * here for this to complete. The watchdog is still running
1381		 * and it will take care of this.
1382		 */
1383		if (!list_empty(&adapter->mac_filter_list))
1384			adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1385		if (!list_empty(&adapter->vlan_filter_list))
1386			adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1387		if (!list_empty(&adapter->cloud_filter_list))
1388			adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1389		if (!list_empty(&adapter->fdir_list_head))
1390			adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1391		if (!list_empty(&adapter->adv_rss_list_head))
1392			adapter->aq_required |= IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1393	}
1394
1395	iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_DISABLE_QUEUES);
1396}
1397
1398/**
1399 * iavf_acquire_msix_vectors - Setup the MSIX capability
1400 * @adapter: board private structure
1401 * @vectors: number of vectors to request
1402 *
1403 * Work with the OS to set up the MSIX vectors needed.
1404 *
1405 * Returns 0 on success, negative on failure
1406 **/
1407static int
1408iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1409{
1410	int err, vector_threshold;
1411
1412	/* We'll want at least 3 (vector_threshold):
1413	 * 0) Other (Admin Queue and link, mostly)
1414	 * 1) TxQ[0] Cleanup
1415	 * 2) RxQ[0] Cleanup
1416	 */
1417	vector_threshold = MIN_MSIX_COUNT;
1418
1419	/* The more we get, the more we will assign to Tx/Rx Cleanup
1420	 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1421	 * Right now, we simply care about how many we'll get; we'll
1422	 * set them up later while requesting irq's.
1423	 */
1424	err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1425				    vector_threshold, vectors);
1426	if (err < 0) {
1427		dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1428		kfree(adapter->msix_entries);
1429		adapter->msix_entries = NULL;
1430		return err;
1431	}
1432
1433	/* Adjust for only the vectors we'll use, which is minimum
1434	 * of max_msix_q_vectors + NONQ_VECS, or the number of
1435	 * vectors we were allocated.
1436	 */
1437	adapter->num_msix_vectors = err;
1438	return 0;
1439}
1440
1441/**
1442 * iavf_free_queues - Free memory for all rings
1443 * @adapter: board private structure to initialize
1444 *
1445 * Free all of the memory associated with queue pairs.
1446 **/
1447static void iavf_free_queues(struct iavf_adapter *adapter)
1448{
1449	if (!adapter->vsi_res)
1450		return;
1451	adapter->num_active_queues = 0;
1452	kfree(adapter->tx_rings);
1453	adapter->tx_rings = NULL;
1454	kfree(adapter->rx_rings);
1455	adapter->rx_rings = NULL;
1456}
1457
1458/**
1459 * iavf_set_queue_vlan_tag_loc - set location for VLAN tag offload
1460 * @adapter: board private structure
1461 *
1462 * Based on negotiated capabilities, the VLAN tag needs to be inserted and/or
1463 * stripped in certain descriptor fields. Instead of checking the offload
1464 * capability bits in the hot path, cache the location the ring specific
1465 * flags.
1466 */
1467void iavf_set_queue_vlan_tag_loc(struct iavf_adapter *adapter)
1468{
1469	int i;
1470
1471	for (i = 0; i < adapter->num_active_queues; i++) {
1472		struct iavf_ring *tx_ring = &adapter->tx_rings[i];
1473		struct iavf_ring *rx_ring = &adapter->rx_rings[i];
1474
1475		/* prevent multiple L2TAG bits being set after VFR */
1476		tx_ring->flags &=
1477			~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1478			  IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2);
1479		rx_ring->flags &=
1480			~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1481			  IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2);
1482
1483		if (VLAN_ALLOWED(adapter)) {
1484			tx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1485			rx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1486		} else if (VLAN_V2_ALLOWED(adapter)) {
1487			struct virtchnl_vlan_supported_caps *stripping_support;
1488			struct virtchnl_vlan_supported_caps *insertion_support;
1489
1490			stripping_support =
1491				&adapter->vlan_v2_caps.offloads.stripping_support;
1492			insertion_support =
1493				&adapter->vlan_v2_caps.offloads.insertion_support;
1494
1495			if (stripping_support->outer) {
1496				if (stripping_support->outer &
1497				    VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1498					rx_ring->flags |=
1499						IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1500				else if (stripping_support->outer &
1501					 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1502					rx_ring->flags |=
1503						IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1504			} else if (stripping_support->inner) {
1505				if (stripping_support->inner &
1506				    VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1507					rx_ring->flags |=
1508						IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1509				else if (stripping_support->inner &
1510					 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1511					rx_ring->flags |=
1512						IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1513			}
1514
1515			if (insertion_support->outer) {
1516				if (insertion_support->outer &
1517				    VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1518					tx_ring->flags |=
1519						IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1520				else if (insertion_support->outer &
1521					 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1522					tx_ring->flags |=
1523						IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1524			} else if (insertion_support->inner) {
1525				if (insertion_support->inner &
1526				    VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1527					tx_ring->flags |=
1528						IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1529				else if (insertion_support->inner &
1530					 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1531					tx_ring->flags |=
1532						IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1533			}
1534		}
1535	}
1536}
1537
1538/**
1539 * iavf_alloc_queues - Allocate memory for all rings
1540 * @adapter: board private structure to initialize
1541 *
1542 * We allocate one ring per queue at run-time since we don't know the
1543 * number of queues at compile-time.  The polling_netdev array is
1544 * intended for Multiqueue, but should work fine with a single queue.
1545 **/
1546static int iavf_alloc_queues(struct iavf_adapter *adapter)
1547{
1548	int i, num_active_queues;
1549
1550	/* If we're in reset reallocating queues we don't actually know yet for
1551	 * certain the PF gave us the number of queues we asked for but we'll
1552	 * assume it did.  Once basic reset is finished we'll confirm once we
1553	 * start negotiating config with PF.
1554	 */
1555	if (adapter->num_req_queues)
1556		num_active_queues = adapter->num_req_queues;
1557	else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1558		 adapter->num_tc)
1559		num_active_queues = adapter->ch_config.total_qps;
1560	else
1561		num_active_queues = min_t(int,
1562					  adapter->vsi_res->num_queue_pairs,
1563					  (int)(num_online_cpus()));
1564
1565
1566	adapter->tx_rings = kcalloc(num_active_queues,
1567				    sizeof(struct iavf_ring), GFP_KERNEL);
1568	if (!adapter->tx_rings)
1569		goto err_out;
1570	adapter->rx_rings = kcalloc(num_active_queues,
1571				    sizeof(struct iavf_ring), GFP_KERNEL);
1572	if (!adapter->rx_rings)
1573		goto err_out;
1574
1575	for (i = 0; i < num_active_queues; i++) {
1576		struct iavf_ring *tx_ring;
1577		struct iavf_ring *rx_ring;
1578
1579		tx_ring = &adapter->tx_rings[i];
1580
1581		tx_ring->queue_index = i;
1582		tx_ring->netdev = adapter->netdev;
1583		tx_ring->dev = &adapter->pdev->dev;
1584		tx_ring->count = adapter->tx_desc_count;
1585		tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1586		if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1587			tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1588
1589		rx_ring = &adapter->rx_rings[i];
1590		rx_ring->queue_index = i;
1591		rx_ring->netdev = adapter->netdev;
1592		rx_ring->count = adapter->rx_desc_count;
1593		rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1594	}
1595
1596	adapter->num_active_queues = num_active_queues;
1597
1598	iavf_set_queue_vlan_tag_loc(adapter);
1599
1600	return 0;
1601
1602err_out:
1603	iavf_free_queues(adapter);
1604	return -ENOMEM;
1605}
1606
1607/**
1608 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1609 * @adapter: board private structure to initialize
1610 *
1611 * Attempt to configure the interrupts using the best available
1612 * capabilities of the hardware and the kernel.
1613 **/
1614static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1615{
1616	int vector, v_budget;
1617	int pairs = 0;
1618	int err = 0;
1619
1620	if (!adapter->vsi_res) {
1621		err = -EIO;
1622		goto out;
1623	}
1624	pairs = adapter->num_active_queues;
1625
1626	/* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1627	 * us much good if we have more vectors than CPUs. However, we already
1628	 * limit the total number of queues by the number of CPUs so we do not
1629	 * need any further limiting here.
1630	 */
1631	v_budget = min_t(int, pairs + NONQ_VECS,
1632			 (int)adapter->vf_res->max_vectors);
1633
1634	adapter->msix_entries = kcalloc(v_budget,
1635					sizeof(struct msix_entry), GFP_KERNEL);
1636	if (!adapter->msix_entries) {
1637		err = -ENOMEM;
1638		goto out;
1639	}
1640
1641	for (vector = 0; vector < v_budget; vector++)
1642		adapter->msix_entries[vector].entry = vector;
1643
1644	err = iavf_acquire_msix_vectors(adapter, v_budget);
1645	if (!err)
1646		iavf_schedule_finish_config(adapter);
1647
1648out:
1649	return err;
1650}
1651
1652/**
1653 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1654 * @adapter: board private structure
1655 *
1656 * Return 0 on success, negative on failure
1657 **/
1658static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1659{
1660	struct iavf_aqc_get_set_rss_key_data *rss_key =
1661		(struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1662	struct iavf_hw *hw = &adapter->hw;
1663	enum iavf_status status;
1664
1665	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1666		/* bail because we already have a command pending */
1667		dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1668			adapter->current_op);
1669		return -EBUSY;
1670	}
1671
1672	status = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1673	if (status) {
1674		dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1675			iavf_stat_str(hw, status),
1676			libie_aq_str(hw->aq.asq_last_status));
1677		return iavf_status_to_errno(status);
1678
1679	}
1680
1681	status = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1682				     adapter->rss_lut, adapter->rss_lut_size);
1683	if (status) {
1684		dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1685			iavf_stat_str(hw, status),
1686			libie_aq_str(hw->aq.asq_last_status));
1687		return iavf_status_to_errno(status);
1688	}
1689
1690	return 0;
1691
1692}
1693
1694/**
1695 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1696 * @adapter: board private structure
1697 *
1698 * Returns 0 on success, negative on failure
1699 **/
1700static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1701{
1702	struct iavf_hw *hw = &adapter->hw;
1703	u32 *dw;
1704	u16 i;
1705
1706	dw = (u32 *)adapter->rss_key;
1707	for (i = 0; i < adapter->rss_key_size / 4; i++)
1708		wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1709
1710	dw = (u32 *)adapter->rss_lut;
1711	for (i = 0; i < adapter->rss_lut_size / 4; i++)
1712		wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1713
1714	iavf_flush(hw);
1715
1716	return 0;
1717}
1718
1719/**
1720 * iavf_config_rss - Configure RSS keys and lut
1721 * @adapter: board private structure
1722 *
1723 * Returns 0 on success, negative on failure
1724 **/
1725int iavf_config_rss(struct iavf_adapter *adapter)
1726{
1727
1728	if (RSS_PF(adapter)) {
1729		adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1730					IAVF_FLAG_AQ_SET_RSS_KEY;
1731		return 0;
1732	} else if (RSS_AQ(adapter)) {
1733		return iavf_config_rss_aq(adapter);
1734	} else {
1735		return iavf_config_rss_reg(adapter);
1736	}
1737}
1738
1739/**
1740 * iavf_fill_rss_lut - Fill the lut with default values
1741 * @adapter: board private structure
1742 **/
1743static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1744{
1745	u16 i;
1746
1747	for (i = 0; i < adapter->rss_lut_size; i++)
1748		adapter->rss_lut[i] = i % adapter->num_active_queues;
1749}
1750
1751/**
1752 * iavf_init_rss - Prepare for RSS
1753 * @adapter: board private structure
1754 *
1755 * Return 0 on success, negative on failure
1756 **/
1757static int iavf_init_rss(struct iavf_adapter *adapter)
1758{
1759	struct iavf_hw *hw = &adapter->hw;
1760
1761	if (!RSS_PF(adapter)) {
1762		/* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1763		if (adapter->vf_res->vf_cap_flags &
1764		    VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1765			adapter->rss_hashcfg =
1766				IAVF_DEFAULT_RSS_HASHCFG_EXPANDED;
1767		else
1768			adapter->rss_hashcfg = IAVF_DEFAULT_RSS_HASHCFG;
1769
1770		wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->rss_hashcfg);
1771		wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->rss_hashcfg >> 32));
1772	}
1773
1774	iavf_fill_rss_lut(adapter);
1775	netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1776
1777	return iavf_config_rss(adapter);
1778}
1779
1780/**
1781 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1782 * @adapter: board private structure to initialize
1783 *
1784 * We allocate one q_vector per queue interrupt.  If allocation fails we
1785 * return -ENOMEM.
1786 **/
1787static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1788{
1789	int q_idx = 0, num_q_vectors, irq_num;
1790	struct iavf_q_vector *q_vector;
1791
1792	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1793	adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1794				     GFP_KERNEL);
1795	if (!adapter->q_vectors)
1796		return -ENOMEM;
1797
1798	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1799		irq_num = adapter->msix_entries[q_idx + NONQ_VECS].vector;
1800		q_vector = &adapter->q_vectors[q_idx];
1801		q_vector->adapter = adapter;
1802		q_vector->vsi = &adapter->vsi;
1803		q_vector->v_idx = q_idx;
1804		q_vector->reg_idx = q_idx;
1805		netif_napi_add_config_locked(adapter->netdev, &q_vector->napi,
1806					     iavf_napi_poll, q_idx);
1807		netif_napi_set_irq_locked(&q_vector->napi, irq_num);
1808	}
1809
1810	return 0;
1811}
1812
1813/**
1814 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1815 * @adapter: board private structure to initialize
1816 *
1817 * This function frees the memory allocated to the q_vectors.  In addition if
1818 * NAPI is enabled it will delete any references to the NAPI struct prior
1819 * to freeing the q_vector.
1820 **/
1821static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1822{
1823	int q_idx, num_q_vectors;
1824
1825	if (!adapter->q_vectors)
1826		return;
1827
1828	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1829
1830	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1831		struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1832
1833		netif_napi_del_locked(&q_vector->napi);
1834	}
1835	kfree(adapter->q_vectors);
1836	adapter->q_vectors = NULL;
1837}
1838
1839/**
1840 * iavf_reset_interrupt_capability - Reset MSIX setup
1841 * @adapter: board private structure
1842 *
1843 **/
1844static void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1845{
1846	if (!adapter->msix_entries)
1847		return;
1848
1849	pci_disable_msix(adapter->pdev);
1850	kfree(adapter->msix_entries);
1851	adapter->msix_entries = NULL;
1852}
1853
1854/**
1855 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1856 * @adapter: board private structure to initialize
1857 *
1858 **/
1859static int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1860{
1861	int err;
1862
1863	err = iavf_alloc_queues(adapter);
1864	if (err) {
1865		dev_err(&adapter->pdev->dev,
1866			"Unable to allocate memory for queues\n");
1867		goto err_alloc_queues;
1868	}
1869
1870	err = iavf_set_interrupt_capability(adapter);
1871	if (err) {
1872		dev_err(&adapter->pdev->dev,
1873			"Unable to setup interrupt capabilities\n");
1874		goto err_set_interrupt;
1875	}
1876
1877	err = iavf_alloc_q_vectors(adapter);
1878	if (err) {
1879		dev_err(&adapter->pdev->dev,
1880			"Unable to allocate memory for queue vectors\n");
1881		goto err_alloc_q_vectors;
1882	}
1883
1884	/* If we've made it so far while ADq flag being ON, then we haven't
1885	 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1886	 * resources have been allocated in the reset path.
1887	 * Now we can truly claim that ADq is enabled.
1888	 */
1889	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1890	    adapter->num_tc)
1891		dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1892			 adapter->num_tc);
1893
1894	dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1895		 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1896		 adapter->num_active_queues);
1897
1898	return 0;
1899err_alloc_q_vectors:
1900	iavf_reset_interrupt_capability(adapter);
1901err_set_interrupt:
1902	iavf_free_queues(adapter);
1903err_alloc_queues:
1904	return err;
1905}
1906
1907/**
1908 * iavf_free_interrupt_scheme - Undo what iavf_init_interrupt_scheme does
1909 * @adapter: board private structure
1910 **/
1911static void iavf_free_interrupt_scheme(struct iavf_adapter *adapter)
1912{
1913	iavf_free_q_vectors(adapter);
1914	iavf_reset_interrupt_capability(adapter);
1915	iavf_free_queues(adapter);
1916}
1917
1918/**
1919 * iavf_free_rss - Free memory used by RSS structs
1920 * @adapter: board private structure
1921 **/
1922static void iavf_free_rss(struct iavf_adapter *adapter)
1923{
1924	kfree(adapter->rss_key);
1925	adapter->rss_key = NULL;
1926
1927	kfree(adapter->rss_lut);
1928	adapter->rss_lut = NULL;
1929}
1930
1931/**
1932 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1933 * @adapter: board private structure
1934 * @running: true if adapter->state == __IAVF_RUNNING
1935 *
1936 * Returns 0 on success, negative on failure
1937 **/
1938static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter, bool running)
1939{
1940	struct net_device *netdev = adapter->netdev;
1941	int err;
1942
1943	if (running)
1944		iavf_free_traffic_irqs(adapter);
1945	iavf_free_misc_irq(adapter);
1946	iavf_free_interrupt_scheme(adapter);
1947
1948	err = iavf_init_interrupt_scheme(adapter);
1949	if (err)
1950		goto err;
1951
1952	netif_tx_stop_all_queues(netdev);
1953
1954	err = iavf_request_misc_irq(adapter);
1955	if (err)
1956		goto err;
1957
1958	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1959
1960	iavf_map_rings_to_vectors(adapter);
1961err:
1962	return err;
1963}
1964
1965/**
1966 * iavf_finish_config - do all netdev work that needs RTNL
1967 * @work: our work_struct
1968 *
1969 * Do work that needs RTNL.
1970 */
1971static void iavf_finish_config(struct work_struct *work)
1972{
1973	struct iavf_adapter *adapter;
1974	bool netdev_released = false;
1975	int pairs, err;
1976
1977	adapter = container_of(work, struct iavf_adapter, finish_config);
1978
1979	/* Always take RTNL first to prevent circular lock dependency;
1980	 * the dev->lock (== netdev lock) is needed to update the queue number.
1981	 */
1982	rtnl_lock();
1983	netdev_lock(adapter->netdev);
1984
1985	if ((adapter->flags & IAVF_FLAG_SETUP_NETDEV_FEATURES) &&
1986	    adapter->netdev->reg_state == NETREG_REGISTERED &&
1987	    !test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
1988		netdev_update_features(adapter->netdev);
1989		adapter->flags &= ~IAVF_FLAG_SETUP_NETDEV_FEATURES;
1990	}
1991
1992	switch (adapter->state) {
1993	case __IAVF_DOWN:
1994		/* Set the real number of queues when reset occurs while
1995		 * state == __IAVF_DOWN
1996		 */
1997		pairs = adapter->num_active_queues;
1998		netif_set_real_num_rx_queues(adapter->netdev, pairs);
1999		netif_set_real_num_tx_queues(adapter->netdev, pairs);
2000
2001		if (adapter->netdev->reg_state != NETREG_REGISTERED) {
2002			netdev_unlock(adapter->netdev);
2003			netdev_released = true;
2004			err = register_netdevice(adapter->netdev);
2005			if (err) {
2006				dev_err(&adapter->pdev->dev, "Unable to register netdev (%d)\n",
2007					err);
2008
2009				/* go back and try again.*/
2010				netdev_lock(adapter->netdev);
2011				iavf_free_rss(adapter);
2012				iavf_free_misc_irq(adapter);
2013				iavf_reset_interrupt_capability(adapter);
2014				iavf_change_state(adapter,
2015						  __IAVF_INIT_CONFIG_ADAPTER);
2016				netdev_unlock(adapter->netdev);
2017				goto out;
2018			}
2019		}
2020		break;
2021	case __IAVF_RUNNING:
2022		pairs = adapter->num_active_queues;
2023		netif_set_real_num_rx_queues(adapter->netdev, pairs);
2024		netif_set_real_num_tx_queues(adapter->netdev, pairs);
2025		break;
2026
2027	default:
2028		break;
2029	}
2030
2031out:
2032	if (!netdev_released)
2033		netdev_unlock(adapter->netdev);
2034	rtnl_unlock();
2035}
2036
2037/**
2038 * iavf_schedule_finish_config - Set the flags and schedule a reset event
2039 * @adapter: board private structure
2040 **/
2041void iavf_schedule_finish_config(struct iavf_adapter *adapter)
2042{
2043	if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2044		queue_work(adapter->wq, &adapter->finish_config);
2045}
2046
2047/**
2048 * iavf_process_aq_command - process aq_required flags
2049 * and sends aq command
2050 * @adapter: pointer to iavf adapter structure
2051 *
2052 * Returns 0 on success
2053 * Returns error code if no command was sent
2054 * or error code if the command failed.
2055 **/
2056static int iavf_process_aq_command(struct iavf_adapter *adapter)
2057{
2058	if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
2059		return iavf_send_vf_config_msg(adapter);
2060	if (adapter->aq_required & IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS)
2061		return iavf_send_vf_offload_vlan_v2_msg(adapter);
2062	if (adapter->aq_required & IAVF_FLAG_AQ_GET_SUPPORTED_RXDIDS)
2063		return iavf_send_vf_supported_rxdids_msg(adapter);
2064	if (adapter->aq_required & IAVF_FLAG_AQ_GET_PTP_CAPS)
2065		return iavf_send_vf_ptp_caps_msg(adapter);
2066	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
2067		iavf_disable_queues(adapter);
2068		return 0;
2069	}
2070
2071	if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
2072		iavf_map_queues(adapter);
2073		return 0;
2074	}
2075
2076	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
2077		iavf_add_ether_addrs(adapter);
2078		return 0;
2079	}
2080
2081	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
2082		iavf_add_vlans(adapter);
2083		return 0;
2084	}
2085
2086	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
2087		iavf_del_ether_addrs(adapter);
2088		return 0;
2089	}
2090
2091	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
2092		iavf_del_vlans(adapter);
2093		return 0;
2094	}
2095
2096	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
2097		iavf_enable_vlan_stripping(adapter);
2098		return 0;
2099	}
2100
2101	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
2102		iavf_disable_vlan_stripping(adapter);
2103		return 0;
2104	}
2105
2106	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW) {
2107		iavf_cfg_queues_bw(adapter);
2108		return 0;
2109	}
2110
2111	if (adapter->aq_required & IAVF_FLAG_AQ_GET_QOS_CAPS) {
2112		iavf_get_qos_caps(adapter);
2113		return 0;
2114	}
2115
2116	if (adapter->aq_required & IAVF_FLAG_AQ_CFG_QUEUES_QUANTA_SIZE) {
2117		iavf_cfg_queues_quanta_size(adapter);
2118		return 0;
2119	}
2120
2121	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
2122		iavf_configure_queues(adapter);
2123		return 0;
2124	}
2125
2126	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
2127		iavf_enable_queues(adapter);
2128		return 0;
2129	}
2130
2131	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
2132		/* This message goes straight to the firmware, not the
2133		 * PF, so we don't have to set current_op as we will
2134		 * not get a response through the ARQ.
2135		 */
2136		adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
2137		return 0;
2138	}
2139	if (adapter->aq_required & IAVF_FLAG_AQ_GET_RSS_HASHCFG) {
2140		iavf_get_rss_hashcfg(adapter);
2141		return 0;
2142	}
2143	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_HASHCFG) {
2144		iavf_set_rss_hashcfg(adapter);
2145		return 0;
2146	}
2147	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
2148		iavf_set_rss_key(adapter);
2149		return 0;
2150	}
2151	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
2152		iavf_set_rss_lut(adapter);
2153		return 0;
2154	}
2155	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_HFUNC) {
2156		iavf_set_rss_hfunc(adapter);
2157		return 0;
2158	}
2159
2160	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE) {
2161		iavf_set_promiscuous(adapter);
2162		return 0;
2163	}
2164
2165	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
2166		iavf_enable_channels(adapter);
2167		return 0;
2168	}
2169
2170	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
2171		iavf_disable_channels(adapter);
2172		return 0;
2173	}
2174	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2175		iavf_add_cloud_filter(adapter);
2176		return 0;
2177	}
2178	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2179		iavf_del_cloud_filter(adapter);
2180		return 0;
2181	}
2182	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
2183		iavf_add_fdir_filter(adapter);
2184		return IAVF_SUCCESS;
2185	}
2186	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
2187		iavf_del_fdir_filter(adapter);
2188		return IAVF_SUCCESS;
2189	}
2190	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
2191		iavf_add_adv_rss_cfg(adapter);
2192		return 0;
2193	}
2194	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
2195		iavf_del_adv_rss_cfg(adapter);
2196		return 0;
2197	}
2198	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING) {
2199		iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2200		return 0;
2201	}
2202	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING) {
2203		iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2204		return 0;
2205	}
2206	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING) {
2207		iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2208		return 0;
2209	}
2210	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING) {
2211		iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2212		return 0;
2213	}
2214	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION) {
2215		iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2216		return 0;
2217	}
2218	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION) {
2219		iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2220		return 0;
2221	}
2222	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION) {
2223		iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2224		return 0;
2225	}
2226	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION) {
2227		iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2228		return 0;
2229	}
2230	if (adapter->aq_required & IAVF_FLAG_AQ_SEND_PTP_CMD) {
2231		iavf_virtchnl_send_ptp_cmd(adapter);
2232		return IAVF_SUCCESS;
2233	}
2234	if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
2235		iavf_request_stats(adapter);
2236		return 0;
2237	}
2238
2239	return -EAGAIN;
2240}
2241
2242/**
2243 * iavf_set_vlan_offload_features - set VLAN offload configuration
2244 * @adapter: board private structure
2245 * @prev_features: previous features used for comparison
2246 * @features: updated features used for configuration
2247 *
2248 * Set the aq_required bit(s) based on the requested features passed in to
2249 * configure VLAN stripping and/or VLAN insertion if supported. Also, schedule
2250 * the watchdog if any changes are requested to expedite the request via
2251 * virtchnl.
2252 **/
2253static void
2254iavf_set_vlan_offload_features(struct iavf_adapter *adapter,
2255			       netdev_features_t prev_features,
2256			       netdev_features_t features)
2257{
2258	bool enable_stripping = true, enable_insertion = true;
2259	u16 vlan_ethertype = 0;
2260	u64 aq_required = 0;
2261
2262	/* keep cases separate because one ethertype for offloads can be
2263	 * disabled at the same time as another is disabled, so check for an
2264	 * enabled ethertype first, then check for disabled. Default to
2265	 * ETH_P_8021Q so an ethertype is specified if disabling insertion and
2266	 * stripping.
2267	 */
2268	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2269		vlan_ethertype = ETH_P_8021AD;
2270	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2271		vlan_ethertype = ETH_P_8021Q;
2272	else if (prev_features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2273		vlan_ethertype = ETH_P_8021AD;
2274	else if (prev_features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2275		vlan_ethertype = ETH_P_8021Q;
2276	else
2277		vlan_ethertype = ETH_P_8021Q;
2278
2279	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
2280		enable_stripping = false;
2281	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
2282		enable_insertion = false;
2283
2284	if (VLAN_ALLOWED(adapter)) {
2285		/* VIRTCHNL_VF_OFFLOAD_VLAN only has support for toggling VLAN
2286		 * stripping via virtchnl. VLAN insertion can be toggled on the
2287		 * netdev, but it doesn't require a virtchnl message
2288		 */
2289		if (enable_stripping)
2290			aq_required |= IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2291		else
2292			aq_required |= IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2293
2294	} else if (VLAN_V2_ALLOWED(adapter)) {
2295		switch (vlan_ethertype) {
2296		case ETH_P_8021Q:
2297			if (enable_stripping)
2298				aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
2299			else
2300				aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
2301
2302			if (enable_insertion)
2303				aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
2304			else
2305				aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
2306			break;
2307		case ETH_P_8021AD:
2308			if (enable_stripping)
2309				aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
2310			else
2311				aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
2312
2313			if (enable_insertion)
2314				aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
2315			else
2316				aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
2317			break;
2318		}
2319	}
2320
2321	if (aq_required)
2322		iavf_schedule_aq_request(adapter, aq_required);
2323}
2324
2325/**
2326 * iavf_startup - first step of driver startup
2327 * @adapter: board private structure
2328 *
2329 * Function process __IAVF_STARTUP driver state.
2330 * When success the state is changed to __IAVF_INIT_VERSION_CHECK
2331 * when fails the state is changed to __IAVF_INIT_FAILED
2332 **/
2333static void iavf_startup(struct iavf_adapter *adapter)
2334{
2335	struct pci_dev *pdev = adapter->pdev;
2336	struct iavf_hw *hw = &adapter->hw;
2337	enum iavf_status status;
2338	int ret;
2339
2340	WARN_ON(adapter->state != __IAVF_STARTUP);
2341
2342	/* driver loaded, probe complete */
2343	adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2344	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2345
2346	ret = iavf_check_reset_complete(hw);
2347	if (ret) {
2348		dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2349			 ret);
2350		goto err;
2351	}
2352	hw->aq.num_arq_entries = IAVF_AQ_LEN;
2353	hw->aq.num_asq_entries = IAVF_AQ_LEN;
2354	hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2355	hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2356
2357	status = iavf_init_adminq(hw);
2358	if (status) {
2359		dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2360			status);
2361		goto err;
2362	}
2363	ret = iavf_send_api_ver(adapter);
2364	if (ret) {
2365		dev_err(&pdev->dev, "Unable to send to PF (%d)\n", ret);
2366		iavf_shutdown_adminq(hw);
2367		goto err;
2368	}
2369	iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
2370	return;
2371err:
2372	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2373}
2374
2375/**
2376 * iavf_init_version_check - second step of driver startup
2377 * @adapter: board private structure
2378 *
2379 * Function process __IAVF_INIT_VERSION_CHECK driver state.
2380 * When success the state is changed to __IAVF_INIT_GET_RESOURCES
2381 * when fails the state is changed to __IAVF_INIT_FAILED
2382 **/
2383static void iavf_init_version_check(struct iavf_adapter *adapter)
2384{
2385	struct pci_dev *pdev = adapter->pdev;
2386	struct iavf_hw *hw = &adapter->hw;
2387	int err = -EAGAIN;
2388
2389	WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
2390
2391	if (!iavf_asq_done(hw)) {
2392		dev_err(&pdev->dev, "Admin queue command never completed\n");
2393		iavf_shutdown_adminq(hw);
2394		iavf_change_state(adapter, __IAVF_STARTUP);
2395		goto err;
2396	}
2397
2398	/* aq msg sent, awaiting reply */
2399	err = iavf_verify_api_ver(adapter);
2400	if (err) {
2401		if (err == -EALREADY)
2402			err = iavf_send_api_ver(adapter);
2403		else
2404			dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2405				adapter->pf_version.major,
2406				adapter->pf_version.minor,
2407				VIRTCHNL_VERSION_MAJOR,
2408				VIRTCHNL_VERSION_MINOR);
2409		goto err;
2410	}
2411	err = iavf_send_vf_config_msg(adapter);
2412	if (err) {
2413		dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2414			err);
2415		goto err;
2416	}
2417	iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
2418	return;
2419err:
2420	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2421}
2422
2423/**
2424 * iavf_parse_vf_resource_msg - parse response from VIRTCHNL_OP_GET_VF_RESOURCES
2425 * @adapter: board private structure
2426 */
2427int iavf_parse_vf_resource_msg(struct iavf_adapter *adapter)
2428{
2429	int i, num_req_queues = adapter->num_req_queues;
2430	struct iavf_vsi *vsi = &adapter->vsi;
2431
2432	for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2433		if (adapter->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2434			adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2435	}
2436	if (!adapter->vsi_res) {
2437		dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2438		return -ENODEV;
2439	}
2440
2441	if (num_req_queues &&
2442	    num_req_queues > adapter->vsi_res->num_queue_pairs) {
2443		/* Problem.  The PF gave us fewer queues than what we had
2444		 * negotiated in our request.  Need a reset to see if we can't
2445		 * get back to a working state.
2446		 */
2447		dev_err(&adapter->pdev->dev,
2448			"Requested %d queues, but PF only gave us %d.\n",
2449			num_req_queues,
2450			adapter->vsi_res->num_queue_pairs);
2451		adapter->flags |= IAVF_FLAG_REINIT_MSIX_NEEDED;
2452		adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2453		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
2454
2455		return -EAGAIN;
2456	}
2457	adapter->num_req_queues = 0;
2458	adapter->vsi.id = adapter->vsi_res->vsi_id;
2459
2460	adapter->vsi.back = adapter;
2461	adapter->vsi.base_vector = 1;
2462	vsi->netdev = adapter->netdev;
2463	vsi->qs_handle = adapter->vsi_res->qset_handle;
2464	if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2465		adapter->rss_key_size = adapter->vf_res->rss_key_size;
2466		adapter->rss_lut_size = adapter->vf_res->rss_lut_size;
2467	} else {
2468		adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
2469		adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
2470	}
2471
2472	return 0;
2473}
2474
2475/**
2476 * iavf_init_get_resources - third step of driver startup
2477 * @adapter: board private structure
2478 *
2479 * Function process __IAVF_INIT_GET_RESOURCES driver state and
2480 * finishes driver initialization procedure.
2481 * When success the state is changed to __IAVF_DOWN
2482 * when fails the state is changed to __IAVF_INIT_FAILED
2483 **/
2484static void iavf_init_get_resources(struct iavf_adapter *adapter)
2485{
2486	struct pci_dev *pdev = adapter->pdev;
2487	struct iavf_hw *hw = &adapter->hw;
2488	int err;
2489
2490	WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
2491	/* aq msg sent, awaiting reply */
2492	if (!adapter->vf_res) {
2493		adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
2494					  GFP_KERNEL);
2495		if (!adapter->vf_res) {
2496			err = -ENOMEM;
2497			goto err;
2498		}
2499	}
2500	err = iavf_get_vf_config(adapter);
2501	if (err == -EALREADY) {
2502		err = iavf_send_vf_config_msg(adapter);
2503		goto err;
2504	} else if (err == -EINVAL) {
2505		/* We only get -EINVAL if the device is in a very bad
2506		 * state or if we've been disabled for previous bad
2507		 * behavior. Either way, we're done now.
2508		 */
2509		iavf_shutdown_adminq(hw);
2510		dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2511		return;
2512	}
2513	if (err) {
2514		dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
2515		goto err_alloc;
2516	}
2517
2518	err = iavf_parse_vf_resource_msg(adapter);
2519	if (err) {
2520		dev_err(&pdev->dev, "Failed to parse VF resource message from PF (%d)\n",
2521			err);
2522		goto err_alloc;
2523	}
2524	/* Some features require additional messages to negotiate extended
2525	 * capabilities. These are processed in sequence by the
2526	 * __IAVF_INIT_EXTENDED_CAPS driver state.
2527	 */
2528	adapter->extended_caps = IAVF_EXTENDED_CAPS;
2529
2530	iavf_change_state(adapter, __IAVF_INIT_EXTENDED_CAPS);
2531	return;
2532
2533err_alloc:
2534	kfree(adapter->vf_res);
2535	adapter->vf_res = NULL;
2536err:
2537	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2538}
2539
2540/**
2541 * iavf_init_send_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2542 * @adapter: board private structure
2543 *
2544 * Function processes send of the extended VLAN V2 capability message to the
2545 * PF. Must clear IAVF_EXTENDED_CAP_RECV_VLAN_V2 if the message is not sent,
2546 * e.g. due to PF not negotiating VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2547 */
2548static void iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2549{
2550	int ret;
2551
2552	WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2));
2553
2554	ret = iavf_send_vf_offload_vlan_v2_msg(adapter);
2555	if (ret && ret == -EOPNOTSUPP) {
2556		/* PF does not support VIRTCHNL_VF_OFFLOAD_V2. In this case,
2557		 * we did not send the capability exchange message and do not
2558		 * expect a response.
2559		 */
2560		adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2561	}
2562
2563	/* We sent the message, so move on to the next step */
2564	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2565}
2566
2567/**
2568 * iavf_init_recv_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2569 * @adapter: board private structure
2570 *
2571 * Function processes receipt of the extended VLAN V2 capability message from
2572 * the PF.
2573 **/
2574static void iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2575{
2576	int ret;
2577
2578	WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2));
2579
2580	memset(&adapter->vlan_v2_caps, 0, sizeof(adapter->vlan_v2_caps));
2581
2582	ret = iavf_get_vf_vlan_v2_caps(adapter);
2583	if (ret)
2584		goto err;
2585
2586	/* We've processed receipt of the VLAN V2 caps message */
2587	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2588	return;
2589err:
2590	/* We didn't receive a reply. Make sure we try sending again when
2591	 * __IAVF_INIT_FAILED attempts to recover.
2592	 */
2593	adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2594	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2595}
2596
2597/**
2598 * iavf_init_send_supported_rxdids - part of querying for supported RXDID
2599 * formats
2600 * @adapter: board private structure
2601 *
2602 * Function processes send of the request for supported RXDIDs to the PF.
2603 * Must clear IAVF_EXTENDED_CAP_RECV_RXDID if the message is not sent, e.g.
2604 * due to the PF not negotiating VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC.
2605 */
2606static void iavf_init_send_supported_rxdids(struct iavf_adapter *adapter)
2607{
2608	int ret;
2609
2610	ret = iavf_send_vf_supported_rxdids_msg(adapter);
2611	if (ret == -EOPNOTSUPP) {
2612		/* PF does not support VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC. In this
2613		 * case, we did not send the capability exchange message and
2614		 * do not expect a response.
2615		 */
2616		adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_RXDID;
2617	}
2618
2619	/* We sent the message, so move on to the next step */
2620	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_RXDID;
2621}
2622
2623/**
2624 * iavf_init_recv_supported_rxdids - part of querying for supported RXDID
2625 * formats
2626 * @adapter: board private structure
2627 *
2628 * Function processes receipt of the supported RXDIDs message from the PF.
2629 **/
2630static void iavf_init_recv_supported_rxdids(struct iavf_adapter *adapter)
2631{
2632	int ret;
2633
2634	memset(&adapter->supp_rxdids, 0, sizeof(adapter->supp_rxdids));
2635
2636	ret = iavf_get_vf_supported_rxdids(adapter);
2637	if (ret)
2638		goto err;
2639
2640	/* We've processed the PF response to the
2641	 * VIRTCHNL_OP_GET_SUPPORTED_RXDIDS message we sent previously.
2642	 */
2643	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_RXDID;
2644	return;
2645
2646err:
2647	/* We didn't receive a reply. Make sure we try sending again when
2648	 * __IAVF_INIT_FAILED attempts to recover.
2649	 */
2650	adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_RXDID;
2651	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2652}
2653
2654/**
2655 * iavf_init_send_ptp_caps - part of querying for extended PTP capabilities
2656 * @adapter: board private structure
2657 *
2658 * Function processes send of the request for 1588 PTP capabilities to the PF.
2659 * Must clear IAVF_EXTENDED_CAP_SEND_PTP if the message is not sent, e.g.
2660 * due to the PF not negotiating VIRTCHNL_VF_PTP_CAP
2661 */
2662static void iavf_init_send_ptp_caps(struct iavf_adapter *adapter)
2663{
2664	if (iavf_send_vf_ptp_caps_msg(adapter) == -EOPNOTSUPP) {
2665		/* PF does not support VIRTCHNL_VF_PTP_CAP. In this case, we
2666		 * did not send the capability exchange message and do not
2667		 * expect a response.
2668		 */
2669		adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_PTP;
2670	}
2671
2672	/* We sent the message, so move on to the next step */
2673	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_PTP;
2674}
2675
2676/**
2677 * iavf_init_recv_ptp_caps - part of querying for supported PTP capabilities
2678 * @adapter: board private structure
2679 *
2680 * Function processes receipt of the PTP capabilities supported on this VF.
2681 **/
2682static void iavf_init_recv_ptp_caps(struct iavf_adapter *adapter)
2683{
2684	memset(&adapter->ptp.hw_caps, 0, sizeof(adapter->ptp.hw_caps));
2685
2686	if (iavf_get_vf_ptp_caps(adapter))
2687		goto err;
2688
2689	/* We've processed the PF response to the VIRTCHNL_OP_1588_PTP_GET_CAPS
2690	 * message we sent previously.
2691	 */
2692	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_PTP;
2693	return;
2694
2695err:
2696	/* We didn't receive a reply. Make sure we try sending again when
2697	 * __IAVF_INIT_FAILED attempts to recover.
2698	 */
2699	adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_PTP;
2700	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2701}
2702
2703/**
2704 * iavf_init_process_extended_caps - Part of driver startup
2705 * @adapter: board private structure
2706 *
2707 * Function processes __IAVF_INIT_EXTENDED_CAPS driver state. This state
2708 * handles negotiating capabilities for features which require an additional
2709 * message.
2710 *
2711 * Once all extended capabilities exchanges are finished, the driver will
2712 * transition into __IAVF_INIT_CONFIG_ADAPTER.
2713 */
2714static void iavf_init_process_extended_caps(struct iavf_adapter *adapter)
2715{
2716	WARN_ON(adapter->state != __IAVF_INIT_EXTENDED_CAPS);
2717
2718	/* Process capability exchange for VLAN V2 */
2719	if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2) {
2720		iavf_init_send_offload_vlan_v2_caps(adapter);
2721		return;
2722	} else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2) {
2723		iavf_init_recv_offload_vlan_v2_caps(adapter);
2724		return;
2725	}
2726
2727	/* Process capability exchange for RXDID formats */
2728	if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_RXDID) {
2729		iavf_init_send_supported_rxdids(adapter);
2730		return;
2731	} else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_RXDID) {
2732		iavf_init_recv_supported_rxdids(adapter);
2733		return;
2734	}
2735
2736	/* Process capability exchange for PTP features */
2737	if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_PTP) {
2738		iavf_init_send_ptp_caps(adapter);
2739		return;
2740	} else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_PTP) {
2741		iavf_init_recv_ptp_caps(adapter);
2742		return;
2743	}
2744
2745	/* When we reach here, no further extended capabilities exchanges are
2746	 * necessary, so we finally transition into __IAVF_INIT_CONFIG_ADAPTER
2747	 */
2748	iavf_change_state(adapter, __IAVF_INIT_CONFIG_ADAPTER);
2749}
2750
2751/**
2752 * iavf_init_config_adapter - last part of driver startup
2753 * @adapter: board private structure
2754 *
2755 * After all the supported capabilities are negotiated, then the
2756 * __IAVF_INIT_CONFIG_ADAPTER state will finish driver initialization.
2757 */
2758static void iavf_init_config_adapter(struct iavf_adapter *adapter)
2759{
2760	struct net_device *netdev = adapter->netdev;
2761	struct pci_dev *pdev = adapter->pdev;
2762	int err;
2763
2764	WARN_ON(adapter->state != __IAVF_INIT_CONFIG_ADAPTER);
2765
2766	if (iavf_process_config(adapter))
2767		goto err;
2768
2769	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2770
2771	adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
2772
2773	netdev->netdev_ops = &iavf_netdev_ops;
2774	iavf_set_ethtool_ops(netdev);
2775	netdev->watchdog_timeo = 5 * HZ;
2776
2777	netdev->min_mtu = ETH_MIN_MTU;
2778
2779	/* PF/VF API: vf_res->max_mtu is max frame size (not MTU).
2780	 * Convert to MTU.
2781	 */
2782	if (!adapter->vf_res->max_mtu) {
2783		netdev->max_mtu = LIBIE_MAX_MTU;
2784	} else if (adapter->vf_res->max_mtu < LIBETH_RX_LL_LEN + ETH_MIN_MTU ||
2785		   adapter->vf_res->max_mtu >
2786			   LIBETH_RX_LL_LEN + LIBIE_MAX_MTU) {
2787		netdev_warn_once(adapter->netdev,
2788				 "invalid max frame size %d from PF, using default MTU %d",
2789				 adapter->vf_res->max_mtu, LIBIE_MAX_MTU);
2790		netdev->max_mtu = LIBIE_MAX_MTU;
2791	} else {
2792		netdev->max_mtu = adapter->vf_res->max_mtu - LIBETH_RX_LL_LEN;
2793	}
2794
2795	if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2796		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2797			 adapter->hw.mac.addr);
2798		eth_hw_addr_random(netdev);
2799		ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2800	} else {
2801		eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2802		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2803	}
2804
2805	adapter->tx_desc_count = IAVF_DEFAULT_TXD;
2806	adapter->rx_desc_count = IAVF_DEFAULT_RXD;
2807	err = iavf_init_interrupt_scheme(adapter);
2808	if (err)
2809		goto err_sw_init;
2810	iavf_map_rings_to_vectors(adapter);
2811	if (adapter->vf_res->vf_cap_flags &
2812		VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2813		adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
2814
2815	err = iavf_request_misc_irq(adapter);
2816	if (err)
2817		goto err_sw_init;
2818
2819	netif_carrier_off(netdev);
2820	adapter->link_up = false;
2821	netif_tx_stop_all_queues(netdev);
2822
2823	dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2824	if (netdev->features & NETIF_F_GRO)
2825		dev_info(&pdev->dev, "GRO is enabled\n");
2826
2827	iavf_change_state(adapter, __IAVF_DOWN);
2828	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2829
2830	iavf_misc_irq_enable(adapter);
2831	wake_up(&adapter->down_waitqueue);
2832
2833	adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2834	adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2835	if (!adapter->rss_key || !adapter->rss_lut) {
2836		err = -ENOMEM;
2837		goto err_mem;
2838	}
2839	if (RSS_AQ(adapter))
2840		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2841	else
2842		iavf_init_rss(adapter);
2843
2844	if (VLAN_V2_ALLOWED(adapter))
2845		/* request initial VLAN offload settings */
2846		iavf_set_vlan_offload_features(adapter, 0, netdev->features);
2847
2848	if (QOS_ALLOWED(adapter))
2849		adapter->aq_required |= IAVF_FLAG_AQ_GET_QOS_CAPS;
2850
2851	/* Setup initial PTP configuration */
2852	iavf_ptp_init(adapter);
2853
2854	iavf_schedule_finish_config(adapter);
2855	return;
2856
2857err_mem:
2858	iavf_free_rss(adapter);
2859	iavf_free_misc_irq(adapter);
2860err_sw_init:
2861	iavf_reset_interrupt_capability(adapter);
2862err:
2863	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2864}
2865
2866static const int IAVF_NO_RESCHED = -1;
2867
2868/* return: msec delay for requeueing itself */
2869static int iavf_watchdog_step(struct iavf_adapter *adapter)
2870{
2871	struct iavf_hw *hw = &adapter->hw;
2872	u32 reg_val;
2873
2874	netdev_assert_locked(adapter->netdev);
2875
2876	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2877		iavf_change_state(adapter, __IAVF_COMM_FAILED);
2878
2879	switch (adapter->state) {
2880	case __IAVF_STARTUP:
2881		iavf_startup(adapter);
2882		return 30;
2883	case __IAVF_INIT_VERSION_CHECK:
2884		iavf_init_version_check(adapter);
2885		return 30;
2886	case __IAVF_INIT_GET_RESOURCES:
2887		iavf_init_get_resources(adapter);
2888		return 1;
2889	case __IAVF_INIT_EXTENDED_CAPS:
2890		iavf_init_process_extended_caps(adapter);
2891		return 1;
2892	case __IAVF_INIT_CONFIG_ADAPTER:
2893		iavf_init_config_adapter(adapter);
2894		return 1;
2895	case __IAVF_INIT_FAILED:
2896		if (test_bit(__IAVF_IN_REMOVE_TASK,
2897			     &adapter->crit_section)) {
2898			/* Do not update the state and do not reschedule
2899			 * watchdog task, iavf_remove should handle this state
2900			 * as it can loop forever
2901			 */
2902			return IAVF_NO_RESCHED;
2903		}
2904		if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2905			dev_err(&adapter->pdev->dev,
2906				"Failed to communicate with PF; waiting before retry\n");
2907			adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2908			iavf_shutdown_adminq(hw);
2909			return 5000;
2910		}
2911		/* Try again from failed step*/
2912		iavf_change_state(adapter, adapter->last_state);
2913		return 1000;
2914	case __IAVF_COMM_FAILED:
2915		if (test_bit(__IAVF_IN_REMOVE_TASK,
2916			     &adapter->crit_section)) {
2917			/* Set state to __IAVF_INIT_FAILED and perform remove
2918			 * steps. Remove IAVF_FLAG_PF_COMMS_FAILED so the task
2919			 * doesn't bring the state back to __IAVF_COMM_FAILED.
2920			 */
2921			iavf_change_state(adapter, __IAVF_INIT_FAILED);
2922			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2923			return IAVF_NO_RESCHED;
2924		}
2925		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2926			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2927		if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2928		    reg_val == VIRTCHNL_VFR_COMPLETED) {
2929			/* A chance for redemption! */
2930			dev_err(&adapter->pdev->dev,
2931				"Hardware came out of reset. Attempting reinit.\n");
2932			/* When init task contacts the PF and
2933			 * gets everything set up again, it'll restart the
2934			 * watchdog for us. Down, boy. Sit. Stay. Woof.
2935			 */
2936			iavf_change_state(adapter, __IAVF_STARTUP);
2937			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2938		}
2939		adapter->aq_required = 0;
2940		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2941		return 10;
2942	case __IAVF_RESETTING:
2943		return 2000;
2944	case __IAVF_DOWN:
2945	case __IAVF_DOWN_PENDING:
2946	case __IAVF_TESTING:
2947	case __IAVF_RUNNING:
2948		if (adapter->current_op) {
2949			if (!iavf_asq_done(hw)) {
2950				dev_dbg(&adapter->pdev->dev,
2951					"Admin queue timeout\n");
2952				iavf_send_api_ver(adapter);
2953			}
2954		} else {
2955			int ret = iavf_process_aq_command(adapter);
2956
2957			/* An error will be returned if no commands were
2958			 * processed; use this opportunity to update stats
2959			 * if the error isn't -ENOTSUPP
2960			 */
2961			if (ret && ret != -EOPNOTSUPP &&
2962			    adapter->state == __IAVF_RUNNING)
2963				iavf_request_stats(adapter);
2964		}
2965		if (adapter->state == __IAVF_RUNNING)
2966			iavf_detect_recover_hung(&adapter->vsi);
2967		break;
2968	case __IAVF_REMOVE:
2969	default:
2970		return IAVF_NO_RESCHED;
2971	}
2972
2973	/* check for hw reset */
2974	reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2975	if (!reg_val) {
2976		adapter->aq_required = 0;
2977		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2978		dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2979		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_PENDING);
2980	}
2981
2982	return adapter->aq_required ? 20 : 2000;
2983}
2984
2985static void iavf_watchdog_task(struct work_struct *work)
2986{
2987	struct iavf_adapter *adapter = container_of(work,
2988						    struct iavf_adapter,
2989						    watchdog_task.work);
2990	struct net_device *netdev = adapter->netdev;
2991	int msec_delay;
2992
2993	netdev_lock(netdev);
2994	msec_delay = iavf_watchdog_step(adapter);
2995	/* note that we schedule a different task */
2996	if (adapter->state >= __IAVF_DOWN)
2997		queue_work(adapter->wq, &adapter->adminq_task);
2998
2999	if (msec_delay != IAVF_NO_RESCHED)
3000		queue_delayed_work(adapter->wq, &adapter->watchdog_task,
3001				   msecs_to_jiffies(msec_delay));
3002	netdev_unlock(netdev);
3003}
3004
3005/**
3006 * iavf_disable_vf - disable VF
3007 * @adapter: board private structure
3008 *
3009 * Set communication failed flag and free all resources.
3010 */
3011static void iavf_disable_vf(struct iavf_adapter *adapter)
3012{
3013	struct iavf_mac_filter *f, *ftmp;
3014	struct iavf_vlan_filter *fv, *fvtmp;
3015	struct iavf_cloud_filter *cf, *cftmp;
3016
3017	netdev_assert_locked(adapter->netdev);
3018
3019	adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3020
3021	iavf_ptp_release(adapter);
3022
3023	/* We don't use netif_running() because it may be true prior to
3024	 * ndo_open() returning, so we can't assume it means all our open
3025	 * tasks have finished, since we're not holding the rtnl_lock here.
3026	 */
3027	if (adapter->state == __IAVF_RUNNING) {
3028		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3029		netif_carrier_off(adapter->netdev);
3030		netif_tx_disable(adapter->netdev);
3031		adapter->link_up = false;
3032		iavf_napi_disable_all(adapter);
3033		iavf_irq_disable(adapter);
3034		iavf_free_traffic_irqs(adapter);
3035		iavf_free_all_tx_resources(adapter);
3036		iavf_free_all_rx_resources(adapter);
3037	}
3038
3039	spin_lock_bh(&adapter->mac_vlan_list_lock);
3040
3041	/* Delete all of the filters */
3042	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3043		list_del(&f->list);
3044		kfree(f);
3045	}
3046
3047	list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
3048		list_del(&fv->list);
3049		kfree(fv);
3050	}
3051	adapter->num_vlan_filters = 0;
3052
3053	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3054
3055	spin_lock_bh(&adapter->cloud_filter_list_lock);
3056	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3057		list_del(&cf->list);
3058		kfree(cf);
3059		adapter->num_cloud_filters--;
3060	}
3061	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3062
3063	iavf_free_misc_irq(adapter);
3064	iavf_free_interrupt_scheme(adapter);
3065	memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
3066	iavf_shutdown_adminq(&adapter->hw);
3067	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3068	iavf_change_state(adapter, __IAVF_DOWN);
3069	wake_up(&adapter->down_waitqueue);
3070	dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
3071}
3072
3073/**
3074 * iavf_reconfig_qs_bw - Call-back task to handle hardware reset
3075 * @adapter: board private structure
3076 *
3077 * After a reset, the shaper parameters of queues need to be replayed again.
3078 * Since the net_shaper object inside TX rings persists across reset,
3079 * set the update flag for all queues so that the virtchnl message is triggered
3080 * for all queues.
3081 **/
3082static void iavf_reconfig_qs_bw(struct iavf_adapter *adapter)
3083{
3084	int i, num = 0;
3085
3086	for (i = 0; i < adapter->num_active_queues; i++)
3087		if (adapter->tx_rings[i].q_shaper.bw_min ||
3088		    adapter->tx_rings[i].q_shaper.bw_max) {
3089			adapter->tx_rings[i].q_shaper_update = true;
3090			num++;
3091		}
3092
3093	if (num)
3094		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
3095}
3096
3097/**
3098 * iavf_reset_step - Perform the VF reset sequence
3099 * @adapter: board private structure
3100 *
3101 * Requests a reset from PF, polls for completion, and reconfigures
3102 * the driver. Caller must hold the netdev instance lock.
3103 *
3104 * This can sleep for several seconds while polling HW registers.
3105 */
3106void iavf_reset_step(struct iavf_adapter *adapter)
3107{
3108	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3109	struct net_device *netdev = adapter->netdev;
3110	struct iavf_hw *hw = &adapter->hw;
3111	struct iavf_mac_filter *f, *ftmp;
3112	struct iavf_cloud_filter *cf;
3113	enum iavf_status status;
3114	u32 reg_val;
3115	int i = 0, err;
3116	bool running;
3117
3118	netdev_assert_locked(netdev);
3119
3120	iavf_misc_irq_disable(adapter);
3121	if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
3122		adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
3123		/* Restart the AQ here. If we have been reset but didn't
3124		 * detect it, or if the PF had to reinit, our AQ will be hosed.
3125		 */
3126		iavf_shutdown_adminq(hw);
3127		iavf_init_adminq(hw);
3128		iavf_request_reset(adapter);
3129	}
3130	adapter->flags |= IAVF_FLAG_RESET_PENDING;
3131
3132	/* poll until we see the reset actually happen */
3133	for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
3134		reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
3135			  IAVF_VF_ARQLEN1_ARQENABLE_MASK;
3136		if (!reg_val)
3137			break;
3138		usleep_range(5000, 10000);
3139	}
3140	if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
3141		dev_info(&adapter->pdev->dev, "Never saw reset\n");
3142		goto continue_reset; /* act like the reset happened */
3143	}
3144
3145	/* wait until the reset is complete and the PF is responding to us */
3146	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3147		/* sleep first to make sure a minimum wait time is met */
3148		msleep(IAVF_RESET_WAIT_MS);
3149
3150		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
3151			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3152		if (reg_val == VIRTCHNL_VFR_VFACTIVE)
3153			break;
3154	}
3155
3156	pci_set_master(adapter->pdev);
3157	pci_restore_msi_state(adapter->pdev);
3158
3159	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
3160		dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
3161			reg_val);
3162		iavf_disable_vf(adapter);
3163		return; /* Do not attempt to reinit. It's dead, Jim. */
3164	}
3165
3166continue_reset:
3167	/* If we are still early in the state machine, just restart. */
3168	if (adapter->state <= __IAVF_INIT_FAILED) {
3169		iavf_shutdown_adminq(hw);
3170		iavf_change_state(adapter, __IAVF_STARTUP);
3171		iavf_startup(adapter);
3172		queue_delayed_work(adapter->wq, &adapter->watchdog_task,
3173				   msecs_to_jiffies(30));
3174		return;
3175	}
3176
3177	/* We don't use netif_running() because it may be true prior to
3178	 * ndo_open() returning, so we can't assume it means all our open
3179	 * tasks have finished, since we're not holding the rtnl_lock here.
3180	 */
3181	running = adapter->state == __IAVF_RUNNING;
3182
3183	if (running) {
3184		netif_carrier_off(netdev);
3185		netif_tx_stop_all_queues(netdev);
3186		adapter->link_up = false;
3187		iavf_napi_disable_all(adapter);
3188	}
3189	iavf_irq_disable(adapter);
3190
3191	iavf_change_state(adapter, __IAVF_RESETTING);
3192	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3193
3194	iavf_ptp_release(adapter);
3195
3196	/* free the Tx/Rx rings and descriptors, might be better to just
3197	 * re-use them sometime in the future
3198	 */
3199	iavf_free_all_rx_resources(adapter);
3200	iavf_free_all_tx_resources(adapter);
3201
3202	adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
3203	/* kill and reinit the admin queue */
3204	iavf_shutdown_adminq(hw);
3205	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3206	status = iavf_init_adminq(hw);
3207	if (status) {
3208		dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
3209			 status);
3210		goto reset_err;
3211	}
3212	adapter->aq_required = 0;
3213
3214	if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3215	    (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3216		err = iavf_reinit_interrupt_scheme(adapter, running);
3217		if (err)
3218			goto reset_err;
3219	}
3220
3221	if (RSS_AQ(adapter)) {
3222		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3223	} else {
3224		err = iavf_init_rss(adapter);
3225		if (err)
3226			goto reset_err;
3227	}
3228
3229	adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
3230	adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
3231
3232	/* Certain capabilities require an extended negotiation process using
3233	 * extra messages that must be processed after getting the VF
3234	 * configuration. The related checks such as VLAN_V2_ALLOWED() are not
3235	 * reliable here, since the configuration has not yet been negotiated.
3236	 *
3237	 * Always set these flags, since them related VIRTCHNL messages won't
3238	 * be sent until after VIRTCHNL_OP_GET_VF_RESOURCES.
3239	 */
3240	adapter->aq_required |= IAVF_FLAG_AQ_EXTENDED_CAPS;
3241
3242	spin_lock_bh(&adapter->mac_vlan_list_lock);
3243
3244	/* Delete filter for the current MAC address, it could have
3245	 * been changed by the PF via administratively set MAC.
3246	 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
3247	 */
3248	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3249		if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
3250			list_del(&f->list);
3251			kfree(f);
3252		}
3253	}
3254	/* re-add all MAC filters */
3255	list_for_each_entry(f, &adapter->mac_filter_list, list) {
3256		f->add = true;
3257	}
3258	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3259
3260	/* check if TCs are running and re-add all cloud filters */
3261	spin_lock_bh(&adapter->cloud_filter_list_lock);
3262	if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
3263	    adapter->num_tc) {
3264		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
3265			cf->add = true;
3266		}
3267	}
3268	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3269
3270	adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
3271	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3272	iavf_misc_irq_enable(adapter);
3273
3274	mod_delayed_work(adapter->wq, &adapter->watchdog_task, 2);
3275
3276	/* We were running when the reset started, so we need to restore some
3277	 * state here.
3278	 */
3279	if (running) {
3280		/* allocate transmit descriptors */
3281		err = iavf_setup_all_tx_resources(adapter);
3282		if (err)
3283			goto reset_err;
3284
3285		/* allocate receive descriptors */
3286		err = iavf_setup_all_rx_resources(adapter);
3287		if (err)
3288			goto reset_err;
3289
3290		if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3291		    (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3292			err = iavf_request_traffic_irqs(adapter, netdev->name);
3293			if (err)
3294				goto reset_err;
3295
3296			adapter->flags &= ~IAVF_FLAG_REINIT_MSIX_NEEDED;
3297		}
3298
3299		iavf_configure(adapter);
3300
3301		/* iavf_up_complete() will switch device back
3302		 * to __IAVF_RUNNING
3303		 */
3304		iavf_up_complete(adapter);
3305
3306		iavf_irq_enable(adapter, true);
3307
3308		iavf_reconfig_qs_bw(adapter);
3309	} else {
3310		iavf_change_state(adapter, __IAVF_DOWN);
3311		wake_up(&adapter->down_waitqueue);
3312	}
3313
3314	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3315
3316	return;
3317reset_err:
3318	if (running) {
3319		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3320		iavf_free_traffic_irqs(adapter);
3321	}
3322	iavf_disable_vf(adapter);
3323
3324	dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
3325}
3326
3327static void iavf_reset_task(struct work_struct *work)
3328{
3329	struct iavf_adapter *adapter = container_of(work,
3330						      struct iavf_adapter,
3331						      reset_task);
3332	struct net_device *netdev = adapter->netdev;
3333
3334	netdev_lock(netdev);
3335	iavf_reset_step(adapter);
3336	netdev_unlock(netdev);
3337}
3338
3339/**
3340 * iavf_adminq_task - worker thread to clean the admin queue
3341 * @work: pointer to work_struct containing our data
3342 **/
3343static void iavf_adminq_task(struct work_struct *work)
3344{
3345	struct iavf_adapter *adapter =
3346		container_of(work, struct iavf_adapter, adminq_task);
3347	struct net_device *netdev = adapter->netdev;
3348	struct iavf_hw *hw = &adapter->hw;
3349	struct iavf_arq_event_info event;
3350	enum virtchnl_ops v_op;
3351	enum iavf_status ret, v_ret;
3352	u32 val, oldval;
3353	u16 pending;
3354
3355	netdev_lock(netdev);
3356
3357	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
3358		goto unlock;
3359
3360	event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
3361	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
3362	if (!event.msg_buf)
3363		goto unlock;
3364
3365	do {
3366		ret = iavf_clean_arq_element(hw, &event, &pending);
3367		v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
3368		v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
3369
3370		if (ret || !v_op)
3371			break; /* No event to process or error cleaning ARQ */
3372
3373		iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
3374					 event.msg_len);
3375		if (pending != 0)
3376			memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
3377	} while (pending);
3378
3379	if (iavf_is_reset_in_progress(adapter))
3380		goto freedom;
3381
3382	/* check for error indications */
3383	val = rd32(hw, IAVF_VF_ARQLEN1);
3384	if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
3385		goto freedom;
3386	oldval = val;
3387	if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
3388		dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
3389		val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
3390	}
3391	if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
3392		dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
3393		val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
3394	}
3395	if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
3396		dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
3397		val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
3398	}
3399	if (oldval != val)
3400		wr32(hw, IAVF_VF_ARQLEN1, val);
3401
3402	val = rd32(hw, IAVF_VF_ATQLEN1);
3403	oldval = val;
3404	if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
3405		dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
3406		val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
3407	}
3408	if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
3409		dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
3410		val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
3411	}
3412	if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
3413		dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
3414		val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
3415	}
3416	if (oldval != val)
3417		wr32(hw, IAVF_VF_ATQLEN1, val);
3418
3419freedom:
3420	kfree(event.msg_buf);
3421unlock:
3422	netdev_unlock(netdev);
3423	/* re-enable Admin queue interrupt cause */
3424	iavf_misc_irq_enable(adapter);
3425}
3426
3427/**
3428 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
3429 * @adapter: board private structure
3430 *
3431 * Free all transmit software resources
3432 **/
3433void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
3434{
3435	int i;
3436
3437	if (!adapter->tx_rings)
3438		return;
3439
3440	for (i = 0; i < adapter->num_active_queues; i++)
3441		if (adapter->tx_rings[i].desc)
3442			iavf_free_tx_resources(&adapter->tx_rings[i]);
3443}
3444
3445/**
3446 * iavf_setup_all_tx_resources - allocate all queues Tx resources
3447 * @adapter: board private structure
3448 *
3449 * If this function returns with an error, then it's possible one or
3450 * more of the rings is populated (while the rest are not).  It is the
3451 * callers duty to clean those orphaned rings.
3452 *
3453 * Return 0 on success, negative on failure
3454 **/
3455static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
3456{
3457	int i, err = 0;
3458
3459	for (i = 0; i < adapter->num_active_queues; i++) {
3460		adapter->tx_rings[i].count = adapter->tx_desc_count;
3461		err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
3462		if (!err)
3463			continue;
3464		dev_err(&adapter->pdev->dev,
3465			"Allocation for Tx Queue %u failed\n", i);
3466		break;
3467	}
3468
3469	return err;
3470}
3471
3472/**
3473 * iavf_setup_all_rx_resources - allocate all queues Rx resources
3474 * @adapter: board private structure
3475 *
3476 * If this function returns with an error, then it's possible one or
3477 * more of the rings is populated (while the rest are not).  It is the
3478 * callers duty to clean those orphaned rings.
3479 *
3480 * Return 0 on success, negative on failure
3481 **/
3482static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
3483{
3484	int i, err = 0;
3485
3486	for (i = 0; i < adapter->num_active_queues; i++) {
3487		adapter->rx_rings[i].count = adapter->rx_desc_count;
3488		err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
3489		if (!err)
3490			continue;
3491		dev_err(&adapter->pdev->dev,
3492			"Allocation for Rx Queue %u failed\n", i);
3493		break;
3494	}
3495	return err;
3496}
3497
3498/**
3499 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
3500 * @adapter: board private structure
3501 *
3502 * Free all receive software resources
3503 **/
3504void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
3505{
3506	int i;
3507
3508	if (!adapter->rx_rings)
3509		return;
3510
3511	for (i = 0; i < adapter->num_active_queues; i++)
3512		if (adapter->rx_rings[i].desc)
3513			iavf_free_rx_resources(&adapter->rx_rings[i]);
3514}
3515
3516/**
3517 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
3518 * @adapter: board private structure
3519 * @max_tx_rate: max Tx bw for a tc
3520 **/
3521static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
3522				      u64 max_tx_rate)
3523{
3524	int speed = 0, ret = 0;
3525
3526	if (ADV_LINK_SUPPORT(adapter)) {
3527		if (adapter->link_speed_mbps < U32_MAX) {
3528			speed = adapter->link_speed_mbps;
3529			goto validate_bw;
3530		} else {
3531			dev_err(&adapter->pdev->dev, "Unknown link speed\n");
3532			return -EINVAL;
3533		}
3534	}
3535
3536	switch (adapter->link_speed) {
3537	case VIRTCHNL_LINK_SPEED_40GB:
3538		speed = SPEED_40000;
3539		break;
3540	case VIRTCHNL_LINK_SPEED_25GB:
3541		speed = SPEED_25000;
3542		break;
3543	case VIRTCHNL_LINK_SPEED_20GB:
3544		speed = SPEED_20000;
3545		break;
3546	case VIRTCHNL_LINK_SPEED_10GB:
3547		speed = SPEED_10000;
3548		break;
3549	case VIRTCHNL_LINK_SPEED_5GB:
3550		speed = SPEED_5000;
3551		break;
3552	case VIRTCHNL_LINK_SPEED_2_5GB:
3553		speed = SPEED_2500;
3554		break;
3555	case VIRTCHNL_LINK_SPEED_1GB:
3556		speed = SPEED_1000;
3557		break;
3558	case VIRTCHNL_LINK_SPEED_100MB:
3559		speed = SPEED_100;
3560		break;
3561	default:
3562		break;
3563	}
3564
3565validate_bw:
3566	if (max_tx_rate > speed) {
3567		dev_err(&adapter->pdev->dev,
3568			"Invalid tx rate specified\n");
3569		ret = -EINVAL;
3570	}
3571
3572	return ret;
3573}
3574
3575/**
3576 * iavf_validate_ch_config - validate queue mapping info
3577 * @adapter: board private structure
3578 * @mqprio_qopt: queue parameters
3579 *
3580 * This function validates if the config provided by the user to
3581 * configure queue channels is valid or not. Returns 0 on a valid
3582 * config.
3583 **/
3584static int iavf_validate_ch_config(struct iavf_adapter *adapter,
3585				   struct tc_mqprio_qopt_offload *mqprio_qopt)
3586{
3587	u64 total_max_rate = 0;
3588	u32 tx_rate_rem = 0;
3589	int i, num_qps = 0;
3590	u64 tx_rate = 0;
3591	int ret = 0;
3592
3593	if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
3594	    mqprio_qopt->qopt.num_tc < 1)
3595		return -EINVAL;
3596
3597	for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
3598		if (!mqprio_qopt->qopt.count[i] ||
3599		    mqprio_qopt->qopt.offset[i] != num_qps)
3600			return -EINVAL;
3601		if (mqprio_qopt->min_rate[i]) {
3602			dev_err(&adapter->pdev->dev,
3603				"Invalid min tx rate (greater than 0) specified for TC%d\n",
3604				i);
3605			return -EINVAL;
3606		}
3607
3608		/* convert to Mbps */
3609		tx_rate = div_u64(mqprio_qopt->max_rate[i],
3610				  IAVF_MBPS_DIVISOR);
3611
3612		if (mqprio_qopt->max_rate[i] &&
3613		    tx_rate < IAVF_MBPS_QUANTA) {
3614			dev_err(&adapter->pdev->dev,
3615				"Invalid max tx rate for TC%d, minimum %dMbps\n",
3616				i, IAVF_MBPS_QUANTA);
3617			return -EINVAL;
3618		}
3619
3620		(void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
3621
3622		if (tx_rate_rem != 0) {
3623			dev_err(&adapter->pdev->dev,
3624				"Invalid max tx rate for TC%d, not divisible by %d\n",
3625				i, IAVF_MBPS_QUANTA);
3626			return -EINVAL;
3627		}
3628
3629		total_max_rate += tx_rate;
3630		num_qps += mqprio_qopt->qopt.count[i];
3631	}
3632	if (num_qps > adapter->num_active_queues) {
3633		dev_err(&adapter->pdev->dev,
3634			"Cannot support requested number of queues\n");
3635		return -EINVAL;
3636	}
3637
3638	ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
3639	return ret;
3640}
3641
3642/**
3643 * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
3644 * @adapter: board private structure
3645 **/
3646static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
3647{
3648	struct iavf_cloud_filter *cf, *cftmp;
3649
3650	spin_lock_bh(&adapter->cloud_filter_list_lock);
3651	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
3652				 list) {
3653		list_del(&cf->list);
3654		kfree(cf);
3655		adapter->num_cloud_filters--;
3656	}
3657	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3658}
3659
3660/**
3661 * iavf_is_tc_config_same - Compare the mqprio TC config with the
3662 * TC config already configured on this adapter.
3663 * @adapter: board private structure
3664 * @mqprio_qopt: TC config received from kernel.
3665 *
3666 * This function compares the TC config received from the kernel
3667 * with the config already configured on the adapter.
3668 *
3669 * Return: True if configuration is same, false otherwise.
3670 **/
3671static bool iavf_is_tc_config_same(struct iavf_adapter *adapter,
3672				   struct tc_mqprio_qopt *mqprio_qopt)
3673{
3674	struct virtchnl_channel_info *ch = &adapter->ch_config.ch_info[0];
3675	int i;
3676
3677	if (adapter->num_tc != mqprio_qopt->num_tc)
3678		return false;
3679
3680	for (i = 0; i < adapter->num_tc; i++) {
3681		if (ch[i].count != mqprio_qopt->count[i] ||
3682		    ch[i].offset != mqprio_qopt->offset[i])
3683			return false;
3684	}
3685	return true;
3686}
3687
3688/**
3689 * __iavf_setup_tc - configure multiple traffic classes
3690 * @netdev: network interface device structure
3691 * @type_data: tc offload data
3692 *
3693 * This function processes the config information provided by the
3694 * user to configure traffic classes/queue channels and packages the
3695 * information to request the PF to setup traffic classes.
3696 *
3697 * Returns 0 on success.
3698 **/
3699static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
3700{
3701	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
3702	struct iavf_adapter *adapter = netdev_priv(netdev);
3703	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3704	u8 num_tc = 0, total_qps = 0;
3705	int ret = 0, netdev_tc = 0;
3706	u64 max_tx_rate;
3707	u16 mode;
3708	int i;
3709
3710	num_tc = mqprio_qopt->qopt.num_tc;
3711	mode = mqprio_qopt->mode;
3712
3713	/* delete queue_channel */
3714	if (!mqprio_qopt->qopt.hw) {
3715		if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
3716			/* reset the tc configuration */
3717			netdev_reset_tc(netdev);
3718			adapter->num_tc = 0;
3719			netif_tx_stop_all_queues(netdev);
3720			netif_tx_disable(netdev);
3721			iavf_del_all_cloud_filters(adapter);
3722			adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
3723			total_qps = adapter->orig_num_active_queues;
3724			goto exit;
3725		} else {
3726			return -EINVAL;
3727		}
3728	}
3729
3730	/* add queue channel */
3731	if (mode == TC_MQPRIO_MODE_CHANNEL) {
3732		if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3733			dev_err(&adapter->pdev->dev, "ADq not supported\n");
3734			return -EOPNOTSUPP;
3735		}
3736		if (adapter->ch_config.state != __IAVF_TC_INVALID) {
3737			dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
3738			return -EINVAL;
3739		}
3740
3741		ret = iavf_validate_ch_config(adapter, mqprio_qopt);
3742		if (ret)
3743			return ret;
3744		/* Return if same TC config is requested */
3745		if (iavf_is_tc_config_same(adapter, &mqprio_qopt->qopt))
3746			return 0;
3747		adapter->num_tc = num_tc;
3748
3749		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3750			if (i < num_tc) {
3751				adapter->ch_config.ch_info[i].count =
3752					mqprio_qopt->qopt.count[i];
3753				adapter->ch_config.ch_info[i].offset =
3754					mqprio_qopt->qopt.offset[i];
3755				total_qps += mqprio_qopt->qopt.count[i];
3756				max_tx_rate = mqprio_qopt->max_rate[i];
3757				/* convert to Mbps */
3758				max_tx_rate = div_u64(max_tx_rate,
3759						      IAVF_MBPS_DIVISOR);
3760				adapter->ch_config.ch_info[i].max_tx_rate =
3761					max_tx_rate;
3762			} else {
3763				adapter->ch_config.ch_info[i].count = 1;
3764				adapter->ch_config.ch_info[i].offset = 0;
3765			}
3766		}
3767
3768		/* Take snapshot of original config such as "num_active_queues"
3769		 * It is used later when delete ADQ flow is exercised, so that
3770		 * once delete ADQ flow completes, VF shall go back to its
3771		 * original queue configuration
3772		 */
3773
3774		adapter->orig_num_active_queues = adapter->num_active_queues;
3775
3776		/* Store queue info based on TC so that VF gets configured
3777		 * with correct number of queues when VF completes ADQ config
3778		 * flow
3779		 */
3780		adapter->ch_config.total_qps = total_qps;
3781
3782		netif_tx_stop_all_queues(netdev);
3783		netif_tx_disable(netdev);
3784		adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
3785		netdev_reset_tc(netdev);
3786		/* Report the tc mapping up the stack */
3787		netdev_set_num_tc(adapter->netdev, num_tc);
3788		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3789			u16 qcount = mqprio_qopt->qopt.count[i];
3790			u16 qoffset = mqprio_qopt->qopt.offset[i];
3791
3792			if (i < num_tc)
3793				netdev_set_tc_queue(netdev, netdev_tc++, qcount,
3794						    qoffset);
3795		}
3796	}
3797exit:
3798	if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
3799		return 0;
3800
3801	netif_set_real_num_rx_queues(netdev, total_qps);
3802	netif_set_real_num_tx_queues(netdev, total_qps);
3803
3804	return ret;
3805}
3806
3807/**
3808 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
3809 * @adapter: board private structure
3810 * @f: pointer to struct flow_cls_offload
3811 * @filter: pointer to cloud filter structure
3812 */
3813static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
3814				 struct flow_cls_offload *f,
3815				 struct iavf_cloud_filter *filter)
3816{
3817	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3818	struct flow_dissector *dissector = rule->match.dissector;
3819	u16 n_proto_mask = 0;
3820	u16 n_proto_key = 0;
3821	u8 field_flags = 0;
3822	u16 addr_type = 0;
3823	u16 n_proto = 0;
3824	int i = 0;
3825	struct virtchnl_filter *vf = &filter->f;
3826
3827	if (dissector->used_keys &
3828	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
3829	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
3830	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
3831	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
3832	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
3833	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
3834	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
3835	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
3836		dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%llx\n",
3837			dissector->used_keys);
3838		return -EOPNOTSUPP;
3839	}
3840
3841	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
3842		struct flow_match_enc_keyid match;
3843
3844		flow_rule_match_enc_keyid(rule, &match);
3845		if (match.mask->keyid != 0)
3846			field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
3847	}
3848
3849	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
3850		struct flow_match_basic match;
3851
3852		flow_rule_match_basic(rule, &match);
3853		n_proto_key = ntohs(match.key->n_proto);
3854		n_proto_mask = ntohs(match.mask->n_proto);
3855
3856		if (n_proto_key == ETH_P_ALL) {
3857			n_proto_key = 0;
3858			n_proto_mask = 0;
3859		}
3860		n_proto = n_proto_key & n_proto_mask;
3861		if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
3862			return -EINVAL;
3863		if (n_proto == ETH_P_IPV6) {
3864			/* specify flow type as TCP IPv6 */
3865			vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
3866		}
3867
3868		if (match.key->ip_proto != IPPROTO_TCP) {
3869			dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
3870			return -EINVAL;
3871		}
3872	}
3873
3874	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
3875		struct flow_match_eth_addrs match;
3876
3877		flow_rule_match_eth_addrs(rule, &match);
3878
3879		/* use is_broadcast and is_zero to check for all 0xf or 0 */
3880		if (!is_zero_ether_addr(match.mask->dst)) {
3881			if (is_broadcast_ether_addr(match.mask->dst)) {
3882				field_flags |= IAVF_CLOUD_FIELD_OMAC;
3883			} else {
3884				dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
3885					match.mask->dst);
3886				return -EINVAL;
3887			}
3888		}
3889
3890		if (!is_zero_ether_addr(match.mask->src)) {
3891			if (is_broadcast_ether_addr(match.mask->src)) {
3892				field_flags |= IAVF_CLOUD_FIELD_IMAC;
3893			} else {
3894				dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
3895					match.mask->src);
3896				return -EINVAL;
3897			}
3898		}
3899
3900		if (!is_zero_ether_addr(match.key->dst))
3901			if (is_valid_ether_addr(match.key->dst) ||
3902			    is_multicast_ether_addr(match.key->dst)) {
3903				/* set the mask if a valid dst_mac address */
3904				for (i = 0; i < ETH_ALEN; i++)
3905					vf->mask.tcp_spec.dst_mac[i] |= 0xff;
3906				ether_addr_copy(vf->data.tcp_spec.dst_mac,
3907						match.key->dst);
3908			}
3909
3910		if (!is_zero_ether_addr(match.key->src))
3911			if (is_valid_ether_addr(match.key->src) ||
3912			    is_multicast_ether_addr(match.key->src)) {
3913				/* set the mask if a valid dst_mac address */
3914				for (i = 0; i < ETH_ALEN; i++)
3915					vf->mask.tcp_spec.src_mac[i] |= 0xff;
3916				ether_addr_copy(vf->data.tcp_spec.src_mac,
3917						match.key->src);
3918		}
3919	}
3920
3921	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
3922		struct flow_match_vlan match;
3923
3924		flow_rule_match_vlan(rule, &match);
3925		if (match.mask->vlan_id) {
3926			if (match.mask->vlan_id == VLAN_VID_MASK) {
3927				field_flags |= IAVF_CLOUD_FIELD_IVLAN;
3928			} else {
3929				dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
3930					match.mask->vlan_id);
3931				return -EINVAL;
3932			}
3933		}
3934		vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
3935		vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
3936	}
3937
3938	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
3939		struct flow_match_control match;
3940
3941		flow_rule_match_control(rule, &match);
3942		addr_type = match.key->addr_type;
3943
3944		if (flow_rule_has_control_flags(match.mask->flags,
3945						f->common.extack))
3946			return -EOPNOTSUPP;
3947	}
3948
3949	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
3950		struct flow_match_ipv4_addrs match;
3951
3952		flow_rule_match_ipv4_addrs(rule, &match);
3953		if (match.mask->dst) {
3954			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
3955				field_flags |= IAVF_CLOUD_FIELD_IIP;
3956			} else {
3957				dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
3958					be32_to_cpu(match.mask->dst));
3959				return -EINVAL;
3960			}
3961		}
3962
3963		if (match.mask->src) {
3964			if (match.mask->src == cpu_to_be32(0xffffffff)) {
3965				field_flags |= IAVF_CLOUD_FIELD_IIP;
3966			} else {
3967				dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
3968					be32_to_cpu(match.mask->src));
3969				return -EINVAL;
3970			}
3971		}
3972
3973		if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
3974			dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
3975			return -EINVAL;
3976		}
3977		if (match.key->dst) {
3978			vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
3979			vf->data.tcp_spec.dst_ip[0] = match.key->dst;
3980		}
3981		if (match.key->src) {
3982			vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
3983			vf->data.tcp_spec.src_ip[0] = match.key->src;
3984		}
3985	}
3986
3987	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
3988		struct flow_match_ipv6_addrs match;
3989
3990		flow_rule_match_ipv6_addrs(rule, &match);
3991
3992		/* validate mask, make sure it is not IPV6_ADDR_ANY */
3993		if (ipv6_addr_any(&match.mask->dst)) {
3994			dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
3995				IPV6_ADDR_ANY);
3996			return -EINVAL;
3997		}
3998
3999		/* src and dest IPv6 address should not be LOOPBACK
4000		 * (0:0:0:0:0:0:0:1) which can be represented as ::1
4001		 */
4002		if (ipv6_addr_loopback(&match.key->dst) ||
4003		    ipv6_addr_loopback(&match.key->src)) {
4004			dev_err(&adapter->pdev->dev,
4005				"ipv6 addr should not be loopback\n");
4006			return -EINVAL;
4007		}
4008		if (!ipv6_addr_any(&match.mask->dst) ||
4009		    !ipv6_addr_any(&match.mask->src))
4010			field_flags |= IAVF_CLOUD_FIELD_IIP;
4011
4012		for (i = 0; i < 4; i++)
4013			vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
4014		memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
4015		       sizeof(vf->data.tcp_spec.dst_ip));
4016		for (i = 0; i < 4; i++)
4017			vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
4018		memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
4019		       sizeof(vf->data.tcp_spec.src_ip));
4020	}
4021	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
4022		struct flow_match_ports match;
4023
4024		flow_rule_match_ports(rule, &match);
4025		if (match.mask->src) {
4026			if (match.mask->src == cpu_to_be16(0xffff)) {
4027				field_flags |= IAVF_CLOUD_FIELD_IIP;
4028			} else {
4029				dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
4030					be16_to_cpu(match.mask->src));
4031				return -EINVAL;
4032			}
4033		}
4034
4035		if (match.mask->dst) {
4036			if (match.mask->dst == cpu_to_be16(0xffff)) {
4037				field_flags |= IAVF_CLOUD_FIELD_IIP;
4038			} else {
4039				dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
4040					be16_to_cpu(match.mask->dst));
4041				return -EINVAL;
4042			}
4043		}
4044		if (match.key->dst) {
4045			vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
4046			vf->data.tcp_spec.dst_port = match.key->dst;
4047		}
4048
4049		if (match.key->src) {
4050			vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
4051			vf->data.tcp_spec.src_port = match.key->src;
4052		}
4053	}
4054	vf->field_flags = field_flags;
4055
4056	return 0;
4057}
4058
4059/**
4060 * iavf_handle_tclass - Forward to a traffic class on the device
4061 * @adapter: board private structure
4062 * @tc: traffic class index on the device
4063 * @filter: pointer to cloud filter structure
4064 */
4065static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
4066			      struct iavf_cloud_filter *filter)
4067{
4068	if (tc == 0)
4069		return 0;
4070	if (tc < adapter->num_tc) {
4071		if (!filter->f.data.tcp_spec.dst_port) {
4072			dev_err(&adapter->pdev->dev,
4073				"Specify destination port to redirect to traffic class other than TC0\n");
4074			return -EINVAL;
4075		}
4076	}
4077	/* redirect to a traffic class on the same device */
4078	filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
4079	filter->f.action_meta = tc;
4080	return 0;
4081}
4082
4083/**
4084 * iavf_find_cf - Find the cloud filter in the list
4085 * @adapter: Board private structure
4086 * @cookie: filter specific cookie
4087 *
4088 * Returns ptr to the filter object or NULL. Must be called while holding the
4089 * cloud_filter_list_lock.
4090 */
4091static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
4092					      unsigned long *cookie)
4093{
4094	struct iavf_cloud_filter *filter = NULL;
4095
4096	if (!cookie)
4097		return NULL;
4098
4099	list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
4100		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
4101			return filter;
4102	}
4103	return NULL;
4104}
4105
4106/**
4107 * iavf_configure_clsflower - Add tc flower filters
4108 * @adapter: board private structure
4109 * @cls_flower: Pointer to struct flow_cls_offload
4110 */
4111static int iavf_configure_clsflower(struct iavf_adapter *adapter,
4112				    struct flow_cls_offload *cls_flower)
4113{
4114	int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
4115	struct iavf_cloud_filter *filter;
4116	int err;
4117
4118	if (tc < 0) {
4119		dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
4120		return -EINVAL;
4121	}
4122
4123	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
4124	if (!filter)
4125		return -ENOMEM;
4126	filter->cookie = cls_flower->cookie;
4127
4128	netdev_lock(adapter->netdev);
4129
4130	/* bail out here if filter already exists */
4131	spin_lock_bh(&adapter->cloud_filter_list_lock);
4132	if (iavf_find_cf(adapter, &cls_flower->cookie)) {
4133		dev_err(&adapter->pdev->dev, "Failed to add TC Flower filter, it already exists\n");
4134		err = -EEXIST;
4135		goto spin_unlock;
4136	}
4137	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4138
4139	/* set the mask to all zeroes to begin with */
4140	memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
4141	/* start out with flow type and eth type IPv4 to begin with */
4142	filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
4143	err = iavf_parse_cls_flower(adapter, cls_flower, filter);
4144	if (err)
4145		goto err;
4146
4147	err = iavf_handle_tclass(adapter, tc, filter);
4148	if (err)
4149		goto err;
4150
4151	/* add filter to the list */
4152	spin_lock_bh(&adapter->cloud_filter_list_lock);
4153	list_add_tail(&filter->list, &adapter->cloud_filter_list);
4154	adapter->num_cloud_filters++;
4155	filter->add = true;
4156	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
4157spin_unlock:
4158	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4159err:
4160	if (err)
4161		kfree(filter);
4162
4163	netdev_unlock(adapter->netdev);
4164	return err;
4165}
4166
4167/**
4168 * iavf_delete_clsflower - Remove tc flower filters
4169 * @adapter: board private structure
4170 * @cls_flower: Pointer to struct flow_cls_offload
4171 */
4172static int iavf_delete_clsflower(struct iavf_adapter *adapter,
4173				 struct flow_cls_offload *cls_flower)
4174{
4175	struct iavf_cloud_filter *filter = NULL;
4176	int err = 0;
4177
4178	spin_lock_bh(&adapter->cloud_filter_list_lock);
4179	filter = iavf_find_cf(adapter, &cls_flower->cookie);
4180	if (filter) {
4181		filter->del = true;
4182		adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
4183	} else {
4184		err = -EINVAL;
4185	}
4186	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4187
4188	return err;
4189}
4190
4191/**
4192 * iavf_setup_tc_cls_flower - flower classifier offloads
4193 * @adapter: pointer to iavf adapter structure
4194 * @cls_flower: pointer to flow_cls_offload struct with flow info
4195 */
4196static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
4197				    struct flow_cls_offload *cls_flower)
4198{
4199	switch (cls_flower->command) {
4200	case FLOW_CLS_REPLACE:
4201		return iavf_configure_clsflower(adapter, cls_flower);
4202	case FLOW_CLS_DESTROY:
4203		return iavf_delete_clsflower(adapter, cls_flower);
4204	case FLOW_CLS_STATS:
4205		return -EOPNOTSUPP;
4206	default:
4207		return -EOPNOTSUPP;
4208	}
4209}
4210
4211/**
4212 * iavf_add_cls_u32 - Add U32 classifier offloads
4213 * @adapter: pointer to iavf adapter structure
4214 * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4215 *
4216 * Return: 0 on success or negative errno on failure.
4217 */
4218static int iavf_add_cls_u32(struct iavf_adapter *adapter,
4219			    struct tc_cls_u32_offload *cls_u32)
4220{
4221	struct netlink_ext_ack *extack = cls_u32->common.extack;
4222	struct virtchnl_fdir_rule *rule_cfg;
4223	struct virtchnl_filter_action *vact;
4224	struct virtchnl_proto_hdrs *hdrs;
4225	struct ethhdr *spec_h, *mask_h;
4226	const struct tc_action *act;
4227	struct iavf_fdir_fltr *fltr;
4228	struct tcf_exts *exts;
4229	unsigned int q_index;
4230	int i, status = 0;
4231	int off_base = 0;
4232
4233	if (cls_u32->knode.link_handle) {
4234		NL_SET_ERR_MSG_MOD(extack, "Linking not supported");
4235		return -EOPNOTSUPP;
4236	}
4237
4238	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
4239	if (!fltr)
4240		return -ENOMEM;
4241
4242	rule_cfg = &fltr->vc_add_msg.rule_cfg;
4243	hdrs = &rule_cfg->proto_hdrs;
4244	hdrs->count = 0;
4245
4246	/* The parser lib at the PF expects the packet starting with MAC hdr */
4247	switch (ntohs(cls_u32->common.protocol)) {
4248	case ETH_P_802_3:
4249		break;
4250	case ETH_P_IP:
4251		spec_h = (struct ethhdr *)hdrs->raw.spec;
4252		mask_h = (struct ethhdr *)hdrs->raw.mask;
4253		spec_h->h_proto = htons(ETH_P_IP);
4254		mask_h->h_proto = htons(0xFFFF);
4255		off_base += ETH_HLEN;
4256		break;
4257	default:
4258		NL_SET_ERR_MSG_MOD(extack, "Only 802_3 and ip filter protocols are supported");
4259		status = -EOPNOTSUPP;
4260		goto free_alloc;
4261	}
4262
4263	for (i = 0; i < cls_u32->knode.sel->nkeys; i++) {
4264		__be32 val, mask;
4265		int off;
4266
4267		off = off_base + cls_u32->knode.sel->keys[i].off;
4268		val = cls_u32->knode.sel->keys[i].val;
4269		mask = cls_u32->knode.sel->keys[i].mask;
4270
4271		if (off >= sizeof(hdrs->raw.spec)) {
4272			NL_SET_ERR_MSG_MOD(extack, "Input exceeds maximum allowed.");
4273			status = -EINVAL;
4274			goto free_alloc;
4275		}
4276
4277		memcpy(&hdrs->raw.spec[off], &val, sizeof(val));
4278		memcpy(&hdrs->raw.mask[off], &mask, sizeof(mask));
4279		hdrs->raw.pkt_len = off + sizeof(val);
4280	}
4281
4282	/* Only one action is allowed */
4283	rule_cfg->action_set.count = 1;
4284	vact = &rule_cfg->action_set.actions[0];
4285	exts = cls_u32->knode.exts;
4286
4287	tcf_exts_for_each_action(i, act, exts) {
4288		/* FDIR queue */
4289		if (is_tcf_skbedit_rx_queue_mapping(act)) {
4290			q_index = tcf_skbedit_rx_queue_mapping(act);
4291			if (q_index >= adapter->num_active_queues) {
4292				status = -EINVAL;
4293				goto free_alloc;
4294			}
4295
4296			vact->type = VIRTCHNL_ACTION_QUEUE;
4297			vact->act_conf.queue.index = q_index;
4298			break;
4299		}
4300
4301		/* Drop */
4302		if (is_tcf_gact_shot(act)) {
4303			vact->type = VIRTCHNL_ACTION_DROP;
4304			break;
4305		}
4306
4307		/* Unsupported action */
4308		NL_SET_ERR_MSG_MOD(extack, "Unsupported action.");
4309		status = -EOPNOTSUPP;
4310		goto free_alloc;
4311	}
4312
4313	fltr->vc_add_msg.vsi_id = adapter->vsi.id;
4314	fltr->cls_u32_handle = cls_u32->knode.handle;
4315	return iavf_fdir_add_fltr(adapter, fltr);
4316
4317free_alloc:
4318	kfree(fltr);
4319	return status;
4320}
4321
4322/**
4323 * iavf_del_cls_u32 - Delete U32 classifier offloads
4324 * @adapter: pointer to iavf adapter structure
4325 * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4326 *
4327 * Return: 0 on success or negative errno on failure.
4328 */
4329static int iavf_del_cls_u32(struct iavf_adapter *adapter,
4330			    struct tc_cls_u32_offload *cls_u32)
4331{
4332	return iavf_fdir_del_fltr(adapter, true, cls_u32->knode.handle);
4333}
4334
4335/**
4336 * iavf_setup_tc_cls_u32 - U32 filter offloads
4337 * @adapter: pointer to iavf adapter structure
4338 * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4339 *
4340 * Return: 0 on success or negative errno on failure.
4341 */
4342static int iavf_setup_tc_cls_u32(struct iavf_adapter *adapter,
4343				 struct tc_cls_u32_offload *cls_u32)
4344{
4345	if (!TC_U32_SUPPORT(adapter) || !FDIR_FLTR_SUPPORT(adapter))
4346		return -EOPNOTSUPP;
4347
4348	switch (cls_u32->command) {
4349	case TC_CLSU32_NEW_KNODE:
4350	case TC_CLSU32_REPLACE_KNODE:
4351		return iavf_add_cls_u32(adapter, cls_u32);
4352	case TC_CLSU32_DELETE_KNODE:
4353		return iavf_del_cls_u32(adapter, cls_u32);
4354	default:
4355		return -EOPNOTSUPP;
4356	}
4357}
4358
4359/**
4360 * iavf_setup_tc_block_cb - block callback for tc
4361 * @type: type of offload
4362 * @type_data: offload data
4363 * @cb_priv:
4364 *
4365 * This function is the block callback for traffic classes
4366 **/
4367static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4368				  void *cb_priv)
4369{
4370	struct iavf_adapter *adapter = cb_priv;
4371
4372	if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
4373		return -EOPNOTSUPP;
4374
4375	switch (type) {
4376	case TC_SETUP_CLSFLOWER:
4377		return iavf_setup_tc_cls_flower(cb_priv, type_data);
4378	case TC_SETUP_CLSU32:
4379		return iavf_setup_tc_cls_u32(cb_priv, type_data);
4380	default:
4381		return -EOPNOTSUPP;
4382	}
4383}
4384
4385static LIST_HEAD(iavf_block_cb_list);
4386
4387/**
4388 * iavf_setup_tc - configure multiple traffic classes
4389 * @netdev: network interface device structure
4390 * @type: type of offload
4391 * @type_data: tc offload data
4392 *
4393 * This function is the callback to ndo_setup_tc in the
4394 * netdev_ops.
4395 *
4396 * Returns 0 on success
4397 **/
4398static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4399			 void *type_data)
4400{
4401	struct iavf_adapter *adapter = netdev_priv(netdev);
4402
4403	switch (type) {
4404	case TC_SETUP_QDISC_MQPRIO:
4405		return __iavf_setup_tc(netdev, type_data);
4406	case TC_SETUP_BLOCK:
4407		return flow_block_cb_setup_simple(type_data,
4408						  &iavf_block_cb_list,
4409						  iavf_setup_tc_block_cb,
4410						  adapter, adapter, true);
4411	default:
4412		return -EOPNOTSUPP;
4413	}
4414}
4415
4416/**
4417 * iavf_restore_fdir_filters
4418 * @adapter: board private structure
4419 *
4420 * Restore existing FDIR filters when VF netdev comes back up.
4421 **/
4422static void iavf_restore_fdir_filters(struct iavf_adapter *adapter)
4423{
4424	struct iavf_fdir_fltr *f;
4425
4426	spin_lock_bh(&adapter->fdir_fltr_lock);
4427	list_for_each_entry(f, &adapter->fdir_list_head, list) {
4428		if (f->state == IAVF_FDIR_FLTR_DIS_REQUEST) {
4429			/* Cancel a request, keep filter as active */
4430			f->state = IAVF_FDIR_FLTR_ACTIVE;
4431		} else if (f->state == IAVF_FDIR_FLTR_DIS_PENDING ||
4432			   f->state == IAVF_FDIR_FLTR_INACTIVE) {
4433			/* Add filters which are inactive or have a pending
4434			 * request to PF to be deleted
4435			 */
4436			f->state = IAVF_FDIR_FLTR_ADD_REQUEST;
4437			adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
4438		}
4439	}
4440	spin_unlock_bh(&adapter->fdir_fltr_lock);
4441}
4442
4443/**
4444 * iavf_open - Called when a network interface is made active
4445 * @netdev: network interface device structure
4446 *
4447 * Returns 0 on success, negative value on failure
4448 *
4449 * The open entry point is called when a network interface is made
4450 * active by the system (IFF_UP).  At this point all resources needed
4451 * for transmit and receive operations are allocated, the interrupt
4452 * handler is registered with the OS, the watchdog is started,
4453 * and the stack is notified that the interface is ready.
4454 **/
4455static int iavf_open(struct net_device *netdev)
4456{
4457	struct iavf_adapter *adapter = netdev_priv(netdev);
4458	int err;
4459
4460	netdev_assert_locked(netdev);
4461
4462	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
4463		dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
4464		return -EIO;
4465	}
4466
4467	if (adapter->state != __IAVF_DOWN)
4468		return -EBUSY;
4469
4470	if (adapter->state == __IAVF_RUNNING &&
4471	    !test_bit(__IAVF_VSI_DOWN, adapter->vsi.state)) {
4472		dev_dbg(&adapter->pdev->dev, "VF is already open.\n");
4473		return 0;
4474	}
4475
4476	/* allocate transmit descriptors */
4477	err = iavf_setup_all_tx_resources(adapter);
4478	if (err)
4479		goto err_setup_tx;
4480
4481	/* allocate receive descriptors */
4482	err = iavf_setup_all_rx_resources(adapter);
4483	if (err)
4484		goto err_setup_rx;
4485
4486	/* clear any pending interrupts, may auto mask */
4487	err = iavf_request_traffic_irqs(adapter, netdev->name);
4488	if (err)
4489		goto err_req_irq;
4490
4491	spin_lock_bh(&adapter->mac_vlan_list_lock);
4492	iavf_add_filter(adapter, adapter->hw.mac.addr);
4493	spin_unlock_bh(&adapter->mac_vlan_list_lock);
4494
4495	/* Restore filters that were removed with IFF_DOWN */
4496	iavf_restore_filters(adapter);
4497	iavf_restore_fdir_filters(adapter);
4498
4499	iavf_configure(adapter);
4500
4501	iavf_up_complete(adapter);
4502
4503	iavf_irq_enable(adapter, true);
4504
4505	return 0;
4506
4507err_req_irq:
4508	iavf_down(adapter);
4509	iavf_free_traffic_irqs(adapter);
4510err_setup_rx:
4511	iavf_free_all_rx_resources(adapter);
4512err_setup_tx:
4513	iavf_free_all_tx_resources(adapter);
4514
4515	return err;
4516}
4517
4518/**
4519 * iavf_close - Disables a network interface
4520 * @netdev: network interface device structure
4521 *
4522 * Returns 0, this is not allowed to fail
4523 *
4524 * The close entry point is called when an interface is de-activated
4525 * by the OS.  The hardware is still under the drivers control, but
4526 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
4527 * are freed, along with all transmit and receive resources.
4528 **/
4529static int iavf_close(struct net_device *netdev)
4530{
4531	struct iavf_adapter *adapter = netdev_priv(netdev);
4532	u64 aq_to_restore;
4533	int status;
4534
4535	netdev_assert_locked(netdev);
4536
4537	if (adapter->state <= __IAVF_DOWN_PENDING)
4538		return 0;
4539
4540	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
4541	/* We cannot send IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS before
4542	 * IAVF_FLAG_AQ_DISABLE_QUEUES because in such case there is rtnl
4543	 * deadlock with adminq_task() until iavf_close timeouts. We must send
4544	 * IAVF_FLAG_AQ_GET_CONFIG before IAVF_FLAG_AQ_DISABLE_QUEUES to make
4545	 * disable queues possible for vf. Give only necessary flags to
4546	 * iavf_down and save other to set them right before iavf_close()
4547	 * returns, when IAVF_FLAG_AQ_DISABLE_QUEUES will be already sent and
4548	 * iavf will be in DOWN state.
4549	 */
4550	aq_to_restore = adapter->aq_required;
4551	adapter->aq_required &= IAVF_FLAG_AQ_GET_CONFIG;
4552
4553	/* Remove flags which we do not want to send after close or we want to
4554	 * send before disable queues.
4555	 */
4556	aq_to_restore &= ~(IAVF_FLAG_AQ_GET_CONFIG		|
4557			   IAVF_FLAG_AQ_ENABLE_QUEUES		|
4558			   IAVF_FLAG_AQ_CONFIGURE_QUEUES	|
4559			   IAVF_FLAG_AQ_ADD_VLAN_FILTER		|
4560			   IAVF_FLAG_AQ_ADD_MAC_FILTER		|
4561			   IAVF_FLAG_AQ_ADD_CLOUD_FILTER	|
4562			   IAVF_FLAG_AQ_ADD_FDIR_FILTER		|
4563			   IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
4564
4565	iavf_down(adapter);
4566	iavf_change_state(adapter, __IAVF_DOWN_PENDING);
4567	iavf_free_traffic_irqs(adapter);
4568
4569	netdev_unlock(netdev);
4570
4571	/* We explicitly don't free resources here because the hardware is
4572	 * still active and can DMA into memory. Resources are cleared in
4573	 * iavf_virtchnl_completion() after we get confirmation from the PF
4574	 * driver that the rings have been stopped.
4575	 *
4576	 * Also, we wait for state to transition to __IAVF_DOWN before
4577	 * returning. State change occurs in iavf_virtchnl_completion() after
4578	 * VF resources are released (which occurs after PF driver processes and
4579	 * responds to admin queue commands).
4580	 */
4581
4582	status = wait_event_timeout(adapter->down_waitqueue,
4583				    adapter->state == __IAVF_DOWN,
4584				    msecs_to_jiffies(500));
4585	if (!status)
4586		netdev_warn(netdev, "Device resources not yet released\n");
4587	netdev_lock(netdev);
4588
4589	adapter->aq_required |= aq_to_restore;
4590
4591	return 0;
4592}
4593
4594/**
4595 * iavf_change_mtu - Change the Maximum Transfer Unit
4596 * @netdev: network interface device structure
4597 * @new_mtu: new value for maximum frame size
4598 *
4599 * Returns 0 on success, negative on failure
4600 **/
4601static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
4602{
4603	struct iavf_adapter *adapter = netdev_priv(netdev);
4604
4605	netdev_dbg(netdev, "changing MTU from %d to %d\n",
4606		   netdev->mtu, new_mtu);
4607	WRITE_ONCE(netdev->mtu, new_mtu);
4608
4609	if (netif_running(netdev)) {
4610		adapter->flags |= IAVF_FLAG_RESET_NEEDED;
4611		iavf_reset_step(adapter);
4612	}
4613
4614	return 0;
4615}
4616
4617/**
4618 * iavf_disable_fdir - disable Flow Director and clear existing filters
4619 * @adapter: board private structure
4620 **/
4621static void iavf_disable_fdir(struct iavf_adapter *adapter)
4622{
4623	struct iavf_fdir_fltr *fdir, *fdirtmp;
4624	bool del_filters = false;
4625
4626	adapter->flags &= ~IAVF_FLAG_FDIR_ENABLED;
4627
4628	/* remove all Flow Director filters */
4629	spin_lock_bh(&adapter->fdir_fltr_lock);
4630	list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head,
4631				 list) {
4632		if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST ||
4633		    fdir->state == IAVF_FDIR_FLTR_INACTIVE) {
4634			/* Delete filters not registered in PF */
4635			list_del(&fdir->list);
4636			iavf_dec_fdir_active_fltr(adapter, fdir);
4637			kfree(fdir);
4638		} else if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING ||
4639			   fdir->state == IAVF_FDIR_FLTR_DIS_REQUEST ||
4640			   fdir->state == IAVF_FDIR_FLTR_ACTIVE) {
4641			/* Filters registered in PF, schedule their deletion */
4642			fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
4643			del_filters = true;
4644		} else if (fdir->state == IAVF_FDIR_FLTR_DIS_PENDING) {
4645			/* Request to delete filter already sent to PF, change
4646			 * state to DEL_PENDING to delete filter after PF's
4647			 * response, not set as INACTIVE
4648			 */
4649			fdir->state = IAVF_FDIR_FLTR_DEL_PENDING;
4650		}
4651	}
4652	spin_unlock_bh(&adapter->fdir_fltr_lock);
4653
4654	if (del_filters) {
4655		adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
4656		mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
4657	}
4658}
4659
4660#define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
4661					 NETIF_F_HW_VLAN_CTAG_TX | \
4662					 NETIF_F_HW_VLAN_STAG_RX | \
4663					 NETIF_F_HW_VLAN_STAG_TX)
4664
4665/**
4666 * iavf_set_features - set the netdev feature flags
4667 * @netdev: ptr to the netdev being adjusted
4668 * @features: the feature set that the stack is suggesting
4669 * Note: expects to be called while under rtnl_lock()
4670 **/
4671static int iavf_set_features(struct net_device *netdev,
4672			     netdev_features_t features)
4673{
4674	struct iavf_adapter *adapter = netdev_priv(netdev);
4675
4676	/* trigger update on any VLAN feature change */
4677	if ((netdev->features & NETIF_VLAN_OFFLOAD_FEATURES) ^
4678	    (features & NETIF_VLAN_OFFLOAD_FEATURES))
4679		iavf_set_vlan_offload_features(adapter, netdev->features,
4680					       features);
4681	if (CRC_OFFLOAD_ALLOWED(adapter) &&
4682	    ((netdev->features & NETIF_F_RXFCS) ^ (features & NETIF_F_RXFCS)))
4683		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
4684
4685	if ((netdev->features & NETIF_F_NTUPLE) ^ (features & NETIF_F_NTUPLE)) {
4686		if (features & NETIF_F_NTUPLE)
4687			adapter->flags |= IAVF_FLAG_FDIR_ENABLED;
4688		else
4689			iavf_disable_fdir(adapter);
4690	}
4691
4692	return 0;
4693}
4694
4695/**
4696 * iavf_features_check - Validate encapsulated packet conforms to limits
4697 * @skb: skb buff
4698 * @dev: This physical port's netdev
4699 * @features: Offload features that the stack believes apply
4700 **/
4701static netdev_features_t iavf_features_check(struct sk_buff *skb,
4702					     struct net_device *dev,
4703					     netdev_features_t features)
4704{
4705	size_t len;
4706
4707	/* No point in doing any of this if neither checksum nor GSO are
4708	 * being requested for this frame.  We can rule out both by just
4709	 * checking for CHECKSUM_PARTIAL
4710	 */
4711	if (skb->ip_summed != CHECKSUM_PARTIAL)
4712		return features;
4713
4714	/* We cannot support GSO if the MSS is going to be less than
4715	 * 64 bytes.  If it is then we need to drop support for GSO.
4716	 */
4717	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4718		features &= ~NETIF_F_GSO_MASK;
4719
4720	/* MACLEN can support at most 63 words */
4721	len = skb_network_offset(skb);
4722	if (len & ~(63 * 2))
4723		goto out_err;
4724
4725	/* IPLEN and EIPLEN can support at most 127 dwords */
4726	len = skb_network_header_len(skb);
4727	if (len & ~(127 * 4))
4728		goto out_err;
4729
4730	if (skb->encapsulation) {
4731		/* L4TUNLEN can support 127 words */
4732		len = skb_inner_network_header(skb) - skb_transport_header(skb);
4733		if (len & ~(127 * 2))
4734			goto out_err;
4735
4736		/* IPLEN can support at most 127 dwords */
4737		len = skb_inner_transport_header(skb) -
4738		      skb_inner_network_header(skb);
4739		if (len & ~(127 * 4))
4740			goto out_err;
4741	}
4742
4743	/* No need to validate L4LEN as TCP is the only protocol with a
4744	 * flexible value and we support all possible values supported
4745	 * by TCP, which is at most 15 dwords
4746	 */
4747
4748	return features;
4749out_err:
4750	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4751}
4752
4753/**
4754 * iavf_get_netdev_vlan_hw_features - get NETDEV VLAN features that can toggle on/off
4755 * @adapter: board private structure
4756 *
4757 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4758 * were negotiated determine the VLAN features that can be toggled on and off.
4759 **/
4760static netdev_features_t
4761iavf_get_netdev_vlan_hw_features(struct iavf_adapter *adapter)
4762{
4763	netdev_features_t hw_features = 0;
4764
4765	if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4766		return hw_features;
4767
4768	/* Enable VLAN features if supported */
4769	if (VLAN_ALLOWED(adapter)) {
4770		hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
4771				NETIF_F_HW_VLAN_CTAG_RX);
4772	} else if (VLAN_V2_ALLOWED(adapter)) {
4773		struct virtchnl_vlan_caps *vlan_v2_caps =
4774			&adapter->vlan_v2_caps;
4775		struct virtchnl_vlan_supported_caps *stripping_support =
4776			&vlan_v2_caps->offloads.stripping_support;
4777		struct virtchnl_vlan_supported_caps *insertion_support =
4778			&vlan_v2_caps->offloads.insertion_support;
4779
4780		if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4781		    stripping_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4782			if (stripping_support->outer &
4783			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4784				hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4785			if (stripping_support->outer &
4786			    VIRTCHNL_VLAN_ETHERTYPE_88A8)
4787				hw_features |= NETIF_F_HW_VLAN_STAG_RX;
4788		} else if (stripping_support->inner !=
4789			   VIRTCHNL_VLAN_UNSUPPORTED &&
4790			   stripping_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4791			if (stripping_support->inner &
4792			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4793				hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4794		}
4795
4796		if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4797		    insertion_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4798			if (insertion_support->outer &
4799			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4800				hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4801			if (insertion_support->outer &
4802			    VIRTCHNL_VLAN_ETHERTYPE_88A8)
4803				hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4804		} else if (insertion_support->inner &&
4805			   insertion_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4806			if (insertion_support->inner &
4807			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4808				hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4809		}
4810	}
4811
4812	if (CRC_OFFLOAD_ALLOWED(adapter))
4813		hw_features |= NETIF_F_RXFCS;
4814
4815	return hw_features;
4816}
4817
4818/**
4819 * iavf_get_netdev_vlan_features - get the enabled NETDEV VLAN fetures
4820 * @adapter: board private structure
4821 *
4822 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4823 * were negotiated determine the VLAN features that are enabled by default.
4824 **/
4825static netdev_features_t
4826iavf_get_netdev_vlan_features(struct iavf_adapter *adapter)
4827{
4828	netdev_features_t features = 0;
4829
4830	if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4831		return features;
4832
4833	if (VLAN_ALLOWED(adapter)) {
4834		features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4835			NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
4836	} else if (VLAN_V2_ALLOWED(adapter)) {
4837		struct virtchnl_vlan_caps *vlan_v2_caps =
4838			&adapter->vlan_v2_caps;
4839		struct virtchnl_vlan_supported_caps *filtering_support =
4840			&vlan_v2_caps->filtering.filtering_support;
4841		struct virtchnl_vlan_supported_caps *stripping_support =
4842			&vlan_v2_caps->offloads.stripping_support;
4843		struct virtchnl_vlan_supported_caps *insertion_support =
4844			&vlan_v2_caps->offloads.insertion_support;
4845		u32 ethertype_init;
4846
4847		/* give priority to outer stripping and don't support both outer
4848		 * and inner stripping
4849		 */
4850		ethertype_init = vlan_v2_caps->offloads.ethertype_init;
4851		if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4852			if (stripping_support->outer &
4853			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4854			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4855				features |= NETIF_F_HW_VLAN_CTAG_RX;
4856			else if (stripping_support->outer &
4857				 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4858				 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4859				features |= NETIF_F_HW_VLAN_STAG_RX;
4860		} else if (stripping_support->inner !=
4861			   VIRTCHNL_VLAN_UNSUPPORTED) {
4862			if (stripping_support->inner &
4863			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4864			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4865				features |= NETIF_F_HW_VLAN_CTAG_RX;
4866		}
4867
4868		/* give priority to outer insertion and don't support both outer
4869		 * and inner insertion
4870		 */
4871		if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4872			if (insertion_support->outer &
4873			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4874			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4875				features |= NETIF_F_HW_VLAN_CTAG_TX;
4876			else if (insertion_support->outer &
4877				 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4878				 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4879				features |= NETIF_F_HW_VLAN_STAG_TX;
4880		} else if (insertion_support->inner !=
4881			   VIRTCHNL_VLAN_UNSUPPORTED) {
4882			if (insertion_support->inner &
4883			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4884			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4885				features |= NETIF_F_HW_VLAN_CTAG_TX;
4886		}
4887
4888		/* give priority to outer filtering and don't bother if both
4889		 * outer and inner filtering are enabled
4890		 */
4891		ethertype_init = vlan_v2_caps->filtering.ethertype_init;
4892		if (filtering_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4893			if (filtering_support->outer &
4894			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4895			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4896				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4897			if (filtering_support->outer &
4898			    VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4899			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4900				features |= NETIF_F_HW_VLAN_STAG_FILTER;
4901		} else if (filtering_support->inner !=
4902			   VIRTCHNL_VLAN_UNSUPPORTED) {
4903			if (filtering_support->inner &
4904			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4905			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4906				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4907			if (filtering_support->inner &
4908			    VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4909			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4910				features |= NETIF_F_HW_VLAN_STAG_FILTER;
4911		}
4912	}
4913
4914	return features;
4915}
4916
4917#define IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested, allowed, feature_bit) \
4918	(!(((requested) & (feature_bit)) && \
4919	   !((allowed) & (feature_bit))))
4920
4921/**
4922 * iavf_fix_netdev_vlan_features - fix NETDEV VLAN features based on support
4923 * @adapter: board private structure
4924 * @requested_features: stack requested NETDEV features
4925 **/
4926static netdev_features_t
4927iavf_fix_netdev_vlan_features(struct iavf_adapter *adapter,
4928			      netdev_features_t requested_features)
4929{
4930	netdev_features_t allowed_features;
4931
4932	allowed_features = iavf_get_netdev_vlan_hw_features(adapter) |
4933		iavf_get_netdev_vlan_features(adapter);
4934
4935	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4936					      allowed_features,
4937					      NETIF_F_HW_VLAN_CTAG_TX))
4938		requested_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4939
4940	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4941					      allowed_features,
4942					      NETIF_F_HW_VLAN_CTAG_RX))
4943		requested_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4944
4945	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4946					      allowed_features,
4947					      NETIF_F_HW_VLAN_STAG_TX))
4948		requested_features &= ~NETIF_F_HW_VLAN_STAG_TX;
4949	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4950					      allowed_features,
4951					      NETIF_F_HW_VLAN_STAG_RX))
4952		requested_features &= ~NETIF_F_HW_VLAN_STAG_RX;
4953
4954	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4955					      allowed_features,
4956					      NETIF_F_HW_VLAN_CTAG_FILTER))
4957		requested_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4958
4959	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4960					      allowed_features,
4961					      NETIF_F_HW_VLAN_STAG_FILTER))
4962		requested_features &= ~NETIF_F_HW_VLAN_STAG_FILTER;
4963
4964	if ((requested_features &
4965	     (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
4966	    (requested_features &
4967	     (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX)) &&
4968	    adapter->vlan_v2_caps.offloads.ethertype_match ==
4969	    VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION) {
4970		netdev_warn(adapter->netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
4971		requested_features &= ~(NETIF_F_HW_VLAN_STAG_RX |
4972					NETIF_F_HW_VLAN_STAG_TX);
4973	}
4974
4975	return requested_features;
4976}
4977
4978/**
4979 * iavf_fix_strip_features - fix NETDEV CRC and VLAN strip features
4980 * @adapter: board private structure
4981 * @requested_features: stack requested NETDEV features
4982 *
4983 * Returns fixed-up features bits
4984 **/
4985static netdev_features_t
4986iavf_fix_strip_features(struct iavf_adapter *adapter,
4987			netdev_features_t requested_features)
4988{
4989	struct net_device *netdev = adapter->netdev;
4990	bool crc_offload_req, is_vlan_strip;
4991	netdev_features_t vlan_strip;
4992	int num_non_zero_vlan;
4993
4994	crc_offload_req = CRC_OFFLOAD_ALLOWED(adapter) &&
4995			  (requested_features & NETIF_F_RXFCS);
4996	num_non_zero_vlan = iavf_get_num_vlans_added(adapter);
4997	vlan_strip = (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX);
4998	is_vlan_strip = requested_features & vlan_strip;
4999
5000	if (!crc_offload_req)
5001		return requested_features;
5002
5003	if (!num_non_zero_vlan && (netdev->features & vlan_strip) &&
5004	    !(netdev->features & NETIF_F_RXFCS) && is_vlan_strip) {
5005		requested_features &= ~vlan_strip;
5006		netdev_info(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
5007		return requested_features;
5008	}
5009
5010	if ((netdev->features & NETIF_F_RXFCS) && is_vlan_strip) {
5011		requested_features &= ~vlan_strip;
5012		if (!(netdev->features & vlan_strip))
5013			netdev_info(netdev, "To enable VLAN stripping, first need to enable FCS/CRC stripping");
5014
5015		return requested_features;
5016	}
5017
5018	if (num_non_zero_vlan && is_vlan_strip &&
5019	    !(netdev->features & NETIF_F_RXFCS)) {
5020		requested_features &= ~NETIF_F_RXFCS;
5021		netdev_info(netdev, "To disable FCS/CRC stripping, first need to disable VLAN stripping");
5022	}
5023
5024	return requested_features;
5025}
5026
5027/**
5028 * iavf_fix_features - fix up the netdev feature bits
5029 * @netdev: our net device
5030 * @features: desired feature bits
5031 *
5032 * Returns fixed-up features bits
5033 **/
5034static netdev_features_t iavf_fix_features(struct net_device *netdev,
5035					   netdev_features_t features)
5036{
5037	struct iavf_adapter *adapter = netdev_priv(netdev);
5038
5039	features = iavf_fix_netdev_vlan_features(adapter, features);
5040
5041	if (!FDIR_FLTR_SUPPORT(adapter))
5042		features &= ~NETIF_F_NTUPLE;
5043
5044	return iavf_fix_strip_features(adapter, features);
5045}
5046
5047static int iavf_hwstamp_get(struct net_device *netdev,
5048			    struct kernel_hwtstamp_config *config)
5049{
5050	struct iavf_adapter *adapter = netdev_priv(netdev);
5051
5052	*config = adapter->ptp.hwtstamp_config;
5053
5054	return 0;
5055}
5056
5057static int iavf_hwstamp_set(struct net_device *netdev,
5058			    struct kernel_hwtstamp_config *config,
5059			    struct netlink_ext_ack *extack)
5060{
5061	struct iavf_adapter *adapter = netdev_priv(netdev);
5062
5063	return iavf_ptp_set_ts_config(adapter, config, extack);
5064}
5065
5066static int
5067iavf_verify_shaper(struct net_shaper_binding *binding,
5068		   const struct net_shaper *shaper,
5069		   struct netlink_ext_ack *extack)
5070{
5071	struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5072	u64 vf_max;
5073
5074	if (shaper->handle.scope == NET_SHAPER_SCOPE_QUEUE) {
5075		vf_max = adapter->qos_caps->cap[0].shaper.peak;
5076		if (vf_max && shaper->bw_max > vf_max) {
5077			NL_SET_ERR_MSG_FMT(extack, "Max rate (%llu) of queue %d can't exceed max TX rate of VF (%llu kbps)",
5078					   shaper->bw_max, shaper->handle.id,
5079					   vf_max);
5080			return -EINVAL;
5081		}
5082	}
5083	return 0;
5084}
5085
5086static int
5087iavf_shaper_set(struct net_shaper_binding *binding,
5088		const struct net_shaper *shaper,
5089		struct netlink_ext_ack *extack)
5090{
5091	struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5092	const struct net_shaper_handle *handle = &shaper->handle;
5093	struct iavf_ring *tx_ring;
5094	int ret;
5095
5096	netdev_assert_locked(adapter->netdev);
5097
5098	if (handle->id >= adapter->num_active_queues)
5099		return 0;
5100
5101	ret = iavf_verify_shaper(binding, shaper, extack);
5102	if (ret)
5103		return ret;
5104
5105	tx_ring = &adapter->tx_rings[handle->id];
5106
5107	tx_ring->q_shaper.bw_min = div_u64(shaper->bw_min, 1000);
5108	tx_ring->q_shaper.bw_max = div_u64(shaper->bw_max, 1000);
5109	tx_ring->q_shaper_update = true;
5110
5111	adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
5112
5113	return 0;
5114}
5115
5116static int iavf_shaper_del(struct net_shaper_binding *binding,
5117			   const struct net_shaper_handle *handle,
5118			   struct netlink_ext_ack *extack)
5119{
5120	struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5121	struct iavf_ring *tx_ring;
5122
5123	netdev_assert_locked(adapter->netdev);
5124
5125	if (handle->id >= adapter->num_active_queues)
5126		return 0;
5127
5128	tx_ring = &adapter->tx_rings[handle->id];
5129	tx_ring->q_shaper.bw_min = 0;
5130	tx_ring->q_shaper.bw_max = 0;
5131	tx_ring->q_shaper_update = true;
5132
5133	adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
5134
5135	return 0;
5136}
5137
5138static void iavf_shaper_cap(struct net_shaper_binding *binding,
5139			    enum net_shaper_scope scope,
5140			    unsigned long *flags)
5141{
5142	if (scope != NET_SHAPER_SCOPE_QUEUE)
5143		return;
5144
5145	*flags = BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MIN) |
5146		 BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX) |
5147		 BIT(NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS);
5148}
5149
5150static const struct net_shaper_ops iavf_shaper_ops = {
5151	.set = iavf_shaper_set,
5152	.delete = iavf_shaper_del,
5153	.capabilities = iavf_shaper_cap,
5154};
5155
5156static const struct net_device_ops iavf_netdev_ops = {
5157	.ndo_open		= iavf_open,
5158	.ndo_stop		= iavf_close,
5159	.ndo_start_xmit		= iavf_xmit_frame,
5160	.ndo_set_rx_mode	= iavf_set_rx_mode,
5161	.ndo_validate_addr	= eth_validate_addr,
5162	.ndo_set_mac_address	= iavf_set_mac,
5163	.ndo_change_mtu		= iavf_change_mtu,
5164	.ndo_tx_timeout		= iavf_tx_timeout,
5165	.ndo_vlan_rx_add_vid	= iavf_vlan_rx_add_vid,
5166	.ndo_vlan_rx_kill_vid	= iavf_vlan_rx_kill_vid,
5167	.ndo_features_check	= iavf_features_check,
5168	.ndo_fix_features	= iavf_fix_features,
5169	.ndo_set_features	= iavf_set_features,
5170	.ndo_setup_tc		= iavf_setup_tc,
5171	.net_shaper_ops		= &iavf_shaper_ops,
5172	.ndo_hwtstamp_get	= iavf_hwstamp_get,
5173	.ndo_hwtstamp_set	= iavf_hwstamp_set,
5174};
5175
5176/**
5177 * iavf_check_reset_complete - check that VF reset is complete
5178 * @hw: pointer to hw struct
5179 *
5180 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
5181 **/
5182static int iavf_check_reset_complete(struct iavf_hw *hw)
5183{
5184	u32 rstat;
5185	int i;
5186
5187	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
5188		rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
5189			     IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
5190		if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
5191		    (rstat == VIRTCHNL_VFR_COMPLETED))
5192			return 0;
5193		msleep(IAVF_RESET_WAIT_MS);
5194	}
5195	return -EBUSY;
5196}
5197
5198/**
5199 * iavf_process_config - Process the config information we got from the PF
5200 * @adapter: board private structure
5201 *
5202 * Verify that we have a valid config struct, and set up our netdev features
5203 * and our VSI struct.
5204 **/
5205int iavf_process_config(struct iavf_adapter *adapter)
5206{
5207	struct virtchnl_vf_resource *vfres = adapter->vf_res;
5208	netdev_features_t hw_vlan_features, vlan_features;
5209	struct net_device *netdev = adapter->netdev;
5210	netdev_features_t hw_enc_features;
5211	netdev_features_t hw_features;
5212
5213	hw_enc_features = NETIF_F_SG			|
5214			  NETIF_F_IP_CSUM		|
5215			  NETIF_F_IPV6_CSUM		|
5216			  NETIF_F_HIGHDMA		|
5217			  NETIF_F_SOFT_FEATURES	|
5218			  NETIF_F_TSO			|
5219			  NETIF_F_TSO_ECN		|
5220			  NETIF_F_TSO6			|
5221			  NETIF_F_SCTP_CRC		|
5222			  NETIF_F_RXHASH		|
5223			  NETIF_F_RXCSUM		|
5224			  0;
5225
5226	/* advertise to stack only if offloads for encapsulated packets is
5227	 * supported
5228	 */
5229	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
5230		hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL	|
5231				   NETIF_F_GSO_GRE		|
5232				   NETIF_F_GSO_GRE_CSUM		|
5233				   NETIF_F_GSO_IPXIP4		|
5234				   NETIF_F_GSO_IPXIP6		|
5235				   NETIF_F_GSO_UDP_TUNNEL_CSUM	|
5236				   NETIF_F_GSO_PARTIAL		|
5237				   0;
5238
5239		if (!(vfres->vf_cap_flags &
5240		      VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
5241			netdev->gso_partial_features |=
5242				NETIF_F_GSO_UDP_TUNNEL_CSUM;
5243
5244		netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
5245		netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
5246		netdev->hw_enc_features |= hw_enc_features;
5247	}
5248	/* record features VLANs can make use of */
5249	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
5250
5251	/* Write features and hw_features separately to avoid polluting
5252	 * with, or dropping, features that are set when we registered.
5253	 */
5254	hw_features = hw_enc_features;
5255
5256	/* get HW VLAN features that can be toggled */
5257	hw_vlan_features = iavf_get_netdev_vlan_hw_features(adapter);
5258
5259	/* Enable HW TC offload if ADQ or tc U32 is supported */
5260	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ ||
5261	    TC_U32_SUPPORT(adapter))
5262		hw_features |= NETIF_F_HW_TC;
5263
5264	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
5265		hw_features |= NETIF_F_GSO_UDP_L4;
5266
5267	netdev->hw_features |= hw_features | hw_vlan_features;
5268	vlan_features = iavf_get_netdev_vlan_features(adapter);
5269
5270	netdev->features |= hw_features | vlan_features;
5271
5272	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
5273		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5274
5275	if (FDIR_FLTR_SUPPORT(adapter)) {
5276		netdev->hw_features |= NETIF_F_NTUPLE;
5277		netdev->features |= NETIF_F_NTUPLE;
5278		adapter->flags |= IAVF_FLAG_FDIR_ENABLED;
5279	}
5280
5281	netdev->priv_flags |= IFF_UNICAST_FLT;
5282
5283	/* Do not turn on offloads when they are requested to be turned off.
5284	 * TSO needs minimum 576 bytes to work correctly.
5285	 */
5286	if (netdev->wanted_features) {
5287		if (!(netdev->wanted_features & NETIF_F_TSO) ||
5288		    netdev->mtu < 576)
5289			netdev->features &= ~NETIF_F_TSO;
5290		if (!(netdev->wanted_features & NETIF_F_TSO6) ||
5291		    netdev->mtu < 576)
5292			netdev->features &= ~NETIF_F_TSO6;
5293		if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
5294			netdev->features &= ~NETIF_F_TSO_ECN;
5295		if (!(netdev->wanted_features & NETIF_F_GRO))
5296			netdev->features &= ~NETIF_F_GRO;
5297		if (!(netdev->wanted_features & NETIF_F_GSO))
5298			netdev->features &= ~NETIF_F_GSO;
5299	}
5300
5301	return 0;
5302}
5303
5304/**
5305 * iavf_probe - Device Initialization Routine
5306 * @pdev: PCI device information struct
5307 * @ent: entry in iavf_pci_tbl
5308 *
5309 * Returns 0 on success, negative on failure
5310 *
5311 * iavf_probe initializes an adapter identified by a pci_dev structure.
5312 * The OS initialization, configuring of the adapter private structure,
5313 * and a hardware reset occur.
5314 **/
5315static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5316{
5317	struct net_device *netdev;
5318	struct iavf_adapter *adapter = NULL;
5319	struct iavf_hw *hw = NULL;
5320	int err, len;
5321
5322	err = pci_enable_device(pdev);
5323	if (err)
5324		return err;
5325
5326	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
5327	if (err) {
5328		dev_err(&pdev->dev,
5329			"DMA configuration failed: 0x%x\n", err);
5330		goto err_dma;
5331	}
5332
5333	err = pci_request_regions(pdev, iavf_driver_name);
5334	if (err) {
5335		dev_err(&pdev->dev,
5336			"pci_request_regions failed 0x%x\n", err);
5337		goto err_pci_reg;
5338	}
5339
5340	pci_set_master(pdev);
5341
5342	netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
5343				   IAVF_MAX_REQ_QUEUES);
5344	if (!netdev) {
5345		err = -ENOMEM;
5346		goto err_alloc_etherdev;
5347	}
5348
5349	netif_set_affinity_auto(netdev);
5350	SET_NETDEV_DEV(netdev, &pdev->dev);
5351
5352	pci_set_drvdata(pdev, netdev);
5353	adapter = netdev_priv(netdev);
5354
5355	adapter->netdev = netdev;
5356	adapter->pdev = pdev;
5357
5358	hw = &adapter->hw;
5359	hw->back = adapter;
5360
5361	adapter->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
5362					      iavf_driver_name);
5363	if (!adapter->wq) {
5364		err = -ENOMEM;
5365		goto err_alloc_wq;
5366	}
5367
5368	adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
5369	iavf_change_state(adapter, __IAVF_STARTUP);
5370
5371	/* Call save state here because it relies on the adapter struct. */
5372	pci_save_state(pdev);
5373
5374	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
5375			      pci_resource_len(pdev, 0));
5376	if (!hw->hw_addr) {
5377		err = -EIO;
5378		goto err_ioremap;
5379	}
5380	hw->vendor_id = pdev->vendor;
5381	hw->device_id = pdev->device;
5382	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5383	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5384	hw->subsystem_device_id = pdev->subsystem_device;
5385	hw->bus.device = PCI_SLOT(pdev->devfn);
5386	hw->bus.func = PCI_FUNC(pdev->devfn);
5387	hw->bus.bus_id = pdev->bus->number;
5388
5389	len = struct_size(adapter->qos_caps, cap, IAVF_MAX_QOS_TC_NUM);
5390	adapter->qos_caps = kzalloc(len, GFP_KERNEL);
5391	if (!adapter->qos_caps) {
5392		err = -ENOMEM;
5393		goto err_alloc_qos_cap;
5394	}
5395
5396	mutex_init(&hw->aq.asq_mutex);
5397	mutex_init(&hw->aq.arq_mutex);
5398
5399	spin_lock_init(&adapter->mac_vlan_list_lock);
5400	spin_lock_init(&adapter->cloud_filter_list_lock);
5401	spin_lock_init(&adapter->fdir_fltr_lock);
5402	spin_lock_init(&adapter->adv_rss_lock);
5403	spin_lock_init(&adapter->current_netdev_promisc_flags_lock);
5404
5405	INIT_LIST_HEAD(&adapter->mac_filter_list);
5406	INIT_LIST_HEAD(&adapter->vlan_filter_list);
5407	INIT_LIST_HEAD(&adapter->cloud_filter_list);
5408	INIT_LIST_HEAD(&adapter->fdir_list_head);
5409	INIT_LIST_HEAD(&adapter->adv_rss_list_head);
5410
5411	INIT_WORK(&adapter->reset_task, iavf_reset_task);
5412	INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
5413	INIT_WORK(&adapter->finish_config, iavf_finish_config);
5414	INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
5415
5416	/* Setup the wait queue for indicating transition to down status */
5417	init_waitqueue_head(&adapter->down_waitqueue);
5418
5419	/* Setup the wait queue for indicating virtchannel events */
5420	init_waitqueue_head(&adapter->vc_waitqueue);
5421
5422	INIT_LIST_HEAD(&adapter->ptp.aq_cmds);
5423	init_waitqueue_head(&adapter->ptp.phc_time_waitqueue);
5424	mutex_init(&adapter->ptp.aq_cmd_lock);
5425
5426	queue_delayed_work(adapter->wq, &adapter->watchdog_task,
5427			   msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
5428	/* Initialization goes on in the work. Do not add more of it below. */
5429	return 0;
5430
5431err_alloc_qos_cap:
5432	iounmap(hw->hw_addr);
5433err_ioremap:
5434	destroy_workqueue(adapter->wq);
5435err_alloc_wq:
5436	free_netdev(netdev);
5437err_alloc_etherdev:
5438	pci_release_regions(pdev);
5439err_pci_reg:
5440err_dma:
5441	pci_disable_device(pdev);
5442	return err;
5443}
5444
5445/**
5446 * iavf_suspend - Power management suspend routine
5447 * @dev_d: device info pointer
5448 *
5449 * Called when the system (VM) is entering sleep/suspend.
5450 **/
5451static int iavf_suspend(struct device *dev_d)
5452{
5453	struct net_device *netdev = dev_get_drvdata(dev_d);
5454	struct iavf_adapter *adapter = netdev_priv(netdev);
5455	bool running;
5456
5457	netif_device_detach(netdev);
5458
5459	running = netif_running(netdev);
5460	if (running)
5461		rtnl_lock();
5462	netdev_lock(netdev);
5463
5464	if (running)
5465		iavf_down(adapter);
5466
5467	iavf_free_misc_irq(adapter);
5468	iavf_reset_interrupt_capability(adapter);
5469
5470	netdev_unlock(netdev);
5471	if (running)
5472		rtnl_unlock();
5473
5474	return 0;
5475}
5476
5477/**
5478 * iavf_resume - Power management resume routine
5479 * @dev_d: device info pointer
5480 *
5481 * Called when the system (VM) is resumed from sleep/suspend.
5482 **/
5483static int iavf_resume(struct device *dev_d)
5484{
5485	struct pci_dev *pdev = to_pci_dev(dev_d);
5486	struct iavf_adapter *adapter;
5487	int err;
5488
5489	adapter = iavf_pdev_to_adapter(pdev);
5490
5491	pci_set_master(pdev);
5492
5493	rtnl_lock();
5494	err = iavf_set_interrupt_capability(adapter);
5495	if (err) {
5496		rtnl_unlock();
5497		dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
5498		return err;
5499	}
5500	err = iavf_request_misc_irq(adapter);
5501	rtnl_unlock();
5502	if (err) {
5503		dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
5504		return err;
5505	}
5506
5507	queue_work(adapter->wq, &adapter->reset_task);
5508
5509	netif_device_attach(adapter->netdev);
5510
5511	return err;
5512}
5513
5514/**
5515 * iavf_remove - Device Removal Routine
5516 * @pdev: PCI device information struct
5517 *
5518 * iavf_remove is called by the PCI subsystem to alert the driver
5519 * that it should release a PCI device.  The could be caused by a
5520 * Hot-Plug event, or because the driver is going to be removed from
5521 * memory.
5522 **/
5523static void iavf_remove(struct pci_dev *pdev)
5524{
5525	struct iavf_fdir_fltr *fdir, *fdirtmp;
5526	struct iavf_vlan_filter *vlf, *vlftmp;
5527	struct iavf_cloud_filter *cf, *cftmp;
5528	struct iavf_adv_rss *rss, *rsstmp;
5529	struct iavf_mac_filter *f, *ftmp;
5530	struct iavf_adapter *adapter;
5531	struct net_device *netdev;
5532	struct iavf_hw *hw;
5533
5534	/* Don't proceed with remove if netdev is already freed */
5535	netdev = pci_get_drvdata(pdev);
5536	if (!netdev)
5537		return;
5538
5539	adapter = iavf_pdev_to_adapter(pdev);
5540	hw = &adapter->hw;
5541
5542	if (test_and_set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
5543		return;
5544
5545	/* Wait until port initialization is complete.
5546	 * There are flows where register/unregister netdev may race.
5547	 */
5548	while (1) {
5549		netdev_lock(netdev);
5550		if (adapter->state == __IAVF_RUNNING ||
5551		    adapter->state == __IAVF_DOWN ||
5552		    adapter->state == __IAVF_INIT_FAILED) {
5553			netdev_unlock(netdev);
5554			break;
5555		}
5556		/* Simply return if we already went through iavf_shutdown */
5557		if (adapter->state == __IAVF_REMOVE) {
5558			netdev_unlock(netdev);
5559			return;
5560		}
5561
5562		netdev_unlock(netdev);
5563		usleep_range(500, 1000);
5564	}
5565	cancel_delayed_work_sync(&adapter->watchdog_task);
5566	cancel_work_sync(&adapter->finish_config);
5567
5568	if (netdev->reg_state == NETREG_REGISTERED)
5569		unregister_netdev(netdev);
5570
5571	netdev_lock(netdev);
5572	dev_info(&adapter->pdev->dev, "Removing device\n");
5573	iavf_change_state(adapter, __IAVF_REMOVE);
5574
5575	iavf_request_reset(adapter);
5576	msleep(50);
5577	/* If the FW isn't responding, kick it once, but only once. */
5578	if (!iavf_asq_done(hw)) {
5579		iavf_request_reset(adapter);
5580		msleep(50);
5581	}
5582
5583	iavf_ptp_release(adapter);
5584
5585	iavf_misc_irq_disable(adapter);
5586	/* Shut down all the garbage mashers on the detention level */
5587	netdev_unlock(netdev);
5588	cancel_work_sync(&adapter->reset_task);
5589	cancel_delayed_work_sync(&adapter->watchdog_task);
5590	cancel_work_sync(&adapter->adminq_task);
5591	netdev_lock(netdev);
5592
5593	adapter->aq_required = 0;
5594	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
5595
5596	iavf_free_all_tx_resources(adapter);
5597	iavf_free_all_rx_resources(adapter);
5598	iavf_free_misc_irq(adapter);
5599	iavf_free_interrupt_scheme(adapter);
5600
5601	iavf_free_rss(adapter);
5602
5603	if (hw->aq.asq.count)
5604		iavf_shutdown_adminq(hw);
5605
5606	/* destroy the locks only once, here */
5607	mutex_destroy(&hw->aq.arq_mutex);
5608	mutex_destroy(&hw->aq.asq_mutex);
5609	netdev_unlock(netdev);
5610
5611	iounmap(hw->hw_addr);
5612	pci_release_regions(pdev);
5613	kfree(adapter->vf_res);
5614	spin_lock_bh(&adapter->mac_vlan_list_lock);
5615	/* If we got removed before an up/down sequence, we've got a filter
5616	 * hanging out there that we need to get rid of.
5617	 */
5618	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
5619		list_del(&f->list);
5620		kfree(f);
5621	}
5622	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
5623				 list) {
5624		list_del(&vlf->list);
5625		kfree(vlf);
5626	}
5627
5628	spin_unlock_bh(&adapter->mac_vlan_list_lock);
5629
5630	spin_lock_bh(&adapter->cloud_filter_list_lock);
5631	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
5632		list_del(&cf->list);
5633		kfree(cf);
5634	}
5635	spin_unlock_bh(&adapter->cloud_filter_list_lock);
5636
5637	spin_lock_bh(&adapter->fdir_fltr_lock);
5638	list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
5639		list_del(&fdir->list);
5640		kfree(fdir);
5641	}
5642	spin_unlock_bh(&adapter->fdir_fltr_lock);
5643
5644	spin_lock_bh(&adapter->adv_rss_lock);
5645	list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
5646				 list) {
5647		list_del(&rss->list);
5648		kfree(rss);
5649	}
5650	spin_unlock_bh(&adapter->adv_rss_lock);
5651
5652	destroy_workqueue(adapter->wq);
5653
5654	pci_set_drvdata(pdev, NULL);
5655
5656	free_netdev(netdev);
5657
5658	pci_disable_device(pdev);
5659}
5660
5661/**
5662 * iavf_shutdown - Shutdown the device in preparation for a reboot
5663 * @pdev: pci device structure
5664 **/
5665static void iavf_shutdown(struct pci_dev *pdev)
5666{
5667	iavf_remove(pdev);
5668
5669	if (system_state == SYSTEM_POWER_OFF)
5670		pci_set_power_state(pdev, PCI_D3hot);
5671}
5672
5673static DEFINE_SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
5674
5675static struct pci_driver iavf_driver = {
5676	.name      = iavf_driver_name,
5677	.id_table  = iavf_pci_tbl,
5678	.probe     = iavf_probe,
5679	.remove    = iavf_remove,
5680	.driver.pm = pm_sleep_ptr(&iavf_pm_ops),
5681	.shutdown  = iavf_shutdown,
5682};
5683
5684/**
5685 * iavf_init_module - Driver Registration Routine
5686 *
5687 * iavf_init_module is the first routine called when the driver is
5688 * loaded. All it does is register with the PCI subsystem.
5689 **/
5690static int __init iavf_init_module(void)
5691{
5692	pr_info("iavf: %s\n", iavf_driver_string);
5693
5694	pr_info("%s\n", iavf_copyright);
5695
5696	return pci_register_driver(&iavf_driver);
5697}
5698
5699module_init(iavf_init_module);
5700
5701/**
5702 * iavf_exit_module - Driver Exit Cleanup Routine
5703 *
5704 * iavf_exit_module is called just before the driver is removed
5705 * from memory.
5706 **/
5707static void __exit iavf_exit_module(void)
5708{
5709	pci_unregister_driver(&iavf_driver);
5710}
5711
5712module_exit(iavf_exit_module);
5713
5714/* iavf_main.c */
~/ Read-only file preview.