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