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