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