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