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