xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 4201c9260a8d3c4ef238e51692a7e9b4e1e29efe)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 
9 /* Local includes */
10 #include "i40e.h"
11 #include "i40e_diag.h"
12 #include "i40e_xsk.h"
13 #include <net/udp_tunnel.h>
14 #include <net/xdp_sock.h>
15 /* All i40e tracepoints are defined by the include below, which
16  * must be included exactly once across the whole kernel with
17  * CREATE_TRACE_POINTS defined
18  */
19 #define CREATE_TRACE_POINTS
20 #include "i40e_trace.h"
21 
22 const char i40e_driver_name[] = "i40e";
23 static const char i40e_driver_string[] =
24 			"Intel(R) Ethernet Connection XL710 Network Driver";
25 
26 #define DRV_KERN "-k"
27 
28 #define DRV_VERSION_MAJOR 2
29 #define DRV_VERSION_MINOR 8
30 #define DRV_VERSION_BUILD 20
31 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
32 	     __stringify(DRV_VERSION_MINOR) "." \
33 	     __stringify(DRV_VERSION_BUILD)    DRV_KERN
34 const char i40e_driver_version_str[] = DRV_VERSION;
35 static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
36 
37 /* a bit of forward declarations */
38 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
39 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
40 static int i40e_add_vsi(struct i40e_vsi *vsi);
41 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
42 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
43 static int i40e_setup_misc_vector(struct i40e_pf *pf);
44 static void i40e_determine_queue_usage(struct i40e_pf *pf);
45 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
46 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
47 static int i40e_reset(struct i40e_pf *pf);
48 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
49 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
50 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
51 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
52 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
53 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
54 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
55 static int i40e_get_capabilities(struct i40e_pf *pf,
56 				 enum i40e_admin_queue_opc list_type);
57 
58 
59 /* i40e_pci_tbl - PCI Device ID Table
60  *
61  * Last entry must be all 0s
62  *
63  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64  *   Class, Class Mask, private data (not used) }
65  */
66 static const struct pci_device_id i40e_pci_tbl[] = {
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
90 	/* required last entry */
91 	{0, }
92 };
93 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94 
95 #define I40E_MAX_VF_COUNT 128
96 static int debug = -1;
97 module_param(debug, uint, 0);
98 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
99 
100 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102 MODULE_LICENSE("GPL v2");
103 MODULE_VERSION(DRV_VERSION);
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 /**
108  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
109  * @hw:   pointer to the HW structure
110  * @mem:  ptr to mem struct to fill out
111  * @size: size of memory requested
112  * @alignment: what to align the allocation to
113  **/
114 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
115 			    u64 size, u32 alignment)
116 {
117 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118 
119 	mem->size = ALIGN(size, alignment);
120 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
121 				     GFP_KERNEL);
122 	if (!mem->va)
123 		return -ENOMEM;
124 
125 	return 0;
126 }
127 
128 /**
129  * i40e_free_dma_mem_d - OS specific memory free for shared code
130  * @hw:   pointer to the HW structure
131  * @mem:  ptr to mem struct to free
132  **/
133 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
134 {
135 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
136 
137 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
138 	mem->va = NULL;
139 	mem->pa = 0;
140 	mem->size = 0;
141 
142 	return 0;
143 }
144 
145 /**
146  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
147  * @hw:   pointer to the HW structure
148  * @mem:  ptr to mem struct to fill out
149  * @size: size of memory requested
150  **/
151 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
152 			     u32 size)
153 {
154 	mem->size = size;
155 	mem->va = kzalloc(size, GFP_KERNEL);
156 
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_virt_mem_d - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
169 {
170 	/* it's ok to kfree a NULL pointer */
171 	kfree(mem->va);
172 	mem->va = NULL;
173 	mem->size = 0;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_get_lump - find a lump of free generic resource
180  * @pf: board private structure
181  * @pile: the pile of resource to search
182  * @needed: the number of items needed
183  * @id: an owner id to stick on the items assigned
184  *
185  * Returns the base item index of the lump, or negative for error
186  *
187  * The search_hint trick and lack of advanced fit-finding only work
188  * because we're highly likely to have all the same size lump requests.
189  * Linear search time and any fragmentation should be minimal.
190  **/
191 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
192 			 u16 needed, u16 id)
193 {
194 	int ret = -ENOMEM;
195 	int i, j;
196 
197 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
198 		dev_info(&pf->pdev->dev,
199 			 "param err: pile=%s needed=%d id=0x%04x\n",
200 			 pile ? "<valid>" : "<null>", needed, id);
201 		return -EINVAL;
202 	}
203 
204 	/* start the linear search with an imperfect hint */
205 	i = pile->search_hint;
206 	while (i < pile->num_entries) {
207 		/* skip already allocated entries */
208 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
209 			i++;
210 			continue;
211 		}
212 
213 		/* do we have enough in this lump? */
214 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
215 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
216 				break;
217 		}
218 
219 		if (j == needed) {
220 			/* there was enough, so assign it to the requestor */
221 			for (j = 0; j < needed; j++)
222 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
223 			ret = i;
224 			pile->search_hint = i + j;
225 			break;
226 		}
227 
228 		/* not enough, so skip over it and continue looking */
229 		i += j;
230 	}
231 
232 	return ret;
233 }
234 
235 /**
236  * i40e_put_lump - return a lump of generic resource
237  * @pile: the pile of resource to search
238  * @index: the base item index
239  * @id: the owner id of the items assigned
240  *
241  * Returns the count of items in the lump
242  **/
243 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
244 {
245 	int valid_id = (id | I40E_PILE_VALID_BIT);
246 	int count = 0;
247 	int i;
248 
249 	if (!pile || index >= pile->num_entries)
250 		return -EINVAL;
251 
252 	for (i = index;
253 	     i < pile->num_entries && pile->list[i] == valid_id;
254 	     i++) {
255 		pile->list[i] = 0;
256 		count++;
257 	}
258 
259 	if (count && index < pile->search_hint)
260 		pile->search_hint = index;
261 
262 	return count;
263 }
264 
265 /**
266  * i40e_find_vsi_from_id - searches for the vsi with the given id
267  * @pf: the pf structure to search for the vsi
268  * @id: id of the vsi it is searching for
269  **/
270 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
271 {
272 	int i;
273 
274 	for (i = 0; i < pf->num_alloc_vsi; i++)
275 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
276 			return pf->vsi[i];
277 
278 	return NULL;
279 }
280 
281 /**
282  * i40e_service_event_schedule - Schedule the service task to wake up
283  * @pf: board private structure
284  *
285  * If not already scheduled, this puts the task into the work queue
286  **/
287 void i40e_service_event_schedule(struct i40e_pf *pf)
288 {
289 	if ((!test_bit(__I40E_DOWN, pf->state) &&
290 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
291 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
292 		queue_work(i40e_wq, &pf->service_task);
293 }
294 
295 /**
296  * i40e_tx_timeout - Respond to a Tx Hang
297  * @netdev: network interface device structure
298  *
299  * If any port has noticed a Tx timeout, it is likely that the whole
300  * device is munged, not just the one netdev port, so go for the full
301  * reset.
302  **/
303 static void i40e_tx_timeout(struct net_device *netdev)
304 {
305 	struct i40e_netdev_priv *np = netdev_priv(netdev);
306 	struct i40e_vsi *vsi = np->vsi;
307 	struct i40e_pf *pf = vsi->back;
308 	struct i40e_ring *tx_ring = NULL;
309 	unsigned int i, hung_queue = 0;
310 	u32 head, val;
311 
312 	pf->tx_timeout_count++;
313 
314 	/* find the stopped queue the same way the stack does */
315 	for (i = 0; i < netdev->num_tx_queues; i++) {
316 		struct netdev_queue *q;
317 		unsigned long trans_start;
318 
319 		q = netdev_get_tx_queue(netdev, i);
320 		trans_start = q->trans_start;
321 		if (netif_xmit_stopped(q) &&
322 		    time_after(jiffies,
323 			       (trans_start + netdev->watchdog_timeo))) {
324 			hung_queue = i;
325 			break;
326 		}
327 	}
328 
329 	if (i == netdev->num_tx_queues) {
330 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
331 	} else {
332 		/* now that we have an index, find the tx_ring struct */
333 		for (i = 0; i < vsi->num_queue_pairs; i++) {
334 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
335 				if (hung_queue ==
336 				    vsi->tx_rings[i]->queue_index) {
337 					tx_ring = vsi->tx_rings[i];
338 					break;
339 				}
340 			}
341 		}
342 	}
343 
344 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
345 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
346 	else if (time_before(jiffies,
347 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
348 		return;   /* don't do any new action before the next timeout */
349 
350 	/* don't kick off another recovery if one is already pending */
351 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
352 		return;
353 
354 	if (tx_ring) {
355 		head = i40e_get_head(tx_ring);
356 		/* Read interrupt register */
357 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 			val = rd32(&pf->hw,
359 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 						tx_ring->vsi->base_vector - 1));
361 		else
362 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363 
364 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
365 			    vsi->seid, hung_queue, tx_ring->next_to_clean,
366 			    head, tx_ring->next_to_use,
367 			    readl(tx_ring->tail), val);
368 	}
369 
370 	pf->tx_timeout_last_recovery = jiffies;
371 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 		    pf->tx_timeout_recovery_level, hung_queue);
373 
374 	switch (pf->tx_timeout_recovery_level) {
375 	case 1:
376 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
377 		break;
378 	case 2:
379 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
380 		break;
381 	case 3:
382 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
383 		break;
384 	default:
385 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
386 		break;
387 	}
388 
389 	i40e_service_event_schedule(pf);
390 	pf->tx_timeout_recovery_level++;
391 }
392 
393 /**
394  * i40e_get_vsi_stats_struct - Get System Network Statistics
395  * @vsi: the VSI we care about
396  *
397  * Returns the address of the device statistics structure.
398  * The statistics are actually updated from the service task.
399  **/
400 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401 {
402 	return &vsi->net_stats;
403 }
404 
405 /**
406  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
407  * @ring: Tx ring to get statistics from
408  * @stats: statistics entry to be updated
409  **/
410 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
411 					    struct rtnl_link_stats64 *stats)
412 {
413 	u64 bytes, packets;
414 	unsigned int start;
415 
416 	do {
417 		start = u64_stats_fetch_begin_irq(&ring->syncp);
418 		packets = ring->stats.packets;
419 		bytes   = ring->stats.bytes;
420 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
421 
422 	stats->tx_packets += packets;
423 	stats->tx_bytes   += bytes;
424 }
425 
426 /**
427  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
428  * @netdev: network interface device structure
429  * @stats: data structure to store statistics
430  *
431  * Returns the address of the device statistics structure.
432  * The statistics are actually updated from the service task.
433  **/
434 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
435 				  struct rtnl_link_stats64 *stats)
436 {
437 	struct i40e_netdev_priv *np = netdev_priv(netdev);
438 	struct i40e_vsi *vsi = np->vsi;
439 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
440 	struct i40e_ring *ring;
441 	int i;
442 
443 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
444 		return;
445 
446 	if (!vsi->tx_rings)
447 		return;
448 
449 	rcu_read_lock();
450 	for (i = 0; i < vsi->num_queue_pairs; i++) {
451 		u64 bytes, packets;
452 		unsigned int start;
453 
454 		ring = READ_ONCE(vsi->tx_rings[i]);
455 		if (!ring)
456 			continue;
457 		i40e_get_netdev_stats_struct_tx(ring, stats);
458 
459 		if (i40e_enabled_xdp_vsi(vsi)) {
460 			ring++;
461 			i40e_get_netdev_stats_struct_tx(ring, stats);
462 		}
463 
464 		ring++;
465 		do {
466 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
467 			packets = ring->stats.packets;
468 			bytes   = ring->stats.bytes;
469 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
470 
471 		stats->rx_packets += packets;
472 		stats->rx_bytes   += bytes;
473 
474 	}
475 	rcu_read_unlock();
476 
477 	/* following stats updated by i40e_watchdog_subtask() */
478 	stats->multicast	= vsi_stats->multicast;
479 	stats->tx_errors	= vsi_stats->tx_errors;
480 	stats->tx_dropped	= vsi_stats->tx_dropped;
481 	stats->rx_errors	= vsi_stats->rx_errors;
482 	stats->rx_dropped	= vsi_stats->rx_dropped;
483 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
484 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
485 }
486 
487 /**
488  * i40e_vsi_reset_stats - Resets all stats of the given vsi
489  * @vsi: the VSI to have its stats reset
490  **/
491 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
492 {
493 	struct rtnl_link_stats64 *ns;
494 	int i;
495 
496 	if (!vsi)
497 		return;
498 
499 	ns = i40e_get_vsi_stats_struct(vsi);
500 	memset(ns, 0, sizeof(*ns));
501 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
502 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
503 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
504 	if (vsi->rx_rings && vsi->rx_rings[0]) {
505 		for (i = 0; i < vsi->num_queue_pairs; i++) {
506 			memset(&vsi->rx_rings[i]->stats, 0,
507 			       sizeof(vsi->rx_rings[i]->stats));
508 			memset(&vsi->rx_rings[i]->rx_stats, 0,
509 			       sizeof(vsi->rx_rings[i]->rx_stats));
510 			memset(&vsi->tx_rings[i]->stats, 0,
511 			       sizeof(vsi->tx_rings[i]->stats));
512 			memset(&vsi->tx_rings[i]->tx_stats, 0,
513 			       sizeof(vsi->tx_rings[i]->tx_stats));
514 		}
515 	}
516 	vsi->stat_offsets_loaded = false;
517 }
518 
519 /**
520  * i40e_pf_reset_stats - Reset all of the stats for the given PF
521  * @pf: the PF to be reset
522  **/
523 void i40e_pf_reset_stats(struct i40e_pf *pf)
524 {
525 	int i;
526 
527 	memset(&pf->stats, 0, sizeof(pf->stats));
528 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
529 	pf->stat_offsets_loaded = false;
530 
531 	for (i = 0; i < I40E_MAX_VEB; i++) {
532 		if (pf->veb[i]) {
533 			memset(&pf->veb[i]->stats, 0,
534 			       sizeof(pf->veb[i]->stats));
535 			memset(&pf->veb[i]->stats_offsets, 0,
536 			       sizeof(pf->veb[i]->stats_offsets));
537 			pf->veb[i]->stat_offsets_loaded = false;
538 		}
539 	}
540 	pf->hw_csum_rx_error = 0;
541 }
542 
543 /**
544  * i40e_stat_update48 - read and update a 48 bit stat from the chip
545  * @hw: ptr to the hardware info
546  * @hireg: the high 32 bit reg to read
547  * @loreg: the low 32 bit reg to read
548  * @offset_loaded: has the initial offset been loaded yet
549  * @offset: ptr to current offset value
550  * @stat: ptr to the stat
551  *
552  * Since the device stats are not reset at PFReset, they likely will not
553  * be zeroed when the driver starts.  We'll save the first values read
554  * and use them as offsets to be subtracted from the raw values in order
555  * to report stats that count from zero.  In the process, we also manage
556  * the potential roll-over.
557  **/
558 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
559 			       bool offset_loaded, u64 *offset, u64 *stat)
560 {
561 	u64 new_data;
562 
563 	if (hw->device_id == I40E_DEV_ID_QEMU) {
564 		new_data = rd32(hw, loreg);
565 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
566 	} else {
567 		new_data = rd64(hw, loreg);
568 	}
569 	if (!offset_loaded)
570 		*offset = new_data;
571 	if (likely(new_data >= *offset))
572 		*stat = new_data - *offset;
573 	else
574 		*stat = (new_data + BIT_ULL(48)) - *offset;
575 	*stat &= 0xFFFFFFFFFFFFULL;
576 }
577 
578 /**
579  * i40e_stat_update32 - read and update a 32 bit stat from the chip
580  * @hw: ptr to the hardware info
581  * @reg: the hw reg to read
582  * @offset_loaded: has the initial offset been loaded yet
583  * @offset: ptr to current offset value
584  * @stat: ptr to the stat
585  **/
586 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
587 			       bool offset_loaded, u64 *offset, u64 *stat)
588 {
589 	u32 new_data;
590 
591 	new_data = rd32(hw, reg);
592 	if (!offset_loaded)
593 		*offset = new_data;
594 	if (likely(new_data >= *offset))
595 		*stat = (u32)(new_data - *offset);
596 	else
597 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
598 }
599 
600 /**
601  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
602  * @hw: ptr to the hardware info
603  * @reg: the hw reg to read and clear
604  * @stat: ptr to the stat
605  **/
606 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
607 {
608 	u32 new_data = rd32(hw, reg);
609 
610 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
611 	*stat += new_data;
612 }
613 
614 /**
615  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
616  * @vsi: the VSI to be updated
617  **/
618 void i40e_update_eth_stats(struct i40e_vsi *vsi)
619 {
620 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
621 	struct i40e_pf *pf = vsi->back;
622 	struct i40e_hw *hw = &pf->hw;
623 	struct i40e_eth_stats *oes;
624 	struct i40e_eth_stats *es;     /* device's eth stats */
625 
626 	es = &vsi->eth_stats;
627 	oes = &vsi->eth_stats_offsets;
628 
629 	/* Gather up the stats that the hw collects */
630 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
631 			   vsi->stat_offsets_loaded,
632 			   &oes->tx_errors, &es->tx_errors);
633 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
634 			   vsi->stat_offsets_loaded,
635 			   &oes->rx_discards, &es->rx_discards);
636 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
637 			   vsi->stat_offsets_loaded,
638 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
639 
640 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
641 			   I40E_GLV_GORCL(stat_idx),
642 			   vsi->stat_offsets_loaded,
643 			   &oes->rx_bytes, &es->rx_bytes);
644 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
645 			   I40E_GLV_UPRCL(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unicast, &es->rx_unicast);
648 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
649 			   I40E_GLV_MPRCL(stat_idx),
650 			   vsi->stat_offsets_loaded,
651 			   &oes->rx_multicast, &es->rx_multicast);
652 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
653 			   I40E_GLV_BPRCL(stat_idx),
654 			   vsi->stat_offsets_loaded,
655 			   &oes->rx_broadcast, &es->rx_broadcast);
656 
657 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
658 			   I40E_GLV_GOTCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->tx_bytes, &es->tx_bytes);
661 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
662 			   I40E_GLV_UPTCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->tx_unicast, &es->tx_unicast);
665 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
666 			   I40E_GLV_MPTCL(stat_idx),
667 			   vsi->stat_offsets_loaded,
668 			   &oes->tx_multicast, &es->tx_multicast);
669 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
670 			   I40E_GLV_BPTCL(stat_idx),
671 			   vsi->stat_offsets_loaded,
672 			   &oes->tx_broadcast, &es->tx_broadcast);
673 	vsi->stat_offsets_loaded = true;
674 }
675 
676 /**
677  * i40e_update_veb_stats - Update Switch component statistics
678  * @veb: the VEB being updated
679  **/
680 static void i40e_update_veb_stats(struct i40e_veb *veb)
681 {
682 	struct i40e_pf *pf = veb->pf;
683 	struct i40e_hw *hw = &pf->hw;
684 	struct i40e_eth_stats *oes;
685 	struct i40e_eth_stats *es;     /* device's eth stats */
686 	struct i40e_veb_tc_stats *veb_oes;
687 	struct i40e_veb_tc_stats *veb_es;
688 	int i, idx = 0;
689 
690 	idx = veb->stats_idx;
691 	es = &veb->stats;
692 	oes = &veb->stats_offsets;
693 	veb_es = &veb->tc_stats;
694 	veb_oes = &veb->tc_stats_offsets;
695 
696 	/* Gather up the stats that the hw collects */
697 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
698 			   veb->stat_offsets_loaded,
699 			   &oes->tx_discards, &es->tx_discards);
700 	if (hw->revision_id > 0)
701 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
702 				   veb->stat_offsets_loaded,
703 				   &oes->rx_unknown_protocol,
704 				   &es->rx_unknown_protocol);
705 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
706 			   veb->stat_offsets_loaded,
707 			   &oes->rx_bytes, &es->rx_bytes);
708 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
709 			   veb->stat_offsets_loaded,
710 			   &oes->rx_unicast, &es->rx_unicast);
711 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
712 			   veb->stat_offsets_loaded,
713 			   &oes->rx_multicast, &es->rx_multicast);
714 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_broadcast, &es->rx_broadcast);
717 
718 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->tx_bytes, &es->tx_bytes);
721 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->tx_unicast, &es->tx_unicast);
724 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->tx_multicast, &es->tx_multicast);
727 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_broadcast, &es->tx_broadcast);
730 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
731 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
732 				   I40E_GLVEBTC_RPCL(i, idx),
733 				   veb->stat_offsets_loaded,
734 				   &veb_oes->tc_rx_packets[i],
735 				   &veb_es->tc_rx_packets[i]);
736 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
737 				   I40E_GLVEBTC_RBCL(i, idx),
738 				   veb->stat_offsets_loaded,
739 				   &veb_oes->tc_rx_bytes[i],
740 				   &veb_es->tc_rx_bytes[i]);
741 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
742 				   I40E_GLVEBTC_TPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_tx_packets[i],
745 				   &veb_es->tc_tx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
747 				   I40E_GLVEBTC_TBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_tx_bytes[i],
750 				   &veb_es->tc_tx_bytes[i]);
751 	}
752 	veb->stat_offsets_loaded = true;
753 }
754 
755 /**
756  * i40e_update_vsi_stats - Update the vsi statistics counters.
757  * @vsi: the VSI to be updated
758  *
759  * There are a few instances where we store the same stat in a
760  * couple of different structs.  This is partly because we have
761  * the netdev stats that need to be filled out, which is slightly
762  * different from the "eth_stats" defined by the chip and used in
763  * VF communications.  We sort it out here.
764  **/
765 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
766 {
767 	struct i40e_pf *pf = vsi->back;
768 	struct rtnl_link_stats64 *ons;
769 	struct rtnl_link_stats64 *ns;   /* netdev stats */
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	u32 tx_restart, tx_busy;
773 	struct i40e_ring *p;
774 	u32 rx_page, rx_buf;
775 	u64 bytes, packets;
776 	unsigned int start;
777 	u64 tx_linearize;
778 	u64 tx_force_wb;
779 	u64 rx_p, rx_b;
780 	u64 tx_p, tx_b;
781 	u16 q;
782 
783 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
784 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
785 		return;
786 
787 	ns = i40e_get_vsi_stats_struct(vsi);
788 	ons = &vsi->net_stats_offsets;
789 	es = &vsi->eth_stats;
790 	oes = &vsi->eth_stats_offsets;
791 
792 	/* Gather up the netdev and vsi stats that the driver collects
793 	 * on the fly during packet processing
794 	 */
795 	rx_b = rx_p = 0;
796 	tx_b = tx_p = 0;
797 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
798 	rx_page = 0;
799 	rx_buf = 0;
800 	rcu_read_lock();
801 	for (q = 0; q < vsi->num_queue_pairs; q++) {
802 		/* locate Tx ring */
803 		p = READ_ONCE(vsi->tx_rings[q]);
804 
805 		do {
806 			start = u64_stats_fetch_begin_irq(&p->syncp);
807 			packets = p->stats.packets;
808 			bytes = p->stats.bytes;
809 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
810 		tx_b += bytes;
811 		tx_p += packets;
812 		tx_restart += p->tx_stats.restart_queue;
813 		tx_busy += p->tx_stats.tx_busy;
814 		tx_linearize += p->tx_stats.tx_linearize;
815 		tx_force_wb += p->tx_stats.tx_force_wb;
816 
817 		/* Rx queue is part of the same block as Tx queue */
818 		p = &p[1];
819 		do {
820 			start = u64_stats_fetch_begin_irq(&p->syncp);
821 			packets = p->stats.packets;
822 			bytes = p->stats.bytes;
823 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
824 		rx_b += bytes;
825 		rx_p += packets;
826 		rx_buf += p->rx_stats.alloc_buff_failed;
827 		rx_page += p->rx_stats.alloc_page_failed;
828 	}
829 	rcu_read_unlock();
830 	vsi->tx_restart = tx_restart;
831 	vsi->tx_busy = tx_busy;
832 	vsi->tx_linearize = tx_linearize;
833 	vsi->tx_force_wb = tx_force_wb;
834 	vsi->rx_page_failed = rx_page;
835 	vsi->rx_buf_failed = rx_buf;
836 
837 	ns->rx_packets = rx_p;
838 	ns->rx_bytes = rx_b;
839 	ns->tx_packets = tx_p;
840 	ns->tx_bytes = tx_b;
841 
842 	/* update netdev stats from eth stats */
843 	i40e_update_eth_stats(vsi);
844 	ons->tx_errors = oes->tx_errors;
845 	ns->tx_errors = es->tx_errors;
846 	ons->multicast = oes->rx_multicast;
847 	ns->multicast = es->rx_multicast;
848 	ons->rx_dropped = oes->rx_discards;
849 	ns->rx_dropped = es->rx_discards;
850 	ons->tx_dropped = oes->tx_discards;
851 	ns->tx_dropped = es->tx_discards;
852 
853 	/* pull in a couple PF stats if this is the main vsi */
854 	if (vsi == pf->vsi[pf->lan_vsi]) {
855 		ns->rx_crc_errors = pf->stats.crc_errors;
856 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
857 		ns->rx_length_errors = pf->stats.rx_length_errors;
858 	}
859 }
860 
861 /**
862  * i40e_update_pf_stats - Update the PF statistics counters.
863  * @pf: the PF to be updated
864  **/
865 static void i40e_update_pf_stats(struct i40e_pf *pf)
866 {
867 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
868 	struct i40e_hw_port_stats *nsd = &pf->stats;
869 	struct i40e_hw *hw = &pf->hw;
870 	u32 val;
871 	int i;
872 
873 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
874 			   I40E_GLPRT_GORCL(hw->port),
875 			   pf->stat_offsets_loaded,
876 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
877 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
878 			   I40E_GLPRT_GOTCL(hw->port),
879 			   pf->stat_offsets_loaded,
880 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
881 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
882 			   pf->stat_offsets_loaded,
883 			   &osd->eth.rx_discards,
884 			   &nsd->eth.rx_discards);
885 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
886 			   I40E_GLPRT_UPRCL(hw->port),
887 			   pf->stat_offsets_loaded,
888 			   &osd->eth.rx_unicast,
889 			   &nsd->eth.rx_unicast);
890 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
891 			   I40E_GLPRT_MPRCL(hw->port),
892 			   pf->stat_offsets_loaded,
893 			   &osd->eth.rx_multicast,
894 			   &nsd->eth.rx_multicast);
895 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
896 			   I40E_GLPRT_BPRCL(hw->port),
897 			   pf->stat_offsets_loaded,
898 			   &osd->eth.rx_broadcast,
899 			   &nsd->eth.rx_broadcast);
900 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
901 			   I40E_GLPRT_UPTCL(hw->port),
902 			   pf->stat_offsets_loaded,
903 			   &osd->eth.tx_unicast,
904 			   &nsd->eth.tx_unicast);
905 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
906 			   I40E_GLPRT_MPTCL(hw->port),
907 			   pf->stat_offsets_loaded,
908 			   &osd->eth.tx_multicast,
909 			   &nsd->eth.tx_multicast);
910 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
911 			   I40E_GLPRT_BPTCL(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->eth.tx_broadcast,
914 			   &nsd->eth.tx_broadcast);
915 
916 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
917 			   pf->stat_offsets_loaded,
918 			   &osd->tx_dropped_link_down,
919 			   &nsd->tx_dropped_link_down);
920 
921 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
922 			   pf->stat_offsets_loaded,
923 			   &osd->crc_errors, &nsd->crc_errors);
924 
925 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
926 			   pf->stat_offsets_loaded,
927 			   &osd->illegal_bytes, &nsd->illegal_bytes);
928 
929 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->mac_local_faults,
932 			   &nsd->mac_local_faults);
933 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
934 			   pf->stat_offsets_loaded,
935 			   &osd->mac_remote_faults,
936 			   &nsd->mac_remote_faults);
937 
938 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->rx_length_errors,
941 			   &nsd->rx_length_errors);
942 
943 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
944 			   pf->stat_offsets_loaded,
945 			   &osd->link_xon_rx, &nsd->link_xon_rx);
946 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->link_xon_tx, &nsd->link_xon_tx);
949 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
952 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
955 
956 	for (i = 0; i < 8; i++) {
957 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
958 				   pf->stat_offsets_loaded,
959 				   &osd->priority_xoff_rx[i],
960 				   &nsd->priority_xoff_rx[i]);
961 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
962 				   pf->stat_offsets_loaded,
963 				   &osd->priority_xon_rx[i],
964 				   &nsd->priority_xon_rx[i]);
965 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
966 				   pf->stat_offsets_loaded,
967 				   &osd->priority_xon_tx[i],
968 				   &nsd->priority_xon_tx[i]);
969 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
970 				   pf->stat_offsets_loaded,
971 				   &osd->priority_xoff_tx[i],
972 				   &nsd->priority_xoff_tx[i]);
973 		i40e_stat_update32(hw,
974 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
975 				   pf->stat_offsets_loaded,
976 				   &osd->priority_xon_2_xoff[i],
977 				   &nsd->priority_xon_2_xoff[i]);
978 	}
979 
980 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
981 			   I40E_GLPRT_PRC64L(hw->port),
982 			   pf->stat_offsets_loaded,
983 			   &osd->rx_size_64, &nsd->rx_size_64);
984 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
985 			   I40E_GLPRT_PRC127L(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->rx_size_127, &nsd->rx_size_127);
988 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
989 			   I40E_GLPRT_PRC255L(hw->port),
990 			   pf->stat_offsets_loaded,
991 			   &osd->rx_size_255, &nsd->rx_size_255);
992 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
993 			   I40E_GLPRT_PRC511L(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->rx_size_511, &nsd->rx_size_511);
996 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
997 			   I40E_GLPRT_PRC1023L(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1000 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1001 			   I40E_GLPRT_PRC1522L(hw->port),
1002 			   pf->stat_offsets_loaded,
1003 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1004 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1005 			   I40E_GLPRT_PRC9522L(hw->port),
1006 			   pf->stat_offsets_loaded,
1007 			   &osd->rx_size_big, &nsd->rx_size_big);
1008 
1009 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1010 			   I40E_GLPRT_PTC64L(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->tx_size_64, &nsd->tx_size_64);
1013 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1014 			   I40E_GLPRT_PTC127L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->tx_size_127, &nsd->tx_size_127);
1017 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1018 			   I40E_GLPRT_PTC255L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->tx_size_255, &nsd->tx_size_255);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1022 			   I40E_GLPRT_PTC511L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->tx_size_511, &nsd->tx_size_511);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1026 			   I40E_GLPRT_PTC1023L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1030 			   I40E_GLPRT_PTC1522L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1034 			   I40E_GLPRT_PTC9522L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->tx_size_big, &nsd->tx_size_big);
1037 
1038 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_undersize, &nsd->rx_undersize);
1041 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->rx_fragments, &nsd->rx_fragments);
1044 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->rx_oversize, &nsd->rx_oversize);
1047 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_jabber, &nsd->rx_jabber);
1050 
1051 	/* FDIR stats */
1052 	i40e_stat_update_and_clear32(hw,
1053 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1054 			&nsd->fd_atr_match);
1055 	i40e_stat_update_and_clear32(hw,
1056 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1057 			&nsd->fd_sb_match);
1058 	i40e_stat_update_and_clear32(hw,
1059 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1060 			&nsd->fd_atr_tunnel_match);
1061 
1062 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1063 	nsd->tx_lpi_status =
1064 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1065 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1066 	nsd->rx_lpi_status =
1067 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1068 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1069 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1072 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1075 
1076 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1077 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1078 		nsd->fd_sb_status = true;
1079 	else
1080 		nsd->fd_sb_status = false;
1081 
1082 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1083 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1084 		nsd->fd_atr_status = true;
1085 	else
1086 		nsd->fd_atr_status = false;
1087 
1088 	pf->stat_offsets_loaded = true;
1089 }
1090 
1091 /**
1092  * i40e_update_stats - Update the various statistics counters.
1093  * @vsi: the VSI to be updated
1094  *
1095  * Update the various stats for this VSI and its related entities.
1096  **/
1097 void i40e_update_stats(struct i40e_vsi *vsi)
1098 {
1099 	struct i40e_pf *pf = vsi->back;
1100 
1101 	if (vsi == pf->vsi[pf->lan_vsi])
1102 		i40e_update_pf_stats(pf);
1103 
1104 	i40e_update_vsi_stats(vsi);
1105 }
1106 
1107 /**
1108  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1109  * @vsi: the VSI to be searched
1110  * @macaddr: the MAC address
1111  * @vlan: the vlan
1112  *
1113  * Returns ptr to the filter object or NULL
1114  **/
1115 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1116 						const u8 *macaddr, s16 vlan)
1117 {
1118 	struct i40e_mac_filter *f;
1119 	u64 key;
1120 
1121 	if (!vsi || !macaddr)
1122 		return NULL;
1123 
1124 	key = i40e_addr_to_hkey(macaddr);
1125 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1126 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1127 		    (vlan == f->vlan))
1128 			return f;
1129 	}
1130 	return NULL;
1131 }
1132 
1133 /**
1134  * i40e_find_mac - Find a mac addr in the macvlan filters list
1135  * @vsi: the VSI to be searched
1136  * @macaddr: the MAC address we are searching for
1137  *
1138  * Returns the first filter with the provided MAC address or NULL if
1139  * MAC address was not found
1140  **/
1141 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1142 {
1143 	struct i40e_mac_filter *f;
1144 	u64 key;
1145 
1146 	if (!vsi || !macaddr)
1147 		return NULL;
1148 
1149 	key = i40e_addr_to_hkey(macaddr);
1150 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1151 		if ((ether_addr_equal(macaddr, f->macaddr)))
1152 			return f;
1153 	}
1154 	return NULL;
1155 }
1156 
1157 /**
1158  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns true if VSI is in vlan mode or false otherwise
1162  **/
1163 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1164 {
1165 	/* If we have a PVID, always operate in VLAN mode */
1166 	if (vsi->info.pvid)
1167 		return true;
1168 
1169 	/* We need to operate in VLAN mode whenever we have any filters with
1170 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1171 	 * time, incurring search cost repeatedly. However, we can notice two
1172 	 * things:
1173 	 *
1174 	 * 1) the only place where we can gain a VLAN filter is in
1175 	 *    i40e_add_filter.
1176 	 *
1177 	 * 2) the only place where filters are actually removed is in
1178 	 *    i40e_sync_filters_subtask.
1179 	 *
1180 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1181 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1182 	 * we have to perform the full search after deleting filters in
1183 	 * i40e_sync_filters_subtask, but we already have to search
1184 	 * filters here and can perform the check at the same time. This
1185 	 * results in avoiding embedding a loop for VLAN mode inside another
1186 	 * loop over all the filters, and should maintain correctness as noted
1187 	 * above.
1188 	 */
1189 	return vsi->has_vlan_filter;
1190 }
1191 
1192 /**
1193  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1194  * @vsi: the VSI to configure
1195  * @tmp_add_list: list of filters ready to be added
1196  * @tmp_del_list: list of filters ready to be deleted
1197  * @vlan_filters: the number of active VLAN filters
1198  *
1199  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1200  * behave as expected. If we have any active VLAN filters remaining or about
1201  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1202  * so that they only match against untagged traffic. If we no longer have any
1203  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1204  * so that they match against both tagged and untagged traffic. In this way,
1205  * we ensure that we correctly receive the desired traffic. This ensures that
1206  * when we have an active VLAN we will receive only untagged traffic and
1207  * traffic matching active VLANs. If we have no active VLANs then we will
1208  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1209  *
1210  * Finally, in a similar fashion, this function also corrects filters when
1211  * there is an active PVID assigned to this VSI.
1212  *
1213  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1214  *
1215  * This function is only expected to be called from within
1216  * i40e_sync_vsi_filters.
1217  *
1218  * NOTE: This function expects to be called while under the
1219  * mac_filter_hash_lock
1220  */
1221 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1222 					 struct hlist_head *tmp_add_list,
1223 					 struct hlist_head *tmp_del_list,
1224 					 int vlan_filters)
1225 {
1226 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1227 	struct i40e_mac_filter *f, *add_head;
1228 	struct i40e_new_mac_filter *new;
1229 	struct hlist_node *h;
1230 	int bkt, new_vlan;
1231 
1232 	/* To determine if a particular filter needs to be replaced we
1233 	 * have the three following conditions:
1234 	 *
1235 	 * a) if we have a PVID assigned, then all filters which are
1236 	 *    not marked as VLAN=PVID must be replaced with filters that
1237 	 *    are.
1238 	 * b) otherwise, if we have any active VLANS, all filters
1239 	 *    which are marked as VLAN=-1 must be replaced with
1240 	 *    filters marked as VLAN=0
1241 	 * c) finally, if we do not have any active VLANS, all filters
1242 	 *    which are marked as VLAN=0 must be replaced with filters
1243 	 *    marked as VLAN=-1
1244 	 */
1245 
1246 	/* Update the filters about to be added in place */
1247 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1248 		if (pvid && new->f->vlan != pvid)
1249 			new->f->vlan = pvid;
1250 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1251 			new->f->vlan = 0;
1252 		else if (!vlan_filters && new->f->vlan == 0)
1253 			new->f->vlan = I40E_VLAN_ANY;
1254 	}
1255 
1256 	/* Update the remaining active filters */
1257 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1258 		/* Combine the checks for whether a filter needs to be changed
1259 		 * and then determine the new VLAN inside the if block, in
1260 		 * order to avoid duplicating code for adding the new filter
1261 		 * then deleting the old filter.
1262 		 */
1263 		if ((pvid && f->vlan != pvid) ||
1264 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1265 		    (!vlan_filters && f->vlan == 0)) {
1266 			/* Determine the new vlan we will be adding */
1267 			if (pvid)
1268 				new_vlan = pvid;
1269 			else if (vlan_filters)
1270 				new_vlan = 0;
1271 			else
1272 				new_vlan = I40E_VLAN_ANY;
1273 
1274 			/* Create the new filter */
1275 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1276 			if (!add_head)
1277 				return -ENOMEM;
1278 
1279 			/* Create a temporary i40e_new_mac_filter */
1280 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1281 			if (!new)
1282 				return -ENOMEM;
1283 
1284 			new->f = add_head;
1285 			new->state = add_head->state;
1286 
1287 			/* Add the new filter to the tmp list */
1288 			hlist_add_head(&new->hlist, tmp_add_list);
1289 
1290 			/* Put the original filter into the delete list */
1291 			f->state = I40E_FILTER_REMOVE;
1292 			hash_del(&f->hlist);
1293 			hlist_add_head(&f->hlist, tmp_del_list);
1294 		}
1295 	}
1296 
1297 	vsi->has_vlan_filter = !!vlan_filters;
1298 
1299 	return 0;
1300 }
1301 
1302 /**
1303  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1304  * @vsi: the PF Main VSI - inappropriate for any other VSI
1305  * @macaddr: the MAC address
1306  *
1307  * Remove whatever filter the firmware set up so the driver can manage
1308  * its own filtering intelligently.
1309  **/
1310 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1311 {
1312 	struct i40e_aqc_remove_macvlan_element_data element;
1313 	struct i40e_pf *pf = vsi->back;
1314 
1315 	/* Only appropriate for the PF main VSI */
1316 	if (vsi->type != I40E_VSI_MAIN)
1317 		return;
1318 
1319 	memset(&element, 0, sizeof(element));
1320 	ether_addr_copy(element.mac_addr, macaddr);
1321 	element.vlan_tag = 0;
1322 	/* Ignore error returns, some firmware does it this way... */
1323 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1324 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1325 
1326 	memset(&element, 0, sizeof(element));
1327 	ether_addr_copy(element.mac_addr, macaddr);
1328 	element.vlan_tag = 0;
1329 	/* ...and some firmware does it this way. */
1330 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1331 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1332 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1333 }
1334 
1335 /**
1336  * i40e_add_filter - Add a mac/vlan filter to the VSI
1337  * @vsi: the VSI to be searched
1338  * @macaddr: the MAC address
1339  * @vlan: the vlan
1340  *
1341  * Returns ptr to the filter object or NULL when no memory available.
1342  *
1343  * NOTE: This function is expected to be called with mac_filter_hash_lock
1344  * being held.
1345  **/
1346 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1347 					const u8 *macaddr, s16 vlan)
1348 {
1349 	struct i40e_mac_filter *f;
1350 	u64 key;
1351 
1352 	if (!vsi || !macaddr)
1353 		return NULL;
1354 
1355 	f = i40e_find_filter(vsi, macaddr, vlan);
1356 	if (!f) {
1357 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1358 		if (!f)
1359 			return NULL;
1360 
1361 		/* Update the boolean indicating if we need to function in
1362 		 * VLAN mode.
1363 		 */
1364 		if (vlan >= 0)
1365 			vsi->has_vlan_filter = true;
1366 
1367 		ether_addr_copy(f->macaddr, macaddr);
1368 		f->vlan = vlan;
1369 		f->state = I40E_FILTER_NEW;
1370 		INIT_HLIST_NODE(&f->hlist);
1371 
1372 		key = i40e_addr_to_hkey(macaddr);
1373 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1374 
1375 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1376 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1377 	}
1378 
1379 	/* If we're asked to add a filter that has been marked for removal, it
1380 	 * is safe to simply restore it to active state. __i40e_del_filter
1381 	 * will have simply deleted any filters which were previously marked
1382 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1383 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1384 	 * task, just restore this filter to the ACTIVE state so that the
1385 	 * sync task leaves it in place
1386 	 */
1387 	if (f->state == I40E_FILTER_REMOVE)
1388 		f->state = I40E_FILTER_ACTIVE;
1389 
1390 	return f;
1391 }
1392 
1393 /**
1394  * __i40e_del_filter - Remove a specific filter from the VSI
1395  * @vsi: VSI to remove from
1396  * @f: the filter to remove from the list
1397  *
1398  * This function should be called instead of i40e_del_filter only if you know
1399  * the exact filter you will remove already, such as via i40e_find_filter or
1400  * i40e_find_mac.
1401  *
1402  * NOTE: This function is expected to be called with mac_filter_hash_lock
1403  * being held.
1404  * ANOTHER NOTE: This function MUST be called from within the context of
1405  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1406  * instead of list_for_each_entry().
1407  **/
1408 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1409 {
1410 	if (!f)
1411 		return;
1412 
1413 	/* If the filter was never added to firmware then we can just delete it
1414 	 * directly and we don't want to set the status to remove or else an
1415 	 * admin queue command will unnecessarily fire.
1416 	 */
1417 	if ((f->state == I40E_FILTER_FAILED) ||
1418 	    (f->state == I40E_FILTER_NEW)) {
1419 		hash_del(&f->hlist);
1420 		kfree(f);
1421 	} else {
1422 		f->state = I40E_FILTER_REMOVE;
1423 	}
1424 
1425 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1426 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1427 }
1428 
1429 /**
1430  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1431  * @vsi: the VSI to be searched
1432  * @macaddr: the MAC address
1433  * @vlan: the VLAN
1434  *
1435  * NOTE: This function is expected to be called with mac_filter_hash_lock
1436  * being held.
1437  * ANOTHER NOTE: This function MUST be called from within the context of
1438  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1439  * instead of list_for_each_entry().
1440  **/
1441 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1442 {
1443 	struct i40e_mac_filter *f;
1444 
1445 	if (!vsi || !macaddr)
1446 		return;
1447 
1448 	f = i40e_find_filter(vsi, macaddr, vlan);
1449 	__i40e_del_filter(vsi, f);
1450 }
1451 
1452 /**
1453  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1454  * @vsi: the VSI to be searched
1455  * @macaddr: the mac address to be filtered
1456  *
1457  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1458  * go through all the macvlan filters and add a macvlan filter for each
1459  * unique vlan that already exists. If a PVID has been assigned, instead only
1460  * add the macaddr to that VLAN.
1461  *
1462  * Returns last filter added on success, else NULL
1463  **/
1464 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1465 					    const u8 *macaddr)
1466 {
1467 	struct i40e_mac_filter *f, *add = NULL;
1468 	struct hlist_node *h;
1469 	int bkt;
1470 
1471 	if (vsi->info.pvid)
1472 		return i40e_add_filter(vsi, macaddr,
1473 				       le16_to_cpu(vsi->info.pvid));
1474 
1475 	if (!i40e_is_vsi_in_vlan(vsi))
1476 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1477 
1478 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1479 		if (f->state == I40E_FILTER_REMOVE)
1480 			continue;
1481 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1482 		if (!add)
1483 			return NULL;
1484 	}
1485 
1486 	return add;
1487 }
1488 
1489 /**
1490  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1491  * @vsi: the VSI to be searched
1492  * @macaddr: the mac address to be removed
1493  *
1494  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1495  * associated with.
1496  *
1497  * Returns 0 for success, or error
1498  **/
1499 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1500 {
1501 	struct i40e_mac_filter *f;
1502 	struct hlist_node *h;
1503 	bool found = false;
1504 	int bkt;
1505 
1506 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1507 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1508 		if (ether_addr_equal(macaddr, f->macaddr)) {
1509 			__i40e_del_filter(vsi, f);
1510 			found = true;
1511 		}
1512 	}
1513 
1514 	if (found)
1515 		return 0;
1516 	else
1517 		return -ENOENT;
1518 }
1519 
1520 /**
1521  * i40e_set_mac - NDO callback to set mac address
1522  * @netdev: network interface device structure
1523  * @p: pointer to an address structure
1524  *
1525  * Returns 0 on success, negative on failure
1526  **/
1527 static int i40e_set_mac(struct net_device *netdev, void *p)
1528 {
1529 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1530 	struct i40e_vsi *vsi = np->vsi;
1531 	struct i40e_pf *pf = vsi->back;
1532 	struct i40e_hw *hw = &pf->hw;
1533 	struct sockaddr *addr = p;
1534 
1535 	if (!is_valid_ether_addr(addr->sa_data))
1536 		return -EADDRNOTAVAIL;
1537 
1538 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1539 		netdev_info(netdev, "already using mac address %pM\n",
1540 			    addr->sa_data);
1541 		return 0;
1542 	}
1543 
1544 	if (test_bit(__I40E_DOWN, pf->state) ||
1545 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1546 		return -EADDRNOTAVAIL;
1547 
1548 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1549 		netdev_info(netdev, "returning to hw mac address %pM\n",
1550 			    hw->mac.addr);
1551 	else
1552 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1553 
1554 	/* Copy the address first, so that we avoid a possible race with
1555 	 * .set_rx_mode().
1556 	 * - Remove old address from MAC filter
1557 	 * - Copy new address
1558 	 * - Add new address to MAC filter
1559 	 */
1560 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1561 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1562 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1563 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1564 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1565 
1566 	if (vsi->type == I40E_VSI_MAIN) {
1567 		i40e_status ret;
1568 
1569 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1570 						addr->sa_data, NULL);
1571 		if (ret)
1572 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1573 				    i40e_stat_str(hw, ret),
1574 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1575 	}
1576 
1577 	/* schedule our worker thread which will take care of
1578 	 * applying the new filter changes
1579 	 */
1580 	i40e_service_event_schedule(pf);
1581 	return 0;
1582 }
1583 
1584 /**
1585  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1586  * @vsi: vsi structure
1587  * @seed: RSS hash seed
1588  **/
1589 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1590 			      u8 *lut, u16 lut_size)
1591 {
1592 	struct i40e_pf *pf = vsi->back;
1593 	struct i40e_hw *hw = &pf->hw;
1594 	int ret = 0;
1595 
1596 	if (seed) {
1597 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1598 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1599 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1600 		if (ret) {
1601 			dev_info(&pf->pdev->dev,
1602 				 "Cannot set RSS key, err %s aq_err %s\n",
1603 				 i40e_stat_str(hw, ret),
1604 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1605 			return ret;
1606 		}
1607 	}
1608 	if (lut) {
1609 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1610 
1611 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1612 		if (ret) {
1613 			dev_info(&pf->pdev->dev,
1614 				 "Cannot set RSS lut, err %s aq_err %s\n",
1615 				 i40e_stat_str(hw, ret),
1616 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1617 			return ret;
1618 		}
1619 	}
1620 	return ret;
1621 }
1622 
1623 /**
1624  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1625  * @vsi: VSI structure
1626  **/
1627 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1628 {
1629 	struct i40e_pf *pf = vsi->back;
1630 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1631 	u8 *lut;
1632 	int ret;
1633 
1634 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1635 		return 0;
1636 	if (!vsi->rss_size)
1637 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1638 				      vsi->num_queue_pairs);
1639 	if (!vsi->rss_size)
1640 		return -EINVAL;
1641 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1642 	if (!lut)
1643 		return -ENOMEM;
1644 
1645 	/* Use the user configured hash keys and lookup table if there is one,
1646 	 * otherwise use default
1647 	 */
1648 	if (vsi->rss_lut_user)
1649 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1650 	else
1651 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1652 	if (vsi->rss_hkey_user)
1653 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1654 	else
1655 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1656 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1657 	kfree(lut);
1658 	return ret;
1659 }
1660 
1661 /**
1662  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1663  * @vsi: the VSI being configured,
1664  * @ctxt: VSI context structure
1665  * @enabled_tc: number of traffic classes to enable
1666  *
1667  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1668  **/
1669 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1670 					   struct i40e_vsi_context *ctxt,
1671 					   u8 enabled_tc)
1672 {
1673 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1674 	int i, override_q, pow, num_qps, ret;
1675 	u8 netdev_tc = 0, offset = 0;
1676 
1677 	if (vsi->type != I40E_VSI_MAIN)
1678 		return -EINVAL;
1679 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1680 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1681 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1682 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1683 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1684 
1685 	/* find the next higher power-of-2 of num queue pairs */
1686 	pow = ilog2(num_qps);
1687 	if (!is_power_of_2(num_qps))
1688 		pow++;
1689 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1690 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1691 
1692 	/* Setup queue offset/count for all TCs for given VSI */
1693 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1694 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1695 		/* See if the given TC is enabled for the given VSI */
1696 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1697 			offset = vsi->mqprio_qopt.qopt.offset[i];
1698 			qcount = vsi->mqprio_qopt.qopt.count[i];
1699 			if (qcount > max_qcount)
1700 				max_qcount = qcount;
1701 			vsi->tc_config.tc_info[i].qoffset = offset;
1702 			vsi->tc_config.tc_info[i].qcount = qcount;
1703 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1704 		} else {
1705 			/* TC is not enabled so set the offset to
1706 			 * default queue and allocate one queue
1707 			 * for the given TC.
1708 			 */
1709 			vsi->tc_config.tc_info[i].qoffset = 0;
1710 			vsi->tc_config.tc_info[i].qcount = 1;
1711 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1712 		}
1713 	}
1714 
1715 	/* Set actual Tx/Rx queue pairs */
1716 	vsi->num_queue_pairs = offset + qcount;
1717 
1718 	/* Setup queue TC[0].qmap for given VSI context */
1719 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1720 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1721 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1722 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1723 
1724 	/* Reconfigure RSS for main VSI with max queue count */
1725 	vsi->rss_size = max_qcount;
1726 	ret = i40e_vsi_config_rss(vsi);
1727 	if (ret) {
1728 		dev_info(&vsi->back->pdev->dev,
1729 			 "Failed to reconfig rss for num_queues (%u)\n",
1730 			 max_qcount);
1731 		return ret;
1732 	}
1733 	vsi->reconfig_rss = true;
1734 	dev_dbg(&vsi->back->pdev->dev,
1735 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1736 
1737 	/* Find queue count available for channel VSIs and starting offset
1738 	 * for channel VSIs
1739 	 */
1740 	override_q = vsi->mqprio_qopt.qopt.count[0];
1741 	if (override_q && override_q < vsi->num_queue_pairs) {
1742 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1743 		vsi->next_base_queue = override_q;
1744 	}
1745 	return 0;
1746 }
1747 
1748 /**
1749  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1750  * @vsi: the VSI being setup
1751  * @ctxt: VSI context structure
1752  * @enabled_tc: Enabled TCs bitmap
1753  * @is_add: True if called before Add VSI
1754  *
1755  * Setup VSI queue mapping for enabled traffic classes.
1756  **/
1757 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1758 				     struct i40e_vsi_context *ctxt,
1759 				     u8 enabled_tc,
1760 				     bool is_add)
1761 {
1762 	struct i40e_pf *pf = vsi->back;
1763 	u16 sections = 0;
1764 	u8 netdev_tc = 0;
1765 	u16 numtc = 1;
1766 	u16 qcount;
1767 	u8 offset;
1768 	u16 qmap;
1769 	int i;
1770 	u16 num_tc_qps = 0;
1771 
1772 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1773 	offset = 0;
1774 
1775 	/* Number of queues per enabled TC */
1776 	num_tc_qps = vsi->alloc_queue_pairs;
1777 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1778 		/* Find numtc from enabled TC bitmap */
1779 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1780 			if (enabled_tc & BIT(i)) /* TC is enabled */
1781 				numtc++;
1782 		}
1783 		if (!numtc) {
1784 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1785 			numtc = 1;
1786 		}
1787 		num_tc_qps = num_tc_qps / numtc;
1788 		num_tc_qps = min_t(int, num_tc_qps,
1789 				   i40e_pf_get_max_q_per_tc(pf));
1790 	}
1791 
1792 	vsi->tc_config.numtc = numtc;
1793 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1794 
1795 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1796 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1797 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1798 
1799 	/* Setup queue offset/count for all TCs for given VSI */
1800 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1801 		/* See if the given TC is enabled for the given VSI */
1802 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1803 			/* TC is enabled */
1804 			int pow, num_qps;
1805 
1806 			switch (vsi->type) {
1807 			case I40E_VSI_MAIN:
1808 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1809 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1810 				    vsi->tc_config.enabled_tc != 1) {
1811 					qcount = min_t(int, pf->alloc_rss_size,
1812 						       num_tc_qps);
1813 					break;
1814 				}
1815 				/* fall through */
1816 			case I40E_VSI_FDIR:
1817 			case I40E_VSI_SRIOV:
1818 			case I40E_VSI_VMDQ2:
1819 			default:
1820 				qcount = num_tc_qps;
1821 				WARN_ON(i != 0);
1822 				break;
1823 			}
1824 			vsi->tc_config.tc_info[i].qoffset = offset;
1825 			vsi->tc_config.tc_info[i].qcount = qcount;
1826 
1827 			/* find the next higher power-of-2 of num queue pairs */
1828 			num_qps = qcount;
1829 			pow = 0;
1830 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1831 				pow++;
1832 				num_qps >>= 1;
1833 			}
1834 
1835 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1836 			qmap =
1837 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1838 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1839 
1840 			offset += qcount;
1841 		} else {
1842 			/* TC is not enabled so set the offset to
1843 			 * default queue and allocate one queue
1844 			 * for the given TC.
1845 			 */
1846 			vsi->tc_config.tc_info[i].qoffset = 0;
1847 			vsi->tc_config.tc_info[i].qcount = 1;
1848 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1849 
1850 			qmap = 0;
1851 		}
1852 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1853 	}
1854 
1855 	/* Set actual Tx/Rx queue pairs */
1856 	vsi->num_queue_pairs = offset;
1857 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1858 		if (vsi->req_queue_pairs > 0)
1859 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1860 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1861 			vsi->num_queue_pairs = pf->num_lan_msix;
1862 	}
1863 
1864 	/* Scheduler section valid can only be set for ADD VSI */
1865 	if (is_add) {
1866 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1867 
1868 		ctxt->info.up_enable_bits = enabled_tc;
1869 	}
1870 	if (vsi->type == I40E_VSI_SRIOV) {
1871 		ctxt->info.mapping_flags |=
1872 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1873 		for (i = 0; i < vsi->num_queue_pairs; i++)
1874 			ctxt->info.queue_mapping[i] =
1875 					       cpu_to_le16(vsi->base_queue + i);
1876 	} else {
1877 		ctxt->info.mapping_flags |=
1878 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1879 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1880 	}
1881 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1882 }
1883 
1884 /**
1885  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1886  * @netdev: the netdevice
1887  * @addr: address to add
1888  *
1889  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1890  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1891  */
1892 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1893 {
1894 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1895 	struct i40e_vsi *vsi = np->vsi;
1896 
1897 	if (i40e_add_mac_filter(vsi, addr))
1898 		return 0;
1899 	else
1900 		return -ENOMEM;
1901 }
1902 
1903 /**
1904  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1905  * @netdev: the netdevice
1906  * @addr: address to add
1907  *
1908  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1909  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1910  */
1911 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1912 {
1913 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1914 	struct i40e_vsi *vsi = np->vsi;
1915 
1916 	/* Under some circumstances, we might receive a request to delete
1917 	 * our own device address from our uc list. Because we store the
1918 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1919 	 * such requests and not delete our device address from this list.
1920 	 */
1921 	if (ether_addr_equal(addr, netdev->dev_addr))
1922 		return 0;
1923 
1924 	i40e_del_mac_filter(vsi, addr);
1925 
1926 	return 0;
1927 }
1928 
1929 /**
1930  * i40e_set_rx_mode - NDO callback to set the netdev filters
1931  * @netdev: network interface device structure
1932  **/
1933 static void i40e_set_rx_mode(struct net_device *netdev)
1934 {
1935 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1936 	struct i40e_vsi *vsi = np->vsi;
1937 
1938 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1939 
1940 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1941 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1942 
1943 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1944 
1945 	/* check for other flag changes */
1946 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1947 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1948 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1949 	}
1950 }
1951 
1952 /**
1953  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1954  * @vsi: Pointer to VSI struct
1955  * @from: Pointer to list which contains MAC filter entries - changes to
1956  *        those entries needs to be undone.
1957  *
1958  * MAC filter entries from this list were slated for deletion.
1959  **/
1960 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1961 					 struct hlist_head *from)
1962 {
1963 	struct i40e_mac_filter *f;
1964 	struct hlist_node *h;
1965 
1966 	hlist_for_each_entry_safe(f, h, from, hlist) {
1967 		u64 key = i40e_addr_to_hkey(f->macaddr);
1968 
1969 		/* Move the element back into MAC filter list*/
1970 		hlist_del(&f->hlist);
1971 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1972 	}
1973 }
1974 
1975 /**
1976  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1977  * @vsi: Pointer to vsi struct
1978  * @from: Pointer to list which contains MAC filter entries - changes to
1979  *        those entries needs to be undone.
1980  *
1981  * MAC filter entries from this list were slated for addition.
1982  **/
1983 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1984 					 struct hlist_head *from)
1985 {
1986 	struct i40e_new_mac_filter *new;
1987 	struct hlist_node *h;
1988 
1989 	hlist_for_each_entry_safe(new, h, from, hlist) {
1990 		/* We can simply free the wrapper structure */
1991 		hlist_del(&new->hlist);
1992 		kfree(new);
1993 	}
1994 }
1995 
1996 /**
1997  * i40e_next_entry - Get the next non-broadcast filter from a list
1998  * @next: pointer to filter in list
1999  *
2000  * Returns the next non-broadcast filter in the list. Required so that we
2001  * ignore broadcast filters within the list, since these are not handled via
2002  * the normal firmware update path.
2003  */
2004 static
2005 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2006 {
2007 	hlist_for_each_entry_continue(next, hlist) {
2008 		if (!is_broadcast_ether_addr(next->f->macaddr))
2009 			return next;
2010 	}
2011 
2012 	return NULL;
2013 }
2014 
2015 /**
2016  * i40e_update_filter_state - Update filter state based on return data
2017  * from firmware
2018  * @count: Number of filters added
2019  * @add_list: return data from fw
2020  * @add_head: pointer to first filter in current batch
2021  *
2022  * MAC filter entries from list were slated to be added to device. Returns
2023  * number of successful filters. Note that 0 does NOT mean success!
2024  **/
2025 static int
2026 i40e_update_filter_state(int count,
2027 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2028 			 struct i40e_new_mac_filter *add_head)
2029 {
2030 	int retval = 0;
2031 	int i;
2032 
2033 	for (i = 0; i < count; i++) {
2034 		/* Always check status of each filter. We don't need to check
2035 		 * the firmware return status because we pre-set the filter
2036 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2037 		 * request to the adminq. Thus, if it no longer matches then
2038 		 * we know the filter is active.
2039 		 */
2040 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2041 			add_head->state = I40E_FILTER_FAILED;
2042 		} else {
2043 			add_head->state = I40E_FILTER_ACTIVE;
2044 			retval++;
2045 		}
2046 
2047 		add_head = i40e_next_filter(add_head);
2048 		if (!add_head)
2049 			break;
2050 	}
2051 
2052 	return retval;
2053 }
2054 
2055 /**
2056  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2057  * @vsi: ptr to the VSI
2058  * @vsi_name: name to display in messages
2059  * @list: the list of filters to send to firmware
2060  * @num_del: the number of filters to delete
2061  * @retval: Set to -EIO on failure to delete
2062  *
2063  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2064  * *retval instead of a return value so that success does not force ret_val to
2065  * be set to 0. This ensures that a sequence of calls to this function
2066  * preserve the previous value of *retval on successful delete.
2067  */
2068 static
2069 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2070 			  struct i40e_aqc_remove_macvlan_element_data *list,
2071 			  int num_del, int *retval)
2072 {
2073 	struct i40e_hw *hw = &vsi->back->hw;
2074 	i40e_status aq_ret;
2075 	int aq_err;
2076 
2077 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2078 	aq_err = hw->aq.asq_last_status;
2079 
2080 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2081 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2082 		*retval = -EIO;
2083 		dev_info(&vsi->back->pdev->dev,
2084 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2085 			 vsi_name, i40e_stat_str(hw, aq_ret),
2086 			 i40e_aq_str(hw, aq_err));
2087 	}
2088 }
2089 
2090 /**
2091  * i40e_aqc_add_filters - Request firmware to add a set of filters
2092  * @vsi: ptr to the VSI
2093  * @vsi_name: name to display in messages
2094  * @list: the list of filters to send to firmware
2095  * @add_head: Position in the add hlist
2096  * @num_add: the number of filters to add
2097  *
2098  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2099  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2100  * space for more filters.
2101  */
2102 static
2103 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2104 			  struct i40e_aqc_add_macvlan_element_data *list,
2105 			  struct i40e_new_mac_filter *add_head,
2106 			  int num_add)
2107 {
2108 	struct i40e_hw *hw = &vsi->back->hw;
2109 	int aq_err, fcnt;
2110 
2111 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2112 	aq_err = hw->aq.asq_last_status;
2113 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2114 
2115 	if (fcnt != num_add) {
2116 		if (vsi->type == I40E_VSI_MAIN) {
2117 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2118 			dev_warn(&vsi->back->pdev->dev,
2119 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2120 				 i40e_aq_str(hw, aq_err), vsi_name);
2121 		} else if (vsi->type == I40E_VSI_SRIOV ||
2122 			   vsi->type == I40E_VSI_VMDQ1 ||
2123 			   vsi->type == I40E_VSI_VMDQ2) {
2124 			dev_warn(&vsi->back->pdev->dev,
2125 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2126 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2127 		} else {
2128 			dev_warn(&vsi->back->pdev->dev,
2129 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2130 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2131 		}
2132 	}
2133 }
2134 
2135 /**
2136  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2137  * @vsi: pointer to the VSI
2138  * @vsi_name: the VSI name
2139  * @f: filter data
2140  *
2141  * This function sets or clears the promiscuous broadcast flags for VLAN
2142  * filters in order to properly receive broadcast frames. Assumes that only
2143  * broadcast filters are passed.
2144  *
2145  * Returns status indicating success or failure;
2146  **/
2147 static i40e_status
2148 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2149 			  struct i40e_mac_filter *f)
2150 {
2151 	bool enable = f->state == I40E_FILTER_NEW;
2152 	struct i40e_hw *hw = &vsi->back->hw;
2153 	i40e_status aq_ret;
2154 
2155 	if (f->vlan == I40E_VLAN_ANY) {
2156 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2157 						   vsi->seid,
2158 						   enable,
2159 						   NULL);
2160 	} else {
2161 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2162 							    vsi->seid,
2163 							    enable,
2164 							    f->vlan,
2165 							    NULL);
2166 	}
2167 
2168 	if (aq_ret) {
2169 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2170 		dev_warn(&vsi->back->pdev->dev,
2171 			 "Error %s, forcing overflow promiscuous on %s\n",
2172 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2173 			 vsi_name);
2174 	}
2175 
2176 	return aq_ret;
2177 }
2178 
2179 /**
2180  * i40e_set_promiscuous - set promiscuous mode
2181  * @pf: board private structure
2182  * @promisc: promisc on or off
2183  *
2184  * There are different ways of setting promiscuous mode on a PF depending on
2185  * what state/environment we're in.  This identifies and sets it appropriately.
2186  * Returns 0 on success.
2187  **/
2188 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2189 {
2190 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2191 	struct i40e_hw *hw = &pf->hw;
2192 	i40e_status aq_ret;
2193 
2194 	if (vsi->type == I40E_VSI_MAIN &&
2195 	    pf->lan_veb != I40E_NO_VEB &&
2196 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2197 		/* set defport ON for Main VSI instead of true promisc
2198 		 * this way we will get all unicast/multicast and VLAN
2199 		 * promisc behavior but will not get VF or VMDq traffic
2200 		 * replicated on the Main VSI.
2201 		 */
2202 		if (promisc)
2203 			aq_ret = i40e_aq_set_default_vsi(hw,
2204 							 vsi->seid,
2205 							 NULL);
2206 		else
2207 			aq_ret = i40e_aq_clear_default_vsi(hw,
2208 							   vsi->seid,
2209 							   NULL);
2210 		if (aq_ret) {
2211 			dev_info(&pf->pdev->dev,
2212 				 "Set default VSI failed, err %s, aq_err %s\n",
2213 				 i40e_stat_str(hw, aq_ret),
2214 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2215 		}
2216 	} else {
2217 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2218 						  hw,
2219 						  vsi->seid,
2220 						  promisc, NULL,
2221 						  true);
2222 		if (aq_ret) {
2223 			dev_info(&pf->pdev->dev,
2224 				 "set unicast promisc failed, err %s, aq_err %s\n",
2225 				 i40e_stat_str(hw, aq_ret),
2226 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2227 		}
2228 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2229 						  hw,
2230 						  vsi->seid,
2231 						  promisc, NULL);
2232 		if (aq_ret) {
2233 			dev_info(&pf->pdev->dev,
2234 				 "set multicast promisc failed, err %s, aq_err %s\n",
2235 				 i40e_stat_str(hw, aq_ret),
2236 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2237 		}
2238 	}
2239 
2240 	if (!aq_ret)
2241 		pf->cur_promisc = promisc;
2242 
2243 	return aq_ret;
2244 }
2245 
2246 /**
2247  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2248  * @vsi: ptr to the VSI
2249  *
2250  * Push any outstanding VSI filter changes through the AdminQ.
2251  *
2252  * Returns 0 or error value
2253  **/
2254 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2255 {
2256 	struct hlist_head tmp_add_list, tmp_del_list;
2257 	struct i40e_mac_filter *f;
2258 	struct i40e_new_mac_filter *new, *add_head = NULL;
2259 	struct i40e_hw *hw = &vsi->back->hw;
2260 	bool old_overflow, new_overflow;
2261 	unsigned int failed_filters = 0;
2262 	unsigned int vlan_filters = 0;
2263 	char vsi_name[16] = "PF";
2264 	int filter_list_len = 0;
2265 	i40e_status aq_ret = 0;
2266 	u32 changed_flags = 0;
2267 	struct hlist_node *h;
2268 	struct i40e_pf *pf;
2269 	int num_add = 0;
2270 	int num_del = 0;
2271 	int retval = 0;
2272 	u16 cmd_flags;
2273 	int list_size;
2274 	int bkt;
2275 
2276 	/* empty array typed pointers, kcalloc later */
2277 	struct i40e_aqc_add_macvlan_element_data *add_list;
2278 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2279 
2280 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2281 		usleep_range(1000, 2000);
2282 	pf = vsi->back;
2283 
2284 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2285 
2286 	if (vsi->netdev) {
2287 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2288 		vsi->current_netdev_flags = vsi->netdev->flags;
2289 	}
2290 
2291 	INIT_HLIST_HEAD(&tmp_add_list);
2292 	INIT_HLIST_HEAD(&tmp_del_list);
2293 
2294 	if (vsi->type == I40E_VSI_SRIOV)
2295 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2296 	else if (vsi->type != I40E_VSI_MAIN)
2297 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2298 
2299 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2300 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2301 
2302 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2303 		/* Create a list of filters to delete. */
2304 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2305 			if (f->state == I40E_FILTER_REMOVE) {
2306 				/* Move the element into temporary del_list */
2307 				hash_del(&f->hlist);
2308 				hlist_add_head(&f->hlist, &tmp_del_list);
2309 
2310 				/* Avoid counting removed filters */
2311 				continue;
2312 			}
2313 			if (f->state == I40E_FILTER_NEW) {
2314 				/* Create a temporary i40e_new_mac_filter */
2315 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2316 				if (!new)
2317 					goto err_no_memory_locked;
2318 
2319 				/* Store pointer to the real filter */
2320 				new->f = f;
2321 				new->state = f->state;
2322 
2323 				/* Add it to the hash list */
2324 				hlist_add_head(&new->hlist, &tmp_add_list);
2325 			}
2326 
2327 			/* Count the number of active (current and new) VLAN
2328 			 * filters we have now. Does not count filters which
2329 			 * are marked for deletion.
2330 			 */
2331 			if (f->vlan > 0)
2332 				vlan_filters++;
2333 		}
2334 
2335 		retval = i40e_correct_mac_vlan_filters(vsi,
2336 						       &tmp_add_list,
2337 						       &tmp_del_list,
2338 						       vlan_filters);
2339 		if (retval)
2340 			goto err_no_memory_locked;
2341 
2342 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2343 	}
2344 
2345 	/* Now process 'del_list' outside the lock */
2346 	if (!hlist_empty(&tmp_del_list)) {
2347 		filter_list_len = hw->aq.asq_buf_size /
2348 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2349 		list_size = filter_list_len *
2350 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2351 		del_list = kzalloc(list_size, GFP_ATOMIC);
2352 		if (!del_list)
2353 			goto err_no_memory;
2354 
2355 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2356 			cmd_flags = 0;
2357 
2358 			/* handle broadcast filters by updating the broadcast
2359 			 * promiscuous flag and release filter list.
2360 			 */
2361 			if (is_broadcast_ether_addr(f->macaddr)) {
2362 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2363 
2364 				hlist_del(&f->hlist);
2365 				kfree(f);
2366 				continue;
2367 			}
2368 
2369 			/* add to delete list */
2370 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2371 			if (f->vlan == I40E_VLAN_ANY) {
2372 				del_list[num_del].vlan_tag = 0;
2373 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2374 			} else {
2375 				del_list[num_del].vlan_tag =
2376 					cpu_to_le16((u16)(f->vlan));
2377 			}
2378 
2379 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2380 			del_list[num_del].flags = cmd_flags;
2381 			num_del++;
2382 
2383 			/* flush a full buffer */
2384 			if (num_del == filter_list_len) {
2385 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2386 						     num_del, &retval);
2387 				memset(del_list, 0, list_size);
2388 				num_del = 0;
2389 			}
2390 			/* Release memory for MAC filter entries which were
2391 			 * synced up with HW.
2392 			 */
2393 			hlist_del(&f->hlist);
2394 			kfree(f);
2395 		}
2396 
2397 		if (num_del) {
2398 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2399 					     num_del, &retval);
2400 		}
2401 
2402 		kfree(del_list);
2403 		del_list = NULL;
2404 	}
2405 
2406 	if (!hlist_empty(&tmp_add_list)) {
2407 		/* Do all the adds now. */
2408 		filter_list_len = hw->aq.asq_buf_size /
2409 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2410 		list_size = filter_list_len *
2411 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2412 		add_list = kzalloc(list_size, GFP_ATOMIC);
2413 		if (!add_list)
2414 			goto err_no_memory;
2415 
2416 		num_add = 0;
2417 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2418 			/* handle broadcast filters by updating the broadcast
2419 			 * promiscuous flag instead of adding a MAC filter.
2420 			 */
2421 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2422 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2423 							      new->f))
2424 					new->state = I40E_FILTER_FAILED;
2425 				else
2426 					new->state = I40E_FILTER_ACTIVE;
2427 				continue;
2428 			}
2429 
2430 			/* add to add array */
2431 			if (num_add == 0)
2432 				add_head = new;
2433 			cmd_flags = 0;
2434 			ether_addr_copy(add_list[num_add].mac_addr,
2435 					new->f->macaddr);
2436 			if (new->f->vlan == I40E_VLAN_ANY) {
2437 				add_list[num_add].vlan_tag = 0;
2438 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2439 			} else {
2440 				add_list[num_add].vlan_tag =
2441 					cpu_to_le16((u16)(new->f->vlan));
2442 			}
2443 			add_list[num_add].queue_number = 0;
2444 			/* set invalid match method for later detection */
2445 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2446 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2447 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2448 			num_add++;
2449 
2450 			/* flush a full buffer */
2451 			if (num_add == filter_list_len) {
2452 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2453 						     add_head, num_add);
2454 				memset(add_list, 0, list_size);
2455 				num_add = 0;
2456 			}
2457 		}
2458 		if (num_add) {
2459 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2460 					     num_add);
2461 		}
2462 		/* Now move all of the filters from the temp add list back to
2463 		 * the VSI's list.
2464 		 */
2465 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2466 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2467 			/* Only update the state if we're still NEW */
2468 			if (new->f->state == I40E_FILTER_NEW)
2469 				new->f->state = new->state;
2470 			hlist_del(&new->hlist);
2471 			kfree(new);
2472 		}
2473 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2474 		kfree(add_list);
2475 		add_list = NULL;
2476 	}
2477 
2478 	/* Determine the number of active and failed filters. */
2479 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2480 	vsi->active_filters = 0;
2481 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2482 		if (f->state == I40E_FILTER_ACTIVE)
2483 			vsi->active_filters++;
2484 		else if (f->state == I40E_FILTER_FAILED)
2485 			failed_filters++;
2486 	}
2487 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2488 
2489 	/* Check if we are able to exit overflow promiscuous mode. We can
2490 	 * safely exit if we didn't just enter, we no longer have any failed
2491 	 * filters, and we have reduced filters below the threshold value.
2492 	 */
2493 	if (old_overflow && !failed_filters &&
2494 	    vsi->active_filters < vsi->promisc_threshold) {
2495 		dev_info(&pf->pdev->dev,
2496 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2497 			 vsi_name);
2498 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2499 		vsi->promisc_threshold = 0;
2500 	}
2501 
2502 	/* if the VF is not trusted do not do promisc */
2503 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2504 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2505 		goto out;
2506 	}
2507 
2508 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2509 
2510 	/* If we are entering overflow promiscuous, we need to calculate a new
2511 	 * threshold for when we are safe to exit
2512 	 */
2513 	if (!old_overflow && new_overflow)
2514 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2515 
2516 	/* check for changes in promiscuous modes */
2517 	if (changed_flags & IFF_ALLMULTI) {
2518 		bool cur_multipromisc;
2519 
2520 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2521 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2522 							       vsi->seid,
2523 							       cur_multipromisc,
2524 							       NULL);
2525 		if (aq_ret) {
2526 			retval = i40e_aq_rc_to_posix(aq_ret,
2527 						     hw->aq.asq_last_status);
2528 			dev_info(&pf->pdev->dev,
2529 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2530 				 vsi_name,
2531 				 i40e_stat_str(hw, aq_ret),
2532 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2533 		}
2534 	}
2535 
2536 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2537 		bool cur_promisc;
2538 
2539 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2540 			       new_overflow);
2541 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2542 		if (aq_ret) {
2543 			retval = i40e_aq_rc_to_posix(aq_ret,
2544 						     hw->aq.asq_last_status);
2545 			dev_info(&pf->pdev->dev,
2546 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2547 				 cur_promisc ? "on" : "off",
2548 				 vsi_name,
2549 				 i40e_stat_str(hw, aq_ret),
2550 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2551 		}
2552 	}
2553 out:
2554 	/* if something went wrong then set the changed flag so we try again */
2555 	if (retval)
2556 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2557 
2558 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2559 	return retval;
2560 
2561 err_no_memory:
2562 	/* Restore elements on the temporary add and delete lists */
2563 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2564 err_no_memory_locked:
2565 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2566 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2567 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2568 
2569 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2570 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2571 	return -ENOMEM;
2572 }
2573 
2574 /**
2575  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2576  * @pf: board private structure
2577  **/
2578 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2579 {
2580 	int v;
2581 
2582 	if (!pf)
2583 		return;
2584 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2585 		return;
2586 
2587 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2588 		if (pf->vsi[v] &&
2589 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2590 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2591 
2592 			if (ret) {
2593 				/* come back and try again later */
2594 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2595 					pf->state);
2596 				break;
2597 			}
2598 		}
2599 	}
2600 }
2601 
2602 /**
2603  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2604  * @vsi: the vsi
2605  **/
2606 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2607 {
2608 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2609 		return I40E_RXBUFFER_2048;
2610 	else
2611 		return I40E_RXBUFFER_3072;
2612 }
2613 
2614 /**
2615  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2616  * @netdev: network interface device structure
2617  * @new_mtu: new value for maximum frame size
2618  *
2619  * Returns 0 on success, negative on failure
2620  **/
2621 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2622 {
2623 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2624 	struct i40e_vsi *vsi = np->vsi;
2625 	struct i40e_pf *pf = vsi->back;
2626 
2627 	if (i40e_enabled_xdp_vsi(vsi)) {
2628 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2629 
2630 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2631 			return -EINVAL;
2632 	}
2633 
2634 	netdev_info(netdev, "changing MTU from %d to %d\n",
2635 		    netdev->mtu, new_mtu);
2636 	netdev->mtu = new_mtu;
2637 	if (netif_running(netdev))
2638 		i40e_vsi_reinit_locked(vsi);
2639 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2640 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2641 	return 0;
2642 }
2643 
2644 /**
2645  * i40e_ioctl - Access the hwtstamp interface
2646  * @netdev: network interface device structure
2647  * @ifr: interface request data
2648  * @cmd: ioctl command
2649  **/
2650 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2651 {
2652 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2653 	struct i40e_pf *pf = np->vsi->back;
2654 
2655 	switch (cmd) {
2656 	case SIOCGHWTSTAMP:
2657 		return i40e_ptp_get_ts_config(pf, ifr);
2658 	case SIOCSHWTSTAMP:
2659 		return i40e_ptp_set_ts_config(pf, ifr);
2660 	default:
2661 		return -EOPNOTSUPP;
2662 	}
2663 }
2664 
2665 /**
2666  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2667  * @vsi: the vsi being adjusted
2668  **/
2669 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2670 {
2671 	struct i40e_vsi_context ctxt;
2672 	i40e_status ret;
2673 
2674 	/* Don't modify stripping options if a port VLAN is active */
2675 	if (vsi->info.pvid)
2676 		return;
2677 
2678 	if ((vsi->info.valid_sections &
2679 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2680 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2681 		return;  /* already enabled */
2682 
2683 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2684 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2685 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2686 
2687 	ctxt.seid = vsi->seid;
2688 	ctxt.info = vsi->info;
2689 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2690 	if (ret) {
2691 		dev_info(&vsi->back->pdev->dev,
2692 			 "update vlan stripping failed, err %s aq_err %s\n",
2693 			 i40e_stat_str(&vsi->back->hw, ret),
2694 			 i40e_aq_str(&vsi->back->hw,
2695 				     vsi->back->hw.aq.asq_last_status));
2696 	}
2697 }
2698 
2699 /**
2700  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2701  * @vsi: the vsi being adjusted
2702  **/
2703 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2704 {
2705 	struct i40e_vsi_context ctxt;
2706 	i40e_status ret;
2707 
2708 	/* Don't modify stripping options if a port VLAN is active */
2709 	if (vsi->info.pvid)
2710 		return;
2711 
2712 	if ((vsi->info.valid_sections &
2713 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2714 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2715 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2716 		return;  /* already disabled */
2717 
2718 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2719 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2720 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2721 
2722 	ctxt.seid = vsi->seid;
2723 	ctxt.info = vsi->info;
2724 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2725 	if (ret) {
2726 		dev_info(&vsi->back->pdev->dev,
2727 			 "update vlan stripping failed, err %s aq_err %s\n",
2728 			 i40e_stat_str(&vsi->back->hw, ret),
2729 			 i40e_aq_str(&vsi->back->hw,
2730 				     vsi->back->hw.aq.asq_last_status));
2731 	}
2732 }
2733 
2734 /**
2735  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2736  * @vsi: the vsi being configured
2737  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2738  *
2739  * This is a helper function for adding a new MAC/VLAN filter with the
2740  * specified VLAN for each existing MAC address already in the hash table.
2741  * This function does *not* perform any accounting to update filters based on
2742  * VLAN mode.
2743  *
2744  * NOTE: this function expects to be called while under the
2745  * mac_filter_hash_lock
2746  **/
2747 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2748 {
2749 	struct i40e_mac_filter *f, *add_f;
2750 	struct hlist_node *h;
2751 	int bkt;
2752 
2753 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2754 		if (f->state == I40E_FILTER_REMOVE)
2755 			continue;
2756 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2757 		if (!add_f) {
2758 			dev_info(&vsi->back->pdev->dev,
2759 				 "Could not add vlan filter %d for %pM\n",
2760 				 vid, f->macaddr);
2761 			return -ENOMEM;
2762 		}
2763 	}
2764 
2765 	return 0;
2766 }
2767 
2768 /**
2769  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2770  * @vsi: the VSI being configured
2771  * @vid: VLAN id to be added
2772  **/
2773 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2774 {
2775 	int err;
2776 
2777 	if (vsi->info.pvid)
2778 		return -EINVAL;
2779 
2780 	/* The network stack will attempt to add VID=0, with the intention to
2781 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2782 	 * these packets by default when configured to receive untagged
2783 	 * packets, so we don't need to add a filter for this case.
2784 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2785 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2786 	 * Thus, we do not want to actually add a filter for VID=0
2787 	 */
2788 	if (!vid)
2789 		return 0;
2790 
2791 	/* Locked once because all functions invoked below iterates list*/
2792 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2793 	err = i40e_add_vlan_all_mac(vsi, vid);
2794 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2795 	if (err)
2796 		return err;
2797 
2798 	/* schedule our worker thread which will take care of
2799 	 * applying the new filter changes
2800 	 */
2801 	i40e_service_event_schedule(vsi->back);
2802 	return 0;
2803 }
2804 
2805 /**
2806  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2807  * @vsi: the vsi being configured
2808  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2809  *
2810  * This function should be used to remove all VLAN filters which match the
2811  * given VID. It does not schedule the service event and does not take the
2812  * mac_filter_hash_lock so it may be combined with other operations under
2813  * a single invocation of the mac_filter_hash_lock.
2814  *
2815  * NOTE: this function expects to be called while under the
2816  * mac_filter_hash_lock
2817  */
2818 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2819 {
2820 	struct i40e_mac_filter *f;
2821 	struct hlist_node *h;
2822 	int bkt;
2823 
2824 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2825 		if (f->vlan == vid)
2826 			__i40e_del_filter(vsi, f);
2827 	}
2828 }
2829 
2830 /**
2831  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2832  * @vsi: the VSI being configured
2833  * @vid: VLAN id to be removed
2834  **/
2835 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2836 {
2837 	if (!vid || vsi->info.pvid)
2838 		return;
2839 
2840 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2841 	i40e_rm_vlan_all_mac(vsi, vid);
2842 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2843 
2844 	/* schedule our worker thread which will take care of
2845 	 * applying the new filter changes
2846 	 */
2847 	i40e_service_event_schedule(vsi->back);
2848 }
2849 
2850 /**
2851  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2852  * @netdev: network interface to be adjusted
2853  * @proto: unused protocol value
2854  * @vid: vlan id to be added
2855  *
2856  * net_device_ops implementation for adding vlan ids
2857  **/
2858 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2859 				__always_unused __be16 proto, u16 vid)
2860 {
2861 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2862 	struct i40e_vsi *vsi = np->vsi;
2863 	int ret = 0;
2864 
2865 	if (vid >= VLAN_N_VID)
2866 		return -EINVAL;
2867 
2868 	ret = i40e_vsi_add_vlan(vsi, vid);
2869 	if (!ret)
2870 		set_bit(vid, vsi->active_vlans);
2871 
2872 	return ret;
2873 }
2874 
2875 /**
2876  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2877  * @netdev: network interface to be adjusted
2878  * @proto: unused protocol value
2879  * @vid: vlan id to be added
2880  **/
2881 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2882 				    __always_unused __be16 proto, u16 vid)
2883 {
2884 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2885 	struct i40e_vsi *vsi = np->vsi;
2886 
2887 	if (vid >= VLAN_N_VID)
2888 		return;
2889 	set_bit(vid, vsi->active_vlans);
2890 }
2891 
2892 /**
2893  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2894  * @netdev: network interface to be adjusted
2895  * @proto: unused protocol value
2896  * @vid: vlan id to be removed
2897  *
2898  * net_device_ops implementation for removing vlan ids
2899  **/
2900 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2901 				 __always_unused __be16 proto, u16 vid)
2902 {
2903 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2904 	struct i40e_vsi *vsi = np->vsi;
2905 
2906 	/* return code is ignored as there is nothing a user
2907 	 * can do about failure to remove and a log message was
2908 	 * already printed from the other function
2909 	 */
2910 	i40e_vsi_kill_vlan(vsi, vid);
2911 
2912 	clear_bit(vid, vsi->active_vlans);
2913 
2914 	return 0;
2915 }
2916 
2917 /**
2918  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2919  * @vsi: the vsi being brought back up
2920  **/
2921 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2922 {
2923 	u16 vid;
2924 
2925 	if (!vsi->netdev)
2926 		return;
2927 
2928 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2929 		i40e_vlan_stripping_enable(vsi);
2930 	else
2931 		i40e_vlan_stripping_disable(vsi);
2932 
2933 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2934 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2935 					vid);
2936 }
2937 
2938 /**
2939  * i40e_vsi_add_pvid - Add pvid for the VSI
2940  * @vsi: the vsi being adjusted
2941  * @vid: the vlan id to set as a PVID
2942  **/
2943 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2944 {
2945 	struct i40e_vsi_context ctxt;
2946 	i40e_status ret;
2947 
2948 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2949 	vsi->info.pvid = cpu_to_le16(vid);
2950 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2951 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2952 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2953 
2954 	ctxt.seid = vsi->seid;
2955 	ctxt.info = vsi->info;
2956 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2957 	if (ret) {
2958 		dev_info(&vsi->back->pdev->dev,
2959 			 "add pvid failed, err %s aq_err %s\n",
2960 			 i40e_stat_str(&vsi->back->hw, ret),
2961 			 i40e_aq_str(&vsi->back->hw,
2962 				     vsi->back->hw.aq.asq_last_status));
2963 		return -ENOENT;
2964 	}
2965 
2966 	return 0;
2967 }
2968 
2969 /**
2970  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2971  * @vsi: the vsi being adjusted
2972  *
2973  * Just use the vlan_rx_register() service to put it back to normal
2974  **/
2975 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2976 {
2977 	vsi->info.pvid = 0;
2978 
2979 	i40e_vlan_stripping_disable(vsi);
2980 }
2981 
2982 /**
2983  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2984  * @vsi: ptr to the VSI
2985  *
2986  * If this function returns with an error, then it's possible one or
2987  * more of the rings is populated (while the rest are not).  It is the
2988  * callers duty to clean those orphaned rings.
2989  *
2990  * Return 0 on success, negative on failure
2991  **/
2992 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2993 {
2994 	int i, err = 0;
2995 
2996 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2997 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2998 
2999 	if (!i40e_enabled_xdp_vsi(vsi))
3000 		return err;
3001 
3002 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3003 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3004 
3005 	return err;
3006 }
3007 
3008 /**
3009  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3010  * @vsi: ptr to the VSI
3011  *
3012  * Free VSI's transmit software resources
3013  **/
3014 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3015 {
3016 	int i;
3017 
3018 	if (vsi->tx_rings) {
3019 		for (i = 0; i < vsi->num_queue_pairs; i++)
3020 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3021 				i40e_free_tx_resources(vsi->tx_rings[i]);
3022 	}
3023 
3024 	if (vsi->xdp_rings) {
3025 		for (i = 0; i < vsi->num_queue_pairs; i++)
3026 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3027 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3028 	}
3029 }
3030 
3031 /**
3032  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3033  * @vsi: ptr to the VSI
3034  *
3035  * If this function returns with an error, then it's possible one or
3036  * more of the rings is populated (while the rest are not).  It is the
3037  * callers duty to clean those orphaned rings.
3038  *
3039  * Return 0 on success, negative on failure
3040  **/
3041 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3042 {
3043 	int i, err = 0;
3044 
3045 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3046 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3047 	return err;
3048 }
3049 
3050 /**
3051  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3052  * @vsi: ptr to the VSI
3053  *
3054  * Free all receive software resources
3055  **/
3056 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3057 {
3058 	int i;
3059 
3060 	if (!vsi->rx_rings)
3061 		return;
3062 
3063 	for (i = 0; i < vsi->num_queue_pairs; i++)
3064 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3065 			i40e_free_rx_resources(vsi->rx_rings[i]);
3066 }
3067 
3068 /**
3069  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3070  * @ring: The Tx ring to configure
3071  *
3072  * This enables/disables XPS for a given Tx descriptor ring
3073  * based on the TCs enabled for the VSI that ring belongs to.
3074  **/
3075 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3076 {
3077 	int cpu;
3078 
3079 	if (!ring->q_vector || !ring->netdev || ring->ch)
3080 		return;
3081 
3082 	/* We only initialize XPS once, so as not to overwrite user settings */
3083 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3084 		return;
3085 
3086 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3087 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3088 			    ring->queue_index);
3089 }
3090 
3091 /**
3092  * i40e_xsk_umem - Retrieve the AF_XDP ZC if XDP and ZC is enabled
3093  * @ring: The Tx or Rx ring
3094  *
3095  * Returns the UMEM or NULL.
3096  **/
3097 static struct xdp_umem *i40e_xsk_umem(struct i40e_ring *ring)
3098 {
3099 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3100 	int qid = ring->queue_index;
3101 
3102 	if (ring_is_xdp(ring))
3103 		qid -= ring->vsi->alloc_queue_pairs;
3104 
3105 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3106 		return NULL;
3107 
3108 	return xdp_get_umem_from_qid(ring->vsi->netdev, qid);
3109 }
3110 
3111 /**
3112  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3113  * @ring: The Tx ring to configure
3114  *
3115  * Configure the Tx descriptor ring in the HMC context.
3116  **/
3117 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3118 {
3119 	struct i40e_vsi *vsi = ring->vsi;
3120 	u16 pf_q = vsi->base_queue + ring->queue_index;
3121 	struct i40e_hw *hw = &vsi->back->hw;
3122 	struct i40e_hmc_obj_txq tx_ctx;
3123 	i40e_status err = 0;
3124 	u32 qtx_ctl = 0;
3125 
3126 	if (ring_is_xdp(ring))
3127 		ring->xsk_umem = i40e_xsk_umem(ring);
3128 
3129 	/* some ATR related tx ring init */
3130 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3131 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3132 		ring->atr_count = 0;
3133 	} else {
3134 		ring->atr_sample_rate = 0;
3135 	}
3136 
3137 	/* configure XPS */
3138 	i40e_config_xps_tx_ring(ring);
3139 
3140 	/* clear the context structure first */
3141 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3142 
3143 	tx_ctx.new_context = 1;
3144 	tx_ctx.base = (ring->dma / 128);
3145 	tx_ctx.qlen = ring->count;
3146 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3147 					       I40E_FLAG_FD_ATR_ENABLED));
3148 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3149 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3150 	if (vsi->type != I40E_VSI_FDIR)
3151 		tx_ctx.head_wb_ena = 1;
3152 	tx_ctx.head_wb_addr = ring->dma +
3153 			      (ring->count * sizeof(struct i40e_tx_desc));
3154 
3155 	/* As part of VSI creation/update, FW allocates certain
3156 	 * Tx arbitration queue sets for each TC enabled for
3157 	 * the VSI. The FW returns the handles to these queue
3158 	 * sets as part of the response buffer to Add VSI,
3159 	 * Update VSI, etc. AQ commands. It is expected that
3160 	 * these queue set handles be associated with the Tx
3161 	 * queues by the driver as part of the TX queue context
3162 	 * initialization. This has to be done regardless of
3163 	 * DCB as by default everything is mapped to TC0.
3164 	 */
3165 
3166 	if (ring->ch)
3167 		tx_ctx.rdylist =
3168 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3169 
3170 	else
3171 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3172 
3173 	tx_ctx.rdylist_act = 0;
3174 
3175 	/* clear the context in the HMC */
3176 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3177 	if (err) {
3178 		dev_info(&vsi->back->pdev->dev,
3179 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3180 			 ring->queue_index, pf_q, err);
3181 		return -ENOMEM;
3182 	}
3183 
3184 	/* set the context in the HMC */
3185 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3186 	if (err) {
3187 		dev_info(&vsi->back->pdev->dev,
3188 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3189 			 ring->queue_index, pf_q, err);
3190 		return -ENOMEM;
3191 	}
3192 
3193 	/* Now associate this queue with this PCI function */
3194 	if (ring->ch) {
3195 		if (ring->ch->type == I40E_VSI_VMDQ2)
3196 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3197 		else
3198 			return -EINVAL;
3199 
3200 		qtx_ctl |= (ring->ch->vsi_number <<
3201 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3202 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3203 	} else {
3204 		if (vsi->type == I40E_VSI_VMDQ2) {
3205 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3206 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3207 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3208 		} else {
3209 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3210 		}
3211 	}
3212 
3213 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3214 		    I40E_QTX_CTL_PF_INDX_MASK);
3215 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3216 	i40e_flush(hw);
3217 
3218 	/* cache tail off for easier writes later */
3219 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3220 
3221 	return 0;
3222 }
3223 
3224 /**
3225  * i40e_configure_rx_ring - Configure a receive ring context
3226  * @ring: The Rx ring to configure
3227  *
3228  * Configure the Rx descriptor ring in the HMC context.
3229  **/
3230 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3231 {
3232 	struct i40e_vsi *vsi = ring->vsi;
3233 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3234 	u16 pf_q = vsi->base_queue + ring->queue_index;
3235 	struct i40e_hw *hw = &vsi->back->hw;
3236 	struct i40e_hmc_obj_rxq rx_ctx;
3237 	i40e_status err = 0;
3238 	bool ok;
3239 	int ret;
3240 
3241 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3242 
3243 	/* clear the context structure first */
3244 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3245 
3246 	if (ring->vsi->type == I40E_VSI_MAIN)
3247 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3248 
3249 	ring->xsk_umem = i40e_xsk_umem(ring);
3250 	if (ring->xsk_umem) {
3251 		ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
3252 				   XDP_PACKET_HEADROOM;
3253 		/* For AF_XDP ZC, we disallow packets to span on
3254 		 * multiple buffers, thus letting us skip that
3255 		 * handling in the fast-path.
3256 		 */
3257 		chain_len = 1;
3258 		ring->zca.free = i40e_zca_free;
3259 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3260 						 MEM_TYPE_ZERO_COPY,
3261 						 &ring->zca);
3262 		if (ret)
3263 			return ret;
3264 		dev_info(&vsi->back->pdev->dev,
3265 			 "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
3266 			 ring->queue_index);
3267 
3268 	} else {
3269 		ring->rx_buf_len = vsi->rx_buf_len;
3270 		if (ring->vsi->type == I40E_VSI_MAIN) {
3271 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3272 							 MEM_TYPE_PAGE_SHARED,
3273 							 NULL);
3274 			if (ret)
3275 				return ret;
3276 		}
3277 	}
3278 
3279 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3280 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3281 
3282 	rx_ctx.base = (ring->dma / 128);
3283 	rx_ctx.qlen = ring->count;
3284 
3285 	/* use 32 byte descriptors */
3286 	rx_ctx.dsize = 1;
3287 
3288 	/* descriptor type is always zero
3289 	 * rx_ctx.dtype = 0;
3290 	 */
3291 	rx_ctx.hsplit_0 = 0;
3292 
3293 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3294 	if (hw->revision_id == 0)
3295 		rx_ctx.lrxqthresh = 0;
3296 	else
3297 		rx_ctx.lrxqthresh = 1;
3298 	rx_ctx.crcstrip = 1;
3299 	rx_ctx.l2tsel = 1;
3300 	/* this controls whether VLAN is stripped from inner headers */
3301 	rx_ctx.showiv = 0;
3302 	/* set the prefena field to 1 because the manual says to */
3303 	rx_ctx.prefena = 1;
3304 
3305 	/* clear the context in the HMC */
3306 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3307 	if (err) {
3308 		dev_info(&vsi->back->pdev->dev,
3309 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3310 			 ring->queue_index, pf_q, err);
3311 		return -ENOMEM;
3312 	}
3313 
3314 	/* set the context in the HMC */
3315 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3316 	if (err) {
3317 		dev_info(&vsi->back->pdev->dev,
3318 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3319 			 ring->queue_index, pf_q, err);
3320 		return -ENOMEM;
3321 	}
3322 
3323 	/* configure Rx buffer alignment */
3324 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3325 		clear_ring_build_skb_enabled(ring);
3326 	else
3327 		set_ring_build_skb_enabled(ring);
3328 
3329 	/* cache tail for quicker writes, and clear the reg before use */
3330 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3331 	writel(0, ring->tail);
3332 
3333 	ok = ring->xsk_umem ?
3334 	     i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
3335 	     !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3336 	if (!ok) {
3337 		/* Log this in case the user has forgotten to give the kernel
3338 		 * any buffers, even later in the application.
3339 		 */
3340 		dev_info(&vsi->back->pdev->dev,
3341 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3342 			 ring->xsk_umem ? "UMEM enabled " : "",
3343 			 ring->queue_index, pf_q);
3344 	}
3345 
3346 	return 0;
3347 }
3348 
3349 /**
3350  * i40e_vsi_configure_tx - Configure the VSI for Tx
3351  * @vsi: VSI structure describing this set of rings and resources
3352  *
3353  * Configure the Tx VSI for operation.
3354  **/
3355 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3356 {
3357 	int err = 0;
3358 	u16 i;
3359 
3360 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3361 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3362 
3363 	if (!i40e_enabled_xdp_vsi(vsi))
3364 		return err;
3365 
3366 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3367 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3368 
3369 	return err;
3370 }
3371 
3372 /**
3373  * i40e_vsi_configure_rx - Configure the VSI for Rx
3374  * @vsi: the VSI being configured
3375  *
3376  * Configure the Rx VSI for operation.
3377  **/
3378 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3379 {
3380 	int err = 0;
3381 	u16 i;
3382 
3383 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3384 		vsi->max_frame = I40E_MAX_RXBUFFER;
3385 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3386 #if (PAGE_SIZE < 8192)
3387 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3388 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3389 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3390 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3391 #endif
3392 	} else {
3393 		vsi->max_frame = I40E_MAX_RXBUFFER;
3394 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3395 						       I40E_RXBUFFER_2048;
3396 	}
3397 
3398 	/* set up individual rings */
3399 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3400 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3401 
3402 	return err;
3403 }
3404 
3405 /**
3406  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3407  * @vsi: ptr to the VSI
3408  **/
3409 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3410 {
3411 	struct i40e_ring *tx_ring, *rx_ring;
3412 	u16 qoffset, qcount;
3413 	int i, n;
3414 
3415 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3416 		/* Reset the TC information */
3417 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3418 			rx_ring = vsi->rx_rings[i];
3419 			tx_ring = vsi->tx_rings[i];
3420 			rx_ring->dcb_tc = 0;
3421 			tx_ring->dcb_tc = 0;
3422 		}
3423 		return;
3424 	}
3425 
3426 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3427 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3428 			continue;
3429 
3430 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3431 		qcount = vsi->tc_config.tc_info[n].qcount;
3432 		for (i = qoffset; i < (qoffset + qcount); i++) {
3433 			rx_ring = vsi->rx_rings[i];
3434 			tx_ring = vsi->tx_rings[i];
3435 			rx_ring->dcb_tc = n;
3436 			tx_ring->dcb_tc = n;
3437 		}
3438 	}
3439 }
3440 
3441 /**
3442  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3443  * @vsi: ptr to the VSI
3444  **/
3445 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3446 {
3447 	if (vsi->netdev)
3448 		i40e_set_rx_mode(vsi->netdev);
3449 }
3450 
3451 /**
3452  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3453  * @vsi: Pointer to the targeted VSI
3454  *
3455  * This function replays the hlist on the hw where all the SB Flow Director
3456  * filters were saved.
3457  **/
3458 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3459 {
3460 	struct i40e_fdir_filter *filter;
3461 	struct i40e_pf *pf = vsi->back;
3462 	struct hlist_node *node;
3463 
3464 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3465 		return;
3466 
3467 	/* Reset FDir counters as we're replaying all existing filters */
3468 	pf->fd_tcp4_filter_cnt = 0;
3469 	pf->fd_udp4_filter_cnt = 0;
3470 	pf->fd_sctp4_filter_cnt = 0;
3471 	pf->fd_ip4_filter_cnt = 0;
3472 
3473 	hlist_for_each_entry_safe(filter, node,
3474 				  &pf->fdir_filter_list, fdir_node) {
3475 		i40e_add_del_fdir(vsi, filter, true);
3476 	}
3477 }
3478 
3479 /**
3480  * i40e_vsi_configure - Set up the VSI for action
3481  * @vsi: the VSI being configured
3482  **/
3483 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3484 {
3485 	int err;
3486 
3487 	i40e_set_vsi_rx_mode(vsi);
3488 	i40e_restore_vlan(vsi);
3489 	i40e_vsi_config_dcb_rings(vsi);
3490 	err = i40e_vsi_configure_tx(vsi);
3491 	if (!err)
3492 		err = i40e_vsi_configure_rx(vsi);
3493 
3494 	return err;
3495 }
3496 
3497 /**
3498  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3499  * @vsi: the VSI being configured
3500  **/
3501 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3502 {
3503 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3504 	struct i40e_pf *pf = vsi->back;
3505 	struct i40e_hw *hw = &pf->hw;
3506 	u16 vector;
3507 	int i, q;
3508 	u32 qp;
3509 
3510 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3511 	 * and PFINT_LNKLSTn registers, e.g.:
3512 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3513 	 */
3514 	qp = vsi->base_queue;
3515 	vector = vsi->base_vector;
3516 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3517 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3518 
3519 		q_vector->rx.next_update = jiffies + 1;
3520 		q_vector->rx.target_itr =
3521 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3522 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3523 		     q_vector->rx.target_itr);
3524 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3525 
3526 		q_vector->tx.next_update = jiffies + 1;
3527 		q_vector->tx.target_itr =
3528 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3529 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3530 		     q_vector->tx.target_itr);
3531 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3532 
3533 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3534 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3535 
3536 		/* Linked list for the queuepairs assigned to this vector */
3537 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3538 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3539 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3540 			u32 val;
3541 
3542 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3543 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3544 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3545 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3546 			      (I40E_QUEUE_TYPE_TX <<
3547 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3548 
3549 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3550 
3551 			if (has_xdp) {
3552 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3553 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3554 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3555 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3556 				      (I40E_QUEUE_TYPE_TX <<
3557 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3558 
3559 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3560 			}
3561 
3562 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3563 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3564 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3565 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3566 			      (I40E_QUEUE_TYPE_RX <<
3567 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3568 
3569 			/* Terminate the linked list */
3570 			if (q == (q_vector->num_ringpairs - 1))
3571 				val |= (I40E_QUEUE_END_OF_LIST <<
3572 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3573 
3574 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3575 			qp++;
3576 		}
3577 	}
3578 
3579 	i40e_flush(hw);
3580 }
3581 
3582 /**
3583  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3584  * @pf: pointer to private device data structure
3585  **/
3586 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3587 {
3588 	struct i40e_hw *hw = &pf->hw;
3589 	u32 val;
3590 
3591 	/* clear things first */
3592 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3593 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3594 
3595 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3596 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3597 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3598 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3599 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3600 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3601 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3602 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3603 
3604 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3605 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3606 
3607 	if (pf->flags & I40E_FLAG_PTP)
3608 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3609 
3610 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3611 
3612 	/* SW_ITR_IDX = 0, but don't change INTENA */
3613 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3614 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3615 
3616 	/* OTHER_ITR_IDX = 0 */
3617 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3618 }
3619 
3620 /**
3621  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3622  * @vsi: the VSI being configured
3623  **/
3624 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3625 {
3626 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3627 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3628 	struct i40e_pf *pf = vsi->back;
3629 	struct i40e_hw *hw = &pf->hw;
3630 	u32 val;
3631 
3632 	/* set the ITR configuration */
3633 	q_vector->rx.next_update = jiffies + 1;
3634 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3635 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3636 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3637 	q_vector->tx.next_update = jiffies + 1;
3638 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3639 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3640 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3641 
3642 	i40e_enable_misc_int_causes(pf);
3643 
3644 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3645 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3646 
3647 	/* Associate the queue pair to the vector and enable the queue int */
3648 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3649 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3650 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3651 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3652 
3653 	wr32(hw, I40E_QINT_RQCTL(0), val);
3654 
3655 	if (i40e_enabled_xdp_vsi(vsi)) {
3656 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3657 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3658 		      (I40E_QUEUE_TYPE_TX
3659 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3660 
3661 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3662 	}
3663 
3664 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3665 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3666 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3667 
3668 	wr32(hw, I40E_QINT_TQCTL(0), val);
3669 	i40e_flush(hw);
3670 }
3671 
3672 /**
3673  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3674  * @pf: board private structure
3675  **/
3676 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3677 {
3678 	struct i40e_hw *hw = &pf->hw;
3679 
3680 	wr32(hw, I40E_PFINT_DYN_CTL0,
3681 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3682 	i40e_flush(hw);
3683 }
3684 
3685 /**
3686  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3687  * @pf: board private structure
3688  **/
3689 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3690 {
3691 	struct i40e_hw *hw = &pf->hw;
3692 	u32 val;
3693 
3694 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3695 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3696 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3697 
3698 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3699 	i40e_flush(hw);
3700 }
3701 
3702 /**
3703  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3704  * @irq: interrupt number
3705  * @data: pointer to a q_vector
3706  **/
3707 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3708 {
3709 	struct i40e_q_vector *q_vector = data;
3710 
3711 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3712 		return IRQ_HANDLED;
3713 
3714 	napi_schedule_irqoff(&q_vector->napi);
3715 
3716 	return IRQ_HANDLED;
3717 }
3718 
3719 /**
3720  * i40e_irq_affinity_notify - Callback for affinity changes
3721  * @notify: context as to what irq was changed
3722  * @mask: the new affinity mask
3723  *
3724  * This is a callback function used by the irq_set_affinity_notifier function
3725  * so that we may register to receive changes to the irq affinity masks.
3726  **/
3727 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3728 				     const cpumask_t *mask)
3729 {
3730 	struct i40e_q_vector *q_vector =
3731 		container_of(notify, struct i40e_q_vector, affinity_notify);
3732 
3733 	cpumask_copy(&q_vector->affinity_mask, mask);
3734 }
3735 
3736 /**
3737  * i40e_irq_affinity_release - Callback for affinity notifier release
3738  * @ref: internal core kernel usage
3739  *
3740  * This is a callback function used by the irq_set_affinity_notifier function
3741  * to inform the current notification subscriber that they will no longer
3742  * receive notifications.
3743  **/
3744 static void i40e_irq_affinity_release(struct kref *ref) {}
3745 
3746 /**
3747  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3748  * @vsi: the VSI being configured
3749  * @basename: name for the vector
3750  *
3751  * Allocates MSI-X vectors and requests interrupts from the kernel.
3752  **/
3753 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3754 {
3755 	int q_vectors = vsi->num_q_vectors;
3756 	struct i40e_pf *pf = vsi->back;
3757 	int base = vsi->base_vector;
3758 	int rx_int_idx = 0;
3759 	int tx_int_idx = 0;
3760 	int vector, err;
3761 	int irq_num;
3762 	int cpu;
3763 
3764 	for (vector = 0; vector < q_vectors; vector++) {
3765 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3766 
3767 		irq_num = pf->msix_entries[base + vector].vector;
3768 
3769 		if (q_vector->tx.ring && q_vector->rx.ring) {
3770 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3771 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3772 			tx_int_idx++;
3773 		} else if (q_vector->rx.ring) {
3774 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3775 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3776 		} else if (q_vector->tx.ring) {
3777 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3778 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3779 		} else {
3780 			/* skip this unused q_vector */
3781 			continue;
3782 		}
3783 		err = request_irq(irq_num,
3784 				  vsi->irq_handler,
3785 				  0,
3786 				  q_vector->name,
3787 				  q_vector);
3788 		if (err) {
3789 			dev_info(&pf->pdev->dev,
3790 				 "MSIX request_irq failed, error: %d\n", err);
3791 			goto free_queue_irqs;
3792 		}
3793 
3794 		/* register for affinity change notifications */
3795 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3796 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3797 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3798 		/* Spread affinity hints out across online CPUs.
3799 		 *
3800 		 * get_cpu_mask returns a static constant mask with
3801 		 * a permanent lifetime so it's ok to pass to
3802 		 * irq_set_affinity_hint without making a copy.
3803 		 */
3804 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3805 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3806 	}
3807 
3808 	vsi->irqs_ready = true;
3809 	return 0;
3810 
3811 free_queue_irqs:
3812 	while (vector) {
3813 		vector--;
3814 		irq_num = pf->msix_entries[base + vector].vector;
3815 		irq_set_affinity_notifier(irq_num, NULL);
3816 		irq_set_affinity_hint(irq_num, NULL);
3817 		free_irq(irq_num, &vsi->q_vectors[vector]);
3818 	}
3819 	return err;
3820 }
3821 
3822 /**
3823  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3824  * @vsi: the VSI being un-configured
3825  **/
3826 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3827 {
3828 	struct i40e_pf *pf = vsi->back;
3829 	struct i40e_hw *hw = &pf->hw;
3830 	int base = vsi->base_vector;
3831 	int i;
3832 
3833 	/* disable interrupt causation from each queue */
3834 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3835 		u32 val;
3836 
3837 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3838 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3839 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3840 
3841 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3842 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3843 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3844 
3845 		if (!i40e_enabled_xdp_vsi(vsi))
3846 			continue;
3847 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3848 	}
3849 
3850 	/* disable each interrupt */
3851 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3852 		for (i = vsi->base_vector;
3853 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3854 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3855 
3856 		i40e_flush(hw);
3857 		for (i = 0; i < vsi->num_q_vectors; i++)
3858 			synchronize_irq(pf->msix_entries[i + base].vector);
3859 	} else {
3860 		/* Legacy and MSI mode - this stops all interrupt handling */
3861 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3862 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3863 		i40e_flush(hw);
3864 		synchronize_irq(pf->pdev->irq);
3865 	}
3866 }
3867 
3868 /**
3869  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3870  * @vsi: the VSI being configured
3871  **/
3872 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3873 {
3874 	struct i40e_pf *pf = vsi->back;
3875 	int i;
3876 
3877 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3878 		for (i = 0; i < vsi->num_q_vectors; i++)
3879 			i40e_irq_dynamic_enable(vsi, i);
3880 	} else {
3881 		i40e_irq_dynamic_enable_icr0(pf);
3882 	}
3883 
3884 	i40e_flush(&pf->hw);
3885 	return 0;
3886 }
3887 
3888 /**
3889  * i40e_free_misc_vector - Free the vector that handles non-queue events
3890  * @pf: board private structure
3891  **/
3892 static void i40e_free_misc_vector(struct i40e_pf *pf)
3893 {
3894 	/* Disable ICR 0 */
3895 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3896 	i40e_flush(&pf->hw);
3897 
3898 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3899 		synchronize_irq(pf->msix_entries[0].vector);
3900 		free_irq(pf->msix_entries[0].vector, pf);
3901 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3902 	}
3903 }
3904 
3905 /**
3906  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3907  * @irq: interrupt number
3908  * @data: pointer to a q_vector
3909  *
3910  * This is the handler used for all MSI/Legacy interrupts, and deals
3911  * with both queue and non-queue interrupts.  This is also used in
3912  * MSIX mode to handle the non-queue interrupts.
3913  **/
3914 static irqreturn_t i40e_intr(int irq, void *data)
3915 {
3916 	struct i40e_pf *pf = (struct i40e_pf *)data;
3917 	struct i40e_hw *hw = &pf->hw;
3918 	irqreturn_t ret = IRQ_NONE;
3919 	u32 icr0, icr0_remaining;
3920 	u32 val, ena_mask;
3921 
3922 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3923 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3924 
3925 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3926 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3927 		goto enable_intr;
3928 
3929 	/* if interrupt but no bits showing, must be SWINT */
3930 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3931 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3932 		pf->sw_int_count++;
3933 
3934 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3935 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3936 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3937 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3938 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3939 	}
3940 
3941 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3942 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3943 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3944 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3945 
3946 		/* We do not have a way to disarm Queue causes while leaving
3947 		 * interrupt enabled for all other causes, ideally
3948 		 * interrupt should be disabled while we are in NAPI but
3949 		 * this is not a performance path and napi_schedule()
3950 		 * can deal with rescheduling.
3951 		 */
3952 		if (!test_bit(__I40E_DOWN, pf->state))
3953 			napi_schedule_irqoff(&q_vector->napi);
3954 	}
3955 
3956 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3957 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3958 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3959 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3960 	}
3961 
3962 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3963 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3964 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3965 	}
3966 
3967 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3968 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3969 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3970 	}
3971 
3972 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3973 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3974 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3975 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3976 		val = rd32(hw, I40E_GLGEN_RSTAT);
3977 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3978 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3979 		if (val == I40E_RESET_CORER) {
3980 			pf->corer_count++;
3981 		} else if (val == I40E_RESET_GLOBR) {
3982 			pf->globr_count++;
3983 		} else if (val == I40E_RESET_EMPR) {
3984 			pf->empr_count++;
3985 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3986 		}
3987 	}
3988 
3989 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3990 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3991 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3992 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3993 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3994 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3995 	}
3996 
3997 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3998 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3999 
4000 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4001 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4002 			i40e_ptp_tx_hwtstamp(pf);
4003 		}
4004 	}
4005 
4006 	/* If a critical error is pending we have no choice but to reset the
4007 	 * device.
4008 	 * Report and mask out any remaining unexpected interrupts.
4009 	 */
4010 	icr0_remaining = icr0 & ena_mask;
4011 	if (icr0_remaining) {
4012 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4013 			 icr0_remaining);
4014 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4015 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4016 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4017 			dev_info(&pf->pdev->dev, "device will be reset\n");
4018 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4019 			i40e_service_event_schedule(pf);
4020 		}
4021 		ena_mask &= ~icr0_remaining;
4022 	}
4023 	ret = IRQ_HANDLED;
4024 
4025 enable_intr:
4026 	/* re-enable interrupt causes */
4027 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4028 	if (!test_bit(__I40E_DOWN, pf->state) ||
4029 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4030 		i40e_service_event_schedule(pf);
4031 		i40e_irq_dynamic_enable_icr0(pf);
4032 	}
4033 
4034 	return ret;
4035 }
4036 
4037 /**
4038  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4039  * @tx_ring:  tx ring to clean
4040  * @budget:   how many cleans we're allowed
4041  *
4042  * Returns true if there's any budget left (e.g. the clean is finished)
4043  **/
4044 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4045 {
4046 	struct i40e_vsi *vsi = tx_ring->vsi;
4047 	u16 i = tx_ring->next_to_clean;
4048 	struct i40e_tx_buffer *tx_buf;
4049 	struct i40e_tx_desc *tx_desc;
4050 
4051 	tx_buf = &tx_ring->tx_bi[i];
4052 	tx_desc = I40E_TX_DESC(tx_ring, i);
4053 	i -= tx_ring->count;
4054 
4055 	do {
4056 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4057 
4058 		/* if next_to_watch is not set then there is no work pending */
4059 		if (!eop_desc)
4060 			break;
4061 
4062 		/* prevent any other reads prior to eop_desc */
4063 		smp_rmb();
4064 
4065 		/* if the descriptor isn't done, no work yet to do */
4066 		if (!(eop_desc->cmd_type_offset_bsz &
4067 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4068 			break;
4069 
4070 		/* clear next_to_watch to prevent false hangs */
4071 		tx_buf->next_to_watch = NULL;
4072 
4073 		tx_desc->buffer_addr = 0;
4074 		tx_desc->cmd_type_offset_bsz = 0;
4075 		/* move past filter desc */
4076 		tx_buf++;
4077 		tx_desc++;
4078 		i++;
4079 		if (unlikely(!i)) {
4080 			i -= tx_ring->count;
4081 			tx_buf = tx_ring->tx_bi;
4082 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4083 		}
4084 		/* unmap skb header data */
4085 		dma_unmap_single(tx_ring->dev,
4086 				 dma_unmap_addr(tx_buf, dma),
4087 				 dma_unmap_len(tx_buf, len),
4088 				 DMA_TO_DEVICE);
4089 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4090 			kfree(tx_buf->raw_buf);
4091 
4092 		tx_buf->raw_buf = NULL;
4093 		tx_buf->tx_flags = 0;
4094 		tx_buf->next_to_watch = NULL;
4095 		dma_unmap_len_set(tx_buf, len, 0);
4096 		tx_desc->buffer_addr = 0;
4097 		tx_desc->cmd_type_offset_bsz = 0;
4098 
4099 		/* move us past the eop_desc for start of next FD desc */
4100 		tx_buf++;
4101 		tx_desc++;
4102 		i++;
4103 		if (unlikely(!i)) {
4104 			i -= tx_ring->count;
4105 			tx_buf = tx_ring->tx_bi;
4106 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4107 		}
4108 
4109 		/* update budget accounting */
4110 		budget--;
4111 	} while (likely(budget));
4112 
4113 	i += tx_ring->count;
4114 	tx_ring->next_to_clean = i;
4115 
4116 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4117 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4118 
4119 	return budget > 0;
4120 }
4121 
4122 /**
4123  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4124  * @irq: interrupt number
4125  * @data: pointer to a q_vector
4126  **/
4127 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4128 {
4129 	struct i40e_q_vector *q_vector = data;
4130 	struct i40e_vsi *vsi;
4131 
4132 	if (!q_vector->tx.ring)
4133 		return IRQ_HANDLED;
4134 
4135 	vsi = q_vector->tx.ring->vsi;
4136 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4137 
4138 	return IRQ_HANDLED;
4139 }
4140 
4141 /**
4142  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4143  * @vsi: the VSI being configured
4144  * @v_idx: vector index
4145  * @qp_idx: queue pair index
4146  **/
4147 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4148 {
4149 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4150 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4151 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4152 
4153 	tx_ring->q_vector = q_vector;
4154 	tx_ring->next = q_vector->tx.ring;
4155 	q_vector->tx.ring = tx_ring;
4156 	q_vector->tx.count++;
4157 
4158 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4159 	if (i40e_enabled_xdp_vsi(vsi)) {
4160 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4161 
4162 		xdp_ring->q_vector = q_vector;
4163 		xdp_ring->next = q_vector->tx.ring;
4164 		q_vector->tx.ring = xdp_ring;
4165 		q_vector->tx.count++;
4166 	}
4167 
4168 	rx_ring->q_vector = q_vector;
4169 	rx_ring->next = q_vector->rx.ring;
4170 	q_vector->rx.ring = rx_ring;
4171 	q_vector->rx.count++;
4172 }
4173 
4174 /**
4175  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4176  * @vsi: the VSI being configured
4177  *
4178  * This function maps descriptor rings to the queue-specific vectors
4179  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4180  * one vector per queue pair, but on a constrained vector budget, we
4181  * group the queue pairs as "efficiently" as possible.
4182  **/
4183 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4184 {
4185 	int qp_remaining = vsi->num_queue_pairs;
4186 	int q_vectors = vsi->num_q_vectors;
4187 	int num_ringpairs;
4188 	int v_start = 0;
4189 	int qp_idx = 0;
4190 
4191 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4192 	 * group them so there are multiple queues per vector.
4193 	 * It is also important to go through all the vectors available to be
4194 	 * sure that if we don't use all the vectors, that the remaining vectors
4195 	 * are cleared. This is especially important when decreasing the
4196 	 * number of queues in use.
4197 	 */
4198 	for (; v_start < q_vectors; v_start++) {
4199 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4200 
4201 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4202 
4203 		q_vector->num_ringpairs = num_ringpairs;
4204 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4205 
4206 		q_vector->rx.count = 0;
4207 		q_vector->tx.count = 0;
4208 		q_vector->rx.ring = NULL;
4209 		q_vector->tx.ring = NULL;
4210 
4211 		while (num_ringpairs--) {
4212 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4213 			qp_idx++;
4214 			qp_remaining--;
4215 		}
4216 	}
4217 }
4218 
4219 /**
4220  * i40e_vsi_request_irq - Request IRQ from the OS
4221  * @vsi: the VSI being configured
4222  * @basename: name for the vector
4223  **/
4224 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4225 {
4226 	struct i40e_pf *pf = vsi->back;
4227 	int err;
4228 
4229 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4230 		err = i40e_vsi_request_irq_msix(vsi, basename);
4231 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4232 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4233 				  pf->int_name, pf);
4234 	else
4235 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4236 				  pf->int_name, pf);
4237 
4238 	if (err)
4239 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4240 
4241 	return err;
4242 }
4243 
4244 #ifdef CONFIG_NET_POLL_CONTROLLER
4245 /**
4246  * i40e_netpoll - A Polling 'interrupt' handler
4247  * @netdev: network interface device structure
4248  *
4249  * This is used by netconsole to send skbs without having to re-enable
4250  * interrupts.  It's not called while the normal interrupt routine is executing.
4251  **/
4252 static void i40e_netpoll(struct net_device *netdev)
4253 {
4254 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4255 	struct i40e_vsi *vsi = np->vsi;
4256 	struct i40e_pf *pf = vsi->back;
4257 	int i;
4258 
4259 	/* if interface is down do nothing */
4260 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4261 		return;
4262 
4263 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4264 		for (i = 0; i < vsi->num_q_vectors; i++)
4265 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4266 	} else {
4267 		i40e_intr(pf->pdev->irq, netdev);
4268 	}
4269 }
4270 #endif
4271 
4272 #define I40E_QTX_ENA_WAIT_COUNT 50
4273 
4274 /**
4275  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4276  * @pf: the PF being configured
4277  * @pf_q: the PF queue
4278  * @enable: enable or disable state of the queue
4279  *
4280  * This routine will wait for the given Tx queue of the PF to reach the
4281  * enabled or disabled state.
4282  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4283  * multiple retries; else will return 0 in case of success.
4284  **/
4285 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4286 {
4287 	int i;
4288 	u32 tx_reg;
4289 
4290 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4291 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4292 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4293 			break;
4294 
4295 		usleep_range(10, 20);
4296 	}
4297 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4298 		return -ETIMEDOUT;
4299 
4300 	return 0;
4301 }
4302 
4303 /**
4304  * i40e_control_tx_q - Start or stop a particular Tx queue
4305  * @pf: the PF structure
4306  * @pf_q: the PF queue to configure
4307  * @enable: start or stop the queue
4308  *
4309  * This function enables or disables a single queue. Note that any delay
4310  * required after the operation is expected to be handled by the caller of
4311  * this function.
4312  **/
4313 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4314 {
4315 	struct i40e_hw *hw = &pf->hw;
4316 	u32 tx_reg;
4317 	int i;
4318 
4319 	/* warn the TX unit of coming changes */
4320 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4321 	if (!enable)
4322 		usleep_range(10, 20);
4323 
4324 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4325 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4326 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4327 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4328 			break;
4329 		usleep_range(1000, 2000);
4330 	}
4331 
4332 	/* Skip if the queue is already in the requested state */
4333 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4334 		return;
4335 
4336 	/* turn on/off the queue */
4337 	if (enable) {
4338 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4339 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4340 	} else {
4341 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4342 	}
4343 
4344 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4345 }
4346 
4347 /**
4348  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4349  * @seid: VSI SEID
4350  * @pf: the PF structure
4351  * @pf_q: the PF queue to configure
4352  * @is_xdp: true if the queue is used for XDP
4353  * @enable: start or stop the queue
4354  **/
4355 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4356 			   bool is_xdp, bool enable)
4357 {
4358 	int ret;
4359 
4360 	i40e_control_tx_q(pf, pf_q, enable);
4361 
4362 	/* wait for the change to finish */
4363 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4364 	if (ret) {
4365 		dev_info(&pf->pdev->dev,
4366 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4367 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4368 			 (enable ? "en" : "dis"));
4369 	}
4370 
4371 	return ret;
4372 }
4373 
4374 /**
4375  * i40e_vsi_control_tx - Start or stop a VSI's rings
4376  * @vsi: the VSI being configured
4377  * @enable: start or stop the rings
4378  **/
4379 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4380 {
4381 	struct i40e_pf *pf = vsi->back;
4382 	int i, pf_q, ret = 0;
4383 
4384 	pf_q = vsi->base_queue;
4385 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4386 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4387 					     pf_q,
4388 					     false /*is xdp*/, enable);
4389 		if (ret)
4390 			break;
4391 
4392 		if (!i40e_enabled_xdp_vsi(vsi))
4393 			continue;
4394 
4395 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4396 					     pf_q + vsi->alloc_queue_pairs,
4397 					     true /*is xdp*/, enable);
4398 		if (ret)
4399 			break;
4400 	}
4401 	return ret;
4402 }
4403 
4404 /**
4405  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4406  * @pf: the PF being configured
4407  * @pf_q: the PF queue
4408  * @enable: enable or disable state of the queue
4409  *
4410  * This routine will wait for the given Rx queue of the PF to reach the
4411  * enabled or disabled state.
4412  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4413  * multiple retries; else will return 0 in case of success.
4414  **/
4415 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4416 {
4417 	int i;
4418 	u32 rx_reg;
4419 
4420 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4421 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4422 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4423 			break;
4424 
4425 		usleep_range(10, 20);
4426 	}
4427 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4428 		return -ETIMEDOUT;
4429 
4430 	return 0;
4431 }
4432 
4433 /**
4434  * i40e_control_rx_q - Start or stop a particular Rx queue
4435  * @pf: the PF structure
4436  * @pf_q: the PF queue to configure
4437  * @enable: start or stop the queue
4438  *
4439  * This function enables or disables a single queue. Note that
4440  * any delay required after the operation is expected to be
4441  * handled by the caller of this function.
4442  **/
4443 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4444 {
4445 	struct i40e_hw *hw = &pf->hw;
4446 	u32 rx_reg;
4447 	int i;
4448 
4449 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4450 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4451 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4452 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4453 			break;
4454 		usleep_range(1000, 2000);
4455 	}
4456 
4457 	/* Skip if the queue is already in the requested state */
4458 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4459 		return;
4460 
4461 	/* turn on/off the queue */
4462 	if (enable)
4463 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4464 	else
4465 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4466 
4467 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4468 }
4469 
4470 /**
4471  * i40e_control_wait_rx_q
4472  * @pf: the PF structure
4473  * @pf_q: queue being configured
4474  * @enable: start or stop the rings
4475  *
4476  * This function enables or disables a single queue along with waiting
4477  * for the change to finish. The caller of this function should handle
4478  * the delays needed in the case of disabling queues.
4479  **/
4480 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4481 {
4482 	int ret = 0;
4483 
4484 	i40e_control_rx_q(pf, pf_q, enable);
4485 
4486 	/* wait for the change to finish */
4487 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4488 	if (ret)
4489 		return ret;
4490 
4491 	return ret;
4492 }
4493 
4494 /**
4495  * i40e_vsi_control_rx - Start or stop a VSI's rings
4496  * @vsi: the VSI being configured
4497  * @enable: start or stop the rings
4498  **/
4499 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4500 {
4501 	struct i40e_pf *pf = vsi->back;
4502 	int i, pf_q, ret = 0;
4503 
4504 	pf_q = vsi->base_queue;
4505 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4506 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4507 		if (ret) {
4508 			dev_info(&pf->pdev->dev,
4509 				 "VSI seid %d Rx ring %d %sable timeout\n",
4510 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4511 			break;
4512 		}
4513 	}
4514 
4515 	/* Due to HW errata, on Rx disable only, the register can indicate done
4516 	 * before it really is. Needs 50ms to be sure
4517 	 */
4518 	if (!enable)
4519 		mdelay(50);
4520 
4521 	return ret;
4522 }
4523 
4524 /**
4525  * i40e_vsi_start_rings - Start a VSI's rings
4526  * @vsi: the VSI being configured
4527  **/
4528 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4529 {
4530 	int ret = 0;
4531 
4532 	/* do rx first for enable and last for disable */
4533 	ret = i40e_vsi_control_rx(vsi, true);
4534 	if (ret)
4535 		return ret;
4536 	ret = i40e_vsi_control_tx(vsi, true);
4537 
4538 	return ret;
4539 }
4540 
4541 /**
4542  * i40e_vsi_stop_rings - Stop a VSI's rings
4543  * @vsi: the VSI being configured
4544  **/
4545 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4546 {
4547 	/* When port TX is suspended, don't wait */
4548 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4549 		return i40e_vsi_stop_rings_no_wait(vsi);
4550 
4551 	/* do rx first for enable and last for disable
4552 	 * Ignore return value, we need to shutdown whatever we can
4553 	 */
4554 	i40e_vsi_control_tx(vsi, false);
4555 	i40e_vsi_control_rx(vsi, false);
4556 }
4557 
4558 /**
4559  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4560  * @vsi: the VSI being shutdown
4561  *
4562  * This function stops all the rings for a VSI but does not delay to verify
4563  * that rings have been disabled. It is expected that the caller is shutting
4564  * down multiple VSIs at once and will delay together for all the VSIs after
4565  * initiating the shutdown. This is particularly useful for shutting down lots
4566  * of VFs together. Otherwise, a large delay can be incurred while configuring
4567  * each VSI in serial.
4568  **/
4569 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4570 {
4571 	struct i40e_pf *pf = vsi->back;
4572 	int i, pf_q;
4573 
4574 	pf_q = vsi->base_queue;
4575 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4576 		i40e_control_tx_q(pf, pf_q, false);
4577 		i40e_control_rx_q(pf, pf_q, false);
4578 	}
4579 }
4580 
4581 /**
4582  * i40e_vsi_free_irq - Free the irq association with the OS
4583  * @vsi: the VSI being configured
4584  **/
4585 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4586 {
4587 	struct i40e_pf *pf = vsi->back;
4588 	struct i40e_hw *hw = &pf->hw;
4589 	int base = vsi->base_vector;
4590 	u32 val, qp;
4591 	int i;
4592 
4593 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4594 		if (!vsi->q_vectors)
4595 			return;
4596 
4597 		if (!vsi->irqs_ready)
4598 			return;
4599 
4600 		vsi->irqs_ready = false;
4601 		for (i = 0; i < vsi->num_q_vectors; i++) {
4602 			int irq_num;
4603 			u16 vector;
4604 
4605 			vector = i + base;
4606 			irq_num = pf->msix_entries[vector].vector;
4607 
4608 			/* free only the irqs that were actually requested */
4609 			if (!vsi->q_vectors[i] ||
4610 			    !vsi->q_vectors[i]->num_ringpairs)
4611 				continue;
4612 
4613 			/* clear the affinity notifier in the IRQ descriptor */
4614 			irq_set_affinity_notifier(irq_num, NULL);
4615 			/* remove our suggested affinity mask for this IRQ */
4616 			irq_set_affinity_hint(irq_num, NULL);
4617 			synchronize_irq(irq_num);
4618 			free_irq(irq_num, vsi->q_vectors[i]);
4619 
4620 			/* Tear down the interrupt queue link list
4621 			 *
4622 			 * We know that they come in pairs and always
4623 			 * the Rx first, then the Tx.  To clear the
4624 			 * link list, stick the EOL value into the
4625 			 * next_q field of the registers.
4626 			 */
4627 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4628 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4629 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4630 			val |= I40E_QUEUE_END_OF_LIST
4631 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4632 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4633 
4634 			while (qp != I40E_QUEUE_END_OF_LIST) {
4635 				u32 next;
4636 
4637 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4638 
4639 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4640 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4641 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4642 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4643 
4644 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4645 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4646 
4647 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4648 
4649 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4650 
4651 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4652 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4653 
4654 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4655 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4656 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4657 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4658 
4659 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4660 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4661 
4662 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4663 				qp = next;
4664 			}
4665 		}
4666 	} else {
4667 		free_irq(pf->pdev->irq, pf);
4668 
4669 		val = rd32(hw, I40E_PFINT_LNKLST0);
4670 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4671 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4672 		val |= I40E_QUEUE_END_OF_LIST
4673 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4674 		wr32(hw, I40E_PFINT_LNKLST0, val);
4675 
4676 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4677 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4678 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4679 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4680 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4681 
4682 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4683 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4684 
4685 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4686 
4687 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4688 
4689 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4690 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4691 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4692 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4693 
4694 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4695 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4696 
4697 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4698 	}
4699 }
4700 
4701 /**
4702  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4703  * @vsi: the VSI being configured
4704  * @v_idx: Index of vector to be freed
4705  *
4706  * This function frees the memory allocated to the q_vector.  In addition if
4707  * NAPI is enabled it will delete any references to the NAPI struct prior
4708  * to freeing the q_vector.
4709  **/
4710 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4711 {
4712 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4713 	struct i40e_ring *ring;
4714 
4715 	if (!q_vector)
4716 		return;
4717 
4718 	/* disassociate q_vector from rings */
4719 	i40e_for_each_ring(ring, q_vector->tx)
4720 		ring->q_vector = NULL;
4721 
4722 	i40e_for_each_ring(ring, q_vector->rx)
4723 		ring->q_vector = NULL;
4724 
4725 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4726 	if (vsi->netdev)
4727 		netif_napi_del(&q_vector->napi);
4728 
4729 	vsi->q_vectors[v_idx] = NULL;
4730 
4731 	kfree_rcu(q_vector, rcu);
4732 }
4733 
4734 /**
4735  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4736  * @vsi: the VSI being un-configured
4737  *
4738  * This frees the memory allocated to the q_vectors and
4739  * deletes references to the NAPI struct.
4740  **/
4741 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4742 {
4743 	int v_idx;
4744 
4745 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4746 		i40e_free_q_vector(vsi, v_idx);
4747 }
4748 
4749 /**
4750  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4751  * @pf: board private structure
4752  **/
4753 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4754 {
4755 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4756 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4757 		pci_disable_msix(pf->pdev);
4758 		kfree(pf->msix_entries);
4759 		pf->msix_entries = NULL;
4760 		kfree(pf->irq_pile);
4761 		pf->irq_pile = NULL;
4762 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4763 		pci_disable_msi(pf->pdev);
4764 	}
4765 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4766 }
4767 
4768 /**
4769  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4770  * @pf: board private structure
4771  *
4772  * We go through and clear interrupt specific resources and reset the structure
4773  * to pre-load conditions
4774  **/
4775 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4776 {
4777 	int i;
4778 
4779 	i40e_free_misc_vector(pf);
4780 
4781 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4782 		      I40E_IWARP_IRQ_PILE_ID);
4783 
4784 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4785 	for (i = 0; i < pf->num_alloc_vsi; i++)
4786 		if (pf->vsi[i])
4787 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4788 	i40e_reset_interrupt_capability(pf);
4789 }
4790 
4791 /**
4792  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4793  * @vsi: the VSI being configured
4794  **/
4795 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4796 {
4797 	int q_idx;
4798 
4799 	if (!vsi->netdev)
4800 		return;
4801 
4802 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4803 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4804 
4805 		if (q_vector->rx.ring || q_vector->tx.ring)
4806 			napi_enable(&q_vector->napi);
4807 	}
4808 }
4809 
4810 /**
4811  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4812  * @vsi: the VSI being configured
4813  **/
4814 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4815 {
4816 	int q_idx;
4817 
4818 	if (!vsi->netdev)
4819 		return;
4820 
4821 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4822 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4823 
4824 		if (q_vector->rx.ring || q_vector->tx.ring)
4825 			napi_disable(&q_vector->napi);
4826 	}
4827 }
4828 
4829 /**
4830  * i40e_vsi_close - Shut down a VSI
4831  * @vsi: the vsi to be quelled
4832  **/
4833 static void i40e_vsi_close(struct i40e_vsi *vsi)
4834 {
4835 	struct i40e_pf *pf = vsi->back;
4836 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4837 		i40e_down(vsi);
4838 	i40e_vsi_free_irq(vsi);
4839 	i40e_vsi_free_tx_resources(vsi);
4840 	i40e_vsi_free_rx_resources(vsi);
4841 	vsi->current_netdev_flags = 0;
4842 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4843 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4844 		set_bit(__I40E_CLIENT_RESET, pf->state);
4845 }
4846 
4847 /**
4848  * i40e_quiesce_vsi - Pause a given VSI
4849  * @vsi: the VSI being paused
4850  **/
4851 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4852 {
4853 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4854 		return;
4855 
4856 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4857 	if (vsi->netdev && netif_running(vsi->netdev))
4858 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4859 	else
4860 		i40e_vsi_close(vsi);
4861 }
4862 
4863 /**
4864  * i40e_unquiesce_vsi - Resume a given VSI
4865  * @vsi: the VSI being resumed
4866  **/
4867 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4868 {
4869 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4870 		return;
4871 
4872 	if (vsi->netdev && netif_running(vsi->netdev))
4873 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4874 	else
4875 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4876 }
4877 
4878 /**
4879  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4880  * @pf: the PF
4881  **/
4882 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4883 {
4884 	int v;
4885 
4886 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4887 		if (pf->vsi[v])
4888 			i40e_quiesce_vsi(pf->vsi[v]);
4889 	}
4890 }
4891 
4892 /**
4893  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4894  * @pf: the PF
4895  **/
4896 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4897 {
4898 	int v;
4899 
4900 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4901 		if (pf->vsi[v])
4902 			i40e_unquiesce_vsi(pf->vsi[v]);
4903 	}
4904 }
4905 
4906 /**
4907  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4908  * @vsi: the VSI being configured
4909  *
4910  * Wait until all queues on a given VSI have been disabled.
4911  **/
4912 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4913 {
4914 	struct i40e_pf *pf = vsi->back;
4915 	int i, pf_q, ret;
4916 
4917 	pf_q = vsi->base_queue;
4918 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4919 		/* Check and wait for the Tx queue */
4920 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4921 		if (ret) {
4922 			dev_info(&pf->pdev->dev,
4923 				 "VSI seid %d Tx ring %d disable timeout\n",
4924 				 vsi->seid, pf_q);
4925 			return ret;
4926 		}
4927 
4928 		if (!i40e_enabled_xdp_vsi(vsi))
4929 			goto wait_rx;
4930 
4931 		/* Check and wait for the XDP Tx queue */
4932 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4933 				       false);
4934 		if (ret) {
4935 			dev_info(&pf->pdev->dev,
4936 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4937 				 vsi->seid, pf_q);
4938 			return ret;
4939 		}
4940 wait_rx:
4941 		/* Check and wait for the Rx queue */
4942 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4943 		if (ret) {
4944 			dev_info(&pf->pdev->dev,
4945 				 "VSI seid %d Rx ring %d disable timeout\n",
4946 				 vsi->seid, pf_q);
4947 			return ret;
4948 		}
4949 	}
4950 
4951 	return 0;
4952 }
4953 
4954 #ifdef CONFIG_I40E_DCB
4955 /**
4956  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4957  * @pf: the PF
4958  *
4959  * This function waits for the queues to be in disabled state for all the
4960  * VSIs that are managed by this PF.
4961  **/
4962 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4963 {
4964 	int v, ret = 0;
4965 
4966 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4967 		if (pf->vsi[v]) {
4968 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4969 			if (ret)
4970 				break;
4971 		}
4972 	}
4973 
4974 	return ret;
4975 }
4976 
4977 #endif
4978 
4979 /**
4980  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4981  * @pf: pointer to PF
4982  *
4983  * Get TC map for ISCSI PF type that will include iSCSI TC
4984  * and LAN TC.
4985  **/
4986 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4987 {
4988 	struct i40e_dcb_app_priority_table app;
4989 	struct i40e_hw *hw = &pf->hw;
4990 	u8 enabled_tc = 1; /* TC0 is always enabled */
4991 	u8 tc, i;
4992 	/* Get the iSCSI APP TLV */
4993 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4994 
4995 	for (i = 0; i < dcbcfg->numapps; i++) {
4996 		app = dcbcfg->app[i];
4997 		if (app.selector == I40E_APP_SEL_TCPIP &&
4998 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
4999 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5000 			enabled_tc |= BIT(tc);
5001 			break;
5002 		}
5003 	}
5004 
5005 	return enabled_tc;
5006 }
5007 
5008 /**
5009  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5010  * @dcbcfg: the corresponding DCBx configuration structure
5011  *
5012  * Return the number of TCs from given DCBx configuration
5013  **/
5014 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5015 {
5016 	int i, tc_unused = 0;
5017 	u8 num_tc = 0;
5018 	u8 ret = 0;
5019 
5020 	/* Scan the ETS Config Priority Table to find
5021 	 * traffic class enabled for a given priority
5022 	 * and create a bitmask of enabled TCs
5023 	 */
5024 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5025 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5026 
5027 	/* Now scan the bitmask to check for
5028 	 * contiguous TCs starting with TC0
5029 	 */
5030 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5031 		if (num_tc & BIT(i)) {
5032 			if (!tc_unused) {
5033 				ret++;
5034 			} else {
5035 				pr_err("Non-contiguous TC - Disabling DCB\n");
5036 				return 1;
5037 			}
5038 		} else {
5039 			tc_unused = 1;
5040 		}
5041 	}
5042 
5043 	/* There is always at least TC0 */
5044 	if (!ret)
5045 		ret = 1;
5046 
5047 	return ret;
5048 }
5049 
5050 /**
5051  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5052  * @dcbcfg: the corresponding DCBx configuration structure
5053  *
5054  * Query the current DCB configuration and return the number of
5055  * traffic classes enabled from the given DCBX config
5056  **/
5057 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5058 {
5059 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5060 	u8 enabled_tc = 1;
5061 	u8 i;
5062 
5063 	for (i = 0; i < num_tc; i++)
5064 		enabled_tc |= BIT(i);
5065 
5066 	return enabled_tc;
5067 }
5068 
5069 /**
5070  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5071  * @pf: PF being queried
5072  *
5073  * Query the current MQPRIO configuration and return the number of
5074  * traffic classes enabled.
5075  **/
5076 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5077 {
5078 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5079 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5080 	u8 enabled_tc = 1, i;
5081 
5082 	for (i = 1; i < num_tc; i++)
5083 		enabled_tc |= BIT(i);
5084 	return enabled_tc;
5085 }
5086 
5087 /**
5088  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5089  * @pf: PF being queried
5090  *
5091  * Return number of traffic classes enabled for the given PF
5092  **/
5093 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5094 {
5095 	struct i40e_hw *hw = &pf->hw;
5096 	u8 i, enabled_tc = 1;
5097 	u8 num_tc = 0;
5098 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5099 
5100 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5101 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5102 
5103 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5104 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5105 		return 1;
5106 
5107 	/* SFP mode will be enabled for all TCs on port */
5108 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5109 		return i40e_dcb_get_num_tc(dcbcfg);
5110 
5111 	/* MFP mode return count of enabled TCs for this PF */
5112 	if (pf->hw.func_caps.iscsi)
5113 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5114 	else
5115 		return 1; /* Only TC0 */
5116 
5117 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5118 		if (enabled_tc & BIT(i))
5119 			num_tc++;
5120 	}
5121 	return num_tc;
5122 }
5123 
5124 /**
5125  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5126  * @pf: PF being queried
5127  *
5128  * Return a bitmap for enabled traffic classes for this PF.
5129  **/
5130 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5131 {
5132 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5133 		return i40e_mqprio_get_enabled_tc(pf);
5134 
5135 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5136 	 * default TC
5137 	 */
5138 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5139 		return I40E_DEFAULT_TRAFFIC_CLASS;
5140 
5141 	/* SFP mode we want PF to be enabled for all TCs */
5142 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5143 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5144 
5145 	/* MFP enabled and iSCSI PF type */
5146 	if (pf->hw.func_caps.iscsi)
5147 		return i40e_get_iscsi_tc_map(pf);
5148 	else
5149 		return I40E_DEFAULT_TRAFFIC_CLASS;
5150 }
5151 
5152 /**
5153  * i40e_vsi_get_bw_info - Query VSI BW Information
5154  * @vsi: the VSI being queried
5155  *
5156  * Returns 0 on success, negative value on failure
5157  **/
5158 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5159 {
5160 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5161 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5162 	struct i40e_pf *pf = vsi->back;
5163 	struct i40e_hw *hw = &pf->hw;
5164 	i40e_status ret;
5165 	u32 tc_bw_max;
5166 	int i;
5167 
5168 	/* Get the VSI level BW configuration */
5169 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5170 	if (ret) {
5171 		dev_info(&pf->pdev->dev,
5172 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5173 			 i40e_stat_str(&pf->hw, ret),
5174 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5175 		return -EINVAL;
5176 	}
5177 
5178 	/* Get the VSI level BW configuration per TC */
5179 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5180 					       NULL);
5181 	if (ret) {
5182 		dev_info(&pf->pdev->dev,
5183 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5184 			 i40e_stat_str(&pf->hw, ret),
5185 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5186 		return -EINVAL;
5187 	}
5188 
5189 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5190 		dev_info(&pf->pdev->dev,
5191 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5192 			 bw_config.tc_valid_bits,
5193 			 bw_ets_config.tc_valid_bits);
5194 		/* Still continuing */
5195 	}
5196 
5197 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5198 	vsi->bw_max_quanta = bw_config.max_bw;
5199 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5200 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5201 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5202 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5203 		vsi->bw_ets_limit_credits[i] =
5204 					le16_to_cpu(bw_ets_config.credits[i]);
5205 		/* 3 bits out of 4 for each TC */
5206 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5207 	}
5208 
5209 	return 0;
5210 }
5211 
5212 /**
5213  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5214  * @vsi: the VSI being configured
5215  * @enabled_tc: TC bitmap
5216  * @bw_share: BW shared credits per TC
5217  *
5218  * Returns 0 on success, negative value on failure
5219  **/
5220 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5221 				       u8 *bw_share)
5222 {
5223 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5224 	struct i40e_pf *pf = vsi->back;
5225 	i40e_status ret;
5226 	int i;
5227 
5228 	/* There is no need to reset BW when mqprio mode is on.  */
5229 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5230 		return 0;
5231 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5232 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5233 		if (ret)
5234 			dev_info(&pf->pdev->dev,
5235 				 "Failed to reset tx rate for vsi->seid %u\n",
5236 				 vsi->seid);
5237 		return ret;
5238 	}
5239 	bw_data.tc_valid_bits = enabled_tc;
5240 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5241 		bw_data.tc_bw_credits[i] = bw_share[i];
5242 
5243 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5244 	if (ret) {
5245 		dev_info(&pf->pdev->dev,
5246 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5247 			 pf->hw.aq.asq_last_status);
5248 		return -EINVAL;
5249 	}
5250 
5251 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5252 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5253 
5254 	return 0;
5255 }
5256 
5257 /**
5258  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5259  * @vsi: the VSI being configured
5260  * @enabled_tc: TC map to be enabled
5261  *
5262  **/
5263 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5264 {
5265 	struct net_device *netdev = vsi->netdev;
5266 	struct i40e_pf *pf = vsi->back;
5267 	struct i40e_hw *hw = &pf->hw;
5268 	u8 netdev_tc = 0;
5269 	int i;
5270 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5271 
5272 	if (!netdev)
5273 		return;
5274 
5275 	if (!enabled_tc) {
5276 		netdev_reset_tc(netdev);
5277 		return;
5278 	}
5279 
5280 	/* Set up actual enabled TCs on the VSI */
5281 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5282 		return;
5283 
5284 	/* set per TC queues for the VSI */
5285 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5286 		/* Only set TC queues for enabled tcs
5287 		 *
5288 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5289 		 * enabled_tc bitmap would be 0x00001001; the driver
5290 		 * will set the numtc for netdev as 2 that will be
5291 		 * referenced by the netdev layer as TC 0 and 1.
5292 		 */
5293 		if (vsi->tc_config.enabled_tc & BIT(i))
5294 			netdev_set_tc_queue(netdev,
5295 					vsi->tc_config.tc_info[i].netdev_tc,
5296 					vsi->tc_config.tc_info[i].qcount,
5297 					vsi->tc_config.tc_info[i].qoffset);
5298 	}
5299 
5300 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5301 		return;
5302 
5303 	/* Assign UP2TC map for the VSI */
5304 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5305 		/* Get the actual TC# for the UP */
5306 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5307 		/* Get the mapped netdev TC# for the UP */
5308 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5309 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5310 	}
5311 }
5312 
5313 /**
5314  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5315  * @vsi: the VSI being configured
5316  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5317  **/
5318 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5319 				      struct i40e_vsi_context *ctxt)
5320 {
5321 	/* copy just the sections touched not the entire info
5322 	 * since not all sections are valid as returned by
5323 	 * update vsi params
5324 	 */
5325 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5326 	memcpy(&vsi->info.queue_mapping,
5327 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5328 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5329 	       sizeof(vsi->info.tc_mapping));
5330 }
5331 
5332 /**
5333  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5334  * @vsi: VSI to be configured
5335  * @enabled_tc: TC bitmap
5336  *
5337  * This configures a particular VSI for TCs that are mapped to the
5338  * given TC bitmap. It uses default bandwidth share for TCs across
5339  * VSIs to configure TC for a particular VSI.
5340  *
5341  * NOTE:
5342  * It is expected that the VSI queues have been quisced before calling
5343  * this function.
5344  **/
5345 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5346 {
5347 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5348 	struct i40e_pf *pf = vsi->back;
5349 	struct i40e_hw *hw = &pf->hw;
5350 	struct i40e_vsi_context ctxt;
5351 	int ret = 0;
5352 	int i;
5353 
5354 	/* Check if enabled_tc is same as existing or new TCs */
5355 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5356 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5357 		return ret;
5358 
5359 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5360 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5361 		if (enabled_tc & BIT(i))
5362 			bw_share[i] = 1;
5363 	}
5364 
5365 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5366 	if (ret) {
5367 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5368 
5369 		dev_info(&pf->pdev->dev,
5370 			 "Failed configuring TC map %d for VSI %d\n",
5371 			 enabled_tc, vsi->seid);
5372 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5373 						  &bw_config, NULL);
5374 		if (ret) {
5375 			dev_info(&pf->pdev->dev,
5376 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5377 				 i40e_stat_str(hw, ret),
5378 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5379 			goto out;
5380 		}
5381 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5382 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5383 
5384 			if (!valid_tc)
5385 				valid_tc = bw_config.tc_valid_bits;
5386 			/* Always enable TC0, no matter what */
5387 			valid_tc |= 1;
5388 			dev_info(&pf->pdev->dev,
5389 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5390 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5391 			enabled_tc = valid_tc;
5392 		}
5393 
5394 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5395 		if (ret) {
5396 			dev_err(&pf->pdev->dev,
5397 				"Unable to  configure TC map %d for VSI %d\n",
5398 				enabled_tc, vsi->seid);
5399 			goto out;
5400 		}
5401 	}
5402 
5403 	/* Update Queue Pairs Mapping for currently enabled UPs */
5404 	ctxt.seid = vsi->seid;
5405 	ctxt.pf_num = vsi->back->hw.pf_id;
5406 	ctxt.vf_num = 0;
5407 	ctxt.uplink_seid = vsi->uplink_seid;
5408 	ctxt.info = vsi->info;
5409 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5410 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5411 		if (ret)
5412 			goto out;
5413 	} else {
5414 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5415 	}
5416 
5417 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5418 	 * queues changed.
5419 	 */
5420 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5421 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5422 				      vsi->num_queue_pairs);
5423 		ret = i40e_vsi_config_rss(vsi);
5424 		if (ret) {
5425 			dev_info(&vsi->back->pdev->dev,
5426 				 "Failed to reconfig rss for num_queues\n");
5427 			return ret;
5428 		}
5429 		vsi->reconfig_rss = false;
5430 	}
5431 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5432 		ctxt.info.valid_sections |=
5433 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5434 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5435 	}
5436 
5437 	/* Update the VSI after updating the VSI queue-mapping
5438 	 * information
5439 	 */
5440 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5441 	if (ret) {
5442 		dev_info(&pf->pdev->dev,
5443 			 "Update vsi tc config failed, err %s aq_err %s\n",
5444 			 i40e_stat_str(hw, ret),
5445 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5446 		goto out;
5447 	}
5448 	/* update the local VSI info with updated queue map */
5449 	i40e_vsi_update_queue_map(vsi, &ctxt);
5450 	vsi->info.valid_sections = 0;
5451 
5452 	/* Update current VSI BW information */
5453 	ret = i40e_vsi_get_bw_info(vsi);
5454 	if (ret) {
5455 		dev_info(&pf->pdev->dev,
5456 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5457 			 i40e_stat_str(hw, ret),
5458 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5459 		goto out;
5460 	}
5461 
5462 	/* Update the netdev TC setup */
5463 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5464 out:
5465 	return ret;
5466 }
5467 
5468 /**
5469  * i40e_get_link_speed - Returns link speed for the interface
5470  * @vsi: VSI to be configured
5471  *
5472  **/
5473 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5474 {
5475 	struct i40e_pf *pf = vsi->back;
5476 
5477 	switch (pf->hw.phy.link_info.link_speed) {
5478 	case I40E_LINK_SPEED_40GB:
5479 		return 40000;
5480 	case I40E_LINK_SPEED_25GB:
5481 		return 25000;
5482 	case I40E_LINK_SPEED_20GB:
5483 		return 20000;
5484 	case I40E_LINK_SPEED_10GB:
5485 		return 10000;
5486 	case I40E_LINK_SPEED_1GB:
5487 		return 1000;
5488 	default:
5489 		return -EINVAL;
5490 	}
5491 }
5492 
5493 /**
5494  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5495  * @vsi: VSI to be configured
5496  * @seid: seid of the channel/VSI
5497  * @max_tx_rate: max TX rate to be configured as BW limit
5498  *
5499  * Helper function to set BW limit for a given VSI
5500  **/
5501 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5502 {
5503 	struct i40e_pf *pf = vsi->back;
5504 	u64 credits = 0;
5505 	int speed = 0;
5506 	int ret = 0;
5507 
5508 	speed = i40e_get_link_speed(vsi);
5509 	if (max_tx_rate > speed) {
5510 		dev_err(&pf->pdev->dev,
5511 			"Invalid max tx rate %llu specified for VSI seid %d.",
5512 			max_tx_rate, seid);
5513 		return -EINVAL;
5514 	}
5515 	if (max_tx_rate && max_tx_rate < 50) {
5516 		dev_warn(&pf->pdev->dev,
5517 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5518 		max_tx_rate = 50;
5519 	}
5520 
5521 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5522 	credits = max_tx_rate;
5523 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5524 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5525 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5526 	if (ret)
5527 		dev_err(&pf->pdev->dev,
5528 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5529 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5530 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5531 	return ret;
5532 }
5533 
5534 /**
5535  * i40e_remove_queue_channels - Remove queue channels for the TCs
5536  * @vsi: VSI to be configured
5537  *
5538  * Remove queue channels for the TCs
5539  **/
5540 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5541 {
5542 	enum i40e_admin_queue_err last_aq_status;
5543 	struct i40e_cloud_filter *cfilter;
5544 	struct i40e_channel *ch, *ch_tmp;
5545 	struct i40e_pf *pf = vsi->back;
5546 	struct hlist_node *node;
5547 	int ret, i;
5548 
5549 	/* Reset rss size that was stored when reconfiguring rss for
5550 	 * channel VSIs with non-power-of-2 queue count.
5551 	 */
5552 	vsi->current_rss_size = 0;
5553 
5554 	/* perform cleanup for channels if they exist */
5555 	if (list_empty(&vsi->ch_list))
5556 		return;
5557 
5558 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5559 		struct i40e_vsi *p_vsi;
5560 
5561 		list_del(&ch->list);
5562 		p_vsi = ch->parent_vsi;
5563 		if (!p_vsi || !ch->initialized) {
5564 			kfree(ch);
5565 			continue;
5566 		}
5567 		/* Reset queue contexts */
5568 		for (i = 0; i < ch->num_queue_pairs; i++) {
5569 			struct i40e_ring *tx_ring, *rx_ring;
5570 			u16 pf_q;
5571 
5572 			pf_q = ch->base_queue + i;
5573 			tx_ring = vsi->tx_rings[pf_q];
5574 			tx_ring->ch = NULL;
5575 
5576 			rx_ring = vsi->rx_rings[pf_q];
5577 			rx_ring->ch = NULL;
5578 		}
5579 
5580 		/* Reset BW configured for this VSI via mqprio */
5581 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5582 		if (ret)
5583 			dev_info(&vsi->back->pdev->dev,
5584 				 "Failed to reset tx rate for ch->seid %u\n",
5585 				 ch->seid);
5586 
5587 		/* delete cloud filters associated with this channel */
5588 		hlist_for_each_entry_safe(cfilter, node,
5589 					  &pf->cloud_filter_list, cloud_node) {
5590 			if (cfilter->seid != ch->seid)
5591 				continue;
5592 
5593 			hash_del(&cfilter->cloud_node);
5594 			if (cfilter->dst_port)
5595 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5596 									cfilter,
5597 									false);
5598 			else
5599 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5600 								false);
5601 			last_aq_status = pf->hw.aq.asq_last_status;
5602 			if (ret)
5603 				dev_info(&pf->pdev->dev,
5604 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5605 					 i40e_stat_str(&pf->hw, ret),
5606 					 i40e_aq_str(&pf->hw, last_aq_status));
5607 			kfree(cfilter);
5608 		}
5609 
5610 		/* delete VSI from FW */
5611 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5612 					     NULL);
5613 		if (ret)
5614 			dev_err(&vsi->back->pdev->dev,
5615 				"unable to remove channel (%d) for parent VSI(%d)\n",
5616 				ch->seid, p_vsi->seid);
5617 		kfree(ch);
5618 	}
5619 	INIT_LIST_HEAD(&vsi->ch_list);
5620 }
5621 
5622 /**
5623  * i40e_is_any_channel - channel exist or not
5624  * @vsi: ptr to VSI to which channels are associated with
5625  *
5626  * Returns true or false if channel(s) exist for associated VSI or not
5627  **/
5628 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5629 {
5630 	struct i40e_channel *ch, *ch_tmp;
5631 
5632 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5633 		if (ch->initialized)
5634 			return true;
5635 	}
5636 
5637 	return false;
5638 }
5639 
5640 /**
5641  * i40e_get_max_queues_for_channel
5642  * @vsi: ptr to VSI to which channels are associated with
5643  *
5644  * Helper function which returns max value among the queue counts set on the
5645  * channels/TCs created.
5646  **/
5647 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5648 {
5649 	struct i40e_channel *ch, *ch_tmp;
5650 	int max = 0;
5651 
5652 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5653 		if (!ch->initialized)
5654 			continue;
5655 		if (ch->num_queue_pairs > max)
5656 			max = ch->num_queue_pairs;
5657 	}
5658 
5659 	return max;
5660 }
5661 
5662 /**
5663  * i40e_validate_num_queues - validate num_queues w.r.t channel
5664  * @pf: ptr to PF device
5665  * @num_queues: number of queues
5666  * @vsi: the parent VSI
5667  * @reconfig_rss: indicates should the RSS be reconfigured or not
5668  *
5669  * This function validates number of queues in the context of new channel
5670  * which is being established and determines if RSS should be reconfigured
5671  * or not for parent VSI.
5672  **/
5673 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5674 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5675 {
5676 	int max_ch_queues;
5677 
5678 	if (!reconfig_rss)
5679 		return -EINVAL;
5680 
5681 	*reconfig_rss = false;
5682 	if (vsi->current_rss_size) {
5683 		if (num_queues > vsi->current_rss_size) {
5684 			dev_dbg(&pf->pdev->dev,
5685 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5686 				num_queues, vsi->current_rss_size);
5687 			return -EINVAL;
5688 		} else if ((num_queues < vsi->current_rss_size) &&
5689 			   (!is_power_of_2(num_queues))) {
5690 			dev_dbg(&pf->pdev->dev,
5691 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5692 				num_queues, vsi->current_rss_size);
5693 			return -EINVAL;
5694 		}
5695 	}
5696 
5697 	if (!is_power_of_2(num_queues)) {
5698 		/* Find the max num_queues configured for channel if channel
5699 		 * exist.
5700 		 * if channel exist, then enforce 'num_queues' to be more than
5701 		 * max ever queues configured for channel.
5702 		 */
5703 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5704 		if (num_queues < max_ch_queues) {
5705 			dev_dbg(&pf->pdev->dev,
5706 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5707 				num_queues, max_ch_queues);
5708 			return -EINVAL;
5709 		}
5710 		*reconfig_rss = true;
5711 	}
5712 
5713 	return 0;
5714 }
5715 
5716 /**
5717  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5718  * @vsi: the VSI being setup
5719  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5720  *
5721  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5722  **/
5723 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5724 {
5725 	struct i40e_pf *pf = vsi->back;
5726 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5727 	struct i40e_hw *hw = &pf->hw;
5728 	int local_rss_size;
5729 	u8 *lut;
5730 	int ret;
5731 
5732 	if (!vsi->rss_size)
5733 		return -EINVAL;
5734 
5735 	if (rss_size > vsi->rss_size)
5736 		return -EINVAL;
5737 
5738 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5739 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5740 	if (!lut)
5741 		return -ENOMEM;
5742 
5743 	/* Ignoring user configured lut if there is one */
5744 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5745 
5746 	/* Use user configured hash key if there is one, otherwise
5747 	 * use default.
5748 	 */
5749 	if (vsi->rss_hkey_user)
5750 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5751 	else
5752 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5753 
5754 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5755 	if (ret) {
5756 		dev_info(&pf->pdev->dev,
5757 			 "Cannot set RSS lut, err %s aq_err %s\n",
5758 			 i40e_stat_str(hw, ret),
5759 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5760 		kfree(lut);
5761 		return ret;
5762 	}
5763 	kfree(lut);
5764 
5765 	/* Do the update w.r.t. storing rss_size */
5766 	if (!vsi->orig_rss_size)
5767 		vsi->orig_rss_size = vsi->rss_size;
5768 	vsi->current_rss_size = local_rss_size;
5769 
5770 	return ret;
5771 }
5772 
5773 /**
5774  * i40e_channel_setup_queue_map - Setup a channel queue map
5775  * @pf: ptr to PF device
5776  * @vsi: the VSI being setup
5777  * @ctxt: VSI context structure
5778  * @ch: ptr to channel structure
5779  *
5780  * Setup queue map for a specific channel
5781  **/
5782 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5783 					 struct i40e_vsi_context *ctxt,
5784 					 struct i40e_channel *ch)
5785 {
5786 	u16 qcount, qmap, sections = 0;
5787 	u8 offset = 0;
5788 	int pow;
5789 
5790 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5791 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5792 
5793 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5794 	ch->num_queue_pairs = qcount;
5795 
5796 	/* find the next higher power-of-2 of num queue pairs */
5797 	pow = ilog2(qcount);
5798 	if (!is_power_of_2(qcount))
5799 		pow++;
5800 
5801 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5802 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5803 
5804 	/* Setup queue TC[0].qmap for given VSI context */
5805 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5806 
5807 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5808 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5809 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5810 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5811 }
5812 
5813 /**
5814  * i40e_add_channel - add a channel by adding VSI
5815  * @pf: ptr to PF device
5816  * @uplink_seid: underlying HW switching element (VEB) ID
5817  * @ch: ptr to channel structure
5818  *
5819  * Add a channel (VSI) using add_vsi and queue_map
5820  **/
5821 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5822 			    struct i40e_channel *ch)
5823 {
5824 	struct i40e_hw *hw = &pf->hw;
5825 	struct i40e_vsi_context ctxt;
5826 	u8 enabled_tc = 0x1; /* TC0 enabled */
5827 	int ret;
5828 
5829 	if (ch->type != I40E_VSI_VMDQ2) {
5830 		dev_info(&pf->pdev->dev,
5831 			 "add new vsi failed, ch->type %d\n", ch->type);
5832 		return -EINVAL;
5833 	}
5834 
5835 	memset(&ctxt, 0, sizeof(ctxt));
5836 	ctxt.pf_num = hw->pf_id;
5837 	ctxt.vf_num = 0;
5838 	ctxt.uplink_seid = uplink_seid;
5839 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5840 	if (ch->type == I40E_VSI_VMDQ2)
5841 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5842 
5843 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5844 		ctxt.info.valid_sections |=
5845 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5846 		ctxt.info.switch_id =
5847 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5848 	}
5849 
5850 	/* Set queue map for a given VSI context */
5851 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5852 
5853 	/* Now time to create VSI */
5854 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5855 	if (ret) {
5856 		dev_info(&pf->pdev->dev,
5857 			 "add new vsi failed, err %s aq_err %s\n",
5858 			 i40e_stat_str(&pf->hw, ret),
5859 			 i40e_aq_str(&pf->hw,
5860 				     pf->hw.aq.asq_last_status));
5861 		return -ENOENT;
5862 	}
5863 
5864 	/* Success, update channel */
5865 	ch->enabled_tc = enabled_tc;
5866 	ch->seid = ctxt.seid;
5867 	ch->vsi_number = ctxt.vsi_number;
5868 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5869 
5870 	/* copy just the sections touched not the entire info
5871 	 * since not all sections are valid as returned by
5872 	 * update vsi params
5873 	 */
5874 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5875 	memcpy(&ch->info.queue_mapping,
5876 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5877 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5878 	       sizeof(ctxt.info.tc_mapping));
5879 
5880 	return 0;
5881 }
5882 
5883 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5884 				  u8 *bw_share)
5885 {
5886 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5887 	i40e_status ret;
5888 	int i;
5889 
5890 	bw_data.tc_valid_bits = ch->enabled_tc;
5891 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5892 		bw_data.tc_bw_credits[i] = bw_share[i];
5893 
5894 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5895 				       &bw_data, NULL);
5896 	if (ret) {
5897 		dev_info(&vsi->back->pdev->dev,
5898 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5899 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5900 		return -EINVAL;
5901 	}
5902 
5903 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5904 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5905 
5906 	return 0;
5907 }
5908 
5909 /**
5910  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5911  * @pf: ptr to PF device
5912  * @vsi: the VSI being setup
5913  * @ch: ptr to channel structure
5914  *
5915  * Configure TX rings associated with channel (VSI) since queues are being
5916  * from parent VSI.
5917  **/
5918 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5919 				       struct i40e_vsi *vsi,
5920 				       struct i40e_channel *ch)
5921 {
5922 	i40e_status ret;
5923 	int i;
5924 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5925 
5926 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5927 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5928 		if (ch->enabled_tc & BIT(i))
5929 			bw_share[i] = 1;
5930 	}
5931 
5932 	/* configure BW for new VSI */
5933 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5934 	if (ret) {
5935 		dev_info(&vsi->back->pdev->dev,
5936 			 "Failed configuring TC map %d for channel (seid %u)\n",
5937 			 ch->enabled_tc, ch->seid);
5938 		return ret;
5939 	}
5940 
5941 	for (i = 0; i < ch->num_queue_pairs; i++) {
5942 		struct i40e_ring *tx_ring, *rx_ring;
5943 		u16 pf_q;
5944 
5945 		pf_q = ch->base_queue + i;
5946 
5947 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5948 		 * context
5949 		 */
5950 		tx_ring = vsi->tx_rings[pf_q];
5951 		tx_ring->ch = ch;
5952 
5953 		/* Get the RX ring ptr */
5954 		rx_ring = vsi->rx_rings[pf_q];
5955 		rx_ring->ch = ch;
5956 	}
5957 
5958 	return 0;
5959 }
5960 
5961 /**
5962  * i40e_setup_hw_channel - setup new channel
5963  * @pf: ptr to PF device
5964  * @vsi: the VSI being setup
5965  * @ch: ptr to channel structure
5966  * @uplink_seid: underlying HW switching element (VEB) ID
5967  * @type: type of channel to be created (VMDq2/VF)
5968  *
5969  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5970  * and configures TX rings accordingly
5971  **/
5972 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5973 					struct i40e_vsi *vsi,
5974 					struct i40e_channel *ch,
5975 					u16 uplink_seid, u8 type)
5976 {
5977 	int ret;
5978 
5979 	ch->initialized = false;
5980 	ch->base_queue = vsi->next_base_queue;
5981 	ch->type = type;
5982 
5983 	/* Proceed with creation of channel (VMDq2) VSI */
5984 	ret = i40e_add_channel(pf, uplink_seid, ch);
5985 	if (ret) {
5986 		dev_info(&pf->pdev->dev,
5987 			 "failed to add_channel using uplink_seid %u\n",
5988 			 uplink_seid);
5989 		return ret;
5990 	}
5991 
5992 	/* Mark the successful creation of channel */
5993 	ch->initialized = true;
5994 
5995 	/* Reconfigure TX queues using QTX_CTL register */
5996 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5997 	if (ret) {
5998 		dev_info(&pf->pdev->dev,
5999 			 "failed to configure TX rings for channel %u\n",
6000 			 ch->seid);
6001 		return ret;
6002 	}
6003 
6004 	/* update 'next_base_queue' */
6005 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6006 	dev_dbg(&pf->pdev->dev,
6007 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6008 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6009 		ch->num_queue_pairs,
6010 		vsi->next_base_queue);
6011 	return ret;
6012 }
6013 
6014 /**
6015  * i40e_setup_channel - setup new channel using uplink element
6016  * @pf: ptr to PF device
6017  * @type: type of channel to be created (VMDq2/VF)
6018  * @uplink_seid: underlying HW switching element (VEB) ID
6019  * @ch: ptr to channel structure
6020  *
6021  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6022  * and uplink switching element (uplink_seid)
6023  **/
6024 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6025 			       struct i40e_channel *ch)
6026 {
6027 	u8 vsi_type;
6028 	u16 seid;
6029 	int ret;
6030 
6031 	if (vsi->type == I40E_VSI_MAIN) {
6032 		vsi_type = I40E_VSI_VMDQ2;
6033 	} else {
6034 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6035 			vsi->type);
6036 		return false;
6037 	}
6038 
6039 	/* underlying switching element */
6040 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6041 
6042 	/* create channel (VSI), configure TX rings */
6043 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6044 	if (ret) {
6045 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6046 		return false;
6047 	}
6048 
6049 	return ch->initialized ? true : false;
6050 }
6051 
6052 /**
6053  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6054  * @vsi: ptr to VSI which has PF backing
6055  *
6056  * Sets up switch mode correctly if it needs to be changed and perform
6057  * what are allowed modes.
6058  **/
6059 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6060 {
6061 	u8 mode;
6062 	struct i40e_pf *pf = vsi->back;
6063 	struct i40e_hw *hw = &pf->hw;
6064 	int ret;
6065 
6066 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6067 	if (ret)
6068 		return -EINVAL;
6069 
6070 	if (hw->dev_caps.switch_mode) {
6071 		/* if switch mode is set, support mode2 (non-tunneled for
6072 		 * cloud filter) for now
6073 		 */
6074 		u32 switch_mode = hw->dev_caps.switch_mode &
6075 				  I40E_SWITCH_MODE_MASK;
6076 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6077 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6078 				return 0;
6079 			dev_err(&pf->pdev->dev,
6080 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6081 				hw->dev_caps.switch_mode);
6082 			return -EINVAL;
6083 		}
6084 	}
6085 
6086 	/* Set Bit 7 to be valid */
6087 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6088 
6089 	/* Set L4type for TCP support */
6090 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6091 
6092 	/* Set cloud filter mode */
6093 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6094 
6095 	/* Prep mode field for set_switch_config */
6096 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6097 					pf->last_sw_conf_valid_flags,
6098 					mode, NULL);
6099 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6100 		dev_err(&pf->pdev->dev,
6101 			"couldn't set switch config bits, err %s aq_err %s\n",
6102 			i40e_stat_str(hw, ret),
6103 			i40e_aq_str(hw,
6104 				    hw->aq.asq_last_status));
6105 
6106 	return ret;
6107 }
6108 
6109 /**
6110  * i40e_create_queue_channel - function to create channel
6111  * @vsi: VSI to be configured
6112  * @ch: ptr to channel (it contains channel specific params)
6113  *
6114  * This function creates channel (VSI) using num_queues specified by user,
6115  * reconfigs RSS if needed.
6116  **/
6117 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6118 			      struct i40e_channel *ch)
6119 {
6120 	struct i40e_pf *pf = vsi->back;
6121 	bool reconfig_rss;
6122 	int err;
6123 
6124 	if (!ch)
6125 		return -EINVAL;
6126 
6127 	if (!ch->num_queue_pairs) {
6128 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6129 			ch->num_queue_pairs);
6130 		return -EINVAL;
6131 	}
6132 
6133 	/* validate user requested num_queues for channel */
6134 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6135 				       &reconfig_rss);
6136 	if (err) {
6137 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6138 			 ch->num_queue_pairs);
6139 		return -EINVAL;
6140 	}
6141 
6142 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6143 	 * VSI to be added switch to VEB mode.
6144 	 */
6145 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6146 	    (!i40e_is_any_channel(vsi))) {
6147 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6148 			dev_dbg(&pf->pdev->dev,
6149 				"Failed to create channel. Override queues (%u) not power of 2\n",
6150 				vsi->tc_config.tc_info[0].qcount);
6151 			return -EINVAL;
6152 		}
6153 
6154 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6155 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6156 
6157 			if (vsi->type == I40E_VSI_MAIN) {
6158 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6159 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6160 						      true);
6161 				else
6162 					i40e_do_reset_safe(pf,
6163 							   I40E_PF_RESET_FLAG);
6164 			}
6165 		}
6166 		/* now onwards for main VSI, number of queues will be value
6167 		 * of TC0's queue count
6168 		 */
6169 	}
6170 
6171 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6172 	 * it should be more than num_queues
6173 	 */
6174 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6175 		dev_dbg(&pf->pdev->dev,
6176 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6177 			vsi->cnt_q_avail, ch->num_queue_pairs);
6178 		return -EINVAL;
6179 	}
6180 
6181 	/* reconfig_rss only if vsi type is MAIN_VSI */
6182 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6183 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6184 		if (err) {
6185 			dev_info(&pf->pdev->dev,
6186 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6187 				 ch->num_queue_pairs);
6188 			return -EINVAL;
6189 		}
6190 	}
6191 
6192 	if (!i40e_setup_channel(pf, vsi, ch)) {
6193 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6194 		return -EINVAL;
6195 	}
6196 
6197 	dev_info(&pf->pdev->dev,
6198 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6199 		 ch->seid, ch->num_queue_pairs);
6200 
6201 	/* configure VSI for BW limit */
6202 	if (ch->max_tx_rate) {
6203 		u64 credits = ch->max_tx_rate;
6204 
6205 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6206 			return -EINVAL;
6207 
6208 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6209 		dev_dbg(&pf->pdev->dev,
6210 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6211 			ch->max_tx_rate,
6212 			credits,
6213 			ch->seid);
6214 	}
6215 
6216 	/* in case of VF, this will be main SRIOV VSI */
6217 	ch->parent_vsi = vsi;
6218 
6219 	/* and update main_vsi's count for queue_available to use */
6220 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6221 
6222 	return 0;
6223 }
6224 
6225 /**
6226  * i40e_configure_queue_channels - Add queue channel for the given TCs
6227  * @vsi: VSI to be configured
6228  *
6229  * Configures queue channel mapping to the given TCs
6230  **/
6231 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6232 {
6233 	struct i40e_channel *ch;
6234 	u64 max_rate = 0;
6235 	int ret = 0, i;
6236 
6237 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6238 	vsi->tc_seid_map[0] = vsi->seid;
6239 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6240 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6241 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6242 			if (!ch) {
6243 				ret = -ENOMEM;
6244 				goto err_free;
6245 			}
6246 
6247 			INIT_LIST_HEAD(&ch->list);
6248 			ch->num_queue_pairs =
6249 				vsi->tc_config.tc_info[i].qcount;
6250 			ch->base_queue =
6251 				vsi->tc_config.tc_info[i].qoffset;
6252 
6253 			/* Bandwidth limit through tc interface is in bytes/s,
6254 			 * change to Mbit/s
6255 			 */
6256 			max_rate = vsi->mqprio_qopt.max_rate[i];
6257 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6258 			ch->max_tx_rate = max_rate;
6259 
6260 			list_add_tail(&ch->list, &vsi->ch_list);
6261 
6262 			ret = i40e_create_queue_channel(vsi, ch);
6263 			if (ret) {
6264 				dev_err(&vsi->back->pdev->dev,
6265 					"Failed creating queue channel with TC%d: queues %d\n",
6266 					i, ch->num_queue_pairs);
6267 				goto err_free;
6268 			}
6269 			vsi->tc_seid_map[i] = ch->seid;
6270 		}
6271 	}
6272 	return ret;
6273 
6274 err_free:
6275 	i40e_remove_queue_channels(vsi);
6276 	return ret;
6277 }
6278 
6279 /**
6280  * i40e_veb_config_tc - Configure TCs for given VEB
6281  * @veb: given VEB
6282  * @enabled_tc: TC bitmap
6283  *
6284  * Configures given TC bitmap for VEB (switching) element
6285  **/
6286 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6287 {
6288 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6289 	struct i40e_pf *pf = veb->pf;
6290 	int ret = 0;
6291 	int i;
6292 
6293 	/* No TCs or already enabled TCs just return */
6294 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6295 		return ret;
6296 
6297 	bw_data.tc_valid_bits = enabled_tc;
6298 	/* bw_data.absolute_credits is not set (relative) */
6299 
6300 	/* Enable ETS TCs with equal BW Share for now */
6301 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6302 		if (enabled_tc & BIT(i))
6303 			bw_data.tc_bw_share_credits[i] = 1;
6304 	}
6305 
6306 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6307 						   &bw_data, NULL);
6308 	if (ret) {
6309 		dev_info(&pf->pdev->dev,
6310 			 "VEB bw config failed, err %s aq_err %s\n",
6311 			 i40e_stat_str(&pf->hw, ret),
6312 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6313 		goto out;
6314 	}
6315 
6316 	/* Update the BW information */
6317 	ret = i40e_veb_get_bw_info(veb);
6318 	if (ret) {
6319 		dev_info(&pf->pdev->dev,
6320 			 "Failed getting veb bw config, err %s aq_err %s\n",
6321 			 i40e_stat_str(&pf->hw, ret),
6322 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6323 	}
6324 
6325 out:
6326 	return ret;
6327 }
6328 
6329 #ifdef CONFIG_I40E_DCB
6330 /**
6331  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6332  * @pf: PF struct
6333  *
6334  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6335  * the caller would've quiesce all the VSIs before calling
6336  * this function
6337  **/
6338 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6339 {
6340 	u8 tc_map = 0;
6341 	int ret;
6342 	u8 v;
6343 
6344 	/* Enable the TCs available on PF to all VEBs */
6345 	tc_map = i40e_pf_get_tc_map(pf);
6346 	for (v = 0; v < I40E_MAX_VEB; v++) {
6347 		if (!pf->veb[v])
6348 			continue;
6349 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6350 		if (ret) {
6351 			dev_info(&pf->pdev->dev,
6352 				 "Failed configuring TC for VEB seid=%d\n",
6353 				 pf->veb[v]->seid);
6354 			/* Will try to configure as many components */
6355 		}
6356 	}
6357 
6358 	/* Update each VSI */
6359 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6360 		if (!pf->vsi[v])
6361 			continue;
6362 
6363 		/* - Enable all TCs for the LAN VSI
6364 		 * - For all others keep them at TC0 for now
6365 		 */
6366 		if (v == pf->lan_vsi)
6367 			tc_map = i40e_pf_get_tc_map(pf);
6368 		else
6369 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6370 
6371 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6372 		if (ret) {
6373 			dev_info(&pf->pdev->dev,
6374 				 "Failed configuring TC for VSI seid=%d\n",
6375 				 pf->vsi[v]->seid);
6376 			/* Will try to configure as many components */
6377 		} else {
6378 			/* Re-configure VSI vectors based on updated TC map */
6379 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6380 			if (pf->vsi[v]->netdev)
6381 				i40e_dcbnl_set_all(pf->vsi[v]);
6382 		}
6383 	}
6384 }
6385 
6386 /**
6387  * i40e_resume_port_tx - Resume port Tx
6388  * @pf: PF struct
6389  *
6390  * Resume a port's Tx and issue a PF reset in case of failure to
6391  * resume.
6392  **/
6393 static int i40e_resume_port_tx(struct i40e_pf *pf)
6394 {
6395 	struct i40e_hw *hw = &pf->hw;
6396 	int ret;
6397 
6398 	ret = i40e_aq_resume_port_tx(hw, NULL);
6399 	if (ret) {
6400 		dev_info(&pf->pdev->dev,
6401 			 "Resume Port Tx failed, err %s aq_err %s\n",
6402 			  i40e_stat_str(&pf->hw, ret),
6403 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6404 		/* Schedule PF reset to recover */
6405 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6406 		i40e_service_event_schedule(pf);
6407 	}
6408 
6409 	return ret;
6410 }
6411 
6412 /**
6413  * i40e_init_pf_dcb - Initialize DCB configuration
6414  * @pf: PF being configured
6415  *
6416  * Query the current DCB configuration and cache it
6417  * in the hardware structure
6418  **/
6419 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6420 {
6421 	struct i40e_hw *hw = &pf->hw;
6422 	int err = 0;
6423 
6424 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6425 	 * Also do not enable DCBx if FW LLDP agent is disabled
6426 	 */
6427 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6428 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
6429 		goto out;
6430 
6431 	/* Get the initial DCB configuration */
6432 	err = i40e_init_dcb(hw, true);
6433 	if (!err) {
6434 		/* Device/Function is not DCBX capable */
6435 		if ((!hw->func_caps.dcb) ||
6436 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6437 			dev_info(&pf->pdev->dev,
6438 				 "DCBX offload is not supported or is disabled for this PF.\n");
6439 		} else {
6440 			/* When status is not DISABLED then DCBX in FW */
6441 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6442 				       DCB_CAP_DCBX_VER_IEEE;
6443 
6444 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6445 			/* Enable DCB tagging only when more than one TC
6446 			 * or explicitly disable if only one TC
6447 			 */
6448 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6449 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6450 			else
6451 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6452 			dev_dbg(&pf->pdev->dev,
6453 				"DCBX offload is supported for this PF.\n");
6454 		}
6455 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6456 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6457 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6458 	} else {
6459 		dev_info(&pf->pdev->dev,
6460 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6461 			 i40e_stat_str(&pf->hw, err),
6462 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6463 	}
6464 
6465 out:
6466 	return err;
6467 }
6468 #endif /* CONFIG_I40E_DCB */
6469 #define SPEED_SIZE 14
6470 #define FC_SIZE 8
6471 /**
6472  * i40e_print_link_message - print link up or down
6473  * @vsi: the VSI for which link needs a message
6474  * @isup: true of link is up, false otherwise
6475  */
6476 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6477 {
6478 	enum i40e_aq_link_speed new_speed;
6479 	struct i40e_pf *pf = vsi->back;
6480 	char *speed = "Unknown";
6481 	char *fc = "Unknown";
6482 	char *fec = "";
6483 	char *req_fec = "";
6484 	char *an = "";
6485 
6486 	if (isup)
6487 		new_speed = pf->hw.phy.link_info.link_speed;
6488 	else
6489 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6490 
6491 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6492 		return;
6493 	vsi->current_isup = isup;
6494 	vsi->current_speed = new_speed;
6495 	if (!isup) {
6496 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6497 		return;
6498 	}
6499 
6500 	/* Warn user if link speed on NPAR enabled partition is not at
6501 	 * least 10GB
6502 	 */
6503 	if (pf->hw.func_caps.npar_enable &&
6504 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6505 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6506 		netdev_warn(vsi->netdev,
6507 			    "The partition detected link speed that is less than 10Gbps\n");
6508 
6509 	switch (pf->hw.phy.link_info.link_speed) {
6510 	case I40E_LINK_SPEED_40GB:
6511 		speed = "40 G";
6512 		break;
6513 	case I40E_LINK_SPEED_20GB:
6514 		speed = "20 G";
6515 		break;
6516 	case I40E_LINK_SPEED_25GB:
6517 		speed = "25 G";
6518 		break;
6519 	case I40E_LINK_SPEED_10GB:
6520 		speed = "10 G";
6521 		break;
6522 	case I40E_LINK_SPEED_5GB:
6523 		speed = "5 G";
6524 		break;
6525 	case I40E_LINK_SPEED_2_5GB:
6526 		speed = "2.5 G";
6527 		break;
6528 	case I40E_LINK_SPEED_1GB:
6529 		speed = "1000 M";
6530 		break;
6531 	case I40E_LINK_SPEED_100MB:
6532 		speed = "100 M";
6533 		break;
6534 	default:
6535 		break;
6536 	}
6537 
6538 	switch (pf->hw.fc.current_mode) {
6539 	case I40E_FC_FULL:
6540 		fc = "RX/TX";
6541 		break;
6542 	case I40E_FC_TX_PAUSE:
6543 		fc = "TX";
6544 		break;
6545 	case I40E_FC_RX_PAUSE:
6546 		fc = "RX";
6547 		break;
6548 	default:
6549 		fc = "None";
6550 		break;
6551 	}
6552 
6553 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6554 		req_fec = ", Requested FEC: None";
6555 		fec = ", FEC: None";
6556 		an = ", Autoneg: False";
6557 
6558 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6559 			an = ", Autoneg: True";
6560 
6561 		if (pf->hw.phy.link_info.fec_info &
6562 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6563 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6564 		else if (pf->hw.phy.link_info.fec_info &
6565 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6566 			fec = ", FEC: CL108 RS-FEC";
6567 
6568 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6569 		 * both RS and FC are requested
6570 		 */
6571 		if (vsi->back->hw.phy.link_info.req_fec_info &
6572 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6573 			if (vsi->back->hw.phy.link_info.req_fec_info &
6574 			    I40E_AQ_REQUEST_FEC_RS)
6575 				req_fec = ", Requested FEC: CL108 RS-FEC";
6576 			else
6577 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6578 		}
6579 	}
6580 
6581 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6582 		    speed, req_fec, fec, an, fc);
6583 }
6584 
6585 /**
6586  * i40e_up_complete - Finish the last steps of bringing up a connection
6587  * @vsi: the VSI being configured
6588  **/
6589 static int i40e_up_complete(struct i40e_vsi *vsi)
6590 {
6591 	struct i40e_pf *pf = vsi->back;
6592 	int err;
6593 
6594 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6595 		i40e_vsi_configure_msix(vsi);
6596 	else
6597 		i40e_configure_msi_and_legacy(vsi);
6598 
6599 	/* start rings */
6600 	err = i40e_vsi_start_rings(vsi);
6601 	if (err)
6602 		return err;
6603 
6604 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6605 	i40e_napi_enable_all(vsi);
6606 	i40e_vsi_enable_irq(vsi);
6607 
6608 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6609 	    (vsi->netdev)) {
6610 		i40e_print_link_message(vsi, true);
6611 		netif_tx_start_all_queues(vsi->netdev);
6612 		netif_carrier_on(vsi->netdev);
6613 	}
6614 
6615 	/* replay FDIR SB filters */
6616 	if (vsi->type == I40E_VSI_FDIR) {
6617 		/* reset fd counters */
6618 		pf->fd_add_err = 0;
6619 		pf->fd_atr_cnt = 0;
6620 		i40e_fdir_filter_restore(vsi);
6621 	}
6622 
6623 	/* On the next run of the service_task, notify any clients of the new
6624 	 * opened netdev
6625 	 */
6626 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6627 	i40e_service_event_schedule(pf);
6628 
6629 	return 0;
6630 }
6631 
6632 /**
6633  * i40e_vsi_reinit_locked - Reset the VSI
6634  * @vsi: the VSI being configured
6635  *
6636  * Rebuild the ring structs after some configuration
6637  * has changed, e.g. MTU size.
6638  **/
6639 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6640 {
6641 	struct i40e_pf *pf = vsi->back;
6642 
6643 	WARN_ON(in_interrupt());
6644 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6645 		usleep_range(1000, 2000);
6646 	i40e_down(vsi);
6647 
6648 	i40e_up(vsi);
6649 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6650 }
6651 
6652 /**
6653  * i40e_up - Bring the connection back up after being down
6654  * @vsi: the VSI being configured
6655  **/
6656 int i40e_up(struct i40e_vsi *vsi)
6657 {
6658 	int err;
6659 
6660 	err = i40e_vsi_configure(vsi);
6661 	if (!err)
6662 		err = i40e_up_complete(vsi);
6663 
6664 	return err;
6665 }
6666 
6667 /**
6668  * i40e_force_link_state - Force the link status
6669  * @pf: board private structure
6670  * @is_up: whether the link state should be forced up or down
6671  **/
6672 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6673 {
6674 	struct i40e_aq_get_phy_abilities_resp abilities;
6675 	struct i40e_aq_set_phy_config config = {0};
6676 	struct i40e_hw *hw = &pf->hw;
6677 	i40e_status err;
6678 	u64 mask;
6679 	u8 speed;
6680 
6681 	/* Card might've been put in an unstable state by other drivers
6682 	 * and applications, which causes incorrect speed values being
6683 	 * set on startup. In order to clear speed registers, we call
6684 	 * get_phy_capabilities twice, once to get initial state of
6685 	 * available speeds, and once to get current PHY config.
6686 	 */
6687 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6688 					   NULL);
6689 	if (err) {
6690 		dev_err(&pf->pdev->dev,
6691 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6692 			i40e_stat_str(hw, err),
6693 			i40e_aq_str(hw, hw->aq.asq_last_status));
6694 		return err;
6695 	}
6696 	speed = abilities.link_speed;
6697 
6698 	/* Get the current phy config */
6699 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6700 					   NULL);
6701 	if (err) {
6702 		dev_err(&pf->pdev->dev,
6703 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6704 			i40e_stat_str(hw, err),
6705 			i40e_aq_str(hw, hw->aq.asq_last_status));
6706 		return err;
6707 	}
6708 
6709 	/* If link needs to go up, but was not forced to go down,
6710 	 * and its speed values are OK, no need for a flap
6711 	 */
6712 	if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6713 		return I40E_SUCCESS;
6714 
6715 	/* To force link we need to set bits for all supported PHY types,
6716 	 * but there are now more than 32, so we need to split the bitmap
6717 	 * across two fields.
6718 	 */
6719 	mask = I40E_PHY_TYPES_BITMASK;
6720 	config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6721 	config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6722 	/* Copy the old settings, except of phy_type */
6723 	config.abilities = abilities.abilities;
6724 	if (abilities.link_speed != 0)
6725 		config.link_speed = abilities.link_speed;
6726 	else
6727 		config.link_speed = speed;
6728 	config.eee_capability = abilities.eee_capability;
6729 	config.eeer = abilities.eeer_val;
6730 	config.low_power_ctrl = abilities.d3_lpan;
6731 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6732 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6733 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6734 
6735 	if (err) {
6736 		dev_err(&pf->pdev->dev,
6737 			"set phy config ret =  %s last_status =  %s\n",
6738 			i40e_stat_str(&pf->hw, err),
6739 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6740 		return err;
6741 	}
6742 
6743 	/* Update the link info */
6744 	err = i40e_update_link_info(hw);
6745 	if (err) {
6746 		/* Wait a little bit (on 40G cards it sometimes takes a really
6747 		 * long time for link to come back from the atomic reset)
6748 		 * and try once more
6749 		 */
6750 		msleep(1000);
6751 		i40e_update_link_info(hw);
6752 	}
6753 
6754 	i40e_aq_set_link_restart_an(hw, true, NULL);
6755 
6756 	return I40E_SUCCESS;
6757 }
6758 
6759 /**
6760  * i40e_down - Shutdown the connection processing
6761  * @vsi: the VSI being stopped
6762  **/
6763 void i40e_down(struct i40e_vsi *vsi)
6764 {
6765 	int i;
6766 
6767 	/* It is assumed that the caller of this function
6768 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6769 	 */
6770 	if (vsi->netdev) {
6771 		netif_carrier_off(vsi->netdev);
6772 		netif_tx_disable(vsi->netdev);
6773 	}
6774 	i40e_vsi_disable_irq(vsi);
6775 	i40e_vsi_stop_rings(vsi);
6776 	if (vsi->type == I40E_VSI_MAIN &&
6777 	    vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6778 		i40e_force_link_state(vsi->back, false);
6779 	i40e_napi_disable_all(vsi);
6780 
6781 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6782 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6783 		if (i40e_enabled_xdp_vsi(vsi)) {
6784 			/* Make sure that in-progress ndo_xdp_xmit
6785 			 * calls are completed.
6786 			 */
6787 			synchronize_rcu();
6788 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6789 		}
6790 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6791 	}
6792 
6793 }
6794 
6795 /**
6796  * i40e_validate_mqprio_qopt- validate queue mapping info
6797  * @vsi: the VSI being configured
6798  * @mqprio_qopt: queue parametrs
6799  **/
6800 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6801 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6802 {
6803 	u64 sum_max_rate = 0;
6804 	u64 max_rate = 0;
6805 	int i;
6806 
6807 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6808 	    mqprio_qopt->qopt.num_tc < 1 ||
6809 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6810 		return -EINVAL;
6811 	for (i = 0; ; i++) {
6812 		if (!mqprio_qopt->qopt.count[i])
6813 			return -EINVAL;
6814 		if (mqprio_qopt->min_rate[i]) {
6815 			dev_err(&vsi->back->pdev->dev,
6816 				"Invalid min tx rate (greater than 0) specified\n");
6817 			return -EINVAL;
6818 		}
6819 		max_rate = mqprio_qopt->max_rate[i];
6820 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6821 		sum_max_rate += max_rate;
6822 
6823 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6824 			break;
6825 		if (mqprio_qopt->qopt.offset[i + 1] !=
6826 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6827 			return -EINVAL;
6828 	}
6829 	if (vsi->num_queue_pairs <
6830 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6831 		return -EINVAL;
6832 	}
6833 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6834 		dev_err(&vsi->back->pdev->dev,
6835 			"Invalid max tx rate specified\n");
6836 		return -EINVAL;
6837 	}
6838 	return 0;
6839 }
6840 
6841 /**
6842  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6843  * @vsi: the VSI being configured
6844  **/
6845 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6846 {
6847 	u16 qcount;
6848 	int i;
6849 
6850 	/* Only TC0 is enabled */
6851 	vsi->tc_config.numtc = 1;
6852 	vsi->tc_config.enabled_tc = 1;
6853 	qcount = min_t(int, vsi->alloc_queue_pairs,
6854 		       i40e_pf_get_max_q_per_tc(vsi->back));
6855 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6856 		/* For the TC that is not enabled set the offset to to default
6857 		 * queue and allocate one queue for the given TC.
6858 		 */
6859 		vsi->tc_config.tc_info[i].qoffset = 0;
6860 		if (i == 0)
6861 			vsi->tc_config.tc_info[i].qcount = qcount;
6862 		else
6863 			vsi->tc_config.tc_info[i].qcount = 1;
6864 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6865 	}
6866 }
6867 
6868 /**
6869  * i40e_setup_tc - configure multiple traffic classes
6870  * @netdev: net device to configure
6871  * @type_data: tc offload data
6872  **/
6873 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6874 {
6875 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6876 	struct i40e_netdev_priv *np = netdev_priv(netdev);
6877 	struct i40e_vsi *vsi = np->vsi;
6878 	struct i40e_pf *pf = vsi->back;
6879 	u8 enabled_tc = 0, num_tc, hw;
6880 	bool need_reset = false;
6881 	int old_queue_pairs;
6882 	int ret = -EINVAL;
6883 	u16 mode;
6884 	int i;
6885 
6886 	old_queue_pairs = vsi->num_queue_pairs;
6887 	num_tc = mqprio_qopt->qopt.num_tc;
6888 	hw = mqprio_qopt->qopt.hw;
6889 	mode = mqprio_qopt->mode;
6890 	if (!hw) {
6891 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6892 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6893 		goto config_tc;
6894 	}
6895 
6896 	/* Check if MFP enabled */
6897 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6898 		netdev_info(netdev,
6899 			    "Configuring TC not supported in MFP mode\n");
6900 		return ret;
6901 	}
6902 	switch (mode) {
6903 	case TC_MQPRIO_MODE_DCB:
6904 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6905 
6906 		/* Check if DCB enabled to continue */
6907 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6908 			netdev_info(netdev,
6909 				    "DCB is not enabled for adapter\n");
6910 			return ret;
6911 		}
6912 
6913 		/* Check whether tc count is within enabled limit */
6914 		if (num_tc > i40e_pf_get_num_tc(pf)) {
6915 			netdev_info(netdev,
6916 				    "TC count greater than enabled on link for adapter\n");
6917 			return ret;
6918 		}
6919 		break;
6920 	case TC_MQPRIO_MODE_CHANNEL:
6921 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6922 			netdev_info(netdev,
6923 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6924 			return ret;
6925 		}
6926 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6927 			return ret;
6928 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6929 		if (ret)
6930 			return ret;
6931 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6932 		       sizeof(*mqprio_qopt));
6933 		pf->flags |= I40E_FLAG_TC_MQPRIO;
6934 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6935 		break;
6936 	default:
6937 		return -EINVAL;
6938 	}
6939 
6940 config_tc:
6941 	/* Generate TC map for number of tc requested */
6942 	for (i = 0; i < num_tc; i++)
6943 		enabled_tc |= BIT(i);
6944 
6945 	/* Requesting same TC configuration as already enabled */
6946 	if (enabled_tc == vsi->tc_config.enabled_tc &&
6947 	    mode != TC_MQPRIO_MODE_CHANNEL)
6948 		return 0;
6949 
6950 	/* Quiesce VSI queues */
6951 	i40e_quiesce_vsi(vsi);
6952 
6953 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6954 		i40e_remove_queue_channels(vsi);
6955 
6956 	/* Configure VSI for enabled TCs */
6957 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
6958 	if (ret) {
6959 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6960 			    vsi->seid);
6961 		need_reset = true;
6962 		goto exit;
6963 	}
6964 
6965 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6966 		if (vsi->mqprio_qopt.max_rate[0]) {
6967 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6968 
6969 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6970 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6971 			if (!ret) {
6972 				u64 credits = max_tx_rate;
6973 
6974 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
6975 				dev_dbg(&vsi->back->pdev->dev,
6976 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6977 					max_tx_rate,
6978 					credits,
6979 					vsi->seid);
6980 			} else {
6981 				need_reset = true;
6982 				goto exit;
6983 			}
6984 		}
6985 		ret = i40e_configure_queue_channels(vsi);
6986 		if (ret) {
6987 			vsi->num_queue_pairs = old_queue_pairs;
6988 			netdev_info(netdev,
6989 				    "Failed configuring queue channels\n");
6990 			need_reset = true;
6991 			goto exit;
6992 		}
6993 	}
6994 
6995 exit:
6996 	/* Reset the configuration data to defaults, only TC0 is enabled */
6997 	if (need_reset) {
6998 		i40e_vsi_set_default_tc_config(vsi);
6999 		need_reset = false;
7000 	}
7001 
7002 	/* Unquiesce VSI */
7003 	i40e_unquiesce_vsi(vsi);
7004 	return ret;
7005 }
7006 
7007 /**
7008  * i40e_set_cld_element - sets cloud filter element data
7009  * @filter: cloud filter rule
7010  * @cld: ptr to cloud filter element data
7011  *
7012  * This is helper function to copy data into cloud filter element
7013  **/
7014 static inline void
7015 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7016 		     struct i40e_aqc_cloud_filters_element_data *cld)
7017 {
7018 	int i, j;
7019 	u32 ipa;
7020 
7021 	memset(cld, 0, sizeof(*cld));
7022 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7023 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7024 
7025 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7026 		return;
7027 
7028 	if (filter->n_proto == ETH_P_IPV6) {
7029 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7030 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
7031 		     i++, j += 2) {
7032 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7033 			ipa = cpu_to_le32(ipa);
7034 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
7035 		}
7036 	} else {
7037 		ipa = be32_to_cpu(filter->dst_ipv4);
7038 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7039 	}
7040 
7041 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7042 
7043 	/* tenant_id is not supported by FW now, once the support is enabled
7044 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7045 	 */
7046 	if (filter->tenant_id)
7047 		return;
7048 }
7049 
7050 /**
7051  * i40e_add_del_cloud_filter - Add/del cloud filter
7052  * @vsi: pointer to VSI
7053  * @filter: cloud filter rule
7054  * @add: if true, add, if false, delete
7055  *
7056  * Add or delete a cloud filter for a specific flow spec.
7057  * Returns 0 if the filter were successfully added.
7058  **/
7059 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7060 			      struct i40e_cloud_filter *filter, bool add)
7061 {
7062 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7063 	struct i40e_pf *pf = vsi->back;
7064 	int ret;
7065 	static const u16 flag_table[128] = {
7066 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7067 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7068 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7069 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7070 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7071 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7072 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7073 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7074 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7075 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7076 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7077 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7078 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7079 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7080 	};
7081 
7082 	if (filter->flags >= ARRAY_SIZE(flag_table))
7083 		return I40E_ERR_CONFIG;
7084 
7085 	/* copy element needed to add cloud filter from filter */
7086 	i40e_set_cld_element(filter, &cld_filter);
7087 
7088 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7089 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7090 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7091 
7092 	if (filter->n_proto == ETH_P_IPV6)
7093 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7094 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7095 	else
7096 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7097 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7098 
7099 	if (add)
7100 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7101 						&cld_filter, 1);
7102 	else
7103 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7104 						&cld_filter, 1);
7105 	if (ret)
7106 		dev_dbg(&pf->pdev->dev,
7107 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7108 			add ? "add" : "delete", filter->dst_port, ret,
7109 			pf->hw.aq.asq_last_status);
7110 	else
7111 		dev_info(&pf->pdev->dev,
7112 			 "%s cloud filter for VSI: %d\n",
7113 			 add ? "Added" : "Deleted", filter->seid);
7114 	return ret;
7115 }
7116 
7117 /**
7118  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7119  * @vsi: pointer to VSI
7120  * @filter: cloud filter rule
7121  * @add: if true, add, if false, delete
7122  *
7123  * Add or delete a cloud filter for a specific flow spec using big buffer.
7124  * Returns 0 if the filter were successfully added.
7125  **/
7126 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7127 				      struct i40e_cloud_filter *filter,
7128 				      bool add)
7129 {
7130 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7131 	struct i40e_pf *pf = vsi->back;
7132 	int ret;
7133 
7134 	/* Both (src/dst) valid mac_addr are not supported */
7135 	if ((is_valid_ether_addr(filter->dst_mac) &&
7136 	     is_valid_ether_addr(filter->src_mac)) ||
7137 	    (is_multicast_ether_addr(filter->dst_mac) &&
7138 	     is_multicast_ether_addr(filter->src_mac)))
7139 		return -EOPNOTSUPP;
7140 
7141 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7142 	 * ports are not supported via big buffer now.
7143 	 */
7144 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7145 		return -EOPNOTSUPP;
7146 
7147 	/* adding filter using src_port/src_ip is not supported at this stage */
7148 	if (filter->src_port || filter->src_ipv4 ||
7149 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7150 		return -EOPNOTSUPP;
7151 
7152 	/* copy element needed to add cloud filter from filter */
7153 	i40e_set_cld_element(filter, &cld_filter.element);
7154 
7155 	if (is_valid_ether_addr(filter->dst_mac) ||
7156 	    is_valid_ether_addr(filter->src_mac) ||
7157 	    is_multicast_ether_addr(filter->dst_mac) ||
7158 	    is_multicast_ether_addr(filter->src_mac)) {
7159 		/* MAC + IP : unsupported mode */
7160 		if (filter->dst_ipv4)
7161 			return -EOPNOTSUPP;
7162 
7163 		/* since we validated that L4 port must be valid before
7164 		 * we get here, start with respective "flags" value
7165 		 * and update if vlan is present or not
7166 		 */
7167 		cld_filter.element.flags =
7168 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7169 
7170 		if (filter->vlan_id) {
7171 			cld_filter.element.flags =
7172 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7173 		}
7174 
7175 	} else if (filter->dst_ipv4 ||
7176 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7177 		cld_filter.element.flags =
7178 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7179 		if (filter->n_proto == ETH_P_IPV6)
7180 			cld_filter.element.flags |=
7181 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7182 		else
7183 			cld_filter.element.flags |=
7184 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7185 	} else {
7186 		dev_err(&pf->pdev->dev,
7187 			"either mac or ip has to be valid for cloud filter\n");
7188 		return -EINVAL;
7189 	}
7190 
7191 	/* Now copy L4 port in Byte 6..7 in general fields */
7192 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7193 						be16_to_cpu(filter->dst_port);
7194 
7195 	if (add) {
7196 		/* Validate current device switch mode, change if necessary */
7197 		ret = i40e_validate_and_set_switch_mode(vsi);
7198 		if (ret) {
7199 			dev_err(&pf->pdev->dev,
7200 				"failed to set switch mode, ret %d\n",
7201 				ret);
7202 			return ret;
7203 		}
7204 
7205 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7206 						   &cld_filter, 1);
7207 	} else {
7208 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7209 						   &cld_filter, 1);
7210 	}
7211 
7212 	if (ret)
7213 		dev_dbg(&pf->pdev->dev,
7214 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7215 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7216 	else
7217 		dev_info(&pf->pdev->dev,
7218 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7219 			 add ? "add" : "delete", filter->seid,
7220 			 ntohs(filter->dst_port));
7221 	return ret;
7222 }
7223 
7224 /**
7225  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7226  * @vsi: Pointer to VSI
7227  * @cls_flower: Pointer to struct tc_cls_flower_offload
7228  * @filter: Pointer to cloud filter structure
7229  *
7230  **/
7231 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7232 				 struct tc_cls_flower_offload *f,
7233 				 struct i40e_cloud_filter *filter)
7234 {
7235 	struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
7236 	struct flow_dissector *dissector = rule->match.dissector;
7237 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7238 	struct i40e_pf *pf = vsi->back;
7239 	u8 field_flags = 0;
7240 
7241 	if (dissector->used_keys &
7242 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7243 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7244 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7245 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7246 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7247 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7248 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7249 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7250 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7251 			dissector->used_keys);
7252 		return -EOPNOTSUPP;
7253 	}
7254 
7255 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7256 		struct flow_match_enc_keyid match;
7257 
7258 		flow_rule_match_enc_keyid(rule, &match);
7259 		if (match.mask->keyid != 0)
7260 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7261 
7262 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7263 	}
7264 
7265 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7266 		struct flow_match_basic match;
7267 
7268 		flow_rule_match_basic(rule, &match);
7269 		n_proto_key = ntohs(match.key->n_proto);
7270 		n_proto_mask = ntohs(match.mask->n_proto);
7271 
7272 		if (n_proto_key == ETH_P_ALL) {
7273 			n_proto_key = 0;
7274 			n_proto_mask = 0;
7275 		}
7276 		filter->n_proto = n_proto_key & n_proto_mask;
7277 		filter->ip_proto = match.key->ip_proto;
7278 	}
7279 
7280 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7281 		struct flow_match_eth_addrs match;
7282 
7283 		flow_rule_match_eth_addrs(rule, &match);
7284 
7285 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7286 		if (!is_zero_ether_addr(match.mask->dst)) {
7287 			if (is_broadcast_ether_addr(match.mask->dst)) {
7288 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7289 			} else {
7290 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7291 					match.mask->dst);
7292 				return I40E_ERR_CONFIG;
7293 			}
7294 		}
7295 
7296 		if (!is_zero_ether_addr(match.mask->src)) {
7297 			if (is_broadcast_ether_addr(match.mask->src)) {
7298 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7299 			} else {
7300 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7301 					match.mask->src);
7302 				return I40E_ERR_CONFIG;
7303 			}
7304 		}
7305 		ether_addr_copy(filter->dst_mac, match.key->dst);
7306 		ether_addr_copy(filter->src_mac, match.key->src);
7307 	}
7308 
7309 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7310 		struct flow_match_vlan match;
7311 
7312 		flow_rule_match_vlan(rule, &match);
7313 		if (match.mask->vlan_id) {
7314 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7315 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7316 
7317 			} else {
7318 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7319 					match.mask->vlan_id);
7320 				return I40E_ERR_CONFIG;
7321 			}
7322 		}
7323 
7324 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7325 	}
7326 
7327 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7328 		struct flow_match_control match;
7329 
7330 		flow_rule_match_control(rule, &match);
7331 		addr_type = match.key->addr_type;
7332 	}
7333 
7334 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7335 		struct flow_match_ipv4_addrs match;
7336 
7337 		flow_rule_match_ipv4_addrs(rule, &match);
7338 		if (match.mask->dst) {
7339 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7340 				field_flags |= I40E_CLOUD_FIELD_IIP;
7341 			} else {
7342 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7343 					&match.mask->dst);
7344 				return I40E_ERR_CONFIG;
7345 			}
7346 		}
7347 
7348 		if (match.mask->src) {
7349 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7350 				field_flags |= I40E_CLOUD_FIELD_IIP;
7351 			} else {
7352 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7353 					&match.mask->src);
7354 				return I40E_ERR_CONFIG;
7355 			}
7356 		}
7357 
7358 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7359 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7360 			return I40E_ERR_CONFIG;
7361 		}
7362 		filter->dst_ipv4 = match.key->dst;
7363 		filter->src_ipv4 = match.key->src;
7364 	}
7365 
7366 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7367 		struct flow_match_ipv6_addrs match;
7368 
7369 		flow_rule_match_ipv6_addrs(rule, &match);
7370 
7371 		/* src and dest IPV6 address should not be LOOPBACK
7372 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7373 		 */
7374 		if (ipv6_addr_loopback(&match.key->dst) ||
7375 		    ipv6_addr_loopback(&match.key->src)) {
7376 			dev_err(&pf->pdev->dev,
7377 				"Bad ipv6, addr is LOOPBACK\n");
7378 			return I40E_ERR_CONFIG;
7379 		}
7380 		if (!ipv6_addr_any(&match.mask->dst) ||
7381 		    !ipv6_addr_any(&match.mask->src))
7382 			field_flags |= I40E_CLOUD_FIELD_IIP;
7383 
7384 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7385 		       sizeof(filter->src_ipv6));
7386 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7387 		       sizeof(filter->dst_ipv6));
7388 	}
7389 
7390 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7391 		struct flow_match_ports match;
7392 
7393 		flow_rule_match_ports(rule, &match);
7394 		if (match.mask->src) {
7395 			if (match.mask->src == cpu_to_be16(0xffff)) {
7396 				field_flags |= I40E_CLOUD_FIELD_IIP;
7397 			} else {
7398 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7399 					be16_to_cpu(match.mask->src));
7400 				return I40E_ERR_CONFIG;
7401 			}
7402 		}
7403 
7404 		if (match.mask->dst) {
7405 			if (match.mask->dst == cpu_to_be16(0xffff)) {
7406 				field_flags |= I40E_CLOUD_FIELD_IIP;
7407 			} else {
7408 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7409 					be16_to_cpu(match.mask->dst));
7410 				return I40E_ERR_CONFIG;
7411 			}
7412 		}
7413 
7414 		filter->dst_port = match.key->dst;
7415 		filter->src_port = match.key->src;
7416 
7417 		switch (filter->ip_proto) {
7418 		case IPPROTO_TCP:
7419 		case IPPROTO_UDP:
7420 			break;
7421 		default:
7422 			dev_err(&pf->pdev->dev,
7423 				"Only UDP and TCP transport are supported\n");
7424 			return -EINVAL;
7425 		}
7426 	}
7427 	filter->flags = field_flags;
7428 	return 0;
7429 }
7430 
7431 /**
7432  * i40e_handle_tclass: Forward to a traffic class on the device
7433  * @vsi: Pointer to VSI
7434  * @tc: traffic class index on the device
7435  * @filter: Pointer to cloud filter structure
7436  *
7437  **/
7438 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7439 			      struct i40e_cloud_filter *filter)
7440 {
7441 	struct i40e_channel *ch, *ch_tmp;
7442 
7443 	/* direct to a traffic class on the same device */
7444 	if (tc == 0) {
7445 		filter->seid = vsi->seid;
7446 		return 0;
7447 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7448 		if (!filter->dst_port) {
7449 			dev_err(&vsi->back->pdev->dev,
7450 				"Specify destination port to direct to traffic class that is not default\n");
7451 			return -EINVAL;
7452 		}
7453 		if (list_empty(&vsi->ch_list))
7454 			return -EINVAL;
7455 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7456 					 list) {
7457 			if (ch->seid == vsi->tc_seid_map[tc])
7458 				filter->seid = ch->seid;
7459 		}
7460 		return 0;
7461 	}
7462 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7463 	return -EINVAL;
7464 }
7465 
7466 /**
7467  * i40e_configure_clsflower - Configure tc flower filters
7468  * @vsi: Pointer to VSI
7469  * @cls_flower: Pointer to struct tc_cls_flower_offload
7470  *
7471  **/
7472 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7473 				    struct tc_cls_flower_offload *cls_flower)
7474 {
7475 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7476 	struct i40e_cloud_filter *filter = NULL;
7477 	struct i40e_pf *pf = vsi->back;
7478 	int err = 0;
7479 
7480 	if (tc < 0) {
7481 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7482 		return -EOPNOTSUPP;
7483 	}
7484 
7485 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7486 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7487 		return -EBUSY;
7488 
7489 	if (pf->fdir_pf_active_filters ||
7490 	    (!hlist_empty(&pf->fdir_filter_list))) {
7491 		dev_err(&vsi->back->pdev->dev,
7492 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7493 		return -EINVAL;
7494 	}
7495 
7496 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7497 		dev_err(&vsi->back->pdev->dev,
7498 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7499 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7500 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7501 	}
7502 
7503 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7504 	if (!filter)
7505 		return -ENOMEM;
7506 
7507 	filter->cookie = cls_flower->cookie;
7508 
7509 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7510 	if (err < 0)
7511 		goto err;
7512 
7513 	err = i40e_handle_tclass(vsi, tc, filter);
7514 	if (err < 0)
7515 		goto err;
7516 
7517 	/* Add cloud filter */
7518 	if (filter->dst_port)
7519 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7520 	else
7521 		err = i40e_add_del_cloud_filter(vsi, filter, true);
7522 
7523 	if (err) {
7524 		dev_err(&pf->pdev->dev,
7525 			"Failed to add cloud filter, err %s\n",
7526 			i40e_stat_str(&pf->hw, err));
7527 		goto err;
7528 	}
7529 
7530 	/* add filter to the ordered list */
7531 	INIT_HLIST_NODE(&filter->cloud_node);
7532 
7533 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7534 
7535 	pf->num_cloud_filters++;
7536 
7537 	return err;
7538 err:
7539 	kfree(filter);
7540 	return err;
7541 }
7542 
7543 /**
7544  * i40e_find_cloud_filter - Find the could filter in the list
7545  * @vsi: Pointer to VSI
7546  * @cookie: filter specific cookie
7547  *
7548  **/
7549 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7550 							unsigned long *cookie)
7551 {
7552 	struct i40e_cloud_filter *filter = NULL;
7553 	struct hlist_node *node2;
7554 
7555 	hlist_for_each_entry_safe(filter, node2,
7556 				  &vsi->back->cloud_filter_list, cloud_node)
7557 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7558 			return filter;
7559 	return NULL;
7560 }
7561 
7562 /**
7563  * i40e_delete_clsflower - Remove tc flower filters
7564  * @vsi: Pointer to VSI
7565  * @cls_flower: Pointer to struct tc_cls_flower_offload
7566  *
7567  **/
7568 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7569 				 struct tc_cls_flower_offload *cls_flower)
7570 {
7571 	struct i40e_cloud_filter *filter = NULL;
7572 	struct i40e_pf *pf = vsi->back;
7573 	int err = 0;
7574 
7575 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7576 
7577 	if (!filter)
7578 		return -EINVAL;
7579 
7580 	hash_del(&filter->cloud_node);
7581 
7582 	if (filter->dst_port)
7583 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7584 	else
7585 		err = i40e_add_del_cloud_filter(vsi, filter, false);
7586 
7587 	kfree(filter);
7588 	if (err) {
7589 		dev_err(&pf->pdev->dev,
7590 			"Failed to delete cloud filter, err %s\n",
7591 			i40e_stat_str(&pf->hw, err));
7592 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7593 	}
7594 
7595 	pf->num_cloud_filters--;
7596 	if (!pf->num_cloud_filters)
7597 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7598 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7599 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7600 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7601 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7602 		}
7603 	return 0;
7604 }
7605 
7606 /**
7607  * i40e_setup_tc_cls_flower - flower classifier offloads
7608  * @netdev: net device to configure
7609  * @type_data: offload data
7610  **/
7611 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7612 				    struct tc_cls_flower_offload *cls_flower)
7613 {
7614 	struct i40e_vsi *vsi = np->vsi;
7615 
7616 	switch (cls_flower->command) {
7617 	case TC_CLSFLOWER_REPLACE:
7618 		return i40e_configure_clsflower(vsi, cls_flower);
7619 	case TC_CLSFLOWER_DESTROY:
7620 		return i40e_delete_clsflower(vsi, cls_flower);
7621 	case TC_CLSFLOWER_STATS:
7622 		return -EOPNOTSUPP;
7623 	default:
7624 		return -EOPNOTSUPP;
7625 	}
7626 }
7627 
7628 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7629 				  void *cb_priv)
7630 {
7631 	struct i40e_netdev_priv *np = cb_priv;
7632 
7633 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
7634 		return -EOPNOTSUPP;
7635 
7636 	switch (type) {
7637 	case TC_SETUP_CLSFLOWER:
7638 		return i40e_setup_tc_cls_flower(np, type_data);
7639 
7640 	default:
7641 		return -EOPNOTSUPP;
7642 	}
7643 }
7644 
7645 static int i40e_setup_tc_block(struct net_device *dev,
7646 			       struct tc_block_offload *f)
7647 {
7648 	struct i40e_netdev_priv *np = netdev_priv(dev);
7649 
7650 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7651 		return -EOPNOTSUPP;
7652 
7653 	switch (f->command) {
7654 	case TC_BLOCK_BIND:
7655 		return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7656 					     np, np, f->extack);
7657 	case TC_BLOCK_UNBIND:
7658 		tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7659 		return 0;
7660 	default:
7661 		return -EOPNOTSUPP;
7662 	}
7663 }
7664 
7665 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7666 			   void *type_data)
7667 {
7668 	switch (type) {
7669 	case TC_SETUP_QDISC_MQPRIO:
7670 		return i40e_setup_tc(netdev, type_data);
7671 	case TC_SETUP_BLOCK:
7672 		return i40e_setup_tc_block(netdev, type_data);
7673 	default:
7674 		return -EOPNOTSUPP;
7675 	}
7676 }
7677 
7678 /**
7679  * i40e_open - Called when a network interface is made active
7680  * @netdev: network interface device structure
7681  *
7682  * The open entry point is called when a network interface is made
7683  * active by the system (IFF_UP).  At this point all resources needed
7684  * for transmit and receive operations are allocated, the interrupt
7685  * handler is registered with the OS, the netdev watchdog subtask is
7686  * enabled, and the stack is notified that the interface is ready.
7687  *
7688  * Returns 0 on success, negative value on failure
7689  **/
7690 int i40e_open(struct net_device *netdev)
7691 {
7692 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7693 	struct i40e_vsi *vsi = np->vsi;
7694 	struct i40e_pf *pf = vsi->back;
7695 	int err;
7696 
7697 	/* disallow open during test or if eeprom is broken */
7698 	if (test_bit(__I40E_TESTING, pf->state) ||
7699 	    test_bit(__I40E_BAD_EEPROM, pf->state))
7700 		return -EBUSY;
7701 
7702 	netif_carrier_off(netdev);
7703 
7704 	if (i40e_force_link_state(pf, true))
7705 		return -EAGAIN;
7706 
7707 	err = i40e_vsi_open(vsi);
7708 	if (err)
7709 		return err;
7710 
7711 	/* configure global TSO hardware offload settings */
7712 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7713 						       TCP_FLAG_FIN) >> 16);
7714 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7715 						       TCP_FLAG_FIN |
7716 						       TCP_FLAG_CWR) >> 16);
7717 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7718 
7719 	udp_tunnel_get_rx_info(netdev);
7720 
7721 	return 0;
7722 }
7723 
7724 /**
7725  * i40e_vsi_open -
7726  * @vsi: the VSI to open
7727  *
7728  * Finish initialization of the VSI.
7729  *
7730  * Returns 0 on success, negative value on failure
7731  *
7732  * Note: expects to be called while under rtnl_lock()
7733  **/
7734 int i40e_vsi_open(struct i40e_vsi *vsi)
7735 {
7736 	struct i40e_pf *pf = vsi->back;
7737 	char int_name[I40E_INT_NAME_STR_LEN];
7738 	int err;
7739 
7740 	/* allocate descriptors */
7741 	err = i40e_vsi_setup_tx_resources(vsi);
7742 	if (err)
7743 		goto err_setup_tx;
7744 	err = i40e_vsi_setup_rx_resources(vsi);
7745 	if (err)
7746 		goto err_setup_rx;
7747 
7748 	err = i40e_vsi_configure(vsi);
7749 	if (err)
7750 		goto err_setup_rx;
7751 
7752 	if (vsi->netdev) {
7753 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7754 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7755 		err = i40e_vsi_request_irq(vsi, int_name);
7756 		if (err)
7757 			goto err_setup_rx;
7758 
7759 		/* Notify the stack of the actual queue counts. */
7760 		err = netif_set_real_num_tx_queues(vsi->netdev,
7761 						   vsi->num_queue_pairs);
7762 		if (err)
7763 			goto err_set_queues;
7764 
7765 		err = netif_set_real_num_rx_queues(vsi->netdev,
7766 						   vsi->num_queue_pairs);
7767 		if (err)
7768 			goto err_set_queues;
7769 
7770 	} else if (vsi->type == I40E_VSI_FDIR) {
7771 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7772 			 dev_driver_string(&pf->pdev->dev),
7773 			 dev_name(&pf->pdev->dev));
7774 		err = i40e_vsi_request_irq(vsi, int_name);
7775 
7776 	} else {
7777 		err = -EINVAL;
7778 		goto err_setup_rx;
7779 	}
7780 
7781 	err = i40e_up_complete(vsi);
7782 	if (err)
7783 		goto err_up_complete;
7784 
7785 	return 0;
7786 
7787 err_up_complete:
7788 	i40e_down(vsi);
7789 err_set_queues:
7790 	i40e_vsi_free_irq(vsi);
7791 err_setup_rx:
7792 	i40e_vsi_free_rx_resources(vsi);
7793 err_setup_tx:
7794 	i40e_vsi_free_tx_resources(vsi);
7795 	if (vsi == pf->vsi[pf->lan_vsi])
7796 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7797 
7798 	return err;
7799 }
7800 
7801 /**
7802  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7803  * @pf: Pointer to PF
7804  *
7805  * This function destroys the hlist where all the Flow Director
7806  * filters were saved.
7807  **/
7808 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7809 {
7810 	struct i40e_fdir_filter *filter;
7811 	struct i40e_flex_pit *pit_entry, *tmp;
7812 	struct hlist_node *node2;
7813 
7814 	hlist_for_each_entry_safe(filter, node2,
7815 				  &pf->fdir_filter_list, fdir_node) {
7816 		hlist_del(&filter->fdir_node);
7817 		kfree(filter);
7818 	}
7819 
7820 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7821 		list_del(&pit_entry->list);
7822 		kfree(pit_entry);
7823 	}
7824 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7825 
7826 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7827 		list_del(&pit_entry->list);
7828 		kfree(pit_entry);
7829 	}
7830 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7831 
7832 	pf->fdir_pf_active_filters = 0;
7833 	pf->fd_tcp4_filter_cnt = 0;
7834 	pf->fd_udp4_filter_cnt = 0;
7835 	pf->fd_sctp4_filter_cnt = 0;
7836 	pf->fd_ip4_filter_cnt = 0;
7837 
7838 	/* Reprogram the default input set for TCP/IPv4 */
7839 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7840 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7841 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7842 
7843 	/* Reprogram the default input set for UDP/IPv4 */
7844 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7845 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7846 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7847 
7848 	/* Reprogram the default input set for SCTP/IPv4 */
7849 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7850 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7851 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7852 
7853 	/* Reprogram the default input set for Other/IPv4 */
7854 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7855 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7856 
7857 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7858 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7859 }
7860 
7861 /**
7862  * i40e_cloud_filter_exit - Cleans up the cloud filters
7863  * @pf: Pointer to PF
7864  *
7865  * This function destroys the hlist where all the cloud filters
7866  * were saved.
7867  **/
7868 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7869 {
7870 	struct i40e_cloud_filter *cfilter;
7871 	struct hlist_node *node;
7872 
7873 	hlist_for_each_entry_safe(cfilter, node,
7874 				  &pf->cloud_filter_list, cloud_node) {
7875 		hlist_del(&cfilter->cloud_node);
7876 		kfree(cfilter);
7877 	}
7878 	pf->num_cloud_filters = 0;
7879 
7880 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7881 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7882 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7883 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7884 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7885 	}
7886 }
7887 
7888 /**
7889  * i40e_close - Disables a network interface
7890  * @netdev: network interface device structure
7891  *
7892  * The close entry point is called when an interface is de-activated
7893  * by the OS.  The hardware is still under the driver's control, but
7894  * this netdev interface is disabled.
7895  *
7896  * Returns 0, this is not allowed to fail
7897  **/
7898 int i40e_close(struct net_device *netdev)
7899 {
7900 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7901 	struct i40e_vsi *vsi = np->vsi;
7902 
7903 	i40e_vsi_close(vsi);
7904 
7905 	return 0;
7906 }
7907 
7908 /**
7909  * i40e_do_reset - Start a PF or Core Reset sequence
7910  * @pf: board private structure
7911  * @reset_flags: which reset is requested
7912  * @lock_acquired: indicates whether or not the lock has been acquired
7913  * before this function was called.
7914  *
7915  * The essential difference in resets is that the PF Reset
7916  * doesn't clear the packet buffers, doesn't reset the PE
7917  * firmware, and doesn't bother the other PFs on the chip.
7918  **/
7919 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7920 {
7921 	u32 val;
7922 
7923 	WARN_ON(in_interrupt());
7924 
7925 
7926 	/* do the biggest reset indicated */
7927 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7928 
7929 		/* Request a Global Reset
7930 		 *
7931 		 * This will start the chip's countdown to the actual full
7932 		 * chip reset event, and a warning interrupt to be sent
7933 		 * to all PFs, including the requestor.  Our handler
7934 		 * for the warning interrupt will deal with the shutdown
7935 		 * and recovery of the switch setup.
7936 		 */
7937 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7938 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7939 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7940 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7941 
7942 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7943 
7944 		/* Request a Core Reset
7945 		 *
7946 		 * Same as Global Reset, except does *not* include the MAC/PHY
7947 		 */
7948 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7949 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7950 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
7951 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7952 		i40e_flush(&pf->hw);
7953 
7954 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
7955 
7956 		/* Request a PF Reset
7957 		 *
7958 		 * Resets only the PF-specific registers
7959 		 *
7960 		 * This goes directly to the tear-down and rebuild of
7961 		 * the switch, since we need to do all the recovery as
7962 		 * for the Core Reset.
7963 		 */
7964 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
7965 		i40e_handle_reset_warning(pf, lock_acquired);
7966 
7967 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7968 		int v;
7969 
7970 		/* Find the VSI(s) that requested a re-init */
7971 		dev_info(&pf->pdev->dev,
7972 			 "VSI reinit requested\n");
7973 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7974 			struct i40e_vsi *vsi = pf->vsi[v];
7975 
7976 			if (vsi != NULL &&
7977 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7978 					       vsi->state))
7979 				i40e_vsi_reinit_locked(pf->vsi[v]);
7980 		}
7981 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7982 		int v;
7983 
7984 		/* Find the VSI(s) that needs to be brought down */
7985 		dev_info(&pf->pdev->dev, "VSI down requested\n");
7986 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7987 			struct i40e_vsi *vsi = pf->vsi[v];
7988 
7989 			if (vsi != NULL &&
7990 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7991 					       vsi->state)) {
7992 				set_bit(__I40E_VSI_DOWN, vsi->state);
7993 				i40e_down(vsi);
7994 			}
7995 		}
7996 	} else {
7997 		dev_info(&pf->pdev->dev,
7998 			 "bad reset request 0x%08x\n", reset_flags);
7999 	}
8000 }
8001 
8002 #ifdef CONFIG_I40E_DCB
8003 /**
8004  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8005  * @pf: board private structure
8006  * @old_cfg: current DCB config
8007  * @new_cfg: new DCB config
8008  **/
8009 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8010 			    struct i40e_dcbx_config *old_cfg,
8011 			    struct i40e_dcbx_config *new_cfg)
8012 {
8013 	bool need_reconfig = false;
8014 
8015 	/* Check if ETS configuration has changed */
8016 	if (memcmp(&new_cfg->etscfg,
8017 		   &old_cfg->etscfg,
8018 		   sizeof(new_cfg->etscfg))) {
8019 		/* If Priority Table has changed reconfig is needed */
8020 		if (memcmp(&new_cfg->etscfg.prioritytable,
8021 			   &old_cfg->etscfg.prioritytable,
8022 			   sizeof(new_cfg->etscfg.prioritytable))) {
8023 			need_reconfig = true;
8024 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8025 		}
8026 
8027 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8028 			   &old_cfg->etscfg.tcbwtable,
8029 			   sizeof(new_cfg->etscfg.tcbwtable)))
8030 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8031 
8032 		if (memcmp(&new_cfg->etscfg.tsatable,
8033 			   &old_cfg->etscfg.tsatable,
8034 			   sizeof(new_cfg->etscfg.tsatable)))
8035 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8036 	}
8037 
8038 	/* Check if PFC configuration has changed */
8039 	if (memcmp(&new_cfg->pfc,
8040 		   &old_cfg->pfc,
8041 		   sizeof(new_cfg->pfc))) {
8042 		need_reconfig = true;
8043 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8044 	}
8045 
8046 	/* Check if APP Table has changed */
8047 	if (memcmp(&new_cfg->app,
8048 		   &old_cfg->app,
8049 		   sizeof(new_cfg->app))) {
8050 		need_reconfig = true;
8051 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8052 	}
8053 
8054 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8055 	return need_reconfig;
8056 }
8057 
8058 /**
8059  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8060  * @pf: board private structure
8061  * @e: event info posted on ARQ
8062  **/
8063 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8064 				  struct i40e_arq_event_info *e)
8065 {
8066 	struct i40e_aqc_lldp_get_mib *mib =
8067 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8068 	struct i40e_hw *hw = &pf->hw;
8069 	struct i40e_dcbx_config tmp_dcbx_cfg;
8070 	bool need_reconfig = false;
8071 	int ret = 0;
8072 	u8 type;
8073 
8074 	/* Not DCB capable or capability disabled */
8075 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8076 		return ret;
8077 
8078 	/* Ignore if event is not for Nearest Bridge */
8079 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8080 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8081 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8082 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8083 		return ret;
8084 
8085 	/* Check MIB Type and return if event for Remote MIB update */
8086 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8087 	dev_dbg(&pf->pdev->dev,
8088 		"LLDP event mib type %s\n", type ? "remote" : "local");
8089 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8090 		/* Update the remote cached instance and return */
8091 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8092 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8093 				&hw->remote_dcbx_config);
8094 		goto exit;
8095 	}
8096 
8097 	/* Store the old configuration */
8098 	tmp_dcbx_cfg = hw->local_dcbx_config;
8099 
8100 	/* Reset the old DCBx configuration data */
8101 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8102 	/* Get updated DCBX data from firmware */
8103 	ret = i40e_get_dcb_config(&pf->hw);
8104 	if (ret) {
8105 		dev_info(&pf->pdev->dev,
8106 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8107 			 i40e_stat_str(&pf->hw, ret),
8108 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8109 		goto exit;
8110 	}
8111 
8112 	/* No change detected in DCBX configs */
8113 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8114 		    sizeof(tmp_dcbx_cfg))) {
8115 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8116 		goto exit;
8117 	}
8118 
8119 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8120 					       &hw->local_dcbx_config);
8121 
8122 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8123 
8124 	if (!need_reconfig)
8125 		goto exit;
8126 
8127 	/* Enable DCB tagging only when more than one TC */
8128 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8129 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8130 	else
8131 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8132 
8133 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8134 	/* Reconfiguration needed quiesce all VSIs */
8135 	i40e_pf_quiesce_all_vsi(pf);
8136 
8137 	/* Changes in configuration update VEB/VSI */
8138 	i40e_dcb_reconfigure(pf);
8139 
8140 	ret = i40e_resume_port_tx(pf);
8141 
8142 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8143 	/* In case of error no point in resuming VSIs */
8144 	if (ret)
8145 		goto exit;
8146 
8147 	/* Wait for the PF's queues to be disabled */
8148 	ret = i40e_pf_wait_queues_disabled(pf);
8149 	if (ret) {
8150 		/* Schedule PF reset to recover */
8151 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8152 		i40e_service_event_schedule(pf);
8153 	} else {
8154 		i40e_pf_unquiesce_all_vsi(pf);
8155 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8156 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8157 	}
8158 
8159 exit:
8160 	return ret;
8161 }
8162 #endif /* CONFIG_I40E_DCB */
8163 
8164 /**
8165  * i40e_do_reset_safe - Protected reset path for userland calls.
8166  * @pf: board private structure
8167  * @reset_flags: which reset is requested
8168  *
8169  **/
8170 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8171 {
8172 	rtnl_lock();
8173 	i40e_do_reset(pf, reset_flags, true);
8174 	rtnl_unlock();
8175 }
8176 
8177 /**
8178  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8179  * @pf: board private structure
8180  * @e: event info posted on ARQ
8181  *
8182  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8183  * and VF queues
8184  **/
8185 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8186 					   struct i40e_arq_event_info *e)
8187 {
8188 	struct i40e_aqc_lan_overflow *data =
8189 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8190 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8191 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8192 	struct i40e_hw *hw = &pf->hw;
8193 	struct i40e_vf *vf;
8194 	u16 vf_id;
8195 
8196 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8197 		queue, qtx_ctl);
8198 
8199 	/* Queue belongs to VF, find the VF and issue VF reset */
8200 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8201 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8202 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8203 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8204 		vf_id -= hw->func_caps.vf_base_id;
8205 		vf = &pf->vf[vf_id];
8206 		i40e_vc_notify_vf_reset(vf);
8207 		/* Allow VF to process pending reset notification */
8208 		msleep(20);
8209 		i40e_reset_vf(vf, false);
8210 	}
8211 }
8212 
8213 /**
8214  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8215  * @pf: board private structure
8216  **/
8217 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8218 {
8219 	u32 val, fcnt_prog;
8220 
8221 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8222 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8223 	return fcnt_prog;
8224 }
8225 
8226 /**
8227  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8228  * @pf: board private structure
8229  **/
8230 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8231 {
8232 	u32 val, fcnt_prog;
8233 
8234 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8235 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8236 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8237 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8238 	return fcnt_prog;
8239 }
8240 
8241 /**
8242  * i40e_get_global_fd_count - Get total FD filters programmed on device
8243  * @pf: board private structure
8244  **/
8245 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8246 {
8247 	u32 val, fcnt_prog;
8248 
8249 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8250 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8251 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8252 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8253 	return fcnt_prog;
8254 }
8255 
8256 /**
8257  * i40e_reenable_fdir_sb - Restore FDir SB capability
8258  * @pf: board private structure
8259  **/
8260 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8261 {
8262 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8263 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8264 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8265 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8266 }
8267 
8268 /**
8269  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8270  * @pf: board private structure
8271  **/
8272 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8273 {
8274 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8275 		/* ATR uses the same filtering logic as SB rules. It only
8276 		 * functions properly if the input set mask is at the default
8277 		 * settings. It is safe to restore the default input set
8278 		 * because there are no active TCPv4 filter rules.
8279 		 */
8280 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8281 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8282 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8283 
8284 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8285 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8286 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8287 	}
8288 }
8289 
8290 /**
8291  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8292  * @pf: board private structure
8293  * @filter: FDir filter to remove
8294  */
8295 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8296 				       struct i40e_fdir_filter *filter)
8297 {
8298 	/* Update counters */
8299 	pf->fdir_pf_active_filters--;
8300 	pf->fd_inv = 0;
8301 
8302 	switch (filter->flow_type) {
8303 	case TCP_V4_FLOW:
8304 		pf->fd_tcp4_filter_cnt--;
8305 		break;
8306 	case UDP_V4_FLOW:
8307 		pf->fd_udp4_filter_cnt--;
8308 		break;
8309 	case SCTP_V4_FLOW:
8310 		pf->fd_sctp4_filter_cnt--;
8311 		break;
8312 	case IP_USER_FLOW:
8313 		switch (filter->ip4_proto) {
8314 		case IPPROTO_TCP:
8315 			pf->fd_tcp4_filter_cnt--;
8316 			break;
8317 		case IPPROTO_UDP:
8318 			pf->fd_udp4_filter_cnt--;
8319 			break;
8320 		case IPPROTO_SCTP:
8321 			pf->fd_sctp4_filter_cnt--;
8322 			break;
8323 		case IPPROTO_IP:
8324 			pf->fd_ip4_filter_cnt--;
8325 			break;
8326 		}
8327 		break;
8328 	}
8329 
8330 	/* Remove the filter from the list and free memory */
8331 	hlist_del(&filter->fdir_node);
8332 	kfree(filter);
8333 }
8334 
8335 /**
8336  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8337  * @pf: board private structure
8338  **/
8339 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8340 {
8341 	struct i40e_fdir_filter *filter;
8342 	u32 fcnt_prog, fcnt_avail;
8343 	struct hlist_node *node;
8344 
8345 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8346 		return;
8347 
8348 	/* Check if we have enough room to re-enable FDir SB capability. */
8349 	fcnt_prog = i40e_get_global_fd_count(pf);
8350 	fcnt_avail = pf->fdir_pf_filter_count;
8351 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8352 	    (pf->fd_add_err == 0) ||
8353 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8354 		i40e_reenable_fdir_sb(pf);
8355 
8356 	/* We should wait for even more space before re-enabling ATR.
8357 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8358 	 * rules active.
8359 	 */
8360 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8361 	    (pf->fd_tcp4_filter_cnt == 0))
8362 		i40e_reenable_fdir_atr(pf);
8363 
8364 	/* if hw had a problem adding a filter, delete it */
8365 	if (pf->fd_inv > 0) {
8366 		hlist_for_each_entry_safe(filter, node,
8367 					  &pf->fdir_filter_list, fdir_node)
8368 			if (filter->fd_id == pf->fd_inv)
8369 				i40e_delete_invalid_filter(pf, filter);
8370 	}
8371 }
8372 
8373 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8374 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8375 /**
8376  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8377  * @pf: board private structure
8378  **/
8379 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8380 {
8381 	unsigned long min_flush_time;
8382 	int flush_wait_retry = 50;
8383 	bool disable_atr = false;
8384 	int fd_room;
8385 	int reg;
8386 
8387 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8388 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8389 		return;
8390 
8391 	/* If the flush is happening too quick and we have mostly SB rules we
8392 	 * should not re-enable ATR for some time.
8393 	 */
8394 	min_flush_time = pf->fd_flush_timestamp +
8395 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8396 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8397 
8398 	if (!(time_after(jiffies, min_flush_time)) &&
8399 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8400 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8401 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8402 		disable_atr = true;
8403 	}
8404 
8405 	pf->fd_flush_timestamp = jiffies;
8406 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8407 	/* flush all filters */
8408 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8409 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8410 	i40e_flush(&pf->hw);
8411 	pf->fd_flush_cnt++;
8412 	pf->fd_add_err = 0;
8413 	do {
8414 		/* Check FD flush status every 5-6msec */
8415 		usleep_range(5000, 6000);
8416 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8417 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8418 			break;
8419 	} while (flush_wait_retry--);
8420 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8421 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8422 	} else {
8423 		/* replay sideband filters */
8424 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8425 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8426 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8427 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8428 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8429 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8430 	}
8431 }
8432 
8433 /**
8434  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8435  * @pf: board private structure
8436  **/
8437 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8438 {
8439 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8440 }
8441 
8442 /* We can see up to 256 filter programming desc in transit if the filters are
8443  * being applied really fast; before we see the first
8444  * filter miss error on Rx queue 0. Accumulating enough error messages before
8445  * reacting will make sure we don't cause flush too often.
8446  */
8447 #define I40E_MAX_FD_PROGRAM_ERROR 256
8448 
8449 /**
8450  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8451  * @pf: board private structure
8452  **/
8453 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8454 {
8455 
8456 	/* if interface is down do nothing */
8457 	if (test_bit(__I40E_DOWN, pf->state))
8458 		return;
8459 
8460 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8461 		i40e_fdir_flush_and_replay(pf);
8462 
8463 	i40e_fdir_check_and_reenable(pf);
8464 
8465 }
8466 
8467 /**
8468  * i40e_vsi_link_event - notify VSI of a link event
8469  * @vsi: vsi to be notified
8470  * @link_up: link up or down
8471  **/
8472 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8473 {
8474 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8475 		return;
8476 
8477 	switch (vsi->type) {
8478 	case I40E_VSI_MAIN:
8479 		if (!vsi->netdev || !vsi->netdev_registered)
8480 			break;
8481 
8482 		if (link_up) {
8483 			netif_carrier_on(vsi->netdev);
8484 			netif_tx_wake_all_queues(vsi->netdev);
8485 		} else {
8486 			netif_carrier_off(vsi->netdev);
8487 			netif_tx_stop_all_queues(vsi->netdev);
8488 		}
8489 		break;
8490 
8491 	case I40E_VSI_SRIOV:
8492 	case I40E_VSI_VMDQ2:
8493 	case I40E_VSI_CTRL:
8494 	case I40E_VSI_IWARP:
8495 	case I40E_VSI_MIRROR:
8496 	default:
8497 		/* there is no notification for other VSIs */
8498 		break;
8499 	}
8500 }
8501 
8502 /**
8503  * i40e_veb_link_event - notify elements on the veb of a link event
8504  * @veb: veb to be notified
8505  * @link_up: link up or down
8506  **/
8507 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8508 {
8509 	struct i40e_pf *pf;
8510 	int i;
8511 
8512 	if (!veb || !veb->pf)
8513 		return;
8514 	pf = veb->pf;
8515 
8516 	/* depth first... */
8517 	for (i = 0; i < I40E_MAX_VEB; i++)
8518 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8519 			i40e_veb_link_event(pf->veb[i], link_up);
8520 
8521 	/* ... now the local VSIs */
8522 	for (i = 0; i < pf->num_alloc_vsi; i++)
8523 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8524 			i40e_vsi_link_event(pf->vsi[i], link_up);
8525 }
8526 
8527 /**
8528  * i40e_link_event - Update netif_carrier status
8529  * @pf: board private structure
8530  **/
8531 static void i40e_link_event(struct i40e_pf *pf)
8532 {
8533 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8534 	u8 new_link_speed, old_link_speed;
8535 	i40e_status status;
8536 	bool new_link, old_link;
8537 
8538 	/* set this to force the get_link_status call to refresh state */
8539 	pf->hw.phy.get_link_info = true;
8540 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8541 	status = i40e_get_link_status(&pf->hw, &new_link);
8542 
8543 	/* On success, disable temp link polling */
8544 	if (status == I40E_SUCCESS) {
8545 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8546 	} else {
8547 		/* Enable link polling temporarily until i40e_get_link_status
8548 		 * returns I40E_SUCCESS
8549 		 */
8550 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8551 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8552 			status);
8553 		return;
8554 	}
8555 
8556 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
8557 	new_link_speed = pf->hw.phy.link_info.link_speed;
8558 
8559 	if (new_link == old_link &&
8560 	    new_link_speed == old_link_speed &&
8561 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8562 	     new_link == netif_carrier_ok(vsi->netdev)))
8563 		return;
8564 
8565 	i40e_print_link_message(vsi, new_link);
8566 
8567 	/* Notify the base of the switch tree connected to
8568 	 * the link.  Floating VEBs are not notified.
8569 	 */
8570 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
8571 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8572 	else
8573 		i40e_vsi_link_event(vsi, new_link);
8574 
8575 	if (pf->vf)
8576 		i40e_vc_notify_link_state(pf);
8577 
8578 	if (pf->flags & I40E_FLAG_PTP)
8579 		i40e_ptp_set_increment(pf);
8580 }
8581 
8582 /**
8583  * i40e_watchdog_subtask - periodic checks not using event driven response
8584  * @pf: board private structure
8585  **/
8586 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8587 {
8588 	int i;
8589 
8590 	/* if interface is down do nothing */
8591 	if (test_bit(__I40E_DOWN, pf->state) ||
8592 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
8593 		return;
8594 
8595 	/* make sure we don't do these things too often */
8596 	if (time_before(jiffies, (pf->service_timer_previous +
8597 				  pf->service_timer_period)))
8598 		return;
8599 	pf->service_timer_previous = jiffies;
8600 
8601 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8602 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
8603 		i40e_link_event(pf);
8604 
8605 	/* Update the stats for active netdevs so the network stack
8606 	 * can look at updated numbers whenever it cares to
8607 	 */
8608 	for (i = 0; i < pf->num_alloc_vsi; i++)
8609 		if (pf->vsi[i] && pf->vsi[i]->netdev)
8610 			i40e_update_stats(pf->vsi[i]);
8611 
8612 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8613 		/* Update the stats for the active switching components */
8614 		for (i = 0; i < I40E_MAX_VEB; i++)
8615 			if (pf->veb[i])
8616 				i40e_update_veb_stats(pf->veb[i]);
8617 	}
8618 
8619 	i40e_ptp_rx_hang(pf);
8620 	i40e_ptp_tx_hang(pf);
8621 }
8622 
8623 /**
8624  * i40e_reset_subtask - Set up for resetting the device and driver
8625  * @pf: board private structure
8626  **/
8627 static void i40e_reset_subtask(struct i40e_pf *pf)
8628 {
8629 	u32 reset_flags = 0;
8630 
8631 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8632 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8633 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8634 	}
8635 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8636 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8637 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8638 	}
8639 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8640 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8641 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8642 	}
8643 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8644 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8645 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8646 	}
8647 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8648 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8649 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8650 	}
8651 
8652 	/* If there's a recovery already waiting, it takes
8653 	 * precedence before starting a new reset sequence.
8654 	 */
8655 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8656 		i40e_prep_for_reset(pf, false);
8657 		i40e_reset(pf);
8658 		i40e_rebuild(pf, false, false);
8659 	}
8660 
8661 	/* If we're already down or resetting, just bail */
8662 	if (reset_flags &&
8663 	    !test_bit(__I40E_DOWN, pf->state) &&
8664 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8665 		i40e_do_reset(pf, reset_flags, false);
8666 	}
8667 }
8668 
8669 /**
8670  * i40e_handle_link_event - Handle link event
8671  * @pf: board private structure
8672  * @e: event info posted on ARQ
8673  **/
8674 static void i40e_handle_link_event(struct i40e_pf *pf,
8675 				   struct i40e_arq_event_info *e)
8676 {
8677 	struct i40e_aqc_get_link_status *status =
8678 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8679 
8680 	/* Do a new status request to re-enable LSE reporting
8681 	 * and load new status information into the hw struct
8682 	 * This completely ignores any state information
8683 	 * in the ARQ event info, instead choosing to always
8684 	 * issue the AQ update link status command.
8685 	 */
8686 	i40e_link_event(pf);
8687 
8688 	/* Check if module meets thermal requirements */
8689 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8690 		dev_err(&pf->pdev->dev,
8691 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8692 		dev_err(&pf->pdev->dev,
8693 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8694 	} else {
8695 		/* check for unqualified module, if link is down, suppress
8696 		 * the message if link was forced to be down.
8697 		 */
8698 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8699 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8700 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
8701 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8702 			dev_err(&pf->pdev->dev,
8703 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8704 			dev_err(&pf->pdev->dev,
8705 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8706 		}
8707 	}
8708 }
8709 
8710 /**
8711  * i40e_clean_adminq_subtask - Clean the AdminQ rings
8712  * @pf: board private structure
8713  **/
8714 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8715 {
8716 	struct i40e_arq_event_info event;
8717 	struct i40e_hw *hw = &pf->hw;
8718 	u16 pending, i = 0;
8719 	i40e_status ret;
8720 	u16 opcode;
8721 	u32 oldval;
8722 	u32 val;
8723 
8724 	/* Do not run clean AQ when PF reset fails */
8725 	if (test_bit(__I40E_RESET_FAILED, pf->state))
8726 		return;
8727 
8728 	/* check for error indications */
8729 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
8730 	oldval = val;
8731 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8732 		if (hw->debug_mask & I40E_DEBUG_AQ)
8733 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8734 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8735 	}
8736 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8737 		if (hw->debug_mask & I40E_DEBUG_AQ)
8738 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8739 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8740 		pf->arq_overflows++;
8741 	}
8742 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8743 		if (hw->debug_mask & I40E_DEBUG_AQ)
8744 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8745 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8746 	}
8747 	if (oldval != val)
8748 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
8749 
8750 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
8751 	oldval = val;
8752 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8753 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8754 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8755 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8756 	}
8757 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8758 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8759 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8760 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8761 	}
8762 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8763 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8764 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8765 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8766 	}
8767 	if (oldval != val)
8768 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
8769 
8770 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8771 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8772 	if (!event.msg_buf)
8773 		return;
8774 
8775 	do {
8776 		ret = i40e_clean_arq_element(hw, &event, &pending);
8777 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8778 			break;
8779 		else if (ret) {
8780 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8781 			break;
8782 		}
8783 
8784 		opcode = le16_to_cpu(event.desc.opcode);
8785 		switch (opcode) {
8786 
8787 		case i40e_aqc_opc_get_link_status:
8788 			i40e_handle_link_event(pf, &event);
8789 			break;
8790 		case i40e_aqc_opc_send_msg_to_pf:
8791 			ret = i40e_vc_process_vf_msg(pf,
8792 					le16_to_cpu(event.desc.retval),
8793 					le32_to_cpu(event.desc.cookie_high),
8794 					le32_to_cpu(event.desc.cookie_low),
8795 					event.msg_buf,
8796 					event.msg_len);
8797 			break;
8798 		case i40e_aqc_opc_lldp_update_mib:
8799 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8800 #ifdef CONFIG_I40E_DCB
8801 			rtnl_lock();
8802 			ret = i40e_handle_lldp_event(pf, &event);
8803 			rtnl_unlock();
8804 #endif /* CONFIG_I40E_DCB */
8805 			break;
8806 		case i40e_aqc_opc_event_lan_overflow:
8807 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8808 			i40e_handle_lan_overflow_event(pf, &event);
8809 			break;
8810 		case i40e_aqc_opc_send_msg_to_peer:
8811 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8812 			break;
8813 		case i40e_aqc_opc_nvm_erase:
8814 		case i40e_aqc_opc_nvm_update:
8815 		case i40e_aqc_opc_oem_post_update:
8816 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8817 				   "ARQ NVM operation 0x%04x completed\n",
8818 				   opcode);
8819 			break;
8820 		default:
8821 			dev_info(&pf->pdev->dev,
8822 				 "ARQ: Unknown event 0x%04x ignored\n",
8823 				 opcode);
8824 			break;
8825 		}
8826 	} while (i++ < pf->adminq_work_limit);
8827 
8828 	if (i < pf->adminq_work_limit)
8829 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8830 
8831 	/* re-enable Admin queue interrupt cause */
8832 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
8833 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8834 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
8835 	i40e_flush(hw);
8836 
8837 	kfree(event.msg_buf);
8838 }
8839 
8840 /**
8841  * i40e_verify_eeprom - make sure eeprom is good to use
8842  * @pf: board private structure
8843  **/
8844 static void i40e_verify_eeprom(struct i40e_pf *pf)
8845 {
8846 	int err;
8847 
8848 	err = i40e_diag_eeprom_test(&pf->hw);
8849 	if (err) {
8850 		/* retry in case of garbage read */
8851 		err = i40e_diag_eeprom_test(&pf->hw);
8852 		if (err) {
8853 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8854 				 err);
8855 			set_bit(__I40E_BAD_EEPROM, pf->state);
8856 		}
8857 	}
8858 
8859 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8860 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8861 		clear_bit(__I40E_BAD_EEPROM, pf->state);
8862 	}
8863 }
8864 
8865 /**
8866  * i40e_enable_pf_switch_lb
8867  * @pf: pointer to the PF structure
8868  *
8869  * enable switch loop back or die - no point in a return value
8870  **/
8871 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8872 {
8873 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8874 	struct i40e_vsi_context ctxt;
8875 	int ret;
8876 
8877 	ctxt.seid = pf->main_vsi_seid;
8878 	ctxt.pf_num = pf->hw.pf_id;
8879 	ctxt.vf_num = 0;
8880 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8881 	if (ret) {
8882 		dev_info(&pf->pdev->dev,
8883 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8884 			 i40e_stat_str(&pf->hw, ret),
8885 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8886 		return;
8887 	}
8888 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8889 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8890 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8891 
8892 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8893 	if (ret) {
8894 		dev_info(&pf->pdev->dev,
8895 			 "update vsi switch failed, err %s aq_err %s\n",
8896 			 i40e_stat_str(&pf->hw, ret),
8897 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8898 	}
8899 }
8900 
8901 /**
8902  * i40e_disable_pf_switch_lb
8903  * @pf: pointer to the PF structure
8904  *
8905  * disable switch loop back or die - no point in a return value
8906  **/
8907 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8908 {
8909 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8910 	struct i40e_vsi_context ctxt;
8911 	int ret;
8912 
8913 	ctxt.seid = pf->main_vsi_seid;
8914 	ctxt.pf_num = pf->hw.pf_id;
8915 	ctxt.vf_num = 0;
8916 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8917 	if (ret) {
8918 		dev_info(&pf->pdev->dev,
8919 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8920 			 i40e_stat_str(&pf->hw, ret),
8921 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8922 		return;
8923 	}
8924 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8925 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8926 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8927 
8928 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8929 	if (ret) {
8930 		dev_info(&pf->pdev->dev,
8931 			 "update vsi switch failed, err %s aq_err %s\n",
8932 			 i40e_stat_str(&pf->hw, ret),
8933 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8934 	}
8935 }
8936 
8937 /**
8938  * i40e_config_bridge_mode - Configure the HW bridge mode
8939  * @veb: pointer to the bridge instance
8940  *
8941  * Configure the loop back mode for the LAN VSI that is downlink to the
8942  * specified HW bridge instance. It is expected this function is called
8943  * when a new HW bridge is instantiated.
8944  **/
8945 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8946 {
8947 	struct i40e_pf *pf = veb->pf;
8948 
8949 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8950 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8951 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8952 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8953 		i40e_disable_pf_switch_lb(pf);
8954 	else
8955 		i40e_enable_pf_switch_lb(pf);
8956 }
8957 
8958 /**
8959  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8960  * @veb: pointer to the VEB instance
8961  *
8962  * This is a recursive function that first builds the attached VSIs then
8963  * recurses in to build the next layer of VEB.  We track the connections
8964  * through our own index numbers because the seid's from the HW could
8965  * change across the reset.
8966  **/
8967 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8968 {
8969 	struct i40e_vsi *ctl_vsi = NULL;
8970 	struct i40e_pf *pf = veb->pf;
8971 	int v, veb_idx;
8972 	int ret;
8973 
8974 	/* build VSI that owns this VEB, temporarily attached to base VEB */
8975 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8976 		if (pf->vsi[v] &&
8977 		    pf->vsi[v]->veb_idx == veb->idx &&
8978 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8979 			ctl_vsi = pf->vsi[v];
8980 			break;
8981 		}
8982 	}
8983 	if (!ctl_vsi) {
8984 		dev_info(&pf->pdev->dev,
8985 			 "missing owner VSI for veb_idx %d\n", veb->idx);
8986 		ret = -ENOENT;
8987 		goto end_reconstitute;
8988 	}
8989 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
8990 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8991 	ret = i40e_add_vsi(ctl_vsi);
8992 	if (ret) {
8993 		dev_info(&pf->pdev->dev,
8994 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
8995 			 veb->idx, ret);
8996 		goto end_reconstitute;
8997 	}
8998 	i40e_vsi_reset_stats(ctl_vsi);
8999 
9000 	/* create the VEB in the switch and move the VSI onto the VEB */
9001 	ret = i40e_add_veb(veb, ctl_vsi);
9002 	if (ret)
9003 		goto end_reconstitute;
9004 
9005 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9006 		veb->bridge_mode = BRIDGE_MODE_VEB;
9007 	else
9008 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9009 	i40e_config_bridge_mode(veb);
9010 
9011 	/* create the remaining VSIs attached to this VEB */
9012 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9013 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9014 			continue;
9015 
9016 		if (pf->vsi[v]->veb_idx == veb->idx) {
9017 			struct i40e_vsi *vsi = pf->vsi[v];
9018 
9019 			vsi->uplink_seid = veb->seid;
9020 			ret = i40e_add_vsi(vsi);
9021 			if (ret) {
9022 				dev_info(&pf->pdev->dev,
9023 					 "rebuild of vsi_idx %d failed: %d\n",
9024 					 v, ret);
9025 				goto end_reconstitute;
9026 			}
9027 			i40e_vsi_reset_stats(vsi);
9028 		}
9029 	}
9030 
9031 	/* create any VEBs attached to this VEB - RECURSION */
9032 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9033 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9034 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9035 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9036 			if (ret)
9037 				break;
9038 		}
9039 	}
9040 
9041 end_reconstitute:
9042 	return ret;
9043 }
9044 
9045 /**
9046  * i40e_get_capabilities - get info about the HW
9047  * @pf: the PF struct
9048  **/
9049 static int i40e_get_capabilities(struct i40e_pf *pf,
9050 				 enum i40e_admin_queue_opc list_type)
9051 {
9052 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9053 	u16 data_size;
9054 	int buf_len;
9055 	int err;
9056 
9057 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9058 	do {
9059 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9060 		if (!cap_buf)
9061 			return -ENOMEM;
9062 
9063 		/* this loads the data into the hw struct for us */
9064 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9065 						    &data_size, list_type,
9066 						    NULL);
9067 		/* data loaded, buffer no longer needed */
9068 		kfree(cap_buf);
9069 
9070 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9071 			/* retry with a larger buffer */
9072 			buf_len = data_size;
9073 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9074 			dev_info(&pf->pdev->dev,
9075 				 "capability discovery failed, err %s aq_err %s\n",
9076 				 i40e_stat_str(&pf->hw, err),
9077 				 i40e_aq_str(&pf->hw,
9078 					     pf->hw.aq.asq_last_status));
9079 			return -ENODEV;
9080 		}
9081 	} while (err);
9082 
9083 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9084 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9085 			dev_info(&pf->pdev->dev,
9086 				 "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
9087 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9088 				 pf->hw.func_caps.num_msix_vectors,
9089 				 pf->hw.func_caps.num_msix_vectors_vf,
9090 				 pf->hw.func_caps.fd_filters_guaranteed,
9091 				 pf->hw.func_caps.fd_filters_best_effort,
9092 				 pf->hw.func_caps.num_tx_qp,
9093 				 pf->hw.func_caps.num_vsis);
9094 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9095 			dev_info(&pf->pdev->dev,
9096 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9097 				 pf->hw.dev_caps.switch_mode,
9098 				 pf->hw.dev_caps.valid_functions);
9099 			dev_info(&pf->pdev->dev,
9100 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9101 				 pf->hw.dev_caps.sr_iov_1_1,
9102 				 pf->hw.dev_caps.num_vfs);
9103 			dev_info(&pf->pdev->dev,
9104 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9105 				 pf->hw.dev_caps.num_vsis,
9106 				 pf->hw.dev_caps.num_rx_qp,
9107 				 pf->hw.dev_caps.num_tx_qp);
9108 		}
9109 	}
9110 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9111 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9112 		       + pf->hw.func_caps.num_vfs)
9113 		if (pf->hw.revision_id == 0 &&
9114 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9115 			dev_info(&pf->pdev->dev,
9116 				 "got num_vsis %d, setting num_vsis to %d\n",
9117 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9118 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9119 		}
9120 	}
9121 	return 0;
9122 }
9123 
9124 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9125 
9126 /**
9127  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9128  * @pf: board private structure
9129  **/
9130 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9131 {
9132 	struct i40e_vsi *vsi;
9133 
9134 	/* quick workaround for an NVM issue that leaves a critical register
9135 	 * uninitialized
9136 	 */
9137 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9138 		static const u32 hkey[] = {
9139 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9140 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9141 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9142 			0x95b3a76d};
9143 		int i;
9144 
9145 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9146 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9147 	}
9148 
9149 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9150 		return;
9151 
9152 	/* find existing VSI and see if it needs configuring */
9153 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9154 
9155 	/* create a new VSI if none exists */
9156 	if (!vsi) {
9157 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9158 				     pf->vsi[pf->lan_vsi]->seid, 0);
9159 		if (!vsi) {
9160 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9161 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9162 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9163 			return;
9164 		}
9165 	}
9166 
9167 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9168 }
9169 
9170 /**
9171  * i40e_fdir_teardown - release the Flow Director resources
9172  * @pf: board private structure
9173  **/
9174 static void i40e_fdir_teardown(struct i40e_pf *pf)
9175 {
9176 	struct i40e_vsi *vsi;
9177 
9178 	i40e_fdir_filter_exit(pf);
9179 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9180 	if (vsi)
9181 		i40e_vsi_release(vsi);
9182 }
9183 
9184 /**
9185  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9186  * @vsi: PF main vsi
9187  * @seid: seid of main or channel VSIs
9188  *
9189  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9190  * existed before reset
9191  **/
9192 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9193 {
9194 	struct i40e_cloud_filter *cfilter;
9195 	struct i40e_pf *pf = vsi->back;
9196 	struct hlist_node *node;
9197 	i40e_status ret;
9198 
9199 	/* Add cloud filters back if they exist */
9200 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9201 				  cloud_node) {
9202 		if (cfilter->seid != seid)
9203 			continue;
9204 
9205 		if (cfilter->dst_port)
9206 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9207 								true);
9208 		else
9209 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9210 
9211 		if (ret) {
9212 			dev_dbg(&pf->pdev->dev,
9213 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9214 				i40e_stat_str(&pf->hw, ret),
9215 				i40e_aq_str(&pf->hw,
9216 					    pf->hw.aq.asq_last_status));
9217 			return ret;
9218 		}
9219 	}
9220 	return 0;
9221 }
9222 
9223 /**
9224  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9225  * @vsi: PF main vsi
9226  *
9227  * Rebuilds channel VSIs if they existed before reset
9228  **/
9229 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9230 {
9231 	struct i40e_channel *ch, *ch_tmp;
9232 	i40e_status ret;
9233 
9234 	if (list_empty(&vsi->ch_list))
9235 		return 0;
9236 
9237 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9238 		if (!ch->initialized)
9239 			break;
9240 		/* Proceed with creation of channel (VMDq2) VSI */
9241 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9242 		if (ret) {
9243 			dev_info(&vsi->back->pdev->dev,
9244 				 "failed to rebuild channels using uplink_seid %u\n",
9245 				 vsi->uplink_seid);
9246 			return ret;
9247 		}
9248 		/* Reconfigure TX queues using QTX_CTL register */
9249 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9250 		if (ret) {
9251 			dev_info(&vsi->back->pdev->dev,
9252 				 "failed to configure TX rings for channel %u\n",
9253 				 ch->seid);
9254 			return ret;
9255 		}
9256 		/* update 'next_base_queue' */
9257 		vsi->next_base_queue = vsi->next_base_queue +
9258 							ch->num_queue_pairs;
9259 		if (ch->max_tx_rate) {
9260 			u64 credits = ch->max_tx_rate;
9261 
9262 			if (i40e_set_bw_limit(vsi, ch->seid,
9263 					      ch->max_tx_rate))
9264 				return -EINVAL;
9265 
9266 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9267 			dev_dbg(&vsi->back->pdev->dev,
9268 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9269 				ch->max_tx_rate,
9270 				credits,
9271 				ch->seid);
9272 		}
9273 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9274 		if (ret) {
9275 			dev_dbg(&vsi->back->pdev->dev,
9276 				"Failed to rebuild cloud filters for channel VSI %u\n",
9277 				ch->seid);
9278 			return ret;
9279 		}
9280 	}
9281 	return 0;
9282 }
9283 
9284 /**
9285  * i40e_prep_for_reset - prep for the core to reset
9286  * @pf: board private structure
9287  * @lock_acquired: indicates whether or not the lock has been acquired
9288  * before this function was called.
9289  *
9290  * Close up the VFs and other things in prep for PF Reset.
9291   **/
9292 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9293 {
9294 	struct i40e_hw *hw = &pf->hw;
9295 	i40e_status ret = 0;
9296 	u32 v;
9297 
9298 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9299 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9300 		return;
9301 	if (i40e_check_asq_alive(&pf->hw))
9302 		i40e_vc_notify_reset(pf);
9303 
9304 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9305 
9306 	/* quiesce the VSIs and their queues that are not already DOWN */
9307 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9308 	if (!lock_acquired)
9309 		rtnl_lock();
9310 	i40e_pf_quiesce_all_vsi(pf);
9311 	if (!lock_acquired)
9312 		rtnl_unlock();
9313 
9314 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9315 		if (pf->vsi[v])
9316 			pf->vsi[v]->seid = 0;
9317 	}
9318 
9319 	i40e_shutdown_adminq(&pf->hw);
9320 
9321 	/* call shutdown HMC */
9322 	if (hw->hmc.hmc_obj) {
9323 		ret = i40e_shutdown_lan_hmc(hw);
9324 		if (ret)
9325 			dev_warn(&pf->pdev->dev,
9326 				 "shutdown_lan_hmc failed: %d\n", ret);
9327 	}
9328 
9329 	/* Save the current PTP time so that we can restore the time after the
9330 	 * reset completes.
9331 	 */
9332 	i40e_ptp_save_hw_time(pf);
9333 }
9334 
9335 /**
9336  * i40e_send_version - update firmware with driver version
9337  * @pf: PF struct
9338  */
9339 static void i40e_send_version(struct i40e_pf *pf)
9340 {
9341 	struct i40e_driver_version dv;
9342 
9343 	dv.major_version = DRV_VERSION_MAJOR;
9344 	dv.minor_version = DRV_VERSION_MINOR;
9345 	dv.build_version = DRV_VERSION_BUILD;
9346 	dv.subbuild_version = 0;
9347 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9348 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9349 }
9350 
9351 /**
9352  * i40e_get_oem_version - get OEM specific version information
9353  * @hw: pointer to the hardware structure
9354  **/
9355 static void i40e_get_oem_version(struct i40e_hw *hw)
9356 {
9357 	u16 block_offset = 0xffff;
9358 	u16 block_length = 0;
9359 	u16 capabilities = 0;
9360 	u16 gen_snap = 0;
9361 	u16 release = 0;
9362 
9363 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9364 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9365 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9366 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9367 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9368 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9369 #define I40E_NVM_OEM_LENGTH			3
9370 
9371 	/* Check if pointer to OEM version block is valid. */
9372 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9373 	if (block_offset == 0xffff)
9374 		return;
9375 
9376 	/* Check if OEM version block has correct length. */
9377 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9378 			   &block_length);
9379 	if (block_length < I40E_NVM_OEM_LENGTH)
9380 		return;
9381 
9382 	/* Check if OEM version format is as expected. */
9383 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9384 			   &capabilities);
9385 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9386 		return;
9387 
9388 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9389 			   &gen_snap);
9390 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9391 			   &release);
9392 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9393 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9394 }
9395 
9396 /**
9397  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9398  * @pf: board private structure
9399  **/
9400 static int i40e_reset(struct i40e_pf *pf)
9401 {
9402 	struct i40e_hw *hw = &pf->hw;
9403 	i40e_status ret;
9404 
9405 	ret = i40e_pf_reset(hw);
9406 	if (ret) {
9407 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9408 		set_bit(__I40E_RESET_FAILED, pf->state);
9409 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9410 	} else {
9411 		pf->pfr_count++;
9412 	}
9413 	return ret;
9414 }
9415 
9416 /**
9417  * i40e_rebuild - rebuild using a saved config
9418  * @pf: board private structure
9419  * @reinit: if the Main VSI needs to re-initialized.
9420  * @lock_acquired: indicates whether or not the lock has been acquired
9421  * before this function was called.
9422  **/
9423 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9424 {
9425 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
9426 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9427 	struct i40e_hw *hw = &pf->hw;
9428 	u8 set_fc_aq_fail = 0;
9429 	i40e_status ret;
9430 	u32 val;
9431 	int v;
9432 
9433 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9434 	    i40e_check_recovery_mode(pf)) {
9435 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
9436 	}
9437 
9438 	if (test_bit(__I40E_DOWN, pf->state) &&
9439 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
9440 	    !old_recovery_mode_bit)
9441 		goto clear_recovery;
9442 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9443 
9444 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9445 	ret = i40e_init_adminq(&pf->hw);
9446 	if (ret) {
9447 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9448 			 i40e_stat_str(&pf->hw, ret),
9449 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9450 		goto clear_recovery;
9451 	}
9452 	i40e_get_oem_version(&pf->hw);
9453 
9454 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9455 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9456 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9457 		/* The following delay is necessary for 4.33 firmware and older
9458 		 * to recover after EMP reset. 200 ms should suffice but we
9459 		 * put here 300 ms to be sure that FW is ready to operate
9460 		 * after reset.
9461 		 */
9462 		mdelay(300);
9463 	}
9464 
9465 	/* re-verify the eeprom if we just had an EMP reset */
9466 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9467 		i40e_verify_eeprom(pf);
9468 
9469 	/* if we are going out of or into recovery mode we have to act
9470 	 * accordingly with regard to resources initialization
9471 	 * and deinitialization
9472 	 */
9473 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
9474 	    old_recovery_mode_bit) {
9475 		if (i40e_get_capabilities(pf,
9476 					  i40e_aqc_opc_list_func_capabilities))
9477 			goto end_unlock;
9478 
9479 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
9480 			/* we're staying in recovery mode so we'll reinitialize
9481 			 * misc vector here
9482 			 */
9483 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
9484 				goto end_unlock;
9485 		} else {
9486 			if (!lock_acquired)
9487 				rtnl_lock();
9488 			/* we're going out of recovery mode so we'll free
9489 			 * the IRQ allocated specifically for recovery mode
9490 			 * and restore the interrupt scheme
9491 			 */
9492 			free_irq(pf->pdev->irq, pf);
9493 			i40e_clear_interrupt_scheme(pf);
9494 			if (i40e_restore_interrupt_scheme(pf))
9495 				goto end_unlock;
9496 		}
9497 
9498 		/* tell the firmware that we're starting */
9499 		i40e_send_version(pf);
9500 
9501 		/* bail out in case recovery mode was detected, as there is
9502 		 * no need for further configuration.
9503 		 */
9504 		goto end_unlock;
9505 	}
9506 
9507 	i40e_clear_pxe_mode(hw);
9508 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9509 	if (ret)
9510 		goto end_core_reset;
9511 
9512 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9513 				hw->func_caps.num_rx_qp, 0, 0);
9514 	if (ret) {
9515 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9516 		goto end_core_reset;
9517 	}
9518 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9519 	if (ret) {
9520 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9521 		goto end_core_reset;
9522 	}
9523 
9524 	/* Enable FW to write a default DCB config on link-up */
9525 	i40e_aq_set_dcb_parameters(hw, true, NULL);
9526 
9527 #ifdef CONFIG_I40E_DCB
9528 	ret = i40e_init_pf_dcb(pf);
9529 	if (ret) {
9530 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9531 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9532 		/* Continue without DCB enabled */
9533 	}
9534 #endif /* CONFIG_I40E_DCB */
9535 	/* do basic switch setup */
9536 	if (!lock_acquired)
9537 		rtnl_lock();
9538 	ret = i40e_setup_pf_switch(pf, reinit);
9539 	if (ret)
9540 		goto end_unlock;
9541 
9542 	/* The driver only wants link up/down and module qualification
9543 	 * reports from firmware.  Note the negative logic.
9544 	 */
9545 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
9546 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
9547 					 I40E_AQ_EVENT_MEDIA_NA |
9548 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9549 	if (ret)
9550 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9551 			 i40e_stat_str(&pf->hw, ret),
9552 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9553 
9554 	/* make sure our flow control settings are restored */
9555 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9556 	if (ret)
9557 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9558 			i40e_stat_str(&pf->hw, ret),
9559 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9560 
9561 	/* Rebuild the VSIs and VEBs that existed before reset.
9562 	 * They are still in our local switch element arrays, so only
9563 	 * need to rebuild the switch model in the HW.
9564 	 *
9565 	 * If there were VEBs but the reconstitution failed, we'll try
9566 	 * try to recover minimal use by getting the basic PF VSI working.
9567 	 */
9568 	if (vsi->uplink_seid != pf->mac_seid) {
9569 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9570 		/* find the one VEB connected to the MAC, and find orphans */
9571 		for (v = 0; v < I40E_MAX_VEB; v++) {
9572 			if (!pf->veb[v])
9573 				continue;
9574 
9575 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9576 			    pf->veb[v]->uplink_seid == 0) {
9577 				ret = i40e_reconstitute_veb(pf->veb[v]);
9578 
9579 				if (!ret)
9580 					continue;
9581 
9582 				/* If Main VEB failed, we're in deep doodoo,
9583 				 * so give up rebuilding the switch and set up
9584 				 * for minimal rebuild of PF VSI.
9585 				 * If orphan failed, we'll report the error
9586 				 * but try to keep going.
9587 				 */
9588 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9589 					dev_info(&pf->pdev->dev,
9590 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9591 						 ret);
9592 					vsi->uplink_seid = pf->mac_seid;
9593 					break;
9594 				} else if (pf->veb[v]->uplink_seid == 0) {
9595 					dev_info(&pf->pdev->dev,
9596 						 "rebuild of orphan VEB failed: %d\n",
9597 						 ret);
9598 				}
9599 			}
9600 		}
9601 	}
9602 
9603 	if (vsi->uplink_seid == pf->mac_seid) {
9604 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9605 		/* no VEB, so rebuild only the Main VSI */
9606 		ret = i40e_add_vsi(vsi);
9607 		if (ret) {
9608 			dev_info(&pf->pdev->dev,
9609 				 "rebuild of Main VSI failed: %d\n", ret);
9610 			goto end_unlock;
9611 		}
9612 	}
9613 
9614 	if (vsi->mqprio_qopt.max_rate[0]) {
9615 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9616 		u64 credits = 0;
9617 
9618 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9619 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9620 		if (ret)
9621 			goto end_unlock;
9622 
9623 		credits = max_tx_rate;
9624 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
9625 		dev_dbg(&vsi->back->pdev->dev,
9626 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9627 			max_tx_rate,
9628 			credits,
9629 			vsi->seid);
9630 	}
9631 
9632 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9633 	if (ret)
9634 		goto end_unlock;
9635 
9636 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9637 	 * for this main VSI if they exist
9638 	 */
9639 	ret = i40e_rebuild_channels(vsi);
9640 	if (ret)
9641 		goto end_unlock;
9642 
9643 	/* Reconfigure hardware for allowing smaller MSS in the case
9644 	 * of TSO, so that we avoid the MDD being fired and causing
9645 	 * a reset in the case of small MSS+TSO.
9646 	 */
9647 #define I40E_REG_MSS          0x000E64DC
9648 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9649 #define I40E_64BYTE_MSS       0x400000
9650 	val = rd32(hw, I40E_REG_MSS);
9651 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9652 		val &= ~I40E_REG_MSS_MIN_MASK;
9653 		val |= I40E_64BYTE_MSS;
9654 		wr32(hw, I40E_REG_MSS, val);
9655 	}
9656 
9657 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9658 		msleep(75);
9659 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9660 		if (ret)
9661 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9662 				 i40e_stat_str(&pf->hw, ret),
9663 				 i40e_aq_str(&pf->hw,
9664 					     pf->hw.aq.asq_last_status));
9665 	}
9666 	/* reinit the misc interrupt */
9667 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9668 		ret = i40e_setup_misc_vector(pf);
9669 
9670 	/* Add a filter to drop all Flow control frames from any VSI from being
9671 	 * transmitted. By doing so we stop a malicious VF from sending out
9672 	 * PAUSE or PFC frames and potentially controlling traffic for other
9673 	 * PF/VF VSIs.
9674 	 * The FW can still send Flow control frames if enabled.
9675 	 */
9676 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9677 						       pf->main_vsi_seid);
9678 
9679 	/* restart the VSIs that were rebuilt and running before the reset */
9680 	i40e_pf_unquiesce_all_vsi(pf);
9681 
9682 	/* Release the RTNL lock before we start resetting VFs */
9683 	if (!lock_acquired)
9684 		rtnl_unlock();
9685 
9686 	/* Restore promiscuous settings */
9687 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9688 	if (ret)
9689 		dev_warn(&pf->pdev->dev,
9690 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9691 			 pf->cur_promisc ? "on" : "off",
9692 			 i40e_stat_str(&pf->hw, ret),
9693 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9694 
9695 	i40e_reset_all_vfs(pf, true);
9696 
9697 	/* tell the firmware that we're starting */
9698 	i40e_send_version(pf);
9699 
9700 	/* We've already released the lock, so don't do it again */
9701 	goto end_core_reset;
9702 
9703 end_unlock:
9704 	if (!lock_acquired)
9705 		rtnl_unlock();
9706 end_core_reset:
9707 	clear_bit(__I40E_RESET_FAILED, pf->state);
9708 clear_recovery:
9709 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9710 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
9711 }
9712 
9713 /**
9714  * i40e_reset_and_rebuild - reset and rebuild using a saved config
9715  * @pf: board private structure
9716  * @reinit: if the Main VSI needs to re-initialized.
9717  * @lock_acquired: indicates whether or not the lock has been acquired
9718  * before this function was called.
9719  **/
9720 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9721 				   bool lock_acquired)
9722 {
9723 	int ret;
9724 	/* Now we wait for GRST to settle out.
9725 	 * We don't have to delete the VEBs or VSIs from the hw switch
9726 	 * because the reset will make them disappear.
9727 	 */
9728 	ret = i40e_reset(pf);
9729 	if (!ret)
9730 		i40e_rebuild(pf, reinit, lock_acquired);
9731 }
9732 
9733 /**
9734  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9735  * @pf: board private structure
9736  *
9737  * Close up the VFs and other things in prep for a Core Reset,
9738  * then get ready to rebuild the world.
9739  * @lock_acquired: indicates whether or not the lock has been acquired
9740  * before this function was called.
9741  **/
9742 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9743 {
9744 	i40e_prep_for_reset(pf, lock_acquired);
9745 	i40e_reset_and_rebuild(pf, false, lock_acquired);
9746 }
9747 
9748 /**
9749  * i40e_handle_mdd_event
9750  * @pf: pointer to the PF structure
9751  *
9752  * Called from the MDD irq handler to identify possibly malicious vfs
9753  **/
9754 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9755 {
9756 	struct i40e_hw *hw = &pf->hw;
9757 	bool mdd_detected = false;
9758 	struct i40e_vf *vf;
9759 	u32 reg;
9760 	int i;
9761 
9762 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9763 		return;
9764 
9765 	/* find what triggered the MDD event */
9766 	reg = rd32(hw, I40E_GL_MDET_TX);
9767 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9768 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9769 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
9770 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9771 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
9772 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9773 				I40E_GL_MDET_TX_EVENT_SHIFT;
9774 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9775 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
9776 				pf->hw.func_caps.base_queue;
9777 		if (netif_msg_tx_err(pf))
9778 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9779 				 event, queue, pf_num, vf_num);
9780 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9781 		mdd_detected = true;
9782 	}
9783 	reg = rd32(hw, I40E_GL_MDET_RX);
9784 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9785 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9786 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
9787 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9788 				I40E_GL_MDET_RX_EVENT_SHIFT;
9789 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9790 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
9791 				pf->hw.func_caps.base_queue;
9792 		if (netif_msg_rx_err(pf))
9793 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9794 				 event, queue, func);
9795 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9796 		mdd_detected = true;
9797 	}
9798 
9799 	if (mdd_detected) {
9800 		reg = rd32(hw, I40E_PF_MDET_TX);
9801 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9802 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9803 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
9804 		}
9805 		reg = rd32(hw, I40E_PF_MDET_RX);
9806 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9807 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9808 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
9809 		}
9810 	}
9811 
9812 	/* see if one of the VFs needs its hand slapped */
9813 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9814 		vf = &(pf->vf[i]);
9815 		reg = rd32(hw, I40E_VP_MDET_TX(i));
9816 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9817 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9818 			vf->num_mdd_events++;
9819 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9820 				 i);
9821 			dev_info(&pf->pdev->dev,
9822 				 "Use PF Control I/F to re-enable the VF\n");
9823 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9824 		}
9825 
9826 		reg = rd32(hw, I40E_VP_MDET_RX(i));
9827 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9828 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9829 			vf->num_mdd_events++;
9830 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9831 				 i);
9832 			dev_info(&pf->pdev->dev,
9833 				 "Use PF Control I/F to re-enable the VF\n");
9834 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9835 		}
9836 	}
9837 
9838 	/* re-enable mdd interrupt cause */
9839 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9840 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9841 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9842 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9843 	i40e_flush(hw);
9844 }
9845 
9846 static const char *i40e_tunnel_name(u8 type)
9847 {
9848 	switch (type) {
9849 	case UDP_TUNNEL_TYPE_VXLAN:
9850 		return "vxlan";
9851 	case UDP_TUNNEL_TYPE_GENEVE:
9852 		return "geneve";
9853 	default:
9854 		return "unknown";
9855 	}
9856 }
9857 
9858 /**
9859  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9860  * @pf: board private structure
9861  **/
9862 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9863 {
9864 	int i;
9865 
9866 	/* loop through and set pending bit for all active UDP filters */
9867 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9868 		if (pf->udp_ports[i].port)
9869 			pf->pending_udp_bitmap |= BIT_ULL(i);
9870 	}
9871 
9872 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
9873 }
9874 
9875 /**
9876  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9877  * @pf: board private structure
9878  **/
9879 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9880 {
9881 	struct i40e_hw *hw = &pf->hw;
9882 	u8 filter_index, type;
9883 	u16 port;
9884 	int i;
9885 
9886 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
9887 		return;
9888 
9889 	/* acquire RTNL to maintain state of flags and port requests */
9890 	rtnl_lock();
9891 
9892 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9893 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9894 			struct i40e_udp_port_config *udp_port;
9895 			i40e_status ret = 0;
9896 
9897 			udp_port = &pf->udp_ports[i];
9898 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
9899 
9900 			port = READ_ONCE(udp_port->port);
9901 			type = READ_ONCE(udp_port->type);
9902 			filter_index = READ_ONCE(udp_port->filter_index);
9903 
9904 			/* release RTNL while we wait on AQ command */
9905 			rtnl_unlock();
9906 
9907 			if (port)
9908 				ret = i40e_aq_add_udp_tunnel(hw, port,
9909 							     type,
9910 							     &filter_index,
9911 							     NULL);
9912 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
9913 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
9914 							     NULL);
9915 
9916 			/* reacquire RTNL so we can update filter_index */
9917 			rtnl_lock();
9918 
9919 			if (ret) {
9920 				dev_info(&pf->pdev->dev,
9921 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9922 					 i40e_tunnel_name(type),
9923 					 port ? "add" : "delete",
9924 					 port,
9925 					 filter_index,
9926 					 i40e_stat_str(&pf->hw, ret),
9927 					 i40e_aq_str(&pf->hw,
9928 						     pf->hw.aq.asq_last_status));
9929 				if (port) {
9930 					/* failed to add, just reset port,
9931 					 * drop pending bit for any deletion
9932 					 */
9933 					udp_port->port = 0;
9934 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
9935 				}
9936 			} else if (port) {
9937 				/* record filter index on success */
9938 				udp_port->filter_index = filter_index;
9939 			}
9940 		}
9941 	}
9942 
9943 	rtnl_unlock();
9944 }
9945 
9946 /**
9947  * i40e_service_task - Run the driver's async subtasks
9948  * @work: pointer to work_struct containing our data
9949  **/
9950 static void i40e_service_task(struct work_struct *work)
9951 {
9952 	struct i40e_pf *pf = container_of(work,
9953 					  struct i40e_pf,
9954 					  service_task);
9955 	unsigned long start_time = jiffies;
9956 
9957 	/* don't bother with service tasks if a reset is in progress */
9958 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
9959 	    test_bit(__I40E_SUSPENDED, pf->state))
9960 		return;
9961 
9962 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9963 		return;
9964 
9965 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
9966 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
9967 		i40e_sync_filters_subtask(pf);
9968 		i40e_reset_subtask(pf);
9969 		i40e_handle_mdd_event(pf);
9970 		i40e_vc_process_vflr_event(pf);
9971 		i40e_watchdog_subtask(pf);
9972 		i40e_fdir_reinit_subtask(pf);
9973 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
9974 			/* Client subtask will reopen next time through. */
9975 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
9976 							   true);
9977 		} else {
9978 			i40e_client_subtask(pf);
9979 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
9980 					       pf->state))
9981 				i40e_notify_client_of_l2_param_changes(
9982 								pf->vsi[pf->lan_vsi]);
9983 		}
9984 		i40e_sync_filters_subtask(pf);
9985 		i40e_sync_udp_filters_subtask(pf);
9986 	} else {
9987 		i40e_reset_subtask(pf);
9988 	}
9989 
9990 	i40e_clean_adminq_subtask(pf);
9991 
9992 	/* flush memory to make sure state is correct before next watchdog */
9993 	smp_mb__before_atomic();
9994 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
9995 
9996 	/* If the tasks have taken longer than one timer cycle or there
9997 	 * is more work to be done, reschedule the service task now
9998 	 * rather than wait for the timer to tick again.
9999 	 */
10000 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10001 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10002 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10003 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10004 		i40e_service_event_schedule(pf);
10005 }
10006 
10007 /**
10008  * i40e_service_timer - timer callback
10009  * @data: pointer to PF struct
10010  **/
10011 static void i40e_service_timer(struct timer_list *t)
10012 {
10013 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10014 
10015 	mod_timer(&pf->service_timer,
10016 		  round_jiffies(jiffies + pf->service_timer_period));
10017 	i40e_service_event_schedule(pf);
10018 }
10019 
10020 /**
10021  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10022  * @vsi: the VSI being configured
10023  **/
10024 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10025 {
10026 	struct i40e_pf *pf = vsi->back;
10027 
10028 	switch (vsi->type) {
10029 	case I40E_VSI_MAIN:
10030 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10031 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10032 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
10033 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10034 			vsi->num_q_vectors = pf->num_lan_msix;
10035 		else
10036 			vsi->num_q_vectors = 1;
10037 
10038 		break;
10039 
10040 	case I40E_VSI_FDIR:
10041 		vsi->alloc_queue_pairs = 1;
10042 		vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
10043 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
10044 		vsi->num_q_vectors = pf->num_fdsb_msix;
10045 		break;
10046 
10047 	case I40E_VSI_VMDQ2:
10048 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10049 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10050 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
10051 		vsi->num_q_vectors = pf->num_vmdq_msix;
10052 		break;
10053 
10054 	case I40E_VSI_SRIOV:
10055 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10056 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10057 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
10058 		break;
10059 
10060 	default:
10061 		WARN_ON(1);
10062 		return -ENODATA;
10063 	}
10064 
10065 	return 0;
10066 }
10067 
10068 /**
10069  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10070  * @vsi: VSI pointer
10071  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10072  *
10073  * On error: returns error code (negative)
10074  * On success: returns 0
10075  **/
10076 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10077 {
10078 	struct i40e_ring **next_rings;
10079 	int size;
10080 	int ret = 0;
10081 
10082 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10083 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10084 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10085 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10086 	if (!vsi->tx_rings)
10087 		return -ENOMEM;
10088 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10089 	if (i40e_enabled_xdp_vsi(vsi)) {
10090 		vsi->xdp_rings = next_rings;
10091 		next_rings += vsi->alloc_queue_pairs;
10092 	}
10093 	vsi->rx_rings = next_rings;
10094 
10095 	if (alloc_qvectors) {
10096 		/* allocate memory for q_vector pointers */
10097 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10098 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10099 		if (!vsi->q_vectors) {
10100 			ret = -ENOMEM;
10101 			goto err_vectors;
10102 		}
10103 	}
10104 	return ret;
10105 
10106 err_vectors:
10107 	kfree(vsi->tx_rings);
10108 	return ret;
10109 }
10110 
10111 /**
10112  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10113  * @pf: board private structure
10114  * @type: type of VSI
10115  *
10116  * On error: returns error code (negative)
10117  * On success: returns vsi index in PF (positive)
10118  **/
10119 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10120 {
10121 	int ret = -ENODEV;
10122 	struct i40e_vsi *vsi;
10123 	int vsi_idx;
10124 	int i;
10125 
10126 	/* Need to protect the allocation of the VSIs at the PF level */
10127 	mutex_lock(&pf->switch_mutex);
10128 
10129 	/* VSI list may be fragmented if VSI creation/destruction has
10130 	 * been happening.  We can afford to do a quick scan to look
10131 	 * for any free VSIs in the list.
10132 	 *
10133 	 * find next empty vsi slot, looping back around if necessary
10134 	 */
10135 	i = pf->next_vsi;
10136 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10137 		i++;
10138 	if (i >= pf->num_alloc_vsi) {
10139 		i = 0;
10140 		while (i < pf->next_vsi && pf->vsi[i])
10141 			i++;
10142 	}
10143 
10144 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10145 		vsi_idx = i;             /* Found one! */
10146 	} else {
10147 		ret = -ENODEV;
10148 		goto unlock_pf;  /* out of VSI slots! */
10149 	}
10150 	pf->next_vsi = ++i;
10151 
10152 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10153 	if (!vsi) {
10154 		ret = -ENOMEM;
10155 		goto unlock_pf;
10156 	}
10157 	vsi->type = type;
10158 	vsi->back = pf;
10159 	set_bit(__I40E_VSI_DOWN, vsi->state);
10160 	vsi->flags = 0;
10161 	vsi->idx = vsi_idx;
10162 	vsi->int_rate_limit = 0;
10163 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10164 				pf->rss_table_size : 64;
10165 	vsi->netdev_registered = false;
10166 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10167 	hash_init(vsi->mac_filter_hash);
10168 	vsi->irqs_ready = false;
10169 
10170 	if (type == I40E_VSI_MAIN) {
10171 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10172 		if (!vsi->af_xdp_zc_qps)
10173 			goto err_rings;
10174 	}
10175 
10176 	ret = i40e_set_num_rings_in_vsi(vsi);
10177 	if (ret)
10178 		goto err_rings;
10179 
10180 	ret = i40e_vsi_alloc_arrays(vsi, true);
10181 	if (ret)
10182 		goto err_rings;
10183 
10184 	/* Setup default MSIX irq handler for VSI */
10185 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10186 
10187 	/* Initialize VSI lock */
10188 	spin_lock_init(&vsi->mac_filter_hash_lock);
10189 	pf->vsi[vsi_idx] = vsi;
10190 	ret = vsi_idx;
10191 	goto unlock_pf;
10192 
10193 err_rings:
10194 	bitmap_free(vsi->af_xdp_zc_qps);
10195 	pf->next_vsi = i - 1;
10196 	kfree(vsi);
10197 unlock_pf:
10198 	mutex_unlock(&pf->switch_mutex);
10199 	return ret;
10200 }
10201 
10202 /**
10203  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10204  * @vsi: VSI pointer
10205  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10206  *
10207  * On error: returns error code (negative)
10208  * On success: returns 0
10209  **/
10210 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10211 {
10212 	/* free the ring and vector containers */
10213 	if (free_qvectors) {
10214 		kfree(vsi->q_vectors);
10215 		vsi->q_vectors = NULL;
10216 	}
10217 	kfree(vsi->tx_rings);
10218 	vsi->tx_rings = NULL;
10219 	vsi->rx_rings = NULL;
10220 	vsi->xdp_rings = NULL;
10221 }
10222 
10223 /**
10224  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10225  * and lookup table
10226  * @vsi: Pointer to VSI structure
10227  */
10228 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10229 {
10230 	if (!vsi)
10231 		return;
10232 
10233 	kfree(vsi->rss_hkey_user);
10234 	vsi->rss_hkey_user = NULL;
10235 
10236 	kfree(vsi->rss_lut_user);
10237 	vsi->rss_lut_user = NULL;
10238 }
10239 
10240 /**
10241  * i40e_vsi_clear - Deallocate the VSI provided
10242  * @vsi: the VSI being un-configured
10243  **/
10244 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10245 {
10246 	struct i40e_pf *pf;
10247 
10248 	if (!vsi)
10249 		return 0;
10250 
10251 	if (!vsi->back)
10252 		goto free_vsi;
10253 	pf = vsi->back;
10254 
10255 	mutex_lock(&pf->switch_mutex);
10256 	if (!pf->vsi[vsi->idx]) {
10257 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10258 			vsi->idx, vsi->idx, vsi->type);
10259 		goto unlock_vsi;
10260 	}
10261 
10262 	if (pf->vsi[vsi->idx] != vsi) {
10263 		dev_err(&pf->pdev->dev,
10264 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10265 			pf->vsi[vsi->idx]->idx,
10266 			pf->vsi[vsi->idx]->type,
10267 			vsi->idx, vsi->type);
10268 		goto unlock_vsi;
10269 	}
10270 
10271 	/* updates the PF for this cleared vsi */
10272 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10273 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10274 
10275 	bitmap_free(vsi->af_xdp_zc_qps);
10276 	i40e_vsi_free_arrays(vsi, true);
10277 	i40e_clear_rss_config_user(vsi);
10278 
10279 	pf->vsi[vsi->idx] = NULL;
10280 	if (vsi->idx < pf->next_vsi)
10281 		pf->next_vsi = vsi->idx;
10282 
10283 unlock_vsi:
10284 	mutex_unlock(&pf->switch_mutex);
10285 free_vsi:
10286 	kfree(vsi);
10287 
10288 	return 0;
10289 }
10290 
10291 /**
10292  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10293  * @vsi: the VSI being cleaned
10294  **/
10295 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10296 {
10297 	int i;
10298 
10299 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10300 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10301 			kfree_rcu(vsi->tx_rings[i], rcu);
10302 			vsi->tx_rings[i] = NULL;
10303 			vsi->rx_rings[i] = NULL;
10304 			if (vsi->xdp_rings)
10305 				vsi->xdp_rings[i] = NULL;
10306 		}
10307 	}
10308 }
10309 
10310 /**
10311  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10312  * @vsi: the VSI being configured
10313  **/
10314 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10315 {
10316 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10317 	struct i40e_pf *pf = vsi->back;
10318 	struct i40e_ring *ring;
10319 
10320 	/* Set basic values in the rings to be used later during open() */
10321 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10322 		/* allocate space for both Tx and Rx in one shot */
10323 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10324 		if (!ring)
10325 			goto err_out;
10326 
10327 		ring->queue_index = i;
10328 		ring->reg_idx = vsi->base_queue + i;
10329 		ring->ring_active = false;
10330 		ring->vsi = vsi;
10331 		ring->netdev = vsi->netdev;
10332 		ring->dev = &pf->pdev->dev;
10333 		ring->count = vsi->num_desc;
10334 		ring->size = 0;
10335 		ring->dcb_tc = 0;
10336 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10337 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10338 		ring->itr_setting = pf->tx_itr_default;
10339 		vsi->tx_rings[i] = ring++;
10340 
10341 		if (!i40e_enabled_xdp_vsi(vsi))
10342 			goto setup_rx;
10343 
10344 		ring->queue_index = vsi->alloc_queue_pairs + i;
10345 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10346 		ring->ring_active = false;
10347 		ring->vsi = vsi;
10348 		ring->netdev = NULL;
10349 		ring->dev = &pf->pdev->dev;
10350 		ring->count = vsi->num_desc;
10351 		ring->size = 0;
10352 		ring->dcb_tc = 0;
10353 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10354 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10355 		set_ring_xdp(ring);
10356 		ring->itr_setting = pf->tx_itr_default;
10357 		vsi->xdp_rings[i] = ring++;
10358 
10359 setup_rx:
10360 		ring->queue_index = i;
10361 		ring->reg_idx = vsi->base_queue + i;
10362 		ring->ring_active = false;
10363 		ring->vsi = vsi;
10364 		ring->netdev = vsi->netdev;
10365 		ring->dev = &pf->pdev->dev;
10366 		ring->count = vsi->num_desc;
10367 		ring->size = 0;
10368 		ring->dcb_tc = 0;
10369 		ring->itr_setting = pf->rx_itr_default;
10370 		vsi->rx_rings[i] = ring;
10371 	}
10372 
10373 	return 0;
10374 
10375 err_out:
10376 	i40e_vsi_clear_rings(vsi);
10377 	return -ENOMEM;
10378 }
10379 
10380 /**
10381  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10382  * @pf: board private structure
10383  * @vectors: the number of MSI-X vectors to request
10384  *
10385  * Returns the number of vectors reserved, or error
10386  **/
10387 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10388 {
10389 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10390 					I40E_MIN_MSIX, vectors);
10391 	if (vectors < 0) {
10392 		dev_info(&pf->pdev->dev,
10393 			 "MSI-X vector reservation failed: %d\n", vectors);
10394 		vectors = 0;
10395 	}
10396 
10397 	return vectors;
10398 }
10399 
10400 /**
10401  * i40e_init_msix - Setup the MSIX capability
10402  * @pf: board private structure
10403  *
10404  * Work with the OS to set up the MSIX vectors needed.
10405  *
10406  * Returns the number of vectors reserved or negative on failure
10407  **/
10408 static int i40e_init_msix(struct i40e_pf *pf)
10409 {
10410 	struct i40e_hw *hw = &pf->hw;
10411 	int cpus, extra_vectors;
10412 	int vectors_left;
10413 	int v_budget, i;
10414 	int v_actual;
10415 	int iwarp_requested = 0;
10416 
10417 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10418 		return -ENODEV;
10419 
10420 	/* The number of vectors we'll request will be comprised of:
10421 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10422 	 *   - The number of LAN queue pairs
10423 	 *	- Queues being used for RSS.
10424 	 *		We don't need as many as max_rss_size vectors.
10425 	 *		use rss_size instead in the calculation since that
10426 	 *		is governed by number of cpus in the system.
10427 	 *	- assumes symmetric Tx/Rx pairing
10428 	 *   - The number of VMDq pairs
10429 	 *   - The CPU count within the NUMA node if iWARP is enabled
10430 	 * Once we count this up, try the request.
10431 	 *
10432 	 * If we can't get what we want, we'll simplify to nearly nothing
10433 	 * and try again.  If that still fails, we punt.
10434 	 */
10435 	vectors_left = hw->func_caps.num_msix_vectors;
10436 	v_budget = 0;
10437 
10438 	/* reserve one vector for miscellaneous handler */
10439 	if (vectors_left) {
10440 		v_budget++;
10441 		vectors_left--;
10442 	}
10443 
10444 	/* reserve some vectors for the main PF traffic queues. Initially we
10445 	 * only reserve at most 50% of the available vectors, in the case that
10446 	 * the number of online CPUs is large. This ensures that we can enable
10447 	 * extra features as well. Once we've enabled the other features, we
10448 	 * will use any remaining vectors to reach as close as we can to the
10449 	 * number of online CPUs.
10450 	 */
10451 	cpus = num_online_cpus();
10452 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10453 	vectors_left -= pf->num_lan_msix;
10454 
10455 	/* reserve one vector for sideband flow director */
10456 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10457 		if (vectors_left) {
10458 			pf->num_fdsb_msix = 1;
10459 			v_budget++;
10460 			vectors_left--;
10461 		} else {
10462 			pf->num_fdsb_msix = 0;
10463 		}
10464 	}
10465 
10466 	/* can we reserve enough for iWARP? */
10467 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10468 		iwarp_requested = pf->num_iwarp_msix;
10469 
10470 		if (!vectors_left)
10471 			pf->num_iwarp_msix = 0;
10472 		else if (vectors_left < pf->num_iwarp_msix)
10473 			pf->num_iwarp_msix = 1;
10474 		v_budget += pf->num_iwarp_msix;
10475 		vectors_left -= pf->num_iwarp_msix;
10476 	}
10477 
10478 	/* any vectors left over go for VMDq support */
10479 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10480 		if (!vectors_left) {
10481 			pf->num_vmdq_msix = 0;
10482 			pf->num_vmdq_qps = 0;
10483 		} else {
10484 			int vmdq_vecs_wanted =
10485 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
10486 			int vmdq_vecs =
10487 				min_t(int, vectors_left, vmdq_vecs_wanted);
10488 
10489 			/* if we're short on vectors for what's desired, we limit
10490 			 * the queues per vmdq.  If this is still more than are
10491 			 * available, the user will need to change the number of
10492 			 * queues/vectors used by the PF later with the ethtool
10493 			 * channels command
10494 			 */
10495 			if (vectors_left < vmdq_vecs_wanted) {
10496 				pf->num_vmdq_qps = 1;
10497 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
10498 				vmdq_vecs = min_t(int,
10499 						  vectors_left,
10500 						  vmdq_vecs_wanted);
10501 			}
10502 			pf->num_vmdq_msix = pf->num_vmdq_qps;
10503 
10504 			v_budget += vmdq_vecs;
10505 			vectors_left -= vmdq_vecs;
10506 		}
10507 	}
10508 
10509 	/* On systems with a large number of SMP cores, we previously limited
10510 	 * the number of vectors for num_lan_msix to be at most 50% of the
10511 	 * available vectors, to allow for other features. Now, we add back
10512 	 * the remaining vectors. However, we ensure that the total
10513 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10514 	 * calculate the number of vectors we can add without going over the
10515 	 * cap of CPUs. For systems with a small number of CPUs this will be
10516 	 * zero.
10517 	 */
10518 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10519 	pf->num_lan_msix += extra_vectors;
10520 	vectors_left -= extra_vectors;
10521 
10522 	WARN(vectors_left < 0,
10523 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10524 
10525 	v_budget += pf->num_lan_msix;
10526 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10527 				   GFP_KERNEL);
10528 	if (!pf->msix_entries)
10529 		return -ENOMEM;
10530 
10531 	for (i = 0; i < v_budget; i++)
10532 		pf->msix_entries[i].entry = i;
10533 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10534 
10535 	if (v_actual < I40E_MIN_MSIX) {
10536 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10537 		kfree(pf->msix_entries);
10538 		pf->msix_entries = NULL;
10539 		pci_disable_msix(pf->pdev);
10540 		return -ENODEV;
10541 
10542 	} else if (v_actual == I40E_MIN_MSIX) {
10543 		/* Adjust for minimal MSIX use */
10544 		pf->num_vmdq_vsis = 0;
10545 		pf->num_vmdq_qps = 0;
10546 		pf->num_lan_qps = 1;
10547 		pf->num_lan_msix = 1;
10548 
10549 	} else if (v_actual != v_budget) {
10550 		/* If we have limited resources, we will start with no vectors
10551 		 * for the special features and then allocate vectors to some
10552 		 * of these features based on the policy and at the end disable
10553 		 * the features that did not get any vectors.
10554 		 */
10555 		int vec;
10556 
10557 		dev_info(&pf->pdev->dev,
10558 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10559 			 v_actual, v_budget);
10560 		/* reserve the misc vector */
10561 		vec = v_actual - 1;
10562 
10563 		/* Scale vector usage down */
10564 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
10565 		pf->num_vmdq_vsis = 1;
10566 		pf->num_vmdq_qps = 1;
10567 
10568 		/* partition out the remaining vectors */
10569 		switch (vec) {
10570 		case 2:
10571 			pf->num_lan_msix = 1;
10572 			break;
10573 		case 3:
10574 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10575 				pf->num_lan_msix = 1;
10576 				pf->num_iwarp_msix = 1;
10577 			} else {
10578 				pf->num_lan_msix = 2;
10579 			}
10580 			break;
10581 		default:
10582 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10583 				pf->num_iwarp_msix = min_t(int, (vec / 3),
10584 						 iwarp_requested);
10585 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
10586 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10587 			} else {
10588 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
10589 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10590 			}
10591 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10592 				pf->num_fdsb_msix = 1;
10593 				vec--;
10594 			}
10595 			pf->num_lan_msix = min_t(int,
10596 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10597 							      pf->num_lan_msix);
10598 			pf->num_lan_qps = pf->num_lan_msix;
10599 			break;
10600 		}
10601 	}
10602 
10603 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10604 	    (pf->num_fdsb_msix == 0)) {
10605 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10606 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10607 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10608 	}
10609 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10610 	    (pf->num_vmdq_msix == 0)) {
10611 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10612 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10613 	}
10614 
10615 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10616 	    (pf->num_iwarp_msix == 0)) {
10617 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10618 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10619 	}
10620 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10621 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10622 		   pf->num_lan_msix,
10623 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
10624 		   pf->num_fdsb_msix,
10625 		   pf->num_iwarp_msix);
10626 
10627 	return v_actual;
10628 }
10629 
10630 /**
10631  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10632  * @vsi: the VSI being configured
10633  * @v_idx: index of the vector in the vsi struct
10634  * @cpu: cpu to be used on affinity_mask
10635  *
10636  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
10637  **/
10638 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10639 {
10640 	struct i40e_q_vector *q_vector;
10641 
10642 	/* allocate q_vector */
10643 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10644 	if (!q_vector)
10645 		return -ENOMEM;
10646 
10647 	q_vector->vsi = vsi;
10648 	q_vector->v_idx = v_idx;
10649 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10650 
10651 	if (vsi->netdev)
10652 		netif_napi_add(vsi->netdev, &q_vector->napi,
10653 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
10654 
10655 	/* tie q_vector and vsi together */
10656 	vsi->q_vectors[v_idx] = q_vector;
10657 
10658 	return 0;
10659 }
10660 
10661 /**
10662  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10663  * @vsi: the VSI being configured
10664  *
10665  * We allocate one q_vector per queue interrupt.  If allocation fails we
10666  * return -ENOMEM.
10667  **/
10668 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10669 {
10670 	struct i40e_pf *pf = vsi->back;
10671 	int err, v_idx, num_q_vectors, current_cpu;
10672 
10673 	/* if not MSIX, give the one vector only to the LAN VSI */
10674 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10675 		num_q_vectors = vsi->num_q_vectors;
10676 	else if (vsi == pf->vsi[pf->lan_vsi])
10677 		num_q_vectors = 1;
10678 	else
10679 		return -EINVAL;
10680 
10681 	current_cpu = cpumask_first(cpu_online_mask);
10682 
10683 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10684 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10685 		if (err)
10686 			goto err_out;
10687 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10688 		if (unlikely(current_cpu >= nr_cpu_ids))
10689 			current_cpu = cpumask_first(cpu_online_mask);
10690 	}
10691 
10692 	return 0;
10693 
10694 err_out:
10695 	while (v_idx--)
10696 		i40e_free_q_vector(vsi, v_idx);
10697 
10698 	return err;
10699 }
10700 
10701 /**
10702  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10703  * @pf: board private structure to initialize
10704  **/
10705 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10706 {
10707 	int vectors = 0;
10708 	ssize_t size;
10709 
10710 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10711 		vectors = i40e_init_msix(pf);
10712 		if (vectors < 0) {
10713 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
10714 				       I40E_FLAG_IWARP_ENABLED	|
10715 				       I40E_FLAG_RSS_ENABLED	|
10716 				       I40E_FLAG_DCB_CAPABLE	|
10717 				       I40E_FLAG_DCB_ENABLED	|
10718 				       I40E_FLAG_SRIOV_ENABLED	|
10719 				       I40E_FLAG_FD_SB_ENABLED	|
10720 				       I40E_FLAG_FD_ATR_ENABLED	|
10721 				       I40E_FLAG_VMDQ_ENABLED);
10722 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10723 
10724 			/* rework the queue expectations without MSIX */
10725 			i40e_determine_queue_usage(pf);
10726 		}
10727 	}
10728 
10729 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10730 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10731 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10732 		vectors = pci_enable_msi(pf->pdev);
10733 		if (vectors < 0) {
10734 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10735 				 vectors);
10736 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10737 		}
10738 		vectors = 1;  /* one MSI or Legacy vector */
10739 	}
10740 
10741 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10742 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10743 
10744 	/* set up vector assignment tracking */
10745 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10746 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
10747 	if (!pf->irq_pile)
10748 		return -ENOMEM;
10749 
10750 	pf->irq_pile->num_entries = vectors;
10751 	pf->irq_pile->search_hint = 0;
10752 
10753 	/* track first vector for misc interrupts, ignore return */
10754 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10755 
10756 	return 0;
10757 }
10758 
10759 /**
10760  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10761  * @pf: private board data structure
10762  *
10763  * Restore the interrupt scheme that was cleared when we suspended the
10764  * device. This should be called during resume to re-allocate the q_vectors
10765  * and reacquire IRQs.
10766  */
10767 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10768 {
10769 	int err, i;
10770 
10771 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
10772 	 * scheme. We need to re-enabled them here in order to attempt to
10773 	 * re-acquire the MSI or MSI-X vectors
10774 	 */
10775 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10776 
10777 	err = i40e_init_interrupt_scheme(pf);
10778 	if (err)
10779 		return err;
10780 
10781 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
10782 	 * rings together again.
10783 	 */
10784 	for (i = 0; i < pf->num_alloc_vsi; i++) {
10785 		if (pf->vsi[i]) {
10786 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10787 			if (err)
10788 				goto err_unwind;
10789 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10790 		}
10791 	}
10792 
10793 	err = i40e_setup_misc_vector(pf);
10794 	if (err)
10795 		goto err_unwind;
10796 
10797 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
10798 		i40e_client_update_msix_info(pf);
10799 
10800 	return 0;
10801 
10802 err_unwind:
10803 	while (i--) {
10804 		if (pf->vsi[i])
10805 			i40e_vsi_free_q_vectors(pf->vsi[i]);
10806 	}
10807 
10808 	return err;
10809 }
10810 
10811 /**
10812  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
10813  * non queue events in recovery mode
10814  * @pf: board private structure
10815  *
10816  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
10817  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
10818  * This is handled differently than in recovery mode since no Tx/Rx resources
10819  * are being allocated.
10820  **/
10821 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
10822 {
10823 	int err;
10824 
10825 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10826 		err = i40e_setup_misc_vector(pf);
10827 
10828 		if (err) {
10829 			dev_info(&pf->pdev->dev,
10830 				 "MSI-X misc vector request failed, error %d\n",
10831 				 err);
10832 			return err;
10833 		}
10834 	} else {
10835 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
10836 
10837 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
10838 				  pf->int_name, pf);
10839 
10840 		if (err) {
10841 			dev_info(&pf->pdev->dev,
10842 				 "MSI/legacy misc vector request failed, error %d\n",
10843 				 err);
10844 			return err;
10845 		}
10846 		i40e_enable_misc_int_causes(pf);
10847 		i40e_irq_dynamic_enable_icr0(pf);
10848 	}
10849 
10850 	return 0;
10851 }
10852 
10853 /**
10854  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10855  * @pf: board private structure
10856  *
10857  * This sets up the handler for MSIX 0, which is used to manage the
10858  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
10859  * when in MSI or Legacy interrupt mode.
10860  **/
10861 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10862 {
10863 	struct i40e_hw *hw = &pf->hw;
10864 	int err = 0;
10865 
10866 	/* Only request the IRQ once, the first time through. */
10867 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10868 		err = request_irq(pf->msix_entries[0].vector,
10869 				  i40e_intr, 0, pf->int_name, pf);
10870 		if (err) {
10871 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10872 			dev_info(&pf->pdev->dev,
10873 				 "request_irq for %s failed: %d\n",
10874 				 pf->int_name, err);
10875 			return -EFAULT;
10876 		}
10877 	}
10878 
10879 	i40e_enable_misc_int_causes(pf);
10880 
10881 	/* associate no queues to the misc vector */
10882 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10883 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10884 
10885 	i40e_flush(hw);
10886 
10887 	i40e_irq_dynamic_enable_icr0(pf);
10888 
10889 	return err;
10890 }
10891 
10892 /**
10893  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10894  * @vsi: Pointer to vsi structure
10895  * @seed: Buffter to store the hash keys
10896  * @lut: Buffer to store the lookup table entries
10897  * @lut_size: Size of buffer to store the lookup table entries
10898  *
10899  * Return 0 on success, negative on failure
10900  */
10901 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10902 			   u8 *lut, u16 lut_size)
10903 {
10904 	struct i40e_pf *pf = vsi->back;
10905 	struct i40e_hw *hw = &pf->hw;
10906 	int ret = 0;
10907 
10908 	if (seed) {
10909 		ret = i40e_aq_get_rss_key(hw, vsi->id,
10910 			(struct i40e_aqc_get_set_rss_key_data *)seed);
10911 		if (ret) {
10912 			dev_info(&pf->pdev->dev,
10913 				 "Cannot get RSS key, err %s aq_err %s\n",
10914 				 i40e_stat_str(&pf->hw, ret),
10915 				 i40e_aq_str(&pf->hw,
10916 					     pf->hw.aq.asq_last_status));
10917 			return ret;
10918 		}
10919 	}
10920 
10921 	if (lut) {
10922 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10923 
10924 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10925 		if (ret) {
10926 			dev_info(&pf->pdev->dev,
10927 				 "Cannot get RSS lut, err %s aq_err %s\n",
10928 				 i40e_stat_str(&pf->hw, ret),
10929 				 i40e_aq_str(&pf->hw,
10930 					     pf->hw.aq.asq_last_status));
10931 			return ret;
10932 		}
10933 	}
10934 
10935 	return ret;
10936 }
10937 
10938 /**
10939  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10940  * @vsi: Pointer to vsi structure
10941  * @seed: RSS hash seed
10942  * @lut: Lookup table
10943  * @lut_size: Lookup table size
10944  *
10945  * Returns 0 on success, negative on failure
10946  **/
10947 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10948 			       const u8 *lut, u16 lut_size)
10949 {
10950 	struct i40e_pf *pf = vsi->back;
10951 	struct i40e_hw *hw = &pf->hw;
10952 	u16 vf_id = vsi->vf_id;
10953 	u8 i;
10954 
10955 	/* Fill out hash function seed */
10956 	if (seed) {
10957 		u32 *seed_dw = (u32 *)seed;
10958 
10959 		if (vsi->type == I40E_VSI_MAIN) {
10960 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10961 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10962 		} else if (vsi->type == I40E_VSI_SRIOV) {
10963 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10964 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10965 		} else {
10966 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10967 		}
10968 	}
10969 
10970 	if (lut) {
10971 		u32 *lut_dw = (u32 *)lut;
10972 
10973 		if (vsi->type == I40E_VSI_MAIN) {
10974 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
10975 				return -EINVAL;
10976 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10977 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10978 		} else if (vsi->type == I40E_VSI_SRIOV) {
10979 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10980 				return -EINVAL;
10981 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10982 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10983 		} else {
10984 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10985 		}
10986 	}
10987 	i40e_flush(hw);
10988 
10989 	return 0;
10990 }
10991 
10992 /**
10993  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10994  * @vsi: Pointer to VSI structure
10995  * @seed: Buffer to store the keys
10996  * @lut: Buffer to store the lookup table entries
10997  * @lut_size: Size of buffer to store the lookup table entries
10998  *
10999  * Returns 0 on success, negative on failure
11000  */
11001 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11002 			    u8 *lut, u16 lut_size)
11003 {
11004 	struct i40e_pf *pf = vsi->back;
11005 	struct i40e_hw *hw = &pf->hw;
11006 	u16 i;
11007 
11008 	if (seed) {
11009 		u32 *seed_dw = (u32 *)seed;
11010 
11011 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11012 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11013 	}
11014 	if (lut) {
11015 		u32 *lut_dw = (u32 *)lut;
11016 
11017 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11018 			return -EINVAL;
11019 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11020 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11021 	}
11022 
11023 	return 0;
11024 }
11025 
11026 /**
11027  * i40e_config_rss - Configure RSS keys and lut
11028  * @vsi: Pointer to VSI structure
11029  * @seed: RSS hash seed
11030  * @lut: Lookup table
11031  * @lut_size: Lookup table size
11032  *
11033  * Returns 0 on success, negative on failure
11034  */
11035 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11036 {
11037 	struct i40e_pf *pf = vsi->back;
11038 
11039 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11040 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11041 	else
11042 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11043 }
11044 
11045 /**
11046  * i40e_get_rss - Get RSS keys and lut
11047  * @vsi: Pointer to VSI structure
11048  * @seed: Buffer to store the keys
11049  * @lut: Buffer to store the lookup table entries
11050  * @lut_size: Size of buffer to store the lookup table entries
11051  *
11052  * Returns 0 on success, negative on failure
11053  */
11054 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11055 {
11056 	struct i40e_pf *pf = vsi->back;
11057 
11058 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11059 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11060 	else
11061 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11062 }
11063 
11064 /**
11065  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11066  * @pf: Pointer to board private structure
11067  * @lut: Lookup table
11068  * @rss_table_size: Lookup table size
11069  * @rss_size: Range of queue number for hashing
11070  */
11071 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11072 		       u16 rss_table_size, u16 rss_size)
11073 {
11074 	u16 i;
11075 
11076 	for (i = 0; i < rss_table_size; i++)
11077 		lut[i] = i % rss_size;
11078 }
11079 
11080 /**
11081  * i40e_pf_config_rss - Prepare for RSS if used
11082  * @pf: board private structure
11083  **/
11084 static int i40e_pf_config_rss(struct i40e_pf *pf)
11085 {
11086 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11087 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11088 	u8 *lut;
11089 	struct i40e_hw *hw = &pf->hw;
11090 	u32 reg_val;
11091 	u64 hena;
11092 	int ret;
11093 
11094 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11095 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11096 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11097 	hena |= i40e_pf_get_default_rss_hena(pf);
11098 
11099 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11100 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11101 
11102 	/* Determine the RSS table size based on the hardware capabilities */
11103 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11104 	reg_val = (pf->rss_table_size == 512) ?
11105 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11106 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11107 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11108 
11109 	/* Determine the RSS size of the VSI */
11110 	if (!vsi->rss_size) {
11111 		u16 qcount;
11112 		/* If the firmware does something weird during VSI init, we
11113 		 * could end up with zero TCs. Check for that to avoid
11114 		 * divide-by-zero. It probably won't pass traffic, but it also
11115 		 * won't panic.
11116 		 */
11117 		qcount = vsi->num_queue_pairs /
11118 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11119 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11120 	}
11121 	if (!vsi->rss_size)
11122 		return -EINVAL;
11123 
11124 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11125 	if (!lut)
11126 		return -ENOMEM;
11127 
11128 	/* Use user configured lut if there is one, otherwise use default */
11129 	if (vsi->rss_lut_user)
11130 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11131 	else
11132 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11133 
11134 	/* Use user configured hash key if there is one, otherwise
11135 	 * use default.
11136 	 */
11137 	if (vsi->rss_hkey_user)
11138 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11139 	else
11140 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11141 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11142 	kfree(lut);
11143 
11144 	return ret;
11145 }
11146 
11147 /**
11148  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11149  * @pf: board private structure
11150  * @queue_count: the requested queue count for rss.
11151  *
11152  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11153  * count which may be different from the requested queue count.
11154  * Note: expects to be called while under rtnl_lock()
11155  **/
11156 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11157 {
11158 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11159 	int new_rss_size;
11160 
11161 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11162 		return 0;
11163 
11164 	queue_count = min_t(int, queue_count, num_online_cpus());
11165 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11166 
11167 	if (queue_count != vsi->num_queue_pairs) {
11168 		u16 qcount;
11169 
11170 		vsi->req_queue_pairs = queue_count;
11171 		i40e_prep_for_reset(pf, true);
11172 
11173 		pf->alloc_rss_size = new_rss_size;
11174 
11175 		i40e_reset_and_rebuild(pf, true, true);
11176 
11177 		/* Discard the user configured hash keys and lut, if less
11178 		 * queues are enabled.
11179 		 */
11180 		if (queue_count < vsi->rss_size) {
11181 			i40e_clear_rss_config_user(vsi);
11182 			dev_dbg(&pf->pdev->dev,
11183 				"discard user configured hash keys and lut\n");
11184 		}
11185 
11186 		/* Reset vsi->rss_size, as number of enabled queues changed */
11187 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11188 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11189 
11190 		i40e_pf_config_rss(pf);
11191 	}
11192 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11193 		 vsi->req_queue_pairs, pf->rss_size_max);
11194 	return pf->alloc_rss_size;
11195 }
11196 
11197 /**
11198  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11199  * @pf: board private structure
11200  **/
11201 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11202 {
11203 	i40e_status status;
11204 	bool min_valid, max_valid;
11205 	u32 max_bw, min_bw;
11206 
11207 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11208 					   &min_valid, &max_valid);
11209 
11210 	if (!status) {
11211 		if (min_valid)
11212 			pf->min_bw = min_bw;
11213 		if (max_valid)
11214 			pf->max_bw = max_bw;
11215 	}
11216 
11217 	return status;
11218 }
11219 
11220 /**
11221  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11222  * @pf: board private structure
11223  **/
11224 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11225 {
11226 	struct i40e_aqc_configure_partition_bw_data bw_data;
11227 	i40e_status status;
11228 
11229 	/* Set the valid bit for this PF */
11230 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11231 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11232 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11233 
11234 	/* Set the new bandwidths */
11235 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11236 
11237 	return status;
11238 }
11239 
11240 /**
11241  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11242  * @pf: board private structure
11243  **/
11244 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11245 {
11246 	/* Commit temporary BW setting to permanent NVM image */
11247 	enum i40e_admin_queue_err last_aq_status;
11248 	i40e_status ret;
11249 	u16 nvm_word;
11250 
11251 	if (pf->hw.partition_id != 1) {
11252 		dev_info(&pf->pdev->dev,
11253 			 "Commit BW only works on partition 1! This is partition %d",
11254 			 pf->hw.partition_id);
11255 		ret = I40E_NOT_SUPPORTED;
11256 		goto bw_commit_out;
11257 	}
11258 
11259 	/* Acquire NVM for read access */
11260 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11261 	last_aq_status = pf->hw.aq.asq_last_status;
11262 	if (ret) {
11263 		dev_info(&pf->pdev->dev,
11264 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11265 			 i40e_stat_str(&pf->hw, ret),
11266 			 i40e_aq_str(&pf->hw, last_aq_status));
11267 		goto bw_commit_out;
11268 	}
11269 
11270 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11271 	ret = i40e_aq_read_nvm(&pf->hw,
11272 			       I40E_SR_NVM_CONTROL_WORD,
11273 			       0x10, sizeof(nvm_word), &nvm_word,
11274 			       false, NULL);
11275 	/* Save off last admin queue command status before releasing
11276 	 * the NVM
11277 	 */
11278 	last_aq_status = pf->hw.aq.asq_last_status;
11279 	i40e_release_nvm(&pf->hw);
11280 	if (ret) {
11281 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11282 			 i40e_stat_str(&pf->hw, ret),
11283 			 i40e_aq_str(&pf->hw, last_aq_status));
11284 		goto bw_commit_out;
11285 	}
11286 
11287 	/* Wait a bit for NVM release to complete */
11288 	msleep(50);
11289 
11290 	/* Acquire NVM for write access */
11291 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11292 	last_aq_status = pf->hw.aq.asq_last_status;
11293 	if (ret) {
11294 		dev_info(&pf->pdev->dev,
11295 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11296 			 i40e_stat_str(&pf->hw, ret),
11297 			 i40e_aq_str(&pf->hw, last_aq_status));
11298 		goto bw_commit_out;
11299 	}
11300 	/* Write it back out unchanged to initiate update NVM,
11301 	 * which will force a write of the shadow (alt) RAM to
11302 	 * the NVM - thus storing the bandwidth values permanently.
11303 	 */
11304 	ret = i40e_aq_update_nvm(&pf->hw,
11305 				 I40E_SR_NVM_CONTROL_WORD,
11306 				 0x10, sizeof(nvm_word),
11307 				 &nvm_word, true, 0, NULL);
11308 	/* Save off last admin queue command status before releasing
11309 	 * the NVM
11310 	 */
11311 	last_aq_status = pf->hw.aq.asq_last_status;
11312 	i40e_release_nvm(&pf->hw);
11313 	if (ret)
11314 		dev_info(&pf->pdev->dev,
11315 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11316 			 i40e_stat_str(&pf->hw, ret),
11317 			 i40e_aq_str(&pf->hw, last_aq_status));
11318 bw_commit_out:
11319 
11320 	return ret;
11321 }
11322 
11323 /**
11324  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11325  * @pf: board private structure to initialize
11326  *
11327  * i40e_sw_init initializes the Adapter private data structure.
11328  * Fields are initialized based on PCI device information and
11329  * OS network device settings (MTU size).
11330  **/
11331 static int i40e_sw_init(struct i40e_pf *pf)
11332 {
11333 	int err = 0;
11334 	int size;
11335 
11336 	/* Set default capability flags */
11337 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11338 		    I40E_FLAG_MSI_ENABLED     |
11339 		    I40E_FLAG_MSIX_ENABLED;
11340 
11341 	/* Set default ITR */
11342 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11343 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11344 
11345 	/* Depending on PF configurations, it is possible that the RSS
11346 	 * maximum might end up larger than the available queues
11347 	 */
11348 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11349 	pf->alloc_rss_size = 1;
11350 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11351 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11352 				 pf->hw.func_caps.num_tx_qp);
11353 	if (pf->hw.func_caps.rss) {
11354 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11355 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11356 					   num_online_cpus());
11357 	}
11358 
11359 	/* MFP mode enabled */
11360 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11361 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11362 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11363 		if (i40e_get_partition_bw_setting(pf)) {
11364 			dev_warn(&pf->pdev->dev,
11365 				 "Could not get partition bw settings\n");
11366 		} else {
11367 			dev_info(&pf->pdev->dev,
11368 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11369 				 pf->min_bw, pf->max_bw);
11370 
11371 			/* nudge the Tx scheduler */
11372 			i40e_set_partition_bw_setting(pf);
11373 		}
11374 	}
11375 
11376 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11377 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11378 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11379 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11380 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11381 		    pf->hw.num_partitions > 1)
11382 			dev_info(&pf->pdev->dev,
11383 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11384 		else
11385 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11386 		pf->fdir_pf_filter_count =
11387 				 pf->hw.func_caps.fd_filters_guaranteed;
11388 		pf->hw.fdir_shared_filter_count =
11389 				 pf->hw.func_caps.fd_filters_best_effort;
11390 	}
11391 
11392 	if (pf->hw.mac.type == I40E_MAC_X722) {
11393 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11394 				    I40E_HW_128_QP_RSS_CAPABLE |
11395 				    I40E_HW_ATR_EVICT_CAPABLE |
11396 				    I40E_HW_WB_ON_ITR_CAPABLE |
11397 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11398 				    I40E_HW_NO_PCI_LINK_CHECK |
11399 				    I40E_HW_USE_SET_LLDP_MIB |
11400 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11401 				    I40E_HW_PTP_L4_CAPABLE |
11402 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11403 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11404 
11405 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11406 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11407 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11408 			dev_warn(&pf->pdev->dev,
11409 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11410 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11411 		}
11412 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11413 		   ((pf->hw.aq.api_maj_ver == 1) &&
11414 		    (pf->hw.aq.api_min_ver > 4))) {
11415 		/* Supported in FW API version higher than 1.4 */
11416 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11417 	}
11418 
11419 	/* Enable HW ATR eviction if possible */
11420 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11421 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11422 
11423 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11424 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11425 	    (pf->hw.aq.fw_maj_ver < 4))) {
11426 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11427 		/* No DCB support  for FW < v4.33 */
11428 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11429 	}
11430 
11431 	/* Disable FW LLDP if FW < v4.3 */
11432 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11433 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11434 	    (pf->hw.aq.fw_maj_ver < 4)))
11435 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11436 
11437 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11438 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11439 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11440 	    (pf->hw.aq.fw_maj_ver >= 5)))
11441 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11442 
11443 	/* Enable PTP L4 if FW > v6.0 */
11444 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11445 	    pf->hw.aq.fw_maj_ver >= 6)
11446 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11447 
11448 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11449 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11450 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11451 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11452 	}
11453 
11454 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11455 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11456 		/* IWARP needs one extra vector for CQP just like MISC.*/
11457 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11458 	}
11459 	/* Stopping FW LLDP engine is supported on XL710 and X722
11460 	 * starting from FW versions determined in i40e_init_adminq.
11461 	 * Stopping the FW LLDP engine is not supported on XL710
11462 	 * if NPAR is functioning so unset this hw flag in this case.
11463 	 */
11464 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11465 	    pf->hw.func_caps.npar_enable &&
11466 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
11467 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
11468 
11469 #ifdef CONFIG_PCI_IOV
11470 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11471 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11472 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11473 		pf->num_req_vfs = min_t(int,
11474 					pf->hw.func_caps.num_vfs,
11475 					I40E_MAX_VF_COUNT);
11476 	}
11477 #endif /* CONFIG_PCI_IOV */
11478 	pf->eeprom_version = 0xDEAD;
11479 	pf->lan_veb = I40E_NO_VEB;
11480 	pf->lan_vsi = I40E_NO_VSI;
11481 
11482 	/* By default FW has this off for performance reasons */
11483 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11484 
11485 	/* set up queue assignment tracking */
11486 	size = sizeof(struct i40e_lump_tracking)
11487 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11488 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
11489 	if (!pf->qp_pile) {
11490 		err = -ENOMEM;
11491 		goto sw_init_done;
11492 	}
11493 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11494 	pf->qp_pile->search_hint = 0;
11495 
11496 	pf->tx_timeout_recovery_level = 1;
11497 
11498 	mutex_init(&pf->switch_mutex);
11499 
11500 sw_init_done:
11501 	return err;
11502 }
11503 
11504 /**
11505  * i40e_set_ntuple - set the ntuple feature flag and take action
11506  * @pf: board private structure to initialize
11507  * @features: the feature set that the stack is suggesting
11508  *
11509  * returns a bool to indicate if reset needs to happen
11510  **/
11511 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11512 {
11513 	bool need_reset = false;
11514 
11515 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
11516 	 * the state changed, we need to reset.
11517 	 */
11518 	if (features & NETIF_F_NTUPLE) {
11519 		/* Enable filters and mark for reset */
11520 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11521 			need_reset = true;
11522 		/* enable FD_SB only if there is MSI-X vector and no cloud
11523 		 * filters exist
11524 		 */
11525 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11526 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11527 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11528 		}
11529 	} else {
11530 		/* turn off filters, mark for reset and clear SW filter list */
11531 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11532 			need_reset = true;
11533 			i40e_fdir_filter_exit(pf);
11534 		}
11535 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11536 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
11537 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11538 
11539 		/* reset fd counters */
11540 		pf->fd_add_err = 0;
11541 		pf->fd_atr_cnt = 0;
11542 		/* if ATR was auto disabled it can be re-enabled. */
11543 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
11544 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11545 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
11546 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11547 	}
11548 	return need_reset;
11549 }
11550 
11551 /**
11552  * i40e_clear_rss_lut - clear the rx hash lookup table
11553  * @vsi: the VSI being configured
11554  **/
11555 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11556 {
11557 	struct i40e_pf *pf = vsi->back;
11558 	struct i40e_hw *hw = &pf->hw;
11559 	u16 vf_id = vsi->vf_id;
11560 	u8 i;
11561 
11562 	if (vsi->type == I40E_VSI_MAIN) {
11563 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11564 			wr32(hw, I40E_PFQF_HLUT(i), 0);
11565 	} else if (vsi->type == I40E_VSI_SRIOV) {
11566 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11567 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11568 	} else {
11569 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11570 	}
11571 }
11572 
11573 /**
11574  * i40e_set_features - set the netdev feature flags
11575  * @netdev: ptr to the netdev being adjusted
11576  * @features: the feature set that the stack is suggesting
11577  * Note: expects to be called while under rtnl_lock()
11578  **/
11579 static int i40e_set_features(struct net_device *netdev,
11580 			     netdev_features_t features)
11581 {
11582 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11583 	struct i40e_vsi *vsi = np->vsi;
11584 	struct i40e_pf *pf = vsi->back;
11585 	bool need_reset;
11586 
11587 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11588 		i40e_pf_config_rss(pf);
11589 	else if (!(features & NETIF_F_RXHASH) &&
11590 		 netdev->features & NETIF_F_RXHASH)
11591 		i40e_clear_rss_lut(vsi);
11592 
11593 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
11594 		i40e_vlan_stripping_enable(vsi);
11595 	else
11596 		i40e_vlan_stripping_disable(vsi);
11597 
11598 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11599 		dev_err(&pf->pdev->dev,
11600 			"Offloaded tc filters active, can't turn hw_tc_offload off");
11601 		return -EINVAL;
11602 	}
11603 
11604 	need_reset = i40e_set_ntuple(pf, features);
11605 
11606 	if (need_reset)
11607 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11608 
11609 	return 0;
11610 }
11611 
11612 /**
11613  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11614  * @pf: board private structure
11615  * @port: The UDP port to look up
11616  *
11617  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11618  **/
11619 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11620 {
11621 	u8 i;
11622 
11623 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11624 		/* Do not report ports with pending deletions as
11625 		 * being available.
11626 		 */
11627 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
11628 			continue;
11629 		if (pf->udp_ports[i].port == port)
11630 			return i;
11631 	}
11632 
11633 	return i;
11634 }
11635 
11636 /**
11637  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11638  * @netdev: This physical port's netdev
11639  * @ti: Tunnel endpoint information
11640  **/
11641 static void i40e_udp_tunnel_add(struct net_device *netdev,
11642 				struct udp_tunnel_info *ti)
11643 {
11644 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11645 	struct i40e_vsi *vsi = np->vsi;
11646 	struct i40e_pf *pf = vsi->back;
11647 	u16 port = ntohs(ti->port);
11648 	u8 next_idx;
11649 	u8 idx;
11650 
11651 	idx = i40e_get_udp_port_idx(pf, port);
11652 
11653 	/* Check if port already exists */
11654 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11655 		netdev_info(netdev, "port %d already offloaded\n", port);
11656 		return;
11657 	}
11658 
11659 	/* Now check if there is space to add the new port */
11660 	next_idx = i40e_get_udp_port_idx(pf, 0);
11661 
11662 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11663 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11664 			    port);
11665 		return;
11666 	}
11667 
11668 	switch (ti->type) {
11669 	case UDP_TUNNEL_TYPE_VXLAN:
11670 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11671 		break;
11672 	case UDP_TUNNEL_TYPE_GENEVE:
11673 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11674 			return;
11675 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11676 		break;
11677 	default:
11678 		return;
11679 	}
11680 
11681 	/* New port: add it and mark its index in the bitmap */
11682 	pf->udp_ports[next_idx].port = port;
11683 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
11684 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11685 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11686 }
11687 
11688 /**
11689  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11690  * @netdev: This physical port's netdev
11691  * @ti: Tunnel endpoint information
11692  **/
11693 static void i40e_udp_tunnel_del(struct net_device *netdev,
11694 				struct udp_tunnel_info *ti)
11695 {
11696 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11697 	struct i40e_vsi *vsi = np->vsi;
11698 	struct i40e_pf *pf = vsi->back;
11699 	u16 port = ntohs(ti->port);
11700 	u8 idx;
11701 
11702 	idx = i40e_get_udp_port_idx(pf, port);
11703 
11704 	/* Check if port already exists */
11705 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11706 		goto not_found;
11707 
11708 	switch (ti->type) {
11709 	case UDP_TUNNEL_TYPE_VXLAN:
11710 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11711 			goto not_found;
11712 		break;
11713 	case UDP_TUNNEL_TYPE_GENEVE:
11714 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11715 			goto not_found;
11716 		break;
11717 	default:
11718 		goto not_found;
11719 	}
11720 
11721 	/* if port exists, set it to 0 (mark for deletion)
11722 	 * and make it pending
11723 	 */
11724 	pf->udp_ports[idx].port = 0;
11725 
11726 	/* Toggle pending bit instead of setting it. This way if we are
11727 	 * deleting a port that has yet to be added we just clear the pending
11728 	 * bit and don't have to worry about it.
11729 	 */
11730 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
11731 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11732 
11733 	return;
11734 not_found:
11735 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11736 		    port);
11737 }
11738 
11739 static int i40e_get_phys_port_id(struct net_device *netdev,
11740 				 struct netdev_phys_item_id *ppid)
11741 {
11742 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11743 	struct i40e_pf *pf = np->vsi->back;
11744 	struct i40e_hw *hw = &pf->hw;
11745 
11746 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11747 		return -EOPNOTSUPP;
11748 
11749 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11750 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11751 
11752 	return 0;
11753 }
11754 
11755 /**
11756  * i40e_ndo_fdb_add - add an entry to the hardware database
11757  * @ndm: the input from the stack
11758  * @tb: pointer to array of nladdr (unused)
11759  * @dev: the net device pointer
11760  * @addr: the MAC address entry being added
11761  * @vid: VLAN ID
11762  * @flags: instructions from stack about fdb operation
11763  */
11764 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11765 			    struct net_device *dev,
11766 			    const unsigned char *addr, u16 vid,
11767 			    u16 flags,
11768 			    struct netlink_ext_ack *extack)
11769 {
11770 	struct i40e_netdev_priv *np = netdev_priv(dev);
11771 	struct i40e_pf *pf = np->vsi->back;
11772 	int err = 0;
11773 
11774 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11775 		return -EOPNOTSUPP;
11776 
11777 	if (vid) {
11778 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11779 		return -EINVAL;
11780 	}
11781 
11782 	/* Hardware does not support aging addresses so if a
11783 	 * ndm_state is given only allow permanent addresses
11784 	 */
11785 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11786 		netdev_info(dev, "FDB only supports static addresses\n");
11787 		return -EINVAL;
11788 	}
11789 
11790 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11791 		err = dev_uc_add_excl(dev, addr);
11792 	else if (is_multicast_ether_addr(addr))
11793 		err = dev_mc_add_excl(dev, addr);
11794 	else
11795 		err = -EINVAL;
11796 
11797 	/* Only return duplicate errors if NLM_F_EXCL is set */
11798 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
11799 		err = 0;
11800 
11801 	return err;
11802 }
11803 
11804 /**
11805  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11806  * @dev: the netdev being configured
11807  * @nlh: RTNL message
11808  * @flags: bridge flags
11809  * @extack: netlink extended ack
11810  *
11811  * Inserts a new hardware bridge if not already created and
11812  * enables the bridging mode requested (VEB or VEPA). If the
11813  * hardware bridge has already been inserted and the request
11814  * is to change the mode then that requires a PF reset to
11815  * allow rebuild of the components with required hardware
11816  * bridge mode enabled.
11817  *
11818  * Note: expects to be called while under rtnl_lock()
11819  **/
11820 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11821 				   struct nlmsghdr *nlh,
11822 				   u16 flags,
11823 				   struct netlink_ext_ack *extack)
11824 {
11825 	struct i40e_netdev_priv *np = netdev_priv(dev);
11826 	struct i40e_vsi *vsi = np->vsi;
11827 	struct i40e_pf *pf = vsi->back;
11828 	struct i40e_veb *veb = NULL;
11829 	struct nlattr *attr, *br_spec;
11830 	int i, rem;
11831 
11832 	/* Only for PF VSI for now */
11833 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11834 		return -EOPNOTSUPP;
11835 
11836 	/* Find the HW bridge for PF VSI */
11837 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11838 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11839 			veb = pf->veb[i];
11840 	}
11841 
11842 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11843 
11844 	nla_for_each_nested(attr, br_spec, rem) {
11845 		__u16 mode;
11846 
11847 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
11848 			continue;
11849 
11850 		mode = nla_get_u16(attr);
11851 		if ((mode != BRIDGE_MODE_VEPA) &&
11852 		    (mode != BRIDGE_MODE_VEB))
11853 			return -EINVAL;
11854 
11855 		/* Insert a new HW bridge */
11856 		if (!veb) {
11857 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11858 					     vsi->tc_config.enabled_tc);
11859 			if (veb) {
11860 				veb->bridge_mode = mode;
11861 				i40e_config_bridge_mode(veb);
11862 			} else {
11863 				/* No Bridge HW offload available */
11864 				return -ENOENT;
11865 			}
11866 			break;
11867 		} else if (mode != veb->bridge_mode) {
11868 			/* Existing HW bridge but different mode needs reset */
11869 			veb->bridge_mode = mode;
11870 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11871 			if (mode == BRIDGE_MODE_VEB)
11872 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11873 			else
11874 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11875 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11876 			break;
11877 		}
11878 	}
11879 
11880 	return 0;
11881 }
11882 
11883 /**
11884  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11885  * @skb: skb buff
11886  * @pid: process id
11887  * @seq: RTNL message seq #
11888  * @dev: the netdev being configured
11889  * @filter_mask: unused
11890  * @nlflags: netlink flags passed in
11891  *
11892  * Return the mode in which the hardware bridge is operating in
11893  * i.e VEB or VEPA.
11894  **/
11895 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11896 				   struct net_device *dev,
11897 				   u32 __always_unused filter_mask,
11898 				   int nlflags)
11899 {
11900 	struct i40e_netdev_priv *np = netdev_priv(dev);
11901 	struct i40e_vsi *vsi = np->vsi;
11902 	struct i40e_pf *pf = vsi->back;
11903 	struct i40e_veb *veb = NULL;
11904 	int i;
11905 
11906 	/* Only for PF VSI for now */
11907 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11908 		return -EOPNOTSUPP;
11909 
11910 	/* Find the HW bridge for the PF VSI */
11911 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11912 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11913 			veb = pf->veb[i];
11914 	}
11915 
11916 	if (!veb)
11917 		return 0;
11918 
11919 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11920 				       0, 0, nlflags, filter_mask, NULL);
11921 }
11922 
11923 /**
11924  * i40e_features_check - Validate encapsulated packet conforms to limits
11925  * @skb: skb buff
11926  * @dev: This physical port's netdev
11927  * @features: Offload features that the stack believes apply
11928  **/
11929 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11930 					     struct net_device *dev,
11931 					     netdev_features_t features)
11932 {
11933 	size_t len;
11934 
11935 	/* No point in doing any of this if neither checksum nor GSO are
11936 	 * being requested for this frame.  We can rule out both by just
11937 	 * checking for CHECKSUM_PARTIAL
11938 	 */
11939 	if (skb->ip_summed != CHECKSUM_PARTIAL)
11940 		return features;
11941 
11942 	/* We cannot support GSO if the MSS is going to be less than
11943 	 * 64 bytes.  If it is then we need to drop support for GSO.
11944 	 */
11945 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11946 		features &= ~NETIF_F_GSO_MASK;
11947 
11948 	/* MACLEN can support at most 63 words */
11949 	len = skb_network_header(skb) - skb->data;
11950 	if (len & ~(63 * 2))
11951 		goto out_err;
11952 
11953 	/* IPLEN and EIPLEN can support at most 127 dwords */
11954 	len = skb_transport_header(skb) - skb_network_header(skb);
11955 	if (len & ~(127 * 4))
11956 		goto out_err;
11957 
11958 	if (skb->encapsulation) {
11959 		/* L4TUNLEN can support 127 words */
11960 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
11961 		if (len & ~(127 * 2))
11962 			goto out_err;
11963 
11964 		/* IPLEN can support at most 127 dwords */
11965 		len = skb_inner_transport_header(skb) -
11966 		      skb_inner_network_header(skb);
11967 		if (len & ~(127 * 4))
11968 			goto out_err;
11969 	}
11970 
11971 	/* No need to validate L4LEN as TCP is the only protocol with a
11972 	 * a flexible value and we support all possible values supported
11973 	 * by TCP, which is at most 15 dwords
11974 	 */
11975 
11976 	return features;
11977 out_err:
11978 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11979 }
11980 
11981 /**
11982  * i40e_xdp_setup - add/remove an XDP program
11983  * @vsi: VSI to changed
11984  * @prog: XDP program
11985  **/
11986 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11987 			  struct bpf_prog *prog)
11988 {
11989 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11990 	struct i40e_pf *pf = vsi->back;
11991 	struct bpf_prog *old_prog;
11992 	bool need_reset;
11993 	int i;
11994 
11995 	/* Don't allow frames that span over multiple buffers */
11996 	if (frame_size > vsi->rx_buf_len)
11997 		return -EINVAL;
11998 
11999 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12000 		return 0;
12001 
12002 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12003 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12004 
12005 	if (need_reset)
12006 		i40e_prep_for_reset(pf, true);
12007 
12008 	old_prog = xchg(&vsi->xdp_prog, prog);
12009 
12010 	if (need_reset)
12011 		i40e_reset_and_rebuild(pf, true, true);
12012 
12013 	for (i = 0; i < vsi->num_queue_pairs; i++)
12014 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12015 
12016 	if (old_prog)
12017 		bpf_prog_put(old_prog);
12018 
12019 	/* Kick start the NAPI context if there is an AF_XDP socket open
12020 	 * on that queue id. This so that receiving will start.
12021 	 */
12022 	if (need_reset && prog)
12023 		for (i = 0; i < vsi->num_queue_pairs; i++)
12024 			if (vsi->xdp_rings[i]->xsk_umem)
12025 				(void)i40e_xsk_async_xmit(vsi->netdev, i);
12026 
12027 	return 0;
12028 }
12029 
12030 /**
12031  * i40e_enter_busy_conf - Enters busy config state
12032  * @vsi: vsi
12033  *
12034  * Returns 0 on success, <0 for failure.
12035  **/
12036 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12037 {
12038 	struct i40e_pf *pf = vsi->back;
12039 	int timeout = 50;
12040 
12041 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12042 		timeout--;
12043 		if (!timeout)
12044 			return -EBUSY;
12045 		usleep_range(1000, 2000);
12046 	}
12047 
12048 	return 0;
12049 }
12050 
12051 /**
12052  * i40e_exit_busy_conf - Exits busy config state
12053  * @vsi: vsi
12054  **/
12055 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12056 {
12057 	struct i40e_pf *pf = vsi->back;
12058 
12059 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12060 }
12061 
12062 /**
12063  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12064  * @vsi: vsi
12065  * @queue_pair: queue pair
12066  **/
12067 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12068 {
12069 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12070 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12071 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12072 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12073 	if (i40e_enabled_xdp_vsi(vsi)) {
12074 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12075 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12076 	}
12077 }
12078 
12079 /**
12080  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12081  * @vsi: vsi
12082  * @queue_pair: queue pair
12083  **/
12084 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12085 {
12086 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12087 	if (i40e_enabled_xdp_vsi(vsi)) {
12088 		/* Make sure that in-progress ndo_xdp_xmit calls are
12089 		 * completed.
12090 		 */
12091 		synchronize_rcu();
12092 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12093 	}
12094 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12095 }
12096 
12097 /**
12098  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12099  * @vsi: vsi
12100  * @queue_pair: queue pair
12101  * @enable: true for enable, false for disable
12102  **/
12103 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12104 					bool enable)
12105 {
12106 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12107 	struct i40e_q_vector *q_vector = rxr->q_vector;
12108 
12109 	if (!vsi->netdev)
12110 		return;
12111 
12112 	/* All rings in a qp belong to the same qvector. */
12113 	if (q_vector->rx.ring || q_vector->tx.ring) {
12114 		if (enable)
12115 			napi_enable(&q_vector->napi);
12116 		else
12117 			napi_disable(&q_vector->napi);
12118 	}
12119 }
12120 
12121 /**
12122  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12123  * @vsi: vsi
12124  * @queue_pair: queue pair
12125  * @enable: true for enable, false for disable
12126  *
12127  * Returns 0 on success, <0 on failure.
12128  **/
12129 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12130 					bool enable)
12131 {
12132 	struct i40e_pf *pf = vsi->back;
12133 	int pf_q, ret = 0;
12134 
12135 	pf_q = vsi->base_queue + queue_pair;
12136 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12137 				     false /*is xdp*/, enable);
12138 	if (ret) {
12139 		dev_info(&pf->pdev->dev,
12140 			 "VSI seid %d Tx ring %d %sable timeout\n",
12141 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12142 		return ret;
12143 	}
12144 
12145 	i40e_control_rx_q(pf, pf_q, enable);
12146 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12147 	if (ret) {
12148 		dev_info(&pf->pdev->dev,
12149 			 "VSI seid %d Rx ring %d %sable timeout\n",
12150 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12151 		return ret;
12152 	}
12153 
12154 	/* Due to HW errata, on Rx disable only, the register can
12155 	 * indicate done before it really is. Needs 50ms to be sure
12156 	 */
12157 	if (!enable)
12158 		mdelay(50);
12159 
12160 	if (!i40e_enabled_xdp_vsi(vsi))
12161 		return ret;
12162 
12163 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12164 				     pf_q + vsi->alloc_queue_pairs,
12165 				     true /*is xdp*/, enable);
12166 	if (ret) {
12167 		dev_info(&pf->pdev->dev,
12168 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12169 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12170 	}
12171 
12172 	return ret;
12173 }
12174 
12175 /**
12176  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12177  * @vsi: vsi
12178  * @queue_pair: queue_pair
12179  **/
12180 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12181 {
12182 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12183 	struct i40e_pf *pf = vsi->back;
12184 	struct i40e_hw *hw = &pf->hw;
12185 
12186 	/* All rings in a qp belong to the same qvector. */
12187 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12188 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12189 	else
12190 		i40e_irq_dynamic_enable_icr0(pf);
12191 
12192 	i40e_flush(hw);
12193 }
12194 
12195 /**
12196  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12197  * @vsi: vsi
12198  * @queue_pair: queue_pair
12199  **/
12200 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12201 {
12202 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12203 	struct i40e_pf *pf = vsi->back;
12204 	struct i40e_hw *hw = &pf->hw;
12205 
12206 	/* For simplicity, instead of removing the qp interrupt causes
12207 	 * from the interrupt linked list, we simply disable the interrupt, and
12208 	 * leave the list intact.
12209 	 *
12210 	 * All rings in a qp belong to the same qvector.
12211 	 */
12212 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12213 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12214 
12215 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12216 		i40e_flush(hw);
12217 		synchronize_irq(pf->msix_entries[intpf].vector);
12218 	} else {
12219 		/* Legacy and MSI mode - this stops all interrupt handling */
12220 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12221 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12222 		i40e_flush(hw);
12223 		synchronize_irq(pf->pdev->irq);
12224 	}
12225 }
12226 
12227 /**
12228  * i40e_queue_pair_disable - Disables a queue pair
12229  * @vsi: vsi
12230  * @queue_pair: queue pair
12231  *
12232  * Returns 0 on success, <0 on failure.
12233  **/
12234 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12235 {
12236 	int err;
12237 
12238 	err = i40e_enter_busy_conf(vsi);
12239 	if (err)
12240 		return err;
12241 
12242 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12243 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12244 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12245 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12246 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12247 
12248 	return err;
12249 }
12250 
12251 /**
12252  * i40e_queue_pair_enable - Enables a queue pair
12253  * @vsi: vsi
12254  * @queue_pair: queue pair
12255  *
12256  * Returns 0 on success, <0 on failure.
12257  **/
12258 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12259 {
12260 	int err;
12261 
12262 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12263 	if (err)
12264 		return err;
12265 
12266 	if (i40e_enabled_xdp_vsi(vsi)) {
12267 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12268 		if (err)
12269 			return err;
12270 	}
12271 
12272 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12273 	if (err)
12274 		return err;
12275 
12276 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12277 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12278 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12279 
12280 	i40e_exit_busy_conf(vsi);
12281 
12282 	return err;
12283 }
12284 
12285 /**
12286  * i40e_xdp - implements ndo_bpf for i40e
12287  * @dev: netdevice
12288  * @xdp: XDP command
12289  **/
12290 static int i40e_xdp(struct net_device *dev,
12291 		    struct netdev_bpf *xdp)
12292 {
12293 	struct i40e_netdev_priv *np = netdev_priv(dev);
12294 	struct i40e_vsi *vsi = np->vsi;
12295 
12296 	if (vsi->type != I40E_VSI_MAIN)
12297 		return -EINVAL;
12298 
12299 	switch (xdp->command) {
12300 	case XDP_SETUP_PROG:
12301 		return i40e_xdp_setup(vsi, xdp->prog);
12302 	case XDP_QUERY_PROG:
12303 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
12304 		return 0;
12305 	case XDP_SETUP_XSK_UMEM:
12306 		return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
12307 					   xdp->xsk.queue_id);
12308 	default:
12309 		return -EINVAL;
12310 	}
12311 }
12312 
12313 static const struct net_device_ops i40e_netdev_ops = {
12314 	.ndo_open		= i40e_open,
12315 	.ndo_stop		= i40e_close,
12316 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12317 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12318 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12319 	.ndo_validate_addr	= eth_validate_addr,
12320 	.ndo_set_mac_address	= i40e_set_mac,
12321 	.ndo_change_mtu		= i40e_change_mtu,
12322 	.ndo_do_ioctl		= i40e_ioctl,
12323 	.ndo_tx_timeout		= i40e_tx_timeout,
12324 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12325 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12326 #ifdef CONFIG_NET_POLL_CONTROLLER
12327 	.ndo_poll_controller	= i40e_netpoll,
12328 #endif
12329 	.ndo_setup_tc		= __i40e_setup_tc,
12330 	.ndo_set_features	= i40e_set_features,
12331 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12332 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12333 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12334 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12335 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12336 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12337 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12338 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
12339 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
12340 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12341 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12342 	.ndo_features_check	= i40e_features_check,
12343 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12344 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12345 	.ndo_bpf		= i40e_xdp,
12346 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12347 	.ndo_xsk_async_xmit	= i40e_xsk_async_xmit,
12348 };
12349 
12350 /**
12351  * i40e_config_netdev - Setup the netdev flags
12352  * @vsi: the VSI being configured
12353  *
12354  * Returns 0 on success, negative value on failure
12355  **/
12356 static int i40e_config_netdev(struct i40e_vsi *vsi)
12357 {
12358 	struct i40e_pf *pf = vsi->back;
12359 	struct i40e_hw *hw = &pf->hw;
12360 	struct i40e_netdev_priv *np;
12361 	struct net_device *netdev;
12362 	u8 broadcast[ETH_ALEN];
12363 	u8 mac_addr[ETH_ALEN];
12364 	int etherdev_size;
12365 	netdev_features_t hw_enc_features;
12366 	netdev_features_t hw_features;
12367 
12368 	etherdev_size = sizeof(struct i40e_netdev_priv);
12369 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12370 	if (!netdev)
12371 		return -ENOMEM;
12372 
12373 	vsi->netdev = netdev;
12374 	np = netdev_priv(netdev);
12375 	np->vsi = vsi;
12376 
12377 	hw_enc_features = NETIF_F_SG			|
12378 			  NETIF_F_IP_CSUM		|
12379 			  NETIF_F_IPV6_CSUM		|
12380 			  NETIF_F_HIGHDMA		|
12381 			  NETIF_F_SOFT_FEATURES		|
12382 			  NETIF_F_TSO			|
12383 			  NETIF_F_TSO_ECN		|
12384 			  NETIF_F_TSO6			|
12385 			  NETIF_F_GSO_GRE		|
12386 			  NETIF_F_GSO_GRE_CSUM		|
12387 			  NETIF_F_GSO_PARTIAL		|
12388 			  NETIF_F_GSO_IPXIP4		|
12389 			  NETIF_F_GSO_IPXIP6		|
12390 			  NETIF_F_GSO_UDP_TUNNEL	|
12391 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12392 			  NETIF_F_SCTP_CRC		|
12393 			  NETIF_F_RXHASH		|
12394 			  NETIF_F_RXCSUM		|
12395 			  0;
12396 
12397 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12398 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12399 
12400 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12401 
12402 	netdev->hw_enc_features |= hw_enc_features;
12403 
12404 	/* record features VLANs can make use of */
12405 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12406 
12407 	hw_features = hw_enc_features		|
12408 		      NETIF_F_HW_VLAN_CTAG_TX	|
12409 		      NETIF_F_HW_VLAN_CTAG_RX;
12410 
12411 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12412 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12413 
12414 	netdev->hw_features |= hw_features;
12415 
12416 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12417 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12418 
12419 	if (vsi->type == I40E_VSI_MAIN) {
12420 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12421 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12422 		/* The following steps are necessary for two reasons. First,
12423 		 * some older NVM configurations load a default MAC-VLAN
12424 		 * filter that will accept any tagged packet, and we want to
12425 		 * replace this with a normal filter. Additionally, it is
12426 		 * possible our MAC address was provided by the platform using
12427 		 * Open Firmware or similar.
12428 		 *
12429 		 * Thus, we need to remove the default filter and install one
12430 		 * specific to the MAC address.
12431 		 */
12432 		i40e_rm_default_mac_filter(vsi, mac_addr);
12433 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12434 		i40e_add_mac_filter(vsi, mac_addr);
12435 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12436 	} else {
12437 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12438 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12439 		 * the end, which is 4 bytes long, so force truncation of the
12440 		 * original name by IFNAMSIZ - 4
12441 		 */
12442 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12443 			 IFNAMSIZ - 4,
12444 			 pf->vsi[pf->lan_vsi]->netdev->name);
12445 		eth_random_addr(mac_addr);
12446 
12447 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12448 		i40e_add_mac_filter(vsi, mac_addr);
12449 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12450 	}
12451 
12452 	/* Add the broadcast filter so that we initially will receive
12453 	 * broadcast packets. Note that when a new VLAN is first added the
12454 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12455 	 * specific filters as part of transitioning into "vlan" operation.
12456 	 * When more VLANs are added, the driver will copy each existing MAC
12457 	 * filter and add it for the new VLAN.
12458 	 *
12459 	 * Broadcast filters are handled specially by
12460 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
12461 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
12462 	 * filter. The subtask will update the correct broadcast promiscuous
12463 	 * bits as VLANs become active or inactive.
12464 	 */
12465 	eth_broadcast_addr(broadcast);
12466 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12467 	i40e_add_mac_filter(vsi, broadcast);
12468 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12469 
12470 	ether_addr_copy(netdev->dev_addr, mac_addr);
12471 	ether_addr_copy(netdev->perm_addr, mac_addr);
12472 
12473 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
12474 	netdev->neigh_priv_len = sizeof(u32) * 4;
12475 
12476 	netdev->priv_flags |= IFF_UNICAST_FLT;
12477 	netdev->priv_flags |= IFF_SUPP_NOFCS;
12478 	/* Setup netdev TC information */
12479 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12480 
12481 	netdev->netdev_ops = &i40e_netdev_ops;
12482 	netdev->watchdog_timeo = 5 * HZ;
12483 	i40e_set_ethtool_ops(netdev);
12484 
12485 	/* MTU range: 68 - 9706 */
12486 	netdev->min_mtu = ETH_MIN_MTU;
12487 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12488 
12489 	return 0;
12490 }
12491 
12492 /**
12493  * i40e_vsi_delete - Delete a VSI from the switch
12494  * @vsi: the VSI being removed
12495  *
12496  * Returns 0 on success, negative value on failure
12497  **/
12498 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12499 {
12500 	/* remove default VSI is not allowed */
12501 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12502 		return;
12503 
12504 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
12505 }
12506 
12507 /**
12508  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
12509  * @vsi: the VSI being queried
12510  *
12511  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
12512  **/
12513 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
12514 {
12515 	struct i40e_veb *veb;
12516 	struct i40e_pf *pf = vsi->back;
12517 
12518 	/* Uplink is not a bridge so default to VEB */
12519 	if (vsi->veb_idx >= I40E_MAX_VEB)
12520 		return 1;
12521 
12522 	veb = pf->veb[vsi->veb_idx];
12523 	if (!veb) {
12524 		dev_info(&pf->pdev->dev,
12525 			 "There is no veb associated with the bridge\n");
12526 		return -ENOENT;
12527 	}
12528 
12529 	/* Uplink is a bridge in VEPA mode */
12530 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
12531 		return 0;
12532 	} else {
12533 		/* Uplink is a bridge in VEB mode */
12534 		return 1;
12535 	}
12536 
12537 	/* VEPA is now default bridge, so return 0 */
12538 	return 0;
12539 }
12540 
12541 /**
12542  * i40e_add_vsi - Add a VSI to the switch
12543  * @vsi: the VSI being configured
12544  *
12545  * This initializes a VSI context depending on the VSI type to be added and
12546  * passes it down to the add_vsi aq command.
12547  **/
12548 static int i40e_add_vsi(struct i40e_vsi *vsi)
12549 {
12550 	int ret = -ENODEV;
12551 	struct i40e_pf *pf = vsi->back;
12552 	struct i40e_hw *hw = &pf->hw;
12553 	struct i40e_vsi_context ctxt;
12554 	struct i40e_mac_filter *f;
12555 	struct hlist_node *h;
12556 	int bkt;
12557 
12558 	u8 enabled_tc = 0x1; /* TC0 enabled */
12559 	int f_count = 0;
12560 
12561 	memset(&ctxt, 0, sizeof(ctxt));
12562 	switch (vsi->type) {
12563 	case I40E_VSI_MAIN:
12564 		/* The PF's main VSI is already setup as part of the
12565 		 * device initialization, so we'll not bother with
12566 		 * the add_vsi call, but we will retrieve the current
12567 		 * VSI context.
12568 		 */
12569 		ctxt.seid = pf->main_vsi_seid;
12570 		ctxt.pf_num = pf->hw.pf_id;
12571 		ctxt.vf_num = 0;
12572 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
12573 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12574 		if (ret) {
12575 			dev_info(&pf->pdev->dev,
12576 				 "couldn't get PF vsi config, err %s aq_err %s\n",
12577 				 i40e_stat_str(&pf->hw, ret),
12578 				 i40e_aq_str(&pf->hw,
12579 					     pf->hw.aq.asq_last_status));
12580 			return -ENOENT;
12581 		}
12582 		vsi->info = ctxt.info;
12583 		vsi->info.valid_sections = 0;
12584 
12585 		vsi->seid = ctxt.seid;
12586 		vsi->id = ctxt.vsi_number;
12587 
12588 		enabled_tc = i40e_pf_get_tc_map(pf);
12589 
12590 		/* Source pruning is enabled by default, so the flag is
12591 		 * negative logic - if it's set, we need to fiddle with
12592 		 * the VSI to disable source pruning.
12593 		 */
12594 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
12595 			memset(&ctxt, 0, sizeof(ctxt));
12596 			ctxt.seid = pf->main_vsi_seid;
12597 			ctxt.pf_num = pf->hw.pf_id;
12598 			ctxt.vf_num = 0;
12599 			ctxt.info.valid_sections |=
12600 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12601 			ctxt.info.switch_id =
12602 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
12603 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12604 			if (ret) {
12605 				dev_info(&pf->pdev->dev,
12606 					 "update vsi failed, err %s aq_err %s\n",
12607 					 i40e_stat_str(&pf->hw, ret),
12608 					 i40e_aq_str(&pf->hw,
12609 						     pf->hw.aq.asq_last_status));
12610 				ret = -ENOENT;
12611 				goto err;
12612 			}
12613 		}
12614 
12615 		/* MFP mode setup queue map and update VSI */
12616 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
12617 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
12618 			memset(&ctxt, 0, sizeof(ctxt));
12619 			ctxt.seid = pf->main_vsi_seid;
12620 			ctxt.pf_num = pf->hw.pf_id;
12621 			ctxt.vf_num = 0;
12622 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
12623 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12624 			if (ret) {
12625 				dev_info(&pf->pdev->dev,
12626 					 "update vsi failed, err %s aq_err %s\n",
12627 					 i40e_stat_str(&pf->hw, ret),
12628 					 i40e_aq_str(&pf->hw,
12629 						    pf->hw.aq.asq_last_status));
12630 				ret = -ENOENT;
12631 				goto err;
12632 			}
12633 			/* update the local VSI info queue map */
12634 			i40e_vsi_update_queue_map(vsi, &ctxt);
12635 			vsi->info.valid_sections = 0;
12636 		} else {
12637 			/* Default/Main VSI is only enabled for TC0
12638 			 * reconfigure it to enable all TCs that are
12639 			 * available on the port in SFP mode.
12640 			 * For MFP case the iSCSI PF would use this
12641 			 * flow to enable LAN+iSCSI TC.
12642 			 */
12643 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
12644 			if (ret) {
12645 				/* Single TC condition is not fatal,
12646 				 * message and continue
12647 				 */
12648 				dev_info(&pf->pdev->dev,
12649 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12650 					 enabled_tc,
12651 					 i40e_stat_str(&pf->hw, ret),
12652 					 i40e_aq_str(&pf->hw,
12653 						    pf->hw.aq.asq_last_status));
12654 			}
12655 		}
12656 		break;
12657 
12658 	case I40E_VSI_FDIR:
12659 		ctxt.pf_num = hw->pf_id;
12660 		ctxt.vf_num = 0;
12661 		ctxt.uplink_seid = vsi->uplink_seid;
12662 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12663 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12664 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12665 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
12666 			ctxt.info.valid_sections |=
12667 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12668 			ctxt.info.switch_id =
12669 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12670 		}
12671 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12672 		break;
12673 
12674 	case I40E_VSI_VMDQ2:
12675 		ctxt.pf_num = hw->pf_id;
12676 		ctxt.vf_num = 0;
12677 		ctxt.uplink_seid = vsi->uplink_seid;
12678 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12679 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12680 
12681 		/* This VSI is connected to VEB so the switch_id
12682 		 * should be set to zero by default.
12683 		 */
12684 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12685 			ctxt.info.valid_sections |=
12686 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12687 			ctxt.info.switch_id =
12688 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12689 		}
12690 
12691 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12692 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12693 		break;
12694 
12695 	case I40E_VSI_SRIOV:
12696 		ctxt.pf_num = hw->pf_id;
12697 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12698 		ctxt.uplink_seid = vsi->uplink_seid;
12699 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12700 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12701 
12702 		/* This VSI is connected to VEB so the switch_id
12703 		 * should be set to zero by default.
12704 		 */
12705 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12706 			ctxt.info.valid_sections |=
12707 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12708 			ctxt.info.switch_id =
12709 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12710 		}
12711 
12712 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12713 			ctxt.info.valid_sections |=
12714 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12715 			ctxt.info.queueing_opt_flags |=
12716 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12717 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12718 		}
12719 
12720 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12721 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12722 		if (pf->vf[vsi->vf_id].spoofchk) {
12723 			ctxt.info.valid_sections |=
12724 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12725 			ctxt.info.sec_flags |=
12726 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12727 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12728 		}
12729 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12730 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12731 		break;
12732 
12733 	case I40E_VSI_IWARP:
12734 		/* send down message to iWARP */
12735 		break;
12736 
12737 	default:
12738 		return -ENODEV;
12739 	}
12740 
12741 	if (vsi->type != I40E_VSI_MAIN) {
12742 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12743 		if (ret) {
12744 			dev_info(&vsi->back->pdev->dev,
12745 				 "add vsi failed, err %s aq_err %s\n",
12746 				 i40e_stat_str(&pf->hw, ret),
12747 				 i40e_aq_str(&pf->hw,
12748 					     pf->hw.aq.asq_last_status));
12749 			ret = -ENOENT;
12750 			goto err;
12751 		}
12752 		vsi->info = ctxt.info;
12753 		vsi->info.valid_sections = 0;
12754 		vsi->seid = ctxt.seid;
12755 		vsi->id = ctxt.vsi_number;
12756 	}
12757 
12758 	vsi->active_filters = 0;
12759 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12760 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12761 	/* If macvlan filters already exist, force them to get loaded */
12762 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12763 		f->state = I40E_FILTER_NEW;
12764 		f_count++;
12765 	}
12766 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12767 
12768 	if (f_count) {
12769 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12770 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
12771 	}
12772 
12773 	/* Update VSI BW information */
12774 	ret = i40e_vsi_get_bw_info(vsi);
12775 	if (ret) {
12776 		dev_info(&pf->pdev->dev,
12777 			 "couldn't get vsi bw info, err %s aq_err %s\n",
12778 			 i40e_stat_str(&pf->hw, ret),
12779 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12780 		/* VSI is already added so not tearing that up */
12781 		ret = 0;
12782 	}
12783 
12784 err:
12785 	return ret;
12786 }
12787 
12788 /**
12789  * i40e_vsi_release - Delete a VSI and free its resources
12790  * @vsi: the VSI being removed
12791  *
12792  * Returns 0 on success or < 0 on error
12793  **/
12794 int i40e_vsi_release(struct i40e_vsi *vsi)
12795 {
12796 	struct i40e_mac_filter *f;
12797 	struct hlist_node *h;
12798 	struct i40e_veb *veb = NULL;
12799 	struct i40e_pf *pf;
12800 	u16 uplink_seid;
12801 	int i, n, bkt;
12802 
12803 	pf = vsi->back;
12804 
12805 	/* release of a VEB-owner or last VSI is not allowed */
12806 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12807 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12808 			 vsi->seid, vsi->uplink_seid);
12809 		return -ENODEV;
12810 	}
12811 	if (vsi == pf->vsi[pf->lan_vsi] &&
12812 	    !test_bit(__I40E_DOWN, pf->state)) {
12813 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12814 		return -ENODEV;
12815 	}
12816 
12817 	uplink_seid = vsi->uplink_seid;
12818 	if (vsi->type != I40E_VSI_SRIOV) {
12819 		if (vsi->netdev_registered) {
12820 			vsi->netdev_registered = false;
12821 			if (vsi->netdev) {
12822 				/* results in a call to i40e_close() */
12823 				unregister_netdev(vsi->netdev);
12824 			}
12825 		} else {
12826 			i40e_vsi_close(vsi);
12827 		}
12828 		i40e_vsi_disable_irq(vsi);
12829 	}
12830 
12831 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12832 
12833 	/* clear the sync flag on all filters */
12834 	if (vsi->netdev) {
12835 		__dev_uc_unsync(vsi->netdev, NULL);
12836 		__dev_mc_unsync(vsi->netdev, NULL);
12837 	}
12838 
12839 	/* make sure any remaining filters are marked for deletion */
12840 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12841 		__i40e_del_filter(vsi, f);
12842 
12843 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12844 
12845 	i40e_sync_vsi_filters(vsi);
12846 
12847 	i40e_vsi_delete(vsi);
12848 	i40e_vsi_free_q_vectors(vsi);
12849 	if (vsi->netdev) {
12850 		free_netdev(vsi->netdev);
12851 		vsi->netdev = NULL;
12852 	}
12853 	i40e_vsi_clear_rings(vsi);
12854 	i40e_vsi_clear(vsi);
12855 
12856 	/* If this was the last thing on the VEB, except for the
12857 	 * controlling VSI, remove the VEB, which puts the controlling
12858 	 * VSI onto the next level down in the switch.
12859 	 *
12860 	 * Well, okay, there's one more exception here: don't remove
12861 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
12862 	 * from up the network stack.
12863 	 */
12864 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12865 		if (pf->vsi[i] &&
12866 		    pf->vsi[i]->uplink_seid == uplink_seid &&
12867 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12868 			n++;      /* count the VSIs */
12869 		}
12870 	}
12871 	for (i = 0; i < I40E_MAX_VEB; i++) {
12872 		if (!pf->veb[i])
12873 			continue;
12874 		if (pf->veb[i]->uplink_seid == uplink_seid)
12875 			n++;     /* count the VEBs */
12876 		if (pf->veb[i]->seid == uplink_seid)
12877 			veb = pf->veb[i];
12878 	}
12879 	if (n == 0 && veb && veb->uplink_seid != 0)
12880 		i40e_veb_release(veb);
12881 
12882 	return 0;
12883 }
12884 
12885 /**
12886  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12887  * @vsi: ptr to the VSI
12888  *
12889  * This should only be called after i40e_vsi_mem_alloc() which allocates the
12890  * corresponding SW VSI structure and initializes num_queue_pairs for the
12891  * newly allocated VSI.
12892  *
12893  * Returns 0 on success or negative on failure
12894  **/
12895 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12896 {
12897 	int ret = -ENOENT;
12898 	struct i40e_pf *pf = vsi->back;
12899 
12900 	if (vsi->q_vectors[0]) {
12901 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12902 			 vsi->seid);
12903 		return -EEXIST;
12904 	}
12905 
12906 	if (vsi->base_vector) {
12907 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12908 			 vsi->seid, vsi->base_vector);
12909 		return -EEXIST;
12910 	}
12911 
12912 	ret = i40e_vsi_alloc_q_vectors(vsi);
12913 	if (ret) {
12914 		dev_info(&pf->pdev->dev,
12915 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12916 			 vsi->num_q_vectors, vsi->seid, ret);
12917 		vsi->num_q_vectors = 0;
12918 		goto vector_setup_out;
12919 	}
12920 
12921 	/* In Legacy mode, we do not have to get any other vector since we
12922 	 * piggyback on the misc/ICR0 for queue interrupts.
12923 	*/
12924 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12925 		return ret;
12926 	if (vsi->num_q_vectors)
12927 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12928 						 vsi->num_q_vectors, vsi->idx);
12929 	if (vsi->base_vector < 0) {
12930 		dev_info(&pf->pdev->dev,
12931 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12932 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12933 		i40e_vsi_free_q_vectors(vsi);
12934 		ret = -ENOENT;
12935 		goto vector_setup_out;
12936 	}
12937 
12938 vector_setup_out:
12939 	return ret;
12940 }
12941 
12942 /**
12943  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12944  * @vsi: pointer to the vsi.
12945  *
12946  * This re-allocates a vsi's queue resources.
12947  *
12948  * Returns pointer to the successfully allocated and configured VSI sw struct
12949  * on success, otherwise returns NULL on failure.
12950  **/
12951 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12952 {
12953 	u16 alloc_queue_pairs;
12954 	struct i40e_pf *pf;
12955 	u8 enabled_tc;
12956 	int ret;
12957 
12958 	if (!vsi)
12959 		return NULL;
12960 
12961 	pf = vsi->back;
12962 
12963 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12964 	i40e_vsi_clear_rings(vsi);
12965 
12966 	i40e_vsi_free_arrays(vsi, false);
12967 	i40e_set_num_rings_in_vsi(vsi);
12968 	ret = i40e_vsi_alloc_arrays(vsi, false);
12969 	if (ret)
12970 		goto err_vsi;
12971 
12972 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12973 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12974 
12975 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12976 	if (ret < 0) {
12977 		dev_info(&pf->pdev->dev,
12978 			 "failed to get tracking for %d queues for VSI %d err %d\n",
12979 			 alloc_queue_pairs, vsi->seid, ret);
12980 		goto err_vsi;
12981 	}
12982 	vsi->base_queue = ret;
12983 
12984 	/* Update the FW view of the VSI. Force a reset of TC and queue
12985 	 * layout configurations.
12986 	 */
12987 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12988 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12989 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12990 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12991 	if (vsi->type == I40E_VSI_MAIN)
12992 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12993 
12994 	/* assign it some queues */
12995 	ret = i40e_alloc_rings(vsi);
12996 	if (ret)
12997 		goto err_rings;
12998 
12999 	/* map all of the rings to the q_vectors */
13000 	i40e_vsi_map_rings_to_vectors(vsi);
13001 	return vsi;
13002 
13003 err_rings:
13004 	i40e_vsi_free_q_vectors(vsi);
13005 	if (vsi->netdev_registered) {
13006 		vsi->netdev_registered = false;
13007 		unregister_netdev(vsi->netdev);
13008 		free_netdev(vsi->netdev);
13009 		vsi->netdev = NULL;
13010 	}
13011 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13012 err_vsi:
13013 	i40e_vsi_clear(vsi);
13014 	return NULL;
13015 }
13016 
13017 /**
13018  * i40e_vsi_setup - Set up a VSI by a given type
13019  * @pf: board private structure
13020  * @type: VSI type
13021  * @uplink_seid: the switch element to link to
13022  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13023  *
13024  * This allocates the sw VSI structure and its queue resources, then add a VSI
13025  * to the identified VEB.
13026  *
13027  * Returns pointer to the successfully allocated and configure VSI sw struct on
13028  * success, otherwise returns NULL on failure.
13029  **/
13030 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13031 				u16 uplink_seid, u32 param1)
13032 {
13033 	struct i40e_vsi *vsi = NULL;
13034 	struct i40e_veb *veb = NULL;
13035 	u16 alloc_queue_pairs;
13036 	int ret, i;
13037 	int v_idx;
13038 
13039 	/* The requested uplink_seid must be either
13040 	 *     - the PF's port seid
13041 	 *              no VEB is needed because this is the PF
13042 	 *              or this is a Flow Director special case VSI
13043 	 *     - seid of an existing VEB
13044 	 *     - seid of a VSI that owns an existing VEB
13045 	 *     - seid of a VSI that doesn't own a VEB
13046 	 *              a new VEB is created and the VSI becomes the owner
13047 	 *     - seid of the PF VSI, which is what creates the first VEB
13048 	 *              this is a special case of the previous
13049 	 *
13050 	 * Find which uplink_seid we were given and create a new VEB if needed
13051 	 */
13052 	for (i = 0; i < I40E_MAX_VEB; i++) {
13053 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13054 			veb = pf->veb[i];
13055 			break;
13056 		}
13057 	}
13058 
13059 	if (!veb && uplink_seid != pf->mac_seid) {
13060 
13061 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13062 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13063 				vsi = pf->vsi[i];
13064 				break;
13065 			}
13066 		}
13067 		if (!vsi) {
13068 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13069 				 uplink_seid);
13070 			return NULL;
13071 		}
13072 
13073 		if (vsi->uplink_seid == pf->mac_seid)
13074 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13075 					     vsi->tc_config.enabled_tc);
13076 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13077 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13078 					     vsi->tc_config.enabled_tc);
13079 		if (veb) {
13080 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13081 				dev_info(&vsi->back->pdev->dev,
13082 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13083 				return NULL;
13084 			}
13085 			/* We come up by default in VEPA mode if SRIOV is not
13086 			 * already enabled, in which case we can't force VEPA
13087 			 * mode.
13088 			 */
13089 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13090 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13091 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13092 			}
13093 			i40e_config_bridge_mode(veb);
13094 		}
13095 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13096 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13097 				veb = pf->veb[i];
13098 		}
13099 		if (!veb) {
13100 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13101 			return NULL;
13102 		}
13103 
13104 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13105 		uplink_seid = veb->seid;
13106 	}
13107 
13108 	/* get vsi sw struct */
13109 	v_idx = i40e_vsi_mem_alloc(pf, type);
13110 	if (v_idx < 0)
13111 		goto err_alloc;
13112 	vsi = pf->vsi[v_idx];
13113 	if (!vsi)
13114 		goto err_alloc;
13115 	vsi->type = type;
13116 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13117 
13118 	if (type == I40E_VSI_MAIN)
13119 		pf->lan_vsi = v_idx;
13120 	else if (type == I40E_VSI_SRIOV)
13121 		vsi->vf_id = param1;
13122 	/* assign it some queues */
13123 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13124 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13125 
13126 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13127 	if (ret < 0) {
13128 		dev_info(&pf->pdev->dev,
13129 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13130 			 alloc_queue_pairs, vsi->seid, ret);
13131 		goto err_vsi;
13132 	}
13133 	vsi->base_queue = ret;
13134 
13135 	/* get a VSI from the hardware */
13136 	vsi->uplink_seid = uplink_seid;
13137 	ret = i40e_add_vsi(vsi);
13138 	if (ret)
13139 		goto err_vsi;
13140 
13141 	switch (vsi->type) {
13142 	/* setup the netdev if needed */
13143 	case I40E_VSI_MAIN:
13144 	case I40E_VSI_VMDQ2:
13145 		ret = i40e_config_netdev(vsi);
13146 		if (ret)
13147 			goto err_netdev;
13148 		ret = register_netdev(vsi->netdev);
13149 		if (ret)
13150 			goto err_netdev;
13151 		vsi->netdev_registered = true;
13152 		netif_carrier_off(vsi->netdev);
13153 #ifdef CONFIG_I40E_DCB
13154 		/* Setup DCB netlink interface */
13155 		i40e_dcbnl_setup(vsi);
13156 #endif /* CONFIG_I40E_DCB */
13157 		/* fall through */
13158 
13159 	case I40E_VSI_FDIR:
13160 		/* set up vectors and rings if needed */
13161 		ret = i40e_vsi_setup_vectors(vsi);
13162 		if (ret)
13163 			goto err_msix;
13164 
13165 		ret = i40e_alloc_rings(vsi);
13166 		if (ret)
13167 			goto err_rings;
13168 
13169 		/* map all of the rings to the q_vectors */
13170 		i40e_vsi_map_rings_to_vectors(vsi);
13171 
13172 		i40e_vsi_reset_stats(vsi);
13173 		break;
13174 
13175 	default:
13176 		/* no netdev or rings for the other VSI types */
13177 		break;
13178 	}
13179 
13180 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13181 	    (vsi->type == I40E_VSI_VMDQ2)) {
13182 		ret = i40e_vsi_config_rss(vsi);
13183 	}
13184 	return vsi;
13185 
13186 err_rings:
13187 	i40e_vsi_free_q_vectors(vsi);
13188 err_msix:
13189 	if (vsi->netdev_registered) {
13190 		vsi->netdev_registered = false;
13191 		unregister_netdev(vsi->netdev);
13192 		free_netdev(vsi->netdev);
13193 		vsi->netdev = NULL;
13194 	}
13195 err_netdev:
13196 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13197 err_vsi:
13198 	i40e_vsi_clear(vsi);
13199 err_alloc:
13200 	return NULL;
13201 }
13202 
13203 /**
13204  * i40e_veb_get_bw_info - Query VEB BW information
13205  * @veb: the veb to query
13206  *
13207  * Query the Tx scheduler BW configuration data for given VEB
13208  **/
13209 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13210 {
13211 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13212 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13213 	struct i40e_pf *pf = veb->pf;
13214 	struct i40e_hw *hw = &pf->hw;
13215 	u32 tc_bw_max;
13216 	int ret = 0;
13217 	int i;
13218 
13219 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13220 						  &bw_data, NULL);
13221 	if (ret) {
13222 		dev_info(&pf->pdev->dev,
13223 			 "query veb bw config failed, err %s aq_err %s\n",
13224 			 i40e_stat_str(&pf->hw, ret),
13225 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13226 		goto out;
13227 	}
13228 
13229 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13230 						   &ets_data, NULL);
13231 	if (ret) {
13232 		dev_info(&pf->pdev->dev,
13233 			 "query veb bw ets config failed, err %s aq_err %s\n",
13234 			 i40e_stat_str(&pf->hw, ret),
13235 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13236 		goto out;
13237 	}
13238 
13239 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13240 	veb->bw_max_quanta = ets_data.tc_bw_max;
13241 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13242 	veb->enabled_tc = ets_data.tc_valid_bits;
13243 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13244 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13245 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13246 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13247 		veb->bw_tc_limit_credits[i] =
13248 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13249 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13250 	}
13251 
13252 out:
13253 	return ret;
13254 }
13255 
13256 /**
13257  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13258  * @pf: board private structure
13259  *
13260  * On error: returns error code (negative)
13261  * On success: returns vsi index in PF (positive)
13262  **/
13263 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13264 {
13265 	int ret = -ENOENT;
13266 	struct i40e_veb *veb;
13267 	int i;
13268 
13269 	/* Need to protect the allocation of switch elements at the PF level */
13270 	mutex_lock(&pf->switch_mutex);
13271 
13272 	/* VEB list may be fragmented if VEB creation/destruction has
13273 	 * been happening.  We can afford to do a quick scan to look
13274 	 * for any free slots in the list.
13275 	 *
13276 	 * find next empty veb slot, looping back around if necessary
13277 	 */
13278 	i = 0;
13279 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13280 		i++;
13281 	if (i >= I40E_MAX_VEB) {
13282 		ret = -ENOMEM;
13283 		goto err_alloc_veb;  /* out of VEB slots! */
13284 	}
13285 
13286 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13287 	if (!veb) {
13288 		ret = -ENOMEM;
13289 		goto err_alloc_veb;
13290 	}
13291 	veb->pf = pf;
13292 	veb->idx = i;
13293 	veb->enabled_tc = 1;
13294 
13295 	pf->veb[i] = veb;
13296 	ret = i;
13297 err_alloc_veb:
13298 	mutex_unlock(&pf->switch_mutex);
13299 	return ret;
13300 }
13301 
13302 /**
13303  * i40e_switch_branch_release - Delete a branch of the switch tree
13304  * @branch: where to start deleting
13305  *
13306  * This uses recursion to find the tips of the branch to be
13307  * removed, deleting until we get back to and can delete this VEB.
13308  **/
13309 static void i40e_switch_branch_release(struct i40e_veb *branch)
13310 {
13311 	struct i40e_pf *pf = branch->pf;
13312 	u16 branch_seid = branch->seid;
13313 	u16 veb_idx = branch->idx;
13314 	int i;
13315 
13316 	/* release any VEBs on this VEB - RECURSION */
13317 	for (i = 0; i < I40E_MAX_VEB; i++) {
13318 		if (!pf->veb[i])
13319 			continue;
13320 		if (pf->veb[i]->uplink_seid == branch->seid)
13321 			i40e_switch_branch_release(pf->veb[i]);
13322 	}
13323 
13324 	/* Release the VSIs on this VEB, but not the owner VSI.
13325 	 *
13326 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13327 	 *       the VEB itself, so don't use (*branch) after this loop.
13328 	 */
13329 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13330 		if (!pf->vsi[i])
13331 			continue;
13332 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13333 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13334 			i40e_vsi_release(pf->vsi[i]);
13335 		}
13336 	}
13337 
13338 	/* There's one corner case where the VEB might not have been
13339 	 * removed, so double check it here and remove it if needed.
13340 	 * This case happens if the veb was created from the debugfs
13341 	 * commands and no VSIs were added to it.
13342 	 */
13343 	if (pf->veb[veb_idx])
13344 		i40e_veb_release(pf->veb[veb_idx]);
13345 }
13346 
13347 /**
13348  * i40e_veb_clear - remove veb struct
13349  * @veb: the veb to remove
13350  **/
13351 static void i40e_veb_clear(struct i40e_veb *veb)
13352 {
13353 	if (!veb)
13354 		return;
13355 
13356 	if (veb->pf) {
13357 		struct i40e_pf *pf = veb->pf;
13358 
13359 		mutex_lock(&pf->switch_mutex);
13360 		if (pf->veb[veb->idx] == veb)
13361 			pf->veb[veb->idx] = NULL;
13362 		mutex_unlock(&pf->switch_mutex);
13363 	}
13364 
13365 	kfree(veb);
13366 }
13367 
13368 /**
13369  * i40e_veb_release - Delete a VEB and free its resources
13370  * @veb: the VEB being removed
13371  **/
13372 void i40e_veb_release(struct i40e_veb *veb)
13373 {
13374 	struct i40e_vsi *vsi = NULL;
13375 	struct i40e_pf *pf;
13376 	int i, n = 0;
13377 
13378 	pf = veb->pf;
13379 
13380 	/* find the remaining VSI and check for extras */
13381 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13382 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13383 			n++;
13384 			vsi = pf->vsi[i];
13385 		}
13386 	}
13387 	if (n != 1) {
13388 		dev_info(&pf->pdev->dev,
13389 			 "can't remove VEB %d with %d VSIs left\n",
13390 			 veb->seid, n);
13391 		return;
13392 	}
13393 
13394 	/* move the remaining VSI to uplink veb */
13395 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13396 	if (veb->uplink_seid) {
13397 		vsi->uplink_seid = veb->uplink_seid;
13398 		if (veb->uplink_seid == pf->mac_seid)
13399 			vsi->veb_idx = I40E_NO_VEB;
13400 		else
13401 			vsi->veb_idx = veb->veb_idx;
13402 	} else {
13403 		/* floating VEB */
13404 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13405 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13406 	}
13407 
13408 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13409 	i40e_veb_clear(veb);
13410 }
13411 
13412 /**
13413  * i40e_add_veb - create the VEB in the switch
13414  * @veb: the VEB to be instantiated
13415  * @vsi: the controlling VSI
13416  **/
13417 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13418 {
13419 	struct i40e_pf *pf = veb->pf;
13420 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13421 	int ret;
13422 
13423 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13424 			      veb->enabled_tc, false,
13425 			      &veb->seid, enable_stats, NULL);
13426 
13427 	/* get a VEB from the hardware */
13428 	if (ret) {
13429 		dev_info(&pf->pdev->dev,
13430 			 "couldn't add VEB, err %s aq_err %s\n",
13431 			 i40e_stat_str(&pf->hw, ret),
13432 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13433 		return -EPERM;
13434 	}
13435 
13436 	/* get statistics counter */
13437 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13438 					 &veb->stats_idx, NULL, NULL, NULL);
13439 	if (ret) {
13440 		dev_info(&pf->pdev->dev,
13441 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13442 			 i40e_stat_str(&pf->hw, ret),
13443 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13444 		return -EPERM;
13445 	}
13446 	ret = i40e_veb_get_bw_info(veb);
13447 	if (ret) {
13448 		dev_info(&pf->pdev->dev,
13449 			 "couldn't get VEB bw info, err %s aq_err %s\n",
13450 			 i40e_stat_str(&pf->hw, ret),
13451 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13452 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13453 		return -ENOENT;
13454 	}
13455 
13456 	vsi->uplink_seid = veb->seid;
13457 	vsi->veb_idx = veb->idx;
13458 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13459 
13460 	return 0;
13461 }
13462 
13463 /**
13464  * i40e_veb_setup - Set up a VEB
13465  * @pf: board private structure
13466  * @flags: VEB setup flags
13467  * @uplink_seid: the switch element to link to
13468  * @vsi_seid: the initial VSI seid
13469  * @enabled_tc: Enabled TC bit-map
13470  *
13471  * This allocates the sw VEB structure and links it into the switch
13472  * It is possible and legal for this to be a duplicate of an already
13473  * existing VEB.  It is also possible for both uplink and vsi seids
13474  * to be zero, in order to create a floating VEB.
13475  *
13476  * Returns pointer to the successfully allocated VEB sw struct on
13477  * success, otherwise returns NULL on failure.
13478  **/
13479 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13480 				u16 uplink_seid, u16 vsi_seid,
13481 				u8 enabled_tc)
13482 {
13483 	struct i40e_veb *veb, *uplink_veb = NULL;
13484 	int vsi_idx, veb_idx;
13485 	int ret;
13486 
13487 	/* if one seid is 0, the other must be 0 to create a floating relay */
13488 	if ((uplink_seid == 0 || vsi_seid == 0) &&
13489 	    (uplink_seid + vsi_seid != 0)) {
13490 		dev_info(&pf->pdev->dev,
13491 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13492 			 uplink_seid, vsi_seid);
13493 		return NULL;
13494 	}
13495 
13496 	/* make sure there is such a vsi and uplink */
13497 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13498 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13499 			break;
13500 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
13501 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13502 			 vsi_seid);
13503 		return NULL;
13504 	}
13505 
13506 	if (uplink_seid && uplink_seid != pf->mac_seid) {
13507 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
13508 			if (pf->veb[veb_idx] &&
13509 			    pf->veb[veb_idx]->seid == uplink_seid) {
13510 				uplink_veb = pf->veb[veb_idx];
13511 				break;
13512 			}
13513 		}
13514 		if (!uplink_veb) {
13515 			dev_info(&pf->pdev->dev,
13516 				 "uplink seid %d not found\n", uplink_seid);
13517 			return NULL;
13518 		}
13519 	}
13520 
13521 	/* get veb sw struct */
13522 	veb_idx = i40e_veb_mem_alloc(pf);
13523 	if (veb_idx < 0)
13524 		goto err_alloc;
13525 	veb = pf->veb[veb_idx];
13526 	veb->flags = flags;
13527 	veb->uplink_seid = uplink_seid;
13528 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
13529 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
13530 
13531 	/* create the VEB in the switch */
13532 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
13533 	if (ret)
13534 		goto err_veb;
13535 	if (vsi_idx == pf->lan_vsi)
13536 		pf->lan_veb = veb->idx;
13537 
13538 	return veb;
13539 
13540 err_veb:
13541 	i40e_veb_clear(veb);
13542 err_alloc:
13543 	return NULL;
13544 }
13545 
13546 /**
13547  * i40e_setup_pf_switch_element - set PF vars based on switch type
13548  * @pf: board private structure
13549  * @ele: element we are building info from
13550  * @num_reported: total number of elements
13551  * @printconfig: should we print the contents
13552  *
13553  * helper function to assist in extracting a few useful SEID values.
13554  **/
13555 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
13556 				struct i40e_aqc_switch_config_element_resp *ele,
13557 				u16 num_reported, bool printconfig)
13558 {
13559 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
13560 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
13561 	u8 element_type = ele->element_type;
13562 	u16 seid = le16_to_cpu(ele->seid);
13563 
13564 	if (printconfig)
13565 		dev_info(&pf->pdev->dev,
13566 			 "type=%d seid=%d uplink=%d downlink=%d\n",
13567 			 element_type, seid, uplink_seid, downlink_seid);
13568 
13569 	switch (element_type) {
13570 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
13571 		pf->mac_seid = seid;
13572 		break;
13573 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
13574 		/* Main VEB? */
13575 		if (uplink_seid != pf->mac_seid)
13576 			break;
13577 		if (pf->lan_veb >= I40E_MAX_VEB) {
13578 			int v;
13579 
13580 			/* find existing or else empty VEB */
13581 			for (v = 0; v < I40E_MAX_VEB; v++) {
13582 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
13583 					pf->lan_veb = v;
13584 					break;
13585 				}
13586 			}
13587 			if (pf->lan_veb >= I40E_MAX_VEB) {
13588 				v = i40e_veb_mem_alloc(pf);
13589 				if (v < 0)
13590 					break;
13591 				pf->lan_veb = v;
13592 			}
13593 		}
13594 		if (pf->lan_veb >= I40E_MAX_VEB)
13595 			break;
13596 
13597 		pf->veb[pf->lan_veb]->seid = seid;
13598 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
13599 		pf->veb[pf->lan_veb]->pf = pf;
13600 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
13601 		break;
13602 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
13603 		if (num_reported != 1)
13604 			break;
13605 		/* This is immediately after a reset so we can assume this is
13606 		 * the PF's VSI
13607 		 */
13608 		pf->mac_seid = uplink_seid;
13609 		pf->pf_seid = downlink_seid;
13610 		pf->main_vsi_seid = seid;
13611 		if (printconfig)
13612 			dev_info(&pf->pdev->dev,
13613 				 "pf_seid=%d main_vsi_seid=%d\n",
13614 				 pf->pf_seid, pf->main_vsi_seid);
13615 		break;
13616 	case I40E_SWITCH_ELEMENT_TYPE_PF:
13617 	case I40E_SWITCH_ELEMENT_TYPE_VF:
13618 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
13619 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
13620 	case I40E_SWITCH_ELEMENT_TYPE_PE:
13621 	case I40E_SWITCH_ELEMENT_TYPE_PA:
13622 		/* ignore these for now */
13623 		break;
13624 	default:
13625 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
13626 			 element_type, seid);
13627 		break;
13628 	}
13629 }
13630 
13631 /**
13632  * i40e_fetch_switch_configuration - Get switch config from firmware
13633  * @pf: board private structure
13634  * @printconfig: should we print the contents
13635  *
13636  * Get the current switch configuration from the device and
13637  * extract a few useful SEID values.
13638  **/
13639 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
13640 {
13641 	struct i40e_aqc_get_switch_config_resp *sw_config;
13642 	u16 next_seid = 0;
13643 	int ret = 0;
13644 	u8 *aq_buf;
13645 	int i;
13646 
13647 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13648 	if (!aq_buf)
13649 		return -ENOMEM;
13650 
13651 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13652 	do {
13653 		u16 num_reported, num_total;
13654 
13655 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13656 						I40E_AQ_LARGE_BUF,
13657 						&next_seid, NULL);
13658 		if (ret) {
13659 			dev_info(&pf->pdev->dev,
13660 				 "get switch config failed err %s aq_err %s\n",
13661 				 i40e_stat_str(&pf->hw, ret),
13662 				 i40e_aq_str(&pf->hw,
13663 					     pf->hw.aq.asq_last_status));
13664 			kfree(aq_buf);
13665 			return -ENOENT;
13666 		}
13667 
13668 		num_reported = le16_to_cpu(sw_config->header.num_reported);
13669 		num_total = le16_to_cpu(sw_config->header.num_total);
13670 
13671 		if (printconfig)
13672 			dev_info(&pf->pdev->dev,
13673 				 "header: %d reported %d total\n",
13674 				 num_reported, num_total);
13675 
13676 		for (i = 0; i < num_reported; i++) {
13677 			struct i40e_aqc_switch_config_element_resp *ele =
13678 				&sw_config->element[i];
13679 
13680 			i40e_setup_pf_switch_element(pf, ele, num_reported,
13681 						     printconfig);
13682 		}
13683 	} while (next_seid != 0);
13684 
13685 	kfree(aq_buf);
13686 	return ret;
13687 }
13688 
13689 /**
13690  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13691  * @pf: board private structure
13692  * @reinit: if the Main VSI needs to re-initialized.
13693  *
13694  * Returns 0 on success, negative value on failure
13695  **/
13696 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13697 {
13698 	u16 flags = 0;
13699 	int ret;
13700 
13701 	/* find out what's out there already */
13702 	ret = i40e_fetch_switch_configuration(pf, false);
13703 	if (ret) {
13704 		dev_info(&pf->pdev->dev,
13705 			 "couldn't fetch switch config, err %s aq_err %s\n",
13706 			 i40e_stat_str(&pf->hw, ret),
13707 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13708 		return ret;
13709 	}
13710 	i40e_pf_reset_stats(pf);
13711 
13712 	/* set the switch config bit for the whole device to
13713 	 * support limited promisc or true promisc
13714 	 * when user requests promisc. The default is limited
13715 	 * promisc.
13716 	*/
13717 
13718 	if ((pf->hw.pf_id == 0) &&
13719 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13720 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13721 		pf->last_sw_conf_flags = flags;
13722 	}
13723 
13724 	if (pf->hw.pf_id == 0) {
13725 		u16 valid_flags;
13726 
13727 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13728 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13729 						NULL);
13730 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13731 			dev_info(&pf->pdev->dev,
13732 				 "couldn't set switch config bits, err %s aq_err %s\n",
13733 				 i40e_stat_str(&pf->hw, ret),
13734 				 i40e_aq_str(&pf->hw,
13735 					     pf->hw.aq.asq_last_status));
13736 			/* not a fatal problem, just keep going */
13737 		}
13738 		pf->last_sw_conf_valid_flags = valid_flags;
13739 	}
13740 
13741 	/* first time setup */
13742 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13743 		struct i40e_vsi *vsi = NULL;
13744 		u16 uplink_seid;
13745 
13746 		/* Set up the PF VSI associated with the PF's main VSI
13747 		 * that is already in the HW switch
13748 		 */
13749 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
13750 			uplink_seid = pf->veb[pf->lan_veb]->seid;
13751 		else
13752 			uplink_seid = pf->mac_seid;
13753 		if (pf->lan_vsi == I40E_NO_VSI)
13754 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13755 		else if (reinit)
13756 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13757 		if (!vsi) {
13758 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13759 			i40e_cloud_filter_exit(pf);
13760 			i40e_fdir_teardown(pf);
13761 			return -EAGAIN;
13762 		}
13763 	} else {
13764 		/* force a reset of TC and queue layout configurations */
13765 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13766 
13767 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13768 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13769 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13770 	}
13771 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13772 
13773 	i40e_fdir_sb_setup(pf);
13774 
13775 	/* Setup static PF queue filter control settings */
13776 	ret = i40e_setup_pf_filter_control(pf);
13777 	if (ret) {
13778 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13779 			 ret);
13780 		/* Failure here should not stop continuing other steps */
13781 	}
13782 
13783 	/* enable RSS in the HW, even for only one queue, as the stack can use
13784 	 * the hash
13785 	 */
13786 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13787 		i40e_pf_config_rss(pf);
13788 
13789 	/* fill in link information and enable LSE reporting */
13790 	i40e_link_event(pf);
13791 
13792 	/* Initialize user-specific link properties */
13793 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13794 				  I40E_AQ_AN_COMPLETED) ? true : false);
13795 
13796 	i40e_ptp_init(pf);
13797 
13798 	/* repopulate tunnel port filters */
13799 	i40e_sync_udp_filters(pf);
13800 
13801 	return ret;
13802 }
13803 
13804 /**
13805  * i40e_determine_queue_usage - Work out queue distribution
13806  * @pf: board private structure
13807  **/
13808 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13809 {
13810 	int queues_left;
13811 	int q_max;
13812 
13813 	pf->num_lan_qps = 0;
13814 
13815 	/* Find the max queues to be put into basic use.  We'll always be
13816 	 * using TC0, whether or not DCB is running, and TC0 will get the
13817 	 * big RSS set.
13818 	 */
13819 	queues_left = pf->hw.func_caps.num_tx_qp;
13820 
13821 	if ((queues_left == 1) ||
13822 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13823 		/* one qp for PF, no queues for anything else */
13824 		queues_left = 0;
13825 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13826 
13827 		/* make sure all the fancies are disabled */
13828 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13829 			       I40E_FLAG_IWARP_ENABLED	|
13830 			       I40E_FLAG_FD_SB_ENABLED	|
13831 			       I40E_FLAG_FD_ATR_ENABLED	|
13832 			       I40E_FLAG_DCB_CAPABLE	|
13833 			       I40E_FLAG_DCB_ENABLED	|
13834 			       I40E_FLAG_SRIOV_ENABLED	|
13835 			       I40E_FLAG_VMDQ_ENABLED);
13836 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13837 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13838 				  I40E_FLAG_FD_SB_ENABLED |
13839 				  I40E_FLAG_FD_ATR_ENABLED |
13840 				  I40E_FLAG_DCB_CAPABLE))) {
13841 		/* one qp for PF */
13842 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13843 		queues_left -= pf->num_lan_qps;
13844 
13845 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13846 			       I40E_FLAG_IWARP_ENABLED	|
13847 			       I40E_FLAG_FD_SB_ENABLED	|
13848 			       I40E_FLAG_FD_ATR_ENABLED	|
13849 			       I40E_FLAG_DCB_ENABLED	|
13850 			       I40E_FLAG_VMDQ_ENABLED);
13851 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13852 	} else {
13853 		/* Not enough queues for all TCs */
13854 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13855 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13856 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13857 					I40E_FLAG_DCB_ENABLED);
13858 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13859 		}
13860 
13861 		/* limit lan qps to the smaller of qps, cpus or msix */
13862 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13863 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13864 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13865 		pf->num_lan_qps = q_max;
13866 
13867 		queues_left -= pf->num_lan_qps;
13868 	}
13869 
13870 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13871 		if (queues_left > 1) {
13872 			queues_left -= 1; /* save 1 queue for FD */
13873 		} else {
13874 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13875 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13876 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13877 		}
13878 	}
13879 
13880 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13881 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13882 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13883 					(queues_left / pf->num_vf_qps));
13884 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13885 	}
13886 
13887 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13888 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13889 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13890 					  (queues_left / pf->num_vmdq_qps));
13891 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13892 	}
13893 
13894 	pf->queues_left = queues_left;
13895 	dev_dbg(&pf->pdev->dev,
13896 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13897 		pf->hw.func_caps.num_tx_qp,
13898 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13899 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13900 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13901 		queues_left);
13902 }
13903 
13904 /**
13905  * i40e_setup_pf_filter_control - Setup PF static filter control
13906  * @pf: PF to be setup
13907  *
13908  * i40e_setup_pf_filter_control sets up a PF's initial filter control
13909  * settings. If PE/FCoE are enabled then it will also set the per PF
13910  * based filter sizes required for them. It also enables Flow director,
13911  * ethertype and macvlan type filter settings for the pf.
13912  *
13913  * Returns 0 on success, negative on failure
13914  **/
13915 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13916 {
13917 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
13918 
13919 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13920 
13921 	/* Flow Director is enabled */
13922 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13923 		settings->enable_fdir = true;
13924 
13925 	/* Ethtype and MACVLAN filters enabled for PF */
13926 	settings->enable_ethtype = true;
13927 	settings->enable_macvlan = true;
13928 
13929 	if (i40e_set_filter_control(&pf->hw, settings))
13930 		return -ENOENT;
13931 
13932 	return 0;
13933 }
13934 
13935 #define INFO_STRING_LEN 255
13936 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13937 static void i40e_print_features(struct i40e_pf *pf)
13938 {
13939 	struct i40e_hw *hw = &pf->hw;
13940 	char *buf;
13941 	int i;
13942 
13943 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13944 	if (!buf)
13945 		return;
13946 
13947 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13948 #ifdef CONFIG_PCI_IOV
13949 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13950 #endif
13951 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13952 		      pf->hw.func_caps.num_vsis,
13953 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
13954 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
13955 		i += snprintf(&buf[i], REMAIN(i), " RSS");
13956 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13957 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13958 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13959 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13960 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13961 	}
13962 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13963 		i += snprintf(&buf[i], REMAIN(i), " DCB");
13964 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13965 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
13966 	if (pf->flags & I40E_FLAG_PTP)
13967 		i += snprintf(&buf[i], REMAIN(i), " PTP");
13968 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13969 		i += snprintf(&buf[i], REMAIN(i), " VEB");
13970 	else
13971 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
13972 
13973 	dev_info(&pf->pdev->dev, "%s\n", buf);
13974 	kfree(buf);
13975 	WARN_ON(i > INFO_STRING_LEN);
13976 }
13977 
13978 /**
13979  * i40e_get_platform_mac_addr - get platform-specific MAC address
13980  * @pdev: PCI device information struct
13981  * @pf: board private structure
13982  *
13983  * Look up the MAC address for the device. First we'll try
13984  * eth_platform_get_mac_address, which will check Open Firmware, or arch
13985  * specific fallback. Otherwise, we'll default to the stored value in
13986  * firmware.
13987  **/
13988 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13989 {
13990 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13991 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13992 }
13993 
13994 /**
13995  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
13996  * @fec_cfg: FEC option to set in flags
13997  * @flags: ptr to flags in which we set FEC option
13998  **/
13999 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14000 {
14001 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14002 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14003 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14004 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14005 		*flags |= I40E_FLAG_RS_FEC;
14006 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14007 	}
14008 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14009 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14010 		*flags |= I40E_FLAG_BASE_R_FEC;
14011 		*flags &= ~I40E_FLAG_RS_FEC;
14012 	}
14013 	if (fec_cfg == 0)
14014 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14015 }
14016 
14017 /**
14018  * i40e_check_recovery_mode - check if we are running transition firmware
14019  * @pf: board private structure
14020  *
14021  * Check registers indicating the firmware runs in recovery mode. Sets the
14022  * appropriate driver state.
14023  *
14024  * Returns true if the recovery mode was detected, false otherwise
14025  **/
14026 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14027 {
14028 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14029 
14030 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14031 		dev_notice(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14032 		dev_notice(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14033 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14034 
14035 		return true;
14036 	}
14037 	if (test_and_clear_bit(__I40E_RECOVERY_MODE, pf->state))
14038 		dev_info(&pf->pdev->dev, "Reinitializing in normal mode with full functionality.\n");
14039 
14040 	return false;
14041 }
14042 
14043 /**
14044  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14045  * @pf: board private structure
14046  * @hw: ptr to the hardware info
14047  *
14048  * This function does a minimal setup of all subsystems needed for running
14049  * recovery mode.
14050  *
14051  * Returns 0 on success, negative on failure
14052  **/
14053 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14054 {
14055 	struct i40e_vsi *vsi;
14056 	int err;
14057 	int v_idx;
14058 
14059 	pci_save_state(pf->pdev);
14060 
14061 	/* set up periodic task facility */
14062 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14063 	pf->service_timer_period = HZ;
14064 
14065 	INIT_WORK(&pf->service_task, i40e_service_task);
14066 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14067 
14068 	err = i40e_init_interrupt_scheme(pf);
14069 	if (err)
14070 		goto err_switch_setup;
14071 
14072 	/* The number of VSIs reported by the FW is the minimum guaranteed
14073 	 * to us; HW supports far more and we share the remaining pool with
14074 	 * the other PFs. We allocate space for more than the guarantee with
14075 	 * the understanding that we might not get them all later.
14076 	 */
14077 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14078 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14079 	else
14080 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14081 
14082 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14083 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14084 			  GFP_KERNEL);
14085 	if (!pf->vsi) {
14086 		err = -ENOMEM;
14087 		goto err_switch_setup;
14088 	}
14089 
14090 	/* We allocate one VSI which is needed as absolute minimum
14091 	 * in order to register the netdev
14092 	 */
14093 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14094 	if (v_idx < 0)
14095 		goto err_switch_setup;
14096 	pf->lan_vsi = v_idx;
14097 	vsi = pf->vsi[v_idx];
14098 	if (!vsi)
14099 		goto err_switch_setup;
14100 	vsi->alloc_queue_pairs = 1;
14101 	err = i40e_config_netdev(vsi);
14102 	if (err)
14103 		goto err_switch_setup;
14104 	err = register_netdev(vsi->netdev);
14105 	if (err)
14106 		goto err_switch_setup;
14107 	vsi->netdev_registered = true;
14108 	i40e_dbg_pf_init(pf);
14109 
14110 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14111 	if (err)
14112 		goto err_switch_setup;
14113 
14114 	/* tell the firmware that we're starting */
14115 	i40e_send_version(pf);
14116 
14117 	/* since everything's happy, start the service_task timer */
14118 	mod_timer(&pf->service_timer,
14119 		  round_jiffies(jiffies + pf->service_timer_period));
14120 
14121 	return 0;
14122 
14123 err_switch_setup:
14124 	i40e_reset_interrupt_capability(pf);
14125 	del_timer_sync(&pf->service_timer);
14126 	i40e_shutdown_adminq(hw);
14127 	iounmap(hw->hw_addr);
14128 	pci_disable_pcie_error_reporting(pf->pdev);
14129 	pci_release_mem_regions(pf->pdev);
14130 	pci_disable_device(pf->pdev);
14131 	kfree(pf);
14132 
14133 	return err;
14134 }
14135 
14136 /**
14137  * i40e_probe - Device initialization routine
14138  * @pdev: PCI device information struct
14139  * @ent: entry in i40e_pci_tbl
14140  *
14141  * i40e_probe initializes a PF identified by a pci_dev structure.
14142  * The OS initialization, configuring of the PF private structure,
14143  * and a hardware reset occur.
14144  *
14145  * Returns 0 on success, negative on failure
14146  **/
14147 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14148 {
14149 	struct i40e_aq_get_phy_abilities_resp abilities;
14150 	struct i40e_pf *pf;
14151 	struct i40e_hw *hw;
14152 	static u16 pfs_found;
14153 	u16 wol_nvm_bits;
14154 	u16 link_status;
14155 	int err;
14156 	u32 val;
14157 	u32 i;
14158 	u8 set_fc_aq_fail;
14159 
14160 	err = pci_enable_device_mem(pdev);
14161 	if (err)
14162 		return err;
14163 
14164 	/* set up for high or low dma */
14165 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14166 	if (err) {
14167 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14168 		if (err) {
14169 			dev_err(&pdev->dev,
14170 				"DMA configuration failed: 0x%x\n", err);
14171 			goto err_dma;
14172 		}
14173 	}
14174 
14175 	/* set up pci connections */
14176 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14177 	if (err) {
14178 		dev_info(&pdev->dev,
14179 			 "pci_request_selected_regions failed %d\n", err);
14180 		goto err_pci_reg;
14181 	}
14182 
14183 	pci_enable_pcie_error_reporting(pdev);
14184 	pci_set_master(pdev);
14185 
14186 	/* Now that we have a PCI connection, we need to do the
14187 	 * low level device setup.  This is primarily setting up
14188 	 * the Admin Queue structures and then querying for the
14189 	 * device's current profile information.
14190 	 */
14191 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14192 	if (!pf) {
14193 		err = -ENOMEM;
14194 		goto err_pf_alloc;
14195 	}
14196 	pf->next_vsi = 0;
14197 	pf->pdev = pdev;
14198 	set_bit(__I40E_DOWN, pf->state);
14199 
14200 	hw = &pf->hw;
14201 	hw->back = pf;
14202 
14203 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14204 				I40E_MAX_CSR_SPACE);
14205 	/* We believe that the highest register to read is
14206 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14207 	 * is not less than that before mapping to prevent a
14208 	 * kernel panic.
14209 	 */
14210 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14211 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14212 			pf->ioremap_len);
14213 		err = -ENOMEM;
14214 		goto err_ioremap;
14215 	}
14216 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14217 	if (!hw->hw_addr) {
14218 		err = -EIO;
14219 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14220 			 (unsigned int)pci_resource_start(pdev, 0),
14221 			 pf->ioremap_len, err);
14222 		goto err_ioremap;
14223 	}
14224 	hw->vendor_id = pdev->vendor;
14225 	hw->device_id = pdev->device;
14226 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14227 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14228 	hw->subsystem_device_id = pdev->subsystem_device;
14229 	hw->bus.device = PCI_SLOT(pdev->devfn);
14230 	hw->bus.func = PCI_FUNC(pdev->devfn);
14231 	hw->bus.bus_id = pdev->bus->number;
14232 	pf->instance = pfs_found;
14233 
14234 	/* Select something other than the 802.1ad ethertype for the
14235 	 * switch to use internally and drop on ingress.
14236 	 */
14237 	hw->switch_tag = 0xffff;
14238 	hw->first_tag = ETH_P_8021AD;
14239 	hw->second_tag = ETH_P_8021Q;
14240 
14241 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14242 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14243 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14244 
14245 	/* set up the locks for the AQ, do this only once in probe
14246 	 * and destroy them only once in remove
14247 	 */
14248 	mutex_init(&hw->aq.asq_mutex);
14249 	mutex_init(&hw->aq.arq_mutex);
14250 
14251 	pf->msg_enable = netif_msg_init(debug,
14252 					NETIF_MSG_DRV |
14253 					NETIF_MSG_PROBE |
14254 					NETIF_MSG_LINK);
14255 	if (debug < -1)
14256 		pf->hw.debug_mask = debug;
14257 
14258 	/* do a special CORER for clearing PXE mode once at init */
14259 	if (hw->revision_id == 0 &&
14260 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14261 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14262 		i40e_flush(hw);
14263 		msleep(200);
14264 		pf->corer_count++;
14265 
14266 		i40e_clear_pxe_mode(hw);
14267 	}
14268 
14269 	/* Reset here to make sure all is clean and to define PF 'n' */
14270 	i40e_clear_hw(hw);
14271 	if (!i40e_check_recovery_mode(pf)) {
14272 		err = i40e_pf_reset(hw);
14273 		if (err) {
14274 			dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
14275 			goto err_pf_reset;
14276 		}
14277 		pf->pfr_count++;
14278 	}
14279 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14280 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14281 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14282 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14283 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14284 
14285 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14286 		 "%s-%s:misc",
14287 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14288 
14289 	err = i40e_init_shared_code(hw);
14290 	if (err) {
14291 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14292 			 err);
14293 		goto err_pf_reset;
14294 	}
14295 
14296 	/* set up a default setting for link flow control */
14297 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14298 
14299 	err = i40e_init_adminq(hw);
14300 	if (err) {
14301 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14302 			dev_info(&pdev->dev,
14303 				 "The driver for the device stopped because the NVM image v%u.%u is newer than expected v%u.%u. You must install the most recent version of the network driver.\n",
14304 				 hw->aq.api_maj_ver,
14305 				 hw->aq.api_min_ver,
14306 				 I40E_FW_API_VERSION_MAJOR,
14307 				 I40E_FW_MINOR_VERSION(hw));
14308 		else
14309 			dev_info(&pdev->dev,
14310 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14311 
14312 		goto err_pf_reset;
14313 	}
14314 	i40e_get_oem_version(hw);
14315 
14316 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14317 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14318 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14319 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14320 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
14321 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
14322 
14323 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14324 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14325 		dev_info(&pdev->dev,
14326 			 "The driver for the device detected a newer version of the NVM image v%u.%u than expected v%u.%u. Please install the most recent version of the network driver.\n",
14327 			 hw->aq.api_maj_ver,
14328 			 hw->aq.api_min_ver,
14329 			 I40E_FW_API_VERSION_MAJOR,
14330 			 I40E_FW_MINOR_VERSION(hw));
14331 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14332 		dev_info(&pdev->dev,
14333 			 "The driver for the device detected an older version of the NVM image v%u.%u than expected v%u.%u. Please update the NVM image.\n",
14334 			 hw->aq.api_maj_ver,
14335 			 hw->aq.api_min_ver,
14336 			 I40E_FW_API_VERSION_MAJOR,
14337 			 I40E_FW_MINOR_VERSION(hw));
14338 
14339 	i40e_verify_eeprom(pf);
14340 
14341 	/* Rev 0 hardware was never productized */
14342 	if (hw->revision_id < 1)
14343 		dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
14344 
14345 	i40e_clear_pxe_mode(hw);
14346 
14347 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14348 	if (err)
14349 		goto err_adminq_setup;
14350 
14351 	err = i40e_sw_init(pf);
14352 	if (err) {
14353 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14354 		goto err_sw_init;
14355 	}
14356 
14357 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14358 		return i40e_init_recovery_mode(pf, hw);
14359 
14360 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14361 				hw->func_caps.num_rx_qp, 0, 0);
14362 	if (err) {
14363 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14364 		goto err_init_lan_hmc;
14365 	}
14366 
14367 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14368 	if (err) {
14369 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14370 		err = -ENOENT;
14371 		goto err_configure_lan_hmc;
14372 	}
14373 
14374 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14375 	 * Ignore error return codes because if it was already disabled via
14376 	 * hardware settings this will fail
14377 	 */
14378 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14379 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14380 		i40e_aq_stop_lldp(hw, true, false, NULL);
14381 	}
14382 
14383 	/* allow a platform config to override the HW addr */
14384 	i40e_get_platform_mac_addr(pdev, pf);
14385 
14386 	if (!is_valid_ether_addr(hw->mac.addr)) {
14387 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14388 		err = -EIO;
14389 		goto err_mac_addr;
14390 	}
14391 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14392 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14393 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14394 	if (is_valid_ether_addr(hw->mac.port_addr))
14395 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14396 
14397 	pci_set_drvdata(pdev, pf);
14398 	pci_save_state(pdev);
14399 
14400 	/* Enable FW to write default DCB config on link-up */
14401 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14402 
14403 #ifdef CONFIG_I40E_DCB
14404 	err = i40e_init_pf_dcb(pf);
14405 	if (err) {
14406 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14407 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14408 		/* Continue without DCB enabled */
14409 	}
14410 #endif /* CONFIG_I40E_DCB */
14411 
14412 	/* set up periodic task facility */
14413 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14414 	pf->service_timer_period = HZ;
14415 
14416 	INIT_WORK(&pf->service_task, i40e_service_task);
14417 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14418 
14419 	/* NVM bit on means WoL disabled for the port */
14420 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14421 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14422 		pf->wol_en = false;
14423 	else
14424 		pf->wol_en = true;
14425 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14426 
14427 	/* set up the main switch operations */
14428 	i40e_determine_queue_usage(pf);
14429 	err = i40e_init_interrupt_scheme(pf);
14430 	if (err)
14431 		goto err_switch_setup;
14432 
14433 	/* The number of VSIs reported by the FW is the minimum guaranteed
14434 	 * to us; HW supports far more and we share the remaining pool with
14435 	 * the other PFs. We allocate space for more than the guarantee with
14436 	 * the understanding that we might not get them all later.
14437 	 */
14438 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14439 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14440 	else
14441 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14442 
14443 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
14444 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14445 			  GFP_KERNEL);
14446 	if (!pf->vsi) {
14447 		err = -ENOMEM;
14448 		goto err_switch_setup;
14449 	}
14450 
14451 #ifdef CONFIG_PCI_IOV
14452 	/* prep for VF support */
14453 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14454 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
14455 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
14456 		if (pci_num_vf(pdev))
14457 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
14458 	}
14459 #endif
14460 	err = i40e_setup_pf_switch(pf, false);
14461 	if (err) {
14462 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
14463 		goto err_vsis;
14464 	}
14465 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
14466 
14467 	/* Make sure flow control is set according to current settings */
14468 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
14469 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
14470 		dev_dbg(&pf->pdev->dev,
14471 			"Set fc with err %s aq_err %s on get_phy_cap\n",
14472 			i40e_stat_str(hw, err),
14473 			i40e_aq_str(hw, hw->aq.asq_last_status));
14474 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
14475 		dev_dbg(&pf->pdev->dev,
14476 			"Set fc with err %s aq_err %s on set_phy_config\n",
14477 			i40e_stat_str(hw, err),
14478 			i40e_aq_str(hw, hw->aq.asq_last_status));
14479 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
14480 		dev_dbg(&pf->pdev->dev,
14481 			"Set fc with err %s aq_err %s on get_link_info\n",
14482 			i40e_stat_str(hw, err),
14483 			i40e_aq_str(hw, hw->aq.asq_last_status));
14484 
14485 	/* if FDIR VSI was set up, start it now */
14486 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14487 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
14488 			i40e_vsi_open(pf->vsi[i]);
14489 			break;
14490 		}
14491 	}
14492 
14493 	/* The driver only wants link up/down and module qualification
14494 	 * reports from firmware.  Note the negative logic.
14495 	 */
14496 	err = i40e_aq_set_phy_int_mask(&pf->hw,
14497 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
14498 					 I40E_AQ_EVENT_MEDIA_NA |
14499 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
14500 	if (err)
14501 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
14502 			 i40e_stat_str(&pf->hw, err),
14503 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14504 
14505 	/* Reconfigure hardware for allowing smaller MSS in the case
14506 	 * of TSO, so that we avoid the MDD being fired and causing
14507 	 * a reset in the case of small MSS+TSO.
14508 	 */
14509 	val = rd32(hw, I40E_REG_MSS);
14510 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
14511 		val &= ~I40E_REG_MSS_MIN_MASK;
14512 		val |= I40E_64BYTE_MSS;
14513 		wr32(hw, I40E_REG_MSS, val);
14514 	}
14515 
14516 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
14517 		msleep(75);
14518 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
14519 		if (err)
14520 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
14521 				 i40e_stat_str(&pf->hw, err),
14522 				 i40e_aq_str(&pf->hw,
14523 					     pf->hw.aq.asq_last_status));
14524 	}
14525 	/* The main driver is (mostly) up and happy. We need to set this state
14526 	 * before setting up the misc vector or we get a race and the vector
14527 	 * ends up disabled forever.
14528 	 */
14529 	clear_bit(__I40E_DOWN, pf->state);
14530 
14531 	/* In case of MSIX we are going to setup the misc vector right here
14532 	 * to handle admin queue events etc. In case of legacy and MSI
14533 	 * the misc functionality and queue processing is combined in
14534 	 * the same vector and that gets setup at open.
14535 	 */
14536 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
14537 		err = i40e_setup_misc_vector(pf);
14538 		if (err) {
14539 			dev_info(&pdev->dev,
14540 				 "setup of misc vector failed: %d\n", err);
14541 			goto err_vsis;
14542 		}
14543 	}
14544 
14545 #ifdef CONFIG_PCI_IOV
14546 	/* prep for VF support */
14547 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14548 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
14549 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
14550 		/* disable link interrupts for VFs */
14551 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
14552 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
14553 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
14554 		i40e_flush(hw);
14555 
14556 		if (pci_num_vf(pdev)) {
14557 			dev_info(&pdev->dev,
14558 				 "Active VFs found, allocating resources.\n");
14559 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
14560 			if (err)
14561 				dev_info(&pdev->dev,
14562 					 "Error %d allocating resources for existing VFs\n",
14563 					 err);
14564 		}
14565 	}
14566 #endif /* CONFIG_PCI_IOV */
14567 
14568 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14569 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
14570 						      pf->num_iwarp_msix,
14571 						      I40E_IWARP_IRQ_PILE_ID);
14572 		if (pf->iwarp_base_vector < 0) {
14573 			dev_info(&pdev->dev,
14574 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
14575 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
14576 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
14577 		}
14578 	}
14579 
14580 	i40e_dbg_pf_init(pf);
14581 
14582 	/* tell the firmware that we're starting */
14583 	i40e_send_version(pf);
14584 
14585 	/* since everything's happy, start the service_task timer */
14586 	mod_timer(&pf->service_timer,
14587 		  round_jiffies(jiffies + pf->service_timer_period));
14588 
14589 	/* add this PF to client device list and launch a client service task */
14590 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14591 		err = i40e_lan_add_device(pf);
14592 		if (err)
14593 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
14594 				 err);
14595 	}
14596 
14597 #define PCI_SPEED_SIZE 8
14598 #define PCI_WIDTH_SIZE 8
14599 	/* Devices on the IOSF bus do not have this information
14600 	 * and will report PCI Gen 1 x 1 by default so don't bother
14601 	 * checking them.
14602 	 */
14603 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
14604 		char speed[PCI_SPEED_SIZE] = "Unknown";
14605 		char width[PCI_WIDTH_SIZE] = "Unknown";
14606 
14607 		/* Get the negotiated link width and speed from PCI config
14608 		 * space
14609 		 */
14610 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
14611 					  &link_status);
14612 
14613 		i40e_set_pci_config_data(hw, link_status);
14614 
14615 		switch (hw->bus.speed) {
14616 		case i40e_bus_speed_8000:
14617 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
14618 		case i40e_bus_speed_5000:
14619 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
14620 		case i40e_bus_speed_2500:
14621 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
14622 		default:
14623 			break;
14624 		}
14625 		switch (hw->bus.width) {
14626 		case i40e_bus_width_pcie_x8:
14627 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
14628 		case i40e_bus_width_pcie_x4:
14629 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
14630 		case i40e_bus_width_pcie_x2:
14631 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
14632 		case i40e_bus_width_pcie_x1:
14633 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
14634 		default:
14635 			break;
14636 		}
14637 
14638 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
14639 			 speed, width);
14640 
14641 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
14642 		    hw->bus.speed < i40e_bus_speed_8000) {
14643 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
14644 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
14645 		}
14646 	}
14647 
14648 	/* get the requested speeds from the fw */
14649 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
14650 	if (err)
14651 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
14652 			i40e_stat_str(&pf->hw, err),
14653 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14654 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
14655 
14656 	/* set the FEC config due to the board capabilities */
14657 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
14658 
14659 	/* get the supported phy types from the fw */
14660 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
14661 	if (err)
14662 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
14663 			i40e_stat_str(&pf->hw, err),
14664 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14665 
14666 	/* Add a filter to drop all Flow control frames from any VSI from being
14667 	 * transmitted. By doing so we stop a malicious VF from sending out
14668 	 * PAUSE or PFC frames and potentially controlling traffic for other
14669 	 * PF/VF VSIs.
14670 	 * The FW can still send Flow control frames if enabled.
14671 	 */
14672 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
14673 						       pf->main_vsi_seid);
14674 
14675 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
14676 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
14677 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
14678 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
14679 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
14680 	/* print a string summarizing features */
14681 	i40e_print_features(pf);
14682 
14683 	return 0;
14684 
14685 	/* Unwind what we've done if something failed in the setup */
14686 err_vsis:
14687 	set_bit(__I40E_DOWN, pf->state);
14688 	i40e_clear_interrupt_scheme(pf);
14689 	kfree(pf->vsi);
14690 err_switch_setup:
14691 	i40e_reset_interrupt_capability(pf);
14692 	del_timer_sync(&pf->service_timer);
14693 err_mac_addr:
14694 err_configure_lan_hmc:
14695 	(void)i40e_shutdown_lan_hmc(hw);
14696 err_init_lan_hmc:
14697 	kfree(pf->qp_pile);
14698 err_sw_init:
14699 err_adminq_setup:
14700 err_pf_reset:
14701 	iounmap(hw->hw_addr);
14702 err_ioremap:
14703 	kfree(pf);
14704 err_pf_alloc:
14705 	pci_disable_pcie_error_reporting(pdev);
14706 	pci_release_mem_regions(pdev);
14707 err_pci_reg:
14708 err_dma:
14709 	pci_disable_device(pdev);
14710 	return err;
14711 }
14712 
14713 /**
14714  * i40e_remove - Device removal routine
14715  * @pdev: PCI device information struct
14716  *
14717  * i40e_remove is called by the PCI subsystem to alert the driver
14718  * that is should release a PCI device.  This could be caused by a
14719  * Hot-Plug event, or because the driver is going to be removed from
14720  * memory.
14721  **/
14722 static void i40e_remove(struct pci_dev *pdev)
14723 {
14724 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14725 	struct i40e_hw *hw = &pf->hw;
14726 	i40e_status ret_code;
14727 	int i;
14728 
14729 	i40e_dbg_pf_exit(pf);
14730 
14731 	i40e_ptp_stop(pf);
14732 
14733 	/* Disable RSS in hw */
14734 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
14735 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
14736 
14737 	/* no more scheduling of any task */
14738 	set_bit(__I40E_SUSPENDED, pf->state);
14739 	set_bit(__I40E_DOWN, pf->state);
14740 	if (pf->service_timer.function)
14741 		del_timer_sync(&pf->service_timer);
14742 	if (pf->service_task.func)
14743 		cancel_work_sync(&pf->service_task);
14744 
14745 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
14746 		struct i40e_vsi *vsi = pf->vsi[0];
14747 
14748 		/* We know that we have allocated only one vsi for this PF,
14749 		 * it was just for registering netdevice, so the interface
14750 		 * could be visible in the 'ifconfig' output
14751 		 */
14752 		unregister_netdev(vsi->netdev);
14753 		free_netdev(vsi->netdev);
14754 
14755 		goto unmap;
14756 	}
14757 
14758 	/* Client close must be called explicitly here because the timer
14759 	 * has been stopped.
14760 	 */
14761 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14762 
14763 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
14764 		i40e_free_vfs(pf);
14765 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
14766 	}
14767 
14768 	i40e_fdir_teardown(pf);
14769 
14770 	/* If there is a switch structure or any orphans, remove them.
14771 	 * This will leave only the PF's VSI remaining.
14772 	 */
14773 	for (i = 0; i < I40E_MAX_VEB; i++) {
14774 		if (!pf->veb[i])
14775 			continue;
14776 
14777 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
14778 		    pf->veb[i]->uplink_seid == 0)
14779 			i40e_switch_branch_release(pf->veb[i]);
14780 	}
14781 
14782 	/* Now we can shutdown the PF's VSI, just before we kill
14783 	 * adminq and hmc.
14784 	 */
14785 	if (pf->vsi[pf->lan_vsi])
14786 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
14787 
14788 	i40e_cloud_filter_exit(pf);
14789 
14790 	/* remove attached clients */
14791 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14792 		ret_code = i40e_lan_del_device(pf);
14793 		if (ret_code)
14794 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
14795 				 ret_code);
14796 	}
14797 
14798 	/* shutdown and destroy the HMC */
14799 	if (hw->hmc.hmc_obj) {
14800 		ret_code = i40e_shutdown_lan_hmc(hw);
14801 		if (ret_code)
14802 			dev_warn(&pdev->dev,
14803 				 "Failed to destroy the HMC resources: %d\n",
14804 				 ret_code);
14805 	}
14806 
14807 unmap:
14808 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
14809 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
14810 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
14811 		free_irq(pf->pdev->irq, pf);
14812 
14813 	/* shutdown the adminq */
14814 	i40e_shutdown_adminq(hw);
14815 
14816 	/* destroy the locks only once, here */
14817 	mutex_destroy(&hw->aq.arq_mutex);
14818 	mutex_destroy(&hw->aq.asq_mutex);
14819 
14820 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
14821 	rtnl_lock();
14822 	i40e_clear_interrupt_scheme(pf);
14823 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14824 		if (pf->vsi[i]) {
14825 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
14826 				i40e_vsi_clear_rings(pf->vsi[i]);
14827 			i40e_vsi_clear(pf->vsi[i]);
14828 			pf->vsi[i] = NULL;
14829 		}
14830 	}
14831 	rtnl_unlock();
14832 
14833 	for (i = 0; i < I40E_MAX_VEB; i++) {
14834 		kfree(pf->veb[i]);
14835 		pf->veb[i] = NULL;
14836 	}
14837 
14838 	kfree(pf->qp_pile);
14839 	kfree(pf->vsi);
14840 
14841 	iounmap(hw->hw_addr);
14842 	kfree(pf);
14843 	pci_release_mem_regions(pdev);
14844 
14845 	pci_disable_pcie_error_reporting(pdev);
14846 	pci_disable_device(pdev);
14847 }
14848 
14849 /**
14850  * i40e_pci_error_detected - warning that something funky happened in PCI land
14851  * @pdev: PCI device information struct
14852  * @error: the type of PCI error
14853  *
14854  * Called to warn that something happened and the error handling steps
14855  * are in progress.  Allows the driver to quiesce things, be ready for
14856  * remediation.
14857  **/
14858 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14859 						enum pci_channel_state error)
14860 {
14861 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14862 
14863 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14864 
14865 	if (!pf) {
14866 		dev_info(&pdev->dev,
14867 			 "Cannot recover - error happened during device probe\n");
14868 		return PCI_ERS_RESULT_DISCONNECT;
14869 	}
14870 
14871 	/* shutdown all operations */
14872 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14873 		i40e_prep_for_reset(pf, false);
14874 
14875 	/* Request a slot reset */
14876 	return PCI_ERS_RESULT_NEED_RESET;
14877 }
14878 
14879 /**
14880  * i40e_pci_error_slot_reset - a PCI slot reset just happened
14881  * @pdev: PCI device information struct
14882  *
14883  * Called to find if the driver can work with the device now that
14884  * the pci slot has been reset.  If a basic connection seems good
14885  * (registers are readable and have sane content) then return a
14886  * happy little PCI_ERS_RESULT_xxx.
14887  **/
14888 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14889 {
14890 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14891 	pci_ers_result_t result;
14892 	u32 reg;
14893 
14894 	dev_dbg(&pdev->dev, "%s\n", __func__);
14895 	if (pci_enable_device_mem(pdev)) {
14896 		dev_info(&pdev->dev,
14897 			 "Cannot re-enable PCI device after reset.\n");
14898 		result = PCI_ERS_RESULT_DISCONNECT;
14899 	} else {
14900 		pci_set_master(pdev);
14901 		pci_restore_state(pdev);
14902 		pci_save_state(pdev);
14903 		pci_wake_from_d3(pdev, false);
14904 
14905 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14906 		if (reg == 0)
14907 			result = PCI_ERS_RESULT_RECOVERED;
14908 		else
14909 			result = PCI_ERS_RESULT_DISCONNECT;
14910 	}
14911 
14912 	return result;
14913 }
14914 
14915 /**
14916  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14917  * @pdev: PCI device information struct
14918  */
14919 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14920 {
14921 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14922 
14923 	i40e_prep_for_reset(pf, false);
14924 }
14925 
14926 /**
14927  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14928  * @pdev: PCI device information struct
14929  */
14930 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14931 {
14932 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14933 
14934 	i40e_reset_and_rebuild(pf, false, false);
14935 }
14936 
14937 /**
14938  * i40e_pci_error_resume - restart operations after PCI error recovery
14939  * @pdev: PCI device information struct
14940  *
14941  * Called to allow the driver to bring things back up after PCI error
14942  * and/or reset recovery has finished.
14943  **/
14944 static void i40e_pci_error_resume(struct pci_dev *pdev)
14945 {
14946 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14947 
14948 	dev_dbg(&pdev->dev, "%s\n", __func__);
14949 	if (test_bit(__I40E_SUSPENDED, pf->state))
14950 		return;
14951 
14952 	i40e_handle_reset_warning(pf, false);
14953 }
14954 
14955 /**
14956  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14957  * using the mac_address_write admin q function
14958  * @pf: pointer to i40e_pf struct
14959  **/
14960 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14961 {
14962 	struct i40e_hw *hw = &pf->hw;
14963 	i40e_status ret;
14964 	u8 mac_addr[6];
14965 	u16 flags = 0;
14966 
14967 	/* Get current MAC address in case it's an LAA */
14968 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14969 		ether_addr_copy(mac_addr,
14970 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14971 	} else {
14972 		dev_err(&pf->pdev->dev,
14973 			"Failed to retrieve MAC address; using default\n");
14974 		ether_addr_copy(mac_addr, hw->mac.addr);
14975 	}
14976 
14977 	/* The FW expects the mac address write cmd to first be called with
14978 	 * one of these flags before calling it again with the multicast
14979 	 * enable flags.
14980 	 */
14981 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14982 
14983 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14984 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14985 
14986 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14987 	if (ret) {
14988 		dev_err(&pf->pdev->dev,
14989 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14990 		return;
14991 	}
14992 
14993 	flags = I40E_AQC_MC_MAG_EN
14994 			| I40E_AQC_WOL_PRESERVE_ON_PFR
14995 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14996 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14997 	if (ret)
14998 		dev_err(&pf->pdev->dev,
14999 			"Failed to enable Multicast Magic Packet wake up\n");
15000 }
15001 
15002 /**
15003  * i40e_shutdown - PCI callback for shutting down
15004  * @pdev: PCI device information struct
15005  **/
15006 static void i40e_shutdown(struct pci_dev *pdev)
15007 {
15008 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15009 	struct i40e_hw *hw = &pf->hw;
15010 
15011 	set_bit(__I40E_SUSPENDED, pf->state);
15012 	set_bit(__I40E_DOWN, pf->state);
15013 
15014 	del_timer_sync(&pf->service_timer);
15015 	cancel_work_sync(&pf->service_task);
15016 	i40e_cloud_filter_exit(pf);
15017 	i40e_fdir_teardown(pf);
15018 
15019 	/* Client close must be called explicitly here because the timer
15020 	 * has been stopped.
15021 	 */
15022 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15023 
15024 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15025 		i40e_enable_mc_magic_wake(pf);
15026 
15027 	i40e_prep_for_reset(pf, false);
15028 
15029 	wr32(hw, I40E_PFPM_APM,
15030 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15031 	wr32(hw, I40E_PFPM_WUFC,
15032 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15033 
15034 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15035 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15036 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15037 		free_irq(pf->pdev->irq, pf);
15038 
15039 	/* Since we're going to destroy queues during the
15040 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15041 	 * whole section
15042 	 */
15043 	rtnl_lock();
15044 	i40e_clear_interrupt_scheme(pf);
15045 	rtnl_unlock();
15046 
15047 	if (system_state == SYSTEM_POWER_OFF) {
15048 		pci_wake_from_d3(pdev, pf->wol_en);
15049 		pci_set_power_state(pdev, PCI_D3hot);
15050 	}
15051 }
15052 
15053 /**
15054  * i40e_suspend - PM callback for moving to D3
15055  * @dev: generic device information structure
15056  **/
15057 static int __maybe_unused i40e_suspend(struct device *dev)
15058 {
15059 	struct pci_dev *pdev = to_pci_dev(dev);
15060 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15061 	struct i40e_hw *hw = &pf->hw;
15062 
15063 	/* If we're already suspended, then there is nothing to do */
15064 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15065 		return 0;
15066 
15067 	set_bit(__I40E_DOWN, pf->state);
15068 
15069 	/* Ensure service task will not be running */
15070 	del_timer_sync(&pf->service_timer);
15071 	cancel_work_sync(&pf->service_task);
15072 
15073 	/* Client close must be called explicitly here because the timer
15074 	 * has been stopped.
15075 	 */
15076 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15077 
15078 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15079 		i40e_enable_mc_magic_wake(pf);
15080 
15081 	/* Since we're going to destroy queues during the
15082 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15083 	 * whole section
15084 	 */
15085 	rtnl_lock();
15086 
15087 	i40e_prep_for_reset(pf, true);
15088 
15089 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15090 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15091 
15092 	/* Clear the interrupt scheme and release our IRQs so that the system
15093 	 * can safely hibernate even when there are a large number of CPUs.
15094 	 * Otherwise hibernation might fail when mapping all the vectors back
15095 	 * to CPU0.
15096 	 */
15097 	i40e_clear_interrupt_scheme(pf);
15098 
15099 	rtnl_unlock();
15100 
15101 	return 0;
15102 }
15103 
15104 /**
15105  * i40e_resume - PM callback for waking up from D3
15106  * @dev: generic device information structure
15107  **/
15108 static int __maybe_unused i40e_resume(struct device *dev)
15109 {
15110 	struct pci_dev *pdev = to_pci_dev(dev);
15111 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15112 	int err;
15113 
15114 	/* If we're not suspended, then there is nothing to do */
15115 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15116 		return 0;
15117 
15118 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15119 	 * since we're going to be restoring queues
15120 	 */
15121 	rtnl_lock();
15122 
15123 	/* We cleared the interrupt scheme when we suspended, so we need to
15124 	 * restore it now to resume device functionality.
15125 	 */
15126 	err = i40e_restore_interrupt_scheme(pf);
15127 	if (err) {
15128 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
15129 			err);
15130 	}
15131 
15132 	clear_bit(__I40E_DOWN, pf->state);
15133 	i40e_reset_and_rebuild(pf, false, true);
15134 
15135 	rtnl_unlock();
15136 
15137 	/* Clear suspended state last after everything is recovered */
15138 	clear_bit(__I40E_SUSPENDED, pf->state);
15139 
15140 	/* Restart the service task */
15141 	mod_timer(&pf->service_timer,
15142 		  round_jiffies(jiffies + pf->service_timer_period));
15143 
15144 	return 0;
15145 }
15146 
15147 static const struct pci_error_handlers i40e_err_handler = {
15148 	.error_detected = i40e_pci_error_detected,
15149 	.slot_reset = i40e_pci_error_slot_reset,
15150 	.reset_prepare = i40e_pci_error_reset_prepare,
15151 	.reset_done = i40e_pci_error_reset_done,
15152 	.resume = i40e_pci_error_resume,
15153 };
15154 
15155 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15156 
15157 static struct pci_driver i40e_driver = {
15158 	.name     = i40e_driver_name,
15159 	.id_table = i40e_pci_tbl,
15160 	.probe    = i40e_probe,
15161 	.remove   = i40e_remove,
15162 	.driver   = {
15163 		.pm = &i40e_pm_ops,
15164 	},
15165 	.shutdown = i40e_shutdown,
15166 	.err_handler = &i40e_err_handler,
15167 	.sriov_configure = i40e_pci_sriov_configure,
15168 };
15169 
15170 /**
15171  * i40e_init_module - Driver registration routine
15172  *
15173  * i40e_init_module is the first routine called when the driver is
15174  * loaded. All it does is register with the PCI subsystem.
15175  **/
15176 static int __init i40e_init_module(void)
15177 {
15178 	pr_info("%s: %s - version %s\n", i40e_driver_name,
15179 		i40e_driver_string, i40e_driver_version_str);
15180 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15181 
15182 	/* There is no need to throttle the number of active tasks because
15183 	 * each device limits its own task using a state bit for scheduling
15184 	 * the service task, and the device tasks do not interfere with each
15185 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15186 	 * since we need to be able to guarantee forward progress even under
15187 	 * memory pressure.
15188 	 */
15189 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15190 	if (!i40e_wq) {
15191 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15192 		return -ENOMEM;
15193 	}
15194 
15195 	i40e_dbg_init();
15196 	return pci_register_driver(&i40e_driver);
15197 }
15198 module_init(i40e_init_module);
15199 
15200 /**
15201  * i40e_exit_module - Driver exit cleanup routine
15202  *
15203  * i40e_exit_module is called just before the driver is removed
15204  * from memory.
15205  **/
15206 static void __exit i40e_exit_module(void)
15207 {
15208 	pci_unregister_driver(&i40e_driver);
15209 	destroy_workqueue(i40e_wq);
15210 	i40e_dbg_exit();
15211 }
15212 module_exit(i40e_exit_module);
15213