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