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