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