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