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