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