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