xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 975f2d73a99f35b57ffa2ad7bff8562225cdcfcb)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/if_bridge.h>
7 #include <linux/if_macvlan.h>
8 #include <linux/module.h>
9 #include <net/pkt_cls.h>
10 #include <net/xdp_sock_drv.h>
11 
12 /* Local includes */
13 #include "i40e.h"
14 #include "i40e_devids.h"
15 #include "i40e_diag.h"
16 #include "i40e_lan_hmc.h"
17 #include "i40e_virtchnl_pf.h"
18 #include "i40e_xsk.h"
19 
20 /* All i40e tracepoints are defined by the include below, which
21  * must be included exactly once across the whole kernel with
22  * CREATE_TRACE_POINTS defined
23  */
24 #define CREATE_TRACE_POINTS
25 #include "i40e_trace.h"
26 
27 const char i40e_driver_name[] = "i40e";
28 static const char i40e_driver_string[] =
29 			"Intel(R) Ethernet Connection XL710 Network Driver";
30 
31 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
32 
33 /* a bit of forward declarations */
34 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
35 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
36 static int i40e_add_vsi(struct i40e_vsi *vsi);
37 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
38 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
39 static int i40e_setup_misc_vector(struct i40e_pf *pf);
40 static void i40e_determine_queue_usage(struct i40e_pf *pf);
41 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
42 static void i40e_prep_for_reset(struct i40e_pf *pf);
43 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
44 				   bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
48 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
49 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
50 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
51 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
52 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
53 static int i40e_get_capabilities(struct i40e_pf *pf,
54 				 enum i40e_admin_queue_opc list_type);
55 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
56 
57 /* i40e_pci_tbl - PCI Device ID Table
58  *
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
64 static const struct pci_device_id i40e_pci_tbl[] = {
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 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_SFP_X722_A), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
91 	/* required last entry */
92 	{0, }
93 };
94 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95 
96 #define I40E_MAX_VF_COUNT 128
97 static int debug = -1;
98 module_param(debug, uint, 0);
99 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
100 
101 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_LICENSE("GPL v2");
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
108 				  struct net_device *netdev, int delta)
109 {
110 	struct netdev_hw_addr *ha;
111 
112 	if (!f || !netdev)
113 		return;
114 
115 	netdev_for_each_mc_addr(ha, netdev) {
116 		if (ether_addr_equal(ha->addr, f->macaddr)) {
117 			ha->refcount += delta;
118 			if (ha->refcount <= 0)
119 				ha->refcount = 1;
120 			break;
121 		}
122 	}
123 }
124 
125 /**
126  * i40e_hw_to_dev - get device pointer from the hardware structure
127  * @hw: pointer to the device HW structure
128  **/
129 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
130 {
131 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
132 
133 	return &pf->pdev->dev;
134 }
135 
136 /**
137  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
138  * @hw:   pointer to the HW structure
139  * @mem:  ptr to mem struct to fill out
140  * @size: size of memory requested
141  * @alignment: what to align the allocation to
142  **/
143 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
144 			  u64 size, u32 alignment)
145 {
146 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
147 
148 	mem->size = ALIGN(size, alignment);
149 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
150 				     GFP_KERNEL);
151 	if (!mem->va)
152 		return -ENOMEM;
153 
154 	return 0;
155 }
156 
157 /**
158  * i40e_free_dma_mem - OS specific memory free for shared code
159  * @hw:   pointer to the HW structure
160  * @mem:  ptr to mem struct to free
161  **/
162 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
163 {
164 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
165 
166 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
167 	mem->va = NULL;
168 	mem->pa = 0;
169 	mem->size = 0;
170 
171 	return 0;
172 }
173 
174 /**
175  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
176  * @hw:   pointer to the HW structure
177  * @mem:  ptr to mem struct to fill out
178  * @size: size of memory requested
179  **/
180 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
181 			   u32 size)
182 {
183 	mem->size = size;
184 	mem->va = kzalloc(size, GFP_KERNEL);
185 
186 	if (!mem->va)
187 		return -ENOMEM;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_free_virt_mem - OS specific memory free for shared code
194  * @hw:   pointer to the HW structure
195  * @mem:  ptr to mem struct to free
196  **/
197 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
198 {
199 	/* it's ok to kfree a NULL pointer */
200 	kfree(mem->va);
201 	mem->va = NULL;
202 	mem->size = 0;
203 
204 	return 0;
205 }
206 
207 /**
208  * i40e_get_lump - find a lump of free generic resource
209  * @pf: board private structure
210  * @pile: the pile of resource to search
211  * @needed: the number of items needed
212  * @id: an owner id to stick on the items assigned
213  *
214  * Returns the base item index of the lump, or negative for error
215  **/
216 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
217 			 u16 needed, u16 id)
218 {
219 	int ret = -ENOMEM;
220 	int i, j;
221 
222 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
223 		dev_info(&pf->pdev->dev,
224 			 "param err: pile=%s needed=%d id=0x%04x\n",
225 			 pile ? "<valid>" : "<null>", needed, id);
226 		return -EINVAL;
227 	}
228 
229 	/* Allocate last queue in the pile for FDIR VSI queue
230 	 * so it doesn't fragment the qp_pile
231 	 */
232 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
233 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
234 			dev_err(&pf->pdev->dev,
235 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
236 				pile->num_entries - 1);
237 			return -ENOMEM;
238 		}
239 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
240 		return pile->num_entries - 1;
241 	}
242 
243 	i = 0;
244 	while (i < pile->num_entries) {
245 		/* skip already allocated entries */
246 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
247 			i++;
248 			continue;
249 		}
250 
251 		/* do we have enough in this lump? */
252 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
253 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
254 				break;
255 		}
256 
257 		if (j == needed) {
258 			/* there was enough, so assign it to the requestor */
259 			for (j = 0; j < needed; j++)
260 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
261 			ret = i;
262 			break;
263 		}
264 
265 		/* not enough, so skip over it and continue looking */
266 		i += j;
267 	}
268 
269 	return ret;
270 }
271 
272 /**
273  * i40e_put_lump - return a lump of generic resource
274  * @pile: the pile of resource to search
275  * @index: the base item index
276  * @id: the owner id of the items assigned
277  *
278  * Returns the count of items in the lump
279  **/
280 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
281 {
282 	int valid_id = (id | I40E_PILE_VALID_BIT);
283 	int count = 0;
284 	u16 i;
285 
286 	if (!pile || index >= pile->num_entries)
287 		return -EINVAL;
288 
289 	for (i = index;
290 	     i < pile->num_entries && pile->list[i] == valid_id;
291 	     i++) {
292 		pile->list[i] = 0;
293 		count++;
294 	}
295 
296 
297 	return count;
298 }
299 
300 /**
301  * i40e_find_vsi_from_id - searches for the vsi with the given id
302  * @pf: the pf structure to search for the vsi
303  * @id: id of the vsi it is searching for
304  **/
305 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
306 {
307 	int i;
308 
309 	for (i = 0; i < pf->num_alloc_vsi; i++)
310 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
311 			return pf->vsi[i];
312 
313 	return NULL;
314 }
315 
316 /**
317  * i40e_service_event_schedule - Schedule the service task to wake up
318  * @pf: board private structure
319  *
320  * If not already scheduled, this puts the task into the work queue
321  **/
322 void i40e_service_event_schedule(struct i40e_pf *pf)
323 {
324 	if ((!test_bit(__I40E_DOWN, pf->state) &&
325 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
326 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
327 		queue_work(i40e_wq, &pf->service_task);
328 }
329 
330 /**
331  * i40e_tx_timeout - Respond to a Tx Hang
332  * @netdev: network interface device structure
333  * @txqueue: queue number timing out
334  *
335  * If any port has noticed a Tx timeout, it is likely that the whole
336  * device is munged, not just the one netdev port, so go for the full
337  * reset.
338  **/
339 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
340 {
341 	struct i40e_netdev_priv *np = netdev_priv(netdev);
342 	struct i40e_vsi *vsi = np->vsi;
343 	struct i40e_pf *pf = vsi->back;
344 	struct i40e_ring *tx_ring = NULL;
345 	unsigned int i;
346 	u32 head, val;
347 
348 	pf->tx_timeout_count++;
349 
350 	/* with txqueue index, find the tx_ring struct */
351 	for (i = 0; i < vsi->num_queue_pairs; i++) {
352 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
353 			if (txqueue ==
354 			    vsi->tx_rings[i]->queue_index) {
355 				tx_ring = vsi->tx_rings[i];
356 				break;
357 			}
358 		}
359 	}
360 
361 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
362 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
363 	else if (time_before(jiffies,
364 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
365 		return;   /* don't do any new action before the next timeout */
366 
367 	/* don't kick off another recovery if one is already pending */
368 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
369 		return;
370 
371 	if (tx_ring) {
372 		head = i40e_get_head(tx_ring);
373 		/* Read interrupt register */
374 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
375 			val = rd32(&pf->hw,
376 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
377 						tx_ring->vsi->base_vector - 1));
378 		else
379 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
380 
381 		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",
382 			    vsi->seid, txqueue, tx_ring->next_to_clean,
383 			    head, tx_ring->next_to_use,
384 			    readl(tx_ring->tail), val);
385 	}
386 
387 	pf->tx_timeout_last_recovery = jiffies;
388 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
389 		    pf->tx_timeout_recovery_level, txqueue);
390 
391 	switch (pf->tx_timeout_recovery_level) {
392 	case 1:
393 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
394 		break;
395 	case 2:
396 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
397 		break;
398 	case 3:
399 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
400 		break;
401 	default:
402 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
403 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
404 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
405 		break;
406 	}
407 
408 	i40e_service_event_schedule(pf);
409 	pf->tx_timeout_recovery_level++;
410 }
411 
412 /**
413  * i40e_get_vsi_stats_struct - Get System Network Statistics
414  * @vsi: the VSI we care about
415  *
416  * Returns the address of the device statistics structure.
417  * The statistics are actually updated from the service task.
418  **/
419 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
420 {
421 	return &vsi->net_stats;
422 }
423 
424 /**
425  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
426  * @ring: Tx ring to get statistics from
427  * @stats: statistics entry to be updated
428  **/
429 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
430 					    struct rtnl_link_stats64 *stats)
431 {
432 	u64 bytes, packets;
433 	unsigned int start;
434 
435 	do {
436 		start = u64_stats_fetch_begin(&ring->syncp);
437 		packets = ring->stats.packets;
438 		bytes   = ring->stats.bytes;
439 	} while (u64_stats_fetch_retry(&ring->syncp, start));
440 
441 	stats->tx_packets += packets;
442 	stats->tx_bytes   += bytes;
443 }
444 
445 /**
446  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
447  * @netdev: network interface device structure
448  * @stats: data structure to store statistics
449  *
450  * Returns the address of the device statistics structure.
451  * The statistics are actually updated from the service task.
452  **/
453 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
454 				  struct rtnl_link_stats64 *stats)
455 {
456 	struct i40e_netdev_priv *np = netdev_priv(netdev);
457 	struct i40e_vsi *vsi = np->vsi;
458 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
459 	struct i40e_ring *ring;
460 	int i;
461 
462 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
463 		return;
464 
465 	if (!vsi->tx_rings)
466 		return;
467 
468 	rcu_read_lock();
469 	for (i = 0; i < vsi->num_queue_pairs; i++) {
470 		u64 bytes, packets;
471 		unsigned int start;
472 
473 		ring = READ_ONCE(vsi->tx_rings[i]);
474 		if (!ring)
475 			continue;
476 		i40e_get_netdev_stats_struct_tx(ring, stats);
477 
478 		if (i40e_enabled_xdp_vsi(vsi)) {
479 			ring = READ_ONCE(vsi->xdp_rings[i]);
480 			if (!ring)
481 				continue;
482 			i40e_get_netdev_stats_struct_tx(ring, stats);
483 		}
484 
485 		ring = READ_ONCE(vsi->rx_rings[i]);
486 		if (!ring)
487 			continue;
488 		do {
489 			start   = u64_stats_fetch_begin(&ring->syncp);
490 			packets = ring->stats.packets;
491 			bytes   = ring->stats.bytes;
492 		} while (u64_stats_fetch_retry(&ring->syncp, start));
493 
494 		stats->rx_packets += packets;
495 		stats->rx_bytes   += bytes;
496 
497 	}
498 	rcu_read_unlock();
499 
500 	/* following stats updated by i40e_watchdog_subtask() */
501 	stats->multicast	= vsi_stats->multicast;
502 	stats->tx_errors	= vsi_stats->tx_errors;
503 	stats->tx_dropped	= vsi_stats->tx_dropped;
504 	stats->rx_errors	= vsi_stats->rx_errors;
505 	stats->rx_dropped	= vsi_stats->rx_dropped;
506 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
507 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
508 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
509 }
510 
511 /**
512  * i40e_vsi_reset_stats - Resets all stats of the given vsi
513  * @vsi: the VSI to have its stats reset
514  **/
515 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
516 {
517 	struct rtnl_link_stats64 *ns;
518 	int i;
519 
520 	if (!vsi)
521 		return;
522 
523 	ns = i40e_get_vsi_stats_struct(vsi);
524 	memset(ns, 0, sizeof(*ns));
525 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
526 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
527 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
528 	if (vsi->rx_rings && vsi->rx_rings[0]) {
529 		for (i = 0; i < vsi->num_queue_pairs; i++) {
530 			memset(&vsi->rx_rings[i]->stats, 0,
531 			       sizeof(vsi->rx_rings[i]->stats));
532 			memset(&vsi->rx_rings[i]->rx_stats, 0,
533 			       sizeof(vsi->rx_rings[i]->rx_stats));
534 			memset(&vsi->tx_rings[i]->stats, 0,
535 			       sizeof(vsi->tx_rings[i]->stats));
536 			memset(&vsi->tx_rings[i]->tx_stats, 0,
537 			       sizeof(vsi->tx_rings[i]->tx_stats));
538 		}
539 	}
540 	vsi->stat_offsets_loaded = false;
541 }
542 
543 /**
544  * i40e_pf_reset_stats - Reset all of the stats for the given PF
545  * @pf: the PF to be reset
546  **/
547 void i40e_pf_reset_stats(struct i40e_pf *pf)
548 {
549 	int i;
550 
551 	memset(&pf->stats, 0, sizeof(pf->stats));
552 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
553 	pf->stat_offsets_loaded = false;
554 
555 	for (i = 0; i < I40E_MAX_VEB; i++) {
556 		if (pf->veb[i]) {
557 			memset(&pf->veb[i]->stats, 0,
558 			       sizeof(pf->veb[i]->stats));
559 			memset(&pf->veb[i]->stats_offsets, 0,
560 			       sizeof(pf->veb[i]->stats_offsets));
561 			memset(&pf->veb[i]->tc_stats, 0,
562 			       sizeof(pf->veb[i]->tc_stats));
563 			memset(&pf->veb[i]->tc_stats_offsets, 0,
564 			       sizeof(pf->veb[i]->tc_stats_offsets));
565 			pf->veb[i]->stat_offsets_loaded = false;
566 		}
567 	}
568 	pf->hw_csum_rx_error = 0;
569 }
570 
571 /**
572  * i40e_compute_pci_to_hw_id - compute index form PCI function.
573  * @vsi: ptr to the VSI to read from.
574  * @hw: ptr to the hardware info.
575  **/
576 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
577 {
578 	int pf_count = i40e_get_pf_count(hw);
579 
580 	if (vsi->type == I40E_VSI_SRIOV)
581 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
582 
583 	return hw->port + BIT(7);
584 }
585 
586 /**
587  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
588  * @hw: ptr to the hardware info.
589  * @hireg: the high 32 bit reg to read.
590  * @loreg: the low 32 bit reg to read.
591  * @offset_loaded: has the initial offset been loaded yet.
592  * @offset: ptr to current offset value.
593  * @stat: ptr to the stat.
594  *
595  * Since the device stats are not reset at PFReset, they will not
596  * be zeroed when the driver starts.  We'll save the first values read
597  * and use them as offsets to be subtracted from the raw values in order
598  * to report stats that count from zero.
599  **/
600 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
601 			       bool offset_loaded, u64 *offset, u64 *stat)
602 {
603 	u64 new_data;
604 
605 	new_data = rd64(hw, loreg);
606 
607 	if (!offset_loaded || new_data < *offset)
608 		*offset = new_data;
609 	*stat = new_data - *offset;
610 }
611 
612 /**
613  * i40e_stat_update48 - read and update a 48 bit stat from the chip
614  * @hw: ptr to the hardware info
615  * @hireg: the high 32 bit reg to read
616  * @loreg: the low 32 bit reg to read
617  * @offset_loaded: has the initial offset been loaded yet
618  * @offset: ptr to current offset value
619  * @stat: ptr to the stat
620  *
621  * Since the device stats are not reset at PFReset, they likely will not
622  * be zeroed when the driver starts.  We'll save the first values read
623  * and use them as offsets to be subtracted from the raw values in order
624  * to report stats that count from zero.  In the process, we also manage
625  * the potential roll-over.
626  **/
627 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
628 			       bool offset_loaded, u64 *offset, u64 *stat)
629 {
630 	u64 new_data;
631 
632 	if (hw->device_id == I40E_DEV_ID_QEMU) {
633 		new_data = rd32(hw, loreg);
634 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
635 	} else {
636 		new_data = rd64(hw, loreg);
637 	}
638 	if (!offset_loaded)
639 		*offset = new_data;
640 	if (likely(new_data >= *offset))
641 		*stat = new_data - *offset;
642 	else
643 		*stat = (new_data + BIT_ULL(48)) - *offset;
644 	*stat &= 0xFFFFFFFFFFFFULL;
645 }
646 
647 /**
648  * i40e_stat_update32 - read and update a 32 bit stat from the chip
649  * @hw: ptr to the hardware info
650  * @reg: the hw reg to read
651  * @offset_loaded: has the initial offset been loaded yet
652  * @offset: ptr to current offset value
653  * @stat: ptr to the stat
654  **/
655 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
656 			       bool offset_loaded, u64 *offset, u64 *stat)
657 {
658 	u32 new_data;
659 
660 	new_data = rd32(hw, reg);
661 	if (!offset_loaded)
662 		*offset = new_data;
663 	if (likely(new_data >= *offset))
664 		*stat = (u32)(new_data - *offset);
665 	else
666 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
667 }
668 
669 /**
670  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
671  * @hw: ptr to the hardware info
672  * @reg: the hw reg to read and clear
673  * @stat: ptr to the stat
674  **/
675 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
676 {
677 	u32 new_data = rd32(hw, reg);
678 
679 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
680 	*stat += new_data;
681 }
682 
683 /**
684  * i40e_stats_update_rx_discards - update rx_discards.
685  * @vsi: ptr to the VSI to be updated.
686  * @hw: ptr to the hardware info.
687  * @stat_idx: VSI's stat_counter_idx.
688  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
689  * @stat_offset: ptr to stat_offset to store first read of specific register.
690  * @stat: ptr to VSI's stat to be updated.
691  **/
692 static void
693 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
694 			      int stat_idx, bool offset_loaded,
695 			      struct i40e_eth_stats *stat_offset,
696 			      struct i40e_eth_stats *stat)
697 {
698 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
699 			   &stat_offset->rx_discards, &stat->rx_discards);
700 	i40e_stat_update64(hw,
701 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
702 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
703 			   offset_loaded, &stat_offset->rx_discards_other,
704 			   &stat->rx_discards_other);
705 }
706 
707 /**
708  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
709  * @vsi: the VSI to be updated
710  **/
711 void i40e_update_eth_stats(struct i40e_vsi *vsi)
712 {
713 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
714 	struct i40e_pf *pf = vsi->back;
715 	struct i40e_hw *hw = &pf->hw;
716 	struct i40e_eth_stats *oes;
717 	struct i40e_eth_stats *es;     /* device's eth stats */
718 
719 	es = &vsi->eth_stats;
720 	oes = &vsi->eth_stats_offsets;
721 
722 	/* Gather up the stats that the hw collects */
723 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
724 			   vsi->stat_offsets_loaded,
725 			   &oes->tx_errors, &es->tx_errors);
726 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
727 			   vsi->stat_offsets_loaded,
728 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
729 
730 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
731 			   I40E_GLV_GORCL(stat_idx),
732 			   vsi->stat_offsets_loaded,
733 			   &oes->rx_bytes, &es->rx_bytes);
734 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
735 			   I40E_GLV_UPRCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_unicast, &es->rx_unicast);
738 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
739 			   I40E_GLV_MPRCL(stat_idx),
740 			   vsi->stat_offsets_loaded,
741 			   &oes->rx_multicast, &es->rx_multicast);
742 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
743 			   I40E_GLV_BPRCL(stat_idx),
744 			   vsi->stat_offsets_loaded,
745 			   &oes->rx_broadcast, &es->rx_broadcast);
746 
747 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
748 			   I40E_GLV_GOTCL(stat_idx),
749 			   vsi->stat_offsets_loaded,
750 			   &oes->tx_bytes, &es->tx_bytes);
751 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
752 			   I40E_GLV_UPTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_unicast, &es->tx_unicast);
755 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
756 			   I40E_GLV_MPTCL(stat_idx),
757 			   vsi->stat_offsets_loaded,
758 			   &oes->tx_multicast, &es->tx_multicast);
759 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
760 			   I40E_GLV_BPTCL(stat_idx),
761 			   vsi->stat_offsets_loaded,
762 			   &oes->tx_broadcast, &es->tx_broadcast);
763 
764 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
765 				      vsi->stat_offsets_loaded, oes, es);
766 
767 	vsi->stat_offsets_loaded = true;
768 }
769 
770 /**
771  * i40e_update_veb_stats - Update Switch component statistics
772  * @veb: the VEB being updated
773  **/
774 void i40e_update_veb_stats(struct i40e_veb *veb)
775 {
776 	struct i40e_pf *pf = veb->pf;
777 	struct i40e_hw *hw = &pf->hw;
778 	struct i40e_eth_stats *oes;
779 	struct i40e_eth_stats *es;     /* device's eth stats */
780 	struct i40e_veb_tc_stats *veb_oes;
781 	struct i40e_veb_tc_stats *veb_es;
782 	int i, idx = 0;
783 
784 	idx = veb->stats_idx;
785 	es = &veb->stats;
786 	oes = &veb->stats_offsets;
787 	veb_es = &veb->tc_stats;
788 	veb_oes = &veb->tc_stats_offsets;
789 
790 	/* Gather up the stats that the hw collects */
791 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->tx_discards, &es->tx_discards);
794 	if (hw->revision_id > 0)
795 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
796 				   veb->stat_offsets_loaded,
797 				   &oes->rx_unknown_protocol,
798 				   &es->rx_unknown_protocol);
799 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
800 			   veb->stat_offsets_loaded,
801 			   &oes->rx_bytes, &es->rx_bytes);
802 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
803 			   veb->stat_offsets_loaded,
804 			   &oes->rx_unicast, &es->rx_unicast);
805 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
806 			   veb->stat_offsets_loaded,
807 			   &oes->rx_multicast, &es->rx_multicast);
808 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
809 			   veb->stat_offsets_loaded,
810 			   &oes->rx_broadcast, &es->rx_broadcast);
811 
812 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
813 			   veb->stat_offsets_loaded,
814 			   &oes->tx_bytes, &es->tx_bytes);
815 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
816 			   veb->stat_offsets_loaded,
817 			   &oes->tx_unicast, &es->tx_unicast);
818 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
819 			   veb->stat_offsets_loaded,
820 			   &oes->tx_multicast, &es->tx_multicast);
821 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
822 			   veb->stat_offsets_loaded,
823 			   &oes->tx_broadcast, &es->tx_broadcast);
824 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
825 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
826 				   I40E_GLVEBTC_RPCL(i, idx),
827 				   veb->stat_offsets_loaded,
828 				   &veb_oes->tc_rx_packets[i],
829 				   &veb_es->tc_rx_packets[i]);
830 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
831 				   I40E_GLVEBTC_RBCL(i, idx),
832 				   veb->stat_offsets_loaded,
833 				   &veb_oes->tc_rx_bytes[i],
834 				   &veb_es->tc_rx_bytes[i]);
835 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
836 				   I40E_GLVEBTC_TPCL(i, idx),
837 				   veb->stat_offsets_loaded,
838 				   &veb_oes->tc_tx_packets[i],
839 				   &veb_es->tc_tx_packets[i]);
840 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
841 				   I40E_GLVEBTC_TBCL(i, idx),
842 				   veb->stat_offsets_loaded,
843 				   &veb_oes->tc_tx_bytes[i],
844 				   &veb_es->tc_tx_bytes[i]);
845 	}
846 	veb->stat_offsets_loaded = true;
847 }
848 
849 /**
850  * i40e_update_vsi_stats - Update the vsi statistics counters.
851  * @vsi: the VSI to be updated
852  *
853  * There are a few instances where we store the same stat in a
854  * couple of different structs.  This is partly because we have
855  * the netdev stats that need to be filled out, which is slightly
856  * different from the "eth_stats" defined by the chip and used in
857  * VF communications.  We sort it out here.
858  **/
859 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
860 {
861 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
862 	struct i40e_pf *pf = vsi->back;
863 	struct rtnl_link_stats64 *ons;
864 	struct rtnl_link_stats64 *ns;   /* netdev stats */
865 	struct i40e_eth_stats *oes;
866 	struct i40e_eth_stats *es;     /* device's eth stats */
867 	u64 tx_restart, tx_busy;
868 	struct i40e_ring *p;
869 	u64 bytes, packets;
870 	unsigned int start;
871 	u64 tx_linearize;
872 	u64 tx_force_wb;
873 	u64 tx_stopped;
874 	u64 rx_p, rx_b;
875 	u64 tx_p, tx_b;
876 	u16 q;
877 
878 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
879 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
880 		return;
881 
882 	ns = i40e_get_vsi_stats_struct(vsi);
883 	ons = &vsi->net_stats_offsets;
884 	es = &vsi->eth_stats;
885 	oes = &vsi->eth_stats_offsets;
886 
887 	/* Gather up the netdev and vsi stats that the driver collects
888 	 * on the fly during packet processing
889 	 */
890 	rx_b = rx_p = 0;
891 	tx_b = tx_p = 0;
892 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
893 	tx_stopped = 0;
894 	rx_page = 0;
895 	rx_buf = 0;
896 	rx_reuse = 0;
897 	rx_alloc = 0;
898 	rx_waive = 0;
899 	rx_busy = 0;
900 	rcu_read_lock();
901 	for (q = 0; q < vsi->num_queue_pairs; q++) {
902 		/* locate Tx ring */
903 		p = READ_ONCE(vsi->tx_rings[q]);
904 		if (!p)
905 			continue;
906 
907 		do {
908 			start = u64_stats_fetch_begin(&p->syncp);
909 			packets = p->stats.packets;
910 			bytes = p->stats.bytes;
911 		} while (u64_stats_fetch_retry(&p->syncp, start));
912 		tx_b += bytes;
913 		tx_p += packets;
914 		tx_restart += p->tx_stats.restart_queue;
915 		tx_busy += p->tx_stats.tx_busy;
916 		tx_linearize += p->tx_stats.tx_linearize;
917 		tx_force_wb += p->tx_stats.tx_force_wb;
918 		tx_stopped += p->tx_stats.tx_stopped;
919 
920 		/* locate Rx ring */
921 		p = READ_ONCE(vsi->rx_rings[q]);
922 		if (!p)
923 			continue;
924 
925 		do {
926 			start = u64_stats_fetch_begin(&p->syncp);
927 			packets = p->stats.packets;
928 			bytes = p->stats.bytes;
929 		} while (u64_stats_fetch_retry(&p->syncp, start));
930 		rx_b += bytes;
931 		rx_p += packets;
932 		rx_buf += p->rx_stats.alloc_buff_failed;
933 		rx_page += p->rx_stats.alloc_page_failed;
934 		rx_reuse += p->rx_stats.page_reuse_count;
935 		rx_alloc += p->rx_stats.page_alloc_count;
936 		rx_waive += p->rx_stats.page_waive_count;
937 		rx_busy += p->rx_stats.page_busy_count;
938 
939 		if (i40e_enabled_xdp_vsi(vsi)) {
940 			/* locate XDP ring */
941 			p = READ_ONCE(vsi->xdp_rings[q]);
942 			if (!p)
943 				continue;
944 
945 			do {
946 				start = u64_stats_fetch_begin(&p->syncp);
947 				packets = p->stats.packets;
948 				bytes = p->stats.bytes;
949 			} while (u64_stats_fetch_retry(&p->syncp, start));
950 			tx_b += bytes;
951 			tx_p += packets;
952 			tx_restart += p->tx_stats.restart_queue;
953 			tx_busy += p->tx_stats.tx_busy;
954 			tx_linearize += p->tx_stats.tx_linearize;
955 			tx_force_wb += p->tx_stats.tx_force_wb;
956 		}
957 	}
958 	rcu_read_unlock();
959 	vsi->tx_restart = tx_restart;
960 	vsi->tx_busy = tx_busy;
961 	vsi->tx_linearize = tx_linearize;
962 	vsi->tx_force_wb = tx_force_wb;
963 	vsi->tx_stopped = tx_stopped;
964 	vsi->rx_page_failed = rx_page;
965 	vsi->rx_buf_failed = rx_buf;
966 	vsi->rx_page_reuse = rx_reuse;
967 	vsi->rx_page_alloc = rx_alloc;
968 	vsi->rx_page_waive = rx_waive;
969 	vsi->rx_page_busy = rx_busy;
970 
971 	ns->rx_packets = rx_p;
972 	ns->rx_bytes = rx_b;
973 	ns->tx_packets = tx_p;
974 	ns->tx_bytes = tx_b;
975 
976 	/* update netdev stats from eth stats */
977 	i40e_update_eth_stats(vsi);
978 	ons->tx_errors = oes->tx_errors;
979 	ns->tx_errors = es->tx_errors;
980 	ons->multicast = oes->rx_multicast;
981 	ns->multicast = es->rx_multicast;
982 	ons->rx_dropped = oes->rx_discards_other;
983 	ns->rx_dropped = es->rx_discards_other;
984 	ons->rx_missed_errors = oes->rx_discards;
985 	ns->rx_missed_errors = es->rx_discards;
986 	ons->tx_dropped = oes->tx_discards;
987 	ns->tx_dropped = es->tx_discards;
988 
989 	/* pull in a couple PF stats if this is the main vsi */
990 	if (vsi == pf->vsi[pf->lan_vsi]) {
991 		ns->rx_crc_errors = pf->stats.crc_errors;
992 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
993 		ns->rx_length_errors = pf->stats.rx_length_errors;
994 	}
995 }
996 
997 /**
998  * i40e_update_pf_stats - Update the PF statistics counters.
999  * @pf: the PF to be updated
1000  **/
1001 static void i40e_update_pf_stats(struct i40e_pf *pf)
1002 {
1003 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1004 	struct i40e_hw_port_stats *nsd = &pf->stats;
1005 	struct i40e_hw *hw = &pf->hw;
1006 	u32 val;
1007 	int i;
1008 
1009 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1010 			   I40E_GLPRT_GORCL(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1013 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1014 			   I40E_GLPRT_GOTCL(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1017 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->eth.rx_discards,
1020 			   &nsd->eth.rx_discards);
1021 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1022 			   I40E_GLPRT_UPRCL(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->eth.rx_unicast,
1025 			   &nsd->eth.rx_unicast);
1026 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1027 			   I40E_GLPRT_MPRCL(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.rx_multicast,
1030 			   &nsd->eth.rx_multicast);
1031 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1032 			   I40E_GLPRT_BPRCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.rx_broadcast,
1035 			   &nsd->eth.rx_broadcast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1037 			   I40E_GLPRT_UPTCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.tx_unicast,
1040 			   &nsd->eth.tx_unicast);
1041 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1042 			   I40E_GLPRT_MPTCL(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->eth.tx_multicast,
1045 			   &nsd->eth.tx_multicast);
1046 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1047 			   I40E_GLPRT_BPTCL(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->eth.tx_broadcast,
1050 			   &nsd->eth.tx_broadcast);
1051 
1052 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1053 			   pf->stat_offsets_loaded,
1054 			   &osd->tx_dropped_link_down,
1055 			   &nsd->tx_dropped_link_down);
1056 
1057 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1058 			   pf->stat_offsets_loaded,
1059 			   &osd->crc_errors, &nsd->crc_errors);
1060 
1061 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1064 
1065 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1066 			   pf->stat_offsets_loaded,
1067 			   &osd->mac_local_faults,
1068 			   &nsd->mac_local_faults);
1069 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->mac_remote_faults,
1072 			   &nsd->mac_remote_faults);
1073 
1074 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1075 			   pf->stat_offsets_loaded,
1076 			   &osd->rx_length_errors,
1077 			   &nsd->rx_length_errors);
1078 
1079 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1082 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1083 			   pf->stat_offsets_loaded,
1084 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1085 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1086 			   pf->stat_offsets_loaded,
1087 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1088 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1091 
1092 	for (i = 0; i < 8; i++) {
1093 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1094 				   pf->stat_offsets_loaded,
1095 				   &osd->priority_xoff_rx[i],
1096 				   &nsd->priority_xoff_rx[i]);
1097 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1098 				   pf->stat_offsets_loaded,
1099 				   &osd->priority_xon_rx[i],
1100 				   &nsd->priority_xon_rx[i]);
1101 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1102 				   pf->stat_offsets_loaded,
1103 				   &osd->priority_xon_tx[i],
1104 				   &nsd->priority_xon_tx[i]);
1105 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1106 				   pf->stat_offsets_loaded,
1107 				   &osd->priority_xoff_tx[i],
1108 				   &nsd->priority_xoff_tx[i]);
1109 		i40e_stat_update32(hw,
1110 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1111 				   pf->stat_offsets_loaded,
1112 				   &osd->priority_xon_2_xoff[i],
1113 				   &nsd->priority_xon_2_xoff[i]);
1114 	}
1115 
1116 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1117 			   I40E_GLPRT_PRC64L(hw->port),
1118 			   pf->stat_offsets_loaded,
1119 			   &osd->rx_size_64, &nsd->rx_size_64);
1120 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1121 			   I40E_GLPRT_PRC127L(hw->port),
1122 			   pf->stat_offsets_loaded,
1123 			   &osd->rx_size_127, &nsd->rx_size_127);
1124 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1125 			   I40E_GLPRT_PRC255L(hw->port),
1126 			   pf->stat_offsets_loaded,
1127 			   &osd->rx_size_255, &nsd->rx_size_255);
1128 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1129 			   I40E_GLPRT_PRC511L(hw->port),
1130 			   pf->stat_offsets_loaded,
1131 			   &osd->rx_size_511, &nsd->rx_size_511);
1132 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1133 			   I40E_GLPRT_PRC1023L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1137 			   I40E_GLPRT_PRC1522L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1140 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1141 			   I40E_GLPRT_PRC9522L(hw->port),
1142 			   pf->stat_offsets_loaded,
1143 			   &osd->rx_size_big, &nsd->rx_size_big);
1144 
1145 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1146 			   I40E_GLPRT_PTC64L(hw->port),
1147 			   pf->stat_offsets_loaded,
1148 			   &osd->tx_size_64, &nsd->tx_size_64);
1149 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1150 			   I40E_GLPRT_PTC127L(hw->port),
1151 			   pf->stat_offsets_loaded,
1152 			   &osd->tx_size_127, &nsd->tx_size_127);
1153 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1154 			   I40E_GLPRT_PTC255L(hw->port),
1155 			   pf->stat_offsets_loaded,
1156 			   &osd->tx_size_255, &nsd->tx_size_255);
1157 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1158 			   I40E_GLPRT_PTC511L(hw->port),
1159 			   pf->stat_offsets_loaded,
1160 			   &osd->tx_size_511, &nsd->tx_size_511);
1161 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1162 			   I40E_GLPRT_PTC1023L(hw->port),
1163 			   pf->stat_offsets_loaded,
1164 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1165 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1166 			   I40E_GLPRT_PTC1522L(hw->port),
1167 			   pf->stat_offsets_loaded,
1168 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1169 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1170 			   I40E_GLPRT_PTC9522L(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->tx_size_big, &nsd->tx_size_big);
1173 
1174 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1175 			   pf->stat_offsets_loaded,
1176 			   &osd->rx_undersize, &nsd->rx_undersize);
1177 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1178 			   pf->stat_offsets_loaded,
1179 			   &osd->rx_fragments, &nsd->rx_fragments);
1180 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->rx_oversize, &nsd->rx_oversize);
1183 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1184 			   pf->stat_offsets_loaded,
1185 			   &osd->rx_jabber, &nsd->rx_jabber);
1186 
1187 	/* FDIR stats */
1188 	i40e_stat_update_and_clear32(hw,
1189 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1190 			&nsd->fd_atr_match);
1191 	i40e_stat_update_and_clear32(hw,
1192 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1193 			&nsd->fd_sb_match);
1194 	i40e_stat_update_and_clear32(hw,
1195 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1196 			&nsd->fd_atr_tunnel_match);
1197 
1198 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1199 	nsd->tx_lpi_status =
1200 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1201 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1202 	nsd->rx_lpi_status =
1203 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1204 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1205 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1206 			   pf->stat_offsets_loaded,
1207 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1208 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1209 			   pf->stat_offsets_loaded,
1210 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1211 
1212 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1213 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1214 		nsd->fd_sb_status = true;
1215 	else
1216 		nsd->fd_sb_status = false;
1217 
1218 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1219 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1220 		nsd->fd_atr_status = true;
1221 	else
1222 		nsd->fd_atr_status = false;
1223 
1224 	pf->stat_offsets_loaded = true;
1225 }
1226 
1227 /**
1228  * i40e_update_stats - Update the various statistics counters.
1229  * @vsi: the VSI to be updated
1230  *
1231  * Update the various stats for this VSI and its related entities.
1232  **/
1233 void i40e_update_stats(struct i40e_vsi *vsi)
1234 {
1235 	struct i40e_pf *pf = vsi->back;
1236 
1237 	if (vsi == pf->vsi[pf->lan_vsi])
1238 		i40e_update_pf_stats(pf);
1239 
1240 	i40e_update_vsi_stats(vsi);
1241 }
1242 
1243 /**
1244  * i40e_count_filters - counts VSI mac filters
1245  * @vsi: the VSI to be searched
1246  *
1247  * Returns count of mac filters
1248  **/
1249 int i40e_count_filters(struct i40e_vsi *vsi)
1250 {
1251 	struct i40e_mac_filter *f;
1252 	struct hlist_node *h;
1253 	int bkt;
1254 	int cnt = 0;
1255 
1256 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1257 		++cnt;
1258 
1259 	return cnt;
1260 }
1261 
1262 /**
1263  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1264  * @vsi: the VSI to be searched
1265  * @macaddr: the MAC address
1266  * @vlan: the vlan
1267  *
1268  * Returns ptr to the filter object or NULL
1269  **/
1270 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1271 						const u8 *macaddr, s16 vlan)
1272 {
1273 	struct i40e_mac_filter *f;
1274 	u64 key;
1275 
1276 	if (!vsi || !macaddr)
1277 		return NULL;
1278 
1279 	key = i40e_addr_to_hkey(macaddr);
1280 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1281 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1282 		    (vlan == f->vlan))
1283 			return f;
1284 	}
1285 	return NULL;
1286 }
1287 
1288 /**
1289  * i40e_find_mac - Find a mac addr in the macvlan filters list
1290  * @vsi: the VSI to be searched
1291  * @macaddr: the MAC address we are searching for
1292  *
1293  * Returns the first filter with the provided MAC address or NULL if
1294  * MAC address was not found
1295  **/
1296 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1297 {
1298 	struct i40e_mac_filter *f;
1299 	u64 key;
1300 
1301 	if (!vsi || !macaddr)
1302 		return NULL;
1303 
1304 	key = i40e_addr_to_hkey(macaddr);
1305 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1306 		if ((ether_addr_equal(macaddr, f->macaddr)))
1307 			return f;
1308 	}
1309 	return NULL;
1310 }
1311 
1312 /**
1313  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1314  * @vsi: the VSI to be searched
1315  *
1316  * Returns true if VSI is in vlan mode or false otherwise
1317  **/
1318 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1319 {
1320 	/* If we have a PVID, always operate in VLAN mode */
1321 	if (vsi->info.pvid)
1322 		return true;
1323 
1324 	/* We need to operate in VLAN mode whenever we have any filters with
1325 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1326 	 * time, incurring search cost repeatedly. However, we can notice two
1327 	 * things:
1328 	 *
1329 	 * 1) the only place where we can gain a VLAN filter is in
1330 	 *    i40e_add_filter.
1331 	 *
1332 	 * 2) the only place where filters are actually removed is in
1333 	 *    i40e_sync_filters_subtask.
1334 	 *
1335 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1336 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1337 	 * we have to perform the full search after deleting filters in
1338 	 * i40e_sync_filters_subtask, but we already have to search
1339 	 * filters here and can perform the check at the same time. This
1340 	 * results in avoiding embedding a loop for VLAN mode inside another
1341 	 * loop over all the filters, and should maintain correctness as noted
1342 	 * above.
1343 	 */
1344 	return vsi->has_vlan_filter;
1345 }
1346 
1347 /**
1348  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1349  * @vsi: the VSI to configure
1350  * @tmp_add_list: list of filters ready to be added
1351  * @tmp_del_list: list of filters ready to be deleted
1352  * @vlan_filters: the number of active VLAN filters
1353  *
1354  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1355  * behave as expected. If we have any active VLAN filters remaining or about
1356  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1357  * so that they only match against untagged traffic. If we no longer have any
1358  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1359  * so that they match against both tagged and untagged traffic. In this way,
1360  * we ensure that we correctly receive the desired traffic. This ensures that
1361  * when we have an active VLAN we will receive only untagged traffic and
1362  * traffic matching active VLANs. If we have no active VLANs then we will
1363  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1364  *
1365  * Finally, in a similar fashion, this function also corrects filters when
1366  * there is an active PVID assigned to this VSI.
1367  *
1368  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1369  *
1370  * This function is only expected to be called from within
1371  * i40e_sync_vsi_filters.
1372  *
1373  * NOTE: This function expects to be called while under the
1374  * mac_filter_hash_lock
1375  */
1376 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1377 					 struct hlist_head *tmp_add_list,
1378 					 struct hlist_head *tmp_del_list,
1379 					 int vlan_filters)
1380 {
1381 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1382 	struct i40e_mac_filter *f, *add_head;
1383 	struct i40e_new_mac_filter *new;
1384 	struct hlist_node *h;
1385 	int bkt, new_vlan;
1386 
1387 	/* To determine if a particular filter needs to be replaced we
1388 	 * have the three following conditions:
1389 	 *
1390 	 * a) if we have a PVID assigned, then all filters which are
1391 	 *    not marked as VLAN=PVID must be replaced with filters that
1392 	 *    are.
1393 	 * b) otherwise, if we have any active VLANS, all filters
1394 	 *    which are marked as VLAN=-1 must be replaced with
1395 	 *    filters marked as VLAN=0
1396 	 * c) finally, if we do not have any active VLANS, all filters
1397 	 *    which are marked as VLAN=0 must be replaced with filters
1398 	 *    marked as VLAN=-1
1399 	 */
1400 
1401 	/* Update the filters about to be added in place */
1402 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1403 		if (pvid && new->f->vlan != pvid)
1404 			new->f->vlan = pvid;
1405 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1406 			new->f->vlan = 0;
1407 		else if (!vlan_filters && new->f->vlan == 0)
1408 			new->f->vlan = I40E_VLAN_ANY;
1409 	}
1410 
1411 	/* Update the remaining active filters */
1412 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1413 		/* Combine the checks for whether a filter needs to be changed
1414 		 * and then determine the new VLAN inside the if block, in
1415 		 * order to avoid duplicating code for adding the new filter
1416 		 * then deleting the old filter.
1417 		 */
1418 		if ((pvid && f->vlan != pvid) ||
1419 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1420 		    (!vlan_filters && f->vlan == 0)) {
1421 			/* Determine the new vlan we will be adding */
1422 			if (pvid)
1423 				new_vlan = pvid;
1424 			else if (vlan_filters)
1425 				new_vlan = 0;
1426 			else
1427 				new_vlan = I40E_VLAN_ANY;
1428 
1429 			/* Create the new filter */
1430 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1431 			if (!add_head)
1432 				return -ENOMEM;
1433 
1434 			/* Create a temporary i40e_new_mac_filter */
1435 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1436 			if (!new)
1437 				return -ENOMEM;
1438 
1439 			new->f = add_head;
1440 			new->state = add_head->state;
1441 
1442 			/* Add the new filter to the tmp list */
1443 			hlist_add_head(&new->hlist, tmp_add_list);
1444 
1445 			/* Put the original filter into the delete list */
1446 			f->state = I40E_FILTER_REMOVE;
1447 			hash_del(&f->hlist);
1448 			hlist_add_head(&f->hlist, tmp_del_list);
1449 		}
1450 	}
1451 
1452 	vsi->has_vlan_filter = !!vlan_filters;
1453 
1454 	return 0;
1455 }
1456 
1457 /**
1458  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1459  * @vsi: the vsi to configure
1460  * @new_mac: new mac filter to be added
1461  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1462  * @vlan_filters: the number of active VLAN filters
1463  * @trusted: flag if the VF is trusted
1464  *
1465  * Get new VLAN id based on current VLAN filters, trust, PVID
1466  * and vf-vlan-prune-disable flag.
1467  *
1468  * Returns the value of the new vlan filter or
1469  * the old value if no new filter is needed.
1470  */
1471 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1472 				struct i40e_new_mac_filter *new_mac,
1473 				struct i40e_mac_filter *f,
1474 				int vlan_filters,
1475 				bool trusted)
1476 {
1477 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1478 	struct i40e_pf *pf = vsi->back;
1479 	bool is_any;
1480 
1481 	if (new_mac)
1482 		f = new_mac->f;
1483 
1484 	if (pvid && f->vlan != pvid)
1485 		return pvid;
1486 
1487 	is_any = (trusted ||
1488 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1489 
1490 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1491 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1492 	    (is_any && !vlan_filters && f->vlan == 0)) {
1493 		if (is_any)
1494 			return I40E_VLAN_ANY;
1495 		else
1496 			return 0;
1497 	}
1498 
1499 	return f->vlan;
1500 }
1501 
1502 /**
1503  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1504  * @vsi: the vsi to configure
1505  * @tmp_add_list: list of filters ready to be added
1506  * @tmp_del_list: list of filters ready to be deleted
1507  * @vlan_filters: the number of active VLAN filters
1508  * @trusted: flag if the VF is trusted
1509  *
1510  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1511  * and vf-vlan-prune-disable flag.
1512  *
1513  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1514  *
1515  * This function is only expected to be called from within
1516  * i40e_sync_vsi_filters.
1517  *
1518  * NOTE: This function expects to be called while under the
1519  * mac_filter_hash_lock
1520  */
1521 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1522 					    struct hlist_head *tmp_add_list,
1523 					    struct hlist_head *tmp_del_list,
1524 					    int vlan_filters,
1525 					    bool trusted)
1526 {
1527 	struct i40e_mac_filter *f, *add_head;
1528 	struct i40e_new_mac_filter *new_mac;
1529 	struct hlist_node *h;
1530 	int bkt, new_vlan;
1531 
1532 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1533 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1534 							vlan_filters, trusted);
1535 	}
1536 
1537 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1538 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1539 						trusted);
1540 		if (new_vlan != f->vlan) {
1541 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1542 			if (!add_head)
1543 				return -ENOMEM;
1544 			/* Create a temporary i40e_new_mac_filter */
1545 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1546 			if (!new_mac)
1547 				return -ENOMEM;
1548 			new_mac->f = add_head;
1549 			new_mac->state = add_head->state;
1550 
1551 			/* Add the new filter to the tmp list */
1552 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1553 
1554 			/* Put the original filter into the delete list */
1555 			f->state = I40E_FILTER_REMOVE;
1556 			hash_del(&f->hlist);
1557 			hlist_add_head(&f->hlist, tmp_del_list);
1558 		}
1559 	}
1560 
1561 	vsi->has_vlan_filter = !!vlan_filters;
1562 	return 0;
1563 }
1564 
1565 /**
1566  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1567  * @vsi: the PF Main VSI - inappropriate for any other VSI
1568  * @macaddr: the MAC address
1569  *
1570  * Remove whatever filter the firmware set up so the driver can manage
1571  * its own filtering intelligently.
1572  **/
1573 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1574 {
1575 	struct i40e_aqc_remove_macvlan_element_data element;
1576 	struct i40e_pf *pf = vsi->back;
1577 
1578 	/* Only appropriate for the PF main VSI */
1579 	if (vsi->type != I40E_VSI_MAIN)
1580 		return;
1581 
1582 	memset(&element, 0, sizeof(element));
1583 	ether_addr_copy(element.mac_addr, macaddr);
1584 	element.vlan_tag = 0;
1585 	/* Ignore error returns, some firmware does it this way... */
1586 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1587 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1588 
1589 	memset(&element, 0, sizeof(element));
1590 	ether_addr_copy(element.mac_addr, macaddr);
1591 	element.vlan_tag = 0;
1592 	/* ...and some firmware does it this way. */
1593 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1594 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1595 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1596 }
1597 
1598 /**
1599  * i40e_add_filter - Add a mac/vlan filter to the VSI
1600  * @vsi: the VSI to be searched
1601  * @macaddr: the MAC address
1602  * @vlan: the vlan
1603  *
1604  * Returns ptr to the filter object or NULL when no memory available.
1605  *
1606  * NOTE: This function is expected to be called with mac_filter_hash_lock
1607  * being held.
1608  **/
1609 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1610 					const u8 *macaddr, s16 vlan)
1611 {
1612 	struct i40e_mac_filter *f;
1613 	u64 key;
1614 
1615 	if (!vsi || !macaddr)
1616 		return NULL;
1617 
1618 	f = i40e_find_filter(vsi, macaddr, vlan);
1619 	if (!f) {
1620 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1621 		if (!f)
1622 			return NULL;
1623 
1624 		/* Update the boolean indicating if we need to function in
1625 		 * VLAN mode.
1626 		 */
1627 		if (vlan >= 0)
1628 			vsi->has_vlan_filter = true;
1629 
1630 		ether_addr_copy(f->macaddr, macaddr);
1631 		f->vlan = vlan;
1632 		f->state = I40E_FILTER_NEW;
1633 		INIT_HLIST_NODE(&f->hlist);
1634 
1635 		key = i40e_addr_to_hkey(macaddr);
1636 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1637 
1638 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1639 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1640 	}
1641 
1642 	/* If we're asked to add a filter that has been marked for removal, it
1643 	 * is safe to simply restore it to active state. __i40e_del_filter
1644 	 * will have simply deleted any filters which were previously marked
1645 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1646 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1647 	 * task, just restore this filter to the ACTIVE state so that the
1648 	 * sync task leaves it in place
1649 	 */
1650 	if (f->state == I40E_FILTER_REMOVE)
1651 		f->state = I40E_FILTER_ACTIVE;
1652 
1653 	return f;
1654 }
1655 
1656 /**
1657  * __i40e_del_filter - Remove a specific filter from the VSI
1658  * @vsi: VSI to remove from
1659  * @f: the filter to remove from the list
1660  *
1661  * This function should be called instead of i40e_del_filter only if you know
1662  * the exact filter you will remove already, such as via i40e_find_filter or
1663  * i40e_find_mac.
1664  *
1665  * NOTE: This function is expected to be called with mac_filter_hash_lock
1666  * being held.
1667  * ANOTHER NOTE: This function MUST be called from within the context of
1668  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1669  * instead of list_for_each_entry().
1670  **/
1671 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1672 {
1673 	if (!f)
1674 		return;
1675 
1676 	/* If the filter was never added to firmware then we can just delete it
1677 	 * directly and we don't want to set the status to remove or else an
1678 	 * admin queue command will unnecessarily fire.
1679 	 */
1680 	if ((f->state == I40E_FILTER_FAILED) ||
1681 	    (f->state == I40E_FILTER_NEW)) {
1682 		hash_del(&f->hlist);
1683 		kfree(f);
1684 	} else {
1685 		f->state = I40E_FILTER_REMOVE;
1686 	}
1687 
1688 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1689 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1690 }
1691 
1692 /**
1693  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1694  * @vsi: the VSI to be searched
1695  * @macaddr: the MAC address
1696  * @vlan: the VLAN
1697  *
1698  * NOTE: This function is expected to be called with mac_filter_hash_lock
1699  * being held.
1700  * ANOTHER NOTE: This function MUST be called from within the context of
1701  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1702  * instead of list_for_each_entry().
1703  **/
1704 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1705 {
1706 	struct i40e_mac_filter *f;
1707 
1708 	if (!vsi || !macaddr)
1709 		return;
1710 
1711 	f = i40e_find_filter(vsi, macaddr, vlan);
1712 	__i40e_del_filter(vsi, f);
1713 }
1714 
1715 /**
1716  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1717  * @vsi: the VSI to be searched
1718  * @macaddr: the mac address to be filtered
1719  *
1720  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1721  * go through all the macvlan filters and add a macvlan filter for each
1722  * unique vlan that already exists. If a PVID has been assigned, instead only
1723  * add the macaddr to that VLAN.
1724  *
1725  * Returns last filter added on success, else NULL
1726  **/
1727 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1728 					    const u8 *macaddr)
1729 {
1730 	struct i40e_mac_filter *f, *add = NULL;
1731 	struct hlist_node *h;
1732 	int bkt;
1733 
1734 	if (vsi->info.pvid)
1735 		return i40e_add_filter(vsi, macaddr,
1736 				       le16_to_cpu(vsi->info.pvid));
1737 
1738 	if (!i40e_is_vsi_in_vlan(vsi))
1739 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1740 
1741 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1742 		if (f->state == I40E_FILTER_REMOVE)
1743 			continue;
1744 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1745 		if (!add)
1746 			return NULL;
1747 	}
1748 
1749 	return add;
1750 }
1751 
1752 /**
1753  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1754  * @vsi: the VSI to be searched
1755  * @macaddr: the mac address to be removed
1756  *
1757  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1758  * associated with.
1759  *
1760  * Returns 0 for success, or error
1761  **/
1762 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1763 {
1764 	struct i40e_mac_filter *f;
1765 	struct hlist_node *h;
1766 	bool found = false;
1767 	int bkt;
1768 
1769 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1770 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1771 		if (ether_addr_equal(macaddr, f->macaddr)) {
1772 			__i40e_del_filter(vsi, f);
1773 			found = true;
1774 		}
1775 	}
1776 
1777 	if (found)
1778 		return 0;
1779 	else
1780 		return -ENOENT;
1781 }
1782 
1783 /**
1784  * i40e_set_mac - NDO callback to set mac address
1785  * @netdev: network interface device structure
1786  * @p: pointer to an address structure
1787  *
1788  * Returns 0 on success, negative on failure
1789  **/
1790 static int i40e_set_mac(struct net_device *netdev, void *p)
1791 {
1792 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1793 	struct i40e_vsi *vsi = np->vsi;
1794 	struct i40e_pf *pf = vsi->back;
1795 	struct i40e_hw *hw = &pf->hw;
1796 	struct sockaddr *addr = p;
1797 
1798 	if (!is_valid_ether_addr(addr->sa_data))
1799 		return -EADDRNOTAVAIL;
1800 
1801 	if (test_bit(__I40E_DOWN, pf->state) ||
1802 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1803 		return -EADDRNOTAVAIL;
1804 
1805 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1806 		netdev_info(netdev, "returning to hw mac address %pM\n",
1807 			    hw->mac.addr);
1808 	else
1809 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1810 
1811 	/* Copy the address first, so that we avoid a possible race with
1812 	 * .set_rx_mode().
1813 	 * - Remove old address from MAC filter
1814 	 * - Copy new address
1815 	 * - Add new address to MAC filter
1816 	 */
1817 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1818 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1819 	eth_hw_addr_set(netdev, addr->sa_data);
1820 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1821 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1822 
1823 	if (vsi->type == I40E_VSI_MAIN) {
1824 		int ret;
1825 
1826 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1827 						addr->sa_data, NULL);
1828 		if (ret)
1829 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1830 				    ERR_PTR(ret),
1831 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1832 	}
1833 
1834 	/* schedule our worker thread which will take care of
1835 	 * applying the new filter changes
1836 	 */
1837 	i40e_service_event_schedule(pf);
1838 	return 0;
1839 }
1840 
1841 /**
1842  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1843  * @vsi: vsi structure
1844  * @seed: RSS hash seed
1845  * @lut: pointer to lookup table of lut_size
1846  * @lut_size: size of the lookup table
1847  **/
1848 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1849 			      u8 *lut, u16 lut_size)
1850 {
1851 	struct i40e_pf *pf = vsi->back;
1852 	struct i40e_hw *hw = &pf->hw;
1853 	int ret = 0;
1854 
1855 	if (seed) {
1856 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1857 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1858 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1859 		if (ret) {
1860 			dev_info(&pf->pdev->dev,
1861 				 "Cannot set RSS key, err %pe aq_err %s\n",
1862 				 ERR_PTR(ret),
1863 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1864 			return ret;
1865 		}
1866 	}
1867 	if (lut) {
1868 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1869 
1870 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1871 		if (ret) {
1872 			dev_info(&pf->pdev->dev,
1873 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1874 				 ERR_PTR(ret),
1875 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1876 			return ret;
1877 		}
1878 	}
1879 	return ret;
1880 }
1881 
1882 /**
1883  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1884  * @vsi: VSI structure
1885  **/
1886 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1887 {
1888 	struct i40e_pf *pf = vsi->back;
1889 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1890 	u8 *lut;
1891 	int ret;
1892 
1893 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1894 		return 0;
1895 	if (!vsi->rss_size)
1896 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1897 				      vsi->num_queue_pairs);
1898 	if (!vsi->rss_size)
1899 		return -EINVAL;
1900 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1901 	if (!lut)
1902 		return -ENOMEM;
1903 
1904 	/* Use the user configured hash keys and lookup table if there is one,
1905 	 * otherwise use default
1906 	 */
1907 	if (vsi->rss_lut_user)
1908 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1909 	else
1910 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1911 	if (vsi->rss_hkey_user)
1912 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1913 	else
1914 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1915 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1916 	kfree(lut);
1917 	return ret;
1918 }
1919 
1920 /**
1921  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1922  * @vsi: the VSI being configured,
1923  * @ctxt: VSI context structure
1924  * @enabled_tc: number of traffic classes to enable
1925  *
1926  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1927  **/
1928 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1929 					   struct i40e_vsi_context *ctxt,
1930 					   u8 enabled_tc)
1931 {
1932 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1933 	int i, override_q, pow, num_qps, ret;
1934 	u8 netdev_tc = 0, offset = 0;
1935 
1936 	if (vsi->type != I40E_VSI_MAIN)
1937 		return -EINVAL;
1938 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1939 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1940 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1941 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1942 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1943 
1944 	/* find the next higher power-of-2 of num queue pairs */
1945 	pow = ilog2(num_qps);
1946 	if (!is_power_of_2(num_qps))
1947 		pow++;
1948 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1949 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1950 
1951 	/* Setup queue offset/count for all TCs for given VSI */
1952 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1953 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1954 		/* See if the given TC is enabled for the given VSI */
1955 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1956 			offset = vsi->mqprio_qopt.qopt.offset[i];
1957 			qcount = vsi->mqprio_qopt.qopt.count[i];
1958 			if (qcount > max_qcount)
1959 				max_qcount = qcount;
1960 			vsi->tc_config.tc_info[i].qoffset = offset;
1961 			vsi->tc_config.tc_info[i].qcount = qcount;
1962 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1963 		} else {
1964 			/* TC is not enabled so set the offset to
1965 			 * default queue and allocate one queue
1966 			 * for the given TC.
1967 			 */
1968 			vsi->tc_config.tc_info[i].qoffset = 0;
1969 			vsi->tc_config.tc_info[i].qcount = 1;
1970 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1971 		}
1972 	}
1973 
1974 	/* Set actual Tx/Rx queue pairs */
1975 	vsi->num_queue_pairs = offset + qcount;
1976 
1977 	/* Setup queue TC[0].qmap for given VSI context */
1978 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1979 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1980 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1981 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1982 
1983 	/* Reconfigure RSS for main VSI with max queue count */
1984 	vsi->rss_size = max_qcount;
1985 	ret = i40e_vsi_config_rss(vsi);
1986 	if (ret) {
1987 		dev_info(&vsi->back->pdev->dev,
1988 			 "Failed to reconfig rss for num_queues (%u)\n",
1989 			 max_qcount);
1990 		return ret;
1991 	}
1992 	vsi->reconfig_rss = true;
1993 	dev_dbg(&vsi->back->pdev->dev,
1994 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1995 
1996 	/* Find queue count available for channel VSIs and starting offset
1997 	 * for channel VSIs
1998 	 */
1999 	override_q = vsi->mqprio_qopt.qopt.count[0];
2000 	if (override_q && override_q < vsi->num_queue_pairs) {
2001 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2002 		vsi->next_base_queue = override_q;
2003 	}
2004 	return 0;
2005 }
2006 
2007 /**
2008  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2009  * @vsi: the VSI being setup
2010  * @ctxt: VSI context structure
2011  * @enabled_tc: Enabled TCs bitmap
2012  * @is_add: True if called before Add VSI
2013  *
2014  * Setup VSI queue mapping for enabled traffic classes.
2015  **/
2016 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2017 				     struct i40e_vsi_context *ctxt,
2018 				     u8 enabled_tc,
2019 				     bool is_add)
2020 {
2021 	struct i40e_pf *pf = vsi->back;
2022 	u16 num_tc_qps = 0;
2023 	u16 sections = 0;
2024 	u8 netdev_tc = 0;
2025 	u16 numtc = 1;
2026 	u16 qcount;
2027 	u8 offset;
2028 	u16 qmap;
2029 	int i;
2030 
2031 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2032 	offset = 0;
2033 	/* zero out queue mapping, it will get updated on the end of the function */
2034 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2035 
2036 	if (vsi->type == I40E_VSI_MAIN) {
2037 		/* This code helps add more queue to the VSI if we have
2038 		 * more cores than RSS can support, the higher cores will
2039 		 * be served by ATR or other filters. Furthermore, the
2040 		 * non-zero req_queue_pairs says that user requested a new
2041 		 * queue count via ethtool's set_channels, so use this
2042 		 * value for queues distribution across traffic classes
2043 		 * We need at least one queue pair for the interface
2044 		 * to be usable as we see in else statement.
2045 		 */
2046 		if (vsi->req_queue_pairs > 0)
2047 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2048 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2049 			vsi->num_queue_pairs = pf->num_lan_msix;
2050 		else
2051 			vsi->num_queue_pairs = 1;
2052 	}
2053 
2054 	/* Number of queues per enabled TC */
2055 	if (vsi->type == I40E_VSI_MAIN ||
2056 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2057 		num_tc_qps = vsi->num_queue_pairs;
2058 	else
2059 		num_tc_qps = vsi->alloc_queue_pairs;
2060 
2061 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2062 		/* Find numtc from enabled TC bitmap */
2063 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2064 			if (enabled_tc & BIT(i)) /* TC is enabled */
2065 				numtc++;
2066 		}
2067 		if (!numtc) {
2068 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2069 			numtc = 1;
2070 		}
2071 		num_tc_qps = num_tc_qps / numtc;
2072 		num_tc_qps = min_t(int, num_tc_qps,
2073 				   i40e_pf_get_max_q_per_tc(pf));
2074 	}
2075 
2076 	vsi->tc_config.numtc = numtc;
2077 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2078 
2079 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2080 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2081 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2082 
2083 	/* Setup queue offset/count for all TCs for given VSI */
2084 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2085 		/* See if the given TC is enabled for the given VSI */
2086 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2087 			/* TC is enabled */
2088 			int pow, num_qps;
2089 
2090 			switch (vsi->type) {
2091 			case I40E_VSI_MAIN:
2092 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2093 					       pf->flags) &&
2094 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2095 					       pf->flags)) ||
2096 				    vsi->tc_config.enabled_tc != 1) {
2097 					qcount = min_t(int, pf->alloc_rss_size,
2098 						       num_tc_qps);
2099 					break;
2100 				}
2101 				fallthrough;
2102 			case I40E_VSI_FDIR:
2103 			case I40E_VSI_SRIOV:
2104 			case I40E_VSI_VMDQ2:
2105 			default:
2106 				qcount = num_tc_qps;
2107 				WARN_ON(i != 0);
2108 				break;
2109 			}
2110 			vsi->tc_config.tc_info[i].qoffset = offset;
2111 			vsi->tc_config.tc_info[i].qcount = qcount;
2112 
2113 			/* find the next higher power-of-2 of num queue pairs */
2114 			num_qps = qcount;
2115 			pow = 0;
2116 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2117 				pow++;
2118 				num_qps >>= 1;
2119 			}
2120 
2121 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2122 			qmap =
2123 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2124 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2125 
2126 			offset += qcount;
2127 		} else {
2128 			/* TC is not enabled so set the offset to
2129 			 * default queue and allocate one queue
2130 			 * for the given TC.
2131 			 */
2132 			vsi->tc_config.tc_info[i].qoffset = 0;
2133 			vsi->tc_config.tc_info[i].qcount = 1;
2134 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2135 
2136 			qmap = 0;
2137 		}
2138 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2139 	}
2140 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2141 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2142 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2143 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2144 		vsi->num_queue_pairs = offset;
2145 
2146 	/* Scheduler section valid can only be set for ADD VSI */
2147 	if (is_add) {
2148 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2149 
2150 		ctxt->info.up_enable_bits = enabled_tc;
2151 	}
2152 	if (vsi->type == I40E_VSI_SRIOV) {
2153 		ctxt->info.mapping_flags |=
2154 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2155 		for (i = 0; i < vsi->num_queue_pairs; i++)
2156 			ctxt->info.queue_mapping[i] =
2157 					       cpu_to_le16(vsi->base_queue + i);
2158 	} else {
2159 		ctxt->info.mapping_flags |=
2160 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2161 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2162 	}
2163 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2164 }
2165 
2166 /**
2167  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2168  * @netdev: the netdevice
2169  * @addr: address to add
2170  *
2171  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2172  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2173  */
2174 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2175 {
2176 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2177 	struct i40e_vsi *vsi = np->vsi;
2178 
2179 	if (i40e_add_mac_filter(vsi, addr))
2180 		return 0;
2181 	else
2182 		return -ENOMEM;
2183 }
2184 
2185 /**
2186  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2187  * @netdev: the netdevice
2188  * @addr: address to add
2189  *
2190  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2191  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2192  */
2193 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2194 {
2195 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2196 	struct i40e_vsi *vsi = np->vsi;
2197 
2198 	/* Under some circumstances, we might receive a request to delete
2199 	 * our own device address from our uc list. Because we store the
2200 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2201 	 * such requests and not delete our device address from this list.
2202 	 */
2203 	if (ether_addr_equal(addr, netdev->dev_addr))
2204 		return 0;
2205 
2206 	i40e_del_mac_filter(vsi, addr);
2207 
2208 	return 0;
2209 }
2210 
2211 /**
2212  * i40e_set_rx_mode - NDO callback to set the netdev filters
2213  * @netdev: network interface device structure
2214  **/
2215 static void i40e_set_rx_mode(struct net_device *netdev)
2216 {
2217 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2218 	struct i40e_vsi *vsi = np->vsi;
2219 
2220 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2221 
2222 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2223 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2224 
2225 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2226 
2227 	/* check for other flag changes */
2228 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2229 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2230 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2231 	}
2232 }
2233 
2234 /**
2235  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2236  * @vsi: Pointer to VSI struct
2237  * @from: Pointer to list which contains MAC filter entries - changes to
2238  *        those entries needs to be undone.
2239  *
2240  * MAC filter entries from this list were slated for deletion.
2241  **/
2242 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2243 					 struct hlist_head *from)
2244 {
2245 	struct i40e_mac_filter *f;
2246 	struct hlist_node *h;
2247 
2248 	hlist_for_each_entry_safe(f, h, from, hlist) {
2249 		u64 key = i40e_addr_to_hkey(f->macaddr);
2250 
2251 		/* Move the element back into MAC filter list*/
2252 		hlist_del(&f->hlist);
2253 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2254 	}
2255 }
2256 
2257 /**
2258  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2259  * @vsi: Pointer to vsi struct
2260  * @from: Pointer to list which contains MAC filter entries - changes to
2261  *        those entries needs to be undone.
2262  *
2263  * MAC filter entries from this list were slated for addition.
2264  **/
2265 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2266 					 struct hlist_head *from)
2267 {
2268 	struct i40e_new_mac_filter *new;
2269 	struct hlist_node *h;
2270 
2271 	hlist_for_each_entry_safe(new, h, from, hlist) {
2272 		/* We can simply free the wrapper structure */
2273 		hlist_del(&new->hlist);
2274 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2275 		kfree(new);
2276 	}
2277 }
2278 
2279 /**
2280  * i40e_next_filter - Get the next non-broadcast filter from a list
2281  * @next: pointer to filter in list
2282  *
2283  * Returns the next non-broadcast filter in the list. Required so that we
2284  * ignore broadcast filters within the list, since these are not handled via
2285  * the normal firmware update path.
2286  */
2287 static
2288 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2289 {
2290 	hlist_for_each_entry_continue(next, hlist) {
2291 		if (!is_broadcast_ether_addr(next->f->macaddr))
2292 			return next;
2293 	}
2294 
2295 	return NULL;
2296 }
2297 
2298 /**
2299  * i40e_update_filter_state - Update filter state based on return data
2300  * from firmware
2301  * @count: Number of filters added
2302  * @add_list: return data from fw
2303  * @add_head: pointer to first filter in current batch
2304  *
2305  * MAC filter entries from list were slated to be added to device. Returns
2306  * number of successful filters. Note that 0 does NOT mean success!
2307  **/
2308 static int
2309 i40e_update_filter_state(int count,
2310 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2311 			 struct i40e_new_mac_filter *add_head)
2312 {
2313 	int retval = 0;
2314 	int i;
2315 
2316 	for (i = 0; i < count; i++) {
2317 		/* Always check status of each filter. We don't need to check
2318 		 * the firmware return status because we pre-set the filter
2319 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2320 		 * request to the adminq. Thus, if it no longer matches then
2321 		 * we know the filter is active.
2322 		 */
2323 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2324 			add_head->state = I40E_FILTER_FAILED;
2325 		} else {
2326 			add_head->state = I40E_FILTER_ACTIVE;
2327 			retval++;
2328 		}
2329 
2330 		add_head = i40e_next_filter(add_head);
2331 		if (!add_head)
2332 			break;
2333 	}
2334 
2335 	return retval;
2336 }
2337 
2338 /**
2339  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2340  * @vsi: ptr to the VSI
2341  * @vsi_name: name to display in messages
2342  * @list: the list of filters to send to firmware
2343  * @num_del: the number of filters to delete
2344  * @retval: Set to -EIO on failure to delete
2345  *
2346  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2347  * *retval instead of a return value so that success does not force ret_val to
2348  * be set to 0. This ensures that a sequence of calls to this function
2349  * preserve the previous value of *retval on successful delete.
2350  */
2351 static
2352 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2353 			  struct i40e_aqc_remove_macvlan_element_data *list,
2354 			  int num_del, int *retval)
2355 {
2356 	struct i40e_hw *hw = &vsi->back->hw;
2357 	enum i40e_admin_queue_err aq_status;
2358 	int aq_ret;
2359 
2360 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2361 					   &aq_status);
2362 
2363 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2364 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2365 		*retval = -EIO;
2366 		dev_info(&vsi->back->pdev->dev,
2367 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2368 			 vsi_name, ERR_PTR(aq_ret),
2369 			 i40e_aq_str(hw, aq_status));
2370 	}
2371 }
2372 
2373 /**
2374  * i40e_aqc_add_filters - Request firmware to add a set of filters
2375  * @vsi: ptr to the VSI
2376  * @vsi_name: name to display in messages
2377  * @list: the list of filters to send to firmware
2378  * @add_head: Position in the add hlist
2379  * @num_add: the number of filters to add
2380  *
2381  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2382  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2383  * space for more filters.
2384  */
2385 static
2386 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2387 			  struct i40e_aqc_add_macvlan_element_data *list,
2388 			  struct i40e_new_mac_filter *add_head,
2389 			  int num_add)
2390 {
2391 	struct i40e_hw *hw = &vsi->back->hw;
2392 	enum i40e_admin_queue_err aq_status;
2393 	int fcnt;
2394 
2395 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2396 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2397 
2398 	if (fcnt != num_add) {
2399 		if (vsi->type == I40E_VSI_MAIN) {
2400 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2401 			dev_warn(&vsi->back->pdev->dev,
2402 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2403 				 i40e_aq_str(hw, aq_status), vsi_name);
2404 		} else if (vsi->type == I40E_VSI_SRIOV ||
2405 			   vsi->type == I40E_VSI_VMDQ1 ||
2406 			   vsi->type == I40E_VSI_VMDQ2) {
2407 			dev_warn(&vsi->back->pdev->dev,
2408 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2409 				 i40e_aq_str(hw, aq_status), vsi_name,
2410 					     vsi_name);
2411 		} else {
2412 			dev_warn(&vsi->back->pdev->dev,
2413 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2414 				 i40e_aq_str(hw, aq_status), vsi_name,
2415 					     vsi->type);
2416 		}
2417 	}
2418 }
2419 
2420 /**
2421  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2422  * @vsi: pointer to the VSI
2423  * @vsi_name: the VSI name
2424  * @f: filter data
2425  *
2426  * This function sets or clears the promiscuous broadcast flags for VLAN
2427  * filters in order to properly receive broadcast frames. Assumes that only
2428  * broadcast filters are passed.
2429  *
2430  * Returns status indicating success or failure;
2431  **/
2432 static int
2433 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2434 			  struct i40e_mac_filter *f)
2435 {
2436 	bool enable = f->state == I40E_FILTER_NEW;
2437 	struct i40e_hw *hw = &vsi->back->hw;
2438 	int aq_ret;
2439 
2440 	if (f->vlan == I40E_VLAN_ANY) {
2441 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2442 						   vsi->seid,
2443 						   enable,
2444 						   NULL);
2445 	} else {
2446 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2447 							    vsi->seid,
2448 							    enable,
2449 							    f->vlan,
2450 							    NULL);
2451 	}
2452 
2453 	if (aq_ret) {
2454 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2455 		dev_warn(&vsi->back->pdev->dev,
2456 			 "Error %s, forcing overflow promiscuous on %s\n",
2457 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2458 			 vsi_name);
2459 	}
2460 
2461 	return aq_ret;
2462 }
2463 
2464 /**
2465  * i40e_set_promiscuous - set promiscuous mode
2466  * @pf: board private structure
2467  * @promisc: promisc on or off
2468  *
2469  * There are different ways of setting promiscuous mode on a PF depending on
2470  * what state/environment we're in.  This identifies and sets it appropriately.
2471  * Returns 0 on success.
2472  **/
2473 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2474 {
2475 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2476 	struct i40e_hw *hw = &pf->hw;
2477 	int aq_ret;
2478 
2479 	if (vsi->type == I40E_VSI_MAIN &&
2480 	    pf->lan_veb != I40E_NO_VEB &&
2481 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2482 		/* set defport ON for Main VSI instead of true promisc
2483 		 * this way we will get all unicast/multicast and VLAN
2484 		 * promisc behavior but will not get VF or VMDq traffic
2485 		 * replicated on the Main VSI.
2486 		 */
2487 		if (promisc)
2488 			aq_ret = i40e_aq_set_default_vsi(hw,
2489 							 vsi->seid,
2490 							 NULL);
2491 		else
2492 			aq_ret = i40e_aq_clear_default_vsi(hw,
2493 							   vsi->seid,
2494 							   NULL);
2495 		if (aq_ret) {
2496 			dev_info(&pf->pdev->dev,
2497 				 "Set default VSI failed, err %pe, aq_err %s\n",
2498 				 ERR_PTR(aq_ret),
2499 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2500 		}
2501 	} else {
2502 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2503 						  hw,
2504 						  vsi->seid,
2505 						  promisc, NULL,
2506 						  true);
2507 		if (aq_ret) {
2508 			dev_info(&pf->pdev->dev,
2509 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2510 				 ERR_PTR(aq_ret),
2511 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2512 		}
2513 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2514 						  hw,
2515 						  vsi->seid,
2516 						  promisc, NULL);
2517 		if (aq_ret) {
2518 			dev_info(&pf->pdev->dev,
2519 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2520 				 ERR_PTR(aq_ret),
2521 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2522 		}
2523 	}
2524 
2525 	if (!aq_ret)
2526 		pf->cur_promisc = promisc;
2527 
2528 	return aq_ret;
2529 }
2530 
2531 /**
2532  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2533  * @vsi: ptr to the VSI
2534  *
2535  * Push any outstanding VSI filter changes through the AdminQ.
2536  *
2537  * Returns 0 or error value
2538  **/
2539 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2540 {
2541 	struct hlist_head tmp_add_list, tmp_del_list;
2542 	struct i40e_mac_filter *f;
2543 	struct i40e_new_mac_filter *new, *add_head = NULL;
2544 	struct i40e_hw *hw = &vsi->back->hw;
2545 	bool old_overflow, new_overflow;
2546 	unsigned int failed_filters = 0;
2547 	unsigned int vlan_filters = 0;
2548 	char vsi_name[16] = "PF";
2549 	int filter_list_len = 0;
2550 	u32 changed_flags = 0;
2551 	struct hlist_node *h;
2552 	struct i40e_pf *pf;
2553 	int num_add = 0;
2554 	int num_del = 0;
2555 	int aq_ret = 0;
2556 	int retval = 0;
2557 	u16 cmd_flags;
2558 	int list_size;
2559 	int bkt;
2560 
2561 	/* empty array typed pointers, kcalloc later */
2562 	struct i40e_aqc_add_macvlan_element_data *add_list;
2563 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2564 
2565 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2566 		usleep_range(1000, 2000);
2567 	pf = vsi->back;
2568 
2569 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2570 
2571 	if (vsi->netdev) {
2572 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2573 		vsi->current_netdev_flags = vsi->netdev->flags;
2574 	}
2575 
2576 	INIT_HLIST_HEAD(&tmp_add_list);
2577 	INIT_HLIST_HEAD(&tmp_del_list);
2578 
2579 	if (vsi->type == I40E_VSI_SRIOV)
2580 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2581 	else if (vsi->type != I40E_VSI_MAIN)
2582 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2583 
2584 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2585 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2586 
2587 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2588 		/* Create a list of filters to delete. */
2589 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2590 			if (f->state == I40E_FILTER_REMOVE) {
2591 				/* Move the element into temporary del_list */
2592 				hash_del(&f->hlist);
2593 				hlist_add_head(&f->hlist, &tmp_del_list);
2594 
2595 				/* Avoid counting removed filters */
2596 				continue;
2597 			}
2598 			if (f->state == I40E_FILTER_NEW) {
2599 				/* Create a temporary i40e_new_mac_filter */
2600 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2601 				if (!new)
2602 					goto err_no_memory_locked;
2603 
2604 				/* Store pointer to the real filter */
2605 				new->f = f;
2606 				new->state = f->state;
2607 
2608 				/* Add it to the hash list */
2609 				hlist_add_head(&new->hlist, &tmp_add_list);
2610 			}
2611 
2612 			/* Count the number of active (current and new) VLAN
2613 			 * filters we have now. Does not count filters which
2614 			 * are marked for deletion.
2615 			 */
2616 			if (f->vlan > 0)
2617 				vlan_filters++;
2618 		}
2619 
2620 		if (vsi->type != I40E_VSI_SRIOV)
2621 			retval = i40e_correct_mac_vlan_filters
2622 				(vsi, &tmp_add_list, &tmp_del_list,
2623 				 vlan_filters);
2624 		else if (pf->vf)
2625 			retval = i40e_correct_vf_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2628 
2629 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2630 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2631 
2632 		if (retval)
2633 			goto err_no_memory_locked;
2634 
2635 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2636 	}
2637 
2638 	/* Now process 'del_list' outside the lock */
2639 	if (!hlist_empty(&tmp_del_list)) {
2640 		filter_list_len = hw->aq.asq_buf_size /
2641 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2642 		list_size = filter_list_len *
2643 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2644 		del_list = kzalloc(list_size, GFP_ATOMIC);
2645 		if (!del_list)
2646 			goto err_no_memory;
2647 
2648 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2649 			cmd_flags = 0;
2650 
2651 			/* handle broadcast filters by updating the broadcast
2652 			 * promiscuous flag and release filter list.
2653 			 */
2654 			if (is_broadcast_ether_addr(f->macaddr)) {
2655 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2656 
2657 				hlist_del(&f->hlist);
2658 				kfree(f);
2659 				continue;
2660 			}
2661 
2662 			/* add to delete list */
2663 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2664 			if (f->vlan == I40E_VLAN_ANY) {
2665 				del_list[num_del].vlan_tag = 0;
2666 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2667 			} else {
2668 				del_list[num_del].vlan_tag =
2669 					cpu_to_le16((u16)(f->vlan));
2670 			}
2671 
2672 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2673 			del_list[num_del].flags = cmd_flags;
2674 			num_del++;
2675 
2676 			/* flush a full buffer */
2677 			if (num_del == filter_list_len) {
2678 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2679 						     num_del, &retval);
2680 				memset(del_list, 0, list_size);
2681 				num_del = 0;
2682 			}
2683 			/* Release memory for MAC filter entries which were
2684 			 * synced up with HW.
2685 			 */
2686 			hlist_del(&f->hlist);
2687 			kfree(f);
2688 		}
2689 
2690 		if (num_del) {
2691 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2692 					     num_del, &retval);
2693 		}
2694 
2695 		kfree(del_list);
2696 		del_list = NULL;
2697 	}
2698 
2699 	if (!hlist_empty(&tmp_add_list)) {
2700 		/* Do all the adds now. */
2701 		filter_list_len = hw->aq.asq_buf_size /
2702 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2703 		list_size = filter_list_len *
2704 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2705 		add_list = kzalloc(list_size, GFP_ATOMIC);
2706 		if (!add_list)
2707 			goto err_no_memory;
2708 
2709 		num_add = 0;
2710 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2711 			/* handle broadcast filters by updating the broadcast
2712 			 * promiscuous flag instead of adding a MAC filter.
2713 			 */
2714 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2715 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2716 							      new->f))
2717 					new->state = I40E_FILTER_FAILED;
2718 				else
2719 					new->state = I40E_FILTER_ACTIVE;
2720 				continue;
2721 			}
2722 
2723 			/* add to add array */
2724 			if (num_add == 0)
2725 				add_head = new;
2726 			cmd_flags = 0;
2727 			ether_addr_copy(add_list[num_add].mac_addr,
2728 					new->f->macaddr);
2729 			if (new->f->vlan == I40E_VLAN_ANY) {
2730 				add_list[num_add].vlan_tag = 0;
2731 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2732 			} else {
2733 				add_list[num_add].vlan_tag =
2734 					cpu_to_le16((u16)(new->f->vlan));
2735 			}
2736 			add_list[num_add].queue_number = 0;
2737 			/* set invalid match method for later detection */
2738 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2739 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2740 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2741 			num_add++;
2742 
2743 			/* flush a full buffer */
2744 			if (num_add == filter_list_len) {
2745 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2746 						     add_head, num_add);
2747 				memset(add_list, 0, list_size);
2748 				num_add = 0;
2749 			}
2750 		}
2751 		if (num_add) {
2752 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2753 					     num_add);
2754 		}
2755 		/* Now move all of the filters from the temp add list back to
2756 		 * the VSI's list.
2757 		 */
2758 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2759 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2760 			/* Only update the state if we're still NEW */
2761 			if (new->f->state == I40E_FILTER_NEW)
2762 				new->f->state = new->state;
2763 			hlist_del(&new->hlist);
2764 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2765 			kfree(new);
2766 		}
2767 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2768 		kfree(add_list);
2769 		add_list = NULL;
2770 	}
2771 
2772 	/* Determine the number of active and failed filters. */
2773 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2774 	vsi->active_filters = 0;
2775 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2776 		if (f->state == I40E_FILTER_ACTIVE)
2777 			vsi->active_filters++;
2778 		else if (f->state == I40E_FILTER_FAILED)
2779 			failed_filters++;
2780 	}
2781 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2782 
2783 	/* Check if we are able to exit overflow promiscuous mode. We can
2784 	 * safely exit if we didn't just enter, we no longer have any failed
2785 	 * filters, and we have reduced filters below the threshold value.
2786 	 */
2787 	if (old_overflow && !failed_filters &&
2788 	    vsi->active_filters < vsi->promisc_threshold) {
2789 		dev_info(&pf->pdev->dev,
2790 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2791 			 vsi_name);
2792 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 		vsi->promisc_threshold = 0;
2794 	}
2795 
2796 	/* if the VF is not trusted do not do promisc */
2797 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2798 	    !pf->vf[vsi->vf_id].trusted) {
2799 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2800 		goto out;
2801 	}
2802 
2803 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 
2805 	/* If we are entering overflow promiscuous, we need to calculate a new
2806 	 * threshold for when we are safe to exit
2807 	 */
2808 	if (!old_overflow && new_overflow)
2809 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2810 
2811 	/* check for changes in promiscuous modes */
2812 	if (changed_flags & IFF_ALLMULTI) {
2813 		bool cur_multipromisc;
2814 
2815 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2816 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2817 							       vsi->seid,
2818 							       cur_multipromisc,
2819 							       NULL);
2820 		if (aq_ret) {
2821 			retval = i40e_aq_rc_to_posix(aq_ret,
2822 						     hw->aq.asq_last_status);
2823 			dev_info(&pf->pdev->dev,
2824 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2825 				 vsi_name,
2826 				 ERR_PTR(aq_ret),
2827 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2828 		} else {
2829 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2830 				 cur_multipromisc ? "entering" : "leaving");
2831 		}
2832 	}
2833 
2834 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2835 		bool cur_promisc;
2836 
2837 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2838 			       new_overflow);
2839 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2840 		if (aq_ret) {
2841 			retval = i40e_aq_rc_to_posix(aq_ret,
2842 						     hw->aq.asq_last_status);
2843 			dev_info(&pf->pdev->dev,
2844 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2845 				 cur_promisc ? "on" : "off",
2846 				 vsi_name,
2847 				 ERR_PTR(aq_ret),
2848 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2849 		}
2850 	}
2851 out:
2852 	/* if something went wrong then set the changed flag so we try again */
2853 	if (retval)
2854 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2855 
2856 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2857 	return retval;
2858 
2859 err_no_memory:
2860 	/* Restore elements on the temporary add and delete lists */
2861 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2862 err_no_memory_locked:
2863 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2864 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2865 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2866 
2867 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2868 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2869 	return -ENOMEM;
2870 }
2871 
2872 /**
2873  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2874  * @pf: board private structure
2875  **/
2876 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2877 {
2878 	int v;
2879 
2880 	if (!pf)
2881 		return;
2882 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2883 		return;
2884 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2885 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2886 		return;
2887 	}
2888 
2889 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2890 		if (pf->vsi[v] &&
2891 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2892 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2893 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2894 
2895 			if (ret) {
2896 				/* come back and try again later */
2897 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2898 					pf->state);
2899 				break;
2900 			}
2901 		}
2902 	}
2903 }
2904 
2905 /**
2906  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2907  *
2908  * @vsi: VSI to calculate rx_buf_len from
2909  */
2910 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2911 {
2912 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2913 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2914 
2915 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2916 }
2917 
2918 /**
2919  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2920  * @vsi: the vsi
2921  * @xdp_prog: XDP program
2922  **/
2923 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2924 				   struct bpf_prog *xdp_prog)
2925 {
2926 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2927 	u16 chain_len;
2928 
2929 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2930 		chain_len = 1;
2931 	else
2932 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2933 
2934 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2935 }
2936 
2937 /**
2938  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2939  * @netdev: network interface device structure
2940  * @new_mtu: new value for maximum frame size
2941  *
2942  * Returns 0 on success, negative on failure
2943  **/
2944 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2945 {
2946 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2947 	struct i40e_vsi *vsi = np->vsi;
2948 	struct i40e_pf *pf = vsi->back;
2949 	int frame_size;
2950 
2951 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2952 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2953 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2954 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2955 		return -EINVAL;
2956 	}
2957 
2958 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2959 		   netdev->mtu, new_mtu);
2960 	netdev->mtu = new_mtu;
2961 	if (netif_running(netdev))
2962 		i40e_vsi_reinit_locked(vsi);
2963 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2964 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2965 	return 0;
2966 }
2967 
2968 /**
2969  * i40e_ioctl - Access the hwtstamp interface
2970  * @netdev: network interface device structure
2971  * @ifr: interface request data
2972  * @cmd: ioctl command
2973  **/
2974 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2975 {
2976 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2977 	struct i40e_pf *pf = np->vsi->back;
2978 
2979 	switch (cmd) {
2980 	case SIOCGHWTSTAMP:
2981 		return i40e_ptp_get_ts_config(pf, ifr);
2982 	case SIOCSHWTSTAMP:
2983 		return i40e_ptp_set_ts_config(pf, ifr);
2984 	default:
2985 		return -EOPNOTSUPP;
2986 	}
2987 }
2988 
2989 /**
2990  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2991  * @vsi: the vsi being adjusted
2992  **/
2993 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2994 {
2995 	struct i40e_vsi_context ctxt;
2996 	int ret;
2997 
2998 	/* Don't modify stripping options if a port VLAN is active */
2999 	if (vsi->info.pvid)
3000 		return;
3001 
3002 	if ((vsi->info.valid_sections &
3003 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3004 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3005 		return;  /* already enabled */
3006 
3007 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3008 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3009 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3010 
3011 	ctxt.seid = vsi->seid;
3012 	ctxt.info = vsi->info;
3013 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3014 	if (ret) {
3015 		dev_info(&vsi->back->pdev->dev,
3016 			 "update vlan stripping failed, err %pe aq_err %s\n",
3017 			 ERR_PTR(ret),
3018 			 i40e_aq_str(&vsi->back->hw,
3019 				     vsi->back->hw.aq.asq_last_status));
3020 	}
3021 }
3022 
3023 /**
3024  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3025  * @vsi: the vsi being adjusted
3026  **/
3027 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3028 {
3029 	struct i40e_vsi_context ctxt;
3030 	int ret;
3031 
3032 	/* Don't modify stripping options if a port VLAN is active */
3033 	if (vsi->info.pvid)
3034 		return;
3035 
3036 	if ((vsi->info.valid_sections &
3037 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3038 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3039 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3040 		return;  /* already disabled */
3041 
3042 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3043 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3044 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3045 
3046 	ctxt.seid = vsi->seid;
3047 	ctxt.info = vsi->info;
3048 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3049 	if (ret) {
3050 		dev_info(&vsi->back->pdev->dev,
3051 			 "update vlan stripping failed, err %pe aq_err %s\n",
3052 			 ERR_PTR(ret),
3053 			 i40e_aq_str(&vsi->back->hw,
3054 				     vsi->back->hw.aq.asq_last_status));
3055 	}
3056 }
3057 
3058 /**
3059  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3060  * @vsi: the vsi being configured
3061  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3062  *
3063  * This is a helper function for adding a new MAC/VLAN filter with the
3064  * specified VLAN for each existing MAC address already in the hash table.
3065  * This function does *not* perform any accounting to update filters based on
3066  * VLAN mode.
3067  *
3068  * NOTE: this function expects to be called while under the
3069  * mac_filter_hash_lock
3070  **/
3071 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3072 {
3073 	struct i40e_mac_filter *f, *add_f;
3074 	struct hlist_node *h;
3075 	int bkt;
3076 
3077 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3078 		/* If we're asked to add a filter that has been marked for
3079 		 * removal, it is safe to simply restore it to active state.
3080 		 * __i40e_del_filter will have simply deleted any filters which
3081 		 * were previously marked NEW or FAILED, so if it is currently
3082 		 * marked REMOVE it must have previously been ACTIVE. Since we
3083 		 * haven't yet run the sync filters task, just restore this
3084 		 * filter to the ACTIVE state so that the sync task leaves it
3085 		 * in place.
3086 		 */
3087 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3088 			f->state = I40E_FILTER_ACTIVE;
3089 			continue;
3090 		} else if (f->state == I40E_FILTER_REMOVE) {
3091 			continue;
3092 		}
3093 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3094 		if (!add_f) {
3095 			dev_info(&vsi->back->pdev->dev,
3096 				 "Could not add vlan filter %d for %pM\n",
3097 				 vid, f->macaddr);
3098 			return -ENOMEM;
3099 		}
3100 	}
3101 
3102 	return 0;
3103 }
3104 
3105 /**
3106  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3107  * @vsi: the VSI being configured
3108  * @vid: VLAN id to be added
3109  **/
3110 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3111 {
3112 	int err;
3113 
3114 	if (vsi->info.pvid)
3115 		return -EINVAL;
3116 
3117 	/* The network stack will attempt to add VID=0, with the intention to
3118 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3119 	 * these packets by default when configured to receive untagged
3120 	 * packets, so we don't need to add a filter for this case.
3121 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3122 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3123 	 * Thus, we do not want to actually add a filter for VID=0
3124 	 */
3125 	if (!vid)
3126 		return 0;
3127 
3128 	/* Locked once because all functions invoked below iterates list*/
3129 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3130 	err = i40e_add_vlan_all_mac(vsi, vid);
3131 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3132 	if (err)
3133 		return err;
3134 
3135 	/* schedule our worker thread which will take care of
3136 	 * applying the new filter changes
3137 	 */
3138 	i40e_service_event_schedule(vsi->back);
3139 	return 0;
3140 }
3141 
3142 /**
3143  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3144  * @vsi: the vsi being configured
3145  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3146  *
3147  * This function should be used to remove all VLAN filters which match the
3148  * given VID. It does not schedule the service event and does not take the
3149  * mac_filter_hash_lock so it may be combined with other operations under
3150  * a single invocation of the mac_filter_hash_lock.
3151  *
3152  * NOTE: this function expects to be called while under the
3153  * mac_filter_hash_lock
3154  */
3155 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3156 {
3157 	struct i40e_mac_filter *f;
3158 	struct hlist_node *h;
3159 	int bkt;
3160 
3161 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3162 		if (f->vlan == vid)
3163 			__i40e_del_filter(vsi, f);
3164 	}
3165 }
3166 
3167 /**
3168  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3169  * @vsi: the VSI being configured
3170  * @vid: VLAN id to be removed
3171  **/
3172 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3173 {
3174 	if (!vid || vsi->info.pvid)
3175 		return;
3176 
3177 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3178 	i40e_rm_vlan_all_mac(vsi, vid);
3179 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3180 
3181 	/* schedule our worker thread which will take care of
3182 	 * applying the new filter changes
3183 	 */
3184 	i40e_service_event_schedule(vsi->back);
3185 }
3186 
3187 /**
3188  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3189  * @netdev: network interface to be adjusted
3190  * @proto: unused protocol value
3191  * @vid: vlan id to be added
3192  *
3193  * net_device_ops implementation for adding vlan ids
3194  **/
3195 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3196 				__always_unused __be16 proto, u16 vid)
3197 {
3198 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3199 	struct i40e_vsi *vsi = np->vsi;
3200 	int ret = 0;
3201 
3202 	if (vid >= VLAN_N_VID)
3203 		return -EINVAL;
3204 
3205 	ret = i40e_vsi_add_vlan(vsi, vid);
3206 	if (!ret)
3207 		set_bit(vid, vsi->active_vlans);
3208 
3209 	return ret;
3210 }
3211 
3212 /**
3213  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3214  * @netdev: network interface to be adjusted
3215  * @proto: unused protocol value
3216  * @vid: vlan id to be added
3217  **/
3218 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3219 				    __always_unused __be16 proto, u16 vid)
3220 {
3221 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3222 	struct i40e_vsi *vsi = np->vsi;
3223 
3224 	if (vid >= VLAN_N_VID)
3225 		return;
3226 	set_bit(vid, vsi->active_vlans);
3227 }
3228 
3229 /**
3230  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3231  * @netdev: network interface to be adjusted
3232  * @proto: unused protocol value
3233  * @vid: vlan id to be removed
3234  *
3235  * net_device_ops implementation for removing vlan ids
3236  **/
3237 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3238 				 __always_unused __be16 proto, u16 vid)
3239 {
3240 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3241 	struct i40e_vsi *vsi = np->vsi;
3242 
3243 	/* return code is ignored as there is nothing a user
3244 	 * can do about failure to remove and a log message was
3245 	 * already printed from the other function
3246 	 */
3247 	i40e_vsi_kill_vlan(vsi, vid);
3248 
3249 	clear_bit(vid, vsi->active_vlans);
3250 
3251 	return 0;
3252 }
3253 
3254 /**
3255  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3256  * @vsi: the vsi being brought back up
3257  **/
3258 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3259 {
3260 	u16 vid;
3261 
3262 	if (!vsi->netdev)
3263 		return;
3264 
3265 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3266 		i40e_vlan_stripping_enable(vsi);
3267 	else
3268 		i40e_vlan_stripping_disable(vsi);
3269 
3270 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3271 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3272 					vid);
3273 }
3274 
3275 /**
3276  * i40e_vsi_add_pvid - Add pvid for the VSI
3277  * @vsi: the vsi being adjusted
3278  * @vid: the vlan id to set as a PVID
3279  **/
3280 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3281 {
3282 	struct i40e_vsi_context ctxt;
3283 	int ret;
3284 
3285 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3286 	vsi->info.pvid = cpu_to_le16(vid);
3287 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3288 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3289 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3290 
3291 	ctxt.seid = vsi->seid;
3292 	ctxt.info = vsi->info;
3293 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3294 	if (ret) {
3295 		dev_info(&vsi->back->pdev->dev,
3296 			 "add pvid failed, err %pe aq_err %s\n",
3297 			 ERR_PTR(ret),
3298 			 i40e_aq_str(&vsi->back->hw,
3299 				     vsi->back->hw.aq.asq_last_status));
3300 		return -ENOENT;
3301 	}
3302 
3303 	return 0;
3304 }
3305 
3306 /**
3307  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3308  * @vsi: the vsi being adjusted
3309  *
3310  * Just use the vlan_rx_register() service to put it back to normal
3311  **/
3312 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3313 {
3314 	vsi->info.pvid = 0;
3315 
3316 	i40e_vlan_stripping_disable(vsi);
3317 }
3318 
3319 /**
3320  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3321  * @vsi: ptr to the VSI
3322  *
3323  * If this function returns with an error, then it's possible one or
3324  * more of the rings is populated (while the rest are not).  It is the
3325  * callers duty to clean those orphaned rings.
3326  *
3327  * Return 0 on success, negative on failure
3328  **/
3329 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3330 {
3331 	int i, err = 0;
3332 
3333 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3334 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3335 
3336 	if (!i40e_enabled_xdp_vsi(vsi))
3337 		return err;
3338 
3339 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3340 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3341 
3342 	return err;
3343 }
3344 
3345 /**
3346  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3347  * @vsi: ptr to the VSI
3348  *
3349  * Free VSI's transmit software resources
3350  **/
3351 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3352 {
3353 	int i;
3354 
3355 	if (vsi->tx_rings) {
3356 		for (i = 0; i < vsi->num_queue_pairs; i++)
3357 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3358 				i40e_free_tx_resources(vsi->tx_rings[i]);
3359 	}
3360 
3361 	if (vsi->xdp_rings) {
3362 		for (i = 0; i < vsi->num_queue_pairs; i++)
3363 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3364 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3365 	}
3366 }
3367 
3368 /**
3369  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3370  * @vsi: ptr to the VSI
3371  *
3372  * If this function returns with an error, then it's possible one or
3373  * more of the rings is populated (while the rest are not).  It is the
3374  * callers duty to clean those orphaned rings.
3375  *
3376  * Return 0 on success, negative on failure
3377  **/
3378 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3379 {
3380 	int i, err = 0;
3381 
3382 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3383 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3384 	return err;
3385 }
3386 
3387 /**
3388  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3389  * @vsi: ptr to the VSI
3390  *
3391  * Free all receive software resources
3392  **/
3393 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3394 {
3395 	int i;
3396 
3397 	if (!vsi->rx_rings)
3398 		return;
3399 
3400 	for (i = 0; i < vsi->num_queue_pairs; i++)
3401 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3402 			i40e_free_rx_resources(vsi->rx_rings[i]);
3403 }
3404 
3405 /**
3406  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3407  * @ring: The Tx ring to configure
3408  *
3409  * This enables/disables XPS for a given Tx descriptor ring
3410  * based on the TCs enabled for the VSI that ring belongs to.
3411  **/
3412 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3413 {
3414 	int cpu;
3415 
3416 	if (!ring->q_vector || !ring->netdev || ring->ch)
3417 		return;
3418 
3419 	/* We only initialize XPS once, so as not to overwrite user settings */
3420 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3421 		return;
3422 
3423 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3424 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3425 			    ring->queue_index);
3426 }
3427 
3428 /**
3429  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3430  * @ring: The Tx or Rx ring
3431  *
3432  * Returns the AF_XDP buffer pool or NULL.
3433  **/
3434 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3435 {
3436 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3437 	int qid = ring->queue_index;
3438 
3439 	if (ring_is_xdp(ring))
3440 		qid -= ring->vsi->alloc_queue_pairs;
3441 
3442 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3443 		return NULL;
3444 
3445 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3446 }
3447 
3448 /**
3449  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3450  * @ring: The Tx ring to configure
3451  *
3452  * Configure the Tx descriptor ring in the HMC context.
3453  **/
3454 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3455 {
3456 	struct i40e_vsi *vsi = ring->vsi;
3457 	u16 pf_q = vsi->base_queue + ring->queue_index;
3458 	struct i40e_hw *hw = &vsi->back->hw;
3459 	struct i40e_hmc_obj_txq tx_ctx;
3460 	u32 qtx_ctl = 0;
3461 	int err = 0;
3462 
3463 	if (ring_is_xdp(ring))
3464 		ring->xsk_pool = i40e_xsk_pool(ring);
3465 
3466 	/* some ATR related tx ring init */
3467 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3468 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3469 		ring->atr_count = 0;
3470 	} else {
3471 		ring->atr_sample_rate = 0;
3472 	}
3473 
3474 	/* configure XPS */
3475 	i40e_config_xps_tx_ring(ring);
3476 
3477 	/* clear the context structure first */
3478 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3479 
3480 	tx_ctx.new_context = 1;
3481 	tx_ctx.base = (ring->dma / 128);
3482 	tx_ctx.qlen = ring->count;
3483 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3484 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3485 		tx_ctx.fd_ena = 1;
3486 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3487 		tx_ctx.timesync_ena = 1;
3488 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3489 	if (vsi->type != I40E_VSI_FDIR)
3490 		tx_ctx.head_wb_ena = 1;
3491 	tx_ctx.head_wb_addr = ring->dma +
3492 			      (ring->count * sizeof(struct i40e_tx_desc));
3493 
3494 	/* As part of VSI creation/update, FW allocates certain
3495 	 * Tx arbitration queue sets for each TC enabled for
3496 	 * the VSI. The FW returns the handles to these queue
3497 	 * sets as part of the response buffer to Add VSI,
3498 	 * Update VSI, etc. AQ commands. It is expected that
3499 	 * these queue set handles be associated with the Tx
3500 	 * queues by the driver as part of the TX queue context
3501 	 * initialization. This has to be done regardless of
3502 	 * DCB as by default everything is mapped to TC0.
3503 	 */
3504 
3505 	if (ring->ch)
3506 		tx_ctx.rdylist =
3507 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3508 
3509 	else
3510 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3511 
3512 	tx_ctx.rdylist_act = 0;
3513 
3514 	/* clear the context in the HMC */
3515 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3516 	if (err) {
3517 		dev_info(&vsi->back->pdev->dev,
3518 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3519 			 ring->queue_index, pf_q, err);
3520 		return -ENOMEM;
3521 	}
3522 
3523 	/* set the context in the HMC */
3524 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3525 	if (err) {
3526 		dev_info(&vsi->back->pdev->dev,
3527 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3528 			 ring->queue_index, pf_q, err);
3529 		return -ENOMEM;
3530 	}
3531 
3532 	/* Now associate this queue with this PCI function */
3533 	if (ring->ch) {
3534 		if (ring->ch->type == I40E_VSI_VMDQ2)
3535 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3536 		else
3537 			return -EINVAL;
3538 
3539 		qtx_ctl |= (ring->ch->vsi_number <<
3540 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3541 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3542 	} else {
3543 		if (vsi->type == I40E_VSI_VMDQ2) {
3544 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3545 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3546 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3547 		} else {
3548 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3549 		}
3550 	}
3551 
3552 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3553 		    I40E_QTX_CTL_PF_INDX_MASK);
3554 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3555 	i40e_flush(hw);
3556 
3557 	/* cache tail off for easier writes later */
3558 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3559 
3560 	return 0;
3561 }
3562 
3563 /**
3564  * i40e_rx_offset - Return expected offset into page to access data
3565  * @rx_ring: Ring we are requesting offset of
3566  *
3567  * Returns the offset value for ring into the data buffer.
3568  */
3569 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3570 {
3571 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3572 }
3573 
3574 /**
3575  * i40e_configure_rx_ring - Configure a receive ring context
3576  * @ring: The Rx ring to configure
3577  *
3578  * Configure the Rx descriptor ring in the HMC context.
3579  **/
3580 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3581 {
3582 	struct i40e_vsi *vsi = ring->vsi;
3583 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3584 	u16 pf_q = vsi->base_queue + ring->queue_index;
3585 	struct i40e_hw *hw = &vsi->back->hw;
3586 	struct i40e_hmc_obj_rxq rx_ctx;
3587 	int err = 0;
3588 	bool ok;
3589 	int ret;
3590 
3591 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3592 
3593 	/* clear the context structure first */
3594 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3595 
3596 	if (ring->vsi->type == I40E_VSI_MAIN)
3597 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3598 
3599 	ring->xsk_pool = i40e_xsk_pool(ring);
3600 	if (ring->xsk_pool) {
3601 		ring->rx_buf_len =
3602 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3603 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3604 						 MEM_TYPE_XSK_BUFF_POOL,
3605 						 NULL);
3606 		if (ret)
3607 			return ret;
3608 		dev_info(&vsi->back->pdev->dev,
3609 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3610 			 ring->queue_index);
3611 
3612 	} else {
3613 		ring->rx_buf_len = vsi->rx_buf_len;
3614 		if (ring->vsi->type == I40E_VSI_MAIN) {
3615 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3616 							 MEM_TYPE_PAGE_SHARED,
3617 							 NULL);
3618 			if (ret)
3619 				return ret;
3620 		}
3621 	}
3622 
3623 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3624 
3625 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3626 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3627 
3628 	rx_ctx.base = (ring->dma / 128);
3629 	rx_ctx.qlen = ring->count;
3630 
3631 	/* use 16 byte descriptors */
3632 	rx_ctx.dsize = 0;
3633 
3634 	/* descriptor type is always zero
3635 	 * rx_ctx.dtype = 0;
3636 	 */
3637 	rx_ctx.hsplit_0 = 0;
3638 
3639 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3640 	if (hw->revision_id == 0)
3641 		rx_ctx.lrxqthresh = 0;
3642 	else
3643 		rx_ctx.lrxqthresh = 1;
3644 	rx_ctx.crcstrip = 1;
3645 	rx_ctx.l2tsel = 1;
3646 	/* this controls whether VLAN is stripped from inner headers */
3647 	rx_ctx.showiv = 0;
3648 	/* set the prefena field to 1 because the manual says to */
3649 	rx_ctx.prefena = 1;
3650 
3651 	/* clear the context in the HMC */
3652 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3653 	if (err) {
3654 		dev_info(&vsi->back->pdev->dev,
3655 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3656 			 ring->queue_index, pf_q, err);
3657 		return -ENOMEM;
3658 	}
3659 
3660 	/* set the context in the HMC */
3661 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3662 	if (err) {
3663 		dev_info(&vsi->back->pdev->dev,
3664 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3665 			 ring->queue_index, pf_q, err);
3666 		return -ENOMEM;
3667 	}
3668 
3669 	/* configure Rx buffer alignment */
3670 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3671 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3672 			dev_info(&vsi->back->pdev->dev,
3673 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3674 			return -EOPNOTSUPP;
3675 		}
3676 		clear_ring_build_skb_enabled(ring);
3677 	} else {
3678 		set_ring_build_skb_enabled(ring);
3679 	}
3680 
3681 	ring->rx_offset = i40e_rx_offset(ring);
3682 
3683 	/* cache tail for quicker writes, and clear the reg before use */
3684 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3685 	writel(0, ring->tail);
3686 
3687 	if (ring->xsk_pool) {
3688 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3689 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3690 	} else {
3691 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3692 	}
3693 	if (!ok) {
3694 		/* Log this in case the user has forgotten to give the kernel
3695 		 * any buffers, even later in the application.
3696 		 */
3697 		dev_info(&vsi->back->pdev->dev,
3698 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3699 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3700 			 ring->queue_index, pf_q);
3701 	}
3702 
3703 	return 0;
3704 }
3705 
3706 /**
3707  * i40e_vsi_configure_tx - Configure the VSI for Tx
3708  * @vsi: VSI structure describing this set of rings and resources
3709  *
3710  * Configure the Tx VSI for operation.
3711  **/
3712 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3713 {
3714 	int err = 0;
3715 	u16 i;
3716 
3717 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3718 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3719 
3720 	if (err || !i40e_enabled_xdp_vsi(vsi))
3721 		return err;
3722 
3723 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3724 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3725 
3726 	return err;
3727 }
3728 
3729 /**
3730  * i40e_vsi_configure_rx - Configure the VSI for Rx
3731  * @vsi: the VSI being configured
3732  *
3733  * Configure the Rx VSI for operation.
3734  **/
3735 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3736 {
3737 	int err = 0;
3738 	u16 i;
3739 
3740 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3741 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3742 
3743 #if (PAGE_SIZE < 8192)
3744 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3745 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3746 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3747 		vsi->max_frame = vsi->rx_buf_len;
3748 	}
3749 #endif
3750 
3751 	/* set up individual rings */
3752 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3753 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3754 
3755 	return err;
3756 }
3757 
3758 /**
3759  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3760  * @vsi: ptr to the VSI
3761  **/
3762 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3763 {
3764 	struct i40e_ring *tx_ring, *rx_ring;
3765 	u16 qoffset, qcount;
3766 	int i, n;
3767 
3768 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3769 		/* Reset the TC information */
3770 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3771 			rx_ring = vsi->rx_rings[i];
3772 			tx_ring = vsi->tx_rings[i];
3773 			rx_ring->dcb_tc = 0;
3774 			tx_ring->dcb_tc = 0;
3775 		}
3776 		return;
3777 	}
3778 
3779 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3780 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3781 			continue;
3782 
3783 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3784 		qcount = vsi->tc_config.tc_info[n].qcount;
3785 		for (i = qoffset; i < (qoffset + qcount); i++) {
3786 			rx_ring = vsi->rx_rings[i];
3787 			tx_ring = vsi->tx_rings[i];
3788 			rx_ring->dcb_tc = n;
3789 			tx_ring->dcb_tc = n;
3790 		}
3791 	}
3792 }
3793 
3794 /**
3795  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3796  * @vsi: ptr to the VSI
3797  **/
3798 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3799 {
3800 	if (vsi->netdev)
3801 		i40e_set_rx_mode(vsi->netdev);
3802 }
3803 
3804 /**
3805  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3806  * @pf: Pointer to the targeted PF
3807  *
3808  * Set all flow director counters to 0.
3809  */
3810 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3811 {
3812 	pf->fd_tcp4_filter_cnt = 0;
3813 	pf->fd_udp4_filter_cnt = 0;
3814 	pf->fd_sctp4_filter_cnt = 0;
3815 	pf->fd_ip4_filter_cnt = 0;
3816 	pf->fd_tcp6_filter_cnt = 0;
3817 	pf->fd_udp6_filter_cnt = 0;
3818 	pf->fd_sctp6_filter_cnt = 0;
3819 	pf->fd_ip6_filter_cnt = 0;
3820 }
3821 
3822 /**
3823  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3824  * @vsi: Pointer to the targeted VSI
3825  *
3826  * This function replays the hlist on the hw where all the SB Flow Director
3827  * filters were saved.
3828  **/
3829 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3830 {
3831 	struct i40e_fdir_filter *filter;
3832 	struct i40e_pf *pf = vsi->back;
3833 	struct hlist_node *node;
3834 
3835 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3836 		return;
3837 
3838 	/* Reset FDir counters as we're replaying all existing filters */
3839 	i40e_reset_fdir_filter_cnt(pf);
3840 
3841 	hlist_for_each_entry_safe(filter, node,
3842 				  &pf->fdir_filter_list, fdir_node) {
3843 		i40e_add_del_fdir(vsi, filter, true);
3844 	}
3845 }
3846 
3847 /**
3848  * i40e_vsi_configure - Set up the VSI for action
3849  * @vsi: the VSI being configured
3850  **/
3851 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3852 {
3853 	int err;
3854 
3855 	i40e_set_vsi_rx_mode(vsi);
3856 	i40e_restore_vlan(vsi);
3857 	i40e_vsi_config_dcb_rings(vsi);
3858 	err = i40e_vsi_configure_tx(vsi);
3859 	if (!err)
3860 		err = i40e_vsi_configure_rx(vsi);
3861 
3862 	return err;
3863 }
3864 
3865 /**
3866  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3867  * @vsi: the VSI being configured
3868  **/
3869 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3870 {
3871 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3872 	struct i40e_pf *pf = vsi->back;
3873 	struct i40e_hw *hw = &pf->hw;
3874 	u16 vector;
3875 	int i, q;
3876 	u32 qp;
3877 
3878 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3879 	 * and PFINT_LNKLSTn registers, e.g.:
3880 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3881 	 */
3882 	qp = vsi->base_queue;
3883 	vector = vsi->base_vector;
3884 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3885 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3886 
3887 		q_vector->rx.next_update = jiffies + 1;
3888 		q_vector->rx.target_itr =
3889 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3890 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3891 		     q_vector->rx.target_itr >> 1);
3892 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3893 
3894 		q_vector->tx.next_update = jiffies + 1;
3895 		q_vector->tx.target_itr =
3896 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3897 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3898 		     q_vector->tx.target_itr >> 1);
3899 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3900 
3901 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3902 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3903 
3904 		/* begin of linked list for RX queue assigned to this vector */
3905 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3906 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3907 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3908 			u32 val;
3909 
3910 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3911 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3912 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3913 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3914 			      (I40E_QUEUE_TYPE_TX <<
3915 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3916 
3917 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3918 
3919 			if (has_xdp) {
3920 				/* TX queue with next queue set to TX */
3921 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3922 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3923 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3924 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3925 				      (I40E_QUEUE_TYPE_TX <<
3926 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3927 
3928 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3929 			}
3930 			/* TX queue with next RX or end of linked list */
3931 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3932 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3933 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3934 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3935 			      (I40E_QUEUE_TYPE_RX <<
3936 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3937 
3938 			/* Terminate the linked list */
3939 			if (q == (q_vector->num_ringpairs - 1))
3940 				val |= (I40E_QUEUE_END_OF_LIST <<
3941 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3942 
3943 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3944 			qp++;
3945 		}
3946 	}
3947 
3948 	i40e_flush(hw);
3949 }
3950 
3951 /**
3952  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3953  * @pf: pointer to private device data structure
3954  **/
3955 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3956 {
3957 	struct i40e_hw *hw = &pf->hw;
3958 	u32 val;
3959 
3960 	/* clear things first */
3961 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3962 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3963 
3964 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3965 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3966 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3967 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3968 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3969 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3970 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3971 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3972 
3973 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3974 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3975 
3976 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3977 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3978 
3979 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3980 
3981 	/* SW_ITR_IDX = 0, but don't change INTENA */
3982 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3983 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3984 
3985 	/* OTHER_ITR_IDX = 0 */
3986 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3987 }
3988 
3989 /**
3990  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3991  * @vsi: the VSI being configured
3992  **/
3993 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3994 {
3995 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3996 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3997 	struct i40e_pf *pf = vsi->back;
3998 	struct i40e_hw *hw = &pf->hw;
3999 
4000 	/* set the ITR configuration */
4001 	q_vector->rx.next_update = jiffies + 1;
4002 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4003 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4004 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4005 	q_vector->tx.next_update = jiffies + 1;
4006 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4007 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4008 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4009 
4010 	i40e_enable_misc_int_causes(pf);
4011 
4012 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4013 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4014 
4015 	/* Associate the queue pair to the vector and enable the queue
4016 	 * interrupt RX queue in linked list with next queue set to TX
4017 	 */
4018 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4019 
4020 	if (i40e_enabled_xdp_vsi(vsi)) {
4021 		/* TX queue in linked list with next queue set to TX */
4022 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4023 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4024 	}
4025 
4026 	/* last TX queue so the next RX queue doesn't matter */
4027 	wr32(hw, I40E_QINT_TQCTL(0),
4028 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4029 	i40e_flush(hw);
4030 }
4031 
4032 /**
4033  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4034  * @pf: board private structure
4035  **/
4036 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4037 {
4038 	struct i40e_hw *hw = &pf->hw;
4039 
4040 	wr32(hw, I40E_PFINT_DYN_CTL0,
4041 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4042 	i40e_flush(hw);
4043 }
4044 
4045 /**
4046  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4047  * @pf: board private structure
4048  **/
4049 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4050 {
4051 	struct i40e_hw *hw = &pf->hw;
4052 	u32 val;
4053 
4054 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4055 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4056 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4057 
4058 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4059 	i40e_flush(hw);
4060 }
4061 
4062 /**
4063  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4064  * @irq: interrupt number
4065  * @data: pointer to a q_vector
4066  **/
4067 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4068 {
4069 	struct i40e_q_vector *q_vector = data;
4070 
4071 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4072 		return IRQ_HANDLED;
4073 
4074 	napi_schedule_irqoff(&q_vector->napi);
4075 
4076 	return IRQ_HANDLED;
4077 }
4078 
4079 /**
4080  * i40e_irq_affinity_notify - Callback for affinity changes
4081  * @notify: context as to what irq was changed
4082  * @mask: the new affinity mask
4083  *
4084  * This is a callback function used by the irq_set_affinity_notifier function
4085  * so that we may register to receive changes to the irq affinity masks.
4086  **/
4087 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4088 				     const cpumask_t *mask)
4089 {
4090 	struct i40e_q_vector *q_vector =
4091 		container_of(notify, struct i40e_q_vector, affinity_notify);
4092 
4093 	cpumask_copy(&q_vector->affinity_mask, mask);
4094 }
4095 
4096 /**
4097  * i40e_irq_affinity_release - Callback for affinity notifier release
4098  * @ref: internal core kernel usage
4099  *
4100  * This is a callback function used by the irq_set_affinity_notifier function
4101  * to inform the current notification subscriber that they will no longer
4102  * receive notifications.
4103  **/
4104 static void i40e_irq_affinity_release(struct kref *ref) {}
4105 
4106 /**
4107  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4108  * @vsi: the VSI being configured
4109  * @basename: name for the vector
4110  *
4111  * Allocates MSI-X vectors and requests interrupts from the kernel.
4112  **/
4113 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4114 {
4115 	int q_vectors = vsi->num_q_vectors;
4116 	struct i40e_pf *pf = vsi->back;
4117 	int base = vsi->base_vector;
4118 	int rx_int_idx = 0;
4119 	int tx_int_idx = 0;
4120 	int vector, err;
4121 	int irq_num;
4122 	int cpu;
4123 
4124 	for (vector = 0; vector < q_vectors; vector++) {
4125 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4126 
4127 		irq_num = pf->msix_entries[base + vector].vector;
4128 
4129 		if (q_vector->tx.ring && q_vector->rx.ring) {
4130 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4131 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4132 			tx_int_idx++;
4133 		} else if (q_vector->rx.ring) {
4134 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4135 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4136 		} else if (q_vector->tx.ring) {
4137 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4138 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4139 		} else {
4140 			/* skip this unused q_vector */
4141 			continue;
4142 		}
4143 		err = request_irq(irq_num,
4144 				  vsi->irq_handler,
4145 				  0,
4146 				  q_vector->name,
4147 				  q_vector);
4148 		if (err) {
4149 			dev_info(&pf->pdev->dev,
4150 				 "MSIX request_irq failed, error: %d\n", err);
4151 			goto free_queue_irqs;
4152 		}
4153 
4154 		/* register for affinity change notifications */
4155 		q_vector->irq_num = irq_num;
4156 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4157 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4158 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4159 		/* Spread affinity hints out across online CPUs.
4160 		 *
4161 		 * get_cpu_mask returns a static constant mask with
4162 		 * a permanent lifetime so it's ok to pass to
4163 		 * irq_update_affinity_hint without making a copy.
4164 		 */
4165 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4166 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4167 	}
4168 
4169 	vsi->irqs_ready = true;
4170 	return 0;
4171 
4172 free_queue_irqs:
4173 	while (vector) {
4174 		vector--;
4175 		irq_num = pf->msix_entries[base + vector].vector;
4176 		irq_set_affinity_notifier(irq_num, NULL);
4177 		irq_update_affinity_hint(irq_num, NULL);
4178 		free_irq(irq_num, &vsi->q_vectors[vector]);
4179 	}
4180 	return err;
4181 }
4182 
4183 /**
4184  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4185  * @vsi: the VSI being un-configured
4186  **/
4187 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4188 {
4189 	struct i40e_pf *pf = vsi->back;
4190 	struct i40e_hw *hw = &pf->hw;
4191 	int base = vsi->base_vector;
4192 	int i;
4193 
4194 	/* disable interrupt causation from each queue */
4195 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4196 		u32 val;
4197 
4198 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4199 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4200 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4201 
4202 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4203 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4204 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4205 
4206 		if (!i40e_enabled_xdp_vsi(vsi))
4207 			continue;
4208 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4209 	}
4210 
4211 	/* disable each interrupt */
4212 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4213 		for (i = vsi->base_vector;
4214 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4215 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4216 
4217 		i40e_flush(hw);
4218 		for (i = 0; i < vsi->num_q_vectors; i++)
4219 			synchronize_irq(pf->msix_entries[i + base].vector);
4220 	} else {
4221 		/* Legacy and MSI mode - this stops all interrupt handling */
4222 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4223 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4224 		i40e_flush(hw);
4225 		synchronize_irq(pf->pdev->irq);
4226 	}
4227 }
4228 
4229 /**
4230  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4231  * @vsi: the VSI being configured
4232  **/
4233 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4234 {
4235 	struct i40e_pf *pf = vsi->back;
4236 	int i;
4237 
4238 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4239 		for (i = 0; i < vsi->num_q_vectors; i++)
4240 			i40e_irq_dynamic_enable(vsi, i);
4241 	} else {
4242 		i40e_irq_dynamic_enable_icr0(pf);
4243 	}
4244 
4245 	i40e_flush(&pf->hw);
4246 	return 0;
4247 }
4248 
4249 /**
4250  * i40e_free_misc_vector - Free the vector that handles non-queue events
4251  * @pf: board private structure
4252  **/
4253 static void i40e_free_misc_vector(struct i40e_pf *pf)
4254 {
4255 	/* Disable ICR 0 */
4256 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4257 	i40e_flush(&pf->hw);
4258 
4259 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4260 		free_irq(pf->msix_entries[0].vector, pf);
4261 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4262 	}
4263 }
4264 
4265 /**
4266  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4267  * @irq: interrupt number
4268  * @data: pointer to a q_vector
4269  *
4270  * This is the handler used for all MSI/Legacy interrupts, and deals
4271  * with both queue and non-queue interrupts.  This is also used in
4272  * MSIX mode to handle the non-queue interrupts.
4273  **/
4274 static irqreturn_t i40e_intr(int irq, void *data)
4275 {
4276 	struct i40e_pf *pf = (struct i40e_pf *)data;
4277 	struct i40e_hw *hw = &pf->hw;
4278 	irqreturn_t ret = IRQ_NONE;
4279 	u32 icr0, icr0_remaining;
4280 	u32 val, ena_mask;
4281 
4282 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4283 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4284 
4285 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4286 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4287 		goto enable_intr;
4288 
4289 	/* if interrupt but no bits showing, must be SWINT */
4290 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4291 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4292 		pf->sw_int_count++;
4293 
4294 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4295 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4296 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4297 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4298 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4299 	}
4300 
4301 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4302 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4303 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4304 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4305 
4306 		/* We do not have a way to disarm Queue causes while leaving
4307 		 * interrupt enabled for all other causes, ideally
4308 		 * interrupt should be disabled while we are in NAPI but
4309 		 * this is not a performance path and napi_schedule()
4310 		 * can deal with rescheduling.
4311 		 */
4312 		if (!test_bit(__I40E_DOWN, pf->state))
4313 			napi_schedule_irqoff(&q_vector->napi);
4314 	}
4315 
4316 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4317 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4318 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4319 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4320 	}
4321 
4322 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4323 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4324 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4325 	}
4326 
4327 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4328 		/* disable any further VFLR event notifications */
4329 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4330 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4331 
4332 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4333 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4334 		} else {
4335 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4336 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4337 		}
4338 	}
4339 
4340 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4341 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4342 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4343 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4344 		val = rd32(hw, I40E_GLGEN_RSTAT);
4345 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4346 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4347 		if (val == I40E_RESET_CORER) {
4348 			pf->corer_count++;
4349 		} else if (val == I40E_RESET_GLOBR) {
4350 			pf->globr_count++;
4351 		} else if (val == I40E_RESET_EMPR) {
4352 			pf->empr_count++;
4353 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4354 		}
4355 	}
4356 
4357 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4358 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4359 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4360 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4361 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4362 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4363 	}
4364 
4365 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4366 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4367 
4368 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4369 			schedule_work(&pf->ptp_extts0_work);
4370 
4371 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4372 			i40e_ptp_tx_hwtstamp(pf);
4373 
4374 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4375 	}
4376 
4377 	/* If a critical error is pending we have no choice but to reset the
4378 	 * device.
4379 	 * Report and mask out any remaining unexpected interrupts.
4380 	 */
4381 	icr0_remaining = icr0 & ena_mask;
4382 	if (icr0_remaining) {
4383 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4384 			 icr0_remaining);
4385 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4386 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4387 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4388 			dev_info(&pf->pdev->dev, "device will be reset\n");
4389 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4390 			i40e_service_event_schedule(pf);
4391 		}
4392 		ena_mask &= ~icr0_remaining;
4393 	}
4394 	ret = IRQ_HANDLED;
4395 
4396 enable_intr:
4397 	/* re-enable interrupt causes */
4398 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4399 	if (!test_bit(__I40E_DOWN, pf->state) ||
4400 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4401 		i40e_service_event_schedule(pf);
4402 		i40e_irq_dynamic_enable_icr0(pf);
4403 	}
4404 
4405 	return ret;
4406 }
4407 
4408 /**
4409  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4410  * @tx_ring:  tx ring to clean
4411  * @budget:   how many cleans we're allowed
4412  *
4413  * Returns true if there's any budget left (e.g. the clean is finished)
4414  **/
4415 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4416 {
4417 	struct i40e_vsi *vsi = tx_ring->vsi;
4418 	u16 i = tx_ring->next_to_clean;
4419 	struct i40e_tx_buffer *tx_buf;
4420 	struct i40e_tx_desc *tx_desc;
4421 
4422 	tx_buf = &tx_ring->tx_bi[i];
4423 	tx_desc = I40E_TX_DESC(tx_ring, i);
4424 	i -= tx_ring->count;
4425 
4426 	do {
4427 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4428 
4429 		/* if next_to_watch is not set then there is no work pending */
4430 		if (!eop_desc)
4431 			break;
4432 
4433 		/* prevent any other reads prior to eop_desc */
4434 		smp_rmb();
4435 
4436 		/* if the descriptor isn't done, no work yet to do */
4437 		if (!(eop_desc->cmd_type_offset_bsz &
4438 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4439 			break;
4440 
4441 		/* clear next_to_watch to prevent false hangs */
4442 		tx_buf->next_to_watch = NULL;
4443 
4444 		tx_desc->buffer_addr = 0;
4445 		tx_desc->cmd_type_offset_bsz = 0;
4446 		/* move past filter desc */
4447 		tx_buf++;
4448 		tx_desc++;
4449 		i++;
4450 		if (unlikely(!i)) {
4451 			i -= tx_ring->count;
4452 			tx_buf = tx_ring->tx_bi;
4453 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4454 		}
4455 		/* unmap skb header data */
4456 		dma_unmap_single(tx_ring->dev,
4457 				 dma_unmap_addr(tx_buf, dma),
4458 				 dma_unmap_len(tx_buf, len),
4459 				 DMA_TO_DEVICE);
4460 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4461 			kfree(tx_buf->raw_buf);
4462 
4463 		tx_buf->raw_buf = NULL;
4464 		tx_buf->tx_flags = 0;
4465 		tx_buf->next_to_watch = NULL;
4466 		dma_unmap_len_set(tx_buf, len, 0);
4467 		tx_desc->buffer_addr = 0;
4468 		tx_desc->cmd_type_offset_bsz = 0;
4469 
4470 		/* move us past the eop_desc for start of next FD desc */
4471 		tx_buf++;
4472 		tx_desc++;
4473 		i++;
4474 		if (unlikely(!i)) {
4475 			i -= tx_ring->count;
4476 			tx_buf = tx_ring->tx_bi;
4477 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4478 		}
4479 
4480 		/* update budget accounting */
4481 		budget--;
4482 	} while (likely(budget));
4483 
4484 	i += tx_ring->count;
4485 	tx_ring->next_to_clean = i;
4486 
4487 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4488 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4489 
4490 	return budget > 0;
4491 }
4492 
4493 /**
4494  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4495  * @irq: interrupt number
4496  * @data: pointer to a q_vector
4497  **/
4498 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4499 {
4500 	struct i40e_q_vector *q_vector = data;
4501 	struct i40e_vsi *vsi;
4502 
4503 	if (!q_vector->tx.ring)
4504 		return IRQ_HANDLED;
4505 
4506 	vsi = q_vector->tx.ring->vsi;
4507 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4508 
4509 	return IRQ_HANDLED;
4510 }
4511 
4512 /**
4513  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4514  * @vsi: the VSI being configured
4515  * @v_idx: vector index
4516  * @qp_idx: queue pair index
4517  **/
4518 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4519 {
4520 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4521 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4522 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4523 
4524 	tx_ring->q_vector = q_vector;
4525 	tx_ring->next = q_vector->tx.ring;
4526 	q_vector->tx.ring = tx_ring;
4527 	q_vector->tx.count++;
4528 
4529 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4530 	if (i40e_enabled_xdp_vsi(vsi)) {
4531 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4532 
4533 		xdp_ring->q_vector = q_vector;
4534 		xdp_ring->next = q_vector->tx.ring;
4535 		q_vector->tx.ring = xdp_ring;
4536 		q_vector->tx.count++;
4537 	}
4538 
4539 	rx_ring->q_vector = q_vector;
4540 	rx_ring->next = q_vector->rx.ring;
4541 	q_vector->rx.ring = rx_ring;
4542 	q_vector->rx.count++;
4543 }
4544 
4545 /**
4546  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4547  * @vsi: the VSI being configured
4548  *
4549  * This function maps descriptor rings to the queue-specific vectors
4550  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4551  * one vector per queue pair, but on a constrained vector budget, we
4552  * group the queue pairs as "efficiently" as possible.
4553  **/
4554 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4555 {
4556 	int qp_remaining = vsi->num_queue_pairs;
4557 	int q_vectors = vsi->num_q_vectors;
4558 	int num_ringpairs;
4559 	int v_start = 0;
4560 	int qp_idx = 0;
4561 
4562 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4563 	 * group them so there are multiple queues per vector.
4564 	 * It is also important to go through all the vectors available to be
4565 	 * sure that if we don't use all the vectors, that the remaining vectors
4566 	 * are cleared. This is especially important when decreasing the
4567 	 * number of queues in use.
4568 	 */
4569 	for (; v_start < q_vectors; v_start++) {
4570 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4571 
4572 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4573 
4574 		q_vector->num_ringpairs = num_ringpairs;
4575 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4576 
4577 		q_vector->rx.count = 0;
4578 		q_vector->tx.count = 0;
4579 		q_vector->rx.ring = NULL;
4580 		q_vector->tx.ring = NULL;
4581 
4582 		while (num_ringpairs--) {
4583 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4584 			qp_idx++;
4585 			qp_remaining--;
4586 		}
4587 	}
4588 }
4589 
4590 /**
4591  * i40e_vsi_request_irq - Request IRQ from the OS
4592  * @vsi: the VSI being configured
4593  * @basename: name for the vector
4594  **/
4595 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4596 {
4597 	struct i40e_pf *pf = vsi->back;
4598 	int err;
4599 
4600 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4601 		err = i40e_vsi_request_irq_msix(vsi, basename);
4602 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4603 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4604 				  pf->int_name, pf);
4605 	else
4606 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4607 				  pf->int_name, pf);
4608 
4609 	if (err)
4610 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4611 
4612 	return err;
4613 }
4614 
4615 #ifdef CONFIG_NET_POLL_CONTROLLER
4616 /**
4617  * i40e_netpoll - A Polling 'interrupt' handler
4618  * @netdev: network interface device structure
4619  *
4620  * This is used by netconsole to send skbs without having to re-enable
4621  * interrupts.  It's not called while the normal interrupt routine is executing.
4622  **/
4623 static void i40e_netpoll(struct net_device *netdev)
4624 {
4625 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4626 	struct i40e_vsi *vsi = np->vsi;
4627 	struct i40e_pf *pf = vsi->back;
4628 	int i;
4629 
4630 	/* if interface is down do nothing */
4631 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4632 		return;
4633 
4634 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4635 		for (i = 0; i < vsi->num_q_vectors; i++)
4636 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4637 	} else {
4638 		i40e_intr(pf->pdev->irq, netdev);
4639 	}
4640 }
4641 #endif
4642 
4643 #define I40E_QTX_ENA_WAIT_COUNT 50
4644 
4645 /**
4646  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4647  * @pf: the PF being configured
4648  * @pf_q: the PF queue
4649  * @enable: enable or disable state of the queue
4650  *
4651  * This routine will wait for the given Tx queue of the PF to reach the
4652  * enabled or disabled state.
4653  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4654  * multiple retries; else will return 0 in case of success.
4655  **/
4656 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4657 {
4658 	int i;
4659 	u32 tx_reg;
4660 
4661 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4662 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4663 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4664 			break;
4665 
4666 		usleep_range(10, 20);
4667 	}
4668 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4669 		return -ETIMEDOUT;
4670 
4671 	return 0;
4672 }
4673 
4674 /**
4675  * i40e_control_tx_q - Start or stop a particular Tx queue
4676  * @pf: the PF structure
4677  * @pf_q: the PF queue to configure
4678  * @enable: start or stop the queue
4679  *
4680  * This function enables or disables a single queue. Note that any delay
4681  * required after the operation is expected to be handled by the caller of
4682  * this function.
4683  **/
4684 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4685 {
4686 	struct i40e_hw *hw = &pf->hw;
4687 	u32 tx_reg;
4688 	int i;
4689 
4690 	/* warn the TX unit of coming changes */
4691 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4692 	if (!enable)
4693 		usleep_range(10, 20);
4694 
4695 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4696 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4697 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4698 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4699 			break;
4700 		usleep_range(1000, 2000);
4701 	}
4702 
4703 	/* Skip if the queue is already in the requested state */
4704 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4705 		return;
4706 
4707 	/* turn on/off the queue */
4708 	if (enable) {
4709 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4710 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4711 	} else {
4712 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4713 	}
4714 
4715 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4716 }
4717 
4718 /**
4719  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4720  * @seid: VSI SEID
4721  * @pf: the PF structure
4722  * @pf_q: the PF queue to configure
4723  * @is_xdp: true if the queue is used for XDP
4724  * @enable: start or stop the queue
4725  **/
4726 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4727 			   bool is_xdp, bool enable)
4728 {
4729 	int ret;
4730 
4731 	i40e_control_tx_q(pf, pf_q, enable);
4732 
4733 	/* wait for the change to finish */
4734 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4735 	if (ret) {
4736 		dev_info(&pf->pdev->dev,
4737 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4738 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4739 			 (enable ? "en" : "dis"));
4740 	}
4741 
4742 	return ret;
4743 }
4744 
4745 /**
4746  * i40e_vsi_enable_tx - Start a VSI's rings
4747  * @vsi: the VSI being configured
4748  **/
4749 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4750 {
4751 	struct i40e_pf *pf = vsi->back;
4752 	int i, pf_q, ret = 0;
4753 
4754 	pf_q = vsi->base_queue;
4755 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4756 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4757 					     pf_q,
4758 					     false /*is xdp*/, true);
4759 		if (ret)
4760 			break;
4761 
4762 		if (!i40e_enabled_xdp_vsi(vsi))
4763 			continue;
4764 
4765 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4766 					     pf_q + vsi->alloc_queue_pairs,
4767 					     true /*is xdp*/, true);
4768 		if (ret)
4769 			break;
4770 	}
4771 	return ret;
4772 }
4773 
4774 /**
4775  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4776  * @pf: the PF being configured
4777  * @pf_q: the PF queue
4778  * @enable: enable or disable state of the queue
4779  *
4780  * This routine will wait for the given Rx queue of the PF to reach the
4781  * enabled or disabled state.
4782  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4783  * multiple retries; else will return 0 in case of success.
4784  **/
4785 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4786 {
4787 	int i;
4788 	u32 rx_reg;
4789 
4790 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4791 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4792 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4793 			break;
4794 
4795 		usleep_range(10, 20);
4796 	}
4797 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4798 		return -ETIMEDOUT;
4799 
4800 	return 0;
4801 }
4802 
4803 /**
4804  * i40e_control_rx_q - Start or stop a particular Rx queue
4805  * @pf: the PF structure
4806  * @pf_q: the PF queue to configure
4807  * @enable: start or stop the queue
4808  *
4809  * This function enables or disables a single queue. Note that
4810  * any delay required after the operation is expected to be
4811  * handled by the caller of this function.
4812  **/
4813 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4814 {
4815 	struct i40e_hw *hw = &pf->hw;
4816 	u32 rx_reg;
4817 	int i;
4818 
4819 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4820 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4821 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4822 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4823 			break;
4824 		usleep_range(1000, 2000);
4825 	}
4826 
4827 	/* Skip if the queue is already in the requested state */
4828 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4829 		return;
4830 
4831 	/* turn on/off the queue */
4832 	if (enable)
4833 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4834 	else
4835 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4836 
4837 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4838 }
4839 
4840 /**
4841  * i40e_control_wait_rx_q
4842  * @pf: the PF structure
4843  * @pf_q: queue being configured
4844  * @enable: start or stop the rings
4845  *
4846  * This function enables or disables a single queue along with waiting
4847  * for the change to finish. The caller of this function should handle
4848  * the delays needed in the case of disabling queues.
4849  **/
4850 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4851 {
4852 	int ret = 0;
4853 
4854 	i40e_control_rx_q(pf, pf_q, enable);
4855 
4856 	/* wait for the change to finish */
4857 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4858 	if (ret)
4859 		return ret;
4860 
4861 	return ret;
4862 }
4863 
4864 /**
4865  * i40e_vsi_enable_rx - Start a VSI's rings
4866  * @vsi: the VSI being configured
4867  **/
4868 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4869 {
4870 	struct i40e_pf *pf = vsi->back;
4871 	int i, pf_q, ret = 0;
4872 
4873 	pf_q = vsi->base_queue;
4874 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4875 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4876 		if (ret) {
4877 			dev_info(&pf->pdev->dev,
4878 				 "VSI seid %d Rx ring %d enable timeout\n",
4879 				 vsi->seid, pf_q);
4880 			break;
4881 		}
4882 	}
4883 
4884 	return ret;
4885 }
4886 
4887 /**
4888  * i40e_vsi_start_rings - Start a VSI's rings
4889  * @vsi: the VSI being configured
4890  **/
4891 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4892 {
4893 	int ret = 0;
4894 
4895 	/* do rx first for enable and last for disable */
4896 	ret = i40e_vsi_enable_rx(vsi);
4897 	if (ret)
4898 		return ret;
4899 	ret = i40e_vsi_enable_tx(vsi);
4900 
4901 	return ret;
4902 }
4903 
4904 #define I40E_DISABLE_TX_GAP_MSEC	50
4905 
4906 /**
4907  * i40e_vsi_stop_rings - Stop a VSI's rings
4908  * @vsi: the VSI being configured
4909  **/
4910 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4911 {
4912 	struct i40e_pf *pf = vsi->back;
4913 	int pf_q, err, q_end;
4914 
4915 	/* When port TX is suspended, don't wait */
4916 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4917 		return i40e_vsi_stop_rings_no_wait(vsi);
4918 
4919 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4920 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4921 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4922 
4923 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4924 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4925 		if (err)
4926 			dev_info(&pf->pdev->dev,
4927 				 "VSI seid %d Rx ring %d disable timeout\n",
4928 				 vsi->seid, pf_q);
4929 	}
4930 
4931 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4932 	pf_q = vsi->base_queue;
4933 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4934 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4935 
4936 	i40e_vsi_wait_queues_disabled(vsi);
4937 }
4938 
4939 /**
4940  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4941  * @vsi: the VSI being shutdown
4942  *
4943  * This function stops all the rings for a VSI but does not delay to verify
4944  * that rings have been disabled. It is expected that the caller is shutting
4945  * down multiple VSIs at once and will delay together for all the VSIs after
4946  * initiating the shutdown. This is particularly useful for shutting down lots
4947  * of VFs together. Otherwise, a large delay can be incurred while configuring
4948  * each VSI in serial.
4949  **/
4950 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4951 {
4952 	struct i40e_pf *pf = vsi->back;
4953 	int i, pf_q;
4954 
4955 	pf_q = vsi->base_queue;
4956 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4957 		i40e_control_tx_q(pf, pf_q, false);
4958 		i40e_control_rx_q(pf, pf_q, false);
4959 	}
4960 }
4961 
4962 /**
4963  * i40e_vsi_free_irq - Free the irq association with the OS
4964  * @vsi: the VSI being configured
4965  **/
4966 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4967 {
4968 	struct i40e_pf *pf = vsi->back;
4969 	struct i40e_hw *hw = &pf->hw;
4970 	int base = vsi->base_vector;
4971 	u32 val, qp;
4972 	int i;
4973 
4974 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4975 		if (!vsi->q_vectors)
4976 			return;
4977 
4978 		if (!vsi->irqs_ready)
4979 			return;
4980 
4981 		vsi->irqs_ready = false;
4982 		for (i = 0; i < vsi->num_q_vectors; i++) {
4983 			int irq_num;
4984 			u16 vector;
4985 
4986 			vector = i + base;
4987 			irq_num = pf->msix_entries[vector].vector;
4988 
4989 			/* free only the irqs that were actually requested */
4990 			if (!vsi->q_vectors[i] ||
4991 			    !vsi->q_vectors[i]->num_ringpairs)
4992 				continue;
4993 
4994 			/* clear the affinity notifier in the IRQ descriptor */
4995 			irq_set_affinity_notifier(irq_num, NULL);
4996 			/* remove our suggested affinity mask for this IRQ */
4997 			irq_update_affinity_hint(irq_num, NULL);
4998 			free_irq(irq_num, vsi->q_vectors[i]);
4999 
5000 			/* Tear down the interrupt queue link list
5001 			 *
5002 			 * We know that they come in pairs and always
5003 			 * the Rx first, then the Tx.  To clear the
5004 			 * link list, stick the EOL value into the
5005 			 * next_q field of the registers.
5006 			 */
5007 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5008 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5009 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5010 			val |= I40E_QUEUE_END_OF_LIST
5011 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5012 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5013 
5014 			while (qp != I40E_QUEUE_END_OF_LIST) {
5015 				u32 next;
5016 
5017 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5018 
5019 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5020 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5021 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5022 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5023 
5024 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5025 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5026 
5027 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5028 
5029 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5030 
5031 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5032 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5033 
5034 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5035 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5036 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5037 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5038 
5039 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5040 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5041 
5042 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5043 				qp = next;
5044 			}
5045 		}
5046 	} else {
5047 		free_irq(pf->pdev->irq, pf);
5048 
5049 		val = rd32(hw, I40E_PFINT_LNKLST0);
5050 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5051 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5052 		val |= I40E_QUEUE_END_OF_LIST
5053 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5054 		wr32(hw, I40E_PFINT_LNKLST0, val);
5055 
5056 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5057 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5058 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5059 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5060 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5061 
5062 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5063 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5064 
5065 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5066 
5067 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5068 
5069 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5070 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5071 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5072 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5073 
5074 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5075 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5076 
5077 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5078 	}
5079 }
5080 
5081 /**
5082  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5083  * @vsi: the VSI being configured
5084  * @v_idx: Index of vector to be freed
5085  *
5086  * This function frees the memory allocated to the q_vector.  In addition if
5087  * NAPI is enabled it will delete any references to the NAPI struct prior
5088  * to freeing the q_vector.
5089  **/
5090 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5091 {
5092 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5093 	struct i40e_ring *ring;
5094 
5095 	if (!q_vector)
5096 		return;
5097 
5098 	/* disassociate q_vector from rings */
5099 	i40e_for_each_ring(ring, q_vector->tx)
5100 		ring->q_vector = NULL;
5101 
5102 	i40e_for_each_ring(ring, q_vector->rx)
5103 		ring->q_vector = NULL;
5104 
5105 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5106 	if (vsi->netdev)
5107 		netif_napi_del(&q_vector->napi);
5108 
5109 	vsi->q_vectors[v_idx] = NULL;
5110 
5111 	kfree_rcu(q_vector, rcu);
5112 }
5113 
5114 /**
5115  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5116  * @vsi: the VSI being un-configured
5117  *
5118  * This frees the memory allocated to the q_vectors and
5119  * deletes references to the NAPI struct.
5120  **/
5121 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5122 {
5123 	int v_idx;
5124 
5125 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5126 		i40e_free_q_vector(vsi, v_idx);
5127 }
5128 
5129 /**
5130  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5131  * @pf: board private structure
5132  **/
5133 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5134 {
5135 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5136 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5137 		pci_disable_msix(pf->pdev);
5138 		kfree(pf->msix_entries);
5139 		pf->msix_entries = NULL;
5140 		kfree(pf->irq_pile);
5141 		pf->irq_pile = NULL;
5142 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5143 		pci_disable_msi(pf->pdev);
5144 	}
5145 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5146 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5147 }
5148 
5149 /**
5150  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5151  * @pf: board private structure
5152  *
5153  * We go through and clear interrupt specific resources and reset the structure
5154  * to pre-load conditions
5155  **/
5156 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5157 {
5158 	int i;
5159 
5160 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5161 		i40e_free_misc_vector(pf);
5162 
5163 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5164 		      I40E_IWARP_IRQ_PILE_ID);
5165 
5166 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5167 	for (i = 0; i < pf->num_alloc_vsi; i++)
5168 		if (pf->vsi[i])
5169 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5170 	i40e_reset_interrupt_capability(pf);
5171 }
5172 
5173 /**
5174  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5175  * @vsi: the VSI being configured
5176  **/
5177 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5178 {
5179 	int q_idx;
5180 
5181 	if (!vsi->netdev)
5182 		return;
5183 
5184 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5185 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5186 
5187 		if (q_vector->rx.ring || q_vector->tx.ring)
5188 			napi_enable(&q_vector->napi);
5189 	}
5190 }
5191 
5192 /**
5193  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5194  * @vsi: the VSI being configured
5195  **/
5196 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5197 {
5198 	int q_idx;
5199 
5200 	if (!vsi->netdev)
5201 		return;
5202 
5203 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5204 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5205 
5206 		if (q_vector->rx.ring || q_vector->tx.ring)
5207 			napi_disable(&q_vector->napi);
5208 	}
5209 }
5210 
5211 /**
5212  * i40e_vsi_close - Shut down a VSI
5213  * @vsi: the vsi to be quelled
5214  **/
5215 static void i40e_vsi_close(struct i40e_vsi *vsi)
5216 {
5217 	struct i40e_pf *pf = vsi->back;
5218 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5219 		i40e_down(vsi);
5220 	i40e_vsi_free_irq(vsi);
5221 	i40e_vsi_free_tx_resources(vsi);
5222 	i40e_vsi_free_rx_resources(vsi);
5223 	vsi->current_netdev_flags = 0;
5224 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5225 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5226 		set_bit(__I40E_CLIENT_RESET, pf->state);
5227 }
5228 
5229 /**
5230  * i40e_quiesce_vsi - Pause a given VSI
5231  * @vsi: the VSI being paused
5232  **/
5233 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5234 {
5235 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5236 		return;
5237 
5238 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5239 	if (vsi->netdev && netif_running(vsi->netdev))
5240 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5241 	else
5242 		i40e_vsi_close(vsi);
5243 }
5244 
5245 /**
5246  * i40e_unquiesce_vsi - Resume a given VSI
5247  * @vsi: the VSI being resumed
5248  **/
5249 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5250 {
5251 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5252 		return;
5253 
5254 	if (vsi->netdev && netif_running(vsi->netdev))
5255 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5256 	else
5257 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5258 }
5259 
5260 /**
5261  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5262  * @pf: the PF
5263  **/
5264 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5265 {
5266 	int v;
5267 
5268 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5269 		if (pf->vsi[v])
5270 			i40e_quiesce_vsi(pf->vsi[v]);
5271 	}
5272 }
5273 
5274 /**
5275  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5276  * @pf: the PF
5277  **/
5278 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5279 {
5280 	int v;
5281 
5282 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5283 		if (pf->vsi[v])
5284 			i40e_unquiesce_vsi(pf->vsi[v]);
5285 	}
5286 }
5287 
5288 /**
5289  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5290  * @vsi: the VSI being configured
5291  *
5292  * Wait until all queues on a given VSI have been disabled.
5293  **/
5294 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5295 {
5296 	struct i40e_pf *pf = vsi->back;
5297 	int i, pf_q, ret;
5298 
5299 	pf_q = vsi->base_queue;
5300 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5301 		/* Check and wait for the Tx queue */
5302 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5303 		if (ret) {
5304 			dev_info(&pf->pdev->dev,
5305 				 "VSI seid %d Tx ring %d disable timeout\n",
5306 				 vsi->seid, pf_q);
5307 			return ret;
5308 		}
5309 
5310 		if (!i40e_enabled_xdp_vsi(vsi))
5311 			goto wait_rx;
5312 
5313 		/* Check and wait for the XDP Tx queue */
5314 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5315 				       false);
5316 		if (ret) {
5317 			dev_info(&pf->pdev->dev,
5318 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5319 				 vsi->seid, pf_q);
5320 			return ret;
5321 		}
5322 wait_rx:
5323 		/* Check and wait for the Rx queue */
5324 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5325 		if (ret) {
5326 			dev_info(&pf->pdev->dev,
5327 				 "VSI seid %d Rx ring %d disable timeout\n",
5328 				 vsi->seid, pf_q);
5329 			return ret;
5330 		}
5331 	}
5332 
5333 	return 0;
5334 }
5335 
5336 #ifdef CONFIG_I40E_DCB
5337 /**
5338  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5339  * @pf: the PF
5340  *
5341  * This function waits for the queues to be in disabled state for all the
5342  * VSIs that are managed by this PF.
5343  **/
5344 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5345 {
5346 	int v, ret = 0;
5347 
5348 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5349 		if (pf->vsi[v]) {
5350 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5351 			if (ret)
5352 				break;
5353 		}
5354 	}
5355 
5356 	return ret;
5357 }
5358 
5359 #endif
5360 
5361 /**
5362  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5363  * @pf: pointer to PF
5364  *
5365  * Get TC map for ISCSI PF type that will include iSCSI TC
5366  * and LAN TC.
5367  **/
5368 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5369 {
5370 	struct i40e_dcb_app_priority_table app;
5371 	struct i40e_hw *hw = &pf->hw;
5372 	u8 enabled_tc = 1; /* TC0 is always enabled */
5373 	u8 tc, i;
5374 	/* Get the iSCSI APP TLV */
5375 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5376 
5377 	for (i = 0; i < dcbcfg->numapps; i++) {
5378 		app = dcbcfg->app[i];
5379 		if (app.selector == I40E_APP_SEL_TCPIP &&
5380 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5381 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5382 			enabled_tc |= BIT(tc);
5383 			break;
5384 		}
5385 	}
5386 
5387 	return enabled_tc;
5388 }
5389 
5390 /**
5391  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5392  * @dcbcfg: the corresponding DCBx configuration structure
5393  *
5394  * Return the number of TCs from given DCBx configuration
5395  **/
5396 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5397 {
5398 	int i, tc_unused = 0;
5399 	u8 num_tc = 0;
5400 	u8 ret = 0;
5401 
5402 	/* Scan the ETS Config Priority Table to find
5403 	 * traffic class enabled for a given priority
5404 	 * and create a bitmask of enabled TCs
5405 	 */
5406 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5407 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5408 
5409 	/* Now scan the bitmask to check for
5410 	 * contiguous TCs starting with TC0
5411 	 */
5412 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5413 		if (num_tc & BIT(i)) {
5414 			if (!tc_unused) {
5415 				ret++;
5416 			} else {
5417 				pr_err("Non-contiguous TC - Disabling DCB\n");
5418 				return 1;
5419 			}
5420 		} else {
5421 			tc_unused = 1;
5422 		}
5423 	}
5424 
5425 	/* There is always at least TC0 */
5426 	if (!ret)
5427 		ret = 1;
5428 
5429 	return ret;
5430 }
5431 
5432 /**
5433  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5434  * @dcbcfg: the corresponding DCBx configuration structure
5435  *
5436  * Query the current DCB configuration and return the number of
5437  * traffic classes enabled from the given DCBX config
5438  **/
5439 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5440 {
5441 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5442 	u8 enabled_tc = 1;
5443 	u8 i;
5444 
5445 	for (i = 0; i < num_tc; i++)
5446 		enabled_tc |= BIT(i);
5447 
5448 	return enabled_tc;
5449 }
5450 
5451 /**
5452  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5453  * @pf: PF being queried
5454  *
5455  * Query the current MQPRIO configuration and return the number of
5456  * traffic classes enabled.
5457  **/
5458 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5459 {
5460 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5461 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5462 	u8 enabled_tc = 1, i;
5463 
5464 	for (i = 1; i < num_tc; i++)
5465 		enabled_tc |= BIT(i);
5466 	return enabled_tc;
5467 }
5468 
5469 /**
5470  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5471  * @pf: PF being queried
5472  *
5473  * Return number of traffic classes enabled for the given PF
5474  **/
5475 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5476 {
5477 	struct i40e_hw *hw = &pf->hw;
5478 	u8 i, enabled_tc = 1;
5479 	u8 num_tc = 0;
5480 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5481 
5482 	if (i40e_is_tc_mqprio_enabled(pf))
5483 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5484 
5485 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5486 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5487 		return 1;
5488 
5489 	/* SFP mode will be enabled for all TCs on port */
5490 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5491 		return i40e_dcb_get_num_tc(dcbcfg);
5492 
5493 	/* MFP mode return count of enabled TCs for this PF */
5494 	if (pf->hw.func_caps.iscsi)
5495 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5496 	else
5497 		return 1; /* Only TC0 */
5498 
5499 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5500 		if (enabled_tc & BIT(i))
5501 			num_tc++;
5502 	}
5503 	return num_tc;
5504 }
5505 
5506 /**
5507  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5508  * @pf: PF being queried
5509  *
5510  * Return a bitmap for enabled traffic classes for this PF.
5511  **/
5512 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5513 {
5514 	if (i40e_is_tc_mqprio_enabled(pf))
5515 		return i40e_mqprio_get_enabled_tc(pf);
5516 
5517 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5518 	 * default TC
5519 	 */
5520 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5521 		return I40E_DEFAULT_TRAFFIC_CLASS;
5522 
5523 	/* SFP mode we want PF to be enabled for all TCs */
5524 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5525 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5526 
5527 	/* MFP enabled and iSCSI PF type */
5528 	if (pf->hw.func_caps.iscsi)
5529 		return i40e_get_iscsi_tc_map(pf);
5530 	else
5531 		return I40E_DEFAULT_TRAFFIC_CLASS;
5532 }
5533 
5534 /**
5535  * i40e_vsi_get_bw_info - Query VSI BW Information
5536  * @vsi: the VSI being queried
5537  *
5538  * Returns 0 on success, negative value on failure
5539  **/
5540 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5541 {
5542 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5543 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5544 	struct i40e_pf *pf = vsi->back;
5545 	struct i40e_hw *hw = &pf->hw;
5546 	u32 tc_bw_max;
5547 	int ret;
5548 	int i;
5549 
5550 	/* Get the VSI level BW configuration */
5551 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5552 	if (ret) {
5553 		dev_info(&pf->pdev->dev,
5554 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5555 			 ERR_PTR(ret),
5556 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5557 		return -EINVAL;
5558 	}
5559 
5560 	/* Get the VSI level BW configuration per TC */
5561 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5562 					       NULL);
5563 	if (ret) {
5564 		dev_info(&pf->pdev->dev,
5565 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5566 			 ERR_PTR(ret),
5567 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5568 		return -EINVAL;
5569 	}
5570 
5571 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5572 		dev_info(&pf->pdev->dev,
5573 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5574 			 bw_config.tc_valid_bits,
5575 			 bw_ets_config.tc_valid_bits);
5576 		/* Still continuing */
5577 	}
5578 
5579 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5580 	vsi->bw_max_quanta = bw_config.max_bw;
5581 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5582 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5583 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5584 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5585 		vsi->bw_ets_limit_credits[i] =
5586 					le16_to_cpu(bw_ets_config.credits[i]);
5587 		/* 3 bits out of 4 for each TC */
5588 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5589 	}
5590 
5591 	return 0;
5592 }
5593 
5594 /**
5595  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5596  * @vsi: the VSI being configured
5597  * @enabled_tc: TC bitmap
5598  * @bw_share: BW shared credits per TC
5599  *
5600  * Returns 0 on success, negative value on failure
5601  **/
5602 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5603 				       u8 *bw_share)
5604 {
5605 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5606 	struct i40e_pf *pf = vsi->back;
5607 	int ret;
5608 	int i;
5609 
5610 	/* There is no need to reset BW when mqprio mode is on.  */
5611 	if (i40e_is_tc_mqprio_enabled(pf))
5612 		return 0;
5613 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5614 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5615 		if (ret)
5616 			dev_info(&pf->pdev->dev,
5617 				 "Failed to reset tx rate for vsi->seid %u\n",
5618 				 vsi->seid);
5619 		return ret;
5620 	}
5621 	memset(&bw_data, 0, sizeof(bw_data));
5622 	bw_data.tc_valid_bits = enabled_tc;
5623 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5624 		bw_data.tc_bw_credits[i] = bw_share[i];
5625 
5626 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5627 	if (ret) {
5628 		dev_info(&pf->pdev->dev,
5629 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5630 			 pf->hw.aq.asq_last_status);
5631 		return -EINVAL;
5632 	}
5633 
5634 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5635 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5636 
5637 	return 0;
5638 }
5639 
5640 /**
5641  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5642  * @vsi: the VSI being configured
5643  * @enabled_tc: TC map to be enabled
5644  *
5645  **/
5646 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5647 {
5648 	struct net_device *netdev = vsi->netdev;
5649 	struct i40e_pf *pf = vsi->back;
5650 	struct i40e_hw *hw = &pf->hw;
5651 	u8 netdev_tc = 0;
5652 	int i;
5653 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5654 
5655 	if (!netdev)
5656 		return;
5657 
5658 	if (!enabled_tc) {
5659 		netdev_reset_tc(netdev);
5660 		return;
5661 	}
5662 
5663 	/* Set up actual enabled TCs on the VSI */
5664 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5665 		return;
5666 
5667 	/* set per TC queues for the VSI */
5668 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5669 		/* Only set TC queues for enabled tcs
5670 		 *
5671 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5672 		 * enabled_tc bitmap would be 0x00001001; the driver
5673 		 * will set the numtc for netdev as 2 that will be
5674 		 * referenced by the netdev layer as TC 0 and 1.
5675 		 */
5676 		if (vsi->tc_config.enabled_tc & BIT(i))
5677 			netdev_set_tc_queue(netdev,
5678 					vsi->tc_config.tc_info[i].netdev_tc,
5679 					vsi->tc_config.tc_info[i].qcount,
5680 					vsi->tc_config.tc_info[i].qoffset);
5681 	}
5682 
5683 	if (i40e_is_tc_mqprio_enabled(pf))
5684 		return;
5685 
5686 	/* Assign UP2TC map for the VSI */
5687 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5688 		/* Get the actual TC# for the UP */
5689 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5690 		/* Get the mapped netdev TC# for the UP */
5691 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5692 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5693 	}
5694 }
5695 
5696 /**
5697  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5698  * @vsi: the VSI being configured
5699  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5700  **/
5701 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5702 				      struct i40e_vsi_context *ctxt)
5703 {
5704 	/* copy just the sections touched not the entire info
5705 	 * since not all sections are valid as returned by
5706 	 * update vsi params
5707 	 */
5708 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5709 	memcpy(&vsi->info.queue_mapping,
5710 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5711 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5712 	       sizeof(vsi->info.tc_mapping));
5713 }
5714 
5715 /**
5716  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5717  * @vsi: the VSI being reconfigured
5718  * @vsi_offset: offset from main VF VSI
5719  */
5720 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5721 {
5722 	struct i40e_vsi_context ctxt = {};
5723 	struct i40e_pf *pf;
5724 	struct i40e_hw *hw;
5725 	int ret;
5726 
5727 	if (!vsi)
5728 		return -EINVAL;
5729 	pf = vsi->back;
5730 	hw = &pf->hw;
5731 
5732 	ctxt.seid = vsi->seid;
5733 	ctxt.pf_num = hw->pf_id;
5734 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5735 	ctxt.uplink_seid = vsi->uplink_seid;
5736 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5737 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5738 	ctxt.info = vsi->info;
5739 
5740 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5741 				 false);
5742 	if (vsi->reconfig_rss) {
5743 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5744 				      vsi->num_queue_pairs);
5745 		ret = i40e_vsi_config_rss(vsi);
5746 		if (ret) {
5747 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5748 			return ret;
5749 		}
5750 		vsi->reconfig_rss = false;
5751 	}
5752 
5753 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5754 	if (ret) {
5755 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5756 			 ERR_PTR(ret),
5757 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5758 		return ret;
5759 	}
5760 	/* update the local VSI info with updated queue map */
5761 	i40e_vsi_update_queue_map(vsi, &ctxt);
5762 	vsi->info.valid_sections = 0;
5763 
5764 	return ret;
5765 }
5766 
5767 /**
5768  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5769  * @vsi: VSI to be configured
5770  * @enabled_tc: TC bitmap
5771  *
5772  * This configures a particular VSI for TCs that are mapped to the
5773  * given TC bitmap. It uses default bandwidth share for TCs across
5774  * VSIs to configure TC for a particular VSI.
5775  *
5776  * NOTE:
5777  * It is expected that the VSI queues have been quisced before calling
5778  * this function.
5779  **/
5780 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5781 {
5782 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5783 	struct i40e_pf *pf = vsi->back;
5784 	struct i40e_hw *hw = &pf->hw;
5785 	struct i40e_vsi_context ctxt;
5786 	int ret = 0;
5787 	int i;
5788 
5789 	/* Check if enabled_tc is same as existing or new TCs */
5790 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5791 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5792 		return ret;
5793 
5794 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5795 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5796 		if (enabled_tc & BIT(i))
5797 			bw_share[i] = 1;
5798 	}
5799 
5800 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5801 	if (ret) {
5802 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5803 
5804 		dev_info(&pf->pdev->dev,
5805 			 "Failed configuring TC map %d for VSI %d\n",
5806 			 enabled_tc, vsi->seid);
5807 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5808 						  &bw_config, NULL);
5809 		if (ret) {
5810 			dev_info(&pf->pdev->dev,
5811 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5812 				 ERR_PTR(ret),
5813 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5814 			goto out;
5815 		}
5816 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5817 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5818 
5819 			if (!valid_tc)
5820 				valid_tc = bw_config.tc_valid_bits;
5821 			/* Always enable TC0, no matter what */
5822 			valid_tc |= 1;
5823 			dev_info(&pf->pdev->dev,
5824 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5825 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5826 			enabled_tc = valid_tc;
5827 		}
5828 
5829 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5830 		if (ret) {
5831 			dev_err(&pf->pdev->dev,
5832 				"Unable to  configure TC map %d for VSI %d\n",
5833 				enabled_tc, vsi->seid);
5834 			goto out;
5835 		}
5836 	}
5837 
5838 	/* Update Queue Pairs Mapping for currently enabled UPs */
5839 	ctxt.seid = vsi->seid;
5840 	ctxt.pf_num = vsi->back->hw.pf_id;
5841 	ctxt.vf_num = 0;
5842 	ctxt.uplink_seid = vsi->uplink_seid;
5843 	ctxt.info = vsi->info;
5844 	if (i40e_is_tc_mqprio_enabled(pf)) {
5845 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5846 		if (ret)
5847 			goto out;
5848 	} else {
5849 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5850 	}
5851 
5852 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5853 	 * queues changed.
5854 	 */
5855 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5856 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5857 				      vsi->num_queue_pairs);
5858 		ret = i40e_vsi_config_rss(vsi);
5859 		if (ret) {
5860 			dev_info(&vsi->back->pdev->dev,
5861 				 "Failed to reconfig rss for num_queues\n");
5862 			return ret;
5863 		}
5864 		vsi->reconfig_rss = false;
5865 	}
5866 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5867 		ctxt.info.valid_sections |=
5868 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5869 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5870 	}
5871 
5872 	/* Update the VSI after updating the VSI queue-mapping
5873 	 * information
5874 	 */
5875 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5876 	if (ret) {
5877 		dev_info(&pf->pdev->dev,
5878 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5879 			 ERR_PTR(ret),
5880 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5881 		goto out;
5882 	}
5883 	/* update the local VSI info with updated queue map */
5884 	i40e_vsi_update_queue_map(vsi, &ctxt);
5885 	vsi->info.valid_sections = 0;
5886 
5887 	/* Update current VSI BW information */
5888 	ret = i40e_vsi_get_bw_info(vsi);
5889 	if (ret) {
5890 		dev_info(&pf->pdev->dev,
5891 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5892 			 ERR_PTR(ret),
5893 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5894 		goto out;
5895 	}
5896 
5897 	/* Update the netdev TC setup */
5898 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5899 out:
5900 	return ret;
5901 }
5902 
5903 /**
5904  * i40e_get_link_speed - Returns link speed for the interface
5905  * @vsi: VSI to be configured
5906  *
5907  **/
5908 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5909 {
5910 	struct i40e_pf *pf = vsi->back;
5911 
5912 	switch (pf->hw.phy.link_info.link_speed) {
5913 	case I40E_LINK_SPEED_40GB:
5914 		return 40000;
5915 	case I40E_LINK_SPEED_25GB:
5916 		return 25000;
5917 	case I40E_LINK_SPEED_20GB:
5918 		return 20000;
5919 	case I40E_LINK_SPEED_10GB:
5920 		return 10000;
5921 	case I40E_LINK_SPEED_1GB:
5922 		return 1000;
5923 	default:
5924 		return -EINVAL;
5925 	}
5926 }
5927 
5928 /**
5929  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5930  * @vsi: Pointer to vsi structure
5931  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5932  *
5933  * Helper function to convert units before send to set BW limit
5934  **/
5935 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5936 {
5937 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5938 		dev_warn(&vsi->back->pdev->dev,
5939 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5940 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5941 	} else {
5942 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5943 	}
5944 
5945 	return max_tx_rate;
5946 }
5947 
5948 /**
5949  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5950  * @vsi: VSI to be configured
5951  * @seid: seid of the channel/VSI
5952  * @max_tx_rate: max TX rate to be configured as BW limit
5953  *
5954  * Helper function to set BW limit for a given VSI
5955  **/
5956 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5957 {
5958 	struct i40e_pf *pf = vsi->back;
5959 	u64 credits = 0;
5960 	int speed = 0;
5961 	int ret = 0;
5962 
5963 	speed = i40e_get_link_speed(vsi);
5964 	if (max_tx_rate > speed) {
5965 		dev_err(&pf->pdev->dev,
5966 			"Invalid max tx rate %llu specified for VSI seid %d.",
5967 			max_tx_rate, seid);
5968 		return -EINVAL;
5969 	}
5970 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5971 		dev_warn(&pf->pdev->dev,
5972 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5973 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5974 	}
5975 
5976 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5977 	credits = max_tx_rate;
5978 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5979 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5980 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5981 	if (ret)
5982 		dev_err(&pf->pdev->dev,
5983 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5984 			max_tx_rate, seid, ERR_PTR(ret),
5985 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5986 	return ret;
5987 }
5988 
5989 /**
5990  * i40e_remove_queue_channels - Remove queue channels for the TCs
5991  * @vsi: VSI to be configured
5992  *
5993  * Remove queue channels for the TCs
5994  **/
5995 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5996 {
5997 	enum i40e_admin_queue_err last_aq_status;
5998 	struct i40e_cloud_filter *cfilter;
5999 	struct i40e_channel *ch, *ch_tmp;
6000 	struct i40e_pf *pf = vsi->back;
6001 	struct hlist_node *node;
6002 	int ret, i;
6003 
6004 	/* Reset rss size that was stored when reconfiguring rss for
6005 	 * channel VSIs with non-power-of-2 queue count.
6006 	 */
6007 	vsi->current_rss_size = 0;
6008 
6009 	/* perform cleanup for channels if they exist */
6010 	if (list_empty(&vsi->ch_list))
6011 		return;
6012 
6013 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6014 		struct i40e_vsi *p_vsi;
6015 
6016 		list_del(&ch->list);
6017 		p_vsi = ch->parent_vsi;
6018 		if (!p_vsi || !ch->initialized) {
6019 			kfree(ch);
6020 			continue;
6021 		}
6022 		/* Reset queue contexts */
6023 		for (i = 0; i < ch->num_queue_pairs; i++) {
6024 			struct i40e_ring *tx_ring, *rx_ring;
6025 			u16 pf_q;
6026 
6027 			pf_q = ch->base_queue + i;
6028 			tx_ring = vsi->tx_rings[pf_q];
6029 			tx_ring->ch = NULL;
6030 
6031 			rx_ring = vsi->rx_rings[pf_q];
6032 			rx_ring->ch = NULL;
6033 		}
6034 
6035 		/* Reset BW configured for this VSI via mqprio */
6036 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6037 		if (ret)
6038 			dev_info(&vsi->back->pdev->dev,
6039 				 "Failed to reset tx rate for ch->seid %u\n",
6040 				 ch->seid);
6041 
6042 		/* delete cloud filters associated with this channel */
6043 		hlist_for_each_entry_safe(cfilter, node,
6044 					  &pf->cloud_filter_list, cloud_node) {
6045 			if (cfilter->seid != ch->seid)
6046 				continue;
6047 
6048 			hash_del(&cfilter->cloud_node);
6049 			if (cfilter->dst_port)
6050 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6051 									cfilter,
6052 									false);
6053 			else
6054 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6055 								false);
6056 			last_aq_status = pf->hw.aq.asq_last_status;
6057 			if (ret)
6058 				dev_info(&pf->pdev->dev,
6059 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6060 					 ERR_PTR(ret),
6061 					 i40e_aq_str(&pf->hw, last_aq_status));
6062 			kfree(cfilter);
6063 		}
6064 
6065 		/* delete VSI from FW */
6066 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6067 					     NULL);
6068 		if (ret)
6069 			dev_err(&vsi->back->pdev->dev,
6070 				"unable to remove channel (%d) for parent VSI(%d)\n",
6071 				ch->seid, p_vsi->seid);
6072 		kfree(ch);
6073 	}
6074 	INIT_LIST_HEAD(&vsi->ch_list);
6075 }
6076 
6077 /**
6078  * i40e_get_max_queues_for_channel
6079  * @vsi: ptr to VSI to which channels are associated with
6080  *
6081  * Helper function which returns max value among the queue counts set on the
6082  * channels/TCs created.
6083  **/
6084 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6085 {
6086 	struct i40e_channel *ch, *ch_tmp;
6087 	int max = 0;
6088 
6089 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6090 		if (!ch->initialized)
6091 			continue;
6092 		if (ch->num_queue_pairs > max)
6093 			max = ch->num_queue_pairs;
6094 	}
6095 
6096 	return max;
6097 }
6098 
6099 /**
6100  * i40e_validate_num_queues - validate num_queues w.r.t channel
6101  * @pf: ptr to PF device
6102  * @num_queues: number of queues
6103  * @vsi: the parent VSI
6104  * @reconfig_rss: indicates should the RSS be reconfigured or not
6105  *
6106  * This function validates number of queues in the context of new channel
6107  * which is being established and determines if RSS should be reconfigured
6108  * or not for parent VSI.
6109  **/
6110 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6111 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6112 {
6113 	int max_ch_queues;
6114 
6115 	if (!reconfig_rss)
6116 		return -EINVAL;
6117 
6118 	*reconfig_rss = false;
6119 	if (vsi->current_rss_size) {
6120 		if (num_queues > vsi->current_rss_size) {
6121 			dev_dbg(&pf->pdev->dev,
6122 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6123 				num_queues, vsi->current_rss_size);
6124 			return -EINVAL;
6125 		} else if ((num_queues < vsi->current_rss_size) &&
6126 			   (!is_power_of_2(num_queues))) {
6127 			dev_dbg(&pf->pdev->dev,
6128 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6129 				num_queues, vsi->current_rss_size);
6130 			return -EINVAL;
6131 		}
6132 	}
6133 
6134 	if (!is_power_of_2(num_queues)) {
6135 		/* Find the max num_queues configured for channel if channel
6136 		 * exist.
6137 		 * if channel exist, then enforce 'num_queues' to be more than
6138 		 * max ever queues configured for channel.
6139 		 */
6140 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6141 		if (num_queues < max_ch_queues) {
6142 			dev_dbg(&pf->pdev->dev,
6143 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6144 				num_queues, max_ch_queues);
6145 			return -EINVAL;
6146 		}
6147 		*reconfig_rss = true;
6148 	}
6149 
6150 	return 0;
6151 }
6152 
6153 /**
6154  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6155  * @vsi: the VSI being setup
6156  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6157  *
6158  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6159  **/
6160 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6161 {
6162 	struct i40e_pf *pf = vsi->back;
6163 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6164 	struct i40e_hw *hw = &pf->hw;
6165 	int local_rss_size;
6166 	u8 *lut;
6167 	int ret;
6168 
6169 	if (!vsi->rss_size)
6170 		return -EINVAL;
6171 
6172 	if (rss_size > vsi->rss_size)
6173 		return -EINVAL;
6174 
6175 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6176 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6177 	if (!lut)
6178 		return -ENOMEM;
6179 
6180 	/* Ignoring user configured lut if there is one */
6181 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6182 
6183 	/* Use user configured hash key if there is one, otherwise
6184 	 * use default.
6185 	 */
6186 	if (vsi->rss_hkey_user)
6187 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6188 	else
6189 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6190 
6191 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6192 	if (ret) {
6193 		dev_info(&pf->pdev->dev,
6194 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6195 			 ERR_PTR(ret),
6196 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6197 		kfree(lut);
6198 		return ret;
6199 	}
6200 	kfree(lut);
6201 
6202 	/* Do the update w.r.t. storing rss_size */
6203 	if (!vsi->orig_rss_size)
6204 		vsi->orig_rss_size = vsi->rss_size;
6205 	vsi->current_rss_size = local_rss_size;
6206 
6207 	return ret;
6208 }
6209 
6210 /**
6211  * i40e_channel_setup_queue_map - Setup a channel queue map
6212  * @pf: ptr to PF device
6213  * @ctxt: VSI context structure
6214  * @ch: ptr to channel structure
6215  *
6216  * Setup queue map for a specific channel
6217  **/
6218 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6219 					 struct i40e_vsi_context *ctxt,
6220 					 struct i40e_channel *ch)
6221 {
6222 	u16 qcount, qmap, sections = 0;
6223 	u8 offset = 0;
6224 	int pow;
6225 
6226 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6227 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6228 
6229 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6230 	ch->num_queue_pairs = qcount;
6231 
6232 	/* find the next higher power-of-2 of num queue pairs */
6233 	pow = ilog2(qcount);
6234 	if (!is_power_of_2(qcount))
6235 		pow++;
6236 
6237 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6238 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6239 
6240 	/* Setup queue TC[0].qmap for given VSI context */
6241 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6242 
6243 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6244 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6245 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6246 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6247 }
6248 
6249 /**
6250  * i40e_add_channel - add a channel by adding VSI
6251  * @pf: ptr to PF device
6252  * @uplink_seid: underlying HW switching element (VEB) ID
6253  * @ch: ptr to channel structure
6254  *
6255  * Add a channel (VSI) using add_vsi and queue_map
6256  **/
6257 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6258 			    struct i40e_channel *ch)
6259 {
6260 	struct i40e_hw *hw = &pf->hw;
6261 	struct i40e_vsi_context ctxt;
6262 	u8 enabled_tc = 0x1; /* TC0 enabled */
6263 	int ret;
6264 
6265 	if (ch->type != I40E_VSI_VMDQ2) {
6266 		dev_info(&pf->pdev->dev,
6267 			 "add new vsi failed, ch->type %d\n", ch->type);
6268 		return -EINVAL;
6269 	}
6270 
6271 	memset(&ctxt, 0, sizeof(ctxt));
6272 	ctxt.pf_num = hw->pf_id;
6273 	ctxt.vf_num = 0;
6274 	ctxt.uplink_seid = uplink_seid;
6275 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6276 	if (ch->type == I40E_VSI_VMDQ2)
6277 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6278 
6279 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6280 		ctxt.info.valid_sections |=
6281 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6282 		ctxt.info.switch_id =
6283 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6284 	}
6285 
6286 	/* Set queue map for a given VSI context */
6287 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6288 
6289 	/* Now time to create VSI */
6290 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6291 	if (ret) {
6292 		dev_info(&pf->pdev->dev,
6293 			 "add new vsi failed, err %pe aq_err %s\n",
6294 			 ERR_PTR(ret),
6295 			 i40e_aq_str(&pf->hw,
6296 				     pf->hw.aq.asq_last_status));
6297 		return -ENOENT;
6298 	}
6299 
6300 	/* Success, update channel, set enabled_tc only if the channel
6301 	 * is not a macvlan
6302 	 */
6303 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6304 	ch->seid = ctxt.seid;
6305 	ch->vsi_number = ctxt.vsi_number;
6306 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6307 
6308 	/* copy just the sections touched not the entire info
6309 	 * since not all sections are valid as returned by
6310 	 * update vsi params
6311 	 */
6312 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6313 	memcpy(&ch->info.queue_mapping,
6314 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6315 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6316 	       sizeof(ctxt.info.tc_mapping));
6317 
6318 	return 0;
6319 }
6320 
6321 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6322 				  u8 *bw_share)
6323 {
6324 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6325 	int ret;
6326 	int i;
6327 
6328 	memset(&bw_data, 0, sizeof(bw_data));
6329 	bw_data.tc_valid_bits = ch->enabled_tc;
6330 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6331 		bw_data.tc_bw_credits[i] = bw_share[i];
6332 
6333 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6334 				       &bw_data, NULL);
6335 	if (ret) {
6336 		dev_info(&vsi->back->pdev->dev,
6337 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6338 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6339 		return -EINVAL;
6340 	}
6341 
6342 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6343 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6344 
6345 	return 0;
6346 }
6347 
6348 /**
6349  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6350  * @pf: ptr to PF device
6351  * @vsi: the VSI being setup
6352  * @ch: ptr to channel structure
6353  *
6354  * Configure TX rings associated with channel (VSI) since queues are being
6355  * from parent VSI.
6356  **/
6357 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6358 				       struct i40e_vsi *vsi,
6359 				       struct i40e_channel *ch)
6360 {
6361 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6362 	int ret;
6363 	int i;
6364 
6365 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6366 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6367 		if (ch->enabled_tc & BIT(i))
6368 			bw_share[i] = 1;
6369 	}
6370 
6371 	/* configure BW for new VSI */
6372 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6373 	if (ret) {
6374 		dev_info(&vsi->back->pdev->dev,
6375 			 "Failed configuring TC map %d for channel (seid %u)\n",
6376 			 ch->enabled_tc, ch->seid);
6377 		return ret;
6378 	}
6379 
6380 	for (i = 0; i < ch->num_queue_pairs; i++) {
6381 		struct i40e_ring *tx_ring, *rx_ring;
6382 		u16 pf_q;
6383 
6384 		pf_q = ch->base_queue + i;
6385 
6386 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6387 		 * context
6388 		 */
6389 		tx_ring = vsi->tx_rings[pf_q];
6390 		tx_ring->ch = ch;
6391 
6392 		/* Get the RX ring ptr */
6393 		rx_ring = vsi->rx_rings[pf_q];
6394 		rx_ring->ch = ch;
6395 	}
6396 
6397 	return 0;
6398 }
6399 
6400 /**
6401  * i40e_setup_hw_channel - setup new channel
6402  * @pf: ptr to PF device
6403  * @vsi: the VSI being setup
6404  * @ch: ptr to channel structure
6405  * @uplink_seid: underlying HW switching element (VEB) ID
6406  * @type: type of channel to be created (VMDq2/VF)
6407  *
6408  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6409  * and configures TX rings accordingly
6410  **/
6411 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6412 					struct i40e_vsi *vsi,
6413 					struct i40e_channel *ch,
6414 					u16 uplink_seid, u8 type)
6415 {
6416 	int ret;
6417 
6418 	ch->initialized = false;
6419 	ch->base_queue = vsi->next_base_queue;
6420 	ch->type = type;
6421 
6422 	/* Proceed with creation of channel (VMDq2) VSI */
6423 	ret = i40e_add_channel(pf, uplink_seid, ch);
6424 	if (ret) {
6425 		dev_info(&pf->pdev->dev,
6426 			 "failed to add_channel using uplink_seid %u\n",
6427 			 uplink_seid);
6428 		return ret;
6429 	}
6430 
6431 	/* Mark the successful creation of channel */
6432 	ch->initialized = true;
6433 
6434 	/* Reconfigure TX queues using QTX_CTL register */
6435 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6436 	if (ret) {
6437 		dev_info(&pf->pdev->dev,
6438 			 "failed to configure TX rings for channel %u\n",
6439 			 ch->seid);
6440 		return ret;
6441 	}
6442 
6443 	/* update 'next_base_queue' */
6444 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6445 	dev_dbg(&pf->pdev->dev,
6446 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6447 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6448 		ch->num_queue_pairs,
6449 		vsi->next_base_queue);
6450 	return ret;
6451 }
6452 
6453 /**
6454  * i40e_setup_channel - setup new channel using uplink element
6455  * @pf: ptr to PF device
6456  * @vsi: pointer to the VSI to set up the channel within
6457  * @ch: ptr to channel structure
6458  *
6459  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6460  * and uplink switching element (uplink_seid)
6461  **/
6462 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6463 			       struct i40e_channel *ch)
6464 {
6465 	u8 vsi_type;
6466 	u16 seid;
6467 	int ret;
6468 
6469 	if (vsi->type == I40E_VSI_MAIN) {
6470 		vsi_type = I40E_VSI_VMDQ2;
6471 	} else {
6472 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6473 			vsi->type);
6474 		return false;
6475 	}
6476 
6477 	/* underlying switching element */
6478 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6479 
6480 	/* create channel (VSI), configure TX rings */
6481 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6482 	if (ret) {
6483 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6484 		return false;
6485 	}
6486 
6487 	return ch->initialized ? true : false;
6488 }
6489 
6490 /**
6491  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6492  * @vsi: ptr to VSI which has PF backing
6493  *
6494  * Sets up switch mode correctly if it needs to be changed and perform
6495  * what are allowed modes.
6496  **/
6497 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6498 {
6499 	u8 mode;
6500 	struct i40e_pf *pf = vsi->back;
6501 	struct i40e_hw *hw = &pf->hw;
6502 	int ret;
6503 
6504 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6505 	if (ret)
6506 		return -EINVAL;
6507 
6508 	if (hw->dev_caps.switch_mode) {
6509 		/* if switch mode is set, support mode2 (non-tunneled for
6510 		 * cloud filter) for now
6511 		 */
6512 		u32 switch_mode = hw->dev_caps.switch_mode &
6513 				  I40E_SWITCH_MODE_MASK;
6514 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6515 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6516 				return 0;
6517 			dev_err(&pf->pdev->dev,
6518 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6519 				hw->dev_caps.switch_mode);
6520 			return -EINVAL;
6521 		}
6522 	}
6523 
6524 	/* Set Bit 7 to be valid */
6525 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6526 
6527 	/* Set L4type for TCP support */
6528 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6529 
6530 	/* Set cloud filter mode */
6531 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6532 
6533 	/* Prep mode field for set_switch_config */
6534 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6535 					pf->last_sw_conf_valid_flags,
6536 					mode, NULL);
6537 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6538 		dev_err(&pf->pdev->dev,
6539 			"couldn't set switch config bits, err %pe aq_err %s\n",
6540 			ERR_PTR(ret),
6541 			i40e_aq_str(hw,
6542 				    hw->aq.asq_last_status));
6543 
6544 	return ret;
6545 }
6546 
6547 /**
6548  * i40e_create_queue_channel - function to create channel
6549  * @vsi: VSI to be configured
6550  * @ch: ptr to channel (it contains channel specific params)
6551  *
6552  * This function creates channel (VSI) using num_queues specified by user,
6553  * reconfigs RSS if needed.
6554  **/
6555 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6556 			      struct i40e_channel *ch)
6557 {
6558 	struct i40e_pf *pf = vsi->back;
6559 	bool reconfig_rss;
6560 	int err;
6561 
6562 	if (!ch)
6563 		return -EINVAL;
6564 
6565 	if (!ch->num_queue_pairs) {
6566 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6567 			ch->num_queue_pairs);
6568 		return -EINVAL;
6569 	}
6570 
6571 	/* validate user requested num_queues for channel */
6572 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6573 				       &reconfig_rss);
6574 	if (err) {
6575 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6576 			 ch->num_queue_pairs);
6577 		return -EINVAL;
6578 	}
6579 
6580 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6581 	 * VSI to be added switch to VEB mode.
6582 	 */
6583 
6584 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6585 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6586 
6587 		if (vsi->type == I40E_VSI_MAIN) {
6588 			if (i40e_is_tc_mqprio_enabled(pf))
6589 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6590 			else
6591 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6592 		}
6593 		/* now onwards for main VSI, number of queues will be value
6594 		 * of TC0's queue count
6595 		 */
6596 	}
6597 
6598 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6599 	 * it should be more than num_queues
6600 	 */
6601 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6602 		dev_dbg(&pf->pdev->dev,
6603 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6604 			vsi->cnt_q_avail, ch->num_queue_pairs);
6605 		return -EINVAL;
6606 	}
6607 
6608 	/* reconfig_rss only if vsi type is MAIN_VSI */
6609 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6610 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6611 		if (err) {
6612 			dev_info(&pf->pdev->dev,
6613 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6614 				 ch->num_queue_pairs);
6615 			return -EINVAL;
6616 		}
6617 	}
6618 
6619 	if (!i40e_setup_channel(pf, vsi, ch)) {
6620 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6621 		return -EINVAL;
6622 	}
6623 
6624 	dev_info(&pf->pdev->dev,
6625 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6626 		 ch->seid, ch->num_queue_pairs);
6627 
6628 	/* configure VSI for BW limit */
6629 	if (ch->max_tx_rate) {
6630 		u64 credits = ch->max_tx_rate;
6631 
6632 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6633 			return -EINVAL;
6634 
6635 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6636 		dev_dbg(&pf->pdev->dev,
6637 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6638 			ch->max_tx_rate,
6639 			credits,
6640 			ch->seid);
6641 	}
6642 
6643 	/* in case of VF, this will be main SRIOV VSI */
6644 	ch->parent_vsi = vsi;
6645 
6646 	/* and update main_vsi's count for queue_available to use */
6647 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6648 
6649 	return 0;
6650 }
6651 
6652 /**
6653  * i40e_configure_queue_channels - Add queue channel for the given TCs
6654  * @vsi: VSI to be configured
6655  *
6656  * Configures queue channel mapping to the given TCs
6657  **/
6658 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6659 {
6660 	struct i40e_channel *ch;
6661 	u64 max_rate = 0;
6662 	int ret = 0, i;
6663 
6664 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6665 	vsi->tc_seid_map[0] = vsi->seid;
6666 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6667 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6668 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6669 			if (!ch) {
6670 				ret = -ENOMEM;
6671 				goto err_free;
6672 			}
6673 
6674 			INIT_LIST_HEAD(&ch->list);
6675 			ch->num_queue_pairs =
6676 				vsi->tc_config.tc_info[i].qcount;
6677 			ch->base_queue =
6678 				vsi->tc_config.tc_info[i].qoffset;
6679 
6680 			/* Bandwidth limit through tc interface is in bytes/s,
6681 			 * change to Mbit/s
6682 			 */
6683 			max_rate = vsi->mqprio_qopt.max_rate[i];
6684 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6685 			ch->max_tx_rate = max_rate;
6686 
6687 			list_add_tail(&ch->list, &vsi->ch_list);
6688 
6689 			ret = i40e_create_queue_channel(vsi, ch);
6690 			if (ret) {
6691 				dev_err(&vsi->back->pdev->dev,
6692 					"Failed creating queue channel with TC%d: queues %d\n",
6693 					i, ch->num_queue_pairs);
6694 				goto err_free;
6695 			}
6696 			vsi->tc_seid_map[i] = ch->seid;
6697 		}
6698 	}
6699 
6700 	/* reset to reconfigure TX queue contexts */
6701 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6702 	return ret;
6703 
6704 err_free:
6705 	i40e_remove_queue_channels(vsi);
6706 	return ret;
6707 }
6708 
6709 /**
6710  * i40e_veb_config_tc - Configure TCs for given VEB
6711  * @veb: given VEB
6712  * @enabled_tc: TC bitmap
6713  *
6714  * Configures given TC bitmap for VEB (switching) element
6715  **/
6716 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6717 {
6718 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6719 	struct i40e_pf *pf = veb->pf;
6720 	int ret = 0;
6721 	int i;
6722 
6723 	/* No TCs or already enabled TCs just return */
6724 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6725 		return ret;
6726 
6727 	bw_data.tc_valid_bits = enabled_tc;
6728 	/* bw_data.absolute_credits is not set (relative) */
6729 
6730 	/* Enable ETS TCs with equal BW Share for now */
6731 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6732 		if (enabled_tc & BIT(i))
6733 			bw_data.tc_bw_share_credits[i] = 1;
6734 	}
6735 
6736 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6737 						   &bw_data, NULL);
6738 	if (ret) {
6739 		dev_info(&pf->pdev->dev,
6740 			 "VEB bw config failed, err %pe aq_err %s\n",
6741 			 ERR_PTR(ret),
6742 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6743 		goto out;
6744 	}
6745 
6746 	/* Update the BW information */
6747 	ret = i40e_veb_get_bw_info(veb);
6748 	if (ret) {
6749 		dev_info(&pf->pdev->dev,
6750 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6751 			 ERR_PTR(ret),
6752 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6753 	}
6754 
6755 out:
6756 	return ret;
6757 }
6758 
6759 #ifdef CONFIG_I40E_DCB
6760 /**
6761  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6762  * @pf: PF struct
6763  *
6764  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6765  * the caller would've quiesce all the VSIs before calling
6766  * this function
6767  **/
6768 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6769 {
6770 	u8 tc_map = 0;
6771 	int ret;
6772 	u8 v;
6773 
6774 	/* Enable the TCs available on PF to all VEBs */
6775 	tc_map = i40e_pf_get_tc_map(pf);
6776 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6777 		return;
6778 
6779 	for (v = 0; v < I40E_MAX_VEB; v++) {
6780 		if (!pf->veb[v])
6781 			continue;
6782 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6783 		if (ret) {
6784 			dev_info(&pf->pdev->dev,
6785 				 "Failed configuring TC for VEB seid=%d\n",
6786 				 pf->veb[v]->seid);
6787 			/* Will try to configure as many components */
6788 		}
6789 	}
6790 
6791 	/* Update each VSI */
6792 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6793 		if (!pf->vsi[v])
6794 			continue;
6795 
6796 		/* - Enable all TCs for the LAN VSI
6797 		 * - For all others keep them at TC0 for now
6798 		 */
6799 		if (v == pf->lan_vsi)
6800 			tc_map = i40e_pf_get_tc_map(pf);
6801 		else
6802 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6803 
6804 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6805 		if (ret) {
6806 			dev_info(&pf->pdev->dev,
6807 				 "Failed configuring TC for VSI seid=%d\n",
6808 				 pf->vsi[v]->seid);
6809 			/* Will try to configure as many components */
6810 		} else {
6811 			/* Re-configure VSI vectors based on updated TC map */
6812 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6813 			if (pf->vsi[v]->netdev)
6814 				i40e_dcbnl_set_all(pf->vsi[v]);
6815 		}
6816 	}
6817 }
6818 
6819 /**
6820  * i40e_resume_port_tx - Resume port Tx
6821  * @pf: PF struct
6822  *
6823  * Resume a port's Tx and issue a PF reset in case of failure to
6824  * resume.
6825  **/
6826 static int i40e_resume_port_tx(struct i40e_pf *pf)
6827 {
6828 	struct i40e_hw *hw = &pf->hw;
6829 	int ret;
6830 
6831 	ret = i40e_aq_resume_port_tx(hw, NULL);
6832 	if (ret) {
6833 		dev_info(&pf->pdev->dev,
6834 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6835 			  ERR_PTR(ret),
6836 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6837 		/* Schedule PF reset to recover */
6838 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6839 		i40e_service_event_schedule(pf);
6840 	}
6841 
6842 	return ret;
6843 }
6844 
6845 /**
6846  * i40e_suspend_port_tx - Suspend port Tx
6847  * @pf: PF struct
6848  *
6849  * Suspend a port's Tx and issue a PF reset in case of failure.
6850  **/
6851 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6852 {
6853 	struct i40e_hw *hw = &pf->hw;
6854 	int ret;
6855 
6856 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6857 	if (ret) {
6858 		dev_info(&pf->pdev->dev,
6859 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6860 			 ERR_PTR(ret),
6861 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6862 		/* Schedule PF reset to recover */
6863 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6864 		i40e_service_event_schedule(pf);
6865 	}
6866 
6867 	return ret;
6868 }
6869 
6870 /**
6871  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6872  * @pf: PF being configured
6873  * @new_cfg: New DCBX configuration
6874  *
6875  * Program DCB settings into HW and reconfigure VEB/VSIs on
6876  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6877  **/
6878 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6879 				  struct i40e_dcbx_config *new_cfg)
6880 {
6881 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6882 	int ret;
6883 
6884 	/* Check if need reconfiguration */
6885 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6886 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6887 		return 0;
6888 	}
6889 
6890 	/* Config change disable all VSIs */
6891 	i40e_pf_quiesce_all_vsi(pf);
6892 
6893 	/* Copy the new config to the current config */
6894 	*old_cfg = *new_cfg;
6895 	old_cfg->etsrec = old_cfg->etscfg;
6896 	ret = i40e_set_dcb_config(&pf->hw);
6897 	if (ret) {
6898 		dev_info(&pf->pdev->dev,
6899 			 "Set DCB Config failed, err %pe aq_err %s\n",
6900 			 ERR_PTR(ret),
6901 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6902 		goto out;
6903 	}
6904 
6905 	/* Changes in configuration update VEB/VSI */
6906 	i40e_dcb_reconfigure(pf);
6907 out:
6908 	/* In case of reset do not try to resume anything */
6909 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6910 		/* Re-start the VSIs if disabled */
6911 		ret = i40e_resume_port_tx(pf);
6912 		/* In case of error no point in resuming VSIs */
6913 		if (ret)
6914 			goto err;
6915 		i40e_pf_unquiesce_all_vsi(pf);
6916 	}
6917 err:
6918 	return ret;
6919 }
6920 
6921 /**
6922  * i40e_hw_dcb_config - Program new DCBX settings into HW
6923  * @pf: PF being configured
6924  * @new_cfg: New DCBX configuration
6925  *
6926  * Program DCB settings into HW and reconfigure VEB/VSIs on
6927  * given PF
6928  **/
6929 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6930 {
6931 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6932 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6933 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6934 	struct i40e_dcbx_config *old_cfg;
6935 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6936 	struct i40e_rx_pb_config pb_cfg;
6937 	struct i40e_hw *hw = &pf->hw;
6938 	u8 num_ports = hw->num_ports;
6939 	bool need_reconfig;
6940 	int ret = -EINVAL;
6941 	u8 lltc_map = 0;
6942 	u8 tc_map = 0;
6943 	u8 new_numtc;
6944 	u8 i;
6945 
6946 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6947 	/* Un-pack information to Program ETS HW via shared API
6948 	 * numtc, tcmap
6949 	 * LLTC map
6950 	 * ETS/NON-ETS arbiter mode
6951 	 * max exponent (credit refills)
6952 	 * Total number of ports
6953 	 * PFC priority bit-map
6954 	 * Priority Table
6955 	 * BW % per TC
6956 	 * Arbiter mode between UPs sharing same TC
6957 	 * TSA table (ETS or non-ETS)
6958 	 * EEE enabled or not
6959 	 * MFS TC table
6960 	 */
6961 
6962 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6963 
6964 	memset(&ets_data, 0, sizeof(ets_data));
6965 	for (i = 0; i < new_numtc; i++) {
6966 		tc_map |= BIT(i);
6967 		switch (new_cfg->etscfg.tsatable[i]) {
6968 		case I40E_IEEE_TSA_ETS:
6969 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6970 			ets_data.tc_bw_share_credits[i] =
6971 					new_cfg->etscfg.tcbwtable[i];
6972 			break;
6973 		case I40E_IEEE_TSA_STRICT:
6974 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6975 			lltc_map |= BIT(i);
6976 			ets_data.tc_bw_share_credits[i] =
6977 					I40E_DCB_STRICT_PRIO_CREDITS;
6978 			break;
6979 		default:
6980 			/* Invalid TSA type */
6981 			need_reconfig = false;
6982 			goto out;
6983 		}
6984 	}
6985 
6986 	old_cfg = &hw->local_dcbx_config;
6987 	/* Check if need reconfiguration */
6988 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6989 
6990 	/* If needed, enable/disable frame tagging, disable all VSIs
6991 	 * and suspend port tx
6992 	 */
6993 	if (need_reconfig) {
6994 		/* Enable DCB tagging only when more than one TC */
6995 		if (new_numtc > 1)
6996 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
6997 		else
6998 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
6999 
7000 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7001 		/* Reconfiguration needed quiesce all VSIs */
7002 		i40e_pf_quiesce_all_vsi(pf);
7003 		ret = i40e_suspend_port_tx(pf);
7004 		if (ret)
7005 			goto err;
7006 	}
7007 
7008 	/* Configure Port ETS Tx Scheduler */
7009 	ets_data.tc_valid_bits = tc_map;
7010 	ets_data.tc_strict_priority_flags = lltc_map;
7011 	ret = i40e_aq_config_switch_comp_ets
7012 		(hw, pf->mac_seid, &ets_data,
7013 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7014 	if (ret) {
7015 		dev_info(&pf->pdev->dev,
7016 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7017 			 ERR_PTR(ret),
7018 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7019 		goto out;
7020 	}
7021 
7022 	/* Configure Rx ETS HW */
7023 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7024 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7025 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7026 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7027 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7028 				   lltc_map);
7029 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7030 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7031 				     prio_type);
7032 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7033 			       new_cfg->etscfg.prioritytable);
7034 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7035 
7036 	/* Configure Rx Packet Buffers in HW */
7037 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7038 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7039 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7040 	}
7041 
7042 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7043 					 false, new_cfg->pfc.pfcenable,
7044 					 mfs_tc, &pb_cfg);
7045 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7046 
7047 	/* Update the local Rx Packet buffer config */
7048 	pf->pb_cfg = pb_cfg;
7049 
7050 	/* Inform the FW about changes to DCB configuration */
7051 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7052 	if (ret) {
7053 		dev_info(&pf->pdev->dev,
7054 			 "DCB Updated failed, err %pe aq_err %s\n",
7055 			 ERR_PTR(ret),
7056 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7057 		goto out;
7058 	}
7059 
7060 	/* Update the port DCBx configuration */
7061 	*old_cfg = *new_cfg;
7062 
7063 	/* Changes in configuration update VEB/VSI */
7064 	i40e_dcb_reconfigure(pf);
7065 out:
7066 	/* Re-start the VSIs if disabled */
7067 	if (need_reconfig) {
7068 		ret = i40e_resume_port_tx(pf);
7069 
7070 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7071 		/* In case of error no point in resuming VSIs */
7072 		if (ret)
7073 			goto err;
7074 
7075 		/* Wait for the PF's queues to be disabled */
7076 		ret = i40e_pf_wait_queues_disabled(pf);
7077 		if (ret) {
7078 			/* Schedule PF reset to recover */
7079 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7080 			i40e_service_event_schedule(pf);
7081 			goto err;
7082 		} else {
7083 			i40e_pf_unquiesce_all_vsi(pf);
7084 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7085 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7086 		}
7087 		/* registers are set, lets apply */
7088 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7089 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7090 	}
7091 
7092 err:
7093 	return ret;
7094 }
7095 
7096 /**
7097  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7098  * @pf: PF being queried
7099  *
7100  * Set default DCB configuration in case DCB is to be done in SW.
7101  **/
7102 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7103 {
7104 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7105 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7106 	struct i40e_hw *hw = &pf->hw;
7107 	int err;
7108 
7109 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7110 		/* Update the local cached instance with TC0 ETS */
7111 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7112 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7113 		pf->tmp_cfg.etscfg.maxtcs = 0;
7114 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7115 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7116 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7117 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7118 		/* FW needs one App to configure HW */
7119 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7120 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7121 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7122 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7123 
7124 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7125 	}
7126 
7127 	memset(&ets_data, 0, sizeof(ets_data));
7128 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7129 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7130 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7131 
7132 	/* Enable ETS on the Physical port */
7133 	err = i40e_aq_config_switch_comp_ets
7134 		(hw, pf->mac_seid, &ets_data,
7135 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7136 	if (err) {
7137 		dev_info(&pf->pdev->dev,
7138 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7139 			 ERR_PTR(err),
7140 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7141 		err = -ENOENT;
7142 		goto out;
7143 	}
7144 
7145 	/* Update the local cached instance with TC0 ETS */
7146 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7147 	dcb_cfg->etscfg.cbs = 0;
7148 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7149 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7150 
7151 out:
7152 	return err;
7153 }
7154 
7155 /**
7156  * i40e_init_pf_dcb - Initialize DCB configuration
7157  * @pf: PF being configured
7158  *
7159  * Query the current DCB configuration and cache it
7160  * in the hardware structure
7161  **/
7162 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7163 {
7164 	struct i40e_hw *hw = &pf->hw;
7165 	int err;
7166 
7167 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7168 	 * Also do not enable DCBx if FW LLDP agent is disabled
7169 	 */
7170 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7171 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7172 		err = -EOPNOTSUPP;
7173 		goto out;
7174 	}
7175 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7176 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7177 		err = i40e_dcb_sw_default_config(pf);
7178 		if (err) {
7179 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7180 			goto out;
7181 		}
7182 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7183 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7184 			       DCB_CAP_DCBX_VER_IEEE;
7185 		/* at init capable but disabled */
7186 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7187 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7188 		goto out;
7189 	}
7190 	err = i40e_init_dcb(hw, true);
7191 	if (!err) {
7192 		/* Device/Function is not DCBX capable */
7193 		if ((!hw->func_caps.dcb) ||
7194 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7195 			dev_info(&pf->pdev->dev,
7196 				 "DCBX offload is not supported or is disabled for this PF.\n");
7197 		} else {
7198 			/* When status is not DISABLED then DCBX in FW */
7199 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7200 				       DCB_CAP_DCBX_VER_IEEE;
7201 
7202 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7203 			/* Enable DCB tagging only when more than one TC
7204 			 * or explicitly disable if only one TC
7205 			 */
7206 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7207 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7208 			else
7209 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7210 			dev_dbg(&pf->pdev->dev,
7211 				"DCBX offload is supported for this PF.\n");
7212 		}
7213 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7214 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7215 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7216 	} else {
7217 		dev_info(&pf->pdev->dev,
7218 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7219 			 ERR_PTR(err),
7220 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7221 	}
7222 
7223 out:
7224 	return err;
7225 }
7226 #endif /* CONFIG_I40E_DCB */
7227 
7228 /**
7229  * i40e_print_link_message - print link up or down
7230  * @vsi: the VSI for which link needs a message
7231  * @isup: true of link is up, false otherwise
7232  */
7233 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7234 {
7235 	enum i40e_aq_link_speed new_speed;
7236 	struct i40e_pf *pf = vsi->back;
7237 	char *speed = "Unknown";
7238 	char *fc = "Unknown";
7239 	char *fec = "";
7240 	char *req_fec = "";
7241 	char *an = "";
7242 
7243 	if (isup)
7244 		new_speed = pf->hw.phy.link_info.link_speed;
7245 	else
7246 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7247 
7248 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7249 		return;
7250 	vsi->current_isup = isup;
7251 	vsi->current_speed = new_speed;
7252 	if (!isup) {
7253 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7254 		return;
7255 	}
7256 
7257 	/* Warn user if link speed on NPAR enabled partition is not at
7258 	 * least 10GB
7259 	 */
7260 	if (pf->hw.func_caps.npar_enable &&
7261 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7262 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7263 		netdev_warn(vsi->netdev,
7264 			    "The partition detected link speed that is less than 10Gbps\n");
7265 
7266 	switch (pf->hw.phy.link_info.link_speed) {
7267 	case I40E_LINK_SPEED_40GB:
7268 		speed = "40 G";
7269 		break;
7270 	case I40E_LINK_SPEED_20GB:
7271 		speed = "20 G";
7272 		break;
7273 	case I40E_LINK_SPEED_25GB:
7274 		speed = "25 G";
7275 		break;
7276 	case I40E_LINK_SPEED_10GB:
7277 		speed = "10 G";
7278 		break;
7279 	case I40E_LINK_SPEED_5GB:
7280 		speed = "5 G";
7281 		break;
7282 	case I40E_LINK_SPEED_2_5GB:
7283 		speed = "2.5 G";
7284 		break;
7285 	case I40E_LINK_SPEED_1GB:
7286 		speed = "1000 M";
7287 		break;
7288 	case I40E_LINK_SPEED_100MB:
7289 		speed = "100 M";
7290 		break;
7291 	default:
7292 		break;
7293 	}
7294 
7295 	switch (pf->hw.fc.current_mode) {
7296 	case I40E_FC_FULL:
7297 		fc = "RX/TX";
7298 		break;
7299 	case I40E_FC_TX_PAUSE:
7300 		fc = "TX";
7301 		break;
7302 	case I40E_FC_RX_PAUSE:
7303 		fc = "RX";
7304 		break;
7305 	default:
7306 		fc = "None";
7307 		break;
7308 	}
7309 
7310 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7311 		req_fec = "None";
7312 		fec = "None";
7313 		an = "False";
7314 
7315 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7316 			an = "True";
7317 
7318 		if (pf->hw.phy.link_info.fec_info &
7319 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7320 			fec = "CL74 FC-FEC/BASE-R";
7321 		else if (pf->hw.phy.link_info.fec_info &
7322 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7323 			fec = "CL108 RS-FEC";
7324 
7325 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7326 		 * both RS and FC are requested
7327 		 */
7328 		if (vsi->back->hw.phy.link_info.req_fec_info &
7329 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7330 			if (vsi->back->hw.phy.link_info.req_fec_info &
7331 			    I40E_AQ_REQUEST_FEC_RS)
7332 				req_fec = "CL108 RS-FEC";
7333 			else
7334 				req_fec = "CL74 FC-FEC/BASE-R";
7335 		}
7336 		netdev_info(vsi->netdev,
7337 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7338 			    speed, req_fec, fec, an, fc);
7339 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7340 		req_fec = "None";
7341 		fec = "None";
7342 		an = "False";
7343 
7344 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7345 			an = "True";
7346 
7347 		if (pf->hw.phy.link_info.fec_info &
7348 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7349 			fec = "CL74 FC-FEC/BASE-R";
7350 
7351 		if (pf->hw.phy.link_info.req_fec_info &
7352 		    I40E_AQ_REQUEST_FEC_KR)
7353 			req_fec = "CL74 FC-FEC/BASE-R";
7354 
7355 		netdev_info(vsi->netdev,
7356 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7357 			    speed, req_fec, fec, an, fc);
7358 	} else {
7359 		netdev_info(vsi->netdev,
7360 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7361 			    speed, fc);
7362 	}
7363 
7364 }
7365 
7366 /**
7367  * i40e_up_complete - Finish the last steps of bringing up a connection
7368  * @vsi: the VSI being configured
7369  **/
7370 static int i40e_up_complete(struct i40e_vsi *vsi)
7371 {
7372 	struct i40e_pf *pf = vsi->back;
7373 	int err;
7374 
7375 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7376 		i40e_vsi_configure_msix(vsi);
7377 	else
7378 		i40e_configure_msi_and_legacy(vsi);
7379 
7380 	/* start rings */
7381 	err = i40e_vsi_start_rings(vsi);
7382 	if (err)
7383 		return err;
7384 
7385 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7386 	i40e_napi_enable_all(vsi);
7387 	i40e_vsi_enable_irq(vsi);
7388 
7389 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7390 	    (vsi->netdev)) {
7391 		i40e_print_link_message(vsi, true);
7392 		netif_tx_start_all_queues(vsi->netdev);
7393 		netif_carrier_on(vsi->netdev);
7394 	}
7395 
7396 	/* replay FDIR SB filters */
7397 	if (vsi->type == I40E_VSI_FDIR) {
7398 		/* reset fd counters */
7399 		pf->fd_add_err = 0;
7400 		pf->fd_atr_cnt = 0;
7401 		i40e_fdir_filter_restore(vsi);
7402 	}
7403 
7404 	/* On the next run of the service_task, notify any clients of the new
7405 	 * opened netdev
7406 	 */
7407 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7408 	i40e_service_event_schedule(pf);
7409 
7410 	return 0;
7411 }
7412 
7413 /**
7414  * i40e_vsi_reinit_locked - Reset the VSI
7415  * @vsi: the VSI being configured
7416  *
7417  * Rebuild the ring structs after some configuration
7418  * has changed, e.g. MTU size.
7419  **/
7420 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7421 {
7422 	struct i40e_pf *pf = vsi->back;
7423 
7424 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7425 		usleep_range(1000, 2000);
7426 	i40e_down(vsi);
7427 
7428 	i40e_up(vsi);
7429 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7430 }
7431 
7432 /**
7433  * i40e_force_link_state - Force the link status
7434  * @pf: board private structure
7435  * @is_up: whether the link state should be forced up or down
7436  **/
7437 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7438 {
7439 	struct i40e_aq_get_phy_abilities_resp abilities;
7440 	struct i40e_aq_set_phy_config config = {0};
7441 	bool non_zero_phy_type = is_up;
7442 	struct i40e_hw *hw = &pf->hw;
7443 	u64 mask;
7444 	u8 speed;
7445 	int err;
7446 
7447 	/* Card might've been put in an unstable state by other drivers
7448 	 * and applications, which causes incorrect speed values being
7449 	 * set on startup. In order to clear speed registers, we call
7450 	 * get_phy_capabilities twice, once to get initial state of
7451 	 * available speeds, and once to get current PHY config.
7452 	 */
7453 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7454 					   NULL);
7455 	if (err) {
7456 		dev_err(&pf->pdev->dev,
7457 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7458 			ERR_PTR(err),
7459 			i40e_aq_str(hw, hw->aq.asq_last_status));
7460 		return err;
7461 	}
7462 	speed = abilities.link_speed;
7463 
7464 	/* Get the current phy config */
7465 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7466 					   NULL);
7467 	if (err) {
7468 		dev_err(&pf->pdev->dev,
7469 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7470 			ERR_PTR(err),
7471 			i40e_aq_str(hw, hw->aq.asq_last_status));
7472 		return err;
7473 	}
7474 
7475 	/* If link needs to go up, but was not forced to go down,
7476 	 * and its speed values are OK, no need for a flap
7477 	 * if non_zero_phy_type was set, still need to force up
7478 	 */
7479 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7480 		non_zero_phy_type = true;
7481 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7482 		return 0;
7483 
7484 	/* To force link we need to set bits for all supported PHY types,
7485 	 * but there are now more than 32, so we need to split the bitmap
7486 	 * across two fields.
7487 	 */
7488 	mask = I40E_PHY_TYPES_BITMASK;
7489 	config.phy_type =
7490 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7491 	config.phy_type_ext =
7492 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7493 	/* Copy the old settings, except of phy_type */
7494 	config.abilities = abilities.abilities;
7495 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7496 		if (is_up)
7497 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7498 		else
7499 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7500 	}
7501 	if (abilities.link_speed != 0)
7502 		config.link_speed = abilities.link_speed;
7503 	else
7504 		config.link_speed = speed;
7505 	config.eee_capability = abilities.eee_capability;
7506 	config.eeer = abilities.eeer_val;
7507 	config.low_power_ctrl = abilities.d3_lpan;
7508 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7509 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7510 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7511 
7512 	if (err) {
7513 		dev_err(&pf->pdev->dev,
7514 			"set phy config ret =  %pe last_status =  %s\n",
7515 			ERR_PTR(err),
7516 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7517 		return err;
7518 	}
7519 
7520 	/* Update the link info */
7521 	err = i40e_update_link_info(hw);
7522 	if (err) {
7523 		/* Wait a little bit (on 40G cards it sometimes takes a really
7524 		 * long time for link to come back from the atomic reset)
7525 		 * and try once more
7526 		 */
7527 		msleep(1000);
7528 		i40e_update_link_info(hw);
7529 	}
7530 
7531 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7532 
7533 	return 0;
7534 }
7535 
7536 /**
7537  * i40e_up - Bring the connection back up after being down
7538  * @vsi: the VSI being configured
7539  **/
7540 int i40e_up(struct i40e_vsi *vsi)
7541 {
7542 	int err;
7543 
7544 	if (vsi->type == I40E_VSI_MAIN &&
7545 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7546 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7547 		i40e_force_link_state(vsi->back, true);
7548 
7549 	err = i40e_vsi_configure(vsi);
7550 	if (!err)
7551 		err = i40e_up_complete(vsi);
7552 
7553 	return err;
7554 }
7555 
7556 /**
7557  * i40e_down - Shutdown the connection processing
7558  * @vsi: the VSI being stopped
7559  **/
7560 void i40e_down(struct i40e_vsi *vsi)
7561 {
7562 	int i;
7563 
7564 	/* It is assumed that the caller of this function
7565 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7566 	 */
7567 	if (vsi->netdev) {
7568 		netif_carrier_off(vsi->netdev);
7569 		netif_tx_disable(vsi->netdev);
7570 	}
7571 	i40e_vsi_disable_irq(vsi);
7572 	i40e_vsi_stop_rings(vsi);
7573 	if (vsi->type == I40E_VSI_MAIN &&
7574 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7575 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7576 		i40e_force_link_state(vsi->back, false);
7577 	i40e_napi_disable_all(vsi);
7578 
7579 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7580 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7581 		if (i40e_enabled_xdp_vsi(vsi)) {
7582 			/* Make sure that in-progress ndo_xdp_xmit and
7583 			 * ndo_xsk_wakeup calls are completed.
7584 			 */
7585 			synchronize_rcu();
7586 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7587 		}
7588 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7589 	}
7590 
7591 }
7592 
7593 /**
7594  * i40e_validate_mqprio_qopt- validate queue mapping info
7595  * @vsi: the VSI being configured
7596  * @mqprio_qopt: queue parametrs
7597  **/
7598 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7599 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7600 {
7601 	u64 sum_max_rate = 0;
7602 	u64 max_rate = 0;
7603 	int i;
7604 
7605 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7606 	    mqprio_qopt->qopt.num_tc < 1 ||
7607 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7608 		return -EINVAL;
7609 	for (i = 0; ; i++) {
7610 		if (!mqprio_qopt->qopt.count[i])
7611 			return -EINVAL;
7612 		if (mqprio_qopt->min_rate[i]) {
7613 			dev_err(&vsi->back->pdev->dev,
7614 				"Invalid min tx rate (greater than 0) specified\n");
7615 			return -EINVAL;
7616 		}
7617 		max_rate = mqprio_qopt->max_rate[i];
7618 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7619 		sum_max_rate += max_rate;
7620 
7621 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7622 			break;
7623 		if (mqprio_qopt->qopt.offset[i + 1] !=
7624 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7625 			return -EINVAL;
7626 	}
7627 	if (vsi->num_queue_pairs <
7628 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7629 		dev_err(&vsi->back->pdev->dev,
7630 			"Failed to create traffic channel, insufficient number of queues.\n");
7631 		return -EINVAL;
7632 	}
7633 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7634 		dev_err(&vsi->back->pdev->dev,
7635 			"Invalid max tx rate specified\n");
7636 		return -EINVAL;
7637 	}
7638 	return 0;
7639 }
7640 
7641 /**
7642  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7643  * @vsi: the VSI being configured
7644  **/
7645 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7646 {
7647 	u16 qcount;
7648 	int i;
7649 
7650 	/* Only TC0 is enabled */
7651 	vsi->tc_config.numtc = 1;
7652 	vsi->tc_config.enabled_tc = 1;
7653 	qcount = min_t(int, vsi->alloc_queue_pairs,
7654 		       i40e_pf_get_max_q_per_tc(vsi->back));
7655 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7656 		/* For the TC that is not enabled set the offset to default
7657 		 * queue and allocate one queue for the given TC.
7658 		 */
7659 		vsi->tc_config.tc_info[i].qoffset = 0;
7660 		if (i == 0)
7661 			vsi->tc_config.tc_info[i].qcount = qcount;
7662 		else
7663 			vsi->tc_config.tc_info[i].qcount = 1;
7664 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7665 	}
7666 }
7667 
7668 /**
7669  * i40e_del_macvlan_filter
7670  * @hw: pointer to the HW structure
7671  * @seid: seid of the channel VSI
7672  * @macaddr: the mac address to apply as a filter
7673  * @aq_err: store the admin Q error
7674  *
7675  * This function deletes a mac filter on the channel VSI which serves as the
7676  * macvlan. Returns 0 on success.
7677  **/
7678 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7679 				   const u8 *macaddr, int *aq_err)
7680 {
7681 	struct i40e_aqc_remove_macvlan_element_data element;
7682 	int status;
7683 
7684 	memset(&element, 0, sizeof(element));
7685 	ether_addr_copy(element.mac_addr, macaddr);
7686 	element.vlan_tag = 0;
7687 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7688 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7689 	*aq_err = hw->aq.asq_last_status;
7690 
7691 	return status;
7692 }
7693 
7694 /**
7695  * i40e_add_macvlan_filter
7696  * @hw: pointer to the HW structure
7697  * @seid: seid of the channel VSI
7698  * @macaddr: the mac address to apply as a filter
7699  * @aq_err: store the admin Q error
7700  *
7701  * This function adds a mac filter on the channel VSI which serves as the
7702  * macvlan. Returns 0 on success.
7703  **/
7704 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7705 				   const u8 *macaddr, int *aq_err)
7706 {
7707 	struct i40e_aqc_add_macvlan_element_data element;
7708 	u16 cmd_flags = 0;
7709 	int status;
7710 
7711 	ether_addr_copy(element.mac_addr, macaddr);
7712 	element.vlan_tag = 0;
7713 	element.queue_number = 0;
7714 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7715 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7716 	element.flags = cpu_to_le16(cmd_flags);
7717 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7718 	*aq_err = hw->aq.asq_last_status;
7719 
7720 	return status;
7721 }
7722 
7723 /**
7724  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7725  * @vsi: the VSI we want to access
7726  * @ch: the channel we want to access
7727  */
7728 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7729 {
7730 	struct i40e_ring *tx_ring, *rx_ring;
7731 	u16 pf_q;
7732 	int i;
7733 
7734 	for (i = 0; i < ch->num_queue_pairs; i++) {
7735 		pf_q = ch->base_queue + i;
7736 		tx_ring = vsi->tx_rings[pf_q];
7737 		tx_ring->ch = NULL;
7738 		rx_ring = vsi->rx_rings[pf_q];
7739 		rx_ring->ch = NULL;
7740 	}
7741 }
7742 
7743 /**
7744  * i40e_free_macvlan_channels
7745  * @vsi: the VSI we want to access
7746  *
7747  * This function frees the Qs of the channel VSI from
7748  * the stack and also deletes the channel VSIs which
7749  * serve as macvlans.
7750  */
7751 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7752 {
7753 	struct i40e_channel *ch, *ch_tmp;
7754 	int ret;
7755 
7756 	if (list_empty(&vsi->macvlan_list))
7757 		return;
7758 
7759 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7760 		struct i40e_vsi *parent_vsi;
7761 
7762 		if (i40e_is_channel_macvlan(ch)) {
7763 			i40e_reset_ch_rings(vsi, ch);
7764 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7765 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7766 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7767 			kfree(ch->fwd);
7768 			ch->fwd = NULL;
7769 		}
7770 
7771 		list_del(&ch->list);
7772 		parent_vsi = ch->parent_vsi;
7773 		if (!parent_vsi || !ch->initialized) {
7774 			kfree(ch);
7775 			continue;
7776 		}
7777 
7778 		/* remove the VSI */
7779 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7780 					     NULL);
7781 		if (ret)
7782 			dev_err(&vsi->back->pdev->dev,
7783 				"unable to remove channel (%d) for parent VSI(%d)\n",
7784 				ch->seid, parent_vsi->seid);
7785 		kfree(ch);
7786 	}
7787 	vsi->macvlan_cnt = 0;
7788 }
7789 
7790 /**
7791  * i40e_fwd_ring_up - bring the macvlan device up
7792  * @vsi: the VSI we want to access
7793  * @vdev: macvlan netdevice
7794  * @fwd: the private fwd structure
7795  */
7796 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7797 			    struct i40e_fwd_adapter *fwd)
7798 {
7799 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7800 	int ret = 0, num_tc = 1,  i, aq_err;
7801 	struct i40e_pf *pf = vsi->back;
7802 	struct i40e_hw *hw = &pf->hw;
7803 
7804 	/* Go through the list and find an available channel */
7805 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7806 		if (!i40e_is_channel_macvlan(iter)) {
7807 			iter->fwd = fwd;
7808 			/* record configuration for macvlan interface in vdev */
7809 			for (i = 0; i < num_tc; i++)
7810 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7811 							     i,
7812 							     iter->num_queue_pairs,
7813 							     iter->base_queue);
7814 			for (i = 0; i < iter->num_queue_pairs; i++) {
7815 				struct i40e_ring *tx_ring, *rx_ring;
7816 				u16 pf_q;
7817 
7818 				pf_q = iter->base_queue + i;
7819 
7820 				/* Get to TX ring ptr */
7821 				tx_ring = vsi->tx_rings[pf_q];
7822 				tx_ring->ch = iter;
7823 
7824 				/* Get the RX ring ptr */
7825 				rx_ring = vsi->rx_rings[pf_q];
7826 				rx_ring->ch = iter;
7827 			}
7828 			ch = iter;
7829 			break;
7830 		}
7831 	}
7832 
7833 	if (!ch)
7834 		return -EINVAL;
7835 
7836 	/* Guarantee all rings are updated before we update the
7837 	 * MAC address filter.
7838 	 */
7839 	wmb();
7840 
7841 	/* Add a mac filter */
7842 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7843 	if (ret) {
7844 		/* if we cannot add the MAC rule then disable the offload */
7845 		macvlan_release_l2fw_offload(vdev);
7846 		for (i = 0; i < ch->num_queue_pairs; i++) {
7847 			struct i40e_ring *rx_ring;
7848 			u16 pf_q;
7849 
7850 			pf_q = ch->base_queue + i;
7851 			rx_ring = vsi->rx_rings[pf_q];
7852 			rx_ring->netdev = NULL;
7853 		}
7854 		dev_info(&pf->pdev->dev,
7855 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7856 			  ERR_PTR(ret),
7857 			  i40e_aq_str(hw, aq_err));
7858 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7859 	}
7860 
7861 	return ret;
7862 }
7863 
7864 /**
7865  * i40e_setup_macvlans - create the channels which will be macvlans
7866  * @vsi: the VSI we want to access
7867  * @macvlan_cnt: no. of macvlans to be setup
7868  * @qcnt: no. of Qs per macvlan
7869  * @vdev: macvlan netdevice
7870  */
7871 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7872 			       struct net_device *vdev)
7873 {
7874 	struct i40e_pf *pf = vsi->back;
7875 	struct i40e_hw *hw = &pf->hw;
7876 	struct i40e_vsi_context ctxt;
7877 	u16 sections, qmap, num_qps;
7878 	struct i40e_channel *ch;
7879 	int i, pow, ret = 0;
7880 	u8 offset = 0;
7881 
7882 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7883 		return -EINVAL;
7884 
7885 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7886 
7887 	/* find the next higher power-of-2 of num queue pairs */
7888 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7889 
7890 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7891 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7892 
7893 	/* Setup context bits for the main VSI */
7894 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7895 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7896 	memset(&ctxt, 0, sizeof(ctxt));
7897 	ctxt.seid = vsi->seid;
7898 	ctxt.pf_num = vsi->back->hw.pf_id;
7899 	ctxt.vf_num = 0;
7900 	ctxt.uplink_seid = vsi->uplink_seid;
7901 	ctxt.info = vsi->info;
7902 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7903 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7904 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7905 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7906 
7907 	/* Reconfigure RSS for main VSI with new max queue count */
7908 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7909 	ret = i40e_vsi_config_rss(vsi);
7910 	if (ret) {
7911 		dev_info(&pf->pdev->dev,
7912 			 "Failed to reconfig RSS for num_queues (%u)\n",
7913 			 vsi->rss_size);
7914 		return ret;
7915 	}
7916 	vsi->reconfig_rss = true;
7917 	dev_dbg(&vsi->back->pdev->dev,
7918 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7919 	vsi->next_base_queue = num_qps;
7920 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7921 
7922 	/* Update the VSI after updating the VSI queue-mapping
7923 	 * information
7924 	 */
7925 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7926 	if (ret) {
7927 		dev_info(&pf->pdev->dev,
7928 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7929 			 ERR_PTR(ret),
7930 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7931 		return ret;
7932 	}
7933 	/* update the local VSI info with updated queue map */
7934 	i40e_vsi_update_queue_map(vsi, &ctxt);
7935 	vsi->info.valid_sections = 0;
7936 
7937 	/* Create channels for macvlans */
7938 	INIT_LIST_HEAD(&vsi->macvlan_list);
7939 	for (i = 0; i < macvlan_cnt; i++) {
7940 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7941 		if (!ch) {
7942 			ret = -ENOMEM;
7943 			goto err_free;
7944 		}
7945 		INIT_LIST_HEAD(&ch->list);
7946 		ch->num_queue_pairs = qcnt;
7947 		if (!i40e_setup_channel(pf, vsi, ch)) {
7948 			ret = -EINVAL;
7949 			kfree(ch);
7950 			goto err_free;
7951 		}
7952 		ch->parent_vsi = vsi;
7953 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7954 		vsi->macvlan_cnt++;
7955 		list_add_tail(&ch->list, &vsi->macvlan_list);
7956 	}
7957 
7958 	return ret;
7959 
7960 err_free:
7961 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7962 	i40e_free_macvlan_channels(vsi);
7963 
7964 	return ret;
7965 }
7966 
7967 /**
7968  * i40e_fwd_add - configure macvlans
7969  * @netdev: net device to configure
7970  * @vdev: macvlan netdevice
7971  **/
7972 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7973 {
7974 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7975 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7976 	struct i40e_vsi *vsi = np->vsi;
7977 	struct i40e_pf *pf = vsi->back;
7978 	struct i40e_fwd_adapter *fwd;
7979 	int avail_macvlan, ret;
7980 
7981 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
7982 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7983 		return ERR_PTR(-EINVAL);
7984 	}
7985 	if (i40e_is_tc_mqprio_enabled(pf)) {
7986 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7987 		return ERR_PTR(-EINVAL);
7988 	}
7989 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7990 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7991 		return ERR_PTR(-EINVAL);
7992 	}
7993 
7994 	/* The macvlan device has to be a single Q device so that the
7995 	 * tc_to_txq field can be reused to pick the tx queue.
7996 	 */
7997 	if (netif_is_multiqueue(vdev))
7998 		return ERR_PTR(-ERANGE);
7999 
8000 	if (!vsi->macvlan_cnt) {
8001 		/* reserve bit 0 for the pf device */
8002 		set_bit(0, vsi->fwd_bitmask);
8003 
8004 		/* Try to reserve as many queues as possible for macvlans. First
8005 		 * reserve 3/4th of max vectors, then half, then quarter and
8006 		 * calculate Qs per macvlan as you go
8007 		 */
8008 		vectors = pf->num_lan_msix;
8009 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8010 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8011 			q_per_macvlan = 4;
8012 			macvlan_cnt = (vectors - 32) / 4;
8013 		} else if (vectors <= 64 && vectors > 32) {
8014 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8015 			q_per_macvlan = 2;
8016 			macvlan_cnt = (vectors - 16) / 2;
8017 		} else if (vectors <= 32 && vectors > 16) {
8018 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8019 			q_per_macvlan = 1;
8020 			macvlan_cnt = vectors - 16;
8021 		} else if (vectors <= 16 && vectors > 8) {
8022 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8023 			q_per_macvlan = 1;
8024 			macvlan_cnt = vectors - 8;
8025 		} else {
8026 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8027 			q_per_macvlan = 1;
8028 			macvlan_cnt = vectors - 1;
8029 		}
8030 
8031 		if (macvlan_cnt == 0)
8032 			return ERR_PTR(-EBUSY);
8033 
8034 		/* Quiesce VSI queues */
8035 		i40e_quiesce_vsi(vsi);
8036 
8037 		/* sets up the macvlans but does not "enable" them */
8038 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8039 					  vdev);
8040 		if (ret)
8041 			return ERR_PTR(ret);
8042 
8043 		/* Unquiesce VSI */
8044 		i40e_unquiesce_vsi(vsi);
8045 	}
8046 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8047 					    vsi->macvlan_cnt);
8048 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8049 		return ERR_PTR(-EBUSY);
8050 
8051 	/* create the fwd struct */
8052 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8053 	if (!fwd)
8054 		return ERR_PTR(-ENOMEM);
8055 
8056 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8057 	fwd->bit_no = avail_macvlan;
8058 	netdev_set_sb_channel(vdev, avail_macvlan);
8059 	fwd->netdev = vdev;
8060 
8061 	if (!netif_running(netdev))
8062 		return fwd;
8063 
8064 	/* Set fwd ring up */
8065 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8066 	if (ret) {
8067 		/* unbind the queues and drop the subordinate channel config */
8068 		netdev_unbind_sb_channel(netdev, vdev);
8069 		netdev_set_sb_channel(vdev, 0);
8070 
8071 		kfree(fwd);
8072 		return ERR_PTR(-EINVAL);
8073 	}
8074 
8075 	return fwd;
8076 }
8077 
8078 /**
8079  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8080  * @vsi: the VSI we want to access
8081  */
8082 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8083 {
8084 	struct i40e_channel *ch, *ch_tmp;
8085 	struct i40e_pf *pf = vsi->back;
8086 	struct i40e_hw *hw = &pf->hw;
8087 	int aq_err, ret = 0;
8088 
8089 	if (list_empty(&vsi->macvlan_list))
8090 		return;
8091 
8092 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8093 		if (i40e_is_channel_macvlan(ch)) {
8094 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8095 						      i40e_channel_mac(ch),
8096 						      &aq_err);
8097 			if (!ret) {
8098 				/* Reset queue contexts */
8099 				i40e_reset_ch_rings(vsi, ch);
8100 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8101 				netdev_unbind_sb_channel(vsi->netdev,
8102 							 ch->fwd->netdev);
8103 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8104 				kfree(ch->fwd);
8105 				ch->fwd = NULL;
8106 			}
8107 		}
8108 	}
8109 }
8110 
8111 /**
8112  * i40e_fwd_del - delete macvlan interfaces
8113  * @netdev: net device to configure
8114  * @vdev: macvlan netdevice
8115  */
8116 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8117 {
8118 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8119 	struct i40e_fwd_adapter *fwd = vdev;
8120 	struct i40e_channel *ch, *ch_tmp;
8121 	struct i40e_vsi *vsi = np->vsi;
8122 	struct i40e_pf *pf = vsi->back;
8123 	struct i40e_hw *hw = &pf->hw;
8124 	int aq_err, ret = 0;
8125 
8126 	/* Find the channel associated with the macvlan and del mac filter */
8127 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8128 		if (i40e_is_channel_macvlan(ch) &&
8129 		    ether_addr_equal(i40e_channel_mac(ch),
8130 				     fwd->netdev->dev_addr)) {
8131 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8132 						      i40e_channel_mac(ch),
8133 						      &aq_err);
8134 			if (!ret) {
8135 				/* Reset queue contexts */
8136 				i40e_reset_ch_rings(vsi, ch);
8137 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8138 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8139 				netdev_set_sb_channel(fwd->netdev, 0);
8140 				kfree(ch->fwd);
8141 				ch->fwd = NULL;
8142 			} else {
8143 				dev_info(&pf->pdev->dev,
8144 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8145 					  ERR_PTR(ret),
8146 					  i40e_aq_str(hw, aq_err));
8147 			}
8148 			break;
8149 		}
8150 	}
8151 }
8152 
8153 /**
8154  * i40e_setup_tc - configure multiple traffic classes
8155  * @netdev: net device to configure
8156  * @type_data: tc offload data
8157  **/
8158 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8159 {
8160 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8161 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8162 	struct i40e_vsi *vsi = np->vsi;
8163 	struct i40e_pf *pf = vsi->back;
8164 	u8 enabled_tc = 0, num_tc, hw;
8165 	bool need_reset = false;
8166 	int old_queue_pairs;
8167 	int ret = -EINVAL;
8168 	u16 mode;
8169 	int i;
8170 
8171 	old_queue_pairs = vsi->num_queue_pairs;
8172 	num_tc = mqprio_qopt->qopt.num_tc;
8173 	hw = mqprio_qopt->qopt.hw;
8174 	mode = mqprio_qopt->mode;
8175 	if (!hw) {
8176 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8177 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8178 		goto config_tc;
8179 	}
8180 
8181 	/* Check if MFP enabled */
8182 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8183 		netdev_info(netdev,
8184 			    "Configuring TC not supported in MFP mode\n");
8185 		return ret;
8186 	}
8187 	switch (mode) {
8188 	case TC_MQPRIO_MODE_DCB:
8189 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8190 
8191 		/* Check if DCB enabled to continue */
8192 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8193 			netdev_info(netdev,
8194 				    "DCB is not enabled for adapter\n");
8195 			return ret;
8196 		}
8197 
8198 		/* Check whether tc count is within enabled limit */
8199 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8200 			netdev_info(netdev,
8201 				    "TC count greater than enabled on link for adapter\n");
8202 			return ret;
8203 		}
8204 		break;
8205 	case TC_MQPRIO_MODE_CHANNEL:
8206 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8207 			netdev_info(netdev,
8208 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8209 			return ret;
8210 		}
8211 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8212 			return ret;
8213 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8214 		if (ret)
8215 			return ret;
8216 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8217 		       sizeof(*mqprio_qopt));
8218 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8219 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8220 		break;
8221 	default:
8222 		return -EINVAL;
8223 	}
8224 
8225 config_tc:
8226 	/* Generate TC map for number of tc requested */
8227 	for (i = 0; i < num_tc; i++)
8228 		enabled_tc |= BIT(i);
8229 
8230 	/* Requesting same TC configuration as already enabled */
8231 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8232 	    mode != TC_MQPRIO_MODE_CHANNEL)
8233 		return 0;
8234 
8235 	/* Quiesce VSI queues */
8236 	i40e_quiesce_vsi(vsi);
8237 
8238 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8239 		i40e_remove_queue_channels(vsi);
8240 
8241 	/* Configure VSI for enabled TCs */
8242 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8243 	if (ret) {
8244 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8245 			    vsi->seid);
8246 		need_reset = true;
8247 		goto exit;
8248 	} else if (enabled_tc &&
8249 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8250 		netdev_info(netdev,
8251 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8252 			    vsi->tc_config.tc_info[0].qcount);
8253 		ret = -EINVAL;
8254 		need_reset = true;
8255 		goto exit;
8256 	}
8257 
8258 	dev_info(&vsi->back->pdev->dev,
8259 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8260 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8261 
8262 	if (i40e_is_tc_mqprio_enabled(pf)) {
8263 		if (vsi->mqprio_qopt.max_rate[0]) {
8264 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8265 						  vsi->mqprio_qopt.max_rate[0]);
8266 
8267 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8268 			if (!ret) {
8269 				u64 credits = max_tx_rate;
8270 
8271 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8272 				dev_dbg(&vsi->back->pdev->dev,
8273 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8274 					max_tx_rate,
8275 					credits,
8276 					vsi->seid);
8277 			} else {
8278 				need_reset = true;
8279 				goto exit;
8280 			}
8281 		}
8282 		ret = i40e_configure_queue_channels(vsi);
8283 		if (ret) {
8284 			vsi->num_queue_pairs = old_queue_pairs;
8285 			netdev_info(netdev,
8286 				    "Failed configuring queue channels\n");
8287 			need_reset = true;
8288 			goto exit;
8289 		}
8290 	}
8291 
8292 exit:
8293 	/* Reset the configuration data to defaults, only TC0 is enabled */
8294 	if (need_reset) {
8295 		i40e_vsi_set_default_tc_config(vsi);
8296 		need_reset = false;
8297 	}
8298 
8299 	/* Unquiesce VSI */
8300 	i40e_unquiesce_vsi(vsi);
8301 	return ret;
8302 }
8303 
8304 /**
8305  * i40e_set_cld_element - sets cloud filter element data
8306  * @filter: cloud filter rule
8307  * @cld: ptr to cloud filter element data
8308  *
8309  * This is helper function to copy data into cloud filter element
8310  **/
8311 static inline void
8312 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8313 		     struct i40e_aqc_cloud_filters_element_data *cld)
8314 {
8315 	u32 ipa;
8316 	int i;
8317 
8318 	memset(cld, 0, sizeof(*cld));
8319 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8320 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8321 
8322 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8323 		return;
8324 
8325 	if (filter->n_proto == ETH_P_IPV6) {
8326 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8327 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8328 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8329 
8330 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8331 		}
8332 	} else {
8333 		ipa = be32_to_cpu(filter->dst_ipv4);
8334 
8335 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8336 	}
8337 
8338 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8339 
8340 	/* tenant_id is not supported by FW now, once the support is enabled
8341 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8342 	 */
8343 	if (filter->tenant_id)
8344 		return;
8345 }
8346 
8347 /**
8348  * i40e_add_del_cloud_filter - Add/del cloud filter
8349  * @vsi: pointer to VSI
8350  * @filter: cloud filter rule
8351  * @add: if true, add, if false, delete
8352  *
8353  * Add or delete a cloud filter for a specific flow spec.
8354  * Returns 0 if the filter were successfully added.
8355  **/
8356 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8357 			      struct i40e_cloud_filter *filter, bool add)
8358 {
8359 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8360 	struct i40e_pf *pf = vsi->back;
8361 	int ret;
8362 	static const u16 flag_table[128] = {
8363 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8364 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8365 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8366 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8367 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8368 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8369 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8370 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8371 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8372 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8373 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8374 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8375 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8376 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8377 	};
8378 
8379 	if (filter->flags >= ARRAY_SIZE(flag_table))
8380 		return -EIO;
8381 
8382 	memset(&cld_filter, 0, sizeof(cld_filter));
8383 
8384 	/* copy element needed to add cloud filter from filter */
8385 	i40e_set_cld_element(filter, &cld_filter);
8386 
8387 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8388 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8389 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8390 
8391 	if (filter->n_proto == ETH_P_IPV6)
8392 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8393 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8394 	else
8395 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8396 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8397 
8398 	if (add)
8399 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8400 						&cld_filter, 1);
8401 	else
8402 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8403 						&cld_filter, 1);
8404 	if (ret)
8405 		dev_dbg(&pf->pdev->dev,
8406 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8407 			add ? "add" : "delete", filter->dst_port, ret,
8408 			pf->hw.aq.asq_last_status);
8409 	else
8410 		dev_info(&pf->pdev->dev,
8411 			 "%s cloud filter for VSI: %d\n",
8412 			 add ? "Added" : "Deleted", filter->seid);
8413 	return ret;
8414 }
8415 
8416 /**
8417  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8418  * @vsi: pointer to VSI
8419  * @filter: cloud filter rule
8420  * @add: if true, add, if false, delete
8421  *
8422  * Add or delete a cloud filter for a specific flow spec using big buffer.
8423  * Returns 0 if the filter were successfully added.
8424  **/
8425 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8426 				      struct i40e_cloud_filter *filter,
8427 				      bool add)
8428 {
8429 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8430 	struct i40e_pf *pf = vsi->back;
8431 	int ret;
8432 
8433 	/* Both (src/dst) valid mac_addr are not supported */
8434 	if ((is_valid_ether_addr(filter->dst_mac) &&
8435 	     is_valid_ether_addr(filter->src_mac)) ||
8436 	    (is_multicast_ether_addr(filter->dst_mac) &&
8437 	     is_multicast_ether_addr(filter->src_mac)))
8438 		return -EOPNOTSUPP;
8439 
8440 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8441 	 * ports are not supported via big buffer now.
8442 	 */
8443 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8444 		return -EOPNOTSUPP;
8445 
8446 	/* adding filter using src_port/src_ip is not supported at this stage */
8447 	if (filter->src_port ||
8448 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8449 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8450 		return -EOPNOTSUPP;
8451 
8452 	memset(&cld_filter, 0, sizeof(cld_filter));
8453 
8454 	/* copy element needed to add cloud filter from filter */
8455 	i40e_set_cld_element(filter, &cld_filter.element);
8456 
8457 	if (is_valid_ether_addr(filter->dst_mac) ||
8458 	    is_valid_ether_addr(filter->src_mac) ||
8459 	    is_multicast_ether_addr(filter->dst_mac) ||
8460 	    is_multicast_ether_addr(filter->src_mac)) {
8461 		/* MAC + IP : unsupported mode */
8462 		if (filter->dst_ipv4)
8463 			return -EOPNOTSUPP;
8464 
8465 		/* since we validated that L4 port must be valid before
8466 		 * we get here, start with respective "flags" value
8467 		 * and update if vlan is present or not
8468 		 */
8469 		cld_filter.element.flags =
8470 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8471 
8472 		if (filter->vlan_id) {
8473 			cld_filter.element.flags =
8474 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8475 		}
8476 
8477 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8478 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8479 		cld_filter.element.flags =
8480 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8481 		if (filter->n_proto == ETH_P_IPV6)
8482 			cld_filter.element.flags |=
8483 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8484 		else
8485 			cld_filter.element.flags |=
8486 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8487 	} else {
8488 		dev_err(&pf->pdev->dev,
8489 			"either mac or ip has to be valid for cloud filter\n");
8490 		return -EINVAL;
8491 	}
8492 
8493 	/* Now copy L4 port in Byte 6..7 in general fields */
8494 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8495 						be16_to_cpu(filter->dst_port);
8496 
8497 	if (add) {
8498 		/* Validate current device switch mode, change if necessary */
8499 		ret = i40e_validate_and_set_switch_mode(vsi);
8500 		if (ret) {
8501 			dev_err(&pf->pdev->dev,
8502 				"failed to set switch mode, ret %d\n",
8503 				ret);
8504 			return ret;
8505 		}
8506 
8507 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8508 						   &cld_filter, 1);
8509 	} else {
8510 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8511 						   &cld_filter, 1);
8512 	}
8513 
8514 	if (ret)
8515 		dev_dbg(&pf->pdev->dev,
8516 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8517 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8518 	else
8519 		dev_info(&pf->pdev->dev,
8520 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8521 			 add ? "add" : "delete", filter->seid,
8522 			 ntohs(filter->dst_port));
8523 	return ret;
8524 }
8525 
8526 /**
8527  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8528  * @vsi: Pointer to VSI
8529  * @f: Pointer to struct flow_cls_offload
8530  * @filter: Pointer to cloud filter structure
8531  *
8532  **/
8533 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8534 				 struct flow_cls_offload *f,
8535 				 struct i40e_cloud_filter *filter)
8536 {
8537 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8538 	struct flow_dissector *dissector = rule->match.dissector;
8539 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8540 	struct i40e_pf *pf = vsi->back;
8541 	u8 field_flags = 0;
8542 
8543 	if (dissector->used_keys &
8544 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8545 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8546 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8547 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8548 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8549 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8550 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8551 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8552 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8553 			dissector->used_keys);
8554 		return -EOPNOTSUPP;
8555 	}
8556 
8557 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8558 		struct flow_match_enc_keyid match;
8559 
8560 		flow_rule_match_enc_keyid(rule, &match);
8561 		if (match.mask->keyid != 0)
8562 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8563 
8564 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8565 	}
8566 
8567 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8568 		struct flow_match_basic match;
8569 
8570 		flow_rule_match_basic(rule, &match);
8571 		n_proto_key = ntohs(match.key->n_proto);
8572 		n_proto_mask = ntohs(match.mask->n_proto);
8573 
8574 		if (n_proto_key == ETH_P_ALL) {
8575 			n_proto_key = 0;
8576 			n_proto_mask = 0;
8577 		}
8578 		filter->n_proto = n_proto_key & n_proto_mask;
8579 		filter->ip_proto = match.key->ip_proto;
8580 	}
8581 
8582 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8583 		struct flow_match_eth_addrs match;
8584 
8585 		flow_rule_match_eth_addrs(rule, &match);
8586 
8587 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8588 		if (!is_zero_ether_addr(match.mask->dst)) {
8589 			if (is_broadcast_ether_addr(match.mask->dst)) {
8590 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8591 			} else {
8592 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8593 					match.mask->dst);
8594 				return -EIO;
8595 			}
8596 		}
8597 
8598 		if (!is_zero_ether_addr(match.mask->src)) {
8599 			if (is_broadcast_ether_addr(match.mask->src)) {
8600 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8601 			} else {
8602 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8603 					match.mask->src);
8604 				return -EIO;
8605 			}
8606 		}
8607 		ether_addr_copy(filter->dst_mac, match.key->dst);
8608 		ether_addr_copy(filter->src_mac, match.key->src);
8609 	}
8610 
8611 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8612 		struct flow_match_vlan match;
8613 
8614 		flow_rule_match_vlan(rule, &match);
8615 		if (match.mask->vlan_id) {
8616 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8617 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8618 
8619 			} else {
8620 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8621 					match.mask->vlan_id);
8622 				return -EIO;
8623 			}
8624 		}
8625 
8626 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8627 	}
8628 
8629 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8630 		struct flow_match_control match;
8631 
8632 		flow_rule_match_control(rule, &match);
8633 		addr_type = match.key->addr_type;
8634 	}
8635 
8636 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8637 		struct flow_match_ipv4_addrs match;
8638 
8639 		flow_rule_match_ipv4_addrs(rule, &match);
8640 		if (match.mask->dst) {
8641 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8642 				field_flags |= I40E_CLOUD_FIELD_IIP;
8643 			} else {
8644 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8645 					&match.mask->dst);
8646 				return -EIO;
8647 			}
8648 		}
8649 
8650 		if (match.mask->src) {
8651 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8652 				field_flags |= I40E_CLOUD_FIELD_IIP;
8653 			} else {
8654 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8655 					&match.mask->src);
8656 				return -EIO;
8657 			}
8658 		}
8659 
8660 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8661 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8662 			return -EIO;
8663 		}
8664 		filter->dst_ipv4 = match.key->dst;
8665 		filter->src_ipv4 = match.key->src;
8666 	}
8667 
8668 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8669 		struct flow_match_ipv6_addrs match;
8670 
8671 		flow_rule_match_ipv6_addrs(rule, &match);
8672 
8673 		/* src and dest IPV6 address should not be LOOPBACK
8674 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8675 		 */
8676 		if (ipv6_addr_loopback(&match.key->dst) ||
8677 		    ipv6_addr_loopback(&match.key->src)) {
8678 			dev_err(&pf->pdev->dev,
8679 				"Bad ipv6, addr is LOOPBACK\n");
8680 			return -EIO;
8681 		}
8682 		if (!ipv6_addr_any(&match.mask->dst) ||
8683 		    !ipv6_addr_any(&match.mask->src))
8684 			field_flags |= I40E_CLOUD_FIELD_IIP;
8685 
8686 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8687 		       sizeof(filter->src_ipv6));
8688 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8689 		       sizeof(filter->dst_ipv6));
8690 	}
8691 
8692 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8693 		struct flow_match_ports match;
8694 
8695 		flow_rule_match_ports(rule, &match);
8696 		if (match.mask->src) {
8697 			if (match.mask->src == cpu_to_be16(0xffff)) {
8698 				field_flags |= I40E_CLOUD_FIELD_IIP;
8699 			} else {
8700 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8701 					be16_to_cpu(match.mask->src));
8702 				return -EIO;
8703 			}
8704 		}
8705 
8706 		if (match.mask->dst) {
8707 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8708 				field_flags |= I40E_CLOUD_FIELD_IIP;
8709 			} else {
8710 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8711 					be16_to_cpu(match.mask->dst));
8712 				return -EIO;
8713 			}
8714 		}
8715 
8716 		filter->dst_port = match.key->dst;
8717 		filter->src_port = match.key->src;
8718 
8719 		switch (filter->ip_proto) {
8720 		case IPPROTO_TCP:
8721 		case IPPROTO_UDP:
8722 			break;
8723 		default:
8724 			dev_err(&pf->pdev->dev,
8725 				"Only UDP and TCP transport are supported\n");
8726 			return -EINVAL;
8727 		}
8728 	}
8729 	filter->flags = field_flags;
8730 	return 0;
8731 }
8732 
8733 /**
8734  * i40e_handle_tclass: Forward to a traffic class on the device
8735  * @vsi: Pointer to VSI
8736  * @tc: traffic class index on the device
8737  * @filter: Pointer to cloud filter structure
8738  *
8739  **/
8740 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8741 			      struct i40e_cloud_filter *filter)
8742 {
8743 	struct i40e_channel *ch, *ch_tmp;
8744 
8745 	/* direct to a traffic class on the same device */
8746 	if (tc == 0) {
8747 		filter->seid = vsi->seid;
8748 		return 0;
8749 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8750 		if (!filter->dst_port) {
8751 			dev_err(&vsi->back->pdev->dev,
8752 				"Specify destination port to direct to traffic class that is not default\n");
8753 			return -EINVAL;
8754 		}
8755 		if (list_empty(&vsi->ch_list))
8756 			return -EINVAL;
8757 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8758 					 list) {
8759 			if (ch->seid == vsi->tc_seid_map[tc])
8760 				filter->seid = ch->seid;
8761 		}
8762 		return 0;
8763 	}
8764 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8765 	return -EINVAL;
8766 }
8767 
8768 /**
8769  * i40e_configure_clsflower - Configure tc flower filters
8770  * @vsi: Pointer to VSI
8771  * @cls_flower: Pointer to struct flow_cls_offload
8772  *
8773  **/
8774 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8775 				    struct flow_cls_offload *cls_flower)
8776 {
8777 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8778 	struct i40e_cloud_filter *filter = NULL;
8779 	struct i40e_pf *pf = vsi->back;
8780 	int err = 0;
8781 
8782 	if (tc < 0) {
8783 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8784 		return -EOPNOTSUPP;
8785 	}
8786 
8787 	if (!tc) {
8788 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8789 		return -EINVAL;
8790 	}
8791 
8792 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8793 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8794 		return -EBUSY;
8795 
8796 	if (pf->fdir_pf_active_filters ||
8797 	    (!hlist_empty(&pf->fdir_filter_list))) {
8798 		dev_err(&vsi->back->pdev->dev,
8799 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8800 		return -EINVAL;
8801 	}
8802 
8803 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8804 		dev_err(&vsi->back->pdev->dev,
8805 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8806 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8807 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8808 	}
8809 
8810 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8811 	if (!filter)
8812 		return -ENOMEM;
8813 
8814 	filter->cookie = cls_flower->cookie;
8815 
8816 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8817 	if (err < 0)
8818 		goto err;
8819 
8820 	err = i40e_handle_tclass(vsi, tc, filter);
8821 	if (err < 0)
8822 		goto err;
8823 
8824 	/* Add cloud filter */
8825 	if (filter->dst_port)
8826 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8827 	else
8828 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8829 
8830 	if (err) {
8831 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8832 			err);
8833 		goto err;
8834 	}
8835 
8836 	/* add filter to the ordered list */
8837 	INIT_HLIST_NODE(&filter->cloud_node);
8838 
8839 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8840 
8841 	pf->num_cloud_filters++;
8842 
8843 	return err;
8844 err:
8845 	kfree(filter);
8846 	return err;
8847 }
8848 
8849 /**
8850  * i40e_find_cloud_filter - Find the could filter in the list
8851  * @vsi: Pointer to VSI
8852  * @cookie: filter specific cookie
8853  *
8854  **/
8855 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8856 							unsigned long *cookie)
8857 {
8858 	struct i40e_cloud_filter *filter = NULL;
8859 	struct hlist_node *node2;
8860 
8861 	hlist_for_each_entry_safe(filter, node2,
8862 				  &vsi->back->cloud_filter_list, cloud_node)
8863 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8864 			return filter;
8865 	return NULL;
8866 }
8867 
8868 /**
8869  * i40e_delete_clsflower - Remove tc flower filters
8870  * @vsi: Pointer to VSI
8871  * @cls_flower: Pointer to struct flow_cls_offload
8872  *
8873  **/
8874 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8875 				 struct flow_cls_offload *cls_flower)
8876 {
8877 	struct i40e_cloud_filter *filter = NULL;
8878 	struct i40e_pf *pf = vsi->back;
8879 	int err = 0;
8880 
8881 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8882 
8883 	if (!filter)
8884 		return -EINVAL;
8885 
8886 	hash_del(&filter->cloud_node);
8887 
8888 	if (filter->dst_port)
8889 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8890 	else
8891 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8892 
8893 	kfree(filter);
8894 	if (err) {
8895 		dev_err(&pf->pdev->dev,
8896 			"Failed to delete cloud filter, err %pe\n",
8897 			ERR_PTR(err));
8898 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8899 	}
8900 
8901 	pf->num_cloud_filters--;
8902 	if (!pf->num_cloud_filters)
8903 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8904 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8905 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8906 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8907 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8908 		}
8909 	return 0;
8910 }
8911 
8912 /**
8913  * i40e_setup_tc_cls_flower - flower classifier offloads
8914  * @np: net device to configure
8915  * @cls_flower: offload data
8916  **/
8917 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8918 				    struct flow_cls_offload *cls_flower)
8919 {
8920 	struct i40e_vsi *vsi = np->vsi;
8921 
8922 	switch (cls_flower->command) {
8923 	case FLOW_CLS_REPLACE:
8924 		return i40e_configure_clsflower(vsi, cls_flower);
8925 	case FLOW_CLS_DESTROY:
8926 		return i40e_delete_clsflower(vsi, cls_flower);
8927 	case FLOW_CLS_STATS:
8928 		return -EOPNOTSUPP;
8929 	default:
8930 		return -EOPNOTSUPP;
8931 	}
8932 }
8933 
8934 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8935 				  void *cb_priv)
8936 {
8937 	struct i40e_netdev_priv *np = cb_priv;
8938 
8939 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8940 		return -EOPNOTSUPP;
8941 
8942 	switch (type) {
8943 	case TC_SETUP_CLSFLOWER:
8944 		return i40e_setup_tc_cls_flower(np, type_data);
8945 
8946 	default:
8947 		return -EOPNOTSUPP;
8948 	}
8949 }
8950 
8951 static LIST_HEAD(i40e_block_cb_list);
8952 
8953 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8954 			   void *type_data)
8955 {
8956 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8957 
8958 	switch (type) {
8959 	case TC_SETUP_QDISC_MQPRIO:
8960 		return i40e_setup_tc(netdev, type_data);
8961 	case TC_SETUP_BLOCK:
8962 		return flow_block_cb_setup_simple(type_data,
8963 						  &i40e_block_cb_list,
8964 						  i40e_setup_tc_block_cb,
8965 						  np, np, true);
8966 	default:
8967 		return -EOPNOTSUPP;
8968 	}
8969 }
8970 
8971 /**
8972  * i40e_open - Called when a network interface is made active
8973  * @netdev: network interface device structure
8974  *
8975  * The open entry point is called when a network interface is made
8976  * active by the system (IFF_UP).  At this point all resources needed
8977  * for transmit and receive operations are allocated, the interrupt
8978  * handler is registered with the OS, the netdev watchdog subtask is
8979  * enabled, and the stack is notified that the interface is ready.
8980  *
8981  * Returns 0 on success, negative value on failure
8982  **/
8983 int i40e_open(struct net_device *netdev)
8984 {
8985 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8986 	struct i40e_vsi *vsi = np->vsi;
8987 	struct i40e_pf *pf = vsi->back;
8988 	int err;
8989 
8990 	/* disallow open during test or if eeprom is broken */
8991 	if (test_bit(__I40E_TESTING, pf->state) ||
8992 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8993 		return -EBUSY;
8994 
8995 	netif_carrier_off(netdev);
8996 
8997 	if (i40e_force_link_state(pf, true))
8998 		return -EAGAIN;
8999 
9000 	err = i40e_vsi_open(vsi);
9001 	if (err)
9002 		return err;
9003 
9004 	/* configure global TSO hardware offload settings */
9005 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9006 						       TCP_FLAG_FIN) >> 16);
9007 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9008 						       TCP_FLAG_FIN |
9009 						       TCP_FLAG_CWR) >> 16);
9010 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9011 	udp_tunnel_get_rx_info(netdev);
9012 
9013 	return 0;
9014 }
9015 
9016 /**
9017  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9018  * @vsi: vsi structure
9019  *
9020  * This updates netdev's number of tx/rx queues
9021  *
9022  * Returns status of setting tx/rx queues
9023  **/
9024 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9025 {
9026 	int ret;
9027 
9028 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9029 					   vsi->num_queue_pairs);
9030 	if (ret)
9031 		return ret;
9032 
9033 	return netif_set_real_num_tx_queues(vsi->netdev,
9034 					    vsi->num_queue_pairs);
9035 }
9036 
9037 /**
9038  * i40e_vsi_open -
9039  * @vsi: the VSI to open
9040  *
9041  * Finish initialization of the VSI.
9042  *
9043  * Returns 0 on success, negative value on failure
9044  *
9045  * Note: expects to be called while under rtnl_lock()
9046  **/
9047 int i40e_vsi_open(struct i40e_vsi *vsi)
9048 {
9049 	struct i40e_pf *pf = vsi->back;
9050 	char int_name[I40E_INT_NAME_STR_LEN];
9051 	int err;
9052 
9053 	/* allocate descriptors */
9054 	err = i40e_vsi_setup_tx_resources(vsi);
9055 	if (err)
9056 		goto err_setup_tx;
9057 	err = i40e_vsi_setup_rx_resources(vsi);
9058 	if (err)
9059 		goto err_setup_rx;
9060 
9061 	err = i40e_vsi_configure(vsi);
9062 	if (err)
9063 		goto err_setup_rx;
9064 
9065 	if (vsi->netdev) {
9066 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9067 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9068 		err = i40e_vsi_request_irq(vsi, int_name);
9069 		if (err)
9070 			goto err_setup_rx;
9071 
9072 		/* Notify the stack of the actual queue counts. */
9073 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9074 		if (err)
9075 			goto err_set_queues;
9076 
9077 	} else if (vsi->type == I40E_VSI_FDIR) {
9078 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9079 			 dev_driver_string(&pf->pdev->dev),
9080 			 dev_name(&pf->pdev->dev));
9081 		err = i40e_vsi_request_irq(vsi, int_name);
9082 		if (err)
9083 			goto err_setup_rx;
9084 
9085 	} else {
9086 		err = -EINVAL;
9087 		goto err_setup_rx;
9088 	}
9089 
9090 	err = i40e_up_complete(vsi);
9091 	if (err)
9092 		goto err_up_complete;
9093 
9094 	return 0;
9095 
9096 err_up_complete:
9097 	i40e_down(vsi);
9098 err_set_queues:
9099 	i40e_vsi_free_irq(vsi);
9100 err_setup_rx:
9101 	i40e_vsi_free_rx_resources(vsi);
9102 err_setup_tx:
9103 	i40e_vsi_free_tx_resources(vsi);
9104 	if (vsi == pf->vsi[pf->lan_vsi])
9105 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9106 
9107 	return err;
9108 }
9109 
9110 /**
9111  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9112  * @pf: Pointer to PF
9113  *
9114  * This function destroys the hlist where all the Flow Director
9115  * filters were saved.
9116  **/
9117 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9118 {
9119 	struct i40e_fdir_filter *filter;
9120 	struct i40e_flex_pit *pit_entry, *tmp;
9121 	struct hlist_node *node2;
9122 
9123 	hlist_for_each_entry_safe(filter, node2,
9124 				  &pf->fdir_filter_list, fdir_node) {
9125 		hlist_del(&filter->fdir_node);
9126 		kfree(filter);
9127 	}
9128 
9129 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9130 		list_del(&pit_entry->list);
9131 		kfree(pit_entry);
9132 	}
9133 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9134 
9135 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9136 		list_del(&pit_entry->list);
9137 		kfree(pit_entry);
9138 	}
9139 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9140 
9141 	pf->fdir_pf_active_filters = 0;
9142 	i40e_reset_fdir_filter_cnt(pf);
9143 
9144 	/* Reprogram the default input set for TCP/IPv4 */
9145 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9146 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9147 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9148 
9149 	/* Reprogram the default input set for TCP/IPv6 */
9150 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9151 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9152 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9153 
9154 	/* Reprogram the default input set for UDP/IPv4 */
9155 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9156 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9157 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9158 
9159 	/* Reprogram the default input set for UDP/IPv6 */
9160 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9161 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9162 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9163 
9164 	/* Reprogram the default input set for SCTP/IPv4 */
9165 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9166 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9167 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9168 
9169 	/* Reprogram the default input set for SCTP/IPv6 */
9170 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9171 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9172 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9173 
9174 	/* Reprogram the default input set for Other/IPv4 */
9175 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9176 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9177 
9178 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9179 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9180 
9181 	/* Reprogram the default input set for Other/IPv6 */
9182 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9183 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9184 
9185 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9186 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9187 }
9188 
9189 /**
9190  * i40e_cloud_filter_exit - Cleans up the cloud filters
9191  * @pf: Pointer to PF
9192  *
9193  * This function destroys the hlist where all the cloud filters
9194  * were saved.
9195  **/
9196 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9197 {
9198 	struct i40e_cloud_filter *cfilter;
9199 	struct hlist_node *node;
9200 
9201 	hlist_for_each_entry_safe(cfilter, node,
9202 				  &pf->cloud_filter_list, cloud_node) {
9203 		hlist_del(&cfilter->cloud_node);
9204 		kfree(cfilter);
9205 	}
9206 	pf->num_cloud_filters = 0;
9207 
9208 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9209 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9210 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9211 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9212 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9213 	}
9214 }
9215 
9216 /**
9217  * i40e_close - Disables a network interface
9218  * @netdev: network interface device structure
9219  *
9220  * The close entry point is called when an interface is de-activated
9221  * by the OS.  The hardware is still under the driver's control, but
9222  * this netdev interface is disabled.
9223  *
9224  * Returns 0, this is not allowed to fail
9225  **/
9226 int i40e_close(struct net_device *netdev)
9227 {
9228 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9229 	struct i40e_vsi *vsi = np->vsi;
9230 
9231 	i40e_vsi_close(vsi);
9232 
9233 	return 0;
9234 }
9235 
9236 /**
9237  * i40e_do_reset - Start a PF or Core Reset sequence
9238  * @pf: board private structure
9239  * @reset_flags: which reset is requested
9240  * @lock_acquired: indicates whether or not the lock has been acquired
9241  * before this function was called.
9242  *
9243  * The essential difference in resets is that the PF Reset
9244  * doesn't clear the packet buffers, doesn't reset the PE
9245  * firmware, and doesn't bother the other PFs on the chip.
9246  **/
9247 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9248 {
9249 	u32 val;
9250 
9251 	/* do the biggest reset indicated */
9252 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9253 
9254 		/* Request a Global Reset
9255 		 *
9256 		 * This will start the chip's countdown to the actual full
9257 		 * chip reset event, and a warning interrupt to be sent
9258 		 * to all PFs, including the requestor.  Our handler
9259 		 * for the warning interrupt will deal with the shutdown
9260 		 * and recovery of the switch setup.
9261 		 */
9262 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9263 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9264 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9265 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9266 
9267 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9268 
9269 		/* Request a Core Reset
9270 		 *
9271 		 * Same as Global Reset, except does *not* include the MAC/PHY
9272 		 */
9273 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9274 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9275 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9276 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9277 		i40e_flush(&pf->hw);
9278 
9279 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9280 
9281 		/* Request a PF Reset
9282 		 *
9283 		 * Resets only the PF-specific registers
9284 		 *
9285 		 * This goes directly to the tear-down and rebuild of
9286 		 * the switch, since we need to do all the recovery as
9287 		 * for the Core Reset.
9288 		 */
9289 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9290 		i40e_handle_reset_warning(pf, lock_acquired);
9291 
9292 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9293 		/* Request a PF Reset
9294 		 *
9295 		 * Resets PF and reinitializes PFs VSI.
9296 		 */
9297 		i40e_prep_for_reset(pf);
9298 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9299 		dev_info(&pf->pdev->dev,
9300 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9301 			 "FW LLDP is disabled\n" :
9302 			 "FW LLDP is enabled\n");
9303 
9304 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9305 		int v;
9306 
9307 		/* Find the VSI(s) that requested a re-init */
9308 		dev_info(&pf->pdev->dev,
9309 			 "VSI reinit requested\n");
9310 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9311 			struct i40e_vsi *vsi = pf->vsi[v];
9312 
9313 			if (vsi != NULL &&
9314 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9315 					       vsi->state))
9316 				i40e_vsi_reinit_locked(pf->vsi[v]);
9317 		}
9318 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9319 		int v;
9320 
9321 		/* Find the VSI(s) that needs to be brought down */
9322 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9323 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9324 			struct i40e_vsi *vsi = pf->vsi[v];
9325 
9326 			if (vsi != NULL &&
9327 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9328 					       vsi->state)) {
9329 				set_bit(__I40E_VSI_DOWN, vsi->state);
9330 				i40e_down(vsi);
9331 			}
9332 		}
9333 	} else {
9334 		dev_info(&pf->pdev->dev,
9335 			 "bad reset request 0x%08x\n", reset_flags);
9336 	}
9337 }
9338 
9339 #ifdef CONFIG_I40E_DCB
9340 /**
9341  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9342  * @pf: board private structure
9343  * @old_cfg: current DCB config
9344  * @new_cfg: new DCB config
9345  **/
9346 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9347 			    struct i40e_dcbx_config *old_cfg,
9348 			    struct i40e_dcbx_config *new_cfg)
9349 {
9350 	bool need_reconfig = false;
9351 
9352 	/* Check if ETS configuration has changed */
9353 	if (memcmp(&new_cfg->etscfg,
9354 		   &old_cfg->etscfg,
9355 		   sizeof(new_cfg->etscfg))) {
9356 		/* If Priority Table has changed reconfig is needed */
9357 		if (memcmp(&new_cfg->etscfg.prioritytable,
9358 			   &old_cfg->etscfg.prioritytable,
9359 			   sizeof(new_cfg->etscfg.prioritytable))) {
9360 			need_reconfig = true;
9361 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9362 		}
9363 
9364 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9365 			   &old_cfg->etscfg.tcbwtable,
9366 			   sizeof(new_cfg->etscfg.tcbwtable)))
9367 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9368 
9369 		if (memcmp(&new_cfg->etscfg.tsatable,
9370 			   &old_cfg->etscfg.tsatable,
9371 			   sizeof(new_cfg->etscfg.tsatable)))
9372 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9373 	}
9374 
9375 	/* Check if PFC configuration has changed */
9376 	if (memcmp(&new_cfg->pfc,
9377 		   &old_cfg->pfc,
9378 		   sizeof(new_cfg->pfc))) {
9379 		need_reconfig = true;
9380 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9381 	}
9382 
9383 	/* Check if APP Table has changed */
9384 	if (memcmp(&new_cfg->app,
9385 		   &old_cfg->app,
9386 		   sizeof(new_cfg->app))) {
9387 		need_reconfig = true;
9388 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9389 	}
9390 
9391 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9392 	return need_reconfig;
9393 }
9394 
9395 /**
9396  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9397  * @pf: board private structure
9398  * @e: event info posted on ARQ
9399  **/
9400 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9401 				  struct i40e_arq_event_info *e)
9402 {
9403 	struct i40e_aqc_lldp_get_mib *mib =
9404 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9405 	struct i40e_hw *hw = &pf->hw;
9406 	struct i40e_dcbx_config tmp_dcbx_cfg;
9407 	bool need_reconfig = false;
9408 	int ret = 0;
9409 	u8 type;
9410 
9411 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9412 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9413 	    (hw->phy.link_info.link_speed &
9414 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9415 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9416 		/* let firmware decide if the DCB should be disabled */
9417 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9418 
9419 	/* Not DCB capable or capability disabled */
9420 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9421 		return ret;
9422 
9423 	/* Ignore if event is not for Nearest Bridge */
9424 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9425 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9426 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9427 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9428 		return ret;
9429 
9430 	/* Check MIB Type and return if event for Remote MIB update */
9431 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9432 	dev_dbg(&pf->pdev->dev,
9433 		"LLDP event mib type %s\n", type ? "remote" : "local");
9434 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9435 		/* Update the remote cached instance and return */
9436 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9437 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9438 				&hw->remote_dcbx_config);
9439 		goto exit;
9440 	}
9441 
9442 	/* Store the old configuration */
9443 	tmp_dcbx_cfg = hw->local_dcbx_config;
9444 
9445 	/* Reset the old DCBx configuration data */
9446 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9447 	/* Get updated DCBX data from firmware */
9448 	ret = i40e_get_dcb_config(&pf->hw);
9449 	if (ret) {
9450 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9451 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9452 		    (hw->phy.link_info.link_speed &
9453 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9454 			dev_warn(&pf->pdev->dev,
9455 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9456 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9457 		} else {
9458 			dev_info(&pf->pdev->dev,
9459 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9460 				 ERR_PTR(ret),
9461 				 i40e_aq_str(&pf->hw,
9462 					     pf->hw.aq.asq_last_status));
9463 		}
9464 		goto exit;
9465 	}
9466 
9467 	/* No change detected in DCBX configs */
9468 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9469 		    sizeof(tmp_dcbx_cfg))) {
9470 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9471 		goto exit;
9472 	}
9473 
9474 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9475 					       &hw->local_dcbx_config);
9476 
9477 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9478 
9479 	if (!need_reconfig)
9480 		goto exit;
9481 
9482 	/* Enable DCB tagging only when more than one TC */
9483 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9484 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9485 	else
9486 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9487 
9488 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9489 	/* Reconfiguration needed quiesce all VSIs */
9490 	i40e_pf_quiesce_all_vsi(pf);
9491 
9492 	/* Changes in configuration update VEB/VSI */
9493 	i40e_dcb_reconfigure(pf);
9494 
9495 	ret = i40e_resume_port_tx(pf);
9496 
9497 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9498 	/* In case of error no point in resuming VSIs */
9499 	if (ret)
9500 		goto exit;
9501 
9502 	/* Wait for the PF's queues to be disabled */
9503 	ret = i40e_pf_wait_queues_disabled(pf);
9504 	if (ret) {
9505 		/* Schedule PF reset to recover */
9506 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9507 		i40e_service_event_schedule(pf);
9508 	} else {
9509 		i40e_pf_unquiesce_all_vsi(pf);
9510 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9511 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9512 	}
9513 
9514 exit:
9515 	return ret;
9516 }
9517 #endif /* CONFIG_I40E_DCB */
9518 
9519 /**
9520  * i40e_do_reset_safe - Protected reset path for userland calls.
9521  * @pf: board private structure
9522  * @reset_flags: which reset is requested
9523  *
9524  **/
9525 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9526 {
9527 	rtnl_lock();
9528 	i40e_do_reset(pf, reset_flags, true);
9529 	rtnl_unlock();
9530 }
9531 
9532 /**
9533  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9534  * @pf: board private structure
9535  * @e: event info posted on ARQ
9536  *
9537  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9538  * and VF queues
9539  **/
9540 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9541 					   struct i40e_arq_event_info *e)
9542 {
9543 	struct i40e_aqc_lan_overflow *data =
9544 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9545 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9546 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9547 	struct i40e_hw *hw = &pf->hw;
9548 	struct i40e_vf *vf;
9549 	u16 vf_id;
9550 
9551 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9552 		queue, qtx_ctl);
9553 
9554 	/* Queue belongs to VF, find the VF and issue VF reset */
9555 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9556 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9557 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9558 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9559 		vf_id -= hw->func_caps.vf_base_id;
9560 		vf = &pf->vf[vf_id];
9561 		i40e_vc_notify_vf_reset(vf);
9562 		/* Allow VF to process pending reset notification */
9563 		msleep(20);
9564 		i40e_reset_vf(vf, false);
9565 	}
9566 }
9567 
9568 /**
9569  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9570  * @pf: board private structure
9571  **/
9572 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9573 {
9574 	u32 val, fcnt_prog;
9575 
9576 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9577 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9578 	return fcnt_prog;
9579 }
9580 
9581 /**
9582  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9583  * @pf: board private structure
9584  **/
9585 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9586 {
9587 	u32 val, fcnt_prog;
9588 
9589 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9590 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9591 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9592 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9593 	return fcnt_prog;
9594 }
9595 
9596 /**
9597  * i40e_get_global_fd_count - Get total FD filters programmed on device
9598  * @pf: board private structure
9599  **/
9600 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9601 {
9602 	u32 val, fcnt_prog;
9603 
9604 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9605 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9606 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9607 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9608 	return fcnt_prog;
9609 }
9610 
9611 /**
9612  * i40e_reenable_fdir_sb - Restore FDir SB capability
9613  * @pf: board private structure
9614  **/
9615 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9616 {
9617 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9618 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9619 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9620 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9621 }
9622 
9623 /**
9624  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9625  * @pf: board private structure
9626  **/
9627 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9628 {
9629 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9630 		/* ATR uses the same filtering logic as SB rules. It only
9631 		 * functions properly if the input set mask is at the default
9632 		 * settings. It is safe to restore the default input set
9633 		 * because there are no active TCPv4 filter rules.
9634 		 */
9635 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9636 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9637 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9638 
9639 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9640 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9641 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9642 	}
9643 }
9644 
9645 /**
9646  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9647  * @pf: board private structure
9648  * @filter: FDir filter to remove
9649  */
9650 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9651 				       struct i40e_fdir_filter *filter)
9652 {
9653 	/* Update counters */
9654 	pf->fdir_pf_active_filters--;
9655 	pf->fd_inv = 0;
9656 
9657 	switch (filter->flow_type) {
9658 	case TCP_V4_FLOW:
9659 		pf->fd_tcp4_filter_cnt--;
9660 		break;
9661 	case UDP_V4_FLOW:
9662 		pf->fd_udp4_filter_cnt--;
9663 		break;
9664 	case SCTP_V4_FLOW:
9665 		pf->fd_sctp4_filter_cnt--;
9666 		break;
9667 	case TCP_V6_FLOW:
9668 		pf->fd_tcp6_filter_cnt--;
9669 		break;
9670 	case UDP_V6_FLOW:
9671 		pf->fd_udp6_filter_cnt--;
9672 		break;
9673 	case SCTP_V6_FLOW:
9674 		pf->fd_udp6_filter_cnt--;
9675 		break;
9676 	case IP_USER_FLOW:
9677 		switch (filter->ipl4_proto) {
9678 		case IPPROTO_TCP:
9679 			pf->fd_tcp4_filter_cnt--;
9680 			break;
9681 		case IPPROTO_UDP:
9682 			pf->fd_udp4_filter_cnt--;
9683 			break;
9684 		case IPPROTO_SCTP:
9685 			pf->fd_sctp4_filter_cnt--;
9686 			break;
9687 		case IPPROTO_IP:
9688 			pf->fd_ip4_filter_cnt--;
9689 			break;
9690 		}
9691 		break;
9692 	case IPV6_USER_FLOW:
9693 		switch (filter->ipl4_proto) {
9694 		case IPPROTO_TCP:
9695 			pf->fd_tcp6_filter_cnt--;
9696 			break;
9697 		case IPPROTO_UDP:
9698 			pf->fd_udp6_filter_cnt--;
9699 			break;
9700 		case IPPROTO_SCTP:
9701 			pf->fd_sctp6_filter_cnt--;
9702 			break;
9703 		case IPPROTO_IP:
9704 			pf->fd_ip6_filter_cnt--;
9705 			break;
9706 		}
9707 		break;
9708 	}
9709 
9710 	/* Remove the filter from the list and free memory */
9711 	hlist_del(&filter->fdir_node);
9712 	kfree(filter);
9713 }
9714 
9715 /**
9716  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9717  * @pf: board private structure
9718  **/
9719 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9720 {
9721 	struct i40e_fdir_filter *filter;
9722 	u32 fcnt_prog, fcnt_avail;
9723 	struct hlist_node *node;
9724 
9725 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9726 		return;
9727 
9728 	/* Check if we have enough room to re-enable FDir SB capability. */
9729 	fcnt_prog = i40e_get_global_fd_count(pf);
9730 	fcnt_avail = pf->fdir_pf_filter_count;
9731 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9732 	    (pf->fd_add_err == 0) ||
9733 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9734 		i40e_reenable_fdir_sb(pf);
9735 
9736 	/* We should wait for even more space before re-enabling ATR.
9737 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9738 	 * rules active.
9739 	 */
9740 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9741 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9742 		i40e_reenable_fdir_atr(pf);
9743 
9744 	/* if hw had a problem adding a filter, delete it */
9745 	if (pf->fd_inv > 0) {
9746 		hlist_for_each_entry_safe(filter, node,
9747 					  &pf->fdir_filter_list, fdir_node)
9748 			if (filter->fd_id == pf->fd_inv)
9749 				i40e_delete_invalid_filter(pf, filter);
9750 	}
9751 }
9752 
9753 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9754 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9755 /**
9756  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9757  * @pf: board private structure
9758  **/
9759 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9760 {
9761 	unsigned long min_flush_time;
9762 	int flush_wait_retry = 50;
9763 	bool disable_atr = false;
9764 	int fd_room;
9765 	int reg;
9766 
9767 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9768 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9769 		return;
9770 
9771 	/* If the flush is happening too quick and we have mostly SB rules we
9772 	 * should not re-enable ATR for some time.
9773 	 */
9774 	min_flush_time = pf->fd_flush_timestamp +
9775 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9776 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9777 
9778 	if (!(time_after(jiffies, min_flush_time)) &&
9779 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9780 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9781 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9782 		disable_atr = true;
9783 	}
9784 
9785 	pf->fd_flush_timestamp = jiffies;
9786 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9787 	/* flush all filters */
9788 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9789 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9790 	i40e_flush(&pf->hw);
9791 	pf->fd_flush_cnt++;
9792 	pf->fd_add_err = 0;
9793 	do {
9794 		/* Check FD flush status every 5-6msec */
9795 		usleep_range(5000, 6000);
9796 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9797 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9798 			break;
9799 	} while (flush_wait_retry--);
9800 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9801 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9802 	} else {
9803 		/* replay sideband filters */
9804 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9805 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9806 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9807 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9808 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9809 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9810 	}
9811 }
9812 
9813 /**
9814  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9815  * @pf: board private structure
9816  **/
9817 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9818 {
9819 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9820 }
9821 
9822 /**
9823  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9824  * @pf: board private structure
9825  **/
9826 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9827 {
9828 
9829 	/* if interface is down do nothing */
9830 	if (test_bit(__I40E_DOWN, pf->state))
9831 		return;
9832 
9833 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9834 		i40e_fdir_flush_and_replay(pf);
9835 
9836 	i40e_fdir_check_and_reenable(pf);
9837 
9838 }
9839 
9840 /**
9841  * i40e_vsi_link_event - notify VSI of a link event
9842  * @vsi: vsi to be notified
9843  * @link_up: link up or down
9844  **/
9845 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9846 {
9847 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9848 		return;
9849 
9850 	switch (vsi->type) {
9851 	case I40E_VSI_MAIN:
9852 		if (!vsi->netdev || !vsi->netdev_registered)
9853 			break;
9854 
9855 		if (link_up) {
9856 			netif_carrier_on(vsi->netdev);
9857 			netif_tx_wake_all_queues(vsi->netdev);
9858 		} else {
9859 			netif_carrier_off(vsi->netdev);
9860 			netif_tx_stop_all_queues(vsi->netdev);
9861 		}
9862 		break;
9863 
9864 	case I40E_VSI_SRIOV:
9865 	case I40E_VSI_VMDQ2:
9866 	case I40E_VSI_CTRL:
9867 	case I40E_VSI_IWARP:
9868 	case I40E_VSI_MIRROR:
9869 	default:
9870 		/* there is no notification for other VSIs */
9871 		break;
9872 	}
9873 }
9874 
9875 /**
9876  * i40e_veb_link_event - notify elements on the veb of a link event
9877  * @veb: veb to be notified
9878  * @link_up: link up or down
9879  **/
9880 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9881 {
9882 	struct i40e_pf *pf;
9883 	int i;
9884 
9885 	if (!veb || !veb->pf)
9886 		return;
9887 	pf = veb->pf;
9888 
9889 	/* depth first... */
9890 	for (i = 0; i < I40E_MAX_VEB; i++)
9891 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9892 			i40e_veb_link_event(pf->veb[i], link_up);
9893 
9894 	/* ... now the local VSIs */
9895 	for (i = 0; i < pf->num_alloc_vsi; i++)
9896 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9897 			i40e_vsi_link_event(pf->vsi[i], link_up);
9898 }
9899 
9900 /**
9901  * i40e_link_event - Update netif_carrier status
9902  * @pf: board private structure
9903  **/
9904 static void i40e_link_event(struct i40e_pf *pf)
9905 {
9906 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9907 	u8 new_link_speed, old_link_speed;
9908 	bool new_link, old_link;
9909 	int status;
9910 #ifdef CONFIG_I40E_DCB
9911 	int err;
9912 #endif /* CONFIG_I40E_DCB */
9913 
9914 	/* set this to force the get_link_status call to refresh state */
9915 	pf->hw.phy.get_link_info = true;
9916 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9917 	status = i40e_get_link_status(&pf->hw, &new_link);
9918 
9919 	/* On success, disable temp link polling */
9920 	if (status == 0) {
9921 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9922 	} else {
9923 		/* Enable link polling temporarily until i40e_get_link_status
9924 		 * returns 0
9925 		 */
9926 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9927 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9928 			status);
9929 		return;
9930 	}
9931 
9932 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9933 	new_link_speed = pf->hw.phy.link_info.link_speed;
9934 
9935 	if (new_link == old_link &&
9936 	    new_link_speed == old_link_speed &&
9937 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9938 	     new_link == netif_carrier_ok(vsi->netdev)))
9939 		return;
9940 
9941 	i40e_print_link_message(vsi, new_link);
9942 
9943 	/* Notify the base of the switch tree connected to
9944 	 * the link.  Floating VEBs are not notified.
9945 	 */
9946 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9947 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9948 	else
9949 		i40e_vsi_link_event(vsi, new_link);
9950 
9951 	if (pf->vf)
9952 		i40e_vc_notify_link_state(pf);
9953 
9954 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9955 		i40e_ptp_set_increment(pf);
9956 #ifdef CONFIG_I40E_DCB
9957 	if (new_link == old_link)
9958 		return;
9959 	/* Not SW DCB so firmware will take care of default settings */
9960 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9961 		return;
9962 
9963 	/* We cover here only link down, as after link up in case of SW DCB
9964 	 * SW LLDP agent will take care of setting it up
9965 	 */
9966 	if (!new_link) {
9967 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9968 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9969 		err = i40e_dcb_sw_default_config(pf);
9970 		if (err) {
9971 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9972 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9973 		} else {
9974 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9975 				       DCB_CAP_DCBX_VER_IEEE;
9976 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9977 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9978 		}
9979 	}
9980 #endif /* CONFIG_I40E_DCB */
9981 }
9982 
9983 /**
9984  * i40e_watchdog_subtask - periodic checks not using event driven response
9985  * @pf: board private structure
9986  **/
9987 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9988 {
9989 	int i;
9990 
9991 	/* if interface is down do nothing */
9992 	if (test_bit(__I40E_DOWN, pf->state) ||
9993 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9994 		return;
9995 
9996 	/* make sure we don't do these things too often */
9997 	if (time_before(jiffies, (pf->service_timer_previous +
9998 				  pf->service_timer_period)))
9999 		return;
10000 	pf->service_timer_previous = jiffies;
10001 
10002 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10003 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10004 		i40e_link_event(pf);
10005 
10006 	/* Update the stats for active netdevs so the network stack
10007 	 * can look at updated numbers whenever it cares to
10008 	 */
10009 	for (i = 0; i < pf->num_alloc_vsi; i++)
10010 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10011 			i40e_update_stats(pf->vsi[i]);
10012 
10013 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10014 		/* Update the stats for the active switching components */
10015 		for (i = 0; i < I40E_MAX_VEB; i++)
10016 			if (pf->veb[i])
10017 				i40e_update_veb_stats(pf->veb[i]);
10018 	}
10019 
10020 	i40e_ptp_rx_hang(pf);
10021 	i40e_ptp_tx_hang(pf);
10022 }
10023 
10024 /**
10025  * i40e_reset_subtask - Set up for resetting the device and driver
10026  * @pf: board private structure
10027  **/
10028 static void i40e_reset_subtask(struct i40e_pf *pf)
10029 {
10030 	u32 reset_flags = 0;
10031 
10032 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10033 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10034 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10035 	}
10036 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10037 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10038 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10039 	}
10040 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10041 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10042 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10043 	}
10044 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10045 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10046 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10047 	}
10048 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10049 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10050 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10051 	}
10052 
10053 	/* If there's a recovery already waiting, it takes
10054 	 * precedence before starting a new reset sequence.
10055 	 */
10056 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10057 		i40e_prep_for_reset(pf);
10058 		i40e_reset(pf);
10059 		i40e_rebuild(pf, false, false);
10060 	}
10061 
10062 	/* If we're already down or resetting, just bail */
10063 	if (reset_flags &&
10064 	    !test_bit(__I40E_DOWN, pf->state) &&
10065 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10066 		i40e_do_reset(pf, reset_flags, false);
10067 	}
10068 }
10069 
10070 /**
10071  * i40e_handle_link_event - Handle link event
10072  * @pf: board private structure
10073  * @e: event info posted on ARQ
10074  **/
10075 static void i40e_handle_link_event(struct i40e_pf *pf,
10076 				   struct i40e_arq_event_info *e)
10077 {
10078 	struct i40e_aqc_get_link_status *status =
10079 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10080 
10081 	/* Do a new status request to re-enable LSE reporting
10082 	 * and load new status information into the hw struct
10083 	 * This completely ignores any state information
10084 	 * in the ARQ event info, instead choosing to always
10085 	 * issue the AQ update link status command.
10086 	 */
10087 	i40e_link_event(pf);
10088 
10089 	/* Check if module meets thermal requirements */
10090 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10091 		dev_err(&pf->pdev->dev,
10092 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10093 		dev_err(&pf->pdev->dev,
10094 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10095 	} else {
10096 		/* check for unqualified module, if link is down, suppress
10097 		 * the message if link was forced to be down.
10098 		 */
10099 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10100 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10101 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10102 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10103 			dev_err(&pf->pdev->dev,
10104 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10105 			dev_err(&pf->pdev->dev,
10106 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10107 		}
10108 	}
10109 }
10110 
10111 /**
10112  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10113  * @pf: board private structure
10114  **/
10115 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10116 {
10117 	struct i40e_arq_event_info event;
10118 	struct i40e_hw *hw = &pf->hw;
10119 	u16 pending, i = 0;
10120 	u16 opcode;
10121 	u32 oldval;
10122 	int ret;
10123 	u32 val;
10124 
10125 	/* Do not run clean AQ when PF reset fails */
10126 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10127 		return;
10128 
10129 	/* check for error indications */
10130 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10131 	oldval = val;
10132 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10133 		if (hw->debug_mask & I40E_DEBUG_AQ)
10134 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10135 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10136 	}
10137 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10138 		if (hw->debug_mask & I40E_DEBUG_AQ)
10139 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10140 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10141 		pf->arq_overflows++;
10142 	}
10143 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10144 		if (hw->debug_mask & I40E_DEBUG_AQ)
10145 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10146 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10147 	}
10148 	if (oldval != val)
10149 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10150 
10151 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10152 	oldval = val;
10153 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10154 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10155 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10156 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10157 	}
10158 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10159 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10160 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10161 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10162 	}
10163 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10164 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10165 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10166 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10167 	}
10168 	if (oldval != val)
10169 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10170 
10171 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10172 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10173 	if (!event.msg_buf)
10174 		return;
10175 
10176 	do {
10177 		ret = i40e_clean_arq_element(hw, &event, &pending);
10178 		if (ret == -EALREADY)
10179 			break;
10180 		else if (ret) {
10181 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10182 			break;
10183 		}
10184 
10185 		opcode = le16_to_cpu(event.desc.opcode);
10186 		switch (opcode) {
10187 
10188 		case i40e_aqc_opc_get_link_status:
10189 			rtnl_lock();
10190 			i40e_handle_link_event(pf, &event);
10191 			rtnl_unlock();
10192 			break;
10193 		case i40e_aqc_opc_send_msg_to_pf:
10194 			ret = i40e_vc_process_vf_msg(pf,
10195 					le16_to_cpu(event.desc.retval),
10196 					le32_to_cpu(event.desc.cookie_high),
10197 					le32_to_cpu(event.desc.cookie_low),
10198 					event.msg_buf,
10199 					event.msg_len);
10200 			break;
10201 		case i40e_aqc_opc_lldp_update_mib:
10202 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10203 #ifdef CONFIG_I40E_DCB
10204 			rtnl_lock();
10205 			i40e_handle_lldp_event(pf, &event);
10206 			rtnl_unlock();
10207 #endif /* CONFIG_I40E_DCB */
10208 			break;
10209 		case i40e_aqc_opc_event_lan_overflow:
10210 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10211 			i40e_handle_lan_overflow_event(pf, &event);
10212 			break;
10213 		case i40e_aqc_opc_send_msg_to_peer:
10214 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10215 			break;
10216 		case i40e_aqc_opc_nvm_erase:
10217 		case i40e_aqc_opc_nvm_update:
10218 		case i40e_aqc_opc_oem_post_update:
10219 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10220 				   "ARQ NVM operation 0x%04x completed\n",
10221 				   opcode);
10222 			break;
10223 		default:
10224 			dev_info(&pf->pdev->dev,
10225 				 "ARQ: Unknown event 0x%04x ignored\n",
10226 				 opcode);
10227 			break;
10228 		}
10229 	} while (i++ < I40E_AQ_WORK_LIMIT);
10230 
10231 	if (i < I40E_AQ_WORK_LIMIT)
10232 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10233 
10234 	/* re-enable Admin queue interrupt cause */
10235 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10236 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10237 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10238 	i40e_flush(hw);
10239 
10240 	kfree(event.msg_buf);
10241 }
10242 
10243 /**
10244  * i40e_verify_eeprom - make sure eeprom is good to use
10245  * @pf: board private structure
10246  **/
10247 static void i40e_verify_eeprom(struct i40e_pf *pf)
10248 {
10249 	int err;
10250 
10251 	err = i40e_diag_eeprom_test(&pf->hw);
10252 	if (err) {
10253 		/* retry in case of garbage read */
10254 		err = i40e_diag_eeprom_test(&pf->hw);
10255 		if (err) {
10256 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10257 				 err);
10258 			set_bit(__I40E_BAD_EEPROM, pf->state);
10259 		}
10260 	}
10261 
10262 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10263 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10264 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10265 	}
10266 }
10267 
10268 /**
10269  * i40e_enable_pf_switch_lb
10270  * @pf: pointer to the PF structure
10271  *
10272  * enable switch loop back or die - no point in a return value
10273  **/
10274 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10275 {
10276 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10277 	struct i40e_vsi_context ctxt;
10278 	int ret;
10279 
10280 	ctxt.seid = pf->main_vsi_seid;
10281 	ctxt.pf_num = pf->hw.pf_id;
10282 	ctxt.vf_num = 0;
10283 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10284 	if (ret) {
10285 		dev_info(&pf->pdev->dev,
10286 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10287 			 ERR_PTR(ret),
10288 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10289 		return;
10290 	}
10291 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10292 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10293 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10294 
10295 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10296 	if (ret) {
10297 		dev_info(&pf->pdev->dev,
10298 			 "update vsi switch failed, err %pe aq_err %s\n",
10299 			 ERR_PTR(ret),
10300 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10301 	}
10302 }
10303 
10304 /**
10305  * i40e_disable_pf_switch_lb
10306  * @pf: pointer to the PF structure
10307  *
10308  * disable switch loop back or die - no point in a return value
10309  **/
10310 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10311 {
10312 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10313 	struct i40e_vsi_context ctxt;
10314 	int ret;
10315 
10316 	ctxt.seid = pf->main_vsi_seid;
10317 	ctxt.pf_num = pf->hw.pf_id;
10318 	ctxt.vf_num = 0;
10319 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10320 	if (ret) {
10321 		dev_info(&pf->pdev->dev,
10322 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10323 			 ERR_PTR(ret),
10324 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10325 		return;
10326 	}
10327 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10328 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10329 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10330 
10331 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10332 	if (ret) {
10333 		dev_info(&pf->pdev->dev,
10334 			 "update vsi switch failed, err %pe aq_err %s\n",
10335 			 ERR_PTR(ret),
10336 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10337 	}
10338 }
10339 
10340 /**
10341  * i40e_config_bridge_mode - Configure the HW bridge mode
10342  * @veb: pointer to the bridge instance
10343  *
10344  * Configure the loop back mode for the LAN VSI that is downlink to the
10345  * specified HW bridge instance. It is expected this function is called
10346  * when a new HW bridge is instantiated.
10347  **/
10348 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10349 {
10350 	struct i40e_pf *pf = veb->pf;
10351 
10352 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10353 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10354 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10355 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10356 		i40e_disable_pf_switch_lb(pf);
10357 	else
10358 		i40e_enable_pf_switch_lb(pf);
10359 }
10360 
10361 /**
10362  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10363  * @veb: pointer to the VEB instance
10364  *
10365  * This is a recursive function that first builds the attached VSIs then
10366  * recurses in to build the next layer of VEB.  We track the connections
10367  * through our own index numbers because the seid's from the HW could
10368  * change across the reset.
10369  **/
10370 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10371 {
10372 	struct i40e_vsi *ctl_vsi = NULL;
10373 	struct i40e_pf *pf = veb->pf;
10374 	int v, veb_idx;
10375 	int ret;
10376 
10377 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10378 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10379 		if (pf->vsi[v] &&
10380 		    pf->vsi[v]->veb_idx == veb->idx &&
10381 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10382 			ctl_vsi = pf->vsi[v];
10383 			break;
10384 		}
10385 	}
10386 	if (!ctl_vsi) {
10387 		dev_info(&pf->pdev->dev,
10388 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10389 		ret = -ENOENT;
10390 		goto end_reconstitute;
10391 	}
10392 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10393 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10394 	ret = i40e_add_vsi(ctl_vsi);
10395 	if (ret) {
10396 		dev_info(&pf->pdev->dev,
10397 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10398 			 veb->idx, ret);
10399 		goto end_reconstitute;
10400 	}
10401 	i40e_vsi_reset_stats(ctl_vsi);
10402 
10403 	/* create the VEB in the switch and move the VSI onto the VEB */
10404 	ret = i40e_add_veb(veb, ctl_vsi);
10405 	if (ret)
10406 		goto end_reconstitute;
10407 
10408 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10409 		veb->bridge_mode = BRIDGE_MODE_VEB;
10410 	else
10411 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10412 	i40e_config_bridge_mode(veb);
10413 
10414 	/* create the remaining VSIs attached to this VEB */
10415 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10416 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10417 			continue;
10418 
10419 		if (pf->vsi[v]->veb_idx == veb->idx) {
10420 			struct i40e_vsi *vsi = pf->vsi[v];
10421 
10422 			vsi->uplink_seid = veb->seid;
10423 			ret = i40e_add_vsi(vsi);
10424 			if (ret) {
10425 				dev_info(&pf->pdev->dev,
10426 					 "rebuild of vsi_idx %d failed: %d\n",
10427 					 v, ret);
10428 				goto end_reconstitute;
10429 			}
10430 			i40e_vsi_reset_stats(vsi);
10431 		}
10432 	}
10433 
10434 	/* create any VEBs attached to this VEB - RECURSION */
10435 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10436 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10437 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10438 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10439 			if (ret)
10440 				break;
10441 		}
10442 	}
10443 
10444 end_reconstitute:
10445 	return ret;
10446 }
10447 
10448 /**
10449  * i40e_get_capabilities - get info about the HW
10450  * @pf: the PF struct
10451  * @list_type: AQ capability to be queried
10452  **/
10453 static int i40e_get_capabilities(struct i40e_pf *pf,
10454 				 enum i40e_admin_queue_opc list_type)
10455 {
10456 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10457 	u16 data_size;
10458 	int buf_len;
10459 	int err;
10460 
10461 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10462 	do {
10463 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10464 		if (!cap_buf)
10465 			return -ENOMEM;
10466 
10467 		/* this loads the data into the hw struct for us */
10468 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10469 						    &data_size, list_type,
10470 						    NULL);
10471 		/* data loaded, buffer no longer needed */
10472 		kfree(cap_buf);
10473 
10474 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10475 			/* retry with a larger buffer */
10476 			buf_len = data_size;
10477 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10478 			dev_info(&pf->pdev->dev,
10479 				 "capability discovery failed, err %pe aq_err %s\n",
10480 				 ERR_PTR(err),
10481 				 i40e_aq_str(&pf->hw,
10482 					     pf->hw.aq.asq_last_status));
10483 			return -ENODEV;
10484 		}
10485 	} while (err);
10486 
10487 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10488 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10489 			dev_info(&pf->pdev->dev,
10490 				 "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",
10491 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10492 				 pf->hw.func_caps.num_msix_vectors,
10493 				 pf->hw.func_caps.num_msix_vectors_vf,
10494 				 pf->hw.func_caps.fd_filters_guaranteed,
10495 				 pf->hw.func_caps.fd_filters_best_effort,
10496 				 pf->hw.func_caps.num_tx_qp,
10497 				 pf->hw.func_caps.num_vsis);
10498 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10499 			dev_info(&pf->pdev->dev,
10500 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10501 				 pf->hw.dev_caps.switch_mode,
10502 				 pf->hw.dev_caps.valid_functions);
10503 			dev_info(&pf->pdev->dev,
10504 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10505 				 pf->hw.dev_caps.sr_iov_1_1,
10506 				 pf->hw.dev_caps.num_vfs);
10507 			dev_info(&pf->pdev->dev,
10508 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10509 				 pf->hw.dev_caps.num_vsis,
10510 				 pf->hw.dev_caps.num_rx_qp,
10511 				 pf->hw.dev_caps.num_tx_qp);
10512 		}
10513 	}
10514 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10515 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10516 		       + pf->hw.func_caps.num_vfs)
10517 		if (pf->hw.revision_id == 0 &&
10518 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10519 			dev_info(&pf->pdev->dev,
10520 				 "got num_vsis %d, setting num_vsis to %d\n",
10521 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10522 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10523 		}
10524 	}
10525 	return 0;
10526 }
10527 
10528 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10529 
10530 /**
10531  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10532  * @pf: board private structure
10533  **/
10534 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10535 {
10536 	struct i40e_vsi *vsi;
10537 
10538 	/* quick workaround for an NVM issue that leaves a critical register
10539 	 * uninitialized
10540 	 */
10541 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10542 		static const u32 hkey[] = {
10543 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10544 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10545 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10546 			0x95b3a76d};
10547 		int i;
10548 
10549 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10550 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10551 	}
10552 
10553 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10554 		return;
10555 
10556 	/* find existing VSI and see if it needs configuring */
10557 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10558 
10559 	/* create a new VSI if none exists */
10560 	if (!vsi) {
10561 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10562 				     pf->vsi[pf->lan_vsi]->seid, 0);
10563 		if (!vsi) {
10564 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10565 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10566 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10567 			return;
10568 		}
10569 	}
10570 
10571 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10572 }
10573 
10574 /**
10575  * i40e_fdir_teardown - release the Flow Director resources
10576  * @pf: board private structure
10577  **/
10578 static void i40e_fdir_teardown(struct i40e_pf *pf)
10579 {
10580 	struct i40e_vsi *vsi;
10581 
10582 	i40e_fdir_filter_exit(pf);
10583 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10584 	if (vsi)
10585 		i40e_vsi_release(vsi);
10586 }
10587 
10588 /**
10589  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10590  * @vsi: PF main vsi
10591  * @seid: seid of main or channel VSIs
10592  *
10593  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10594  * existed before reset
10595  **/
10596 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10597 {
10598 	struct i40e_cloud_filter *cfilter;
10599 	struct i40e_pf *pf = vsi->back;
10600 	struct hlist_node *node;
10601 	int ret;
10602 
10603 	/* Add cloud filters back if they exist */
10604 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10605 				  cloud_node) {
10606 		if (cfilter->seid != seid)
10607 			continue;
10608 
10609 		if (cfilter->dst_port)
10610 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10611 								true);
10612 		else
10613 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10614 
10615 		if (ret) {
10616 			dev_dbg(&pf->pdev->dev,
10617 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10618 				ERR_PTR(ret),
10619 				i40e_aq_str(&pf->hw,
10620 					    pf->hw.aq.asq_last_status));
10621 			return ret;
10622 		}
10623 	}
10624 	return 0;
10625 }
10626 
10627 /**
10628  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10629  * @vsi: PF main vsi
10630  *
10631  * Rebuilds channel VSIs if they existed before reset
10632  **/
10633 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10634 {
10635 	struct i40e_channel *ch, *ch_tmp;
10636 	int ret;
10637 
10638 	if (list_empty(&vsi->ch_list))
10639 		return 0;
10640 
10641 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10642 		if (!ch->initialized)
10643 			break;
10644 		/* Proceed with creation of channel (VMDq2) VSI */
10645 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10646 		if (ret) {
10647 			dev_info(&vsi->back->pdev->dev,
10648 				 "failed to rebuild channels using uplink_seid %u\n",
10649 				 vsi->uplink_seid);
10650 			return ret;
10651 		}
10652 		/* Reconfigure TX queues using QTX_CTL register */
10653 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10654 		if (ret) {
10655 			dev_info(&vsi->back->pdev->dev,
10656 				 "failed to configure TX rings for channel %u\n",
10657 				 ch->seid);
10658 			return ret;
10659 		}
10660 		/* update 'next_base_queue' */
10661 		vsi->next_base_queue = vsi->next_base_queue +
10662 							ch->num_queue_pairs;
10663 		if (ch->max_tx_rate) {
10664 			u64 credits = ch->max_tx_rate;
10665 
10666 			if (i40e_set_bw_limit(vsi, ch->seid,
10667 					      ch->max_tx_rate))
10668 				return -EINVAL;
10669 
10670 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10671 			dev_dbg(&vsi->back->pdev->dev,
10672 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10673 				ch->max_tx_rate,
10674 				credits,
10675 				ch->seid);
10676 		}
10677 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10678 		if (ret) {
10679 			dev_dbg(&vsi->back->pdev->dev,
10680 				"Failed to rebuild cloud filters for channel VSI %u\n",
10681 				ch->seid);
10682 			return ret;
10683 		}
10684 	}
10685 	return 0;
10686 }
10687 
10688 /**
10689  * i40e_clean_xps_state - clean xps state for every tx_ring
10690  * @vsi: ptr to the VSI
10691  **/
10692 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10693 {
10694 	int i;
10695 
10696 	if (vsi->tx_rings)
10697 		for (i = 0; i < vsi->num_queue_pairs; i++)
10698 			if (vsi->tx_rings[i])
10699 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10700 					  vsi->tx_rings[i]->state);
10701 }
10702 
10703 /**
10704  * i40e_prep_for_reset - prep for the core to reset
10705  * @pf: board private structure
10706  *
10707  * Close up the VFs and other things in prep for PF Reset.
10708   **/
10709 static void i40e_prep_for_reset(struct i40e_pf *pf)
10710 {
10711 	struct i40e_hw *hw = &pf->hw;
10712 	int ret = 0;
10713 	u32 v;
10714 
10715 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10716 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10717 		return;
10718 	if (i40e_check_asq_alive(&pf->hw))
10719 		i40e_vc_notify_reset(pf);
10720 
10721 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10722 
10723 	/* quiesce the VSIs and their queues that are not already DOWN */
10724 	i40e_pf_quiesce_all_vsi(pf);
10725 
10726 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10727 		if (pf->vsi[v]) {
10728 			i40e_clean_xps_state(pf->vsi[v]);
10729 			pf->vsi[v]->seid = 0;
10730 		}
10731 	}
10732 
10733 	i40e_shutdown_adminq(&pf->hw);
10734 
10735 	/* call shutdown HMC */
10736 	if (hw->hmc.hmc_obj) {
10737 		ret = i40e_shutdown_lan_hmc(hw);
10738 		if (ret)
10739 			dev_warn(&pf->pdev->dev,
10740 				 "shutdown_lan_hmc failed: %d\n", ret);
10741 	}
10742 
10743 	/* Save the current PTP time so that we can restore the time after the
10744 	 * reset completes.
10745 	 */
10746 	i40e_ptp_save_hw_time(pf);
10747 }
10748 
10749 /**
10750  * i40e_send_version - update firmware with driver version
10751  * @pf: PF struct
10752  */
10753 static void i40e_send_version(struct i40e_pf *pf)
10754 {
10755 	struct i40e_driver_version dv;
10756 
10757 	dv.major_version = 0xff;
10758 	dv.minor_version = 0xff;
10759 	dv.build_version = 0xff;
10760 	dv.subbuild_version = 0;
10761 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10762 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10763 }
10764 
10765 /**
10766  * i40e_get_oem_version - get OEM specific version information
10767  * @hw: pointer to the hardware structure
10768  **/
10769 static void i40e_get_oem_version(struct i40e_hw *hw)
10770 {
10771 	u16 block_offset = 0xffff;
10772 	u16 block_length = 0;
10773 	u16 capabilities = 0;
10774 	u16 gen_snap = 0;
10775 	u16 release = 0;
10776 
10777 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10778 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10779 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10780 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10781 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10782 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10783 #define I40E_NVM_OEM_LENGTH			3
10784 
10785 	/* Check if pointer to OEM version block is valid. */
10786 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10787 	if (block_offset == 0xffff)
10788 		return;
10789 
10790 	/* Check if OEM version block has correct length. */
10791 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10792 			   &block_length);
10793 	if (block_length < I40E_NVM_OEM_LENGTH)
10794 		return;
10795 
10796 	/* Check if OEM version format is as expected. */
10797 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10798 			   &capabilities);
10799 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10800 		return;
10801 
10802 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10803 			   &gen_snap);
10804 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10805 			   &release);
10806 	hw->nvm.oem_ver =
10807 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10808 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10809 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10810 }
10811 
10812 /**
10813  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10814  * @pf: board private structure
10815  **/
10816 static int i40e_reset(struct i40e_pf *pf)
10817 {
10818 	struct i40e_hw *hw = &pf->hw;
10819 	int ret;
10820 
10821 	ret = i40e_pf_reset(hw);
10822 	if (ret) {
10823 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10824 		set_bit(__I40E_RESET_FAILED, pf->state);
10825 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10826 	} else {
10827 		pf->pfr_count++;
10828 	}
10829 	return ret;
10830 }
10831 
10832 /**
10833  * i40e_rebuild - rebuild using a saved config
10834  * @pf: board private structure
10835  * @reinit: if the Main VSI needs to re-initialized.
10836  * @lock_acquired: indicates whether or not the lock has been acquired
10837  * before this function was called.
10838  **/
10839 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10840 {
10841 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10842 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10843 	struct i40e_hw *hw = &pf->hw;
10844 	int ret;
10845 	u32 val;
10846 	int v;
10847 
10848 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10849 	    is_recovery_mode_reported)
10850 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10851 
10852 	if (test_bit(__I40E_DOWN, pf->state) &&
10853 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10854 		goto clear_recovery;
10855 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10856 
10857 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10858 	ret = i40e_init_adminq(&pf->hw);
10859 	if (ret) {
10860 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10861 			 ERR_PTR(ret),
10862 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10863 		goto clear_recovery;
10864 	}
10865 	i40e_get_oem_version(&pf->hw);
10866 
10867 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10868 		/* The following delay is necessary for firmware update. */
10869 		mdelay(1000);
10870 	}
10871 
10872 	/* re-verify the eeprom if we just had an EMP reset */
10873 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10874 		i40e_verify_eeprom(pf);
10875 
10876 	/* if we are going out of or into recovery mode we have to act
10877 	 * accordingly with regard to resources initialization
10878 	 * and deinitialization
10879 	 */
10880 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10881 		if (i40e_get_capabilities(pf,
10882 					  i40e_aqc_opc_list_func_capabilities))
10883 			goto end_unlock;
10884 
10885 		if (is_recovery_mode_reported) {
10886 			/* we're staying in recovery mode so we'll reinitialize
10887 			 * misc vector here
10888 			 */
10889 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10890 				goto end_unlock;
10891 		} else {
10892 			if (!lock_acquired)
10893 				rtnl_lock();
10894 			/* we're going out of recovery mode so we'll free
10895 			 * the IRQ allocated specifically for recovery mode
10896 			 * and restore the interrupt scheme
10897 			 */
10898 			free_irq(pf->pdev->irq, pf);
10899 			i40e_clear_interrupt_scheme(pf);
10900 			if (i40e_restore_interrupt_scheme(pf))
10901 				goto end_unlock;
10902 		}
10903 
10904 		/* tell the firmware that we're starting */
10905 		i40e_send_version(pf);
10906 
10907 		/* bail out in case recovery mode was detected, as there is
10908 		 * no need for further configuration.
10909 		 */
10910 		goto end_unlock;
10911 	}
10912 
10913 	i40e_clear_pxe_mode(hw);
10914 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10915 	if (ret)
10916 		goto end_core_reset;
10917 
10918 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10919 				hw->func_caps.num_rx_qp, 0, 0);
10920 	if (ret) {
10921 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10922 		goto end_core_reset;
10923 	}
10924 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10925 	if (ret) {
10926 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10927 		goto end_core_reset;
10928 	}
10929 
10930 #ifdef CONFIG_I40E_DCB
10931 	/* Enable FW to write a default DCB config on link-up
10932 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10933 	 * is not supported with new link speed
10934 	 */
10935 	if (i40e_is_tc_mqprio_enabled(pf)) {
10936 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10937 	} else {
10938 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10939 		    (hw->phy.link_info.link_speed &
10940 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10941 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10942 			dev_warn(&pf->pdev->dev,
10943 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10944 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10945 		} else {
10946 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10947 			ret = i40e_init_pf_dcb(pf);
10948 			if (ret) {
10949 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10950 					 ret);
10951 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10952 				/* Continue without DCB enabled */
10953 			}
10954 		}
10955 	}
10956 
10957 #endif /* CONFIG_I40E_DCB */
10958 	if (!lock_acquired)
10959 		rtnl_lock();
10960 	ret = i40e_setup_pf_switch(pf, reinit, true);
10961 	if (ret)
10962 		goto end_unlock;
10963 
10964 	/* The driver only wants link up/down and module qualification
10965 	 * reports from firmware.  Note the negative logic.
10966 	 */
10967 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10968 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10969 					 I40E_AQ_EVENT_MEDIA_NA |
10970 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10971 	if (ret)
10972 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10973 			 ERR_PTR(ret),
10974 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10975 
10976 	/* Rebuild the VSIs and VEBs that existed before reset.
10977 	 * They are still in our local switch element arrays, so only
10978 	 * need to rebuild the switch model in the HW.
10979 	 *
10980 	 * If there were VEBs but the reconstitution failed, we'll try
10981 	 * to recover minimal use by getting the basic PF VSI working.
10982 	 */
10983 	if (vsi->uplink_seid != pf->mac_seid) {
10984 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10985 		/* find the one VEB connected to the MAC, and find orphans */
10986 		for (v = 0; v < I40E_MAX_VEB; v++) {
10987 			if (!pf->veb[v])
10988 				continue;
10989 
10990 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10991 			    pf->veb[v]->uplink_seid == 0) {
10992 				ret = i40e_reconstitute_veb(pf->veb[v]);
10993 
10994 				if (!ret)
10995 					continue;
10996 
10997 				/* If Main VEB failed, we're in deep doodoo,
10998 				 * so give up rebuilding the switch and set up
10999 				 * for minimal rebuild of PF VSI.
11000 				 * If orphan failed, we'll report the error
11001 				 * but try to keep going.
11002 				 */
11003 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
11004 					dev_info(&pf->pdev->dev,
11005 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11006 						 ret);
11007 					vsi->uplink_seid = pf->mac_seid;
11008 					break;
11009 				} else if (pf->veb[v]->uplink_seid == 0) {
11010 					dev_info(&pf->pdev->dev,
11011 						 "rebuild of orphan VEB failed: %d\n",
11012 						 ret);
11013 				}
11014 			}
11015 		}
11016 	}
11017 
11018 	if (vsi->uplink_seid == pf->mac_seid) {
11019 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11020 		/* no VEB, so rebuild only the Main VSI */
11021 		ret = i40e_add_vsi(vsi);
11022 		if (ret) {
11023 			dev_info(&pf->pdev->dev,
11024 				 "rebuild of Main VSI failed: %d\n", ret);
11025 			goto end_unlock;
11026 		}
11027 	}
11028 
11029 	if (vsi->mqprio_qopt.max_rate[0]) {
11030 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11031 						  vsi->mqprio_qopt.max_rate[0]);
11032 		u64 credits = 0;
11033 
11034 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11035 		if (ret)
11036 			goto end_unlock;
11037 
11038 		credits = max_tx_rate;
11039 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11040 		dev_dbg(&vsi->back->pdev->dev,
11041 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11042 			max_tx_rate,
11043 			credits,
11044 			vsi->seid);
11045 	}
11046 
11047 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11048 	if (ret)
11049 		goto end_unlock;
11050 
11051 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11052 	 * for this main VSI if they exist
11053 	 */
11054 	ret = i40e_rebuild_channels(vsi);
11055 	if (ret)
11056 		goto end_unlock;
11057 
11058 	/* Reconfigure hardware for allowing smaller MSS in the case
11059 	 * of TSO, so that we avoid the MDD being fired and causing
11060 	 * a reset in the case of small MSS+TSO.
11061 	 */
11062 #define I40E_REG_MSS          0x000E64DC
11063 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11064 #define I40E_64BYTE_MSS       0x400000
11065 	val = rd32(hw, I40E_REG_MSS);
11066 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11067 		val &= ~I40E_REG_MSS_MIN_MASK;
11068 		val |= I40E_64BYTE_MSS;
11069 		wr32(hw, I40E_REG_MSS, val);
11070 	}
11071 
11072 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11073 		msleep(75);
11074 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11075 		if (ret)
11076 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11077 				 ERR_PTR(ret),
11078 				 i40e_aq_str(&pf->hw,
11079 					     pf->hw.aq.asq_last_status));
11080 	}
11081 	/* reinit the misc interrupt */
11082 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11083 		ret = i40e_setup_misc_vector(pf);
11084 		if (ret)
11085 			goto end_unlock;
11086 	}
11087 
11088 	/* Add a filter to drop all Flow control frames from any VSI from being
11089 	 * transmitted. By doing so we stop a malicious VF from sending out
11090 	 * PAUSE or PFC frames and potentially controlling traffic for other
11091 	 * PF/VF VSIs.
11092 	 * The FW can still send Flow control frames if enabled.
11093 	 */
11094 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11095 						       pf->main_vsi_seid);
11096 
11097 	/* restart the VSIs that were rebuilt and running before the reset */
11098 	i40e_pf_unquiesce_all_vsi(pf);
11099 
11100 	/* Release the RTNL lock before we start resetting VFs */
11101 	if (!lock_acquired)
11102 		rtnl_unlock();
11103 
11104 	/* Restore promiscuous settings */
11105 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11106 	if (ret)
11107 		dev_warn(&pf->pdev->dev,
11108 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11109 			 pf->cur_promisc ? "on" : "off",
11110 			 ERR_PTR(ret),
11111 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11112 
11113 	i40e_reset_all_vfs(pf, true);
11114 
11115 	/* tell the firmware that we're starting */
11116 	i40e_send_version(pf);
11117 
11118 	/* We've already released the lock, so don't do it again */
11119 	goto end_core_reset;
11120 
11121 end_unlock:
11122 	if (!lock_acquired)
11123 		rtnl_unlock();
11124 end_core_reset:
11125 	clear_bit(__I40E_RESET_FAILED, pf->state);
11126 clear_recovery:
11127 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11128 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11129 }
11130 
11131 /**
11132  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11133  * @pf: board private structure
11134  * @reinit: if the Main VSI needs to re-initialized.
11135  * @lock_acquired: indicates whether or not the lock has been acquired
11136  * before this function was called.
11137  **/
11138 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11139 				   bool lock_acquired)
11140 {
11141 	int ret;
11142 
11143 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11144 		return;
11145 	/* Now we wait for GRST to settle out.
11146 	 * We don't have to delete the VEBs or VSIs from the hw switch
11147 	 * because the reset will make them disappear.
11148 	 */
11149 	ret = i40e_reset(pf);
11150 	if (!ret)
11151 		i40e_rebuild(pf, reinit, lock_acquired);
11152 }
11153 
11154 /**
11155  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11156  * @pf: board private structure
11157  *
11158  * Close up the VFs and other things in prep for a Core Reset,
11159  * then get ready to rebuild the world.
11160  * @lock_acquired: indicates whether or not the lock has been acquired
11161  * before this function was called.
11162  **/
11163 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11164 {
11165 	i40e_prep_for_reset(pf);
11166 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11167 }
11168 
11169 /**
11170  * i40e_handle_mdd_event
11171  * @pf: pointer to the PF structure
11172  *
11173  * Called from the MDD irq handler to identify possibly malicious vfs
11174  **/
11175 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11176 {
11177 	struct i40e_hw *hw = &pf->hw;
11178 	bool mdd_detected = false;
11179 	struct i40e_vf *vf;
11180 	u32 reg;
11181 	int i;
11182 
11183 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11184 		return;
11185 
11186 	/* find what triggered the MDD event */
11187 	reg = rd32(hw, I40E_GL_MDET_TX);
11188 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11189 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11190 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11191 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11192 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11193 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11194 				I40E_GL_MDET_TX_EVENT_SHIFT;
11195 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11196 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11197 				pf->hw.func_caps.base_queue;
11198 		if (netif_msg_tx_err(pf))
11199 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11200 				 event, queue, pf_num, vf_num);
11201 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11202 		mdd_detected = true;
11203 	}
11204 	reg = rd32(hw, I40E_GL_MDET_RX);
11205 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11206 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11207 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11208 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11209 				I40E_GL_MDET_RX_EVENT_SHIFT;
11210 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11211 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11212 				pf->hw.func_caps.base_queue;
11213 		if (netif_msg_rx_err(pf))
11214 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11215 				 event, queue, func);
11216 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11217 		mdd_detected = true;
11218 	}
11219 
11220 	if (mdd_detected) {
11221 		reg = rd32(hw, I40E_PF_MDET_TX);
11222 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11223 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11224 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11225 		}
11226 		reg = rd32(hw, I40E_PF_MDET_RX);
11227 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11228 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11229 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11230 		}
11231 	}
11232 
11233 	/* see if one of the VFs needs its hand slapped */
11234 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11235 		vf = &(pf->vf[i]);
11236 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11237 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11238 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11239 			vf->num_mdd_events++;
11240 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11241 				 i);
11242 			dev_info(&pf->pdev->dev,
11243 				 "Use PF Control I/F to re-enable the VF\n");
11244 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11245 		}
11246 
11247 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11248 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11249 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11250 			vf->num_mdd_events++;
11251 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11252 				 i);
11253 			dev_info(&pf->pdev->dev,
11254 				 "Use PF Control I/F to re-enable the VF\n");
11255 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11256 		}
11257 	}
11258 
11259 	/* re-enable mdd interrupt cause */
11260 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11261 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11262 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11263 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11264 	i40e_flush(hw);
11265 }
11266 
11267 /**
11268  * i40e_service_task - Run the driver's async subtasks
11269  * @work: pointer to work_struct containing our data
11270  **/
11271 static void i40e_service_task(struct work_struct *work)
11272 {
11273 	struct i40e_pf *pf = container_of(work,
11274 					  struct i40e_pf,
11275 					  service_task);
11276 	unsigned long start_time = jiffies;
11277 
11278 	/* don't bother with service tasks if a reset is in progress */
11279 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11280 	    test_bit(__I40E_SUSPENDED, pf->state))
11281 		return;
11282 
11283 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11284 		return;
11285 
11286 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11287 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11288 		i40e_sync_filters_subtask(pf);
11289 		i40e_reset_subtask(pf);
11290 		i40e_handle_mdd_event(pf);
11291 		i40e_vc_process_vflr_event(pf);
11292 		i40e_watchdog_subtask(pf);
11293 		i40e_fdir_reinit_subtask(pf);
11294 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11295 			/* Client subtask will reopen next time through. */
11296 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11297 							   true);
11298 		} else {
11299 			i40e_client_subtask(pf);
11300 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11301 					       pf->state))
11302 				i40e_notify_client_of_l2_param_changes(
11303 								pf->vsi[pf->lan_vsi]);
11304 		}
11305 		i40e_sync_filters_subtask(pf);
11306 	} else {
11307 		i40e_reset_subtask(pf);
11308 	}
11309 
11310 	i40e_clean_adminq_subtask(pf);
11311 
11312 	/* flush memory to make sure state is correct before next watchdog */
11313 	smp_mb__before_atomic();
11314 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11315 
11316 	/* If the tasks have taken longer than one timer cycle or there
11317 	 * is more work to be done, reschedule the service task now
11318 	 * rather than wait for the timer to tick again.
11319 	 */
11320 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11321 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11322 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11323 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11324 		i40e_service_event_schedule(pf);
11325 }
11326 
11327 /**
11328  * i40e_service_timer - timer callback
11329  * @t: timer list pointer
11330  **/
11331 static void i40e_service_timer(struct timer_list *t)
11332 {
11333 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11334 
11335 	mod_timer(&pf->service_timer,
11336 		  round_jiffies(jiffies + pf->service_timer_period));
11337 	i40e_service_event_schedule(pf);
11338 }
11339 
11340 /**
11341  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11342  * @vsi: the VSI being configured
11343  **/
11344 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11345 {
11346 	struct i40e_pf *pf = vsi->back;
11347 
11348 	switch (vsi->type) {
11349 	case I40E_VSI_MAIN:
11350 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11351 		if (!vsi->num_tx_desc)
11352 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11353 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11354 		if (!vsi->num_rx_desc)
11355 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11356 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11357 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11358 			vsi->num_q_vectors = pf->num_lan_msix;
11359 		else
11360 			vsi->num_q_vectors = 1;
11361 
11362 		break;
11363 
11364 	case I40E_VSI_FDIR:
11365 		vsi->alloc_queue_pairs = 1;
11366 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11367 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11368 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11369 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11370 		vsi->num_q_vectors = pf->num_fdsb_msix;
11371 		break;
11372 
11373 	case I40E_VSI_VMDQ2:
11374 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11375 		if (!vsi->num_tx_desc)
11376 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11377 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11378 		if (!vsi->num_rx_desc)
11379 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11380 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11381 		vsi->num_q_vectors = pf->num_vmdq_msix;
11382 		break;
11383 
11384 	case I40E_VSI_SRIOV:
11385 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11386 		if (!vsi->num_tx_desc)
11387 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11388 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11389 		if (!vsi->num_rx_desc)
11390 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11391 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11392 		break;
11393 
11394 	default:
11395 		WARN_ON(1);
11396 		return -ENODATA;
11397 	}
11398 
11399 	if (is_kdump_kernel()) {
11400 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11401 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11402 	}
11403 
11404 	return 0;
11405 }
11406 
11407 /**
11408  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11409  * @vsi: VSI pointer
11410  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11411  *
11412  * On error: returns error code (negative)
11413  * On success: returns 0
11414  **/
11415 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11416 {
11417 	struct i40e_ring **next_rings;
11418 	int size;
11419 	int ret = 0;
11420 
11421 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11422 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11423 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11424 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11425 	if (!vsi->tx_rings)
11426 		return -ENOMEM;
11427 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11428 	if (i40e_enabled_xdp_vsi(vsi)) {
11429 		vsi->xdp_rings = next_rings;
11430 		next_rings += vsi->alloc_queue_pairs;
11431 	}
11432 	vsi->rx_rings = next_rings;
11433 
11434 	if (alloc_qvectors) {
11435 		/* allocate memory for q_vector pointers */
11436 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11437 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11438 		if (!vsi->q_vectors) {
11439 			ret = -ENOMEM;
11440 			goto err_vectors;
11441 		}
11442 	}
11443 	return ret;
11444 
11445 err_vectors:
11446 	kfree(vsi->tx_rings);
11447 	return ret;
11448 }
11449 
11450 /**
11451  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11452  * @pf: board private structure
11453  * @type: type of VSI
11454  *
11455  * On error: returns error code (negative)
11456  * On success: returns vsi index in PF (positive)
11457  **/
11458 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11459 {
11460 	int ret = -ENODEV;
11461 	struct i40e_vsi *vsi;
11462 	int vsi_idx;
11463 	int i;
11464 
11465 	/* Need to protect the allocation of the VSIs at the PF level */
11466 	mutex_lock(&pf->switch_mutex);
11467 
11468 	/* VSI list may be fragmented if VSI creation/destruction has
11469 	 * been happening.  We can afford to do a quick scan to look
11470 	 * for any free VSIs in the list.
11471 	 *
11472 	 * find next empty vsi slot, looping back around if necessary
11473 	 */
11474 	i = pf->next_vsi;
11475 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11476 		i++;
11477 	if (i >= pf->num_alloc_vsi) {
11478 		i = 0;
11479 		while (i < pf->next_vsi && pf->vsi[i])
11480 			i++;
11481 	}
11482 
11483 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11484 		vsi_idx = i;             /* Found one! */
11485 	} else {
11486 		ret = -ENODEV;
11487 		goto unlock_pf;  /* out of VSI slots! */
11488 	}
11489 	pf->next_vsi = ++i;
11490 
11491 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11492 	if (!vsi) {
11493 		ret = -ENOMEM;
11494 		goto unlock_pf;
11495 	}
11496 	vsi->type = type;
11497 	vsi->back = pf;
11498 	set_bit(__I40E_VSI_DOWN, vsi->state);
11499 	vsi->flags = 0;
11500 	vsi->idx = vsi_idx;
11501 	vsi->int_rate_limit = 0;
11502 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11503 				pf->rss_table_size : 64;
11504 	vsi->netdev_registered = false;
11505 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11506 	hash_init(vsi->mac_filter_hash);
11507 	vsi->irqs_ready = false;
11508 
11509 	if (type == I40E_VSI_MAIN) {
11510 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11511 		if (!vsi->af_xdp_zc_qps)
11512 			goto err_rings;
11513 	}
11514 
11515 	ret = i40e_set_num_rings_in_vsi(vsi);
11516 	if (ret)
11517 		goto err_rings;
11518 
11519 	ret = i40e_vsi_alloc_arrays(vsi, true);
11520 	if (ret)
11521 		goto err_rings;
11522 
11523 	/* Setup default MSIX irq handler for VSI */
11524 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11525 
11526 	/* Initialize VSI lock */
11527 	spin_lock_init(&vsi->mac_filter_hash_lock);
11528 	pf->vsi[vsi_idx] = vsi;
11529 	ret = vsi_idx;
11530 	goto unlock_pf;
11531 
11532 err_rings:
11533 	bitmap_free(vsi->af_xdp_zc_qps);
11534 	pf->next_vsi = i - 1;
11535 	kfree(vsi);
11536 unlock_pf:
11537 	mutex_unlock(&pf->switch_mutex);
11538 	return ret;
11539 }
11540 
11541 /**
11542  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11543  * @vsi: VSI pointer
11544  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11545  *
11546  * On error: returns error code (negative)
11547  * On success: returns 0
11548  **/
11549 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11550 {
11551 	/* free the ring and vector containers */
11552 	if (free_qvectors) {
11553 		kfree(vsi->q_vectors);
11554 		vsi->q_vectors = NULL;
11555 	}
11556 	kfree(vsi->tx_rings);
11557 	vsi->tx_rings = NULL;
11558 	vsi->rx_rings = NULL;
11559 	vsi->xdp_rings = NULL;
11560 }
11561 
11562 /**
11563  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11564  * and lookup table
11565  * @vsi: Pointer to VSI structure
11566  */
11567 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11568 {
11569 	if (!vsi)
11570 		return;
11571 
11572 	kfree(vsi->rss_hkey_user);
11573 	vsi->rss_hkey_user = NULL;
11574 
11575 	kfree(vsi->rss_lut_user);
11576 	vsi->rss_lut_user = NULL;
11577 }
11578 
11579 /**
11580  * i40e_vsi_clear - Deallocate the VSI provided
11581  * @vsi: the VSI being un-configured
11582  **/
11583 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11584 {
11585 	struct i40e_pf *pf;
11586 
11587 	if (!vsi)
11588 		return 0;
11589 
11590 	if (!vsi->back)
11591 		goto free_vsi;
11592 	pf = vsi->back;
11593 
11594 	mutex_lock(&pf->switch_mutex);
11595 	if (!pf->vsi[vsi->idx]) {
11596 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11597 			vsi->idx, vsi->idx, vsi->type);
11598 		goto unlock_vsi;
11599 	}
11600 
11601 	if (pf->vsi[vsi->idx] != vsi) {
11602 		dev_err(&pf->pdev->dev,
11603 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11604 			pf->vsi[vsi->idx]->idx,
11605 			pf->vsi[vsi->idx]->type,
11606 			vsi->idx, vsi->type);
11607 		goto unlock_vsi;
11608 	}
11609 
11610 	/* updates the PF for this cleared vsi */
11611 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11612 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11613 
11614 	bitmap_free(vsi->af_xdp_zc_qps);
11615 	i40e_vsi_free_arrays(vsi, true);
11616 	i40e_clear_rss_config_user(vsi);
11617 
11618 	pf->vsi[vsi->idx] = NULL;
11619 	if (vsi->idx < pf->next_vsi)
11620 		pf->next_vsi = vsi->idx;
11621 
11622 unlock_vsi:
11623 	mutex_unlock(&pf->switch_mutex);
11624 free_vsi:
11625 	kfree(vsi);
11626 
11627 	return 0;
11628 }
11629 
11630 /**
11631  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11632  * @vsi: the VSI being cleaned
11633  **/
11634 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11635 {
11636 	int i;
11637 
11638 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11639 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11640 			kfree_rcu(vsi->tx_rings[i], rcu);
11641 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11642 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11643 			if (vsi->xdp_rings)
11644 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11645 		}
11646 	}
11647 }
11648 
11649 /**
11650  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11651  * @vsi: the VSI being configured
11652  **/
11653 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11654 {
11655 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11656 	struct i40e_pf *pf = vsi->back;
11657 	struct i40e_ring *ring;
11658 
11659 	/* Set basic values in the rings to be used later during open() */
11660 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11661 		/* allocate space for both Tx and Rx in one shot */
11662 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11663 		if (!ring)
11664 			goto err_out;
11665 
11666 		ring->queue_index = i;
11667 		ring->reg_idx = vsi->base_queue + i;
11668 		ring->ring_active = false;
11669 		ring->vsi = vsi;
11670 		ring->netdev = vsi->netdev;
11671 		ring->dev = &pf->pdev->dev;
11672 		ring->count = vsi->num_tx_desc;
11673 		ring->size = 0;
11674 		ring->dcb_tc = 0;
11675 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11676 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11677 		ring->itr_setting = pf->tx_itr_default;
11678 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11679 
11680 		if (!i40e_enabled_xdp_vsi(vsi))
11681 			goto setup_rx;
11682 
11683 		ring->queue_index = vsi->alloc_queue_pairs + i;
11684 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11685 		ring->ring_active = false;
11686 		ring->vsi = vsi;
11687 		ring->netdev = NULL;
11688 		ring->dev = &pf->pdev->dev;
11689 		ring->count = vsi->num_tx_desc;
11690 		ring->size = 0;
11691 		ring->dcb_tc = 0;
11692 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11693 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11694 		set_ring_xdp(ring);
11695 		ring->itr_setting = pf->tx_itr_default;
11696 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11697 
11698 setup_rx:
11699 		ring->queue_index = i;
11700 		ring->reg_idx = vsi->base_queue + i;
11701 		ring->ring_active = false;
11702 		ring->vsi = vsi;
11703 		ring->netdev = vsi->netdev;
11704 		ring->dev = &pf->pdev->dev;
11705 		ring->count = vsi->num_rx_desc;
11706 		ring->size = 0;
11707 		ring->dcb_tc = 0;
11708 		ring->itr_setting = pf->rx_itr_default;
11709 		WRITE_ONCE(vsi->rx_rings[i], ring);
11710 	}
11711 
11712 	return 0;
11713 
11714 err_out:
11715 	i40e_vsi_clear_rings(vsi);
11716 	return -ENOMEM;
11717 }
11718 
11719 /**
11720  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11721  * @pf: board private structure
11722  * @vectors: the number of MSI-X vectors to request
11723  *
11724  * Returns the number of vectors reserved, or error
11725  **/
11726 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11727 {
11728 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11729 					I40E_MIN_MSIX, vectors);
11730 	if (vectors < 0) {
11731 		dev_info(&pf->pdev->dev,
11732 			 "MSI-X vector reservation failed: %d\n", vectors);
11733 		vectors = 0;
11734 	}
11735 
11736 	return vectors;
11737 }
11738 
11739 /**
11740  * i40e_init_msix - Setup the MSIX capability
11741  * @pf: board private structure
11742  *
11743  * Work with the OS to set up the MSIX vectors needed.
11744  *
11745  * Returns the number of vectors reserved or negative on failure
11746  **/
11747 static int i40e_init_msix(struct i40e_pf *pf)
11748 {
11749 	struct i40e_hw *hw = &pf->hw;
11750 	int cpus, extra_vectors;
11751 	int vectors_left;
11752 	int v_budget, i;
11753 	int v_actual;
11754 	int iwarp_requested = 0;
11755 
11756 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11757 		return -ENODEV;
11758 
11759 	/* The number of vectors we'll request will be comprised of:
11760 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11761 	 *   - The number of LAN queue pairs
11762 	 *	- Queues being used for RSS.
11763 	 *		We don't need as many as max_rss_size vectors.
11764 	 *		use rss_size instead in the calculation since that
11765 	 *		is governed by number of cpus in the system.
11766 	 *	- assumes symmetric Tx/Rx pairing
11767 	 *   - The number of VMDq pairs
11768 	 *   - The CPU count within the NUMA node if iWARP is enabled
11769 	 * Once we count this up, try the request.
11770 	 *
11771 	 * If we can't get what we want, we'll simplify to nearly nothing
11772 	 * and try again.  If that still fails, we punt.
11773 	 */
11774 	vectors_left = hw->func_caps.num_msix_vectors;
11775 	v_budget = 0;
11776 
11777 	/* reserve one vector for miscellaneous handler */
11778 	if (vectors_left) {
11779 		v_budget++;
11780 		vectors_left--;
11781 	}
11782 
11783 	/* reserve some vectors for the main PF traffic queues. Initially we
11784 	 * only reserve at most 50% of the available vectors, in the case that
11785 	 * the number of online CPUs is large. This ensures that we can enable
11786 	 * extra features as well. Once we've enabled the other features, we
11787 	 * will use any remaining vectors to reach as close as we can to the
11788 	 * number of online CPUs.
11789 	 */
11790 	cpus = num_online_cpus();
11791 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11792 	vectors_left -= pf->num_lan_msix;
11793 
11794 	/* reserve one vector for sideband flow director */
11795 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11796 		if (vectors_left) {
11797 			pf->num_fdsb_msix = 1;
11798 			v_budget++;
11799 			vectors_left--;
11800 		} else {
11801 			pf->num_fdsb_msix = 0;
11802 		}
11803 	}
11804 
11805 	/* can we reserve enough for iWARP? */
11806 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11807 		iwarp_requested = pf->num_iwarp_msix;
11808 
11809 		if (!vectors_left)
11810 			pf->num_iwarp_msix = 0;
11811 		else if (vectors_left < pf->num_iwarp_msix)
11812 			pf->num_iwarp_msix = 1;
11813 		v_budget += pf->num_iwarp_msix;
11814 		vectors_left -= pf->num_iwarp_msix;
11815 	}
11816 
11817 	/* any vectors left over go for VMDq support */
11818 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11819 		if (!vectors_left) {
11820 			pf->num_vmdq_msix = 0;
11821 			pf->num_vmdq_qps = 0;
11822 		} else {
11823 			int vmdq_vecs_wanted =
11824 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11825 			int vmdq_vecs =
11826 				min_t(int, vectors_left, vmdq_vecs_wanted);
11827 
11828 			/* if we're short on vectors for what's desired, we limit
11829 			 * the queues per vmdq.  If this is still more than are
11830 			 * available, the user will need to change the number of
11831 			 * queues/vectors used by the PF later with the ethtool
11832 			 * channels command
11833 			 */
11834 			if (vectors_left < vmdq_vecs_wanted) {
11835 				pf->num_vmdq_qps = 1;
11836 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11837 				vmdq_vecs = min_t(int,
11838 						  vectors_left,
11839 						  vmdq_vecs_wanted);
11840 			}
11841 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11842 
11843 			v_budget += vmdq_vecs;
11844 			vectors_left -= vmdq_vecs;
11845 		}
11846 	}
11847 
11848 	/* On systems with a large number of SMP cores, we previously limited
11849 	 * the number of vectors for num_lan_msix to be at most 50% of the
11850 	 * available vectors, to allow for other features. Now, we add back
11851 	 * the remaining vectors. However, we ensure that the total
11852 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11853 	 * calculate the number of vectors we can add without going over the
11854 	 * cap of CPUs. For systems with a small number of CPUs this will be
11855 	 * zero.
11856 	 */
11857 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11858 	pf->num_lan_msix += extra_vectors;
11859 	vectors_left -= extra_vectors;
11860 
11861 	WARN(vectors_left < 0,
11862 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11863 
11864 	v_budget += pf->num_lan_msix;
11865 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11866 				   GFP_KERNEL);
11867 	if (!pf->msix_entries)
11868 		return -ENOMEM;
11869 
11870 	for (i = 0; i < v_budget; i++)
11871 		pf->msix_entries[i].entry = i;
11872 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11873 
11874 	if (v_actual < I40E_MIN_MSIX) {
11875 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11876 		kfree(pf->msix_entries);
11877 		pf->msix_entries = NULL;
11878 		pci_disable_msix(pf->pdev);
11879 		return -ENODEV;
11880 
11881 	} else if (v_actual == I40E_MIN_MSIX) {
11882 		/* Adjust for minimal MSIX use */
11883 		pf->num_vmdq_vsis = 0;
11884 		pf->num_vmdq_qps = 0;
11885 		pf->num_lan_qps = 1;
11886 		pf->num_lan_msix = 1;
11887 
11888 	} else if (v_actual != v_budget) {
11889 		/* If we have limited resources, we will start with no vectors
11890 		 * for the special features and then allocate vectors to some
11891 		 * of these features based on the policy and at the end disable
11892 		 * the features that did not get any vectors.
11893 		 */
11894 		int vec;
11895 
11896 		dev_info(&pf->pdev->dev,
11897 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11898 			 v_actual, v_budget);
11899 		/* reserve the misc vector */
11900 		vec = v_actual - 1;
11901 
11902 		/* Scale vector usage down */
11903 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11904 		pf->num_vmdq_vsis = 1;
11905 		pf->num_vmdq_qps = 1;
11906 
11907 		/* partition out the remaining vectors */
11908 		switch (vec) {
11909 		case 2:
11910 			pf->num_lan_msix = 1;
11911 			break;
11912 		case 3:
11913 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11914 				pf->num_lan_msix = 1;
11915 				pf->num_iwarp_msix = 1;
11916 			} else {
11917 				pf->num_lan_msix = 2;
11918 			}
11919 			break;
11920 		default:
11921 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11922 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11923 						 iwarp_requested);
11924 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11925 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11926 			} else {
11927 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11928 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11929 			}
11930 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11931 				pf->num_fdsb_msix = 1;
11932 				vec--;
11933 			}
11934 			pf->num_lan_msix = min_t(int,
11935 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11936 							      pf->num_lan_msix);
11937 			pf->num_lan_qps = pf->num_lan_msix;
11938 			break;
11939 		}
11940 	}
11941 
11942 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11943 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11944 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11945 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11946 	}
11947 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11948 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11949 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
11950 	}
11951 
11952 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
11953 	    pf->num_iwarp_msix == 0) {
11954 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11955 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
11956 	}
11957 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11958 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11959 		   pf->num_lan_msix,
11960 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11961 		   pf->num_fdsb_msix,
11962 		   pf->num_iwarp_msix);
11963 
11964 	return v_actual;
11965 }
11966 
11967 /**
11968  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11969  * @vsi: the VSI being configured
11970  * @v_idx: index of the vector in the vsi struct
11971  *
11972  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11973  **/
11974 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11975 {
11976 	struct i40e_q_vector *q_vector;
11977 
11978 	/* allocate q_vector */
11979 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11980 	if (!q_vector)
11981 		return -ENOMEM;
11982 
11983 	q_vector->vsi = vsi;
11984 	q_vector->v_idx = v_idx;
11985 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11986 
11987 	if (vsi->netdev)
11988 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11989 
11990 	/* tie q_vector and vsi together */
11991 	vsi->q_vectors[v_idx] = q_vector;
11992 
11993 	return 0;
11994 }
11995 
11996 /**
11997  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11998  * @vsi: the VSI being configured
11999  *
12000  * We allocate one q_vector per queue interrupt.  If allocation fails we
12001  * return -ENOMEM.
12002  **/
12003 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12004 {
12005 	struct i40e_pf *pf = vsi->back;
12006 	int err, v_idx, num_q_vectors;
12007 
12008 	/* if not MSIX, give the one vector only to the LAN VSI */
12009 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12010 		num_q_vectors = vsi->num_q_vectors;
12011 	else if (vsi == pf->vsi[pf->lan_vsi])
12012 		num_q_vectors = 1;
12013 	else
12014 		return -EINVAL;
12015 
12016 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12017 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12018 		if (err)
12019 			goto err_out;
12020 	}
12021 
12022 	return 0;
12023 
12024 err_out:
12025 	while (v_idx--)
12026 		i40e_free_q_vector(vsi, v_idx);
12027 
12028 	return err;
12029 }
12030 
12031 /**
12032  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12033  * @pf: board private structure to initialize
12034  **/
12035 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12036 {
12037 	int vectors = 0;
12038 	ssize_t size;
12039 
12040 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12041 		vectors = i40e_init_msix(pf);
12042 		if (vectors < 0) {
12043 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12044 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12045 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12046 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12047 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12048 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12049 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12050 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12051 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12052 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12053 
12054 			/* rework the queue expectations without MSIX */
12055 			i40e_determine_queue_usage(pf);
12056 		}
12057 	}
12058 
12059 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12060 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12061 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12062 		vectors = pci_enable_msi(pf->pdev);
12063 		if (vectors < 0) {
12064 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12065 				 vectors);
12066 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12067 		}
12068 		vectors = 1;  /* one MSI or Legacy vector */
12069 	}
12070 
12071 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12072 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12073 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12074 
12075 	/* set up vector assignment tracking */
12076 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12077 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12078 	if (!pf->irq_pile)
12079 		return -ENOMEM;
12080 
12081 	pf->irq_pile->num_entries = vectors;
12082 
12083 	/* track first vector for misc interrupts, ignore return */
12084 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12085 
12086 	return 0;
12087 }
12088 
12089 /**
12090  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12091  * @pf: private board data structure
12092  *
12093  * Restore the interrupt scheme that was cleared when we suspended the
12094  * device. This should be called during resume to re-allocate the q_vectors
12095  * and reacquire IRQs.
12096  */
12097 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12098 {
12099 	int err, i;
12100 
12101 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12102 	 * scheme. We need to re-enabled them here in order to attempt to
12103 	 * re-acquire the MSI or MSI-X vectors
12104 	 */
12105 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12106 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12107 
12108 	err = i40e_init_interrupt_scheme(pf);
12109 	if (err)
12110 		return err;
12111 
12112 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12113 	 * rings together again.
12114 	 */
12115 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12116 		if (pf->vsi[i]) {
12117 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12118 			if (err)
12119 				goto err_unwind;
12120 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12121 		}
12122 	}
12123 
12124 	err = i40e_setup_misc_vector(pf);
12125 	if (err)
12126 		goto err_unwind;
12127 
12128 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12129 		i40e_client_update_msix_info(pf);
12130 
12131 	return 0;
12132 
12133 err_unwind:
12134 	while (i--) {
12135 		if (pf->vsi[i])
12136 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12137 	}
12138 
12139 	return err;
12140 }
12141 
12142 /**
12143  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12144  * non queue events in recovery mode
12145  * @pf: board private structure
12146  *
12147  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12148  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12149  * This is handled differently than in recovery mode since no Tx/Rx resources
12150  * are being allocated.
12151  **/
12152 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12153 {
12154 	int err;
12155 
12156 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12157 		err = i40e_setup_misc_vector(pf);
12158 
12159 		if (err) {
12160 			dev_info(&pf->pdev->dev,
12161 				 "MSI-X misc vector request failed, error %d\n",
12162 				 err);
12163 			return err;
12164 		}
12165 	} else {
12166 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12167 
12168 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12169 				  pf->int_name, pf);
12170 
12171 		if (err) {
12172 			dev_info(&pf->pdev->dev,
12173 				 "MSI/legacy misc vector request failed, error %d\n",
12174 				 err);
12175 			return err;
12176 		}
12177 		i40e_enable_misc_int_causes(pf);
12178 		i40e_irq_dynamic_enable_icr0(pf);
12179 	}
12180 
12181 	return 0;
12182 }
12183 
12184 /**
12185  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12186  * @pf: board private structure
12187  *
12188  * This sets up the handler for MSIX 0, which is used to manage the
12189  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12190  * when in MSI or Legacy interrupt mode.
12191  **/
12192 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12193 {
12194 	struct i40e_hw *hw = &pf->hw;
12195 	int err = 0;
12196 
12197 	/* Only request the IRQ once, the first time through. */
12198 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12199 		err = request_irq(pf->msix_entries[0].vector,
12200 				  i40e_intr, 0, pf->int_name, pf);
12201 		if (err) {
12202 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12203 			dev_info(&pf->pdev->dev,
12204 				 "request_irq for %s failed: %d\n",
12205 				 pf->int_name, err);
12206 			return -EFAULT;
12207 		}
12208 	}
12209 
12210 	i40e_enable_misc_int_causes(pf);
12211 
12212 	/* associate no queues to the misc vector */
12213 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12214 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12215 
12216 	i40e_flush(hw);
12217 
12218 	i40e_irq_dynamic_enable_icr0(pf);
12219 
12220 	return err;
12221 }
12222 
12223 /**
12224  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12225  * @vsi: Pointer to vsi structure
12226  * @seed: Buffter to store the hash keys
12227  * @lut: Buffer to store the lookup table entries
12228  * @lut_size: Size of buffer to store the lookup table entries
12229  *
12230  * Return 0 on success, negative on failure
12231  */
12232 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12233 			   u8 *lut, u16 lut_size)
12234 {
12235 	struct i40e_pf *pf = vsi->back;
12236 	struct i40e_hw *hw = &pf->hw;
12237 	int ret = 0;
12238 
12239 	if (seed) {
12240 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12241 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12242 		if (ret) {
12243 			dev_info(&pf->pdev->dev,
12244 				 "Cannot get RSS key, err %pe aq_err %s\n",
12245 				 ERR_PTR(ret),
12246 				 i40e_aq_str(&pf->hw,
12247 					     pf->hw.aq.asq_last_status));
12248 			return ret;
12249 		}
12250 	}
12251 
12252 	if (lut) {
12253 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12254 
12255 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12256 		if (ret) {
12257 			dev_info(&pf->pdev->dev,
12258 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12259 				 ERR_PTR(ret),
12260 				 i40e_aq_str(&pf->hw,
12261 					     pf->hw.aq.asq_last_status));
12262 			return ret;
12263 		}
12264 	}
12265 
12266 	return ret;
12267 }
12268 
12269 /**
12270  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12271  * @vsi: Pointer to vsi structure
12272  * @seed: RSS hash seed
12273  * @lut: Lookup table
12274  * @lut_size: Lookup table size
12275  *
12276  * Returns 0 on success, negative on failure
12277  **/
12278 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12279 			       const u8 *lut, u16 lut_size)
12280 {
12281 	struct i40e_pf *pf = vsi->back;
12282 	struct i40e_hw *hw = &pf->hw;
12283 	u16 vf_id = vsi->vf_id;
12284 	u8 i;
12285 
12286 	/* Fill out hash function seed */
12287 	if (seed) {
12288 		u32 *seed_dw = (u32 *)seed;
12289 
12290 		if (vsi->type == I40E_VSI_MAIN) {
12291 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12292 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12293 		} else if (vsi->type == I40E_VSI_SRIOV) {
12294 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12295 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12296 		} else {
12297 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12298 		}
12299 	}
12300 
12301 	if (lut) {
12302 		u32 *lut_dw = (u32 *)lut;
12303 
12304 		if (vsi->type == I40E_VSI_MAIN) {
12305 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12306 				return -EINVAL;
12307 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12308 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12309 		} else if (vsi->type == I40E_VSI_SRIOV) {
12310 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12311 				return -EINVAL;
12312 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12313 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12314 		} else {
12315 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12316 		}
12317 	}
12318 	i40e_flush(hw);
12319 
12320 	return 0;
12321 }
12322 
12323 /**
12324  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12325  * @vsi: Pointer to VSI structure
12326  * @seed: Buffer to store the keys
12327  * @lut: Buffer to store the lookup table entries
12328  * @lut_size: Size of buffer to store the lookup table entries
12329  *
12330  * Returns 0 on success, negative on failure
12331  */
12332 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12333 			    u8 *lut, u16 lut_size)
12334 {
12335 	struct i40e_pf *pf = vsi->back;
12336 	struct i40e_hw *hw = &pf->hw;
12337 	u16 i;
12338 
12339 	if (seed) {
12340 		u32 *seed_dw = (u32 *)seed;
12341 
12342 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12343 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12344 	}
12345 	if (lut) {
12346 		u32 *lut_dw = (u32 *)lut;
12347 
12348 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12349 			return -EINVAL;
12350 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12351 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12352 	}
12353 
12354 	return 0;
12355 }
12356 
12357 /**
12358  * i40e_config_rss - Configure RSS keys and lut
12359  * @vsi: Pointer to VSI structure
12360  * @seed: RSS hash seed
12361  * @lut: Lookup table
12362  * @lut_size: Lookup table size
12363  *
12364  * Returns 0 on success, negative on failure
12365  */
12366 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12367 {
12368 	struct i40e_pf *pf = vsi->back;
12369 
12370 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12371 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12372 	else
12373 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12374 }
12375 
12376 /**
12377  * i40e_get_rss - Get RSS keys and lut
12378  * @vsi: Pointer to VSI structure
12379  * @seed: Buffer to store the keys
12380  * @lut: Buffer to store the lookup table entries
12381  * @lut_size: Size of buffer to store the lookup table entries
12382  *
12383  * Returns 0 on success, negative on failure
12384  */
12385 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12386 {
12387 	struct i40e_pf *pf = vsi->back;
12388 
12389 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12390 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12391 	else
12392 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12393 }
12394 
12395 /**
12396  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12397  * @pf: Pointer to board private structure
12398  * @lut: Lookup table
12399  * @rss_table_size: Lookup table size
12400  * @rss_size: Range of queue number for hashing
12401  */
12402 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12403 		       u16 rss_table_size, u16 rss_size)
12404 {
12405 	u16 i;
12406 
12407 	for (i = 0; i < rss_table_size; i++)
12408 		lut[i] = i % rss_size;
12409 }
12410 
12411 /**
12412  * i40e_pf_config_rss - Prepare for RSS if used
12413  * @pf: board private structure
12414  **/
12415 static int i40e_pf_config_rss(struct i40e_pf *pf)
12416 {
12417 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12418 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12419 	u8 *lut;
12420 	struct i40e_hw *hw = &pf->hw;
12421 	u32 reg_val;
12422 	u64 hena;
12423 	int ret;
12424 
12425 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12426 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12427 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12428 	hena |= i40e_pf_get_default_rss_hena(pf);
12429 
12430 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12431 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12432 
12433 	/* Determine the RSS table size based on the hardware capabilities */
12434 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12435 	reg_val = (pf->rss_table_size == 512) ?
12436 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12437 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12438 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12439 
12440 	/* Determine the RSS size of the VSI */
12441 	if (!vsi->rss_size) {
12442 		u16 qcount;
12443 		/* If the firmware does something weird during VSI init, we
12444 		 * could end up with zero TCs. Check for that to avoid
12445 		 * divide-by-zero. It probably won't pass traffic, but it also
12446 		 * won't panic.
12447 		 */
12448 		qcount = vsi->num_queue_pairs /
12449 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12450 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12451 	}
12452 	if (!vsi->rss_size)
12453 		return -EINVAL;
12454 
12455 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12456 	if (!lut)
12457 		return -ENOMEM;
12458 
12459 	/* Use user configured lut if there is one, otherwise use default */
12460 	if (vsi->rss_lut_user)
12461 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12462 	else
12463 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12464 
12465 	/* Use user configured hash key if there is one, otherwise
12466 	 * use default.
12467 	 */
12468 	if (vsi->rss_hkey_user)
12469 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12470 	else
12471 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12472 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12473 	kfree(lut);
12474 
12475 	return ret;
12476 }
12477 
12478 /**
12479  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12480  * @pf: board private structure
12481  * @queue_count: the requested queue count for rss.
12482  *
12483  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12484  * count which may be different from the requested queue count.
12485  * Note: expects to be called while under rtnl_lock()
12486  **/
12487 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12488 {
12489 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12490 	int new_rss_size;
12491 
12492 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12493 		return 0;
12494 
12495 	queue_count = min_t(int, queue_count, num_online_cpus());
12496 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12497 
12498 	if (queue_count != vsi->num_queue_pairs) {
12499 		u16 qcount;
12500 
12501 		vsi->req_queue_pairs = queue_count;
12502 		i40e_prep_for_reset(pf);
12503 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12504 			return pf->alloc_rss_size;
12505 
12506 		pf->alloc_rss_size = new_rss_size;
12507 
12508 		i40e_reset_and_rebuild(pf, true, true);
12509 
12510 		/* Discard the user configured hash keys and lut, if less
12511 		 * queues are enabled.
12512 		 */
12513 		if (queue_count < vsi->rss_size) {
12514 			i40e_clear_rss_config_user(vsi);
12515 			dev_dbg(&pf->pdev->dev,
12516 				"discard user configured hash keys and lut\n");
12517 		}
12518 
12519 		/* Reset vsi->rss_size, as number of enabled queues changed */
12520 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12521 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12522 
12523 		i40e_pf_config_rss(pf);
12524 	}
12525 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12526 		 vsi->req_queue_pairs, pf->rss_size_max);
12527 	return pf->alloc_rss_size;
12528 }
12529 
12530 /**
12531  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12532  * @pf: board private structure
12533  **/
12534 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12535 {
12536 	bool min_valid, max_valid;
12537 	u32 max_bw, min_bw;
12538 	int status;
12539 
12540 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12541 					   &min_valid, &max_valid);
12542 
12543 	if (!status) {
12544 		if (min_valid)
12545 			pf->min_bw = min_bw;
12546 		if (max_valid)
12547 			pf->max_bw = max_bw;
12548 	}
12549 
12550 	return status;
12551 }
12552 
12553 /**
12554  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12555  * @pf: board private structure
12556  **/
12557 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12558 {
12559 	struct i40e_aqc_configure_partition_bw_data bw_data;
12560 	int status;
12561 
12562 	memset(&bw_data, 0, sizeof(bw_data));
12563 
12564 	/* Set the valid bit for this PF */
12565 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12566 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12567 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12568 
12569 	/* Set the new bandwidths */
12570 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12571 
12572 	return status;
12573 }
12574 
12575 /**
12576  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12577  * @pf: board private structure
12578  **/
12579 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12580 {
12581 	/* Commit temporary BW setting to permanent NVM image */
12582 	enum i40e_admin_queue_err last_aq_status;
12583 	u16 nvm_word;
12584 	int ret;
12585 
12586 	if (pf->hw.partition_id != 1) {
12587 		dev_info(&pf->pdev->dev,
12588 			 "Commit BW only works on partition 1! This is partition %d",
12589 			 pf->hw.partition_id);
12590 		ret = -EOPNOTSUPP;
12591 		goto bw_commit_out;
12592 	}
12593 
12594 	/* Acquire NVM for read access */
12595 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12596 	last_aq_status = pf->hw.aq.asq_last_status;
12597 	if (ret) {
12598 		dev_info(&pf->pdev->dev,
12599 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12600 			 ERR_PTR(ret),
12601 			 i40e_aq_str(&pf->hw, last_aq_status));
12602 		goto bw_commit_out;
12603 	}
12604 
12605 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12606 	ret = i40e_aq_read_nvm(&pf->hw,
12607 			       I40E_SR_NVM_CONTROL_WORD,
12608 			       0x10, sizeof(nvm_word), &nvm_word,
12609 			       false, NULL);
12610 	/* Save off last admin queue command status before releasing
12611 	 * the NVM
12612 	 */
12613 	last_aq_status = pf->hw.aq.asq_last_status;
12614 	i40e_release_nvm(&pf->hw);
12615 	if (ret) {
12616 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12617 			 ERR_PTR(ret),
12618 			 i40e_aq_str(&pf->hw, last_aq_status));
12619 		goto bw_commit_out;
12620 	}
12621 
12622 	/* Wait a bit for NVM release to complete */
12623 	msleep(50);
12624 
12625 	/* Acquire NVM for write access */
12626 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12627 	last_aq_status = pf->hw.aq.asq_last_status;
12628 	if (ret) {
12629 		dev_info(&pf->pdev->dev,
12630 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12631 			 ERR_PTR(ret),
12632 			 i40e_aq_str(&pf->hw, last_aq_status));
12633 		goto bw_commit_out;
12634 	}
12635 	/* Write it back out unchanged to initiate update NVM,
12636 	 * which will force a write of the shadow (alt) RAM to
12637 	 * the NVM - thus storing the bandwidth values permanently.
12638 	 */
12639 	ret = i40e_aq_update_nvm(&pf->hw,
12640 				 I40E_SR_NVM_CONTROL_WORD,
12641 				 0x10, sizeof(nvm_word),
12642 				 &nvm_word, true, 0, NULL);
12643 	/* Save off last admin queue command status before releasing
12644 	 * the NVM
12645 	 */
12646 	last_aq_status = pf->hw.aq.asq_last_status;
12647 	i40e_release_nvm(&pf->hw);
12648 	if (ret)
12649 		dev_info(&pf->pdev->dev,
12650 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12651 			 ERR_PTR(ret),
12652 			 i40e_aq_str(&pf->hw, last_aq_status));
12653 bw_commit_out:
12654 
12655 	return ret;
12656 }
12657 
12658 /**
12659  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12660  * if total port shutdown feature is enabled for this PF
12661  * @pf: board private structure
12662  **/
12663 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12664 {
12665 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12666 #define I40E_FEATURES_ENABLE_PTR		0x2A
12667 #define I40E_CURRENT_SETTING_PTR		0x2B
12668 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12669 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12670 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12671 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12672 	u16 sr_emp_sr_settings_ptr = 0;
12673 	u16 features_enable = 0;
12674 	u16 link_behavior = 0;
12675 	int read_status = 0;
12676 	bool ret = false;
12677 
12678 	read_status = i40e_read_nvm_word(&pf->hw,
12679 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12680 					 &sr_emp_sr_settings_ptr);
12681 	if (read_status)
12682 		goto err_nvm;
12683 	read_status = i40e_read_nvm_word(&pf->hw,
12684 					 sr_emp_sr_settings_ptr +
12685 					 I40E_FEATURES_ENABLE_PTR,
12686 					 &features_enable);
12687 	if (read_status)
12688 		goto err_nvm;
12689 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12690 		read_status = i40e_read_nvm_module_data(&pf->hw,
12691 							I40E_SR_EMP_SR_SETTINGS_PTR,
12692 							I40E_CURRENT_SETTING_PTR,
12693 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12694 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12695 							&link_behavior);
12696 		if (read_status)
12697 			goto err_nvm;
12698 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12699 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12700 	}
12701 	return ret;
12702 
12703 err_nvm:
12704 	dev_warn(&pf->pdev->dev,
12705 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12706 		 ERR_PTR(read_status));
12707 	return ret;
12708 }
12709 
12710 /**
12711  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12712  * @pf: board private structure to initialize
12713  *
12714  * i40e_sw_init initializes the Adapter private data structure.
12715  * Fields are initialized based on PCI device information and
12716  * OS network device settings (MTU size).
12717  **/
12718 static int i40e_sw_init(struct i40e_pf *pf)
12719 {
12720 	int err = 0;
12721 	int size;
12722 	u16 pow;
12723 
12724 	/* Set default capability flags */
12725 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12726 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12727 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12728 
12729 	/* Set default ITR */
12730 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12731 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12732 
12733 	/* Depending on PF configurations, it is possible that the RSS
12734 	 * maximum might end up larger than the available queues
12735 	 */
12736 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12737 	pf->alloc_rss_size = 1;
12738 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12739 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12740 				 pf->hw.func_caps.num_tx_qp);
12741 
12742 	/* find the next higher power-of-2 of num cpus */
12743 	pow = roundup_pow_of_two(num_online_cpus());
12744 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12745 
12746 	if (pf->hw.func_caps.rss) {
12747 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12748 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12749 					   num_online_cpus());
12750 	}
12751 
12752 	/* MFP mode enabled */
12753 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12754 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12755 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12756 		if (i40e_get_partition_bw_setting(pf)) {
12757 			dev_warn(&pf->pdev->dev,
12758 				 "Could not get partition bw settings\n");
12759 		} else {
12760 			dev_info(&pf->pdev->dev,
12761 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12762 				 pf->min_bw, pf->max_bw);
12763 
12764 			/* nudge the Tx scheduler */
12765 			i40e_set_partition_bw_setting(pf);
12766 		}
12767 	}
12768 
12769 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12770 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12771 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12772 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12773 		    pf->hw.num_partitions > 1)
12774 			dev_info(&pf->pdev->dev,
12775 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12776 		else
12777 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12778 		pf->fdir_pf_filter_count =
12779 				 pf->hw.func_caps.fd_filters_guaranteed;
12780 		pf->hw.fdir_shared_filter_count =
12781 				 pf->hw.func_caps.fd_filters_best_effort;
12782 	}
12783 
12784 	/* Enable HW ATR eviction if possible */
12785 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12786 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12787 
12788 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12789 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12790 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12791 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12792 	}
12793 
12794 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12795 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12796 		/* IWARP needs one extra vector for CQP just like MISC.*/
12797 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12798 	}
12799 	/* Stopping FW LLDP engine is supported on XL710 and X722
12800 	 * starting from FW versions determined in i40e_init_adminq.
12801 	 * Stopping the FW LLDP engine is not supported on XL710
12802 	 * if NPAR is functioning so unset this hw flag in this case.
12803 	 */
12804 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12805 	    pf->hw.func_caps.npar_enable)
12806 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12807 
12808 #ifdef CONFIG_PCI_IOV
12809 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12810 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12811 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12812 		pf->num_req_vfs = min_t(int,
12813 					pf->hw.func_caps.num_vfs,
12814 					I40E_MAX_VF_COUNT);
12815 	}
12816 #endif /* CONFIG_PCI_IOV */
12817 	pf->lan_veb = I40E_NO_VEB;
12818 	pf->lan_vsi = I40E_NO_VSI;
12819 
12820 	/* By default FW has this off for performance reasons */
12821 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12822 
12823 	/* set up queue assignment tracking */
12824 	size = sizeof(struct i40e_lump_tracking)
12825 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12826 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12827 	if (!pf->qp_pile) {
12828 		err = -ENOMEM;
12829 		goto sw_init_done;
12830 	}
12831 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12832 
12833 	pf->tx_timeout_recovery_level = 1;
12834 
12835 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12836 	    i40e_is_total_port_shutdown_enabled(pf)) {
12837 		/* Link down on close must be on when total port shutdown
12838 		 * is enabled for a given port
12839 		 */
12840 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12841 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12842 		dev_info(&pf->pdev->dev,
12843 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12844 	}
12845 	mutex_init(&pf->switch_mutex);
12846 
12847 sw_init_done:
12848 	return err;
12849 }
12850 
12851 /**
12852  * i40e_set_ntuple - set the ntuple feature flag and take action
12853  * @pf: board private structure to initialize
12854  * @features: the feature set that the stack is suggesting
12855  *
12856  * returns a bool to indicate if reset needs to happen
12857  **/
12858 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12859 {
12860 	bool need_reset = false;
12861 
12862 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12863 	 * the state changed, we need to reset.
12864 	 */
12865 	if (features & NETIF_F_NTUPLE) {
12866 		/* Enable filters and mark for reset */
12867 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12868 			need_reset = true;
12869 		/* enable FD_SB only if there is MSI-X vector and no cloud
12870 		 * filters exist
12871 		 */
12872 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12873 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12874 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12875 		}
12876 	} else {
12877 		/* turn off filters, mark for reset and clear SW filter list */
12878 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12879 			need_reset = true;
12880 			i40e_fdir_filter_exit(pf);
12881 		}
12882 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12883 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12884 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12885 
12886 		/* reset fd counters */
12887 		pf->fd_add_err = 0;
12888 		pf->fd_atr_cnt = 0;
12889 		/* if ATR was auto disabled it can be re-enabled. */
12890 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12891 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12892 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12893 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12894 	}
12895 	return need_reset;
12896 }
12897 
12898 /**
12899  * i40e_clear_rss_lut - clear the rx hash lookup table
12900  * @vsi: the VSI being configured
12901  **/
12902 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12903 {
12904 	struct i40e_pf *pf = vsi->back;
12905 	struct i40e_hw *hw = &pf->hw;
12906 	u16 vf_id = vsi->vf_id;
12907 	u8 i;
12908 
12909 	if (vsi->type == I40E_VSI_MAIN) {
12910 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12911 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12912 	} else if (vsi->type == I40E_VSI_SRIOV) {
12913 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12914 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12915 	} else {
12916 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12917 	}
12918 }
12919 
12920 /**
12921  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12922  * @vsi: ptr to VSI
12923  * @ena: flag to indicate the on/off setting
12924  */
12925 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12926 {
12927 	bool if_running = netif_running(vsi->netdev) &&
12928 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12929 	int ret;
12930 
12931 	if (if_running)
12932 		i40e_down(vsi);
12933 
12934 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12935 	if (ret)
12936 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12937 	if (if_running)
12938 		i40e_up(vsi);
12939 
12940 	return ret;
12941 }
12942 
12943 /**
12944  * i40e_set_features - set the netdev feature flags
12945  * @netdev: ptr to the netdev being adjusted
12946  * @features: the feature set that the stack is suggesting
12947  * Note: expects to be called while under rtnl_lock()
12948  **/
12949 static int i40e_set_features(struct net_device *netdev,
12950 			     netdev_features_t features)
12951 {
12952 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12953 	struct i40e_vsi *vsi = np->vsi;
12954 	struct i40e_pf *pf = vsi->back;
12955 	bool need_reset;
12956 
12957 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12958 		i40e_pf_config_rss(pf);
12959 	else if (!(features & NETIF_F_RXHASH) &&
12960 		 netdev->features & NETIF_F_RXHASH)
12961 		i40e_clear_rss_lut(vsi);
12962 
12963 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12964 		i40e_vlan_stripping_enable(vsi);
12965 	else
12966 		i40e_vlan_stripping_disable(vsi);
12967 
12968 	if (!(features & NETIF_F_HW_TC) &&
12969 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12970 		dev_err(&pf->pdev->dev,
12971 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12972 		return -EINVAL;
12973 	}
12974 
12975 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12976 		i40e_del_all_macvlans(vsi);
12977 
12978 	need_reset = i40e_set_ntuple(pf, features);
12979 
12980 	if (need_reset)
12981 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12982 
12983 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12984 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12985 
12986 	return 0;
12987 }
12988 
12989 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12990 				    unsigned int table, unsigned int idx,
12991 				    struct udp_tunnel_info *ti)
12992 {
12993 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12994 	struct i40e_hw *hw = &np->vsi->back->hw;
12995 	u8 type, filter_index;
12996 	int ret;
12997 
12998 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12999 						   I40E_AQC_TUNNEL_TYPE_NGE;
13000 
13001 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13002 				     NULL);
13003 	if (ret) {
13004 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13005 			    ERR_PTR(ret),
13006 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13007 		return -EIO;
13008 	}
13009 
13010 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13011 	return 0;
13012 }
13013 
13014 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13015 				      unsigned int table, unsigned int idx,
13016 				      struct udp_tunnel_info *ti)
13017 {
13018 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13019 	struct i40e_hw *hw = &np->vsi->back->hw;
13020 	int ret;
13021 
13022 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13023 	if (ret) {
13024 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13025 			    ERR_PTR(ret),
13026 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13027 		return -EIO;
13028 	}
13029 
13030 	return 0;
13031 }
13032 
13033 static int i40e_get_phys_port_id(struct net_device *netdev,
13034 				 struct netdev_phys_item_id *ppid)
13035 {
13036 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13037 	struct i40e_pf *pf = np->vsi->back;
13038 	struct i40e_hw *hw = &pf->hw;
13039 
13040 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13041 		return -EOPNOTSUPP;
13042 
13043 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13044 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13045 
13046 	return 0;
13047 }
13048 
13049 /**
13050  * i40e_ndo_fdb_add - add an entry to the hardware database
13051  * @ndm: the input from the stack
13052  * @tb: pointer to array of nladdr (unused)
13053  * @dev: the net device pointer
13054  * @addr: the MAC address entry being added
13055  * @vid: VLAN ID
13056  * @flags: instructions from stack about fdb operation
13057  * @extack: netlink extended ack, unused currently
13058  */
13059 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13060 			    struct net_device *dev,
13061 			    const unsigned char *addr, u16 vid,
13062 			    u16 flags,
13063 			    struct netlink_ext_ack *extack)
13064 {
13065 	struct i40e_netdev_priv *np = netdev_priv(dev);
13066 	struct i40e_pf *pf = np->vsi->back;
13067 	int err = 0;
13068 
13069 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13070 		return -EOPNOTSUPP;
13071 
13072 	if (vid) {
13073 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13074 		return -EINVAL;
13075 	}
13076 
13077 	/* Hardware does not support aging addresses so if a
13078 	 * ndm_state is given only allow permanent addresses
13079 	 */
13080 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13081 		netdev_info(dev, "FDB only supports static addresses\n");
13082 		return -EINVAL;
13083 	}
13084 
13085 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13086 		err = dev_uc_add_excl(dev, addr);
13087 	else if (is_multicast_ether_addr(addr))
13088 		err = dev_mc_add_excl(dev, addr);
13089 	else
13090 		err = -EINVAL;
13091 
13092 	/* Only return duplicate errors if NLM_F_EXCL is set */
13093 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13094 		err = 0;
13095 
13096 	return err;
13097 }
13098 
13099 /**
13100  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13101  * @dev: the netdev being configured
13102  * @nlh: RTNL message
13103  * @flags: bridge flags
13104  * @extack: netlink extended ack
13105  *
13106  * Inserts a new hardware bridge if not already created and
13107  * enables the bridging mode requested (VEB or VEPA). If the
13108  * hardware bridge has already been inserted and the request
13109  * is to change the mode then that requires a PF reset to
13110  * allow rebuild of the components with required hardware
13111  * bridge mode enabled.
13112  *
13113  * Note: expects to be called while under rtnl_lock()
13114  **/
13115 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13116 				   struct nlmsghdr *nlh,
13117 				   u16 flags,
13118 				   struct netlink_ext_ack *extack)
13119 {
13120 	struct i40e_netdev_priv *np = netdev_priv(dev);
13121 	struct i40e_vsi *vsi = np->vsi;
13122 	struct i40e_pf *pf = vsi->back;
13123 	struct i40e_veb *veb = NULL;
13124 	struct nlattr *attr, *br_spec;
13125 	int i, rem;
13126 
13127 	/* Only for PF VSI for now */
13128 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13129 		return -EOPNOTSUPP;
13130 
13131 	/* Find the HW bridge for PF VSI */
13132 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13133 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13134 			veb = pf->veb[i];
13135 	}
13136 
13137 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13138 	if (!br_spec)
13139 		return -EINVAL;
13140 
13141 	nla_for_each_nested(attr, br_spec, rem) {
13142 		__u16 mode;
13143 
13144 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13145 			continue;
13146 
13147 		mode = nla_get_u16(attr);
13148 		if ((mode != BRIDGE_MODE_VEPA) &&
13149 		    (mode != BRIDGE_MODE_VEB))
13150 			return -EINVAL;
13151 
13152 		/* Insert a new HW bridge */
13153 		if (!veb) {
13154 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13155 					     vsi->tc_config.enabled_tc);
13156 			if (veb) {
13157 				veb->bridge_mode = mode;
13158 				i40e_config_bridge_mode(veb);
13159 			} else {
13160 				/* No Bridge HW offload available */
13161 				return -ENOENT;
13162 			}
13163 			break;
13164 		} else if (mode != veb->bridge_mode) {
13165 			/* Existing HW bridge but different mode needs reset */
13166 			veb->bridge_mode = mode;
13167 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13168 			if (mode == BRIDGE_MODE_VEB)
13169 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13170 			else
13171 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13172 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13173 			break;
13174 		}
13175 	}
13176 
13177 	return 0;
13178 }
13179 
13180 /**
13181  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13182  * @skb: skb buff
13183  * @pid: process id
13184  * @seq: RTNL message seq #
13185  * @dev: the netdev being configured
13186  * @filter_mask: unused
13187  * @nlflags: netlink flags passed in
13188  *
13189  * Return the mode in which the hardware bridge is operating in
13190  * i.e VEB or VEPA.
13191  **/
13192 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13193 				   struct net_device *dev,
13194 				   u32 __always_unused filter_mask,
13195 				   int nlflags)
13196 {
13197 	struct i40e_netdev_priv *np = netdev_priv(dev);
13198 	struct i40e_vsi *vsi = np->vsi;
13199 	struct i40e_pf *pf = vsi->back;
13200 	struct i40e_veb *veb = NULL;
13201 	int i;
13202 
13203 	/* Only for PF VSI for now */
13204 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13205 		return -EOPNOTSUPP;
13206 
13207 	/* Find the HW bridge for the PF VSI */
13208 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13209 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13210 			veb = pf->veb[i];
13211 	}
13212 
13213 	if (!veb)
13214 		return 0;
13215 
13216 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13217 				       0, 0, nlflags, filter_mask, NULL);
13218 }
13219 
13220 /**
13221  * i40e_features_check - Validate encapsulated packet conforms to limits
13222  * @skb: skb buff
13223  * @dev: This physical port's netdev
13224  * @features: Offload features that the stack believes apply
13225  **/
13226 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13227 					     struct net_device *dev,
13228 					     netdev_features_t features)
13229 {
13230 	size_t len;
13231 
13232 	/* No point in doing any of this if neither checksum nor GSO are
13233 	 * being requested for this frame.  We can rule out both by just
13234 	 * checking for CHECKSUM_PARTIAL
13235 	 */
13236 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13237 		return features;
13238 
13239 	/* We cannot support GSO if the MSS is going to be less than
13240 	 * 64 bytes.  If it is then we need to drop support for GSO.
13241 	 */
13242 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13243 		features &= ~NETIF_F_GSO_MASK;
13244 
13245 	/* MACLEN can support at most 63 words */
13246 	len = skb_network_header(skb) - skb->data;
13247 	if (len & ~(63 * 2))
13248 		goto out_err;
13249 
13250 	/* IPLEN and EIPLEN can support at most 127 dwords */
13251 	len = skb_transport_header(skb) - skb_network_header(skb);
13252 	if (len & ~(127 * 4))
13253 		goto out_err;
13254 
13255 	if (skb->encapsulation) {
13256 		/* L4TUNLEN can support 127 words */
13257 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13258 		if (len & ~(127 * 2))
13259 			goto out_err;
13260 
13261 		/* IPLEN can support at most 127 dwords */
13262 		len = skb_inner_transport_header(skb) -
13263 		      skb_inner_network_header(skb);
13264 		if (len & ~(127 * 4))
13265 			goto out_err;
13266 	}
13267 
13268 	/* No need to validate L4LEN as TCP is the only protocol with a
13269 	 * flexible value and we support all possible values supported
13270 	 * by TCP, which is at most 15 dwords
13271 	 */
13272 
13273 	return features;
13274 out_err:
13275 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13276 }
13277 
13278 /**
13279  * i40e_xdp_setup - add/remove an XDP program
13280  * @vsi: VSI to changed
13281  * @prog: XDP program
13282  * @extack: netlink extended ack
13283  **/
13284 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13285 			  struct netlink_ext_ack *extack)
13286 {
13287 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13288 	struct i40e_pf *pf = vsi->back;
13289 	struct bpf_prog *old_prog;
13290 	bool need_reset;
13291 	int i;
13292 
13293 	/* Don't allow frames that span over multiple buffers */
13294 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13295 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13296 		return -EINVAL;
13297 	}
13298 
13299 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13300 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13301 
13302 	if (need_reset)
13303 		i40e_prep_for_reset(pf);
13304 
13305 	/* VSI shall be deleted in a moment, just return EINVAL */
13306 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13307 		return -EINVAL;
13308 
13309 	old_prog = xchg(&vsi->xdp_prog, prog);
13310 
13311 	if (need_reset) {
13312 		if (!prog) {
13313 			xdp_features_clear_redirect_target(vsi->netdev);
13314 			/* Wait until ndo_xsk_wakeup completes. */
13315 			synchronize_rcu();
13316 		}
13317 		i40e_reset_and_rebuild(pf, true, true);
13318 	}
13319 
13320 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13321 		if (i40e_realloc_rx_bi_zc(vsi, true))
13322 			return -ENOMEM;
13323 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13324 		if (i40e_realloc_rx_bi_zc(vsi, false))
13325 			return -ENOMEM;
13326 	}
13327 
13328 	for (i = 0; i < vsi->num_queue_pairs; i++)
13329 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13330 
13331 	if (old_prog)
13332 		bpf_prog_put(old_prog);
13333 
13334 	/* Kick start the NAPI context if there is an AF_XDP socket open
13335 	 * on that queue id. This so that receiving will start.
13336 	 */
13337 	if (need_reset && prog) {
13338 		for (i = 0; i < vsi->num_queue_pairs; i++)
13339 			if (vsi->xdp_rings[i]->xsk_pool)
13340 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13341 						      XDP_WAKEUP_RX);
13342 		xdp_features_set_redirect_target(vsi->netdev, true);
13343 	}
13344 
13345 	return 0;
13346 }
13347 
13348 /**
13349  * i40e_enter_busy_conf - Enters busy config state
13350  * @vsi: vsi
13351  *
13352  * Returns 0 on success, <0 for failure.
13353  **/
13354 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13355 {
13356 	struct i40e_pf *pf = vsi->back;
13357 	int timeout = 50;
13358 
13359 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13360 		timeout--;
13361 		if (!timeout)
13362 			return -EBUSY;
13363 		usleep_range(1000, 2000);
13364 	}
13365 
13366 	return 0;
13367 }
13368 
13369 /**
13370  * i40e_exit_busy_conf - Exits busy config state
13371  * @vsi: vsi
13372  **/
13373 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13374 {
13375 	struct i40e_pf *pf = vsi->back;
13376 
13377 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13378 }
13379 
13380 /**
13381  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13382  * @vsi: vsi
13383  * @queue_pair: queue pair
13384  **/
13385 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13386 {
13387 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13388 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13389 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13390 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13391 	if (i40e_enabled_xdp_vsi(vsi)) {
13392 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13393 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13394 	}
13395 }
13396 
13397 /**
13398  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13399  * @vsi: vsi
13400  * @queue_pair: queue pair
13401  **/
13402 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13403 {
13404 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13405 	if (i40e_enabled_xdp_vsi(vsi)) {
13406 		/* Make sure that in-progress ndo_xdp_xmit calls are
13407 		 * completed.
13408 		 */
13409 		synchronize_rcu();
13410 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13411 	}
13412 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13413 }
13414 
13415 /**
13416  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13417  * @vsi: vsi
13418  * @queue_pair: queue pair
13419  * @enable: true for enable, false for disable
13420  **/
13421 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13422 					bool enable)
13423 {
13424 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13425 	struct i40e_q_vector *q_vector = rxr->q_vector;
13426 
13427 	if (!vsi->netdev)
13428 		return;
13429 
13430 	/* All rings in a qp belong to the same qvector. */
13431 	if (q_vector->rx.ring || q_vector->tx.ring) {
13432 		if (enable)
13433 			napi_enable(&q_vector->napi);
13434 		else
13435 			napi_disable(&q_vector->napi);
13436 	}
13437 }
13438 
13439 /**
13440  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13441  * @vsi: vsi
13442  * @queue_pair: queue pair
13443  * @enable: true for enable, false for disable
13444  *
13445  * Returns 0 on success, <0 on failure.
13446  **/
13447 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13448 					bool enable)
13449 {
13450 	struct i40e_pf *pf = vsi->back;
13451 	int pf_q, ret = 0;
13452 
13453 	pf_q = vsi->base_queue + queue_pair;
13454 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13455 				     false /*is xdp*/, enable);
13456 	if (ret) {
13457 		dev_info(&pf->pdev->dev,
13458 			 "VSI seid %d Tx ring %d %sable timeout\n",
13459 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13460 		return ret;
13461 	}
13462 
13463 	i40e_control_rx_q(pf, pf_q, enable);
13464 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13465 	if (ret) {
13466 		dev_info(&pf->pdev->dev,
13467 			 "VSI seid %d Rx ring %d %sable timeout\n",
13468 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13469 		return ret;
13470 	}
13471 
13472 	/* Due to HW errata, on Rx disable only, the register can
13473 	 * indicate done before it really is. Needs 50ms to be sure
13474 	 */
13475 	if (!enable)
13476 		mdelay(50);
13477 
13478 	if (!i40e_enabled_xdp_vsi(vsi))
13479 		return ret;
13480 
13481 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13482 				     pf_q + vsi->alloc_queue_pairs,
13483 				     true /*is xdp*/, enable);
13484 	if (ret) {
13485 		dev_info(&pf->pdev->dev,
13486 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13487 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13488 	}
13489 
13490 	return ret;
13491 }
13492 
13493 /**
13494  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13495  * @vsi: vsi
13496  * @queue_pair: queue_pair
13497  **/
13498 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13499 {
13500 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13501 	struct i40e_pf *pf = vsi->back;
13502 	struct i40e_hw *hw = &pf->hw;
13503 
13504 	/* All rings in a qp belong to the same qvector. */
13505 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13506 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13507 	else
13508 		i40e_irq_dynamic_enable_icr0(pf);
13509 
13510 	i40e_flush(hw);
13511 }
13512 
13513 /**
13514  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13515  * @vsi: vsi
13516  * @queue_pair: queue_pair
13517  **/
13518 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13519 {
13520 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13521 	struct i40e_pf *pf = vsi->back;
13522 	struct i40e_hw *hw = &pf->hw;
13523 
13524 	/* For simplicity, instead of removing the qp interrupt causes
13525 	 * from the interrupt linked list, we simply disable the interrupt, and
13526 	 * leave the list intact.
13527 	 *
13528 	 * All rings in a qp belong to the same qvector.
13529 	 */
13530 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13531 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13532 
13533 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13534 		i40e_flush(hw);
13535 		synchronize_irq(pf->msix_entries[intpf].vector);
13536 	} else {
13537 		/* Legacy and MSI mode - this stops all interrupt handling */
13538 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13539 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13540 		i40e_flush(hw);
13541 		synchronize_irq(pf->pdev->irq);
13542 	}
13543 }
13544 
13545 /**
13546  * i40e_queue_pair_disable - Disables a queue pair
13547  * @vsi: vsi
13548  * @queue_pair: queue pair
13549  *
13550  * Returns 0 on success, <0 on failure.
13551  **/
13552 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13553 {
13554 	int err;
13555 
13556 	err = i40e_enter_busy_conf(vsi);
13557 	if (err)
13558 		return err;
13559 
13560 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13561 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13562 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13563 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13564 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13565 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13566 
13567 	return err;
13568 }
13569 
13570 /**
13571  * i40e_queue_pair_enable - Enables a queue pair
13572  * @vsi: vsi
13573  * @queue_pair: queue pair
13574  *
13575  * Returns 0 on success, <0 on failure.
13576  **/
13577 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13578 {
13579 	int err;
13580 
13581 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13582 	if (err)
13583 		return err;
13584 
13585 	if (i40e_enabled_xdp_vsi(vsi)) {
13586 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13587 		if (err)
13588 			return err;
13589 	}
13590 
13591 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13592 	if (err)
13593 		return err;
13594 
13595 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13596 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13597 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13598 
13599 	i40e_exit_busy_conf(vsi);
13600 
13601 	return err;
13602 }
13603 
13604 /**
13605  * i40e_xdp - implements ndo_bpf for i40e
13606  * @dev: netdevice
13607  * @xdp: XDP command
13608  **/
13609 static int i40e_xdp(struct net_device *dev,
13610 		    struct netdev_bpf *xdp)
13611 {
13612 	struct i40e_netdev_priv *np = netdev_priv(dev);
13613 	struct i40e_vsi *vsi = np->vsi;
13614 
13615 	if (vsi->type != I40E_VSI_MAIN)
13616 		return -EINVAL;
13617 
13618 	switch (xdp->command) {
13619 	case XDP_SETUP_PROG:
13620 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13621 	case XDP_SETUP_XSK_POOL:
13622 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13623 					   xdp->xsk.queue_id);
13624 	default:
13625 		return -EINVAL;
13626 	}
13627 }
13628 
13629 static const struct net_device_ops i40e_netdev_ops = {
13630 	.ndo_open		= i40e_open,
13631 	.ndo_stop		= i40e_close,
13632 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13633 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13634 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13635 	.ndo_validate_addr	= eth_validate_addr,
13636 	.ndo_set_mac_address	= i40e_set_mac,
13637 	.ndo_change_mtu		= i40e_change_mtu,
13638 	.ndo_eth_ioctl		= i40e_ioctl,
13639 	.ndo_tx_timeout		= i40e_tx_timeout,
13640 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13641 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13642 #ifdef CONFIG_NET_POLL_CONTROLLER
13643 	.ndo_poll_controller	= i40e_netpoll,
13644 #endif
13645 	.ndo_setup_tc		= __i40e_setup_tc,
13646 	.ndo_select_queue	= i40e_lan_select_queue,
13647 	.ndo_set_features	= i40e_set_features,
13648 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13649 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13650 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13651 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13652 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13653 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13654 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13655 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13656 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13657 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13658 	.ndo_features_check	= i40e_features_check,
13659 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13660 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13661 	.ndo_bpf		= i40e_xdp,
13662 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13663 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13664 	.ndo_dfwd_add_station	= i40e_fwd_add,
13665 	.ndo_dfwd_del_station	= i40e_fwd_del,
13666 };
13667 
13668 /**
13669  * i40e_config_netdev - Setup the netdev flags
13670  * @vsi: the VSI being configured
13671  *
13672  * Returns 0 on success, negative value on failure
13673  **/
13674 static int i40e_config_netdev(struct i40e_vsi *vsi)
13675 {
13676 	struct i40e_pf *pf = vsi->back;
13677 	struct i40e_hw *hw = &pf->hw;
13678 	struct i40e_netdev_priv *np;
13679 	struct net_device *netdev;
13680 	u8 broadcast[ETH_ALEN];
13681 	u8 mac_addr[ETH_ALEN];
13682 	int etherdev_size;
13683 	netdev_features_t hw_enc_features;
13684 	netdev_features_t hw_features;
13685 
13686 	etherdev_size = sizeof(struct i40e_netdev_priv);
13687 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13688 	if (!netdev)
13689 		return -ENOMEM;
13690 
13691 	vsi->netdev = netdev;
13692 	np = netdev_priv(netdev);
13693 	np->vsi = vsi;
13694 
13695 	hw_enc_features = NETIF_F_SG			|
13696 			  NETIF_F_HW_CSUM		|
13697 			  NETIF_F_HIGHDMA		|
13698 			  NETIF_F_SOFT_FEATURES		|
13699 			  NETIF_F_TSO			|
13700 			  NETIF_F_TSO_ECN		|
13701 			  NETIF_F_TSO6			|
13702 			  NETIF_F_GSO_GRE		|
13703 			  NETIF_F_GSO_GRE_CSUM		|
13704 			  NETIF_F_GSO_PARTIAL		|
13705 			  NETIF_F_GSO_IPXIP4		|
13706 			  NETIF_F_GSO_IPXIP6		|
13707 			  NETIF_F_GSO_UDP_TUNNEL	|
13708 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13709 			  NETIF_F_GSO_UDP_L4		|
13710 			  NETIF_F_SCTP_CRC		|
13711 			  NETIF_F_RXHASH		|
13712 			  NETIF_F_RXCSUM		|
13713 			  0;
13714 
13715 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13716 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13717 
13718 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13719 
13720 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13721 
13722 	netdev->hw_enc_features |= hw_enc_features;
13723 
13724 	/* record features VLANs can make use of */
13725 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13726 
13727 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13728 				   NETIF_F_GSO_GRE_CSUM |	\
13729 				   NETIF_F_GSO_IPXIP4 |		\
13730 				   NETIF_F_GSO_IPXIP6 |		\
13731 				   NETIF_F_GSO_UDP_TUNNEL |	\
13732 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13733 
13734 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13735 	netdev->features |= NETIF_F_GSO_PARTIAL |
13736 			    I40E_GSO_PARTIAL_FEATURES;
13737 
13738 	netdev->mpls_features |= NETIF_F_SG;
13739 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13740 	netdev->mpls_features |= NETIF_F_TSO;
13741 	netdev->mpls_features |= NETIF_F_TSO6;
13742 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13743 
13744 	/* enable macvlan offloads */
13745 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13746 
13747 	hw_features = hw_enc_features		|
13748 		      NETIF_F_HW_VLAN_CTAG_TX	|
13749 		      NETIF_F_HW_VLAN_CTAG_RX;
13750 
13751 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13752 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13753 
13754 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13755 
13756 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13757 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13758 
13759 	netdev->features &= ~NETIF_F_HW_TC;
13760 
13761 	if (vsi->type == I40E_VSI_MAIN) {
13762 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13763 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13764 		/* The following steps are necessary for two reasons. First,
13765 		 * some older NVM configurations load a default MAC-VLAN
13766 		 * filter that will accept any tagged packet, and we want to
13767 		 * replace this with a normal filter. Additionally, it is
13768 		 * possible our MAC address was provided by the platform using
13769 		 * Open Firmware or similar.
13770 		 *
13771 		 * Thus, we need to remove the default filter and install one
13772 		 * specific to the MAC address.
13773 		 */
13774 		i40e_rm_default_mac_filter(vsi, mac_addr);
13775 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13776 		i40e_add_mac_filter(vsi, mac_addr);
13777 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13778 
13779 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13780 				       NETDEV_XDP_ACT_REDIRECT |
13781 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13782 				       NETDEV_XDP_ACT_RX_SG;
13783 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13784 	} else {
13785 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13786 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13787 		 * the end, which is 4 bytes long, so force truncation of the
13788 		 * original name by IFNAMSIZ - 4
13789 		 */
13790 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13791 			 IFNAMSIZ - 4,
13792 			 pf->vsi[pf->lan_vsi]->netdev->name);
13793 		eth_random_addr(mac_addr);
13794 
13795 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13796 		i40e_add_mac_filter(vsi, mac_addr);
13797 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13798 	}
13799 
13800 	/* Add the broadcast filter so that we initially will receive
13801 	 * broadcast packets. Note that when a new VLAN is first added the
13802 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13803 	 * specific filters as part of transitioning into "vlan" operation.
13804 	 * When more VLANs are added, the driver will copy each existing MAC
13805 	 * filter and add it for the new VLAN.
13806 	 *
13807 	 * Broadcast filters are handled specially by
13808 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13809 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13810 	 * filter. The subtask will update the correct broadcast promiscuous
13811 	 * bits as VLANs become active or inactive.
13812 	 */
13813 	eth_broadcast_addr(broadcast);
13814 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13815 	i40e_add_mac_filter(vsi, broadcast);
13816 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13817 
13818 	eth_hw_addr_set(netdev, mac_addr);
13819 	ether_addr_copy(netdev->perm_addr, mac_addr);
13820 
13821 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13822 	netdev->neigh_priv_len = sizeof(u32) * 4;
13823 
13824 	netdev->priv_flags |= IFF_UNICAST_FLT;
13825 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13826 	/* Setup netdev TC information */
13827 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13828 
13829 	netdev->netdev_ops = &i40e_netdev_ops;
13830 	netdev->watchdog_timeo = 5 * HZ;
13831 	i40e_set_ethtool_ops(netdev);
13832 
13833 	/* MTU range: 68 - 9706 */
13834 	netdev->min_mtu = ETH_MIN_MTU;
13835 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13836 
13837 	return 0;
13838 }
13839 
13840 /**
13841  * i40e_vsi_delete - Delete a VSI from the switch
13842  * @vsi: the VSI being removed
13843  *
13844  * Returns 0 on success, negative value on failure
13845  **/
13846 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13847 {
13848 	/* remove default VSI is not allowed */
13849 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13850 		return;
13851 
13852 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13853 }
13854 
13855 /**
13856  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13857  * @vsi: the VSI being queried
13858  *
13859  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13860  **/
13861 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13862 {
13863 	struct i40e_veb *veb;
13864 	struct i40e_pf *pf = vsi->back;
13865 
13866 	/* Uplink is not a bridge so default to VEB */
13867 	if (vsi->veb_idx >= I40E_MAX_VEB)
13868 		return 1;
13869 
13870 	veb = pf->veb[vsi->veb_idx];
13871 	if (!veb) {
13872 		dev_info(&pf->pdev->dev,
13873 			 "There is no veb associated with the bridge\n");
13874 		return -ENOENT;
13875 	}
13876 
13877 	/* Uplink is a bridge in VEPA mode */
13878 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13879 		return 0;
13880 	} else {
13881 		/* Uplink is a bridge in VEB mode */
13882 		return 1;
13883 	}
13884 
13885 	/* VEPA is now default bridge, so return 0 */
13886 	return 0;
13887 }
13888 
13889 /**
13890  * i40e_add_vsi - Add a VSI to the switch
13891  * @vsi: the VSI being configured
13892  *
13893  * This initializes a VSI context depending on the VSI type to be added and
13894  * passes it down to the add_vsi aq command.
13895  **/
13896 static int i40e_add_vsi(struct i40e_vsi *vsi)
13897 {
13898 	int ret = -ENODEV;
13899 	struct i40e_pf *pf = vsi->back;
13900 	struct i40e_hw *hw = &pf->hw;
13901 	struct i40e_vsi_context ctxt;
13902 	struct i40e_mac_filter *f;
13903 	struct hlist_node *h;
13904 	int bkt;
13905 
13906 	u8 enabled_tc = 0x1; /* TC0 enabled */
13907 	int f_count = 0;
13908 
13909 	memset(&ctxt, 0, sizeof(ctxt));
13910 	switch (vsi->type) {
13911 	case I40E_VSI_MAIN:
13912 		/* The PF's main VSI is already setup as part of the
13913 		 * device initialization, so we'll not bother with
13914 		 * the add_vsi call, but we will retrieve the current
13915 		 * VSI context.
13916 		 */
13917 		ctxt.seid = pf->main_vsi_seid;
13918 		ctxt.pf_num = pf->hw.pf_id;
13919 		ctxt.vf_num = 0;
13920 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13921 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13922 		if (ret) {
13923 			dev_info(&pf->pdev->dev,
13924 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13925 				 ERR_PTR(ret),
13926 				 i40e_aq_str(&pf->hw,
13927 					     pf->hw.aq.asq_last_status));
13928 			return -ENOENT;
13929 		}
13930 		vsi->info = ctxt.info;
13931 		vsi->info.valid_sections = 0;
13932 
13933 		vsi->seid = ctxt.seid;
13934 		vsi->id = ctxt.vsi_number;
13935 
13936 		enabled_tc = i40e_pf_get_tc_map(pf);
13937 
13938 		/* Source pruning is enabled by default, so the flag is
13939 		 * negative logic - if it's set, we need to fiddle with
13940 		 * the VSI to disable source pruning.
13941 		 */
13942 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13943 			memset(&ctxt, 0, sizeof(ctxt));
13944 			ctxt.seid = pf->main_vsi_seid;
13945 			ctxt.pf_num = pf->hw.pf_id;
13946 			ctxt.vf_num = 0;
13947 			ctxt.info.valid_sections |=
13948 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13949 			ctxt.info.switch_id =
13950 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13951 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13952 			if (ret) {
13953 				dev_info(&pf->pdev->dev,
13954 					 "update vsi failed, err %d aq_err %s\n",
13955 					 ret,
13956 					 i40e_aq_str(&pf->hw,
13957 						     pf->hw.aq.asq_last_status));
13958 				ret = -ENOENT;
13959 				goto err;
13960 			}
13961 		}
13962 
13963 		/* MFP mode setup queue map and update VSI */
13964 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13965 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13966 			memset(&ctxt, 0, sizeof(ctxt));
13967 			ctxt.seid = pf->main_vsi_seid;
13968 			ctxt.pf_num = pf->hw.pf_id;
13969 			ctxt.vf_num = 0;
13970 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13971 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13972 			if (ret) {
13973 				dev_info(&pf->pdev->dev,
13974 					 "update vsi failed, err %pe aq_err %s\n",
13975 					 ERR_PTR(ret),
13976 					 i40e_aq_str(&pf->hw,
13977 						    pf->hw.aq.asq_last_status));
13978 				ret = -ENOENT;
13979 				goto err;
13980 			}
13981 			/* update the local VSI info queue map */
13982 			i40e_vsi_update_queue_map(vsi, &ctxt);
13983 			vsi->info.valid_sections = 0;
13984 		} else {
13985 			/* Default/Main VSI is only enabled for TC0
13986 			 * reconfigure it to enable all TCs that are
13987 			 * available on the port in SFP mode.
13988 			 * For MFP case the iSCSI PF would use this
13989 			 * flow to enable LAN+iSCSI TC.
13990 			 */
13991 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13992 			if (ret) {
13993 				/* Single TC condition is not fatal,
13994 				 * message and continue
13995 				 */
13996 				dev_info(&pf->pdev->dev,
13997 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13998 					 enabled_tc,
13999 					 ERR_PTR(ret),
14000 					 i40e_aq_str(&pf->hw,
14001 						    pf->hw.aq.asq_last_status));
14002 			}
14003 		}
14004 		break;
14005 
14006 	case I40E_VSI_FDIR:
14007 		ctxt.pf_num = hw->pf_id;
14008 		ctxt.vf_num = 0;
14009 		ctxt.uplink_seid = vsi->uplink_seid;
14010 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14011 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14012 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
14013 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14014 			ctxt.info.valid_sections |=
14015 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14016 			ctxt.info.switch_id =
14017 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14018 		}
14019 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14020 		break;
14021 
14022 	case I40E_VSI_VMDQ2:
14023 		ctxt.pf_num = hw->pf_id;
14024 		ctxt.vf_num = 0;
14025 		ctxt.uplink_seid = vsi->uplink_seid;
14026 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14027 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14028 
14029 		/* This VSI is connected to VEB so the switch_id
14030 		 * should be set to zero by default.
14031 		 */
14032 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14033 			ctxt.info.valid_sections |=
14034 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14035 			ctxt.info.switch_id =
14036 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14037 		}
14038 
14039 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14040 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14041 		break;
14042 
14043 	case I40E_VSI_SRIOV:
14044 		ctxt.pf_num = hw->pf_id;
14045 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14046 		ctxt.uplink_seid = vsi->uplink_seid;
14047 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14048 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14049 
14050 		/* This VSI is connected to VEB so the switch_id
14051 		 * should be set to zero by default.
14052 		 */
14053 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14054 			ctxt.info.valid_sections |=
14055 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14056 			ctxt.info.switch_id =
14057 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14058 		}
14059 
14060 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14061 			ctxt.info.valid_sections |=
14062 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14063 			ctxt.info.queueing_opt_flags |=
14064 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14065 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14066 		}
14067 
14068 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14069 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14070 		if (pf->vf[vsi->vf_id].spoofchk) {
14071 			ctxt.info.valid_sections |=
14072 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14073 			ctxt.info.sec_flags |=
14074 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14075 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14076 		}
14077 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14078 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14079 		break;
14080 
14081 	case I40E_VSI_IWARP:
14082 		/* send down message to iWARP */
14083 		break;
14084 
14085 	default:
14086 		return -ENODEV;
14087 	}
14088 
14089 	if (vsi->type != I40E_VSI_MAIN) {
14090 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14091 		if (ret) {
14092 			dev_info(&vsi->back->pdev->dev,
14093 				 "add vsi failed, err %pe aq_err %s\n",
14094 				 ERR_PTR(ret),
14095 				 i40e_aq_str(&pf->hw,
14096 					     pf->hw.aq.asq_last_status));
14097 			ret = -ENOENT;
14098 			goto err;
14099 		}
14100 		vsi->info = ctxt.info;
14101 		vsi->info.valid_sections = 0;
14102 		vsi->seid = ctxt.seid;
14103 		vsi->id = ctxt.vsi_number;
14104 	}
14105 
14106 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14107 	vsi->active_filters = 0;
14108 	/* If macvlan filters already exist, force them to get loaded */
14109 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14110 		f->state = I40E_FILTER_NEW;
14111 		f_count++;
14112 	}
14113 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14114 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14115 
14116 	if (f_count) {
14117 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14118 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14119 	}
14120 
14121 	/* Update VSI BW information */
14122 	ret = i40e_vsi_get_bw_info(vsi);
14123 	if (ret) {
14124 		dev_info(&pf->pdev->dev,
14125 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14126 			 ERR_PTR(ret),
14127 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14128 		/* VSI is already added so not tearing that up */
14129 		ret = 0;
14130 	}
14131 
14132 err:
14133 	return ret;
14134 }
14135 
14136 /**
14137  * i40e_vsi_release - Delete a VSI and free its resources
14138  * @vsi: the VSI being removed
14139  *
14140  * Returns 0 on success or < 0 on error
14141  **/
14142 int i40e_vsi_release(struct i40e_vsi *vsi)
14143 {
14144 	struct i40e_mac_filter *f;
14145 	struct hlist_node *h;
14146 	struct i40e_veb *veb = NULL;
14147 	struct i40e_pf *pf;
14148 	u16 uplink_seid;
14149 	int i, n, bkt;
14150 
14151 	pf = vsi->back;
14152 
14153 	/* release of a VEB-owner or last VSI is not allowed */
14154 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14155 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14156 			 vsi->seid, vsi->uplink_seid);
14157 		return -ENODEV;
14158 	}
14159 	if (vsi == pf->vsi[pf->lan_vsi] &&
14160 	    !test_bit(__I40E_DOWN, pf->state)) {
14161 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14162 		return -ENODEV;
14163 	}
14164 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14165 	uplink_seid = vsi->uplink_seid;
14166 
14167 	if (vsi->type != I40E_VSI_SRIOV) {
14168 		if (vsi->netdev_registered) {
14169 			vsi->netdev_registered = false;
14170 			if (vsi->netdev) {
14171 				/* results in a call to i40e_close() */
14172 				unregister_netdev(vsi->netdev);
14173 			}
14174 		} else {
14175 			i40e_vsi_close(vsi);
14176 		}
14177 		i40e_vsi_disable_irq(vsi);
14178 	}
14179 
14180 	if (vsi->type == I40E_VSI_MAIN)
14181 		i40e_devlink_destroy_port(pf);
14182 
14183 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14184 
14185 	/* clear the sync flag on all filters */
14186 	if (vsi->netdev) {
14187 		__dev_uc_unsync(vsi->netdev, NULL);
14188 		__dev_mc_unsync(vsi->netdev, NULL);
14189 	}
14190 
14191 	/* make sure any remaining filters are marked for deletion */
14192 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14193 		__i40e_del_filter(vsi, f);
14194 
14195 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14196 
14197 	i40e_sync_vsi_filters(vsi);
14198 
14199 	i40e_vsi_delete(vsi);
14200 	i40e_vsi_free_q_vectors(vsi);
14201 	if (vsi->netdev) {
14202 		free_netdev(vsi->netdev);
14203 		vsi->netdev = NULL;
14204 	}
14205 	i40e_vsi_clear_rings(vsi);
14206 	i40e_vsi_clear(vsi);
14207 
14208 	/* If this was the last thing on the VEB, except for the
14209 	 * controlling VSI, remove the VEB, which puts the controlling
14210 	 * VSI onto the next level down in the switch.
14211 	 *
14212 	 * Well, okay, there's one more exception here: don't remove
14213 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14214 	 * from up the network stack.
14215 	 */
14216 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14217 		if (pf->vsi[i] &&
14218 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14219 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14220 			n++;      /* count the VSIs */
14221 		}
14222 	}
14223 	for (i = 0; i < I40E_MAX_VEB; i++) {
14224 		if (!pf->veb[i])
14225 			continue;
14226 		if (pf->veb[i]->uplink_seid == uplink_seid)
14227 			n++;     /* count the VEBs */
14228 		if (pf->veb[i]->seid == uplink_seid)
14229 			veb = pf->veb[i];
14230 	}
14231 	if (n == 0 && veb && veb->uplink_seid != 0)
14232 		i40e_veb_release(veb);
14233 
14234 	return 0;
14235 }
14236 
14237 /**
14238  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14239  * @vsi: ptr to the VSI
14240  *
14241  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14242  * corresponding SW VSI structure and initializes num_queue_pairs for the
14243  * newly allocated VSI.
14244  *
14245  * Returns 0 on success or negative on failure
14246  **/
14247 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14248 {
14249 	int ret = -ENOENT;
14250 	struct i40e_pf *pf = vsi->back;
14251 
14252 	if (vsi->q_vectors[0]) {
14253 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14254 			 vsi->seid);
14255 		return -EEXIST;
14256 	}
14257 
14258 	if (vsi->base_vector) {
14259 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14260 			 vsi->seid, vsi->base_vector);
14261 		return -EEXIST;
14262 	}
14263 
14264 	ret = i40e_vsi_alloc_q_vectors(vsi);
14265 	if (ret) {
14266 		dev_info(&pf->pdev->dev,
14267 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14268 			 vsi->num_q_vectors, vsi->seid, ret);
14269 		vsi->num_q_vectors = 0;
14270 		goto vector_setup_out;
14271 	}
14272 
14273 	/* In Legacy mode, we do not have to get any other vector since we
14274 	 * piggyback on the misc/ICR0 for queue interrupts.
14275 	*/
14276 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14277 		return ret;
14278 	if (vsi->num_q_vectors)
14279 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14280 						 vsi->num_q_vectors, vsi->idx);
14281 	if (vsi->base_vector < 0) {
14282 		dev_info(&pf->pdev->dev,
14283 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14284 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14285 		i40e_vsi_free_q_vectors(vsi);
14286 		ret = -ENOENT;
14287 		goto vector_setup_out;
14288 	}
14289 
14290 vector_setup_out:
14291 	return ret;
14292 }
14293 
14294 /**
14295  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14296  * @vsi: pointer to the vsi.
14297  *
14298  * This re-allocates a vsi's queue resources.
14299  *
14300  * Returns pointer to the successfully allocated and configured VSI sw struct
14301  * on success, otherwise returns NULL on failure.
14302  **/
14303 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14304 {
14305 	u16 alloc_queue_pairs;
14306 	struct i40e_pf *pf;
14307 	u8 enabled_tc;
14308 	int ret;
14309 
14310 	if (!vsi)
14311 		return NULL;
14312 
14313 	pf = vsi->back;
14314 
14315 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14316 	i40e_vsi_clear_rings(vsi);
14317 
14318 	i40e_vsi_free_arrays(vsi, false);
14319 	i40e_set_num_rings_in_vsi(vsi);
14320 	ret = i40e_vsi_alloc_arrays(vsi, false);
14321 	if (ret)
14322 		goto err_vsi;
14323 
14324 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14325 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14326 
14327 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14328 	if (ret < 0) {
14329 		dev_info(&pf->pdev->dev,
14330 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14331 			 alloc_queue_pairs, vsi->seid, ret);
14332 		goto err_vsi;
14333 	}
14334 	vsi->base_queue = ret;
14335 
14336 	/* Update the FW view of the VSI. Force a reset of TC and queue
14337 	 * layout configurations.
14338 	 */
14339 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14340 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14341 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14342 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14343 	if (vsi->type == I40E_VSI_MAIN)
14344 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14345 
14346 	/* assign it some queues */
14347 	ret = i40e_alloc_rings(vsi);
14348 	if (ret)
14349 		goto err_rings;
14350 
14351 	/* map all of the rings to the q_vectors */
14352 	i40e_vsi_map_rings_to_vectors(vsi);
14353 	return vsi;
14354 
14355 err_rings:
14356 	i40e_vsi_free_q_vectors(vsi);
14357 	if (vsi->netdev_registered) {
14358 		vsi->netdev_registered = false;
14359 		unregister_netdev(vsi->netdev);
14360 		free_netdev(vsi->netdev);
14361 		vsi->netdev = NULL;
14362 	}
14363 	if (vsi->type == I40E_VSI_MAIN)
14364 		i40e_devlink_destroy_port(pf);
14365 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14366 err_vsi:
14367 	i40e_vsi_clear(vsi);
14368 	return NULL;
14369 }
14370 
14371 /**
14372  * i40e_vsi_setup - Set up a VSI by a given type
14373  * @pf: board private structure
14374  * @type: VSI type
14375  * @uplink_seid: the switch element to link to
14376  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14377  *
14378  * This allocates the sw VSI structure and its queue resources, then add a VSI
14379  * to the identified VEB.
14380  *
14381  * Returns pointer to the successfully allocated and configure VSI sw struct on
14382  * success, otherwise returns NULL on failure.
14383  **/
14384 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14385 				u16 uplink_seid, u32 param1)
14386 {
14387 	struct i40e_vsi *vsi = NULL;
14388 	struct i40e_veb *veb = NULL;
14389 	u16 alloc_queue_pairs;
14390 	int ret, i;
14391 	int v_idx;
14392 
14393 	/* The requested uplink_seid must be either
14394 	 *     - the PF's port seid
14395 	 *              no VEB is needed because this is the PF
14396 	 *              or this is a Flow Director special case VSI
14397 	 *     - seid of an existing VEB
14398 	 *     - seid of a VSI that owns an existing VEB
14399 	 *     - seid of a VSI that doesn't own a VEB
14400 	 *              a new VEB is created and the VSI becomes the owner
14401 	 *     - seid of the PF VSI, which is what creates the first VEB
14402 	 *              this is a special case of the previous
14403 	 *
14404 	 * Find which uplink_seid we were given and create a new VEB if needed
14405 	 */
14406 	for (i = 0; i < I40E_MAX_VEB; i++) {
14407 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14408 			veb = pf->veb[i];
14409 			break;
14410 		}
14411 	}
14412 
14413 	if (!veb && uplink_seid != pf->mac_seid) {
14414 
14415 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14416 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14417 				vsi = pf->vsi[i];
14418 				break;
14419 			}
14420 		}
14421 		if (!vsi) {
14422 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14423 				 uplink_seid);
14424 			return NULL;
14425 		}
14426 
14427 		if (vsi->uplink_seid == pf->mac_seid)
14428 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14429 					     vsi->tc_config.enabled_tc);
14430 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14431 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14432 					     vsi->tc_config.enabled_tc);
14433 		if (veb) {
14434 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14435 				dev_info(&vsi->back->pdev->dev,
14436 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14437 				return NULL;
14438 			}
14439 			/* We come up by default in VEPA mode if SRIOV is not
14440 			 * already enabled, in which case we can't force VEPA
14441 			 * mode.
14442 			 */
14443 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14444 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14445 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14446 			}
14447 			i40e_config_bridge_mode(veb);
14448 		}
14449 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14450 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14451 				veb = pf->veb[i];
14452 		}
14453 		if (!veb) {
14454 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14455 			return NULL;
14456 		}
14457 
14458 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14459 		uplink_seid = veb->seid;
14460 	}
14461 
14462 	/* get vsi sw struct */
14463 	v_idx = i40e_vsi_mem_alloc(pf, type);
14464 	if (v_idx < 0)
14465 		goto err_alloc;
14466 	vsi = pf->vsi[v_idx];
14467 	if (!vsi)
14468 		goto err_alloc;
14469 	vsi->type = type;
14470 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14471 
14472 	if (type == I40E_VSI_MAIN)
14473 		pf->lan_vsi = v_idx;
14474 	else if (type == I40E_VSI_SRIOV)
14475 		vsi->vf_id = param1;
14476 	/* assign it some queues */
14477 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14478 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14479 
14480 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14481 	if (ret < 0) {
14482 		dev_info(&pf->pdev->dev,
14483 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14484 			 alloc_queue_pairs, vsi->seid, ret);
14485 		goto err_vsi;
14486 	}
14487 	vsi->base_queue = ret;
14488 
14489 	/* get a VSI from the hardware */
14490 	vsi->uplink_seid = uplink_seid;
14491 	ret = i40e_add_vsi(vsi);
14492 	if (ret)
14493 		goto err_vsi;
14494 
14495 	switch (vsi->type) {
14496 	/* setup the netdev if needed */
14497 	case I40E_VSI_MAIN:
14498 	case I40E_VSI_VMDQ2:
14499 		ret = i40e_config_netdev(vsi);
14500 		if (ret)
14501 			goto err_netdev;
14502 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14503 		if (ret)
14504 			goto err_netdev;
14505 		if (vsi->type == I40E_VSI_MAIN) {
14506 			ret = i40e_devlink_create_port(pf);
14507 			if (ret)
14508 				goto err_netdev;
14509 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14510 		}
14511 		ret = register_netdev(vsi->netdev);
14512 		if (ret)
14513 			goto err_dl_port;
14514 		vsi->netdev_registered = true;
14515 		netif_carrier_off(vsi->netdev);
14516 #ifdef CONFIG_I40E_DCB
14517 		/* Setup DCB netlink interface */
14518 		i40e_dcbnl_setup(vsi);
14519 #endif /* CONFIG_I40E_DCB */
14520 		fallthrough;
14521 	case I40E_VSI_FDIR:
14522 		/* set up vectors and rings if needed */
14523 		ret = i40e_vsi_setup_vectors(vsi);
14524 		if (ret)
14525 			goto err_msix;
14526 
14527 		ret = i40e_alloc_rings(vsi);
14528 		if (ret)
14529 			goto err_rings;
14530 
14531 		/* map all of the rings to the q_vectors */
14532 		i40e_vsi_map_rings_to_vectors(vsi);
14533 
14534 		i40e_vsi_reset_stats(vsi);
14535 		break;
14536 	default:
14537 		/* no netdev or rings for the other VSI types */
14538 		break;
14539 	}
14540 
14541 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14542 	    vsi->type == I40E_VSI_VMDQ2) {
14543 		ret = i40e_vsi_config_rss(vsi);
14544 		if (ret)
14545 			goto err_config;
14546 	}
14547 	return vsi;
14548 
14549 err_config:
14550 	i40e_vsi_clear_rings(vsi);
14551 err_rings:
14552 	i40e_vsi_free_q_vectors(vsi);
14553 err_msix:
14554 	if (vsi->netdev_registered) {
14555 		vsi->netdev_registered = false;
14556 		unregister_netdev(vsi->netdev);
14557 		free_netdev(vsi->netdev);
14558 		vsi->netdev = NULL;
14559 	}
14560 err_dl_port:
14561 	if (vsi->type == I40E_VSI_MAIN)
14562 		i40e_devlink_destroy_port(pf);
14563 err_netdev:
14564 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14565 err_vsi:
14566 	i40e_vsi_clear(vsi);
14567 err_alloc:
14568 	return NULL;
14569 }
14570 
14571 /**
14572  * i40e_veb_get_bw_info - Query VEB BW information
14573  * @veb: the veb to query
14574  *
14575  * Query the Tx scheduler BW configuration data for given VEB
14576  **/
14577 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14578 {
14579 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14580 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14581 	struct i40e_pf *pf = veb->pf;
14582 	struct i40e_hw *hw = &pf->hw;
14583 	u32 tc_bw_max;
14584 	int ret = 0;
14585 	int i;
14586 
14587 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14588 						  &bw_data, NULL);
14589 	if (ret) {
14590 		dev_info(&pf->pdev->dev,
14591 			 "query veb bw config failed, err %pe aq_err %s\n",
14592 			 ERR_PTR(ret),
14593 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14594 		goto out;
14595 	}
14596 
14597 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14598 						   &ets_data, NULL);
14599 	if (ret) {
14600 		dev_info(&pf->pdev->dev,
14601 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14602 			 ERR_PTR(ret),
14603 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14604 		goto out;
14605 	}
14606 
14607 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14608 	veb->bw_max_quanta = ets_data.tc_bw_max;
14609 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14610 	veb->enabled_tc = ets_data.tc_valid_bits;
14611 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14612 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14613 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14614 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14615 		veb->bw_tc_limit_credits[i] =
14616 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14617 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14618 	}
14619 
14620 out:
14621 	return ret;
14622 }
14623 
14624 /**
14625  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14626  * @pf: board private structure
14627  *
14628  * On error: returns error code (negative)
14629  * On success: returns vsi index in PF (positive)
14630  **/
14631 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14632 {
14633 	int ret = -ENOENT;
14634 	struct i40e_veb *veb;
14635 	int i;
14636 
14637 	/* Need to protect the allocation of switch elements at the PF level */
14638 	mutex_lock(&pf->switch_mutex);
14639 
14640 	/* VEB list may be fragmented if VEB creation/destruction has
14641 	 * been happening.  We can afford to do a quick scan to look
14642 	 * for any free slots in the list.
14643 	 *
14644 	 * find next empty veb slot, looping back around if necessary
14645 	 */
14646 	i = 0;
14647 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14648 		i++;
14649 	if (i >= I40E_MAX_VEB) {
14650 		ret = -ENOMEM;
14651 		goto err_alloc_veb;  /* out of VEB slots! */
14652 	}
14653 
14654 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14655 	if (!veb) {
14656 		ret = -ENOMEM;
14657 		goto err_alloc_veb;
14658 	}
14659 	veb->pf = pf;
14660 	veb->idx = i;
14661 	veb->enabled_tc = 1;
14662 
14663 	pf->veb[i] = veb;
14664 	ret = i;
14665 err_alloc_veb:
14666 	mutex_unlock(&pf->switch_mutex);
14667 	return ret;
14668 }
14669 
14670 /**
14671  * i40e_switch_branch_release - Delete a branch of the switch tree
14672  * @branch: where to start deleting
14673  *
14674  * This uses recursion to find the tips of the branch to be
14675  * removed, deleting until we get back to and can delete this VEB.
14676  **/
14677 static void i40e_switch_branch_release(struct i40e_veb *branch)
14678 {
14679 	struct i40e_pf *pf = branch->pf;
14680 	u16 branch_seid = branch->seid;
14681 	u16 veb_idx = branch->idx;
14682 	int i;
14683 
14684 	/* release any VEBs on this VEB - RECURSION */
14685 	for (i = 0; i < I40E_MAX_VEB; i++) {
14686 		if (!pf->veb[i])
14687 			continue;
14688 		if (pf->veb[i]->uplink_seid == branch->seid)
14689 			i40e_switch_branch_release(pf->veb[i]);
14690 	}
14691 
14692 	/* Release the VSIs on this VEB, but not the owner VSI.
14693 	 *
14694 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14695 	 *       the VEB itself, so don't use (*branch) after this loop.
14696 	 */
14697 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14698 		if (!pf->vsi[i])
14699 			continue;
14700 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14701 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14702 			i40e_vsi_release(pf->vsi[i]);
14703 		}
14704 	}
14705 
14706 	/* There's one corner case where the VEB might not have been
14707 	 * removed, so double check it here and remove it if needed.
14708 	 * This case happens if the veb was created from the debugfs
14709 	 * commands and no VSIs were added to it.
14710 	 */
14711 	if (pf->veb[veb_idx])
14712 		i40e_veb_release(pf->veb[veb_idx]);
14713 }
14714 
14715 /**
14716  * i40e_veb_clear - remove veb struct
14717  * @veb: the veb to remove
14718  **/
14719 static void i40e_veb_clear(struct i40e_veb *veb)
14720 {
14721 	if (!veb)
14722 		return;
14723 
14724 	if (veb->pf) {
14725 		struct i40e_pf *pf = veb->pf;
14726 
14727 		mutex_lock(&pf->switch_mutex);
14728 		if (pf->veb[veb->idx] == veb)
14729 			pf->veb[veb->idx] = NULL;
14730 		mutex_unlock(&pf->switch_mutex);
14731 	}
14732 
14733 	kfree(veb);
14734 }
14735 
14736 /**
14737  * i40e_veb_release - Delete a VEB and free its resources
14738  * @veb: the VEB being removed
14739  **/
14740 void i40e_veb_release(struct i40e_veb *veb)
14741 {
14742 	struct i40e_vsi *vsi = NULL;
14743 	struct i40e_pf *pf;
14744 	int i, n = 0;
14745 
14746 	pf = veb->pf;
14747 
14748 	/* find the remaining VSI and check for extras */
14749 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14750 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14751 			n++;
14752 			vsi = pf->vsi[i];
14753 		}
14754 	}
14755 	if (n != 1) {
14756 		dev_info(&pf->pdev->dev,
14757 			 "can't remove VEB %d with %d VSIs left\n",
14758 			 veb->seid, n);
14759 		return;
14760 	}
14761 
14762 	/* move the remaining VSI to uplink veb */
14763 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14764 	if (veb->uplink_seid) {
14765 		vsi->uplink_seid = veb->uplink_seid;
14766 		if (veb->uplink_seid == pf->mac_seid)
14767 			vsi->veb_idx = I40E_NO_VEB;
14768 		else
14769 			vsi->veb_idx = veb->veb_idx;
14770 	} else {
14771 		/* floating VEB */
14772 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14773 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14774 	}
14775 
14776 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14777 	i40e_veb_clear(veb);
14778 }
14779 
14780 /**
14781  * i40e_add_veb - create the VEB in the switch
14782  * @veb: the VEB to be instantiated
14783  * @vsi: the controlling VSI
14784  **/
14785 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14786 {
14787 	struct i40e_pf *pf = veb->pf;
14788 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14789 	int ret;
14790 
14791 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14792 			      veb->enabled_tc, false,
14793 			      &veb->seid, enable_stats, NULL);
14794 
14795 	/* get a VEB from the hardware */
14796 	if (ret) {
14797 		dev_info(&pf->pdev->dev,
14798 			 "couldn't add VEB, err %pe aq_err %s\n",
14799 			 ERR_PTR(ret),
14800 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14801 		return -EPERM;
14802 	}
14803 
14804 	/* get statistics counter */
14805 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14806 					 &veb->stats_idx, NULL, NULL, NULL);
14807 	if (ret) {
14808 		dev_info(&pf->pdev->dev,
14809 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14810 			 ERR_PTR(ret),
14811 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14812 		return -EPERM;
14813 	}
14814 	ret = i40e_veb_get_bw_info(veb);
14815 	if (ret) {
14816 		dev_info(&pf->pdev->dev,
14817 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14818 			 ERR_PTR(ret),
14819 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14820 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14821 		return -ENOENT;
14822 	}
14823 
14824 	vsi->uplink_seid = veb->seid;
14825 	vsi->veb_idx = veb->idx;
14826 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14827 
14828 	return 0;
14829 }
14830 
14831 /**
14832  * i40e_veb_setup - Set up a VEB
14833  * @pf: board private structure
14834  * @flags: VEB setup flags
14835  * @uplink_seid: the switch element to link to
14836  * @vsi_seid: the initial VSI seid
14837  * @enabled_tc: Enabled TC bit-map
14838  *
14839  * This allocates the sw VEB structure and links it into the switch
14840  * It is possible and legal for this to be a duplicate of an already
14841  * existing VEB.  It is also possible for both uplink and vsi seids
14842  * to be zero, in order to create a floating VEB.
14843  *
14844  * Returns pointer to the successfully allocated VEB sw struct on
14845  * success, otherwise returns NULL on failure.
14846  **/
14847 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14848 				u16 uplink_seid, u16 vsi_seid,
14849 				u8 enabled_tc)
14850 {
14851 	struct i40e_veb *veb, *uplink_veb = NULL;
14852 	int vsi_idx, veb_idx;
14853 	int ret;
14854 
14855 	/* if one seid is 0, the other must be 0 to create a floating relay */
14856 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14857 	    (uplink_seid + vsi_seid != 0)) {
14858 		dev_info(&pf->pdev->dev,
14859 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14860 			 uplink_seid, vsi_seid);
14861 		return NULL;
14862 	}
14863 
14864 	/* make sure there is such a vsi and uplink */
14865 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14866 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14867 			break;
14868 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14869 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14870 			 vsi_seid);
14871 		return NULL;
14872 	}
14873 
14874 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14875 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14876 			if (pf->veb[veb_idx] &&
14877 			    pf->veb[veb_idx]->seid == uplink_seid) {
14878 				uplink_veb = pf->veb[veb_idx];
14879 				break;
14880 			}
14881 		}
14882 		if (!uplink_veb) {
14883 			dev_info(&pf->pdev->dev,
14884 				 "uplink seid %d not found\n", uplink_seid);
14885 			return NULL;
14886 		}
14887 	}
14888 
14889 	/* get veb sw struct */
14890 	veb_idx = i40e_veb_mem_alloc(pf);
14891 	if (veb_idx < 0)
14892 		goto err_alloc;
14893 	veb = pf->veb[veb_idx];
14894 	veb->flags = flags;
14895 	veb->uplink_seid = uplink_seid;
14896 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14897 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14898 
14899 	/* create the VEB in the switch */
14900 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14901 	if (ret)
14902 		goto err_veb;
14903 	if (vsi_idx == pf->lan_vsi)
14904 		pf->lan_veb = veb->idx;
14905 
14906 	return veb;
14907 
14908 err_veb:
14909 	i40e_veb_clear(veb);
14910 err_alloc:
14911 	return NULL;
14912 }
14913 
14914 /**
14915  * i40e_setup_pf_switch_element - set PF vars based on switch type
14916  * @pf: board private structure
14917  * @ele: element we are building info from
14918  * @num_reported: total number of elements
14919  * @printconfig: should we print the contents
14920  *
14921  * helper function to assist in extracting a few useful SEID values.
14922  **/
14923 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14924 				struct i40e_aqc_switch_config_element_resp *ele,
14925 				u16 num_reported, bool printconfig)
14926 {
14927 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14928 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14929 	u8 element_type = ele->element_type;
14930 	u16 seid = le16_to_cpu(ele->seid);
14931 
14932 	if (printconfig)
14933 		dev_info(&pf->pdev->dev,
14934 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14935 			 element_type, seid, uplink_seid, downlink_seid);
14936 
14937 	switch (element_type) {
14938 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14939 		pf->mac_seid = seid;
14940 		break;
14941 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14942 		/* Main VEB? */
14943 		if (uplink_seid != pf->mac_seid)
14944 			break;
14945 		if (pf->lan_veb >= I40E_MAX_VEB) {
14946 			int v;
14947 
14948 			/* find existing or else empty VEB */
14949 			for (v = 0; v < I40E_MAX_VEB; v++) {
14950 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14951 					pf->lan_veb = v;
14952 					break;
14953 				}
14954 			}
14955 			if (pf->lan_veb >= I40E_MAX_VEB) {
14956 				v = i40e_veb_mem_alloc(pf);
14957 				if (v < 0)
14958 					break;
14959 				pf->lan_veb = v;
14960 			}
14961 		}
14962 		if (pf->lan_veb >= I40E_MAX_VEB)
14963 			break;
14964 
14965 		pf->veb[pf->lan_veb]->seid = seid;
14966 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14967 		pf->veb[pf->lan_veb]->pf = pf;
14968 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14969 		break;
14970 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14971 		if (num_reported != 1)
14972 			break;
14973 		/* This is immediately after a reset so we can assume this is
14974 		 * the PF's VSI
14975 		 */
14976 		pf->mac_seid = uplink_seid;
14977 		pf->main_vsi_seid = seid;
14978 		if (printconfig)
14979 			dev_info(&pf->pdev->dev,
14980 				 "pf_seid=%d main_vsi_seid=%d\n",
14981 				 downlink_seid, pf->main_vsi_seid);
14982 		break;
14983 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14984 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14985 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14986 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14987 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14988 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14989 		/* ignore these for now */
14990 		break;
14991 	default:
14992 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14993 			 element_type, seid);
14994 		break;
14995 	}
14996 }
14997 
14998 /**
14999  * i40e_fetch_switch_configuration - Get switch config from firmware
15000  * @pf: board private structure
15001  * @printconfig: should we print the contents
15002  *
15003  * Get the current switch configuration from the device and
15004  * extract a few useful SEID values.
15005  **/
15006 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15007 {
15008 	struct i40e_aqc_get_switch_config_resp *sw_config;
15009 	u16 next_seid = 0;
15010 	int ret = 0;
15011 	u8 *aq_buf;
15012 	int i;
15013 
15014 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15015 	if (!aq_buf)
15016 		return -ENOMEM;
15017 
15018 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15019 	do {
15020 		u16 num_reported, num_total;
15021 
15022 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15023 						I40E_AQ_LARGE_BUF,
15024 						&next_seid, NULL);
15025 		if (ret) {
15026 			dev_info(&pf->pdev->dev,
15027 				 "get switch config failed err %d aq_err %s\n",
15028 				 ret,
15029 				 i40e_aq_str(&pf->hw,
15030 					     pf->hw.aq.asq_last_status));
15031 			kfree(aq_buf);
15032 			return -ENOENT;
15033 		}
15034 
15035 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15036 		num_total = le16_to_cpu(sw_config->header.num_total);
15037 
15038 		if (printconfig)
15039 			dev_info(&pf->pdev->dev,
15040 				 "header: %d reported %d total\n",
15041 				 num_reported, num_total);
15042 
15043 		for (i = 0; i < num_reported; i++) {
15044 			struct i40e_aqc_switch_config_element_resp *ele =
15045 				&sw_config->element[i];
15046 
15047 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15048 						     printconfig);
15049 		}
15050 	} while (next_seid != 0);
15051 
15052 	kfree(aq_buf);
15053 	return ret;
15054 }
15055 
15056 /**
15057  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15058  * @pf: board private structure
15059  * @reinit: if the Main VSI needs to re-initialized.
15060  * @lock_acquired: indicates whether or not the lock has been acquired
15061  *
15062  * Returns 0 on success, negative value on failure
15063  **/
15064 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15065 {
15066 	u16 flags = 0;
15067 	int ret;
15068 
15069 	/* find out what's out there already */
15070 	ret = i40e_fetch_switch_configuration(pf, false);
15071 	if (ret) {
15072 		dev_info(&pf->pdev->dev,
15073 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15074 			 ERR_PTR(ret),
15075 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15076 		return ret;
15077 	}
15078 	i40e_pf_reset_stats(pf);
15079 
15080 	/* set the switch config bit for the whole device to
15081 	 * support limited promisc or true promisc
15082 	 * when user requests promisc. The default is limited
15083 	 * promisc.
15084 	*/
15085 
15086 	if ((pf->hw.pf_id == 0) &&
15087 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15088 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15089 		pf->last_sw_conf_flags = flags;
15090 	}
15091 
15092 	if (pf->hw.pf_id == 0) {
15093 		u16 valid_flags;
15094 
15095 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15096 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15097 						NULL);
15098 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15099 			dev_info(&pf->pdev->dev,
15100 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15101 				 ERR_PTR(ret),
15102 				 i40e_aq_str(&pf->hw,
15103 					     pf->hw.aq.asq_last_status));
15104 			/* not a fatal problem, just keep going */
15105 		}
15106 		pf->last_sw_conf_valid_flags = valid_flags;
15107 	}
15108 
15109 	/* first time setup */
15110 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15111 		struct i40e_vsi *vsi = NULL;
15112 		u16 uplink_seid;
15113 
15114 		/* Set up the PF VSI associated with the PF's main VSI
15115 		 * that is already in the HW switch
15116 		 */
15117 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15118 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15119 		else
15120 			uplink_seid = pf->mac_seid;
15121 		if (pf->lan_vsi == I40E_NO_VSI)
15122 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15123 		else if (reinit)
15124 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15125 		if (!vsi) {
15126 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15127 			i40e_cloud_filter_exit(pf);
15128 			i40e_fdir_teardown(pf);
15129 			return -EAGAIN;
15130 		}
15131 	} else {
15132 		/* force a reset of TC and queue layout configurations */
15133 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15134 
15135 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15136 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15137 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15138 	}
15139 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15140 
15141 	i40e_fdir_sb_setup(pf);
15142 
15143 	/* Setup static PF queue filter control settings */
15144 	ret = i40e_setup_pf_filter_control(pf);
15145 	if (ret) {
15146 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15147 			 ret);
15148 		/* Failure here should not stop continuing other steps */
15149 	}
15150 
15151 	/* enable RSS in the HW, even for only one queue, as the stack can use
15152 	 * the hash
15153 	 */
15154 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15155 		i40e_pf_config_rss(pf);
15156 
15157 	/* fill in link information and enable LSE reporting */
15158 	i40e_link_event(pf);
15159 
15160 	i40e_ptp_init(pf);
15161 
15162 	if (!lock_acquired)
15163 		rtnl_lock();
15164 
15165 	/* repopulate tunnel port filters */
15166 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15167 
15168 	if (!lock_acquired)
15169 		rtnl_unlock();
15170 
15171 	return ret;
15172 }
15173 
15174 /**
15175  * i40e_determine_queue_usage - Work out queue distribution
15176  * @pf: board private structure
15177  **/
15178 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15179 {
15180 	int queues_left;
15181 	int q_max;
15182 
15183 	pf->num_lan_qps = 0;
15184 
15185 	/* Find the max queues to be put into basic use.  We'll always be
15186 	 * using TC0, whether or not DCB is running, and TC0 will get the
15187 	 * big RSS set.
15188 	 */
15189 	queues_left = pf->hw.func_caps.num_tx_qp;
15190 
15191 	if ((queues_left == 1) ||
15192 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15193 		/* one qp for PF, no queues for anything else */
15194 		queues_left = 0;
15195 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15196 
15197 		/* make sure all the fancies are disabled */
15198 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15199 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15200 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15201 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15202 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15203 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15204 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15205 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15206 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15207 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15208 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15209 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15210 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15211 		/* one qp for PF */
15212 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15213 		queues_left -= pf->num_lan_qps;
15214 
15215 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15216 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15217 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15218 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15219 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15220 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15221 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15222 	} else {
15223 		/* Not enough queues for all TCs */
15224 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15225 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15226 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15227 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15228 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15229 		}
15230 
15231 		/* limit lan qps to the smaller of qps, cpus or msix */
15232 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15233 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15234 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15235 		pf->num_lan_qps = q_max;
15236 
15237 		queues_left -= pf->num_lan_qps;
15238 	}
15239 
15240 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15241 		if (queues_left > 1) {
15242 			queues_left -= 1; /* save 1 queue for FD */
15243 		} else {
15244 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15245 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15246 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15247 		}
15248 	}
15249 
15250 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15251 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15252 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15253 					(queues_left / pf->num_vf_qps));
15254 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15255 	}
15256 
15257 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15258 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15259 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15260 					  (queues_left / pf->num_vmdq_qps));
15261 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15262 	}
15263 
15264 	pf->queues_left = queues_left;
15265 	dev_dbg(&pf->pdev->dev,
15266 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15267 		pf->hw.func_caps.num_tx_qp,
15268 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15269 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15270 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15271 		queues_left);
15272 }
15273 
15274 /**
15275  * i40e_setup_pf_filter_control - Setup PF static filter control
15276  * @pf: PF to be setup
15277  *
15278  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15279  * settings. If PE/FCoE are enabled then it will also set the per PF
15280  * based filter sizes required for them. It also enables Flow director,
15281  * ethertype and macvlan type filter settings for the pf.
15282  *
15283  * Returns 0 on success, negative on failure
15284  **/
15285 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15286 {
15287 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15288 
15289 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15290 
15291 	/* Flow Director is enabled */
15292 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15293 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15294 		settings->enable_fdir = true;
15295 
15296 	/* Ethtype and MACVLAN filters enabled for PF */
15297 	settings->enable_ethtype = true;
15298 	settings->enable_macvlan = true;
15299 
15300 	if (i40e_set_filter_control(&pf->hw, settings))
15301 		return -ENOENT;
15302 
15303 	return 0;
15304 }
15305 
15306 #define INFO_STRING_LEN 255
15307 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15308 static void i40e_print_features(struct i40e_pf *pf)
15309 {
15310 	struct i40e_hw *hw = &pf->hw;
15311 	char *buf;
15312 	int i;
15313 
15314 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15315 	if (!buf)
15316 		return;
15317 
15318 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15319 #ifdef CONFIG_PCI_IOV
15320 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15321 #endif
15322 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15323 		      pf->hw.func_caps.num_vsis,
15324 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15325 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15326 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15327 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15328 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15329 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15330 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15331 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15332 	}
15333 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15334 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15335 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15336 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15337 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15338 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15339 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15340 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15341 	else
15342 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15343 
15344 	dev_info(&pf->pdev->dev, "%s\n", buf);
15345 	kfree(buf);
15346 	WARN_ON(i > INFO_STRING_LEN);
15347 }
15348 
15349 /**
15350  * i40e_get_platform_mac_addr - get platform-specific MAC address
15351  * @pdev: PCI device information struct
15352  * @pf: board private structure
15353  *
15354  * Look up the MAC address for the device. First we'll try
15355  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15356  * specific fallback. Otherwise, we'll default to the stored value in
15357  * firmware.
15358  **/
15359 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15360 {
15361 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15362 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15363 }
15364 
15365 /**
15366  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15367  * @fec_cfg: FEC option to set in flags
15368  * @flags: ptr to flags in which we set FEC option
15369  **/
15370 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15371 {
15372 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15373 		set_bit(I40E_FLAG_RS_FEC, flags);
15374 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15375 	}
15376 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15377 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15378 		set_bit(I40E_FLAG_RS_FEC, flags);
15379 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15380 	}
15381 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15382 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15383 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15384 		clear_bit(I40E_FLAG_RS_FEC, flags);
15385 	}
15386 	if (fec_cfg == 0) {
15387 		clear_bit(I40E_FLAG_RS_FEC, flags);
15388 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15389 	}
15390 }
15391 
15392 /**
15393  * i40e_check_recovery_mode - check if we are running transition firmware
15394  * @pf: board private structure
15395  *
15396  * Check registers indicating the firmware runs in recovery mode. Sets the
15397  * appropriate driver state.
15398  *
15399  * Returns true if the recovery mode was detected, false otherwise
15400  **/
15401 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15402 {
15403 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15404 
15405 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15406 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15407 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15408 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15409 
15410 		return true;
15411 	}
15412 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15413 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15414 
15415 	return false;
15416 }
15417 
15418 /**
15419  * i40e_pf_loop_reset - perform reset in a loop.
15420  * @pf: board private structure
15421  *
15422  * This function is useful when a NIC is about to enter recovery mode.
15423  * When a NIC's internal data structures are corrupted the NIC's
15424  * firmware is going to enter recovery mode.
15425  * Right after a POR it takes about 7 minutes for firmware to enter
15426  * recovery mode. Until that time a NIC is in some kind of intermediate
15427  * state. After that time period the NIC almost surely enters
15428  * recovery mode. The only way for a driver to detect intermediate
15429  * state is to issue a series of pf-resets and check a return value.
15430  * If a PF reset returns success then the firmware could be in recovery
15431  * mode so the caller of this code needs to check for recovery mode
15432  * if this function returns success. There is a little chance that
15433  * firmware will hang in intermediate state forever.
15434  * Since waiting 7 minutes is quite a lot of time this function waits
15435  * 10 seconds and then gives up by returning an error.
15436  *
15437  * Return 0 on success, negative on failure.
15438  **/
15439 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15440 {
15441 	/* wait max 10 seconds for PF reset to succeed */
15442 	const unsigned long time_end = jiffies + 10 * HZ;
15443 	struct i40e_hw *hw = &pf->hw;
15444 	int ret;
15445 
15446 	ret = i40e_pf_reset(hw);
15447 	while (ret != 0 && time_before(jiffies, time_end)) {
15448 		usleep_range(10000, 20000);
15449 		ret = i40e_pf_reset(hw);
15450 	}
15451 
15452 	if (ret == 0)
15453 		pf->pfr_count++;
15454 	else
15455 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15456 
15457 	return ret;
15458 }
15459 
15460 /**
15461  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15462  * @pf: board private structure
15463  *
15464  * Check FW registers to determine if FW issued unexpected EMP Reset.
15465  * Every time when unexpected EMP Reset occurs the FW increments
15466  * a counter of unexpected EMP Resets. When the counter reaches 10
15467  * the FW should enter the Recovery mode
15468  *
15469  * Returns true if FW issued unexpected EMP Reset
15470  **/
15471 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15472 {
15473 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15474 			   I40E_GL_FWSTS_FWS1B_MASK;
15475 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15476 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15477 }
15478 
15479 /**
15480  * i40e_handle_resets - handle EMP resets and PF resets
15481  * @pf: board private structure
15482  *
15483  * Handle both EMP resets and PF resets and conclude whether there are
15484  * any issues regarding these resets. If there are any issues then
15485  * generate log entry.
15486  *
15487  * Return 0 if NIC is healthy or negative value when there are issues
15488  * with resets
15489  **/
15490 static int i40e_handle_resets(struct i40e_pf *pf)
15491 {
15492 	const int pfr = i40e_pf_loop_reset(pf);
15493 	const bool is_empr = i40e_check_fw_empr(pf);
15494 
15495 	if (is_empr || pfr != 0)
15496 		dev_crit(&pf->pdev->dev, "Entering recovery mode due to repeated FW resets. This may take several minutes. Refer to the Intel(R) Ethernet Adapters and Devices User Guide.\n");
15497 
15498 	return is_empr ? -EIO : pfr;
15499 }
15500 
15501 /**
15502  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15503  * @pf: board private structure
15504  * @hw: ptr to the hardware info
15505  *
15506  * This function does a minimal setup of all subsystems needed for running
15507  * recovery mode.
15508  *
15509  * Returns 0 on success, negative on failure
15510  **/
15511 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15512 {
15513 	struct i40e_vsi *vsi;
15514 	int err;
15515 	int v_idx;
15516 
15517 	pci_set_drvdata(pf->pdev, pf);
15518 	pci_save_state(pf->pdev);
15519 
15520 	/* set up periodic task facility */
15521 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15522 	pf->service_timer_period = HZ;
15523 
15524 	INIT_WORK(&pf->service_task, i40e_service_task);
15525 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15526 
15527 	err = i40e_init_interrupt_scheme(pf);
15528 	if (err)
15529 		goto err_switch_setup;
15530 
15531 	/* The number of VSIs reported by the FW is the minimum guaranteed
15532 	 * to us; HW supports far more and we share the remaining pool with
15533 	 * the other PFs. We allocate space for more than the guarantee with
15534 	 * the understanding that we might not get them all later.
15535 	 */
15536 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15537 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15538 	else
15539 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15540 
15541 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15542 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15543 			  GFP_KERNEL);
15544 	if (!pf->vsi) {
15545 		err = -ENOMEM;
15546 		goto err_switch_setup;
15547 	}
15548 
15549 	/* We allocate one VSI which is needed as absolute minimum
15550 	 * in order to register the netdev
15551 	 */
15552 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15553 	if (v_idx < 0) {
15554 		err = v_idx;
15555 		goto err_switch_setup;
15556 	}
15557 	pf->lan_vsi = v_idx;
15558 	vsi = pf->vsi[v_idx];
15559 	if (!vsi) {
15560 		err = -EFAULT;
15561 		goto err_switch_setup;
15562 	}
15563 	vsi->alloc_queue_pairs = 1;
15564 	err = i40e_config_netdev(vsi);
15565 	if (err)
15566 		goto err_switch_setup;
15567 	err = register_netdev(vsi->netdev);
15568 	if (err)
15569 		goto err_switch_setup;
15570 	vsi->netdev_registered = true;
15571 	i40e_dbg_pf_init(pf);
15572 
15573 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15574 	if (err)
15575 		goto err_switch_setup;
15576 
15577 	/* tell the firmware that we're starting */
15578 	i40e_send_version(pf);
15579 
15580 	/* since everything's happy, start the service_task timer */
15581 	mod_timer(&pf->service_timer,
15582 		  round_jiffies(jiffies + pf->service_timer_period));
15583 
15584 	return 0;
15585 
15586 err_switch_setup:
15587 	i40e_reset_interrupt_capability(pf);
15588 	timer_shutdown_sync(&pf->service_timer);
15589 	i40e_shutdown_adminq(hw);
15590 	iounmap(hw->hw_addr);
15591 	pci_release_mem_regions(pf->pdev);
15592 	pci_disable_device(pf->pdev);
15593 	i40e_free_pf(pf);
15594 
15595 	return err;
15596 }
15597 
15598 /**
15599  * i40e_set_subsystem_device_id - set subsystem device id
15600  * @hw: pointer to the hardware info
15601  *
15602  * Set PCI subsystem device id either from a pci_dev structure or
15603  * a specific FW register.
15604  **/
15605 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15606 {
15607 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15608 
15609 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15610 		pf->pdev->subsystem_device :
15611 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15612 }
15613 
15614 /**
15615  * i40e_probe - Device initialization routine
15616  * @pdev: PCI device information struct
15617  * @ent: entry in i40e_pci_tbl
15618  *
15619  * i40e_probe initializes a PF identified by a pci_dev structure.
15620  * The OS initialization, configuring of the PF private structure,
15621  * and a hardware reset occur.
15622  *
15623  * Returns 0 on success, negative on failure
15624  **/
15625 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15626 {
15627 	struct i40e_aq_get_phy_abilities_resp abilities;
15628 #ifdef CONFIG_I40E_DCB
15629 	enum i40e_get_fw_lldp_status_resp lldp_status;
15630 #endif /* CONFIG_I40E_DCB */
15631 	struct i40e_pf *pf;
15632 	struct i40e_hw *hw;
15633 	u16 wol_nvm_bits;
15634 	char nvm_ver[32];
15635 	u16 link_status;
15636 #ifdef CONFIG_I40E_DCB
15637 	int status;
15638 #endif /* CONFIG_I40E_DCB */
15639 	int err;
15640 	u32 val;
15641 	u32 i;
15642 
15643 	err = pci_enable_device_mem(pdev);
15644 	if (err)
15645 		return err;
15646 
15647 	/* set up for high or low dma */
15648 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15649 	if (err) {
15650 		dev_err(&pdev->dev,
15651 			"DMA configuration failed: 0x%x\n", err);
15652 		goto err_dma;
15653 	}
15654 
15655 	/* set up pci connections */
15656 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15657 	if (err) {
15658 		dev_info(&pdev->dev,
15659 			 "pci_request_selected_regions failed %d\n", err);
15660 		goto err_pci_reg;
15661 	}
15662 
15663 	pci_set_master(pdev);
15664 
15665 	/* Now that we have a PCI connection, we need to do the
15666 	 * low level device setup.  This is primarily setting up
15667 	 * the Admin Queue structures and then querying for the
15668 	 * device's current profile information.
15669 	 */
15670 	pf = i40e_alloc_pf(&pdev->dev);
15671 	if (!pf) {
15672 		err = -ENOMEM;
15673 		goto err_pf_alloc;
15674 	}
15675 	pf->next_vsi = 0;
15676 	pf->pdev = pdev;
15677 	set_bit(__I40E_DOWN, pf->state);
15678 
15679 	hw = &pf->hw;
15680 
15681 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15682 				I40E_MAX_CSR_SPACE);
15683 	/* We believe that the highest register to read is
15684 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15685 	 * is not less than that before mapping to prevent a
15686 	 * kernel panic.
15687 	 */
15688 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15689 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15690 			pf->ioremap_len);
15691 		err = -ENOMEM;
15692 		goto err_ioremap;
15693 	}
15694 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15695 	if (!hw->hw_addr) {
15696 		err = -EIO;
15697 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15698 			 (unsigned int)pci_resource_start(pdev, 0),
15699 			 pf->ioremap_len, err);
15700 		goto err_ioremap;
15701 	}
15702 	hw->vendor_id = pdev->vendor;
15703 	hw->device_id = pdev->device;
15704 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15705 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15706 	i40e_set_subsystem_device_id(hw);
15707 	hw->bus.device = PCI_SLOT(pdev->devfn);
15708 	hw->bus.func = PCI_FUNC(pdev->devfn);
15709 	hw->bus.bus_id = pdev->bus->number;
15710 
15711 	/* Select something other than the 802.1ad ethertype for the
15712 	 * switch to use internally and drop on ingress.
15713 	 */
15714 	hw->switch_tag = 0xffff;
15715 	hw->first_tag = ETH_P_8021AD;
15716 	hw->second_tag = ETH_P_8021Q;
15717 
15718 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15719 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15720 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15721 
15722 	/* set up the locks for the AQ, do this only once in probe
15723 	 * and destroy them only once in remove
15724 	 */
15725 	mutex_init(&hw->aq.asq_mutex);
15726 	mutex_init(&hw->aq.arq_mutex);
15727 
15728 	pf->msg_enable = netif_msg_init(debug,
15729 					NETIF_MSG_DRV |
15730 					NETIF_MSG_PROBE |
15731 					NETIF_MSG_LINK);
15732 	if (debug < -1)
15733 		pf->hw.debug_mask = debug;
15734 
15735 	/* do a special CORER for clearing PXE mode once at init */
15736 	if (hw->revision_id == 0 &&
15737 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15738 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15739 		i40e_flush(hw);
15740 		msleep(200);
15741 		pf->corer_count++;
15742 
15743 		i40e_clear_pxe_mode(hw);
15744 	}
15745 
15746 	/* Reset here to make sure all is clean and to define PF 'n' */
15747 	i40e_clear_hw(hw);
15748 
15749 	err = i40e_set_mac_type(hw);
15750 	if (err) {
15751 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15752 			 err);
15753 		goto err_pf_reset;
15754 	}
15755 
15756 	err = i40e_handle_resets(pf);
15757 	if (err)
15758 		goto err_pf_reset;
15759 
15760 	i40e_check_recovery_mode(pf);
15761 
15762 	if (is_kdump_kernel()) {
15763 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15764 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15765 	} else {
15766 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15767 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15768 	}
15769 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15770 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15771 
15772 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15773 		 "%s-%s:misc",
15774 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15775 
15776 	err = i40e_init_shared_code(hw);
15777 	if (err) {
15778 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15779 			 err);
15780 		goto err_pf_reset;
15781 	}
15782 
15783 	/* set up a default setting for link flow control */
15784 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15785 
15786 	err = i40e_init_adminq(hw);
15787 	if (err) {
15788 		if (err == -EIO)
15789 			dev_info(&pdev->dev,
15790 				 "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",
15791 				 hw->aq.api_maj_ver,
15792 				 hw->aq.api_min_ver,
15793 				 I40E_FW_API_VERSION_MAJOR,
15794 				 I40E_FW_MINOR_VERSION(hw));
15795 		else
15796 			dev_info(&pdev->dev,
15797 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15798 
15799 		goto err_pf_reset;
15800 	}
15801 	i40e_get_oem_version(hw);
15802 	i40e_get_pba_string(hw);
15803 
15804 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15805 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15806 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15807 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15808 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15809 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15810 		 hw->subsystem_device_id);
15811 
15812 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15813 				  I40E_FW_MINOR_VERSION(hw) + 1))
15814 		dev_dbg(&pdev->dev,
15815 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15816 			 hw->aq.api_maj_ver,
15817 			 hw->aq.api_min_ver,
15818 			 I40E_FW_API_VERSION_MAJOR,
15819 			 I40E_FW_MINOR_VERSION(hw));
15820 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15821 		dev_info(&pdev->dev,
15822 			 "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",
15823 			 hw->aq.api_maj_ver,
15824 			 hw->aq.api_min_ver,
15825 			 I40E_FW_API_VERSION_MAJOR,
15826 			 I40E_FW_MINOR_VERSION(hw));
15827 
15828 	i40e_verify_eeprom(pf);
15829 
15830 	/* Rev 0 hardware was never productized */
15831 	if (hw->revision_id < 1)
15832 		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");
15833 
15834 	i40e_clear_pxe_mode(hw);
15835 
15836 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15837 	if (err)
15838 		goto err_adminq_setup;
15839 
15840 	err = i40e_sw_init(pf);
15841 	if (err) {
15842 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15843 		goto err_sw_init;
15844 	}
15845 
15846 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15847 		return i40e_init_recovery_mode(pf, hw);
15848 
15849 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15850 				hw->func_caps.num_rx_qp, 0, 0);
15851 	if (err) {
15852 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15853 		goto err_init_lan_hmc;
15854 	}
15855 
15856 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15857 	if (err) {
15858 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15859 		err = -ENOENT;
15860 		goto err_configure_lan_hmc;
15861 	}
15862 
15863 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15864 	 * Ignore error return codes because if it was already disabled via
15865 	 * hardware settings this will fail
15866 	 */
15867 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15868 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15869 		i40e_aq_stop_lldp(hw, true, false, NULL);
15870 	}
15871 
15872 	/* allow a platform config to override the HW addr */
15873 	i40e_get_platform_mac_addr(pdev, pf);
15874 
15875 	if (!is_valid_ether_addr(hw->mac.addr)) {
15876 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15877 		err = -EIO;
15878 		goto err_mac_addr;
15879 	}
15880 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15881 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15882 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15883 	if (is_valid_ether_addr(hw->mac.port_addr))
15884 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15885 
15886 	i40e_ptp_alloc_pins(pf);
15887 	pci_set_drvdata(pdev, pf);
15888 	pci_save_state(pdev);
15889 
15890 #ifdef CONFIG_I40E_DCB
15891 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15892 	(!status &&
15893 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15894 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15895 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15896 	dev_info(&pdev->dev,
15897 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15898 			"FW LLDP is disabled\n" :
15899 			"FW LLDP is enabled\n");
15900 
15901 	/* Enable FW to write default DCB config on link-up */
15902 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15903 
15904 	err = i40e_init_pf_dcb(pf);
15905 	if (err) {
15906 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15907 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15908 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15909 		/* Continue without DCB enabled */
15910 	}
15911 #endif /* CONFIG_I40E_DCB */
15912 
15913 	/* set up periodic task facility */
15914 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15915 	pf->service_timer_period = HZ;
15916 
15917 	INIT_WORK(&pf->service_task, i40e_service_task);
15918 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15919 
15920 	/* NVM bit on means WoL disabled for the port */
15921 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15922 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15923 		pf->wol_en = false;
15924 	else
15925 		pf->wol_en = true;
15926 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15927 
15928 	/* set up the main switch operations */
15929 	i40e_determine_queue_usage(pf);
15930 	err = i40e_init_interrupt_scheme(pf);
15931 	if (err)
15932 		goto err_switch_setup;
15933 
15934 	/* Reduce Tx and Rx pairs for kdump
15935 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15936 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15937 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15938 	 */
15939 	if (is_kdump_kernel())
15940 		pf->num_lan_msix = 1;
15941 
15942 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15943 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15944 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15945 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15946 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15947 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15948 						    UDP_TUNNEL_TYPE_GENEVE;
15949 
15950 	/* The number of VSIs reported by the FW is the minimum guaranteed
15951 	 * to us; HW supports far more and we share the remaining pool with
15952 	 * the other PFs. We allocate space for more than the guarantee with
15953 	 * the understanding that we might not get them all later.
15954 	 */
15955 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15956 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15957 	else
15958 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15959 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15960 		dev_warn(&pf->pdev->dev,
15961 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15962 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15963 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15964 	}
15965 
15966 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15967 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15968 			  GFP_KERNEL);
15969 	if (!pf->vsi) {
15970 		err = -ENOMEM;
15971 		goto err_switch_setup;
15972 	}
15973 
15974 #ifdef CONFIG_PCI_IOV
15975 	/* prep for VF support */
15976 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15977 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15978 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15979 		if (pci_num_vf(pdev))
15980 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15981 	}
15982 #endif
15983 	err = i40e_setup_pf_switch(pf, false, false);
15984 	if (err) {
15985 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15986 		goto err_vsis;
15987 	}
15988 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15989 
15990 	/* if FDIR VSI was set up, start it now */
15991 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15992 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15993 			i40e_vsi_open(pf->vsi[i]);
15994 			break;
15995 		}
15996 	}
15997 
15998 	/* The driver only wants link up/down and module qualification
15999 	 * reports from firmware.  Note the negative logic.
16000 	 */
16001 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16002 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16003 					 I40E_AQ_EVENT_MEDIA_NA |
16004 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16005 	if (err)
16006 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16007 			 ERR_PTR(err),
16008 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16009 
16010 	/* Reconfigure hardware for allowing smaller MSS in the case
16011 	 * of TSO, so that we avoid the MDD being fired and causing
16012 	 * a reset in the case of small MSS+TSO.
16013 	 */
16014 	val = rd32(hw, I40E_REG_MSS);
16015 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16016 		val &= ~I40E_REG_MSS_MIN_MASK;
16017 		val |= I40E_64BYTE_MSS;
16018 		wr32(hw, I40E_REG_MSS, val);
16019 	}
16020 
16021 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
16022 		msleep(75);
16023 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16024 		if (err)
16025 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16026 				 ERR_PTR(err),
16027 				 i40e_aq_str(&pf->hw,
16028 					     pf->hw.aq.asq_last_status));
16029 	}
16030 	/* The main driver is (mostly) up and happy. We need to set this state
16031 	 * before setting up the misc vector or we get a race and the vector
16032 	 * ends up disabled forever.
16033 	 */
16034 	clear_bit(__I40E_DOWN, pf->state);
16035 
16036 	/* In case of MSIX we are going to setup the misc vector right here
16037 	 * to handle admin queue events etc. In case of legacy and MSI
16038 	 * the misc functionality and queue processing is combined in
16039 	 * the same vector and that gets setup at open.
16040 	 */
16041 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
16042 		err = i40e_setup_misc_vector(pf);
16043 		if (err) {
16044 			dev_info(&pdev->dev,
16045 				 "setup of misc vector failed: %d\n", err);
16046 			i40e_cloud_filter_exit(pf);
16047 			i40e_fdir_teardown(pf);
16048 			goto err_vsis;
16049 		}
16050 	}
16051 
16052 #ifdef CONFIG_PCI_IOV
16053 	/* prep for VF support */
16054 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
16055 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
16056 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16057 		/* disable link interrupts for VFs */
16058 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16059 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16060 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16061 		i40e_flush(hw);
16062 
16063 		if (pci_num_vf(pdev)) {
16064 			dev_info(&pdev->dev,
16065 				 "Active VFs found, allocating resources.\n");
16066 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16067 			if (err)
16068 				dev_info(&pdev->dev,
16069 					 "Error %d allocating resources for existing VFs\n",
16070 					 err);
16071 		}
16072 	}
16073 #endif /* CONFIG_PCI_IOV */
16074 
16075 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16076 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16077 						      pf->num_iwarp_msix,
16078 						      I40E_IWARP_IRQ_PILE_ID);
16079 		if (pf->iwarp_base_vector < 0) {
16080 			dev_info(&pdev->dev,
16081 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16082 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16083 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16084 		}
16085 	}
16086 
16087 	i40e_dbg_pf_init(pf);
16088 
16089 	/* tell the firmware that we're starting */
16090 	i40e_send_version(pf);
16091 
16092 	/* since everything's happy, start the service_task timer */
16093 	mod_timer(&pf->service_timer,
16094 		  round_jiffies(jiffies + pf->service_timer_period));
16095 
16096 	/* add this PF to client device list and launch a client service task */
16097 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16098 		err = i40e_lan_add_device(pf);
16099 		if (err)
16100 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16101 				 err);
16102 	}
16103 
16104 #define PCI_SPEED_SIZE 8
16105 #define PCI_WIDTH_SIZE 8
16106 	/* Devices on the IOSF bus do not have this information
16107 	 * and will report PCI Gen 1 x 1 by default so don't bother
16108 	 * checking them.
16109 	 */
16110 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16111 		char speed[PCI_SPEED_SIZE] = "Unknown";
16112 		char width[PCI_WIDTH_SIZE] = "Unknown";
16113 
16114 		/* Get the negotiated link width and speed from PCI config
16115 		 * space
16116 		 */
16117 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16118 					  &link_status);
16119 
16120 		i40e_set_pci_config_data(hw, link_status);
16121 
16122 		switch (hw->bus.speed) {
16123 		case i40e_bus_speed_8000:
16124 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16125 		case i40e_bus_speed_5000:
16126 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16127 		case i40e_bus_speed_2500:
16128 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16129 		default:
16130 			break;
16131 		}
16132 		switch (hw->bus.width) {
16133 		case i40e_bus_width_pcie_x8:
16134 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16135 		case i40e_bus_width_pcie_x4:
16136 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16137 		case i40e_bus_width_pcie_x2:
16138 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16139 		case i40e_bus_width_pcie_x1:
16140 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16141 		default:
16142 			break;
16143 		}
16144 
16145 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16146 			 speed, width);
16147 
16148 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16149 		    hw->bus.speed < i40e_bus_speed_8000) {
16150 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16151 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16152 		}
16153 	}
16154 
16155 	/* get the requested speeds from the fw */
16156 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16157 	if (err)
16158 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16159 			ERR_PTR(err),
16160 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16161 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16162 
16163 	/* set the FEC config due to the board capabilities */
16164 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16165 
16166 	/* get the supported phy types from the fw */
16167 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16168 	if (err)
16169 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16170 			ERR_PTR(err),
16171 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16172 
16173 	/* make sure the MFS hasn't been set lower than the default */
16174 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16175 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16176 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16177 	if (val < MAX_FRAME_SIZE_DEFAULT)
16178 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16179 			 i, val);
16180 
16181 	/* Add a filter to drop all Flow control frames from any VSI from being
16182 	 * transmitted. By doing so we stop a malicious VF from sending out
16183 	 * PAUSE or PFC frames and potentially controlling traffic for other
16184 	 * PF/VF VSIs.
16185 	 * The FW can still send Flow control frames if enabled.
16186 	 */
16187 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16188 						       pf->main_vsi_seid);
16189 
16190 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16191 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16192 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16193 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16194 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16195 	/* print a string summarizing features */
16196 	i40e_print_features(pf);
16197 
16198 	i40e_devlink_register(pf);
16199 
16200 	return 0;
16201 
16202 	/* Unwind what we've done if something failed in the setup */
16203 err_vsis:
16204 	set_bit(__I40E_DOWN, pf->state);
16205 	i40e_clear_interrupt_scheme(pf);
16206 	kfree(pf->vsi);
16207 err_switch_setup:
16208 	i40e_reset_interrupt_capability(pf);
16209 	timer_shutdown_sync(&pf->service_timer);
16210 err_mac_addr:
16211 err_configure_lan_hmc:
16212 	(void)i40e_shutdown_lan_hmc(hw);
16213 err_init_lan_hmc:
16214 	kfree(pf->qp_pile);
16215 err_sw_init:
16216 err_adminq_setup:
16217 err_pf_reset:
16218 	iounmap(hw->hw_addr);
16219 err_ioremap:
16220 	i40e_free_pf(pf);
16221 err_pf_alloc:
16222 	pci_release_mem_regions(pdev);
16223 err_pci_reg:
16224 err_dma:
16225 	pci_disable_device(pdev);
16226 	return err;
16227 }
16228 
16229 /**
16230  * i40e_remove - Device removal routine
16231  * @pdev: PCI device information struct
16232  *
16233  * i40e_remove is called by the PCI subsystem to alert the driver
16234  * that is should release a PCI device.  This could be caused by a
16235  * Hot-Plug event, or because the driver is going to be removed from
16236  * memory.
16237  **/
16238 static void i40e_remove(struct pci_dev *pdev)
16239 {
16240 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16241 	struct i40e_hw *hw = &pf->hw;
16242 	int ret_code;
16243 	int i;
16244 
16245 	i40e_devlink_unregister(pf);
16246 
16247 	i40e_dbg_pf_exit(pf);
16248 
16249 	i40e_ptp_stop(pf);
16250 
16251 	/* Disable RSS in hw */
16252 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16253 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16254 
16255 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16256 	 * flags, once they are set, i40e_rebuild should not be called as
16257 	 * i40e_prep_for_reset always returns early.
16258 	 */
16259 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16260 		usleep_range(1000, 2000);
16261 	set_bit(__I40E_IN_REMOVE, pf->state);
16262 
16263 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16264 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16265 		i40e_free_vfs(pf);
16266 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16267 	}
16268 	/* no more scheduling of any task */
16269 	set_bit(__I40E_SUSPENDED, pf->state);
16270 	set_bit(__I40E_DOWN, pf->state);
16271 	if (pf->service_timer.function)
16272 		timer_shutdown_sync(&pf->service_timer);
16273 	if (pf->service_task.func)
16274 		cancel_work_sync(&pf->service_task);
16275 
16276 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16277 		struct i40e_vsi *vsi = pf->vsi[0];
16278 
16279 		/* We know that we have allocated only one vsi for this PF,
16280 		 * it was just for registering netdevice, so the interface
16281 		 * could be visible in the 'ifconfig' output
16282 		 */
16283 		unregister_netdev(vsi->netdev);
16284 		free_netdev(vsi->netdev);
16285 
16286 		goto unmap;
16287 	}
16288 
16289 	/* Client close must be called explicitly here because the timer
16290 	 * has been stopped.
16291 	 */
16292 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16293 
16294 	i40e_fdir_teardown(pf);
16295 
16296 	/* If there is a switch structure or any orphans, remove them.
16297 	 * This will leave only the PF's VSI remaining.
16298 	 */
16299 	for (i = 0; i < I40E_MAX_VEB; i++) {
16300 		if (!pf->veb[i])
16301 			continue;
16302 
16303 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16304 		    pf->veb[i]->uplink_seid == 0)
16305 			i40e_switch_branch_release(pf->veb[i]);
16306 	}
16307 
16308 	/* Now we can shutdown the PF's VSIs, just before we kill
16309 	 * adminq and hmc.
16310 	 */
16311 	for (i = pf->num_alloc_vsi; i--;)
16312 		if (pf->vsi[i]) {
16313 			i40e_vsi_close(pf->vsi[i]);
16314 			i40e_vsi_release(pf->vsi[i]);
16315 			pf->vsi[i] = NULL;
16316 		}
16317 
16318 	i40e_cloud_filter_exit(pf);
16319 
16320 	/* remove attached clients */
16321 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16322 		ret_code = i40e_lan_del_device(pf);
16323 		if (ret_code)
16324 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16325 				 ret_code);
16326 	}
16327 
16328 	/* shutdown and destroy the HMC */
16329 	if (hw->hmc.hmc_obj) {
16330 		ret_code = i40e_shutdown_lan_hmc(hw);
16331 		if (ret_code)
16332 			dev_warn(&pdev->dev,
16333 				 "Failed to destroy the HMC resources: %d\n",
16334 				 ret_code);
16335 	}
16336 
16337 unmap:
16338 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16339 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16340 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16341 		free_irq(pf->pdev->irq, pf);
16342 
16343 	/* shutdown the adminq */
16344 	i40e_shutdown_adminq(hw);
16345 
16346 	/* destroy the locks only once, here */
16347 	mutex_destroy(&hw->aq.arq_mutex);
16348 	mutex_destroy(&hw->aq.asq_mutex);
16349 
16350 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16351 	rtnl_lock();
16352 	i40e_clear_interrupt_scheme(pf);
16353 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16354 		if (pf->vsi[i]) {
16355 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16356 				i40e_vsi_clear_rings(pf->vsi[i]);
16357 			i40e_vsi_clear(pf->vsi[i]);
16358 			pf->vsi[i] = NULL;
16359 		}
16360 	}
16361 	rtnl_unlock();
16362 
16363 	for (i = 0; i < I40E_MAX_VEB; i++) {
16364 		kfree(pf->veb[i]);
16365 		pf->veb[i] = NULL;
16366 	}
16367 
16368 	kfree(pf->qp_pile);
16369 	kfree(pf->vsi);
16370 
16371 	iounmap(hw->hw_addr);
16372 	i40e_free_pf(pf);
16373 	pci_release_mem_regions(pdev);
16374 
16375 	pci_disable_device(pdev);
16376 }
16377 
16378 /**
16379  * i40e_pci_error_detected - warning that something funky happened in PCI land
16380  * @pdev: PCI device information struct
16381  * @error: the type of PCI error
16382  *
16383  * Called to warn that something happened and the error handling steps
16384  * are in progress.  Allows the driver to quiesce things, be ready for
16385  * remediation.
16386  **/
16387 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16388 						pci_channel_state_t error)
16389 {
16390 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16391 
16392 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16393 
16394 	if (!pf) {
16395 		dev_info(&pdev->dev,
16396 			 "Cannot recover - error happened during device probe\n");
16397 		return PCI_ERS_RESULT_DISCONNECT;
16398 	}
16399 
16400 	/* shutdown all operations */
16401 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16402 		i40e_prep_for_reset(pf);
16403 
16404 	/* Request a slot reset */
16405 	return PCI_ERS_RESULT_NEED_RESET;
16406 }
16407 
16408 /**
16409  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16410  * @pdev: PCI device information struct
16411  *
16412  * Called to find if the driver can work with the device now that
16413  * the pci slot has been reset.  If a basic connection seems good
16414  * (registers are readable and have sane content) then return a
16415  * happy little PCI_ERS_RESULT_xxx.
16416  **/
16417 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16418 {
16419 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16420 	pci_ers_result_t result;
16421 	u32 reg;
16422 
16423 	dev_dbg(&pdev->dev, "%s\n", __func__);
16424 	if (pci_enable_device_mem(pdev)) {
16425 		dev_info(&pdev->dev,
16426 			 "Cannot re-enable PCI device after reset.\n");
16427 		result = PCI_ERS_RESULT_DISCONNECT;
16428 	} else {
16429 		pci_set_master(pdev);
16430 		pci_restore_state(pdev);
16431 		pci_save_state(pdev);
16432 		pci_wake_from_d3(pdev, false);
16433 
16434 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16435 		if (reg == 0)
16436 			result = PCI_ERS_RESULT_RECOVERED;
16437 		else
16438 			result = PCI_ERS_RESULT_DISCONNECT;
16439 	}
16440 
16441 	return result;
16442 }
16443 
16444 /**
16445  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16446  * @pdev: PCI device information struct
16447  */
16448 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16449 {
16450 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16451 
16452 	i40e_prep_for_reset(pf);
16453 }
16454 
16455 /**
16456  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16457  * @pdev: PCI device information struct
16458  */
16459 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16460 {
16461 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16462 
16463 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16464 		return;
16465 
16466 	i40e_reset_and_rebuild(pf, false, false);
16467 }
16468 
16469 /**
16470  * i40e_pci_error_resume - restart operations after PCI error recovery
16471  * @pdev: PCI device information struct
16472  *
16473  * Called to allow the driver to bring things back up after PCI error
16474  * and/or reset recovery has finished.
16475  **/
16476 static void i40e_pci_error_resume(struct pci_dev *pdev)
16477 {
16478 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16479 
16480 	dev_dbg(&pdev->dev, "%s\n", __func__);
16481 	if (test_bit(__I40E_SUSPENDED, pf->state))
16482 		return;
16483 
16484 	i40e_handle_reset_warning(pf, false);
16485 }
16486 
16487 /**
16488  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16489  * using the mac_address_write admin q function
16490  * @pf: pointer to i40e_pf struct
16491  **/
16492 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16493 {
16494 	struct i40e_hw *hw = &pf->hw;
16495 	u8 mac_addr[6];
16496 	u16 flags = 0;
16497 	int ret;
16498 
16499 	/* Get current MAC address in case it's an LAA */
16500 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16501 		ether_addr_copy(mac_addr,
16502 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16503 	} else {
16504 		dev_err(&pf->pdev->dev,
16505 			"Failed to retrieve MAC address; using default\n");
16506 		ether_addr_copy(mac_addr, hw->mac.addr);
16507 	}
16508 
16509 	/* The FW expects the mac address write cmd to first be called with
16510 	 * one of these flags before calling it again with the multicast
16511 	 * enable flags.
16512 	 */
16513 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16514 
16515 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16516 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16517 
16518 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16519 	if (ret) {
16520 		dev_err(&pf->pdev->dev,
16521 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16522 		return;
16523 	}
16524 
16525 	flags = I40E_AQC_MC_MAG_EN
16526 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16527 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16528 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16529 	if (ret)
16530 		dev_err(&pf->pdev->dev,
16531 			"Failed to enable Multicast Magic Packet wake up\n");
16532 }
16533 
16534 /**
16535  * i40e_shutdown - PCI callback for shutting down
16536  * @pdev: PCI device information struct
16537  **/
16538 static void i40e_shutdown(struct pci_dev *pdev)
16539 {
16540 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16541 	struct i40e_hw *hw = &pf->hw;
16542 
16543 	set_bit(__I40E_SUSPENDED, pf->state);
16544 	set_bit(__I40E_DOWN, pf->state);
16545 
16546 	del_timer_sync(&pf->service_timer);
16547 	cancel_work_sync(&pf->service_task);
16548 	i40e_cloud_filter_exit(pf);
16549 	i40e_fdir_teardown(pf);
16550 
16551 	/* Client close must be called explicitly here because the timer
16552 	 * has been stopped.
16553 	 */
16554 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16555 
16556 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16557 	    pf->wol_en)
16558 		i40e_enable_mc_magic_wake(pf);
16559 
16560 	i40e_prep_for_reset(pf);
16561 
16562 	wr32(hw, I40E_PFPM_APM,
16563 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16564 	wr32(hw, I40E_PFPM_WUFC,
16565 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16566 
16567 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16568 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16569 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16570 		free_irq(pf->pdev->irq, pf);
16571 
16572 	/* Since we're going to destroy queues during the
16573 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16574 	 * whole section
16575 	 */
16576 	rtnl_lock();
16577 	i40e_clear_interrupt_scheme(pf);
16578 	rtnl_unlock();
16579 
16580 	if (system_state == SYSTEM_POWER_OFF) {
16581 		pci_wake_from_d3(pdev, pf->wol_en);
16582 		pci_set_power_state(pdev, PCI_D3hot);
16583 	}
16584 }
16585 
16586 /**
16587  * i40e_suspend - PM callback for moving to D3
16588  * @dev: generic device information structure
16589  **/
16590 static int __maybe_unused i40e_suspend(struct device *dev)
16591 {
16592 	struct i40e_pf *pf = dev_get_drvdata(dev);
16593 	struct i40e_hw *hw = &pf->hw;
16594 
16595 	/* If we're already suspended, then there is nothing to do */
16596 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16597 		return 0;
16598 
16599 	set_bit(__I40E_DOWN, pf->state);
16600 
16601 	/* Ensure service task will not be running */
16602 	del_timer_sync(&pf->service_timer);
16603 	cancel_work_sync(&pf->service_task);
16604 
16605 	/* Client close must be called explicitly here because the timer
16606 	 * has been stopped.
16607 	 */
16608 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16609 
16610 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16611 	    pf->wol_en)
16612 		i40e_enable_mc_magic_wake(pf);
16613 
16614 	/* Since we're going to destroy queues during the
16615 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16616 	 * whole section
16617 	 */
16618 	rtnl_lock();
16619 
16620 	i40e_prep_for_reset(pf);
16621 
16622 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16623 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16624 
16625 	/* Clear the interrupt scheme and release our IRQs so that the system
16626 	 * can safely hibernate even when there are a large number of CPUs.
16627 	 * Otherwise hibernation might fail when mapping all the vectors back
16628 	 * to CPU0.
16629 	 */
16630 	i40e_clear_interrupt_scheme(pf);
16631 
16632 	rtnl_unlock();
16633 
16634 	return 0;
16635 }
16636 
16637 /**
16638  * i40e_resume - PM callback for waking up from D3
16639  * @dev: generic device information structure
16640  **/
16641 static int __maybe_unused i40e_resume(struct device *dev)
16642 {
16643 	struct i40e_pf *pf = dev_get_drvdata(dev);
16644 	int err;
16645 
16646 	/* If we're not suspended, then there is nothing to do */
16647 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16648 		return 0;
16649 
16650 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16651 	 * since we're going to be restoring queues
16652 	 */
16653 	rtnl_lock();
16654 
16655 	/* We cleared the interrupt scheme when we suspended, so we need to
16656 	 * restore it now to resume device functionality.
16657 	 */
16658 	err = i40e_restore_interrupt_scheme(pf);
16659 	if (err) {
16660 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16661 			err);
16662 	}
16663 
16664 	clear_bit(__I40E_DOWN, pf->state);
16665 	i40e_reset_and_rebuild(pf, false, true);
16666 
16667 	rtnl_unlock();
16668 
16669 	/* Clear suspended state last after everything is recovered */
16670 	clear_bit(__I40E_SUSPENDED, pf->state);
16671 
16672 	/* Restart the service task */
16673 	mod_timer(&pf->service_timer,
16674 		  round_jiffies(jiffies + pf->service_timer_period));
16675 
16676 	return 0;
16677 }
16678 
16679 static const struct pci_error_handlers i40e_err_handler = {
16680 	.error_detected = i40e_pci_error_detected,
16681 	.slot_reset = i40e_pci_error_slot_reset,
16682 	.reset_prepare = i40e_pci_error_reset_prepare,
16683 	.reset_done = i40e_pci_error_reset_done,
16684 	.resume = i40e_pci_error_resume,
16685 };
16686 
16687 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16688 
16689 static struct pci_driver i40e_driver = {
16690 	.name     = i40e_driver_name,
16691 	.id_table = i40e_pci_tbl,
16692 	.probe    = i40e_probe,
16693 	.remove   = i40e_remove,
16694 	.driver   = {
16695 		.pm = &i40e_pm_ops,
16696 	},
16697 	.shutdown = i40e_shutdown,
16698 	.err_handler = &i40e_err_handler,
16699 	.sriov_configure = i40e_pci_sriov_configure,
16700 };
16701 
16702 /**
16703  * i40e_init_module - Driver registration routine
16704  *
16705  * i40e_init_module is the first routine called when the driver is
16706  * loaded. All it does is register with the PCI subsystem.
16707  **/
16708 static int __init i40e_init_module(void)
16709 {
16710 	int err;
16711 
16712 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16713 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16714 
16715 	/* There is no need to throttle the number of active tasks because
16716 	 * each device limits its own task using a state bit for scheduling
16717 	 * the service task, and the device tasks do not interfere with each
16718 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16719 	 * since we need to be able to guarantee forward progress even under
16720 	 * memory pressure.
16721 	 */
16722 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16723 	if (!i40e_wq) {
16724 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16725 		return -ENOMEM;
16726 	}
16727 
16728 	i40e_dbg_init();
16729 	err = pci_register_driver(&i40e_driver);
16730 	if (err) {
16731 		destroy_workqueue(i40e_wq);
16732 		i40e_dbg_exit();
16733 		return err;
16734 	}
16735 
16736 	return 0;
16737 }
16738 module_init(i40e_init_module);
16739 
16740 /**
16741  * i40e_exit_module - Driver exit cleanup routine
16742  *
16743  * i40e_exit_module is called just before the driver is removed
16744  * from memory.
16745  **/
16746 static void __exit i40e_exit_module(void)
16747 {
16748 	pci_unregister_driver(&i40e_driver);
16749 	destroy_workqueue(i40e_wq);
16750 	ida_destroy(&i40e_client_ida);
16751 	i40e_dbg_exit();
16752 }
16753 module_exit(i40e_exit_module);
16754