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