xref: /linux/drivers/net/ethernet/intel/iavf/iavf_main.c (revision 27605c8c0f69e319df156b471974e4e223035378)
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  */
iavf_up_complete(struct iavf_adapter * adapter)1291 static void iavf_up_complete(struct iavf_adapter *adapter)
1292 {
1293 	netdev_assert_locked(adapter->netdev);
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  */
iavf_down(struct iavf_adapter * adapter)1414 void iavf_down(struct iavf_adapter *adapter)
1415 {
1416 	struct net_device *netdev = adapter->netdev;
1417 
1418 	netdev_assert_locked(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 RTNL.
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 netdev_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 (== netdev lock) is needed to update the queue number.
2040 	 */
2041 	rtnl_lock();
2042 	netdev_lock(adapter->netdev);
2043 
2044 	if ((adapter->flags & IAVF_FLAG_SETUP_NETDEV_FEATURES) &&
2045 	    adapter->netdev->reg_state == NETREG_REGISTERED &&
2046 	    !test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
2047 		netdev_update_features(adapter->netdev);
2048 		adapter->flags &= ~IAVF_FLAG_SETUP_NETDEV_FEATURES;
2049 	}
2050 
2051 	switch (adapter->state) {
2052 	case __IAVF_DOWN:
2053 		/* Set the real number of queues when reset occurs while
2054 		 * state == __IAVF_DOWN
2055 		 */
2056 		pairs = adapter->num_active_queues;
2057 		netif_set_real_num_rx_queues(adapter->netdev, pairs);
2058 		netif_set_real_num_tx_queues(adapter->netdev, pairs);
2059 
2060 		if (adapter->netdev->reg_state != NETREG_REGISTERED) {
2061 			netdev_unlock(adapter->netdev);
2062 			netdev_released = true;
2063 			err = register_netdevice(adapter->netdev);
2064 			if (err) {
2065 				dev_err(&adapter->pdev->dev, "Unable to register netdev (%d)\n",
2066 					err);
2067 
2068 				/* go back and try again.*/
2069 				netdev_lock(adapter->netdev);
2070 				iavf_free_rss(adapter);
2071 				iavf_free_misc_irq(adapter);
2072 				iavf_reset_interrupt_capability(adapter);
2073 				iavf_change_state(adapter,
2074 						  __IAVF_INIT_CONFIG_ADAPTER);
2075 				netdev_unlock(adapter->netdev);
2076 				goto out;
2077 			}
2078 		}
2079 		break;
2080 	case __IAVF_RUNNING:
2081 		pairs = adapter->num_active_queues;
2082 		netif_set_real_num_rx_queues(adapter->netdev, pairs);
2083 		netif_set_real_num_tx_queues(adapter->netdev, pairs);
2084 		break;
2085 
2086 	default:
2087 		break;
2088 	}
2089 
2090 out:
2091 	if (!netdev_released)
2092 		netdev_unlock(adapter->netdev);
2093 	rtnl_unlock();
2094 }
2095 
2096 /**
2097  * iavf_schedule_finish_config - Set the flags and schedule a reset event
2098  * @adapter: board private structure
2099  **/
iavf_schedule_finish_config(struct iavf_adapter * adapter)2100 void iavf_schedule_finish_config(struct iavf_adapter *adapter)
2101 {
2102 	if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2103 		queue_work(adapter->wq, &adapter->finish_config);
2104 }
2105 
2106 /**
2107  * iavf_process_aq_command - process aq_required flags
2108  * and sends aq command
2109  * @adapter: pointer to iavf adapter structure
2110  *
2111  * Returns 0 on success
2112  * Returns error code if no command was sent
2113  * or error code if the command failed.
2114  **/
iavf_process_aq_command(struct iavf_adapter * adapter)2115 static int iavf_process_aq_command(struct iavf_adapter *adapter)
2116 {
2117 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
2118 		return iavf_send_vf_config_msg(adapter);
2119 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS)
2120 		return iavf_send_vf_offload_vlan_v2_msg(adapter);
2121 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_SUPPORTED_RXDIDS)
2122 		return iavf_send_vf_supported_rxdids_msg(adapter);
2123 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_PTP_CAPS)
2124 		return iavf_send_vf_ptp_caps_msg(adapter);
2125 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
2126 		iavf_disable_queues(adapter);
2127 		return 0;
2128 	}
2129 
2130 	if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
2131 		iavf_map_queues(adapter);
2132 		return 0;
2133 	}
2134 
2135 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
2136 		iavf_add_ether_addrs(adapter);
2137 		return 0;
2138 	}
2139 
2140 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
2141 		iavf_add_vlans(adapter);
2142 		return 0;
2143 	}
2144 
2145 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
2146 		iavf_del_ether_addrs(adapter);
2147 		return 0;
2148 	}
2149 
2150 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
2151 		iavf_del_vlans(adapter);
2152 		return 0;
2153 	}
2154 
2155 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
2156 		iavf_enable_vlan_stripping(adapter);
2157 		return 0;
2158 	}
2159 
2160 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
2161 		iavf_disable_vlan_stripping(adapter);
2162 		return 0;
2163 	}
2164 
2165 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW) {
2166 		iavf_cfg_queues_bw(adapter);
2167 		return 0;
2168 	}
2169 
2170 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_QOS_CAPS) {
2171 		iavf_get_qos_caps(adapter);
2172 		return 0;
2173 	}
2174 
2175 	if (adapter->aq_required & IAVF_FLAG_AQ_CFG_QUEUES_QUANTA_SIZE) {
2176 		iavf_cfg_queues_quanta_size(adapter);
2177 		return 0;
2178 	}
2179 
2180 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
2181 		iavf_configure_queues(adapter);
2182 		return 0;
2183 	}
2184 
2185 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
2186 		iavf_enable_queues(adapter);
2187 		return 0;
2188 	}
2189 
2190 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
2191 		/* This message goes straight to the firmware, not the
2192 		 * PF, so we don't have to set current_op as we will
2193 		 * not get a response through the ARQ.
2194 		 */
2195 		adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
2196 		return 0;
2197 	}
2198 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
2199 		iavf_get_hena(adapter);
2200 		return 0;
2201 	}
2202 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
2203 		iavf_set_hena(adapter);
2204 		return 0;
2205 	}
2206 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
2207 		iavf_set_rss_key(adapter);
2208 		return 0;
2209 	}
2210 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
2211 		iavf_set_rss_lut(adapter);
2212 		return 0;
2213 	}
2214 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_HFUNC) {
2215 		iavf_set_rss_hfunc(adapter);
2216 		return 0;
2217 	}
2218 
2219 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE) {
2220 		iavf_set_promiscuous(adapter);
2221 		return 0;
2222 	}
2223 
2224 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
2225 		iavf_enable_channels(adapter);
2226 		return 0;
2227 	}
2228 
2229 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
2230 		iavf_disable_channels(adapter);
2231 		return 0;
2232 	}
2233 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2234 		iavf_add_cloud_filter(adapter);
2235 		return 0;
2236 	}
2237 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2238 		iavf_del_cloud_filter(adapter);
2239 		return 0;
2240 	}
2241 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
2242 		iavf_add_fdir_filter(adapter);
2243 		return IAVF_SUCCESS;
2244 	}
2245 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
2246 		iavf_del_fdir_filter(adapter);
2247 		return IAVF_SUCCESS;
2248 	}
2249 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
2250 		iavf_add_adv_rss_cfg(adapter);
2251 		return 0;
2252 	}
2253 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
2254 		iavf_del_adv_rss_cfg(adapter);
2255 		return 0;
2256 	}
2257 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING) {
2258 		iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2259 		return 0;
2260 	}
2261 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING) {
2262 		iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2263 		return 0;
2264 	}
2265 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING) {
2266 		iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2267 		return 0;
2268 	}
2269 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING) {
2270 		iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2271 		return 0;
2272 	}
2273 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION) {
2274 		iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2275 		return 0;
2276 	}
2277 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION) {
2278 		iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2279 		return 0;
2280 	}
2281 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION) {
2282 		iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2283 		return 0;
2284 	}
2285 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION) {
2286 		iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2287 		return 0;
2288 	}
2289 	if (adapter->aq_required & IAVF_FLAG_AQ_SEND_PTP_CMD) {
2290 		iavf_virtchnl_send_ptp_cmd(adapter);
2291 		return IAVF_SUCCESS;
2292 	}
2293 	if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
2294 		iavf_request_stats(adapter);
2295 		return 0;
2296 	}
2297 
2298 	return -EAGAIN;
2299 }
2300 
2301 /**
2302  * iavf_set_vlan_offload_features - set VLAN offload configuration
2303  * @adapter: board private structure
2304  * @prev_features: previous features used for comparison
2305  * @features: updated features used for configuration
2306  *
2307  * Set the aq_required bit(s) based on the requested features passed in to
2308  * configure VLAN stripping and/or VLAN insertion if supported. Also, schedule
2309  * the watchdog if any changes are requested to expedite the request via
2310  * virtchnl.
2311  **/
2312 static void
iavf_set_vlan_offload_features(struct iavf_adapter * adapter,netdev_features_t prev_features,netdev_features_t features)2313 iavf_set_vlan_offload_features(struct iavf_adapter *adapter,
2314 			       netdev_features_t prev_features,
2315 			       netdev_features_t features)
2316 {
2317 	bool enable_stripping = true, enable_insertion = true;
2318 	u16 vlan_ethertype = 0;
2319 	u64 aq_required = 0;
2320 
2321 	/* keep cases separate because one ethertype for offloads can be
2322 	 * disabled at the same time as another is disabled, so check for an
2323 	 * enabled ethertype first, then check for disabled. Default to
2324 	 * ETH_P_8021Q so an ethertype is specified if disabling insertion and
2325 	 * stripping.
2326 	 */
2327 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2328 		vlan_ethertype = ETH_P_8021AD;
2329 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2330 		vlan_ethertype = ETH_P_8021Q;
2331 	else if (prev_features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2332 		vlan_ethertype = ETH_P_8021AD;
2333 	else if (prev_features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2334 		vlan_ethertype = ETH_P_8021Q;
2335 	else
2336 		vlan_ethertype = ETH_P_8021Q;
2337 
2338 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
2339 		enable_stripping = false;
2340 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
2341 		enable_insertion = false;
2342 
2343 	if (VLAN_ALLOWED(adapter)) {
2344 		/* VIRTCHNL_VF_OFFLOAD_VLAN only has support for toggling VLAN
2345 		 * stripping via virtchnl. VLAN insertion can be toggled on the
2346 		 * netdev, but it doesn't require a virtchnl message
2347 		 */
2348 		if (enable_stripping)
2349 			aq_required |= IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2350 		else
2351 			aq_required |= IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2352 
2353 	} else if (VLAN_V2_ALLOWED(adapter)) {
2354 		switch (vlan_ethertype) {
2355 		case ETH_P_8021Q:
2356 			if (enable_stripping)
2357 				aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
2358 			else
2359 				aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
2360 
2361 			if (enable_insertion)
2362 				aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
2363 			else
2364 				aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
2365 			break;
2366 		case ETH_P_8021AD:
2367 			if (enable_stripping)
2368 				aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
2369 			else
2370 				aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
2371 
2372 			if (enable_insertion)
2373 				aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
2374 			else
2375 				aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
2376 			break;
2377 		}
2378 	}
2379 
2380 	if (aq_required)
2381 		iavf_schedule_aq_request(adapter, aq_required);
2382 }
2383 
2384 /**
2385  * iavf_startup - first step of driver startup
2386  * @adapter: board private structure
2387  *
2388  * Function process __IAVF_STARTUP driver state.
2389  * When success the state is changed to __IAVF_INIT_VERSION_CHECK
2390  * when fails the state is changed to __IAVF_INIT_FAILED
2391  **/
iavf_startup(struct iavf_adapter * adapter)2392 static void iavf_startup(struct iavf_adapter *adapter)
2393 {
2394 	struct pci_dev *pdev = adapter->pdev;
2395 	struct iavf_hw *hw = &adapter->hw;
2396 	enum iavf_status status;
2397 	int ret;
2398 
2399 	WARN_ON(adapter->state != __IAVF_STARTUP);
2400 
2401 	/* driver loaded, probe complete */
2402 	adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2403 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2404 
2405 	ret = iavf_check_reset_complete(hw);
2406 	if (ret) {
2407 		dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2408 			 ret);
2409 		goto err;
2410 	}
2411 	hw->aq.num_arq_entries = IAVF_AQ_LEN;
2412 	hw->aq.num_asq_entries = IAVF_AQ_LEN;
2413 	hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2414 	hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2415 
2416 	status = iavf_init_adminq(hw);
2417 	if (status) {
2418 		dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2419 			status);
2420 		goto err;
2421 	}
2422 	ret = iavf_send_api_ver(adapter);
2423 	if (ret) {
2424 		dev_err(&pdev->dev, "Unable to send to PF (%d)\n", ret);
2425 		iavf_shutdown_adminq(hw);
2426 		goto err;
2427 	}
2428 	iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
2429 	return;
2430 err:
2431 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2432 }
2433 
2434 /**
2435  * iavf_init_version_check - second step of driver startup
2436  * @adapter: board private structure
2437  *
2438  * Function process __IAVF_INIT_VERSION_CHECK driver state.
2439  * When success the state is changed to __IAVF_INIT_GET_RESOURCES
2440  * when fails the state is changed to __IAVF_INIT_FAILED
2441  **/
iavf_init_version_check(struct iavf_adapter * adapter)2442 static void iavf_init_version_check(struct iavf_adapter *adapter)
2443 {
2444 	struct pci_dev *pdev = adapter->pdev;
2445 	struct iavf_hw *hw = &adapter->hw;
2446 	int err = -EAGAIN;
2447 
2448 	WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
2449 
2450 	if (!iavf_asq_done(hw)) {
2451 		dev_err(&pdev->dev, "Admin queue command never completed\n");
2452 		iavf_shutdown_adminq(hw);
2453 		iavf_change_state(adapter, __IAVF_STARTUP);
2454 		goto err;
2455 	}
2456 
2457 	/* aq msg sent, awaiting reply */
2458 	err = iavf_verify_api_ver(adapter);
2459 	if (err) {
2460 		if (err == -EALREADY)
2461 			err = iavf_send_api_ver(adapter);
2462 		else
2463 			dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2464 				adapter->pf_version.major,
2465 				adapter->pf_version.minor,
2466 				VIRTCHNL_VERSION_MAJOR,
2467 				VIRTCHNL_VERSION_MINOR);
2468 		goto err;
2469 	}
2470 	err = iavf_send_vf_config_msg(adapter);
2471 	if (err) {
2472 		dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2473 			err);
2474 		goto err;
2475 	}
2476 	iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
2477 	return;
2478 err:
2479 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2480 }
2481 
2482 /**
2483  * iavf_parse_vf_resource_msg - parse response from VIRTCHNL_OP_GET_VF_RESOURCES
2484  * @adapter: board private structure
2485  */
iavf_parse_vf_resource_msg(struct iavf_adapter * adapter)2486 int iavf_parse_vf_resource_msg(struct iavf_adapter *adapter)
2487 {
2488 	int i, num_req_queues = adapter->num_req_queues;
2489 	struct iavf_vsi *vsi = &adapter->vsi;
2490 
2491 	for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2492 		if (adapter->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2493 			adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2494 	}
2495 	if (!adapter->vsi_res) {
2496 		dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2497 		return -ENODEV;
2498 	}
2499 
2500 	if (num_req_queues &&
2501 	    num_req_queues > adapter->vsi_res->num_queue_pairs) {
2502 		/* Problem.  The PF gave us fewer queues than what we had
2503 		 * negotiated in our request.  Need a reset to see if we can't
2504 		 * get back to a working state.
2505 		 */
2506 		dev_err(&adapter->pdev->dev,
2507 			"Requested %d queues, but PF only gave us %d.\n",
2508 			num_req_queues,
2509 			adapter->vsi_res->num_queue_pairs);
2510 		adapter->flags |= IAVF_FLAG_REINIT_MSIX_NEEDED;
2511 		adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2512 		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
2513 
2514 		return -EAGAIN;
2515 	}
2516 	adapter->num_req_queues = 0;
2517 	adapter->vsi.id = adapter->vsi_res->vsi_id;
2518 
2519 	adapter->vsi.back = adapter;
2520 	adapter->vsi.base_vector = 1;
2521 	vsi->netdev = adapter->netdev;
2522 	vsi->qs_handle = adapter->vsi_res->qset_handle;
2523 	if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2524 		adapter->rss_key_size = adapter->vf_res->rss_key_size;
2525 		adapter->rss_lut_size = adapter->vf_res->rss_lut_size;
2526 	} else {
2527 		adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
2528 		adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
2529 	}
2530 
2531 	return 0;
2532 }
2533 
2534 /**
2535  * iavf_init_get_resources - third step of driver startup
2536  * @adapter: board private structure
2537  *
2538  * Function process __IAVF_INIT_GET_RESOURCES driver state and
2539  * finishes driver initialization procedure.
2540  * When success the state is changed to __IAVF_DOWN
2541  * when fails the state is changed to __IAVF_INIT_FAILED
2542  **/
iavf_init_get_resources(struct iavf_adapter * adapter)2543 static void iavf_init_get_resources(struct iavf_adapter *adapter)
2544 {
2545 	struct pci_dev *pdev = adapter->pdev;
2546 	struct iavf_hw *hw = &adapter->hw;
2547 	int err;
2548 
2549 	WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
2550 	/* aq msg sent, awaiting reply */
2551 	if (!adapter->vf_res) {
2552 		adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
2553 					  GFP_KERNEL);
2554 		if (!adapter->vf_res) {
2555 			err = -ENOMEM;
2556 			goto err;
2557 		}
2558 	}
2559 	err = iavf_get_vf_config(adapter);
2560 	if (err == -EALREADY) {
2561 		err = iavf_send_vf_config_msg(adapter);
2562 		goto err;
2563 	} else if (err == -EINVAL) {
2564 		/* We only get -EINVAL if the device is in a very bad
2565 		 * state or if we've been disabled for previous bad
2566 		 * behavior. Either way, we're done now.
2567 		 */
2568 		iavf_shutdown_adminq(hw);
2569 		dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2570 		return;
2571 	}
2572 	if (err) {
2573 		dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
2574 		goto err_alloc;
2575 	}
2576 
2577 	err = iavf_parse_vf_resource_msg(adapter);
2578 	if (err) {
2579 		dev_err(&pdev->dev, "Failed to parse VF resource message from PF (%d)\n",
2580 			err);
2581 		goto err_alloc;
2582 	}
2583 	/* Some features require additional messages to negotiate extended
2584 	 * capabilities. These are processed in sequence by the
2585 	 * __IAVF_INIT_EXTENDED_CAPS driver state.
2586 	 */
2587 	adapter->extended_caps = IAVF_EXTENDED_CAPS;
2588 
2589 	iavf_change_state(adapter, __IAVF_INIT_EXTENDED_CAPS);
2590 	return;
2591 
2592 err_alloc:
2593 	kfree(adapter->vf_res);
2594 	adapter->vf_res = NULL;
2595 err:
2596 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2597 }
2598 
2599 /**
2600  * iavf_init_send_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2601  * @adapter: board private structure
2602  *
2603  * Function processes send of the extended VLAN V2 capability message to the
2604  * PF. Must clear IAVF_EXTENDED_CAP_RECV_VLAN_V2 if the message is not sent,
2605  * e.g. due to PF not negotiating VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2606  */
iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter * adapter)2607 static void iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2608 {
2609 	int ret;
2610 
2611 	WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2));
2612 
2613 	ret = iavf_send_vf_offload_vlan_v2_msg(adapter);
2614 	if (ret && ret == -EOPNOTSUPP) {
2615 		/* PF does not support VIRTCHNL_VF_OFFLOAD_V2. In this case,
2616 		 * we did not send the capability exchange message and do not
2617 		 * expect a response.
2618 		 */
2619 		adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2620 	}
2621 
2622 	/* We sent the message, so move on to the next step */
2623 	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2624 }
2625 
2626 /**
2627  * iavf_init_recv_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2628  * @adapter: board private structure
2629  *
2630  * Function processes receipt of the extended VLAN V2 capability message from
2631  * the PF.
2632  **/
iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter * adapter)2633 static void iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2634 {
2635 	int ret;
2636 
2637 	WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2));
2638 
2639 	memset(&adapter->vlan_v2_caps, 0, sizeof(adapter->vlan_v2_caps));
2640 
2641 	ret = iavf_get_vf_vlan_v2_caps(adapter);
2642 	if (ret)
2643 		goto err;
2644 
2645 	/* We've processed receipt of the VLAN V2 caps message */
2646 	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2647 	return;
2648 err:
2649 	/* We didn't receive a reply. Make sure we try sending again when
2650 	 * __IAVF_INIT_FAILED attempts to recover.
2651 	 */
2652 	adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2653 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2654 }
2655 
2656 /**
2657  * iavf_init_send_supported_rxdids - part of querying for supported RXDID
2658  * formats
2659  * @adapter: board private structure
2660  *
2661  * Function processes send of the request for supported RXDIDs to the PF.
2662  * Must clear IAVF_EXTENDED_CAP_RECV_RXDID if the message is not sent, e.g.
2663  * due to the PF not negotiating VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC.
2664  */
iavf_init_send_supported_rxdids(struct iavf_adapter * adapter)2665 static void iavf_init_send_supported_rxdids(struct iavf_adapter *adapter)
2666 {
2667 	int ret;
2668 
2669 	ret = iavf_send_vf_supported_rxdids_msg(adapter);
2670 	if (ret == -EOPNOTSUPP) {
2671 		/* PF does not support VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC. In this
2672 		 * case, we did not send the capability exchange message and
2673 		 * do not expect a response.
2674 		 */
2675 		adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_RXDID;
2676 	}
2677 
2678 	/* We sent the message, so move on to the next step */
2679 	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_RXDID;
2680 }
2681 
2682 /**
2683  * iavf_init_recv_supported_rxdids - part of querying for supported RXDID
2684  * formats
2685  * @adapter: board private structure
2686  *
2687  * Function processes receipt of the supported RXDIDs message from the PF.
2688  **/
iavf_init_recv_supported_rxdids(struct iavf_adapter * adapter)2689 static void iavf_init_recv_supported_rxdids(struct iavf_adapter *adapter)
2690 {
2691 	int ret;
2692 
2693 	memset(&adapter->supp_rxdids, 0, sizeof(adapter->supp_rxdids));
2694 
2695 	ret = iavf_get_vf_supported_rxdids(adapter);
2696 	if (ret)
2697 		goto err;
2698 
2699 	/* We've processed the PF response to the
2700 	 * VIRTCHNL_OP_GET_SUPPORTED_RXDIDS message we sent previously.
2701 	 */
2702 	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_RXDID;
2703 	return;
2704 
2705 err:
2706 	/* We didn't receive a reply. Make sure we try sending again when
2707 	 * __IAVF_INIT_FAILED attempts to recover.
2708 	 */
2709 	adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_RXDID;
2710 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2711 }
2712 
2713 /**
2714  * iavf_init_send_ptp_caps - part of querying for extended PTP capabilities
2715  * @adapter: board private structure
2716  *
2717  * Function processes send of the request for 1588 PTP capabilities to the PF.
2718  * Must clear IAVF_EXTENDED_CAP_SEND_PTP if the message is not sent, e.g.
2719  * due to the PF not negotiating VIRTCHNL_VF_PTP_CAP
2720  */
iavf_init_send_ptp_caps(struct iavf_adapter * adapter)2721 static void iavf_init_send_ptp_caps(struct iavf_adapter *adapter)
2722 {
2723 	if (iavf_send_vf_ptp_caps_msg(adapter) == -EOPNOTSUPP) {
2724 		/* PF does not support VIRTCHNL_VF_PTP_CAP. In this case, we
2725 		 * did not send the capability exchange message and do not
2726 		 * expect a response.
2727 		 */
2728 		adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_PTP;
2729 	}
2730 
2731 	/* We sent the message, so move on to the next step */
2732 	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_PTP;
2733 }
2734 
2735 /**
2736  * iavf_init_recv_ptp_caps - part of querying for supported PTP capabilities
2737  * @adapter: board private structure
2738  *
2739  * Function processes receipt of the PTP capabilities supported on this VF.
2740  **/
iavf_init_recv_ptp_caps(struct iavf_adapter * adapter)2741 static void iavf_init_recv_ptp_caps(struct iavf_adapter *adapter)
2742 {
2743 	memset(&adapter->ptp.hw_caps, 0, sizeof(adapter->ptp.hw_caps));
2744 
2745 	if (iavf_get_vf_ptp_caps(adapter))
2746 		goto err;
2747 
2748 	/* We've processed the PF response to the VIRTCHNL_OP_1588_PTP_GET_CAPS
2749 	 * message we sent previously.
2750 	 */
2751 	adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_PTP;
2752 	return;
2753 
2754 err:
2755 	/* We didn't receive a reply. Make sure we try sending again when
2756 	 * __IAVF_INIT_FAILED attempts to recover.
2757 	 */
2758 	adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_PTP;
2759 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2760 }
2761 
2762 /**
2763  * iavf_init_process_extended_caps - Part of driver startup
2764  * @adapter: board private structure
2765  *
2766  * Function processes __IAVF_INIT_EXTENDED_CAPS driver state. This state
2767  * handles negotiating capabilities for features which require an additional
2768  * message.
2769  *
2770  * Once all extended capabilities exchanges are finished, the driver will
2771  * transition into __IAVF_INIT_CONFIG_ADAPTER.
2772  */
iavf_init_process_extended_caps(struct iavf_adapter * adapter)2773 static void iavf_init_process_extended_caps(struct iavf_adapter *adapter)
2774 {
2775 	WARN_ON(adapter->state != __IAVF_INIT_EXTENDED_CAPS);
2776 
2777 	/* Process capability exchange for VLAN V2 */
2778 	if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2) {
2779 		iavf_init_send_offload_vlan_v2_caps(adapter);
2780 		return;
2781 	} else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2) {
2782 		iavf_init_recv_offload_vlan_v2_caps(adapter);
2783 		return;
2784 	}
2785 
2786 	/* Process capability exchange for RXDID formats */
2787 	if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_RXDID) {
2788 		iavf_init_send_supported_rxdids(adapter);
2789 		return;
2790 	} else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_RXDID) {
2791 		iavf_init_recv_supported_rxdids(adapter);
2792 		return;
2793 	}
2794 
2795 	/* Process capability exchange for PTP features */
2796 	if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_PTP) {
2797 		iavf_init_send_ptp_caps(adapter);
2798 		return;
2799 	} else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_PTP) {
2800 		iavf_init_recv_ptp_caps(adapter);
2801 		return;
2802 	}
2803 
2804 	/* When we reach here, no further extended capabilities exchanges are
2805 	 * necessary, so we finally transition into __IAVF_INIT_CONFIG_ADAPTER
2806 	 */
2807 	iavf_change_state(adapter, __IAVF_INIT_CONFIG_ADAPTER);
2808 }
2809 
2810 /**
2811  * iavf_init_config_adapter - last part of driver startup
2812  * @adapter: board private structure
2813  *
2814  * After all the supported capabilities are negotiated, then the
2815  * __IAVF_INIT_CONFIG_ADAPTER state will finish driver initialization.
2816  */
iavf_init_config_adapter(struct iavf_adapter * adapter)2817 static void iavf_init_config_adapter(struct iavf_adapter *adapter)
2818 {
2819 	struct net_device *netdev = adapter->netdev;
2820 	struct pci_dev *pdev = adapter->pdev;
2821 	int err;
2822 
2823 	WARN_ON(adapter->state != __IAVF_INIT_CONFIG_ADAPTER);
2824 
2825 	if (iavf_process_config(adapter))
2826 		goto err;
2827 
2828 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2829 
2830 	adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
2831 
2832 	netdev->netdev_ops = &iavf_netdev_ops;
2833 	iavf_set_ethtool_ops(netdev);
2834 	netdev->watchdog_timeo = 5 * HZ;
2835 
2836 	netdev->min_mtu = ETH_MIN_MTU;
2837 	netdev->max_mtu = LIBIE_MAX_MTU;
2838 
2839 	if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2840 		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2841 			 adapter->hw.mac.addr);
2842 		eth_hw_addr_random(netdev);
2843 		ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2844 	} else {
2845 		eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2846 		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2847 	}
2848 
2849 	adapter->tx_desc_count = IAVF_DEFAULT_TXD;
2850 	adapter->rx_desc_count = IAVF_DEFAULT_RXD;
2851 	err = iavf_init_interrupt_scheme(adapter);
2852 	if (err)
2853 		goto err_sw_init;
2854 	iavf_map_rings_to_vectors(adapter);
2855 	if (adapter->vf_res->vf_cap_flags &
2856 		VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2857 		adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
2858 
2859 	err = iavf_request_misc_irq(adapter);
2860 	if (err)
2861 		goto err_sw_init;
2862 
2863 	netif_carrier_off(netdev);
2864 	adapter->link_up = false;
2865 	netif_tx_stop_all_queues(netdev);
2866 
2867 	dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2868 	if (netdev->features & NETIF_F_GRO)
2869 		dev_info(&pdev->dev, "GRO is enabled\n");
2870 
2871 	iavf_change_state(adapter, __IAVF_DOWN);
2872 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2873 
2874 	iavf_misc_irq_enable(adapter);
2875 	wake_up(&adapter->down_waitqueue);
2876 
2877 	adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2878 	adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2879 	if (!adapter->rss_key || !adapter->rss_lut) {
2880 		err = -ENOMEM;
2881 		goto err_mem;
2882 	}
2883 	if (RSS_AQ(adapter))
2884 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2885 	else
2886 		iavf_init_rss(adapter);
2887 
2888 	if (VLAN_V2_ALLOWED(adapter))
2889 		/* request initial VLAN offload settings */
2890 		iavf_set_vlan_offload_features(adapter, 0, netdev->features);
2891 
2892 	if (QOS_ALLOWED(adapter))
2893 		adapter->aq_required |= IAVF_FLAG_AQ_GET_QOS_CAPS;
2894 
2895 	/* Setup initial PTP configuration */
2896 	iavf_ptp_init(adapter);
2897 
2898 	iavf_schedule_finish_config(adapter);
2899 	return;
2900 
2901 err_mem:
2902 	iavf_free_rss(adapter);
2903 	iavf_free_misc_irq(adapter);
2904 err_sw_init:
2905 	iavf_reset_interrupt_capability(adapter);
2906 err:
2907 	iavf_change_state(adapter, __IAVF_INIT_FAILED);
2908 }
2909 
2910 static const int IAVF_NO_RESCHED = -1;
2911 
2912 /* return: msec delay for requeueing itself */
iavf_watchdog_step(struct iavf_adapter * adapter)2913 static int iavf_watchdog_step(struct iavf_adapter *adapter)
2914 {
2915 	struct iavf_hw *hw = &adapter->hw;
2916 	u32 reg_val;
2917 
2918 	netdev_assert_locked(adapter->netdev);
2919 
2920 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2921 		iavf_change_state(adapter, __IAVF_COMM_FAILED);
2922 
2923 	switch (adapter->state) {
2924 	case __IAVF_STARTUP:
2925 		iavf_startup(adapter);
2926 		return 30;
2927 	case __IAVF_INIT_VERSION_CHECK:
2928 		iavf_init_version_check(adapter);
2929 		return 30;
2930 	case __IAVF_INIT_GET_RESOURCES:
2931 		iavf_init_get_resources(adapter);
2932 		return 1;
2933 	case __IAVF_INIT_EXTENDED_CAPS:
2934 		iavf_init_process_extended_caps(adapter);
2935 		return 1;
2936 	case __IAVF_INIT_CONFIG_ADAPTER:
2937 		iavf_init_config_adapter(adapter);
2938 		return 1;
2939 	case __IAVF_INIT_FAILED:
2940 		if (test_bit(__IAVF_IN_REMOVE_TASK,
2941 			     &adapter->crit_section)) {
2942 			/* Do not update the state and do not reschedule
2943 			 * watchdog task, iavf_remove should handle this state
2944 			 * as it can loop forever
2945 			 */
2946 			return IAVF_NO_RESCHED;
2947 		}
2948 		if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2949 			dev_err(&adapter->pdev->dev,
2950 				"Failed to communicate with PF; waiting before retry\n");
2951 			adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2952 			iavf_shutdown_adminq(hw);
2953 			return 5000;
2954 		}
2955 		/* Try again from failed step*/
2956 		iavf_change_state(adapter, adapter->last_state);
2957 		return 1000;
2958 	case __IAVF_COMM_FAILED:
2959 		if (test_bit(__IAVF_IN_REMOVE_TASK,
2960 			     &adapter->crit_section)) {
2961 			/* Set state to __IAVF_INIT_FAILED and perform remove
2962 			 * steps. Remove IAVF_FLAG_PF_COMMS_FAILED so the task
2963 			 * doesn't bring the state back to __IAVF_COMM_FAILED.
2964 			 */
2965 			iavf_change_state(adapter, __IAVF_INIT_FAILED);
2966 			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2967 			return IAVF_NO_RESCHED;
2968 		}
2969 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2970 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2971 		if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2972 		    reg_val == VIRTCHNL_VFR_COMPLETED) {
2973 			/* A chance for redemption! */
2974 			dev_err(&adapter->pdev->dev,
2975 				"Hardware came out of reset. Attempting reinit.\n");
2976 			/* When init task contacts the PF and
2977 			 * gets everything set up again, it'll restart the
2978 			 * watchdog for us. Down, boy. Sit. Stay. Woof.
2979 			 */
2980 			iavf_change_state(adapter, __IAVF_STARTUP);
2981 			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2982 		}
2983 		adapter->aq_required = 0;
2984 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2985 		return 10;
2986 	case __IAVF_RESETTING:
2987 		return 2000;
2988 	case __IAVF_DOWN:
2989 	case __IAVF_DOWN_PENDING:
2990 	case __IAVF_TESTING:
2991 	case __IAVF_RUNNING:
2992 		if (adapter->current_op) {
2993 			if (!iavf_asq_done(hw)) {
2994 				dev_dbg(&adapter->pdev->dev,
2995 					"Admin queue timeout\n");
2996 				iavf_send_api_ver(adapter);
2997 			}
2998 		} else {
2999 			int ret = iavf_process_aq_command(adapter);
3000 
3001 			/* An error will be returned if no commands were
3002 			 * processed; use this opportunity to update stats
3003 			 * if the error isn't -ENOTSUPP
3004 			 */
3005 			if (ret && ret != -EOPNOTSUPP &&
3006 			    adapter->state == __IAVF_RUNNING)
3007 				iavf_request_stats(adapter);
3008 		}
3009 		if (adapter->state == __IAVF_RUNNING)
3010 			iavf_detect_recover_hung(&adapter->vsi);
3011 		break;
3012 	case __IAVF_REMOVE:
3013 	default:
3014 		return IAVF_NO_RESCHED;
3015 	}
3016 
3017 	/* check for hw reset */
3018 	reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
3019 	if (!reg_val) {
3020 		adapter->aq_required = 0;
3021 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3022 		dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
3023 		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_PENDING);
3024 	}
3025 
3026 	return adapter->aq_required ? 20 : 2000;
3027 }
3028 
iavf_watchdog_task(struct work_struct * work)3029 static void iavf_watchdog_task(struct work_struct *work)
3030 {
3031 	struct iavf_adapter *adapter = container_of(work,
3032 						    struct iavf_adapter,
3033 						    watchdog_task.work);
3034 	struct net_device *netdev = adapter->netdev;
3035 	int msec_delay;
3036 
3037 	netdev_lock(netdev);
3038 	msec_delay = iavf_watchdog_step(adapter);
3039 	/* note that we schedule a different task */
3040 	if (adapter->state >= __IAVF_DOWN)
3041 		queue_work(adapter->wq, &adapter->adminq_task);
3042 
3043 	if (msec_delay != IAVF_NO_RESCHED)
3044 		queue_delayed_work(adapter->wq, &adapter->watchdog_task,
3045 				   msecs_to_jiffies(msec_delay));
3046 	netdev_unlock(netdev);
3047 }
3048 
3049 /**
3050  * iavf_disable_vf - disable VF
3051  * @adapter: board private structure
3052  *
3053  * Set communication failed flag and free all resources.
3054  */
iavf_disable_vf(struct iavf_adapter * adapter)3055 static void iavf_disable_vf(struct iavf_adapter *adapter)
3056 {
3057 	struct iavf_mac_filter *f, *ftmp;
3058 	struct iavf_vlan_filter *fv, *fvtmp;
3059 	struct iavf_cloud_filter *cf, *cftmp;
3060 
3061 	netdev_assert_locked(adapter->netdev);
3062 
3063 	adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3064 
3065 	/* We don't use netif_running() because it may be true prior to
3066 	 * ndo_open() returning, so we can't assume it means all our open
3067 	 * tasks have finished, since we're not holding the rtnl_lock here.
3068 	 */
3069 	if (adapter->state == __IAVF_RUNNING) {
3070 		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3071 		netif_carrier_off(adapter->netdev);
3072 		netif_tx_disable(adapter->netdev);
3073 		adapter->link_up = false;
3074 		iavf_napi_disable_all(adapter);
3075 		iavf_irq_disable(adapter);
3076 		iavf_free_traffic_irqs(adapter);
3077 		iavf_free_all_tx_resources(adapter);
3078 		iavf_free_all_rx_resources(adapter);
3079 	}
3080 
3081 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3082 
3083 	/* Delete all of the filters */
3084 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3085 		list_del(&f->list);
3086 		kfree(f);
3087 	}
3088 
3089 	list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
3090 		list_del(&fv->list);
3091 		kfree(fv);
3092 	}
3093 	adapter->num_vlan_filters = 0;
3094 
3095 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3096 
3097 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3098 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3099 		list_del(&cf->list);
3100 		kfree(cf);
3101 		adapter->num_cloud_filters--;
3102 	}
3103 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3104 
3105 	iavf_free_misc_irq(adapter);
3106 	iavf_free_interrupt_scheme(adapter);
3107 	memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
3108 	iavf_shutdown_adminq(&adapter->hw);
3109 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3110 	iavf_change_state(adapter, __IAVF_DOWN);
3111 	wake_up(&adapter->down_waitqueue);
3112 	dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
3113 }
3114 
3115 /**
3116  * iavf_reconfig_qs_bw - Call-back task to handle hardware reset
3117  * @adapter: board private structure
3118  *
3119  * After a reset, the shaper parameters of queues need to be replayed again.
3120  * Since the net_shaper object inside TX rings persists across reset,
3121  * set the update flag for all queues so that the virtchnl message is triggered
3122  * for all queues.
3123  **/
iavf_reconfig_qs_bw(struct iavf_adapter * adapter)3124 static void iavf_reconfig_qs_bw(struct iavf_adapter *adapter)
3125 {
3126 	int i, num = 0;
3127 
3128 	for (i = 0; i < adapter->num_active_queues; i++)
3129 		if (adapter->tx_rings[i].q_shaper.bw_min ||
3130 		    adapter->tx_rings[i].q_shaper.bw_max) {
3131 			adapter->tx_rings[i].q_shaper_update = true;
3132 			num++;
3133 		}
3134 
3135 	if (num)
3136 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
3137 }
3138 
3139 /**
3140  * iavf_reset_task - Call-back task to handle hardware reset
3141  * @work: pointer to work_struct
3142  *
3143  * During reset we need to shut down and reinitialize the admin queue
3144  * before we can use it to communicate with the PF again. We also clear
3145  * and reinit the rings because that context is lost as well.
3146  **/
iavf_reset_task(struct work_struct * work)3147 static void iavf_reset_task(struct work_struct *work)
3148 {
3149 	struct iavf_adapter *adapter = container_of(work,
3150 						      struct iavf_adapter,
3151 						      reset_task);
3152 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3153 	struct net_device *netdev = adapter->netdev;
3154 	struct iavf_hw *hw = &adapter->hw;
3155 	struct iavf_mac_filter *f, *ftmp;
3156 	struct iavf_cloud_filter *cf;
3157 	enum iavf_status status;
3158 	u32 reg_val;
3159 	int i = 0, err;
3160 	bool running;
3161 
3162 	netdev_lock(netdev);
3163 
3164 	iavf_misc_irq_disable(adapter);
3165 	if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
3166 		adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
3167 		/* Restart the AQ here. If we have been reset but didn't
3168 		 * detect it, or if the PF had to reinit, our AQ will be hosed.
3169 		 */
3170 		iavf_shutdown_adminq(hw);
3171 		iavf_init_adminq(hw);
3172 		iavf_request_reset(adapter);
3173 	}
3174 	adapter->flags |= IAVF_FLAG_RESET_PENDING;
3175 
3176 	/* poll until we see the reset actually happen */
3177 	for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
3178 		reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
3179 			  IAVF_VF_ARQLEN1_ARQENABLE_MASK;
3180 		if (!reg_val)
3181 			break;
3182 		usleep_range(5000, 10000);
3183 	}
3184 	if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
3185 		dev_info(&adapter->pdev->dev, "Never saw reset\n");
3186 		goto continue_reset; /* act like the reset happened */
3187 	}
3188 
3189 	/* wait until the reset is complete and the PF is responding to us */
3190 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3191 		/* sleep first to make sure a minimum wait time is met */
3192 		msleep(IAVF_RESET_WAIT_MS);
3193 
3194 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
3195 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3196 		if (reg_val == VIRTCHNL_VFR_VFACTIVE)
3197 			break;
3198 	}
3199 
3200 	pci_set_master(adapter->pdev);
3201 	pci_restore_msi_state(adapter->pdev);
3202 
3203 	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
3204 		dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
3205 			reg_val);
3206 		iavf_disable_vf(adapter);
3207 		netdev_unlock(netdev);
3208 		return; /* Do not attempt to reinit. It's dead, Jim. */
3209 	}
3210 
3211 continue_reset:
3212 	/* If we are still early in the state machine, just restart. */
3213 	if (adapter->state <= __IAVF_INIT_FAILED) {
3214 		iavf_shutdown_adminq(hw);
3215 		iavf_change_state(adapter, __IAVF_STARTUP);
3216 		iavf_startup(adapter);
3217 		queue_delayed_work(adapter->wq, &adapter->watchdog_task,
3218 				   msecs_to_jiffies(30));
3219 		netdev_unlock(netdev);
3220 		return;
3221 	}
3222 
3223 	/* We don't use netif_running() because it may be true prior to
3224 	 * ndo_open() returning, so we can't assume it means all our open
3225 	 * tasks have finished, since we're not holding the rtnl_lock here.
3226 	 */
3227 	running = adapter->state == __IAVF_RUNNING;
3228 
3229 	if (running) {
3230 		netif_carrier_off(netdev);
3231 		netif_tx_stop_all_queues(netdev);
3232 		adapter->link_up = false;
3233 		iavf_napi_disable_all(adapter);
3234 	}
3235 	iavf_irq_disable(adapter);
3236 
3237 	iavf_change_state(adapter, __IAVF_RESETTING);
3238 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3239 
3240 	/* free the Tx/Rx rings and descriptors, might be better to just
3241 	 * re-use them sometime in the future
3242 	 */
3243 	iavf_free_all_rx_resources(adapter);
3244 	iavf_free_all_tx_resources(adapter);
3245 
3246 	adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
3247 	/* kill and reinit the admin queue */
3248 	iavf_shutdown_adminq(hw);
3249 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3250 	status = iavf_init_adminq(hw);
3251 	if (status) {
3252 		dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
3253 			 status);
3254 		goto reset_err;
3255 	}
3256 	adapter->aq_required = 0;
3257 
3258 	if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3259 	    (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3260 		err = iavf_reinit_interrupt_scheme(adapter, running);
3261 		if (err)
3262 			goto reset_err;
3263 	}
3264 
3265 	if (RSS_AQ(adapter)) {
3266 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3267 	} else {
3268 		err = iavf_init_rss(adapter);
3269 		if (err)
3270 			goto reset_err;
3271 	}
3272 
3273 	adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
3274 	adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
3275 
3276 	/* Certain capabilities require an extended negotiation process using
3277 	 * extra messages that must be processed after getting the VF
3278 	 * configuration. The related checks such as VLAN_V2_ALLOWED() are not
3279 	 * reliable here, since the configuration has not yet been negotiated.
3280 	 *
3281 	 * Always set these flags, since them related VIRTCHNL messages won't
3282 	 * be sent until after VIRTCHNL_OP_GET_VF_RESOURCES.
3283 	 */
3284 	adapter->aq_required |= IAVF_FLAG_AQ_EXTENDED_CAPS;
3285 
3286 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3287 
3288 	/* Delete filter for the current MAC address, it could have
3289 	 * been changed by the PF via administratively set MAC.
3290 	 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
3291 	 */
3292 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3293 		if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
3294 			list_del(&f->list);
3295 			kfree(f);
3296 		}
3297 	}
3298 	/* re-add all MAC filters */
3299 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
3300 		f->add = true;
3301 	}
3302 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3303 
3304 	/* check if TCs are running and re-add all cloud filters */
3305 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3306 	if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
3307 	    adapter->num_tc) {
3308 		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
3309 			cf->add = true;
3310 		}
3311 	}
3312 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3313 
3314 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
3315 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3316 	iavf_misc_irq_enable(adapter);
3317 
3318 	mod_delayed_work(adapter->wq, &adapter->watchdog_task, 2);
3319 
3320 	/* We were running when the reset started, so we need to restore some
3321 	 * state here.
3322 	 */
3323 	if (running) {
3324 		/* allocate transmit descriptors */
3325 		err = iavf_setup_all_tx_resources(adapter);
3326 		if (err)
3327 			goto reset_err;
3328 
3329 		/* allocate receive descriptors */
3330 		err = iavf_setup_all_rx_resources(adapter);
3331 		if (err)
3332 			goto reset_err;
3333 
3334 		if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3335 		    (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3336 			err = iavf_request_traffic_irqs(adapter, netdev->name);
3337 			if (err)
3338 				goto reset_err;
3339 
3340 			adapter->flags &= ~IAVF_FLAG_REINIT_MSIX_NEEDED;
3341 		}
3342 
3343 		iavf_configure(adapter);
3344 
3345 		/* iavf_up_complete() will switch device back
3346 		 * to __IAVF_RUNNING
3347 		 */
3348 		iavf_up_complete(adapter);
3349 
3350 		iavf_irq_enable(adapter, true);
3351 
3352 		iavf_reconfig_qs_bw(adapter);
3353 	} else {
3354 		iavf_change_state(adapter, __IAVF_DOWN);
3355 		wake_up(&adapter->down_waitqueue);
3356 	}
3357 
3358 	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3359 
3360 	wake_up(&adapter->reset_waitqueue);
3361 	netdev_unlock(netdev);
3362 
3363 	return;
3364 reset_err:
3365 	if (running) {
3366 		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3367 		iavf_free_traffic_irqs(adapter);
3368 	}
3369 	iavf_disable_vf(adapter);
3370 
3371 	netdev_unlock(netdev);
3372 	dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
3373 }
3374 
3375 /**
3376  * iavf_adminq_task - worker thread to clean the admin queue
3377  * @work: pointer to work_struct containing our data
3378  **/
iavf_adminq_task(struct work_struct * work)3379 static void iavf_adminq_task(struct work_struct *work)
3380 {
3381 	struct iavf_adapter *adapter =
3382 		container_of(work, struct iavf_adapter, adminq_task);
3383 	struct net_device *netdev = adapter->netdev;
3384 	struct iavf_hw *hw = &adapter->hw;
3385 	struct iavf_arq_event_info event;
3386 	enum virtchnl_ops v_op;
3387 	enum iavf_status ret, v_ret;
3388 	u32 val, oldval;
3389 	u16 pending;
3390 
3391 	netdev_lock(netdev);
3392 
3393 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
3394 		goto unlock;
3395 
3396 	event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
3397 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
3398 	if (!event.msg_buf)
3399 		goto unlock;
3400 
3401 	do {
3402 		ret = iavf_clean_arq_element(hw, &event, &pending);
3403 		v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
3404 		v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
3405 
3406 		if (ret || !v_op)
3407 			break; /* No event to process or error cleaning ARQ */
3408 
3409 		iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
3410 					 event.msg_len);
3411 		if (pending != 0)
3412 			memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
3413 	} while (pending);
3414 
3415 	if (iavf_is_reset_in_progress(adapter))
3416 		goto freedom;
3417 
3418 	/* check for error indications */
3419 	val = rd32(hw, IAVF_VF_ARQLEN1);
3420 	if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
3421 		goto freedom;
3422 	oldval = val;
3423 	if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
3424 		dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
3425 		val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
3426 	}
3427 	if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
3428 		dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
3429 		val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
3430 	}
3431 	if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
3432 		dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
3433 		val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
3434 	}
3435 	if (oldval != val)
3436 		wr32(hw, IAVF_VF_ARQLEN1, val);
3437 
3438 	val = rd32(hw, IAVF_VF_ATQLEN1);
3439 	oldval = val;
3440 	if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
3441 		dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
3442 		val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
3443 	}
3444 	if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
3445 		dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
3446 		val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
3447 	}
3448 	if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
3449 		dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
3450 		val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
3451 	}
3452 	if (oldval != val)
3453 		wr32(hw, IAVF_VF_ATQLEN1, val);
3454 
3455 freedom:
3456 	kfree(event.msg_buf);
3457 unlock:
3458 	netdev_unlock(netdev);
3459 	/* re-enable Admin queue interrupt cause */
3460 	iavf_misc_irq_enable(adapter);
3461 }
3462 
3463 /**
3464  * iavf_free_all_tx_resources - Free Tx Resources for All Queues
3465  * @adapter: board private structure
3466  *
3467  * Free all transmit software resources
3468  **/
iavf_free_all_tx_resources(struct iavf_adapter * adapter)3469 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
3470 {
3471 	int i;
3472 
3473 	if (!adapter->tx_rings)
3474 		return;
3475 
3476 	for (i = 0; i < adapter->num_active_queues; i++)
3477 		if (adapter->tx_rings[i].desc)
3478 			iavf_free_tx_resources(&adapter->tx_rings[i]);
3479 }
3480 
3481 /**
3482  * iavf_setup_all_tx_resources - allocate all queues Tx resources
3483  * @adapter: board private structure
3484  *
3485  * If this function returns with an error, then it's possible one or
3486  * more of the rings is populated (while the rest are not).  It is the
3487  * callers duty to clean those orphaned rings.
3488  *
3489  * Return 0 on success, negative on failure
3490  **/
iavf_setup_all_tx_resources(struct iavf_adapter * adapter)3491 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
3492 {
3493 	int i, err = 0;
3494 
3495 	for (i = 0; i < adapter->num_active_queues; i++) {
3496 		adapter->tx_rings[i].count = adapter->tx_desc_count;
3497 		err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
3498 		if (!err)
3499 			continue;
3500 		dev_err(&adapter->pdev->dev,
3501 			"Allocation for Tx Queue %u failed\n", i);
3502 		break;
3503 	}
3504 
3505 	return err;
3506 }
3507 
3508 /**
3509  * iavf_setup_all_rx_resources - allocate all queues Rx resources
3510  * @adapter: board private structure
3511  *
3512  * If this function returns with an error, then it's possible one or
3513  * more of the rings is populated (while the rest are not).  It is the
3514  * callers duty to clean those orphaned rings.
3515  *
3516  * Return 0 on success, negative on failure
3517  **/
iavf_setup_all_rx_resources(struct iavf_adapter * adapter)3518 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
3519 {
3520 	int i, err = 0;
3521 
3522 	for (i = 0; i < adapter->num_active_queues; i++) {
3523 		adapter->rx_rings[i].count = adapter->rx_desc_count;
3524 		err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
3525 		if (!err)
3526 			continue;
3527 		dev_err(&adapter->pdev->dev,
3528 			"Allocation for Rx Queue %u failed\n", i);
3529 		break;
3530 	}
3531 	return err;
3532 }
3533 
3534 /**
3535  * iavf_free_all_rx_resources - Free Rx Resources for All Queues
3536  * @adapter: board private structure
3537  *
3538  * Free all receive software resources
3539  **/
iavf_free_all_rx_resources(struct iavf_adapter * adapter)3540 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
3541 {
3542 	int i;
3543 
3544 	if (!adapter->rx_rings)
3545 		return;
3546 
3547 	for (i = 0; i < adapter->num_active_queues; i++)
3548 		if (adapter->rx_rings[i].desc)
3549 			iavf_free_rx_resources(&adapter->rx_rings[i]);
3550 }
3551 
3552 /**
3553  * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
3554  * @adapter: board private structure
3555  * @max_tx_rate: max Tx bw for a tc
3556  **/
iavf_validate_tx_bandwidth(struct iavf_adapter * adapter,u64 max_tx_rate)3557 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
3558 				      u64 max_tx_rate)
3559 {
3560 	int speed = 0, ret = 0;
3561 
3562 	if (ADV_LINK_SUPPORT(adapter)) {
3563 		if (adapter->link_speed_mbps < U32_MAX) {
3564 			speed = adapter->link_speed_mbps;
3565 			goto validate_bw;
3566 		} else {
3567 			dev_err(&adapter->pdev->dev, "Unknown link speed\n");
3568 			return -EINVAL;
3569 		}
3570 	}
3571 
3572 	switch (adapter->link_speed) {
3573 	case VIRTCHNL_LINK_SPEED_40GB:
3574 		speed = SPEED_40000;
3575 		break;
3576 	case VIRTCHNL_LINK_SPEED_25GB:
3577 		speed = SPEED_25000;
3578 		break;
3579 	case VIRTCHNL_LINK_SPEED_20GB:
3580 		speed = SPEED_20000;
3581 		break;
3582 	case VIRTCHNL_LINK_SPEED_10GB:
3583 		speed = SPEED_10000;
3584 		break;
3585 	case VIRTCHNL_LINK_SPEED_5GB:
3586 		speed = SPEED_5000;
3587 		break;
3588 	case VIRTCHNL_LINK_SPEED_2_5GB:
3589 		speed = SPEED_2500;
3590 		break;
3591 	case VIRTCHNL_LINK_SPEED_1GB:
3592 		speed = SPEED_1000;
3593 		break;
3594 	case VIRTCHNL_LINK_SPEED_100MB:
3595 		speed = SPEED_100;
3596 		break;
3597 	default:
3598 		break;
3599 	}
3600 
3601 validate_bw:
3602 	if (max_tx_rate > speed) {
3603 		dev_err(&adapter->pdev->dev,
3604 			"Invalid tx rate specified\n");
3605 		ret = -EINVAL;
3606 	}
3607 
3608 	return ret;
3609 }
3610 
3611 /**
3612  * iavf_validate_ch_config - validate queue mapping info
3613  * @adapter: board private structure
3614  * @mqprio_qopt: queue parameters
3615  *
3616  * This function validates if the config provided by the user to
3617  * configure queue channels is valid or not. Returns 0 on a valid
3618  * config.
3619  **/
iavf_validate_ch_config(struct iavf_adapter * adapter,struct tc_mqprio_qopt_offload * mqprio_qopt)3620 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
3621 				   struct tc_mqprio_qopt_offload *mqprio_qopt)
3622 {
3623 	u64 total_max_rate = 0;
3624 	u32 tx_rate_rem = 0;
3625 	int i, num_qps = 0;
3626 	u64 tx_rate = 0;
3627 	int ret = 0;
3628 
3629 	if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
3630 	    mqprio_qopt->qopt.num_tc < 1)
3631 		return -EINVAL;
3632 
3633 	for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
3634 		if (!mqprio_qopt->qopt.count[i] ||
3635 		    mqprio_qopt->qopt.offset[i] != num_qps)
3636 			return -EINVAL;
3637 		if (mqprio_qopt->min_rate[i]) {
3638 			dev_err(&adapter->pdev->dev,
3639 				"Invalid min tx rate (greater than 0) specified for TC%d\n",
3640 				i);
3641 			return -EINVAL;
3642 		}
3643 
3644 		/* convert to Mbps */
3645 		tx_rate = div_u64(mqprio_qopt->max_rate[i],
3646 				  IAVF_MBPS_DIVISOR);
3647 
3648 		if (mqprio_qopt->max_rate[i] &&
3649 		    tx_rate < IAVF_MBPS_QUANTA) {
3650 			dev_err(&adapter->pdev->dev,
3651 				"Invalid max tx rate for TC%d, minimum %dMbps\n",
3652 				i, IAVF_MBPS_QUANTA);
3653 			return -EINVAL;
3654 		}
3655 
3656 		(void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
3657 
3658 		if (tx_rate_rem != 0) {
3659 			dev_err(&adapter->pdev->dev,
3660 				"Invalid max tx rate for TC%d, not divisible by %d\n",
3661 				i, IAVF_MBPS_QUANTA);
3662 			return -EINVAL;
3663 		}
3664 
3665 		total_max_rate += tx_rate;
3666 		num_qps += mqprio_qopt->qopt.count[i];
3667 	}
3668 	if (num_qps > adapter->num_active_queues) {
3669 		dev_err(&adapter->pdev->dev,
3670 			"Cannot support requested number of queues\n");
3671 		return -EINVAL;
3672 	}
3673 
3674 	ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
3675 	return ret;
3676 }
3677 
3678 /**
3679  * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
3680  * @adapter: board private structure
3681  **/
iavf_del_all_cloud_filters(struct iavf_adapter * adapter)3682 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
3683 {
3684 	struct iavf_cloud_filter *cf, *cftmp;
3685 
3686 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3687 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
3688 				 list) {
3689 		list_del(&cf->list);
3690 		kfree(cf);
3691 		adapter->num_cloud_filters--;
3692 	}
3693 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3694 }
3695 
3696 /**
3697  * iavf_is_tc_config_same - Compare the mqprio TC config with the
3698  * TC config already configured on this adapter.
3699  * @adapter: board private structure
3700  * @mqprio_qopt: TC config received from kernel.
3701  *
3702  * This function compares the TC config received from the kernel
3703  * with the config already configured on the adapter.
3704  *
3705  * Return: True if configuration is same, false otherwise.
3706  **/
iavf_is_tc_config_same(struct iavf_adapter * adapter,struct tc_mqprio_qopt * mqprio_qopt)3707 static bool iavf_is_tc_config_same(struct iavf_adapter *adapter,
3708 				   struct tc_mqprio_qopt *mqprio_qopt)
3709 {
3710 	struct virtchnl_channel_info *ch = &adapter->ch_config.ch_info[0];
3711 	int i;
3712 
3713 	if (adapter->num_tc != mqprio_qopt->num_tc)
3714 		return false;
3715 
3716 	for (i = 0; i < adapter->num_tc; i++) {
3717 		if (ch[i].count != mqprio_qopt->count[i] ||
3718 		    ch[i].offset != mqprio_qopt->offset[i])
3719 			return false;
3720 	}
3721 	return true;
3722 }
3723 
3724 /**
3725  * __iavf_setup_tc - configure multiple traffic classes
3726  * @netdev: network interface device structure
3727  * @type_data: tc offload data
3728  *
3729  * This function processes the config information provided by the
3730  * user to configure traffic classes/queue channels and packages the
3731  * information to request the PF to setup traffic classes.
3732  *
3733  * Returns 0 on success.
3734  **/
__iavf_setup_tc(struct net_device * netdev,void * type_data)3735 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
3736 {
3737 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
3738 	struct iavf_adapter *adapter = netdev_priv(netdev);
3739 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3740 	u8 num_tc = 0, total_qps = 0;
3741 	int ret = 0, netdev_tc = 0;
3742 	u64 max_tx_rate;
3743 	u16 mode;
3744 	int i;
3745 
3746 	num_tc = mqprio_qopt->qopt.num_tc;
3747 	mode = mqprio_qopt->mode;
3748 
3749 	/* delete queue_channel */
3750 	if (!mqprio_qopt->qopt.hw) {
3751 		if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
3752 			/* reset the tc configuration */
3753 			netdev_reset_tc(netdev);
3754 			adapter->num_tc = 0;
3755 			netif_tx_stop_all_queues(netdev);
3756 			netif_tx_disable(netdev);
3757 			iavf_del_all_cloud_filters(adapter);
3758 			adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
3759 			total_qps = adapter->orig_num_active_queues;
3760 			goto exit;
3761 		} else {
3762 			return -EINVAL;
3763 		}
3764 	}
3765 
3766 	/* add queue channel */
3767 	if (mode == TC_MQPRIO_MODE_CHANNEL) {
3768 		if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3769 			dev_err(&adapter->pdev->dev, "ADq not supported\n");
3770 			return -EOPNOTSUPP;
3771 		}
3772 		if (adapter->ch_config.state != __IAVF_TC_INVALID) {
3773 			dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
3774 			return -EINVAL;
3775 		}
3776 
3777 		ret = iavf_validate_ch_config(adapter, mqprio_qopt);
3778 		if (ret)
3779 			return ret;
3780 		/* Return if same TC config is requested */
3781 		if (iavf_is_tc_config_same(adapter, &mqprio_qopt->qopt))
3782 			return 0;
3783 		adapter->num_tc = num_tc;
3784 
3785 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3786 			if (i < num_tc) {
3787 				adapter->ch_config.ch_info[i].count =
3788 					mqprio_qopt->qopt.count[i];
3789 				adapter->ch_config.ch_info[i].offset =
3790 					mqprio_qopt->qopt.offset[i];
3791 				total_qps += mqprio_qopt->qopt.count[i];
3792 				max_tx_rate = mqprio_qopt->max_rate[i];
3793 				/* convert to Mbps */
3794 				max_tx_rate = div_u64(max_tx_rate,
3795 						      IAVF_MBPS_DIVISOR);
3796 				adapter->ch_config.ch_info[i].max_tx_rate =
3797 					max_tx_rate;
3798 			} else {
3799 				adapter->ch_config.ch_info[i].count = 1;
3800 				adapter->ch_config.ch_info[i].offset = 0;
3801 			}
3802 		}
3803 
3804 		/* Take snapshot of original config such as "num_active_queues"
3805 		 * It is used later when delete ADQ flow is exercised, so that
3806 		 * once delete ADQ flow completes, VF shall go back to its
3807 		 * original queue configuration
3808 		 */
3809 
3810 		adapter->orig_num_active_queues = adapter->num_active_queues;
3811 
3812 		/* Store queue info based on TC so that VF gets configured
3813 		 * with correct number of queues when VF completes ADQ config
3814 		 * flow
3815 		 */
3816 		adapter->ch_config.total_qps = total_qps;
3817 
3818 		netif_tx_stop_all_queues(netdev);
3819 		netif_tx_disable(netdev);
3820 		adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
3821 		netdev_reset_tc(netdev);
3822 		/* Report the tc mapping up the stack */
3823 		netdev_set_num_tc(adapter->netdev, num_tc);
3824 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3825 			u16 qcount = mqprio_qopt->qopt.count[i];
3826 			u16 qoffset = mqprio_qopt->qopt.offset[i];
3827 
3828 			if (i < num_tc)
3829 				netdev_set_tc_queue(netdev, netdev_tc++, qcount,
3830 						    qoffset);
3831 		}
3832 	}
3833 exit:
3834 	if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
3835 		return 0;
3836 
3837 	netif_set_real_num_rx_queues(netdev, total_qps);
3838 	netif_set_real_num_tx_queues(netdev, total_qps);
3839 
3840 	return ret;
3841 }
3842 
3843 /**
3844  * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
3845  * @adapter: board private structure
3846  * @f: pointer to struct flow_cls_offload
3847  * @filter: pointer to cloud filter structure
3848  */
iavf_parse_cls_flower(struct iavf_adapter * adapter,struct flow_cls_offload * f,struct iavf_cloud_filter * filter)3849 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
3850 				 struct flow_cls_offload *f,
3851 				 struct iavf_cloud_filter *filter)
3852 {
3853 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3854 	struct flow_dissector *dissector = rule->match.dissector;
3855 	u16 n_proto_mask = 0;
3856 	u16 n_proto_key = 0;
3857 	u8 field_flags = 0;
3858 	u16 addr_type = 0;
3859 	u16 n_proto = 0;
3860 	int i = 0;
3861 	struct virtchnl_filter *vf = &filter->f;
3862 
3863 	if (dissector->used_keys &
3864 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
3865 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
3866 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
3867 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
3868 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
3869 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
3870 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
3871 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
3872 		dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%llx\n",
3873 			dissector->used_keys);
3874 		return -EOPNOTSUPP;
3875 	}
3876 
3877 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
3878 		struct flow_match_enc_keyid match;
3879 
3880 		flow_rule_match_enc_keyid(rule, &match);
3881 		if (match.mask->keyid != 0)
3882 			field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
3883 	}
3884 
3885 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
3886 		struct flow_match_basic match;
3887 
3888 		flow_rule_match_basic(rule, &match);
3889 		n_proto_key = ntohs(match.key->n_proto);
3890 		n_proto_mask = ntohs(match.mask->n_proto);
3891 
3892 		if (n_proto_key == ETH_P_ALL) {
3893 			n_proto_key = 0;
3894 			n_proto_mask = 0;
3895 		}
3896 		n_proto = n_proto_key & n_proto_mask;
3897 		if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
3898 			return -EINVAL;
3899 		if (n_proto == ETH_P_IPV6) {
3900 			/* specify flow type as TCP IPv6 */
3901 			vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
3902 		}
3903 
3904 		if (match.key->ip_proto != IPPROTO_TCP) {
3905 			dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
3906 			return -EINVAL;
3907 		}
3908 	}
3909 
3910 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
3911 		struct flow_match_eth_addrs match;
3912 
3913 		flow_rule_match_eth_addrs(rule, &match);
3914 
3915 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
3916 		if (!is_zero_ether_addr(match.mask->dst)) {
3917 			if (is_broadcast_ether_addr(match.mask->dst)) {
3918 				field_flags |= IAVF_CLOUD_FIELD_OMAC;
3919 			} else {
3920 				dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
3921 					match.mask->dst);
3922 				return -EINVAL;
3923 			}
3924 		}
3925 
3926 		if (!is_zero_ether_addr(match.mask->src)) {
3927 			if (is_broadcast_ether_addr(match.mask->src)) {
3928 				field_flags |= IAVF_CLOUD_FIELD_IMAC;
3929 			} else {
3930 				dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
3931 					match.mask->src);
3932 				return -EINVAL;
3933 			}
3934 		}
3935 
3936 		if (!is_zero_ether_addr(match.key->dst))
3937 			if (is_valid_ether_addr(match.key->dst) ||
3938 			    is_multicast_ether_addr(match.key->dst)) {
3939 				/* set the mask if a valid dst_mac address */
3940 				for (i = 0; i < ETH_ALEN; i++)
3941 					vf->mask.tcp_spec.dst_mac[i] |= 0xff;
3942 				ether_addr_copy(vf->data.tcp_spec.dst_mac,
3943 						match.key->dst);
3944 			}
3945 
3946 		if (!is_zero_ether_addr(match.key->src))
3947 			if (is_valid_ether_addr(match.key->src) ||
3948 			    is_multicast_ether_addr(match.key->src)) {
3949 				/* set the mask if a valid dst_mac address */
3950 				for (i = 0; i < ETH_ALEN; i++)
3951 					vf->mask.tcp_spec.src_mac[i] |= 0xff;
3952 				ether_addr_copy(vf->data.tcp_spec.src_mac,
3953 						match.key->src);
3954 		}
3955 	}
3956 
3957 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
3958 		struct flow_match_vlan match;
3959 
3960 		flow_rule_match_vlan(rule, &match);
3961 		if (match.mask->vlan_id) {
3962 			if (match.mask->vlan_id == VLAN_VID_MASK) {
3963 				field_flags |= IAVF_CLOUD_FIELD_IVLAN;
3964 			} else {
3965 				dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
3966 					match.mask->vlan_id);
3967 				return -EINVAL;
3968 			}
3969 		}
3970 		vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
3971 		vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
3972 	}
3973 
3974 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
3975 		struct flow_match_control match;
3976 
3977 		flow_rule_match_control(rule, &match);
3978 		addr_type = match.key->addr_type;
3979 
3980 		if (flow_rule_has_control_flags(match.mask->flags,
3981 						f->common.extack))
3982 			return -EOPNOTSUPP;
3983 	}
3984 
3985 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
3986 		struct flow_match_ipv4_addrs match;
3987 
3988 		flow_rule_match_ipv4_addrs(rule, &match);
3989 		if (match.mask->dst) {
3990 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
3991 				field_flags |= IAVF_CLOUD_FIELD_IIP;
3992 			} else {
3993 				dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
3994 					be32_to_cpu(match.mask->dst));
3995 				return -EINVAL;
3996 			}
3997 		}
3998 
3999 		if (match.mask->src) {
4000 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
4001 				field_flags |= IAVF_CLOUD_FIELD_IIP;
4002 			} else {
4003 				dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
4004 					be32_to_cpu(match.mask->src));
4005 				return -EINVAL;
4006 			}
4007 		}
4008 
4009 		if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
4010 			dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
4011 			return -EINVAL;
4012 		}
4013 		if (match.key->dst) {
4014 			vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
4015 			vf->data.tcp_spec.dst_ip[0] = match.key->dst;
4016 		}
4017 		if (match.key->src) {
4018 			vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
4019 			vf->data.tcp_spec.src_ip[0] = match.key->src;
4020 		}
4021 	}
4022 
4023 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
4024 		struct flow_match_ipv6_addrs match;
4025 
4026 		flow_rule_match_ipv6_addrs(rule, &match);
4027 
4028 		/* validate mask, make sure it is not IPV6_ADDR_ANY */
4029 		if (ipv6_addr_any(&match.mask->dst)) {
4030 			dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
4031 				IPV6_ADDR_ANY);
4032 			return -EINVAL;
4033 		}
4034 
4035 		/* src and dest IPv6 address should not be LOOPBACK
4036 		 * (0:0:0:0:0:0:0:1) which can be represented as ::1
4037 		 */
4038 		if (ipv6_addr_loopback(&match.key->dst) ||
4039 		    ipv6_addr_loopback(&match.key->src)) {
4040 			dev_err(&adapter->pdev->dev,
4041 				"ipv6 addr should not be loopback\n");
4042 			return -EINVAL;
4043 		}
4044 		if (!ipv6_addr_any(&match.mask->dst) ||
4045 		    !ipv6_addr_any(&match.mask->src))
4046 			field_flags |= IAVF_CLOUD_FIELD_IIP;
4047 
4048 		for (i = 0; i < 4; i++)
4049 			vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
4050 		memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
4051 		       sizeof(vf->data.tcp_spec.dst_ip));
4052 		for (i = 0; i < 4; i++)
4053 			vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
4054 		memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
4055 		       sizeof(vf->data.tcp_spec.src_ip));
4056 	}
4057 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
4058 		struct flow_match_ports match;
4059 
4060 		flow_rule_match_ports(rule, &match);
4061 		if (match.mask->src) {
4062 			if (match.mask->src == cpu_to_be16(0xffff)) {
4063 				field_flags |= IAVF_CLOUD_FIELD_IIP;
4064 			} else {
4065 				dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
4066 					be16_to_cpu(match.mask->src));
4067 				return -EINVAL;
4068 			}
4069 		}
4070 
4071 		if (match.mask->dst) {
4072 			if (match.mask->dst == cpu_to_be16(0xffff)) {
4073 				field_flags |= IAVF_CLOUD_FIELD_IIP;
4074 			} else {
4075 				dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
4076 					be16_to_cpu(match.mask->dst));
4077 				return -EINVAL;
4078 			}
4079 		}
4080 		if (match.key->dst) {
4081 			vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
4082 			vf->data.tcp_spec.dst_port = match.key->dst;
4083 		}
4084 
4085 		if (match.key->src) {
4086 			vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
4087 			vf->data.tcp_spec.src_port = match.key->src;
4088 		}
4089 	}
4090 	vf->field_flags = field_flags;
4091 
4092 	return 0;
4093 }
4094 
4095 /**
4096  * iavf_handle_tclass - Forward to a traffic class on the device
4097  * @adapter: board private structure
4098  * @tc: traffic class index on the device
4099  * @filter: pointer to cloud filter structure
4100  */
iavf_handle_tclass(struct iavf_adapter * adapter,u32 tc,struct iavf_cloud_filter * filter)4101 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
4102 			      struct iavf_cloud_filter *filter)
4103 {
4104 	if (tc == 0)
4105 		return 0;
4106 	if (tc < adapter->num_tc) {
4107 		if (!filter->f.data.tcp_spec.dst_port) {
4108 			dev_err(&adapter->pdev->dev,
4109 				"Specify destination port to redirect to traffic class other than TC0\n");
4110 			return -EINVAL;
4111 		}
4112 	}
4113 	/* redirect to a traffic class on the same device */
4114 	filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
4115 	filter->f.action_meta = tc;
4116 	return 0;
4117 }
4118 
4119 /**
4120  * iavf_find_cf - Find the cloud filter in the list
4121  * @adapter: Board private structure
4122  * @cookie: filter specific cookie
4123  *
4124  * Returns ptr to the filter object or NULL. Must be called while holding the
4125  * cloud_filter_list_lock.
4126  */
iavf_find_cf(struct iavf_adapter * adapter,unsigned long * cookie)4127 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
4128 					      unsigned long *cookie)
4129 {
4130 	struct iavf_cloud_filter *filter = NULL;
4131 
4132 	if (!cookie)
4133 		return NULL;
4134 
4135 	list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
4136 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
4137 			return filter;
4138 	}
4139 	return NULL;
4140 }
4141 
4142 /**
4143  * iavf_configure_clsflower - Add tc flower filters
4144  * @adapter: board private structure
4145  * @cls_flower: Pointer to struct flow_cls_offload
4146  */
iavf_configure_clsflower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)4147 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
4148 				    struct flow_cls_offload *cls_flower)
4149 {
4150 	int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
4151 	struct iavf_cloud_filter *filter;
4152 	int err;
4153 
4154 	if (tc < 0) {
4155 		dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
4156 		return -EINVAL;
4157 	}
4158 
4159 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
4160 	if (!filter)
4161 		return -ENOMEM;
4162 	filter->cookie = cls_flower->cookie;
4163 
4164 	netdev_lock(adapter->netdev);
4165 
4166 	/* bail out here if filter already exists */
4167 	spin_lock_bh(&adapter->cloud_filter_list_lock);
4168 	if (iavf_find_cf(adapter, &cls_flower->cookie)) {
4169 		dev_err(&adapter->pdev->dev, "Failed to add TC Flower filter, it already exists\n");
4170 		err = -EEXIST;
4171 		goto spin_unlock;
4172 	}
4173 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4174 
4175 	/* set the mask to all zeroes to begin with */
4176 	memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
4177 	/* start out with flow type and eth type IPv4 to begin with */
4178 	filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
4179 	err = iavf_parse_cls_flower(adapter, cls_flower, filter);
4180 	if (err)
4181 		goto err;
4182 
4183 	err = iavf_handle_tclass(adapter, tc, filter);
4184 	if (err)
4185 		goto err;
4186 
4187 	/* add filter to the list */
4188 	spin_lock_bh(&adapter->cloud_filter_list_lock);
4189 	list_add_tail(&filter->list, &adapter->cloud_filter_list);
4190 	adapter->num_cloud_filters++;
4191 	filter->add = true;
4192 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
4193 spin_unlock:
4194 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4195 err:
4196 	if (err)
4197 		kfree(filter);
4198 
4199 	netdev_unlock(adapter->netdev);
4200 	return err;
4201 }
4202 
4203 /**
4204  * iavf_delete_clsflower - Remove tc flower filters
4205  * @adapter: board private structure
4206  * @cls_flower: Pointer to struct flow_cls_offload
4207  */
iavf_delete_clsflower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)4208 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
4209 				 struct flow_cls_offload *cls_flower)
4210 {
4211 	struct iavf_cloud_filter *filter = NULL;
4212 	int err = 0;
4213 
4214 	spin_lock_bh(&adapter->cloud_filter_list_lock);
4215 	filter = iavf_find_cf(adapter, &cls_flower->cookie);
4216 	if (filter) {
4217 		filter->del = true;
4218 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
4219 	} else {
4220 		err = -EINVAL;
4221 	}
4222 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4223 
4224 	return err;
4225 }
4226 
4227 /**
4228  * iavf_setup_tc_cls_flower - flower classifier offloads
4229  * @adapter: pointer to iavf adapter structure
4230  * @cls_flower: pointer to flow_cls_offload struct with flow info
4231  */
iavf_setup_tc_cls_flower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)4232 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
4233 				    struct flow_cls_offload *cls_flower)
4234 {
4235 	switch (cls_flower->command) {
4236 	case FLOW_CLS_REPLACE:
4237 		return iavf_configure_clsflower(adapter, cls_flower);
4238 	case FLOW_CLS_DESTROY:
4239 		return iavf_delete_clsflower(adapter, cls_flower);
4240 	case FLOW_CLS_STATS:
4241 		return -EOPNOTSUPP;
4242 	default:
4243 		return -EOPNOTSUPP;
4244 	}
4245 }
4246 
4247 /**
4248  * iavf_add_cls_u32 - Add U32 classifier offloads
4249  * @adapter: pointer to iavf adapter structure
4250  * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4251  *
4252  * Return: 0 on success or negative errno on failure.
4253  */
iavf_add_cls_u32(struct iavf_adapter * adapter,struct tc_cls_u32_offload * cls_u32)4254 static int iavf_add_cls_u32(struct iavf_adapter *adapter,
4255 			    struct tc_cls_u32_offload *cls_u32)
4256 {
4257 	struct netlink_ext_ack *extack = cls_u32->common.extack;
4258 	struct virtchnl_fdir_rule *rule_cfg;
4259 	struct virtchnl_filter_action *vact;
4260 	struct virtchnl_proto_hdrs *hdrs;
4261 	struct ethhdr *spec_h, *mask_h;
4262 	const struct tc_action *act;
4263 	struct iavf_fdir_fltr *fltr;
4264 	struct tcf_exts *exts;
4265 	unsigned int q_index;
4266 	int i, status = 0;
4267 	int off_base = 0;
4268 
4269 	if (cls_u32->knode.link_handle) {
4270 		NL_SET_ERR_MSG_MOD(extack, "Linking not supported");
4271 		return -EOPNOTSUPP;
4272 	}
4273 
4274 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
4275 	if (!fltr)
4276 		return -ENOMEM;
4277 
4278 	rule_cfg = &fltr->vc_add_msg.rule_cfg;
4279 	hdrs = &rule_cfg->proto_hdrs;
4280 	hdrs->count = 0;
4281 
4282 	/* The parser lib at the PF expects the packet starting with MAC hdr */
4283 	switch (ntohs(cls_u32->common.protocol)) {
4284 	case ETH_P_802_3:
4285 		break;
4286 	case ETH_P_IP:
4287 		spec_h = (struct ethhdr *)hdrs->raw.spec;
4288 		mask_h = (struct ethhdr *)hdrs->raw.mask;
4289 		spec_h->h_proto = htons(ETH_P_IP);
4290 		mask_h->h_proto = htons(0xFFFF);
4291 		off_base += ETH_HLEN;
4292 		break;
4293 	default:
4294 		NL_SET_ERR_MSG_MOD(extack, "Only 802_3 and ip filter protocols are supported");
4295 		status = -EOPNOTSUPP;
4296 		goto free_alloc;
4297 	}
4298 
4299 	for (i = 0; i < cls_u32->knode.sel->nkeys; i++) {
4300 		__be32 val, mask;
4301 		int off;
4302 
4303 		off = off_base + cls_u32->knode.sel->keys[i].off;
4304 		val = cls_u32->knode.sel->keys[i].val;
4305 		mask = cls_u32->knode.sel->keys[i].mask;
4306 
4307 		if (off >= sizeof(hdrs->raw.spec)) {
4308 			NL_SET_ERR_MSG_MOD(extack, "Input exceeds maximum allowed.");
4309 			status = -EINVAL;
4310 			goto free_alloc;
4311 		}
4312 
4313 		memcpy(&hdrs->raw.spec[off], &val, sizeof(val));
4314 		memcpy(&hdrs->raw.mask[off], &mask, sizeof(mask));
4315 		hdrs->raw.pkt_len = off + sizeof(val);
4316 	}
4317 
4318 	/* Only one action is allowed */
4319 	rule_cfg->action_set.count = 1;
4320 	vact = &rule_cfg->action_set.actions[0];
4321 	exts = cls_u32->knode.exts;
4322 
4323 	tcf_exts_for_each_action(i, act, exts) {
4324 		/* FDIR queue */
4325 		if (is_tcf_skbedit_rx_queue_mapping(act)) {
4326 			q_index = tcf_skbedit_rx_queue_mapping(act);
4327 			if (q_index >= adapter->num_active_queues) {
4328 				status = -EINVAL;
4329 				goto free_alloc;
4330 			}
4331 
4332 			vact->type = VIRTCHNL_ACTION_QUEUE;
4333 			vact->act_conf.queue.index = q_index;
4334 			break;
4335 		}
4336 
4337 		/* Drop */
4338 		if (is_tcf_gact_shot(act)) {
4339 			vact->type = VIRTCHNL_ACTION_DROP;
4340 			break;
4341 		}
4342 
4343 		/* Unsupported action */
4344 		NL_SET_ERR_MSG_MOD(extack, "Unsupported action.");
4345 		status = -EOPNOTSUPP;
4346 		goto free_alloc;
4347 	}
4348 
4349 	fltr->vc_add_msg.vsi_id = adapter->vsi.id;
4350 	fltr->cls_u32_handle = cls_u32->knode.handle;
4351 	return iavf_fdir_add_fltr(adapter, fltr);
4352 
4353 free_alloc:
4354 	kfree(fltr);
4355 	return status;
4356 }
4357 
4358 /**
4359  * iavf_del_cls_u32 - Delete U32 classifier offloads
4360  * @adapter: pointer to iavf adapter structure
4361  * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4362  *
4363  * Return: 0 on success or negative errno on failure.
4364  */
iavf_del_cls_u32(struct iavf_adapter * adapter,struct tc_cls_u32_offload * cls_u32)4365 static int iavf_del_cls_u32(struct iavf_adapter *adapter,
4366 			    struct tc_cls_u32_offload *cls_u32)
4367 {
4368 	return iavf_fdir_del_fltr(adapter, true, cls_u32->knode.handle);
4369 }
4370 
4371 /**
4372  * iavf_setup_tc_cls_u32 - U32 filter offloads
4373  * @adapter: pointer to iavf adapter structure
4374  * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4375  *
4376  * Return: 0 on success or negative errno on failure.
4377  */
iavf_setup_tc_cls_u32(struct iavf_adapter * adapter,struct tc_cls_u32_offload * cls_u32)4378 static int iavf_setup_tc_cls_u32(struct iavf_adapter *adapter,
4379 				 struct tc_cls_u32_offload *cls_u32)
4380 {
4381 	if (!TC_U32_SUPPORT(adapter) || !FDIR_FLTR_SUPPORT(adapter))
4382 		return -EOPNOTSUPP;
4383 
4384 	switch (cls_u32->command) {
4385 	case TC_CLSU32_NEW_KNODE:
4386 	case TC_CLSU32_REPLACE_KNODE:
4387 		return iavf_add_cls_u32(adapter, cls_u32);
4388 	case TC_CLSU32_DELETE_KNODE:
4389 		return iavf_del_cls_u32(adapter, cls_u32);
4390 	default:
4391 		return -EOPNOTSUPP;
4392 	}
4393 }
4394 
4395 /**
4396  * iavf_setup_tc_block_cb - block callback for tc
4397  * @type: type of offload
4398  * @type_data: offload data
4399  * @cb_priv:
4400  *
4401  * This function is the block callback for traffic classes
4402  **/
iavf_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)4403 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4404 				  void *cb_priv)
4405 {
4406 	struct iavf_adapter *adapter = cb_priv;
4407 
4408 	if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
4409 		return -EOPNOTSUPP;
4410 
4411 	switch (type) {
4412 	case TC_SETUP_CLSFLOWER:
4413 		return iavf_setup_tc_cls_flower(cb_priv, type_data);
4414 	case TC_SETUP_CLSU32:
4415 		return iavf_setup_tc_cls_u32(cb_priv, type_data);
4416 	default:
4417 		return -EOPNOTSUPP;
4418 	}
4419 }
4420 
4421 static LIST_HEAD(iavf_block_cb_list);
4422 
4423 /**
4424  * iavf_setup_tc - configure multiple traffic classes
4425  * @netdev: network interface device structure
4426  * @type: type of offload
4427  * @type_data: tc offload data
4428  *
4429  * This function is the callback to ndo_setup_tc in the
4430  * netdev_ops.
4431  *
4432  * Returns 0 on success
4433  **/
iavf_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)4434 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4435 			 void *type_data)
4436 {
4437 	struct iavf_adapter *adapter = netdev_priv(netdev);
4438 
4439 	switch (type) {
4440 	case TC_SETUP_QDISC_MQPRIO:
4441 		return __iavf_setup_tc(netdev, type_data);
4442 	case TC_SETUP_BLOCK:
4443 		return flow_block_cb_setup_simple(type_data,
4444 						  &iavf_block_cb_list,
4445 						  iavf_setup_tc_block_cb,
4446 						  adapter, adapter, true);
4447 	default:
4448 		return -EOPNOTSUPP;
4449 	}
4450 }
4451 
4452 /**
4453  * iavf_restore_fdir_filters
4454  * @adapter: board private structure
4455  *
4456  * Restore existing FDIR filters when VF netdev comes back up.
4457  **/
iavf_restore_fdir_filters(struct iavf_adapter * adapter)4458 static void iavf_restore_fdir_filters(struct iavf_adapter *adapter)
4459 {
4460 	struct iavf_fdir_fltr *f;
4461 
4462 	spin_lock_bh(&adapter->fdir_fltr_lock);
4463 	list_for_each_entry(f, &adapter->fdir_list_head, list) {
4464 		if (f->state == IAVF_FDIR_FLTR_DIS_REQUEST) {
4465 			/* Cancel a request, keep filter as active */
4466 			f->state = IAVF_FDIR_FLTR_ACTIVE;
4467 		} else if (f->state == IAVF_FDIR_FLTR_DIS_PENDING ||
4468 			   f->state == IAVF_FDIR_FLTR_INACTIVE) {
4469 			/* Add filters which are inactive or have a pending
4470 			 * request to PF to be deleted
4471 			 */
4472 			f->state = IAVF_FDIR_FLTR_ADD_REQUEST;
4473 			adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
4474 		}
4475 	}
4476 	spin_unlock_bh(&adapter->fdir_fltr_lock);
4477 }
4478 
4479 /**
4480  * iavf_open - Called when a network interface is made active
4481  * @netdev: network interface device structure
4482  *
4483  * Returns 0 on success, negative value on failure
4484  *
4485  * The open entry point is called when a network interface is made
4486  * active by the system (IFF_UP).  At this point all resources needed
4487  * for transmit and receive operations are allocated, the interrupt
4488  * handler is registered with the OS, the watchdog is started,
4489  * and the stack is notified that the interface is ready.
4490  **/
iavf_open(struct net_device * netdev)4491 static int iavf_open(struct net_device *netdev)
4492 {
4493 	struct iavf_adapter *adapter = netdev_priv(netdev);
4494 	int err;
4495 
4496 	netdev_assert_locked(netdev);
4497 
4498 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
4499 		dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
4500 		return -EIO;
4501 	}
4502 
4503 	if (adapter->state != __IAVF_DOWN)
4504 		return -EBUSY;
4505 
4506 	if (adapter->state == __IAVF_RUNNING &&
4507 	    !test_bit(__IAVF_VSI_DOWN, adapter->vsi.state)) {
4508 		dev_dbg(&adapter->pdev->dev, "VF is already open.\n");
4509 		return 0;
4510 	}
4511 
4512 	/* allocate transmit descriptors */
4513 	err = iavf_setup_all_tx_resources(adapter);
4514 	if (err)
4515 		goto err_setup_tx;
4516 
4517 	/* allocate receive descriptors */
4518 	err = iavf_setup_all_rx_resources(adapter);
4519 	if (err)
4520 		goto err_setup_rx;
4521 
4522 	/* clear any pending interrupts, may auto mask */
4523 	err = iavf_request_traffic_irqs(adapter, netdev->name);
4524 	if (err)
4525 		goto err_req_irq;
4526 
4527 	spin_lock_bh(&adapter->mac_vlan_list_lock);
4528 	iavf_add_filter(adapter, adapter->hw.mac.addr);
4529 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
4530 
4531 	/* Restore filters that were removed with IFF_DOWN */
4532 	iavf_restore_filters(adapter);
4533 	iavf_restore_fdir_filters(adapter);
4534 
4535 	iavf_configure(adapter);
4536 
4537 	iavf_up_complete(adapter);
4538 
4539 	iavf_irq_enable(adapter, true);
4540 
4541 	return 0;
4542 
4543 err_req_irq:
4544 	iavf_down(adapter);
4545 	iavf_free_traffic_irqs(adapter);
4546 err_setup_rx:
4547 	iavf_free_all_rx_resources(adapter);
4548 err_setup_tx:
4549 	iavf_free_all_tx_resources(adapter);
4550 
4551 	return err;
4552 }
4553 
4554 /**
4555  * iavf_close - Disables a network interface
4556  * @netdev: network interface device structure
4557  *
4558  * Returns 0, this is not allowed to fail
4559  *
4560  * The close entry point is called when an interface is de-activated
4561  * by the OS.  The hardware is still under the drivers control, but
4562  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
4563  * are freed, along with all transmit and receive resources.
4564  **/
iavf_close(struct net_device * netdev)4565 static int iavf_close(struct net_device *netdev)
4566 {
4567 	struct iavf_adapter *adapter = netdev_priv(netdev);
4568 	u64 aq_to_restore;
4569 	int status;
4570 
4571 	netdev_assert_locked(netdev);
4572 
4573 	if (adapter->state <= __IAVF_DOWN_PENDING)
4574 		return 0;
4575 
4576 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
4577 	/* We cannot send IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS before
4578 	 * IAVF_FLAG_AQ_DISABLE_QUEUES because in such case there is rtnl
4579 	 * deadlock with adminq_task() until iavf_close timeouts. We must send
4580 	 * IAVF_FLAG_AQ_GET_CONFIG before IAVF_FLAG_AQ_DISABLE_QUEUES to make
4581 	 * disable queues possible for vf. Give only necessary flags to
4582 	 * iavf_down and save other to set them right before iavf_close()
4583 	 * returns, when IAVF_FLAG_AQ_DISABLE_QUEUES will be already sent and
4584 	 * iavf will be in DOWN state.
4585 	 */
4586 	aq_to_restore = adapter->aq_required;
4587 	adapter->aq_required &= IAVF_FLAG_AQ_GET_CONFIG;
4588 
4589 	/* Remove flags which we do not want to send after close or we want to
4590 	 * send before disable queues.
4591 	 */
4592 	aq_to_restore &= ~(IAVF_FLAG_AQ_GET_CONFIG		|
4593 			   IAVF_FLAG_AQ_ENABLE_QUEUES		|
4594 			   IAVF_FLAG_AQ_CONFIGURE_QUEUES	|
4595 			   IAVF_FLAG_AQ_ADD_VLAN_FILTER		|
4596 			   IAVF_FLAG_AQ_ADD_MAC_FILTER		|
4597 			   IAVF_FLAG_AQ_ADD_CLOUD_FILTER	|
4598 			   IAVF_FLAG_AQ_ADD_FDIR_FILTER		|
4599 			   IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
4600 
4601 	iavf_down(adapter);
4602 	iavf_change_state(adapter, __IAVF_DOWN_PENDING);
4603 	iavf_free_traffic_irqs(adapter);
4604 
4605 	netdev_unlock(netdev);
4606 
4607 	/* We explicitly don't free resources here because the hardware is
4608 	 * still active and can DMA into memory. Resources are cleared in
4609 	 * iavf_virtchnl_completion() after we get confirmation from the PF
4610 	 * driver that the rings have been stopped.
4611 	 *
4612 	 * Also, we wait for state to transition to __IAVF_DOWN before
4613 	 * returning. State change occurs in iavf_virtchnl_completion() after
4614 	 * VF resources are released (which occurs after PF driver processes and
4615 	 * responds to admin queue commands).
4616 	 */
4617 
4618 	status = wait_event_timeout(adapter->down_waitqueue,
4619 				    adapter->state == __IAVF_DOWN,
4620 				    msecs_to_jiffies(500));
4621 	if (!status)
4622 		netdev_warn(netdev, "Device resources not yet released\n");
4623 	netdev_lock(netdev);
4624 
4625 	adapter->aq_required |= aq_to_restore;
4626 
4627 	return 0;
4628 }
4629 
4630 /**
4631  * iavf_change_mtu - Change the Maximum Transfer Unit
4632  * @netdev: network interface device structure
4633  * @new_mtu: new value for maximum frame size
4634  *
4635  * Returns 0 on success, negative on failure
4636  **/
iavf_change_mtu(struct net_device * netdev,int new_mtu)4637 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
4638 {
4639 	struct iavf_adapter *adapter = netdev_priv(netdev);
4640 	int ret = 0;
4641 
4642 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
4643 		   netdev->mtu, new_mtu);
4644 	WRITE_ONCE(netdev->mtu, new_mtu);
4645 
4646 	if (netif_running(netdev)) {
4647 		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
4648 		ret = iavf_wait_for_reset(adapter);
4649 		if (ret < 0)
4650 			netdev_warn(netdev, "MTU change interrupted waiting for reset");
4651 		else if (ret)
4652 			netdev_warn(netdev, "MTU change timed out waiting for reset");
4653 	}
4654 
4655 	return ret;
4656 }
4657 
4658 /**
4659  * iavf_disable_fdir - disable Flow Director and clear existing filters
4660  * @adapter: board private structure
4661  **/
iavf_disable_fdir(struct iavf_adapter * adapter)4662 static void iavf_disable_fdir(struct iavf_adapter *adapter)
4663 {
4664 	struct iavf_fdir_fltr *fdir, *fdirtmp;
4665 	bool del_filters = false;
4666 
4667 	adapter->flags &= ~IAVF_FLAG_FDIR_ENABLED;
4668 
4669 	/* remove all Flow Director filters */
4670 	spin_lock_bh(&adapter->fdir_fltr_lock);
4671 	list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head,
4672 				 list) {
4673 		if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST ||
4674 		    fdir->state == IAVF_FDIR_FLTR_INACTIVE) {
4675 			/* Delete filters not registered in PF */
4676 			list_del(&fdir->list);
4677 			iavf_dec_fdir_active_fltr(adapter, fdir);
4678 			kfree(fdir);
4679 		} else if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING ||
4680 			   fdir->state == IAVF_FDIR_FLTR_DIS_REQUEST ||
4681 			   fdir->state == IAVF_FDIR_FLTR_ACTIVE) {
4682 			/* Filters registered in PF, schedule their deletion */
4683 			fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
4684 			del_filters = true;
4685 		} else if (fdir->state == IAVF_FDIR_FLTR_DIS_PENDING) {
4686 			/* Request to delete filter already sent to PF, change
4687 			 * state to DEL_PENDING to delete filter after PF's
4688 			 * response, not set as INACTIVE
4689 			 */
4690 			fdir->state = IAVF_FDIR_FLTR_DEL_PENDING;
4691 		}
4692 	}
4693 	spin_unlock_bh(&adapter->fdir_fltr_lock);
4694 
4695 	if (del_filters) {
4696 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
4697 		mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
4698 	}
4699 }
4700 
4701 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
4702 					 NETIF_F_HW_VLAN_CTAG_TX | \
4703 					 NETIF_F_HW_VLAN_STAG_RX | \
4704 					 NETIF_F_HW_VLAN_STAG_TX)
4705 
4706 /**
4707  * iavf_set_features - set the netdev feature flags
4708  * @netdev: ptr to the netdev being adjusted
4709  * @features: the feature set that the stack is suggesting
4710  * Note: expects to be called while under rtnl_lock()
4711  **/
iavf_set_features(struct net_device * netdev,netdev_features_t features)4712 static int iavf_set_features(struct net_device *netdev,
4713 			     netdev_features_t features)
4714 {
4715 	struct iavf_adapter *adapter = netdev_priv(netdev);
4716 
4717 	/* trigger update on any VLAN feature change */
4718 	if ((netdev->features & NETIF_VLAN_OFFLOAD_FEATURES) ^
4719 	    (features & NETIF_VLAN_OFFLOAD_FEATURES))
4720 		iavf_set_vlan_offload_features(adapter, netdev->features,
4721 					       features);
4722 	if (CRC_OFFLOAD_ALLOWED(adapter) &&
4723 	    ((netdev->features & NETIF_F_RXFCS) ^ (features & NETIF_F_RXFCS)))
4724 		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
4725 
4726 	if ((netdev->features & NETIF_F_NTUPLE) ^ (features & NETIF_F_NTUPLE)) {
4727 		if (features & NETIF_F_NTUPLE)
4728 			adapter->flags |= IAVF_FLAG_FDIR_ENABLED;
4729 		else
4730 			iavf_disable_fdir(adapter);
4731 	}
4732 
4733 	return 0;
4734 }
4735 
4736 /**
4737  * iavf_features_check - Validate encapsulated packet conforms to limits
4738  * @skb: skb buff
4739  * @dev: This physical port's netdev
4740  * @features: Offload features that the stack believes apply
4741  **/
iavf_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)4742 static netdev_features_t iavf_features_check(struct sk_buff *skb,
4743 					     struct net_device *dev,
4744 					     netdev_features_t features)
4745 {
4746 	size_t len;
4747 
4748 	/* No point in doing any of this if neither checksum nor GSO are
4749 	 * being requested for this frame.  We can rule out both by just
4750 	 * checking for CHECKSUM_PARTIAL
4751 	 */
4752 	if (skb->ip_summed != CHECKSUM_PARTIAL)
4753 		return features;
4754 
4755 	/* We cannot support GSO if the MSS is going to be less than
4756 	 * 64 bytes.  If it is then we need to drop support for GSO.
4757 	 */
4758 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4759 		features &= ~NETIF_F_GSO_MASK;
4760 
4761 	/* MACLEN can support at most 63 words */
4762 	len = skb_network_offset(skb);
4763 	if (len & ~(63 * 2))
4764 		goto out_err;
4765 
4766 	/* IPLEN and EIPLEN can support at most 127 dwords */
4767 	len = skb_network_header_len(skb);
4768 	if (len & ~(127 * 4))
4769 		goto out_err;
4770 
4771 	if (skb->encapsulation) {
4772 		/* L4TUNLEN can support 127 words */
4773 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
4774 		if (len & ~(127 * 2))
4775 			goto out_err;
4776 
4777 		/* IPLEN can support at most 127 dwords */
4778 		len = skb_inner_transport_header(skb) -
4779 		      skb_inner_network_header(skb);
4780 		if (len & ~(127 * 4))
4781 			goto out_err;
4782 	}
4783 
4784 	/* No need to validate L4LEN as TCP is the only protocol with a
4785 	 * flexible value and we support all possible values supported
4786 	 * by TCP, which is at most 15 dwords
4787 	 */
4788 
4789 	return features;
4790 out_err:
4791 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4792 }
4793 
4794 /**
4795  * iavf_get_netdev_vlan_hw_features - get NETDEV VLAN features that can toggle on/off
4796  * @adapter: board private structure
4797  *
4798  * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4799  * were negotiated determine the VLAN features that can be toggled on and off.
4800  **/
4801 static netdev_features_t
iavf_get_netdev_vlan_hw_features(struct iavf_adapter * adapter)4802 iavf_get_netdev_vlan_hw_features(struct iavf_adapter *adapter)
4803 {
4804 	netdev_features_t hw_features = 0;
4805 
4806 	if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4807 		return hw_features;
4808 
4809 	/* Enable VLAN features if supported */
4810 	if (VLAN_ALLOWED(adapter)) {
4811 		hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
4812 				NETIF_F_HW_VLAN_CTAG_RX);
4813 	} else if (VLAN_V2_ALLOWED(adapter)) {
4814 		struct virtchnl_vlan_caps *vlan_v2_caps =
4815 			&adapter->vlan_v2_caps;
4816 		struct virtchnl_vlan_supported_caps *stripping_support =
4817 			&vlan_v2_caps->offloads.stripping_support;
4818 		struct virtchnl_vlan_supported_caps *insertion_support =
4819 			&vlan_v2_caps->offloads.insertion_support;
4820 
4821 		if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4822 		    stripping_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4823 			if (stripping_support->outer &
4824 			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4825 				hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4826 			if (stripping_support->outer &
4827 			    VIRTCHNL_VLAN_ETHERTYPE_88A8)
4828 				hw_features |= NETIF_F_HW_VLAN_STAG_RX;
4829 		} else if (stripping_support->inner !=
4830 			   VIRTCHNL_VLAN_UNSUPPORTED &&
4831 			   stripping_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4832 			if (stripping_support->inner &
4833 			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4834 				hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4835 		}
4836 
4837 		if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4838 		    insertion_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4839 			if (insertion_support->outer &
4840 			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4841 				hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4842 			if (insertion_support->outer &
4843 			    VIRTCHNL_VLAN_ETHERTYPE_88A8)
4844 				hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4845 		} else if (insertion_support->inner &&
4846 			   insertion_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4847 			if (insertion_support->inner &
4848 			    VIRTCHNL_VLAN_ETHERTYPE_8100)
4849 				hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4850 		}
4851 	}
4852 
4853 	if (CRC_OFFLOAD_ALLOWED(adapter))
4854 		hw_features |= NETIF_F_RXFCS;
4855 
4856 	return hw_features;
4857 }
4858 
4859 /**
4860  * iavf_get_netdev_vlan_features - get the enabled NETDEV VLAN fetures
4861  * @adapter: board private structure
4862  *
4863  * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4864  * were negotiated determine the VLAN features that are enabled by default.
4865  **/
4866 static netdev_features_t
iavf_get_netdev_vlan_features(struct iavf_adapter * adapter)4867 iavf_get_netdev_vlan_features(struct iavf_adapter *adapter)
4868 {
4869 	netdev_features_t features = 0;
4870 
4871 	if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4872 		return features;
4873 
4874 	if (VLAN_ALLOWED(adapter)) {
4875 		features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4876 			NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
4877 	} else if (VLAN_V2_ALLOWED(adapter)) {
4878 		struct virtchnl_vlan_caps *vlan_v2_caps =
4879 			&adapter->vlan_v2_caps;
4880 		struct virtchnl_vlan_supported_caps *filtering_support =
4881 			&vlan_v2_caps->filtering.filtering_support;
4882 		struct virtchnl_vlan_supported_caps *stripping_support =
4883 			&vlan_v2_caps->offloads.stripping_support;
4884 		struct virtchnl_vlan_supported_caps *insertion_support =
4885 			&vlan_v2_caps->offloads.insertion_support;
4886 		u32 ethertype_init;
4887 
4888 		/* give priority to outer stripping and don't support both outer
4889 		 * and inner stripping
4890 		 */
4891 		ethertype_init = vlan_v2_caps->offloads.ethertype_init;
4892 		if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4893 			if (stripping_support->outer &
4894 			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4895 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4896 				features |= NETIF_F_HW_VLAN_CTAG_RX;
4897 			else if (stripping_support->outer &
4898 				 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4899 				 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4900 				features |= NETIF_F_HW_VLAN_STAG_RX;
4901 		} else if (stripping_support->inner !=
4902 			   VIRTCHNL_VLAN_UNSUPPORTED) {
4903 			if (stripping_support->inner &
4904 			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4905 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4906 				features |= NETIF_F_HW_VLAN_CTAG_RX;
4907 		}
4908 
4909 		/* give priority to outer insertion and don't support both outer
4910 		 * and inner insertion
4911 		 */
4912 		if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4913 			if (insertion_support->outer &
4914 			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4915 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4916 				features |= NETIF_F_HW_VLAN_CTAG_TX;
4917 			else if (insertion_support->outer &
4918 				 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4919 				 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4920 				features |= NETIF_F_HW_VLAN_STAG_TX;
4921 		} else if (insertion_support->inner !=
4922 			   VIRTCHNL_VLAN_UNSUPPORTED) {
4923 			if (insertion_support->inner &
4924 			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4925 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4926 				features |= NETIF_F_HW_VLAN_CTAG_TX;
4927 		}
4928 
4929 		/* give priority to outer filtering and don't bother if both
4930 		 * outer and inner filtering are enabled
4931 		 */
4932 		ethertype_init = vlan_v2_caps->filtering.ethertype_init;
4933 		if (filtering_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4934 			if (filtering_support->outer &
4935 			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4936 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4937 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4938 			if (filtering_support->outer &
4939 			    VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4940 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4941 				features |= NETIF_F_HW_VLAN_STAG_FILTER;
4942 		} else if (filtering_support->inner !=
4943 			   VIRTCHNL_VLAN_UNSUPPORTED) {
4944 			if (filtering_support->inner &
4945 			    VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4946 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4947 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4948 			if (filtering_support->inner &
4949 			    VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4950 			    ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4951 				features |= NETIF_F_HW_VLAN_STAG_FILTER;
4952 		}
4953 	}
4954 
4955 	return features;
4956 }
4957 
4958 #define IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested, allowed, feature_bit) \
4959 	(!(((requested) & (feature_bit)) && \
4960 	   !((allowed) & (feature_bit))))
4961 
4962 /**
4963  * iavf_fix_netdev_vlan_features - fix NETDEV VLAN features based on support
4964  * @adapter: board private structure
4965  * @requested_features: stack requested NETDEV features
4966  **/
4967 static netdev_features_t
iavf_fix_netdev_vlan_features(struct iavf_adapter * adapter,netdev_features_t requested_features)4968 iavf_fix_netdev_vlan_features(struct iavf_adapter *adapter,
4969 			      netdev_features_t requested_features)
4970 {
4971 	netdev_features_t allowed_features;
4972 
4973 	allowed_features = iavf_get_netdev_vlan_hw_features(adapter) |
4974 		iavf_get_netdev_vlan_features(adapter);
4975 
4976 	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4977 					      allowed_features,
4978 					      NETIF_F_HW_VLAN_CTAG_TX))
4979 		requested_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4980 
4981 	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4982 					      allowed_features,
4983 					      NETIF_F_HW_VLAN_CTAG_RX))
4984 		requested_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4985 
4986 	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4987 					      allowed_features,
4988 					      NETIF_F_HW_VLAN_STAG_TX))
4989 		requested_features &= ~NETIF_F_HW_VLAN_STAG_TX;
4990 	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4991 					      allowed_features,
4992 					      NETIF_F_HW_VLAN_STAG_RX))
4993 		requested_features &= ~NETIF_F_HW_VLAN_STAG_RX;
4994 
4995 	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4996 					      allowed_features,
4997 					      NETIF_F_HW_VLAN_CTAG_FILTER))
4998 		requested_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4999 
5000 	if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
5001 					      allowed_features,
5002 					      NETIF_F_HW_VLAN_STAG_FILTER))
5003 		requested_features &= ~NETIF_F_HW_VLAN_STAG_FILTER;
5004 
5005 	if ((requested_features &
5006 	     (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
5007 	    (requested_features &
5008 	     (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX)) &&
5009 	    adapter->vlan_v2_caps.offloads.ethertype_match ==
5010 	    VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION) {
5011 		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");
5012 		requested_features &= ~(NETIF_F_HW_VLAN_STAG_RX |
5013 					NETIF_F_HW_VLAN_STAG_TX);
5014 	}
5015 
5016 	return requested_features;
5017 }
5018 
5019 /**
5020  * iavf_fix_strip_features - fix NETDEV CRC and VLAN strip features
5021  * @adapter: board private structure
5022  * @requested_features: stack requested NETDEV features
5023  *
5024  * Returns fixed-up features bits
5025  **/
5026 static netdev_features_t
iavf_fix_strip_features(struct iavf_adapter * adapter,netdev_features_t requested_features)5027 iavf_fix_strip_features(struct iavf_adapter *adapter,
5028 			netdev_features_t requested_features)
5029 {
5030 	struct net_device *netdev = adapter->netdev;
5031 	bool crc_offload_req, is_vlan_strip;
5032 	netdev_features_t vlan_strip;
5033 	int num_non_zero_vlan;
5034 
5035 	crc_offload_req = CRC_OFFLOAD_ALLOWED(adapter) &&
5036 			  (requested_features & NETIF_F_RXFCS);
5037 	num_non_zero_vlan = iavf_get_num_vlans_added(adapter);
5038 	vlan_strip = (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX);
5039 	is_vlan_strip = requested_features & vlan_strip;
5040 
5041 	if (!crc_offload_req)
5042 		return requested_features;
5043 
5044 	if (!num_non_zero_vlan && (netdev->features & vlan_strip) &&
5045 	    !(netdev->features & NETIF_F_RXFCS) && is_vlan_strip) {
5046 		requested_features &= ~vlan_strip;
5047 		netdev_info(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
5048 		return requested_features;
5049 	}
5050 
5051 	if ((netdev->features & NETIF_F_RXFCS) && is_vlan_strip) {
5052 		requested_features &= ~vlan_strip;
5053 		if (!(netdev->features & vlan_strip))
5054 			netdev_info(netdev, "To enable VLAN stripping, first need to enable FCS/CRC stripping");
5055 
5056 		return requested_features;
5057 	}
5058 
5059 	if (num_non_zero_vlan && is_vlan_strip &&
5060 	    !(netdev->features & NETIF_F_RXFCS)) {
5061 		requested_features &= ~NETIF_F_RXFCS;
5062 		netdev_info(netdev, "To disable FCS/CRC stripping, first need to disable VLAN stripping");
5063 	}
5064 
5065 	return requested_features;
5066 }
5067 
5068 /**
5069  * iavf_fix_features - fix up the netdev feature bits
5070  * @netdev: our net device
5071  * @features: desired feature bits
5072  *
5073  * Returns fixed-up features bits
5074  **/
iavf_fix_features(struct net_device * netdev,netdev_features_t features)5075 static netdev_features_t iavf_fix_features(struct net_device *netdev,
5076 					   netdev_features_t features)
5077 {
5078 	struct iavf_adapter *adapter = netdev_priv(netdev);
5079 
5080 	features = iavf_fix_netdev_vlan_features(adapter, features);
5081 
5082 	if (!FDIR_FLTR_SUPPORT(adapter))
5083 		features &= ~NETIF_F_NTUPLE;
5084 
5085 	return iavf_fix_strip_features(adapter, features);
5086 }
5087 
iavf_hwstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)5088 static int iavf_hwstamp_get(struct net_device *netdev,
5089 			    struct kernel_hwtstamp_config *config)
5090 {
5091 	struct iavf_adapter *adapter = netdev_priv(netdev);
5092 
5093 	*config = adapter->ptp.hwtstamp_config;
5094 
5095 	return 0;
5096 }
5097 
iavf_hwstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)5098 static int iavf_hwstamp_set(struct net_device *netdev,
5099 			    struct kernel_hwtstamp_config *config,
5100 			    struct netlink_ext_ack *extack)
5101 {
5102 	struct iavf_adapter *adapter = netdev_priv(netdev);
5103 
5104 	return iavf_ptp_set_ts_config(adapter, config, extack);
5105 }
5106 
5107 static int
iavf_verify_shaper(struct net_shaper_binding * binding,const struct net_shaper * shaper,struct netlink_ext_ack * extack)5108 iavf_verify_shaper(struct net_shaper_binding *binding,
5109 		   const struct net_shaper *shaper,
5110 		   struct netlink_ext_ack *extack)
5111 {
5112 	struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5113 	u64 vf_max;
5114 
5115 	if (shaper->handle.scope == NET_SHAPER_SCOPE_QUEUE) {
5116 		vf_max = adapter->qos_caps->cap[0].shaper.peak;
5117 		if (vf_max && shaper->bw_max > vf_max) {
5118 			NL_SET_ERR_MSG_FMT(extack, "Max rate (%llu) of queue %d can't exceed max TX rate of VF (%llu kbps)",
5119 					   shaper->bw_max, shaper->handle.id,
5120 					   vf_max);
5121 			return -EINVAL;
5122 		}
5123 	}
5124 	return 0;
5125 }
5126 
5127 static int
iavf_shaper_set(struct net_shaper_binding * binding,const struct net_shaper * shaper,struct netlink_ext_ack * extack)5128 iavf_shaper_set(struct net_shaper_binding *binding,
5129 		const struct net_shaper *shaper,
5130 		struct netlink_ext_ack *extack)
5131 {
5132 	struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5133 	const struct net_shaper_handle *handle = &shaper->handle;
5134 	struct iavf_ring *tx_ring;
5135 	int ret;
5136 
5137 	netdev_assert_locked(adapter->netdev);
5138 
5139 	if (handle->id >= adapter->num_active_queues)
5140 		return 0;
5141 
5142 	ret = iavf_verify_shaper(binding, shaper, extack);
5143 	if (ret)
5144 		return ret;
5145 
5146 	tx_ring = &adapter->tx_rings[handle->id];
5147 
5148 	tx_ring->q_shaper.bw_min = div_u64(shaper->bw_min, 1000);
5149 	tx_ring->q_shaper.bw_max = div_u64(shaper->bw_max, 1000);
5150 	tx_ring->q_shaper_update = true;
5151 
5152 	adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
5153 
5154 	return 0;
5155 }
5156 
iavf_shaper_del(struct net_shaper_binding * binding,const struct net_shaper_handle * handle,struct netlink_ext_ack * extack)5157 static int iavf_shaper_del(struct net_shaper_binding *binding,
5158 			   const struct net_shaper_handle *handle,
5159 			   struct netlink_ext_ack *extack)
5160 {
5161 	struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5162 	struct iavf_ring *tx_ring;
5163 
5164 	netdev_assert_locked(adapter->netdev);
5165 
5166 	if (handle->id >= adapter->num_active_queues)
5167 		return 0;
5168 
5169 	tx_ring = &adapter->tx_rings[handle->id];
5170 	tx_ring->q_shaper.bw_min = 0;
5171 	tx_ring->q_shaper.bw_max = 0;
5172 	tx_ring->q_shaper_update = true;
5173 
5174 	adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
5175 
5176 	return 0;
5177 }
5178 
iavf_shaper_cap(struct net_shaper_binding * binding,enum net_shaper_scope scope,unsigned long * flags)5179 static void iavf_shaper_cap(struct net_shaper_binding *binding,
5180 			    enum net_shaper_scope scope,
5181 			    unsigned long *flags)
5182 {
5183 	if (scope != NET_SHAPER_SCOPE_QUEUE)
5184 		return;
5185 
5186 	*flags = BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MIN) |
5187 		 BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX) |
5188 		 BIT(NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS);
5189 }
5190 
5191 static const struct net_shaper_ops iavf_shaper_ops = {
5192 	.set = iavf_shaper_set,
5193 	.delete = iavf_shaper_del,
5194 	.capabilities = iavf_shaper_cap,
5195 };
5196 
5197 static const struct net_device_ops iavf_netdev_ops = {
5198 	.ndo_open		= iavf_open,
5199 	.ndo_stop		= iavf_close,
5200 	.ndo_start_xmit		= iavf_xmit_frame,
5201 	.ndo_set_rx_mode	= iavf_set_rx_mode,
5202 	.ndo_validate_addr	= eth_validate_addr,
5203 	.ndo_set_mac_address	= iavf_set_mac,
5204 	.ndo_change_mtu		= iavf_change_mtu,
5205 	.ndo_tx_timeout		= iavf_tx_timeout,
5206 	.ndo_vlan_rx_add_vid	= iavf_vlan_rx_add_vid,
5207 	.ndo_vlan_rx_kill_vid	= iavf_vlan_rx_kill_vid,
5208 	.ndo_features_check	= iavf_features_check,
5209 	.ndo_fix_features	= iavf_fix_features,
5210 	.ndo_set_features	= iavf_set_features,
5211 	.ndo_setup_tc		= iavf_setup_tc,
5212 	.net_shaper_ops		= &iavf_shaper_ops,
5213 	.ndo_hwtstamp_get	= iavf_hwstamp_get,
5214 	.ndo_hwtstamp_set	= iavf_hwstamp_set,
5215 };
5216 
5217 /**
5218  * iavf_check_reset_complete - check that VF reset is complete
5219  * @hw: pointer to hw struct
5220  *
5221  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
5222  **/
iavf_check_reset_complete(struct iavf_hw * hw)5223 static int iavf_check_reset_complete(struct iavf_hw *hw)
5224 {
5225 	u32 rstat;
5226 	int i;
5227 
5228 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
5229 		rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
5230 			     IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
5231 		if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
5232 		    (rstat == VIRTCHNL_VFR_COMPLETED))
5233 			return 0;
5234 		msleep(IAVF_RESET_WAIT_MS);
5235 	}
5236 	return -EBUSY;
5237 }
5238 
5239 /**
5240  * iavf_process_config - Process the config information we got from the PF
5241  * @adapter: board private structure
5242  *
5243  * Verify that we have a valid config struct, and set up our netdev features
5244  * and our VSI struct.
5245  **/
iavf_process_config(struct iavf_adapter * adapter)5246 int iavf_process_config(struct iavf_adapter *adapter)
5247 {
5248 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
5249 	netdev_features_t hw_vlan_features, vlan_features;
5250 	struct net_device *netdev = adapter->netdev;
5251 	netdev_features_t hw_enc_features;
5252 	netdev_features_t hw_features;
5253 
5254 	hw_enc_features = NETIF_F_SG			|
5255 			  NETIF_F_IP_CSUM		|
5256 			  NETIF_F_IPV6_CSUM		|
5257 			  NETIF_F_HIGHDMA		|
5258 			  NETIF_F_SOFT_FEATURES	|
5259 			  NETIF_F_TSO			|
5260 			  NETIF_F_TSO_ECN		|
5261 			  NETIF_F_TSO6			|
5262 			  NETIF_F_SCTP_CRC		|
5263 			  NETIF_F_RXHASH		|
5264 			  NETIF_F_RXCSUM		|
5265 			  0;
5266 
5267 	/* advertise to stack only if offloads for encapsulated packets is
5268 	 * supported
5269 	 */
5270 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
5271 		hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL	|
5272 				   NETIF_F_GSO_GRE		|
5273 				   NETIF_F_GSO_GRE_CSUM		|
5274 				   NETIF_F_GSO_IPXIP4		|
5275 				   NETIF_F_GSO_IPXIP6		|
5276 				   NETIF_F_GSO_UDP_TUNNEL_CSUM	|
5277 				   NETIF_F_GSO_PARTIAL		|
5278 				   0;
5279 
5280 		if (!(vfres->vf_cap_flags &
5281 		      VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
5282 			netdev->gso_partial_features |=
5283 				NETIF_F_GSO_UDP_TUNNEL_CSUM;
5284 
5285 		netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
5286 		netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
5287 		netdev->hw_enc_features |= hw_enc_features;
5288 	}
5289 	/* record features VLANs can make use of */
5290 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
5291 
5292 	/* Write features and hw_features separately to avoid polluting
5293 	 * with, or dropping, features that are set when we registered.
5294 	 */
5295 	hw_features = hw_enc_features;
5296 
5297 	/* get HW VLAN features that can be toggled */
5298 	hw_vlan_features = iavf_get_netdev_vlan_hw_features(adapter);
5299 
5300 	/* Enable HW TC offload if ADQ or tc U32 is supported */
5301 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ ||
5302 	    TC_U32_SUPPORT(adapter))
5303 		hw_features |= NETIF_F_HW_TC;
5304 
5305 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
5306 		hw_features |= NETIF_F_GSO_UDP_L4;
5307 
5308 	netdev->hw_features |= hw_features | hw_vlan_features;
5309 	vlan_features = iavf_get_netdev_vlan_features(adapter);
5310 
5311 	netdev->features |= hw_features | vlan_features;
5312 
5313 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
5314 		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5315 
5316 	if (FDIR_FLTR_SUPPORT(adapter)) {
5317 		netdev->hw_features |= NETIF_F_NTUPLE;
5318 		netdev->features |= NETIF_F_NTUPLE;
5319 		adapter->flags |= IAVF_FLAG_FDIR_ENABLED;
5320 	}
5321 
5322 	netdev->priv_flags |= IFF_UNICAST_FLT;
5323 
5324 	/* Do not turn on offloads when they are requested to be turned off.
5325 	 * TSO needs minimum 576 bytes to work correctly.
5326 	 */
5327 	if (netdev->wanted_features) {
5328 		if (!(netdev->wanted_features & NETIF_F_TSO) ||
5329 		    netdev->mtu < 576)
5330 			netdev->features &= ~NETIF_F_TSO;
5331 		if (!(netdev->wanted_features & NETIF_F_TSO6) ||
5332 		    netdev->mtu < 576)
5333 			netdev->features &= ~NETIF_F_TSO6;
5334 		if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
5335 			netdev->features &= ~NETIF_F_TSO_ECN;
5336 		if (!(netdev->wanted_features & NETIF_F_GRO))
5337 			netdev->features &= ~NETIF_F_GRO;
5338 		if (!(netdev->wanted_features & NETIF_F_GSO))
5339 			netdev->features &= ~NETIF_F_GSO;
5340 	}
5341 
5342 	return 0;
5343 }
5344 
5345 /**
5346  * iavf_probe - Device Initialization Routine
5347  * @pdev: PCI device information struct
5348  * @ent: entry in iavf_pci_tbl
5349  *
5350  * Returns 0 on success, negative on failure
5351  *
5352  * iavf_probe initializes an adapter identified by a pci_dev structure.
5353  * The OS initialization, configuring of the adapter private structure,
5354  * and a hardware reset occur.
5355  **/
iavf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)5356 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5357 {
5358 	struct net_device *netdev;
5359 	struct iavf_adapter *adapter = NULL;
5360 	struct iavf_hw *hw = NULL;
5361 	int err, len;
5362 
5363 	err = pci_enable_device(pdev);
5364 	if (err)
5365 		return err;
5366 
5367 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
5368 	if (err) {
5369 		dev_err(&pdev->dev,
5370 			"DMA configuration failed: 0x%x\n", err);
5371 		goto err_dma;
5372 	}
5373 
5374 	err = pci_request_regions(pdev, iavf_driver_name);
5375 	if (err) {
5376 		dev_err(&pdev->dev,
5377 			"pci_request_regions failed 0x%x\n", err);
5378 		goto err_pci_reg;
5379 	}
5380 
5381 	pci_set_master(pdev);
5382 
5383 	netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
5384 				   IAVF_MAX_REQ_QUEUES);
5385 	if (!netdev) {
5386 		err = -ENOMEM;
5387 		goto err_alloc_etherdev;
5388 	}
5389 
5390 	SET_NETDEV_DEV(netdev, &pdev->dev);
5391 
5392 	pci_set_drvdata(pdev, netdev);
5393 	adapter = netdev_priv(netdev);
5394 
5395 	adapter->netdev = netdev;
5396 	adapter->pdev = pdev;
5397 
5398 	hw = &adapter->hw;
5399 	hw->back = adapter;
5400 
5401 	adapter->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
5402 					      iavf_driver_name);
5403 	if (!adapter->wq) {
5404 		err = -ENOMEM;
5405 		goto err_alloc_wq;
5406 	}
5407 
5408 	adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
5409 	iavf_change_state(adapter, __IAVF_STARTUP);
5410 
5411 	/* Call save state here because it relies on the adapter struct. */
5412 	pci_save_state(pdev);
5413 
5414 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
5415 			      pci_resource_len(pdev, 0));
5416 	if (!hw->hw_addr) {
5417 		err = -EIO;
5418 		goto err_ioremap;
5419 	}
5420 	hw->vendor_id = pdev->vendor;
5421 	hw->device_id = pdev->device;
5422 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5423 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5424 	hw->subsystem_device_id = pdev->subsystem_device;
5425 	hw->bus.device = PCI_SLOT(pdev->devfn);
5426 	hw->bus.func = PCI_FUNC(pdev->devfn);
5427 	hw->bus.bus_id = pdev->bus->number;
5428 
5429 	len = struct_size(adapter->qos_caps, cap, IAVF_MAX_QOS_TC_NUM);
5430 	adapter->qos_caps = kzalloc(len, GFP_KERNEL);
5431 	if (!adapter->qos_caps) {
5432 		err = -ENOMEM;
5433 		goto err_alloc_qos_cap;
5434 	}
5435 
5436 	mutex_init(&hw->aq.asq_mutex);
5437 	mutex_init(&hw->aq.arq_mutex);
5438 
5439 	spin_lock_init(&adapter->mac_vlan_list_lock);
5440 	spin_lock_init(&adapter->cloud_filter_list_lock);
5441 	spin_lock_init(&adapter->fdir_fltr_lock);
5442 	spin_lock_init(&adapter->adv_rss_lock);
5443 	spin_lock_init(&adapter->current_netdev_promisc_flags_lock);
5444 
5445 	INIT_LIST_HEAD(&adapter->mac_filter_list);
5446 	INIT_LIST_HEAD(&adapter->vlan_filter_list);
5447 	INIT_LIST_HEAD(&adapter->cloud_filter_list);
5448 	INIT_LIST_HEAD(&adapter->fdir_list_head);
5449 	INIT_LIST_HEAD(&adapter->adv_rss_list_head);
5450 
5451 	INIT_WORK(&adapter->reset_task, iavf_reset_task);
5452 	INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
5453 	INIT_WORK(&adapter->finish_config, iavf_finish_config);
5454 	INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
5455 
5456 	/* Setup the wait queue for indicating transition to down status */
5457 	init_waitqueue_head(&adapter->down_waitqueue);
5458 
5459 	/* Setup the wait queue for indicating transition to running state */
5460 	init_waitqueue_head(&adapter->reset_waitqueue);
5461 
5462 	/* Setup the wait queue for indicating virtchannel events */
5463 	init_waitqueue_head(&adapter->vc_waitqueue);
5464 
5465 	INIT_LIST_HEAD(&adapter->ptp.aq_cmds);
5466 	init_waitqueue_head(&adapter->ptp.phc_time_waitqueue);
5467 	mutex_init(&adapter->ptp.aq_cmd_lock);
5468 
5469 	queue_delayed_work(adapter->wq, &adapter->watchdog_task,
5470 			   msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
5471 	/* Initialization goes on in the work. Do not add more of it below. */
5472 	return 0;
5473 
5474 err_alloc_qos_cap:
5475 	iounmap(hw->hw_addr);
5476 err_ioremap:
5477 	destroy_workqueue(adapter->wq);
5478 err_alloc_wq:
5479 	free_netdev(netdev);
5480 err_alloc_etherdev:
5481 	pci_release_regions(pdev);
5482 err_pci_reg:
5483 err_dma:
5484 	pci_disable_device(pdev);
5485 	return err;
5486 }
5487 
5488 /**
5489  * iavf_suspend - Power management suspend routine
5490  * @dev_d: device info pointer
5491  *
5492  * Called when the system (VM) is entering sleep/suspend.
5493  **/
iavf_suspend(struct device * dev_d)5494 static int iavf_suspend(struct device *dev_d)
5495 {
5496 	struct net_device *netdev = dev_get_drvdata(dev_d);
5497 	struct iavf_adapter *adapter = netdev_priv(netdev);
5498 	bool running;
5499 
5500 	netif_device_detach(netdev);
5501 
5502 	running = netif_running(netdev);
5503 	if (running)
5504 		rtnl_lock();
5505 	netdev_lock(netdev);
5506 
5507 	if (running)
5508 		iavf_down(adapter);
5509 
5510 	iavf_free_misc_irq(adapter);
5511 	iavf_reset_interrupt_capability(adapter);
5512 
5513 	netdev_unlock(netdev);
5514 	if (running)
5515 		rtnl_unlock();
5516 
5517 	return 0;
5518 }
5519 
5520 /**
5521  * iavf_resume - Power management resume routine
5522  * @dev_d: device info pointer
5523  *
5524  * Called when the system (VM) is resumed from sleep/suspend.
5525  **/
iavf_resume(struct device * dev_d)5526 static int iavf_resume(struct device *dev_d)
5527 {
5528 	struct pci_dev *pdev = to_pci_dev(dev_d);
5529 	struct iavf_adapter *adapter;
5530 	u32 err;
5531 
5532 	adapter = iavf_pdev_to_adapter(pdev);
5533 
5534 	pci_set_master(pdev);
5535 
5536 	rtnl_lock();
5537 	err = iavf_set_interrupt_capability(adapter);
5538 	if (err) {
5539 		rtnl_unlock();
5540 		dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
5541 		return err;
5542 	}
5543 	err = iavf_request_misc_irq(adapter);
5544 	rtnl_unlock();
5545 	if (err) {
5546 		dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
5547 		return err;
5548 	}
5549 
5550 	queue_work(adapter->wq, &adapter->reset_task);
5551 
5552 	netif_device_attach(adapter->netdev);
5553 
5554 	return err;
5555 }
5556 
5557 /**
5558  * iavf_remove - Device Removal Routine
5559  * @pdev: PCI device information struct
5560  *
5561  * iavf_remove is called by the PCI subsystem to alert the driver
5562  * that it should release a PCI device.  The could be caused by a
5563  * Hot-Plug event, or because the driver is going to be removed from
5564  * memory.
5565  **/
iavf_remove(struct pci_dev * pdev)5566 static void iavf_remove(struct pci_dev *pdev)
5567 {
5568 	struct iavf_fdir_fltr *fdir, *fdirtmp;
5569 	struct iavf_vlan_filter *vlf, *vlftmp;
5570 	struct iavf_cloud_filter *cf, *cftmp;
5571 	struct iavf_adv_rss *rss, *rsstmp;
5572 	struct iavf_mac_filter *f, *ftmp;
5573 	struct iavf_adapter *adapter;
5574 	struct net_device *netdev;
5575 	struct iavf_hw *hw;
5576 
5577 	/* Don't proceed with remove if netdev is already freed */
5578 	netdev = pci_get_drvdata(pdev);
5579 	if (!netdev)
5580 		return;
5581 
5582 	adapter = iavf_pdev_to_adapter(pdev);
5583 	hw = &adapter->hw;
5584 
5585 	if (test_and_set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
5586 		return;
5587 
5588 	/* Wait until port initialization is complete.
5589 	 * There are flows where register/unregister netdev may race.
5590 	 */
5591 	while (1) {
5592 		netdev_lock(netdev);
5593 		if (adapter->state == __IAVF_RUNNING ||
5594 		    adapter->state == __IAVF_DOWN ||
5595 		    adapter->state == __IAVF_INIT_FAILED) {
5596 			netdev_unlock(netdev);
5597 			break;
5598 		}
5599 		/* Simply return if we already went through iavf_shutdown */
5600 		if (adapter->state == __IAVF_REMOVE) {
5601 			netdev_unlock(netdev);
5602 			return;
5603 		}
5604 
5605 		netdev_unlock(netdev);
5606 		usleep_range(500, 1000);
5607 	}
5608 	cancel_delayed_work_sync(&adapter->watchdog_task);
5609 	cancel_work_sync(&adapter->finish_config);
5610 
5611 	if (netdev->reg_state == NETREG_REGISTERED)
5612 		unregister_netdev(netdev);
5613 
5614 	netdev_lock(netdev);
5615 	dev_info(&adapter->pdev->dev, "Removing device\n");
5616 	iavf_change_state(adapter, __IAVF_REMOVE);
5617 
5618 	iavf_request_reset(adapter);
5619 	msleep(50);
5620 	/* If the FW isn't responding, kick it once, but only once. */
5621 	if (!iavf_asq_done(hw)) {
5622 		iavf_request_reset(adapter);
5623 		msleep(50);
5624 	}
5625 
5626 	iavf_ptp_release(adapter);
5627 
5628 	iavf_misc_irq_disable(adapter);
5629 	/* Shut down all the garbage mashers on the detention level */
5630 	netdev_unlock(netdev);
5631 	cancel_work_sync(&adapter->reset_task);
5632 	cancel_delayed_work_sync(&adapter->watchdog_task);
5633 	cancel_work_sync(&adapter->adminq_task);
5634 	netdev_lock(netdev);
5635 
5636 	adapter->aq_required = 0;
5637 	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
5638 
5639 	iavf_free_all_tx_resources(adapter);
5640 	iavf_free_all_rx_resources(adapter);
5641 	iavf_free_misc_irq(adapter);
5642 	iavf_free_interrupt_scheme(adapter);
5643 
5644 	iavf_free_rss(adapter);
5645 
5646 	if (hw->aq.asq.count)
5647 		iavf_shutdown_adminq(hw);
5648 
5649 	/* destroy the locks only once, here */
5650 	mutex_destroy(&hw->aq.arq_mutex);
5651 	mutex_destroy(&hw->aq.asq_mutex);
5652 	netdev_unlock(netdev);
5653 
5654 	iounmap(hw->hw_addr);
5655 	pci_release_regions(pdev);
5656 	kfree(adapter->vf_res);
5657 	spin_lock_bh(&adapter->mac_vlan_list_lock);
5658 	/* If we got removed before an up/down sequence, we've got a filter
5659 	 * hanging out there that we need to get rid of.
5660 	 */
5661 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
5662 		list_del(&f->list);
5663 		kfree(f);
5664 	}
5665 	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
5666 				 list) {
5667 		list_del(&vlf->list);
5668 		kfree(vlf);
5669 	}
5670 
5671 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
5672 
5673 	spin_lock_bh(&adapter->cloud_filter_list_lock);
5674 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
5675 		list_del(&cf->list);
5676 		kfree(cf);
5677 	}
5678 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
5679 
5680 	spin_lock_bh(&adapter->fdir_fltr_lock);
5681 	list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
5682 		list_del(&fdir->list);
5683 		kfree(fdir);
5684 	}
5685 	spin_unlock_bh(&adapter->fdir_fltr_lock);
5686 
5687 	spin_lock_bh(&adapter->adv_rss_lock);
5688 	list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
5689 				 list) {
5690 		list_del(&rss->list);
5691 		kfree(rss);
5692 	}
5693 	spin_unlock_bh(&adapter->adv_rss_lock);
5694 
5695 	destroy_workqueue(adapter->wq);
5696 
5697 	pci_set_drvdata(pdev, NULL);
5698 
5699 	free_netdev(netdev);
5700 
5701 	pci_disable_device(pdev);
5702 }
5703 
5704 /**
5705  * iavf_shutdown - Shutdown the device in preparation for a reboot
5706  * @pdev: pci device structure
5707  **/
iavf_shutdown(struct pci_dev * pdev)5708 static void iavf_shutdown(struct pci_dev *pdev)
5709 {
5710 	iavf_remove(pdev);
5711 
5712 	if (system_state == SYSTEM_POWER_OFF)
5713 		pci_set_power_state(pdev, PCI_D3hot);
5714 }
5715 
5716 static DEFINE_SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
5717 
5718 static struct pci_driver iavf_driver = {
5719 	.name      = iavf_driver_name,
5720 	.id_table  = iavf_pci_tbl,
5721 	.probe     = iavf_probe,
5722 	.remove    = iavf_remove,
5723 	.driver.pm = pm_sleep_ptr(&iavf_pm_ops),
5724 	.shutdown  = iavf_shutdown,
5725 };
5726 
5727 /**
5728  * iavf_init_module - Driver Registration Routine
5729  *
5730  * iavf_init_module is the first routine called when the driver is
5731  * loaded. All it does is register with the PCI subsystem.
5732  **/
iavf_init_module(void)5733 static int __init iavf_init_module(void)
5734 {
5735 	pr_info("iavf: %s\n", iavf_driver_string);
5736 
5737 	pr_info("%s\n", iavf_copyright);
5738 
5739 	return pci_register_driver(&iavf_driver);
5740 }
5741 
5742 module_init(iavf_init_module);
5743 
5744 /**
5745  * iavf_exit_module - Driver Exit Cleanup Routine
5746  *
5747  * iavf_exit_module is called just before the driver is removed
5748  * from memory.
5749  **/
iavf_exit_module(void)5750 static void __exit iavf_exit_module(void)
5751 {
5752 	pci_unregister_driver(&iavf_driver);
5753 }
5754 
5755 module_exit(iavf_exit_module);
5756 
5757 /* iavf_main.c */
5758