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