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