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